00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifdef USE_BOOST
00010 #else
00011 #ifndef windows_platform
00012 #include <pthread.h>
00013 #endif
00014 #endif
00015
00016 #include "initServer.h"
00017 #include "resource.h"
00018 #include "rsGlobalExtern.h"
00019 #include "rcGlobalExtern.h"
00020 #include "genQuery.h"
00021 #include "rsIcatOpr.h"
00022 #include "miscServerFunct.h"
00023 #include "reGlobalsExtern.h"
00024 #include "reDefines.h"
00025 #include "getRemoteZoneResc.h"
00026 #include "getRescQuota.h"
00027 #include "physPath.h"
00028 #include "eirods_stacktrace.h"
00029 #ifdef HPSS
00030 #include "hpssFileDriver.h"
00031 #endif
00032
00033 #include "eirods_log.h"
00034
00035 static time_t LastBrokenPipeTime = 0;
00036 static int BrokenPipeCnt = 0;
00037
00038 int
00039 resolveHost (rodsHostAddr_t *addr, rodsServerHost_t **rodsServerHost)
00040 {
00041 rodsServerHost_t *tmpRodsServerHost;
00042 char *myHostAddr;
00043 char *myZoneName;
00044
00045
00046
00047 myHostAddr = addr->hostAddr;
00048
00049 if (strlen (myHostAddr) == 0) {
00050 *rodsServerHost = ServerHostHead;
00051 return LOCAL_HOST;
00052 }
00053 if (strlen (addr->zoneName) == 0) {
00054 myZoneName = ZoneInfoHead->zoneName;
00055 } else {
00056 myZoneName = addr->zoneName;
00057 }
00058
00059 tmpRodsServerHost = ServerHostHead;
00060 while (tmpRodsServerHost != NULL) {
00061 hostName_t *tmpName;
00062 zoneInfo_t *serverZoneInfo = (zoneInfo_t *) tmpRodsServerHost->zoneInfo;
00063 if (strcmp (myZoneName, serverZoneInfo->zoneName) == 0) {
00064 tmpName = tmpRodsServerHost->hostName;
00065 while (tmpName != NULL) {
00066 if (strcasecmp (tmpName->name, myHostAddr) == 0) {
00067 *rodsServerHost = tmpRodsServerHost;
00068 return (tmpRodsServerHost->localFlag);
00069 }
00070 tmpName = tmpName->next;
00071 }
00072 }
00073 tmpRodsServerHost = tmpRodsServerHost->next;
00074 }
00075
00076
00077
00078 tmpRodsServerHost = mkServerHost (myHostAddr, myZoneName);
00079
00080 if (tmpRodsServerHost == NULL) {
00081 rodsLog (LOG_ERROR,
00082 "resolveHost: mkServerHost error");
00083 return (SYS_INVALID_SERVER_HOST);
00084 }
00085
00086
00087 if (tmpRodsServerHost->localFlag == UNKNOWN_HOST_LOC) {
00088 tmpRodsServerHost->localFlag = REMOTE_HOST;
00089 }
00090
00091 int status = queRodsServerHost (&ServerHostHead, tmpRodsServerHost);
00092 *rodsServerHost = tmpRodsServerHost;
00093
00094 return (tmpRodsServerHost->localFlag);
00095 }
00096 #if 0 // JMC - UNUSED
00097 int
00098 resolveHostByDataObjInfo (dataObjInfo_t *dataObjInfo,
00099 rodsServerHost_t **rodsServerHost)
00100 {
00101 rodsHostAddr_t addr;
00102 int remoteFlag;
00103
00104 if (dataObjInfo == NULL || dataObjInfo->rescInfo == NULL ||
00105 dataObjInfo->rescInfo->rescLoc == NULL) {
00106 rodsLog (LOG_NOTICE,
00107 "resolveHostByDataObjInfo: NULL input");
00108 return (SYS_INTERNAL_NULL_INPUT_ERR);
00109 }
00110
00111 memset (&addr, 0, sizeof (addr));
00112
00113
00114 remoteFlag = resolveHost (&addr, rodsServerHost);
00115
00116 return (remoteFlag);
00117 }
00118 #endif // JMC - UNUSED
00119 int
00120 resolveHostByRescInfo (rescInfo_t *rescInfo, rodsServerHost_t **rodsServerHost)
00121 {
00122 rodsHostAddr_t addr;
00123 int remoteFlag;
00124
00125 if (rescInfo == NULL || rescInfo->rescLoc == NULL) {
00126 rodsLog (LOG_NOTICE,
00127 "resolveHostByRescInfo: NULL input");
00128 return (SYS_INTERNAL_NULL_INPUT_ERR);
00129 }
00130
00131 memset (&addr, 0, sizeof (addr));
00132 rstrcpy (addr.hostAddr, rescInfo->rescLoc, NAME_LEN);
00133
00134 remoteFlag = resolveHost (&addr, rodsServerHost);
00135
00136 return (remoteFlag);
00137 }
00138
00139 rodsServerHost_t *
00140 mkServerHost (char *myHostAddr, char *zoneName)
00141 {
00142 rodsServerHost_t *tmpRodsServerHost;
00143 int status;
00144
00145 tmpRodsServerHost = (rodsServerHost_t*)malloc (sizeof (rodsServerHost_t));
00146 memset (tmpRodsServerHost, 0, sizeof (rodsServerHost_t));
00147
00148 #if 0
00149 if (portNum > 0) {
00150 tmpRodsServerHost->portNum = portNum;
00151 } else {
00152 tmpRodsServerHost->portNum = ServerHostHead->portNum;
00153 }
00154 #endif
00155
00156 status = queHostName (tmpRodsServerHost, myHostAddr, 0);
00157 if (status < 0) {
00158 free (tmpRodsServerHost);
00159 return (NULL);
00160 }
00161
00162 tmpRodsServerHost->localFlag = UNKNOWN_HOST_LOC;
00163
00164 status = queAddr (tmpRodsServerHost, myHostAddr);
00165
00166 status = matchHostConfig (tmpRodsServerHost);
00167
00168 status = getZoneInfo (zoneName,
00169 (zoneInfo_t **) &tmpRodsServerHost->zoneInfo);
00170
00171 if (status < 0) {
00172 free (tmpRodsServerHost);
00173 return (NULL);
00174 } else {
00175 return (tmpRodsServerHost);
00176 }
00177 }
00178
00179 int
00180 initServerInfo (rsComm_t *rsComm)
00181 {
00182 int status;
00183
00184
00185
00186 queZone (rsComm->myEnv.rodsZone, rsComm->myEnv.rodsPort, NULL, NULL);
00187
00188 status = initHostConfigByFile (rsComm);
00189 if (status < 0) {
00190 rodsLog (LOG_NOTICE,
00191 "initServerInfo: initHostConfigByFile error, status = %d",
00192 status);
00193 return (status);
00194 }
00195
00196 status = initLocalServerHost (rsComm);
00197 if (status < 0) {
00198 rodsLog (LOG_NOTICE,
00199 "initServerInfo: initLocalServerHost error, status = %d",
00200 status);
00201 return (status);
00202 }
00203 status = initRcatServerHostByFile (rsComm);
00204 if (status < 0) {
00205 rodsLog (LOG_SYS_FATAL,
00206 "initServerInfo: initRcatServerHostByFile error, status = %d",
00207 status);
00208 return (status);
00209 }
00210
00211 #ifdef RODS_CAT
00212 status = connectRcat (rsComm);
00213 if (status < 0) {
00214 return (status);
00215 }
00216 #endif
00217 status = initZone (rsComm);
00218 if (status < 0) {
00219 rodsLog (LOG_SYS_FATAL,
00220 "initServerInfo: initZone error, status = %d",
00221 status);
00222 return (status);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 eirods::error ret = resc_mgr.init_from_catalog( rsComm );
00240 if( !ret.ok() ) {
00241 eirods::error log_err = PASSMSG( "init_from_catalog failed", ret );
00242 eirods::log( log_err );
00243 }
00244
00245 return (status);
00246 }
00247
00248 int
00249 initLocalServerHost (rsComm_t *rsComm)
00250 {
00251 int status;
00252 char myHostName[MAX_NAME_LEN];
00253
00254 LocalServerHost = ServerHostHead = (rodsServerHost_t*)malloc (sizeof (rodsServerHost_t));
00255 memset (ServerHostHead, 0, sizeof (rodsServerHost_t));
00256
00257 LocalServerHost->localFlag = LOCAL_HOST;
00258 LocalServerHost->zoneInfo = ZoneInfoHead;
00259
00260 status = matchHostConfig (LocalServerHost);
00261
00262 queHostName (ServerHostHead, "localhost", 0);
00263 status = gethostname (myHostName, MAX_NAME_LEN);
00264 if (status < 0) {
00265 status = SYS_GET_HOSTNAME_ERR - errno;
00266 rodsLog (LOG_NOTICE,
00267 "initLocalServerHost: gethostname error, status = %d",
00268 status);
00269 return (status);
00270 }
00271 status = queHostName (ServerHostHead, myHostName, 0);
00272 if (status < 0) {
00273 return (status);
00274 }
00275
00276 status = queAddr (ServerHostHead, myHostName);
00277 if (status < 0) {
00278
00279
00280 rodsLog (LOG_NOTICE,
00281 "initLocalServerHost: queAddr error, status = %d",
00282 status);
00283 status = 0;
00284 }
00285
00286 #if 0
00287 if (myEnv != NULL) {
00288
00289 ZoneInfoHead->portNum = myEnv->rodsPort;
00290 }
00291 #endif
00292
00293 if (ProcessType == SERVER_PT) {
00294 printServerHost (LocalServerHost);
00295 }
00296
00297 return (status);
00298 }
00299
00300 int
00301 printServerHost (rodsServerHost_t *myServerHost)
00302 {
00303 hostName_t *tmpHostName;
00304
00305 if (myServerHost->localFlag == LOCAL_HOST) {
00306 #ifndef windows_platform
00307 #ifdef IRODS_SYSLOG
00308 rodsLog (LOG_NOTICE," LocalHostName: ");
00309 #else
00310 fprintf (stderr, " LocalHostName: ");
00311 #endif
00312 #else
00313 rodsLog (LOG_NOTICE," LocalHostName: ");
00314 #endif
00315 } else {
00316 #ifndef windows_platform
00317 #ifdef IRODS_SYSLOG
00318 rodsLog (LOG_NOTICE," RemoteHostName: ");
00319 #else
00320 fprintf (stderr, " RemoteHostName: ");
00321 #endif
00322 #else
00323 rodsLog (LOG_NOTICE," RemoteHostName: ");
00324 #endif
00325 }
00326
00327 tmpHostName = myServerHost->hostName;
00328
00329 while (tmpHostName != NULL) {
00330 #ifndef windows_platform
00331 #ifdef IRODS_SYSLOG
00332 rodsLog (LOG_NOTICE," %s,", tmpHostName->name);
00333 #else
00334 fprintf (stderr, " %s,", tmpHostName->name);
00335 #endif
00336 #else
00337 rodsLog (LOG_NOTICE," %s,", tmpHostName->name);
00338 #endif
00339 tmpHostName = tmpHostName->next;
00340 }
00341
00342 #ifndef windows_platform
00343 #ifdef IRODS_SYSLOG
00344 rodsLog (LOG_NOTICE," Port Num: %d.\n\n", ((zoneInfo_t *)myServerHost->zoneInfo)->portNum);
00345 #else
00346 fprintf (stderr, " Port Num: %d.\n\n",
00347 ((zoneInfo_t *)myServerHost->zoneInfo)->portNum);
00348 #endif
00349 #else
00350 rodsLog (LOG_NOTICE," Port Num: %d.\n\n", ((zoneInfo_t *)myServerHost->zoneInfo)->portNum);
00351 #endif
00352
00353 return (0);
00354 }
00355
00356 int
00357 printZoneInfo ()
00358 {
00359 zoneInfo_t *tmpZoneInfo;
00360 rodsServerHost_t *tmpRodsServerHost;
00361
00362 tmpZoneInfo = ZoneInfoHead;
00363 #ifndef windows_platform
00364 #ifdef IRODS_SYSLOG
00365 rodsLog (LOG_NOTICE,"Zone Info:\n");
00366 #else
00367 fprintf (stderr, "Zone Info:\n");
00368 #endif
00369 #else
00370 rodsLog (LOG_NOTICE,"Zone Info:\n");
00371 #endif
00372 while (tmpZoneInfo != NULL) {
00373
00374 tmpRodsServerHost = (rodsServerHost_t *) tmpZoneInfo->masterServerHost;
00375 #ifndef windows_platform
00376 #ifdef IRODS_SYSLOG
00377 rodsLog (LOG_NOTICE," ZoneName: %s ", tmpZoneInfo->zoneName);
00378 #else
00379 fprintf (stderr, " ZoneName: %s ", tmpZoneInfo->zoneName);
00380 #endif
00381 #else
00382 rodsLog (LOG_NOTICE," ZoneName: %s ", tmpZoneInfo->zoneName);
00383 #endif
00384 if (tmpRodsServerHost->rcatEnabled == LOCAL_ICAT) {
00385 #ifndef windows_platform
00386 #ifdef IRODS_SYSLOG
00387 rodsLog (LOG_NOTICE,"Type: LOCAL_ICAT ");
00388 #else
00389 fprintf (stderr, "Type: LOCAL_ICAT ");
00390 #endif
00391 #else
00392 rodsLog (LOG_NOTICE,"Type: LOCAL_ICAT ");
00393 #endif
00394 } else {
00395 #ifndef windows_platform
00396 #ifdef IRODS_SYSLOG
00397 rodsLog (LOG_NOTICE,"Type: REMOTE_ICAT ");
00398 #else
00399 fprintf (stderr, "Type: REMOTE_ICAT ");
00400 #endif
00401 #else
00402 rodsLog (LOG_NOTICE,"Type: REMOTE_ICAT ");
00403 #endif
00404 }
00405
00406 #ifndef windows_platform
00407 #ifdef IRODS_SYSLOG
00408 rodsLog (LOG_NOTICE, " HostAddr: %s PortNum: %d\n\n",
00409 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00410 #else
00411 fprintf (stderr, " HostAddr: %s PortNum: %d\n\n",
00412 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00413 #endif
00414 #else
00415 rodsLog (LOG_NOTICE, " HostAddr: %s PortNum: %d\n\n",
00416 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00417 #endif
00418
00419
00420 tmpRodsServerHost = (rodsServerHost_t *) tmpZoneInfo->slaveServerHost;
00421 if (tmpRodsServerHost != NULL) {
00422 #ifndef windows_platform
00423 #ifdef IRODS_SYSLOG
00424 rodsLog (LOG_NOTICE, " ZoneName: %s ", tmpZoneInfo->zoneName);
00425 rodsLog (LOG_NOTICE, "Type: LOCAL_SLAVE_ICAT ");
00426 rodsLog (LOG_NOTICE, " HostAddr: %s PortNum: %d\n\n",
00427 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00428 #else
00429 fprintf (stderr, " ZoneName: %s ", tmpZoneInfo->zoneName);
00430 fprintf (stderr, "Type: LOCAL_SLAVE_ICAT ");
00431 fprintf (stderr, " HostAddr: %s PortNum: %d\nn",
00432 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00433 #endif
00434 #else
00435 rodsLog (LOG_NOTICE, " ZoneName: %s ", tmpZoneInfo->zoneName);
00436 rodsLog (LOG_NOTICE, "Type: LOCAL_SLAVE_ICAT ");
00437 rodsLog (LOG_NOTICE, " HostAddr: %s PortNum: %d\n\n",
00438 tmpRodsServerHost->hostName->name, tmpZoneInfo->portNum);
00439 #endif
00440 }
00441
00442 tmpZoneInfo = tmpZoneInfo->next;
00443 }
00444
00445 if (getReHost (&tmpRodsServerHost) >= 0) {
00446 #ifndef windows_platform
00447 #ifdef IRODS_SYSLOG
00448 rodsLog (LOG_NOTICE,"reHost: %s", tmpRodsServerHost->hostName->name);
00449 #else
00450 fprintf (stderr, "reHost: %s\n\n", tmpRodsServerHost->hostName->name);
00451 #endif
00452 #else
00453 rodsLog (LOG_NOTICE,"reHost: %s", tmpRodsServerHost->hostName->name);
00454 #endif
00455 } else {
00456 #ifndef windows_platform
00457 #ifdef IRODS_SYSLOG
00458 rodsLog (LOG_ERROR,"reHost error");
00459 #else
00460 fprintf (stderr,"reHost error");
00461 #endif
00462 #else
00463 rodsLog (LOG_ERROR,"reHost error");
00464 #endif
00465 }
00466
00467 if (getXmsgHost (&tmpRodsServerHost) >= 0) {
00468 #ifndef windows_platform
00469 #ifdef IRODS_SYSLOG
00470 rodsLog (LOG_NOTICE,"xmsgHost: %s", tmpRodsServerHost->hostName->name);
00471 #else
00472 fprintf (stderr, "xmsgHost: %s\n\n",tmpRodsServerHost->hostName->name);
00473 #endif
00474 #else
00475 rodsLog (LOG_NOTICE,"xmsgHost: %s", tmpRodsServerHost->hostName->name);
00476 #endif
00477 }
00478
00479 return (0);
00480 }
00481
00482 int
00483 initRcatServerHostByFile (rsComm_t *rsComm)
00484 {
00485 FILE *fptr;
00486 char *rcatCongFile;
00487 char inbuf[MAX_NAME_LEN];
00488 rodsHostAddr_t addr;
00489 rodsServerHost_t *tmpRodsServerHost;
00490 int lineLen, bytesCopied, remoteFlag;
00491 char keyWdName[MAX_NAME_LEN];
00492 int gptRcatFlag = 0;
00493 int remoteSidCount = 0;
00494 char sidKey[MAX_PASSWORD_LEN]="";
00495 int i;
00496
00497 localSID[0]='\0';
00498 for (i=0;i<MAX_FED_RSIDS;i++) {
00499 remoteSID[i][0]='\0';
00500 }
00501
00502 rcatCongFile = (char *) malloc((strlen (getConfigDir()) +
00503 strlen(RCAT_HOST_FILE) + 24));
00504
00505 #ifndef windows_platform
00506 sprintf (rcatCongFile, "%-s/%-s", getConfigDir(), RCAT_HOST_FILE);
00507 fptr = fopen (rcatCongFile, "r");
00508 #else
00509 sprintf(rcatCongFile, "%s\\%s", getConfigDir(),RCAT_HOST_FILE);
00510 fptr = iRODSNt_fopen(rcatCongFile, "r");
00511 #endif
00512
00513 if (fptr == NULL) {
00514 rodsLog (LOG_SYS_FATAL,
00515 "Cannot open RCAT_HOST_FILE file %s. ernro = %d\n",
00516 rcatCongFile, errno);
00517 free (rcatCongFile);
00518 return (SYS_CONFIG_FILE_ERR);
00519 }
00520
00521 free (rcatCongFile);
00522
00523 memset (&addr, 0, sizeof (addr));
00524 while ((lineLen = getLine (fptr, inbuf, MAX_NAME_LEN)) > 0) {
00525 char *inPtr = inbuf;
00526 if ((bytesCopied = getStrInBuf (&inPtr, keyWdName,
00527 &lineLen, LONG_NAME_LEN)) > 0) {
00528
00529 if (strcmp (keyWdName, RE_RULESET_KW) == 0) {
00530 if ((bytesCopied = getStrInBuf (&inPtr, reRuleStr,
00531 &lineLen, LONG_NAME_LEN)) < 0) {
00532 rodsLog (LOG_SYS_FATAL,
00533 "initRcatServerHostByFile: parsing error for keywd %s",
00534 keyWdName);
00535 return (SYS_CONFIG_FILE_ERR);
00536 }
00537 } else if (strcmp (keyWdName, RE_FUNCMAPSET_KW) == 0) {
00538 if ((bytesCopied = getStrInBuf (&inPtr, reFuncMapStr,
00539 &lineLen, LONG_NAME_LEN)) < 0) {
00540 rodsLog (LOG_SYS_FATAL,
00541 "initRcatServerHostByFile: parsing error for keywd %s",
00542 keyWdName);
00543 return (SYS_CONFIG_FILE_ERR);
00544 }
00545 } else if (strcmp (keyWdName, RE_VARIABLEMAPSET_KW) == 0) {
00546 if ((bytesCopied = getStrInBuf (&inPtr, reVariableMapStr,
00547 &lineLen, LONG_NAME_LEN)) < 0) {
00548 rodsLog (LOG_SYS_FATAL,
00549 "initRcatServerHostByFile: parsing error for keywd %s",
00550 keyWdName);
00551 return (SYS_CONFIG_FILE_ERR);
00552 }
00553 } else if (strcmp (keyWdName, KERBEROS_NAME_KW) == 0) {
00554 if ((bytesCopied = getStrInBuf (&inPtr, KerberosName,
00555 &lineLen, LONG_NAME_LEN)) < 0) {
00556 rodsLog (LOG_SYS_FATAL,
00557 "initRcatServerHostByFile: parsing error for keywd %s",
00558 keyWdName);
00559 return (SYS_CONFIG_FILE_ERR);
00560 }
00561 } else if (strcmp (keyWdName, ICAT_HOST_KW) == 0) {
00562 if ((bytesCopied = getStrInBuf (&inPtr, addr.hostAddr,
00563 &lineLen, LONG_NAME_LEN)) > 0) {
00564 remoteFlag = resolveHost (&addr, &tmpRodsServerHost);
00565 if (remoteFlag < 0) {
00566 rodsLog (LOG_SYS_FATAL,
00567 "initRcatServerHostByFile: resolveHost error for %s, status = %d",
00568 addr.hostAddr, remoteFlag);
00569 return (remoteFlag);
00570 }
00571 tmpRodsServerHost->rcatEnabled = LOCAL_ICAT;
00572 gptRcatFlag = 1;
00573 } else {
00574 rodsLog (LOG_SYS_FATAL,
00575 "initRcatServerHostByFile: parsing error for keywd %s",
00576 keyWdName);
00577 return (SYS_CONFIG_FILE_ERR);
00578 }
00579 } else if (strcmp (keyWdName, RE_HOST_KW) == 0) {
00580 if ((bytesCopied = getStrInBuf (&inPtr, addr.hostAddr,
00581 &lineLen, LONG_NAME_LEN)) > 0) {
00582 remoteFlag = resolveHost (&addr, &tmpRodsServerHost);
00583 if (remoteFlag < 0) {
00584 rodsLog (LOG_SYS_FATAL,
00585 "initRcatServerHostByFile: resolveHost error for %s, status = %d",
00586 addr.hostAddr, remoteFlag);
00587 return (remoteFlag);
00588 }
00589 tmpRodsServerHost->reHostFlag = 1;
00590 } else {
00591 rodsLog (LOG_SYS_FATAL,
00592 "initRcatServerHostByFile: parsing error for keywd %s",
00593 keyWdName);
00594 return (SYS_CONFIG_FILE_ERR);
00595 }
00596 } else if (strcmp (keyWdName, XMSG_HOST_KW) == 0) {
00597 if ((bytesCopied = getStrInBuf (&inPtr, addr.hostAddr,
00598 &lineLen, LONG_NAME_LEN)) > 0) {
00599 remoteFlag = resolveHost (&addr, &tmpRodsServerHost);
00600 if (remoteFlag < 0) {
00601 rodsLog (LOG_SYS_FATAL,
00602 "initRcatServerHostByFile: resolveHost error for %s, status = %d",
00603 addr.hostAddr, remoteFlag);
00604 return (remoteFlag);
00605 }
00606 tmpRodsServerHost->xmsgHostFlag = 1;
00607 } else {
00608 rodsLog (LOG_SYS_FATAL,
00609 "initRcatServerHostByFile: parsing error for keywd %s",
00610 keyWdName);
00611 return (SYS_CONFIG_FILE_ERR);
00612 }
00613 } else if (strcmp (keyWdName, SLAVE_ICAT_HOST_KW) == 0) {
00614 if ((bytesCopied = getStrInBuf (&inPtr, addr.hostAddr,
00615 &lineLen, LONG_NAME_LEN)) > 0) {
00616 remoteFlag = resolveHost (&addr, &tmpRodsServerHost);
00617 if (remoteFlag < 0) {
00618 rodsLog (LOG_SYS_FATAL,
00619 "initRcatServerHostByFile: resolveHost error for %s, status = %d",
00620 addr.hostAddr, remoteFlag);
00621 return (remoteFlag);
00622 }
00623 tmpRodsServerHost->rcatEnabled = LOCAL_SLAVE_ICAT;
00624 } else {
00625 rodsLog (LOG_SYS_FATAL,
00626 "initRcatServerHostByFile: parsing error for keywd %s",
00627 keyWdName);
00628 return (SYS_CONFIG_FILE_ERR);
00629 }
00630 } else if (strcmp(keyWdName, LOCAL_ZONE_SID_KW) == 0) {
00631 getStrInBuf(&inPtr, localSID, &lineLen, MAX_PASSWORD_LEN);
00632 } else if (strcmp(keyWdName, REMOTE_ZONE_SID_KW) == 0) {
00633 if (remoteSidCount < MAX_FED_RSIDS) {
00634 getStrInBuf(&inPtr, remoteSID[remoteSidCount],
00635 &lineLen, MAX_PASSWORD_LEN);
00636 remoteSidCount++;
00637 }
00638 } else if (strcmp(keyWdName, SID_KEY_KW) == 0) {
00639 getStrInBuf(&inPtr, sidKey, &lineLen, MAX_PASSWORD_LEN);
00640 }
00641 }
00642 }
00643 fclose (fptr);
00644
00645
00646 if (strlen(sidKey) > 0) {
00647 char SID[MAX_PASSWORD_LEN+10];
00648 int i;
00649 if (strlen(localSID) > 0) {
00650 strncpy(SID, localSID, MAX_PASSWORD_LEN);
00651 obfDecodeByKey(SID, sidKey, localSID);
00652 }
00653 for (i=0;i<MAX_FED_RSIDS;i++) {
00654 if (strlen(remoteSID[i]) > 0) {
00655 strncpy(SID, remoteSID[i], MAX_PASSWORD_LEN);
00656 obfDecodeByKey(SID,
00657 sidKey,
00658 remoteSID[i]);
00659 }
00660 else {
00661 break;
00662 }
00663 }
00664 }
00665
00666 if (gptRcatFlag <= 0) {
00667 rodsLog (LOG_SYS_FATAL,
00668 "initRcatServerHostByFile: icatHost entry missing in %s.\n",
00669 RCAT_HOST_FILE);
00670 return (SYS_CONFIG_FILE_ERR);
00671 }
00672
00673 return (0);
00674 }
00675
00676 int
00677 queAddr (rodsServerHost_t *rodsServerHost, char *myHostName)
00678 {
00679 struct hostent *hostEnt;
00680 time_t beforeTime, afterTime;
00681 int status;
00682
00683 if (rodsServerHost == NULL || myHostName == NULL) {
00684 return (0);
00685 }
00686
00687
00688
00689
00690 if( eirods::EMPTY_RESC_HOST != myHostName ) {
00691 beforeTime = time (0);
00692 if ((hostEnt = gethostbyname (myHostName)) == NULL) {
00693 status = SYS_GET_HOSTNAME_ERR - errno;
00694 if (ProcessType == SERVER_PT) {
00695 rodsLog (LOG_NOTICE,
00696 "queAddr: gethostbyname error for %s ,errno = %d\n",
00697 myHostName, errno);
00698 }
00699 return (status);
00700 }
00701 afterTime = time (0);
00702 if (afterTime - beforeTime >= 2) {
00703 rodsLog (LOG_NOTICE,
00704 "WARNING WARNING: gethostbyname of %s is taking %d sec. This could severely affect interactivity of your Rods system",
00705 myHostName, afterTime - beforeTime);
00706
00707 }
00708 }
00709
00710 if (strcasecmp (myHostName, hostEnt->h_name) != 0) {
00711 queHostName (rodsServerHost, hostEnt->h_name, 0);
00712 }
00713 return (0);
00714 }
00715
00716 int
00717 queHostName (rodsServerHost_t *rodsServerHost, char *myName, int topFlag)
00718 {
00719 hostName_t *myHostName, *lastHostName;
00720 hostName_t *tmpHostName;
00721
00722 myHostName = lastHostName = rodsServerHost->hostName;
00723
00724 while (myHostName != NULL) {
00725 if (strcmp (myName, myHostName->name) == 0) {
00726 return (0);
00727 }
00728 lastHostName = myHostName;
00729 myHostName = myHostName->next;
00730 }
00731
00732 tmpHostName = (hostName_t*)malloc (sizeof (hostName_t));
00733 tmpHostName->name = strdup (myName);
00734
00735 if (topFlag > 0) {
00736 tmpHostName->next = rodsServerHost->hostName;
00737 rodsServerHost->hostName = tmpHostName;
00738 } else {
00739 if (lastHostName == NULL) {
00740 rodsServerHost->hostName = tmpHostName;
00741 } else {
00742 lastHostName->next = tmpHostName;
00743 }
00744 tmpHostName->next = NULL;
00745 }
00746
00747 return (0);
00748 }
00749
00750 int
00751 queRodsServerHost (rodsServerHost_t **rodsServerHostHead,
00752 rodsServerHost_t *myRodsServerHost)
00753 {
00754 rodsServerHost_t *lastRodsServerHost, *tmpRodsServerHost;
00755
00756 lastRodsServerHost = tmpRodsServerHost = *rodsServerHostHead;
00757 while (tmpRodsServerHost != NULL) {
00758 lastRodsServerHost = tmpRodsServerHost;
00759 tmpRodsServerHost = tmpRodsServerHost->next;
00760 }
00761
00762 if (lastRodsServerHost == NULL) {
00763 *rodsServerHostHead = myRodsServerHost;
00764 } else {
00765 lastRodsServerHost->next = myRodsServerHost;
00766 }
00767 myRodsServerHost->next = NULL;
00768
00769 return (0);
00770 }
00771
00772 char *
00773 getConfigDir()
00774 {
00775 #ifndef windows_platform
00776 char *myDir;
00777
00778 if ((myDir = (char *) getenv("irodsConfigDir")) != (char *) NULL) {
00779 return (myDir);
00780 }
00781 return (DEF_CONFIG_DIR);
00782 #else
00783 return iRODSNtGetServerConfigPath();
00784 #endif
00785 }
00786
00787 char *
00788 getLogDir()
00789 {
00790 #ifndef windows_platform
00791 char *myDir;
00792
00793 if ((myDir = (char *) getenv("irodsLogDir")) != (char *) NULL) {
00794 return (myDir);
00795 }
00796 return (DEF_LOG_DIR);
00797 #else
00798 return iRODSNtServerGetLogDir;
00799 #endif
00800 }
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812 int
00813 getAndConnRcatHost (rsComm_t *rsComm, int rcatType, char *rcatZoneHint,
00814 rodsServerHost_t **rodsServerHost)
00815 {
00816 int status;
00817
00818 status = getRcatHost( rcatType, rcatZoneHint, rodsServerHost );
00819
00820 if (status < 0) {
00821 rodsLog (LOG_NOTICE,
00822 "getAndConnRcatHost:getRcatHost() failed. erro=%d", status);
00823 return (status);
00824 }
00825
00826 if ((*rodsServerHost)->localFlag == LOCAL_HOST) {
00827 return (LOCAL_HOST);
00828 }
00829 status = svrToSvrConnect (rsComm, *rodsServerHost);
00830
00831 if (status < 0) {
00832 rodsLog (LOG_NOTICE,
00833 "getAndConnRcatHost: svrToSvrConnect to %s failed",
00834 (*rodsServerHost)->hostName->name);
00835 if ((*rodsServerHost)->rcatEnabled == REMOTE_ICAT)
00836 status = convZoneSockError (status);
00837 }
00838 if (status >= 0) {
00839 return (REMOTE_HOST);
00840 } else {
00841 return (status);
00842 }
00843 }
00844
00845 int
00846 getAndConnRcatHostNoLogin (rsComm_t *rsComm, int rcatType, char *rcatZoneHint,
00847 rodsServerHost_t **rodsServerHost)
00848 {
00849 int status;
00850
00851 status = getRcatHost (rcatType, rcatZoneHint, rodsServerHost);
00852
00853 if (status < 0) {
00854 return (status);
00855 }
00856
00857 if ((*rodsServerHost)->localFlag == LOCAL_HOST) {
00858 return (LOCAL_HOST);
00859 }
00860
00861 status = svrToSvrConnectNoLogin (rsComm, *rodsServerHost);
00862
00863 if (status < 0) {
00864 rodsLog (LOG_NOTICE,
00865 "getAndConnRcatHost: svrToSvrConnectNoLogin to %s failed",
00866 (*rodsServerHost)->hostName->name);
00867 if ((*rodsServerHost)->rcatEnabled == REMOTE_ICAT)
00868 status = convZoneSockError (status);
00869 }
00870 return (status);
00871 }
00872
00873 int
00874 convZoneSockError (int inStatus)
00875 {
00876 int unixErr = getErrno (inStatus);
00877 if (inStatus + unixErr == USER_SOCK_CONNECT_ERR)
00878 return (CROSS_ZONE_SOCK_CONNECT_ERR - unixErr);
00879 else
00880 return inStatus;
00881 }
00882
00883
00884
00885
00886
00887
00888
00889
00890 int
00891 getZoneInfo (char *rcatZoneHint, zoneInfo_t **myZoneInfo)
00892 {
00893 int status;
00894 zoneInfo_t *tmpZoneInfo;
00895 int zoneInput;
00896 char zoneName[NAME_LEN];
00897
00898 if (rcatZoneHint == NULL || strlen (rcatZoneHint) == 0) {
00899 zoneInput = 0;
00900 } else {
00901 zoneInput = 1;
00902 getZoneNameFromHint (rcatZoneHint, zoneName, NAME_LEN);
00903 }
00904
00905 *myZoneInfo = NULL;
00906 tmpZoneInfo = ZoneInfoHead;
00907 while (tmpZoneInfo != NULL) {
00908 if (zoneInput == 0) {
00909 if (tmpZoneInfo->masterServerHost->rcatEnabled == LOCAL_ICAT) {
00910 *myZoneInfo = tmpZoneInfo;
00911 }
00912 } else {
00913 if (strcmp (zoneName, tmpZoneInfo->zoneName) == 0) {
00914 *myZoneInfo = tmpZoneInfo;
00915 }
00916 }
00917 if (*myZoneInfo != NULL) return 0;
00918 tmpZoneInfo = tmpZoneInfo->next;
00919 }
00920
00921 if (zoneInput == 0) {
00922 rodsLog (LOG_ERROR,
00923 "getRcatHost: No local Rcat");
00924 return (SYS_INVALID_ZONE_NAME);
00925 } else {
00926 rodsLog (LOG_DEBUG,
00927 "getZoneInfo: Invalid zone name from hint %s", rcatZoneHint);
00928 status = getZoneInfo (NULL, myZoneInfo);
00929 if (status < 0) {
00930 return (SYS_INVALID_ZONE_NAME);
00931 } else {
00932 return (0);
00933 }
00934 }
00935 }
00936
00937 int
00938 getRcatHost (int rcatType, char *rcatZoneHint,
00939 rodsServerHost_t **rodsServerHost)
00940 {
00941 int status;
00942 zoneInfo_t *myZoneInfo = NULL;
00943
00944 status = getZoneInfo (rcatZoneHint, &myZoneInfo);
00945 if (status < 0) return status;
00946
00947 if (rcatType == MASTER_RCAT ||
00948 myZoneInfo->slaveServerHost == NULL) {
00949 *rodsServerHost = myZoneInfo->masterServerHost;
00950 return (myZoneInfo->masterServerHost->localFlag);
00951 } else {
00952 *rodsServerHost = myZoneInfo->slaveServerHost;
00953 return (myZoneInfo->slaveServerHost->localFlag);
00954 }
00955 }
00956
00957 int
00958 getLocalZoneInfo (zoneInfo_t **outZoneInfo)
00959 {
00960 zoneInfo_t *tmpZoneInfo;
00961
00962 tmpZoneInfo = ZoneInfoHead;
00963 while (tmpZoneInfo != NULL) {
00964 if (tmpZoneInfo->masterServerHost->rcatEnabled == LOCAL_ICAT) {
00965 *outZoneInfo = tmpZoneInfo;
00966 return (0);
00967 }
00968 tmpZoneInfo = tmpZoneInfo->next;
00969 }
00970 rodsLog (LOG_ERROR,
00971 "getLocalZoneInfo: Local Zone does not exist");
00972
00973 *outZoneInfo = NULL;
00974 return (SYS_INVALID_ZONE_NAME);
00975 }
00976
00977 char*
00978 getLocalZoneName ()
00979 {
00980 zoneInfo_t *tmpZoneInfo;
00981
00982 if (getLocalZoneInfo (&tmpZoneInfo) >= 0) {
00983 return (tmpZoneInfo->zoneName);
00984 } else {
00985 return NULL;
00986 }
00987 }
00988
00989
00990 int
00991 getAndDisconnRcatHost (rsComm_t *rsComm, int rcatType, char *rcatZoneHint,
00992 rodsServerHost_t **rodsServerHost)
00993 {
00994 int status;
00995
00996 status = getRcatHost (rcatType, rcatZoneHint, rodsServerHost);
00997
00998 if (status < 0) return(status);
00999
01000 if ((*rodsServerHost)->conn != NULL) {
01001 status = rcDisconnect((*rodsServerHost)->conn);
01002 return(status);
01003 }
01004 return(0);
01005 }
01006
01007 int
01008 disconnRcatHost (rsComm_t *rsComm, int rcatType, char *rcatZoneHint)
01009 {
01010 int status;
01011 rodsServerHost_t *rodsServerHost = NULL;
01012
01013 status = getRcatHost (rcatType, rcatZoneHint, &rodsServerHost);
01014
01015 if (status < 0 || NULL == rodsServerHost ) {
01016 return (status);
01017 }
01018
01019 if ((rodsServerHost)->localFlag == LOCAL_HOST) {
01020 return (LOCAL_HOST);
01021 }
01022
01023 if (rodsServerHost->conn != NULL) {
01024 status = rcDisconnect(rodsServerHost->conn);
01025 rodsServerHost->conn = NULL;
01026 }
01027 if (status >= 0) {
01028 return (REMOTE_HOST);
01029 } else {
01030 return (status);
01031 }
01032 }
01033
01034
01035
01036 int
01037 resetRcatHost (rsComm_t *rsComm, int rcatType, char *rcatZoneHint)
01038 {
01039 int status;
01040 rodsServerHost_t *rodsServerHost = NULL;
01041
01042 status = getRcatHost (rcatType, rcatZoneHint, &rodsServerHost);
01043
01044 if (status < 0 || NULL == rodsServerHost ) {
01045 return (status);
01046 }
01047
01048 if ((rodsServerHost)->localFlag == LOCAL_HOST) {
01049 return (LOCAL_HOST);
01050 }
01051
01052 if (rodsServerHost->conn != NULL) {
01053 rodsServerHost->conn = NULL;
01054 }
01055 if (status >= 0) {
01056 return (REMOTE_HOST);
01057 } else {
01058 return (status);
01059 }
01060 }
01061
01062 int
01063 initZone (rsComm_t *rsComm)
01064 {
01065 rodsEnv *myEnv = &rsComm->myEnv;
01066 rodsServerHost_t *tmpRodsServerHost;
01067 rodsServerHost_t *masterServerHost = NULL;
01068 rodsServerHost_t *slaveServerHost = NULL;
01069 genQueryInp_t genQueryInp;
01070 genQueryOut_t *genQueryOut = NULL;
01071 int status, i;
01072 sqlResult_t *zoneName, *zoneType, *zoneConn, *zoneComment;
01073 char *tmpZoneName, *tmpZoneType, *tmpZoneConn, *tmpZoneComment;
01074
01075
01076
01077 tmpRodsServerHost = ServerHostHead;
01078 while (tmpRodsServerHost != NULL) {
01079 if (tmpRodsServerHost->rcatEnabled == LOCAL_ICAT) {
01080 tmpRodsServerHost->zoneInfo = ZoneInfoHead;
01081 masterServerHost = tmpRodsServerHost;
01082 } else if (tmpRodsServerHost->rcatEnabled == LOCAL_SLAVE_ICAT) {
01083 tmpRodsServerHost->zoneInfo = ZoneInfoHead;
01084 slaveServerHost = tmpRodsServerHost;
01085 }
01086 tmpRodsServerHost = tmpRodsServerHost->next;
01087 }
01088 ZoneInfoHead->masterServerHost = masterServerHost;
01089 ZoneInfoHead->slaveServerHost = slaveServerHost;
01090
01091
01092 memset (&genQueryInp, 0, sizeof (genQueryInp));
01093 addInxIval (&genQueryInp.selectInp, COL_ZONE_NAME, 1);
01094 addInxIval (&genQueryInp.selectInp, COL_ZONE_TYPE, 1);
01095 addInxIval (&genQueryInp.selectInp, COL_ZONE_CONNECTION, 1);
01096 addInxIval (&genQueryInp.selectInp, COL_ZONE_COMMENT, 1);
01097 genQueryInp.maxRows = MAX_SQL_ROWS;
01098
01099 status = rsGenQuery (rsComm, &genQueryInp, &genQueryOut);
01100
01101 clearGenQueryInp (&genQueryInp);
01102
01103 if (status < 0) {
01104 rodsLog (LOG_NOTICE,
01105 "initZone: rsGenQuery error, status = %d", status);
01106 return (status);
01107 }
01108
01109 if (genQueryOut == NULL) {
01110 rodsLog (LOG_NOTICE,
01111 "initZone: NULL genQueryOut");
01112 return (CAT_NO_ROWS_FOUND);
01113 }
01114
01115 if ((zoneName = getSqlResultByInx (genQueryOut, COL_ZONE_NAME)) == NULL) {
01116 rodsLog (LOG_NOTICE,
01117 "initZone: getSqlResultByInx for COL_ZONE_NAME failed");
01118 return (UNMATCHED_KEY_OR_INDEX);
01119 }
01120 if ((zoneType = getSqlResultByInx (genQueryOut, COL_ZONE_TYPE)) == NULL) {
01121 rodsLog (LOG_NOTICE,
01122 "initZone: getSqlResultByInx for COL_ZONE_TYPE failed");
01123 return (UNMATCHED_KEY_OR_INDEX);
01124 }
01125 if ((zoneConn = getSqlResultByInx (genQueryOut, COL_ZONE_CONNECTION)) == NULL) {
01126 rodsLog (LOG_NOTICE,
01127 "initZone: getSqlResultByInx for COL_ZONE_CONNECTION failed");
01128 return (UNMATCHED_KEY_OR_INDEX);
01129 }
01130 if ((zoneComment = getSqlResultByInx (genQueryOut, COL_ZONE_COMMENT)) == NULL) {
01131 rodsLog (LOG_NOTICE,
01132 "initZone: getSqlResultByInx for COL_ZONE_COMMENT failed");
01133 return (UNMATCHED_KEY_OR_INDEX);
01134 }
01135
01136 for (i = 0;i < genQueryOut->rowCnt; i++) {
01137 rodsHostAddr_t addr;
01138
01139 tmpZoneName = &zoneName->value[zoneName->len * i];
01140 tmpZoneType = &zoneType->value[zoneType->len * i];
01141 tmpZoneConn = &zoneConn->value[zoneConn->len * i];
01142 tmpZoneComment = &zoneComment->value[zoneComment->len * i];
01143 if (strcmp (tmpZoneType, "local") == 0) {
01144 if (strcmp (myEnv->rodsZone, tmpZoneName) != 0) {
01145 rodsLog (LOG_ERROR,
01146 "initZone: zoneName in env %s does not match %s in icat ",
01147 myEnv->rodsZone, tmpZoneName);
01148 }
01149
01150 if (strlen (rsComm->proxyUser.rodsZone) == 0)
01151 rstrcpy (rsComm->proxyUser.rodsZone, tmpZoneName, NAME_LEN);
01152 if (strlen (rsComm->clientUser.rodsZone) == 0)
01153 rstrcpy (rsComm->clientUser.rodsZone, tmpZoneName, NAME_LEN);
01154 continue;
01155 } else if (strlen (tmpZoneConn) <= 0) {
01156 rodsLog (LOG_ERROR,
01157 "initZone: connection info for zone %s not configured",
01158 tmpZoneName);
01159 continue;
01160 }
01161
01162 memset (&addr, 0, sizeof (addr));
01163
01164 parseHostAddrStr (tmpZoneConn, &addr);
01165 if (addr.portNum == 0) addr.portNum = ZoneInfoHead->portNum;
01166 rstrcpy( addr.zoneName, tmpZoneName, NAME_LEN );
01167 status = resolveHost (&addr, &tmpRodsServerHost);
01168 if (status < 0) {
01169 rodsLog (LOG_ERROR,
01170 "initZone: resolveHost error for %s for zone %s. status = %d",
01171 addr.hostAddr, tmpZoneName, status);
01172 continue;
01173 }
01174 if (tmpRodsServerHost->rcatEnabled == LOCAL_ICAT) {
01175 rodsLog (LOG_ERROR,
01176 "initZone: address %s for remote zone %s already in use",
01177 tmpZoneConn, tmpZoneName);
01178 continue;
01179 }
01180
01181 tmpRodsServerHost->rcatEnabled = REMOTE_ICAT;
01182
01183
01184 tmpRodsServerHost->localFlag = REMOTE_HOST;
01185 queZone (tmpZoneName, addr.portNum, tmpRodsServerHost, NULL);
01186 }
01187
01188 freeGenQueryOut (&genQueryOut);
01189
01190 return (0);
01191 }
01192
01193 int
01194 queZone (char *zoneName, int portNum, rodsServerHost_t *masterServerHost,
01195 rodsServerHost_t *slaveServerHost)
01196 {
01197 zoneInfo_t *tmpZoneInfo, *lastZoneInfo;
01198 zoneInfo_t *myZoneInfo;
01199
01200 myZoneInfo = (zoneInfo_t *) malloc (sizeof (zoneInfo_t));
01201
01202 memset (myZoneInfo, 0, sizeof (zoneInfo_t));
01203
01204 rstrcpy (myZoneInfo->zoneName, zoneName, NAME_LEN);
01205 if (masterServerHost != NULL) {
01206 myZoneInfo->masterServerHost = masterServerHost;
01207 masterServerHost->zoneInfo = myZoneInfo;
01208 }
01209 if (slaveServerHost != NULL) {
01210 myZoneInfo->slaveServerHost = slaveServerHost;
01211 slaveServerHost->zoneInfo = myZoneInfo;
01212 }
01213
01214 if (portNum <= 0) {
01215 if (ZoneInfoHead != NULL) {
01216 myZoneInfo->portNum = ZoneInfoHead->portNum;
01217 } else {
01218 rodsLog (LOG_ERROR,
01219 "queZone: Bad input portNum %d for %s", portNum, zoneName);
01220 free (myZoneInfo);
01221 return (SYS_INVALID_SERVER_HOST);
01222 }
01223 } else {
01224 myZoneInfo->portNum = portNum;
01225 }
01226
01227
01228
01229 lastZoneInfo = tmpZoneInfo = ZoneInfoHead;
01230 while (tmpZoneInfo != NULL) {
01231 lastZoneInfo = tmpZoneInfo;
01232 tmpZoneInfo = tmpZoneInfo->next;
01233 }
01234
01235 if (lastZoneInfo == NULL) {
01236 ZoneInfoHead = myZoneInfo;
01237 } else {
01238 lastZoneInfo->next = myZoneInfo;
01239 }
01240 myZoneInfo->next = NULL;
01241
01242 if (masterServerHost == NULL) {
01243 rodsLog (LOG_DEBUG,
01244 "queZone: masterServerHost for %s is NULL", zoneName);
01245 return (SYS_INVALID_SERVER_HOST);
01246 } else {
01247 return (0);
01248 }
01249 }
01250
01251 int
01252 setExecArg (char *commandArgv, char *av[])
01253 {
01254 char *inpPtr, *outPtr;
01255 int inx = 1;
01256 int c;
01257 int len = 0;
01258 int openQuote = 0;
01259
01260 if (commandArgv != NULL) {
01261 inpPtr = strdup (commandArgv);
01262 outPtr = inpPtr;
01263 while ((c = *inpPtr) != '\0') {
01264 if ((c == ' ' && openQuote == 0) || openQuote == 2) {
01265 openQuote = 0;
01266 if (len > 0) {
01267 *inpPtr = '\0';
01268 av[inx] = outPtr;
01269
01270 inpPtr++;
01271 outPtr = inpPtr;
01272 inx ++;
01273 len = 0;
01274 } else {
01275 inpPtr++;
01276 outPtr = inpPtr;
01277 }
01278 } else if (c == '\'' || c == '\"') {
01279 openQuote ++;
01280 if (openQuote == 1) {
01281
01282 inpPtr++;
01283 outPtr = inpPtr;
01284 }
01285 } else {
01286 len ++;
01287 inpPtr++;
01288 }
01289 }
01290 if (len > 0) {
01291 av[inx] = outPtr;
01292 inx++;
01293 }
01294 }
01295
01296 av[inx] = NULL;
01297
01298 return (0);
01299 }
01300 int
01301 initAgent (int processType, rsComm_t *rsComm)
01302 {
01303 int status;
01304 rsComm_t myComm;
01305 ruleExecInfo_t rei;
01306
01307 initProcLog ();
01308
01309 status = initServerInfo (rsComm);
01310 if (status < 0) {
01311 rodsLog (LOG_ERROR,
01312 "initAgent: initServerInfo error, status = %d",
01313 status);
01314 return (status);
01315 }
01316
01317 initL1desc ();
01318 initSpecCollDesc ();
01319 initCollHandle ();
01320 status = initFileDesc ();
01321 if (status < 0) {
01322 rodsLog (LOG_ERROR,
01323 "initAgent: initFileDesc error, status = %d",
01324 status);
01325 return (status);
01326 }
01327 #ifdef TAR_STRUCT_FILE
01328
01329
01330 #endif
01331
01332 status = initRuleEngine(processType, rsComm, reRuleStr, reFuncMapStr, reVariableMapStr);
01333 if (status < 0) {
01334 rodsLog (LOG_ERROR,
01335 "initAgent: initRuleEngine error, status = %d", status);
01336 return(status);
01337 }
01338
01339 memset (&rei, 0, sizeof (rei));
01340 rei.rsComm = rsComm;
01341
01342 if (ProcessType == AGENT_PT) {
01343 status = applyRule ("acChkHostAccessControl", NULL, &rei,
01344 NO_SAVE_REI);
01345
01346 if (status < 0) {
01347 rodsLog (LOG_ERROR,
01348 "initAgent: acChkHostAccessControl error, status = %d",
01349 status);
01350 return (status);
01351 }
01352 }
01353
01354
01355
01356 GlobalQuotaLimit = RESC_QUOTA_UNINIT;
01357 GlobalQuotaOverrun = 0;
01358 RescQuotaPolicy = RESC_QUOTA_UNINIT;
01359
01360 seedRandom ();
01361
01362 #ifndef windows_platform
01363 if (rsComm->reconnFlag == RECONN_TIMEOUT) {
01364 rsComm->reconnSock = svrSockOpenForInConn (rsComm, &rsComm->reconnPort,
01365 &rsComm->reconnAddr, SOCK_STREAM);
01366 if (rsComm->reconnSock < 0) {
01367 rsComm->reconnPort = 0;
01368 rsComm->reconnAddr = NULL;
01369 } else {
01370 rsComm->cookie = random ();
01371 }
01372 #ifdef USE_BOOST
01373 rsComm->lock = new boost::mutex;
01374 rsComm->cond = new boost::condition_variable;
01375 rsComm->reconnThr = new boost::thread( reconnManager, rsComm );
01376 #else
01377 pthread_mutex_init (&rsComm->lock, NULL);
01378 pthread_cond_init (&rsComm->cond, NULL);
01379 status = pthread_create (&rsComm->reconnThr, pthread_attr_default,
01380 (void *(*)(void *)) reconnManager,
01381 (void *) rsComm);
01382 #endif
01383 if (status < 0) {
01384 rodsLog (LOG_ERROR, "initAgent: pthread_create failed, stat=%d",
01385 status);
01386 }
01387 }
01388 initExecCmdMutex ();
01389 #endif
01390
01391 InitialState = INITIAL_DONE;
01392 ThisComm = rsComm;
01393
01394
01395 myComm = *rsComm;
01396 myComm.clientUser.authInfo.authFlag = LOCAL_PRIV_USER_AUTH;
01397 rei.rsComm = &myComm;
01398
01399 status = applyRule ("acSetPublicUserPolicy", NULL, &rei, NO_SAVE_REI);
01400
01401 if (status < 0) {
01402 rodsLog (LOG_ERROR,
01403 "initAgent: acSetPublicUserPolicy error, status = %d",
01404 status);
01405 return (status);
01406 }
01407
01408 return (status);
01409 }
01410
01411 void
01412 cleanupAndExit (int status)
01413 {
01414 rodsLog (LOG_NOTICE,
01415 "Agent exiting with status = %d", status);
01416
01417 #ifdef RODS_CAT
01418 disconnectRcat (ThisComm);
01419 #endif
01420
01421
01422 finalzeRuleEngine(ThisComm);
01423
01424 if (InitialState == INITIAL_DONE) {
01425
01426 closeAllL1desc (ThisComm);
01427
01428 disconnectAllSvrToSvrConn ();
01429 }
01430
01431 #if 0 // JMC - fixes double free on resource server, fixes lean on icat server... ?
01432
01433
01434 if( ServerHostHead ) {
01435 rodsServerHost_t* tmp_host = ServerHostHead;
01436 while( tmp_host ) {
01437 rodsServerHost_t* free_me = tmp_host;
01438 tmp_host = tmp_host->next;
01439
01440 hostName_t* tmp_name = free_me->hostName;
01441 while( tmp_name ) {
01442 hostName_t* free_name = tmp_name;
01443 tmp_name = free_name->next;
01444 free( free_name->name );
01445 free( free_name );
01446 }
01447
01448 free( free_me->zoneInfo );
01449 free( free_me );
01450 }
01451 }
01452 #endif
01453
01454 if (status >= 0) {
01455 exit (0);
01456 } else {
01457 exit (1);
01458 }
01459 }
01460
01461 void
01462 #ifdef __cplusplus
01463 signalExit (int)
01464 #else
01465 signalExit ()
01466 #endif
01467 {
01468 rodsLog (LOG_NOTICE,
01469 "caught a signal and exiting\n");
01470 cleanupAndExit (SYS_CAUGHT_SIGNAL);
01471 }
01472
01473 void
01474 #ifdef __cplusplus
01475 rsPipSigalHandler (int)
01476 #else
01477 rsPipSigalHandler ()
01478 #endif
01479 {
01480 time_t curTime;
01481
01482 if (ThisComm == NULL || ThisComm->reconnSock <= 0) {
01483 rodsLog (LOG_NOTICE,
01484 "caught a broken pipe signal and exiting");
01485 cleanupAndExit (SYS_CAUGHT_SIGNAL);
01486 } else {
01487 curTime = time (0);
01488 if (curTime - LastBrokenPipeTime < BROKEN_PIPE_INT) {
01489 BrokenPipeCnt ++;
01490 if (BrokenPipeCnt > MAX_BROKEN_PIPE_CNT) {
01491 rodsLog (LOG_NOTICE,
01492 "caught a broken pipe signal and exiting");
01493 cleanupAndExit (SYS_CAUGHT_SIGNAL);
01494 }
01495 } else {
01496 BrokenPipeCnt = 1;
01497 }
01498 LastBrokenPipeTime = curTime;
01499 rodsLog (LOG_NOTICE,
01500 "caught a broken pipe signal. Attempt to reconnect");
01501 #ifndef _WIN32
01502 signal(SIGPIPE, (void (*)(int)) rsPipSigalHandler);
01503 #endif
01504
01505 }
01506 }
01507
01508 int
01509 initHostConfigByFile (rsComm_t *rsComm)
01510 {
01511 FILE *fptr;
01512 char *hostCongFile;
01513 char inbuf[MAX_NAME_LEN];
01514 char hostBuf[LONG_NAME_LEN];
01515 rodsServerHost_t *tmpRodsServerHost;
01516 int lineLen, bytesCopied;
01517 int status;
01518
01519 hostCongFile = (char *) malloc((strlen (getConfigDir()) +
01520 strlen(HOST_CONFIG_FILE) + 24));
01521
01522 #ifndef windows_platform
01523 sprintf (hostCongFile, "%-s/%-s", getConfigDir(), HOST_CONFIG_FILE);
01524 fptr = fopen (hostCongFile, "r");
01525 #else
01526 sprintf(hostCongFile, "%s\\%s", getConfigDir(),HOST_CONFIG_FILE);
01527 fptr = iRODSNt_fopen(hostCongFile, "r");
01528 #endif
01529
01530 if (fptr == NULL) {
01531 rodsLog (LOG_NOTICE,
01532 "Cannot open HOST_CONFIG_FILE file %s. ernro = %d\n",
01533 hostCongFile, errno);
01534 free (hostCongFile);
01535 return (SYS_CONFIG_FILE_ERR);
01536 }
01537
01538 free (hostCongFile);
01539
01540 while ((lineLen = getLine (fptr, inbuf, MAX_NAME_LEN)) > 0) {
01541 char *inPtr = inbuf;
01542 int cnt = 0;
01543 while ((bytesCopied = getStrInBuf (&inPtr, hostBuf,
01544 &lineLen, LONG_NAME_LEN)) > 0) {
01545 if (cnt == 0) {
01546
01547 tmpRodsServerHost = (rodsServerHost_t*)malloc (sizeof (rodsServerHost_t));
01548 memset (tmpRodsServerHost, 0, sizeof (rodsServerHost_t));
01549
01550 tmpRodsServerHost->localFlag = REMOTE_HOST;
01551
01552 tmpRodsServerHost->zoneInfo = ZoneInfoHead;
01553 status = queRodsServerHost (&HostConfigHead, tmpRodsServerHost);
01554
01555 }
01556 cnt ++;
01557 if (strcmp (hostBuf, "localhost") == 0) {
01558 tmpRodsServerHost->localFlag = LOCAL_HOST;
01559 } else {
01560 status = queHostName (tmpRodsServerHost, hostBuf, 0);
01561 }
01562 }
01563 }
01564 fclose (fptr);
01565 return (0);
01566 }
01567
01568 int
01569 matchHostConfig (rodsServerHost_t *myRodsServerHost)
01570 {
01571 rodsServerHost_t *tmpRodsServerHost;
01572 int status;
01573
01574 if (myRodsServerHost == NULL)
01575 return (0);
01576
01577 if (myRodsServerHost->localFlag == LOCAL_HOST) {
01578 tmpRodsServerHost = HostConfigHead;
01579 while (tmpRodsServerHost != NULL) {
01580 if (tmpRodsServerHost->localFlag == LOCAL_HOST) {
01581 status = queConfigName (tmpRodsServerHost, myRodsServerHost);
01582 return (status);
01583 }
01584 tmpRodsServerHost = tmpRodsServerHost->next;
01585 }
01586 } else {
01587 tmpRodsServerHost = HostConfigHead;
01588 while (tmpRodsServerHost != NULL) {
01589 hostName_t *tmpHostName, *tmpConfigName;
01590
01591 if (tmpRodsServerHost->localFlag == LOCAL_HOST &&
01592 myRodsServerHost->localFlag != UNKNOWN_HOST_LOC) {
01593 tmpRodsServerHost = tmpRodsServerHost->next;
01594 continue;
01595 }
01596
01597 tmpConfigName = tmpRodsServerHost->hostName;
01598 while (tmpConfigName != NULL) {
01599 tmpHostName = myRodsServerHost->hostName;
01600 while (tmpHostName != NULL) {
01601 if (strcmp (tmpHostName->name, tmpConfigName->name) == 0) {
01602 myRodsServerHost->localFlag =
01603 tmpRodsServerHost->localFlag;
01604 status = queConfigName (tmpRodsServerHost,
01605 myRodsServerHost);
01606 return (0);
01607 }
01608 tmpHostName = tmpHostName->next;
01609 }
01610 tmpConfigName = tmpConfigName->next;
01611 }
01612 tmpRodsServerHost = tmpRodsServerHost->next;
01613 }
01614 }
01615
01616 return (0);
01617 }
01618
01619 int
01620 queConfigName (rodsServerHost_t *configServerHost,
01621 rodsServerHost_t *myRodsServerHost)
01622 {
01623 hostName_t *tmpHostName = configServerHost->hostName;
01624 int cnt = 0;
01625
01626 while (tmpHostName != NULL) {
01627 if (cnt == 0) {
01628
01629 queHostName (myRodsServerHost, tmpHostName->name, 1);
01630 } else {
01631 queHostName (myRodsServerHost, tmpHostName->name, 0);
01632 }
01633 cnt ++;
01634 tmpHostName = tmpHostName->next;
01635 }
01636
01637 return (0);
01638 }
01639
01640 int
01641 disconnectAllSvrToSvrConn ()
01642 {
01643 rodsServerHost_t *tmpRodsServerHost;
01644
01645
01646
01647 tmpRodsServerHost = ServerHostHead;
01648 while (tmpRodsServerHost != NULL) {
01649 if (tmpRodsServerHost->conn != NULL) {
01650 rcDisconnect (tmpRodsServerHost->conn);
01651 tmpRodsServerHost->conn = NULL;
01652 }
01653 tmpRodsServerHost = tmpRodsServerHost->next;
01654 }
01655 return (0);
01656 }
01657
01658 int
01659 initRsComm (rsComm_t *rsComm)
01660 {
01661 int status;
01662
01663 memset (rsComm, 0, sizeof (rsComm_t));
01664 status = getRodsEnv (&rsComm->myEnv);
01665
01666
01667 if (status < 0) {
01668 rodsLog (LOG_ERROR,
01669 "initRsComm: getRodsEnv serror, status = %d", status);
01670 return (status);
01671 }
01672
01673
01674
01675
01676 rstrcpy (rsComm->proxyUser.userName, rsComm->myEnv.rodsUserName, NAME_LEN);
01677 rstrcpy (rsComm->proxyUser.rodsZone, rsComm->myEnv.rodsZone, NAME_LEN);
01678 rstrcpy (rsComm->proxyUser.authInfo.authScheme,
01679 rsComm->myEnv.rodsAuthScheme, NAME_LEN);
01680 rstrcpy (rsComm->clientUser.userName, rsComm->myEnv.rodsUserName, NAME_LEN);
01681 rstrcpy (rsComm->clientUser.rodsZone, rsComm->myEnv.rodsZone, NAME_LEN);
01682 rstrcpy (rsComm->clientUser.authInfo.authScheme,
01683 rsComm->myEnv.rodsAuthScheme, NAME_LEN);
01684
01685 rsComm->clientUser.authInfo.authFlag =
01686 rsComm->proxyUser.authInfo.authFlag = LOCAL_PRIV_USER_AUTH;
01687
01688 return (0);
01689 }
01690
01691 void
01692 daemonize (int runMode, int logFd)
01693 {
01694 #ifndef _WIN32
01695 if (runMode == SINGLE_PASS)
01696 return;
01697
01698 if (runMode == STANDALONE_SERVER) {
01699 if (fork())
01700 exit (0);
01701
01702 if (setsid() < 0) {
01703 fprintf(stderr, "daemonize");
01704 perror("cannot create a new session.");
01705 exit(1);
01706 }
01707 }
01708
01709 close (0);
01710 close (1);
01711 close (2);
01712
01713 (void) dup2 (logFd, 0);
01714 (void) dup2 (logFd, 1);
01715 (void) dup2 (logFd, 2);
01716 close (logFd);
01717 #endif
01718 }
01719
01720
01721
01722
01723
01724
01725
01726 int
01727 logFileOpen (int runMode, char *logDir, char *logFileName)
01728 {
01729 char *logFile = NULL;
01730 #ifdef IRODS_SYSLOG
01731 int logFd = 0;
01732 #else
01733 int logFd;
01734 #endif
01735
01736 if (runMode == SINGLE_PASS && logDir == NULL) {
01737 return (1);
01738 }
01739
01740 if (logFileName == NULL) {
01741 fprintf (stderr, "logFileOpen: NULL input logFileName\n");
01742 return SYS_INTERNAL_NULL_INPUT_ERR;
01743 }
01744
01745 getLogfileName (&logFile, logDir, logFileName);
01746 if( NULL == logFile ) {
01747 fprintf( stderr, "logFileOpen: unable to open log file" );
01748 return -1;
01749 }
01750
01751 #ifndef IRODS_SYSLOG
01752 logFd = open (logFile, O_CREAT|O_WRONLY|O_APPEND, 0666);
01753 #endif
01754 if (logFd < 0) {
01755 fprintf (stderr, "logFileOpen: Unable to open %s. errno = %d\n",
01756 logFile, errno);
01757 return (-1 * errno);
01758 }
01759
01760
01761 return (logFd);
01762 }
01763
01764 int
01765 initRsCommWithStartupPack (rsComm_t *rsComm, startupPack_t *startupPack)
01766 {
01767 char *tmpStr;
01768 static char tmpStr2[LONG_NAME_LEN];
01769
01770
01771 snprintf (tmpStr2, LONG_NAME_LEN, "%s=%d", IRODS_PROT, NATIVE_PROT);
01772 putenv (tmpStr2);
01773
01774 if (startupPack != NULL) {
01775 rsComm->connectCnt = startupPack->connectCnt;
01776 rsComm->irodsProt = startupPack->irodsProt;
01777 rsComm->reconnFlag = startupPack->reconnFlag;
01778 rstrcpy (rsComm->proxyUser.userName, startupPack->proxyUser,
01779 NAME_LEN);
01780 if (strcmp (startupPack->proxyUser, PUBLIC_USER_NAME) == 0) {
01781 rsComm->proxyUser.authInfo.authFlag = PUBLIC_USER_AUTH;
01782 }
01783 rstrcpy (rsComm->proxyUser.rodsZone, startupPack->proxyRodsZone,
01784 NAME_LEN);
01785 rstrcpy (rsComm->clientUser.userName, startupPack->clientUser,
01786 NAME_LEN);
01787 if (strcmp (startupPack->clientUser, PUBLIC_USER_NAME) == 0) {
01788 rsComm->clientUser.authInfo.authFlag = PUBLIC_USER_AUTH;
01789 }
01790 rstrcpy (rsComm->clientUser.rodsZone, startupPack->clientRodsZone,
01791 NAME_LEN);
01792 rstrcpy (rsComm->cliVersion.relVersion, startupPack->relVersion,
01793 NAME_LEN);
01794 rstrcpy (rsComm->cliVersion.apiVersion, startupPack->apiVersion,
01795 NAME_LEN);
01796 rstrcpy (rsComm->option, startupPack->option, NAME_LEN);
01797 } else {
01798 tmpStr = getenv (SP_NEW_SOCK);
01799 if (tmpStr == NULL) {
01800 rodsLog (LOG_NOTICE,
01801 "initRsCommWithStartupPack: env %s does not exist",
01802 SP_NEW_SOCK);
01803 return (SYS_GETSTARTUP_PACK_ERR);
01804 }
01805 rsComm->sock = atoi (tmpStr);
01806
01807 tmpStr = getenv (SP_CONNECT_CNT);
01808 if (tmpStr == NULL) {
01809 rodsLog (LOG_NOTICE,
01810 "initRsCommWithStartupPack: env %s does not exist",
01811 SP_CONNECT_CNT);
01812 return (SYS_GETSTARTUP_PACK_ERR);
01813 }
01814 rsComm->connectCnt = atoi (tmpStr) + 1;
01815
01816 tmpStr = getenv (SP_PROTOCOL);
01817 if (tmpStr == NULL) {
01818 rodsLog (LOG_NOTICE,
01819 "initRsCommWithStartupPack: env %s does not exist",
01820 SP_PROTOCOL);
01821 return (SYS_GETSTARTUP_PACK_ERR);
01822 }
01823 rsComm->irodsProt = (irodsProt_t)atoi (tmpStr);
01824
01825 tmpStr = getenv (SP_RECONN_FLAG);
01826 if (tmpStr == NULL) {
01827 rodsLog (LOG_NOTICE,
01828 "initRsCommWithStartupPack: env %s does not exist",
01829 SP_RECONN_FLAG);
01830 return (SYS_GETSTARTUP_PACK_ERR);
01831 }
01832 rsComm->reconnFlag = atoi (tmpStr);
01833
01834 tmpStr = getenv (SP_PROXY_USER);
01835 if (tmpStr == NULL) {
01836 rodsLog (LOG_NOTICE,
01837 "initRsCommWithStartupPack: env %s does not exist",
01838 SP_PROXY_USER);
01839 return (SYS_GETSTARTUP_PACK_ERR);
01840 }
01841 rstrcpy (rsComm->proxyUser.userName, tmpStr, NAME_LEN);
01842 if (strcmp (tmpStr, PUBLIC_USER_NAME) == 0) {
01843 rsComm->proxyUser.authInfo.authFlag = PUBLIC_USER_AUTH;
01844 }
01845
01846 tmpStr = getenv (SP_PROXY_RODS_ZONE);
01847 if (tmpStr == NULL) {
01848 rodsLog (LOG_NOTICE,
01849 "initRsCommWithStartupPack: env %s does not exist",
01850 SP_PROXY_RODS_ZONE);
01851 return (SYS_GETSTARTUP_PACK_ERR);
01852 }
01853 rstrcpy (rsComm->proxyUser.rodsZone, tmpStr, NAME_LEN);
01854
01855 tmpStr = getenv (SP_CLIENT_USER);
01856 if (tmpStr == NULL) {
01857 rodsLog (LOG_NOTICE,
01858 "initRsCommWithStartupPack: env %s does not exist",
01859 SP_CLIENT_USER);
01860 return (SYS_GETSTARTUP_PACK_ERR);
01861 }
01862 rstrcpy (rsComm->clientUser.userName, tmpStr, NAME_LEN);
01863 if (strcmp (tmpStr, PUBLIC_USER_NAME) == 0) {
01864 rsComm->clientUser.authInfo.authFlag = PUBLIC_USER_AUTH;
01865 }
01866
01867 tmpStr = getenv (SP_CLIENT_RODS_ZONE);
01868 if (tmpStr == NULL) {
01869 rodsLog (LOG_NOTICE,
01870 "initRsCommWithStartupPack: env %s does not exist",
01871 SP_CLIENT_RODS_ZONE);
01872 return (SYS_GETSTARTUP_PACK_ERR);
01873 }
01874 rstrcpy (rsComm->clientUser.rodsZone, tmpStr, NAME_LEN);
01875
01876 tmpStr = getenv (SP_REL_VERSION);
01877 if (tmpStr == NULL) {
01878 rodsLog (LOG_NOTICE,
01879 "getstartupPackFromEnv: env %s does not exist",
01880 SP_REL_VERSION);
01881 return (SYS_GETSTARTUP_PACK_ERR);
01882 }
01883 rstrcpy (rsComm->cliVersion.relVersion, tmpStr, NAME_LEN);
01884
01885 tmpStr = getenv (SP_API_VERSION);
01886 if (tmpStr == NULL) {
01887 rodsLog (LOG_NOTICE,
01888 "initRsCommWithStartupPack: env %s does not exist",
01889 SP_API_VERSION);
01890 return (SYS_GETSTARTUP_PACK_ERR);
01891 }
01892 rstrcpy (rsComm->cliVersion.apiVersion, tmpStr, NAME_LEN);
01893
01894 tmpStr = getenv (SP_OPTION);
01895 #ifndef windows_platform
01896 if (tmpStr == NULL) {
01897 rodsLog (LOG_NOTICE,
01898 "initRsCommWithStartupPack: env %s does not exist",
01899 SP_OPTION);
01900 } else {
01901 rstrcpy (rsComm->option, tmpStr, NAME_LEN);
01902 }
01903 #else
01904 if(tmpStr != NULL) {
01905 rstrcpy (rsComm->option, tmpStr, NAME_LEN);
01906 }
01907 #endif
01908
01909 }
01910 if (rsComm->sock != 0) {
01911
01912 setLocalAddr (rsComm->sock, &rsComm->localAddr);
01913 setRemoteAddr (rsComm->sock, &rsComm->remoteAddr);
01914 }
01915
01916 tmpStr = inet_ntoa (rsComm->remoteAddr.sin_addr);
01917 if (tmpStr == NULL || *tmpStr == '\0') tmpStr = "UNKNOWN";
01918 rstrcpy (rsComm->clientAddr, tmpStr, NAME_LEN);
01919
01920 return (0);
01921 }
01922
01923
01924
01925
01926
01927
01928 int
01929 getAndConnRemoteZone (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
01930 rodsServerHost_t **rodsServerHost, char *remotZoneOpr)
01931 {
01932 int status;
01933
01934 status = getRemoteZoneHost (rsComm, dataObjInp, rodsServerHost,
01935 remotZoneOpr);
01936
01937 if (status == LOCAL_HOST) {
01938 return LOCAL_HOST;
01939 } else if (status < 0) {
01940 return status;
01941 }
01942
01943 status = svrToSvrConnect (rsComm, *rodsServerHost);
01944
01945 if (status < 0) {
01946 rodsLog (LOG_ERROR,
01947 "getAndConnRemoteZone: svrToSvrConnect to %s failed",
01948 (*rodsServerHost)->hostName->name);
01949 }
01950 if (status >= 0) {
01951 return (REMOTE_HOST);
01952 } else {
01953 return (status);
01954 }
01955 }
01956
01957 int
01958 getAndConnRemoteZoneForCopy (rsComm_t *rsComm, dataObjCopyInp_t *dataObjCopyInp,
01959 rodsServerHost_t **rodsServerHost)
01960 {
01961 int status;
01962 dataObjInp_t *srcDataObjInp, *destDataObjInp;
01963 rodsServerHost_t *srcIcatServerHost = NULL;
01964 rodsServerHost_t *destIcatServerHost = NULL;
01965
01966 srcDataObjInp = &dataObjCopyInp->srcDataObjInp;
01967 destDataObjInp = &dataObjCopyInp->destDataObjInp;
01968
01969 status = getRcatHost (MASTER_RCAT, srcDataObjInp->objPath,
01970 &srcIcatServerHost);
01971
01972 if (status < 0 || NULL == srcIcatServerHost ) {
01973 rodsLog (LOG_ERROR,
01974 "getAndConnRemoteZoneForCopy: getRcatHost error for %s",
01975 srcDataObjInp->objPath);
01976 return (status);
01977 }
01978
01979 if (srcIcatServerHost->rcatEnabled != REMOTE_ICAT) {
01980
01981 return (LOCAL_HOST);
01982 }
01983
01984 status = getRcatHost (MASTER_RCAT, destDataObjInp->objPath,
01985 &destIcatServerHost);
01986
01987 if (status < 0 || NULL == destIcatServerHost ) {
01988 rodsLog (LOG_ERROR,
01989 "getAndConnRemoteZoneForCopy: getRcatHost error for %s",
01990 destDataObjInp->objPath);
01991 return (status);
01992 }
01993
01994 if (destIcatServerHost->rcatEnabled != REMOTE_ICAT) {
01995
01996 return (LOCAL_HOST);
01997 }
01998
01999 #if 0
02000 if (srcIcatServerHost != destIcatServerHost) {
02001 return (LOCAL_HOST);
02002 }
02003
02004
02005 #endif
02006
02007 status = getAndConnRemoteZone (rsComm, destDataObjInp, rodsServerHost,
02008 REMOTE_CREATE);
02009
02010 return status;
02011 }
02012
02013 int
02014 isLocalZone (char *zoneHint)
02015 {
02016 int status;
02017 rodsServerHost_t *icatServerHost = NULL;
02018
02019 status = getRcatHost (MASTER_RCAT, zoneHint, &icatServerHost);
02020
02021 if (status < 0 || NULL == icatServerHost ) {
02022 return (0);
02023 }
02024
02025 if (icatServerHost->rcatEnabled != REMOTE_ICAT) {
02026
02027 return 1;
02028 } else {
02029 return 0;
02030 }
02031 }
02032
02033
02034
02035 int
02036 isSameZone (char *zoneHint1, char *zoneHint2)
02037 {
02038 char zoneName1[NAME_LEN], zoneName2[NAME_LEN];
02039
02040 if (zoneHint1 == NULL || zoneHint2 == NULL) return 0;
02041
02042 getZoneNameFromHint (zoneHint1, zoneName1, NAME_LEN);
02043 getZoneNameFromHint (zoneHint2, zoneName2, NAME_LEN);
02044
02045 if (strcmp (zoneName1, zoneName2) == 0)
02046 return 1;
02047 else
02048 return 0;
02049 }
02050
02051 int
02052 getRemoteZoneHost (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
02053 rodsServerHost_t **rodsServerHost, char *remotZoneOpr)
02054 {
02055 int status;
02056 rodsServerHost_t *icatServerHost = NULL;
02057 rodsHostAddr_t *rescAddr = NULL;
02058
02059 status = getRcatHost (MASTER_RCAT, dataObjInp->objPath, &icatServerHost);
02060
02061 if (status < 0 || NULL == icatServerHost ) {
02062 return (status);
02063 }
02064
02065 if (icatServerHost->rcatEnabled != REMOTE_ICAT) {
02066
02067 return (LOCAL_HOST);
02068 }
02069
02070 status = svrToSvrConnect (rsComm, icatServerHost);
02071
02072 if (status < 0) {
02073 rodsLog (LOG_ERROR,
02074 "getRemoteZoneHost: svrToSvrConnect to %s failed, status = %d",
02075 icatServerHost->hostName->name, status);
02076 status = convZoneSockError (status);
02077 return status;
02078 }
02079
02080 addKeyVal (&dataObjInp->condInput, REMOTE_ZONE_OPR_KW, remotZoneOpr);
02081
02082 status = rcGetRemoteZoneResc (icatServerHost->conn, dataObjInp, &rescAddr);
02083 if (status < 0) {
02084 rodsLog (LOG_ERROR,
02085 "getRemoteZoneHost: rcGetRemoteZoneResc for %s failed, status = %d",
02086 dataObjInp->objPath, status);
02087 return status;
02088 }
02089
02090 status = resolveHost (rescAddr, rodsServerHost);
02091
02092 free (rescAddr);
02093
02094 return (status);
02095 }
02096
02097 #if 0 // JMC - UNUSED
02098 int
02099 resolveAndConnHost (rsComm_t *rsComm, rodsHostAddr_t *addr,
02100 rodsServerHost_t **rodsServerHost)
02101 {
02102 int remoteFlag;
02103 int status;
02104
02105 remoteFlag = resolveHost (addr, rodsServerHost);
02106
02107 if (remoteFlag == LOCAL_HOST) {
02108 return LOCAL_HOST;
02109 }
02110
02111 status = svrToSvrConnect (rsComm, *rodsServerHost);
02112
02113 if (status < 0) {
02114 rodsLog (LOG_ERROR,
02115 "resolveAndConnHost: svrToSvrConnect to %s failed",
02116 (*rodsServerHost)->hostName->name);
02117 }
02118 if (status >= 0) {
02119 return (REMOTE_HOST);
02120 } else {
02121 return (status);
02122 }
02123 }
02124 #endif // JMC - UNUSED
02125 int
02126 isLocalHost (char *hostAddr)
02127 {
02128 int remoteFlag;
02129 rodsServerHost_t *rodsServerHost;
02130 rodsHostAddr_t addr;
02131
02132 bzero (&addr, sizeof (addr));
02133 rstrcpy (addr.hostAddr, hostAddr, NAME_LEN);
02134 remoteFlag = resolveHost (&addr, &rodsServerHost);
02135 if (remoteFlag == LOCAL_HOST)
02136 return 1;
02137 else
02138 return 0;
02139 }
02140
02141 int
02142 getReHost (rodsServerHost_t **rodsServerHost)
02143 {
02144 int status;
02145
02146 rodsServerHost_t *tmpRodsServerHost;
02147
02148 tmpRodsServerHost = ServerHostHead;
02149 while (tmpRodsServerHost != NULL) {
02150 if (tmpRodsServerHost->reHostFlag == 1) {
02151 *rodsServerHost = tmpRodsServerHost;
02152 return 0;
02153 }
02154 tmpRodsServerHost = tmpRodsServerHost->next;
02155 }
02156 status = getRcatHost (MASTER_RCAT, NULL, rodsServerHost);
02157
02158 return status;
02159 }
02160
02161 int
02162 getXmsgHost (rodsServerHost_t **rodsServerHost)
02163 {
02164 rodsServerHost_t *tmpRodsServerHost;
02165
02166 tmpRodsServerHost = ServerHostHead;
02167 while (tmpRodsServerHost != NULL) {
02168 if (tmpRodsServerHost->xmsgHostFlag == 1) {
02169 *rodsServerHost = tmpRodsServerHost;
02170 return 0;
02171 }
02172 tmpRodsServerHost = tmpRodsServerHost->next;
02173 }
02174 *rodsServerHost = NULL;
02175
02176 return SYS_INVALID_SERVER_HOST;
02177 }
02178
02179
02180
02181
02182
02183
02184
02185
02186 int
02187 getAndConnReHost (rsComm_t *rsComm, rodsServerHost_t **rodsServerHost)
02188 {
02189 int status;
02190
02191 status = getReHost (rodsServerHost);
02192
02193 if (status < 0) {
02194 rodsLog (LOG_NOTICE,
02195 "getAndConnReHost:getReHost() failed. erro=%d", status);
02196 return (status);
02197 }
02198
02199 if ((*rodsServerHost)->localFlag == LOCAL_HOST) {
02200 return (LOCAL_HOST);
02201 }
02202
02203 status = svrToSvrConnect (rsComm, *rodsServerHost);
02204
02205 if (status < 0) {
02206 rodsLog (LOG_NOTICE,
02207 "getAndConnReHost: svrToSvrConnect to %s failed",
02208 (*rodsServerHost)->hostName->name);
02209 }
02210 if (status >= 0) {
02211 return (REMOTE_HOST);
02212 } else {
02213 return (status);
02214 }
02215 }
02216
02217 int
02218 initConnectControl ()
02219 {
02220 char *conFile;
02221 char *configDir;
02222 FILE *file;
02223 char buf[LONG_NAME_LEN * 5];
02224 int len;
02225 char *bufPtr;
02226 int status;
02227 struct allowedUser *tmpAllowedUser;
02228 int allowUserFlag = 0;
02229 int disallowUserFlag = 0;
02230
02231 configDir = getConfigDir ();
02232 len = strlen (configDir) + strlen(CONNECT_CONTROL_FILE) + 2;
02233 ;
02234
02235 conFile = (char *) malloc(len);
02236
02237 snprintf (conFile, len, "%s/%s", configDir, CONNECT_CONTROL_FILE);
02238 file = fopen(conFile, "r");
02239
02240 if (file == NULL) {
02241 #ifdef DEBUG_CONNECT_CONTROL
02242 fprintf (stderr, "Unable to open CONNECT_CONTROL_FILE file %s\n",
02243 conFile);
02244 #endif
02245 free (conFile);
02246 return (0);
02247 }
02248
02249 free (conFile);
02250
02251 MaxConnections = DEF_MAX_CONNECTION;
02252 freeAllAllowedUser (AllowedUserHead);
02253 freeAllAllowedUser (DisallowedUserHead);
02254 AllowedUserHead = DisallowedUserHead = NULL;
02255 while (fgets (buf, LONG_NAME_LEN * 5, file) != NULL) {
02256 char myuser[NAME_LEN];
02257 char myZone[NAME_LEN];
02258 char myInput[NAME_LEN * 2];
02259
02260 if (*buf == '#')
02261 continue;
02262
02263 bufPtr = buf;
02264
02265 while (copyStrFromBuf (&bufPtr, myInput, NAME_LEN * 2) > 0) {
02266 if (strcmp (myInput, MAX_CONNECTIONS_KW) == 0) {
02267 if (copyStrFromBuf (&bufPtr, myInput, NAME_LEN) > 0) {
02268
02269 if (isdigit (*myInput)) {
02270 MaxConnections = atoi (myInput);
02271 } else {
02272 rodsLog (LOG_ERROR,
02273 "initConnectControl: inp maxConnections %d is not an int",
02274 myInput);
02275 }
02276 break;
02277 }
02278 } else if (strcmp (myInput, ALLOWED_USER_LIST_KW) == 0) {
02279 if (disallowUserFlag == 0) {
02280 allowUserFlag = 1;
02281 break;
02282 } else {
02283 rodsLog (LOG_ERROR,
02284 "initConnectControl: both allowUserList and disallowUserList are set");
02285 return SYS_CONNECT_CONTROL_CONFIG_ERR;
02286 }
02287 } else if (strcmp (myInput, DISALLOWED_USER_LIST_KW) == 0) {
02288 if (allowUserFlag == 0) {
02289 disallowUserFlag = 1;
02290 break;
02291 } else {
02292 rodsLog (LOG_ERROR,
02293 "initConnectControl: both allowUserList and disallowUserList are set");
02294 return SYS_CONNECT_CONTROL_CONFIG_ERR;
02295 }
02296 }
02297 status = parseUserName (myInput, myuser, myZone);
02298 if (status >= 0) {
02299 if (strlen (myZone) == 0) {
02300 zoneInfo_t *tmpZoneInfo;
02301 if (getLocalZoneInfo (&tmpZoneInfo) >= 0) {
02302 rstrcpy (myZone, tmpZoneInfo->zoneName, NAME_LEN);
02303 }
02304 }
02305 tmpAllowedUser = (struct allowedUser*)
02306 malloc (sizeof (struct allowedUser));
02307 memset (tmpAllowedUser, 0, sizeof (struct allowedUser));
02308 rstrcpy (tmpAllowedUser->userName, myuser, NAME_LEN);
02309 rstrcpy (tmpAllowedUser->rodsZone, myZone, NAME_LEN);
02310
02311
02312 if (allowUserFlag != 0) {
02313 queAllowedUser (tmpAllowedUser, &AllowedUserHead);
02314 } else if (disallowUserFlag != 0) {
02315 queAllowedUser (tmpAllowedUser, &DisallowedUserHead);
02316 } else {
02317 rodsLog (LOG_ERROR,
02318 "initConnectControl: neither allowUserList nor disallowUserList has been set");
02319 return SYS_CONNECT_CONTROL_CONFIG_ERR;
02320 }
02321 } else {
02322 rodsLog (LOG_NOTICE,
02323 "initConnectControl: cannot parse input %s. status = %d",
02324 myInput, status);
02325 }
02326 }
02327 }
02328
02329 fclose (file);
02330 return (0);
02331 }
02332
02333 int
02334 chkAllowedUser (char *userName, char *rodsZone)
02335 {
02336 int status;
02337
02338 if (userName == NULL || rodsZone == 0) {
02339 return (SYS_USER_NOT_ALLOWED_TO_CONN);
02340 }
02341
02342 if (strlen (userName) == 0) {
02343
02344 return 0;
02345 }
02346
02347 if (AllowedUserHead != NULL) {
02348 status = matchAllowedUser (userName, rodsZone, AllowedUserHead);
02349 if (status == 1) {
02350 return 0;
02351 } else {
02352 return SYS_USER_NOT_ALLOWED_TO_CONN;
02353 }
02354 } else if (DisallowedUserHead != NULL) {
02355 status = matchAllowedUser (userName, rodsZone, DisallowedUserHead);
02356 if (status == 1) {
02357 return SYS_USER_NOT_ALLOWED_TO_CONN;
02358 } else {
02359 return 0;
02360 }
02361 } else {
02362
02363 return 0;
02364 }
02365 }
02366
02367 int
02368 matchAllowedUser (char *userName, char *rodsZone,
02369 struct allowedUser *allowedUserHead)
02370 {
02371 struct allowedUser *tmpAllowedUser;
02372
02373 if (allowedUserHead == NULL)
02374 return 0;
02375
02376 tmpAllowedUser = allowedUserHead;
02377 while (tmpAllowedUser != NULL) {
02378 if (tmpAllowedUser->userName != NULL &&
02379 strcmp (tmpAllowedUser->userName, userName) == 0 &&
02380 tmpAllowedUser->rodsZone != NULL &&
02381 strcmp (tmpAllowedUser->rodsZone, rodsZone) == 0) {
02382
02383 break;
02384 }
02385 tmpAllowedUser = tmpAllowedUser->next;
02386 }
02387 if (tmpAllowedUser == NULL) {
02388
02389 return (0);
02390 } else {
02391 return (1);
02392 }
02393 }
02394
02395 int
02396 queAllowedUser (struct allowedUser *allowedUser,
02397 struct allowedUser **allowedUserHead)
02398 {
02399 if (allowedUserHead == NULL || allowedUser == NULL)
02400 return USER__NULL_INPUT_ERR;
02401
02402 if (*allowedUserHead == NULL) {
02403 *allowedUserHead = allowedUser;
02404 } else {
02405 allowedUser->next = *allowedUserHead;
02406 *allowedUserHead = allowedUser;
02407 }
02408 return 0;
02409 }
02410
02411 int
02412 freeAllAllowedUser (struct allowedUser *allowedUserHead)
02413 {
02414 struct allowedUser *tmpAllowedUser, *nextAllowedUser;
02415 tmpAllowedUser = allowedUserHead;
02416 while (tmpAllowedUser != NULL) {
02417 nextAllowedUser = tmpAllowedUser->next;
02418 free (tmpAllowedUser);
02419 tmpAllowedUser = nextAllowedUser;
02420 }
02421 return (0);
02422 }
02423
02424 int
02425 initAndClearProcLog ()
02426 {
02427 initProcLog ();
02428 mkdir (ProcLogDir, DEFAULT_DIR_MODE);
02429 rmFilesInDir (ProcLogDir);
02430
02431 return 0;
02432 }
02433
02434 int
02435 initProcLog ()
02436 {
02437 snprintf (ProcLogDir, MAX_NAME_LEN, "%s/%s",
02438 getLogDir(), PROC_LOG_DIR_NAME);
02439 return 0;
02440 }
02441
02442 int
02443 logAgentProc (rsComm_t *rsComm)
02444 {
02445 FILE *fptr;
02446 char procPath[MAX_NAME_LEN];
02447 char *remoteAddr;
02448 char *progName;
02449 char *clientZone, *proxyZone;
02450
02451 if (rsComm->procLogFlag == PROC_LOG_DONE) return 0;
02452
02453 if (*rsComm->clientUser.userName == '\0' ||
02454 *rsComm->proxyUser.userName == '\0')
02455 return 0;
02456
02457 if (*rsComm->clientUser.rodsZone == '\0') {
02458 if ((clientZone = getLocalZoneName ()) == NULL) {
02459 clientZone = "UNKNOWN";
02460 }
02461 } else {
02462 clientZone = rsComm->clientUser.rodsZone;
02463 }
02464
02465 if (*rsComm->proxyUser.rodsZone == '\0') {
02466 if ((proxyZone = getLocalZoneName ()) == NULL) {
02467 proxyZone = "UNKNOWN";
02468 }
02469 } else {
02470 proxyZone = rsComm->proxyUser.rodsZone;
02471 }
02472
02473 remoteAddr = inet_ntoa (rsComm->remoteAddr.sin_addr);
02474 if (remoteAddr == NULL || *remoteAddr == '\0') remoteAddr = "UNKNOWN";
02475 if (*rsComm->option == '\0')
02476 progName = "UNKNOWN";
02477 else
02478 progName = rsComm->option;
02479
02480 snprintf (procPath, MAX_NAME_LEN, "%s/%-d", ProcLogDir, getpid ());
02481
02482 fptr = fopen (procPath, "w");
02483
02484 if (fptr == NULL) {
02485 rodsLog (LOG_ERROR,
02486 "logAgentProc: Cannot open input file %s. ernro = %d",
02487 procPath, errno);
02488 return (UNIX_FILE_OPEN_ERR - errno);
02489 }
02490
02491 fprintf (fptr, "%s %s %s %s %s %s %u\n",
02492 rsComm->proxyUser.userName, clientZone,
02493 rsComm->clientUser.userName, proxyZone,
02494 progName, remoteAddr, (unsigned int) time (0));
02495
02496 rsComm->procLogFlag = PROC_LOG_DONE;
02497 fclose (fptr);
02498 return 0;
02499 }
02500
02501 int
02502 rmProcLog (int pid)
02503 {
02504 char procPath[MAX_NAME_LEN];
02505
02506 snprintf (procPath, MAX_NAME_LEN, "%s/%-d", ProcLogDir, pid);
02507 unlink (procPath);
02508 return 0;
02509 }
02510
02511 int
02512 readProcLog (int pid, procLog_t *procLog)
02513 {
02514 FILE *fptr;
02515 char procPath[MAX_NAME_LEN];
02516 int status;
02517
02518 if (procLog == NULL) return USER__NULL_INPUT_ERR;
02519
02520 snprintf (procPath, MAX_NAME_LEN, "%s/%-d", ProcLogDir, pid);
02521
02522 fptr = fopen (procPath, "r");
02523
02524 if (fptr == NULL) {
02525 rodsLog (LOG_ERROR,
02526 "readProcLog: Cannot open input file %s. ernro = %d",
02527 procPath, errno);
02528 return (UNIX_FILE_OPEN_ERR - errno);
02529 }
02530
02531 procLog->pid = pid;
02532
02533 status = fscanf (fptr, "%s %s %s %s %s %s %u",
02534 procLog->clientName, procLog->clientZone,
02535 procLog->proxyName, procLog->proxyZone,
02536 procLog->progName, procLog->remoteAddr, &procLog->startTime);
02537
02538 if (status == 7) {
02539 status = 0;
02540 } else {
02541 rodsLog (LOG_ERROR,
02542 "readProcLog: error fscanf file %s. Number of param read = %d",
02543 procPath, status);
02544 status = UNIX_FILE_READ_ERR;
02545 }
02546 fclose (fptr);
02547 return status;
02548 }
02549
02550 int
02551 setRsCommFromRodsEnv (rsComm_t *rsComm)
02552 {
02553 rodsEnv *myEnv = &rsComm->myEnv;
02554
02555 rstrcpy (rsComm->proxyUser.userName, myEnv->rodsUserName, NAME_LEN);
02556 rstrcpy (rsComm->clientUser.userName, myEnv->rodsUserName, NAME_LEN);
02557
02558 rstrcpy (rsComm->proxyUser.rodsZone, myEnv->rodsZone, NAME_LEN);
02559 rstrcpy (rsComm->clientUser.rodsZone, myEnv->rodsZone, NAME_LEN);
02560
02561 return (0);
02562 }
02563
02564 int
02565 queAgentProc (agentProc_t *agentProc, agentProc_t **agentProcHead,
02566 irodsPosition_t position)
02567 {
02568 if (agentProc == NULL || agentProcHead == NULL) return USER__NULL_INPUT_ERR;
02569
02570 if (*agentProcHead == NULL) {
02571 *agentProcHead = agentProc;
02572 agentProc->next = NULL;
02573 return 0;
02574 }
02575
02576 if (position == TOP_POS) {
02577 agentProc->next = *agentProcHead;
02578 *agentProcHead = agentProc;
02579 } else {
02580 agentProc_t *tmpAgentProc = *agentProcHead;
02581 while (tmpAgentProc->next != NULL) {
02582 tmpAgentProc = tmpAgentProc->next;
02583 }
02584 tmpAgentProc->next = agentProc;
02585 agentProc->next = NULL;
02586 }
02587 return 0;
02588 }
02589
02590
02591
02592 int
02593 purgeLockFileDir (int chkLockFlag)
02594 {
02595 char lockFileDir[MAX_NAME_LEN];
02596 char lockFilePath[MAX_NAME_LEN*2];
02597 DIR *dirPtr;
02598 struct dirent *myDirent;
02599 struct stat statbuf;
02600 int status;
02601 int savedStatus = 0;
02602 struct flock myflock;
02603 uint purgeTime;
02604
02605 snprintf (lockFileDir, MAX_NAME_LEN, "%-s/%-s", getConfigDir(),
02606 LOCK_FILE_DIR);
02607
02608 dirPtr = opendir (lockFileDir);
02609 if (dirPtr == NULL) {
02610 rodsLog (LOG_ERROR,
02611 "purgeLockFileDir: opendir error for %s, errno = %d",
02612 lockFileDir, errno);
02613 return (UNIX_FILE_OPENDIR_ERR - errno);
02614 }
02615 bzero (&myflock, sizeof (myflock));
02616 myflock.l_whence = SEEK_SET;
02617 purgeTime = time (0) - LOCK_FILE_PURGE_TIME;
02618 while ((myDirent = readdir (dirPtr)) != NULL) {
02619 if (strcmp (myDirent->d_name, ".") == 0 ||
02620 strcmp (myDirent->d_name, "..") == 0) {
02621 continue;
02622 }
02623 snprintf (lockFilePath, MAX_NAME_LEN, "%-s/%-s",
02624 lockFileDir, myDirent->d_name);
02625 status = stat (lockFilePath, &statbuf);
02626
02627 if (status != 0) {
02628 rodsLog (LOG_ERROR,
02629 "purgeLockFileDir: stat error for %s, errno = %d",
02630 lockFilePath, errno);
02631 savedStatus = UNIX_FILE_STAT_ERR - errno;
02632 unlink (lockFilePath);
02633 continue;
02634 }
02635 if ((statbuf.st_mode & S_IFREG) == 0) continue;
02636 if (chkLockFlag) {
02637 int myFd;
02638 if ((int)purgeTime < statbuf.st_mtime) continue;
02639 myFd = open (lockFilePath, O_RDWR | O_CREAT, 0644);
02640 if (myFd < 0) {
02641 savedStatus = FILE_OPEN_ERR - errno;
02642 rodsLogError (LOG_ERROR, savedStatus,
02643 "purgeLockFileDir: open error for %s", lockFilePath);
02644 continue;
02645 }
02646 myflock.l_type = F_WRLCK;
02647 fcntl (myFd, F_GETLK, &myflock);
02648 close (myFd);
02649
02650 if (myflock.l_type != F_UNLCK) continue;
02651 }
02652 unlink (lockFilePath);
02653 }
02654 closedir (dirPtr);
02655
02656 return savedStatus;
02657 }
02658
02659