UserUtils.cpp

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

Generated on Fri Jun 16 15:39:09 2006 for blocxx by  doxygen 1.4.6