2 * Copyright (c) 1996, 1998-2005, 2007-2011
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)
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_STRINGS_H */
47 #endif /* HAVE_UNISTD_H */
50 #endif /* HAVE_SETAUTHDB */
55 #endif /* HAVE_UTMPX_H */
64 * The passwd and group caches.
66 static struct rbtree *pwcache_byuid, *pwcache_byname;
67 static struct rbtree *grcache_bygid, *grcache_byname;
68 static struct rbtree *grlist_cache;
70 static int cmp_pwuid(const void *, const void *);
71 static int cmp_pwnam(const void *, const void *);
72 static int cmp_grgid(const void *, const void *);
74 #define cmp_grnam cmp_pwnam
76 #define ptr_to_item(p) ((struct cache_item *)((char *)(p) - sizeof(struct cache_item)))
90 struct group_list *grlist;
98 cmp_pwuid(const void *v1, const void *v2)
100 const struct cache_item *ci1 = (const struct cache_item *) v1;
101 const struct cache_item *ci2 = (const struct cache_item *) v2;
102 return ci1->k.uid - ci2->k.uid;
106 * Compare by user name.
109 cmp_pwnam(const void *v1, const void *v2)
111 const struct cache_item *ci1 = (const struct cache_item *) v1;
112 const struct cache_item *ci2 = (const struct cache_item *) v2;
113 return strcmp(ci1->k.name, ci2->k.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 item plus the key and data
135 * elements. If name is non-NULL it is used as the key, else the
136 * uid is the key. Fills in datum from struct password.
138 * We would like to fill in the encrypted password too but the
139 * call to the shadow function could overwrite the pw buffer (NIS).
141 static struct cache_item *
142 make_pwitem(const struct passwd *pw, const char *name)
145 const char *pw_shell;
146 size_t nsize, psize, csize, gsize, dsize, ssize, total;
147 struct cache_item *item;
148 struct passwd *newpw;
150 /* If shell field is empty, expand to _PATH_BSHELL. */
151 pw_shell = (pw->pw_shell == NULL || pw->pw_shell[0] == '\0')
152 ? _PATH_BSHELL : pw->pw_shell;
154 /* Allocate in one big chunk for easy freeing. */
155 nsize = psize = csize = gsize = dsize = ssize = 0;
156 total = sizeof(struct cache_item) + sizeof(struct passwd);
157 FIELD_SIZE(pw, pw_name, nsize);
158 FIELD_SIZE(pw, pw_passwd, psize);
159 #ifdef HAVE_LOGIN_CAP_H
160 FIELD_SIZE(pw, pw_class, csize);
162 FIELD_SIZE(pw, pw_gecos, gsize);
163 FIELD_SIZE(pw, pw_dir, dsize);
164 /* Treat shell specially since we expand "" -> _PATH_BSHELL */
165 ssize = strlen(pw_shell) + 1;
168 total += strlen(name) + 1;
170 /* Allocate space for struct item, struct passwd and the strings. */
171 item = emalloc(total);
172 cp = (char *) item + sizeof(struct cache_item);
175 * Copy in passwd contents and make strings relative to space
176 * at the end of the buffer.
178 newpw = (struct passwd *) cp;
179 memcpy(newpw, pw, sizeof(struct passwd));
180 cp += sizeof(struct passwd);
181 FIELD_COPY(pw, newpw, pw_name, nsize);
182 FIELD_COPY(pw, newpw, pw_passwd, psize);
183 #ifdef HAVE_LOGIN_CAP_H
184 FIELD_COPY(pw, newpw, pw_class, csize);
186 FIELD_COPY(pw, newpw, pw_gecos, gsize);
187 FIELD_COPY(pw, newpw, pw_dir, dsize);
188 /* Treat shell specially since we expand "" -> _PATH_BSHELL */
189 memcpy(cp, pw_shell, ssize);
190 newpw->pw_shell = cp;
193 /* Set key and datum. */
195 memcpy(cp, name, strlen(name) + 1);
198 item->k.uid = pw->pw_uid;
207 pw_addref(struct passwd *pw)
209 ptr_to_item(pw)->refcnt++;
213 pw_delref_item(void *v)
215 struct cache_item *item = v;
217 if (--item->refcnt == 0)
222 pw_delref(struct passwd *pw)
224 pw_delref_item(ptr_to_item(pw));
228 * Get a password entry by uid and allocate space for it.
229 * Fills in pw_passwd from shadow file if necessary.
232 sudo_getpwuid(uid_t uid)
234 struct cache_item key, *item;
238 if ((node = rbfind(pwcache_byuid, &key)) != NULL) {
239 item = (struct cache_item *) node->data;
243 * Cache passwd db entry if it exists or a negative response if not.
245 #ifdef HAVE_SETAUTHDB
246 aix_setauthdb(IDtouser(uid));
248 if ((key.d.pw = getpwuid(uid)) != NULL) {
249 item = make_pwitem(key.d.pw, NULL);
250 if (rbinsert(pwcache_byuid, item) != NULL)
251 errorx(1, _("unable to cache uid %u (%s), already exists"),
252 (unsigned int) uid, item->d.pw->pw_name);
254 item = emalloc(sizeof(*item));
258 if (rbinsert(pwcache_byuid, item) != NULL)
259 errorx(1, _("unable to cache uid %u, already exists"),
262 #ifdef HAVE_SETAUTHDB
271 * Get a password entry by name and allocate space for it.
272 * Fills in pw_passwd from shadow file if necessary.
275 sudo_getpwnam(const char *name)
277 struct cache_item key, *item;
281 key.k.name = (char *) name;
282 if ((node = rbfind(pwcache_byname, &key)) != NULL) {
283 item = (struct cache_item *) node->data;
287 * Cache passwd db entry if it exists or a negative response if not.
289 #ifdef HAVE_SETAUTHDB
290 aix_setauthdb((char *) name);
292 if ((key.d.pw = getpwnam(name)) != NULL) {
293 item = make_pwitem(key.d.pw, name);
294 if (rbinsert(pwcache_byname, item) != NULL)
295 errorx(1, _("unable to cache user %s, already exists"), name);
297 len = strlen(name) + 1;
298 item = emalloc(sizeof(*item) + len);
300 item->k.name = (char *) item + sizeof(*item);
301 memcpy(item->k.name, name, len);
303 if (rbinsert(pwcache_byname, item) != NULL)
304 errorx(1, _("unable to cache user %s, already exists"), name);
306 #ifdef HAVE_SETAUTHDB
315 * Take a uid in string form "#123" and return a faked up passwd struct.
318 sudo_fakepwnam(const char *user, gid_t gid)
320 struct cache_item *item;
326 namelen = strlen(user);
327 len = sizeof(*item) + sizeof(*pw) + namelen + 1 /* pw_name */ +
328 sizeof("*") /* pw_passwd */ + sizeof("") /* pw_gecos */ +
329 sizeof("/") /* pw_dir */ + sizeof(_PATH_BSHELL);
331 for (i = 0; i < 2; i++) {
333 zero_bytes(item, sizeof(*item) + sizeof(*pw));
334 pw = (struct passwd *) ((char *)item + sizeof(*item));
335 pw->pw_uid = (uid_t) atoi(user + 1);
337 pw->pw_name = (char *)pw + sizeof(struct passwd);
338 memcpy(pw->pw_name, user, namelen + 1);
339 pw->pw_passwd = pw->pw_name + namelen + 1;
340 memcpy(pw->pw_passwd, "*", 2);
341 pw->pw_gecos = pw->pw_passwd + 2;
342 pw->pw_gecos[0] = '\0';
343 pw->pw_dir = pw->pw_gecos + 1;
344 memcpy(pw->pw_dir, "/", 2);
345 pw->pw_shell = pw->pw_dir + 2;
346 memcpy(pw->pw_shell, _PATH_BSHELL, sizeof(_PATH_BSHELL));
351 /* Store by uid, overwriting cached version. */
352 item->k.uid = pw->pw_uid;
353 if ((node = rbinsert(pwcache_byuid, item)) != NULL) {
354 pw_delref_item(node->data);
358 /* Store by name, overwriting cached version. */
359 item->k.name = pw->pw_name;
360 if ((node = rbinsert(pwcache_byname, item)) != NULL) {
361 pw_delref_item(node->data);
374 if (pwcache_byuid == NULL)
375 pwcache_byuid = rbcreate(cmp_pwuid);
376 if (pwcache_byname == NULL)
377 pwcache_byname = rbcreate(cmp_pwnam);
381 sudo_freepwcache(void)
383 if (pwcache_byuid != NULL) {
384 rbdestroy(pwcache_byuid, pw_delref_item);
385 pwcache_byuid = NULL;
387 if (pwcache_byname != NULL) {
388 rbdestroy(pwcache_byname, pw_delref_item);
389 pwcache_byname = NULL;
404 cmp_grgid(const void *v1, const void *v2)
406 const struct cache_item *ci1 = (const struct cache_item *) v1;
407 const struct cache_item *ci2 = (const struct cache_item *) v2;
408 return ci1->k.gid - ci2->k.gid;
412 * Dynamically allocate space for a struct item plus the key and data
413 * elements. If name is non-NULL it is used as the key, else the
414 * gid is the key. Fills in datum from struct group.
416 static struct cache_item *
417 make_gritem(const struct group *gr, const char *name)
420 size_t nsize, psize, nmem, total, len;
421 struct cache_item *item;
424 /* Allocate in one big chunk for easy freeing. */
425 nsize = psize = nmem = 0;
426 total = sizeof(struct cache_item) + sizeof(struct group);
427 FIELD_SIZE(gr, gr_name, nsize);
428 FIELD_SIZE(gr, gr_passwd, psize);
430 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++)
431 total += strlen(gr->gr_mem[nmem]) + 1;
433 total += sizeof(char *) * nmem;
436 total += strlen(name) + 1;
438 item = emalloc(total);
439 cp = (char *) item + sizeof(struct cache_item);
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 newgr = (struct group *)cp;
447 memcpy(newgr, gr, sizeof(struct group));
448 cp += sizeof(struct group);
450 newgr->gr_mem = (char **)cp;
451 cp += sizeof(char *) * nmem;
452 for (nmem = 0; gr->gr_mem[nmem] != NULL; nmem++) {
453 len = strlen(gr->gr_mem[nmem]) + 1;
454 memcpy(cp, gr->gr_mem[nmem], len);
455 newgr->gr_mem[nmem] = cp;
458 newgr->gr_mem[nmem] = NULL;
460 FIELD_COPY(gr, newgr, gr_passwd, psize);
461 FIELD_COPY(gr, newgr, gr_name, nsize);
463 /* Set key and datum. */
465 memcpy(cp, name, strlen(name) + 1);
468 item->k.gid = gr->gr_gid;
477 # define GROUPNAME_LEN (sizeof((struct utmpx *)0)->ut_user)
479 # ifdef HAVE_STRUCT_UTMP_UT_USER
480 # define GROUPNAME_LEN (sizeof((struct utmp *)0)->ut_user)
482 # define GROUPNAME_LEN (sizeof((struct utmp *)0)->ut_name)
484 #endif /* HAVE_UTMPX_H */
487 * Dynamically allocate space for a struct item plus the key and data
488 * elements. Fills in datum from the groups and gids arrays.
490 static struct cache_item *
491 make_grlist_item(const char *user, GETGROUPS_T *gids, int ngids)
494 size_t i, nsize, ngroups = 0, total, len;
495 struct cache_item *item;
496 struct group_list *grlist;
499 /* Allocate in one big chunk for easy freeing. */
500 nsize = strlen(user) + 1;
501 total = sizeof(struct cache_item) + sizeof(struct group_list) + nsize;
502 total += sizeof(char *) * ngids;
503 total += sizeof(gid_t *) * ngids;
504 total += GROUPNAME_LEN * ngids;
506 item = emalloc(total);
507 cp = (char *) item + sizeof(struct cache_item);
510 * Copy in group list and make pointers relative to space
511 * at the end of the buffer. Note that the gids array must come
512 * immediately after struct group to guarantee proper alignment.
514 grlist = (struct group_list *)cp;
515 zero_bytes(grlist, sizeof(struct group_list));
516 cp += sizeof(struct group_list);
517 grlist->gids = (gid_t *)cp;
518 cp += sizeof(gid_t) * ngids;
519 grlist->groups = (char **)cp;
520 cp += sizeof(char *) * ngids;
522 /* Set key and datum. */
523 memcpy(cp, user, nsize);
525 item->d.grlist = grlist;
532 for (i = 0; i < ngids; i++)
533 grlist->gids[i] = gids[i];
534 grlist->ngids = ngids;
536 #ifdef HAVE_SETAUTHDB
537 aix_setauthdb((char *) user);
540 * Resolve group names by ID and store at the end.
542 for (i = 0; i < ngids; i++) {
543 if ((grp = sudo_getgrgid(gids[i])) != NULL) {
544 len = strlen(grp->gr_name) + 1;
545 if (cp - (char *)grlist + len > total) {
546 void *ptr = erealloc(grlist, total + len + GROUPNAME_LEN);
547 total += len + GROUPNAME_LEN;
548 cp = (char *)ptr + (cp - (char *)grlist);
551 memcpy(cp, grp->gr_name, len);
552 grlist->groups[ngroups++] = cp;
557 grlist->ngroups = ngroups;
559 #ifdef HAVE_SETAUTHDB
567 gr_addref(struct group *gr)
569 ptr_to_item(gr)->refcnt++;
573 gr_delref_item(void *v)
575 struct cache_item *item = v;
577 if (--item->refcnt == 0)
582 gr_delref(struct group *gr)
584 gr_delref_item(ptr_to_item(gr));
588 * Get a group entry by gid and allocate space for it.
591 sudo_getgrgid(gid_t gid)
593 struct cache_item key, *item;
597 if ((node = rbfind(grcache_bygid, &key)) != NULL) {
598 item = (struct cache_item *) node->data;
602 * Cache group db entry if it exists or a negative response if not.
604 if ((key.d.gr = getgrgid(gid)) != NULL) {
605 item = make_gritem(key.d.gr, NULL);
606 if (rbinsert(grcache_bygid, item) != NULL)
607 errorx(1, _("unable to cache gid %u (%s), already exists"),
608 (unsigned int) gid, key.d.gr->gr_name);
610 item = emalloc(sizeof(*item));
614 if (rbinsert(grcache_bygid, item) != NULL)
615 errorx(1, _("unable to cache gid %u, already exists"),
624 * Get a group entry by name and allocate space for it.
627 sudo_getgrnam(const char *name)
629 struct cache_item key, *item;
633 key.k.name = (char *) name;
634 if ((node = rbfind(grcache_byname, &key)) != NULL) {
635 item = (struct cache_item *) node->data;
639 * Cache group db entry if it exists or a negative response if not.
641 if ((key.d.gr = getgrnam(name)) != NULL) {
642 item = make_gritem(key.d.gr, name);
643 if (rbinsert(grcache_byname, item) != NULL)
644 errorx(1, _("unable to cache group %s, already exists"), name);
646 len = strlen(name) + 1;
647 item = emalloc(sizeof(*item) + len);
649 item->k.name = (char *) item + sizeof(*item);
650 memcpy(item->k.name, name, len);
652 if (rbinsert(grcache_byname, item) != NULL)
653 errorx(1, _("unable to cache group %s, already exists"), name);
661 * Take a gid in string form "#123" and return a faked up group struct.
664 sudo_fakegrnam(const char *group)
666 struct cache_item *item;
672 namelen = strlen(group);
673 len = sizeof(*item) + sizeof(*gr) + namelen + 1;
675 for (i = 0; i < 2; i++) {
677 zero_bytes(item, sizeof(*item) + sizeof(*gr));
678 gr = (struct group *) ((char *)item + sizeof(*item));
679 gr->gr_gid = (gid_t) atoi(group + 1);
680 gr->gr_name = (char *)gr + sizeof(struct group);
681 memcpy(gr->gr_name, group, namelen + 1);
686 /* Store by gid, overwriting cached version. */
687 item->k.gid = gr->gr_gid;
688 if ((node = rbinsert(grcache_bygid, item)) != NULL) {
689 gr_delref_item(node->data);
693 /* Store by name, overwriting cached version. */
694 item->k.name = gr->gr_name;
695 if ((node = rbinsert(grcache_byname, item)) != NULL) {
696 gr_delref_item(node->data);
706 grlist_addref(struct group_list *grlist)
708 ptr_to_item(grlist)->refcnt++;
712 grlist_delref_item(void *v)
714 struct cache_item *item = v;
716 if (--item->refcnt == 0)
721 grlist_delref(struct group_list *grlist)
723 grlist_delref_item(ptr_to_item(grlist));
730 if (grcache_bygid == NULL)
731 grcache_bygid = rbcreate(cmp_grgid);
732 if (grcache_byname == NULL)
733 grcache_byname = rbcreate(cmp_grnam);
734 if (grlist_cache == NULL)
735 grlist_cache = rbcreate(cmp_grnam);
739 sudo_freegrcache(void)
741 if (grcache_bygid != NULL) {
742 rbdestroy(grcache_bygid, gr_delref_item);
743 grcache_bygid = NULL;
745 if (grcache_byname != NULL) {
746 rbdestroy(grcache_byname, gr_delref_item);
747 grcache_byname = NULL;
749 if (grlist_cache != NULL) {
750 rbdestroy(grlist_cache, grlist_delref_item);
763 get_group_list(struct passwd *pw)
765 struct cache_item key, *item;
771 key.k.name = pw->pw_name;
772 if ((node = rbfind(grlist_cache, &key)) != NULL) {
773 item = (struct cache_item *) node->data;
777 * Cache group db entry if it exists or a negative response if not.
779 #if defined(HAVE_SYSCONF) && defined(_SC_NGROUPS_MAX)
780 ngids = (int)sysconf(_SC_NGROUPS_MAX) * 2;
783 ngids = NGROUPS_MAX * 2;
784 gids = emalloc2(ngids, sizeof(GETGROUPS_T));
785 if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1) {
787 gids = emalloc2(ngids, sizeof(GETGROUPS_T));
788 if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1) {
794 if ((item = make_grlist_item(pw->pw_name, gids, ngids)) == NULL)
795 errorx(1, "unable to parse group list for %s", pw->pw_name);
797 if (rbinsert(grlist_cache, item) != NULL)
798 errorx(1, "unable to cache group list for %s, already exists",
801 /* Should not happen. */
802 len = strlen(pw->pw_name) + 1;
803 item = emalloc(sizeof(*item) + len);
805 item->k.name = (char *) item + sizeof(*item);
806 memcpy(item->k.name, pw->pw_name, len);
807 item->d.grlist = NULL;
808 if (rbinsert(grlist_cache, item) != NULL)
809 errorx(1, "unable to cache group list for %s, already exists",
814 return item->d.grlist;
818 set_group_list(const char *user, GETGROUPS_T *gids, int ngids)
820 struct cache_item key, *item;
824 * Cache group db entry if it doesn't already exist
826 key.k.name = (char *) user;
827 if ((node = rbfind(grlist_cache, &key)) == NULL) {
828 if ((item = make_grlist_item(user, gids, ngids)) == NULL)
829 errorx(1, "unable to parse group list for %s", user);
830 if (rbinsert(grlist_cache, item) != NULL)
831 errorx(1, "unable to cache group list for %s, already exists",
837 user_in_group(struct passwd *pw, const char *group)
839 struct group_list *grlist;
840 struct group *grp = NULL;
841 int i, matched = FALSE;
843 if ((grlist = get_group_list(pw)) != NULL) {
845 * If it could be a sudo-style group ID check gids first.
847 if (group[0] == '#') {
848 gid_t gid = atoi(group + 1);
849 if (gid == pw->pw_gid) {
853 for (i = 0; i < grlist->ngids; i++) {
854 if (gid == grlist->gids[i]) {
862 * Next check the supplementary group vector.
863 * It usually includes the password db group too.
865 for (i = 0; i < grlist->ngroups; i++) {
866 if (strcasecmp(group, grlist->groups[i]) == 0) {
872 /* Finally check against user's primary (passwd file) group. */
873 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
874 if (strcasecmp(group, grp->gr_name) == 0) {
882 grlist_delref(grlist);