]> granicus.if.org Git - sudo/commitdiff
Eliminate use of setpwent()/endpwent() and setgrent()/endgrent().
authorTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 11 May 2016 13:06:45 +0000 (07:06 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 11 May 2016 13:06:45 +0000 (07:06 -0600)
Sudo never iterates over the passwd or group file.
Rename sudo_set{pw,gr}ent() -> sudo_mk{pw,gr}cache() and
use sudo_free{pw,gr}cache() instead of sudo_end{pw,gr}ent().

plugins/sudoers/iolog.c
plugins/sudoers/logging.c
plugins/sudoers/pwutil.c
plugins/sudoers/sudoers.c
plugins/sudoers/sudoers.h
plugins/sudoers/testsudoers.c
plugins/sudoers/visudo.c

index f08b7a5b07a30ddd2943d90b403dab651281a625..1989fe96975123fbc611f04e03b620744799098d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2015 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2016 Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -601,7 +601,7 @@ sudoers_io_open(unsigned int version, sudo_conv_t conversation,
 
     bindtextdomain("sudoers", LOCALEDIR);
 
-    if (sudo_setpwent() == -1 || sudo_setgrent() == -1) {
+    if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) {
        sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
        debug_return_int(-1);
     }
@@ -687,10 +687,10 @@ done:
     free(tofree);
     if (details.runas_pw)
        sudo_pw_delref(details.runas_pw);
-    sudo_endpwent();
     if (details.runas_gr)
        sudo_gr_delref(details.runas_gr);
-    sudo_endgrent();
+    sudo_freepwcache();
+    sudo_freegrcache();
 
     debug_return_int(rval);
 }
index ca82e49b344707abb4e17b44adce1e91596dfa24..8d4de99111e4700a6ccb1a29ad91b6b2d30c8600 100644 (file)
@@ -623,9 +623,7 @@ send_mail(const char *fmt, ...)
 
     sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, NULL);
 
-    /* Close password, group and other fds so we don't leak. */
-    sudo_endpwent();
-    sudo_endgrent();
+    /* Close fds so we don't leak anything. */
     closefrom(STDERR_FILENO + 1);
 
     if (pipe(pfd) == -1) {
index 5c3ba4bfbba0f31b2d717913a829be5c0aa1fe71..23398c33530af6d67b8dd5d85c81ea45e80e9aff 100644 (file)
@@ -359,9 +359,9 @@ sudo_fakepwnam(const char *user, gid_t gid)
 }
 
 int
-sudo_setpwent(void)
+sudo_mkpwcache(void)
 {
-    debug_decl(sudo_setpwent, SUDOERS_DEBUG_NSS)
+    debug_decl(sudo_mkpwcache, SUDOERS_DEBUG_NSS)
 
     if (pwcache_byuid == NULL)
        pwcache_byuid = rbcreate(cmp_pwuid);
@@ -370,8 +370,6 @@ sudo_setpwent(void)
     if (pwcache_byuid == NULL || pwcache_byname == NULL)
        debug_return_int(-1);
 
-    setpwent();
-
     debug_return_int(0);
 }
 
@@ -392,17 +390,6 @@ sudo_freepwcache(void)
     debug_return;
 }
 
-void
-sudo_endpwent(void)
-{
-    debug_decl(sudo_endpwent, SUDOERS_DEBUG_NSS)
-
-    endpwent();
-    sudo_freepwcache();
-
-    debug_return;
-}
-
 /*
  * Compare by gid.
  */
@@ -659,9 +646,9 @@ sudo_grlist_delref(struct group_list *grlist)
 }
 
 int
-sudo_setgrent(void)
+sudo_mkgrcache(void)
 {
-    debug_decl(sudo_setgrent, SUDOERS_DEBUG_NSS)
+    debug_decl(sudo_mkgrcache, SUDOERS_DEBUG_NSS)
 
     if (grcache_bygid == NULL)
        grcache_bygid = rbcreate(cmp_grgid);
@@ -672,8 +659,6 @@ sudo_setgrent(void)
     if (grcache_bygid == NULL || grcache_byname == NULL || grlist_cache == NULL)
        debug_return_int(-1);
 
-    setgrent();
-
     debug_return_int(0);
 }
 
@@ -698,17 +683,6 @@ sudo_freegrcache(void)
     debug_return;
 }
 
