00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "eirods_error.h"
00022 #include "eirods_zone_info.h"
00023 #include "eirods_sql_logger.h"
00024 #include "eirods_string_tokenize.h"
00025 #include "eirods_log.h"
00026 #include "eirods_tmp_string.h"
00027 #include "eirods_children_parser.h"
00028 #include "eirods_stacktrace.h"
00029 #include "eirods_hierarchy_parser.h"
00030 #include "eirods_catalog_properties.h"
00031 #include "eirods_kvp_string_parser.h"
00032 #include "eirods_auth_object.h"
00033 #include "eirods_auth_factory.h"
00034 #include "eirods_auth_plugin.h"
00035 #include "eirods_auth_manager.h"
00036 #include "eirods_auth_constants.h"
00037
00038
00039
00040 #include "rods.h"
00041 #include "rcMisc.h"
00042 #include "icatMidLevelRoutines.h"
00043 #include "icatMidLevelHelpers.h"
00044 #include "icatHighLevelRoutines.h"
00045 #include "icatLowLevel.h"
00046
00047
00048
00049 #include <string>
00050 #include <iostream>
00051 #include <sstream>
00052 #include <vector>
00053 #include <boost/regex.hpp>
00054
00055 extern int get64RandomBytes(char *buf);
00056
00057 static char prevChalSig[200];
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #define AP_READ "read"
00086 #define AP_WRITE "write"
00087 #define AP_OWN "own"
00088 #define AP_NULL "null"
00089
00090 #define MAX_PASSWORDS 40
00091
00092
00093
00094
00095
00096
00097
00098 #define TEMP_PASSWORD_TIME 120
00099 #define TEMP_PASSWORD_MAX_TIME 1000
00100
00101
00102 #if 0
00103
00104
00105
00106
00107
00108
00109
00110
00111 #define IRODS_PAM_PASSWORD_LEN 20
00112
00113
00114
00115
00116 #define IRODS_PAM_PASSWORD_MIN_TIME "121"
00117 #define IRODS_PAM_PASSWORD_MAX_TIME "1209600"
00118 #ifdef PAM_AUTH_NO_EXTEND
00119 #define IRODS_PAM_PASSWORD_DEFAULT_TIME "28800"
00120 #else
00121 #define IRODS_PAM_PASSWORD_DEFAULT_TIME "1209600"
00122 #endif
00123 #endif
00124
00125 #define PASSWORD_SCRAMBLE_PREFIX ".E_"
00126 #define PASSWORD_KEY_ENV_VAR "irodsPKey"
00127 #define PASSWORD_DEFAULT_KEY "a9_3fker"
00128
00129 #define MAX_HOST_STR 2700
00130
00131
00132
00133 bool eirods_pam_auth_no_extend = false;
00134 size_t eirods_pam_password_len = 20;
00135 char eirods_pam_password_min_time[ NAME_LEN ] = { "121" };
00136 char eirods_pam_password_max_time[ NAME_LEN ] = { "1209600" };
00137 char eirods_pam_password_default_time[ NAME_LEN ] = { "1209600" };
00138
00139
00140
00141 int logSQL=0;
00142
00143 static int _delColl(rsComm_t *rsComm, collInfo_t *collInfo);
00144 static int removeAVUs();
00145
00146 icatSessionStruct icss={0};
00147 char localZone[MAX_NAME_LEN]={""};
00148
00149 int creatingUserByGroupAdmin=0;
00150
00151
00152
00153
00154
00155
00156
00157 int chlDebug(char *debugMode) {
00158 if (strstr(debugMode, "SQL")) {
00159 logSQL=1;
00160 chlDebugGenQuery(1);
00161 chlDebugGenUpdate(1);
00162 cmlDebug(2);
00163 }
00164 else {
00165 if (strstr(debugMode, "sql")) {
00166 logSQL=1;
00167 chlDebugGenQuery(1);
00168 chlDebugGenUpdate(1);
00169 cmlDebug(1);
00170 }
00171 else {
00172 logSQL=0;
00173 chlDebugGenQuery(0);
00174 chlDebugGenUpdate(0);
00175 cmlDebug(0);
00176 }
00177 }
00178 return(0);
00179 }
00180
00181
00182
00183
00184
00185 static int
00186 icatDescramble(char *pw) {
00187 char *cp1, *cp2, *cp3;
00188 int i,len;
00189 char pw2[MAX_PASSWORD_LEN+10];
00190 char unscrambled[MAX_PASSWORD_LEN+10];
00191
00192 len = strlen(PASSWORD_SCRAMBLE_PREFIX);
00193 cp1=pw;
00194 cp2=PASSWORD_SCRAMBLE_PREFIX;
00195 for (i=0;i<len;i++) {
00196 if (*cp1++ != *cp2++) {
00197 return 0;
00198 }
00199 }
00200 strncpy(pw2, cp1, MAX_PASSWORD_LEN);
00201 cp3 = getenv(PASSWORD_KEY_ENV_VAR);
00202 if (cp3==NULL) {
00203 cp3 = PASSWORD_DEFAULT_KEY;
00204 }
00205 obfDecodeByKey(pw2, cp3, unscrambled);
00206 strncpy(pw, unscrambled, MAX_PASSWORD_LEN);
00207
00208 return 0;
00209 }
00210
00211
00212
00213
00214
00215 static int
00216 icatScramble(char *pw) {
00217 char *cp1;
00218 char newPw[MAX_PASSWORD_LEN+10];
00219 char scrambled[MAX_PASSWORD_LEN+10];
00220
00221 cp1 = getenv(PASSWORD_KEY_ENV_VAR);
00222 if (cp1==NULL) {
00223 cp1 = PASSWORD_DEFAULT_KEY;
00224 }
00225 obfEncodeByKey(pw, cp1, scrambled);
00226 strncpy(newPw, PASSWORD_SCRAMBLE_PREFIX, MAX_PASSWORD_LEN);
00227 strncat(newPw, scrambled, MAX_PASSWORD_LEN);
00228 strncpy(pw, newPw, MAX_PASSWORD_LEN);
00229 return 0;
00230 }
00231
00232
00233
00234
00235
00236 int chlOpen(
00237 rodsServerConfig* _config ) {
00238
00239 int i;
00240 if (logSQL!=0) rodsLog(LOG_SQL, "chlOpen");
00241 strncpy( icss.databaseUsername, _config->DBUsername, DB_USERNAME_LEN );
00242 strncpy( icss.databasePassword, _config->DBPassword, DB_PASSWORD_LEN );
00243 i = cmlOpen(&icss);
00244 if (i != 0) {
00245 rodsLog(LOG_NOTICE, "chlOpen cmlOpen failure %d",i);
00246 }
00247 else {
00248 icss.status=1;
00249
00250
00251 eirods::catalog_properties::getInstance().capture();
00252 }
00253
00254
00255
00256 eirods_pam_auth_no_extend = _config->eirods_pam_auth_no_extend;
00257 eirods_pam_password_len = _config->eirods_pam_password_len;
00258 strncpy(
00259 eirods_pam_password_min_time,
00260 _config->eirods_pam_password_min_time,
00261 NAME_LEN );
00262 strncpy(
00263 eirods_pam_password_max_time,
00264 _config->eirods_pam_password_max_time,
00265 NAME_LEN );
00266 if( eirods_pam_auth_no_extend ) {
00267 strncpy(
00268 eirods_pam_password_default_time,
00269 "28800",
00270 NAME_LEN );
00271 }
00272
00273 return(i);
00274 }
00275
00276
00277
00278
00279
00280 int chlClose() {
00281 int i;
00282
00283 i = cmlClose(&icss);
00284 if (i == 0) icss.status=0;
00285 return(i);
00286 }
00287
00288 int chlIsConnected() {
00289 if (logSQL!=0) rodsLog(LOG_SQL, "chlIsConnected");
00290 return(icss.status);
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 icatSessionStruct *
00301 chlGetRcs()
00302 {
00303 if (logSQL!=0) rodsLog(LOG_SQL, "chlGetRcs");
00304 if (icss.status != 1) {
00305 return(NULL);
00306 }
00307 return(&icss);
00308 }
00309
00310
00311
00312
00313 int
00314 _rollback(const char *functionName) {
00315 int status;
00316 #if ORA_ICAT
00317 status = 0;
00318 #else
00319
00320
00321 status = cmlExecuteNoAnswerSql("rollback", &icss);
00322 if (status == 0) {
00323 rodsLog(LOG_NOTICE,
00324 "%s cmlExecuteNoAnswerSql(rollback) succeeded", functionName);
00325 }
00326 else {
00327 rodsLog(LOG_NOTICE,
00328 "%s cmlExecuteNoAnswerSql(rollback) failure %d",
00329 functionName, status);
00330 }
00331 #endif
00332
00333 return(status);
00334 }
00335
00336
00337
00338
00339
00340
00341
00342 int
00343 getLocalZone()
00344 {
00345 std::string local_zone;
00346 eirods::error e = eirods::zone_info::get_instance()->get_local_zone(icss, logSQL, local_zone);
00347 strncpy(localZone, local_zone.c_str(), MAX_NAME_LEN);
00348 return e.code();
00349 }
00350
00351
00352
00353
00354
00355 char *
00356 chlGetLocalZone() {
00357 getLocalZone();
00358 return(localZone);
00359 }
00360
00361
00362
00363
00364 static int
00365 _updateRescObjCount(
00366 const std::string& _resc_name,
00367 const std::string& _zone,
00368 int _amount) {
00369
00370 int result = 0;
00371 int status;
00372 char resc_id[MAX_NAME_LEN];
00373 char myTime[50];
00374 eirods::sql_logger logger(__FUNCTION__, logSQL);
00375 eirods::hierarchy_parser hparse;
00376
00377 resc_id[0] = '\0';
00378
00379 std::stringstream ss;
00380 if((status = cmlGetStringValueFromSql("select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
00381 resc_id, MAX_NAME_LEN, _resc_name.c_str(), _zone.c_str(), 0,
00382 &icss)) != 0) {
00383 if(status == CAT_NO_ROWS_FOUND) {
00384 result = CAT_INVALID_RESOURCE;
00385 } else {
00386 _rollback(__FUNCTION__);
00387 result = status;
00388 }
00389 } else {
00390 std::stringstream ss;
00391 ss << "update R_RESC_MAIN set resc_objcount=resc_objcount+";
00392 ss << _amount;
00393 ss << ", modify_ts=? where resc_id=?";
00394 getNowStr(myTime);
00395 cllBindVarCount = 0;
00396 cllBindVars[cllBindVarCount++] = myTime;
00397 cllBindVars[cllBindVarCount++] = resc_id;
00398 if((status = cmlExecuteNoAnswerSql(ss.str().c_str(), &icss)) != 0) {
00399 std::stringstream ss;
00400 ss << __FUNCTION__ << " cmlExecuteNoAnswerSql update failure " << status;
00401 eirods::log(LOG_NOTICE, ss.str());
00402 _rollback(__FUNCTION__);
00403 result = status;
00404 }
00405 }
00406 return result;
00407 }
00408
00409
00410
00411
00412 static int
00413 _updateObjCountOfResources(
00414 const std::string _resc_hier,
00415 const std::string _zone,
00416 int _amount) {
00417 int result = 0;
00418 eirods::hierarchy_parser hparse;
00419
00420 hparse.set_string(_resc_hier);
00421 for(eirods::hierarchy_parser::const_iterator it = hparse.begin();
00422 result == 0 && it != hparse.end(); ++it) {
00423 result = _updateRescObjCount(*it, _zone, _amount);
00424 }
00425 return result;
00426 }
00427
00428
00429
00430
00431 int
00432 chlUpdateRescObjCount(
00433 const std::string& _resc,
00434 int _delta) {
00435
00436 int result = 0;
00437 int ret;
00438 if((ret = getLocalZone()) != 0) {
00439 std::stringstream msg;
00440 msg << __FUNCTION__ << " - Failed to set the local zone.";
00441 eirods::log(LOG_ERROR, msg.str());
00442 result = ret;
00443 } else if((ret = _updateRescObjCount(_resc, localZone, _delta)) != 0) {
00444 std::stringstream msg;
00445 msg << __FUNCTION__ << " - Failed to update the object count for resource \"" << _resc << "\"";
00446 eirods::log(LOG_ERROR, msg.str());
00447 result = ret;
00448 }
00449
00450 return result;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 int chlModDataObjMeta(rsComm_t *rsComm, dataObjInfo_t *dataObjInfo,
00467 keyValPair_t *regParam) {
00468 int i, j, status, upCols;
00469 rodsLong_t iVal = 0;
00470 int status2;
00471
00472 int mode=0;
00473
00474 char logicalFileName[MAX_NAME_LEN];
00475 char logicalDirName[MAX_NAME_LEN];
00476 char *theVal;
00477 char replNum1[MAX_NAME_LEN];
00478
00479 char *whereColsAndConds[10];
00480 char *whereValues[10];
00481 char idVal[MAX_NAME_LEN];
00482 int numConditions;
00483 char oldCopy[NAME_LEN];
00484 char newCopy[NAME_LEN];
00485 int adminMode;
00486
00487 int maxCols=90;
00488 char *updateCols[90];
00489 char *updateVals[90];
00490
00491
00492
00493
00494 int dataTypeIndex=1;
00495
00496 char *regParamNames[]={
00497 REPL_NUM_KW, DATA_TYPE_KW, DATA_SIZE_KW,
00498 RESC_NAME_KW,FILE_PATH_KW, DATA_OWNER_KW, DATA_OWNER_ZONE_KW,
00499 REPL_STATUS_KW, CHKSUM_KW, DATA_EXPIRY_KW,
00500 DATA_COMMENTS_KW, DATA_CREATE_KW, DATA_MODIFY_KW, RESC_GROUP_NAME_KW,
00501 DATA_MODE_KW, RESC_HIER_STR_KW, "END"
00502 };
00503
00504
00505
00506 char *colNames[]={
00507 "data_repl_num", "data_type_name", "data_size",
00508 "resc_name", "data_path", "data_owner_name", "data_owner_zone",
00509 "data_is_dirty", "data_checksum", "data_expiry_ts",
00510 "r_comment", "create_ts", "modify_ts", "resc_group_name",
00511 "data_mode", "resc_hier"
00512 };
00513 int DATA_EXPIRY_TS_IX=9;
00514 int MODIFY_TS_IX=12;
00515
00516 char objIdString[MAX_NAME_LEN];
00517 char *neededAccess;
00518
00519 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta");
00520
00521 if (regParam == NULL || dataObjInfo == NULL) {
00522 return (CAT_INVALID_ARGUMENT);
00523 }
00524
00525 adminMode=0;
00526 theVal = getValByKey(regParam, IRODS_ADMIN_KW);
00527 if (theVal != NULL) {
00528 adminMode=1;
00529 }
00530
00531
00532 for (i=0, j=0; i<maxCols; i++) {
00533 if (strcmp(regParamNames[i],"END")==0) break;
00534 theVal = getValByKey(regParam, regParamNames[i]);
00535 if (theVal != NULL) {
00536 updateCols[j]=colNames[i];
00537 updateVals[j]=theVal;
00538 if (i==DATA_EXPIRY_TS_IX) {
00539
00540
00541 if (strcmp(colNames[i],"data_expiry_ts") == 0) {
00542 if (strlen(theVal) < 11) {
00543 static char theVal2[20];
00544 time_t myTimeValue;
00545 myTimeValue=atoll(theVal);
00546 snprintf(theVal2, sizeof theVal2, "%011d", (int)myTimeValue);
00547 updateVals[j]=theVal2;
00548 }
00549 }
00550 }
00551
00552 if (i==MODIFY_TS_IX) {
00553
00554
00555 if (strcmp(colNames[i],"modify_ts") == 0) {
00556 if (strlen(theVal) < 11) {
00557 static char theVal3[20];
00558 time_t myTimeValue;
00559 myTimeValue=atoll(theVal);
00560 snprintf(theVal3, sizeof theVal3, "%011d", (int)myTimeValue);
00561 updateVals[j]=theVal3;
00562 }
00563 }
00564 }
00565
00566 j++;
00567
00568
00569 if (i==dataTypeIndex) {
00570 status = cmlCheckNameToken("data_type",
00571 theVal, &icss);
00572 if (status !=0 ) {
00573 std::stringstream msg;
00574 msg << __FUNCTION__;
00575 msg << " - Invalid data type specified.";
00576 addRErrorMsg (&rsComm->rError, 0, msg.str().c_str());
00577 return(CAT_INVALID_DATA_TYPE);
00578 }
00579 }
00580 }
00581 }
00582 upCols=j;
00583
00584
00585
00586
00587
00588
00589
00590 neededAccess = ACCESS_MODIFY_METADATA;
00591 if (upCols==1 && strcmp(updateCols[0],"chksum")==0) {
00592 neededAccess = ACCESS_READ_OBJECT;
00593 }
00594
00595
00596
00597 theVal = getValByKey(regParam, DATA_EXPIRY_KW);
00598 if (theVal != NULL) {
00599 neededAccess = ACCESS_DELETE_OBJECT;
00600 }
00601
00602 if (dataObjInfo->dataId <= 0) {
00603 status = splitPathByKey(dataObjInfo->objPath,
00604 logicalDirName, logicalFileName, '/');
00605
00606 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta SQL 1 ");
00607 status = cmlGetIntegerValueFromSql(
00608 "select coll_id from R_COLL_MAIN where coll_name=?", &iVal,
00609 logicalDirName, 0, 0, 0, 0, &icss);
00610
00611 if (status != 0) {
00612 char errMsg[105];
00613 snprintf(errMsg, 100, "collection '%s' is unknown",
00614 logicalDirName);
00615 addRErrorMsg (&rsComm->rError, 0, errMsg);
00616 _rollback("chlModDataObjMeta");
00617 return(CAT_UNKNOWN_COLLECTION);
00618 }
00619 snprintf(objIdString, MAX_NAME_LEN, "%lld", iVal);
00620
00621 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta SQL 2");
00622 status = cmlGetIntegerValueFromSql(
00623 "select data_id from R_DATA_MAIN where coll_id=? and data_name=?",
00624 &iVal, objIdString, logicalFileName, 0, 0, 0, &icss);
00625 if (status != 0) {
00626 std::stringstream msg;
00627 msg << __FUNCTION__;
00628 msg << " - Failed to find file in database by its logical path.";
00629 addRErrorMsg (&rsComm->rError, 0, msg.str().c_str());
00630 _rollback("chlModDataObjMeta");
00631 return(CAT_UNKNOWN_FILE);
00632 }
00633
00634 dataObjInfo->dataId = iVal;
00635
00636 }
00637
00638 snprintf(objIdString, MAX_NAME_LEN, "%lld", dataObjInfo->dataId);
00639
00640 if (adminMode) {
00641 if (rsComm->clientUser.authInfo.authFlag != LOCAL_PRIV_USER_AUTH) {
00642 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
00643 }
00644 }
00645 else {
00646 status = cmlCheckDataObjId(objIdString, rsComm->clientUser.userName,
00647 rsComm->clientUser.rodsZone, neededAccess, &icss);
00648 if (status != 0) {
00649 theVal = getValByKey(regParam, ACL_COLLECTION_KW);
00650 if (theVal != NULL && dataObjInfo->objPath != NULL &&
00651 upCols==1 && strcmp(updateCols[0],"data_path")==0) {
00652 int len, iVal = 0;
00653
00654
00655
00656
00657 len = strlen(theVal);
00658 if (strncmp(theVal, dataObjInfo->objPath, len) == 0) {
00659
00660 iVal = cmlCheckDir(theVal,
00661 rsComm->clientUser.userName,
00662 rsComm->clientUser.rodsZone,
00663 ACCESS_OWN,
00664 &icss);
00665 }
00666 if (iVal > 0) status=0;
00667
00668 }
00669 if (status) {
00670 _rollback("chlModDataObjMeta");
00671 return(CAT_NO_ACCESS_PERMISSION);
00672 }
00673 }
00674 }
00675
00676 whereColsAndConds[0]="data_id=";
00677 snprintf(idVal, MAX_NAME_LEN, "%lld", dataObjInfo->dataId);
00678 whereValues[0]=idVal;
00679 numConditions=1;
00680
00681
00682
00683
00684
00685
00686 if (getValByKey(regParam, ALL_KW) == NULL) {
00687 j = numConditions;
00688 whereColsAndConds[j]="data_repl_num=";
00689 snprintf(replNum1, MAX_NAME_LEN, "%d", dataObjInfo->replNum);
00690 whereValues[j]=replNum1;
00691 numConditions++;
00692 }
00693
00694 mode =0;
00695 if (getValByKey(regParam, ALL_REPL_STATUS_KW)) {
00696 mode=1;
00697
00698 }
00699
00700 status = getLocalZone();
00701 if( status < 0 ) {
00702 rodsLog( LOG_ERROR, "chlModObjMeta - failed in getLocalZone with status [%d]", status );
00703 return status;
00704 }
00705
00706
00707
00708 char* new_resc_hier = getValByKey(regParam, RESC_HIER_STR_KW);
00709 if(new_resc_hier != NULL) {
00710 std::stringstream id_stream;
00711 id_stream << dataObjInfo->dataId;
00712 std::stringstream repl_stream;
00713 repl_stream << dataObjInfo->replNum;
00714 char resc_hier[MAX_NAME_LEN];
00715 if((status = cmlGetStringValueFromSql("select resc_hier from R_DATA_MAIN where data_id=? and data_repl_num=?",
00716 resc_hier, MAX_NAME_LEN, id_stream.str().c_str(), repl_stream.str().c_str(),
00717 0, &icss)) != 0) {
00718 std::stringstream msg;
00719 msg << __FUNCTION__;
00720 msg << " - Failed to get the resc hierarchy from object with id: ";
00721 msg << id_stream.str();
00722 msg << " and replNum: ";
00723 msg << repl_stream.str();
00724 eirods::log(LOG_NOTICE, msg.str());
00725 return status;
00726 }
00727
00728 else if((status = _updateObjCountOfResources(resc_hier, localZone, -1)) != 0) {
00729 return status;
00730 } else if((status = _updateObjCountOfResources(new_resc_hier, localZone, +1)) != 0) {
00731 return status;
00732 }
00733 }
00734
00735 if (mode == 0) {
00736 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta SQL 4");
00737 status = cmlModifySingleTable("R_DATA_MAIN", updateCols, updateVals,
00738 whereColsAndConds, whereValues, upCols,
00739 numConditions, &icss);
00740 } else {
00741
00742 j = upCols;
00743 updateCols[j]="data_is_dirty";
00744 snprintf(newCopy, NAME_LEN, "%d", NEWLY_CREATED_COPY);
00745 updateVals[j]=newCopy;
00746 upCols++;
00747 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta SQL 5");
00748 status = cmlModifySingleTable("R_DATA_MAIN", updateCols, updateVals,
00749 whereColsAndConds, whereValues, upCols,
00750 numConditions, &icss);
00751 if (status == 0) {
00752 j = numConditions-1;
00753 whereColsAndConds[j]="data_repl_num!=";
00754 snprintf(replNum1, MAX_NAME_LEN, "%d", dataObjInfo->replNum);
00755 whereValues[j]=replNum1;
00756
00757 updateCols[0]="data_is_dirty";
00758 snprintf(oldCopy, NAME_LEN, "%d", OLD_COPY);
00759 updateVals[0]=oldCopy;
00760 if (logSQL!=0) rodsLog(LOG_SQL, "chlModDataObjMeta SQL 6");
00761 status2 = cmlModifySingleTable("R_DATA_MAIN", updateCols, updateVals,
00762 whereColsAndConds, whereValues, 1,
00763 numConditions, &icss);
00764
00765 if (status2 != 0 && status2 != CAT_SUCCESS_BUT_WITH_NO_INFO) {
00766
00767 rodsLog(LOG_NOTICE,
00768 "chlModDataObjMeta cmlModifySingleTable failure for other replicas %d",
00769 status2);
00770 _rollback("chlModDataObjMeta");
00771 return(status2);
00772 }
00773
00774 }
00775 }
00776 if (status != 0) {
00777 _rollback("chlModDataObjMeta");
00778 rodsLog(LOG_NOTICE,
00779 "chlModDataObjMeta cmlModifySingleTable failure %d",
00780 status);
00781 return(status);
00782 }
00783
00784 if ( !(dataObjInfo->flags & NO_COMMIT_FLAG) ) {
00785 status = cmlExecuteNoAnswerSql("commit", &icss);
00786 if (status != 0) {
00787 rodsLog(LOG_NOTICE,
00788 "chlModDataObjMeta cmlExecuteNoAnswerSql commit failure %d",
00789 status);
00790 return(status);
00791 }
00792 }
00793 return status;
00794 }
00795
00796
00797
00798
00799
00800
00801 int chlRegDataObj(rsComm_t *rsComm, dataObjInfo_t *dataObjInfo) {
00802 char myTime[50];
00803 char logicalFileName[MAX_NAME_LEN];
00804 char logicalDirName[MAX_NAME_LEN];
00805 rodsLong_t seqNum;
00806 rodsLong_t iVal;
00807 char dataIdNum[MAX_NAME_LEN];
00808 char collIdNum[MAX_NAME_LEN];
00809 char dataReplNum[MAX_NAME_LEN];
00810 char dataSizeNum[MAX_NAME_LEN];
00811 char dataStatusNum[MAX_NAME_LEN];
00812 int status;
00813 int inheritFlag;
00814
00815 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj");
00816 if (!icss.status) {
00817 return(CATALOG_NOT_CONNECTED);
00818 }
00819
00820 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 1 ");
00821 seqNum = cmlGetNextSeqVal(&icss);
00822 if (seqNum < 0) {
00823 rodsLog(LOG_NOTICE, "chlRegDataObj cmlGetNextSeqVal failure %d",
00824 seqNum);
00825 _rollback("chlRegDataObj");
00826 return(seqNum);
00827 }
00828 snprintf(dataIdNum, MAX_NAME_LEN, "%lld", seqNum);
00829 dataObjInfo->dataId=seqNum;
00830
00831 status = splitPathByKey(dataObjInfo->objPath,
00832 logicalDirName, logicalFileName, '/');
00833
00834
00835
00836
00837 iVal = cmlCheckDirAndGetInheritFlag(logicalDirName,
00838 rsComm->clientUser.userName,
00839 rsComm->clientUser.rodsZone,
00840 ACCESS_MODIFY_OBJECT, &inheritFlag, &icss);
00841 if (iVal < 0) {
00842 char errMsg[105];
00843 if (iVal==CAT_UNKNOWN_COLLECTION) {
00844 snprintf(errMsg, 100, "collection '%s' is unknown",
00845 logicalDirName);
00846 addRErrorMsg (&rsComm->rError, 0, errMsg);
00847 }
00848 if (iVal==CAT_NO_ACCESS_PERMISSION) {
00849 snprintf(errMsg, 100, "no permission to update collection '%s'",
00850 logicalDirName);
00851 addRErrorMsg (&rsComm->rError, 0, errMsg);
00852 }
00853 return (iVal);
00854 }
00855 snprintf(collIdNum, MAX_NAME_LEN, "%lld", iVal);
00856
00857
00858 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 4");
00859 status = cmlGetIntegerValueFromSql(
00860 "select coll_id from R_COLL_MAIN where coll_name=?",
00861 &iVal,
00862 dataObjInfo->objPath, 0, 0, 0, 0, &icss);
00863 if (status == 0) {
00864 return(CAT_NAME_EXISTS_AS_COLLECTION);
00865 }
00866
00867 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 5");
00868 status = cmlCheckNameToken("data_type",
00869 dataObjInfo->dataType, &icss);
00870 if (status !=0 ) {
00871 return(CAT_INVALID_DATA_TYPE);
00872 }
00873
00874 snprintf(dataReplNum, MAX_NAME_LEN, "%d", dataObjInfo->replNum);
00875 snprintf(dataStatusNum, MAX_NAME_LEN, "%d", dataObjInfo->replStatus);
00876 snprintf(dataSizeNum, MAX_NAME_LEN, "%lld", dataObjInfo->dataSize);
00877 getNowStr(myTime);
00878 cllBindVars[0]=dataIdNum;
00879 cllBindVars[1]=collIdNum;
00880 cllBindVars[2]=logicalFileName;
00881 cllBindVars[3]=dataReplNum;
00882 cllBindVars[4]=dataObjInfo->version;
00883 cllBindVars[5]=dataObjInfo->dataType;
00884 cllBindVars[6]=dataSizeNum;
00885 cllBindVars[7]=dataObjInfo->rescGroupName;
00886 cllBindVars[8]=dataObjInfo->rescName;
00887 cllBindVars[9]=dataObjInfo->rescHier;
00888 cllBindVars[10]=dataObjInfo->filePath;
00889 cllBindVars[11]=rsComm->clientUser.userName;
00890 cllBindVars[12]=rsComm->clientUser.rodsZone;
00891 cllBindVars[13]=dataStatusNum;
00892 cllBindVars[14]=dataObjInfo->chksum;
00893 cllBindVars[15]=dataObjInfo->dataMode;
00894 cllBindVars[16]=myTime;
00895 cllBindVars[17]=myTime;
00896 cllBindVarCount=18;
00897 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 6");
00898 status = cmlExecuteNoAnswerSql(
00899 "insert into R_DATA_MAIN (data_id, coll_id, data_name, data_repl_num, data_version, data_type_name, data_size, resc_group_name, resc_name, resc_hier, data_path, data_owner_name, data_owner_zone, data_is_dirty, data_checksum, data_mode, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
00900 &icss);
00901 if (status != 0) {
00902 rodsLog(LOG_NOTICE,
00903 "chlRegDataObj cmlExecuteNoAnswerSql failure %d",status);
00904 _rollback("chlRegDataObj");
00905 return(status);
00906 }
00907
00908 status = getLocalZone();
00909 if( status < 0 ) {
00910 rodsLog( LOG_ERROR, "chlRegDataInfo - failed in getLocalZone with status [%d]", status );
00911 return status;
00912 }
00913
00914 if((status = _updateObjCountOfResources( dataObjInfo->rescHier, localZone, 1)) != 0) {
00915 return status;
00916 }
00917
00918 if (inheritFlag) {
00919
00920
00921 cllBindVars[0]=dataIdNum;
00922 cllBindVars[1]=myTime;
00923 cllBindVars[2]=myTime;
00924 cllBindVars[3]=collIdNum;
00925 cllBindVarCount=4;
00926 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 7");
00927 status = cmlExecuteNoAnswerSql(
00928 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select ?, user_id, access_type_id, ?, ? from R_OBJT_ACCESS where object_id = ?)",
00929 &icss);
00930 if (status != 0) {
00931 rodsLog(LOG_NOTICE,
00932 "chlRegDataObj cmlExecuteNoAnswerSql insert access failure %d",
00933 status);
00934 _rollback("chlRegDataObj");
00935 return(status);
00936 }
00937 }
00938 else {
00939 cllBindVars[0]=dataIdNum;
00940 cllBindVars[1]=rsComm->clientUser.userName;
00941 cllBindVars[2]=rsComm->clientUser.rodsZone;
00942 cllBindVars[3]=ACCESS_OWN;
00943 cllBindVars[4]=myTime;
00944 cllBindVars[5]=myTime;
00945 cllBindVarCount=6;
00946 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegDataObj SQL 8");
00947 status = cmlExecuteNoAnswerSql(
00948 "insert into R_OBJT_ACCESS values (?, (select user_id from R_USER_MAIN where user_name=? and zone_name=?), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
00949 &icss);
00950 if (status != 0) {
00951 rodsLog(LOG_NOTICE,
00952 "chlRegDataObj cmlExecuteNoAnswerSql insert access failure %d",
00953 status);
00954 _rollback("chlRegDataObj");
00955 return(status);
00956 }
00957 }
00958
00959 status = cmlAudit3(AU_REGISTER_DATA_OBJ, dataIdNum,
00960 rsComm->clientUser.userName,
00961 rsComm->clientUser.rodsZone, "", &icss);
00962 if (status != 0) {
00963 rodsLog(LOG_NOTICE,
00964 "chlRegDataObj cmlAudit3 failure %d",
00965 status);
00966 _rollback("chlRegDataObj");
00967 return(status);
00968 }
00969
00970
00971 if ( !(dataObjInfo->flags & NO_COMMIT_FLAG) ) {
00972 status = cmlExecuteNoAnswerSql("commit", &icss);
00973 if (status != 0) {
00974 rodsLog(LOG_NOTICE,
00975 "chlRegDataObj cmlExecuteNoAnswerSql commit failure %d",
00976 status);
00977 return(status);
00978 }
00979 }
00980
00981 return(0);
00982 }
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992 int chlRegReplica(rsComm_t *rsComm, dataObjInfo_t *srcDataObjInfo,
00993 dataObjInfo_t *dstDataObjInfo, keyValPair_t *condInput) {
00994 char myTime[50];
00995 char logicalFileName[MAX_NAME_LEN];
00996 char logicalDirName[MAX_NAME_LEN];
00997 rodsLong_t iVal;
00998 rodsLong_t status;
00999 char tSQL[MAX_SQL_SIZE];
01000 char *cVal[30];
01001 int i;
01002 int statementNumber;
01003 int nextReplNum;
01004 char nextRepl[30];
01005 char theColls[]="data_id, coll_id, data_name, data_repl_num, data_version, data_type_name, data_size, resc_group_name, resc_name, resc_hier, data_path, data_owner_name, data_owner_zone, data_is_dirty, data_status, data_checksum, data_expiry_ts, data_map_id, data_mode, r_comment, create_ts, modify_ts";
01006 int IX_DATA_REPL_NUM=3;
01007 int IX_RESC_GROUP_NAME=7;
01008 int IX_RESC_NAME=8;
01009 int IX_RESC_HIER=9;
01010 int IX_DATA_PATH=10;
01011
01012 int IX_DATA_MODE=18;
01013 int IX_CREATE_TS=20;
01014 int IX_MODIFY_TS=21;
01015 int IX_RESC_NAME2=22;
01016 int IX_DATA_PATH2=23;
01017 int IX_DATA_ID2=24;
01018 int nColumns=25;
01019
01020 char objIdString[MAX_NAME_LEN];
01021 char replNumString[MAX_NAME_LEN];
01022 int adminMode;
01023 char *theVal;
01024
01025 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegReplica");
01026
01027 adminMode=0;
01028 if (condInput != NULL) {
01029 theVal = getValByKey(condInput, IRODS_ADMIN_KW);
01030 if (theVal != NULL) {
01031 adminMode=1;
01032 }
01033 }
01034
01035 if (!icss.status) {
01036 return(CATALOG_NOT_CONNECTED);
01037 }
01038
01039 status = splitPathByKey(srcDataObjInfo->objPath,
01040 logicalDirName, logicalFileName, '/');
01041
01042 if (adminMode) {
01043 if (rsComm->clientUser.authInfo.authFlag != LOCAL_PRIV_USER_AUTH) {
01044 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
01045 }
01046 }
01047 else {
01048
01049 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegReplica SQL 1 ");
01050 status = cmlCheckDataObjOnly(logicalDirName, logicalFileName,
01051 rsComm->clientUser.userName,
01052 rsComm->clientUser.rodsZone,
01053 ACCESS_READ_OBJECT, &icss);
01054 if (status < 0) {
01055 _rollback("chlRegReplica");
01056 return(status);
01057 }
01058 }
01059
01060
01061 snprintf(objIdString, MAX_NAME_LEN, "%lld", srcDataObjInfo->dataId);
01062 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegReplica SQL 2");
01063 status = cmlGetIntegerValueFromSql(
01064 "select max(data_repl_num) from R_DATA_MAIN where data_id = ?",
01065 &iVal, objIdString, 0, 0, 0, 0, &icss);
01066
01067 if (status != 0) {
01068 _rollback("chlRegReplica");
01069 return(status);
01070 }
01071
01072 nextReplNum = iVal+1;
01073 snprintf(nextRepl, sizeof nextRepl, "%d", nextReplNum);
01074 dstDataObjInfo->replNum = nextReplNum;
01075 snprintf(replNumString, MAX_NAME_LEN, "%d", srcDataObjInfo->replNum);
01076 snprintf(tSQL, MAX_SQL_SIZE,
01077 "select %s from R_DATA_MAIN where data_id = ? and data_repl_num = ?",
01078 theColls);
01079 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegReplica SQL 3");
01080 status = cmlGetOneRowFromSqlV2(tSQL, cVal, nColumns,
01081 objIdString, replNumString, &icss);
01082 if (status < 0) {
01083 _rollback("chlRegReplica");
01084 return(status);
01085 }
01086 statementNumber = status;
01087
01088 cVal[IX_DATA_REPL_NUM]=nextRepl;
01089 cVal[IX_RESC_NAME]=dstDataObjInfo->rescName;
01090 cVal[IX_RESC_HIER]=dstDataObjInfo->rescHier;
01091 cVal[IX_RESC_GROUP_NAME]=dstDataObjInfo->rescGroupName;
01092 cVal[IX_DATA_PATH]=dstDataObjInfo->filePath;
01093 cVal[IX_DATA_MODE]=dstDataObjInfo->dataMode;
01094
01095
01096 getNowStr(myTime);
01097 cVal[IX_MODIFY_TS]=myTime;
01098 cVal[IX_CREATE_TS]=myTime;
01099
01100 cVal[IX_RESC_NAME2]=dstDataObjInfo->rescName;
01101 cVal[IX_DATA_PATH2]=dstDataObjInfo->filePath;
01102 cVal[IX_DATA_ID2]=objIdString;
01103
01104
01105 for (i=0;i<nColumns;i++) {
01106 cllBindVars[i]=cVal[i];
01107 }
01108 cllBindVarCount = nColumns;
01109 #if (defined ORA_ICAT || defined MY_ICAT) // JMC - backport 4685
01110
01111 snprintf(tSQL, MAX_SQL_SIZE, "insert into R_DATA_MAIN ( %s ) select ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? from DUAL where not exists (select data_id from R_DATA_MAIN where resc_name=? and data_path=? and data_id=?)",
01112 theColls);
01113 #else
01114
01115 snprintf(tSQL, MAX_SQL_SIZE, "insert into R_DATA_MAIN ( %s ) select ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? where not exists (select data_id from R_DATA_MAIN where resc_name=? and data_path=? and data_id=?)",
01116 theColls);
01117
01118 #endif
01119 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegReplica SQL 4");
01120 status = cmlExecuteNoAnswerSql(tSQL, &icss);
01121 if (status < 0) {
01122 rodsLog(LOG_NOTICE,
01123 "chlRegReplica cmlExecuteNoAnswerSql(insert) failure %d",
01124 status);
01125 _rollback("chlRegReplica");
01126 return(status);
01127 }
01128
01129 status = getLocalZone();
01130 if( status < 0 ) {
01131 rodsLog( LOG_ERROR, "chlRegReplica - failed in getLocalZone with status [%d]", status );
01132 return status;
01133 }
01134
01135 if((status = _updateObjCountOfResources(dstDataObjInfo->rescHier, localZone, +1)) != 0) {
01136 return status;
01137 }
01138
01139 cmlFreeStatement(statementNumber, &icss);
01140 if (status < 0) {
01141 rodsLog(LOG_NOTICE, "chlRegReplica cmlFreeStatement failure %d", status);
01142 return(status);
01143 }
01144
01145 status = cmlAudit3(AU_REGISTER_DATA_REPLICA, objIdString,
01146 rsComm->clientUser.userName,
01147 rsComm->clientUser.rodsZone, nextRepl, &icss);
01148 if (status != 0) {
01149 rodsLog(LOG_NOTICE,
01150 "chlRegDataReplica cmlAudit3 failure %d",
01151 status);
01152 _rollback("chlRegReplica");
01153 return(status);
01154 }
01155
01156 status = cmlExecuteNoAnswerSql("commit", &icss);
01157 if (status != 0) {
01158 rodsLog(LOG_NOTICE,
01159 "chlRegReplica cmlExecuteNoAnswerSql commit failure %d",
01160 status);
01161 return(status);
01162 }
01163
01164 return(0);
01165 }
01166
01167
01168
01169
01170
01171
01172
01173 void removeMetaMapAndAVU(char *dataObjNumber) {
01174 char tSQL[MAX_SQL_SIZE];
01175 int status;
01176 cllBindVars[0]=dataObjNumber;
01177 cllBindVarCount=1;
01178 if (logSQL!=0) rodsLog(LOG_SQL, "removeMetaMapAndAVU SQL 1 ");
01179 snprintf(tSQL, MAX_SQL_SIZE,
01180 "delete from R_OBJT_METAMAP where object_id=?");
01181 status = cmlExecuteNoAnswerSql(tSQL, &icss);
01182
01183
01184
01185
01186
01187 if (status == 0) {
01188 #ifdef METADATA_CLEANUP
01189 removeAVUs();
01190 #endif
01191 }
01192 return;
01193 }
01194
01195
01196
01197
01198 static int removeAVUs() {
01199 char tSQL[MAX_SQL_SIZE];
01200 int status;
01201
01202 if (logSQL!=0) rodsLog(LOG_SQL, "removeAVUs SQL 1 ");
01203 cllBindVarCount=0;
01204
01205 #if ORA_ICAT
01206 snprintf(tSQL, MAX_SQL_SIZE,
01207 "delete from R_META_MAIN where meta_id in (select meta_id from R_META_MAIN minus select meta_id from R_OBJT_METAMAP)");
01208 #elif MY_ICAT
01209
01210
01211 snprintf(tSQL, MAX_SQL_SIZE,
01212 "delete from R_META_MAIN where meta_id not in (select meta_id from R_OBJT_METAMAP)");
01213 #else
01214
01215 snprintf(tSQL, MAX_SQL_SIZE,
01216 "delete from R_META_MAIN where meta_id in (select meta_id from R_META_MAIN except select meta_id from R_OBJT_METAMAP)");
01217 #endif
01218 status = cmlExecuteNoAnswerSql(tSQL, &icss);
01219 rodsLog (LOG_NOTICE, "removeAVUs status=%d\n",status);
01220
01221 return status;
01222 }
01223
01224
01225
01226
01227 bool
01228 _dataIsLastRepl(
01229 dataObjInfo_t* _dataObjInfo) {
01230
01231 bool result = true;
01232 int status;
01233 eirods::sql_logger logger("_dataIsLastRepl", logSQL);
01234 static const unsigned int length = 30;
01235 char cVal[length];
01236
01237 logger.log();
01238 std::stringstream id_stream;
01239 id_stream << _dataObjInfo->dataId;
01240 std::string id_string = id_stream.str();
01241 std::stringstream repl_stream;
01242 repl_stream << _dataObjInfo->replNum;
01243 std::string repl_string = repl_stream.str();
01244 status = cmlGetStringValueFromSql("select data_repl_num from R_DATA_MAIN where data_id=? and data_repl_num!=?",
01245 cVal, length, id_string.c_str(), repl_string.c_str(), 0, &icss);
01246 if(status != 0) {
01247 result = false;
01248 }
01249 return result;
01250 }
01251
01252
01253
01254
01255
01256
01257
01258 int chlUnregDataObj (rsComm_t *rsComm, dataObjInfo_t *dataObjInfo,
01259 keyValPair_t *condInput) {
01260 char logicalFileName[MAX_NAME_LEN];
01261 char logicalDirName[MAX_NAME_LEN];
01262 rodsLong_t status;
01263 char tSQL[MAX_SQL_SIZE];
01264 char replNumber[30];
01265 char dataObjNumber[30];
01266 char cVal[MAX_NAME_LEN];
01267 int adminMode;
01268 int trashMode;
01269 char *theVal;
01270 char checkPath[MAX_NAME_LEN];
01271
01272 dataObjNumber[0]='\0';
01273 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj");
01274
01275 if (!icss.status) {
01276 return(CATALOG_NOT_CONNECTED);
01277 }
01278
01279 adminMode=0;
01280 trashMode=0;
01281 if (condInput != NULL) {
01282 theVal = getValByKey(condInput, IRODS_ADMIN_KW);
01283 if (theVal != NULL) {
01284 adminMode=1;
01285 }
01286 theVal = getValByKey(condInput, IRODS_ADMIN_RMTRASH_KW);
01287 if (theVal != NULL) {
01288 adminMode=1;
01289 trashMode=1;
01290 }
01291 }
01292
01293 status = splitPathByKey(dataObjInfo->objPath,
01294 logicalDirName, logicalFileName, '/');
01295
01296
01297 if (adminMode==0) {
01298
01299 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj SQL 1 ");
01300 status = cmlCheckDataObjOnly(logicalDirName, logicalFileName,
01301 rsComm->clientUser.userName,
01302 rsComm->clientUser.rodsZone,
01303 ACCESS_DELETE_OBJECT, &icss);
01304 if (status < 0) {
01305 _rollback("chlUnregDataObj");
01306 return(status);
01307 }
01308 snprintf(dataObjNumber, sizeof dataObjNumber, "%lld", status);
01309 }
01310 else {
01311 if (rsComm->clientUser.authInfo.authFlag != LOCAL_PRIV_USER_AUTH) {
01312 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
01313 }
01314 if (trashMode) {
01315 int len;
01316 status = getLocalZone();
01317 if (status != 0) return(status);
01318 snprintf(checkPath, MAX_NAME_LEN, "/%s/trash", localZone);
01319 len = strlen(checkPath);
01320 if (strncmp(checkPath, logicalDirName, len) != 0) {
01321 addRErrorMsg (&rsComm->rError, 0,
01322 "TRASH_KW but not zone/trash path");
01323 return(CAT_INVALID_ARGUMENT);
01324 }
01325 if (dataObjInfo->dataId > 0) {
01326 snprintf(dataObjNumber, sizeof dataObjNumber, "%lld",
01327 dataObjInfo->dataId);
01328 }
01329 }
01330 else {
01331 if (dataObjInfo->replNum >= 0 && dataObjInfo->dataId >= 0) {
01332
01333 snprintf(dataObjNumber, sizeof dataObjNumber, "%lld",
01334 dataObjInfo->dataId);
01335 snprintf(replNumber, sizeof replNumber, "%d", dataObjInfo->replNum);
01336 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj SQL 2");
01337 status = cmlGetStringValueFromSql(
01338 "select data_repl_num from R_DATA_MAIN where data_id=? and data_repl_num!=?",
01339 cVal,
01340 sizeof cVal,
01341 dataObjNumber,
01342 replNumber,
01343 0,
01344 &icss);
01345 if (status != 0) {
01346 addRErrorMsg (&rsComm->rError, 0,
01347 "This is the last replica, removal by admin not allowed");
01348 return(CAT_LAST_REPLICA);
01349 }
01350 }
01351 else {
01352 addRErrorMsg (&rsComm->rError, 0,
01353 "dataId and replNum required");
01354 _rollback("chlUnregDataObj");
01355 return (CAT_INVALID_ARGUMENT);
01356 }
01357 }
01358 }
01359
01360
01361 std::string resc_hier;
01362 if(!dataObjInfo->rescHier || strlen(dataObjInfo->rescHier) == 0) {
01363 if(dataObjInfo->replNum >= 0) {
01364 snprintf(replNumber, sizeof replNumber, "%d", dataObjInfo->replNum);
01365 if((status = cmlGetStringValueFromSql("select resc_hier from R_DATA_MAIN where data_id=? and data_repl_num=?",
01366 cVal, sizeof cVal, dataObjNumber, replNumber, 0, &icss)) != 0) {
01367 return status;
01368 }
01369 } else {
01370 if((status = cmlGetStringValueFromSql("select resc_hier from R_DATA_MAIN where data_id=?",
01371 cVal, sizeof cVal, dataObjNumber, 0, 0, &icss)) != 0) {
01372 return status;
01373 }
01374 }
01375 resc_hier = std::string(cVal);
01376 } else {
01377 resc_hier = std::string(dataObjInfo->rescHier);
01378 }
01379
01380 cllBindVars[0]=logicalDirName;
01381 cllBindVars[1]=logicalFileName;
01382 if (dataObjInfo->replNum >= 0) {
01383 snprintf(replNumber, sizeof replNumber, "%d", dataObjInfo->replNum);
01384 cllBindVars[2]=replNumber;
01385 cllBindVarCount=3;
01386 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj SQL 4");
01387 snprintf(tSQL, MAX_SQL_SIZE,
01388 "delete from R_DATA_MAIN where coll_id=(select coll_id from R_COLL_MAIN where coll_name=?) and data_name=? and data_repl_num=?");
01389 }
01390 else {
01391 cllBindVarCount=2;
01392 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj SQL 5");
01393 snprintf(tSQL, MAX_SQL_SIZE,
01394 "delete from R_DATA_MAIN where coll_id=(select coll_id from R_COLL_MAIN where coll_name=?) and data_name=?");
01395 }
01396 status = cmlExecuteNoAnswerSql(tSQL, &icss);
01397 if (status != 0) {
01398 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) {
01399 char errMsg[105];
01400 status = CAT_UNKNOWN_FILE;
01401 snprintf(errMsg, 100, "data object '%s' is unknown",
01402 logicalFileName);
01403 addRErrorMsg (&rsComm->rError, 0, errMsg);
01404 return(status);
01405 }
01406 _rollback("chlUnregDataObj");
01407 return(status);
01408 }
01409
01410 status = getLocalZone();
01411 if( status < 0 ) {
01412 rodsLog( LOG_ERROR, "chlUnRegDataObj - failed in getLocalZone with status [%d]", status );
01413 return status;
01414 }
01415
01416
01417 if((status = _updateObjCountOfResources(resc_hier, localZone, -1)) != 0) {
01418 return status;
01419 }
01420
01421
01422 if (dataObjNumber[0]!='\0') {
01423 cllBindVars[0]=dataObjNumber;
01424 cllBindVars[1]=dataObjNumber;
01425 cllBindVarCount=2;
01426 if (logSQL!=0) rodsLog(LOG_SQL, "chlUnregDataObj SQL 3");
01427 status = cmlExecuteNoAnswerSql(
01428 "delete from R_OBJT_ACCESS where object_id=? and not exists (select * from R_DATA_MAIN where data_id=?)", &icss);
01429 if (status == 0) {
01430 removeMetaMapAndAVU(dataObjNumber);
01431 }
01432 }
01433
01434
01435 if (dataObjNumber[0]!='\0') {
01436 status = cmlAudit3(AU_UNREGISTER_DATA_OBJ, dataObjNumber,
01437 rsComm->clientUser.userName,
01438 rsComm->clientUser.rodsZone, "", &icss);
01439 }
01440 else {
01441 status = cmlAudit3(AU_UNREGISTER_DATA_OBJ, "0",
01442 rsComm->clientUser.userName,
01443 rsComm->clientUser.rodsZone,
01444 dataObjInfo->objPath, &icss);
01445 }
01446 if (status != 0) {
01447 rodsLog(LOG_NOTICE,
01448 "chlUnregDataObj cmlAudit3 failure %d",
01449 status);
01450 _rollback("chlUnregDataObj");
01451 return(status);
01452 }
01453
01454
01455 status = cmlExecuteNoAnswerSql("commit", &icss);
01456 if (status != 0) {
01457 rodsLog(LOG_NOTICE,
01458 "chlUnregDataObj cmlExecuteNoAnswerSql commit failure %d",
01459 status);
01460 return(status);
01461 }
01462
01463 return(status);
01464
01465 }
01466
01467
01468
01469
01470
01471
01472
01473 int chlRegRuleExec(rsComm_t *rsComm,
01474 ruleExecSubmitInp_t *ruleExecSubmitInp) {
01475 char myTime[50];
01476 rodsLong_t seqNum;
01477 char ruleExecIdNum[MAX_NAME_LEN];
01478 int status;
01479
01480 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegRuleExec");
01481 if (!icss.status) {
01482 return(CATALOG_NOT_CONNECTED);
01483 }
01484
01485 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegRuleExec SQL 1 ");
01486 seqNum = cmlGetNextSeqVal(&icss);
01487 if (seqNum < 0) {
01488 rodsLog(LOG_NOTICE, "chlRegRuleExec cmlGetNextSeqVal failure %d",
01489 seqNum);
01490 _rollback("chlRegRuleExec");
01491 return(seqNum);
01492 }
01493 snprintf(ruleExecIdNum, MAX_NAME_LEN, "%lld", seqNum);
01494
01495
01496 strncpy(ruleExecSubmitInp->ruleExecId,ruleExecIdNum, NAME_LEN);
01497
01498 getNowStr(myTime);
01499
01500 cllBindVars[0]=ruleExecIdNum;
01501 cllBindVars[1]=ruleExecSubmitInp->ruleName;
01502 cllBindVars[2]=ruleExecSubmitInp->reiFilePath;
01503 cllBindVars[3]=ruleExecSubmitInp->userName;
01504 cllBindVars[4]=ruleExecSubmitInp->exeAddress;
01505 cllBindVars[5]=ruleExecSubmitInp->exeTime;
01506 cllBindVars[6]=ruleExecSubmitInp->exeFrequency;
01507 cllBindVars[7]=ruleExecSubmitInp->priority;
01508 cllBindVars[8]=ruleExecSubmitInp->estimateExeTime;
01509 cllBindVars[9]=ruleExecSubmitInp->notificationAddr;
01510 cllBindVars[10]=myTime;
01511 cllBindVars[11]=myTime;
01512
01513 cllBindVarCount=12;
01514 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegRuleExec SQL 2");
01515 status = cmlExecuteNoAnswerSql(
01516 "insert into R_RULE_EXEC (rule_exec_id, rule_name, rei_file_path, user_name, exe_address, exe_time, exe_frequency, priority, estimated_exe_time, notification_addr, create_ts, modify_ts) values (?,?,?,?,?,?,?,?,?,?,?,?)",
01517 &icss);
01518 if (status != 0) {
01519 rodsLog(LOG_NOTICE,
01520 "chlRegRuleExec cmlExecuteNoAnswerSql(insert) failure %d",status);
01521 _rollback("chlRegRuleExec");
01522 return(status);
01523
01524 }
01525
01526
01527 status = cmlAudit3(AU_REGISTER_DELAYED_RULE, ruleExecIdNum,
01528 rsComm->clientUser.userName,
01529 rsComm->clientUser.rodsZone,
01530 ruleExecSubmitInp->ruleName, &icss);
01531 if (status != 0) {
01532 rodsLog(LOG_NOTICE,
01533 "chlRegRuleExec cmlAudit3 failure %d",
01534 status);
01535 _rollback("chlRegRuleExec");
01536 return(status);
01537 }
01538
01539 status = cmlExecuteNoAnswerSql("commit", &icss);
01540 if (status != 0) {
01541 rodsLog(LOG_NOTICE,
01542 "chlRegRuleExec cmlExecuteNoAnswerSql commit failure %d",
01543 status);
01544 return(status);
01545 }
01546
01547 return(0);
01548 }
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558 int chlModRuleExec(rsComm_t *rsComm, char *ruleExecId,
01559 keyValPair_t *regParam) {
01560 int i, j, status;
01561
01562 char tSQL[MAX_SQL_SIZE];
01563 char *theVal;
01564
01565 int maxCols=90;
01566
01567
01568
01569
01570 char *regParamNames[]={
01571 RULE_NAME_KW, RULE_REI_FILE_PATH_KW, RULE_USER_NAME_KW,
01572 RULE_EXE_ADDRESS_KW, RULE_EXE_TIME_KW,
01573 RULE_EXE_FREQUENCY_KW, RULE_PRIORITY_KW, RULE_ESTIMATE_EXE_TIME_KW,
01574 RULE_NOTIFICATION_ADDR_KW, RULE_LAST_EXE_TIME_KW,
01575 RULE_EXE_STATUS_KW,
01576 "END"
01577 };
01578 char *colNames[]={
01579 "rule_name", "rei_file_path", "user_name",
01580 "exe_address", "exe_time", "exe_frequency", "priority",
01581 "estimated_exe_time", "notification_addr",
01582 "last_exe_time", "exe_status",
01583 "create_ts", "modify_ts",
01584 };
01585
01586 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRuleExec");
01587
01588 if (regParam == NULL || ruleExecId == NULL) {
01589 return (CAT_INVALID_ARGUMENT);
01590 }
01591
01592 snprintf(tSQL, MAX_SQL_SIZE, "update R_RULE_EXEC set ");
01593
01594 for (i=0, j=0; i<maxCols; i++) {
01595 if (strcmp(regParamNames[i],"END")==0) break;
01596 theVal = getValByKey(regParam, regParamNames[i]);
01597 if (theVal != NULL) {
01598 if (j>0) rstrcat(tSQL, "," , MAX_SQL_SIZE);
01599 rstrcat(tSQL, colNames[i] , MAX_SQL_SIZE);
01600 rstrcat(tSQL, "=?", MAX_SQL_SIZE);
01601 cllBindVars[j++]=theVal;
01602 }
01603 }
01604
01605 if (j == 0) {
01606 return (CAT_INVALID_ARGUMENT);
01607 }
01608
01609 rstrcat(tSQL, "where rule_exec_id=?", MAX_SQL_SIZE);
01610 cllBindVars[j++]=ruleExecId;
01611 cllBindVarCount=j;
01612
01613 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRuleExec SQL 1 ");
01614 status = cmlExecuteNoAnswerSql(tSQL, &icss);
01615
01616 if (status != 0) {
01617 _rollback("chlModRuleExec");
01618 rodsLog(LOG_NOTICE,
01619 "chlModRuleExec cmlExecuteNoAnswer(update) failure %d",
01620 status);
01621 return(status);
01622 }
01623
01624
01625 status = cmlAudit3(AU_MODIFY_DELAYED_RULE, ruleExecId,
01626 rsComm->clientUser.userName,
01627 rsComm->clientUser.rodsZone,
01628 "", &icss);
01629 if (status != 0) {
01630 rodsLog(LOG_NOTICE,
01631 "chlModRuleExec cmlAudit3 failure %d",
01632 status);
01633 _rollback("chlModRuleExec");
01634 return(status);
01635 }
01636
01637
01638 status = cmlExecuteNoAnswerSql("commit", &icss);
01639 if (status != 0) {
01640 rodsLog(LOG_NOTICE,
01641 "chlModRuleExecMeta cmlExecuteNoAnswerSql commit failure %d",
01642 status);
01643 return(status);
01644 }
01645 return status;
01646 }
01647
01648
01649 int chlDelRuleExec(rsComm_t *rsComm,
01650 char *ruleExecId) {
01651 int status;
01652 char userName[MAX_NAME_LEN+2];
01653
01654 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelRuleExec");
01655
01656 if (!icss.status) {
01657 return(CATALOG_NOT_CONNECTED);
01658 }
01659
01660 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
01661 if (rsComm->proxyUser.authInfo.authFlag == LOCAL_USER_AUTH) {
01662 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelRuleExec SQL 1 ");
01663 status = cmlGetStringValueFromSql(
01664 "select user_name from R_RULE_EXEC where rule_exec_id=?",
01665 userName, MAX_NAME_LEN, ruleExecId, 0, 0, &icss);
01666 if (strncmp(userName, rsComm->clientUser.userName, MAX_NAME_LEN)
01667 != 0) {
01668 return(CAT_NO_ACCESS_PERMISSION);
01669 }
01670 }
01671 else {
01672 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
01673 }
01674 }
01675
01676 cllBindVars[cllBindVarCount++]=ruleExecId;
01677 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelRuleExec SQL 2 ");
01678 status = cmlExecuteNoAnswerSql(
01679 "delete from R_RULE_EXEC where rule_exec_id=?",
01680 &icss);
01681 if (status != 0) {
01682 rodsLog(LOG_NOTICE,
01683 "chlDelRuleExec delete failure %d",
01684 status);
01685 _rollback("chlDelRuleExec");
01686 return(status);
01687 }
01688
01689
01690 status = cmlAudit3(AU_DELETE_DELAYED_RULE, ruleExecId,
01691 rsComm->clientUser.userName,
01692 rsComm->clientUser.rodsZone,
01693 "", &icss);
01694 if (status != 0) {
01695 rodsLog(LOG_NOTICE,
01696 "chlDelRuleExec cmlAudit3 failure %d",
01697 status);
01698 _rollback("chlDelRuleExec");
01699 return(status);
01700 }
01701
01702 status = cmlExecuteNoAnswerSql("commit", &icss);
01703 if (status != 0) {
01704 rodsLog(LOG_NOTICE,
01705 "chlDelRuleExec cmlExecuteNoAnswerSql commit failure %d",
01706 status);
01707 return(status);
01708 }
01709 return(status);
01710 }
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724 int chlTest(rsComm_t *rsComm, char *name) {
01725 dataObjInfo_t dataObjInfo;
01726
01727 strcpy(dataObjInfo.objPath, name);
01728 dataObjInfo.replNum=1;
01729 strcpy(dataObjInfo.version, "12");
01730 strcpy(dataObjInfo.dataType, "URL");
01731 dataObjInfo.dataSize=42;
01732
01733 strcpy(dataObjInfo.rescName, "resc A");
01734
01735 strcpy(dataObjInfo.filePath, "/scratch/slocal/test1");
01736
01737 dataObjInfo.replStatus=5;
01738
01739 return (chlRegDataObj(rsComm, &dataObjInfo));
01740 }
01741
01742 int
01743 _canConnectToCatalog(
01744 rsComm_t* _rsComm)
01745 {
01746 int result = 0;
01747 if (!icss.status) {
01748 result = CATALOG_NOT_CONNECTED;
01749 } else if (_rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
01750 result = CAT_INSUFFICIENT_PRIVILEGE_LEVEL;
01751 } else if (_rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
01752 result = CAT_INSUFFICIENT_PRIVILEGE_LEVEL;
01753 }
01754 return result;
01755 }
01756
01757 int
01758 _resolveHostName(
01759 rsComm_t* _rsComm,
01760 rescInfo_t* _rescInfo)
01761 {
01762 int result = 0;
01763 struct hostent *myHostEnt;
01764
01765 myHostEnt = gethostbyname(_rescInfo->rescLoc);
01766 if (myHostEnt <= 0) {
01767 char errMsg[155];
01768 snprintf(errMsg, 150,
01769 "Warning, resource host address '%s' is not a valid DNS entry, gethostbyname failed.",
01770 _rescInfo->rescLoc);
01771 addRErrorMsg (&_rsComm->rError, 0, errMsg);
01772 }
01773 if (strcmp(_rescInfo->rescLoc, "localhost") == 0) {
01774 addRErrorMsg( &_rsComm->rError, 0,
01775 "Warning, resource host address 'localhost' will not work properly as it maps to the local host from each client.");
01776 }
01777
01778 return result;
01779 }
01780
01781 int
01782 _childIsValid(
01783 const std::string& _new_child) {
01784
01785
01786 int result = 0;
01787 char parent[MAX_NAME_LEN];
01788 int status;
01789
01790
01791 std::string resc_name;
01792 eirods::children_parser parser;
01793 parser.set_string(_new_child);
01794 parser.first_child(resc_name);
01795
01796
01797 eirods::sql_logger logger("_childIsValid", logSQL);
01798 logger.log();
01799 parent[0] = '\0';
01800 if((status = cmlGetStringValueFromSql("select resc_parent from R_RESC_MAIN where resc_name=? and zone_name=?",
01801 parent, MAX_NAME_LEN, resc_name.c_str(), localZone, 0, &icss)) != 0) {
01802 if(status == CAT_NO_ROWS_FOUND) {
01803 std::stringstream ss;
01804 ss << "Child resource \"" << resc_name << "\" not found";
01805 eirods::log(LOG_NOTICE, ss.str());
01806 } else {
01807 _rollback("_childIsValid");
01808 }
01809 result = status;
01810
01811 } else if(strlen(parent) != 0) {
01812
01813 std::stringstream ss;
01814 ss << "Child resource \"" << resc_name << "\" already has a parent \"" << parent << "\"";
01815 eirods::log(LOG_NOTICE, ss.str());
01816 result = EIRODS_CHILD_HAS_PARENT;
01817 }
01818 return result;
01819 }
01820
01821 int
01822 _updateRescChildren(
01823 char* _resc_id,
01824 const std::string& _new_child_string) {
01825
01826 int result = 0;
01827 int status;
01828 char children[MAX_PATH_ALLOWED];
01829 char myTime[50];
01830 eirods::sql_logger logger("_updateRescChildren", logSQL);
01831
01832 if((status = cmlGetStringValueFromSql("select resc_children from R_RESC_MAIN where resc_id=?",
01833 children, MAX_PATH_ALLOWED, _resc_id, 0, 0, &icss)) != 0) {
01834 _rollback("_updateRescChildren");
01835 result = status;
01836 } else {
01837 std::string children_string(children);
01838 std::stringstream ss;
01839 if(children_string.empty()) {
01840 ss << _new_child_string;
01841 } else {
01842 ss << children_string << ";" << _new_child_string;
01843 }
01844 std::string combined_children = ss.str();
01845
01846
01847 eirods::tmp_string ts(combined_children.c_str());
01848 getNowStr(myTime);
01849 cllBindVarCount = 0;
01850 cllBindVars[cllBindVarCount++] = ts.str();
01851 cllBindVars[cllBindVarCount++] = myTime;
01852 cllBindVars[cllBindVarCount++] = _resc_id;
01853 logger.log();
01854 if((status = cmlExecuteNoAnswerSql("update R_RESC_MAIN set resc_children=?, modify_ts=? "
01855 "where resc_id=?", &icss)) != 0) {
01856 std::stringstream ss;
01857 ss << "_updateRescChildren cmlExecuteNoAnswerSql update failure " << status;
01858 eirods::log(LOG_NOTICE, ss.str());
01859 _rollback("_updateRescChildren");
01860 result = status;
01861 }
01862 }
01863 return result;
01864 }
01865
01866 int
01867 _updateChildParent(
01868 const std::string& _new_child,
01869 const std::string& _parent) {
01870
01871 int result = 0;
01872 char resc_id[MAX_NAME_LEN];
01873 char myTime[50];
01874 eirods::sql_logger logger("_updateChildParent", logSQL);
01875 int status;
01876
01877
01878 eirods::children_parser parser;
01879 std::string child;
01880 parser.set_string(_new_child);
01881 parser.first_child(child);
01882
01883
01884 resc_id[0] = '\0';
01885 if((status = cmlGetStringValueFromSql("select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
01886 resc_id, MAX_NAME_LEN, child.c_str(), localZone, 0,
01887 &icss)) != 0) {
01888 if(status == CAT_NO_ROWS_FOUND) {
01889 result = CAT_INVALID_RESOURCE;
01890 } else {
01891 _rollback("_updateChildParent");
01892 result = status;
01893 }
01894 } else {
01895
01896
01897
01898 eirods::tmp_string ts(_parent.c_str());
01899 getNowStr(myTime);
01900 cllBindVarCount = 0;
01901 cllBindVars[cllBindVarCount++] = ts.str();
01902 cllBindVars[cllBindVarCount++] = myTime;
01903 cllBindVars[cllBindVarCount++] = resc_id;
01904 logger.log();
01905 if((status = cmlExecuteNoAnswerSql("update R_RESC_MAIN set resc_parent=?, modify_ts=? "
01906 "where resc_id=?", &icss)) != 0) {
01907 std::stringstream ss;
01908 ss << "_updateChildParent cmlExecuteNoAnswerSql update failure " << status;
01909 eirods::log(LOG_NOTICE, ss.str());
01910 _rollback("_updateChildParent");
01911 result = status;
01912 }
01913 }
01914
01915 return result;
01916 }
01917
01918 eirods::error chlRescObjCount(
01919 const std::string& _resc_name,
01920 rodsLong_t & _rtn_obj_count)
01921 {
01922 eirods::error result = SUCCESS();
01923 rodsLong_t obj_count = 0;
01924 int status;
01925
01926 if((status = cmlGetIntegerValueFromSql("select resc_objcount from R_RESC_MAIN where resc_name=?",
01927 &obj_count, _resc_name.c_str(), 0, 0, 0, 0, &icss)) != 0) {
01928 _rollback(__FUNCTION__);
01929 std::stringstream msg;
01930 msg << __FUNCTION__ << " - Failed to get object count for resource: \"" << _resc_name << "\"";
01931 result = ERROR(status, msg.str());
01932 } else {
01933 _rtn_obj_count = obj_count;
01934 }
01935
01936 return result;
01937 }
01938
01939
01940
01941
01942 bool
01943 _rescHasData(
01944 const std::string& _resc_name) {
01945
01946 bool result = false;
01947 eirods::sql_logger logger("_rescHasData", logSQL);
01948 int status;
01949 static const char* func_name = "_rescHasData";
01950 rodsLong_t obj_count;
01951
01952 logger.log();
01953 if((status = cmlGetIntegerValueFromSql("select resc_objcount from R_RESC_MAIN where resc_name=?",
01954 &obj_count, _resc_name.c_str(), 0, 0, 0, 0, &icss)) != 0) {
01955 _rollback(func_name);
01956 } else {
01957 if(obj_count > 0) {
01958 result = true;
01959 }
01960 }
01961 return result;
01962 }
01963
01964
01965
01966
01967 bool
01968 _childHasData(
01969 const std::string& _child) {
01970
01971 bool result = true;
01972 eirods::children_parser parser;
01973 parser.set_string(_child);
01974 std::string child;
01975 parser.first_child(child);
01976 result = _rescHasData(child);
01977 return result;
01978 }
01979
01980
01981
01982
01983 int
01984 chlAddChildResc(
01985 rsComm_t* rsComm,
01986 rescInfo_t* rescInfo)
01987 {
01988 int result = 0;
01989
01990 int status;
01991 static const char* func_name = "chlAddChildResc";
01992 eirods::sql_logger logger(func_name, logSQL);
01993 std::string new_child_string(rescInfo->rescChildren);
01994 std::string hierarchy, child_resc, root_resc;
01995 eirods::children_parser parser;
01996 eirods::hierarchy_parser hier_parser;
01997 rodsLong_t obj_count;
01998 char resc_id[MAX_NAME_LEN];
01999
02000
02001 logger.log();
02002
02003 if(!(result = _canConnectToCatalog(rsComm))) {
02004
02005 if((status = getLocalZone())) {
02006 result = status;
02007
02008 } else if (rescInfo->zoneName != NULL && strlen(rescInfo->zoneName) > 0 && strcmp(rescInfo->zoneName, localZone) !=0) {
02009 addRErrorMsg (&rsComm->rError, 0,
02010 "Currently, resources must be in the local zone");
02011 result = CAT_INVALID_ZONE;
02012
02013 } else {
02014
02015 logger.log();
02016
02017 resc_id[0] = '\0';
02018 if((status = cmlGetStringValueFromSql("select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
02019 resc_id, MAX_NAME_LEN, rescInfo->rescName, localZone, 0,
02020 &icss)) != 0) {
02021 if(status == CAT_NO_ROWS_FOUND) {
02022 result = CAT_INVALID_RESOURCE;
02023 } else {
02024 _rollback(func_name);
02025 result = status;
02026 }
02027 } else if((status =_childIsValid(new_child_string)) == 0) {
02028 if((status = _updateRescChildren(resc_id, new_child_string)) != 0) {
02029 result = status;
02030 } else if((status = _updateChildParent(new_child_string, rescInfo->rescName)) != 0) {
02031 result = status;
02032 } else {
02033
02034
02035 if(_childHasData(new_child_string)) {
02036
02037
02038
02039
02040 status = chlGetHierarchyForResc(rescInfo->rescName, localZone, hierarchy);
02041 if (status < 0) {
02042 std::stringstream ss;
02043 ss << func_name << ": chlGetHierarchyForResc failed, status = " << status;
02044 eirods::log(LOG_NOTICE, ss.str());
02045 _rollback(func_name);
02046 return status;
02047 }
02048
02049
02050
02051
02052
02053
02054 parser.set_string(new_child_string);
02055 parser.first_child(child_resc);
02056
02057 if (chlRescObjCount(child_resc, obj_count).ok()) {
02058 status = _updateObjCountOfResources(hierarchy, localZone, obj_count);
02059 } else {
02060 status = CAT_INVALID_OBJ_COUNT;
02061 }
02062
02063 if (status < 0) {
02064
02065 std::stringstream ss;
02066 ss << func_name << " aborted. Object count update error, status = " << status;
02067 eirods::log(LOG_NOTICE, ss.str());
02068 _rollback(func_name);
02069 return status;
02070 }
02071
02072
02073
02074
02075
02076
02077 hierarchy += eirods::hierarchy_parser::delimiter() + child_resc;
02078
02079
02080 status = chlSubstituteResourceHierarchies(rsComm, child_resc.c_str(), hierarchy.c_str());
02081
02082
02083
02084
02085
02086
02087
02088 hier_parser.set_string(hierarchy);
02089 hier_parser.first_resc(root_resc);
02090
02091 cllBindVars[cllBindVarCount++]=(char*)root_resc.c_str();
02092 cllBindVars[cllBindVarCount++]=(char*)root_resc.c_str();
02093 cllBindVars[cllBindVarCount++]=(char*)child_resc.c_str();
02094 status = cmlExecuteNoAnswerSql("update R_DATA_MAIN set resc_name = ?, resc_group_name = ? where resc_name = ?", &icss);
02095
02096 if (status < 0) {
02097
02098 std::stringstream ss;
02099 ss << func_name << " aborted. Resource update error, status = " << status;
02100 eirods::log(LOG_NOTICE, ss.str());
02101 _rollback(func_name);
02102 return status;
02103 }
02104
02105 }
02106
02107
02108
02109 char commentStr[1024];
02110 snprintf(commentStr, sizeof commentStr, "%s %s", rescInfo->rescName, new_child_string.c_str());
02111 if((status = cmlAudit3(AU_ADD_CHILD_RESOURCE, resc_id, rsComm->clientUser.userName, rsComm->clientUser.rodsZone,
02112 commentStr, &icss)) != 0) {
02113 std::stringstream ss;
02114 ss << func_name << " cmlAudit3 failure " << status;
02115 eirods::log(LOG_NOTICE, ss.str());
02116 _rollback(func_name);
02117 result = status;
02118 } else if((status = cmlExecuteNoAnswerSql("commit", &icss)) != 0) {
02119 std::stringstream ss;
02120 ss << func_name<< " cmlExecuteNoAnswerSql commit failure " << status;
02121 eirods::log(LOG_NOTICE, ss.str());
02122 result = status;
02123 }
02124 }
02125 } else {
02126 std::string resc_name;
02127 eirods::children_parser parser;
02128 parser.set_string(new_child_string);
02129 parser.first_child(resc_name);
02130
02131 std::stringstream msg;
02132 msg << __FUNCTION__;
02133 msg << " - Resource '" << resc_name << "' already has a parent.";
02134 addRErrorMsg(&rsComm->rError, 0, msg.str().c_str());
02135 result = status;
02136 }
02137 }
02138 }
02139 return result;
02140 }
02141
02142
02143
02144 eirods::error validate_resource_name(std::string _resc_name) {
02145
02146
02147
02148
02149 boost::regex re("^(?=.{1,63}$)\\w(\\w*(-\\w+)?)*$");
02150
02151 if (!boost::regex_match(_resc_name, re)) {
02152 std::stringstream msg;
02153 msg << "validate_resource_name failed for resource [";
02154 msg << _resc_name;
02155 msg << "]";
02156 return ERROR( SYS_INVALID_INPUT_PARAM, msg.str() );
02157 }
02158
02159 return SUCCESS();
02160
02161 }
02162
02163
02164
02165 int chlRegResc(rsComm_t *rsComm,
02166 rescInfo_t *rescInfo) {
02167 rodsLong_t seqNum;
02168 char idNum[MAX_SQL_SIZE];
02169 int status;
02170 char myTime[50];
02171 struct hostent *myHostEnt;
02172
02173 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegResc");
02174
02175
02176
02177 if( strlen( rescInfo->rescName ) < 1 ) {
02178 addRErrorMsg( &rsComm->rError, 0, "resource name is empty" );
02179 return CAT_INVALID_RESOURCE_NAME;
02180 }
02181
02182
02183
02184 if( strlen( rescInfo->rescType ) < 1 ) {
02185 addRErrorMsg( &rsComm->rError, 0, "resource type is empty" );
02186 return CAT_INVALID_RESOURCE_TYPE;
02187 }
02188
02189 if (!icss.status) {
02190 return(CATALOG_NOT_CONNECTED);
02191 }
02192
02193 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02194 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02195 }
02196 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02197 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02198 }
02199
02200
02201
02202
02203 eirods::error ret = validate_resource_name(rescInfo->rescName);
02204 if(!ret.ok()) {
02205 eirods::log(ret);
02206 return SYS_INVALID_INPUT_PARAM;
02207 }
02208
02209
02210
02211 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegResc SQL 1 ");
02212 seqNum = cmlGetNextSeqVal(&icss);
02213 if (seqNum < 0) {
02214 rodsLog(LOG_NOTICE, "chlRegResc cmlGetNextSeqVal failure %d",
02215 seqNum);
02216 _rollback("chlRegResc");
02217 return(seqNum);
02218 }
02219 snprintf(idNum, MAX_SQL_SIZE, "%lld", seqNum);
02220
02221 status = getLocalZone();
02222 if (status != 0) return(status);
02223
02224 if (rescInfo->zoneName != NULL && strlen(rescInfo->zoneName) > 0) {
02225 if (strcmp(rescInfo->zoneName, localZone) !=0) {
02226 addRErrorMsg (&rsComm->rError, 0,
02227 "Currently, resources must be in the local zone");
02228 return(CAT_INVALID_ZONE);
02229 }
02230 }
02231
02232 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegResc SQL 2");
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242 if( eirods::EMPTY_RESC_HOST != rescInfo->rescLoc ) {
02243
02244
02245 myHostEnt = gethostbyname(rescInfo->rescLoc);
02246 if (myHostEnt <= 0) {
02247 char errMsg[155];
02248 snprintf(errMsg, 150,
02249 "Warning, resource host address '%s' is not a valid DNS entry, gethostbyname failed.",
02250 rescInfo->rescLoc);
02251 addRErrorMsg (&rsComm->rError, 0, errMsg);
02252 }
02253 if (strcmp(rescInfo->rescLoc, "localhost") == 0) {
02254 addRErrorMsg( &rsComm->rError, 0,
02255 "Warning, resource host address 'localhost' will not work properly as it maps to the local host from each client.");
02256 }
02257
02258 }
02259 #if 0
02260 if (false &&
02261 (strcmp(rescInfo->rescType, "database") !=0) &&
02262 (strcmp(rescInfo->rescType, "mso") !=0) ) {
02263 if (strlen(rescInfo->rescVaultPath)<1) {
02264 return(CAT_INVALID_RESOURCE_VAULT_PATH);
02265 }
02266 }
02267 #endif
02268
02269 status = getLocalZone();
02270 if (status != 0) return(status);
02271
02272 getNowStr(myTime);
02273
02274 cllBindVars[0]=idNum;
02275 cllBindVars[1]=rescInfo->rescName;
02276 cllBindVars[2]=localZone;
02277 cllBindVars[3]=rescInfo->rescType;
02278 cllBindVars[4]=rescInfo->rescClass;
02279 cllBindVars[5]=rescInfo->rescLoc;
02280 cllBindVars[6]=rescInfo->rescVaultPath;
02281 cllBindVars[7]=myTime;
02282 cllBindVars[8]=myTime;
02283 cllBindVars[9]=rescInfo->rescChildren;
02284 cllBindVars[10]=rescInfo->rescContext;
02285 cllBindVars[11]=rescInfo->rescParent;
02286 cllBindVars[12]="0";
02287 cllBindVarCount=13;
02288
02289 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegResc SQL 4");
02290 status = cmlExecuteNoAnswerSql(
02291 "insert into R_RESC_MAIN (resc_id, resc_name, zone_name, resc_type_name, resc_class_name, resc_net, resc_def_path, create_ts, modify_ts, resc_children, resc_context, resc_parent, resc_objcount) values (?,?,?,?,?,?,?,?,?,?,?,?,?)",
02292 &icss);
02293
02294 if (status != 0) {
02295 rodsLog(LOG_NOTICE,
02296 "chlRegResc cmlExectuteNoAnswerSql(insert) failure %d",
02297 status);
02298 _rollback("chlRegResc");
02299 return(status);
02300 }
02301
02302
02303 status = cmlAudit3(AU_REGISTER_RESOURCE, idNum,
02304 rsComm->clientUser.userName,
02305 rsComm->clientUser.rodsZone,
02306 rescInfo->rescName, &icss);
02307 if (status != 0) {
02308 rodsLog(LOG_NOTICE,
02309 "chlRegResc cmlAudit3 failure %d",
02310 status);
02311 _rollback("chlRegResc");
02312 return(status);
02313 }
02314
02315 status = cmlExecuteNoAnswerSql("commit", &icss);
02316 if (status != 0) {
02317 rodsLog(LOG_NOTICE,
02318 "chlRegResc cmlExecuteNoAnswerSql commit failure %d",status);
02319 return(status);
02320 }
02321 return(status);
02322 }
02323
02324 int
02325 _removeRescChild(
02326 char* _resc_id,
02327 const std::string& _child_string) {
02328
02329 int result = 0;
02330 int status;
02331 char children[MAX_PATH_ALLOWED];
02332 char myTime[50];
02333 eirods::sql_logger logger("_removeRescChild", logSQL);
02334
02335
02336 if((status = cmlGetStringValueFromSql("select resc_children from R_RESC_MAIN where resc_id=?",
02337 children, MAX_PATH_ALLOWED, _resc_id, 0, 0, &icss)) != 0) {
02338 _rollback("_updateRescChildren");
02339 result = status;
02340 } else {
02341
02342
02343 eirods::children_parser parser;
02344 eirods::error ret = parser.set_string(children);
02345 if(!ret.ok()) {
02346 std::stringstream ss;
02347 ss << "_removeChildFromResource resource has invalid children string \"" << children << "\'";
02348 ss << ret.result();
02349 eirods::log(LOG_NOTICE, ss.str());
02350 result = CAT_INVALID_CHILD;
02351 } else {
02352
02353
02354 ret = parser.remove_child(_child_string);
02355 if(!ret.ok()) {
02356 std::stringstream ss;
02357 ss << "_removeChildFromResource parent has no child \"" << _child_string << "\'";
02358 ss << ret.result();
02359 eirods::log(LOG_NOTICE, ss.str());
02360 result = CAT_INVALID_CHILD;
02361 } else {
02362
02363
02364
02365 std::string children_string;
02366 parser.str(children_string);
02367 eirods::tmp_string tmp_children(children_string.c_str());
02368 getNowStr(myTime);
02369 cllBindVarCount = 0;
02370 cllBindVars[cllBindVarCount++] = tmp_children.str();
02371 cllBindVars[cllBindVarCount++] = myTime;
02372 cllBindVars[cllBindVarCount++] = _resc_id;
02373 logger.log();
02374 if((status = cmlExecuteNoAnswerSql("update R_RESC_MAIN set resc_children=?, modify_ts=? "
02375 "where resc_id=?", &icss)) != 0) {
02376 std::stringstream ss;
02377 ss << "_removeRescChild cmlExecuteNoAnswerSql update failure " << status;
02378 eirods::log(LOG_NOTICE, ss.str());
02379 _rollback("_removeRescChild");
02380 result = status;
02381 }
02382 }
02383 }
02384 }
02385 return result;
02386 }
02387
02388
02389
02390
02391 #if 0 // currently unused
02392 static bool
02393 _rescHasChildren(
02394 const std::string _resc_name) {
02395
02396 bool result = false;
02397 int status;
02398 char children[MAX_NAME_LEN];
02399 eirods::sql_logger logger("_rescHasChildren", logSQL);
02400
02401 logger.log();
02402 if((status = cmlGetStringValueFromSql("select resc_children from R_RESC_MAIN where resc_name=?",
02403 children, MAX_NAME_LEN, _resc_name.c_str(), 0, 0, &icss)) !=0) {
02404 if(status != CAT_NO_ROWS_FOUND) {
02405 _rollback("_rescHasChildren");
02406 }
02407 result = false;
02408 } else if(strlen(children) != 0) {
02409 result = true;
02410 }
02411 return result;
02412 }
02413 #endif
02414
02415
02416
02417
02418 int
02419 chlDelChildResc(
02420 rsComm_t* rsComm,
02421 rescInfo_t* rescInfo) {
02422
02423 eirods::sql_logger logger("chlDelChildResc", logSQL);
02424 int result = 0;
02425 int status;
02426 rodsLong_t obj_count;
02427 char resc_id[MAX_NAME_LEN];
02428 std::string child_string(rescInfo->rescChildren);
02429 std::string child, hierarchy, partial_hier;
02430 eirods::children_parser parser;
02431
02432 parser.set_string(child_string);
02433 parser.first_child(child);
02434
02435 if(!(result = _canConnectToCatalog(rsComm))) {
02436 if((status = getLocalZone())) {
02437 result = status;
02438 } else {
02439 logger.log();
02440
02441 resc_id[0] = '\0';
02442 if((status = cmlGetStringValueFromSql("select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
02443 resc_id, MAX_NAME_LEN, rescInfo->rescName, localZone, 0,
02444 &icss)) != 0) {
02445 if(status == CAT_NO_ROWS_FOUND) {
02446 result = CAT_INVALID_RESOURCE;
02447 } else {
02448 _rollback("chlDelChildResc");
02449 result = status;
02450 }
02451
02452 } else if((status = _updateChildParent(child, std::string(""))) != 0) {
02453 result = status;
02454 } else if((status = _removeRescChild(resc_id, child)) != 0) {
02455 result = status;
02456 } else {
02457
02458
02459 if(_childHasData(child_string)) {
02460
02461
02462
02463
02464 status = chlGetHierarchyForResc(rescInfo->rescName, localZone, hierarchy);
02465 if (status < 0) {
02466 std::stringstream ss;
02467 ss << "chlDelChildResc: chlGetHierarchyForResc failed, status = " << status;
02468 eirods::log(LOG_NOTICE, ss.str());
02469 _rollback("chlDelChildResc");
02470 return status;
02471 }
02472
02473
02474
02475
02476 if (chlRescObjCount(child, obj_count).ok()) {
02477 status = _updateObjCountOfResources(hierarchy, localZone, -obj_count);
02478 } else {
02479 status = CAT_INVALID_OBJ_COUNT;
02480 }
02481
02482 if (status < 0) {
02483
02484 std::stringstream ss;
02485 ss << "chlDelChildResc aborted. Object count update error, status = " << status;
02486 eirods::log(LOG_NOTICE, ss.str());
02487 _rollback("chlDelChildResc");
02488 return status;
02489 }
02490
02491
02492
02493
02494
02495
02496 hierarchy += eirods::hierarchy_parser::delimiter() + child;
02497
02498
02499 status = chlSubstituteResourceHierarchies(rsComm, hierarchy.c_str(), child.c_str());
02500
02501
02502
02503
02504 partial_hier = child + eirods::hierarchy_parser::delimiter() + "%";
02505
02506 cllBindVars[cllBindVarCount++]=(char*)child.c_str();
02507 cllBindVars[cllBindVarCount++]=(char*)child.c_str();
02508 cllBindVars[cllBindVarCount++]=(char*)child.c_str();
02509 cllBindVars[cllBindVarCount++]=(char*)partial_hier.c_str();
02510 status = cmlExecuteNoAnswerSql("update R_DATA_MAIN set resc_name = ?, resc_group_name = ? where resc_hier = ? or resc_hier like ?", &icss);
02511
02512 if (status < 0) {
02513
02514 std::stringstream ss;
02515 ss << "chlDelChildResc" << " aborted. Resource update error, status = " << status;
02516 eirods::log(LOG_NOTICE, ss.str());
02517 _rollback("chlDelChildResc");
02518 return status;
02519 }
02520
02521 }
02522
02523
02524
02525
02526
02527 char commentStr[1024];
02528 snprintf(commentStr, sizeof commentStr, "%s %s", rescInfo->rescName, child_string.c_str());
02529 if((status = cmlAudit3(AU_DEL_CHILD_RESOURCE, resc_id, rsComm->clientUser.userName, rsComm->clientUser.rodsZone,
02530 commentStr, &icss)) != 0) {
02531 std::stringstream ss;
02532 ss << "chlDelChildResc cmlAudit3 failure " << status;
02533 eirods::log(LOG_NOTICE, ss.str());
02534 _rollback("chlDelChildResc");
02535 result = status;
02536 } else if((status = cmlExecuteNoAnswerSql("commit", &icss)) != 0) {
02537 std::stringstream ss;
02538 ss << "chlDelChildResc cmlExecuteNoAnswerSql commit failure " << status;
02539 eirods::log(LOG_NOTICE, ss.str());
02540 result = status;
02541 }
02542 }
02543 }
02544 }
02545 return result;
02546 }
02547
02548 bool
02549 _rescHasParentOrChild(
02550 char* rescId) {
02551
02552 bool result = false;
02553 char parent[MAX_NAME_LEN];
02554 char children[MAX_NAME_LEN];
02555 int status;
02556 eirods::sql_logger logger("_rescHasParentOrChild", logSQL);
02557
02558 logger.log();
02559 parent[0] = '\0';
02560 children[0] = '\0';
02561 if((status = cmlGetStringValueFromSql("select resc_parent from R_RESC_MAIN where resc_id=?",
02562 parent, MAX_NAME_LEN, rescId, 0, 0, &icss)) != 0) {
02563 if(status == CAT_NO_ROWS_FOUND) {
02564 std::stringstream ss;
02565 ss << "Resource \"" << rescId << "\" not found";
02566 eirods::log(LOG_NOTICE, ss.str());
02567 } else {
02568 _rollback("_rescHasParentOrChild");
02569 }
02570 result = false;
02571 } else if(strlen(parent) != 0) {
02572 result = true;
02573 } else if((status = cmlGetStringValueFromSql("select resc_children from R_RESC_MAIN where resc_id=?",
02574 children, MAX_NAME_LEN, rescId, 0, 0, &icss)) !=0) {
02575 if(status != CAT_NO_ROWS_FOUND) {
02576 _rollback("_rescHasParentOrChild");
02577 }
02578 result = false;
02579 } else if(strlen(children) != 0) {
02580 result = true;
02581 }
02582 return result;
02583
02584 }
02585
02586
02587 int chlDelResc(rsComm_t *rsComm, rescInfo_t *rescInfo, int _dryrun ) {
02588
02589 int status;
02590 char rescId[MAX_NAME_LEN];
02591
02592 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelResc");
02593
02594 if (!icss.status) {
02595 return(CATALOG_NOT_CONNECTED);
02596 }
02597
02598 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02599 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02600 }
02601 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02602 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02603 }
02604
02605
02606
02607 if (strncmp(rescInfo->rescName, BUNDLE_RESC, strlen(BUNDLE_RESC))==0) {
02608 char errMsg[155];
02609 snprintf(errMsg, 150,
02610 "%s is a built-in resource needed for bundle operations.",
02611 BUNDLE_RESC);
02612 addRErrorMsg (&rsComm->rError, 0, errMsg);
02613 return(CAT_PSEUDO_RESC_MODIFY_DISALLOWED);
02614 }
02615
02616
02617 if (_rescHasData(rescInfo->rescName)) {
02618 char errMsg[105];
02619 snprintf(errMsg, 100,
02620 "resource '%s' contains one or more dataObjects",
02621 rescInfo->rescName);
02622 addRErrorMsg (&rsComm->rError, 0, errMsg);
02623 return(CAT_RESOURCE_NOT_EMPTY);
02624 }
02625
02626 status = getLocalZone();
02627 if (status != 0) return(status);
02628
02629
02630 rescId[0]='\0';
02631 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelResc SQL 2 ");
02632 status = cmlGetStringValueFromSql(
02633 "select resc_id from R_RESC_MAIN where resc_name=?",
02634 rescId, MAX_NAME_LEN, rescInfo->rescName, 0, 0, &icss);
02635 if (status != 0) {
02636 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) {
02637 char errMsg[105];
02638 snprintf(errMsg, 100,
02639 "resource '%s' does not exist",
02640 rescInfo->rescName);
02641 addRErrorMsg (&rsComm->rError, 0, errMsg);
02642 return(status);
02643 }
02644 _rollback("chlDelResc");
02645 return(status);
02646 }
02647
02648 if(_rescHasParentOrChild(rescId)) {
02649 char errMsg[105];
02650 snprintf(errMsg, 100,
02651 "resource '%s' has a parent or child",
02652 rescInfo->rescName);
02653 addRErrorMsg (&rsComm->rError, 0, errMsg);
02654 return(CAT_RESOURCE_NOT_EMPTY);
02655 }
02656
02657 cllBindVars[cllBindVarCount++]=rescInfo->rescName;
02658 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelResc SQL 3");
02659 status = cmlExecuteNoAnswerSql(
02660 "delete from R_RESC_MAIN where resc_name=?",
02661 &icss);
02662 if (status != 0) {
02663 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) {
02664 char errMsg[105];
02665 snprintf(errMsg, 100,
02666 "resource '%s' does not exist",
02667 rescInfo->rescName);
02668 addRErrorMsg (&rsComm->rError, 0, errMsg);
02669 return(status);
02670 }
02671 _rollback("chlDelResc");
02672 return(status);
02673 }
02674
02675
02676 cllBindVars[cllBindVarCount++]=rescId;
02677 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelResc SQL 4");
02678 status = cmlExecuteNoAnswerSql(
02679 "delete from R_RESC_GROUP where resc_id=?",
02680 &icss);
02681 if (status != 0 &&
02682 status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
02683 rodsLog(LOG_NOTICE,
02684 "chlDelResc delete from R_RESC_GROUP failure %d",
02685 status);
02686 _rollback("chlDelResc");
02687 return(status);
02688 }
02689
02690
02691
02692 removeMetaMapAndAVU(rescId);
02693
02694
02695
02696 status = cmlAudit3(AU_DELETE_RESOURCE,
02697 rescId,
02698 rsComm->clientUser.userName,
02699 rsComm->clientUser.rodsZone,
02700 rescInfo->rescName,
02701 &icss);
02702 if (status != 0) {
02703 rodsLog(LOG_NOTICE,
02704 "chlDelResc cmlAudit3 failure %d",
02705 status);
02706 _rollback("chlDelResc");
02707 return(status);
02708 }
02709
02710 if( _dryrun ) {
02711 _rollback( "chlDelResc" );
02712 return status;
02713 }
02714
02715 status = cmlExecuteNoAnswerSql("commit", &icss);
02716 if (status != 0) {
02717 rodsLog(LOG_NOTICE,
02718 "chlDelResc cmlExecuteNoAnswerSql commit failure %d",
02719 status);
02720 return(status);
02721 }
02722 return(status);
02723 }
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738 int chlRollback(rsComm_t *rsComm) {
02739 int status;
02740 if (logSQL!=0) rodsLog(LOG_SQL, "chlRollback - SQL 1 ");
02741 status = cmlExecuteNoAnswerSql("rollback", &icss);
02742 if (status != 0) {
02743 rodsLog(LOG_NOTICE,
02744 "chlRollback cmlExecuteNoAnswerSql failure %d",
02745 status);
02746 }
02747 return(status);
02748 }
02749
02750
02751
02752
02753
02754
02755
02756
02757 int chlCommit(rsComm_t *rsComm) {
02758 int status;
02759 if (logSQL!=0) rodsLog(LOG_SQL, "chlCommit - SQL 1 ");
02760 status = cmlExecuteNoAnswerSql("commit", &icss);
02761 if (status != 0) {
02762 rodsLog(LOG_NOTICE,
02763 "chlCommit cmlExecuteNoAnswerSql failure %d",
02764 status);
02765 }
02766 return(status);
02767 }
02768
02769
02770 int chlDelUserRE(rsComm_t *rsComm, userInfo_t *userInfo) {
02771 int status;
02772 char iValStr[200];
02773 char zoneToUse[MAX_NAME_LEN];
02774 char userStr[200];
02775 char userName2[NAME_LEN];
02776 char zoneName[NAME_LEN];
02777
02778 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE");
02779
02780 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02781 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02782 }
02783 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02784 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02785 }
02786
02787 status = getLocalZone();
02788 if (status != 0) return(status);
02789
02790 strncpy(zoneToUse, localZone, MAX_NAME_LEN);
02791 if (strlen(userInfo->rodsZone)>0) {
02792 strncpy(zoneToUse, userInfo->rodsZone, MAX_NAME_LEN);
02793 }
02794
02795 status = parseUserName(userInfo->userName, userName2, zoneName);
02796 if (zoneName[0]!='\0') {
02797 rstrcpy(zoneToUse, zoneName, NAME_LEN);
02798 }
02799
02800 if (strncmp(rsComm->clientUser.userName, userName2, sizeof(userName2))==0 &&
02801 strncmp(rsComm->clientUser.rodsZone, zoneToUse, sizeof(zoneToUse))==0) {
02802 addRErrorMsg (&rsComm->rError, 0, "Cannot remove your own admin account, probably unintended");
02803 return(CAT_INVALID_USER);
02804 }
02805
02806
02807 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE SQL 1 ");
02808 status = cmlGetStringValueFromSql(
02809 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
02810 iValStr, 200, userName2, zoneToUse, 0, &icss);
02811 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO ||
02812 status==CAT_NO_ROWS_FOUND) {
02813 addRErrorMsg (&rsComm->rError, 0, "Invalid user");
02814 return(CAT_INVALID_USER);
02815 }
02816 if (status != 0) {
02817 _rollback("chlDelUserRE");
02818 return(status);
02819 }
02820
02821 cllBindVars[cllBindVarCount++]=userName2;
02822 cllBindVars[cllBindVarCount++]=zoneToUse;
02823 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE SQL 2");
02824 status = cmlExecuteNoAnswerSql(
02825 "delete from R_USER_MAIN where user_name=? and zone_name=?",
02826 &icss);
02827 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) return(CAT_INVALID_USER);
02828 if (status != 0) {
02829 _rollback("chlDelUserRE");
02830 return(status);
02831 }
02832
02833 cllBindVars[cllBindVarCount++]=iValStr;
02834 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE SQL 3");
02835 status = cmlExecuteNoAnswerSql(
02836 "delete from R_USER_PASSWORD where user_id=?",
02837 &icss);
02838 if (status!=0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
02839 char errMsg[MAX_NAME_LEN+40];
02840 rodsLog(LOG_NOTICE,
02841 "chlDelUserRE delete password failure %d",
02842 status);
02843 snprintf(errMsg, sizeof errMsg, "Error removing password entry");
02844 addRErrorMsg (&rsComm->rError, 0, errMsg);
02845 _rollback("chlDelUserRE");
02846 return(status);
02847 }
02848
02849
02850
02851 cllBindVars[cllBindVarCount++]=iValStr;
02852 cllBindVars[cllBindVarCount++]=iValStr;
02853 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE SQL 4");
02854 status = cmlExecuteNoAnswerSql(
02855 "delete from R_USER_GROUP where user_id=? or group_user_id=?",
02856 &icss);
02857 if (status!=0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
02858 char errMsg[MAX_NAME_LEN+40];
02859 rodsLog(LOG_NOTICE,
02860 "chlDelUserRE delete user_group entry failure %d",
02861 status);
02862 snprintf(errMsg, sizeof errMsg, "Error removing user_group entry");
02863 addRErrorMsg (&rsComm->rError, 0, errMsg);
02864 _rollback("chlDelUserRE");
02865 return(status);
02866 }
02867
02868
02869 cllBindVars[cllBindVarCount++]=iValStr;
02870 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelUserRE SQL 4");
02871 status = cmlExecuteNoAnswerSql(
02872 "delete from R_USER_AUTH where user_id=?",
02873 &icss);
02874 if (status!=0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
02875 char errMsg[MAX_NAME_LEN+40];
02876 rodsLog(LOG_NOTICE,
02877 "chlDelUserRE delete user_auth entries failure %d",
02878 status);
02879 snprintf(errMsg, sizeof errMsg, "Error removing user_auth entries");
02880 addRErrorMsg (&rsComm->rError, 0, errMsg);
02881 _rollback("chlDelUserRE");
02882 return(status);
02883 }
02884
02885
02886 removeMetaMapAndAVU(iValStr);
02887
02888
02889 snprintf(userStr, sizeof userStr, "%s#%s",
02890 userName2, zoneToUse);
02891 status = cmlAudit3(AU_DELETE_USER_RE,
02892 iValStr,
02893 rsComm->clientUser.userName,
02894 rsComm->clientUser.rodsZone,
02895 userStr,
02896 &icss);
02897 if (status != 0) {
02898 rodsLog(LOG_NOTICE,
02899 "chlDelUserRE cmlAudit3 failure %d",
02900 status);
02901 _rollback("chlDelUserRE");
02902 return(status);
02903 }
02904
02905 return(0);
02906 }
02907
02908
02909
02910
02911
02912
02913
02914 int chlRegCollByAdmin(rsComm_t *rsComm, collInfo_t *collInfo)
02915 {
02916 char myTime[50];
02917 char logicalEndName[MAX_NAME_LEN];
02918 char logicalParentDirName[MAX_NAME_LEN];
02919 rodsLong_t iVal;
02920 char collIdNum[MAX_NAME_LEN];
02921 char nextStr[MAX_NAME_LEN];
02922 char currStr[MAX_NAME_LEN];
02923 char currStr2[MAX_SQL_SIZE];
02924 int status;
02925 char tSQL[MAX_SQL_SIZE];
02926 char userName2[NAME_LEN];
02927 char zoneName[NAME_LEN];
02928
02929 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegCollByAdmin");
02930
02931 if (!icss.status) {
02932 return(CATALOG_NOT_CONNECTED);
02933 }
02934
02935
02936
02937 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ||
02938 rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
02939 int status2;
02940 status2 = cmlCheckGroupAdminAccess(
02941 rsComm->clientUser.userName,
02942 rsComm->clientUser.rodsZone,
02943 "", &icss);
02944 if (status2 != 0) return(status2);
02945 if (creatingUserByGroupAdmin==0) {
02946 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
02947 }
02948
02949 }
02950
02951 if (collInfo==0) {
02952 return(CAT_INVALID_ARGUMENT);
02953 }
02954
02955 status = splitPathByKey(collInfo->collName,
02956 logicalParentDirName, logicalEndName, '/');
02957
02958 if (strlen(logicalParentDirName)==0) {
02959 strcpy(logicalParentDirName, "/");
02960 strcpy(logicalEndName, collInfo->collName+1);
02961 }
02962
02963
02964 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegCollByAdmin SQL 1 ");
02965 status = cmlGetIntegerValueFromSql(
02966 "select coll_id from R_COLL_MAIN where coll_name=?",
02967 &iVal, logicalParentDirName, 0, 0, 0, 0, &icss);
02968 if (status < 0) {
02969 char errMsg[MAX_NAME_LEN+40];
02970 if (status == CAT_NO_ROWS_FOUND) {
02971 snprintf(errMsg, sizeof errMsg,
02972 "collection '%s' is unknown, cannot create %s under it",
02973 logicalParentDirName, logicalEndName);
02974 addRErrorMsg (&rsComm->rError, 0, errMsg);
02975 return(status);
02976 }
02977 _rollback("chlRegCollByAdmin");
02978 return(status);
02979 }
02980
02981 snprintf(collIdNum, MAX_NAME_LEN, "%d", status);
02982
02983
02984 cllNextValueString("R_ObjectID", nextStr, MAX_NAME_LEN);
02985
02986 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegCollByAdmin SQL 2");
02987 snprintf(tSQL, MAX_SQL_SIZE,
02988 "insert into R_COLL_MAIN (coll_id, parent_coll_name, coll_name, coll_owner_name, coll_owner_zone, coll_type, coll_info1, coll_info2, create_ts, modify_ts) values (%s, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
02989 nextStr);
02990
02991 getNowStr(myTime);
02992
02993 status = getLocalZone();
02994 if (status != 0) return(status);
02995
02996
02997 status = parseUserName(collInfo->collOwnerName, userName2, zoneName);
02998 if (zoneName[0]=='\0') {
02999 rstrcpy(zoneName, localZone, NAME_LEN);
03000 }
03001
03002 cllBindVars[cllBindVarCount++]=logicalParentDirName;
03003 cllBindVars[cllBindVarCount++]=collInfo->collName;
03004 cllBindVars[cllBindVarCount++]=userName2;
03005 if (strlen(collInfo->collOwnerZone)>0) {
03006 cllBindVars[cllBindVarCount++]=collInfo->collOwnerZone;
03007 }
03008 else {
03009 cllBindVars[cllBindVarCount++]=zoneName;
03010 }
03011 if (collInfo->collType != NULL) {
03012 cllBindVars[cllBindVarCount++]=collInfo->collType;
03013 }
03014 else {
03015 cllBindVars[cllBindVarCount++]="";
03016 }
03017 if (collInfo->collInfo1 != NULL) {
03018 cllBindVars[cllBindVarCount++]=collInfo->collInfo1;
03019 }
03020 else {
03021 cllBindVars[cllBindVarCount++]="";
03022 }
03023 if (collInfo->collInfo2 != NULL) {
03024 cllBindVars[cllBindVarCount++]=collInfo->collInfo2;
03025 }
03026 else {
03027 cllBindVars[cllBindVarCount++]="";
03028 }
03029 cllBindVars[cllBindVarCount++]=myTime;
03030 cllBindVars[cllBindVarCount++]=myTime;
03031 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegCollByAdmin SQL 3");
03032 status = cmlExecuteNoAnswerSql(tSQL,
03033 &icss);
03034 if (status != 0) {
03035 char errMsg[105];
03036 if (status == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME) {
03037 snprintf(errMsg, 100, "Error %d %s",
03038 status,
03039 "CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME"
03040 );
03041 addRErrorMsg (&rsComm->rError, 0, errMsg);
03042 }
03043
03044 rodsLog(LOG_NOTICE,
03045 "chlRegCollByAdmin cmlExecuteNoAnswerSQL(insert) failure %d"
03046 ,status);
03047 _rollback("chlRegCollByAdmin");
03048 return(status);
03049 }
03050
03051
03052 cllCurrentValueString("R_ObjectID", currStr, MAX_NAME_LEN);
03053 snprintf(currStr2, MAX_SQL_SIZE, " %s ", currStr);
03054
03055 cllBindVars[cllBindVarCount++]=userName2;
03056 cllBindVars[cllBindVarCount++]=zoneName;
03057 cllBindVars[cllBindVarCount++]=ACCESS_OWN;
03058 cllBindVars[cllBindVarCount++]=myTime;
03059 cllBindVars[cllBindVarCount++]=myTime;
03060
03061 snprintf(tSQL, MAX_SQL_SIZE,
03062 "insert into R_OBJT_ACCESS values (%s, (select user_id from R_USER_MAIN where user_name=? and zone_name=?), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
03063 currStr2);
03064 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegCollByAdmin SQL 4");
03065 status = cmlExecuteNoAnswerSql(tSQL, &icss);
03066 if (status != 0) {
03067 rodsLog(LOG_NOTICE,
03068 "chlRegCollByAdmin cmlExecuteNoAnswerSql(insert access) failure %d",
03069 status);
03070 _rollback("chlRegCollByAdmin");
03071 return(status);
03072 }
03073
03074
03075 status = cmlAudit4(AU_REGISTER_COLL_BY_ADMIN,
03076 currStr2,
03077 "",
03078 userName2,
03079 zoneName,
03080 rsComm->clientUser.userName,
03081 &icss);
03082 if (status != 0) {
03083 rodsLog(LOG_NOTICE,
03084 "chlRegCollByAdmin cmlAudit4 failure %d",
03085 status);
03086 _rollback("chlRegCollByAdmin");
03087 return(status);
03088 }
03089
03090 return(0);
03091 }
03092
03093
03094
03095
03096
03097
03098
03099
03100
03101
03102 int chlRegColl(rsComm_t *rsComm, collInfo_t *collInfo) {
03103 char myTime[50];
03104 char logicalEndName[MAX_NAME_LEN];
03105 char logicalParentDirName[MAX_NAME_LEN];
03106 rodsLong_t iVal;
03107 char collIdNum[MAX_NAME_LEN];
03108 char nextStr[MAX_NAME_LEN];
03109 char currStr[MAX_NAME_LEN];
03110 char currStr2[MAX_SQL_SIZE];
03111 rodsLong_t status;
03112 char tSQL[MAX_SQL_SIZE];
03113 int inheritFlag;
03114
03115 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl");
03116
03117 if (!icss.status) {
03118 return(CATALOG_NOT_CONNECTED);
03119 }
03120
03121 status = splitPathByKey(collInfo->collName,
03122 logicalParentDirName, logicalEndName, '/');
03123
03124 if (strlen(logicalParentDirName)==0) {
03125 strcpy(logicalParentDirName, "/");
03126 strcpy(logicalEndName, collInfo->collName+1);
03127 }
03128
03129
03130
03131 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 1 ");
03132 status = cmlCheckDirAndGetInheritFlag(logicalParentDirName,
03133 rsComm->clientUser.userName,
03134 rsComm->clientUser.rodsZone,
03135 ACCESS_MODIFY_OBJECT, &inheritFlag, &icss);
03136 if (status < 0) {
03137 char errMsg[105];
03138 if (status == CAT_UNKNOWN_COLLECTION) {
03139 snprintf(errMsg, 100, "collection '%s' is unknown",
03140 logicalParentDirName);
03141 addRErrorMsg (&rsComm->rError, 0, errMsg);
03142 return(status);
03143 }
03144 _rollback("chlRegColl");
03145 return(status);
03146 }
03147 snprintf(collIdNum, MAX_NAME_LEN, "%lld", status);
03148
03149
03150 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 2");
03151 status = cmlGetIntegerValueFromSql(
03152 "select data_id from R_DATA_MAIN where data_name=? and coll_id=?",
03153 &iVal, logicalEndName, collIdNum, 0, 0, 0, &icss);
03154
03155 if (status == 0) {
03156 return(CAT_NAME_EXISTS_AS_DATAOBJ);
03157 }
03158
03159
03160
03161 cllNextValueString("R_ObjectID", nextStr, MAX_NAME_LEN);
03162
03163 getNowStr(myTime);
03164
03165 cllBindVars[cllBindVarCount++]=logicalParentDirName;
03166 cllBindVars[cllBindVarCount++]=collInfo->collName;
03167 cllBindVars[cllBindVarCount++]=rsComm->clientUser.userName;
03168 cllBindVars[cllBindVarCount++]=rsComm->clientUser.rodsZone;
03169 if (collInfo->collType != NULL) {
03170 cllBindVars[cllBindVarCount++]=collInfo->collType;
03171 }
03172 else {
03173 cllBindVars[cllBindVarCount++]="";
03174 }
03175 if (collInfo->collInfo1 != NULL) {
03176 cllBindVars[cllBindVarCount++]=collInfo->collInfo1;
03177 }
03178 else {
03179 cllBindVars[cllBindVarCount++]="";
03180 }
03181 if (collInfo->collInfo2 != NULL) {
03182 cllBindVars[cllBindVarCount++]=collInfo->collInfo2;
03183 }
03184 else {
03185 cllBindVars[cllBindVarCount++]="";
03186 }
03187 cllBindVars[cllBindVarCount++]=myTime;
03188 cllBindVars[cllBindVarCount++]=myTime;
03189 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 3");
03190 snprintf(tSQL, MAX_SQL_SIZE,
03191 "insert into R_COLL_MAIN (coll_id, parent_coll_name, coll_name, coll_owner_name, coll_owner_zone, coll_type, coll_info1, coll_info2, create_ts, modify_ts) values (%s, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
03192 nextStr);
03193 status = cmlExecuteNoAnswerSql(tSQL,
03194 &icss);
03195 if (status != 0) {
03196 rodsLog(LOG_NOTICE,
03197 "chlRegColl cmlExecuteNoAnswerSql(insert) failure %d",status);
03198 _rollback("chlRegColl");
03199 return(status);
03200 }
03201
03202
03203 cllCurrentValueString("R_ObjectID", currStr, MAX_NAME_LEN);
03204 snprintf(currStr2, MAX_SQL_SIZE, " %s ", currStr);
03205
03206 if (inheritFlag) {
03207
03208
03209 cllBindVars[0]=myTime;
03210 cllBindVars[1]=myTime;
03211 cllBindVars[2]=collIdNum;
03212 cllBindVarCount=3;
03213 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 4");
03214 snprintf(tSQL, MAX_SQL_SIZE,
03215 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select %s, user_id, access_type_id, ?, ? from R_OBJT_ACCESS where object_id = ?)",
03216 currStr2);
03217 status = cmlExecuteNoAnswerSql(tSQL, &icss);
03218
03219 if (status == 0) {
03220 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 5");
03221 #if ORA_ICAT
03222 char newCollectionID[MAX_NAME_LEN];
03223
03224
03225
03226
03227 status = cmlGetCurrentSeqVal(&icss);
03228
03229 if (status > 0) {
03230
03231 snprintf(newCollectionID, MAX_NAME_LEN, "%lld", status);
03232 cllBindVars[cllBindVarCount++]="1";
03233 cllBindVars[cllBindVarCount++]=myTime;
03234 cllBindVars[cllBindVarCount++]=newCollectionID;
03235 status = cmlExecuteNoAnswerSql(
03236 "update R_COLL_MAIN set coll_inheritance=?, modify_ts=? where coll_id=?",
03237 &icss);
03238 }
03239 #else
03240
03241
03242
03243
03244 cllBindVars[cllBindVarCount++]="1";
03245 cllBindVars[cllBindVarCount++]=myTime;
03246 snprintf(tSQL, MAX_SQL_SIZE,
03247 "update R_COLL_MAIN set coll_inheritance=?, modify_ts=? where coll_id=%s",
03248 currStr2);
03249 status = cmlExecuteNoAnswerSql(tSQL, &icss);
03250 #endif
03251 }
03252 }
03253 else {
03254 cllBindVars[cllBindVarCount++]=rsComm->clientUser.userName;
03255 cllBindVars[cllBindVarCount++]=rsComm->clientUser.rodsZone;
03256 cllBindVars[cllBindVarCount++]=ACCESS_OWN;
03257 cllBindVars[cllBindVarCount++]=myTime;
03258 cllBindVars[cllBindVarCount++]=myTime;
03259 snprintf(tSQL, MAX_SQL_SIZE,
03260 "insert into R_OBJT_ACCESS values (%s, (select user_id from R_USER_MAIN where user_name=? and zone_name=?), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
03261 currStr2);
03262 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegColl SQL 6");
03263 status = cmlExecuteNoAnswerSql(tSQL, &icss);
03264 }
03265 if (status != 0) {
03266 rodsLog(LOG_NOTICE,
03267 "chlRegColl cmlExecuteNoAnswerSql(insert access) failure %d",
03268 status);
03269 _rollback("chlRegColl");
03270 return(status);
03271 }
03272
03273
03274 status = cmlAudit4(AU_REGISTER_COLL,
03275 currStr2,
03276 "",
03277 rsComm->clientUser.userName,
03278 rsComm->clientUser.rodsZone,
03279 collInfo->collName,
03280 &icss);
03281 if (status != 0) {
03282 rodsLog(LOG_NOTICE,
03283 "chlRegColl cmlAudit4 failure %d",
03284 status);
03285 _rollback("chlRegColl");
03286 return(status);
03287 }
03288
03289 status = cmlExecuteNoAnswerSql("commit", &icss);
03290 if (status != 0) {
03291 rodsLog(LOG_NOTICE,
03292 "chlRegColl cmlExecuteNoAnswerSql commit failure %d",
03293 status);
03294 return(status);
03295 }
03296
03297 return(status);
03298 }
03299
03300
03301
03302
03303
03304
03305
03306
03307
03308
03309 int chlModColl(rsComm_t *rsComm, collInfo_t *collInfo) {
03310 char myTime[50];
03311 rodsLong_t status;
03312 char tSQL[MAX_SQL_SIZE];
03313 int count;
03314 rodsLong_t iVal;
03315 char iValStr[60];
03316
03317 if (logSQL!=0) rodsLog(LOG_SQL, "chlModColl");
03318
03319 if( NULL == collInfo ) {
03320 rodsLog( LOG_ERROR, "chlModColl :: null input parameter collInfo" );
03321 return -1;
03322 }
03323
03324 if (!icss.status) {
03325 return(CATALOG_NOT_CONNECTED);
03326 }
03327
03328
03329 iVal = cmlCheckDir(collInfo->collName, rsComm->clientUser.userName,
03330 rsComm->clientUser.rodsZone,
03331 ACCESS_MODIFY_OBJECT, &icss);
03332
03333 if (iVal < 0) {
03334 char errMsg[105];
03335 if (iVal==CAT_UNKNOWN_COLLECTION) {
03336 snprintf(errMsg, 100, "collection '%s' is unknown",
03337 collInfo->collName);
03338 addRErrorMsg (&rsComm->rError, 0, errMsg);
03339 return(CAT_UNKNOWN_COLLECTION);
03340 }
03341 if (iVal==CAT_NO_ACCESS_PERMISSION) {
03342 snprintf(errMsg, 100, "no permission to update collection '%s'",
03343 collInfo->collName);
03344 addRErrorMsg (&rsComm->rError, 0, errMsg);
03345 return (CAT_NO_ACCESS_PERMISSION);
03346 }
03347 return(iVal);
03348 }
03349
03350
03351
03352
03353
03354 strncpy(tSQL, "update R_COLL_MAIN set ", MAX_SQL_SIZE);
03355 count=0;
03356 if (collInfo->collType != NULL && strlen(collInfo->collType)>0) {
03357 if (strcmp(collInfo->collType,"NULL_SPECIAL_VALUE")==0) {
03358
03359 cllBindVars[cllBindVarCount++]="";
03360 }
03361 else {
03362 cllBindVars[cllBindVarCount++]=collInfo->collType;
03363 }
03364 strncat(tSQL, "coll_type=? ", MAX_SQL_SIZE);
03365 count++;
03366 }
03367 if (collInfo->collInfo1 != NULL && strlen(collInfo->collInfo1)>0) {
03368 if (strcmp(collInfo->collInfo1,"NULL_SPECIAL_VALUE")==0) {
03369
03370 cllBindVars[cllBindVarCount++]="";
03371 } else {
03372 cllBindVars[cllBindVarCount++]=collInfo->collInfo1;
03373 }
03374 if (count>0) strncat(tSQL, ",", MAX_SQL_SIZE);
03375 strncat(tSQL, "coll_info1=? ", MAX_SQL_SIZE);
03376 count++;
03377 }
03378 if (collInfo->collInfo2 != NULL && strlen(collInfo->collInfo2)>0) {
03379 if (strcmp(collInfo->collInfo2,"NULL_SPECIAL_VALUE")==0) {
03380
03381 cllBindVars[cllBindVarCount++]="";
03382 } else {
03383 cllBindVars[cllBindVarCount++]=collInfo->collInfo2;
03384 }
03385 if (count>0) strncat(tSQL, ",", MAX_SQL_SIZE);
03386 strncat(tSQL, "coll_info2=? ", MAX_SQL_SIZE);
03387 count++;
03388 }
03389 if (count==0) return(CAT_INVALID_ARGUMENT);
03390 getNowStr(myTime);
03391 cllBindVars[cllBindVarCount++]=myTime;
03392 cllBindVars[cllBindVarCount++]=collInfo->collName;
03393 strncat(tSQL, ", modify_ts=? where coll_name=?", MAX_SQL_SIZE);
03394
03395 if (logSQL!=0) rodsLog(LOG_SQL, "chlModColl SQL 1");
03396 status = cmlExecuteNoAnswerSql(tSQL,
03397 &icss);
03398
03399
03400 snprintf(iValStr, sizeof iValStr, "%lld", iVal);
03401 status = cmlAudit3(AU_REGISTER_COLL,
03402 iValStr,
03403 rsComm->clientUser.userName,
03404 rsComm->clientUser.rodsZone,
03405 collInfo->collName,
03406 &icss);
03407 if (status != 0) {
03408 rodsLog(LOG_NOTICE,
03409 "chlModColl cmlAudit3 failure %d",
03410 status);
03411 return(status);
03412 }
03413
03414
03415 if (status != 0) {
03416 rodsLog(LOG_NOTICE,
03417 "chlModColl cmlExecuteNoAnswerSQL(update) failure %d", status);
03418 return(status);
03419 }
03420 return(0);
03421 }
03422
03423
03424
03425 static bool allowed_zone_char( const char _c ) {
03426 return ( !std::isalnum( _c ) &&
03427 !( '_' == _c ) &&
03428 !( '-' == _c ) );
03429 }
03430
03431
03432
03433 eirods::error validate_zone_name(
03434 std::string _zone_name ) {
03435 std::string::iterator itr = std::find_if( _zone_name.begin(),
03436 _zone_name.end(),
03437 allowed_zone_char );
03438 if( itr != _zone_name.end() ) {
03439 std::stringstream msg;
03440 msg << "validate_zone_name failed for zone [";
03441 msg << _zone_name;
03442 msg << "]";
03443 return ERROR( SYS_INVALID_INPUT_PARAM, msg.str() );
03444 }
03445
03446 return SUCCESS();
03447
03448 }
03449
03450
03451 int chlRegZone(rsComm_t *rsComm,
03452 char *zoneName, char *zoneType, char *zoneConnInfo,
03453 char *zoneComment) {
03454 char nextStr[MAX_NAME_LEN];
03455 char tSQL[MAX_SQL_SIZE];
03456 int status;
03457 char myTime[50];
03458
03459 if( !rsComm || !zoneName || !zoneType || !zoneConnInfo || !zoneComment ) {
03460 return SYS_INVALID_INPUT_PARAM;
03461 }
03462
03463 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegZone");
03464
03465 if (!icss.status) {
03466 return(CATALOG_NOT_CONNECTED);
03467 }
03468
03469 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03470 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03471 }
03472 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03473 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03474 }
03475
03476 if (strncmp(zoneType, "remote", 6) != 0) {
03477 addRErrorMsg (&rsComm->rError, 0,
03478 "Currently, only zones of type 'remote' are allowed");
03479 return(CAT_INVALID_ARGUMENT);
03480 }
03481
03482
03483
03484 eirods::error ret = validate_zone_name( zoneName );
03485 if( !ret.ok() ) {
03486 eirods::log( ret );
03487 return SYS_INVALID_INPUT_PARAM;
03488 }
03489
03490
03491 cllNextValueString("R_ObjectID", nextStr, MAX_NAME_LEN);
03492
03493 getNowStr(myTime);
03494
03495 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegZone SQL 1 ");
03496 cllBindVars[cllBindVarCount++]=zoneName;
03497 cllBindVars[cllBindVarCount++]=zoneConnInfo;
03498 cllBindVars[cllBindVarCount++]=zoneComment;
03499 cllBindVars[cllBindVarCount++]=myTime;
03500 cllBindVars[cllBindVarCount++]=myTime;
03501
03502 snprintf(tSQL, MAX_SQL_SIZE,
03503 "insert into R_ZONE_MAIN (zone_id, zone_name, zone_type_name, zone_conn_string, r_comment, create_ts, modify_ts) values (%s, ?, 'remote', ?, ?, ?, ?)",
03504 nextStr);
03505 status = cmlExecuteNoAnswerSql(tSQL,
03506 &icss);
03507 if (status != 0) {
03508 rodsLog(LOG_NOTICE,
03509 "chlRegZone cmlExecuteNoAnswerSql(insert) failure %d",status);
03510 _rollback("chlRegZone");
03511 return(status);
03512 }
03513
03514
03515 status = cmlAudit3(AU_REGISTER_ZONE, "0",
03516 rsComm->clientUser.userName,
03517 rsComm->clientUser.rodsZone,
03518 "", &icss);
03519 if (status != 0) {
03520 rodsLog(LOG_NOTICE,
03521 "chlRegResc cmlAudit3 failure %d",
03522 status);
03523 return(status);
03524 }
03525
03526
03527 status = cmlExecuteNoAnswerSql("commit", &icss);
03528 if (status != 0) {
03529 rodsLog(LOG_NOTICE,
03530 "chlRegZone cmlExecuteNoAnswerSql commit failure %d",
03531 status);
03532 return(status);
03533 }
03534
03535 return(0);
03536 }
03537
03538
03539
03540 int chlModZone(rsComm_t *rsComm, char *zoneName, char *option,
03541 char *optionValue) {
03542 int status, OK;
03543 char myTime[50];
03544 char zoneId[MAX_NAME_LEN];
03545 char commentStr[200];
03546
03547 if (logSQL!=0) rodsLog(LOG_SQL, "chlModZone");
03548
03549 if (zoneName == NULL || option==NULL || optionValue==NULL) {
03550 return (CAT_INVALID_ARGUMENT);
03551 }
03552
03553 if (*zoneName == '\0' || *option == '\0' || *optionValue=='\0') {
03554 return (CAT_INVALID_ARGUMENT);
03555 }
03556
03557 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03558 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03559 }
03560 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03561 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03562 }
03563
03564 status = getLocalZone();
03565 if (status != 0) return(status);
03566
03567 zoneId[0]='\0';
03568 if (logSQL!=0) rodsLog(LOG_SQL, "chlModZone SQL 1 ");
03569 status = cmlGetStringValueFromSql(
03570 "select zone_id from R_ZONE_MAIN where zone_name=?",
03571 zoneId, MAX_NAME_LEN, zoneName, "", 0, &icss);
03572 if (status != 0) {
03573 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_ZONE);
03574 return(status);
03575 }
03576
03577 getNowStr(myTime);
03578 OK=0;
03579 if (strcmp(option, "comment")==0) {
03580 cllBindVars[cllBindVarCount++]=optionValue;
03581 cllBindVars[cllBindVarCount++]=myTime;
03582 cllBindVars[cllBindVarCount++]=zoneId;
03583 if (logSQL!=0) rodsLog(LOG_SQL, "chlModZone SQL 3");
03584 status = cmlExecuteNoAnswerSql(
03585 "update R_ZONE_MAIN set r_comment = ?, modify_ts=? where zone_id=?",
03586 &icss);
03587 if (status != 0) {
03588 rodsLog(LOG_NOTICE,
03589 "chlModZone cmlExecuteNoAnswerSql update failure %d",
03590 status);
03591 return(status);
03592 }
03593 OK=1;
03594 }
03595 if (strcmp(option, "conn")==0) {
03596 cllBindVars[cllBindVarCount++]=optionValue;
03597 cllBindVars[cllBindVarCount++]=myTime;
03598 cllBindVars[cllBindVarCount++]=zoneId;
03599 if (logSQL!=0) rodsLog(LOG_SQL, "chlModZone SQL 5");
03600 status = cmlExecuteNoAnswerSql(
03601 "update R_ZONE_MAIN set zone_conn_string = ?, modify_ts=? where zone_id=?",
03602 &icss);
03603 if (status != 0) {
03604 rodsLog(LOG_NOTICE,
03605 "chlModZone cmlExecuteNoAnswerSql update failure %d",
03606 status);
03607 return(status);
03608 }
03609 OK=1;
03610 }
03611 if (strcmp(option, "name")==0) {
03612 if (strcmp(zoneName,localZone)==0) {
03613 addRErrorMsg (&rsComm->rError, 0,
03614 "It is not valid to rename the local zone via chlModZone; iadmin should use acRenameLocalZone");
03615 return (CAT_INVALID_ARGUMENT);
03616 }
03617 cllBindVars[cllBindVarCount++]=optionValue;
03618 cllBindVars[cllBindVarCount++]=myTime;
03619 cllBindVars[cllBindVarCount++]=zoneId;
03620 if (logSQL!=0) rodsLog(LOG_SQL, "chlModZone SQL 5");
03621 status = cmlExecuteNoAnswerSql(
03622 "update R_ZONE_MAIN set zone_name = ?, modify_ts=? where zone_id=?",
03623 &icss);
03624 if (status != 0) {
03625 rodsLog(LOG_NOTICE,
03626 "chlModZone cmlExecuteNoAnswerSql update failure %d",
03627 status);
03628 return(status);
03629 }
03630 OK=1;
03631 }
03632 if (OK==0) {
03633 return (CAT_INVALID_ARGUMENT);
03634 }
03635
03636
03637 snprintf(commentStr, sizeof commentStr, "%s %s", option, optionValue);
03638 status = cmlAudit3(AU_MOD_ZONE,
03639 zoneId,
03640 rsComm->clientUser.userName,
03641 rsComm->clientUser.rodsZone,
03642 commentStr,
03643 &icss);
03644 if (status != 0) {
03645 rodsLog(LOG_NOTICE,
03646 "chlModZone cmlAudit3 failure %d",
03647 status);
03648 return(status);
03649 }
03650
03651 status = cmlExecuteNoAnswerSql("commit", &icss);
03652 if (status != 0) {
03653 rodsLog(LOG_NOTICE,
03654 "chlModZone cmlExecuteNoAnswerSql commit failure %d",
03655 status);
03656 return(status);
03657 }
03658 return(0);
03659 }
03660
03661
03662 int chlRenameColl(rsComm_t *rsComm, char *oldCollName, char *newCollName) {
03663 int status;
03664 rodsLong_t status1;
03665
03666
03667
03668 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameColl SQL 1 ");
03669
03670 status1 = cmlCheckDir(oldCollName,
03671 rsComm->clientUser.userName,
03672 rsComm->clientUser.rodsZone,
03673 ACCESS_OWN,
03674 &icss);
03675
03676 if (status1 < 0) {
03677 return(status1);
03678 }
03679
03680
03681 status = chlRenameObject(rsComm, status1, newCollName);
03682 return(status);
03683 }
03684
03685
03686
03687 int chlRenameLocalZone(rsComm_t *rsComm, char *oldZoneName, char *newZoneName) {
03688 int status;
03689 char zoneId[MAX_NAME_LEN];
03690 char myTime[50];
03691 char commentStr[200];
03692
03693 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone");
03694
03695 if (!icss.status) {
03696 return(CATALOG_NOT_CONNECTED);
03697 }
03698
03699 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03700 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03701 }
03702 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03703 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03704 }
03705
03706 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 1 ");
03707 getLocalZone();
03708
03709 if (strcmp(localZone, oldZoneName) != 0) {
03710 return(CAT_INVALID_ARGUMENT);
03711 }
03712
03713
03714 zoneId[0]='\0';
03715 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 2 ");
03716 status = cmlGetStringValueFromSql(
03717 "select zone_id from R_ZONE_MAIN where zone_name=?",
03718 zoneId, MAX_NAME_LEN, newZoneName, "", 0, &icss);
03719 if (status != CAT_NO_ROWS_FOUND) return(CAT_INVALID_ZONE);
03720
03721 getNowStr(myTime);
03722
03723
03724
03725
03726
03727 snprintf(commentStr, sizeof commentStr, "renamed local zone %s to %s",
03728 oldZoneName, newZoneName);
03729 status = cmlAudit3(AU_MOD_ZONE,
03730 "0",
03731 rsComm->clientUser.userName,
03732 rsComm->clientUser.rodsZone,
03733 commentStr,
03734 &icss);
03735 if (status != 0) {
03736 rodsLog(LOG_NOTICE,
03737 "chlRenameLocalZone cmlAudit3 failure %d",
03738 status);
03739 return(status);
03740 }
03741
03742
03743 cllBindVars[cllBindVarCount++]=newZoneName;
03744 cllBindVars[cllBindVarCount++]=myTime;
03745 cllBindVars[cllBindVarCount++]=oldZoneName;
03746 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 3 ");
03747 status = cmlExecuteNoAnswerSql(
03748 "update R_COLL_MAIN set coll_owner_zone = ?, modify_ts=? where coll_owner_zone=?",
03749 &icss);
03750 if (status != 0) {
03751 rodsLog(LOG_NOTICE,
03752 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03753 status);
03754 return(status);
03755 }
03756
03757
03758 cllBindVars[cllBindVarCount++]=newZoneName;
03759 cllBindVars[cllBindVarCount++]=myTime;
03760 cllBindVars[cllBindVarCount++]=oldZoneName;
03761 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 4 ");
03762 status = cmlExecuteNoAnswerSql(
03763 "update R_DATA_MAIN set data_owner_zone = ?, modify_ts=? where data_owner_zone=?",
03764 &icss);
03765 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
03766 rodsLog(LOG_NOTICE,
03767 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03768 status);
03769 return(status);
03770 }
03771
03772
03773 cllBindVars[cllBindVarCount++]=newZoneName;
03774 cllBindVars[cllBindVarCount++]=myTime;
03775 cllBindVars[cllBindVarCount++]=oldZoneName;
03776 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 5 ");
03777 status = cmlExecuteNoAnswerSql(
03778 "update R_RESC_MAIN set zone_name = ?, modify_ts=? where zone_name=?",
03779 &icss);
03780 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
03781 rodsLog(LOG_NOTICE,
03782 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03783 status);
03784 return(status);
03785 }
03786
03787
03788 cllBindVars[cllBindVarCount++]=newZoneName;
03789 cllBindVars[cllBindVarCount++]=myTime;
03790 cllBindVars[cllBindVarCount++]=oldZoneName;
03791 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 6 ");
03792 status = cmlExecuteNoAnswerSql(
03793 "update R_RULE_MAIN set rule_owner_zone=?, modify_ts=? where rule_owner_zone=?",
03794 &icss);
03795 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
03796 rodsLog(LOG_NOTICE,
03797 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03798 status);
03799 return(status);
03800 }
03801
03802
03803 cllBindVars[cllBindVarCount++]=newZoneName;
03804 cllBindVars[cllBindVarCount++]=myTime;
03805 cllBindVars[cllBindVarCount++]=oldZoneName;
03806 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 7 ");
03807 status = cmlExecuteNoAnswerSql(
03808 "update R_USER_MAIN set zone_name=?, modify_ts=? where zone_name=?",
03809 &icss);
03810 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
03811 rodsLog(LOG_NOTICE,
03812 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03813 status);
03814 return(status);
03815 }
03816
03817
03818 cllBindVars[cllBindVarCount++]=newZoneName;
03819 cllBindVars[cllBindVarCount++]=myTime;
03820 cllBindVars[cllBindVarCount++]=oldZoneName;
03821 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameLocalZone SQL 8 ");
03822 status = cmlExecuteNoAnswerSql(
03823 "update R_ZONE_MAIN set zone_name=?, modify_ts=? where zone_name=?",
03824 &icss);
03825 if (status != 0) {
03826 rodsLog(LOG_NOTICE,
03827 "chlRenameLocalZone cmlExecuteNoAnswerSql update failure %d",
03828 status);
03829 return(status);
03830 }
03831
03832 return(0);
03833 }
03834
03835
03836 int chlDelZone(rsComm_t *rsComm, char *zoneName) {
03837 int status;
03838 char zoneType[MAX_NAME_LEN];
03839
03840 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelZone");
03841
03842 if (!icss.status) {
03843 return(CATALOG_NOT_CONNECTED);
03844 }
03845
03846 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03847 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03848 }
03849 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03850 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03851 }
03852
03853 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelZone SQL 1 ");
03854
03855 status = cmlGetStringValueFromSql(
03856 "select zone_type_name from R_ZONE_MAIN where zone_name=?",
03857 zoneType, MAX_NAME_LEN, zoneName, 0, 0, &icss);
03858 if (status != 0) {
03859 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_ZONE);
03860 return(status);
03861 }
03862
03863 if (strcmp(zoneType, "remote") != 0) {
03864 addRErrorMsg (&rsComm->rError, 0,
03865 "It is not permitted to remove the local zone");
03866 return(CAT_INVALID_ARGUMENT);
03867 }
03868
03869 cllBindVars[cllBindVarCount++]=zoneName;
03870 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelZone 2");
03871 status = cmlExecuteNoAnswerSql(
03872 "delete from R_ZONE_MAIN where zone_name = ?",
03873 &icss);
03874 if (status != 0) {
03875 rodsLog(LOG_NOTICE,
03876 "chlDelZone cmlExecuteNoAnswerSql delete failure %d",
03877 status);
03878 return(status);
03879 }
03880
03881
03882 status = cmlAudit3(AU_DELETE_ZONE,
03883 "0",
03884 rsComm->clientUser.userName,
03885 rsComm->clientUser.rodsZone,
03886 zoneName,
03887 &icss);
03888 if (status != 0) {
03889 rodsLog(LOG_NOTICE,
03890 "chlDelZone cmlAudit3 failure %d",
03891 status);
03892 _rollback("chlDelZone");
03893 return(status);
03894 }
03895
03896 status = cmlExecuteNoAnswerSql("commit", &icss);
03897 if (status != 0) {
03898 rodsLog(LOG_NOTICE,
03899 "chlDelZone cmlExecuteNoAnswerSql commit failure %d",
03900 status);
03901 return(status);
03902 }
03903
03904 return(0);
03905 }
03906
03907
03908
03909
03910
03911
03912
03913
03914
03915
03916
03917
03918
03919
03920
03921
03922
03923
03924
03925
03926
03927
03928
03929
03930
03931 int chlSimpleQuery(rsComm_t *rsComm, char *sql,
03932 char *arg1, char *arg2, char *arg3, char *arg4,
03933 int format, int *control,
03934 char *outBuf, int maxOutBuf) {
03935 int stmtNum, status, nCols, i, needToGet, didGet;
03936 int rowSize;
03937 int rows;
03938 int OK;
03939
03940 char *allowedSQL[]={
03941 "select token_name from R_TOKN_MAIN where token_namespace = 'token_namespace'",
03942 "select token_name from R_TOKN_MAIN where token_namespace = ?",
03943 "select * from R_TOKN_MAIN where token_namespace = ? and token_name like ?",
03944 "select resc_name from R_RESC_MAIN",
03945 "select * from R_RESC_MAIN where resc_name=?",
03946 "select zone_name from R_ZONE_MAIN",
03947 "select * from R_ZONE_MAIN where zone_name=?",
03948 "select user_name from R_USER_MAIN where user_type_name='rodsgroup'",
03949 "select user_name||'#'||zone_name from R_USER_MAIN, R_USER_GROUP where R_USER_GROUP.user_id=R_USER_MAIN.user_id and R_USER_GROUP.group_user_id=(select user_id from R_USER_MAIN where user_name=?)",
03950 "select * from R_DATA_MAIN where data_id=?",
03951 "select data_name, data_id, data_repl_num from R_DATA_MAIN where coll_id =(select coll_id from R_COLL_MAIN where coll_name=?)",
03952 "select coll_name from R_COLL_MAIN where parent_coll_name=?",
03953 "select * from R_USER_MAIN where user_name=?",
03954 "select user_name||'#'||zone_name from R_USER_MAIN where user_type_name != 'rodsgroup'",
03955 "select R_RESC_GROUP.resc_group_name, R_RESC_GROUP.resc_id, resc_name, R_RESC_GROUP.create_ts, R_RESC_GROUP.modify_ts from R_RESC_MAIN, R_RESC_GROUP where R_RESC_MAIN.resc_id = R_RESC_GROUP.resc_id and resc_group_name=?",
03956 "select distinct resc_group_name from R_RESC_GROUP",
03957 "select coll_id from R_COLL_MAIN where coll_name = ?",
03958 "select * from R_USER_MAIN where user_name=? and zone_name=?",
03959 "select user_name from R_USER_MAIN where zone_name=? and user_type_name != 'rodsgroup'",
03960 "select zone_name from R_ZONE_MAIN where zone_type_name=?",
03961 "select user_name, user_auth_name from R_USER_AUTH, R_USER_MAIN where R_USER_AUTH.user_id = R_USER_MAIN.user_id and R_USER_MAIN.user_name=?",
03962 "select user_name, user_auth_name from R_USER_AUTH, R_USER_MAIN where R_USER_AUTH.user_id = R_USER_MAIN.user_id and R_USER_MAIN.user_name=? and R_USER_MAIN.zone_name=?",
03963 "select user_name, user_auth_name from R_USER_AUTH, R_USER_MAIN where R_USER_AUTH.user_id = R_USER_MAIN.user_id",
03964 "select user_name, user_auth_name from R_USER_AUTH, R_USER_MAIN where R_USER_AUTH.user_id = R_USER_MAIN.user_id and R_USER_AUTH.user_auth_name=?",
03965 "select user_name, R_USER_MAIN.zone_name, resc_name, quota_limit, quota_over, R_QUOTA_MAIN.modify_ts from R_QUOTA_MAIN, R_USER_MAIN, R_RESC_MAIN where R_USER_MAIN.user_id = R_QUOTA_MAIN.user_id and R_RESC_MAIN.resc_id = R_QUOTA_MAIN.resc_id",
03966 "select user_name, R_USER_MAIN.zone_name, resc_name, quota_limit, quota_over, R_QUOTA_MAIN.modify_ts from R_QUOTA_MAIN, R_USER_MAIN, R_RESC_MAIN where R_USER_MAIN.user_id = R_QUOTA_MAIN.user_id and R_RESC_MAIN.resc_id = R_QUOTA_MAIN.resc_id and user_name=? and R_USER_MAIN.zone_name=?",
03967 "select user_name, R_USER_MAIN.zone_name, quota_limit, quota_over, R_QUOTA_MAIN.modify_ts from R_QUOTA_MAIN, R_USER_MAIN where R_USER_MAIN.user_id = R_QUOTA_MAIN.user_id and R_QUOTA_MAIN.resc_id = 0",
03968 "select user_name, R_USER_MAIN.zone_name, quota_limit, quota_over, R_QUOTA_MAIN.modify_ts from R_QUOTA_MAIN, R_USER_MAIN where R_USER_MAIN.user_id = R_QUOTA_MAIN.user_id and R_QUOTA_MAIN.resc_id = 0 and user_name=? and R_USER_MAIN.zone_name=?",
03969 ""
03970 };
03971
03972 if (logSQL!=0) rodsLog(LOG_SQL, "chlSimpleQuery");
03973
03974 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03975 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03976 }
03977 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
03978 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
03979 }
03980
03981
03982 OK=0;
03983 for (i=0;;i++) {
03984 if (strlen(allowedSQL[i]) < 1) break;
03985 if (strcasecmp(allowedSQL[i], sql)==0) {
03986 OK=1;
03987 break;
03988 }
03989 }
03990
03991 if (OK == 0) {
03992 return(CAT_INVALID_ARGUMENT);
03993 }
03994
03995
03996
03997 if (i==0 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 1 ");
03998 if (i==1 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 2");
03999 if (i==2 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 3");
04000 if (i==3 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 4");
04001 if (i==4 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 5");
04002 if (i==5 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 6");
04003 if (i==6 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 7");
04004 if (i==7 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 8");
04005 if (i==8 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 9");
04006 if (i==9 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 10");
04007 if (i==10 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 11");
04008 if (i==11 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 12");
04009 if (i==12 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 13");
04010 if (i==13 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 14");
04011
04012
04013
04014 if (i==17 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 18");
04015 if (i==18 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 19");
04016 if (i==19 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 20");
04017 if (i==20 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 21");
04018 if (i==21 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 22");
04019 if (i==22 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 23");
04020 if (i==23 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 24");
04021 if (i==24 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 25");
04022 if (i==25 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 26");
04023 if (i==26 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 27");
04024 if (i==27 && logSQL) rodsLog(LOG_SQL, "chlSimpleQuery SQL 28");
04025
04026 outBuf[0]='\0';
04027 needToGet=1;
04028 didGet=0;
04029 rowSize=0;
04030 rows=0;
04031 if (*control==0) {
04032 status = cmlGetFirstRowFromSqlBV(sql, arg1, arg2, arg3, arg4,
04033 &stmtNum, &icss);
04034 if (status < 0) {
04035 if (status != CAT_NO_ROWS_FOUND) {
04036 rodsLog(LOG_NOTICE,
04037 "chlSimpleQuery cmlGetFirstRowFromSqlBV failure %d",
04038 status);
04039 }
04040 return(status);
04041 }
04042 didGet=1;
04043 needToGet=0;
04044 *control = stmtNum+1;
04045 }
04046 else {
04047 stmtNum = *control - 1;
04048 }
04049
04050 for (;;) {
04051 if (needToGet) {
04052 status = cmlGetNextRowFromStatement(stmtNum, &icss);
04053 if (status == CAT_NO_ROWS_FOUND) {
04054 *control = 0;
04055 if (didGet) {
04056 if (format == 2) {
04057 i = strlen(outBuf);
04058 outBuf[i-1]='\0';
04059 }
04060 return(0);
04061 }
04062 return(status);
04063 }
04064 if (status < 0) {
04065 rodsLog(LOG_NOTICE,
04066 "chlSimpleQuery cmlGetNextRowFromStatement failure %d",
04067 status);
04068 return(status);
04069 }
04070 *control = stmtNum+1;
04071 didGet=1;
04072 }
04073 needToGet=1;
04074 nCols = icss.stmtPtr[stmtNum]->numOfCols;
04075 if (rows==0 && format==3) {
04076 for (i = 0; i < nCols ; i++ ) {
04077 rstrcat(outBuf, icss.stmtPtr[stmtNum]->resultColName[i],maxOutBuf);
04078 rstrcat(outBuf, " ", maxOutBuf);
04079 }
04080 if (i != nCols-1) {
04081
04082 rstrcat(outBuf, " ", maxOutBuf);
04083 }
04084
04085 }
04086 rows++;
04087 for (i = 0; i < nCols ; i++ ) {
04088 if (format==1 || format==3) {
04089 if (strlen(icss.stmtPtr[stmtNum]->resultValue[i])==0) {
04090 rstrcat(outBuf, "- ", maxOutBuf);
04091 }
04092 else {
04093 rstrcat(outBuf, icss.stmtPtr[stmtNum]->resultValue[i],
04094 maxOutBuf);
04095 rstrcat(outBuf, " ", maxOutBuf);
04096 }
04097 }
04098 if (format == 2) {
04099 rstrcat(outBuf, icss.stmtPtr[stmtNum]->resultColName[i],maxOutBuf);
04100 rstrcat(outBuf, ": ", maxOutBuf);
04101 rstrcat(outBuf, icss.stmtPtr[stmtNum]->resultValue[i], maxOutBuf);
04102 rstrcat(outBuf, "\n", maxOutBuf);
04103 }
04104 }
04105 rstrcat(outBuf, "\n", maxOutBuf);
04106 if (rowSize==0) rowSize=strlen(outBuf);
04107 if ((int) strlen(outBuf)+rowSize+20 > maxOutBuf) {
04108 return(0);
04109 }
04110 }
04111 return 0;
04112 }
04113
04114
04115
04116 int chlDelCollByAdmin(rsComm_t *rsComm, collInfo_t *collInfo) {
04117 rodsLong_t iVal;
04118 char logicalEndName[MAX_NAME_LEN];
04119 char logicalParentDirName[MAX_NAME_LEN];
04120 char collIdNum[MAX_NAME_LEN];
04121 int status;
04122
04123 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelCollByAdmin");
04124
04125 if (!icss.status) {
04126 return(CATALOG_NOT_CONNECTED);
04127 }
04128
04129 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
04130 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
04131 }
04132 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
04133 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
04134 }
04135
04136 if (collInfo==0) {
04137 return(CAT_INVALID_ARGUMENT);
04138 }
04139
04140 status = splitPathByKey(collInfo->collName,
04141 logicalParentDirName, logicalEndName, '/');
04142
04143 if (strlen(logicalParentDirName)==0) {
04144 strcpy(logicalParentDirName, "/");
04145 strcpy(logicalEndName, collInfo->collName+1);
04146 }
04147
04148
04149 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelCollByAdmin SQL 1 ");
04150 status = cmlGetIntegerValueFromSql(
04151 "select coll_id from R_COLL_MAIN where parent_coll_name=? union select coll_id from R_DATA_MAIN where coll_id=(select coll_id from R_COLL_MAIN where coll_name=?)",
04152 &iVal, collInfo->collName, collInfo->collName, 0, 0, 0, &icss);
04153
04154 if (status != CAT_NO_ROWS_FOUND) {
04155 if (status == 0) {
04156 char errMsg[105];
04157 snprintf(errMsg, 100, "collection '%s' is not empty",
04158 collInfo->collName);
04159 addRErrorMsg (&rsComm->rError, 0, errMsg);
04160 return(CAT_COLLECTION_NOT_EMPTY);
04161 }
04162 _rollback("chlDelCollByAdmin");
04163 return(status);
04164 }
04165
04166
04167 cllBindVars[cllBindVarCount++]=collInfo->collName;
04168 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelCollByAdmin SQL 2");
04169 status = cmlExecuteNoAnswerSql(
04170 "delete from R_OBJT_ACCESS where object_id=(select coll_id from R_COLL_MAIN where coll_name=?)",
04171 &icss);
04172 if (status != 0) {
04173
04174 rodsLog(LOG_NOTICE,
04175 "chlDelCollByAdmin delete access failure %d",
04176 status);
04177 _rollback("chlDelCollByAdmin");
04178 }
04179
04180
04181 snprintf(collIdNum, MAX_NAME_LEN, "%lld", iVal);
04182 removeMetaMapAndAVU(collIdNum);
04183
04184
04185 status = cmlAudit4(AU_DELETE_COLL_BY_ADMIN,
04186 "select coll_id from R_COLL_MAIN where coll_name=?",
04187 collInfo->collName,
04188 rsComm->clientUser.userName,
04189 rsComm->clientUser.rodsZone,
04190 collInfo->collName,
04191 &icss);
04192 if (status != 0) {
04193 rodsLog(LOG_NOTICE,
04194 "chlDelCollByAdmin cmlAudit4 failure %d",
04195 status);
04196 _rollback("chlDelCollByAdmin");
04197 return(status);
04198 }
04199
04200
04201
04202 cllBindVars[cllBindVarCount++]=collInfo->collName;
04203 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelCollByAdmin SQL 3");
04204 status = cmlExecuteNoAnswerSql("delete from R_COLL_MAIN where coll_name=?",
04205 &icss);
04206
04207 if (status != 0) {
04208 char errMsg[105];
04209 snprintf(errMsg, 100, "collection '%s' is unknown",
04210 collInfo->collName);
04211 addRErrorMsg (&rsComm->rError, 0, errMsg);
04212 _rollback("chlDelCollByAdmin");
04213 return(CAT_UNKNOWN_COLLECTION);
04214 }
04215
04216 return(0);
04217 }
04218
04219
04220
04221 int chlDelColl(rsComm_t *rsComm, collInfo_t *collInfo) {
04222
04223 int status;
04224
04225 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelColl");
04226
04227 status = _delColl(rsComm, collInfo);
04228 if (status != 0) return(status);
04229
04230 status = cmlExecuteNoAnswerSql("commit", &icss);
04231 if (status != 0) {
04232 rodsLog(LOG_NOTICE,
04233 "chlDelColl cmlExecuteNoAnswerSql commit failure %d",
04234 status);
04235 _rollback("chlDelColl");
04236 return(status);
04237 }
04238 return(0);
04239 }
04240
04241
04242
04243
04244 static int _delColl(rsComm_t *rsComm, collInfo_t *collInfo) {
04245 rodsLong_t iVal;
04246 char logicalEndName[MAX_NAME_LEN];
04247 char logicalParentDirName[MAX_NAME_LEN];
04248 char collIdNum[MAX_NAME_LEN];
04249 char parentCollIdNum[MAX_NAME_LEN];
04250 rodsLong_t status;
04251
04252 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl");
04253
04254 if (!icss.status) {
04255 return(CATALOG_NOT_CONNECTED);
04256 }
04257
04258 status = splitPathByKey(collInfo->collName,
04259 logicalParentDirName, logicalEndName, '/');
04260
04261 if (strlen(logicalParentDirName)==0) {
04262 strcpy(logicalParentDirName, "/");
04263 strcpy(logicalEndName, collInfo->collName+1);
04264 }
04265
04266
04267
04268 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl SQL 1 ");
04269 status = cmlCheckDir(logicalParentDirName,
04270 rsComm->clientUser.userName,
04271 rsComm->clientUser.rodsZone,
04272 ACCESS_MODIFY_OBJECT,
04273 &icss);
04274 if (status < 0) {
04275 char errMsg[105];
04276 if (status == CAT_UNKNOWN_COLLECTION) {
04277 snprintf(errMsg, 100, "collection '%s' is unknown",
04278 logicalParentDirName);
04279 addRErrorMsg (&rsComm->rError, 0, errMsg);
04280 return(status);
04281 }
04282 _rollback("_delColl");
04283 return(status);
04284 }
04285 snprintf(parentCollIdNum, MAX_NAME_LEN, "%lld", status);
04286
04287
04288
04289 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl SQL 2");
04290 status = cmlCheckDir(collInfo->collName,
04291 rsComm->clientUser.userName,
04292 rsComm->clientUser.rodsZone,
04293 ACCESS_DELETE_OBJECT,
04294 &icss);
04295 if (status < 0) return(status);
04296 snprintf(collIdNum, MAX_NAME_LEN, "%lld", status);
04297
04298
04299 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl SQL 3");
04300 status = cmlGetIntegerValueFromSql(
04301 "select coll_id from R_COLL_MAIN where parent_coll_name=? union select coll_id from R_DATA_MAIN where coll_id=(select coll_id from R_COLL_MAIN where coll_name=?)",
04302 &iVal, collInfo->collName, collInfo->collName, 0, 0, 0, &icss);
04303 if (status != CAT_NO_ROWS_FOUND) {
04304 return(CAT_COLLECTION_NOT_EMPTY);
04305 }
04306
04307
04308
04309
04310
04311 cllBindVars[cllBindVarCount++]=collInfo->collName;
04312 cllBindVars[cllBindVarCount++]=collIdNum;
04313 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl SQL 4");
04314 status = cmlExecuteNoAnswerSql(
04315 "delete from R_COLL_MAIN where coll_name=? and coll_id=?",
04316 &icss);
04317 if (status != 0) {
04318 rodsLog(LOG_NOTICE,
04319 "_delColl cmlExecuteNoAnswerSql delete failure %d",
04320 status);
04321 _rollback("_delColl");
04322 return(status);
04323 }
04324
04325
04326 cllBindVars[cllBindVarCount++]=collIdNum;
04327 if (logSQL!=0) rodsLog(LOG_SQL, "_delColl SQL 5");
04328 status = cmlExecuteNoAnswerSql(
04329 "delete from R_OBJT_ACCESS where object_id=?",
04330 &icss);
04331 if (status != 0) {
04332 rodsLog(LOG_NOTICE,
04333 "_delColl cmlExecuteNoAnswerSql delete access failure %d",
04334 status);
04335 _rollback("_delColl");
04336 }
04337
04338
04339 removeMetaMapAndAVU(collIdNum);
04340
04341
04342 status = cmlAudit3(AU_DELETE_COLL,
04343 collIdNum,
04344 rsComm->clientUser.userName,
04345 rsComm->clientUser.rodsZone,
04346 collInfo->collName,
04347 &icss);
04348 if (status != 0) {
04349 rodsLog(LOG_NOTICE,
04350 "chlModColl cmlAudit3 failure %d",
04351 status);
04352 _rollback("_delColl");
04353 return(status);
04354 }
04355
04356 return(status);
04357 }
04358
04359
04360
04361
04362 static int _modRescInHierarchies(const std::string& old_resc, const std::string& new_resc) {
04363 char update_sql[MAX_SQL_SIZE];
04364 int status;
04365 const char *sep = eirods::hierarchy_parser::delimiter().c_str();
04366 std::string std_conf_str;
04367
04368 #if ORA_ICAT
04369
04370 return SYS_NOT_IMPLEMENTED;
04371 #elif MY_ICAT
04372 return SYS_NOT_IMPLEMENTED;
04373 #endif
04374
04375
04376
04377 eirods::catalog_properties::getInstance().get_property<std::string>(eirods::STANDARD_CONFORMING_STRINGS, std_conf_str);
04378
04379
04380
04381
04382
04383
04384
04385
04386 if (std_conf_str == "on") {
04387
04388 snprintf(update_sql, MAX_SQL_SIZE,
04389 "update r_data_main set resc_hier = regexp_replace(resc_hier, '(^|(.+%s))%s($|(%s.+))', '\\1%s\\3');",
04390 sep, old_resc.c_str(), sep, new_resc.c_str());
04391 } else {
04392
04393 snprintf(update_sql, MAX_SQL_SIZE,
04394 "update r_data_main set resc_hier = regexp_replace(resc_hier, '(^|(.+%s))%s($|(%s.+))', '\\\\1%s\\\\3');",
04395 sep, old_resc.c_str(), sep, new_resc.c_str());
04396 }
04397
04398
04399
04400 status = cmlExecuteNoAnswerSql(update_sql, &icss);
04401
04402
04403
04404 if (status < 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
04405 std::stringstream ss;
04406 ss << "_modRescInHierarchies: cmlExecuteNoAnswerSql update failure, status = " << status;
04407 eirods::log(LOG_NOTICE, ss.str());
04408
04409 }
04410
04411 return status;
04412 }
04413
04414
04415
04416
04417 static int _modRescInChildren(const std::string& old_resc, const std::string& new_resc) {
04418 char update_sql[MAX_SQL_SIZE];
04419 int status;
04420 char sep[] = ";";
04421 std::string std_conf_str;
04422
04423 #if ORA_ICAT
04424
04425 return SYS_NOT_IMPLEMENTED;
04426 #elif MY_ICAT
04427 return SYS_NOT_IMPLEMENTED;
04428 #endif
04429
04430
04431
04432 eirods::catalog_properties::getInstance().get_property<std::string>(eirods::STANDARD_CONFORMING_STRINGS, std_conf_str);
04433
04434
04435
04436
04437
04438
04439
04440
04441
04442 if (std_conf_str == "on") {
04443
04444 snprintf(update_sql, MAX_SQL_SIZE,
04445 "update r_resc_main set resc_children = regexp_replace(resc_children, '(^|(.+%s))%s{}(.*)', '\\1%s{}\\3');",
04446 sep, old_resc.c_str(), new_resc.c_str());
04447 } else {
04448
04449 snprintf(update_sql, MAX_SQL_SIZE,
04450 "update r_resc_main set resc_children = regexp_replace(resc_children, '(^|(.+%s))%s{}(.*)', '\\\\1%s{}\\\\3');",
04451 sep, old_resc.c_str(), new_resc.c_str());
04452 }
04453
04454
04455
04456 status = cmlExecuteNoAnswerSql(update_sql, &icss);
04457
04458
04459
04460 if (status < 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
04461 std::stringstream ss;
04462 ss << "_modRescInChildren: cmlExecuteNoAnswerSql update failure, status = " << status;
04463 eirods::log(LOG_NOTICE, ss.str());
04464
04465 }
04466
04467 return status;
04468 }
04469
04470
04471
04472
04473 static
04474 eirods::error verify_auth_response(
04475 const char* _scheme,
04476 const char* _challenge,
04477 const char* _user_name,
04478 const char* _response ) {
04479
04480
04481 if( !_scheme ) {
04482 return ERROR(
04483 SYS_INVALID_INPUT_PARAM,
04484 "null _scheme ptr" );
04485 } else if( !_challenge ) {
04486 return ERROR(
04487 SYS_INVALID_INPUT_PARAM,
04488 "null _challenge ptr" );
04489 } else if( !_user_name ) {
04490 return ERROR(
04491 SYS_INVALID_INPUT_PARAM,
04492 "null _user_name ptr" );
04493 } else if( !_response ) {
04494 return ERROR(
04495 SYS_INVALID_INPUT_PARAM,
04496 "null _response ptr" );
04497 }
04498
04499
04500
04501 eirods::auth_object_ptr auth_obj;
04502 eirods::error ret = eirods::auth_factory(
04503 _scheme,
04504 0,
04505 auth_obj );
04506 if( !ret.ok() ){
04507 return ret;
04508 }
04509
04510
04511
04512 eirods::plugin_ptr ptr;
04513 ret = auth_obj->resolve(
04514 eirods::AUTH_INTERFACE,
04515 ptr );
04516 if( !ret.ok() ){
04517 return ret;
04518 }
04519 eirods::auth_ptr auth_plugin = boost::dynamic_pointer_cast< eirods::auth >( ptr );
04520
04521
04522
04523 ret = auth_plugin->call<
04524 const char*,
04525 const char*,
04526 const char* >(
04527 eirods::AUTH_AGENT_AUTH_VERIFY,
04528 auth_obj,
04529 _challenge,
04530 _user_name,
04531 _response );
04532 if( !ret.ok() ){
04533 eirods::log( PASS( ret ) );
04534 return ret;
04535 }
04536
04537 return SUCCESS();
04538
04539 }
04540
04541
04542
04543
04544
04545
04546
04547
04548
04549
04550
04551
04552
04553 int chlCheckAuth(
04554 rsComm_t* rsComm,
04555 const char* scheme,
04556 char* challenge,
04557 char* response,
04558 char* username,
04559 int* userPrivLevel,
04560 int* clientPrivLevel ) {
04561
04562
04563 int status;
04564 char md5Buf[CHALLENGE_LEN+MAX_PASSWORD_LEN+2];
04565 char digest[RESPONSE_LEN+2];
04566 MD5_CTX context;
04567 char *cp;
04568 int i, OK, k;
04569 char userType[MAX_NAME_LEN];
04570 static int prevFailure=0;
04571 char pwInfoArray[MAX_PASSWORD_LEN*MAX_PASSWORDS*3];
04572 char goodPw[MAX_PASSWORD_LEN+10];
04573 char lastPw[MAX_PASSWORD_LEN+10];
04574 char goodPwExpiry[MAX_PASSWORD_LEN+10];
04575 char goodPwTs[MAX_PASSWORD_LEN+10];
04576 char goodPwModTs[MAX_PASSWORD_LEN+10];
04577 rodsLong_t expireTime;
04578 char *cpw;
04579 int nPasswords;
04580 char myTime[50];
04581 time_t nowTime;
04582 time_t pwExpireMaxCreateTime;
04583 char expireStr[50];
04584 char expireStrCreate[50];
04585 char myUserZone[MAX_NAME_LEN];
04586 char userName2[NAME_LEN+2];
04587 char userZone[NAME_LEN+2];
04588 rodsLong_t pamMinTime;
04589 rodsLong_t pamMaxTime;
04590
04591 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth");
04592
04593 if (prevFailure > 1) {
04594
04595 if (prevFailure > 5) sleep(20);
04596 sleep(2);
04597 }
04598 *userPrivLevel = NO_USER_AUTH;
04599 *clientPrivLevel = NO_USER_AUTH;
04600
04601 memset(md5Buf, 0, sizeof(md5Buf));
04602 strncpy(md5Buf, challenge, CHALLENGE_LEN);
04603 snprintf(prevChalSig,sizeof prevChalSig,
04604 "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
04605 (unsigned char)md5Buf[0], (unsigned char)md5Buf[1],
04606 (unsigned char)md5Buf[2], (unsigned char)md5Buf[3],
04607 (unsigned char)md5Buf[4], (unsigned char)md5Buf[5],
04608 (unsigned char)md5Buf[6], (unsigned char)md5Buf[7],
04609 (unsigned char)md5Buf[8], (unsigned char)md5Buf[9],
04610 (unsigned char)md5Buf[10],(unsigned char)md5Buf[11],
04611 (unsigned char)md5Buf[12],(unsigned char)md5Buf[13],
04612 (unsigned char)md5Buf[14],(unsigned char)md5Buf[15]);
04613
04614 status = parseUserName(username, userName2, userZone);
04615 if (userZone[0]=='\0') {
04616 status = getLocalZone();
04617 if (status != 0) return(status);
04618 strncpy(myUserZone, localZone, MAX_NAME_LEN);
04619 }
04620 else {
04621 strncpy(myUserZone, userZone, MAX_NAME_LEN);
04622 }
04623
04624
04625 if( scheme && strlen( scheme ) > 0 ) {
04626 eirods::error ret = verify_auth_response(
04627 scheme,
04628 challenge,
04629 userName2,
04630 response );
04631 if( !ret.ok() ) {
04632 eirods::log( PASS( ret ) );
04633 return ret.code();
04634 }
04635 goto checkLevel;
04636 }
04637
04638
04639 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 1 ");
04640
04641 status = cmlGetMultiRowStringValuesFromSql(
04642 "select rcat_password, pass_expiry_ts, R_USER_PASSWORD.create_ts, R_USER_PASSWORD.modify_ts from R_USER_PASSWORD, R_USER_MAIN where user_name=? and zone_name=? and R_USER_MAIN.user_id = R_USER_PASSWORD.user_id",
04643 pwInfoArray, MAX_PASSWORD_LEN,
04644 MAX_PASSWORDS*4,
04645 userName2, myUserZone, &icss);
04646
04647 if (status < 4) {
04648 if (status == CAT_NO_ROWS_FOUND) {
04649 status = CAT_INVALID_USER;
04650 if (strncmp(ANONYMOUS_USER, userName2, NAME_LEN)==0) {
04651
04652 goto checkLevel;
04653 }
04654 }
04655 return(status);
04656 }
04657
04658 nPasswords=status/4;
04659 goodPwExpiry[0]='\0';
04660 goodPwTs[0]='\0';
04661 goodPwModTs[0]='\0';
04662
04663 cpw=pwInfoArray;
04664 for (k=0;k<MAX_PASSWORDS && k<nPasswords;k++) {
04665 memset(md5Buf, 0, sizeof(md5Buf));
04666 strncpy(md5Buf, challenge, CHALLENGE_LEN);
04667 icatDescramble(cpw);
04668 strncpy(md5Buf+CHALLENGE_LEN, cpw, MAX_PASSWORD_LEN);
04669
04670 MD5Init (&context);
04671 MD5Update (&context, (unsigned char *) md5Buf, CHALLENGE_LEN+MAX_PASSWORD_LEN);
04672 MD5Final ((unsigned char *) digest, &context);
04673
04674 for (i=0;i<RESPONSE_LEN;i++) {
04675 if (digest[i]=='\0') digest[i]++;
04676
04677 }
04678
04679 cp = response;
04680 OK=1;
04681 for (i=0;i<RESPONSE_LEN;i++) {
04682 if (*cp++ != digest[i]) OK=0;
04683 }
04684
04685 memset(md5Buf, 0, sizeof(md5Buf));
04686 if (OK==1) {
04687 rstrcpy(goodPw, cpw, MAX_PASSWORD_LEN);
04688 cpw+=MAX_PASSWORD_LEN;
04689 rstrcpy(goodPwExpiry, cpw, MAX_PASSWORD_LEN);
04690 cpw+=MAX_PASSWORD_LEN;
04691 rstrcpy(goodPwTs, cpw, MAX_PASSWORD_LEN);
04692 cpw+=MAX_PASSWORD_LEN;
04693 rstrcpy(goodPwModTs, cpw, MAX_PASSWORD_LEN);
04694 break;
04695 }
04696 cpw+=MAX_PASSWORD_LEN*4;
04697 }
04698 memset(pwInfoArray, 0, sizeof(pwInfoArray));
04699
04700 if (OK==0) {
04701 prevFailure++;
04702 return(CAT_INVALID_AUTHENTICATION);
04703 }
04704
04705 expireTime=atoll(goodPwExpiry);
04706 getNowStr(myTime);
04707 nowTime=atoll(myTime);
04708
04709
04710 pamMaxTime=atoll(eirods_pam_password_max_time);
04711 pamMinTime=atoll(eirods_pam_password_min_time);
04712
04713 if( ( strncmp(goodPwExpiry, "9999",4)!=0) &&
04714 expireTime >= pamMinTime &&
04715 expireTime <= pamMaxTime) {
04716 time_t modTime;
04717
04718 getNowStr(myTime);
04719 nowTime=atoll(myTime);
04720 modTime=atoll(goodPwModTs);
04721
04722 if (modTime+expireTime < nowTime) {
04723
04724 cllBindVars[cllBindVarCount++]=lastPw;
04725 cllBindVars[cllBindVarCount++]=goodPwTs;
04726 cllBindVars[cllBindVarCount++]=userName2;
04727 cllBindVars[cllBindVarCount++]=myUserZone;
04728 if (logSQL!=0)
04729 rodsLog(LOG_SQL, "chlCheckAuth SQL 2");
04730 status = cmlExecuteNoAnswerSql("delete from R_USER_PASSWORD where rcat_password=? and create_ts=? and user_id = (select user_id from R_USER_MAIN where user_name=? and zone_name=?)", &icss);
04731 memset(goodPw, 0, sizeof(goodPw));
04732 memset(lastPw, 0, sizeof(lastPw));
04733 if (status != 0) {
04734 rodsLog(LOG_NOTICE,
04735 "chlCheckAuth cmlExecuteNoAnswerSql delete expired password failure %d",
04736 status);
04737 return(status);
04738 }
04739 status = cmlExecuteNoAnswerSql("commit", &icss);
04740 if (status != 0) {
04741 rodsLog(LOG_NOTICE,
04742 "chlCheckAuth cmlExecuteNoAnswerSql commit failure %d",
04743 status);
04744 return(status);
04745 }
04746 return(CAT_PASSWORD_EXPIRED);
04747 }
04748 }
04749
04750
04751 if (expireTime < TEMP_PASSWORD_MAX_TIME) {
04752
04753
04754
04755 time_t createTime;
04756 int returnExpired;
04757
04758
04759
04760 returnExpired=0;
04761 getNowStr(myTime);
04762 nowTime=atoll(myTime);
04763 createTime=atoll(goodPwTs);
04764 if (createTime==0 || nowTime==0 ) returnExpired=1;
04765 if (createTime+expireTime < nowTime) returnExpired=1;
04766
04767
04768
04769
04770 cllBindVars[cllBindVarCount++]=goodPw;
04771 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 2");
04772 status = cmlExecuteNoAnswerSql(
04773 "delete from R_USER_PASSWORD where rcat_password=?",
04774 &icss);
04775 if (status !=0) {
04776 rodsLog(LOG_NOTICE,
04777 "chlCheckAuth cmlExecuteNoAnswerSql delete failure %d",
04778 status);
04779 _rollback("chlCheckAuth");
04780 return(status);
04781 }
04782
04783
04784
04785 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 3");
04786 snprintf(expireStr, sizeof expireStr, "%d", TEMP_PASSWORD_TIME);
04787 cllBindVars[cllBindVarCount++]=expireStr;
04788
04789 pwExpireMaxCreateTime = nowTime-TEMP_PASSWORD_TIME;
04790
04791 snprintf(expireStrCreate, sizeof expireStrCreate, "%011d",
04792 (int)pwExpireMaxCreateTime);
04793 cllBindVars[cllBindVarCount++]=expireStrCreate;
04794
04795 status = cmlExecuteNoAnswerSql(
04796 "delete from R_USER_PASSWORD where pass_expiry_ts = ? and create_ts < ?",
04797 &icss);
04798 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
04799 rodsLog(LOG_NOTICE,
04800 "chlCheckAuth cmlExecuteNoAnswerSql delete2 failure %d",
04801 status);
04802 _rollback("chlCheckAuth");
04803 return(status);
04804 }
04805
04806 memset(goodPw, 0, MAX_PASSWORD_LEN);
04807 if (returnExpired) return(CAT_PASSWORD_EXPIRED);
04808
04809 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 4");
04810 status = cmlExecuteNoAnswerSql("commit", &icss);
04811 if (status != 0) {
04812 rodsLog(LOG_NOTICE,
04813 "chlCheckAuth cmlExecuteNoAnswerSql commit failure %d",
04814 status);
04815 return(status);
04816 }
04817 memset(goodPw, 0, MAX_PASSWORD_LEN);
04818 if (returnExpired) return(CAT_PASSWORD_EXPIRED);
04819 }
04820
04821
04822 checkLevel:
04823
04824 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 5");
04825 status = cmlGetStringValueFromSql(
04826 "select user_type_name from R_USER_MAIN where user_name=? and zone_name=?",
04827 userType, MAX_NAME_LEN, userName2, myUserZone, 0, &icss);
04828 if (status !=0) {
04829 if (status == CAT_NO_ROWS_FOUND) {
04830 status = CAT_INVALID_USER;
04831 }
04832 else {
04833 _rollback("chlCheckAuth");
04834 }
04835 return(status);
04836 }
04837 *userPrivLevel = LOCAL_USER_AUTH;
04838 if (strcmp(userType, "rodsadmin") == 0) {
04839 *userPrivLevel = LOCAL_PRIV_USER_AUTH;
04840
04841
04842 if (strcmp( rsComm->clientUser.userName, userName2)==0 &&
04843 strcmp( rsComm->clientUser.rodsZone, userZone)==0) {
04844 *clientPrivLevel = LOCAL_PRIV_USER_AUTH;
04845 }
04846 else {
04847 if (rsComm->clientUser.userName[0]=='\0') {
04848
04849
04850
04851
04852
04853
04854
04855
04856
04857
04858
04859
04860 *clientPrivLevel = REMOTE_USER_AUTH;
04861 prevFailure=0;
04862 return(0);
04863 }
04864 else {
04865 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckAuth SQL 6");
04866 status = cmlGetStringValueFromSql(
04867 "select user_type_name from R_USER_MAIN where user_name=? and zone_name=?",
04868 userType, MAX_NAME_LEN, rsComm->clientUser.userName,
04869 rsComm->clientUser.rodsZone, 0, &icss);
04870 if (status !=0) {
04871 if (status == CAT_NO_ROWS_FOUND) {
04872 status = CAT_INVALID_CLIENT_USER;
04873 }
04874 else {
04875 _rollback("chlCheckAuth");
04876 }
04877 return(status);
04878 }
04879 *clientPrivLevel = LOCAL_USER_AUTH;
04880 if (strcmp(userType, "rodsadmin") == 0) {
04881 *clientPrivLevel = LOCAL_PRIV_USER_AUTH;
04882 }
04883 }
04884 }
04885 }
04886
04887 prevFailure=0;
04888 return(0);
04889 }
04890
04891
04892
04893
04894
04895
04896
04897
04898
04899 int chlMakeTempPw(rsComm_t *rsComm, char *pwValueToHash) {
04900 int status;
04901 char md5Buf[100];
04902 unsigned char digest[RESPONSE_LEN+2];
04903 MD5_CTX context;
04904 int i;
04905 char password[MAX_PASSWORD_LEN+10];
04906 char newPw[MAX_PASSWORD_LEN+10];
04907 char myTime[50];
04908 char myTimeExp[50];
04909 char rBuf[200];
04910 char hashValue[50];
04911 int j=0;
04912 char tSQL[MAX_SQL_SIZE];
04913
04914 if (logSQL!=0) rodsLog(LOG_SQL, "chlMakeTempPw");
04915
04916 if (logSQL!=0) rodsLog(LOG_SQL, "chlMakeTempPw SQL 1 ");
04917
04918 snprintf(tSQL, MAX_SQL_SIZE,
04919 "select rcat_password from R_USER_PASSWORD, R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=? and R_USER_MAIN.user_id = R_USER_PASSWORD.user_id and pass_expiry_ts != '%d'",
04920 TEMP_PASSWORD_TIME);
04921
04922 status = cmlGetStringValueFromSql(tSQL,
04923 password, MAX_PASSWORD_LEN,
04924 rsComm->clientUser.userName,
04925 rsComm->clientUser.rodsZone, 0, &icss);
04926 if (status !=0) {
04927 if (status == CAT_NO_ROWS_FOUND) {
04928 status = CAT_INVALID_USER;
04929 }
04930 else {
04931 _rollback("chlMakeTempPw");
04932 }
04933 return(status);
04934 }
04935
04936 icatDescramble(password);
04937
04938 j=0;
04939 get64RandomBytes(rBuf);
04940 for (i=0;i<50 && j<MAX_PASSWORD_LEN-1;i++) {
04941 char c;
04942 c = rBuf[i] &0x7f;
04943 if (c < '0') c+='0';
04944 if ( (c > 'a' && c < 'z') || (c > 'A' && c < 'Z') ||
04945 (c > '0' && c < '9') ){
04946 hashValue[j++]=c;
04947 }
04948 }
04949 hashValue[j]='\0';
04950
04951
04952
04953
04954 memset(md5Buf, 0, sizeof(md5Buf));
04955 strncpy(md5Buf, hashValue, 100);
04956 strncat(md5Buf, password, 100);
04957
04958 MD5Init (&context);
04959 MD5Update (&context, (unsigned char *) md5Buf, 100);
04960 MD5Final ((unsigned char *) digest, &context);
04961
04962 md5ToStr(digest, newPw);
04963
04964
04965 rstrcpy(pwValueToHash, hashValue, MAX_PASSWORD_LEN);
04966
04967
04968
04969
04970 getNowStr(myTime);
04971 sprintf(myTimeExp, "%d", TEMP_PASSWORD_TIME);
04972
04973 cllBindVars[cllBindVarCount++]=rsComm->clientUser.userName;
04974 cllBindVars[cllBindVarCount++]=rsComm->clientUser.rodsZone,
04975 cllBindVars[cllBindVarCount++]=newPw;
04976 cllBindVars[cllBindVarCount++]=myTimeExp;
04977 cllBindVars[cllBindVarCount++]=myTime;
04978 cllBindVars[cllBindVarCount++]=myTime;
04979 if (logSQL!=0) rodsLog(LOG_SQL, "chlMakeTempPw SQL 2");
04980 status = cmlExecuteNoAnswerSql(
04981 "insert into R_USER_PASSWORD (user_id, rcat_password, pass_expiry_ts, create_ts, modify_ts) values ((select user_id from R_USER_MAIN where user_name=? and zone_name=?), ?, ?, ?, ?)",
04982 &icss);
04983 if (status !=0) {
04984 rodsLog(LOG_NOTICE,
04985 "chlMakeTempPw cmlExecuteNoAnswerSql insert failure %d",
04986 status);
04987 _rollback("chlMakeTempPw");
04988 return(status);
04989 }
04990
04991 status = cmlExecuteNoAnswerSql("commit", &icss);
04992 if (status != 0) {
04993 rodsLog(LOG_NOTICE,
04994 "chlMakeTempPw cmlExecuteNoAnswerSql commit failure %d",
04995 status);
04996 return(status);
04997 }
04998
04999 memset(newPw, 0, MAX_PASSWORD_LEN);
05000 return(0);
05001 }
05002
05003
05004
05005
05006
05007
05008 int decodePw(rsComm_t *rsComm, char *in, char *out) {
05009 int status;
05010 char *cp;
05011 char password[MAX_PASSWORD_LEN];
05012 char upassword[MAX_PASSWORD_LEN+10];
05013 char rand[]=
05014 "1gCBizHWbwIYyWLo";
05015 int pwLen1, pwLen2;
05016
05017 if (logSQL!=0) rodsLog(LOG_SQL, "decodePw - SQL 1 ");
05018 status = cmlGetStringValueFromSql(
05019 "select rcat_password from R_USER_PASSWORD, R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=? and R_USER_MAIN.user_id = R_USER_PASSWORD.user_id",
05020 password, MAX_PASSWORD_LEN,
05021 rsComm->clientUser.userName,
05022 rsComm->clientUser.rodsZone,
05023 0, &icss);
05024 if (status !=0) {
05025 if (status == CAT_NO_ROWS_FOUND) {
05026 status = CAT_INVALID_USER;
05027 }
05028 else {
05029 _rollback("decodePw");
05030 }
05031 return(status);
05032 }
05033
05034 icatDescramble(password);
05035
05036 obfDecodeByKeyV2(in, password, prevChalSig, upassword);
05037
05038 pwLen1=strlen(upassword);
05039
05040 memset(password, 0, MAX_PASSWORD_LEN);
05041
05042 cp = strstr(upassword, rand);
05043 if (cp !=NULL) *cp='\0';
05044
05045 pwLen2 = strlen(upassword);
05046
05047 if (pwLen2 > MAX_PASSWORD_LEN-5 && pwLen2==pwLen1) {
05048
05049 char errMsg[160];
05050 snprintf(errMsg, 150,
05051 "Error with password encoding.\nPlease try connecting directly to the ICAT host (setenv irodsHost)");
05052 addRErrorMsg (&rsComm->rError, 0, errMsg);
05053 return(CAT_PASSWORD_ENCODING_ERROR);
05054 }
05055 strcpy(out, upassword);
05056 memset(upassword, 0, MAX_PASSWORD_LEN);
05057
05058 return(0);
05059 }
05060
05061
05062
05063
05064
05065
05066
05067
05068
05069
05070 int chlUpdateIrodsPamPassword(rsComm_t *rsComm,
05071 char *username, int timeToLive,
05072 char *testTime,
05073 char **irodsPassword) {
05074 char myTime[50];
05075 char rBuf[200];
05076 int i, j;
05077 char randomPw[50];
05078 char randomPwEncoded[50];
05079 int status;
05080 char passwordInIcat[MAX_PASSWORD_LEN+2];
05081 char passwordModifyTime[50];
05082 char *cVal[3];
05083 int iVal[3];
05084 char selUserId[MAX_NAME_LEN];
05085 char expTime[50];
05086
05087 status = getLocalZone();
05088 if (status != 0) return(status);
05089
05090 getNowStr(myTime);
05091
05092
05093 if (timeToLive == 0) {
05094 rstrcpy(expTime, eirods_pam_password_default_time, sizeof expTime);
05095 }
05096 else {
05097
05098 rodsLong_t pamMinTime, pamMaxTime;
05099 pamMinTime=atoll(eirods_pam_password_min_time);
05100 pamMaxTime=atoll(eirods_pam_password_max_time);
05101 timeToLive = timeToLive * 3600;
05102 if (timeToLive < pamMinTime ||
05103 timeToLive > pamMaxTime) {
05104 return PAM_AUTH_PASSWORD_INVALID_TTL;
05105 }
05106 snprintf(expTime, sizeof expTime, "%d", timeToLive);
05107 }
05108
05109
05110 if (logSQL!=0) rodsLog(LOG_SQL, "chlUpdateIrodsPamPassword SQL 1");
05111 status = cmlGetStringValueFromSql(
05112 "select user_id from R_USER_MAIN where user_name=? and zone_name=? and user_type_name!='rodsgroup'",
05113 selUserId, MAX_NAME_LEN, username, localZone, 0, &icss);
05114 if (status==CAT_NO_ROWS_FOUND) return (CAT_INVALID_USER);
05115 if (status) return(status);
05116
05117
05118 if (logSQL!=0) rodsLog(LOG_SQL, "chlUpdateIrodsPamPassword SQL 2");
05119 cllBindVars[cllBindVarCount++]=eirods_pam_password_min_time;
05120 cllBindVars[cllBindVarCount++]=eirods_pam_password_max_time;
05121 cllBindVars[cllBindVarCount++]=myTime;
05122 #if MY_ICAT
05123 status = cmlExecuteNoAnswerSql("delete from R_USER_PASSWORD where pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as signed integer)>=? and cast(pass_expiry_ts as signed integer)<=? and (cast(pass_expiry_ts as signed integer) + cast(modify_ts as signed integer) < ?)",
05124 #else
05125 status = cmlExecuteNoAnswerSql("delete from R_USER_PASSWORD where pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as integer)>=? and cast(pass_expiry_ts as integer)<=? and (cast(pass_expiry_ts as integer) + cast(modify_ts as integer) < ?)",
05126 #endif
05127 &icss);
05128 if (logSQL!=0) rodsLog(LOG_SQL, "chlUpdateIrodsPamPassword SQL 3");
05129 cVal[0]=passwordInIcat;
05130 iVal[0]=MAX_PASSWORD_LEN;
05131 cVal[1]=passwordModifyTime;
05132 iVal[1]=sizeof(passwordModifyTime);
05133 status = cmlGetStringValuesFromSql(
05134 #if MY_ICAT
05135 "select rcat_password, modify_ts from R_USER_PASSWORD where user_id=? and pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as signed integer) >= ? and cast (pass_expiry_ts as signed integer) <= ?",
05136 #else
05137 "select rcat_password, modify_ts from R_USER_PASSWORD where user_id=? and pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as integer) >= ? and cast (pass_expiry_ts as integer) <= ?",
05138 #endif
05139 cVal, iVal, 2,
05140 selUserId,
05141 eirods_pam_password_min_time,
05142 eirods_pam_password_max_time, &icss);
05143
05144 if (status==0) {
05145 if( !eirods_pam_auth_no_extend ) {
05146 if (logSQL!=0) rodsLog(LOG_SQL, "chlUpdateIrodsPamPassword SQL 4");
05147 cllBindVars[cllBindVarCount++]=myTime;
05148 cllBindVars[cllBindVarCount++]=expTime;
05149 cllBindVars[cllBindVarCount++]=selUserId;
05150 cllBindVars[cllBindVarCount++]=passwordInIcat;
05151 status = cmlExecuteNoAnswerSql("update R_USER_PASSWORD set modify_ts=?, pass_expiry_ts=? where user_id = ? and rcat_password = ?",
05152 &icss);
05153 if (status) return(status);
05154
05155 status = cmlExecuteNoAnswerSql("commit", &icss);
05156 if (status != 0) {
05157 rodsLog(LOG_NOTICE,
05158 "chlUpdateIrodsPamPassword cmlExecuteNoAnswerSql commit failure %d",
05159 status);
05160 return(status);
05161 }
05162 }
05163
05164 icatDescramble(passwordInIcat);
05165 strncpy( *irodsPassword, passwordInIcat, eirods_pam_password_len );
05166 return(0);
05167 }
05168
05169
05170
05171
05172
05173
05174
05175 bool pw_good = false;
05176 while( !pw_good ) {
05177
05178 j=0;
05179 get64RandomBytes(rBuf);
05180 for (i=0;i<50 && j<eirods_pam_password_len-1;i++) {
05181 char c;
05182 c = rBuf[i] & 0x7f;
05183 if (c < '0') c+='0';
05184 if ( (c > 'a' && c < 'z') || (c > 'A' && c < 'Z') ||
05185 (c > '0' && c < '9') ){
05186 randomPw[j++]=c;
05187 }
05188 }
05189 randomPw[j]='\0';
05190
05191 strncpy(randomPwEncoded, randomPw, 50);
05192 icatScramble(randomPwEncoded);
05193 if( !strstr( randomPwEncoded, "\'" ) ) {
05194 pw_good = true;
05195
05196 } else {
05197 rodsLog( LOG_STATUS, "chlUpdateIrodsPamPassword :: getting a new password [%s] has a single quote", randomPwEncoded );
05198
05199 }
05200
05201 }
05202
05203 if (testTime!=NULL && strlen(testTime)>0) {
05204 strncpy(myTime, testTime, sizeof(myTime));
05205 }
05206
05207 if (logSQL!=0) rodsLog(LOG_SQL, "chlUpdateIrodsPamPassword SQL 5");
05208 cllBindVars[cllBindVarCount++]=selUserId;
05209 cllBindVars[cllBindVarCount++]=randomPwEncoded;
05210 cllBindVars[cllBindVarCount++]=expTime;
05211 cllBindVars[cllBindVarCount++]=myTime;
05212 cllBindVars[cllBindVarCount++]=myTime;
05213 status = cmlExecuteNoAnswerSql("insert into R_USER_PASSWORD (user_id, rcat_password, pass_expiry_ts, create_ts, modify_ts) values (?, ?, ?, ?, ?)",
05214 &icss);
05215 if (status) return(status);
05216
05217 status = cmlExecuteNoAnswerSql("commit", &icss);
05218 if (status != 0) {
05219 rodsLog(LOG_NOTICE,
05220 "chlUpdateIrodsPamPassword cmlExecuteNoAnswerSql commit failure %d",
05221 status);
05222 return(status);
05223 }
05224
05225 strncpy(*irodsPassword, randomPw, eirods_pam_password_len);
05226 return(0);
05227 }
05228
05229
05230
05231
05232 int chlModUser(rsComm_t *rsComm, char *userName, char *option,
05233 char *newValue) {
05234 int status;
05235 int opType;
05236 char decoded[MAX_PASSWORD_LEN+20];
05237 char tSQL[MAX_SQL_SIZE];
05238 char form1[]="update R_USER_MAIN set %s=?, modify_ts=? where user_name=? and zone_name=?";
05239 char form2[]="update R_USER_MAIN set %s=%s, modify_ts=? where user_name=? and zone_name=?";
05240 char form3[]="update R_USER_PASSWORD set rcat_password=?, modify_ts=? where user_id=?";
05241 char form4[]="insert into R_USER_PASSWORD (user_id, rcat_password, pass_expiry_ts, create_ts, modify_ts) values ((select user_id from R_USER_MAIN where user_name=? and zone_name=?), ?, ?, ?, ?)";
05242 char form5[]="insert into R_USER_AUTH (user_id, user_auth_name, create_ts) values ((select user_id from R_USER_MAIN where user_name=? and zone_name=?), ?, ?)";
05243 char form6[]="delete from R_USER_AUTH where user_id = (select user_id from R_USER_MAIN where user_name=? and zone_name=?) and user_auth_name = ?";
05244 #if MY_ICAT
05245 char form7[]="delete from R_USER_PASSWORD where pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as signed integer)>=? and cast(pass_expiry_ts as signed integer)<=? and user_id = (select user_id from R_USER_MAIN where user_name=? and zone_name=?)";
05246 #else
05247 char form7[]="delete from R_USER_PASSWORD where pass_expiry_ts not like '9999%' and cast(pass_expiry_ts as integer)>=? and cast(pass_expiry_ts as integer)<=? and user_id = (select user_id from R_USER_MAIN where user_name=? and zone_name=?)";
05248 #endif
05249
05250 char myTime[50];
05251 rodsLong_t iVal;
05252
05253 int auditId;
05254 char auditComment[110];
05255 char auditUserName[110];
05256 int userSettingOwnPassword;
05257 int groupAdminSettingPassword;
05258
05259 char userName2[NAME_LEN];
05260 char zoneName[NAME_LEN];
05261
05262 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser");
05263
05264 if (userName == NULL || option == NULL || newValue==NULL) {
05265 return (CAT_INVALID_ARGUMENT);
05266 }
05267
05268 if (*userName == '\0' || *option == '\0' || newValue=='\0') {
05269 return (CAT_INVALID_ARGUMENT);
05270 }
05271
05272 userSettingOwnPassword=0;
05273
05274
05275 groupAdminSettingPassword=0;
05276 if (rsComm->clientUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH &&
05277 rsComm->proxyUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH) {
05278
05279 }
05280 else {
05281
05282 if ( strcmp(option,"password")!=0) {
05283
05284 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
05285 }
05286 if ( strcmp(userName, rsComm->clientUser.userName)==0) {
05287 userSettingOwnPassword=1;
05288 }
05289 else {
05290 int status2;
05291 status2 = cmlCheckGroupAdminAccess(
05292 rsComm->clientUser.userName,
05293 rsComm->clientUser.rodsZone,
05294 "", &icss);
05295 if (status2 != 0) return(status2);
05296 groupAdminSettingPassword=1;
05297 }
05298
05299 if (userSettingOwnPassword==0 && groupAdminSettingPassword==0) {
05300 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
05301 }
05302 }
05303
05304 status = getLocalZone();
05305 if (status != 0) return(status);
05306
05307 tSQL[0]='\0';
05308 opType=0;
05309
05310 getNowStr(myTime);
05311
05312 auditComment[0]='\0';
05313 strncpy(auditUserName,userName,100);
05314
05315 status = parseUserName(userName, userName2, zoneName);
05316 if (zoneName[0]=='\0') {
05317 rstrcpy(zoneName, localZone, NAME_LEN);
05318 }
05319 if (status != 0) {
05320 return (CAT_INVALID_ARGUMENT);
05321 }
05322
05323 #if 0
05324
05325
05326 if (strcmp(option,"name" )==0 ||
05327 strcmp(option,"user_name" )==0) {
05328 snprintf(tSQL, MAX_SQL_SIZE, form1,
05329 "user_name");
05330 cllBindVars[cllBindVarCount++]=newValue;
05331 cllBindVars[cllBindVarCount++]=myTime;
05332 cllBindVars[cllBindVarCount++]=userName2;
05333 cllBindVars[cllBindVarCount++]=zoneName;
05334 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUserSQLxx1x");
05335 auditId = AU_MOD_USER_NAME;
05336 strncpy(auditComment, userName, 100);
05337 strncpy(auditUserName,newValue,100);
05338 }
05339 #endif
05340 if (strcmp(option,"type")==0 ||
05341 strcmp(option,"user_type_name")==0) {
05342 char tsubSQL[MAX_SQL_SIZE];
05343 snprintf(tsubSQL, MAX_SQL_SIZE, "(select token_name from R_TOKN_MAIN where token_namespace='user_type' and token_name=?)");
05344 cllBindVars[cllBindVarCount++]=newValue;
05345 snprintf(tSQL, MAX_SQL_SIZE, form2,
05346 "user_type_name", tsubSQL);
05347 cllBindVars[cllBindVarCount++]=myTime;
05348 cllBindVars[cllBindVarCount++]=userName2;
05349 cllBindVars[cllBindVarCount++]=zoneName;
05350 opType=1;
05351 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 2");
05352 auditId = AU_MOD_USER_TYPE;
05353 strncpy(auditComment, newValue, 100);
05354 }
05355 if (strcmp(option,"zone")==0 ||
05356 strcmp(option,"zone_name")==0) {
05357 snprintf(tSQL, MAX_SQL_SIZE, form1, "zone_name");
05358 cllBindVars[cllBindVarCount++]=newValue;
05359 cllBindVars[cllBindVarCount++]=myTime;
05360 cllBindVars[cllBindVarCount++]=userName2;
05361 cllBindVars[cllBindVarCount++]=zoneName;
05362 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 3");
05363 auditId = AU_MOD_USER_ZONE;
05364 strncpy(auditComment, newValue, 100);
05365 strncpy(auditUserName,userName,100);
05366 }
05367 if (strcmp(option,"addAuth")==0) {
05368 opType=4;
05369 rstrcpy(tSQL, form5, MAX_SQL_SIZE);
05370 cllBindVars[cllBindVarCount++]=userName2;
05371 cllBindVars[cllBindVarCount++]=zoneName;
05372 cllBindVars[cllBindVarCount++]=newValue;
05373 cllBindVars[cllBindVarCount++]=myTime;
05374 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 4");
05375 auditId = AU_ADD_USER_AUTH_NAME;
05376 strncpy(auditComment, newValue, 100);
05377 }
05378 if (strcmp(option,"rmAuth")==0) {
05379 rstrcpy(tSQL, form6, MAX_SQL_SIZE);
05380 cllBindVars[cllBindVarCount++]=userName2;
05381 cllBindVars[cllBindVarCount++]=zoneName;
05382 cllBindVars[cllBindVarCount++]=newValue;
05383 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 5");
05384 auditId = AU_DELETE_USER_AUTH_NAME;
05385 strncpy(auditComment, newValue, 100);
05386
05387 }
05388
05389 if (strncmp(option, "rmPamPw", 9)==0) {
05390 rstrcpy(tSQL, form7, MAX_SQL_SIZE);
05391 cllBindVars[cllBindVarCount++]=eirods_pam_password_min_time;
05392 cllBindVars[cllBindVarCount++]=eirods_pam_password_max_time;
05393 cllBindVars[cllBindVarCount++]=userName2;
05394 cllBindVars[cllBindVarCount++]=zoneName;
05395 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 6");
05396 auditId = AU_MOD_USER_PASSWORD;
05397 strncpy(auditComment, "Deleted user iRODS-PAM password (if any)", 100);
05398 }
05399
05400 if (strcmp(option,"info")==0 ||
05401 strcmp(option,"user_info")==0) {
05402 snprintf(tSQL, MAX_SQL_SIZE, form1,
05403 "user_info");
05404 cllBindVars[cllBindVarCount++]=newValue;
05405 cllBindVars[cllBindVarCount++]=myTime;
05406 cllBindVars[cllBindVarCount++]=userName2;
05407 cllBindVars[cllBindVarCount++]=zoneName;
05408 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 6");
05409 auditId = AU_MOD_USER_INFO;
05410 strncpy(auditComment, newValue, 100);
05411 }
05412 if (strcmp(option,"comment")==0 ||
05413 strcmp(option,"r_comment")==0) {
05414 snprintf(tSQL, MAX_SQL_SIZE, form1,
05415 "r_comment");
05416 cllBindVars[cllBindVarCount++]=newValue;
05417 cllBindVars[cllBindVarCount++]=myTime;
05418 cllBindVars[cllBindVarCount++]=userName2;
05419 cllBindVars[cllBindVarCount++]=zoneName;
05420 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 7");
05421 auditId = AU_MOD_USER_COMMENT;
05422 strncpy(auditComment, newValue, 100);
05423 }
05424 if (strcmp(option,"password")==0) {
05425 int i;
05426 char userIdStr[MAX_NAME_LEN];
05427 i = decodePw(rsComm, newValue, decoded);
05428
05429 icatScramble(decoded);
05430
05431 if (i) return(i);
05432 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 8");
05433 i = cmlGetStringValueFromSql(
05434 "select R_USER_PASSWORD.user_id from R_USER_PASSWORD, R_USER_MAIN where R_USER_MAIN.user_name=? and R_USER_MAIN.zone_name=? and R_USER_MAIN.user_id = R_USER_PASSWORD.user_id",
05435 userIdStr, MAX_NAME_LEN, userName2, zoneName, 0, &icss);
05436 if (i != 0 && i !=CAT_NO_ROWS_FOUND) return(i);
05437 if (i == 0) {
05438 if (groupAdminSettingPassword == 1) {
05439
05440 return( CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
05441 }
05442 rstrcpy(tSQL, form3, MAX_SQL_SIZE);
05443 cllBindVars[cllBindVarCount++]=decoded;
05444 cllBindVars[cllBindVarCount++]=myTime;
05445 cllBindVars[cllBindVarCount++]=userIdStr;
05446 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 9");
05447 }
05448 else {
05449 opType=4;
05450 rstrcpy(tSQL, form4, MAX_SQL_SIZE);
05451 cllBindVars[cllBindVarCount++]=userName2;
05452 cllBindVars[cllBindVarCount++]=zoneName;
05453 cllBindVars[cllBindVarCount++]=decoded;
05454 cllBindVars[cllBindVarCount++]="9999-12-31-23.59.01";
05455 cllBindVars[cllBindVarCount++]=myTime;
05456 cllBindVars[cllBindVarCount++]=myTime;
05457 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 10");
05458 }
05459 auditId = AU_MOD_USER_PASSWORD;
05460 }
05461
05462 if (tSQL[0]=='\0') {
05463 return (CAT_INVALID_ARGUMENT);
05464 }
05465
05466 status = cmlExecuteNoAnswerSql(tSQL, &icss);
05467 memset(decoded, 0, MAX_PASSWORD_LEN);
05468
05469 if (status != 0 ) {
05470 if (opType==1) {
05471 int status2;
05472 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 11");
05473 status2 = cmlGetIntegerValueFromSql(
05474 "select token_name from R_TOKN_MAIN where token_namespace='user_type' and token_name=?",
05475 &iVal, newValue, 0, 0, 0, 0, &icss);
05476 if (status2 != 0) {
05477 char errMsg[105];
05478 snprintf(errMsg, 100, "user_type '%s' is not valid",
05479 newValue);
05480 addRErrorMsg (&rsComm->rError, 0, errMsg);
05481
05482 rodsLog(LOG_NOTICE,
05483 "chlModUser invalid user_type");
05484 return(CAT_INVALID_USER_TYPE);
05485 }
05486 }
05487 if (opType==4) {
05488
05489 int status2;
05490 _rollback("chlModUser");
05491 if (logSQL!=0) rodsLog(LOG_SQL, "chlModUser SQL 12");
05492 status2 = cmlGetIntegerValueFromSql(
05493 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
05494 &iVal, userName2, zoneName, 0, 0, 0, &icss);
05495 if (status2 != 0) {
05496 rodsLog(LOG_NOTICE,
05497 "chlModUser invalid user %s zone %s", userName2, zoneName);
05498 return(CAT_INVALID_USER);
05499 }
05500 }
05501 rodsLog(LOG_NOTICE,
05502 "chlModUser cmlExecuteNoAnswerSql failure %d",
05503 status);
05504 return(status);
05505 }
05506
05507 status = cmlAudit1(auditId, rsComm->clientUser.userName,
05508 localZone, auditUserName, auditComment, &icss);
05509 if (status != 0) {
05510 rodsLog(LOG_NOTICE,
05511 "chlModUser cmlAudit1 failure %d",
05512 status);
05513 _rollback("chlModUser");
05514 return(status);
05515 }
05516
05517 status = cmlExecuteNoAnswerSql("commit", &icss);
05518 if (status != 0) {
05519 rodsLog(LOG_NOTICE,
05520 "chlModUser cmlExecuteNoAnswerSql commit failure %d",
05521 status);
05522 return(status);
05523 }
05524 return(0);
05525 }
05526
05527
05528
05529
05530 int chlModGroup(rsComm_t *rsComm, char *groupName, char *option,
05531 char *userName, char *userZone) {
05532 int status, OK;
05533 char myTime[50];
05534 char userId[MAX_NAME_LEN];
05535 char groupId[MAX_NAME_LEN];
05536 char commentStr[100];
05537 char zoneToUse[MAX_NAME_LEN];
05538
05539 char userName2[NAME_LEN];
05540 char zoneName[NAME_LEN];
05541
05542
05543 if (logSQL!=0) rodsLog(LOG_SQL, "chlModGroup");
05544
05545 if (groupName == NULL || option == NULL || userName==NULL) {
05546 return (CAT_INVALID_ARGUMENT);
05547 }
05548
05549 if (*groupName == '\0' || *option == '\0' || userName=='\0') {
05550 return (CAT_INVALID_ARGUMENT);
05551 }
05552
05553 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ||
05554 rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
05555 int status2;
05556 status2 = cmlCheckGroupAdminAccess(
05557 rsComm->clientUser.userName,
05558 rsComm->clientUser.rodsZone, groupName, &icss);
05559 if (status2 != 0) return(status2);
05560 }
05561
05562 status = getLocalZone();
05563 if (status != 0) return(status);
05564
05565 strncpy(zoneToUse, localZone, MAX_NAME_LEN);
05566 if (userZone != NULL && *userZone != '\0') {
05567 strncpy(zoneToUse, userZone, MAX_NAME_LEN);
05568 }
05569
05570 status = parseUserName(userName, userName2, zoneName);
05571 if (zoneName[0]!='\0') {
05572 rstrcpy(zoneToUse, zoneName, NAME_LEN);
05573 }
05574
05575 userId[0]='\0';
05576 if (logSQL!=0) rodsLog(LOG_SQL, "chlModGroup SQL 1 ");
05577 status = cmlGetStringValueFromSql(
05578 "select user_id from R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=? and user_type_name !='rodsgroup'",
05579 userId, MAX_NAME_LEN, userName2, zoneToUse, 0, &icss);
05580 if (status != 0) {
05581 if (status==CAT_NO_ROWS_FOUND) {
05582 return(CAT_INVALID_USER);
05583 }
05584 _rollback("chlModGroup");
05585 return(status);
05586 }
05587
05588 groupId[0]='\0';
05589 if (logSQL!=0) rodsLog(LOG_SQL, "chlModGroup SQL 2");
05590 status = cmlGetStringValueFromSql(
05591 "select user_id from R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=? and user_type_name='rodsgroup'",
05592 groupId, MAX_NAME_LEN, groupName, localZone, 0, &icss);
05593 if (status != 0) {
05594 if (status==CAT_NO_ROWS_FOUND) {
05595 return(CAT_INVALID_GROUP);
05596 }
05597 _rollback("chlModGroup");
05598 return(status);
05599 }
05600 OK=0;
05601 if (strcmp(option, "remove")==0) {
05602 if (logSQL!=0) rodsLog(LOG_SQL, "chlModGroup SQL 3");
05603 cllBindVars[cllBindVarCount++]=groupId;
05604 cllBindVars[cllBindVarCount++]=userId;
05605 status = cmlExecuteNoAnswerSql(
05606 "delete from R_USER_GROUP where group_user_id = ? and user_id = ?",
05607 &icss);
05608 if (status != 0) {
05609 rodsLog(LOG_NOTICE,
05610 "chlModGroup cmlExecuteNoAnswerSql delete failure %d",
05611 status);
05612 _rollback("chlModGroup");
05613 return(status);
05614 }
05615 OK=1;
05616 }
05617
05618 if (strcmp(option, "add")==0) {
05619 getNowStr(myTime);
05620 cllBindVars[cllBindVarCount++]=groupId;
05621 cllBindVars[cllBindVarCount++]=userId;
05622 cllBindVars[cllBindVarCount++]=myTime;
05623 cllBindVars[cllBindVarCount++]=myTime;
05624 if (logSQL!=0) rodsLog(LOG_SQL, "chlModGroup SQL 4");
05625 status = cmlExecuteNoAnswerSql(
05626 "insert into R_USER_GROUP (group_user_id, user_id , create_ts, modify_ts) values (?, ?, ?, ?)",
05627 &icss);
05628 if (status != 0) {
05629 rodsLog(LOG_NOTICE,
05630 "chlModGroup cmlExecuteNoAnswerSql delete failure %d",
05631 status);
05632 _rollback("chlModGroup");
05633 return(status);
05634 }
05635 OK=1;
05636 }
05637
05638 if (OK==0) {
05639 return (CAT_INVALID_ARGUMENT);
05640 }
05641
05642
05643 snprintf(commentStr, sizeof commentStr, "%s %s", option, userId);
05644 status = cmlAudit3(AU_MOD_GROUP,
05645 groupId,
05646 rsComm->clientUser.userName,
05647 rsComm->clientUser.rodsZone,
05648 commentStr,
05649 &icss);
05650 if (status != 0) {
05651 rodsLog(LOG_NOTICE,
05652 "chlModGroup cmlAudit3 failure %d",
05653 status);
05654 _rollback("chlModGroup");
05655 return(status);
05656 }
05657
05658 status = cmlExecuteNoAnswerSql("commit", &icss);
05659 if (status != 0) {
05660 rodsLog(LOG_NOTICE,
05661 "chlModGroup cmlExecuteNoAnswerSql commit failure %d",
05662 status);
05663 return(status);
05664 }
05665 return(0);
05666 }
05667
05668
05669 int chlModResc(
05670 rsComm_t* rsComm,
05671 char* rescName,
05672 char* option,
05673 char* optionValue ) {
05674 int status, OK;
05675 char myTime[50];
05676 char rescId[MAX_NAME_LEN];
05677 char rescPath[MAX_NAME_LEN]="";
05678 char rescPathMsg[MAX_NAME_LEN+100];
05679 char commentStr[200];
05680 struct hostent *myHostEnt;
05681
05682 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc");
05683
05684 if (rescName == NULL || option==NULL || optionValue==NULL) {
05685 return (CAT_INVALID_ARGUMENT);
05686 }
05687
05688 if (*rescName == '\0' || *option == '\0' || *optionValue=='\0') {
05689 return (CAT_INVALID_ARGUMENT);
05690 }
05691
05692 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
05693 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
05694 }
05695 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
05696 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
05697 }
05698
05699
05700
05701 if (strncmp(rescName, BUNDLE_RESC, strlen(BUNDLE_RESC))==0) {
05702 char errMsg[155];
05703 snprintf(errMsg, 150,
05704 "%s is a built-in resource needed for bundle operations.",
05705 BUNDLE_RESC);
05706 addRErrorMsg (&rsComm->rError, 0, errMsg);
05707 return(CAT_PSEUDO_RESC_MODIFY_DISALLOWED);
05708 }
05709
05710 status = getLocalZone();
05711 if (status != 0) return(status);
05712
05713 rescId[0]='\0';
05714 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 1 ");
05715 status = cmlGetStringValueFromSql(
05716 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
05717 rescId, MAX_NAME_LEN, rescName, localZone, 0, &icss);
05718 if (status != 0) {
05719 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
05720 _rollback("chlModResc");
05721 return(status);
05722 }
05723
05724 getNowStr(myTime);
05725 OK=0;
05726 if (strcmp(option, "info")==0) {
05727 cllBindVars[cllBindVarCount++]=optionValue;
05728 cllBindVars[cllBindVarCount++]=myTime;
05729 cllBindVars[cllBindVarCount++]=rescId;
05730 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 2");
05731 status = cmlExecuteNoAnswerSql(
05732 "update R_RESC_MAIN set resc_info=?, modify_ts=? where resc_id=?",
05733 &icss);
05734 if (status != 0) {
05735 rodsLog(LOG_NOTICE,
05736 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05737 status);
05738 _rollback("chlModResc");
05739 return(status);
05740 }
05741 OK=1;
05742 }
05743 if (strcmp(option, "comment")==0) {
05744 cllBindVars[cllBindVarCount++]=optionValue;
05745 cllBindVars[cllBindVarCount++]=myTime;
05746 cllBindVars[cllBindVarCount++]=rescId;
05747 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 3");
05748 status = cmlExecuteNoAnswerSql(
05749 "update R_RESC_MAIN set r_comment = ?, modify_ts=? where resc_id=?",
05750 &icss);
05751 if (status != 0) {
05752 rodsLog(LOG_NOTICE,
05753 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05754 status);
05755 _rollback("chlModResc");
05756 return(status);
05757 }
05758 OK=1;
05759 }
05760 if (strcmp(option, "freespace")==0) {
05761 int inType=0;
05762 if (*optionValue=='+') {
05763 inType=1;
05764 optionValue++;
05765 }
05766 if (*optionValue=='-') {
05767 inType=2;
05768 optionValue++;
05769 }
05770 cllBindVars[cllBindVarCount++]=optionValue;
05771 cllBindVars[cllBindVarCount++]=myTime;
05772 cllBindVars[cllBindVarCount++]=myTime;
05773 cllBindVars[cllBindVarCount++]=rescId;
05774 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 4");
05775 if (inType==0) {
05776 status = cmlExecuteNoAnswerSql(
05777 "update R_RESC_MAIN set free_space = ?, free_space_ts = ?, modify_ts=? where resc_id=?",
05778 &icss);
05779 }
05780 if (inType==1) {
05781 #if ORA_ICAT
05782
05783 status = cmlExecuteNoAnswerSql(
05784 "update R_RESC_MAIN set free_space = cast(free_space as integer) + cast(? as integer), free_space_ts = ?, modify_ts=? where resc_id=?",
05785 &icss);
05786 #elif MY_ICAT
05787 status = cmlExecuteNoAnswerSql(
05788 "update R_RESC_MAIN set free_space = free_space + ?, free_space_ts = ?, modify_ts=? where resc_id=?",
05789 &icss);
05790 #else
05791 status = cmlExecuteNoAnswerSql(
05792 "update R_RESC_MAIN set free_space = cast(free_space as bigint) + cast(? as bigint), free_space_ts = ?, modify_ts=? where resc_id=?",
05793 &icss);
05794 #endif
05795 }
05796 if (inType==2) {
05797 #if ORA_ICAT
05798
05799 status = cmlExecuteNoAnswerSql(
05800 "update R_RESC_MAIN set free_space = cast(free_space as integer) - cast(? as integer), free_space_ts = ?, modify_ts=? where resc_id=?",
05801 &icss);
05802 #elif MY_ICAT
05803 status = cmlExecuteNoAnswerSql(
05804 "update R_RESC_MAIN set free_space = free_space - ?, free_space_ts = ?, modify_ts=? where resc_id=?",
05805 &icss);
05806 #else
05807 status = cmlExecuteNoAnswerSql(
05808 "update R_RESC_MAIN set free_space = cast(free_space as bigint) - cast(? as bigint), free_space_ts = ?, modify_ts=? where resc_id=?",
05809 &icss);
05810 #endif
05811 }
05812 if (status != 0) {
05813 rodsLog(LOG_NOTICE,
05814 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05815 status);
05816 _rollback("chlModResc");
05817 return(status);
05818 }
05819 OK=1;
05820 }
05821 if (strcmp(option, "host")==0) {
05822
05823
05824 myHostEnt = gethostbyname(optionValue);
05825 if (myHostEnt <= 0) {
05826 char errMsg[155];
05827 snprintf(errMsg, 150,
05828 "Warning, resource host address '%s' is not a valid DNS entry, gethostbyname failed.",
05829 optionValue);
05830 addRErrorMsg (&rsComm->rError, 0, errMsg);
05831 }
05832 if (strcmp(optionValue, "localhost") == 0) {
05833 addRErrorMsg (&rsComm->rError, 0,
05834 "Warning, resource host address 'localhost' will not work properly as it maps to the local host from each client.");
05835 }
05836
05837
05838 cllBindVars[cllBindVarCount++]=optionValue;
05839 cllBindVars[cllBindVarCount++]=myTime;
05840 cllBindVars[cllBindVarCount++]=rescId;
05841 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 5");
05842 status = cmlExecuteNoAnswerSql(
05843 "update R_RESC_MAIN set resc_net = ?, modify_ts=? where resc_id=?",
05844 &icss);
05845 if (status != 0) {
05846 rodsLog(LOG_NOTICE,
05847 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05848 status);
05849 _rollback("chlModResc");
05850 return(status);
05851 }
05852 OK=1;
05853 }
05854 if (strcmp(option, "type")==0) {
05855 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 6");
05856 #if 0 // JMC :: resource type is now dynamic
05857 status = cmlCheckNameToken("resc_type", optionValue, &icss);
05858 if (status !=0 ) {
05859 char errMsg[105];
05860 snprintf(errMsg, 100, "resource_type '%s' is not valid",
05861 optionValue);
05862 addRErrorMsg (&rsComm->rError, 0, errMsg);
05863 return(CAT_INVALID_RESOURCE_TYPE);
05864 }
05865 #endif
05866 cllBindVars[cllBindVarCount++]=optionValue;
05867 cllBindVars[cllBindVarCount++]=myTime;
05868 cllBindVars[cllBindVarCount++]=rescId;
05869 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 7");
05870 status = cmlExecuteNoAnswerSql(
05871 "update R_RESC_MAIN set resc_type_name = ?, modify_ts=? where resc_id=?",
05872 &icss);
05873 if (status != 0) {
05874 rodsLog(LOG_NOTICE,
05875 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05876 status);
05877 _rollback("chlModResc");
05878 return(status);
05879 }
05880 OK=1;
05881 }
05882
05883 #if 0 // JMC - no longer support resource classes
05884 if (strcmp(option, "class")==0) {
05885 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc S---Q---L 8");
05886 status = cmlCheckNameToken("resc_class", optionValue, &icss);
05887 if (status !=0 ) {
05888 char errMsg[105];
05889 snprintf(errMsg, 100, "resource_class '%s' is not valid",
05890 optionValue);
05891 addRErrorMsg (&rsComm->rError, 0, errMsg);
05892 return(CAT_INVALID_RESOURCE_CLASS);
05893 }
05894
05895 cllBindVars[cllBindVarCount++]=optionValue;
05896 cllBindVars[cllBindVarCount++]=myTime;
05897 cllBindVars[cllBindVarCount++]=rescId;
05898 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc S---Q---L 9");
05899 status = cmlExecuteNoAnswerSql(
05900 "update R_RESC_MAIN set resc_class_name = ?, modify_ts=? where resc_id=?",
05901 &icss);
05902 if (status != 0) {
05903 rodsLog(LOG_NOTICE,
05904 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05905 status);
05906 _rollback("chlModResc");
05907 return(status);
05908 }
05909 OK=1;
05910 }
05911 #endif
05912 if (strcmp(option, "path")==0) {
05913 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 10");
05914 status = cmlGetStringValueFromSql(
05915 "select resc_def_path from R_RESC_MAIN where resc_id=?",
05916 rescPath, MAX_NAME_LEN, rescId, 0, 0, &icss);
05917 if (status != 0) {
05918 rodsLog(LOG_NOTICE,
05919 "chlModResc cmlGetStringValueFromSql query failure %d",
05920 status);
05921 _rollback("chlModResc");
05922 return(status);
05923 }
05924
05925 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 11");
05926
05927 cllBindVars[cllBindVarCount++]=optionValue;
05928 cllBindVars[cllBindVarCount++]=myTime;
05929 cllBindVars[cllBindVarCount++]=rescId;
05930 status = cmlExecuteNoAnswerSql(
05931 "update R_RESC_MAIN set resc_def_path=?, modify_ts=? where resc_id=?",
05932 &icss);
05933 if (status != 0) {
05934 rodsLog(LOG_NOTICE,
05935 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05936 status);
05937 _rollback("chlModResc");
05938 return(status);
05939 }
05940 OK=1;
05941 }
05942
05943 if (strcmp(option, "status")==0) {
05944 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 12");
05945 cllBindVars[cllBindVarCount++]=optionValue;
05946 cllBindVars[cllBindVarCount++]=myTime;
05947 cllBindVars[cllBindVarCount++]=rescId;
05948 status = cmlExecuteNoAnswerSql(
05949 "update R_RESC_MAIN set resc_status=?, modify_ts=? where resc_id=?",
05950 &icss);
05951 if (status != 0) {
05952 rodsLog(LOG_NOTICE,
05953 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05954 status);
05955 _rollback("chlModResc");
05956 return(status);
05957 }
05958 OK=1;
05959 }
05960
05961 if (strcmp(option, "name")==0) {
05962 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 13");
05963 cllBindVars[cllBindVarCount++]=optionValue;
05964 cllBindVars[cllBindVarCount++]=myTime;
05965 cllBindVars[cllBindVarCount++]=rescId;
05966
05967 status = cmlExecuteNoAnswerSql(
05968 "update R_RESC_MAIN set resc_name=?, modify_ts=? where resc_id=?",
05969 &icss);
05970 if (status != 0) {
05971 rodsLog(LOG_NOTICE,
05972 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05973 status);
05974 _rollback("chlModResc");
05975 return(status);
05976 }
05977
05978 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 14");
05979 cllBindVars[cllBindVarCount++]=optionValue;
05980 cllBindVars[cllBindVarCount++]=rescName;
05981 status = cmlExecuteNoAnswerSql(
05982 "update R_DATA_MAIN set resc_name=? where resc_name=?",
05983 &icss);
05984 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
05985 if (status != 0) {
05986 rodsLog(LOG_NOTICE,
05987 "chlModResc cmlExecuteNoAnswerSql update failure %d",
05988 status);
05989 _rollback("chlModResc");
05990 return(status);
05991 }
05992
05993 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 15");
05994 cllBindVars[cllBindVarCount++]=optionValue;
05995 cllBindVars[cllBindVarCount++]=rescName;
05996 status = cmlExecuteNoAnswerSql(
05997 "update R_SERVER_LOAD set resc_name=? where resc_name=?",
05998 &icss);
05999 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
06000 if (status != 0) {
06001 rodsLog(LOG_NOTICE,
06002 "chlModResc cmlExecuteNoAnswerSql update failure %d",
06003 status);
06004 _rollback("chlModResc");
06005 return(status);
06006 }
06007
06008 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 16");
06009 cllBindVars[cllBindVarCount++]=optionValue;
06010 cllBindVars[cllBindVarCount++]=rescName;
06011 status = cmlExecuteNoAnswerSql(
06012 "update R_SERVER_LOAD_DIGEST set resc_name=? where resc_name=?",
06013 &icss);
06014 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
06015 if (status != 0) {
06016 rodsLog(LOG_NOTICE,
06017 "chlModResc cmlExecuteNoAnswerSql update failure %d",
06018 status);
06019 _rollback("chlModResc");
06020 return(status);
06021 }
06022
06023
06024 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 17");
06025 cllBindVars[cllBindVarCount++]=optionValue;
06026 cllBindVars[cllBindVarCount++]=rescName;
06027 status = cmlExecuteNoAnswerSql(
06028 "update R_RESC_MAIN set resc_parent=? where resc_parent=?",
06029 &icss);
06030 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
06031 if (status != 0) {
06032 rodsLog(LOG_NOTICE,
06033 "chlModResc cmlExecuteNoAnswerSql update failure %d",
06034 status);
06035 _rollback("chlModResc");
06036 return(status);
06037 }
06038
06039
06040 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 18");
06041 status = _modRescInHierarchies(rescName, optionValue);
06042 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
06043 if (status != 0) {
06044 rodsLog(LOG_NOTICE,
06045 "chlModResc: _modRescInHierarchies error, status = %d",
06046 status);
06047 _rollback("chlModResc");
06048 return(status);
06049 }
06050
06051
06052 if (logSQL!=0) rodsLog(LOG_SQL, "chlModResc SQL 19");
06053 status = _modRescInChildren(rescName, optionValue);
06054 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
06055 if (status != 0) {
06056 rodsLog(LOG_NOTICE,
06057 "chlModResc: _modRescInChildren error, status = %d",
06058 status);
06059 _rollback("chlModResc");
06060 return(status);
06061 }
06062
06063 OK=1;
06064 }
06065
06066 if (strcmp(option, "context")==0) {
06067 cllBindVars[cllBindVarCount++]=optionValue;
06068 cllBindVars[cllBindVarCount++]=myTime;
06069 cllBindVars[cllBindVarCount++]=rescId;
06070 status = cmlExecuteNoAnswerSql(
06071 "update R_RESC_MAIN set resc_context=?, modify_ts=? where resc_id=?",
06072 &icss);
06073 if (status != 0) {
06074 rodsLog(LOG_NOTICE,
06075 "chlModResc cmlExecuteNoAnswerSql update failure for resc context %d",
06076 status);
06077 _rollback("chlModResc");
06078 return(status);
06079 }
06080 OK=1;
06081 }
06082
06083 if (OK==0) {
06084 return (CAT_INVALID_ARGUMENT);
06085 }
06086
06087
06088 snprintf(commentStr, sizeof commentStr, "%s %s", option, optionValue);
06089 status = cmlAudit3(AU_MOD_RESC,
06090 rescId,
06091 rsComm->clientUser.userName,
06092 rsComm->clientUser.rodsZone,
06093 commentStr,
06094 &icss);
06095 if (status != 0) {
06096 rodsLog(LOG_NOTICE,
06097 "chlModResc cmlAudit3 failure %d",
06098 status);
06099 _rollback("chlModResc");
06100 return(status);
06101 }
06102
06103 status = cmlExecuteNoAnswerSql("commit", &icss);
06104 if (status != 0) {
06105 rodsLog(LOG_NOTICE,
06106 "chlModResc cmlExecuteNoAnswerSql commit failure %d",
06107 status);
06108 return(status);
06109 }
06110
06111 if (rescPath[0]!='\0') {
06112
06113
06114 snprintf(rescPathMsg, sizeof(rescPathMsg), "Previous resource path: %s",
06115 rescPath);
06116 addRErrorMsg (&rsComm->rError, 0, rescPathMsg);
06117 }
06118
06119 return(0);
06120 }
06121
06122
06123 int chlModRescDataPaths(rsComm_t *rsComm, char *rescName, char *oldPath,
06124 char *newPath, char *userName) {
06125 char rescId[MAX_NAME_LEN];
06126 int status, len, rows;
06127 char *cptr;
06128
06129 char userZone[NAME_LEN];
06130 char zoneToUse[NAME_LEN];
06131 char userName2[NAME_LEN];
06132
06133 char oldPath2[MAX_NAME_LEN];
06134
06135 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescDataPaths");
06136
06137 if (rescName == NULL || oldPath==NULL || newPath==NULL) {
06138 return (CAT_INVALID_ARGUMENT);
06139 }
06140
06141 if (*rescName == '\0' || *oldPath == '\0' || *newPath=='\0') {
06142 return (CAT_INVALID_ARGUMENT);
06143 }
06144
06145
06146 if (*oldPath != '/' or *newPath != '/') {
06147 return (CAT_INVALID_ARGUMENT);
06148 }
06149 len = strlen(oldPath);
06150 cptr = oldPath+len-1;
06151 if (*cptr != '/') return (CAT_INVALID_ARGUMENT);
06152 len = strlen(newPath);
06153 cptr = newPath+len-1;
06154 if (*cptr != '/') return (CAT_INVALID_ARGUMENT);
06155
06156 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06157 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06158 }
06159 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06160 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06161 }
06162
06163 status = getLocalZone();
06164 if (status != 0) return(status);
06165
06166 rescId[0]='\0';
06167 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescDataPaths SQL 1 ");
06168 status = cmlGetStringValueFromSql(
06169 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
06170 rescId, MAX_NAME_LEN, rescName, localZone, 0, &icss);
06171 if (status != 0) {
06172 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
06173 _rollback("chlModRescDataPaths");
06174 return(status);
06175 }
06176
06177
06178
06179
06180 strncpy(oldPath2, oldPath, sizeof(oldPath2));
06181 strncat(oldPath2, "%", sizeof(oldPath2));
06182
06183 if (userName != NULL && *userName != '\0') {
06184 strncpy(zoneToUse, localZone, NAME_LEN);
06185 status = parseUserName(userName, userName2, userZone);
06186 if (userZone[0]!='\0') {
06187 rstrcpy(zoneToUse, userZone, NAME_LEN);
06188 }
06189
06190 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescDataPaths SQL 2");
06191 cllBindVars[cllBindVarCount++]=oldPath;
06192 cllBindVars[cllBindVarCount++]=newPath;
06193 cllBindVars[cllBindVarCount++]=rescName;
06194 cllBindVars[cllBindVarCount++]=oldPath2;
06195 cllBindVars[cllBindVarCount++]=userName2;
06196 cllBindVars[cllBindVarCount++]=zoneToUse;
06197 status = cmlExecuteNoAnswerSql(
06198 "update R_DATA_MAIN set data_path = replace (R_DATA_MAIN.data_path, ?, ?) where resc_name=? and data_path like ? and data_owner_name=? and data_owner_zone=?",
06199 &icss);
06200 }
06201 else {
06202 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescDataPaths SQL 3");
06203 cllBindVars[cllBindVarCount++]=oldPath;
06204 cllBindVars[cllBindVarCount++]=newPath;
06205 cllBindVars[cllBindVarCount++]=rescName;
06206 cllBindVars[cllBindVarCount++]=oldPath2;
06207 status = cmlExecuteNoAnswerSql(
06208 "update R_DATA_MAIN set data_path = replace (R_DATA_MAIN.data_path, ?, ?) where resc_name=? and data_path like ?",
06209 &icss);
06210 }
06211 if (status != 0) {
06212 rodsLog(LOG_NOTICE,
06213 "chlModRescDataPaths cmlExecuteNoAnswerSql update failure %d",
06214 status);
06215 _rollback("chlModResc");
06216 return(status);
06217 }
06218
06219 rows = cllGetRowCount(&icss,-1);
06220
06221 status = cmlExecuteNoAnswerSql("commit", &icss);
06222 if (status != 0) {
06223 rodsLog(LOG_NOTICE,
06224 "chlModResc cmlExecuteNoAnswerSql commit failure %d",
06225 status);
06226 return(status);
06227 }
06228
06229 if (rows > 0) {
06230 char rowsMsg[100];
06231 snprintf(rowsMsg, 100, "%d rows updated",
06232 rows);
06233 status = addRErrorMsg (&rsComm->rError, 0, rowsMsg);
06234 }
06235
06236 return(0);
06237 }
06238
06239
06240
06241
06242
06243
06244 int chlModRescFreeSpace(rsComm_t *rsComm, char *rescName, int updateValue) {
06245 int status;
06246 char myTime[50];
06247 char updateValueStr[MAX_NAME_LEN];
06248
06249 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescFreeSpace");
06250
06251 if (rescName == NULL) {
06252 return (CAT_INVALID_ARGUMENT);
06253 }
06254
06255 if (*rescName == '\0') {
06256 return (CAT_INVALID_ARGUMENT);
06257 }
06258
06259
06260
06261
06262
06263 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06264 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06265 }
06266 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06267 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06268 }
06269
06270 status = getLocalZone();
06271 if (status != 0) return(status);
06272
06273 getNowStr(myTime);
06274
06275 snprintf(updateValueStr,MAX_NAME_LEN, "%d", updateValue);
06276
06277 cllBindVars[cllBindVarCount++]=updateValueStr;
06278 cllBindVars[cllBindVarCount++]=myTime;
06279 cllBindVars[cllBindVarCount++]=rescName;
06280
06281 if (logSQL!=0) rodsLog(LOG_SQL, "chlModRescFreeSpace SQL 1 ");
06282 status = cmlExecuteNoAnswerSql(
06283 "update R_RESC_MAIN set free_space = ?, free_space_ts=? where resc_name=?",
06284 &icss);
06285 if (status != 0) {
06286 rodsLog(LOG_NOTICE,
06287 "chlModRescFreeSpace cmlExecuteNoAnswerSql update failure %d",
06288 status);
06289 _rollback("chlModRescFreeSpace");
06290 return(status);
06291 }
06292
06293
06294 status = cmlAudit4(AU_MOD_RESC_FREE_SPACE,
06295 "select resc_id from R_RESC_MAIN where resc_name=?",
06296 rescName,
06297 rsComm->clientUser.userName,
06298 rsComm->clientUser.rodsZone,
06299 updateValueStr,
06300 &icss);
06301 if (status != 0) {
06302 rodsLog(LOG_NOTICE,
06303 "chlModRescFreeSpace cmlAudit4 failure %d",
06304 status);
06305 _rollback("chlModRescFreeSpace");
06306 return(status);
06307 }
06308
06309 return(0);
06310 }
06311
06312
06313 int chlModRescGroup(rsComm_t *rsComm, char *rescGroupName, char *option,
06314 char *rescName) {
06315 int status, OK;
06316 char myTime[50];
06317 char rescId[MAX_NAME_LEN];
06318 rodsLong_t seqNum;
06319 char rescGroupId[MAX_NAME_LEN];
06320 char dataObjNumber[MAX_NAME_LEN];
06321 char commentStr[200];
06322
06323 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group");
06324
06325 if (rescGroupName == NULL || option==NULL || rescName==NULL) {
06326 return (CAT_INVALID_ARGUMENT);
06327 }
06328
06329 if (*rescGroupName == '\0' || *option == '\0' || *rescName=='\0') {
06330 return (CAT_INVALID_ARGUMENT);
06331 }
06332
06333 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06334 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06335 }
06336 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06337 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06338 }
06339
06340 status = getLocalZone();
06341 if (status != 0) return(status);
06342
06343 rescId[0]='\0';
06344 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 1 ");
06345 status = cmlGetStringValueFromSql(
06346 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
06347 rescId, MAX_NAME_LEN, rescName, localZone, 0, &icss);
06348 if (status != 0) {
06349 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
06350 _rollback("chlModRescGroup");
06351 return(status);
06352 }
06353
06354 getNowStr(myTime);
06355 OK=0;
06356 if (strcmp(option, "add")==0) {
06357
06358 rescGroupId[0]='\0';
06359 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 2a ");
06360 status = cmlGetStringValueFromSql(
06361 "select distinct resc_group_id from R_RESC_GROUP where resc_group_name=?",
06362 rescGroupId, MAX_NAME_LEN, rescGroupName, 0, 0, &icss);
06363 if (status != 0) {
06364 if (status==CAT_NO_ROWS_FOUND) {
06365
06366 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 2b ");
06367 seqNum = cmlGetNextSeqVal(&icss);
06368 if (seqNum < 0) {
06369 rodsLog(LOG_NOTICE, "chlModRescGroup cmlGetNextSeqVal failure %d",
06370 seqNum);
06371 _rollback("chlModRescGroup");
06372 return(seqNum);
06373 }
06374 snprintf(rescGroupId, MAX_NAME_LEN, "%lld", seqNum);
06375 } else {
06376 _rollback("chlModRescGroup");
06377 return(status);
06378 }
06379 }
06380
06381 cllBindVars[cllBindVarCount++]=rescGroupName;
06382 cllBindVars[cllBindVarCount++]=rescGroupId;
06383 cllBindVars[cllBindVarCount++]=rescId;
06384 cllBindVars[cllBindVarCount++]=myTime;
06385 cllBindVars[cllBindVarCount++]=myTime;
06386 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 2");
06387 status = cmlExecuteNoAnswerSql(
06388 "insert into R_RESC_GROUP (resc_group_name, resc_group_id, resc_id , create_ts, modify_ts) values (?, ?, ?, ?, ?)",
06389 &icss);
06390 if (status != 0) {
06391 rodsLog(LOG_NOTICE,
06392 "chlModRescGroup cmlExecuteNoAnswerSql insert failure %d",
06393 status);
06394 _rollback("chlModRescGroup");
06395 return(status);
06396 }
06397 OK=1;
06398 }
06399
06400 if (strcmp(option, "remove")==0) {
06401
06402 dataObjNumber[0]='\0';
06403 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 3a ");
06404 status = cmlGetStringValueFromSql(
06405 "select distinct resc_group_id from R_RESC_GROUP where resc_id=? and resc_group_name=?",
06406 dataObjNumber, MAX_NAME_LEN, rescId, rescGroupName, 0, &icss);
06407 if (status != 0) {
06408 _rollback("chlModRescGroup");
06409 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
06410 return(status);
06411 }
06412
06413
06414 cllBindVars[cllBindVarCount++]=rescGroupName;
06415 cllBindVars[cllBindVarCount++]=rescId;
06416 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 3b");
06417 status = cmlExecuteNoAnswerSql(
06418 "delete from R_RESC_GROUP where resc_group_name=? and resc_id=?",
06419 &icss);
06420 if (status != 0) {
06421 rodsLog(LOG_NOTICE,
06422 "chlModRescGroup cmlExecuteNoAnswerSql delete failure %d",
06423 status);
06424 _rollback("chlModRescGroup");
06425 return(status);
06426 }
06427
06428
06429 rescGroupId[0]='\0';
06430 if (logSQL!=0) rodsLog(LOG_SQL, "chl-Mod-Resc-Group S Q L 3c ");
06431 status = cmlGetStringValueFromSql(
06432 "select distinct resc_group_id from R_RESC_GROUP where resc_group_name=?",
06433 rescGroupId, MAX_NAME_LEN, rescGroupName, 0, 0, &icss);
06434 if (status != 0) {
06435 if (status==CAT_NO_ROWS_FOUND) {
06436
06437 removeMetaMapAndAVU(dataObjNumber);
06438 }
06439 }
06440 OK=1;
06441 }
06442
06443 if (OK==0) {
06444 return (CAT_INVALID_ARGUMENT);
06445 }
06446
06447
06448 snprintf(commentStr, sizeof commentStr, "%s %s", option, rescGroupName);
06449 status = cmlAudit3(AU_MOD_RESC_GROUP,
06450 rescId,
06451 rsComm->clientUser.userName,
06452 rsComm->clientUser.rodsZone,
06453 commentStr,
06454 &icss);
06455 if (status != 0) {
06456 rodsLog(LOG_NOTICE,
06457 "chlModRescGroup cmlAudit3 failure %d",
06458 status);
06459 _rollback("chlModRescGroup");
06460 return(status);
06461 }
06462
06463 status = cmlExecuteNoAnswerSql("commit", &icss);
06464 if (status != 0) {
06465 rodsLog(LOG_NOTICE,
06466 "chlModRescGroup cmlExecuteNoAnswerSql commit failure %d",
06467 status);
06468 return(status);
06469 }
06470 return(0);
06471 }
06472
06473
06474
06475 eirods::error validate_user_name(std::string _user_name) {
06476
06477
06478
06479
06480 boost::regex re("^(?=.{3,63}$)\\w(\\w*([.-]\\w+)?)*$");
06481
06482
06483
06484
06485 if (!boost::regex_match(_user_name, re)) {
06486 std::stringstream msg;
06487 msg << "validate_user_name failed for user [";
06488 msg << _user_name;
06489 msg << "]";
06490 return ERROR( SYS_INVALID_INPUT_PARAM, msg.str() );
06491 }
06492
06493 return SUCCESS();
06494
06495 }
06496
06497
06498
06499 int chlRegUserRE(rsComm_t *rsComm, userInfo_t *userInfo) {
06500 char myTime[50];
06501 int status;
06502 char seqStr[MAX_NAME_LEN];
06503 char auditSQL[MAX_SQL_SIZE];
06504 char userZone[MAX_NAME_LEN];
06505 char zoneId[MAX_NAME_LEN];
06506
06507 int zoneForm;
06508 char userName2[NAME_LEN];
06509 char zoneName[NAME_LEN];
06510
06511 static char lastValidUserType[MAX_NAME_LEN]="";
06512 static char userTypeTokenName[MAX_NAME_LEN]="";
06513
06514 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE");
06515
06516 if (!icss.status) {
06517 return(CATALOG_NOT_CONNECTED);
06518 }
06519
06520 if (!userInfo) {
06521 return(SYS_INTERNAL_NULL_INPUT_ERR);
06522 }
06523
06524
06525
06526
06527 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ||
06528 rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06529 int status2;
06530 status2 = cmlCheckGroupAdminAccess(
06531 rsComm->clientUser.userName,
06532 rsComm->clientUser.rodsZone,
06533 "",
06534 &icss);
06535 if (status2 != 0) return(status2);
06536 creatingUserByGroupAdmin=1;
06537 }
06538
06539
06540
06541
06542
06543
06544 if ( *userInfo->userType=='\0' ||
06545 strcmp(userInfo->userType, lastValidUserType)!=0 ) {
06546 char errMsg[105];
06547 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE SQL 1 ");
06548 status = cmlGetStringValueFromSql(
06549 "select token_name from R_TOKN_MAIN where token_namespace='user_type' and token_name=?",
06550 userTypeTokenName, MAX_NAME_LEN, userInfo->userType, 0, 0, &icss);
06551 if (status==0) {
06552 strncpy(lastValidUserType, userInfo->userType, MAX_NAME_LEN);
06553 }
06554 else {
06555 snprintf(errMsg, 100, "user_type '%s' is not valid",
06556 userInfo->userType);
06557 addRErrorMsg (&rsComm->rError, 0, errMsg);
06558 return(CAT_INVALID_USER_TYPE);
06559 }
06560 }
06561
06562 status = getLocalZone();
06563 if (status != 0) return(status);
06564
06565 if (strlen(userInfo->rodsZone)>0) {
06566 zoneForm=1;
06567 strncpy(userZone, userInfo->rodsZone, MAX_NAME_LEN);
06568 }
06569 else {
06570 zoneForm=0;
06571 strncpy(userZone, localZone, MAX_NAME_LEN);
06572 }
06573
06574 status = parseUserName(userInfo->userName, userName2, zoneName);
06575 if (zoneName[0]!='\0') {
06576 rstrcpy(userZone, zoneName, NAME_LEN);
06577 zoneForm=2;
06578 }
06579 if (status != 0) {
06580 return (CAT_INVALID_ARGUMENT);
06581 }
06582
06583
06584
06585
06586 eirods::error ret = validate_user_name(userName2);
06587 if(!ret.ok()) {
06588 eirods::log(ret);
06589 return SYS_INVALID_INPUT_PARAM;
06590 }
06591
06592
06593
06594 if (zoneForm) {
06595
06596 zoneId[0]='\0';
06597 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE SQL 5 ");
06598 status = cmlGetStringValueFromSql(
06599 "select zone_id from R_ZONE_MAIN where zone_name=?",
06600 zoneId, MAX_NAME_LEN, userZone, "", 0, &icss);
06601 if (status != 0) {
06602 if (status==CAT_NO_ROWS_FOUND) {
06603 char errMsg[105];
06604 snprintf(errMsg, 100,
06605 "zone '%s' does not exist",
06606 userZone);
06607 addRErrorMsg (&rsComm->rError, 0, errMsg);
06608 return(CAT_INVALID_ZONE);
06609 }
06610 return(status);
06611 }
06612 }
06613
06614 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE SQL 2");
06615 status = cmlGetNextSeqStr(seqStr, MAX_NAME_LEN, &icss);
06616 if (status != 0) {
06617 rodsLog(LOG_NOTICE, "chlRegUserRE cmlGetNextSeqStr failure %d",
06618 status);
06619 return(status);
06620 }
06621
06622 getNowStr(myTime);
06623
06624 cllBindVars[cllBindVarCount++]=seqStr;
06625 cllBindVars[cllBindVarCount++]=userName2;
06626 cllBindVars[cllBindVarCount++]=userTypeTokenName;
06627 cllBindVars[cllBindVarCount++]=userZone;
06628 cllBindVars[cllBindVarCount++]=myTime;
06629 cllBindVars[cllBindVarCount++]=myTime;
06630
06631 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE SQL 3");
06632 status = cmlExecuteNoAnswerSql(
06633 "insert into R_USER_MAIN (user_id, user_name, user_type_name, zone_name, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?)",
06634 &icss);
06635
06636 if (status != 0) {
06637 if (status == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME) {
06638 char errMsg[105];
06639 snprintf(errMsg, 100, "Error %d %s",
06640 status,
06641 "CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME"
06642 );
06643 addRErrorMsg (&rsComm->rError, 0, errMsg);
06644 }
06645 _rollback("chlRegUserRE");
06646 rodsLog(LOG_NOTICE,
06647 "chlRegUserRE insert failure %d",status);
06648 return(status);
06649 }
06650
06651
06652 cllBindVars[cllBindVarCount++]=seqStr;
06653 cllBindVars[cllBindVarCount++]=seqStr;
06654 cllBindVars[cllBindVarCount++]=myTime;
06655 cllBindVars[cllBindVarCount++]=myTime;
06656
06657 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegUserRE SQL 4");
06658 status = cmlExecuteNoAnswerSql(
06659 "insert into R_USER_GROUP (group_user_id, user_id, create_ts, modify_ts) values (?, ?, ?, ?)",
06660 &icss);
06661 if (status != 0) {
06662 rodsLog(LOG_NOTICE,
06663 "chlRegUserRE insert into R_USER_GROUP failure %d",status);
06664 _rollback("chlRegUserRE");
06665 return(status);
06666 }
06667
06668
06669
06670
06671
06672
06673
06674 if (strlen(userInfo->authInfo.authStr) > 0) {
06675 status = chlModUser(rsComm, userInfo->userName, "addAuth",
06676 userInfo->authInfo.authStr);
06677 if (status != 0) {
06678 rodsLog(LOG_NOTICE,
06679 "chlRegUserRE chlModUser insert auth failure %d",status);
06680 _rollback("chlRegUserRE");
06681 return(status);
06682 }
06683 }
06684
06685
06686 snprintf(auditSQL, MAX_SQL_SIZE-1,
06687 "select user_id from R_USER_MAIN where user_name=? and zone_name='%s'",
06688 userZone);
06689 status = cmlAudit4(AU_REGISTER_USER_RE,
06690 auditSQL,
06691 userName2,
06692 rsComm->clientUser.userName,
06693 rsComm->clientUser.rodsZone,
06694 userZone,
06695 &icss);
06696 if (status != 0) {
06697 rodsLog(LOG_NOTICE,
06698 "chlRegUserRE cmlAudit4 failure %d",
06699 status);
06700 _rollback("chlRegUserRE");
06701 return(status);
06702 }
06703
06704
06705 return(status);
06706 }
06707
06708 int
06709 convertTypeOption(char *typeStr) {
06710 if (strcmp(typeStr, "-d") == 0) return(1);
06711 if (strcmp(typeStr, "-D") == 0) return(1);
06712 if (strcmp(typeStr, "-c") == 0) return(2);
06713 if (strcmp(typeStr, "-C") == 0) return(2);
06714 if (strcmp(typeStr, "-r") == 0) return(3);
06715 if (strcmp(typeStr, "-R") == 0) return(3);
06716 if (strcmp(typeStr, "-u") == 0) return(4);
06717 if (strcmp(typeStr, "-U") == 0) return(4);
06718 if (strcmp(typeStr, "-g") == 0) return(5);
06719 if (strcmp(typeStr, "-G") == 0) return(5);
06720 return (0);
06721 }
06722
06723
06724
06725
06726
06727 rodsLong_t checkAndGetObjectId(rsComm_t *rsComm, char *type,
06728 char *name, char *access) {
06729 int itype;
06730 char logicalEndName[MAX_NAME_LEN];
06731 char logicalParentDirName[MAX_NAME_LEN];
06732 rodsLong_t status;
06733 rodsLong_t objId;
06734 char userName[NAME_LEN];
06735 char userZone[NAME_LEN];
06736
06737
06738 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId");
06739
06740 if (!icss.status) {
06741 return(CATALOG_NOT_CONNECTED);
06742 }
06743
06744 if (type == NULL || *type=='\0') {
06745 return (CAT_INVALID_ARGUMENT);
06746 }
06747
06748 if (name == NULL || *name=='\0') {
06749 return (CAT_INVALID_ARGUMENT);
06750 }
06751
06752 itype = convertTypeOption(type);
06753 if (itype==0) return(CAT_INVALID_ARGUMENT);
06754
06755 if (itype==1) {
06756 status = splitPathByKey(name,
06757 logicalParentDirName, logicalEndName, '/');
06758 if (strlen(logicalParentDirName)==0) {
06759 strcpy(logicalParentDirName, "/");
06760 strcpy(logicalEndName, name);
06761 }
06762 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId SQL 1 ");
06763 status = cmlCheckDataObjOnly(logicalParentDirName, logicalEndName,
06764 rsComm->clientUser.userName,
06765 rsComm->clientUser.rodsZone,
06766 access, &icss);
06767 if (status < 0) {
06768 _rollback("checkAndGetObjectId");
06769 return(status);
06770 }
06771 objId=status;
06772 }
06773
06774 if (itype==2) {
06775
06776
06777 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId SQL 2");
06778 status = cmlCheckDir(name,
06779 rsComm->clientUser.userName,
06780 rsComm->clientUser.rodsZone,
06781 access, &icss);
06782 if (status < 0) {
06783 char errMsg[105];
06784 if (status == CAT_UNKNOWN_COLLECTION) {
06785 snprintf(errMsg, 100, "collection '%s' is unknown",
06786 name);
06787 addRErrorMsg (&rsComm->rError, 0, errMsg);
06788 }
06789 return(status);
06790 }
06791 objId=status;
06792 }
06793
06794 if (itype==3) {
06795 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06796 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06797 }
06798
06799 status = getLocalZone();
06800 if (status != 0) return(status);
06801
06802 objId=0;
06803 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId SQL 3");
06804 status = cmlGetIntegerValueFromSql(
06805 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
06806 &objId, name, localZone, 0, 0, 0, &icss);
06807 if (status != 0) {
06808 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
06809 _rollback("checkAndGetObjectId");
06810 return(status);
06811 }
06812 }
06813
06814 if (itype==4) {
06815 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06816 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06817 }
06818
06819 status = parseUserName(name, userName, userZone);
06820 if (userZone[0]=='\0') {
06821 status = getLocalZone();
06822 if (status != 0) return(status);
06823 strncpy(userZone, localZone, NAME_LEN);
06824 }
06825
06826 objId=0;
06827 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId SQL 4");
06828 status = cmlGetIntegerValueFromSql(
06829 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
06830 &objId, userName, userZone, 0, 0, 0, &icss);
06831 if (status != 0) {
06832 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
06833 _rollback("checkAndGetObjectId");
06834 return(status);
06835 }
06836 }
06837
06838 if (itype==5) {
06839 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
06840 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
06841 }
06842
06843 status = getLocalZone();
06844 if (status != 0) return(status);
06845
06846 objId=0;
06847 if (logSQL!=0) rodsLog(LOG_SQL, "checkAndGetObjectId S Q L 5");
06848 status = cmlGetIntegerValueFromSql(
06849 "select distinct resc_group_id from R_RESC_GROUP where resc_group_name=?",
06850 &objId, name, 0, 0, 0, 0, &icss);
06851 if (status != 0) {
06852 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
06853 _rollback("checkAndGetObjectId");
06854 return(status);
06855 }
06856 }
06857
06858 return(objId);
06859 }
06860
06861
06862
06863
06864
06865
06866 rodsLong_t
06867 findAVU(char *attribute, char *value, char *units) {
06868 rodsLong_t status;
06869
06870
06871 rodsLong_t iVal;
06872 iVal=0;
06873 if (*units!='\0') {
06874 if (logSQL!=0) rodsLog(LOG_SQL, "findAVU SQL 1");
06875 status = cmlGetIntegerValueFromSql(
06876 "select meta_id from R_META_MAIN where meta_attr_name=? and meta_attr_value=? and meta_attr_unit=?",
06877 &iVal, attribute, value, units, 0, 0, &icss);
06878 }
06879 else {
06880 if (logSQL!=0) rodsLog(LOG_SQL, "findAVU SQL 2");
06881 status = cmlGetIntegerValueFromSql(
06882 "select meta_id from R_META_MAIN where meta_attr_name=? and meta_attr_value=? and (meta_attr_unit='' or meta_attr_unit IS NULL)",
06883 &iVal, attribute, value, 0, 0, 0, &icss);
06884 }
06885 if (status == 0) {
06886 status = iVal;
06887 return(status);
06888 }
06889
06890
06891 return(status);
06892 }
06893
06894
06895
06896
06897
06898 int
06899 findOrInsertAVU(char *attribute, char *value, char *units) {
06900 char nextStr[MAX_NAME_LEN];
06901 char myTime[50];
06902 rodsLong_t status, seqNum;
06903 rodsLong_t iVal;
06904 iVal = findAVU(attribute, value, units);
06905 if (iVal > 0) {
06906 return iVal;
06907 }
06908 if (logSQL!=0) rodsLog(LOG_SQL, "findOrInsertAVU SQL 1");
06909
06910 status = cmlGetNextSeqVal(&icss);
06911 if (status < 0) {
06912 rodsLog(LOG_NOTICE, "findAVU cmlGetNextSeqVal failure %d",
06913 status);
06914 return(status);
06915 }
06916 seqNum = status;
06917
06918 snprintf(nextStr, sizeof nextStr, "%lld", seqNum);
06919
06920 getNowStr(myTime);
06921
06922 cllBindVars[cllBindVarCount++]=nextStr;
06923 cllBindVars[cllBindVarCount++]=attribute;
06924 cllBindVars[cllBindVarCount++]=value;
06925 cllBindVars[cllBindVarCount++]=units;
06926 cllBindVars[cllBindVarCount++]=myTime;
06927 cllBindVars[cllBindVarCount++]=myTime;
06928
06929 if (logSQL!=0) rodsLog(LOG_SQL, "findOrInsertAVU SQL 2");
06930 status = cmlExecuteNoAnswerSql(
06931 "insert into R_META_MAIN (meta_id, meta_attr_name, meta_attr_value, meta_attr_unit, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?)",
06932 &icss);
06933 if (status < 0) {
06934 rodsLog(LOG_NOTICE, "findOrInsertAVU insert failure %d", status);
06935 return(status);
06936 }
06937 return(seqNum);
06938 }
06939
06940
06941
06942
06943 int chlSetAVUMetadata(rsComm_t *rsComm, char *type,
06944 char *name, char *attribute, char *newValue,
06945 char *newUnit) {
06946 int status;
06947 char myTime[50];
06948 rodsLong_t objId;
06949 char metaIdStr[MAX_NAME_LEN*2];
06950 char objIdStr[MAX_NAME_LEN];
06951
06952 memset(metaIdStr, 0, sizeof(metaIdStr));
06953 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata");
06954
06955 if (!icss.status) {
06956 return(CATALOG_NOT_CONNECTED);
06957 }
06958
06959 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata SQL 1 ");
06960 objId = checkAndGetObjectId(rsComm, type, name, ACCESS_CREATE_METADATA);
06961 if (objId < 0) return objId;
06962 snprintf(objIdStr, MAX_NAME_LEN, "%lld", objId);
06963
06964 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata SQL 2");
06965
06966 status = cmlGetMultiRowStringValuesFromSql("select meta_id from R_OBJT_METAMAP where meta_id in (select meta_id from R_META_MAIN where meta_attr_name=? AND meta_id in (select meta_id from R_OBJT_METAMAP where object_id=?))",
06967 metaIdStr, MAX_NAME_LEN, 2, attribute, objIdStr, &icss);
06968
06969 if (status <= 0) {
06970 if (status == CAT_NO_ROWS_FOUND) {
06971
06972 status = chlAddAVUMetadata(rsComm, 0, type, name, attribute,
06973 newValue, newUnit);
06974 } else {
06975 rodsLog(LOG_NOTICE,
06976 "chlSetAVUMetadata cmlGetMultiRowStringValuesFromSql failure %d",
06977 status);
06978 }
06979 return status;
06980 }
06981
06982 if (status > 1) {
06983
06984 status = chlDeleteAVUMetadata(rsComm, 1, type, name, attribute, "%",
06985 "%", 1);
06986 if (status != 0) {
06987 _rollback("chlSetAVUMetadata");
06988 return(status);
06989 }
06990 status = chlAddAVUMetadata(rsComm, 0, type, name, attribute,
06991 newValue, newUnit);
06992 return status;
06993 }
06994
06995
06996
06997 rodsLog(LOG_NOTICE, "chlSetAVUMetadata found metaId %s", metaIdStr);
06998
06999 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata SQL 4");
07000 status = cmlGetMultiRowStringValuesFromSql("select meta_id from R_META_MAIN where meta_attr_name=?",
07001 metaIdStr, MAX_NAME_LEN, 2, attribute, objIdStr, &icss);
07002 if (status <= 0) {
07003 rodsLog(LOG_NOTICE,
07004 "chlSetAVUMetadata cmlGetMultiRowStringValueFromSql failure %d",
07005 status);
07006 return(status);
07007 }
07008 if (status > 1) {
07009
07010
07011
07012 status = chlDeleteAVUMetadata(rsComm, 1, type, name, attribute,
07013 "%", "%", 1);
07014 if (status != 0) {
07015 _rollback("chlSetAVUMetadata");
07016 return(status);
07017 }
07018 status = chlAddAVUMetadata(rsComm, 0, type, name, attribute,
07019 newValue, newUnit);
07020 }
07021 else {
07022 getNowStr(myTime);
07023 cllBindVarCount = 0;
07024 cllBindVars[cllBindVarCount++] = newValue;
07025 if (newUnit != NULL && *newUnit!='\0') {
07026 cllBindVars[cllBindVarCount++] = newUnit;
07027 }
07028 cllBindVars[cllBindVarCount++] = myTime;
07029 cllBindVars[cllBindVarCount++] = attribute;
07030 cllBindVars[cllBindVarCount++] = metaIdStr;
07031 if (newUnit != NULL && *newUnit!='\0') {
07032 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata SQL 5");
07033 status = cmlExecuteNoAnswerSql(
07034 "update R_META_MAIN set meta_attr_value=?,meta_attr_unit=?,modify_ts=? where meta_attr_name=? and meta_id=?",
07035 &icss);
07036 }
07037 else {
07038 if (logSQL != 0) rodsLog(LOG_SQL, "chlSetAVUMetadata SQL 6");
07039 status = cmlExecuteNoAnswerSql(
07040 "update R_META_MAIN set meta_attr_value=?,modify_ts=? where meta_attr_name=? and meta_id=?",
07041 &icss);
07042 }
07043 if (status != 0) {
07044 rodsLog(LOG_NOTICE,
07045 "chlSetAVUMetadata cmlExecuteNoAnswerSql update failure %d",
07046 status);
07047 _rollback("chlSetAVUMetadata");
07048 return(status);
07049 }
07050 }
07051
07052
07053 status = cmlAudit3(AU_ADD_AVU_METADATA,
07054 objIdStr,
07055 rsComm->clientUser.userName,
07056 rsComm->clientUser.rodsZone,
07057 type,
07058 &icss);
07059 if (status != 0) {
07060 rodsLog(LOG_NOTICE,
07061 "chlSetAVUMetadata cmlAudit3 failure %d",
07062 status);
07063 _rollback("chlSetAVUMetadata");
07064 return(status);
07065 }
07066
07067 status = cmlExecuteNoAnswerSql("commit", &icss);
07068 if (status != 0) {
07069 rodsLog(LOG_NOTICE,
07070 "chlSetAVUMetadata cmlExecuteNoAnswerSql commit failure %d",
07071 status);
07072 return(status);
07073 }
07074
07075 return(status);
07076 }
07077
07078
07079
07080
07081
07082
07083
07084
07085
07086 #define ACCESS_MAX 999999
07087
07088
07089 int
07090 chlAddAVUMetadataWild(rsComm_t *rsComm, int adminMode, char *type,
07091 char *name, char *attribute, char *value, char *units) {
07092 rodsLong_t status, status2;
07093 rodsLong_t seqNum;
07094 int numObjects;
07095 int nAccess=0;
07096 static int accessNeeded=ACCESS_MAX;
07097 rodsLong_t iVal;
07098 char collection[MAX_NAME_LEN];
07099 char objectName[MAX_NAME_LEN];
07100 char myTime[50];
07101 char seqNumStr[MAX_NAME_LEN];
07102 int itype;
07103
07104 itype = convertTypeOption(type);
07105 if (itype!=1) return(CAT_INVALID_ARGUMENT);
07106
07107 status = splitPathByKey(name, collection, objectName, '/');
07108 if (strlen(collection)==0) {
07109 strcpy(collection, "/");
07110 strcpy(objectName, name);
07111 }
07112
07113
07114
07115
07116
07117
07118
07119
07120
07121
07122 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 1");
07123 status = cmlGetIntegerValueFromSql(
07124 "select count(DISTINCT DM.data_id) from R_DATA_MAIN DM, R_COLL_MAIN CM where DM.data_name like ? and DM.coll_id=CM.coll_id and CM.coll_name like ?",
07125 &iVal, objectName, collection, 0, 0, 0, &icss);
07126 if (status != 0) {
07127 rodsLog(LOG_NOTICE,
07128 "chlAddAVUMetadataWild get count failure %d",
07129 status);
07130 _rollback("chlAddAVUMetadataWild");
07131 return(status);
07132 }
07133 numObjects = iVal;
07134 if (numObjects == 0) {
07135 return(CAT_NO_ROWS_FOUND);
07136 }
07137
07138
07139
07140
07141
07142 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 2");
07143 #if ORA_ICAT
07144
07145
07146 status = cmlExecuteNoAnswerSql("purge recyclebin",
07147 &icss);
07148 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07149 if (status != 0) {
07150 rodsLog(LOG_NOTICE,
07151 "chlAddAVUMetadata cmlExecuteNoAnswerSql (drop table ACCESS_VIEW_ONE) failure %d",
07152 status);
07153 }
07154 status = cmlExecuteNoAnswerSql("drop table ACCESS_VIEW_ONE",
07155 &icss);
07156 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07157 if (status != 0) {
07158 rodsLog(LOG_NOTICE,
07159 "chlAddAVUMetadata cmlExecuteNoAnswerSql (drop table ACCESS_VIEW_ONE) failure %d",
07160 status);
07161 }
07162
07163 status = cmlExecuteNoAnswerSql("create table ACCESS_VIEW_ONE (access_type_id integer, data_id integer)",
07164 &icss);
07165 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07166 if (status != 0) {
07167 rodsLog(LOG_NOTICE,
07168 "chlAddAVUMetadata cmlExecuteNoAnswerSql (create table ACCESS_VIEW_ONE) failure %d",
07169 status);
07170 _rollback("chlAddAVUMetadataWild");
07171 return(status);
07172 }
07173
07174 cllBindVars[cllBindVarCount++]=objectName;
07175 cllBindVars[cllBindVarCount++]=collection;
07176 cllBindVars[cllBindVarCount++]=rsComm->clientUser.userName;
07177 cllBindVars[cllBindVarCount++]=rsComm->clientUser.rodsZone;
07178 status = cmlExecuteNoAnswerSql(
07179 "insert into ACCESS_VIEW_ONE (access_type_id, data_id) (select access_type_id, DM.data_id from R_DATA_MAIN DM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_COLL_MAIN CM where DM.data_name like ? and DM.coll_id=CM.coll_id and CM.coll_name like ? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = DM.data_id and UG.group_user_id = OA.user_id)",
07180 &icss);
07181 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07182 if (status==CAT_NO_ROWS_FOUND) status=CAT_NO_ACCESS_PERMISSION;
07183 if (status != 0) {
07184 rodsLog(LOG_NOTICE,
07185 "chlAddAVUMetadata cmlExecuteNoAnswerSql (create view) failure %d",
07186 status);
07187 _rollback("chlAddAVUMetadataWild");
07188 return(status);
07189 }
07190 #else
07191 cllBindVars[cllBindVarCount++]=objectName;
07192 cllBindVars[cllBindVarCount++]=collection;
07193 cllBindVars[cllBindVarCount++]=rsComm->clientUser.userName;
07194 cllBindVars[cllBindVarCount++]=rsComm->clientUser.rodsZone;
07195 status = cmlExecuteNoAnswerSql(
07196 "create view ACCESS_VIEW_ONE as select access_type_id, DM.data_id from R_DATA_MAIN DM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_COLL_MAIN CM where DM.data_name like ? and DM.coll_id=CM.coll_id and CM.coll_name like ? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = DM.data_id and UG.group_user_id = OA.user_id",
07197 &icss);
07198 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07199 if (status==CAT_NO_ROWS_FOUND) status=CAT_NO_ACCESS_PERMISSION;
07200 if (status != 0) {
07201 rodsLog(LOG_NOTICE,
07202 "chlAddAVUMetadata cmlExecuteNoAnswerSql (create view) failure %d",
07203 status);
07204 _rollback("chlAddAVUMetadataWild");
07205 return(status);
07206 }
07207 #endif
07208
07209
07210
07211
07212
07213
07214 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 3");
07215 #if (defined ORA_ICAT || defined MY_ICAT)
07216 status = cmlExecuteNoAnswerSql(
07217 "create or replace view ACCESS_VIEW_TWO as select max(access_type_id) max from ACCESS_VIEW_ONE group by data_id",
07218 &icss);
07219 #else
07220 status = cmlExecuteNoAnswerSql(
07221 "create or replace view ACCESS_VIEW_TWO as select max(access_type_id) from ACCESS_VIEW_ONE group by data_id",
07222 &icss);
07223 #endif
07224 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
07225 if (status==CAT_NO_ROWS_FOUND) status=CAT_NO_ACCESS_PERMISSION;
07226 if (status != 0) {
07227 rodsLog(LOG_NOTICE,
07228 "chlAddAVUMetadata cmlExecuteNoAnswerSql (create view) failure %d",
07229 status);
07230 _rollback("chlAddAVUMetadataWild");
07231 return(status);
07232 }
07233
07234 if (accessNeeded>=ACCESS_MAX) {
07235 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 4");
07236 status = cmlGetIntegerValueFromSql(
07237 "select token_id from R_TOKN_MAIN where token_name = 'modify metadata' and token_namespace = 'access_type'",
07238 &iVal, 0, 0, 0, 0, 0, &icss);
07239 if (status==0) accessNeeded = iVal;
07240 }
07241
07242
07243
07244 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 5");
07245 iVal=-1;
07246 status = cmlGetIntegerValueFromSql(
07247 "select min(max) from ACCESS_VIEW_TWO",
07248 &iVal, 0, 0, 0, 0, 0, &icss);
07249
07250 if (status==CAT_NO_ROWS_FOUND) status=CAT_NO_ACCESS_PERMISSION;
07251
07252 if (status==0) {
07253 if (iVal < accessNeeded) {
07254 status = CAT_NO_ACCESS_PERMISSION;
07255 }
07256 }
07257
07258
07259
07260
07261 if (status==0) {
07262 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 6");
07263 status = cmlGetIntegerValueFromSql(
07264 "select count(*) from ACCESS_VIEW_TWO",
07265 &iVal, 0, 0, 0, 0, 0, &icss);
07266 if (status==0) {
07267 nAccess = iVal;
07268
07269 if (numObjects > nAccess) {
07270 status=CAT_NO_ACCESS_PERMISSION;
07271 }
07272 }
07273 }
07274
07275 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 7");
07276 #if ORA_ICAT
07277 status2 = cmlExecuteNoAnswerSql(
07278 "drop table ACCESS_VIEW_ONE",
07279 &icss);
07280 if (status2==CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
07281 if (status2==0) {
07282 status2 = cmlExecuteNoAnswerSql(
07283 "drop view ACCESS_VIEW_TWO",
07284 &icss);
07285 if (status2==CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
07286 }
07287 #else
07288 status2 = cmlExecuteNoAnswerSql(
07289 "drop view ACCESS_VIEW_TWO, ACCESS_VIEW_ONE",
07290 &icss);
07291 if (status2==CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
07292 #endif
07293
07294 if (status2 != 0) {
07295 rodsLog(LOG_NOTICE,
07296 "chlAddAVUMetadataWild cmlExecuteNoAnswerSql (drop view (or table)) failure %d",
07297 status2);
07298 }
07299
07300 if (status != 0) return(status);
07301
07302
07303
07304
07305 status = findOrInsertAVU(attribute, value, units);
07306 if (status<0) {
07307 rodsLog(LOG_NOTICE,
07308 "chlAddAVUMetadataWild findOrInsertAVU failure %d",
07309 status);
07310 _rollback("chlAddAVUMetadata");
07311 return(status);
07312 }
07313 seqNum = status;
07314
07315 getNowStr(myTime);
07316 snprintf(seqNumStr, sizeof seqNumStr, "%lld", seqNum);
07317 cllBindVars[cllBindVarCount++]=seqNumStr;
07318 cllBindVars[cllBindVarCount++]=myTime;
07319 cllBindVars[cllBindVarCount++]=myTime;
07320 cllBindVars[cllBindVarCount++]=objectName;
07321 cllBindVars[cllBindVarCount++]=collection;
07322 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadataWild SQL 8");
07323 status = cmlExecuteNoAnswerSql(
07324 "insert into R_OBJT_METAMAP (object_id, meta_id, create_ts, modify_ts) select DM.data_id, ?, ?, ? from R_DATA_MAIN DM, R_COLL_MAIN CM where DM.data_name like ? and DM.coll_id=CM.coll_id and CM.coll_name like ? group by DM.data_id",
07325 &icss);
07326 if (status != 0) {
07327 rodsLog(LOG_NOTICE,
07328 "chlAddAVUMetadataWild cmlExecuteNoAnswerSql insert failure %d",
07329 status);
07330 _rollback("chlAddAVUMetadataWild");
07331 return(status);
07332 }
07333
07334
07335 status = cmlAudit3(AU_ADD_AVU_WILD_METADATA,
07336 seqNumStr,
07337 rsComm->clientUser.userName,
07338 rsComm->clientUser.rodsZone,
07339 name,
07340 &icss);
07341 if (status != 0) {
07342 rodsLog(LOG_NOTICE,
07343 "chlAddAVUMetadataWild cmlAudit3 failure %d",
07344 status);
07345 _rollback("chlAddAVUMetadataWild");
07346 return(status);
07347 }
07348
07349
07350
07351 status = cmlExecuteNoAnswerSql("commit", &icss);
07352 if (status != 0) {
07353 rodsLog(LOG_NOTICE,
07354 "chlAddAVUMetadataWild cmlExecuteNoAnswerSql commit failure %d",
07355 status);
07356 return(status);
07357 }
07358
07359 if (status != 0) return(status);
07360 return(numObjects);
07361 }
07362
07363
07364
07365 int chlAddAVUMetadata(rsComm_t *rsComm, int adminMode, char *type,
07366 char *name, char *attribute, char *value, char *units) {
07367 int itype;
07368 char myTime[50];
07369 char logicalEndName[MAX_NAME_LEN];
07370 char logicalParentDirName[MAX_NAME_LEN];
07371 rodsLong_t seqNum, iVal;
07372 rodsLong_t objId, status;
07373 char objIdStr[MAX_NAME_LEN];
07374 char seqNumStr[MAX_NAME_LEN];
07375 char userName[NAME_LEN];
07376 char userZone[NAME_LEN];
07377
07378 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata");
07379
07380 if (!icss.status) {
07381 return(CATALOG_NOT_CONNECTED);
07382 }
07383
07384 if (type == NULL || *type=='\0') {
07385 return (CAT_INVALID_ARGUMENT);
07386 }
07387
07388 if (name == NULL || *name=='\0') {
07389 return (CAT_INVALID_ARGUMENT);
07390 }
07391
07392 if (attribute == NULL || *attribute=='\0') {
07393 return (CAT_INVALID_ARGUMENT);
07394 }
07395
07396 if (value == NULL || *value=='\0') {
07397 return (CAT_INVALID_ARGUMENT);
07398 }
07399
07400 if (adminMode==1) {
07401 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07402 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07403 }
07404 }
07405
07406 if (units == NULL) units="";
07407
07408 itype = convertTypeOption(type);
07409 if (itype==0) return(CAT_INVALID_ARGUMENT);
07410
07411 if (itype==1) {
07412 status = splitPathByKey(name,
07413 logicalParentDirName, logicalEndName, '/');
07414 if (strlen(logicalParentDirName)==0) {
07415 strcpy(logicalParentDirName, "/");
07416 strcpy(logicalEndName, name);
07417 }
07418 if (adminMode==1) {
07419 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 1 ");
07420 status = cmlGetIntegerValueFromSql(
07421 "select data_id from R_DATA_MAIN DM, R_COLL_MAIN CM where DM.data_name=? and DM.coll_id=CM.coll_id and CM.coll_name=?",
07422 &iVal, logicalEndName, logicalParentDirName, 0, 0, 0, &icss);
07423 if (status==0) status=iVal;
07424 }
07425 else {
07426 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 2");
07427 status = cmlCheckDataObjOnly(logicalParentDirName, logicalEndName,
07428 rsComm->clientUser.userName,
07429 rsComm->clientUser.rodsZone,
07430 ACCESS_CREATE_METADATA, &icss);
07431 }
07432 if (status < 0) {
07433 _rollback("chlAddAVUMetadata");
07434 return(status);
07435 }
07436 objId=status;
07437 }
07438
07439 if (itype==2) {
07440 if (adminMode==1) {
07441 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 3");
07442 status = cmlGetIntegerValueFromSql(
07443 "select coll_id from R_COLL_MAIN where coll_name=?",
07444 &iVal, name, 0, 0, 0, 0, &icss);
07445 if (status==0) status=iVal;
07446 }
07447 else {
07448
07449
07450 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 4");
07451 status = cmlCheckDir(name,
07452 rsComm->clientUser.userName,
07453 rsComm->clientUser.rodsZone,
07454 ACCESS_CREATE_METADATA, &icss);
07455 }
07456 if (status < 0) {
07457 char errMsg[105];
07458 _rollback("chlAddAVUMetadata");
07459 if (status == CAT_UNKNOWN_COLLECTION) {
07460 snprintf(errMsg, 100, "collection '%s' is unknown",
07461 name);
07462 addRErrorMsg (&rsComm->rError, 0, errMsg);
07463 } else {
07464 _rollback("chlAddAVUMetadata");
07465 }
07466 return(status);
07467 }
07468 objId=status;
07469 }
07470
07471 if (itype==3) {
07472 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07473 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07474 }
07475
07476 status = getLocalZone();
07477 if (status != 0) return(status);
07478
07479 objId=0;
07480 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 5");
07481 status = cmlGetIntegerValueFromSql(
07482 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
07483 &objId, name, localZone, 0, 0, 0, &icss);
07484 if (status != 0) {
07485 _rollback("chlAddAVUMetadata");
07486 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
07487 return(status);
07488 }
07489 }
07490
07491 if (itype==4) {
07492 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07493 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07494 }
07495
07496 status = parseUserName(name, userName, userZone);
07497 if (userZone[0]=='\0') {
07498 status = getLocalZone();
07499 if (status != 0) return(status);
07500 strncpy(userZone, localZone, NAME_LEN);
07501 }
07502
07503 objId=0;
07504 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 6");
07505 status = cmlGetIntegerValueFromSql(
07506 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
07507 &objId, userName, userZone, 0, 0, 0, &icss);
07508 if (status != 0) {
07509 _rollback("chlAddAVUMetadata");
07510 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
07511 return(status);
07512 }
07513 }
07514
07515 if (itype==5) {
07516 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07517 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07518 }
07519
07520 status = getLocalZone();
07521 if (status != 0) return(status);
07522
07523 objId=0;
07524 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 7");
07525 status = cmlGetIntegerValueFromSql(
07526 "select distinct resc_group_id from R_RESC_GROUP where resc_group_name=?",
07527 &objId, name, 0, 0, 0, 0, &icss);
07528 if (status != 0) {
07529 _rollback("chlAddAVUMetadata");
07530 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
07531 return(status);
07532 }
07533 }
07534
07535 status = findOrInsertAVU(attribute, value, units);
07536 if (status<0) {
07537 rodsLog(LOG_NOTICE,
07538 "chlAddAVUMetadata findOrInsertAVU failure %d",
07539 status);
07540 _rollback("chlAddAVUMetadata");
07541 return(status);
07542 }
07543 seqNum = status;
07544
07545 getNowStr(myTime);
07546 snprintf(objIdStr, sizeof objIdStr, "%lld", objId);
07547 snprintf(seqNumStr, sizeof seqNumStr, "%lld", seqNum);
07548 cllBindVars[cllBindVarCount++]=objIdStr;
07549 cllBindVars[cllBindVarCount++]=seqNumStr;
07550 cllBindVars[cllBindVarCount++]=myTime;
07551 cllBindVars[cllBindVarCount++]=myTime;
07552 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddAVUMetadata SQL 7");
07553 status = cmlExecuteNoAnswerSql(
07554 "insert into R_OBJT_METAMAP (object_id, meta_id, create_ts, modify_ts) values (?, ?, ?, ?)",
07555 &icss);
07556 if (status != 0) {
07557 rodsLog(LOG_NOTICE,
07558 "chlAddAVUMetadata cmlExecuteNoAnswerSql insert failure %d",
07559 status);
07560 _rollback("chlAddAVUMetadata");
07561 return(status);
07562 }
07563
07564
07565 status = cmlAudit3(AU_ADD_AVU_METADATA,
07566 objIdStr,
07567 rsComm->clientUser.userName,
07568 rsComm->clientUser.rodsZone,
07569 type,
07570 &icss);
07571 if (status != 0) {
07572 rodsLog(LOG_NOTICE,
07573 "chlAddAVUMetadata cmlAudit3 failure %d",
07574 status);
07575 _rollback("chlAddAVUMetadata");
07576 return(status);
07577 }
07578
07579 status = cmlExecuteNoAnswerSql("commit", &icss);
07580 if (status != 0) {
07581 rodsLog(LOG_NOTICE,
07582 "chlAddAVUMetadata cmlExecuteNoAnswerSql commit failure %d",
07583 status);
07584 return(status);
07585 }
07586
07587 return(status);
07588 }
07589
07590
07591
07592
07593 int
07594 checkModArgType(char *arg) {
07595 if (arg == NULL || *arg=='\0')
07596 return(CAT_INVALID_ARGUMENT);
07597 if (*(arg+1)!=':') return(0);
07598 if (*arg=='n') return(1);
07599 if (*arg=='v') return(2);
07600 if (*arg=='u') return(3);
07601 return(0);
07602 }
07603
07604
07605 int chlModAVUMetadata(rsComm_t *rsComm, char *type,
07606 char *name, char *attribute, char *value,
07607 char *unitsOrArg0, char *arg1, char *arg2, char *arg3) {
07608 int status, atype;
07609 char myUnits[MAX_NAME_LEN]="";
07610 char *addAttr="", *addValue="", *addUnits="";
07611 int newUnits=0;
07612 if (unitsOrArg0 == NULL || *unitsOrArg0=='\0')
07613 return(CAT_INVALID_ARGUMENT);
07614 atype = checkModArgType(unitsOrArg0);
07615 if (atype==0) strncpy(myUnits, unitsOrArg0, MAX_NAME_LEN);
07616
07617 status = chlDeleteAVUMetadata(rsComm, 0, type, name, attribute, value,
07618 myUnits, 1);
07619 if (status != 0) {
07620 _rollback("chlModAVUMetadata");
07621 return(status);
07622 }
07623
07624 if (atype==1) {
07625 addAttr=unitsOrArg0+2;
07626 }
07627 if (atype==2) {
07628 addValue=unitsOrArg0+2;
07629 }
07630 if (atype==3) {
07631 addUnits=unitsOrArg0+2;
07632 }
07633
07634 atype = checkModArgType(arg1);
07635 if (atype==1) {
07636 addAttr=arg1+2;
07637 }
07638 if (atype==2) {
07639 addValue=arg1+2;
07640 }
07641 if (atype==3) {
07642 addUnits=arg1+2;
07643 }
07644
07645 atype = checkModArgType(arg2);
07646 if (atype==1) {
07647 addAttr=arg2+2;
07648 }
07649 if (atype==2) {
07650 addValue=arg2+2;
07651 }
07652 if (atype==3) {
07653 addUnits=arg2+2;
07654 }
07655
07656 atype = checkModArgType(arg3);
07657 if (atype==1) {
07658 addAttr=arg3+2;
07659 }
07660 if (atype==2) {
07661 addValue=arg3+2;
07662 }
07663 if (atype==3) {
07664 addUnits=arg3+2;
07665 newUnits=1;
07666 }
07667
07668 if (*addAttr=='\0' &&
07669 *addValue=='\0' &&
07670 *addUnits=='\0') {
07671 _rollback("chlModAVUMetadata");
07672 return (CAT_INVALID_ARGUMENT);
07673 }
07674
07675 if (*addAttr=='\0') addAttr=attribute;
07676 if (*addValue=='\0') addValue=value;
07677 if (*addUnits=='\0' && newUnits==0) addUnits=myUnits;
07678
07679 status = chlAddAVUMetadata(rsComm, 0, type, name, addAttr, addValue,
07680 addUnits);
07681 return(status);
07682
07683 }
07684
07685
07686
07687
07688 int chlDeleteAVUMetadata(rsComm_t *rsComm, int option, char *type,
07689 char *name, char *attribute, char *value,
07690 char *units, int noCommit ) {
07691 int itype;
07692 char logicalEndName[MAX_NAME_LEN];
07693 char logicalParentDirName[MAX_NAME_LEN];
07694 rodsLong_t status;
07695 rodsLong_t objId;
07696 char objIdStr[MAX_NAME_LEN];
07697 int allowNullUnits;
07698 char userName[NAME_LEN];
07699 char userZone[NAME_LEN];
07700
07701 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata");
07702
07703 if (!icss.status) {
07704 return(CATALOG_NOT_CONNECTED);
07705 }
07706
07707 if (type == NULL || *type=='\0') {
07708 return (CAT_INVALID_ARGUMENT);
07709 }
07710
07711 if (name == NULL || *name=='\0') {
07712 return (CAT_INVALID_ARGUMENT);
07713 }
07714
07715 if (option != 2) {
07716 if (attribute == NULL || *attribute=='\0') {
07717 return (CAT_INVALID_ARGUMENT);
07718 }
07719
07720 if (value == NULL || *value=='\0') {
07721 return (CAT_INVALID_ARGUMENT);
07722 }
07723 }
07724
07725 if (units == NULL) units="";
07726
07727 itype = convertTypeOption(type);
07728 if (itype==0) return(CAT_INVALID_ARGUMENT);
07729
07730 if (itype==1) {
07731 status = splitPathByKey(name,
07732 logicalParentDirName, logicalEndName, '/');
07733 if (strlen(logicalParentDirName)==0) {
07734 strcpy(logicalParentDirName, "/");
07735 strcpy(logicalEndName, name);
07736 }
07737 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 1 ");
07738 status = cmlCheckDataObjOnly(logicalParentDirName, logicalEndName,
07739 rsComm->clientUser.userName,
07740 rsComm->clientUser.rodsZone,
07741 ACCESS_DELETE_METADATA, &icss);
07742 if (status < 0) {
07743 _rollback("chlDeleteAVUMetadata");
07744 return(status);
07745 }
07746 objId=status;
07747 }
07748
07749 if (itype==2) {
07750
07751
07752 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 2");
07753 status = cmlCheckDir(name,
07754 rsComm->clientUser.userName,
07755 rsComm->clientUser.rodsZone,
07756 ACCESS_DELETE_METADATA, &icss);
07757 if (status < 0) {
07758 char errMsg[105];
07759 if (status == CAT_UNKNOWN_COLLECTION) {
07760 snprintf(errMsg, 100, "collection '%s' is unknown",
07761 name);
07762 addRErrorMsg (&rsComm->rError, 0, errMsg);
07763 }
07764 return(status);
07765 }
07766 objId=status;
07767 }
07768
07769 if (itype==3) {
07770 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07771 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07772 }
07773
07774 status = getLocalZone();
07775 if (status != 0) return(status);
07776
07777 objId=0;
07778 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 3");
07779 status = cmlGetIntegerValueFromSql(
07780 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
07781 &objId, name, localZone, 0, 0, 0, &icss);
07782 if (status != 0) {
07783 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
07784 _rollback("chlDeleteAVUMetadata");
07785 return(status);
07786 }
07787 }
07788
07789 if (itype==4) {
07790 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07791 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07792 }
07793
07794 status = parseUserName(name, userName, userZone);
07795 if (userZone[0]=='\0') {
07796 status = getLocalZone();
07797 if (status != 0) return(status);
07798 strncpy(userZone, localZone, NAME_LEN);
07799 }
07800
07801 objId=0;
07802 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 4");
07803 status = cmlGetIntegerValueFromSql(
07804 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
07805 &objId, userName, userZone, 0, 0, 0, &icss);
07806 if (status != 0) {
07807 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
07808 _rollback("chlDeleteAVUMetadata");
07809 return(status);
07810 }
07811 }
07812
07813 if (itype==5) {
07814 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
07815 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
07816 }
07817
07818 status = getLocalZone();
07819 if (status != 0) return(status);
07820
07821 objId=0;
07822 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 5");
07823 status = cmlGetIntegerValueFromSql(
07824 "select resc_group_id from R_RESC_GROUP where resc_group_name=?",
07825 &objId, name, 0, 0, 0, 0, &icss);
07826 if (status != 0) {
07827 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
07828 _rollback("chlDeleteAVUMetadata");
07829 return(status);
07830 }
07831 }
07832
07833
07834 snprintf(objIdStr, MAX_NAME_LEN, "%lld", objId);
07835
07836 if (option==2) {
07837 cllBindVars[cllBindVarCount++]=objIdStr;
07838 cllBindVars[cllBindVarCount++]=attribute;
07839
07840 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 9");
07841 status = cmlExecuteNoAnswerSql(
07842 "delete from R_OBJT_METAMAP where object_id=? and meta_id =?",
07843 &icss);
07844 if (status != 0) {
07845 rodsLog(LOG_NOTICE,
07846 "chlDeleteAVUMetadata cmlExecuteNoAnswerSql delete failure %d",
07847 status);
07848 _rollback("chlDeleteAVUMetadata");
07849 return(status);
07850 }
07851
07852
07853 #ifdef METADATA_CLEANUP
07854 removeAVUs();
07855 #endif
07856
07857
07858 status = cmlAudit3(AU_DELETE_AVU_METADATA,
07859 objIdStr,
07860 rsComm->clientUser.userName,
07861 rsComm->clientUser.rodsZone,
07862 type,
07863 &icss);
07864 if (status != 0) {
07865 rodsLog(LOG_NOTICE,
07866 "chlDeleteAVUMetadata cmlAudit3 failure %d",
07867 status);
07868 _rollback("chlDeleteAVUMetadata");
07869 return(status);
07870 }
07871
07872 if (noCommit != 1) {
07873 status = cmlExecuteNoAnswerSql("commit", &icss);
07874 if (status != 0) {
07875 rodsLog(LOG_NOTICE,
07876 "chlDeleteAVUMetadata cmlExecuteNoAnswerSql commit failure %d",
07877 status);
07878 return(status);
07879 }
07880 }
07881 return(status);
07882 }
07883
07884 cllBindVars[cllBindVarCount++]=objIdStr;
07885 cllBindVars[cllBindVarCount++]=attribute;
07886 cllBindVars[cllBindVarCount++]=value;
07887 cllBindVars[cllBindVarCount++]=units;
07888
07889 allowNullUnits=0;
07890 if (*units=='\0') {
07891 allowNullUnits=1;
07892 }
07893 if (option==1 && *units=='%' && *(units+1)=='\0') {
07894 allowNullUnits=1;
07895 }
07896
07897 if (allowNullUnits) {
07898 if (option==1) {
07899 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 5");
07900 status = cmlExecuteNoAnswerSql(
07901 "delete from R_OBJT_METAMAP where object_id=? and meta_id IN (select meta_id from R_META_MAIN where meta_attr_name like ? and meta_attr_value like ? and (meta_attr_unit like ? or meta_attr_unit IS NULL) )",
07902 &icss);
07903 }
07904 else {
07905 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 6");
07906 status = cmlExecuteNoAnswerSql(
07907 "delete from R_OBJT_METAMAP where object_id=? and meta_id IN (select meta_id from R_META_MAIN where meta_attr_name = ? and meta_attr_value = ? and (meta_attr_unit = ? or meta_attr_unit IS NULL) )",
07908 &icss);
07909 }
07910 }
07911 else {
07912 if (option==1) {
07913 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 7");
07914 status = cmlExecuteNoAnswerSql(
07915 "delete from R_OBJT_METAMAP where object_id=? and meta_id IN (select meta_id from R_META_MAIN where meta_attr_name like ? and meta_attr_value like ? and meta_attr_unit like ?)",
07916 &icss);
07917 }
07918 else {
07919 if (logSQL!=0) rodsLog(LOG_SQL, "chlDeleteAVUMetadata SQL 8");
07920 status = cmlExecuteNoAnswerSql(
07921 "delete from R_OBJT_METAMAP where object_id=? and meta_id IN (select meta_id from R_META_MAIN where meta_attr_name = ? and meta_attr_value = ? and meta_attr_unit = ?)",
07922 &icss);
07923 }
07924 }
07925 if (status != 0) {
07926 rodsLog(LOG_NOTICE,
07927 "chlDeleteAVUMetadata cmlExecuteNoAnswerSql delete failure %d",
07928 status);
07929 _rollback("chlDeleteAVUMetadata");
07930 return(status);
07931 }
07932
07933
07934 #ifdef METADATA_CLEANUP
07935 removeAVUs();
07936 #endif
07937
07938
07939 status = cmlAudit3(AU_DELETE_AVU_METADATA,
07940 objIdStr,
07941 rsComm->clientUser.userName,
07942 rsComm->clientUser.rodsZone,
07943 type,
07944 &icss);
07945 if (status != 0) {
07946 rodsLog(LOG_NOTICE,
07947 "chlDeleteAVUMetadata cmlAudit3 failure %d",
07948 status);
07949 _rollback("chlDeleteAVUMetadata");
07950 return(status);
07951 }
07952
07953 if (noCommit != 1) {
07954 status = cmlExecuteNoAnswerSql("commit", &icss);
07955 if (status != 0) {
07956 rodsLog(LOG_NOTICE,
07957 "chlDeleteAVUMetadata cmlExecuteNoAnswerSql commit failure %d",
07958 status);
07959 return(status);
07960 }
07961 }
07962
07963 return(status);
07964 }
07965
07966
07967
07968 int chlCopyAVUMetadata(rsComm_t *rsComm, char *type1, char *type2,
07969 char *name1, char *name2) {
07970 char myTime[50];
07971 int status;
07972 rodsLong_t objId1, objId2;
07973 char objIdStr1[MAX_NAME_LEN];
07974 char objIdStr2[MAX_NAME_LEN];
07975
07976 if (logSQL!=0) rodsLog(LOG_SQL, "chlCopyAVUMetadata");
07977
07978 if (!icss.status) {
07979 return(CATALOG_NOT_CONNECTED);
07980 }
07981
07982 if (logSQL!=0) rodsLog(LOG_SQL, "chlCopyAVUMetadata SQL 1 ");
07983 objId1 = checkAndGetObjectId(rsComm, type1, name1, ACCESS_READ_METADATA);
07984 if (objId1 < 0) return(objId1);
07985
07986 if (logSQL!=0) rodsLog(LOG_SQL, "chlCopyAVUMetadata SQL 2");
07987 objId2 = checkAndGetObjectId(rsComm, type2, name2, ACCESS_CREATE_METADATA);
07988
07989 if (objId2 < 0) return(objId2);
07990
07991 snprintf(objIdStr1, MAX_NAME_LEN, "%lld", objId1);
07992 snprintf(objIdStr2, MAX_NAME_LEN, "%lld", objId2);
07993
07994 getNowStr(myTime);
07995 cllBindVars[cllBindVarCount++]=objIdStr2;
07996 cllBindVars[cllBindVarCount++]=myTime;
07997 cllBindVars[cllBindVarCount++]=myTime;
07998 cllBindVars[cllBindVarCount++]=objIdStr1;
07999 if (logSQL!=0) rodsLog(LOG_SQL, "chlCopyAVUMetadata SQL 3");
08000 status = cmlExecuteNoAnswerSql(
08001 "insert into R_OBJT_METAMAP (object_id, meta_id, create_ts, modify_ts) select ?, meta_id, ?, ? from R_OBJT_METAMAP where object_id=?",
08002 &icss);
08003 if (status != 0) {
08004 rodsLog(LOG_NOTICE,
08005 "chlCopyAVUMetadata cmlExecuteNoAnswerSql insert failure %d",
08006 status);
08007 _rollback("chlCopyAVUMetadata");
08008 return(status);
08009 }
08010
08011
08012 status = cmlAudit3(AU_COPY_AVU_METADATA,
08013 objIdStr1,
08014 rsComm->clientUser.userName,
08015 rsComm->clientUser.rodsZone,
08016 objIdStr2,
08017 &icss);
08018 if (status != 0) {
08019 rodsLog(LOG_NOTICE,
08020 "chlCopyAVUMetadata cmlAudit3 failure %d",
08021 status);
08022 _rollback("chlCopyAVUMetadata");
08023 return(status);
08024 }
08025
08026 status = cmlExecuteNoAnswerSql("commit", &icss);
08027 if (status != 0) {
08028 rodsLog(LOG_NOTICE,
08029 "chlCopyAVUMetadata cmlExecuteNoAnswerSql commit failure %d",
08030 status);
08031 return(status);
08032 }
08033
08034 return(status);
08035 }
08036
08037
08038 void
08039 makeEscapedPath(char *inPath, char *outPath, int size) {
08040 int i;
08041 for (i=0;i<size-1;i++) {
08042 if (*inPath=='%' || *inPath=='_') {
08043 *outPath++='\\';
08044 }
08045 if (*inPath=='\0') {
08046 *outPath++=*inPath++;
08047 break;
08048 }
08049 *outPath++=*inPath++;
08050 }
08051 return;
08052 }
08053
08054
08055
08056 int _modInheritance(int inheritFlag, int recursiveFlag, char *collIdStr, char *pathName) {
08057 rodsLong_t status;
08058 char myTime[50];
08059 char newValue[10];
08060 char pathStart[MAX_NAME_LEN*2];
08061 char auditStr[30];
08062
08063 if (recursiveFlag==0) {
08064 strcpy(auditStr, "inheritance non-recursive ");
08065 }
08066 else {
08067 strcpy(auditStr, "inheritance recursive ");
08068 }
08069
08070 if (inheritFlag==1) {
08071 newValue[0]='1';
08072 newValue[1]='\0';
08073 }
08074 else {
08075 newValue[0]='0';
08076 newValue[1]='\0';
08077 }
08078 strcat(auditStr, newValue);
08079
08080 getNowStr(myTime);
08081
08082
08083 if (recursiveFlag==0) {
08084
08085 if (logSQL!=0) rodsLog(LOG_SQL, "_modInheritance SQL 1");
08086
08087 cllBindVars[cllBindVarCount++]=newValue;
08088 cllBindVars[cllBindVarCount++]=myTime;
08089 cllBindVars[cllBindVarCount++]=collIdStr;
08090 status = cmlExecuteNoAnswerSql(
08091 "update R_COLL_MAIN set coll_inheritance=?, modify_ts=? where coll_id=?",
08092 &icss);
08093 }
08094 else {
08095
08096 makeEscapedPath(pathName, pathStart, sizeof(pathStart));
08097 strncat(pathStart, "/%", sizeof(pathStart));
08098
08099 cllBindVars[cllBindVarCount++]=newValue;
08100 cllBindVars[cllBindVarCount++]=myTime;
08101 cllBindVars[cllBindVarCount++]=pathName;
08102 cllBindVars[cllBindVarCount++]=pathStart;
08103 if (logSQL!=0) rodsLog(LOG_SQL, "_modInheritance SQL 2");
08104 status = cmlExecuteNoAnswerSql(
08105 "update R_COLL_MAIN set coll_inheritance=?, modify_ts=? where coll_name = ? or coll_name like ?",
08106 &icss);
08107 }
08108 if (status != 0) {
08109 _rollback("_modInheritance");
08110 return(status);
08111 }
08112
08113
08114 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_COLL,
08115 collIdStr,
08116 "0",
08117 auditStr,
08118 &icss);
08119 if (status != 0) {
08120 rodsLog(LOG_NOTICE,
08121 "_modInheritance cmlAudit5 failure %d",
08122 status);
08123 _rollback("_modInheritance");
08124 return(status);
08125 }
08126
08127 status = cmlExecuteNoAnswerSql("commit", &icss);
08128 return(status);
08129 }
08130
08131 int chlModAccessControlResc(rsComm_t *rsComm, int recursiveFlag,
08132 char* accessLevel, char *userName, char *zone,
08133 char* rescName) {
08134 char myAccessStr[LONG_NAME_LEN];
08135 char rescIdStr[MAX_NAME_LEN];
08136 char *myAccessLev=NULL;
08137 int rmFlag=0;
08138 rodsLong_t status;
08139 char *myZone;
08140 rodsLong_t userId;
08141 char userIdStr[MAX_NAME_LEN];
08142 char myTime[50];
08143 rodsLong_t iVal;
08144 int debug=0;
08145
08146 strncpy(myAccessStr,accessLevel+strlen(MOD_RESC_PREFIX),LONG_NAME_LEN);
08147 myAccessStr[ LONG_NAME_LEN-1 ]='\0';
08148 if (debug>0) {
08149 printf("accessLevel: %s\n", accessLevel);
08150 printf("rescName: %s\n", rescName);
08151 }
08152
08153 if (strcmp(myAccessStr, AP_NULL)==0) {myAccessLev=ACCESS_NULL; rmFlag=1;}
08154 else if (strcmp(myAccessStr,AP_READ)==0) {myAccessLev=ACCESS_READ_OBJECT;}
08155 else if (strcmp(myAccessStr,AP_WRITE)==0){myAccessLev=ACCESS_MODIFY_OBJECT;}
08156 else if (strcmp(myAccessStr,AP_OWN)==0) {myAccessLev=ACCESS_OWN;}
08157 else {
08158 char errMsg[105];
08159 snprintf(errMsg, 100, "access level '%s' is invalid for a resource",
08160 myAccessStr);
08161 addRErrorMsg (&rsComm->rError, 0, errMsg);
08162 return(CAT_INVALID_ARGUMENT);
08163 }
08164
08165 if (rsComm->clientUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH) {
08166
08167 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControlResc SQL 1");
08168 status = cmlGetIntegerValueFromSql(
08169 "select resc_id from R_RESC_MAIN where resc_name=?",
08170 &iVal, rescName, 0, 0, 0, 0, &icss);
08171 if (status==CAT_NO_ROWS_FOUND) return(CAT_UNKNOWN_RESOURCE);
08172 if (status<0) return(status);
08173 status = iVal;
08174 }
08175 else {
08176 status = cmlCheckResc(rescName,
08177 rsComm->clientUser.userName,
08178 rsComm->clientUser.rodsZone,
08179 ACCESS_OWN,
08180 &icss);
08181 if (status<0) return(status);
08182 }
08183 snprintf(rescIdStr, MAX_NAME_LEN, "%lld", status);
08184
08185
08186 status = getLocalZone();
08187 if (status != 0) return(status);
08188
08189 myZone=zone;
08190 if (zone == NULL || strlen(zone)==0) {
08191 myZone=localZone;
08192 }
08193
08194 userId=0;
08195 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControlResc SQL 2");
08196 status = cmlGetIntegerValueFromSql(
08197 "select user_id from R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=?",
08198 &userId, userName, myZone, 0, 0, 0, &icss);
08199 if (status != 0) {
08200 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
08201 return(status);
08202 }
08203
08204 snprintf(userIdStr, MAX_NAME_LEN, "%lld", userId);
08205
08206
08207 cllBindVars[cllBindVarCount++]=userIdStr;
08208 cllBindVars[cllBindVarCount++]=rescIdStr;
08209 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControlResc SQL 3");
08210 status = cmlExecuteNoAnswerSql(
08211 "delete from R_OBJT_ACCESS where user_id=? and object_id=?",
08212 &icss);
08213 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08214 return(status);
08215 }
08216
08217
08218 if (rmFlag==0) {
08219 getNowStr(myTime);
08220 cllBindVars[cllBindVarCount++]=rescIdStr;
08221 cllBindVars[cllBindVarCount++]=userIdStr;
08222 cllBindVars[cllBindVarCount++]=myAccessLev;
08223 cllBindVars[cllBindVarCount++]=myTime;
08224 cllBindVars[cllBindVarCount++]=myTime;
08225 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControlResc SQL 4");
08226 status = cmlExecuteNoAnswerSql(
08227 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) values (?, ?, (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
08228 &icss);
08229 if (status != 0) {
08230 _rollback("chlModAccessControlResc");
08231 return(status);
08232 }
08233 }
08234
08235
08236 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_RESOURCE,
08237 rescIdStr,
08238 userIdStr,
08239 myAccessLev,
08240 &icss);
08241 if (status != 0) {
08242 rodsLog(LOG_NOTICE,
08243 "chlModAccessControlResc cmlAudit5 failure %d",
08244 status);
08245 _rollback("chlModAccessControlResc");
08246 return(status);
08247 }
08248
08249 status = cmlExecuteNoAnswerSql("commit", &icss);
08250 return(status);
08251 }
08252
08253
08254
08255
08256
08257
08258 int chlModAccessControl(rsComm_t *rsComm, int recursiveFlag,
08259 char* accessLevel, char *userName, char *zone,
08260 char* pathName) {
08261 char *myAccessLev=NULL;
08262 char logicalEndName[MAX_NAME_LEN];
08263 char logicalParentDirName[MAX_NAME_LEN];
08264 char collIdStr[MAX_NAME_LEN];
08265 rodsLong_t objId=0;
08266 rodsLong_t status, status1, status2, status3;
08267 int rmFlag=0;
08268 rodsLong_t userId;
08269 char myTime[50];
08270 char *myZone;
08271 char userIdStr[MAX_NAME_LEN];
08272 char objIdStr[MAX_NAME_LEN];
08273 char pathStart[MAX_NAME_LEN*2];
08274 int inheritFlag=0;
08275 char myAccessStr[LONG_NAME_LEN];
08276 int adminMode=0;
08277 rodsLong_t iVal;
08278
08279 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl");
08280
08281 if (strncmp(accessLevel, MOD_RESC_PREFIX, strlen(MOD_RESC_PREFIX))==0) {
08282 return(chlModAccessControlResc(rsComm, recursiveFlag,
08283 accessLevel, userName, zone, pathName));
08284 }
08285
08286 adminMode=0;
08287 if (strncmp(accessLevel, MOD_ADMIN_MODE_PREFIX,
08288 strlen(MOD_ADMIN_MODE_PREFIX))==0) {
08289 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
08290 addRErrorMsg (&rsComm->rError, 0,
08291 "You must be the admin to use the -M admin mode");
08292 return(CAT_NO_ACCESS_PERMISSION);
08293 }
08294 strncpy(myAccessStr,accessLevel+strlen(MOD_ADMIN_MODE_PREFIX),
08295 LONG_NAME_LEN);
08296 accessLevel = myAccessStr;
08297 adminMode=1;
08298 }
08299
08300 if (strcmp(accessLevel, AP_NULL)==0) {myAccessLev=ACCESS_NULL; rmFlag=1;}
08301 else if (strcmp(accessLevel,AP_READ)==0) {myAccessLev=ACCESS_READ_OBJECT;}
08302 else if (strcmp(accessLevel,AP_WRITE)==0){myAccessLev=ACCESS_MODIFY_OBJECT;}
08303 else if (strcmp(accessLevel,AP_OWN)==0) {myAccessLev=ACCESS_OWN;}
08304 else if (strcmp(accessLevel,ACCESS_INHERIT)==0) {
08305 inheritFlag=1;
08306 }
08307 else if (strcmp(accessLevel,ACCESS_NO_INHERIT)==0) {
08308 inheritFlag=2;
08309 }
08310 else {
08311 char errMsg[105];
08312 snprintf(errMsg, 100, "access level '%s' is invalid",
08313 accessLevel);
08314 addRErrorMsg (&rsComm->rError, 0, errMsg);
08315 return(CAT_INVALID_ARGUMENT);
08316 }
08317
08318 if (!icss.status) {
08319 return(CATALOG_NOT_CONNECTED);
08320 }
08321
08322 if (adminMode) {
08323
08324
08325 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 14");
08326 status1 = cmlGetIntegerValueFromSql(
08327 "select coll_id from R_COLL_MAIN where coll_name=?",
08328 &iVal, pathName, 0, 0, 0, 0, &icss);
08329 if (status1==CAT_NO_ROWS_FOUND) {
08330 status1=CAT_UNKNOWN_COLLECTION;
08331 }
08332 if (status1==0) status1=iVal;
08333 }
08334 else {
08335
08336
08337 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 1 ");
08338 status1 = cmlCheckDir(pathName,
08339 rsComm->clientUser.userName,
08340 rsComm->clientUser.rodsZone,
08341 ACCESS_OWN,
08342 &icss);
08343 }
08344 if (status1 >= 0) {
08345 snprintf(collIdStr, MAX_NAME_LEN, "%lld", status1);
08346 }
08347
08348 if (status1 < 0 && inheritFlag!=0) {
08349 char errMsg[105];
08350 snprintf(errMsg, 100, "access level '%s' is valid only for collections",
08351 accessLevel);
08352 addRErrorMsg (&rsComm->rError, 0, errMsg);
08353 return(CAT_INVALID_ARGUMENT);
08354 }
08355
08356
08357 if (status1 < 0) {
08358 status2 = splitPathByKey(pathName,
08359 logicalParentDirName, logicalEndName, '/');
08360 if (strlen(logicalParentDirName)==0) {
08361 strcpy(logicalParentDirName, "/");
08362 strcpy(logicalEndName, pathName+1);
08363 }
08364 if (adminMode) {
08365 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 15");
08366 status2 = cmlGetIntegerValueFromSql(
08367 "select data_id from R_DATA_MAIN DM, R_COLL_MAIN CM where DM.data_name=? and DM.coll_id=CM.coll_id and CM.coll_name=?",
08368 &iVal, logicalEndName, logicalParentDirName, 0, 0, 0, &icss);
08369 if (status2==CAT_NO_ROWS_FOUND) status2=CAT_UNKNOWN_FILE;
08370 if (status2==0) status2=iVal;
08371 }
08372 else {
08373
08374
08375 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 2");
08376 status2 = cmlCheckDataObjOnly(logicalParentDirName, logicalEndName,
08377 rsComm->clientUser.userName,
08378 rsComm->clientUser.rodsZone,
08379 ACCESS_OWN, &icss);
08380 }
08381 if (status2 > 0) objId=status2;
08382 }
08383
08384
08385 if (status1 < 0 && status2 < 0) {
08386 char errMsg[205];
08387
08388 if (status1 == CAT_UNKNOWN_COLLECTION && status2 == CAT_UNKNOWN_FILE) {
08389 snprintf(errMsg, 200,
08390 "Input path is not a collection and not a dataObj: %s",
08391 pathName);
08392 addRErrorMsg (&rsComm->rError, 0, errMsg);
08393 return(CAT_INVALID_ARGUMENT);
08394 }
08395 if (status1 != CAT_UNKNOWN_COLLECTION) {
08396 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 12");
08397 status3 = cmlCheckDirOwn(pathName,
08398 rsComm->clientUser.userName,
08399 rsComm->clientUser.rodsZone,
08400 &icss);
08401 if (status3 < 0) return(status1);
08402 snprintf(collIdStr, MAX_NAME_LEN, "%lld", status3);
08403 }
08404 else {
08405 if (status2 == CAT_NO_ACCESS_PERMISSION) {
08406
08407
08408 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 13");
08409 status3 = cmlCheckDataObjOwn(logicalParentDirName, logicalEndName,
08410 rsComm->clientUser.userName,
08411 rsComm->clientUser.rodsZone,
08412 &icss);
08413 if (status3 < 0) {
08414 _rollback("chlModAccessControl");
08415 return(status2);
08416 }
08417 objId = status3;
08418 } else {
08419 return(status2);
08420 }
08421 }
08422 }
08423
08424
08425 if (inheritFlag!=0) {
08426 status = _modInheritance(inheritFlag, recursiveFlag, collIdStr, pathName);
08427 return(status);
08428 }
08429
08430
08431 status = getLocalZone();
08432 if (status != 0) return(status);
08433
08434 myZone=zone;
08435 if (zone == NULL || strlen(zone)==0) {
08436 myZone=localZone;
08437 }
08438
08439 userId=0;
08440 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 3");
08441 status = cmlGetIntegerValueFromSql(
08442 "select user_id from R_USER_MAIN where user_name=? and R_USER_MAIN.zone_name=?",
08443 &userId, userName, myZone, 0, 0, 0, &icss);
08444 if (status != 0) {
08445 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
08446 return(status);
08447 }
08448
08449 snprintf(userIdStr, MAX_NAME_LEN, "%lld", userId);
08450 snprintf(objIdStr, MAX_NAME_LEN, "%lld", objId);
08451
08452 rodsLog(LOG_NOTICE, "recursiveFlag %d",recursiveFlag);
08453
08454
08455 if (recursiveFlag==0) {
08456
08457
08458 if (objId) {
08459 cllBindVars[cllBindVarCount++]=userIdStr;
08460 cllBindVars[cllBindVarCount++]=objIdStr;
08461 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 4");
08462 status = cmlExecuteNoAnswerSql(
08463 "delete from R_OBJT_ACCESS where user_id=? and object_id=?",
08464 &icss);
08465 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08466 return(status);
08467 }
08468 if (rmFlag==0) {
08469 getNowStr(myTime);
08470 cllBindVars[cllBindVarCount++]=objIdStr;
08471 cllBindVars[cllBindVarCount++]=userIdStr;
08472 cllBindVars[cllBindVarCount++]=myAccessLev;
08473 cllBindVars[cllBindVarCount++]=myTime;
08474 cllBindVars[cllBindVarCount++]=myTime;
08475 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 5");
08476 status = cmlExecuteNoAnswerSql(
08477 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) values (?, ?, (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
08478 &icss);
08479 if (status != 0) {
08480 _rollback("chlModAccessControl");
08481 return(status);
08482 }
08483 }
08484
08485
08486 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_OBJ,
08487 objIdStr,
08488 userIdStr,
08489 myAccessLev,
08490 &icss);
08491 if (status != 0) {
08492 rodsLog(LOG_NOTICE,
08493 "chlModAccessControl cmlAudit5 failure %d",
08494 status);
08495 _rollback("chlModAccessControl");
08496 return(status);
08497 }
08498
08499 status = cmlExecuteNoAnswerSql("commit", &icss);
08500 return(status);
08501 }
08502
08503
08504 cllBindVars[cllBindVarCount++]=userIdStr;
08505 cllBindVars[cllBindVarCount++]=collIdStr;
08506 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 6");
08507 status = cmlExecuteNoAnswerSql(
08508 "delete from R_OBJT_ACCESS where user_id=? and object_id=?",
08509 &icss);
08510 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08511 _rollback("chlModAccessControl");
08512 return(status);
08513 }
08514 if (rmFlag) {
08515
08516 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_COLL,
08517 collIdStr,
08518 userIdStr,
08519 myAccessLev,
08520 &icss);
08521 if (status != 0) {
08522 rodsLog(LOG_NOTICE,
08523 "chlModAccessControl cmlAudit5 failure %d",
08524 status);
08525 _rollback("chlModAccessControl");
08526 return(status);
08527 }
08528 status = cmlExecuteNoAnswerSql("commit", &icss);
08529 return(status);
08530 }
08531
08532 getNowStr(myTime);
08533 cllBindVars[cllBindVarCount++]=collIdStr;
08534 cllBindVars[cllBindVarCount++]=userIdStr;
08535 cllBindVars[cllBindVarCount++]=myAccessLev;
08536 cllBindVars[cllBindVarCount++]=myTime;
08537 cllBindVars[cllBindVarCount++]=myTime;
08538 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 7");
08539 status = cmlExecuteNoAnswerSql(
08540 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) values (?, ?, (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ?)",
08541 &icss);
08542
08543 if (status != 0) {
08544 _rollback("chlModAccessControl");
08545 return(status);
08546 }
08547
08548 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_COLL,
08549 collIdStr,
08550 userIdStr,
08551 myAccessLev,
08552 &icss);
08553 if (status != 0) {
08554 rodsLog(LOG_NOTICE,
08555 "chlModAccessControl cmlAudit5 failure %d",
08556 status);
08557 _rollback("chlModAccessControl");
08558 return(status);
08559 }
08560
08561 status = cmlExecuteNoAnswerSql("commit", &icss);
08562 return(status);
08563 }
08564
08565
08566
08567 if (objId) {
08568 char errMsg[205];
08569
08570 snprintf(errMsg, 200,
08571 "Input path is not a collection and recursion was requested: %s",
08572 pathName);
08573 addRErrorMsg (&rsComm->rError, 0, errMsg);
08574 return(CAT_INVALID_ARGUMENT);
08575 }
08576
08577
08578 makeEscapedPath(pathName, pathStart, sizeof(pathStart));
08579 strncat(pathStart, "/%", sizeof(pathStart));
08580
08581 cllBindVars[cllBindVarCount++]=userIdStr;
08582 cllBindVars[cllBindVarCount++]=pathName;
08583 cllBindVars[cllBindVarCount++]=pathStart;
08584
08585 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 8");
08586 status = cmlExecuteNoAnswerSql(
08587 "delete from R_OBJT_ACCESS where user_id=? and object_id in (select data_id from R_DATA_MAIN where coll_id in (select coll_id from R_COLL_MAIN where coll_name = ? or coll_name like ?))",
08588 &icss);
08589
08590 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08591 _rollback("chlModAccessControl");
08592 return(status);
08593 }
08594
08595 cllBindVars[cllBindVarCount++]=userIdStr;
08596 cllBindVars[cllBindVarCount++]=pathName;
08597 cllBindVars[cllBindVarCount++]=pathStart;
08598
08599 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 9");
08600 status = cmlExecuteNoAnswerSql(
08601 "delete from R_OBJT_ACCESS where user_id=? and object_id in (select coll_id from R_COLL_MAIN where coll_name = ? or coll_name like ?)",
08602 &icss);
08603 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08604 _rollback("chlModAccessControl");
08605 return(status);
08606 }
08607 if (rmFlag) {
08608
08609
08610 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_COLL_RECURSIVE,
08611 collIdStr,
08612 userIdStr,
08613 myAccessLev,
08614 &icss);
08615 if (status != 0) {
08616 rodsLog(LOG_NOTICE,
08617 "chlModAccessControl cmlAudit5 failure %d",
08618 status);
08619 _rollback("chlModAccessControl");
08620 return(status);
08621 }
08622
08623 status = cmlExecuteNoAnswerSql("commit", &icss);
08624 return(status);
08625 }
08626
08627 getNowStr(myTime);
08628 makeEscapedPath(pathName, pathStart, sizeof(pathStart));
08629 strncat(pathStart, "/%", sizeof(pathStart));
08630 cllBindVars[cllBindVarCount++]=userIdStr;
08631 cllBindVars[cllBindVarCount++]=myAccessLev;
08632 cllBindVars[cllBindVarCount++]=myTime;
08633 cllBindVars[cllBindVarCount++]=myTime;
08634 cllBindVars[cllBindVarCount++]=pathName;
08635 cllBindVars[cllBindVarCount++]=pathStart;
08636 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 10");
08637 #if ORA_ICAT
08638
08639 status = cmlExecuteNoAnswerSql(
08640 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct data_id, cast(? as integer), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_DATA_MAIN where coll_id in (select coll_id from R_COLL_MAIN where coll_name = ? or coll_name like ?))",
08641 &icss);
08642 #elif MY_ICAT
08643 status = cmlExecuteNoAnswerSql(
08644 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct data_id, ?, (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_DATA_MAIN where coll_id in (select coll_id from R_COLL_MAIN where coll_name = ? or coll_name like ?))",
08645 &icss);
08646 #else
08647 status = cmlExecuteNoAnswerSql(
08648 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct data_id, cast(? as bigint), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_DATA_MAIN where coll_id in (select coll_id from R_COLL_MAIN where coll_name = ? or coll_name like ?))",
08649 &icss);
08650 #endif
08651 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
08652 if (status != 0) {
08653 _rollback("chlModAccessControl");
08654 return(status);
08655 }
08656
08657
08658
08659 cllBindVars[cllBindVarCount++]=userIdStr;
08660 cllBindVars[cllBindVarCount++]=myAccessLev;
08661 cllBindVars[cllBindVarCount++]=myTime;
08662 cllBindVars[cllBindVarCount++]=myTime;
08663 cllBindVars[cllBindVarCount++]=pathName;
08664 cllBindVars[cllBindVarCount++]=pathStart;
08665 if (logSQL!=0) rodsLog(LOG_SQL, "chlModAccessControl SQL 11");
08666 #if ORA_ICAT
08667
08668 status = cmlExecuteNoAnswerSql(
08669 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct coll_id, cast(? as integer), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_COLL_MAIN where coll_name = ? or coll_name like ?)",
08670 &icss);
08671 #elif MY_ICAT
08672 status = cmlExecuteNoAnswerSql(
08673 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct coll_id, ?, (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_COLL_MAIN where coll_name = ? or coll_name like ?)",
08674 &icss);
08675 #else
08676 status = cmlExecuteNoAnswerSql(
08677 "insert into R_OBJT_ACCESS (object_id, user_id, access_type_id, create_ts, modify_ts) (select distinct coll_id, cast(? as bigint), (select token_id from R_TOKN_MAIN where token_namespace = 'access_type' and token_name = ?), ?, ? from R_COLL_MAIN where coll_name = ? or coll_name like ?)",
08678 &icss);
08679 #endif
08680 if (status != 0) {
08681 _rollback("chlModAccessControl");
08682 return(status);
08683 }
08684
08685
08686 status = cmlAudit5(AU_MOD_ACCESS_CONTROL_COLL_RECURSIVE,
08687 collIdStr,
08688 userIdStr,
08689 myAccessLev,
08690 &icss);
08691 if (status != 0) {
08692 rodsLog(LOG_NOTICE,
08693 "chlModAccessControl cmlAudit5 failure %d",
08694 status);
08695 _rollback("chlModAccessControl");
08696 return(status);
08697 }
08698
08699 status = cmlExecuteNoAnswerSql("commit", &icss);
08700 return(status);
08701 }
08702
08703
08704
08705
08706 int chlRenameObject(rsComm_t *rsComm, rodsLong_t objId,
08707 char* newName) {
08708 int status;
08709 rodsLong_t collId;
08710 rodsLong_t otherDataId;
08711 rodsLong_t otherCollId;
08712 char myTime[50];
08713
08714 char parentCollName[MAX_NAME_LEN]="";
08715 char collName[MAX_NAME_LEN]="";
08716 char *cVal[3];
08717 int iVal[3];
08718 int pLen, cLen, len;
08719 int isRootDir=0;
08720 char objIdString[MAX_NAME_LEN];
08721 char collIdString[MAX_NAME_LEN];
08722 char collNameTmp[MAX_NAME_LEN];
08723
08724 char pLenStr[MAX_NAME_LEN];
08725 char cLenStr[MAX_NAME_LEN];
08726 char collNameSlash[MAX_NAME_LEN];
08727 char collNameSlashLen[20];
08728 char slashNewName[MAX_NAME_LEN];
08729
08730 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject");
08731
08732 if (strstr(newName, "/")) {
08733 return(CAT_INVALID_ARGUMENT);
08734 }
08735
08736
08737
08738 collId=0;
08739
08740 snprintf(objIdString, MAX_NAME_LEN, "%lld", objId);
08741 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 1 ");
08742
08743 status = cmlGetIntegerValueFromSql(
08744 "select coll_id from R_DATA_MAIN DM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where DM.data_id=? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = DM.data_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'own'",
08745 &collId, objIdString, rsComm->clientUser.userName, rsComm->clientUser.rodsZone,
08746 0, 0, &icss);
08747
08748 if (status == 0) {
08749
08750
08751 snprintf(collIdString, MAX_NAME_LEN, "%lld", collId);
08752 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 2");
08753 status = cmlGetIntegerValueFromSql(
08754 "select data_id from R_DATA_MAIN where data_name=? and coll_id=?",
08755 &otherDataId,
08756 newName, collIdString, 0, 0, 0, &icss);
08757 if (status!=CAT_NO_ROWS_FOUND) {
08758 return( CAT_NAME_EXISTS_AS_DATAOBJ);
08759 }
08760
08761
08762
08763 snprintf(collNameTmp, MAX_NAME_LEN, "/%s", newName);
08764 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 3");
08765 status = cmlGetIntegerValueFromSql(
08766 "select coll_id from R_COLL_MAIN where coll_name = ( select coll_name from R_COLL_MAIN where coll_id=? ) || ?",
08767 &otherCollId, collIdString, collNameTmp, 0, 0, 0, &icss);
08768 if (status!=CAT_NO_ROWS_FOUND) {
08769 return( CAT_NAME_EXISTS_AS_COLLECTION);
08770 }
08771
08772
08773 getNowStr(myTime);
08774 cllBindVars[cllBindVarCount++]=newName;
08775 cllBindVars[cllBindVarCount++]=objIdString;
08776 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 4");
08777 status = cmlExecuteNoAnswerSql(
08778 "update R_DATA_MAIN set data_name = ? where data_id=?",
08779 &icss);
08780 if (status != 0) {
08781 rodsLog(LOG_NOTICE,
08782 "chlRenameObject cmlExecuteNoAnswerSql update1 failure %d",
08783 status);
08784 _rollback("chlRenameObject");
08785 return(status);
08786 }
08787
08788 cllBindVars[cllBindVarCount++]=myTime;
08789 cllBindVars[cllBindVarCount++]=collIdString;
08790 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 5");
08791 status = cmlExecuteNoAnswerSql(
08792 "update R_COLL_MAIN set modify_ts=? where coll_id=?",
08793 &icss);
08794 if (status != 0) {
08795 rodsLog(LOG_NOTICE,
08796 "chlRenameObject cmlExecuteNoAnswerSql update2 failure %d",
08797 status);
08798 _rollback("chlRenameObject");
08799 return(status);
08800 }
08801
08802
08803 status = cmlAudit3(AU_RENAME_DATA_OBJ,
08804 objIdString,
08805 rsComm->clientUser.userName,
08806 rsComm->clientUser.rodsZone,
08807 newName,
08808 &icss);
08809 if (status != 0) {
08810 rodsLog(LOG_NOTICE,
08811 "chlRenameObject cmlAudit3 failure %d",
08812 status);
08813 _rollback("chlRenameObject");
08814 return(status);
08815 }
08816
08817 return(status);
08818 }
08819
08820
08821
08822
08823 cVal[0]=parentCollName;
08824 iVal[0]=MAX_NAME_LEN;
08825 cVal[1]=collName;
08826 iVal[1]=MAX_NAME_LEN;
08827
08828 snprintf(objIdString, MAX_NAME_LEN, "%lld", objId);
08829 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 6");
08830
08831 status = cmlGetStringValuesFromSql(
08832 "select parent_coll_name, coll_name from R_COLL_MAIN CM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where CM.coll_id=? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = CM.coll_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'own'",
08833 cVal, iVal, 2, objIdString,
08834 rsComm->clientUser.userName, rsComm->clientUser.rodsZone, &icss);
08835 if (status == 0) {
08836
08837
08838
08839 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 7");
08840 status = cmlGetIntegerValueFromSql(
08841 "select data_id from R_DATA_MAIN where data_name=? and coll_id= (select coll_id from R_COLL_MAIN where coll_name = ?)",
08842 &otherDataId, newName, parentCollName, 0, 0, 0, &icss);
08843 if (status!=CAT_NO_ROWS_FOUND) {
08844 return( CAT_NAME_EXISTS_AS_DATAOBJ);
08845 }
08846
08847
08848
08849 snprintf(collNameTmp, MAX_NAME_LEN, "%s/%s", parentCollName, newName);
08850 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 8");
08851 status = cmlGetIntegerValueFromSql(
08852 "select coll_id from R_COLL_MAIN where coll_name = ?",
08853 &otherCollId, collNameTmp, 0, 0, 0, 0, &icss);
08854 if (status!=CAT_NO_ROWS_FOUND) {
08855 return( CAT_NAME_EXISTS_AS_COLLECTION);
08856 }
08857
08858
08859 pLen = strlen(parentCollName);
08860 cLen=strlen(collName);
08861 if (pLen<=0 || cLen <=0) return(CAT_INVALID_ARGUMENT);
08862
08863
08864 if (pLen==1) {
08865 if (strncmp(parentCollName, "/", 20) == 0) {
08866 isRootDir=1;
08867 }
08868 }
08869
08870
08871
08872
08873
08874
08875 snprintf(pLenStr,MAX_NAME_LEN, "%d", pLen);
08876
08877
08878 snprintf(cLenStr,MAX_NAME_LEN, "%d", cLen+1);
08879 snprintf(collNameSlash, MAX_NAME_LEN, "%s/", collName);
08880 len = strlen(collNameSlash);
08881 snprintf(collNameSlashLen, 10, "%d", len);
08882 snprintf(slashNewName, MAX_NAME_LEN, "/%s", newName);
08883 if (isRootDir) {
08884 snprintf(slashNewName, MAX_NAME_LEN, "%s", newName);
08885 }
08886 cllBindVars[cllBindVarCount++]=pLenStr;
08887 cllBindVars[cllBindVarCount++]=slashNewName;
08888 cllBindVars[cllBindVarCount++]=cLenStr;
08889 cllBindVars[cllBindVarCount++]=collNameSlashLen;
08890 cllBindVars[cllBindVarCount++]=collNameSlash;
08891 cllBindVars[cllBindVarCount++]=collName;
08892 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 9");
08893 status = cmlExecuteNoAnswerSql(
08894 "update R_COLL_MAIN set coll_name = substr(coll_name,1,?) || ? || substr(coll_name, ?) where substr(parent_coll_name,1,?) = ? or parent_coll_name = ?",
08895 &icss);
08896 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08897 rodsLog(LOG_NOTICE,
08898 "chlRenameObject cmlExecuteNoAnswerSql update failure %d",
08899 status);
08900 _rollback("chlRenameObject");
08901 return(status);
08902 }
08903
08904
08905 cllBindVars[cllBindVarCount++]=pLenStr;
08906 cllBindVars[cllBindVarCount++]=slashNewName;
08907 cllBindVars[cllBindVarCount++]=cLenStr;
08908 cllBindVars[cllBindVarCount++]=collNameSlashLen;
08909 cllBindVars[cllBindVarCount++]=collNameSlash;
08910 cllBindVars[cllBindVarCount++]=collName;
08911 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 10");
08912 status = cmlExecuteNoAnswerSql(
08913 "update R_COLL_MAIN set parent_coll_name = substr(parent_coll_name,1,?) || ? || substr(parent_coll_name, ?) where substr(parent_coll_name,1,?) = ? or parent_coll_name = ?",
08914 &icss);
08915 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
08916 rodsLog(LOG_NOTICE,
08917 "chlRenameObject cmlExecuteNoAnswerSql update failure %d",
08918 status);
08919 _rollback("chlRenameObject");
08920 return(status);
08921 }
08922
08923
08924 getNowStr(myTime);
08925 snprintf(collNameTmp, MAX_NAME_LEN, "%s/%s", parentCollName, newName);
08926 if (isRootDir) {
08927 snprintf(collNameTmp, MAX_NAME_LEN, "%s%s", parentCollName, newName);
08928 }
08929 cllBindVars[cllBindVarCount++]=collNameTmp;
08930 cllBindVars[cllBindVarCount++]=myTime;
08931 cllBindVars[cllBindVarCount++]=objIdString;
08932 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 11");
08933 status = cmlExecuteNoAnswerSql(
08934 "update R_COLL_MAIN set coll_name=?, modify_ts=? where coll_id=?",
08935 &icss);
08936 if (status != 0) {
08937 rodsLog(LOG_NOTICE,
08938 "chlRenameObject cmlExecuteNoAnswerSql update failure %d",
08939 status);
08940 _rollback("chlRenameObject");
08941 return(status);
08942 }
08943
08944
08945 status = cmlAudit3(AU_RENAME_COLLECTION,
08946 objIdString,
08947 rsComm->clientUser.userName,
08948 rsComm->clientUser.rodsZone,
08949 newName,
08950 &icss);
08951 if (status != 0) {
08952 rodsLog(LOG_NOTICE,
08953 "chlRenameObject cmlAudit3 failure %d",
08954 status);
08955 _rollback("chlRenameObject");
08956 return(status);
08957 }
08958
08959 return(status);
08960
08961 }
08962
08963
08964
08965
08966
08967 snprintf(objIdString, MAX_NAME_LEN, "%lld", objId);
08968 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 12");
08969 status = cmlGetIntegerValueFromSql(
08970 "select coll_id from R_DATA_MAIN where data_id=?",
08971 &otherDataId, objIdString, 0, 0, 0, 0, &icss);
08972 if (status == 0) {
08973
08974 return (CAT_NO_ACCESS_PERMISSION);
08975 }
08976
08977 snprintf(collIdString, MAX_NAME_LEN, "%lld", objId);
08978 if (logSQL!=0) rodsLog(LOG_SQL, "chlRenameObject SQL 12");
08979 status = cmlGetIntegerValueFromSql(
08980 "select coll_id from R_COLL_MAIN where coll_id=?",
08981 &otherDataId, collIdString, 0, 0, 0, 0, &icss);
08982 if (status == 0) {
08983
08984 return (CAT_NO_ACCESS_PERMISSION);
08985 }
08986
08987 return(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION);
08988 }
08989
08990
08991
08992
08993
08994 int chlMoveObject(rsComm_t *rsComm, rodsLong_t objId,
08995 rodsLong_t targetCollId) {
08996 int status;
08997 rodsLong_t collId;
08998 rodsLong_t otherDataId;
08999 rodsLong_t otherCollId;
09000 char myTime[50];
09001
09002 char dataObjName[MAX_NAME_LEN]="";
09003 char *cVal[3];
09004 int iVal[3];
09005
09006 char parentCollName[MAX_NAME_LEN]="";
09007 char oldCollName[MAX_NAME_LEN]="";
09008 char endCollName[MAX_NAME_LEN]="";
09009
09010
09011 char targetCollName[MAX_NAME_LEN]="";
09012 char parentTargetCollName[MAX_NAME_LEN]="";
09013 char newCollName[MAX_NAME_LEN]="";
09014 int pLen, ocLen;
09015 int i, OK, len;
09016 char *cp;
09017 char objIdString[MAX_NAME_LEN];
09018 char collIdString[MAX_NAME_LEN];
09019 char nameTmp[MAX_NAME_LEN];
09020 char ocLenStr[MAX_NAME_LEN];
09021 char collNameSlash[MAX_NAME_LEN];
09022 char collNameSlashLen[20];
09023
09024 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject");
09025
09026
09027
09028 cVal[0]=parentTargetCollName;
09029 iVal[0]=MAX_NAME_LEN;
09030 cVal[1]=targetCollName;
09031 iVal[1]=MAX_NAME_LEN;
09032 snprintf(objIdString, MAX_NAME_LEN, "%lld", targetCollId);
09033 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 1 ");
09034 status = cmlGetStringValuesFromSql(
09035 "select parent_coll_name, coll_name from R_COLL_MAIN CM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where CM.coll_id=? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = CM.coll_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'own'",
09036 cVal, iVal, 2, objIdString,
09037 rsComm->clientUser.userName,
09038 rsComm->clientUser.rodsZone, &icss);
09039
09040 snprintf(collIdString, MAX_NAME_LEN, "%lld", targetCollId);
09041 if (status != 0) {
09042 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 2");
09043 status = cmlGetIntegerValueFromSql(
09044 "select coll_id from R_COLL_MAIN where coll_id=?",
09045 &collId, collIdString, 0, 0, 0, 0, &icss);
09046 if (status==0) {
09047 return (CAT_NO_ACCESS_PERMISSION);
09048
09049 }
09050 return(CAT_UNKNOWN_COLLECTION);
09051 }
09052
09053
09054
09055
09056 snprintf(objIdString, MAX_NAME_LEN, "%lld", objId);
09057 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 3");
09058 status = cmlGetStringValueFromSql(
09059 "select data_name from R_DATA_MAIN DM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where DM.data_id=? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = DM.data_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'own'",
09060 dataObjName, MAX_NAME_LEN, objIdString,
09061 rsComm->clientUser.userName,
09062 rsComm->clientUser.rodsZone, &icss);
09063 snprintf(collIdString, MAX_NAME_LEN, "%lld", targetCollId);
09064 if (status == 0) {
09065
09066
09067
09068 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 4");
09069 status = cmlGetIntegerValueFromSql(
09070 "select data_id from R_DATA_MAIN where data_name=? and coll_id=?",
09071 &otherDataId, dataObjName, collIdString, 0, 0, 0, &icss);
09072 if (status!=CAT_NO_ROWS_FOUND) {
09073 return( CAT_NAME_EXISTS_AS_DATAOBJ);
09074 }
09075
09076
09077
09078
09079 snprintf(nameTmp, MAX_NAME_LEN, "/%s", dataObjName);
09080 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 5");
09081 status = cmlGetIntegerValueFromSql(
09082 "select coll_id from R_COLL_MAIN where coll_name = ( select coll_name from R_COLL_MAIN where coll_id=? ) || ?",
09083 &otherCollId, collIdString, nameTmp, 0, 0, 0, &icss);
09084 if (status!=CAT_NO_ROWS_FOUND) {
09085 return( CAT_NAME_EXISTS_AS_COLLECTION);
09086 }
09087
09088
09089 getNowStr(myTime);
09090 cllBindVars[cllBindVarCount++]=collIdString;
09091 cllBindVars[cllBindVarCount++]=myTime;
09092 cllBindVars[cllBindVarCount++]=objIdString;
09093 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 6");
09094 status = cmlExecuteNoAnswerSql(
09095 "update R_DATA_MAIN set coll_id=?, modify_ts=? where data_id=?",
09096 &icss);
09097 if (status != 0) {
09098 rodsLog(LOG_NOTICE,
09099 "chlMoveObject cmlExecuteNoAnswerSql update1 failure %d",
09100 status);
09101 _rollback("chlMoveObject");
09102 return(status);
09103 }
09104
09105
09106 cllBindVars[cllBindVarCount++]=myTime;
09107 cllBindVars[cllBindVarCount++]=collIdString;
09108 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 7");
09109 status = cmlExecuteNoAnswerSql(
09110 "update R_COLL_MAIN set modify_ts=? where coll_id=?",
09111 &icss);
09112 if (status != 0) {
09113 rodsLog(LOG_NOTICE,
09114 "chlMoveObject cmlExecuteNoAnswerSql update2 failure %d",
09115 status);
09116 _rollback("chlMoveObject");
09117 return(status);
09118 }
09119
09120
09121 status = cmlAudit3(AU_MOVE_DATA_OBJ,
09122 objIdString,
09123 rsComm->clientUser.userName,
09124 rsComm->clientUser.rodsZone,
09125 collIdString,
09126 &icss);
09127 if (status != 0) {
09128 rodsLog(LOG_NOTICE,
09129 "chlMoveObject cmlAudit3 failure %d",
09130 status);
09131 _rollback("chlMoveObject");
09132 return(status);
09133 }
09134
09135 return(status);
09136 }
09137
09138
09139
09140 cVal[0]=parentCollName;
09141 iVal[0]=MAX_NAME_LEN;
09142 cVal[1]=oldCollName;
09143 iVal[1]=MAX_NAME_LEN;
09144
09145 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 8");
09146 status = cmlGetStringValuesFromSql(
09147 "select parent_coll_name, coll_name from R_COLL_MAIN CM, R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where CM.coll_id=? and UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = CM.coll_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'own'",
09148 cVal, iVal, 2, objIdString, rsComm->clientUser.userName,
09149 rsComm->clientUser.rodsZone, &icss);
09150 if (status == 0) {
09151
09152
09153 pLen = strlen(parentCollName);
09154
09155 ocLen=strlen(oldCollName);
09156 if (pLen<=0 || ocLen <=0) return(CAT_INVALID_ARGUMENT);
09157
09158
09159 OK=0;
09160 for (i=ocLen;i>0;i--) {
09161 if (oldCollName[i]=='/') {
09162 OK=1;
09163 strncpy(endCollName, (char*)&oldCollName[i+1], MAX_NAME_LEN);
09164 break;
09165 }
09166 }
09167 if (OK==0) return (CAT_INVALID_ARGUMENT);
09168
09169
09170
09171 snprintf(collIdString, MAX_NAME_LEN, "%lld", targetCollId);
09172 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 9");
09173 status = cmlGetIntegerValueFromSql(
09174 "select data_id from R_DATA_MAIN where data_name=? and coll_id=?",
09175 &otherDataId, endCollName, collIdString, 0, 0, 0, &icss);
09176 if (status!=CAT_NO_ROWS_FOUND) {
09177 return( CAT_NAME_EXISTS_AS_DATAOBJ);
09178 }
09179
09180
09181
09182 strncpy(newCollName, targetCollName, MAX_NAME_LEN);
09183 strncat(newCollName, "/", MAX_NAME_LEN);
09184 strncat(newCollName, endCollName, MAX_NAME_LEN);
09185
09186 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 10");
09187 status = cmlGetIntegerValueFromSql(
09188 "select coll_id from R_COLL_MAIN where coll_name = ?",
09189 &otherCollId, newCollName, 0, 0, 0, 0, &icss);
09190 if (status!=CAT_NO_ROWS_FOUND) {
09191 return( CAT_NAME_EXISTS_AS_COLLECTION);
09192 }
09193
09194
09195
09196
09197 cp = strstr(targetCollName, oldCollName);
09198 if (cp == targetCollName &&
09199 (targetCollName[strlen(oldCollName)] == '/' ||
09200 targetCollName[strlen(oldCollName)] == '\0')) {
09201 return(CAT_RECURSIVE_MOVE);
09202 }
09203
09204
09205
09206
09207
09208
09209 getNowStr(myTime);
09210 cllBindVars[cllBindVarCount++]=newCollName;
09211 cllBindVars[cllBindVarCount++]=targetCollName;
09212 cllBindVars[cllBindVarCount++]=myTime;
09213 cllBindVars[cllBindVarCount++]=objIdString;
09214 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 11");
09215 status = cmlExecuteNoAnswerSql(
09216 "update R_COLL_MAIN set coll_name = ?, parent_coll_name=?, modify_ts=? where coll_id = ?",
09217 &icss);
09218 if (status != 0) {
09219 rodsLog(LOG_NOTICE,
09220 "chlMoveObject cmlExecuteNoAnswerSql update failure %d",
09221 status);
09222 _rollback("chlMoveObject");
09223 return(status);
09224 }
09225
09226
09227
09228
09229
09230
09231 snprintf(ocLenStr, MAX_NAME_LEN, "%d", ocLen+1);
09232 snprintf(collNameSlash, MAX_NAME_LEN, "%s/", oldCollName);
09233 len = strlen(collNameSlash);
09234 snprintf(collNameSlashLen, 10, "%d", len);
09235 cllBindVars[cllBindVarCount++]=newCollName;
09236 cllBindVars[cllBindVarCount++]=ocLenStr;
09237 cllBindVars[cllBindVarCount++]=newCollName;
09238 cllBindVars[cllBindVarCount++]=ocLenStr;
09239 cllBindVars[cllBindVarCount++]=collNameSlashLen;
09240 cllBindVars[cllBindVarCount++]=collNameSlash;
09241 cllBindVars[cllBindVarCount++]=oldCollName;
09242 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 12");
09243 status = cmlExecuteNoAnswerSql(
09244 "update R_COLL_MAIN set parent_coll_name = ? || substr(parent_coll_name, ?), coll_name = ? || substr(coll_name, ?) where substr(parent_coll_name,1,?) = ? or parent_coll_name = ?",
09245 &icss);
09246 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) status = 0;
09247 if (status != 0) {
09248 rodsLog(LOG_NOTICE,
09249 "chlMoveObject cmlExecuteNoAnswerSql update failure %d",
09250 status);
09251 _rollback("chlMoveObject");
09252 return(status);
09253 }
09254
09255
09256 status = cmlAudit3(AU_MOVE_COLL,
09257 objIdString,
09258 rsComm->clientUser.userName,
09259 rsComm->clientUser.rodsZone,
09260 targetCollName,
09261 &icss);
09262 if (status != 0) {
09263 rodsLog(LOG_NOTICE,
09264 "chlMoveObject cmlAudit3 failure %d",
09265 status);
09266 _rollback("chlMoveObject");
09267 return(status);
09268 }
09269
09270 return(status);
09271 }
09272
09273
09274
09275
09276 snprintf(objIdString, MAX_NAME_LEN, "%lld", objId);
09277 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 13");
09278 status = cmlGetIntegerValueFromSql(
09279 "select coll_id from R_DATA_MAIN where data_id=?",
09280 &otherDataId, objIdString, 0, 0, 0, 0, &icss);
09281 if (status == 0) {
09282
09283 return (CAT_NO_ACCESS_PERMISSION);
09284 }
09285
09286 if (logSQL!=0) rodsLog(LOG_SQL, "chlMoveObject SQL 14");
09287 status = cmlGetIntegerValueFromSql(
09288 "select coll_id from R_COLL_MAIN where coll_id=?",
09289 &otherDataId, objIdString, 0, 0, 0, 0, &icss);
09290 if (status == 0) {
09291
09292 return (CAT_NO_ACCESS_PERMISSION);
09293 }
09294
09295 return(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION);
09296 }
09297
09298
09299
09300
09301 int chlRegToken(rsComm_t *rsComm, char *nameSpace, char *name, char *value,
09302 char *value2, char *value3, char *comment)
09303 {
09304 int status;
09305 rodsLong_t objId;
09306 char *myValue1, *myValue2, *myValue3, *myComment;
09307 char myTime[50];
09308 rodsLong_t seqNum;
09309 char errMsg[205];
09310 char seqNumStr[MAX_NAME_LEN];
09311
09312 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegToken");
09313
09314 if (nameSpace==NULL || strlen(nameSpace)==0) return (CAT_INVALID_ARGUMENT);
09315 if (name==NULL || strlen(name)==0) return (CAT_INVALID_ARGUMENT);
09316
09317 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegToken SQL 1 ");
09318 status = cmlGetIntegerValueFromSql(
09319 "select token_id from R_TOKN_MAIN where token_namespace=? and token_name=?",
09320 &objId, "token_namespace", nameSpace, 0, 0, 0, &icss);
09321 if (status != 0) {
09322 snprintf(errMsg, 200,
09323 "Token namespace '%s' does not exist",
09324 nameSpace);
09325 addRErrorMsg (&rsComm->rError, 0, errMsg);
09326 return (CAT_INVALID_ARGUMENT);
09327 }
09328
09329 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegToken SQL 2");
09330 status = cmlGetIntegerValueFromSql(
09331 "select token_id from R_TOKN_MAIN where token_namespace=? and token_name=?",
09332 &objId, nameSpace, name, 0, 0, 0, &icss);
09333 if (status == 0) {
09334 snprintf(errMsg, 200,
09335 "Token '%s' already exists in namespace '%s'",
09336 name, nameSpace);
09337 addRErrorMsg (&rsComm->rError, 0, errMsg);
09338 return (CAT_INVALID_ARGUMENT);
09339 }
09340
09341 myValue1=value;
09342 if (myValue1==NULL) myValue1="";
09343 myValue2=value2;
09344 if (myValue2==NULL) myValue2="";
09345 myValue3=value3;
09346 if (myValue3==NULL) myValue3="";
09347 myComment=comment;
09348 if (myComment==NULL) myComment="";
09349
09350 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegToken SQL 3");
09351 seqNum = cmlGetNextSeqVal(&icss);
09352 if (seqNum < 0) {
09353 rodsLog(LOG_NOTICE, "chlRegToken cmlGetNextSeqVal failure %d",
09354 seqNum);
09355 return(seqNum);
09356 }
09357
09358 getNowStr(myTime);
09359 snprintf(seqNumStr, sizeof seqNumStr, "%lld", seqNum);
09360 cllBindVars[cllBindVarCount++]=nameSpace;
09361 cllBindVars[cllBindVarCount++]=seqNumStr;
09362 cllBindVars[cllBindVarCount++]=name;
09363 cllBindVars[cllBindVarCount++]=myValue1;
09364 cllBindVars[cllBindVarCount++]=myValue2;
09365 cllBindVars[cllBindVarCount++]=myValue3;
09366 cllBindVars[cllBindVarCount++]=myComment;
09367 cllBindVars[cllBindVarCount++]=myTime;
09368 cllBindVars[cllBindVarCount++]=myTime;
09369 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegToken SQL 4");
09370 status = cmlExecuteNoAnswerSql(
09371 "insert into R_TOKN_MAIN values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
09372 &icss);
09373 if (status != 0) {
09374 _rollback("chlRegToken");
09375 return(status);
09376 }
09377
09378
09379 status = cmlAudit3(AU_REG_TOKEN,
09380 seqNumStr,
09381 rsComm->clientUser.userName,
09382 rsComm->clientUser.rodsZone,
09383 name,
09384 &icss);
09385 if (status != 0) {
09386 rodsLog(LOG_NOTICE,
09387 "chlRegToken cmlAudit3 failure %d",
09388 status);
09389 _rollback("chlRegToken");
09390 return(status);
09391 }
09392
09393 status = cmlExecuteNoAnswerSql("commit", &icss);
09394 return(status);
09395 }
09396
09397
09398
09399
09400
09401 int chlDelToken(rsComm_t *rsComm, char *nameSpace, char *name)
09402 {
09403 int status;
09404 rodsLong_t objId;
09405 char errMsg[205];
09406 char objIdStr[60];
09407
09408 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelToken");
09409
09410 if (nameSpace==NULL || strlen(nameSpace)==0) return (CAT_INVALID_ARGUMENT);
09411 if (name==NULL || strlen(name)==0) return (CAT_INVALID_ARGUMENT);
09412
09413 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelToken SQL 1 ");
09414 status = cmlGetIntegerValueFromSql(
09415 "select token_id from R_TOKN_MAIN where token_namespace=? and token_name=?",
09416 &objId, nameSpace, name, 0, 0, 0, &icss);
09417 if (status != 0) {
09418 snprintf(errMsg, 200,
09419 "Token '%s' does not exist in namespace '%s'",
09420 name, nameSpace);
09421 addRErrorMsg (&rsComm->rError, 0, errMsg);
09422 return (CAT_INVALID_ARGUMENT);
09423 }
09424
09425 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelToken SQL 2");
09426 cllBindVars[cllBindVarCount++]=nameSpace;
09427 cllBindVars[cllBindVarCount++]=name;
09428 status = cmlExecuteNoAnswerSql(
09429 "delete from R_TOKN_MAIN where token_namespace=? and token_name=?",
09430 &icss);
09431 if (status != 0) {
09432 _rollback("chlDelToken");
09433 return(status);
09434 }
09435
09436
09437 snprintf(objIdStr, sizeof objIdStr, "%lld", objId);
09438 status = cmlAudit3(AU_DEL_TOKEN,
09439 objIdStr,
09440 rsComm->clientUser.userName,
09441 rsComm->clientUser.rodsZone,
09442 name,
09443 &icss);
09444 if (status != 0) {
09445 rodsLog(LOG_NOTICE,
09446 "chlDelToken cmlAudit3 failure %d",
09447 status);
09448 _rollback("chlDelToken");
09449 return(status);
09450 }
09451
09452 status = cmlExecuteNoAnswerSql("commit", &icss);
09453 return(status);
09454 }
09455
09456
09457
09458
09459
09460
09461
09462 int chlRegServerLoad(rsComm_t *rsComm,
09463 char *hostName, char *rescName,
09464 char *cpuUsed, char *memUsed, char *swapUsed,
09465 char *runqLoad, char *diskSpace, char *netInput,
09466 char *netOutput) {
09467 char myTime[50];
09468 int status;
09469 int i;
09470
09471 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegServerLoad");
09472
09473 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
09474 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
09475 }
09476
09477 if (!icss.status) {
09478 return(CATALOG_NOT_CONNECTED);
09479 }
09480
09481 getNowStr(myTime);
09482
09483 i=0;
09484 cllBindVars[i++]=hostName;
09485 cllBindVars[i++]=rescName;
09486 cllBindVars[i++]=cpuUsed;
09487 cllBindVars[i++]=memUsed;
09488 cllBindVars[i++]=swapUsed;
09489 cllBindVars[i++]=runqLoad;
09490 cllBindVars[i++]=diskSpace;
09491 cllBindVars[i++]=netInput;
09492 cllBindVars[i++]=netOutput;
09493 cllBindVars[i++]=myTime;
09494 cllBindVarCount=i;
09495 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegServerLoad SQL 1");
09496 status = cmlExecuteNoAnswerSql(
09497 "insert into R_SERVER_LOAD (host_name, resc_name, cpu_used, mem_used, swap_used, runq_load, disk_space, net_input, net_output, create_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
09498 &icss);
09499 if (status != 0) {
09500 rodsLog(LOG_NOTICE,
09501 "chlRegServerLoad cmlExecuteNoAnswerSql failure %d",status);
09502 _rollback("chlRegServerLoad");
09503 return(status);
09504 }
09505
09506 status = cmlExecuteNoAnswerSql("commit", &icss);
09507 if (status != 0) {
09508 rodsLog(LOG_NOTICE,
09509 "chlRegServerLoad cmlExecuteNoAnswerSql commit failure %d",
09510 status);
09511 return(status);
09512 }
09513
09514 return(0);
09515 }
09516
09517
09518
09519
09520
09521
09522
09523 int chlPurgeServerLoad(rsComm_t *rsComm, char *secondsAgo) {
09524
09525
09526 int status;
09527 char nowStr[50];
09528 static char thenStr[50];
09529 time_t nowTime;
09530 time_t thenTime;
09531 time_t secondsAgoTime;
09532
09533 if (logSQL!=0) rodsLog(LOG_SQL, "chlPurgeServerLoad");
09534
09535 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
09536 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
09537 }
09538
09539 getNowStr(nowStr);
09540 nowTime=atoll(nowStr);
09541 secondsAgoTime=atoll(secondsAgo);
09542 thenTime = nowTime - secondsAgoTime;
09543 snprintf(thenStr, sizeof thenStr, "%011d", (uint) thenTime);
09544
09545 if (logSQL!=0) rodsLog(LOG_SQL, "chlPurgeServerLoad SQL 1");
09546
09547 cllBindVars[cllBindVarCount++]=thenStr;
09548 status = cmlExecuteNoAnswerSql(
09549 "delete from R_SERVER_LOAD where create_ts <?",
09550 &icss);
09551 if (status != 0) {
09552 _rollback("chlPurgeServerLoad");
09553 return(status);
09554 }
09555
09556 status = cmlExecuteNoAnswerSql("commit", &icss);
09557 return(status);
09558 }
09559
09560
09561
09562
09563
09564
09565 int chlRegServerLoadDigest(rsComm_t *rsComm,
09566 char *rescName, char *loadFactor) {
09567 char myTime[50];
09568 int status;
09569 int i;
09570
09571 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegServerLoadDigest");
09572
09573 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
09574 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
09575 }
09576
09577 if (!icss.status) {
09578 return(CATALOG_NOT_CONNECTED);
09579 }
09580
09581 getNowStr(myTime);
09582
09583 i=0;
09584 cllBindVars[i++]=rescName;
09585 cllBindVars[i++]=loadFactor;
09586 cllBindVars[i++]=myTime;
09587 cllBindVarCount=i;
09588
09589 if (logSQL!=0) rodsLog(LOG_SQL, "chlRegServerLoadDigest SQL 1");
09590 status = cmlExecuteNoAnswerSql(
09591 "insert into R_SERVER_LOAD_DIGEST (resc_name, load_factor, create_ts) values (?, ?, ?)",
09592 &icss);
09593 if (status != 0) {
09594 rodsLog(LOG_NOTICE,
09595 "chlRegServerLoadDigest cmlExecuteNoAnswerSql failure %d", status);
09596 _rollback("chlRegServerLoadDigest");
09597 return(status);
09598 }
09599
09600 status = cmlExecuteNoAnswerSql("commit", &icss);
09601 if (status != 0) {
09602 rodsLog(LOG_NOTICE,
09603 "chlRegServerLoadDigest cmlExecuteNoAnswerSql commit failure %d",
09604 status);
09605 return(status);
09606 }
09607
09608 return(0);
09609 }
09610
09611
09612
09613
09614
09615
09616
09617 int chlPurgeServerLoadDigest(rsComm_t *rsComm, char *secondsAgo) {
09618
09619 int status;
09620 char nowStr[50];
09621 static char thenStr[50];
09622 time_t nowTime;
09623 time_t thenTime;
09624 time_t secondsAgoTime;
09625
09626 if (logSQL!=0) rodsLog(LOG_SQL, "chlPurgeServerLoadDigest");
09627
09628 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
09629 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
09630 }
09631
09632 getNowStr(nowStr);
09633 nowTime=atoll(nowStr);
09634 secondsAgoTime=atoll(secondsAgo);
09635 thenTime = nowTime - secondsAgoTime;
09636 snprintf(thenStr, sizeof thenStr, "%011d", (uint) thenTime);
09637
09638 if (logSQL!=0) rodsLog(LOG_SQL, "chlPurgeServerLoadDigest SQL 1");
09639
09640 cllBindVars[cllBindVarCount++]=thenStr;
09641 status = cmlExecuteNoAnswerSql(
09642 "delete from R_SERVER_LOAD_DIGEST where create_ts <?",
09643 &icss);
09644 if (status != 0) {
09645 _rollback("chlPurgeServerLoadDigest");
09646 return(status);
09647 }
09648
09649 status = cmlExecuteNoAnswerSql("commit", &icss);
09650 return(status);
09651 }
09652
09653
09654
09655
09656
09657
09658
09659
09660
09661 int setOverQuota(rsComm_t *rsComm) {
09662 int status;
09663 int rowsFound;
09664 int statementNum;
09665 char myTime[50];
09666
09667
09668
09669 char mySQL1[]="select sum(quota_usage), UM1.user_id, R_QUOTA_USAGE.resc_id from R_QUOTA_USAGE, R_QUOTA_MAIN, R_USER_MAIN UM1, R_USER_GROUP, R_USER_MAIN UM2 where R_QUOTA_MAIN.user_id = UM1.user_id and UM1.user_type_name = 'rodsgroup' and R_USER_GROUP.group_user_id = UM1.user_id and UM2.user_id = R_USER_GROUP.user_id and R_QUOTA_USAGE.user_id = UM2.user_id and R_QUOTA_MAIN.resc_id = R_QUOTA_USAGE.resc_id group by UM1.user_id, R_QUOTA_USAGE.resc_id";
09670
09671
09672
09673 char mySQL2a[]="select sum(quota_usage), R_QUOTA_MAIN.quota_limit, UM1.user_id from R_QUOTA_USAGE, R_QUOTA_MAIN, R_USER_MAIN UM1, R_USER_GROUP, R_USER_MAIN UM2 where R_QUOTA_MAIN.user_id = UM1.user_id and UM1.user_type_name = 'rodsgroup' and R_USER_GROUP.group_user_id = UM1.user_id and UM2.user_id = R_USER_GROUP.user_id and R_QUOTA_USAGE.user_id = UM2.user_id and R_QUOTA_USAGE.resc_id != %s and R_QUOTA_MAIN.resc_id = %s group by UM1.user_id, R_QUOTA_MAIN.quota_limit";
09674 char mySQL2b[MAX_SQL_SIZE];
09675
09676 char mySQL3a[]="update R_QUOTA_MAIN set quota_over= %s - ?, modify_ts=? where user_id=? and %s - ? > quota_over";
09677 char mySQL3b[MAX_SQL_SIZE];
09678
09679
09680
09681
09682 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 1");
09683 status = cmlExecuteNoAnswerSql(
09684 "update R_QUOTA_MAIN set quota_over = -quota_limit", &icss);
09685 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) return(0);
09686 if (status != 0) return(status);
09687
09688
09689 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 2");
09690 status = cmlExecuteNoAnswerSql(
09691 #if ORA_ICAT
09692 "update R_QUOTA_MAIN set quota_over = (select R_QUOTA_USAGE.quota_usage - R_QUOTA_MAIN.quota_limit from R_QUOTA_USAGE, R_QUOTA_MAIN where R_QUOTA_MAIN.user_id = R_QUOTA_USAGE.user_id and R_QUOTA_MAIN.resc_id = R_QUOTA_USAGE.resc_id) where exists (select 1 from R_QUOTA_USAGE, R_QUOTA_MAIN where R_QUOTA_MAIN.user_id = R_QUOTA_USAGE.user_id and R_QUOTA_MAIN.resc_id = R_QUOTA_USAGE.resc_id)",
09693 #elif MY_ICAT
09694 "update R_QUOTA_MAIN, R_QUOTA_USAGE set R_QUOTA_MAIN.quota_over = R_QUOTA_USAGE.quota_usage - R_QUOTA_MAIN.quota_limit where R_QUOTA_MAIN.user_id = R_QUOTA_USAGE.user_id and R_QUOTA_MAIN.resc_id = R_QUOTA_USAGE.resc_id",
09695 #else
09696 "update R_QUOTA_MAIN set quota_over = quota_usage - quota_limit from R_QUOTA_USAGE where R_QUOTA_MAIN.user_id = R_QUOTA_USAGE.user_id and R_QUOTA_MAIN.resc_id = R_QUOTA_USAGE.resc_id",
09697 #endif
09698 &icss);
09699 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
09700 if (status != 0) return(status);
09701
09702
09703
09704
09705
09706 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 3");
09707 getNowStr(myTime);
09708 for (rowsFound=0;;rowsFound++) {
09709 int status2;
09710 if (rowsFound==0) {
09711 status = cmlGetFirstRowFromSql("select sum(quota_usage), R_QUOTA_MAIN.user_id from R_QUOTA_USAGE, R_QUOTA_MAIN where R_QUOTA_MAIN.user_id = R_QUOTA_USAGE.user_id and R_QUOTA_MAIN.resc_id = '0' group by R_QUOTA_MAIN.user_id",
09712 &statementNum, 0, &icss);
09713 }
09714 else {
09715 status = cmlGetNextRowFromStatement(statementNum, &icss);
09716 }
09717 if (status != 0) break;
09718 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09719 cllBindVars[cllBindVarCount++]=myTime;
09720 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[1];
09721 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09722 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 4");
09723 status2 = cmlExecuteNoAnswerSql("update R_QUOTA_MAIN set quota_over=?-quota_limit, modify_ts=? where user_id=? and ?-quota_limit > quota_over and resc_id='0'",
09724 &icss);
09725 if (status2 == CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
09726 if (status2 != 0) return(status2);
09727 }
09728
09729
09730 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 5");
09731 for (rowsFound=0;;rowsFound++) {
09732 int status2;
09733 if (rowsFound==0) {
09734 status = cmlGetFirstRowFromSql(mySQL1, &statementNum,
09735 0, &icss);
09736 }
09737 else {
09738 status = cmlGetNextRowFromStatement(statementNum, &icss);
09739 }
09740 if (status != 0) break;
09741 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09742 cllBindVars[cllBindVarCount++]=myTime;
09743 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[1];
09744 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09745 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[2];
09746 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 6");
09747 status2 = cmlExecuteNoAnswerSql("update R_QUOTA_MAIN set quota_over=?-quota_limit, modify_ts=? where user_id=? and ?-quota_limit > quota_over and R_QUOTA_MAIN.resc_id=?",
09748 &icss);
09749 if (status2 == CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
09750 if (status2 != 0) return(status2);
09751 }
09752 if (status==CAT_NO_ROWS_FOUND) status=0;
09753 if (status != 0) return(status);
09754
09755
09756 #if ORA_ICAT
09757
09758 snprintf(mySQL2b, sizeof mySQL2b, mySQL2a,
09759 "cast('0' as integer)", "cast('0' as integer)");
09760 snprintf(mySQL3b, sizeof mySQL3b, mySQL3a,
09761 "cast(? as integer)", "cast(? as integer)");
09762 #elif MY_ICAT
09763 snprintf(mySQL2b, sizeof mySQL2b, mySQL2a, "'0'", "'0'");
09764 snprintf(mySQL3b, sizeof mySQL3b, mySQL3a, "?", "?");
09765 #else
09766 snprintf(mySQL2b, sizeof mySQL2b, mySQL2a,
09767 "cast('0' as bigint)", "cast('0' as bigint)");
09768 snprintf(mySQL3b, sizeof mySQL3b, mySQL3a,
09769 "cast(? as bigint)", "cast(? as bigint)");
09770 #endif
09771 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 7");
09772 getNowStr(myTime);
09773 for (rowsFound=0;;rowsFound++) {
09774 int status2;
09775 if (rowsFound==0) {
09776 status = cmlGetFirstRowFromSql(mySQL2b, &statementNum,
09777 0, &icss);
09778 }
09779 else {
09780 status = cmlGetNextRowFromStatement(statementNum, &icss);
09781 }
09782 if (status != 0) break;
09783 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09784 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[1];
09785 cllBindVars[cllBindVarCount++]=myTime;
09786 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[2];
09787 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[0];
09788 cllBindVars[cllBindVarCount++]=icss.stmtPtr[statementNum]->resultValue[1];
09789 if (logSQL!=0) rodsLog(LOG_SQL, "setOverQuota SQL 8");
09790 status2 = cmlExecuteNoAnswerSql(mySQL3b,
09791 &icss);
09792 if (status2 == CAT_SUCCESS_BUT_WITH_NO_INFO) status2=0;
09793 if (status2 != 0) return(status2);
09794 }
09795 if (status==CAT_NO_ROWS_FOUND) status=0;
09796 if (status != 0) return(status);
09797
09798
09799
09800
09801
09802
09803 return(status);
09804 }
09805
09806
09807 int chlCalcUsageAndQuota(rsComm_t *rsComm) {
09808 int status;
09809 char myTime[50];
09810
09811 status = 0;
09812 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
09813 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
09814 }
09815
09816 rodsLog(LOG_NOTICE,
09817 "chlCalcUsageAndQuota called");
09818
09819
09820 getNowStr(myTime);
09821
09822
09823 if (logSQL!=0) rodsLog(LOG_SQL, "chlCalcUsageAndQuota SQL 1");
09824 cllBindVars[cllBindVarCount++]=myTime;
09825 status = cmlExecuteNoAnswerSql(
09826 "delete from R_QUOTA_USAGE where modify_ts < ?", &icss);
09827 if (status !=0 && status !=CAT_SUCCESS_BUT_WITH_NO_INFO) {
09828 _rollback("chlCalcUsageAndQuota");
09829 return(status);
09830 }
09831
09832
09833 if (logSQL!=0) rodsLog(LOG_SQL, "chlCalcUsageAndQuota SQL 2");
09834 cllBindVars[cllBindVarCount++]=myTime;
09835 status = cmlExecuteNoAnswerSql(
09836 "insert into R_QUOTA_USAGE (quota_usage, resc_id, user_id, modify_ts) (select sum(R_DATA_MAIN.data_size), R_RESC_MAIN.resc_id, R_USER_MAIN.user_id, ? from R_DATA_MAIN, R_USER_MAIN, R_RESC_MAIN where R_USER_MAIN.user_name = R_DATA_MAIN.data_owner_name and R_USER_MAIN.zone_name = R_DATA_MAIN.data_owner_zone and R_RESC_MAIN.resc_name = R_DATA_MAIN.resc_name group by R_RESC_MAIN.resc_id, user_id)",
09837 &icss);
09838 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) status=0;
09839 if (status != 0) {
09840 _rollback("chlCalcUsageAndQuota");
09841 return(status);
09842 }
09843
09844
09845 status = setOverQuota(rsComm);
09846 if (status != 0) {
09847 _rollback("chlCalcUsageAndQuota");
09848 return(status);
09849 }
09850
09851 status = cmlExecuteNoAnswerSql("commit", &icss);
09852 return(status);
09853 }
09854
09855 int chlSetQuota(rsComm_t *rsComm, char *type, char *name,
09856 char *rescName, char* limit) {
09857 int status;
09858 rodsLong_t rescId;
09859 rodsLong_t userId;
09860 char userZone[NAME_LEN];
09861 char userName[NAME_LEN];
09862 char rescIdStr[60];
09863 char userIdStr[60];
09864 char myTime[50];
09865 int itype=0;
09866
09867 if (strncmp(type, "user",4)==0) itype=1;
09868 if (strncmp(type, "group",5)==0) itype=2;
09869 if (itype==0) return (CAT_INVALID_ARGUMENT);
09870
09871 status = getLocalZone();
09872 if (status != 0) return(status);
09873
09874
09875 rescId=0;
09876 if (strncmp(rescName,"total",5)!=0) {
09877 if (logSQL!=0) rodsLog(LOG_SQL, "chlSetQuota SQL 1");
09878 status = cmlGetIntegerValueFromSql(
09879 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
09880 &rescId, rescName, localZone, 0, 0, 0, &icss);
09881 if (status != 0) {
09882 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
09883 _rollback("chlSetQuota");
09884 return(status);
09885 }
09886 }
09887
09888
09889 status = parseUserName(name, userName, userZone);
09890 if (userZone[0]=='\0') {
09891 strncpy(userZone, localZone, NAME_LEN);
09892 }
09893
09894 if (itype==1) {
09895 userId=0;
09896 if (logSQL!=0) rodsLog(LOG_SQL, "chlSetQuota SQL 2");
09897 status = cmlGetIntegerValueFromSql(
09898 "select user_id from R_USER_MAIN where user_name=? and zone_name=?",
09899 &userId, userName, userZone, 0, 0, 0, &icss);
09900 if (status != 0) {
09901 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_USER);
09902 _rollback("chlSetQuota");
09903 return(status);
09904 }
09905 }
09906 else {
09907 userId=0;
09908 if (logSQL!=0) rodsLog(LOG_SQL, "chlSetQuota SQL 3");
09909 status = cmlGetIntegerValueFromSql(
09910 "select user_id from R_USER_MAIN where user_name=? and zone_name=? and user_type_name='rodsgroup'",
09911 &userId, userName, userZone, 0, 0, 0, &icss);
09912 if (status != 0) {
09913 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_GROUP);
09914 _rollback("chlSetQuota");
09915 return(status);
09916 }
09917 }
09918
09919 snprintf(userIdStr, sizeof userIdStr, "%lld", userId);
09920 snprintf(rescIdStr, sizeof rescIdStr, "%lld", rescId);
09921
09922
09923 cllBindVars[cllBindVarCount++]=userIdStr;
09924 cllBindVars[cllBindVarCount++]=rescIdStr;
09925 if (logSQL!=0) rodsLog(LOG_SQL, "chlSetQuota SQL 4");
09926 status = cmlExecuteNoAnswerSql(
09927 "delete from R_QUOTA_MAIN where user_id=? and resc_id=?",
09928 &icss);
09929 if (status != 0) {
09930 rodsLog(LOG_DEBUG,
09931 "chlSetQuota cmlExecuteNoAnswerSql delete failure %d",
09932 status);
09933 }
09934 if (atol(limit)>0) {
09935 getNowStr(myTime);
09936 cllBindVars[cllBindVarCount++]=userIdStr;
09937 cllBindVars[cllBindVarCount++]=rescIdStr;
09938 cllBindVars[cllBindVarCount++]=limit;
09939 cllBindVars[cllBindVarCount++]=myTime;
09940 if (logSQL!=0) rodsLog(LOG_SQL, "chlSetQuota SQL 5");
09941 status = cmlExecuteNoAnswerSql(
09942 "insert into R_QUOTA_MAIN (user_id, resc_id, quota_limit, modify_ts) values (?, ?, ?, ?)",
09943 &icss);
09944 if (status != 0) {
09945 rodsLog(LOG_NOTICE,
09946 "chlSetQuota cmlExecuteNoAnswerSql insert failure %d",
09947 status);
09948 _rollback("chlSetQuota");
09949 return(status);
09950 }
09951 }
09952
09953
09954
09955
09956 status = setOverQuota(rsComm);
09957 if (status != 0) {
09958 _rollback("chlSetQuota");
09959 return(status);
09960 }
09961
09962 status = cmlExecuteNoAnswerSql("commit", &icss);
09963 return(status);
09964 }
09965
09966
09967 int
09968 chlCheckQuota(rsComm_t *rsComm, char *userName, char *rescName,
09969 rodsLong_t *userQuota, int *quotaStatus) {
09970
09971
09972
09973
09974
09975
09976
09977
09978
09979 int status;
09980 int statementNum;
09981
09982 char mySQL[]="select distinct QM.user_id, QM.resc_id, QM.quota_limit, QM.quota_over from R_QUOTA_MAIN QM, R_USER_MAIN UM, R_RESC_MAIN RM, R_USER_GROUP UG, R_USER_MAIN UM2 where ( (QM.user_id = UM.user_id and UM.user_name = ?) or (QM.user_id = UG.group_user_id and UM2.user_name = ? and UG.user_id = UM2.user_id) ) and ((QM.resc_id = RM.resc_id and RM.resc_name = ?) or QM.resc_id = '0') order by quota_over desc";
09983
09984 *userQuota = 0;
09985 if (logSQL!=0) rodsLog(LOG_SQL, "chlCheckQuota SQL 1");
09986 cllBindVars[cllBindVarCount++]=userName;
09987 cllBindVars[cllBindVarCount++]=userName;
09988 cllBindVars[cllBindVarCount++]=rescName;
09989
09990 status = cmlGetFirstRowFromSql(mySQL, &statementNum,
09991 0, &icss);
09992
09993 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) {
09994 rodsLog(LOG_NOTICE,
09995 "chlCheckQuota - CAT_SUCCESS_BUT_WITH_NO_INFO");
09996 *quotaStatus = QUOTA_UNRESTRICTED;
09997 return(0);
09998 }
09999
10000 if (status == CAT_NO_ROWS_FOUND) {
10001 rodsLog(LOG_NOTICE,
10002 "chlCheckQuota - CAT_NO_ROWS_FOUND");
10003 *quotaStatus = QUOTA_UNRESTRICTED;
10004 return(0);
10005 }
10006
10007 if (status != 0) return(status);
10008
10009 #if 0
10010 for (i=0;i<4;i++) {
10011 rodsLog(LOG_NOTICE, "checkvalue: %s",
10012 icss.stmtPtr[statementNum]->resultValue[i]);
10013 }
10014 #endif
10015
10016
10017 rodsLog(LOG_NOTICE, "checkQuota: inUser:%s inResc:%s RescId:%s Quota:%s",
10018 userName, rescName,
10019 icss.stmtPtr[statementNum]->resultValue[1],
10020 icss.stmtPtr[statementNum]->resultValue[3]);
10021
10022 *userQuota = atoll(icss.stmtPtr[statementNum]->resultValue[3]);
10023 if (atoi(icss.stmtPtr[statementNum]->resultValue[1])==0) {
10024 *quotaStatus=QUOTA_GLOBAL;
10025 }
10026 else {
10027 *quotaStatus=QUOTA_RESOURCE;
10028 }
10029 cmlFreeStatement(statementNum, &icss);
10030
10031 return(status);
10032 }
10033
10034 int
10035 chlDelUnusedAVUs(rsComm_t *rsComm) {
10036
10037
10038
10039
10040
10041 int status;
10042 status = removeAVUs();
10043
10044 if (status == 0) {
10045 status = cmlExecuteNoAnswerSql("commit", &icss);
10046 }
10047 return(status);
10048
10049 }
10050
10051
10052
10053
10054
10055
10056
10057 int
10058 chlInsRuleTable(rsComm_t *rsComm,
10059 char *baseName, char *mapPriorityStr, char *ruleName,
10060 char *ruleHead, char *ruleCondition, char *ruleAction,
10061 char *ruleRecovery, char *ruleIdStr, char *myTime) {
10062 int status;
10063 int i;
10064 rodsLong_t seqNum = -1;
10065
10066 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsRuleTable");
10067
10068 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10069 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10070 }
10071
10072 if (!icss.status) {
10073 return(CATALOG_NOT_CONNECTED);
10074 }
10075
10076
10077 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsRuleTable SQL 1");
10078 i=0;
10079 cllBindVars[i++]=baseName;
10080 cllBindVars[i++]=ruleName;
10081 cllBindVars[i++]=ruleHead;
10082 cllBindVars[i++]=ruleCondition;
10083 cllBindVars[i++]=ruleAction;
10084 cllBindVars[i++]=ruleRecovery;
10085 cllBindVarCount=i;
10086 status = cmlGetIntegerValueFromSqlV3(
10087 "select rule_id from R_RULE_MAIN where rule_base_name = ? and rule_name = ? and rule_event = ? and rule_condition = ? and rule_body = ? and rule_recovery = ?",
10088 &seqNum,
10089 &icss);
10090 if (status != 0 && status != CAT_NO_ROWS_FOUND) {
10091 rodsLog(LOG_NOTICE,
10092 "chlInsRuleTable cmlGetIntegerValueFromSqlV3 find rule if any failure %d",status);
10093 return(status);
10094 }
10095 if (seqNum < 0) {
10096 seqNum = cmlGetNextSeqVal(&icss);
10097 if (seqNum < 0) {
10098 rodsLog(LOG_NOTICE, "chlInsRuleTable cmlGetNextSeqVal failure %d",
10099 seqNum);
10100 _rollback("chlInsRuleTable");
10101 return(seqNum);
10102 }
10103 snprintf(ruleIdStr, MAX_NAME_LEN, "%lld", seqNum);
10104
10105 i=0;
10106 cllBindVars[i++]=ruleIdStr;
10107 cllBindVars[i++]=baseName;
10108 cllBindVars[i++]=ruleName;
10109 cllBindVars[i++]=ruleHead;
10110 cllBindVars[i++]=ruleCondition;
10111 cllBindVars[i++]=ruleAction;
10112 cllBindVars[i++]=ruleRecovery;
10113 cllBindVars[i++]=rsComm->clientUser.userName;
10114 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10115 cllBindVars[i++]=myTime;
10116 cllBindVars[i++]=myTime;
10117 cllBindVarCount=i;
10118 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsRuleTable SQL 2");
10119 status = cmlExecuteNoAnswerSql(
10120 "insert into R_RULE_MAIN(rule_id, rule_base_name, rule_name, rule_event, rule_condition, rule_body, rule_recovery, rule_owner_name, rule_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
10121 &icss);
10122 if (status != 0) {
10123 rodsLog(LOG_NOTICE,
10124 "chlInsRuleTable cmlExecuteNoAnswerSql Rule Main Insert failure %d",status);
10125 return(status);
10126 }
10127 }
10128 else {
10129 snprintf(ruleIdStr, MAX_NAME_LEN, "%lld", seqNum);
10130 }
10131 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsRuleTable SQL 3");
10132 i = 0;
10133 cllBindVars[i++]=baseName;
10134 cllBindVars[i++]=mapPriorityStr;
10135 cllBindVars[i++]=ruleIdStr;
10136 cllBindVars[i++]=rsComm->clientUser.userName;
10137 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10138 cllBindVars[i++]=myTime;
10139 cllBindVars[i++]=myTime;
10140 cllBindVarCount=i;
10141 status = cmlExecuteNoAnswerSql(
10142 "insert into R_RULE_BASE_MAP (map_base_name, map_priority, rule_id, map_owner_name,map_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?)",
10143 &icss);
10144 if (status != 0) {
10145 rodsLog(LOG_NOTICE,
10146 "chlInsRuleTable cmlExecuteNoAnswerSql Rule Map insert failure %d" , status);
10147
10148 return(status);
10149 }
10150
10151 return(0);
10152 }
10153
10154
10155
10156
10157
10158
10159 int
10160 chlInsDvmTable(rsComm_t *rsComm,
10161 char *baseName, char *varName, char *action,
10162 char *var2CMap, char *myTime) {
10163 int status;
10164 int i;
10165 rodsLong_t seqNum = -1;
10166 char dvmIdStr[MAX_NAME_LEN];
10167 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsDvmTable");
10168
10169 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10170 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10171 }
10172
10173 if (!icss.status) {
10174 return(CATALOG_NOT_CONNECTED);
10175 }
10176
10177
10178 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsDvmTable SQL 1");
10179 i=0;
10180 cllBindVars[i++]=baseName;
10181 cllBindVars[i++]=varName;
10182 cllBindVars[i++]=action;
10183 cllBindVars[i++]=var2CMap;
10184 cllBindVarCount=i;
10185 status = cmlGetIntegerValueFromSqlV3(
10186 "select dvm_id from R_RULE_DVM where dvm_base_name = ? and dvm_ext_var_name = ? and dvm_condition = ? and dvm_int_map_path = ? ",
10187 &seqNum,
10188 &icss);
10189 if (status != 0 && status != CAT_NO_ROWS_FOUND) {
10190 rodsLog(LOG_NOTICE,
10191 "chlInsDvmTable cmlGetIntegerValueFromSqlV3 find DVM if any failure %d",status);
10192 return(status);
10193 }
10194 if (seqNum < 0) {
10195 seqNum = cmlGetNextSeqVal(&icss);
10196 if (seqNum < 0) {
10197 rodsLog(LOG_NOTICE, "chlInsDvmTable cmlGetNextSeqVal failure %d",
10198 seqNum);
10199 _rollback("chlInsDvmTable");
10200 return(seqNum);
10201 }
10202 snprintf(dvmIdStr, MAX_NAME_LEN, "%lld", seqNum);
10203
10204 i=0;
10205 cllBindVars[i++]=dvmIdStr;
10206 cllBindVars[i++]=baseName;
10207 cllBindVars[i++]=varName;
10208 cllBindVars[i++]=action;
10209 cllBindVars[i++]=var2CMap;
10210 cllBindVars[i++]=rsComm->clientUser.userName;
10211 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10212 cllBindVars[i++]=myTime;
10213 cllBindVars[i++]=myTime;
10214 cllBindVarCount=i;
10215 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsDvmTable SQL 2");
10216 status = cmlExecuteNoAnswerSql(
10217 "insert into R_RULE_DVM(dvm_id, dvm_base_name, dvm_ext_var_name, dvm_condition, dvm_int_map_path, dvm_owner_name, dvm_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
10218 &icss);
10219 if (status != 0) {
10220 rodsLog(LOG_NOTICE,
10221 "chlInsDvmTable cmlExecuteNoAnswerSql DVM Main Insert failure %d",status);
10222 return(status);
10223 }
10224 }
10225 else {
10226 snprintf(dvmIdStr, MAX_NAME_LEN, "%lld", seqNum);
10227 }
10228 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsDvmTable SQL 3");
10229 i = 0;
10230 cllBindVars[i++]=baseName;
10231 cllBindVars[i++]=dvmIdStr;
10232 cllBindVars[i++]=rsComm->clientUser.userName;
10233 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10234 cllBindVars[i++]=myTime;
10235 cllBindVars[i++]=myTime;
10236 cllBindVarCount=i;
10237 status = cmlExecuteNoAnswerSql(
10238 "insert into R_RULE_DVM_MAP (map_dvm_base_name, dvm_id, map_owner_name,map_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?)",
10239 &icss);
10240 if (status != 0) {
10241 rodsLog(LOG_NOTICE,
10242 "chlInsDvmTable cmlExecuteNoAnswerSql DVM Map insert failure %d" , status);
10243
10244 return(status);
10245 }
10246
10247 return(0);
10248 }
10249
10250
10251
10252
10253
10254
10255
10256
10257 int
10258 chlInsFnmTable(rsComm_t *rsComm,
10259 char *baseName, char *funcName,
10260 char *func2CMap, char *myTime) {
10261 int status;
10262 int i;
10263 rodsLong_t seqNum = -1;
10264 char fnmIdStr[MAX_NAME_LEN];
10265 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsFnmTable");
10266
10267 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10268 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10269 }
10270
10271 if (!icss.status) {
10272 return(CATALOG_NOT_CONNECTED);
10273 }
10274
10275
10276 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsFnmTable SQL 1");
10277 i=0;
10278 cllBindVars[i++]=baseName;
10279 cllBindVars[i++]=funcName;
10280 cllBindVars[i++]=func2CMap;
10281 cllBindVarCount=i;
10282 status = cmlGetIntegerValueFromSqlV3(
10283 "select fnm_id from R_RULE_FNM where fnm_base_name = ? and fnm_ext_func_name = ? and fnm_int_func_name = ? ",
10284 &seqNum,
10285 &icss);
10286 if (status != 0 && status != CAT_NO_ROWS_FOUND) {
10287 rodsLog(LOG_NOTICE,
10288 "chlInsFnmTable cmlGetIntegerValueFromSqlV3 find FNM if any failure %d",status);
10289 return(status);
10290 }
10291 if (seqNum < 0) {
10292 seqNum = cmlGetNextSeqVal(&icss);
10293 if (seqNum < 0) {
10294 rodsLog(LOG_NOTICE, "chlInsFnmTable cmlGetNextSeqVal failure %d",
10295 seqNum);
10296 _rollback("chlInsFnmTable");
10297 return(seqNum);
10298 }
10299 snprintf(fnmIdStr, MAX_NAME_LEN, "%lld", seqNum);
10300
10301 i=0;
10302 cllBindVars[i++]=fnmIdStr;
10303 cllBindVars[i++]=baseName;
10304 cllBindVars[i++]=funcName;
10305 cllBindVars[i++]=func2CMap;
10306 cllBindVars[i++]=rsComm->clientUser.userName;
10307 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10308 cllBindVars[i++]=myTime;
10309 cllBindVars[i++]=myTime;
10310 cllBindVarCount=i;
10311 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsFnmTable SQL 2");
10312 status = cmlExecuteNoAnswerSql(
10313 "insert into R_RULE_FNM(fnm_id, fnm_base_name, fnm_ext_func_name, fnm_int_func_name, fnm_owner_name, fnm_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?)",
10314 &icss);
10315 if (status != 0) {
10316 rodsLog(LOG_NOTICE,
10317 "chlInsFnmTable cmlExecuteNoAnswerSql FNM Main Insert failure %d",status);
10318 return(status);
10319 }
10320 }
10321 else {
10322 snprintf(fnmIdStr, MAX_NAME_LEN, "%lld", seqNum);
10323 }
10324 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsFnmTable SQL 3");
10325 i = 0;
10326 cllBindVars[i++]=baseName;
10327 cllBindVars[i++]=fnmIdStr;
10328 cllBindVars[i++]=rsComm->clientUser.userName;
10329 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10330 cllBindVars[i++]=myTime;
10331 cllBindVars[i++]=myTime;
10332 cllBindVarCount=i;
10333 status = cmlExecuteNoAnswerSql(
10334 "insert into R_RULE_FNM_MAP (map_fnm_base_name, fnm_id, map_owner_name,map_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?)",
10335 &icss);
10336 if (status != 0) {
10337 rodsLog(LOG_NOTICE,
10338 "chlInsFnmTable cmlExecuteNoAnswerSql FNM Map insert failure %d" , status);
10339
10340 return(status);
10341 }
10342
10343 return(0);
10344 }
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355 int chlInsMsrvcTable(rsComm_t *rsComm,
10356 char *moduleName, char *msrvcName,
10357 char *msrvcSignature, char *msrvcVersion,
10358 char *msrvcHost, char *msrvcLocation,
10359 char *msrvcLanguage, char *msrvcTypeName, char *msrvcStatus,
10360 char *myTime)
10361 {
10362 int status;
10363 int i;
10364 rodsLong_t seqNum = -1;
10365 char msrvcIdStr[MAX_NAME_LEN];
10366 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable");
10367
10368 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10369 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10370 }
10371
10372 if (!icss.status) {
10373 return(CATALOG_NOT_CONNECTED);
10374 }
10375
10376
10377 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable SQL 1");
10378 i=0;
10379 cllBindVars[i++]=moduleName;
10380 cllBindVars[i++]=msrvcName;
10381 cllBindVarCount=i;
10382 status = cmlGetIntegerValueFromSqlV3(
10383 "select msrvc_id from R_MICROSRVC_MAIN where msrvc_module_name = ? and msrvc_name = ? ",
10384 &seqNum,
10385 &icss);
10386 if (status != 0 && status != CAT_NO_ROWS_FOUND) {
10387 rodsLog(LOG_NOTICE,
10388 "chlInsMsrvcTable cmlGetIntegerValueFromSqlV3 find MSRVC if any failure %d",status);
10389 return(status);
10390 }
10391 if (seqNum < 0) {
10392 seqNum = cmlGetNextSeqVal(&icss);
10393 if (seqNum < 0) {
10394 rodsLog(LOG_NOTICE, "chlInsMsrvcTable cmlGetNextSeqVal failure %d",
10395 seqNum);
10396 _rollback("chlInsMsrvcTable");
10397 return(seqNum);
10398 }
10399 snprintf(msrvcIdStr, MAX_NAME_LEN, "%lld", seqNum);
10400
10401 i=0;
10402 cllBindVars[i++]=msrvcIdStr;
10403 cllBindVars[i++]=msrvcName;
10404 cllBindVars[i++]=moduleName;
10405 cllBindVars[i++]=msrvcSignature;
10406 cllBindVars[i++]=rsComm->clientUser.userName;
10407 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10408 cllBindVars[i++]=myTime;
10409 cllBindVars[i++]=myTime;
10410 cllBindVarCount=i;
10411 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable SQL 2");
10412 status = cmlExecuteNoAnswerSql(
10413 "insert into R_MICROSRVC_MAIN(msrvc_id, msrvc_name, msrvc_module_name, msrvc_signature, msrvc_doxygen, msrvc_variations, msrvc_owner_name, msrvc_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, 'NONE', 'NONE', ?, ?, ?, ?)",
10414 &icss);
10415 if (status != 0) {
10416 rodsLog(LOG_NOTICE,
10417 "chlInsMsrvcTable cmlExecuteNoAnswerSql R_MICROSRVC_MAIN Insert failure %d",status);
10418 return(status);
10419 }
10420
10421 i=0;
10422 cllBindVars[i++]=msrvcIdStr;
10423 cllBindVars[i++]=msrvcVersion;
10424 cllBindVars[i++]=msrvcHost;
10425 cllBindVars[i++]=msrvcLocation;
10426 cllBindVars[i++]=msrvcLanguage;
10427 cllBindVars[i++]=msrvcTypeName;
10428 cllBindVars[i++]=msrvcStatus;
10429 cllBindVars[i++]=rsComm->clientUser.userName;
10430 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10431 cllBindVars[i++]=myTime;
10432 cllBindVars[i++]=myTime;
10433 cllBindVarCount=i;
10434 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable SQL 3");
10435 status = cmlExecuteNoAnswerSql(
10436 "insert into R_MICROSRVC_VER(msrvc_id, msrvc_version, msrvc_host, msrvc_location, msrvc_language, msrvc_type_name, msrvc_status, msrvc_owner_name, msrvc_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
10437 &icss);
10438 if (status != 0) {
10439 rodsLog(LOG_NOTICE,
10440 "chlInsMsrvcTable cmlExecuteNoAnswerSql R_MICROSRVC_VER Insert failure %d",status);
10441 return(status);
10442 }
10443 }
10444 else {
10445 snprintf(msrvcIdStr, MAX_NAME_LEN, "%lld", seqNum);
10446
10447 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable SQL 4");
10448 i=0;
10449 cllBindVars[i++]=msrvcIdStr;
10450 cllBindVars[i++]=msrvcHost;
10451 cllBindVars[i++]=msrvcLocation;
10452 cllBindVarCount=i;
10453 status = cmlGetIntegerValueFromSqlV3(
10454 "select msrvc_id from R_MICROSRVC_VER where msrvc_id = ? and msrvc_host = ? and msrvc_location = ? ",
10455 &seqNum, &icss);
10456 if (status != 0 && status != CAT_NO_ROWS_FOUND) {
10457 rodsLog(LOG_NOTICE,
10458 "chlInsMsrvcTable cmlGetIntegerValueFromSqlV4 find MSRVC_HOST if any failure %d",status);
10459 return(status);
10460 }
10461
10462 i=0;
10463 cllBindVars[i++]=msrvcIdStr;
10464 cllBindVars[i++]=msrvcVersion;
10465 cllBindVars[i++]=msrvcHost;
10466 cllBindVars[i++]=msrvcLocation;
10467 cllBindVars[i++]=msrvcLanguage;
10468 cllBindVars[i++]=msrvcTypeName;
10469 cllBindVars[i++]=rsComm->clientUser.userName;
10470 cllBindVars[i++]=rsComm->clientUser.rodsZone;
10471 cllBindVars[i++]=myTime;
10472 cllBindVars[i++]=myTime;
10473 cllBindVarCount=i;
10474 if (logSQL!=0) rodsLog(LOG_SQL, "chlInsMsrvcTable SQL 3");
10475 status = cmlExecuteNoAnswerSql(
10476 "insert into R_MICROSRVC_VER(msrvc_id, msrvc_version, msrvc_host, msrvc_location, msrvc_language, msrvc_type_name, msrvc_owner_name, msrvc_owner_zone, create_ts, modify_ts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
10477 &icss);
10478 if (status != 0) {
10479 rodsLog(LOG_NOTICE,
10480 "chlInsMsrvcTable cmlExecuteNoAnswerSql R_MICROSRVC_VER Insert failure %d",status);
10481 return(status);
10482 }
10483 }
10484
10485 return(0);
10486 }
10487
10488
10489
10490
10491
10492
10493
10494 int
10495 chlVersionRuleBase(rsComm_t *rsComm,
10496 char *baseName, char *myTime) {
10497
10498 int i, status;
10499
10500 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionRuleBase");
10501
10502 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10503 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10504 }
10505
10506 if (!icss.status) {
10507 return(CATALOG_NOT_CONNECTED);
10508 }
10509
10510 i=0;
10511 cllBindVars[i++]=myTime;
10512 cllBindVars[i++]=myTime;
10513 cllBindVars[i++]=baseName;
10514 cllBindVarCount=i;
10515 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionRuleBase SQL 1");
10516
10517 status = cmlExecuteNoAnswerSql(
10518 "update R_RULE_BASE_MAP set map_version = ?, modify_ts = ? where map_base_name = ? and map_version = '0'",&icss);
10519 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
10520 rodsLog(LOG_NOTICE,
10521 "chlVersionRuleBase cmlExecuteNoAnswerSql Rule Map version update failure %d" , status);
10522 return(status);
10523
10524 }
10525
10526 return(0);
10527 }
10528
10529
10530
10531
10532
10533
10534
10535 int
10536 chlVersionDvmBase(rsComm_t *rsComm,
10537 char *baseName, char *myTime) {
10538 int i, status;
10539
10540 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionDvmBase");
10541
10542 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10543 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10544 }
10545
10546 if (!icss.status) {
10547 return(CATALOG_NOT_CONNECTED);
10548 }
10549
10550 i=0;
10551 cllBindVars[i++]=myTime;
10552 cllBindVars[i++]=myTime;
10553 cllBindVars[i++]=baseName;
10554 cllBindVarCount=i;
10555 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionDvmBase SQL 1");
10556
10557 status = cmlExecuteNoAnswerSql(
10558 "update R_RULE_DVM_MAP set map_dvm_version = ?, modify_ts = ? where map_dvm_base_name = ? and map_dvm_version = '0'",&icss);
10559 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
10560 rodsLog(LOG_NOTICE,
10561 "chlVersionDvmBase cmlExecuteNoAnswerSql DVM Map version update failure %d" , status);
10562 return(status);
10563
10564 }
10565
10566 return(0);
10567 }
10568
10569
10570
10571
10572
10573
10574
10575 int
10576 chlVersionFnmBase(rsComm_t *rsComm,
10577 char *baseName, char *myTime) {
10578
10579 int i, status;
10580
10581 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionFnmBase");
10582
10583 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10584 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10585 }
10586
10587 if (!icss.status) {
10588 return(CATALOG_NOT_CONNECTED);
10589 }
10590
10591 i=0;
10592 cllBindVars[i++]=myTime;
10593 cllBindVars[i++]=myTime;
10594 cllBindVars[i++]=baseName;
10595 cllBindVarCount=i;
10596 if (logSQL!=0) rodsLog(LOG_SQL, "chlVersionFnmBase SQL 1");
10597
10598 status = cmlExecuteNoAnswerSql(
10599 "update R_RULE_FNM_MAP set map_fnm_version = ?, modify_ts = ? where map_fnm_base_name = ? and map_fnm_version = '0'",&icss);
10600 if (status != 0 && status != CAT_SUCCESS_BUT_WITH_NO_INFO) {
10601 rodsLog(LOG_NOTICE,
10602 "chlVersionFnmBase cmlExecuteNoAnswerSql FNM Map version update failure %d" , status);
10603 return(status);
10604
10605 }
10606
10607 return(0);
10608 }
10609
10610
10611
10612
10613
10614
10615
10616 int
10617 icatCheckResc(char *rescName) {
10618 int status;
10619 rodsLong_t rescId;
10620 status = getLocalZone();
10621 if (status != 0) return(status);
10622
10623 rescId=0;
10624 if (logSQL!=0) rodsLog(LOG_SQL, "icatCheckResc SQxL 1");
10625 status = cmlGetIntegerValueFromSql(
10626 "select resc_id from R_RESC_MAIN where resc_name=? and zone_name=?",
10627 &rescId, rescName, localZone, 0, 0, 0, &icss);
10628 if (status != 0) {
10629 if (status==CAT_NO_ROWS_FOUND) return(CAT_INVALID_RESOURCE);
10630 _rollback("icatCheckResc");
10631 }
10632 return(status);
10633 }
10634
10635 int
10636 chlAddSpecificQuery(rsComm_t *rsComm, char *sql, char *alias) {
10637 int status, i;
10638 char myTime[50];
10639 char tsCreateTime[50];
10640 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddSpecificQuery");
10641
10642 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10643 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10644 }
10645
10646 if (strlen(sql) < 5) {
10647 return(CAT_INVALID_ARGUMENT);
10648 }
10649
10650 if (!icss.status) {
10651 return(CATALOG_NOT_CONNECTED);
10652 }
10653
10654 getNowStr(myTime);
10655
10656 if (alias != NULL && strlen(alias)>0) {
10657 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddSpecificQuery SQL 1");
10658 status = cmlGetStringValueFromSql(
10659 "select create_ts from R_SPECIFIC_QUERY where alias=?",
10660 tsCreateTime, 50,
10661 alias, "" , "", &icss);
10662 if (status==0) {
10663 i = addRErrorMsg (&rsComm->rError, 0, "Alias is not unique");
10664 return(CAT_INVALID_ARGUMENT);
10665 }
10666 i=0;
10667 cllBindVars[i++]=sql;
10668 cllBindVars[i++]=alias;
10669 cllBindVars[i++]=myTime;
10670 cllBindVarCount=i;
10671 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddSpecificQuery SQL 2");
10672 status = cmlExecuteNoAnswerSql(
10673 "insert into R_SPECIFIC_QUERY (sqlStr, alias, create_ts) values (?, ?, ?)",
10674 &icss);
10675 }
10676 else {
10677 i=0;
10678 cllBindVars[i++]=sql;
10679 cllBindVars[i++]=myTime;
10680 cllBindVarCount=i;
10681 if (logSQL!=0) rodsLog(LOG_SQL, "chlAddSpecificQuery SQL 3");
10682 status = cmlExecuteNoAnswerSql(
10683 "insert into R_SPECIFIC_QUERY (sqlStr, create_ts) values (?, ?)",
10684 &icss);
10685 }
10686
10687 if (status != 0) {
10688 rodsLog(LOG_NOTICE,
10689 "chlAddSpecificQuery cmlExecuteNoAnswerSql insert failure %d",
10690 status);
10691 return(status);
10692 }
10693
10694 status = cmlExecuteNoAnswerSql("commit", &icss);
10695 return(status);
10696 }
10697
10698 int
10699 chlDelSpecificQuery(rsComm_t *rsComm, char *sqlOrAlias) {
10700 int status, i;
10701 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelSpecificQuery");
10702
10703 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10704 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10705 }
10706
10707 if (!icss.status) {
10708 return(CATALOG_NOT_CONNECTED);
10709 }
10710
10711 i=0;
10712 cllBindVars[i++]=sqlOrAlias;
10713 cllBindVarCount=i;
10714 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelSpecificQuery SQL 1");
10715 status = cmlExecuteNoAnswerSql(
10716 "delete from R_SPECIFIC_QUERY where sqlStr = ?",
10717 &icss);
10718
10719 if (status==CAT_SUCCESS_BUT_WITH_NO_INFO) {
10720 if (logSQL!=0) rodsLog(LOG_SQL, "chlDelSpecificQuery SQL 2");
10721 i=0;
10722 cllBindVars[i++]=sqlOrAlias;
10723 cllBindVarCount=i;
10724 status = cmlExecuteNoAnswerSql(
10725 "delete from R_SPECIFIC_QUERY where alias = ?",
10726 &icss);
10727 }
10728
10729 if (status != 0) {
10730 rodsLog(LOG_NOTICE,
10731 "chlDelSpecificQuery cmlExecuteNoAnswerSql delete failure %d",
10732 status);
10733 return(status);
10734 }
10735
10736 status = cmlExecuteNoAnswerSql("commit", &icss);
10737 return(status);
10738 }
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748 #define MINIMUM_COL_SIZE 50
10749
10750 int
10751 chlSpecificQuery(specificQueryInp_t specificQueryInp, genQueryOut_t *result) {
10752 int i, j, k;
10753 int needToGetNextRow;
10754
10755 char combinedSQL[MAX_SQL_SIZE];
10756
10757 int status, statementNum;
10758 int numOfCols;
10759 int attriTextLen;
10760 int totalLen;
10761 int maxColSize;
10762 int currentMaxColSize;
10763 char *tResult, *tResult2;
10764 char tsCreateTime[50];
10765
10766 int debug=0;
10767
10768 icatSessionStruct *icss;
10769
10770 if (logSQL!=0) rodsLog(LOG_SQL, "chlSpecificQuery");
10771
10772 result->attriCnt=0;
10773 result->rowCnt=0;
10774 result->totalRowCount = 0;
10775
10776 currentMaxColSize=0;
10777
10778 icss = chlGetRcs();
10779 if (icss==NULL) return(CAT_NOT_OPEN);
10780 #ifdef ADDR_64BITS
10781 if (debug) printf("icss=%ld\n",(long int)icss);
10782 #else
10783 if (debug) printf("icss=%d\n",(int)icss);
10784 #endif
10785
10786 if (specificQueryInp.continueInx == 0) {
10787 if (specificQueryInp.sql == NULL) {
10788 return(CAT_INVALID_ARGUMENT);
10789 }
10790
10791
10792
10793 if (logSQL!=0) rodsLog(LOG_SQL, "chlSpecificQuery SQL 1");
10794 status = cmlGetStringValueFromSql(
10795 "select create_ts from R_SPECIFIC_QUERY where sqlStr=?",
10796 tsCreateTime, 50,
10797 specificQueryInp.sql, "" , "", icss);
10798 if (status == CAT_NO_ROWS_FOUND) {
10799 int status2;
10800 if (logSQL!=0) rodsLog(LOG_SQL, "chlSpecificQuery SQL 2");
10801 status2 = cmlGetStringValueFromSql(
10802 "select sqlStr from R_SPECIFIC_QUERY where alias=?",
10803 combinedSQL, sizeof(combinedSQL),
10804 specificQueryInp.sql, "" , "", icss);
10805 if (status2 == CAT_NO_ROWS_FOUND) return(CAT_UNKNOWN_SPECIFIC_QUERY);
10806 if (status2 != 0) return(status2);
10807 }
10808 else {
10809 if (status != 0) return(status);
10810 strncpy(combinedSQL, specificQueryInp.sql, sizeof(combinedSQL));
10811 }
10812
10813 i=0;
10814 while (specificQueryInp.args[i]!=NULL && strlen(specificQueryInp.args[i])>0) {
10815 cllBindVars[cllBindVarCount++]=specificQueryInp.args[i++];
10816 }
10817
10818 if (logSQL!=0) rodsLog(LOG_SQL, "chlSpecificQuery SQL 3");
10819 status = cmlGetFirstRowFromSql(combinedSQL, &statementNum,
10820 specificQueryInp.rowOffset, icss);
10821 if (status < 0) {
10822 if (status != CAT_NO_ROWS_FOUND) {
10823 rodsLog(LOG_NOTICE,
10824 "chlSpecificQuery cmlGetFirstRowFromSql failure %d",
10825 status);
10826 }
10827 return(status);
10828 }
10829
10830 result->continueInx = statementNum+1;
10831 if (debug) printf("statement number =%d\n", statementNum);
10832 needToGetNextRow = 0;
10833 }
10834 else {
10835 statementNum = specificQueryInp.continueInx-1;
10836 needToGetNextRow = 1;
10837 if (specificQueryInp.maxRows<=0) {
10838 status = cmlFreeStatement(statementNum, icss);
10839 return(status);
10840 }
10841 }
10842 for (i=0;i<specificQueryInp.maxRows;i++) {
10843 if (needToGetNextRow) {
10844 status = cmlGetNextRowFromStatement(statementNum, icss);
10845 if (status == CAT_NO_ROWS_FOUND) {
10846 cmlFreeStatement(statementNum, icss);
10847 result->continueInx=0;
10848 if (result->rowCnt==0) return(status);
10849
10850 return(0);
10851 }
10852 if (status < 0) return(status);
10853 }
10854 needToGetNextRow = 1;
10855
10856 result->rowCnt++;
10857 if (debug) printf("result->rowCnt=%d\n", result->rowCnt);
10858 numOfCols = icss->stmtPtr[statementNum]->numOfCols;
10859 if (debug) printf("numOfCols=%d\n",numOfCols);
10860 result->attriCnt=numOfCols;
10861 result->continueInx = statementNum+1;
10862
10863 maxColSize=0;
10864
10865 for (k=0;k<numOfCols;k++) {
10866 j = strlen(icss->stmtPtr[statementNum]->resultValue[k]);
10867 if (maxColSize <= j) maxColSize=j;
10868 }
10869 maxColSize++;
10870 if (maxColSize < MINIMUM_COL_SIZE) {
10871 maxColSize=MINIMUM_COL_SIZE;
10872 }
10873 if (debug) printf("maxColSize=%d\n",maxColSize);
10874
10875 if (i==0) {
10876 attriTextLen= numOfCols * maxColSize;
10877 if (debug) printf("attriTextLen=%d\n",attriTextLen);
10878 totalLen = attriTextLen * specificQueryInp.maxRows;
10879 for (j=0;j<numOfCols;j++) {
10880 tResult = (char *) malloc(totalLen);
10881 if (tResult==NULL) return(SYS_MALLOC_ERR);
10882 memset(tResult, 0, totalLen);
10883 result->sqlResult[j].attriInx = 0;
10884
10885
10886 result->sqlResult[j].len = maxColSize;
10887 result->sqlResult[j].value = tResult;
10888 }
10889 currentMaxColSize = maxColSize;
10890 }
10891
10892
10893
10894
10895
10896
10897 if (maxColSize > currentMaxColSize) {
10898 maxColSize += MINIMUM_COL_SIZE;
10899
10900 if (debug) printf("Bumping %d to %d\n",
10901 currentMaxColSize, maxColSize);
10902 attriTextLen= numOfCols * maxColSize;
10903 if (debug) printf("attriTextLen=%d\n",attriTextLen);
10904 totalLen = attriTextLen * specificQueryInp.maxRows;
10905 for (j=0;j<numOfCols;j++) {
10906 char *cp1, *cp2;
10907 int k;
10908 tResult = (char *) malloc(totalLen);
10909 if (tResult==NULL) return(SYS_MALLOC_ERR);
10910 memset(tResult, 0, totalLen);
10911 cp1 = result->sqlResult[j].value;
10912 cp2 = tResult;
10913 for (k=0;k<result->rowCnt;k++) {
10914 strncpy(cp2, cp1, result->sqlResult[j].len);
10915 cp1 += result->sqlResult[j].len;
10916 cp2 += maxColSize;
10917 }
10918 free(result->sqlResult[j].value);
10919 result->sqlResult[j].len = maxColSize;
10920 result->sqlResult[j].value = tResult;
10921 }
10922 currentMaxColSize = maxColSize;
10923 }
10924
10925
10926
10927 for (j=0;j<numOfCols;j++) {
10928 tResult2 = result->sqlResult[j].value;
10929 tResult2 += currentMaxColSize*(result->rowCnt-1);
10930
10931 strncpy(tResult2, icss->stmtPtr[statementNum]->resultValue[j],
10932 currentMaxColSize);
10933 }
10934
10935 }
10936
10937 result->continueInx=statementNum+1;
10938
10939 return(0);
10940
10941 }
10942
10943
10944
10945
10946
10947
10948
10949 int chlSubstituteResourceHierarchies(rsComm_t *rsComm, const char *old_hier, const char *new_hier) {
10950 int status = 0;
10951 char old_hier_partial[MAX_NAME_LEN];
10952 eirods::sql_logger logger("chlSubstituteResourceHierarchies", logSQL);
10953
10954 logger.log();
10955
10956
10957
10958 if (!icss.status) {
10959 return(CATALOG_NOT_CONNECTED);
10960 }
10961 if (!rsComm || !old_hier || !new_hier) {
10962 return(SYS_INTERNAL_NULL_INPUT_ERR);
10963 }
10964 if (rsComm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH || rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
10965 return(CAT_INSUFFICIENT_PRIVILEGE_LEVEL);
10966 }
10967
10968
10969
10970 snprintf(old_hier_partial, MAX_NAME_LEN, "%s%s%%", old_hier, eirods::hierarchy_parser::delimiter().c_str());
10971
10972
10973
10974 cllBindVars[cllBindVarCount++]=(char*)new_hier;
10975 cllBindVars[cllBindVarCount++]=(char*)old_hier;
10976 cllBindVars[cllBindVarCount++]=(char*)old_hier;
10977 cllBindVars[cllBindVarCount++]=old_hier_partial;
10978 #if ORA_ICAT // Oracle
10979 status = cmlExecuteNoAnswerSql("update R_DATA_MAIN set resc_hier = ? || substr(resc_hier, (length(?)+1)) where resc_hier = ? or resc_hier like ?", &icss);
10980 #else // Postgres and MySQL
10981 status = cmlExecuteNoAnswerSql("update R_DATA_MAIN set resc_hier = ? || substring(resc_hier from (char_length(?)+1)) where resc_hier = ? or resc_hier like ?", &icss);
10982 #endif
10983
10984
10985 if (status == CAT_SUCCESS_BUT_WITH_NO_INFO) {
10986 return 0;
10987 }
10988
10989
10990
10991 if (status < 0) {
10992 std::stringstream ss;
10993 ss << "chlSubstituteResourceHierarchies: cmlExecuteNoAnswerSql update failure " << status;
10994 eirods::log(LOG_NOTICE, ss.str());
10995 _rollback("chlSubstituteResourceHierarchies");
10996 }
10997
10998 return status;
10999 }
11000
11001
11002
11003
11004 int chlGetDistinctDataObjCountOnResource(
11005 const std::string& _resc_name,
11006 long long& _count ) {
11007
11008
11009 char query[ MAX_NAME_LEN ];
11010 std::string base_query = "select count(distinct data_id) from r_data_main where resc_hier like '%s;%s' or resc_hier like '%s;%s;%s' or resc_hier like '%s;%s';";
11011 sprintf(
11012 query,
11013 base_query.c_str(),
11014 _resc_name.c_str(), "%",
11015 "%", _resc_name.c_str(), "%",
11016 "%", _resc_name.c_str() );
11017
11018
11019
11020 int statement_num = 0;
11021 int status = cmlGetFirstRowFromSql(
11022 query,
11023 &statement_num,
11024 0, &icss );
11025 if( status != 0 ) {
11026 return status;
11027 }
11028
11029 _count = atol( icss.stmtPtr[ statement_num ]->resultValue[0] );
11030
11031 return 0;
11032
11033 }
11034
11035
11036
11037
11038
11039 int chlGetDistinctDataObjsMissingFromChildGivenParent(
11040 const std::string& _parent,
11041 const std::string& _child,
11042 int _limit,
11043 dist_child_result_t& _results ) {
11044
11045
11046 char query[ MAX_NAME_LEN ];
11047 std::string base_query = "select distinct data_id from r_data_main where resc_hier like '%s;%s' or resc_hier like '%s;%s;%s' or resc_hier like '%s;%s' except ( select distinct data_id from r_data_main where resc_hier like '%s;%s' or resc_hier like '%s;%s;%s' or resc_hier like '%s;%s' ) limit %d;";
11048 sprintf(
11049 query,
11050 base_query.c_str(),
11051 _parent.c_str(), "%",
11052 "%", _parent.c_str(), "%",
11053 "%", _parent.c_str(),
11054 _child.c_str(), "%",
11055 "%", _child.c_str(), "%",
11056 "%", _child.c_str(),
11057 _limit );
11058
11059
11060
11061 int statement_num = 0;
11062
11063
11064
11065 for( int i = 0; ; i++ ) {
11066
11067
11068 int status = 0;
11069 if( 0 == i ) {
11070 status = cmlGetFirstRowFromSql(
11071 query,
11072 &statement_num,
11073 0, &icss );
11074 } else {
11075 status = cmlGetNextRowFromStatement( statement_num, &icss );
11076 }
11077
11078 if ( status != 0 ) {
11079 return status;
11080 }
11081
11082 _results.push_back( atoi( icss.stmtPtr[ statement_num ]->resultValue[0] ) );
11083
11084 }
11085
11086 cmlFreeStatement( statement_num, &icss );
11087
11088 return 0;
11089
11090 }
11091
11092
11093
11094
11095 int chlGetHierarchyForResc(const std::string& resc_name, const std::string& zone_name, std::string& hierarchy) {
11096 char *current_node;
11097 char parent[MAX_NAME_LEN];
11098 int status;
11099
11100
11101 eirods::sql_logger logger("chlGetHierarchyForResc", logSQL);
11102 logger.log();
11103
11104 if (!icss.status) {
11105 return(CATALOG_NOT_CONNECTED);
11106 }
11107
11108 hierarchy = resc_name;
11109
11110 current_node = (char *)resc_name.c_str();
11111 while (current_node) {
11112
11113 status = cmlGetStringValueFromSql("select resc_parent from R_RESC_MAIN where resc_name=? and zone_name=?",
11114 parent, MAX_NAME_LEN, current_node, zone_name.c_str(), NULL, &icss);
11115
11116 if (status == CAT_NO_ROWS_FOUND) {
11117 return CAT_UNKNOWN_RESOURCE;
11118 }
11119
11120 if (status < 0) {
11121 return status;
11122 }
11123
11124 if (strlen(parent)) {
11125 hierarchy = parent + eirods::hierarchy_parser::delimiter() + hierarchy;
11126 current_node = parent;
11127 }
11128 else {
11129 current_node = NULL;
11130 }
11131 }
11132
11133 return 0;
11134 }
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151