OW_UserUtils.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00035 #include "OW_config.h"
00036 #include "OW_Mutex.hpp"
00037 #include "OW_MutexLock.hpp"
00038 #include "OW_UserUtils.hpp"
00039 
00040 #ifdef OW_HAVE_UNISTD_H
00041 #include <unistd.h>
00042 #endif
00043 
00044 #ifdef OW_HAVE_SYS_TYPES_H
00045 #include <sys/types.h>
00046 #endif
00047 
00048 #ifdef OW_HAVE_PWD_H
00049 #include <pwd.h>
00050 #endif
00051 
00052 #include <cerrno>
00053 #include <vector>
00054 
00055 namespace OW_NAMESPACE
00056 {
00057 
00058 namespace UserUtils
00059 {
00060 
00062 String getEffectiveUserId()
00063 {
00064 #ifdef OW_WIN32
00065 #pragma message(Reminder "TODO: Implement getEffectiveUserID using SID method!")
00066    // TODO
00067    // The user ID is represented by a SID on Win32. Going to return 0 for
00068    // root user until I get through the Win32 CIMOM. Eventually OW will
00069    // deal with userid on Win32 the proper way.
00070    return String("0");
00071 #else
00072    return String(Int64(::geteuid()));
00073 #endif
00074 }
00075 
00077 String getCurrentUserName()
00078 {
00079    bool ok;
00080 #ifdef OW_WIN32
00081    return getUserName(0, ok);
00082 #else
00083    return getUserName(getuid(),ok);
00084 #endif
00085 }
00086 
00087 namespace
00088 {
00089 Mutex g_getpwMutex;
00090 }
00091 
00093 String getUserName(uid_t uid,bool& ok)
00094 {
00095 #ifdef OW_WIN32
00096 #pragma message(Reminder "TODO: HONOR uid parm in getUserName!")
00097    // TODO
00098    // Ignore uid for right now. Just return the current User (WRONG!)
00099    // Need to come back to this later when the uid_t stuff is worked out.
00100    char name[256];
00101    unsigned long size = sizeof(name);
00102    size = sizeof(name);
00103    if (!::GetUserName(name, &size))
00104    {
00105       return String();
00106    }
00107 
00108    return String(name);
00109 #else
00110 
00111 #ifdef OW_HAVE_GETPWUID_R
00112    passwd pw;
00113    size_t const additionalSize =
00114 #ifdef _SC_GETPW_R_SIZE_MAX
00115       sysconf (_SC_GETPW_R_SIZE_MAX);
00116 #else
00117       10240;
00118 #endif
00119    std::vector<char> additional(additionalSize);
00120    passwd* result;
00121    int rv = 0;
00122    do
00123    {
00124       rv = ::getpwuid_r(uid, &pw, &additional[0], additional.size(), &result);
00125       if (rv == ERANGE)
00126       {
00127          additional.resize(additional.size() * 2);
00128       }
00129    } while (rv == ERANGE);
00130 #else
00131    MutexLock lock(g_getpwMutex);
00132    passwd* result = ::getpwuid(uid);
00133 #endif  
00134    if (result)
00135    {
00136       ok = true;
00137       return result->pw_name;
00138    }
00139    ok = false;
00140    return "";
00141 #endif
00142 }
00143 
00145 UserID
00146 getUserId(const String& userName, bool& validUserName)
00147 {
00148    validUserName = false;
00149 
00150 #ifdef OW_WIN32
00151 #pragma message(Reminder "TODO: Write getUserId!")
00152    return 0;
00153 #else
00154 
00155 
00156 #ifdef OW_HAVE_GETPWNAM_R
00157    size_t bufsize =
00158 #ifdef _SC_GETPW_R_SIZE_MAX
00159       sysconf (_SC_GETPW_R_SIZE_MAX);
00160 #else
00161       1024;
00162 #endif
00163    std::vector<char> buf(bufsize);
00164    struct passwd pwd;
00165    passwd* result = 0;
00166    int rv = 0;
00167    do
00168    {
00169       rv = ::getpwnam_r(userName.c_str(), &pwd, &buf[0], bufsize, &result);
00170       if (rv == ERANGE)
00171       {
00172          buf.resize(buf.size() * 2);
00173       }
00174    } while (rv == ERANGE);
00175 
00176    if (rv != 0)
00177    {
00178       return INVALID_USERID;
00179    }
00180 
00181 #else
00182    MutexLock ml(g_getpwMutex);
00183    struct passwd* result;
00184    result = ::getpwnam(userName.c_str());
00185 #endif
00186    if (result)
00187    {
00188       validUserName = true;
00189       return result->pw_uid;
00190    }
00191    return INVALID_USERID;
00192 #endif
00193 }
00194 } // end namespace UserUtils
00195 } // end namespace OW_NAMESPACE
00196 
00197 

Generated on Thu Feb 9 08:48:17 2006 for openwbem by  doxygen 1.4.6