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 extern void (*my_setgrent) __P((void));
57 extern void (*my_endgrent) __P((void));
58 extern struct group *(*my_getgrnam) __P((const char *));
59 extern struct group *(*my_getgrgid) __P((gid_t));
60 #define setgrent() my_setgrent()
61 #define endgrent() my_endgrent()
62 #define getgrnam(n) my_getgrnam(n)
63 #define getgrgid(g) my_getgrgid(g)
65 extern void (*my_setpwent) __P((void));
66 extern void (*my_endpwent) __P((void));
67 extern struct passwd *(*my_getpwnam) __P((const char *));
68 extern struct passwd *(*my_getpwuid) __P((uid_t));
69 #define setpwent() my_setpwent()
70 #define endpwent() my_endpwent()
71 #define getpwnam(n) my_getpwnam(n)
72 #define getpwuid(u) my_getpwuid(u)
76 * The passwd and group caches.
78 static struct rbtree *pwcache_byuid, *pwcache_byname;
79 static struct rbtree *grcache_bygid, *grcache_byname;
81 static int cmp_pwuid __P((const void *, const void *));
82 static int cmp_pwnam __P((const void *, const void *));
83 static int cmp_grgid __P((const void *, const void *));
84 static int cmp_grnam __P((const void *, const void *));
94 const struct passwd *pw1 = (const struct passwd *) v1;
95 const struct passwd *pw2 = (const struct passwd *) v2;
96 return(pw1->pw_uid - pw2->pw_uid);
100 * Compare by user name.
107 const struct passwd *pw1 = (const struct passwd *) v1;
108 const struct passwd *pw2 = (const struct passwd *) v2;
109 return(strcmp(pw1->pw_name, pw2->pw_name));
112 #define FIELD_SIZE(src, name, size) \
115 size = strlen(src->name) + 1; \
120 #define FIELD_COPY(src, dst, name, size) \
123 memcpy(cp, src->name, size); \
130 * Dynamically allocate space for a struct password and the constituent parts
131 * that we care about. Fills in pw_passwd from shadow file.
133 static struct passwd *
135 const struct passwd *pw;
138 const char *pw_shell;
139 size_t nsize, psize, csize, gsize, dsize, ssize, total;
140 struct passwd *newpw;
142 /* If shell field is empty, expand to _PATH_BSHELL. */
143 pw_shell = (pw->pw_shell == NULL || pw->pw_shell[0] == '\0')
144 ? _PATH_BSHELL : pw->pw_shell;
146 /* Allocate in one big chunk for easy freeing. */
147 nsize = psize = csize = gsize = dsize = ssize = 0;
148 total = sizeof(struct passwd);
149 FIELD_SIZE(pw, pw_name, nsize);
150 FIELD_SIZE(pw, pw_passwd, psize);
151 #ifdef HAVE_LOGIN_CAP_H
152 FIELD_SIZE(pw, pw_class, csize);
154 FIELD_SIZE(pw, pw_gecos, gsize);
155 FIELD_SIZE(pw, pw_dir, dsize);
156 FIELD_SIZE(pw, pw_shell, ssize);
158 if ((cp = malloc(total)) == NULL)
160 newpw = (struct passwd *) cp;
163 * Copy in passwd contents and make strings relative to space
164 * at the end of the buffer.
166 memcpy(newpw, pw, sizeof(struct passwd));
167 cp += sizeof(struct passwd);
168 FIELD_COPY(pw, newpw, pw_name, nsize);
169 FIELD_COPY(pw, newpw, pw_passwd, psize);
170 #ifdef HAVE_LOGIN_CAP_H
171 FIELD_COPY(pw, newpw, pw_class, csize);
173 FIELD_COPY(pw, newpw, pw_gecos, gsize);
174 FIELD_COPY(pw, newpw, pw_dir, dsize);
175 FIELD_COPY(pw, newpw, pw_shell, ssize);
181 * Get a password entry by uid and allocate space for it.
182 * Fills in pw_passwd from shadow file if necessary.
188 struct passwd key, *pw;
193 if ((node = rbfind(pwcache_byuid, &key)) != NULL) {
194 pw = (struct passwd *) node->data;
195 return(pw->pw_name != NULL ? pw : NULL);
198 * Cache passwd db entry if it exists or a negative response if not.
200 if ((pw = getpwuid(uid)) != NULL) {
202 cp = sudo_getepw(pw); /* get shadow password */
203 if (pw->pw_passwd != NULL)
204 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
206 if (rbinsert(pwcache_byuid, (void *) pw) != NULL)
207 errorx(1, "unable to cache uid %lu (%s), already exists",
211 pw = emalloc(sizeof(*pw));
212 zero_bytes(pw, sizeof(*pw));
214 if (rbinsert(pwcache_byuid, (void *) pw) != NULL)
215 errorx(1, "unable to cache uid %lu, already exists", uid);
221 * Get a password entry by name and allocate space for it.
222 * Fills in pw_passwd from shadow file if necessary.
228 struct passwd key, *pw;
233 key.pw_name = (char *) name;
234 if ((node = rbfind(pwcache_byname, &key)) != NULL) {
235 pw = (struct passwd *) node->data;
236 return(pw->pw_uid != (uid_t) -1 ? pw : NULL);
239 * Cache passwd db entry if it exists or a negative response if not.
241 if ((pw = getpwnam(name)) != NULL) {
243 cp = sudo_getepw(pw); /* get shadow password */
244 if (pw->pw_passwd != NULL)
245 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
247 if (rbinsert(pwcache_byname, (void *) pw) != NULL)
248 errorx(1, "unable to cache user %s, already exists", name);
251 len = strlen(name) + 1;
252 cp = emalloc(sizeof(*pw) + len);
253 zero_bytes(cp, sizeof(*pw));
254 pw = (struct passwd *) cp;
256 memcpy(cp, name, len);
258 pw->pw_uid = (uid_t) -1;
259 if (rbinsert(pwcache_byname, (void *) pw) != NULL)
260 errorx(1, "unable to cache user %s, already exists", name);
266 * Take a uid in string form "#123" and return a faked up passwd struct.
269 sudo_fakepwnam(user, gid)
278 pw = emalloc(sizeof(struct passwd) + len + 1 /* pw_name */ +
279 sizeof("*") /* pw_passwd */ + sizeof("") /* pw_gecos */ +
280 sizeof("/") /* pw_dir */ + sizeof(_PATH_BSHELL));
281 zero_bytes(pw, sizeof(struct passwd));
282 pw->pw_uid = (uid_t) atoi(user + 1);
284 pw->pw_name = (char *)pw + sizeof(struct passwd);
285 memcpy(pw->pw_name, user, len + 1);
286 pw->pw_passwd = pw->pw_name + len + 1;
287 memcpy(pw->pw_passwd, "*", 2);
288 pw->pw_gecos = pw->pw_passwd + 2;
289 pw->pw_gecos[0] = '\0';
290 pw->pw_dir = pw->pw_gecos + 1;
291 memcpy(pw->pw_dir, "/", 2);
292 pw->pw_shell = pw->pw_dir + 2;
293 memcpy(pw->pw_shell, _PATH_BSHELL, sizeof(_PATH_BSHELL));
295 /* Store by uid and by name, overwriting cached version. */
296 if ((node = rbinsert(pwcache_byuid, pw)) != NULL) {
298 node->data = (void *) pw;
300 if ((node = rbinsert(pwcache_byname, pw)) != NULL) {
302 node->data = (void *) pw;
308 * Take a gid in string form "#123" and return a faked up group struct.
311 sudo_fakegrnam(group)
319 gr = emalloc(sizeof(struct group) + len + 1);
320 zero_bytes(gr, sizeof(struct group));
321 gr->gr_gid = (gid_t) atoi(group + 1);
322 gr->gr_name = (char *)gr + sizeof(struct group);
323 strlcpy(gr->gr_name, group, len + 1);
325 /* Store by gid and by name, overwriting cached version. */
326 if ((node = rbinsert(grcache_bygid, gr)) != NULL) {
328 node->data = (void *) gr;
330 if ((node = rbinsert(grcache_byname, gr)) != NULL) {
332 node->data = (void *) gr;
342 if (pwcache_byuid == NULL)
343 pwcache_byuid = rbcreate(cmp_pwuid);
344 if (pwcache_byname == NULL)
345 pwcache_byname = rbcreate(cmp_pwnam);
349 static void pw_free __P((void *));
354 if (pwcache_byuid != NULL) {
355 rbdestroy(pwcache_byuid, pw_free);
356 pwcache_byuid = NULL;
358 if (pwcache_byname != NULL) {
359 rbdestroy(pwcache_byname, NULL);
360 pwcache_byname = NULL;
368 struct passwd *pw = (struct passwd *) v;
370 if (pw->pw_passwd != NULL) {
371 zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd));
372 efree(pw->pw_passwd);
396 const struct group *grp1 = (const struct group *) v1;
397 const struct group *grp2 = (const struct group *) v2;
398 return(grp1->gr_gid - grp2->gr_gid);
402 * Compare by group name.
409 const struct group *grp1 = (const struct group *) v1;
410 const struct group *grp2 = (const struct group *) v2;
411 return(strcmp(grp1->gr_name, grp2->gr_name));
416 const struct group *gr;
419 size_t nsize, psize, nmem, total, len;
422 /* Allocate in one big chunk for easy freeing. */
423 nsize = psize = nmem = 0;
424 total = sizeof(struct group);
425 FIELD_SIZE(gr, gr_name, nsize);
426 FIELD_SIZE(gr, gr_passwd, psize);
428 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++)
429 total += strlen(gr->gr_mem[nmem]) + 1;
431 total += sizeof(char *) * nmem;
433 if ((cp = malloc(total)) == NULL)
435 newgr = (struct group *)cp;
438 * Copy in group contents and make strings relative to space
439 * at the end of the buffer. Note that gr_mem must come
440 * immediately after struct group to guarantee proper alignment.
442 (void)memcpy(newgr, gr, sizeof(struct group));
443 cp += sizeof(struct group);
445 newgr->gr_mem = (char **)cp;
446 cp += sizeof(char *) * nmem;
447 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++) {
448 len = strlen(gr->gr_mem[nmem]) + 1;
449 memcpy(cp, gr->gr_mem[nmem], len);
450 newgr->gr_mem[nmem] = cp;
453 newgr->gr_mem[nmem] = NULL;
455 FIELD_COPY(gr, newgr, gr_passwd, psize);
456 FIELD_COPY(gr, newgr, gr_name, nsize);
462 * Get a group entry by gid and allocate space for it.
468 struct group key, *gr;
472 if ((node = rbfind(grcache_bygid, &key)) != NULL) {
473 gr = (struct group *) node->data;
474 return(gr->gr_name != NULL ? gr : NULL);
477 * Cache group db entry if it exists or a negative response if not.
479 if ((gr = getgrgid(gid)) != NULL) {
481 if (rbinsert(grcache_bygid, (void *) gr) != NULL)
482 errorx(1, "unable to cache gid %lu (%s), already exists",
486 gr = emalloc(sizeof(*gr));
487 zero_bytes(gr, sizeof(*gr));
489 if (rbinsert(grcache_bygid, (void *) gr) != NULL)
490 errorx(1, "unable to cache gid %lu, already exists, gid");
496 * Get a group entry by name and allocate space for it.
502 struct group key, *gr;
507 key.gr_name = (char *) name;
508 if ((node = rbfind(grcache_byname, &key)) != NULL) {
509 gr = (struct group *) node->data;
510 return(gr->gr_gid != (gid_t) -1 ? gr : NULL);
513 * Cache group db entry if it exists or a negative response if not.
515 if ((gr = getgrnam(name)) != NULL) {
517 if (rbinsert(grcache_byname, (void *) gr) != NULL)
518 errorx(1, "unable to cache group %s, already exists", name);
521 len = strlen(name) + 1;
522 cp = emalloc(sizeof(*gr) + len);
523 zero_bytes(cp, sizeof(*gr));
524 gr = (struct group *) cp;
526 memcpy(cp, name, len);
528 gr->gr_gid = (gid_t) -1;
529 if (rbinsert(grcache_byname, (void *) gr) != NULL)
530 errorx(1, "unable to cache group %s, already exists", name);
539 if (grcache_bygid == NULL)
540 grcache_bygid = rbcreate(cmp_grgid);
541 if (grcache_byname == NULL)
542 grcache_byname = rbcreate(cmp_grnam);
549 if (grcache_bygid != NULL) {
550 rbdestroy(grcache_bygid, free);
551 grcache_bygid = NULL;
553 if (grcache_byname != NULL) {
554 rbdestroy(grcache_byname, NULL);
555 grcache_byname = NULL;