2 * Copyright (c) 1996, 1998-2005, 2007-2009
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
26 #include <sys/param.h>
35 #endif /* STDC_HEADERS */
37 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
42 # ifdef HAVE_STRINGS_H
45 #endif /* HAVE_STRING_H */
48 #endif /* HAVE_UNISTD_H */
56 __unused static const char rcsid[] = "$Sudo: pwutil.c,v 1.23 2009/05/25 12:02:41 millert Exp $";
60 extern void (*my_setgrent) __P((void));
61 extern void (*my_endgrent) __P((void));
62 extern struct group *(*my_getgrnam) __P((const char *));
63 extern struct group *(*my_getgrgid) __P((gid_t));
64 #define setgrent() my_setgrent()
65 #define endgrent() my_endgrent()
66 #define getgrnam(n) my_getgrnam(n)
67 #define getgrgid(g) my_getgrgid(g)
69 extern void (*my_setpwent) __P((void));
70 extern void (*my_endpwent) __P((void));
71 extern struct passwd *(*my_getpwnam) __P((const char *));
72 extern struct passwd *(*my_getpwuid) __P((uid_t));
73 #define setpwent() my_setpwent()
74 #define endpwent() my_endpwent()
75 #define getpwnam(n) my_getpwnam(n)
76 #define getpwuid(u) my_getpwuid(u)
80 * The passwd and group caches.
82 static struct rbtree *pwcache_byuid, *pwcache_byname;
83 static struct rbtree *grcache_bygid, *grcache_byname;
85 static int cmp_pwuid __P((const void *, const void *));
86 static int cmp_pwnam __P((const void *, const void *));
87 static int cmp_grgid __P((const void *, const void *));
88 static int cmp_grnam __P((const void *, const void *));
98 const struct passwd *pw1 = (const struct passwd *) v1;
99 const struct passwd *pw2 = (const struct passwd *) v2;
100 return(pw1->pw_uid - pw2->pw_uid);
104 * Compare by user name.
111 const struct passwd *pw1 = (const struct passwd *) v1;
112 const struct passwd *pw2 = (const struct passwd *) v2;
113 return(strcmp(pw1->pw_name, pw2->pw_name));
116 #define FIELD_SIZE(src, name, size) \
119 size = strlen(src->name) + 1; \
124 #define FIELD_COPY(src, dst, name, size) \
127 memcpy(cp, src->name, size); \
134 * Dynamically allocate space for a struct password and the constituent parts
135 * that we care about. Fills in pw_passwd from shadow file.
137 static struct passwd *
139 const struct passwd *pw;
142 const char *pw_shell;
143 size_t nsize, psize, csize, gsize, dsize, ssize, total;
144 struct passwd *newpw;
146 /* If shell field is empty, expand to _PATH_BSHELL. */
147 pw_shell = (pw->pw_shell == NULL || pw->pw_shell[0] == '\0')
148 ? _PATH_BSHELL : pw->pw_shell;
150 /* Allocate in one big chunk for easy freeing. */
151 nsize = psize = csize = gsize = dsize = ssize = 0;
152 total = sizeof(struct passwd);
153 FIELD_SIZE(pw, pw_name, nsize);
154 FIELD_SIZE(pw, pw_passwd, psize);
155 #ifdef HAVE_LOGIN_CAP_H
156 FIELD_SIZE(pw, pw_class, csize);
158 FIELD_SIZE(pw, pw_gecos, gsize);
159 FIELD_SIZE(pw, pw_dir, dsize);
160 FIELD_SIZE(pw, pw_shell, ssize);
162 if ((cp = malloc(total)) == NULL)
164 newpw = (struct passwd *) cp;
167 * Copy in passwd contents and make strings relative to space
168 * at the end of the buffer.
170 memcpy(newpw, pw, sizeof(struct passwd));
171 cp += sizeof(struct passwd);
172 FIELD_COPY(pw, newpw, pw_name, nsize);
173 FIELD_COPY(pw, newpw, pw_passwd, psize);
174 #ifdef HAVE_LOGIN_CAP_H
175 FIELD_COPY(pw, newpw, pw_class, csize);
177 FIELD_COPY(pw, newpw, pw_gecos, gsize);
178 FIELD_COPY(pw, newpw, pw_dir, dsize);
179 FIELD_COPY(pw, newpw, pw_shell, ssize);
185 * Get a password entry by uid and allocate space for it.
186 * Fills in pw_passwd from shadow file if necessary.
192 struct passwd key, *pw;
197 if ((node = rbfind(pwcache_byuid, &key)) != NULL) {
198 pw = (struct passwd *) node->data;
199 return(pw->pw_name != NULL ? pw : NULL);
202 * Cache passwd db entry if it exists or a negative response if not.
204 if ((pw = getpwuid(uid)) != NULL) {
206 cp = sudo_getepw(pw); /* get shadow password */
207 if (pw->pw_passwd != NULL)
208 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
210 if (rbinsert(pwcache_byuid, (void *) pw) != NULL)
211 errorx(1, "unable to cache uid %lu (%s), already exists",
215 pw = emalloc(sizeof(*pw));
216 zero_bytes(pw, sizeof(*pw));
218 if (rbinsert(pwcache_byuid, (void *) pw) != NULL)
219 errorx(1, "unable to cache uid %lu, already exists", uid);
225 * Get a password entry by name and allocate space for it.
226 * Fills in pw_passwd from shadow file if necessary.
232 struct passwd key, *pw;
237 key.pw_name = (char *) name;
238 if ((node = rbfind(pwcache_byname, &key)) != NULL) {
239 pw = (struct passwd *) node->data;
240 return(pw->pw_uid != (uid_t) -1 ? pw : NULL);
243 * Cache passwd db entry if it exists or a negative response if not.
245 if ((pw = getpwnam(name)) != NULL) {
247 cp = sudo_getepw(pw); /* get shadow password */
248 if (pw->pw_passwd != NULL)
249 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
251 if (rbinsert(pwcache_byname, (void *) pw) != NULL)
252 errorx(1, "unable to cache user %s, already exists", name);
255 len = strlen(name) + 1;
256 cp = emalloc(sizeof(*pw) + len);
257 zero_bytes(cp, sizeof(*pw));
258 pw = (struct passwd *) cp;
260 memcpy(cp, name, len);
262 pw->pw_uid = (uid_t) -1;
263 if (rbinsert(pwcache_byname, (void *) pw) != NULL)
264 errorx(1, "unable to cache user %s, already exists", name);
270 * Take a uid in string form "#123" and return a faked up passwd struct.
273 sudo_fakepwnam(user, gid)
282 pw = emalloc(sizeof(struct passwd) + len + 1 /* pw_name */ +
283 sizeof("*") /* pw_passwd */ + sizeof("") /* pw_gecos */ +
284 sizeof("/") /* pw_dir */ + sizeof(_PATH_BSHELL));
285 zero_bytes(pw, sizeof(struct passwd));
286 pw->pw_uid = (uid_t) atoi(user + 1);
288 pw->pw_name = (char *)pw + sizeof(struct passwd);
289 memcpy(pw->pw_name, user, len + 1);
290 pw->pw_passwd = pw->pw_name + len + 1;
291 memcpy(pw->pw_passwd, "*", 2);
292 pw->pw_gecos = pw->pw_passwd + 2;
293 pw->pw_gecos[0] = '\0';
294 pw->pw_dir = pw->pw_gecos + 1;
295 memcpy(pw->pw_dir, "/", 2);
296 pw->pw_shell = pw->pw_dir + 2;
297 memcpy(pw->pw_shell, _PATH_BSHELL, sizeof(_PATH_BSHELL));
299 /* Store by uid and by name, overwriting cached version. */
300 if ((node = rbinsert(pwcache_byuid, pw)) != NULL) {
302 node->data = (void *) pw;
304 if ((node = rbinsert(pwcache_byname, pw)) != NULL) {
306 node->data = (void *) pw;
312 * Take a gid in string form "#123" and return a faked up group struct.
315 sudo_fakegrnam(group)
323 gr = emalloc(sizeof(struct group) + len + 1);
324 zero_bytes(gr, sizeof(struct group));
325 gr->gr_gid = (gid_t) atoi(group + 1);
326 gr->gr_name = (char *)gr + sizeof(struct group);
327 strlcpy(gr->gr_name, group, len + 1);
329 /* Store by gid and by name, overwriting cached version. */
330 if ((node = rbinsert(grcache_bygid, gr)) != NULL) {
332 node->data = (void *) gr;
334 if ((node = rbinsert(grcache_byname, gr)) != NULL) {
336 node->data = (void *) gr;
346 if (pwcache_byuid == NULL)
347 pwcache_byuid = rbcreate(cmp_pwuid);
348 if (pwcache_byname == NULL)
349 pwcache_byname = rbcreate(cmp_pwnam);
353 static void pw_free __P((void *));
358 if (pwcache_byuid != NULL) {
359 rbdestroy(pwcache_byuid, pw_free);
360 pwcache_byuid = NULL;
362 if (pwcache_byname != NULL) {
363 rbdestroy(pwcache_byname, NULL);
364 pwcache_byname = NULL;
372 struct passwd *pw = (struct passwd *) v;
374 if (pw->pw_passwd != NULL) {
375 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
376 efree(pw->pw_passwd);
400 const struct group *grp1 = (const struct group *) v1;
401 const struct group *grp2 = (const struct group *) v2;
402 return(grp1->gr_gid - grp2->gr_gid);
406 * Compare by group name.
413 const struct group *grp1 = (const struct group *) v1;
414 const struct group *grp2 = (const struct group *) v2;
415 return(strcmp(grp1->gr_name, grp2->gr_name));
420 const struct group *gr;
423 size_t nsize, psize, nmem, total, len;
426 /* Allocate in one big chunk for easy freeing. */
427 nsize = psize = nmem = 0;
428 total = sizeof(struct group);
429 FIELD_SIZE(gr, gr_name, nsize);
430 FIELD_SIZE(gr, gr_passwd, psize);
432 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++)
433 total += strlen(gr->gr_mem[nmem]) + 1;
435 total += sizeof(char *) * nmem;
437 if ((cp = malloc(total)) == NULL)
439 newgr = (struct group *)cp;
442 * Copy in group contents and make strings relative to space
443 * at the end of the buffer. Note that gr_mem must come
444 * immediately after struct group to guarantee proper alignment.
446 (void)memcpy(newgr, gr, sizeof(struct group));
447 cp += sizeof(struct group);
449 newgr->gr_mem = (char **)cp;
450 cp += sizeof(char *) * nmem;
451 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++) {
452 len = strlen(gr->gr_mem[nmem]) + 1;
453 memcpy(cp, gr->gr_mem[nmem], len);
454 newgr->gr_mem[nmem] = cp;
457 newgr->gr_mem[nmem] = NULL;
459 FIELD_COPY(gr, newgr, gr_passwd, psize);
460 FIELD_COPY(gr, newgr, gr_name, nsize);
466 * Get a group entry by gid and allocate space for it.
472 struct group key, *gr;
476 if ((node = rbfind(grcache_bygid, &key)) != NULL) {
477 gr = (struct group *) node->data;
478 return(gr->gr_name != NULL ? gr : NULL);
481 * Cache group db entry if it exists or a negative response if not.
483 if ((gr = getgrgid(gid)) != NULL) {
485 if (rbinsert(grcache_bygid, (void *) gr) != NULL)
486 errorx(1, "unable to cache gid %lu (%s), already exists",
490 gr = emalloc(sizeof(*gr));
491 zero_bytes(gr, sizeof(*gr));
493 if (rbinsert(grcache_bygid, (void *) gr) != NULL)
494 errorx(1, "unable to cache gid %lu, already exists, gid");
500 * Get a group entry by name and allocate space for it.
506 struct group key, *gr;
511 key.gr_name = (char *) name;
512 if ((node = rbfind(grcache_byname, &key)) != NULL) {
513 gr = (struct group *) node->data;
514 return(gr->gr_gid != (gid_t) -1 ? gr : NULL);
517 * Cache group db entry if it exists or a negative response if not.
519 if ((gr = getgrnam(name)) != NULL) {
521 if (rbinsert(grcache_byname, (void *) gr) != NULL)
522 errorx(1, "unable to cache group %s, already exists", name);
525 len = strlen(name) + 1;
526 cp = emalloc(sizeof(*gr) + len);
527 zero_bytes(cp, sizeof(*gr));
528 gr = (struct group *) cp;
530 memcpy(cp, name, len);
532 gr->gr_gid = (gid_t) -1;
533 if (rbinsert(grcache_byname, (void *) gr) != NULL)
534 errorx(1, "unable to cache group %s, already exists", name);
543 if (grcache_bygid == NULL)
544 grcache_bygid = rbcreate(cmp_grgid);
545 if (grcache_byname == NULL)
546 grcache_byname = rbcreate(cmp_grnam);
553 if (grcache_bygid != NULL) {
554 rbdestroy(grcache_bygid, free);
555 grcache_bygid = NULL;
557 if (grcache_byname != NULL) {
558 rbdestroy(grcache_byname, NULL);
559 grcache_byname = NULL;