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
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
00067
00068
00069
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
00098
00099
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 }
00195 }
00196
00197