(file) Return to SystemUnix.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/SystemUnix.cpp between version 1.30 and 1.127

version 1.30, 2002/03/21 18:49:58 version 1.127, 2013/04/26 08:57:36
Line 1 
Line 1 
 //%/////////////////////////////////////////////////////////////////////////////  //%LICENSE////////////////////////////////////////////////////////////////
 //  
 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM  
 //  
 // Permission is hereby granted, free of charge, to any person obtaining a copy  
 // of this software and associated documentation files (the "Software"), to  
 // deal in the Software without restriction, including without limitation the  
 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  
 // sell copies of the Software, and to permit persons to whom the Software is  
 // furnished to do so, subject to the following conditions:  
 //  
 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN  
 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED  
 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT  
 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR  
 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT  
 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN  
 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
 //  
 //==============================================================================  
 //  
 // Author: Mike Brasher (mbrasher@bmc.com)  
 // //
 // Modified By: Ben Heilbronn (ben_heilbronn@hp.com)  // Licensed to The Open Group (TOG) under one or more contributor license
   // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
   // this work for additional information regarding copyright ownership.
   // Each contributor licenses this file to you under the OpenPegasus Open
   // Source License; you may not use this file except in compliance with the
   // License.
   //
   // Permission is hereby granted, free of charge, to any person obtaining a
   // copy of this software and associated documentation files (the "Software"),
   // to deal in the Software without restriction, including without limitation
   // the rights to use, copy, modify, merge, publish, distribute, sublicense,
   // and/or sell copies of the Software, and to permit persons to whom the
   // Software is furnished to do so, subject to the following conditions:
   //
   // The above copyright notice and this permission notice shall be included
   // in all copies or substantial portions of the Software.
   //
   // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 // //
 //              Sushma Fernandes (sushma_fernandes@hp.com)  //////////////////////////////////////////////////////////////////////////
 //  
 //              Nag Boranna (nagaraja_boranna@hp.com)  
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #ifdef PEGASUS_OS_HPUX  #if !defined(PEGASUS_OS_ZOS) && \
 # include <dl.h>      !defined(PEGASUS_OS_DARWIN)
 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)  
 # include <dll.h>  
 #else  
 # include <dlfcn.h>  
 #endif  
   
 #include <unistd.h>  
 #include <dirent.h>  
 #include <pwd.h>  
   
 #ifndef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM  
 #include <crypt.h> #include <crypt.h>
 #else  
 #include <unistd.h>  
 #endif  
   
 #include "System.h"  
 #include <sys/stat.h>  
 #include <sys/types.h>  
 #include <cstdio>  
 #include <time.h>  
 #include <netdb.h>  
 #include <Pegasus/Common/Tracer.h>  
 #include <Pegasus/Common/Destroyer.h>  
 #include <Pegasus/Common/Exception.h>  
   
 #ifdef PEGASUS_PLATFORM_LINUX_IX86_GNU  
 #include <pwd.h>  
 #endif #endif
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 #include <sys/time.h>  
 #include <unistd.h>  
   
 inline void sleep_wrapper(Uint32 seconds)  
 {  
     sleep(seconds);  
 }  
   
 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)  
 {  
     timeval tv;  
     gettimeofday(&tv, 0);  
     seconds = int(tv.tv_sec);  
     milliseconds = int(tv.tv_usec) / 1000;  
 }  
   
 String System::getCurrentASCIITime()  
 {  
     char    str[50];  
     time_t  rawTime;  
   
     time(&rawTime);  
     strftime(str, 40,"%T-%D", localtime(&rawTime));  
     String time = str;  
     return time;  
 }  
   
 void System::sleep(Uint32 seconds)  
 {  
     sleep_wrapper(seconds);  
 }  
   
 Boolean System::exists(const char* path)  
 {  
     return access(path, F_OK) == 0;  
 }  
   
 Boolean System::canRead(const char* path) Boolean System::canRead(const char* path)
 { {
     return access(path, R_OK) == 0;     return access(path, R_OK) == 0;
Line 111 
Line 46 
     return access(path, W_OK) == 0;     return access(path, W_OK) == 0;
 } }
  
 Boolean System::getCurrentDirectory(char* path, Uint32 size)  String System::getPassword(const char* prompt)
 { {
     return getcwd(path, size) != NULL;  #if  defined(PEGASUS_OS_PASE)
 }  
  
 Boolean System::isDirectory(const char* path)      char* umepass = umeGetPass();
       if(NULL == umepass)
 { {
     struct stat st;          return String::EMPTY;
   
     if (stat(path, &st) != 0)  
         return false;  
   
     return S_ISDIR(st.st_mode);  
 } }
       else
 Boolean System::changeDirectory(const char* path)  
 { {
     return chdir(path) == 0;          return String(umepass);
 } }
  
 Boolean System::makeDirectory(const char* path)  #else /* default */
 {  
     return mkdir(path, 0777) == 0;  
 }  
  
 Boolean System::getFileSize(const char* path, Uint32& size)      return String(getpass(prompt));
 {  
     struct stat st;  
  
     if (stat(path, &st) != 0)  #endif /* default */
         return false;  
   
     size = st.st_size;  
     return true;  
 }  
  
 Boolean System::removeDirectory(const char* path)  
 {  
     return rmdir(path) == 0;  
 } }
  
 Boolean System::removeFile(const char* path)  String System::encryptPassword(const char* password, const char* salt)
 {  
     return unlink(path) == 0;  
 }  
   
 Boolean System::renameFile(const char* oldPath, const char* newPath)  
 { {
     if (link(oldPath, newPath) != 0)      return String(crypt(password, salt));
         return false;  
   
     return unlink(oldPath) == 0;  
 } }
  
 DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)  Boolean System::isPrivilegedUser(const String& userName)
 { {
     const char METHOD_NAME[] = "System::loadDynamicLibrary()";  #if defined(PEGASUS_OS_PASE)
       CString user = userName.getCString();
       // this function only can be found in PASE environment
       return umeIsPrivilegedUser((const char *)user);
  
     PEG_FUNC_ENTER(TRC_OS_ABSTRACTION, METHOD_NAME);  
   
     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2,  
                   "Attempting to load library %s", fileName);  
   
 #if defined(PEGASUS_OS_HPUX)  
     void* handle = shl_load(fileName, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);  
   
     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2,  
                   "After loading lib %s, error code is %d", fileName, errno);  
   
     PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME);  
     return DynamicLibraryHandle(handle);  
 #elif defined(PEGASUS_OS_TRU64)  
     PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME);  
     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW));  
 #elif defined(PEGASUS_OS_ZOS)  
     PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME);  
     return DynamicLibraryHandle(dllload(fileName));  
 #else #else
     PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME);      struct passwd   pwd;
     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL));      struct passwd   *result;
 #endif      const unsigned int PWD_BUFF_SIZE = 1024;
       char            pwdBuffer[PWD_BUFF_SIZE];
 }  
  
 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)      if (getpwnam_r(
             userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE, &result) != 0)
 { {
     // ATTN: Should this method indicate success/failure?          PEG_TRACE((
 #ifdef PEGASUS_OS_LINUX              TRC_OS_ABSTRACTION,
     dlclose(libraryHandle);              Tracer::LEVEL1,
 #endif              "getpwnam_r failure : %s",
               strerror(errno)));
 #ifdef PEGASUS_OS_HPUX  
     // Note: shl_unload will unload the library even if it has been loaded  
     // multiple times.  No reference count is kept.  
     int ignored = shl_unload(shl_t(libraryHandle));  
 #endif  
 }  
   
 String System::dynamicLoadError() {  
     // ATTN: Is this safe in a multi-threaded process?  Should this string  
     // be returned from loadDynamicLibrary?  
 #ifdef PEGASUS_OS_HPUX  
     // ATTN: If shl_load() returns NULL, this value should be strerror(errno)  
     return String();  
 #elif defined(PEGASUS_OS_ZOS)  
     return String();  
 #else  
     String dlerr = dlerror();  
     return dlerr;  
 #endif  
 } }
  
       // Check if the requested entry was found. If not return false.
 DynamicSymbolHandle System::loadDynamicSymbol(      if ( result != NULL )
     DynamicLibraryHandle libraryHandle,  
     const char* symbolName)  
 { {
 #ifdef PEGASUS_OS_HPUX          // Check if the uid is 0.
     char* p = (char*)symbolName;          if ( pwd.pw_gid == 0 || pwd.pw_uid == 0 )
     void* proc = 0;  
   
     if (shl_findsym((shl_t*)&libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)  
         return DynamicSymbolHandle(proc);  
   
     p = strcpy(new char[strlen(symbolName) + 2], symbolName);  
     strcpy(p, "_");  
     strcat(p, symbolName);  
   
     if (shl_findsym((shl_t*)libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)  
     {     {
         delete [] p;              return true;
         return DynamicSymbolHandle(proc);  
     }     }
       }
     return 0;      return false;
   
 #elif defined(PEGASUS_OS_ZOS)  
     return DynamicSymbolHandle(dllqueryfn((dllhandle *)libraryHandle,  
                                (char*)symbolName));  
 #else  
   
     return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName));  
   
 #endif #endif
 } }
  
 String System::getHostName()  #if defined(PEGASUS_ENABLE_USERGROUP_AUTHORIZATION)
 {  
     static char hostname[64];  
  
     if (!*hostname)  
         gethostname(hostname, sizeof(hostname));  
   
     return hostname;  
 }  
  
 String System::getFullyQualifiedHostName ()  inline void* peg_realloc(void *oldPtr, size_t newSize)
 { {
 #ifdef PEGASUS_OS_HPUX      void *newPtr = NULL;
     char hostName [MAXHOSTNAMELEN];      if ( (newPtr = ::realloc( oldPtr, newSize) ) == NULL)
     struct hostent *he;  
     String fqName;  
   
     if (gethostname (hostName, MAXHOSTNAMELEN) != 0)  
     {     {
         return String::EMPTY;          free( oldPtr);
           throw PEGASUS_STD(bad_alloc)();
     }     }
       return newPtr;
     if (he = gethostbyname (hostName))  }
   static void doFreeIfNeeded( const Boolean freeNeeded, char* ptrToFree)
   {
       if (freeNeeded)
     {     {
        strcpy (hostName, he->h_name);          free(ptrToFree);
           ptrToFree = NULL;
     }     }
   
     fqName.assign (hostName);  
   
     return fqName;  
 #else  
     //  
     //  ATTN: Implement this method to return the fully qualified host name  
     //  
     return String::EMPTY;  
 #endif  
 } }
  
 String System::getSystemCreationClassName ()  Boolean System::isGroupMember(const char* userName, const char* groupName)
 { {
 #ifdef PEGASUS_OS_HPUX      struct group grp;
     return "CIM_ComputerSystem";      Boolean retVal = false;
 #else      const  Uint32 PWD_BUFF_SIZE = 1024;
     //      struct passwd pwd;
     //  ATTN: Implement this method to return the system creation class name      struct passwd* result;
     //      struct group* grpresult;
     return String::EMPTY;      char pwdBuffer[PWD_BUFF_SIZE];
 #endif      // Search Primary group information.
 }  
  
 Uint32 System::lookupPort(      // Find the entry that matches "userName"
     const char * serviceName,      Sint32 errCode = 0;
     Uint32 defaultPort)      if((errCode = getpwnam_r(userName, &pwd, pwdBuffer,
           PWD_BUFF_SIZE, &result)) != 0)
 { {
     Uint32 localPort;          String errorMsg = String("getpwnam_r failure : ") +
                               String(strerror(errCode));
           Logger::put(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
                                     errorMsg);
           throw InternalSystemError();
       }
  
     struct servent *serv;      Uint32 grpbuflen = 1024;
  
     //      //tell if we need to free due to dynamically allocated memory
     // Get wbem-local port from /etc/services      Boolean isDynamicMemory = false;
     //      char grpBuf[grpbuflen];
     if (  (serv = getservbyname(serviceName, TCP)) != NULL )      char* grpBuffer = grpBuf;
     {  
         localPort = serv->s_port;  
     }  
     else  
     {  
         localPort = defaultPort;  
     }  
  
     return localPort;  
 }  
  
 String System::getPassword(const char* prompt)      if ( result != NULL )
 { {
  
     String password;          // User found, check for group information.
           gid_t group_id;
           group_id = pwd.pw_gid;
  
     password = String(getpass( prompt ));  
  
     return password;          //getgrgid_r may failed with groups with large number of users in group
 }          //Hence use loop here if ERANGE occurs
           while((errCode = getgrgid_r(group_id, &grp, grpBuffer,
 String System::getCurrentLoginName()              grpbuflen, &grpresult) == ERANGE))
 { {
     String userName = String::EMPTY;  
     struct passwd*   pwd = NULL;  
  
     //              grpBuffer = (!isDynamicMemory) ? NULL: grpBuffer;
     //  get the currently logged in user's UID.              isDynamicMemory = true;
     //              grpbuflen *= 2;
     pwd = getpwuid(getuid());  
     if ( pwd == NULL )  
     {  
         //ATTN: Log a message  
         // "User might have been removed just after login"  
     }  
     else  
     {  
         //  
         //  get the user name  
         //  
         userName.assign(pwd->pw_name);  
     }  
  
     return(userName);              grpBuffer = (char*)peg_realloc( grpBuffer, grpbuflen);
 } }
  
 String System::encryptPassword(const char* password, const char* salt)          if (errCode != 0 )
 { {
     return ( String(crypt( password,salt)) );              String errorMsg = String("getgrgid_r failure : ") +
                   String(strerror(errCode));
 } }
  
 Boolean System::isSystemUser(char* userName)  
 {  
     //  
     //  get the password entry for the user  
     //  
     if  ( getpwnam(userName) == NULL )  
     {  
         return false;  
     }  
     return true;  
 }  
  
 Boolean System::isPrivilegedUser(const String userName)          // Compare the user's group name to groupName.
           if (strcmp(grp.gr_name, groupName) == 0)
 { {
     //              doFreeIfNeeded(isDynamicMemory, grpBuffer);
     // Check if username has been passed  
     //  
     if ( userName != String::EMPTY )  
     {  
         //  
         // Check if the given user is a privileged user  
         //  
         struct passwd   pwd;  
         struct passwd   *result;  
         char            pwdBuffer[1024];  
  
         ArrayDestroyer<char> userName_(userName.allocateCString());               // User is a member of the group.
         if (getpwnam_r(userName_.getPointer(), &pwd, pwdBuffer, 1024, &result) == 0)  
         {  
             if ( pwd.pw_uid == 0 )  
             {  
                 return true;                 return true;
             }             }
             return false;  
         }  
     }     }
     else  
     {      // Search supplemental groups.
         //      // Get a user group entry
         // Get the effective UID for the user  
         //      // The grpbuflen which has been calculated
         if ( geteuid() != 0 )      // succeed getgrgid_r is good enough
       // for getgrnam_r so no need to repeat
       // above step for getgrnam_r
   
       errCode = 0;
       if ((errCode = getgrnam_r((char *)groupName, &grp,
           grpBuffer, grpbuflen, &grpresult)) != 0)
         {         {
             return false;          doFreeIfNeeded(isDynamicMemory, grpBuffer);
         }          String errorMsg = String("getgrnam_r failure : ") +
         return true;              String(strerror(errCode));
     }          Logger::put(
 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM              Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, errorMsg);
     return false; // keep the compiler happy          throw InternalSystemError();
 #endif  
 } }
  
 String System::getPrivilegedUserName()      // Check if the requested group was found.
       if (grpresult == NULL)
 { {
     static String userName = String::EMPTY;          doFreeIfNeeded(isDynamicMemory, grpBuffer);
           return false;
       }
  
     if (userName == String::EMPTY)  
     {  
         struct passwd*   pwd = NULL;  
  
         //         //
         //  get the privileged user's UID.      // Get all the members of the group
         //         //
         pwd = getpwuid(0);      Uint32 j = 0;
         if ( pwd != NULL )      char *member = NULL;
       while ( (member = grp.gr_mem[j++]) )
         {         {
             //             //
             //  get the user name          // Check if the user is a member of the group
             //             //
             userName.assign(pwd->pw_name);          if ( strcmp(userName, member) == 0 )
         }  
         else  
         {         {
             PEGASUS_ASSERT(0);              retVal = true;
               break;
         }         }
     }     }
  
     return (userName);      doFreeIfNeeded( isDynamicMemory, grpBuffer);
       return retVal;
 } }
  
 Uint32 System::getPID()  #endif /* PEGASUS_ENABLE_USERGROUP_AUTHORIZATION */
 {  
     //  
     // Get the Process ID  
     //  
     Uint32 pid = getpid();  
   
     return pid;  
 }  
  
 Boolean System::truncateFile(  
     const char* path,  
     size_t newSize)  
 {  
     return (truncate(path, newSize) == 0);  
 }  
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.30  
changed lines
  Added in v.1.127

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2