Apache HTTPD
userinfo.c
Go to the documentation of this file.
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
17#include "apr_private.h"
18#include "apr_strings.h"
19#include "apr_portable.h"
20#include "apr_user.h"
21#include "apr_arch_file_io.h"
22#if APR_HAVE_SYS_TYPES_H
23#include <sys/types.h>
24#endif
25
26#ifndef _WIN32_WCE
27/* Internal sid binary to string translation, see MSKB Q131320.
28 * Several user related operations require our SID to access
29 * the registry, but in a string format. All error handling
30 * depends on IsValidSid(), which internally we better test long
31 * before we get here!
32 */
33static void get_sid_string(char *buf, apr_size_t blen, apr_uid_t id)
34{
36 DWORD nsa;
37 DWORD sa;
38 int slen;
39
40 /* Determine authority values (these is a big-endian value,
41 * and NT records the value as hex if the value is > 2^32.)
42 */
44 nsa = (DWORD)(psia->Value[5]) + ((DWORD)(psia->Value[4]) << 8)
45 + ((DWORD)(psia->Value[3]) << 16) + ((DWORD)(psia->Value[2]) << 24);
46 sa = (DWORD)(psia->Value[1]) + ((DWORD)(psia->Value[0]) << 8);
47 if (sa) {
48 slen = apr_snprintf(buf, blen, "S-%d-0x%04x%08x",
49 SID_REVISION, (unsigned int)sa, (unsigned int)nsa);
50 } else {
51 slen = apr_snprintf(buf, blen, "S-%d-%lu",
53 }
54
55 /* Now append all the subauthority strings.
56 */
58 for (sa = 0; sa < nsa; ++sa) {
59 slen += apr_snprintf(buf + slen, blen - slen, "-%lu",
61 }
62}
63#endif
64/* Query the ProfileImagePath from the version-specific branch, where the
65 * regkey uses the user's name on 9x, and user's sid string on NT.
66 */
68 const char *username,
70{
71#ifdef _WIN32_WCE
72 *dirname = apr_pstrdup(p, "/My Documents");
73 return APR_SUCCESS;
74#else
75 apr_status_t rv;
76 char regkey[MAX_PATH * 2];
77 char *fixch;
79 DWORD type;
80 HKEY key;
81
82 if (apr_os_level >= APR_WIN_NT) {
85
86 if ((rv = apr_uid_get(&uid, &gid, username, p)) != APR_SUCCESS)
87 return rv;
88
89 strcpy(regkey, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"
90 "ProfileList\\");
91 keylen = (DWORD)strlen(regkey);
93 }
94 else {
95 strcpy(regkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\"
96 "ProfileList\\");
97 keylen = (DWORD)strlen(regkey);
99 }
100
103 return APR_FROM_OS_ERROR(rv);
104
105#if APR_HAS_UNICODE_FS
107 {
108 keylen = sizeof(regkey);
109 rv = RegQueryValueExW(key, L"ProfileImagePath", NULL, &type,
110 (void*)regkey, &keylen);
112 if (rv != ERROR_SUCCESS)
113 return APR_FROM_OS_ERROR(rv);
114 if (type == REG_SZ) {
115 char retdir[MAX_PATH];
116 if ((rv = unicode_to_utf8_path(retdir, sizeof(retdir),
118 return rv;
120 }
121 else if (type == REG_EXPAND_SZ) {
123 char retdir[MAX_PATH];
125 sizeof(path) / 2);
126 if ((rv = unicode_to_utf8_path(retdir, sizeof(retdir), path))
127 != APR_SUCCESS)
128 return rv;
130 }
131 else
132 return APR_ENOENT;
133 }
134#endif
135#if APR_HAS_ANSI_FS
137 {
138 keylen = sizeof(regkey);
139 rv = RegQueryValueEx(key, "ProfileImagePath", NULL, &type,
140 (void*)regkey, &keylen);
142 if (rv != ERROR_SUCCESS)
143 return APR_FROM_OS_ERROR(rv);
144 if (type == REG_SZ) {
146 }
147 else if (type == REG_EXPAND_SZ) {
148 char path[MAX_PATH];
151 }
152 else
153 return APR_ENOENT;
154 }
155#endif /* APR_HAS_ANSI_FS */
156 for (fixch = *dirname; *fixch; ++fixch)
157 if (*fixch == '\\')
158 *fixch = '/';
159 return APR_SUCCESS;
160#endif /* _WIN32_WCE */
161}
162
164 apr_gid_t *gid,
165 apr_pool_t *p)
166{
167#ifdef _WIN32_WCE
168 return APR_ENOTIMPL;
169#else
174 apr_status_t rv;
175
177 return apr_get_os_error();
178 }
179
180 *uid = NULL;
183 && (usr = apr_palloc(p, needed))
185 *uid = usr->User.Sid;
186 }
187 else {
188 rv = apr_get_os_error();
190 return rv;
191 }
192
195 && (grp = apr_palloc(p, needed))
197 *gid = grp->PrimaryGroup;
198 }
199 else {
200 rv = apr_get_os_error();
202 return rv;
203 }
204
206
207 return APR_SUCCESS;
208#endif
209}
210
212 const char *username, apr_pool_t *p)
213{
214#ifdef _WIN32_WCE
215 return APR_ENOTIMPL;
216#else
218 char anydomain[256];
219 char *domain;
220 DWORD sidlen = 0;
221 DWORD domlen = sizeof(anydomain);
222 DWORD rv;
223 char *pos;
224
225 if ((pos = strchr(username, '/'))) {
226 domain = apr_pstrmemdup(p, username, pos - username);
227 username = pos + 1;
228 }
229 else if ((pos = strchr(username, '\\'))) {
230 domain = apr_pstrmemdup(p, username, pos - username);
231 username = pos + 1;
232 }
233 else {
234 domain = NULL;
235 }
236 /* Get nothing on the first pass ... need to size the sid buffer
237 */
238 rv = LookupAccountName(domain, username, domain, &sidlen,
240 if (sidlen) {
241 /* Give it back on the second pass
242 */
243 *uid = apr_palloc(p, sidlen);
244 domlen = sizeof(anydomain);
245 rv = LookupAccountName(domain, username, *uid, &sidlen,
247 }
248 if (!sidlen || !rv) {
249 return apr_get_os_error();
250 }
251 /* There doesn't seem to be a simple way to retrieve the primary group sid
252 */
253 *gid = NULL;
254 return APR_SUCCESS;
255#endif
256}
257
259 apr_pool_t *p)
260{
261#ifdef _WIN32_WCE
262 *username = apr_pstrdup(p, "Administrator");
263 return APR_SUCCESS;
264#else
266 char name[MAX_PATH], domain[MAX_PATH];
267 DWORD cbname = sizeof(name), cbdomain = sizeof(domain);
268 if (!userid)
269 return APR_EINVAL;
270 if (!LookupAccountSid(NULL, userid, name, &cbname, domain, &cbdomain, &type))
271 return apr_get_os_error();
273 return APR_EINVAL;
275 return APR_SUCCESS;
276#endif
277}
278
280{
281 if (!left || !right)
282 return APR_EINVAL;
283#ifndef _WIN32_WCE
284 if (!IsValidSid(left) || !IsValidSid(right))
285 return APR_EINVAL;
286 if (!EqualSid(left, right))
287 return APR_EMISMATCH;
288#endif
289 return APR_SUCCESS;
290}
291
apr_uint16_t apr_wchar_t
#define GetCurrentProcess()
#define CloseHandle(h)
APR Portability Routines.
APR Strings library.
APR User ID Services.
request_rec int int apr_table_t const char * path
const unsigned char * buf
Definition util_md5.h:50
#define APR_EMISMATCH
Definition apr_errno.h:478
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_ENOENT
Definition apr_errno.h:662
#define APR_EINVAL
Definition apr_errno.h:711
apr_fileperms_t apr_uid_t uid
apr_fileperms_t apr_uid_t apr_gid_t gid
const char apr_ssize_t slen
Definition apr_encode.h:168
#define ERROR_INSUFFICIENT_BUFFER
Definition mod_isapi.h:225
#define ERROR_SUCCESS
Definition mod_isapi.h:240
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
const char * dirname
#define APR_FROM_OS_ERROR(e)
Definition apr_errno.h:1214
#define APR_SUCCESS
Definition apr_errno.h:225
#define apr_get_os_error()
Definition apr_errno.h:1217
int apr_status_t
Definition apr_errno.h:44
const char * key
int type
apr_sockaddr_t * sa
const char * username
gid_t apr_gid_t
Definition apr_user.h:54
uid_t apr_uid_t
Definition apr_user.h:45
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
char * name
#define ELSE_WIN_OS_IS_ANSI
typedef HANDLE(WINAPI *apr_winapi_fpt_CreateToolhelp32Snapshot)(DWORD dwFlags
APR_DECLARE_DATA apr_oslevel_e apr_os_level
Definition misc.c:24
@ APR_WIN_NT
#define IF_WIN_OS_IS_UNICODE
typedef DWORD(WINAPI *apr_winapi_fpt_GetCompressedFileSizeA)(IN LPCSTR lpFileName
static void get_sid_string(char *buf, apr_size_t blen, apr_uid_t id)
Definition userinfo.c:33
static size_t keylen(KEY s)
Definition xmlparse.c:7166