00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00069
00070
00071
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
00100
00101
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 }
00197 }
00198
00199