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