~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/user/win32/userinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
 */
 
33
void get_sid_string(char *buf, apr_size_t blen, apr_uid_t id)
 
34
{
 
35
    PSID_IDENTIFIER_AUTHORITY psia;
 
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
     */
 
43
    psia = GetSidIdentifierAuthority(id);
 
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-%lu-0x%04x%08x",
 
49
                            SID_REVISION, sa, nsa);
 
50
    } else {
 
51
        slen = apr_snprintf(buf, blen, "S-%lu-%lu",
 
52
                            SID_REVISION, nsa);
 
53
    }
 
54
 
 
55
    /* Now append all the subauthority strings.
 
56
     */
 
57
    nsa = *GetSidSubAuthorityCount(id);
 
58
    for (sa = 0; sa < nsa; ++sa) {
 
59
        slen += apr_snprintf(buf + slen, blen - slen, "-%lu",
 
60
                             *GetSidSubAuthority(id, sa));
 
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
 */
 
67
APR_DECLARE(apr_status_t) apr_uid_homepath_get(char **dirname, 
 
68
                                               const char *username, 
 
69
                                               apr_pool_t *p)
 
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;
 
78
    DWORD keylen;
 
79
    DWORD type;
 
80
    HKEY key;
 
81
 
 
82
    if (apr_os_level >= APR_WIN_NT) {
 
83
        apr_uid_t uid;
 
84
        apr_gid_t gid;
 
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);
 
92
        get_sid_string(regkey + keylen, sizeof(regkey) - keylen, uid);
 
93
    }
 
94
    else {
 
95
        strcpy(regkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\"
 
96
                       "ProfileList\\");
 
97
        keylen = (DWORD)strlen(regkey);
 
98
        apr_cpystrn(regkey + keylen, username, sizeof(regkey) - keylen);
 
99
    }
 
100
 
 
101
    if ((rv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey, 0, 
 
102
                           KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS)
 
103
        return APR_FROM_OS_ERROR(rv);
 
104
 
 
105
#if APR_HAS_UNICODE_FS
 
106
    IF_WIN_OS_IS_UNICODE
 
107
    {
 
108
        keylen = sizeof(regkey);
 
109
        rv = RegQueryValueExW(key, L"ProfileImagePath", NULL, &type,
 
110
                                   (void*)regkey, &keylen);
 
111
        RegCloseKey(key);
 
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), 
 
117
                                           (apr_wchar_t*)regkey)) != APR_SUCCESS)
 
118
                return rv;
 
119
            *dirname = apr_pstrdup(p, retdir);
 
120
        }
 
121
        else if (type == REG_EXPAND_SZ) {
 
122
            apr_wchar_t path[MAX_PATH];
 
123
            char retdir[MAX_PATH];
 
124
            ExpandEnvironmentStringsW((apr_wchar_t*)regkey, path, sizeof(path));
 
125
            if ((rv = unicode_to_utf8_path(retdir, sizeof(retdir), path))
 
126
                    != APR_SUCCESS)
 
127
                return rv;
 
128
            *dirname = apr_pstrdup(p, retdir);
 
129
        }
 
130
        else
 
131
            return APR_ENOENT;
 
132
    }
 
133
#endif
 
134
#if APR_HAS_ANSI_FS
 
135
    ELSE_WIN_OS_IS_ANSI
 
136
    {
 
137
        keylen = sizeof(regkey);
 
138
        rv = RegQueryValueEx(key, "ProfileImagePath", NULL, &type,
 
139
                                  (void*)regkey, &keylen);
 
140
        RegCloseKey(key);
 
141
        if (rv != ERROR_SUCCESS)
 
142
            return APR_FROM_OS_ERROR(rv);
 
143
        if (type == REG_SZ) {
 
144
            *dirname = apr_pstrdup(p, regkey);
 
145
        }
 
146
        else if (type == REG_EXPAND_SZ) {
 
147
            char path[MAX_PATH];
 
148
            ExpandEnvironmentStrings(regkey, path, sizeof(path));
 
149
            *dirname = apr_pstrdup(p, path);
 
150
        }
 
151
        else
 
152
            return APR_ENOENT;
 
153
    }
 
154
#endif /* APR_HAS_ANSI_FS */
 
155
    for (fixch = *dirname; *fixch; ++fixch)
 
156
        if (*fixch == '\\')
 
157
            *fixch = '/';
 
158
    return APR_SUCCESS;
 
159
#endif /* _WIN32_WCE */
 
160
}
 
161
 
 
162
APR_DECLARE(apr_status_t) apr_uid_current(apr_uid_t *uid,
 
163
                                          apr_gid_t *gid,
 
164
                                          apr_pool_t *p)
 
