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