/src/samba/third_party/heimdal/lib/roken/getuserinfo.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Kungliga Tekniska Högskolan |
3 | | * (Royal Institute of Technology, Stockholm, Sweden). |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * 2. Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in the |
15 | | * documentation and/or other materials provided with the distribution. |
16 | | * |
17 | | * 3. Neither the name of the Institute nor the names of its contributors |
18 | | * may be used to endorse or promote products derived from this software |
19 | | * without specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
22 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
25 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | | * SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include <config.h> |
35 | | #include "roken.h" |
36 | | |
37 | | #ifdef WIN32 |
38 | | #include <Shlobj.h> // need to include definitions of constants |
39 | | #define SECURITY_WIN32 |
40 | | #include <security.h> |
41 | | #else |
42 | | #include <sys/types.h> |
43 | | #include <pwd.h> |
44 | | #include <unistd.h> |
45 | | #endif |
46 | | |
47 | | /** |
48 | | * Returns the user's SHELL. |
49 | | */ |
50 | | ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
51 | | roken_get_shell(char *shell, size_t shellsz) |
52 | 0 | { |
53 | 0 | char *p; |
54 | |
|
55 | 0 | #ifndef WIN32 |
56 | 0 | #ifdef HAVE_GETPWNAM_R |
57 | 0 | size_t buflen = 2048; |
58 | |
|
59 | 0 | if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0) |
60 | 0 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
61 | 0 | #endif |
62 | |
|
63 | 0 | if (issuid()) |
64 | 0 | return "/bin/sh"; |
65 | | |
66 | 0 | p = secure_getenv("SHELL"); |
67 | 0 | if (p != NULL && p[0] != '\0') { |
68 | 0 | if (strlcpy(shell, p, shellsz) < shellsz) |
69 | 0 | return shell; |
70 | 0 | errno = ERANGE; |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | #ifdef HAVE_GETPWNAM_R |
75 | 0 | { |
76 | 0 | struct passwd pwd; |
77 | 0 | struct passwd *pwdp; |
78 | 0 | char buf[buflen]; |
79 | 0 | char user[128]; |
80 | 0 | const char *username = roken_get_username(user, sizeof(user)); |
81 | |
|
82 | 0 | if (username && |
83 | 0 | getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 && |
84 | 0 | pwdp != NULL && pwdp->pw_shell != NULL) { |
85 | 0 | if (strlcpy(shell, pwdp->pw_shell, shellsz) < shellsz) |
86 | 0 | return shell; |
87 | 0 | errno = ERANGE; |
88 | 0 | return NULL; |
89 | 0 | } |
90 | 0 | } |
91 | 0 | #endif |
92 | 0 | errno = 0; |
93 | 0 | return "/bin/sh"; |
94 | | #else |
95 | | /* Windows */ |
96 | | p = getenv("SHELL"); |
97 | | if (p != NULL && p[0] != '\0') { |
98 | | if (strlcpy(shell, p, shellsz) < shellsz) |
99 | | return shell; |
100 | | errno = ERANGE; |
101 | | return NULL; |
102 | | } |
103 | | errno = 0; |
104 | | return NULL; |
105 | | #endif |
106 | 0 | } |
107 | | |
108 | | /** |
109 | | * Returns the home directory. |
110 | | */ |
111 | | ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
112 | | roken_get_homedir(char *home, size_t homesz) |
113 | 0 | { |
114 | 0 | char *p; |
115 | |
|
116 | | #ifdef WIN32 |
117 | | if (homesz < MAX_PATH) { |
118 | | errno = ERANGE; |
119 | | return NULL; |
120 | | } |
121 | | |
122 | | if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, |
123 | | SHGFP_TYPE_CURRENT, home))) |
124 | | return home; |
125 | | |
126 | | if ((p = getenv("HOMEDRIVE")) != NULL && p[0] != '\0') { |
127 | | if (strlcpy(home, p, homesz) >= homesz) { |
128 | | errno = ERANGE; |
129 | | return NULL; |
130 | | } |
131 | | if ((p = getenv("HOMEPATH")) != NULL) { |
132 | | if (strlcat(home, p, homesz) < homesz) |
133 | | return home; |
134 | | errno = ERANGE; |
135 | | return NULL; |
136 | | } |
137 | | return home; |
138 | | } |
139 | | HEIM_FALLTHROUGH; |
140 | | #else |
141 | 0 | #ifdef HAVE_GETPWNAM_R |
142 | 0 | size_t buflen = 2048; |
143 | |
|
144 | 0 | if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0) |
145 | 0 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
146 | 0 | #endif |
147 | |
|
148 | 0 | if (issuid()) { |
149 | 0 | errno = 0; |
150 | 0 | return NULL; |
151 | 0 | } |
152 | | |
153 | 0 | p = secure_getenv("HOME"); |
154 | 0 | if (p != NULL && p[0] != '\0') { |
155 | 0 | if (strlcpy(home, p, homesz) < homesz) |
156 | 0 | return home; |
157 | 0 | errno = ERANGE; |
158 | 0 | return NULL; |
159 | 0 | } |
160 | | |
161 | 0 | #ifdef HAVE_GETPWNAM_R |
162 | 0 | { |
163 | 0 | char user[128]; |
164 | 0 | const char *username = roken_get_username(user, sizeof(user)); |
165 | 0 | struct passwd pwd; |
166 | 0 | struct passwd *pwdp; |
167 | 0 | char buf[buflen]; |
168 | |
|
169 | 0 | if (username && |
170 | 0 | getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 && |
171 | 0 | pwdp != NULL && pwdp->pw_dir != NULL) { |
172 | 0 | if (strlcpy(home, pwdp->pw_dir, homesz) < homesz) |
173 | 0 | return home; |
174 | 0 | errno = ERANGE; |
175 | 0 | return NULL; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | #endif |
179 | 0 | #endif |
180 | 0 | errno = 0; |
181 | 0 | return NULL; |
182 | 0 | } |
183 | | |
184 | | /** |
185 | | * Returns the home directory on Unix, or the AppData directory on |
186 | | * Windows. |
187 | | */ |
188 | | ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
189 | | roken_get_appdatadir(char *appdata, size_t appdatasz) |
190 | 0 | { |
191 | | #ifdef WIN32 |
192 | | char *p; |
193 | | #endif |
194 | |
|
195 | 0 | #ifndef WIN32 |
196 | 0 | return roken_get_homedir(appdata, appdatasz); |
197 | | #else |
198 | | if (appdatasz < MAX_PATH) { |
199 | | errno = ERANGE; |
200 | | return NULL; |
201 | | } |
202 | | |
203 | | if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, |
204 | | SHGFP_TYPE_CURRENT, appdata))) |
205 | | return appdata; |
206 | | |
207 | | if ((p = getenv("APPDATA")) != NULL && p[0] != '\0') { |
208 | | if (strlcpy(appdata, p, appdatasz) < appdatasz) |
209 | | return appdata; |
210 | | errno = ERANGE; |
211 | | return NULL; |
212 | | } |
213 | | |
214 | | errno = 0; |
215 | | return NULL; |
216 | | #endif |
217 | 0 | } |
218 | | |
219 | | /** |
220 | | * Return a bare username. This is used for, e.g., constructing default |
221 | | * principal names. |
222 | | * |
223 | | * On POSIX systems, if the caller is not set-uid-like, then this will return |
224 | | * the value of the USER or LOGNAME environment variables (in that order of |
225 | | * preference), else the username found by looking up the effective UID. |
226 | | */ |
227 | | ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
228 | | roken_get_username(char *user, size_t usersz) |
229 | 0 | { |
230 | 0 | char *p; |
231 | |
|
232 | | #ifdef WIN32 |
233 | | ULONG sz = usersz; |
234 | | |
235 | | if (GetUserNameEx(NameSamCompatible, user, &sz)) { |
236 | | /* |
237 | | * There's no EXTENDED_NAME_FORMAT for "bare username". We we |
238 | | * have to parse one. |
239 | | */ |
240 | | p = strchr(user, '\\'); |
241 | | if (p != NULL) { |
242 | | p++; |
243 | | memmove(user, p, strlen(p) + 1); |
244 | | } |
245 | | return user; |
246 | | } else { |
247 | | DWORD err = GetLastError(); |
248 | | if (err == ERROR_MORE_DATA) { |
249 | | errno = ERANGE; |
250 | | return NULL; |
251 | | } |
252 | | /* %USERNAME% is generally bare */ |
253 | | p = getenv("USERNAME"); |
254 | | if (p != NULL && p[0] != '\0') { |
255 | | if (strchr(p, '\\') != NULL) |
256 | | p = strchr(p, '\\') + 1; |
257 | | if (strlcpy(user, p, usersz) < usersz) |
258 | | return user; |
259 | | errno = ERANGE; |
260 | | return NULL; |
261 | | } |
262 | | } |
263 | | #else |
264 | 0 | #ifdef HAVE_GETPWUID_R |
265 | 0 | size_t buflen = 2048; |
266 | |
|
267 | 0 | if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0) |
268 | 0 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
269 | 0 | #endif |
270 | |
|
271 | 0 | p = secure_getenv("USER"); |
272 | 0 | if (p == NULL || p[0] == '\0') |
273 | 0 | p = secure_getenv("LOGNAME"); |
274 | 0 | if (p != NULL && p[0] != '\0') { |
275 | 0 | if (strlcpy(user, p, usersz) < usersz) |
276 | 0 | return user; |
277 | 0 | errno = ERANGE; |
278 | 0 | return NULL; |
279 | 0 | } |
280 | | |
281 | 0 | #ifdef HAVE_GETPWUID_R |
282 | 0 | { |
283 | 0 | struct passwd pwd; |
284 | 0 | struct passwd *pwdp; |
285 | 0 | char buf[buflen]; |
286 | |
|
287 | 0 | if (getpwuid_r(getuid(), &pwd, buf, buflen, &pwdp) == 0 && |
288 | 0 | pwdp != NULL && pwdp->pw_name != NULL) { |
289 | 0 | if (strlcpy(user, pwdp->pw_name, usersz) < usersz) |
290 | 0 | return user; |
291 | 0 | errno = ERANGE; |
292 | 0 | return NULL; |
293 | 0 | } |
294 | 0 | } |
295 | 0 | #endif |
296 | 0 | #endif |
297 | 0 | errno = 0; |
298 | 0 | return NULL; |
299 | 0 | } |
300 | | |
301 | | /** |
302 | | * Return a bare username. This is used for, e.g., constructing default |
303 | | * principal names. |
304 | | * |
305 | | * On POSIX systems this returns the name recorded in the system as currently |
306 | | * logged in on the current terminal. |
307 | | */ |
308 | | ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
309 | | roken_get_loginname(char *user, size_t usersz) |
310 | 0 | { |
311 | | #ifdef WIN32 |
312 | | return roken_get_username(user, usersz); |
313 | | #else |
314 | | #ifdef HAVE_GETLOGIN_R |
315 | | if ((errno = getlogin_r(user, usersz)) == 0) |
316 | | return user; |
317 | | if (errno != ENOENT) |
318 | | return NULL; |
319 | | #else |
320 | | #ifdef HAVE_GETLOGIN |
321 | | if ((p = getlogin()) != NULL && p[0] != '\0') { |
322 | | if (strlcpy(user, p, usersz) < usersz) |
323 | | return user; |
324 | | errno = ERANGE; |
325 | | return NULL; |
326 | | } |
327 | | if (errno != ENOENT) |
328 | | return NULL; |
329 | | #endif |
330 | 0 | #endif |
331 | 0 | errno = 0; |
332 | | return NULL; |
333 | 0 | #endif |
334 | 0 | } |