165
{
 
166
#ifdef _WIN32_WCE
 
167
    return APR_ENOTIMPL;
 
168
#else
 
169
    HANDLE threadtok;
 
170
    DWORD needed;
 
171
    TOKEN_USER *usr;
 
172
    TOKEN_PRIMARY_GROUP *grp;
 
173
    
 
174
    if(!OpenProcessToken(GetCurrentProcess(), STANDARD_RIGHTS_READ | READ_CONTROL | TOKEN_QUERY, &threadtok)) {
 
175
        return apr_get_os_error();
 
176
    }
 
177
 
 
178
    *uid = NULL;
 
179
    if (!GetTokenInformation(threadtok, TokenUser, NULL, 0, &needed)
 
180
        && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
 
181
        && (usr = apr_palloc(p, needed))
 
182
        && GetTokenInformation(threadtok, TokenUser, usr, needed, &needed))
 
183
        *uid = usr->User.Sid;
 
184
    else
 
185
        return apr_get_os_error();
 
186
 
 
187
    if (!GetTokenInformation(threadtok, TokenPrimaryGroup, NULL, 0, &needed)
 
188
        && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
 
189
        && (grp = apr_palloc(p, needed))
 
190
        && GetTokenInformation(threadtok, TokenPrimaryGroup, grp, needed, &needed))
 
191
        *gid = grp->PrimaryGroup;
 
192
    else
 
193
        return apr_get_os_error();
 
194
 
 
195
    return APR_SUCCESS;
 
196
#endif 
 
197
}
 
198
 
 
199
APR_DECLARE(apr_status_t) apr_uid_get(apr_uid_t *uid, apr_gid_t *gid,
 
200
                                      const char *username, apr_pool_t *p)
 
201
{
 
202
#ifdef _WIN32_WCE
 
203
    return APR_ENOTIMPL;
 
204
#else
 
205
    SID_NAME_USE sidtype;
 
206
    char anydomain[256];
 
207
    char *domain;
 
208
    DWORD sidlen = 0;
 
209
    DWORD domlen = sizeof(anydomain);
 
210
    DWORD rv;
 
211
    char *pos;
 
212
 
 
213
    if (pos = strchr(username, '/')) {
 
214
        domain = apr_pstrndup(p, username, pos - username);
 
215
        username = pos + 1;
 
216
    }
 
217
    else if (pos = strchr(username, '\\')) {
 
218
        domain = apr_pstrndup(p, username, pos - username);
 
219
        username = pos + 1;
 
220
    }
 
221
    else {
 
222
        domain = NULL;
 
223
    }
 
224
    /* Get nothing on the first pass ... need to size the sid buffer 
 
225
     */
 
226
    rv = LookupAccountName(domain, username, domain, &sidlen, 
 
227
                           anydomain, &domlen, &sidtype);
 
228
    if (sidlen) {
 
229
        /* Give it back on the second pass
 
230
         */
 
231
        *uid = apr_palloc(p, sidlen);
 
232
        domlen = sizeof(anydomain);
 
233
        rv = LookupAccountName(domain, username, *uid, &sidlen, 
 
234
                               anydomain, &domlen, &sidtype);
 
235
    }
 
236
    if (!sidlen || !rv) {
 
237
        return apr_get_os_error();
 
238
    }
 
239
    /* There doesn't seem to be a simple way to retrieve the primary group sid
 
240
     */
 
241
    *gid = NULL;
 
242
    return APR_SUCCESS;
 
243
#endif
 
244
}
 
245
 
 
246
APR_DECLARE(apr_status_t) apr_uid_name_get(char **username, apr_uid_t userid,
 
247
                                           apr_pool_t *p)
 
248
{
 
249
#ifdef _WIN32_WCE
 
250
    *username = apr_pstrdup(p, "Administrator");
 
251
    return APR_SUCCESS;
 
252
#else
 
253
    SID_NAME_USE type;
 
254
    char name[MAX_PATH], domain[MAX_PATH];
 
255
    DWORD cbname = sizeof(name), cbdomain = sizeof(domain);
 
256
    if (!userid)
 
257
        return APR_EINVAL;
 
258
    if (!LookupAccountSid(NULL, userid, name, &cbname, domain, &cbdomain, &type))
 
259
        return apr_get_os_error();
 
260
    if (type != SidTypeUser && type != SidTypeAlias && type != SidTypeWellKnownGroup)
 
261
        return APR_EINVAL;
 
262
    *username = apr_pstrdup(p, name);
 
263
    return APR_SUCCESS;
 
264
#endif
 
265
}
 
266
  
 
267
APR_DECLARE(apr_status_t) apr_uid_compare(apr_uid_t left, apr_uid_t right)
 
268
{
 
269
    if (!left || !right)
 
270
        return APR_EINVAL;
 
271
#ifndef _WIN32_WCE
 
272
    if (!IsValidSid(left) || !IsValidSid(right))
 
273
        return APR_EINVAL;
 
274
    if (!EqualSid(left, right))
 
275
        return APR_EMISMATCH;
 
276
#endif
 
277
    return APR_SUCCESS;
 
278
}
 
279