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 00209 //---------------------------------------------------------------------------- 00211 //---------------------------------------------------------------------------- 00212 class ScopedDescriptor 00213 { 00214 public: 00215 //------------------------------------------------------------------------ 00217 //------------------------------------------------------------------------ 00218 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {} 00219 00220 //------------------------------------------------------------------------ 00222 //------------------------------------------------------------------------ 00223 ~ScopedDescriptor() { if( pDescriptor >= 0 ) close( pDescriptor ); } 00224 00225 //------------------------------------------------------------------------ 00227 //------------------------------------------------------------------------ 00228 int Release() 00229 { 00230 int desc = pDescriptor; 00231 pDescriptor = -1; 00232 return desc; 00233 } 00234 00235 //------------------------------------------------------------------------ 00237 //------------------------------------------------------------------------ 00238 int GetDescriptor() 00239 { 00240 return pDescriptor; 00241 } 00242 00243 private: 00244 int pDescriptor; 00245 }; 00246 00247 #ifdef __linux__ 00248 //---------------------------------------------------------------------------- 00250 //---------------------------------------------------------------------------- 00251 class ScopedFsUidSetter 00252 { 00253 public: 00254 //------------------------------------------------------------------------ 00256 //------------------------------------------------------------------------ 00257 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName) 00258 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName) 00259 { 00260 pOk = true; 00261 pPrevFsUid = -1; 00262 pPrevFsGid = -1; 00263 00264 //---------------------------------------------------------------------- 00266 //---------------------------------------------------------------------- 00267 if(pFsUid >= 0) { 00268 pPrevFsUid = setfsuid(pFsUid); 00269 00270 if(setfsuid(pFsUid) != pFsUid) { 00271 pOk = false; 00272 return; 00273 } 00274 } 00275 00276 //---------------------------------------------------------------------- 00278 //---------------------------------------------------------------------- 00279 if(pFsGid >= 0) { 00280 pPrevFsGid = setfsgid(pFsGid); 00281 00282 if(setfsgid(pFsGid) != pFsGid) { 00283 pOk = false; 00284 return; 00285 } 00286 } 00287 } 00288 00289 //------------------------------------------------------------------------ 00291 //------------------------------------------------------------------------ 00292 ~ScopedFsUidSetter() { 00293 Log *log = DefaultEnv::GetLog(); 00294 00295 if(pPrevFsUid >= 0) { 00296 int retcode = setfsuid(pPrevFsUid); 00297 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid); 00298 } 00299 00300 if(pPrevFsGid >= 0) { 00301 int retcode = setfsgid(pPrevFsGid); 00302 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid); 00303 } 00304 } 00305 00306 bool IsOk() const { 00307 return pOk; 00308 } 00309 00310 private: 00311 int pFsUid; 00312 int pFsGid; 00313 00314 const std::string &pStreamName; 00315 00316 int pPrevFsUid; 00317 int pPrevFsGid; 00318 00319 bool pOk; 00320 }; 00321 #endif 00322 00323 } 00324 00325 #endif // __XRD_CL_UTILS_HH__