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