-void
-sudo_endgrent(void)
-{
-    debug_decl(sudo_endgrent, SUDOERS_DEBUG_NSS)
-
-    endgrent();
-    sudo_freegrcache();
-
-    debug_return;
-}
-
 struct group_list *
 sudo_get_grlist(const struct passwd *pw)
 {
index 2e3137682c80848a7815123b5ba6be12227d45f2..6a69bc2bb38803a80884f9dce8fed0403a7a1ace 100644 (file)
@@ -155,7 +155,7 @@ sudoers_policy_init(void *info, char * const envp[])
 
     bindtextdomain("sudoers", LOCALEDIR);
 
-    if (sudo_setpwent() == -1 || sudo_setgrent() == -1) {
+    if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1) {
        sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
        debug_return_int(-1);
     }
@@ -670,9 +670,9 @@ done:
 
     restore_nproc();
 
-    /* Close the password and group files and free up memory. */
-    sudo_endpwent();
-    sudo_endgrent();
+    /* Destroy the password and group caches and free the contents. */
+    sudo_freepwcache();
+    sudo_freegrcache();
 
     sudo_warn_set_locale_func(NULL);
 
@@ -1202,8 +1202,8 @@ sudoers_cleanup(void)
     }
     if (def_group_plugin)
        group_plugin_unload();
-    sudo_endpwent();
-    sudo_endgrent();
+    sudo_freepwcache();
+    sudo_freegrcache();
 
     debug_return;
 }
index da2698c5333c523b0e5412fd7ba28b5f4b04f6cd..362b971812843490e19455f1b8708f180bcb1f25 100644 (file)
@@ -299,16 +299,16 @@ struct passwd *sudo_fakepwnam(const char *, gid_t);
 struct passwd *sudo_mkpwent(const char *user, uid_t uid, gid_t gid, const char *home, const char *shell);
 struct passwd *sudo_getpwnam(const char *);
 struct passwd *sudo_getpwuid(uid_t);
-void sudo_endgrent(void);
-void sudo_endpwent(void);
 void sudo_endspent(void);
+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);
-int sudo_setgrent(void);
-int sudo_setpwent(void);
 void sudo_setspent(void);
 
 /* timestr.c */
index 28f514709f4ccc235be8770d2f31b8769bb01870..bfa97edaeaf0c23819d0acd831ab51b6da8b6ed5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 1998-2005, 2007-2015
+ * Copyright (c) 1996, 1998-2005, 2007-2016
  *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -184,7 +184,7 @@ main(int argc, char *argv[])
        setgrfile(grfile);
     if (pwfile)
        setpwfile(pwfile);
-    if (sudo_setpwent() == -1 || sudo_setgrent() == -1)
+    if (sudo_mkpwcache() == -1 || sudo_mkgrcache() == -1)
        sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
 
     if (argc < 2) {
@@ -336,8 +336,8 @@ main(int argc, char *argv[])
      */
     exitcode = parse_error ? 1 : (match == ALLOW ? 0 : match + 3);
 done:
-    sudo_endpwent();
-    sudo_endgrent();
+    sudo_freepwcache();
+    sudo_freegrcache();
     sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
     exit(exitcode);
 }
index 1e594589a42fe1bfda08e9685d47952701d32807..fef973904f767a5fc87e6dd82c34dcd8ce585858 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 1998-2005, 2007-2015
+ * Copyright (c) 1996, 1998-2005, 2007-2016
  *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -209,7 +209,7 @@ main(int argc, char *argv[])
     if (argc - optind != 0)
        usage(1);
 
-    if (sudo_setpwent() == -1 || sudo_setgrent() == -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. */
@@ -842,8 +842,8 @@ run_command(char *path, char **argv)
            sudo_fatal(U_("unable to execute %s"), path);
            break;      /* NOTREACHED */
        case 0:
-           sudo_endpwent();
-           sudo_endgrent();
+           sudo_freepwcache();
+           sudo_freegrcache();
            closefrom(STDERR_FILENO + 1);
            execv(path, argv);
            sudo_warn(U_("unable to run %s"), path);
@@ -1304,8 +1304,8 @@ visudo_cleanup(void)
        if (sp->tpath != NULL)
            (void) unlink(sp->tpath);
     }
-    sudo_endpwent();
-    sudo_endgrent();
+    sudo_freepwcache();
+    sudo_freegrcache();
 }
 
 /*