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