the WHATSNEW file is gone
[debian/sudo] / tsgetgrpw.c
index 7f35269a94e54bd92d2d82f6dc1a349cff8b4b30..6f14d3fb5ab928b01dc734432e1906dd34d6e792 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005,2008 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2005, 2008, 2010 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
 #  include <memory.h>
 # endif
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
+#include <fcntl.h>
 #include <limits.h>
 #include <pwd.h>
 #include <grp.h>
@@ -64,41 +64,44 @@ static FILE *grf;
 static const char *grfile = "/etc/group";
 static int gr_stayopen;
 
-void ts_setgrfile __P((const char *));
-void ts_setgrent __P((void));
-void ts_endgrent __P((void));
-struct group *ts_getgrent __P((void));
-struct group *ts_getgrnam __P((const char *));
-struct group *ts_getgrgid __P((gid_t));
+void setgrfile __P((const char *));
+void setgrent __P((void));
+void endgrent __P((void));
+struct group *getgrent __P((void));
+struct group *getgrnam __P((const char *));
+struct group *getgrgid __P((gid_t));
 
-void ts_setpwfile __P((const char *));
-void ts_setpwent __P((void));
-void ts_endpwent __P((void));
-struct passwd *ts_getpwent __P((void));
-struct passwd *ts_getpwnam __P((const char *));
-struct passwd *ts_getpwuid __P((uid_t));
+void setpwfile __P((const char *));
+void setpwent __P((void));
+void endpwent __P((void));
+struct passwd *getpwent __P((void));
+struct passwd *getpwnam __P((const char *));
+struct passwd *getpwuid __P((uid_t));
 
 void
-ts_setpwfile(file)
+setpwfile(file)
     const char *file;
 {
     pwfile = file;
     if (pwf != NULL)
-       ts_endpwent();
+       endpwent();
 }
 
 void
-ts_setpwent()
+setpwent()
 {
-    if (pwf == NULL)
+    if (pwf == NULL) {
        pwf = fopen(pwfile, "r");
-    else
+       if (pwf != NULL)
+           fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(pwf);
+    }
     pw_stayopen = 1;
 }
 
 void
-ts_endpwent()
+endpwent()
 {
     if (pwf != NULL) {
        fclose(pwf);
@@ -108,7 +111,7 @@ ts_endpwent()
 }
 
 struct passwd *
-ts_getpwent()
+getpwent()
 {
     static struct passwd pw;
     static char pwbuf[LINE_MAX];
@@ -151,16 +154,19 @@ ts_getpwent()
 }
 
 struct passwd *
-ts_getpwnam(name)
+getpwnam(name)
     const char *name;
 {
     struct passwd *pw;
 
-    if (pwf != NULL)
+    if (pwf == NULL) {
+       if ((pwf = fopen(pwfile, "r")) == NULL)
+           return(NULL);
+       fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(pwf);
-    else if ((pwf = fopen(pwfile, "r")) == NULL)
-       return(NULL);
-    while ((pw = ts_getpwent()) != NULL) {
+    }
+    while ((pw = getpwent()) != NULL) {
        if (strcmp(pw->pw_name, name) == 0)
            break;
     }
@@ -172,16 +178,19 @@ ts_getpwnam(name)
 }
 
 struct passwd *
-ts_getpwuid(uid)
+getpwuid(uid)
     uid_t uid;
 {
     struct passwd *pw;
 
-    if (pwf != NULL)
+    if (pwf == NULL) {
+       if ((pwf = fopen(pwfile, "r")) == NULL)
+           return(NULL);
+       fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(pwf);
-    else if ((pwf = fopen(pwfile, "r")) == NULL)
-       return(NULL);
-    while ((pw = ts_getpwent()) != NULL) {
+    }
+    while ((pw = getpwent()) != NULL) {
        if (pw->pw_uid == uid)
            break;
     }
@@ -193,26 +202,29 @@ ts_getpwuid(uid)
 }
 
 void
-ts_setgrfile(file)
+setgrfile(file)
     const char *file;
 {
     grfile = file;
     if (grf != NULL)
-       ts_endgrent();
+       endgrent();
 }
 
 void
-ts_setgrent()
+setgrent()
 {
-    if (grf == NULL)
+    if (grf == NULL) {
        grf = fopen(grfile, "r");
-    else
+       if (grf != NULL)
+           fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(grf);
+    }
     gr_stayopen = 1;
 }
 
 void
-ts_endgrent()
+endgrent()
 {
     if (grf != NULL) {
        fclose(grf);
@@ -222,7 +234,7 @@ ts_endgrent()
 }
 
 struct group *
-ts_getgrent()
+getgrent()
 {
     static struct group gr;
     static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
@@ -263,16 +275,19 @@ ts_getgrent()
 }
 
 struct group *
-ts_getgrnam(name)
+getgrnam(name)
     const char *name;
 {
     struct group *gr;
 
-    if (grf != NULL)
+    if (grf == NULL) {
+       if ((grf = fopen(grfile, "r")) == NULL)
+           return(NULL);
+       fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(grf);
-    else if ((grf = fopen(grfile, "r")) == NULL)
-       return(NULL);
-    while ((gr = ts_getgrent()) != NULL) {
+    }
+    while ((gr = getgrent()) != NULL) {
        if (strcmp(gr->gr_name, name) == 0)
            break;
     }
@@ -284,16 +299,19 @@ ts_getgrnam(name)
 }
 
 struct group *
-ts_getgrgid(gid)
+getgrgid(gid)
     gid_t gid;
 {
     struct group *gr;
 
-    if (grf != NULL)
+    if (grf == NULL) {
+       if ((grf = fopen(grfile, "r")) == NULL)
+           return(NULL);
+       fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+    } else {
        rewind(grf);
-    else if ((grf = fopen(grfile, "r")) == NULL)
-       return(NULL);
-    while ((gr = ts_getgrent()) != NULL) {
+    }
+    while ((gr = getgrent()) != NULL) {
        if (gr->gr_gid == gid)
            break;
     }