00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_UTILS_HH__ 00020 #define __XRD_CL_UTILS_HH__ 00021 00022 #include <string> 00023 #include <vector> 00024 #include "XrdCl/XrdClStatus.hh" 00025 #include "XrdCl/XrdClLog.hh" 00026 #include "XrdCl/XrdClURL.hh" 00027 #include "XrdCl/XrdClXRootDResponses.hh" 00028 #include "XrdCl/XrdClPropertyList.hh" 00029 #include "XrdCl/XrdClDefaultEnv.hh" 00030 #include "XrdCl/XrdClConstants.hh" 00031 #include "XrdNet/XrdNetUtils.hh" 00032 00033 #include <sys/time.h> 00034 00035 #ifdef __linux__ 00036 #include <sys/fsuid.h> 00037 #endif 00038 00039 namespace XrdCl 00040 { 00041 //---------------------------------------------------------------------------- 00043 //---------------------------------------------------------------------------- 00044 class Utils 00045 { 00046 public: 00047 //------------------------------------------------------------------------ 00049 //------------------------------------------------------------------------ 00050 template<class Container> 00051 static void splitString( Container &result, 00052 const std::string &input, 00053 const std::string &delimiter ) 00054 { 00055 size_t start = 0; 00056 size_t end = 0; 00057 size_t length = 0; 00058 00059 do 00060 { 00061 end = input.find( delimiter, start ); 00062 00063 if( end != std::string::npos ) 00064 length = end - start; 00065 else 00066 length = input.length() - start; 00067 00068 if( length ) 00069 result.push_back( input.substr( start, length ) ); 00070 00071 start = end + delimiter.size(); 00072 } 00073 while( end != std::string::npos ); 00074 } 00075 00076 //------------------------------------------------------------------------ 00078 //------------------------------------------------------------------------ 00079 static int GetIntParameter( const URL &url, 00080 const std::string &name, 00081 int defaultVal ); 00082 00083 //------------------------------------------------------------------------ 00085 //------------------------------------------------------------------------ 00086 static std::string GetStringParameter( const URL &url, 00087 const std::string &name, 00088 const std::string &defaultVal ); 00089 00090 //------------------------------------------------------------------------ 00092 //------------------------------------------------------------------------ 00093 enum AddressType 00094 { 00095 IPAuto = 0, 00096 IPAll = 1, 00097 IPv6 = 2, 00098 IPv4 = 3, 00099 IPv4Mapped6 = 4 00100 }; 00101 00102 //------------------------------------------------------------------------ 00104 //------------------------------------------------------------------------ 00105 static AddressType String2AddressType( const std::string &addressType ); 00106 00107 //------------------------------------------------------------------------ 00109 //------------------------------------------------------------------------ 00110 static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses, 00111 const URL &url, 00112 AddressType type ); 00113 00114 //------------------------------------------------------------------------ 00116 //------------------------------------------------------------------------ 00117 static void LogHostAddresses( Log *log, 00118 uint64_t type, 00119 const std::string &hostId, 00120 std::vector<XrdNetAddr> &addresses ); 00121 00122 //------------------------------------------------------------------------ 00124 //------------------------------------------------------------------------ 00125 static std::string TimeToString( time_t timestamp ); 00126 00127 //------------------------------------------------------------------------ 00129 //------------------------------------------------------------------------ 00130 static uint64_t GetElapsedMicroSecs( timeval start, timeval end ); 00131 00132 //------------------------------------------------------------------------ 00134 //------------------------------------------------------------------------ 00135 static XRootDStatus GetRemoteCheckSum( std::string &checkSum, 00136 const std::string &checkSumType, 00137 const std::string &server, 00138 const std::string &path ); 00139 00140 //------------------------------------------------------------------------ 00142 //------------------------------------------------------------------------ 00143 static XRootDStatus GetLocalCheckSum( std::string &checkSum, 00144 const std::string &checkSumType, 00145 const std::string &path ); 00146 00147 //------------------------------------------------------------------------ 00149 //------------------------------------------------------------------------ 00150 static std::string BytesToString( uint64_t bytes ); 00151 00152 //------------------------------------------------------------------------ 00154 //------------------------------------------------------------------------ 00155 static XRootDStatus CheckTPC( const std::string &server, 00156 uint16_t timeout = 0 ); 00157 00158 //------------------------------------------------------------------------ 00163 //------------------------------------------------------------------------ 00164 static XRootDStatus CheckTPCLite( const std::string &server, 00165 uint16_t timeout = 0 ); 00166 00167 //------------------------------------------------------------------------ 00169 //------------------------------------------------------------------------ 00170 static std::string FQDNToCC( const std::string &fqdn ); 00171 00172 //------------------------------------------------------------------------ 00174 //------------------------------------------------------------------------ 00175 static Status GetDirectoryEntries( std::vector<std::string> &entries, 00176 const std::string &path ); 00177 00178 //------------------------------------------------------------------------ 00180 //------------------------------------------------------------------------ 00181 static Status ProcessConfig( std::map<std::string, std::string> &config, 00182 const std::string &file ); 00183 00184 //------------------------------------------------------------------------ 00186 //------------------------------------------------------------------------ 00187 static void Trim( std::string &str ); 00188 00189 //------------------------------------------------------------------------ 00191 //------------------------------------------------------------------------ 00192 static void LogPropertyList( Log *log, 00193 uint64_t topic, 00194 const char *format, 00195 const PropertyList &list ); 00196 00197 //------------------------------------------------------------------------ 00199 //------------------------------------------------------------------------ 00200 static std::string Char2Hex( uint8_t *array, uint16_t size ); 00201 00202 //------------------------------------------------------------------------ 00204 //------------------------------------------------------------------------ 00205 static std::string NormalizeChecksum( const std::string &name, 00206 const std::string &checksum ); 00207 00208 //------------------------------------------------------------------------ 00210 //------------------------------------------------------------------------ 00211 static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url ); 00212 00213 //------------------------------------------------------------------------ 00221 //------------------------------------------------------------------------ 00222 static std::string InferChecksumType( const XrdCl::URL &source, 00223 const XrdCl::URL &destination, 00224 bool zip = false ); 00225 }; 00226 00227 //---------------------------------------------------------------------------- 00229 //---------------------------------------------------------------------------- 00230 class ScopedDescriptor 00231 { 00232 public: 00233 //------------------------------------------------------------------------ 00235 //------------------------------------------------------------------------ 00236 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {} 00237 00238 //------------------------------------------------------------------------ 00240 //------------------------------------------------------------------------ 00241 ~ScopedDescriptor() { if( pDescriptor >= 0 ) close( pDescriptor ); } 00242 00243 //------------------------------------------------------------------------ 00245 //------------------------------------------------------------------------ 00246 int Release() 00247 { 00248 int desc = pDescriptor; 00249 pDescriptor = -1; 00250 return desc; 00251 } 00252 00253 //------------------------------------------------------------------------ 00255 //------------------------------------------------------------------------ 00256 int GetDescriptor() 00257 { 00258 return pDescriptor; 00259 } 00260 00261 private: 00262 int pDescriptor; 00263 }; 00264 00265 #ifdef __linux__ 00266 //---------------------------------------------------------------------------- 00268 //---------------------------------------------------------------------------- 00269 class ScopedFsUidSetter 00270 { 00271 public: 00272 //------------------------------------------------------------------------ 00274 //------------------------------------------------------------------------ 00275 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName) 00276 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName) 00277 { 00278 pOk = true; 00279 pPrevFsUid = -1; 00280 pPrevFsGid = -1; 00281 00282 //---------------------------------------------------------------------- 00284 //---------------------------------------------------------------------- 00285 if(pFsUid >= 0) { 00286 pPrevFsUid = setfsuid(pFsUid); 00287 00288 if(setfsuid(pFsUid) != pFsUid) { 00289 pOk = false; 00290 return; 00291 } 00292 } 00293 00294 //---------------------------------------------------------------------- 00296 //---------------------------------------------------------------------- 00297 if(pFsGid >= 0) { 00298 pPrevFsGid = setfsgid(pFsGid); 00299 00300 if(setfsgid(pFsGid) != pFsGid) { 00301 pOk = false; 00302 return; 00303 } 00304 } 00305 } 00306 00307 //------------------------------------------------------------------------ 00309 //------------------------------------------------------------------------ 00310 ~ScopedFsUidSetter() { 00311 Log *log = DefaultEnv::GetLog(); 00312 00313 if(pPrevFsUid >= 0) { 00314 int retcode = setfsuid(pPrevFsUid); 00315 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid); 00316 } 00317 00318 if(pPrevFsGid >= 0) { 00319 int retcode = setfsgid(pPrevFsGid); 00320 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid); 00321 } 00322 } 00323 00324 bool IsOk() const { 00325 return pOk; 00326 } 00327 00328 private: 00329 int pFsUid; 00330 int pFsGid; 00331 00332 const std::string &pStreamName; 00333 00334 int pPrevFsUid; 00335 int pPrevFsGid; 00336 00337 bool pOk; 00338 }; 00339 #endif 00340 00341 } 00342 00343 #endif // __XRD_CL_UTILS_HH__