00001
00002
00003
00004
00005
00006
00007
00008 #include "chkNVPathPerm.h"
00009 #include "fileStat.h"
00010 #include "miscServerFunct.h"
00011 #include "dataObjOpr.h"
00012
00013
00014
00015 #include "eirods_log.h"
00016 #include "eirods_file_object.h"
00017 #include "eirods_stacktrace.h"
00018 #include "eirods_resource_backport.h"
00019
00020 int
00021 rsChkNVPathPerm (rsComm_t *rsComm, fileOpenInp_t *chkNVPathPermInp)
00022 {
00023 rodsServerHost_t *rodsServerHost;
00024 int remoteFlag;
00025 int status;
00026
00027
00028 eirods::error ret = eirods::get_host_for_hier_string( chkNVPathPermInp->resc_hier_, remoteFlag, rodsServerHost );
00029 if( !ret.ok() ) {
00030 eirods::log( PASSMSG( "rsChkNVPathPerm - failed in call to eirods::get_host_for_hier_string", ret ) );
00031 return -1;
00032 }
00033
00034 if (remoteFlag < 0) {
00035 return (remoteFlag);
00036 } else {
00037 status = rsChkNVPathPermByHost (rsComm, chkNVPathPermInp,
00038 rodsServerHost);
00039 return (status);
00040 }
00041 }
00042
00043 int
00044 rsChkNVPathPermByHost (rsComm_t *rsComm, fileOpenInp_t *chkNVPathPermInp,
00045 rodsServerHost_t *rodsServerHost)
00046 {
00047 int remoteFlag;
00048 int status;
00049
00050 if (rodsServerHost == NULL) {
00051 rodsLog (LOG_NOTICE,
00052 "rsChkNVPathPermByHost: Input NULL rodsServerHost");
00053 return (SYS_INTERNAL_NULL_INPUT_ERR);
00054 }
00055
00056 remoteFlag = rodsServerHost->localFlag;
00057
00058 if (remoteFlag == LOCAL_HOST) {
00059 status = _rsChkNVPathPerm (rsComm, chkNVPathPermInp);
00060 } else if (remoteFlag == REMOTE_HOST) {
00061 status = remoteChkNVPathPerm (rsComm, chkNVPathPermInp, rodsServerHost);
00062 } else {
00063 if (remoteFlag < 0) {
00064 return (remoteFlag);
00065 } else {
00066 rodsLog (LOG_NOTICE,
00067 "rsChkNVPathPerm: resolveHost returned unrecognized value %d",
00068 remoteFlag);
00069 return (SYS_UNRECOGNIZED_REMOTE_FLAG);
00070 }
00071 }
00072
00073 return (status);
00074 }
00075
00076 int
00077 remoteChkNVPathPerm (rsComm_t *rsComm, fileOpenInp_t *chkNVPathPermInp,
00078 rodsServerHost_t *rodsServerHost)
00079 {
00080 int status;
00081
00082 if (rodsServerHost == NULL) {
00083 rodsLog (LOG_NOTICE,
00084 "remoteChkNVPathPerm: Invalid rodsServerHost");
00085 return SYS_INVALID_SERVER_HOST;
00086 }
00087
00088 if ((status = svrToSvrConnect (rsComm, rodsServerHost)) < 0) {
00089 return status;
00090 }
00091
00092
00093 status = rcChkNVPathPerm (rodsServerHost->conn, chkNVPathPermInp);
00094
00095 if (status < 0) {
00096 rodsLog (LOG_NOTICE,
00097 "remoteChkNVPathPerm: rcChkNVPathPerm failed for %s",
00098 chkNVPathPermInp->fileName);
00099 }
00100
00101 return status;
00102 }
00103
00104 int
00105 _rsChkNVPathPerm (rsComm_t *rsComm, fileOpenInp_t *chkNVPathPermInp)
00106 {
00107 struct stat myFileStat;
00108 int sysUid;
00109 #if 0
00110 char userName[NAME_LEN]
00111 #endif
00112 char tmpPath[MAX_NAME_LEN];
00113 int len;
00114 char *tmpPtr;
00115
00116 if(chkNVPathPermInp->objPath[0] == '\0') {
00117 std::stringstream msg;
00118 msg << __FUNCTION__;
00119 msg << " - Empty logical path.";
00120 eirods::log(LOG_ERROR, msg.str());
00121 return -1;
00122 }
00123
00124
00125 sysUid = rsComm->clientUser.sysUid;
00126 if (sysUid < 0) {
00127
00128 return (SYS_NO_PATH_PERMISSION);
00129 } else if (sysUid == 0) {
00130 #if 0
00131 if (strstr (rsComm->clientUser.userName, "@") != NULL) {
00132 splitPathByKey (rsComm->clientUser.userName, userName, tmpPath, '@');
00133 } else {
00134 rstrcpy (userName, rsComm->clientUser.userName, NAME_LEN);
00135 }
00136 #endif
00137 sysUid = rsComm->clientUser.sysUid =
00138 getUnixUid (rsComm->clientUser.userName);
00139
00140 if (sysUid < 0) {
00141 rsComm->clientUser.sysUid = sysUid;
00142 return (SYS_NO_PATH_PERMISSION);
00143 }
00144 }
00145
00146
00147 rstrcpy (tmpPath, chkNVPathPermInp->fileName, MAX_NAME_LEN);
00148
00149 len = strlen (tmpPath);
00150 eirods::error stat_err;
00151 while (1) {
00152
00153 eirods::file_object_ptr file_obj(
00154 new eirods::file_object(
00155 rsComm,
00156 chkNVPathPermInp->objPath,
00157 tmpPath,
00158 chkNVPathPermInp->resc_hier_,
00159 0, 0, 0 ) );
00160 stat_err = fileStat( rsComm, file_obj, &myFileStat );
00161 if ( stat_err.code() >= 0) {
00162 break;
00163 } else if ( errno == EEXIST || getErrno ( stat_err.code() ) == EEXIST) {
00164
00165
00166 tmpPtr = tmpPath + len;
00167
00168 while (len > 0) {
00169 len --;
00170 if (*tmpPtr == '/') {
00171 *tmpPtr = '\0';
00172 break;
00173 }
00174 tmpPtr--;
00175 }
00176
00177 if (len > 0) {
00178
00179 continue;
00180 } else {
00181 break;
00182 }
00183 } else {
00184 break;
00185 }
00186 }
00187
00188 if ( stat_err.code() < 0) {
00189 return (SYS_NO_PATH_PERMISSION);
00190 }
00191
00192 if( sysUid != (int) myFileStat.st_uid &&
00193 (myFileStat.st_mode & S_IWOTH) == 0) {
00194 return (SYS_NO_PATH_PERMISSION);
00195 } else {
00196 return (0);
00197 }
00198 }