userinfo.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_private.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_portable.h"
  19. #include "fspr_user.h"
  20. #include "fspr_arch_file_io.h"
  21. #if APR_HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #ifndef _WIN32_WCE
  25. /* Internal sid binary to string translation, see MSKB Q131320.
  26. * Several user related operations require our SID to access
  27. * the registry, but in a string format. All error handling
  28. * depends on IsValidSid(), which internally we better test long
  29. * before we get here!
  30. */
  31. void get_sid_string(char *buf, fspr_size_t blen, fspr_uid_t id)
  32. {
  33. PSID_IDENTIFIER_AUTHORITY psia;
  34. DWORD nsa;
  35. DWORD sa;
  36. int slen;
  37. /* Determine authority values (these is a big-endian value,
  38. * and NT records the value as hex if the value is > 2^32.)
  39. */
  40. psia = GetSidIdentifierAuthority(id);
  41. nsa = (DWORD)(psia->Value[5]) + ((DWORD)(psia->Value[4]) << 8)
  42. + ((DWORD)(psia->Value[3]) << 16) + ((DWORD)(psia->Value[2]) << 24);
  43. sa = (DWORD)(psia->Value[1]) + ((DWORD)(psia->Value[0]) << 8);
  44. if (sa) {
  45. slen = fspr_snprintf(buf, blen, "S-%lu-0x%04x%08x",
  46. SID_REVISION, sa, nsa);
  47. } else {
  48. slen = fspr_snprintf(buf, blen, "S-%lu-%lu",
  49. SID_REVISION, nsa);
  50. }
  51. /* Now append all the subauthority strings.
  52. */
  53. nsa = *GetSidSubAuthorityCount(id);
  54. for (sa = 0; sa < nsa; ++sa) {
  55. slen += fspr_snprintf(buf + slen, blen - slen, "-%lu",
  56. *GetSidSubAuthority(id, sa));
  57. }
  58. }
  59. #endif
  60. /* Query the ProfileImagePath from the version-specific branch, where the
  61. * regkey uses the user's name on 9x, and user's sid string on NT.
  62. */
  63. APR_DECLARE(fspr_status_t) fspr_uid_homepath_get(char **dirname,
  64. const char *username,
  65. fspr_pool_t *p)
  66. {
  67. #ifdef _WIN32_WCE
  68. *dirname = fspr_pstrdup(p, "/My Documents");
  69. return APR_SUCCESS;
  70. #else
  71. fspr_status_t rv;
  72. char regkey[MAX_PATH * 2];
  73. char *fixch;
  74. DWORD keylen;
  75. DWORD type;
  76. HKEY key;
  77. if (fspr_os_level >= APR_WIN_NT) {
  78. fspr_uid_t uid;
  79. fspr_gid_t gid;
  80. if ((rv = fspr_uid_get(&uid, &gid, username, p)) != APR_SUCCESS)
  81. return rv;
  82. strcpy(regkey, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"
  83. "ProfileList\\");
  84. keylen = (DWORD)strlen(regkey);
  85. get_sid_string(regkey + keylen, sizeof(regkey) - keylen, uid);
  86. }
  87. else {
  88. strcpy(regkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\"
  89. "ProfileList\\");
  90. keylen = (DWORD)strlen(regkey);
  91. fspr_cpystrn(regkey + keylen, username, sizeof(regkey) - keylen);
  92. }
  93. if ((rv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey, 0,
  94. KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS)
  95. return APR_FROM_OS_ERROR(rv);
  96. #if APR_HAS_UNICODE_FS
  97. IF_WIN_OS_IS_UNICODE
  98. {
  99. keylen = sizeof(regkey);
  100. rv = RegQueryValueExW(key, L"ProfileImagePath", NULL, &type,
  101. (void*)regkey, &keylen);
  102. RegCloseKey(key);
  103. if (rv != ERROR_SUCCESS)
  104. return APR_FROM_OS_ERROR(rv);
  105. if (type == REG_SZ) {
  106. char retdir[MAX_PATH];
  107. if ((rv = unicode_to_utf8_path(retdir, sizeof(retdir),
  108. (fspr_wchar_t*)regkey)) != APR_SUCCESS)
  109. return rv;
  110. *dirname = fspr_pstrdup(p, retdir);
  111. }
  112. else if (type == REG_EXPAND_SZ) {
  113. fspr_wchar_t path[MAX_PATH];
  114. char retdir[MAX_PATH];
  115. ExpandEnvironmentStringsW((fspr_wchar_t*)regkey, path, sizeof(path));
  116. if ((rv = unicode_to_utf8_path(retdir, sizeof(retdir), path))
  117. != APR_SUCCESS)
  118. return rv;
  119. *dirname = fspr_pstrdup(p, retdir);
  120. }
  121. else
  122. return APR_ENOENT;
  123. }
  124. #endif
  125. #if APR_HAS_ANSI_FS
  126. ELSE_WIN_OS_IS_ANSI
  127. {
  128. keylen = sizeof(regkey);
  129. rv = RegQueryValueEx(key, "ProfileImagePath", NULL, &type,
  130. (void*)regkey, &keylen);
  131. RegCloseKey(key);
  132. if (rv != ERROR_SUCCESS)
  133. return APR_FROM_OS_ERROR(rv);
  134. if (type == REG_SZ) {
  135. *dirname = fspr_pstrdup(p, regkey);
  136. }
  137. else if (type == REG_EXPAND_SZ) {
  138. char path[MAX_PATH];
  139. ExpandEnvironmentStrings(regkey, path, sizeof(path));
  140. *dirname = fspr_pstrdup(p, path);
  141. }
  142. else
  143. return APR_ENOENT;
  144. }
  145. #endif /* APR_HAS_ANSI_FS */
  146. for (fixch = *dirname; *fixch; ++fixch)
  147. if (*fixch == '\\')
  148. *fixch = '/';
  149. return APR_SUCCESS;
  150. #endif /* _WIN32_WCE */
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_uid_current(fspr_uid_t *uid,
  153. fspr_gid_t *gid,
  154. fspr_pool_t *p)
  155. {
  156. #ifdef _WIN32_WCE
  157. return APR_ENOTIMPL;
  158. #else
  159. HANDLE threadtok;
  160. DWORD needed;
  161. TOKEN_USER *usr;
  162. TOKEN_PRIMARY_GROUP *grp;
  163. if(!OpenProcessToken(GetCurrentProcess(), STANDARD_RIGHTS_READ | READ_CONTROL | TOKEN_QUERY, &threadtok)) {
  164. return fspr_get_os_error();
  165. }
  166. *uid = NULL;
  167. if (!GetTokenInformation(threadtok, TokenUser, NULL, 0, &needed)
  168. && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  169. && (usr = fspr_palloc(p, needed))
  170. && GetTokenInformation(threadtok, TokenUser, usr, needed, &needed))
  171. *uid = usr->User.Sid;
  172. else
  173. return fspr_get_os_error();
  174. if (!GetTokenInformation(threadtok, TokenPrimaryGroup, NULL, 0, &needed)
  175. && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  176. && (grp = fspr_palloc(p, needed))
  177. && GetTokenInformation(threadtok, TokenPrimaryGroup, grp, needed, &needed))
  178. *gid = grp->PrimaryGroup;
  179. else
  180. return fspr_get_os_error();
  181. return APR_SUCCESS;
  182. #endif
  183. }
  184. APR_DECLARE(fspr_status_t) fspr_uid_get(fspr_uid_t *uid, fspr_gid_t *gid,
  185. const char *username, fspr_pool_t *p)
  186. {
  187. #ifdef _WIN32_WCE
  188. return APR_ENOTIMPL;
  189. #else
  190. SID_NAME_USE sidtype;
  191. char anydomain[256];
  192. char *domain;
  193. DWORD sidlen = 0;
  194. DWORD domlen = sizeof(anydomain);
  195. DWORD rv;
  196. char *pos;
  197. if (pos = strchr(username, '/')) {
  198. domain = fspr_pstrndup(p, username, pos - username);
  199. username = pos + 1;
  200. }
  201. else if (pos = strchr(username, '\\')) {
  202. domain = fspr_pstrndup(p, username, pos - username);
  203. username = pos + 1;
  204. }
  205. else {
  206. domain = NULL;
  207. }
  208. /* Get nothing on the first pass ... need to size the sid buffer
  209. */
  210. rv = LookupAccountName(domain, username, domain, &sidlen,
  211. anydomain, &domlen, &sidtype);
  212. if (sidlen) {
  213. /* Give it back on the second pass
  214. */
  215. *uid = fspr_palloc(p, sidlen);
  216. domlen = sizeof(anydomain);
  217. rv = LookupAccountName(domain, username, *uid, &sidlen,
  218. anydomain, &domlen, &sidtype);
  219. }
  220. if (!sidlen || !rv) {
  221. return fspr_get_os_error();
  222. }
  223. /* There doesn't seem to be a simple way to retrieve the primary group sid
  224. */
  225. *gid = NULL;
  226. return APR_SUCCESS;
  227. #endif
  228. }
  229. APR_DECLARE(fspr_status_t) fspr_uid_name_get(char **username, fspr_uid_t userid,
  230. fspr_pool_t *p)
  231. {
  232. #ifdef _WIN32_WCE
  233. *username = fspr_pstrdup(p, "Administrator");
  234. return APR_SUCCESS;
  235. #else
  236. SID_NAME_USE type;
  237. char name[MAX_PATH], domain[MAX_PATH];
  238. DWORD cbname = sizeof(name), cbdomain = sizeof(domain);
  239. if (!userid)
  240. return APR_EINVAL;
  241. if (!LookupAccountSid(NULL, userid, name, &cbname, domain, &cbdomain, &type))
  242. return fspr_get_os_error();
  243. if (type != SidTypeUser && type != SidTypeAlias && type != SidTypeWellKnownGroup)
  244. return APR_EINVAL;
  245. *username = fspr_pstrdup(p, name);
  246. return APR_SUCCESS;
  247. #endif
  248. }
  249. APR_DECLARE(fspr_status_t) fspr_uid_compare(fspr_uid_t left, fspr_uid_t right)
  250. {
  251. if (!left || !right)
  252. return APR_EINVAL;
  253. #ifndef _WIN32_WCE
  254. if (!IsValidSid(left) || !IsValidSid(right))
  255. return APR_EINVAL;
  256. if (!EqualSid(left, right))
  257. return APR_EMISMATCH;
  258. #endif
  259. return APR_SUCCESS;
  260. }