From: Todd C. Miller Date: Wed, 11 May 2016 15:40:31 +0000 (-0600) Subject: Remove sudo_mkpwcache() and sudo_mkgrcache(). We now create the X-Git-Tag: SUDO_1_8_17^2~71 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=05db5aa3b885e00e9345ea5ed80e9f2a91c4c2fb;p=sudo Remove sudo_mkpwcache() and sudo_mkgrcache(). We now create the caches as needed on demand. Also remove calls to sudo_freepwcache() and sudo_freegrcache() that are immediately followed by execve(), they are not needed. --- diff --git a/plugins/sudoers/iolog.c b/plugins/sudoers/iolog.c index 1989fe969..a0ba31933 100644 --- a/plugins/sudoers/iolog.c +++ b/plugins/sudoers/iolog.c @@ -601,11 +601,6 @@ sudoers_io_open(unsigned int version, sudo_conv_t conversation, bindtextdomain("sudoers", LOCALEDIR); - if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) { - sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); - debug_return_int(-1); - } - /* Initialize the debug subsystem. */ for (cur = settings; (cp = *cur) != NULL; cur++) { if (strncmp(cp, "debug_flags=", sizeof("debug_flags=") - 1) == 0) { diff --git a/plugins/sudoers/pwutil.c b/plugins/sudoers/pwutil.c index 23398c335..354866a20 100644 --- a/plugins/sudoers/pwutil.c +++ b/plugins/sudoers/pwutil.c @@ -133,6 +133,14 @@ sudo_getpwuid(uid_t uid) struct rbnode *node; debug_decl(sudo_getpwuid, SUDOERS_DEBUG_NSS) + if (pwcache_byuid == NULL) { + pwcache_byuid = rbcreate(cmp_pwuid); + if (pwcache_byuid == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + } + key.k.uid = uid; getauthregistry(IDtouser(uid), key.registry); if ((node = rbfind(pwcache_byuid, &key)) != NULL) { @@ -196,6 +204,14 @@ sudo_getpwnam(const char *name) struct rbnode *node; debug_decl(sudo_getpwnam, SUDOERS_DEBUG_NSS) + if (pwcache_byname == NULL) { + pwcache_byname = rbcreate(cmp_pwnam); + if (pwcache_byname == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + } + key.k.name = (char *) name; getauthregistry((char *) name, key.registry); if ((node = rbfind(pwcache_byname, &key)) != NULL) { @@ -262,6 +278,15 @@ sudo_mkpwent(const char *user, uid_t uid, gid_t gid, const char *home, int i; debug_decl(sudo_mkpwent, SUDOERS_DEBUG_NSS) + if (pwcache_byuid == NULL) + pwcache_byuid = rbcreate(cmp_pwuid); + if (pwcache_byname == NULL) + pwcache_byname = rbcreate(cmp_pwnam); + if (pwcache_byuid == NULL || pwcache_byname == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + /* Optional arguments. */ if (home == NULL) home = "/"; @@ -358,21 +383,6 @@ sudo_fakepwnam(const char *user, gid_t gid) debug_return_ptr(sudo_mkpwent(user, uid, gid, NULL, NULL)); } -int -sudo_mkpwcache(void) -{ - debug_decl(sudo_mkpwcache, SUDOERS_DEBUG_NSS) - - if (pwcache_byuid == NULL) - pwcache_byuid = rbcreate(cmp_pwuid); - if (pwcache_byname == NULL) - pwcache_byname = rbcreate(cmp_pwnam); - if (pwcache_byuid == NULL || pwcache_byname == NULL) - debug_return_int(-1); - - debug_return_int(0); -} - void sudo_freepwcache(void) { @@ -441,6 +451,14 @@ sudo_getgrgid(gid_t gid) struct rbnode *node; debug_decl(sudo_getgrgid, SUDOERS_DEBUG_NSS) + if (grcache_bygid == NULL) { + grcache_bygid = rbcreate(cmp_grgid); + if (grcache_bygid == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + } + key.k.gid = gid; getauthregistry(NULL, key.registry); if ((node = rbfind(grcache_bygid, &key)) != NULL) { @@ -498,6 +516,14 @@ sudo_getgrnam(const char *name) struct rbnode *node; debug_decl(sudo_getgrnam, SUDOERS_DEBUG_NSS) + if (grcache_byname == NULL) { + grcache_byname = rbcreate(cmp_grnam); + if (grcache_byname == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + } + key.k.name = (char *) name; getauthregistry(NULL, key.registry); if ((node = rbfind(grcache_byname, &key)) != NULL) { @@ -557,6 +583,15 @@ sudo_fakegrnam(const char *group) int i; debug_decl(sudo_fakegrnam, SUDOERS_DEBUG_NSS) + if (grcache_bygid == NULL) + grcache_bygid = rbcreate(cmp_grgid); + if (grcache_byname == NULL) + grcache_byname = rbcreate(cmp_grnam); + if (grcache_bygid == NULL || grcache_byname == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + name_len = strlen(group); len = sizeof(*gritem) + name_len + 1; @@ -645,23 +680,6 @@ sudo_grlist_delref(struct group_list *grlist) debug_return; } -int -sudo_mkgrcache(void) -{ - debug_decl(sudo_mkgrcache, SUDOERS_DEBUG_NSS) - - if (grcache_bygid == NULL) - grcache_bygid = rbcreate(cmp_grgid); - if (grcache_byname == NULL) - grcache_byname = rbcreate(cmp_grnam); - if (grlist_cache == NULL) - grlist_cache = rbcreate(cmp_grnam); - if (grcache_bygid == NULL || grcache_byname == NULL || grlist_cache == NULL) - debug_return_int(-1); - - debug_return_int(0); -} - void sudo_freegrcache(void) { @@ -690,6 +708,14 @@ sudo_get_grlist(const struct passwd *pw) struct rbnode *node; debug_decl(sudo_get_grlist, SUDOERS_DEBUG_NSS) + if (grlist_cache == NULL) { + grlist_cache = rbcreate(cmp_grnam); + if (grlist_cache == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_ptr(NULL); + } + } + key.k.name = pw->pw_name; getauthregistry(pw->pw_name, key.registry); if ((node = rbfind(grlist_cache, &key)) != NULL) { @@ -739,6 +765,14 @@ sudo_set_grlist(struct passwd *pw, char * const *groups, char * const *gids) struct rbnode *node; debug_decl(sudo_set_grlist, SUDOERS_DEBUG_NSS) + if (grlist_cache == NULL) { + grlist_cache = rbcreate(cmp_grnam); + if (grlist_cache == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_int(-1); + } + } + /* * Cache group db entry if it doesn't already exist */ diff --git a/plugins/sudoers/sudoers.c b/plugins/sudoers/sudoers.c index 6a69bc2bb..5f33c2889 100644 --- a/plugins/sudoers/sudoers.c +++ b/plugins/sudoers/sudoers.c @@ -155,11 +155,6 @@ sudoers_policy_init(void *info, char * const envp[]) bindtextdomain("sudoers", LOCALEDIR); - if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) { - sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); - debug_return_int(-1); - } - /* Register fatal/fatalx callback. */ sudo_fatal_callback_register(sudoers_cleanup); diff --git a/plugins/sudoers/sudoers.h b/plugins/sudoers/sudoers.h index 362b97181..6f41aeb2e 100644 --- a/plugins/sudoers/sudoers.h +++ b/plugins/sudoers/sudoers.h @@ -304,8 +304,6 @@ void sudo_freegrcache(void); void sudo_freepwcache(void); void sudo_grlist_addref(struct group_list *); void sudo_grlist_delref(struct group_list *); -int sudo_mkgrcache(void); -int sudo_mkpwcache(void); void sudo_pw_addref(struct passwd *); void sudo_pw_delref(struct passwd *); int sudo_set_grlist(struct passwd *pw, char * const *groups, char * const *gids); diff --git a/plugins/sudoers/testsudoers.c b/plugins/sudoers/testsudoers.c index bfa97edae..f59c448db 100644 --- a/plugins/sudoers/testsudoers.c +++ b/plugins/sudoers/testsudoers.c @@ -184,8 +184,6 @@ main(int argc, char *argv[]) setgrfile(grfile); if (pwfile) setpwfile(pwfile); - if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) - sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); if (argc < 2) { if (!dflag) diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index fef973904..4dc9257d0 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -209,9 +209,6 @@ main(int argc, char *argv[]) if (argc - optind != 0) usage(1); - if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) - sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); - /* Mock up a fake sudo_user struct. */ user_cmnd = user_base = ""; if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL) @@ -842,8 +839,6 @@ run_command(char *path, char **argv) sudo_fatal(U_("unable to execute %s"), path); break; /* NOTREACHED */ case 0: - sudo_freepwcache(); - sudo_freegrcache(); closefrom(STDERR_FILENO + 1); execv(path, argv); sudo_warn(U_("unable to run %s"), path); @@ -1304,8 +1299,6 @@ visudo_cleanup(void) if (sp->tpath != NULL) (void) unlink(sp->tpath); } - sudo_freepwcache(); - sudo_freegrcache(); } /*