update pam config to use common-session
[debian/sudo] / tsgetgrpw.c
index 7f35269a94e54bd92d2d82f6dc1a349cff8b4b30..5b38298c54348a898604940c1348c7ad0d62207f 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>
 
+#include "tsgetgrpw.h"
 #include "sudo.h"
 
 #ifndef LINE_MAX
@@ -64,41 +63,30 @@ 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 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
-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 +96,7 @@ ts_endpwent()
 }
 
 struct passwd *
-ts_getpwent()
+getpwent()
 {
     static struct passwd pw;
     static char pwbuf[LINE_MAX];
@@ -116,51 +104,54 @@ ts_getpwent()
     char *cp, *colon;
 
     if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
-       return(NULL);
+       return NULL;
 
     zero_bytes(&pw, sizeof(pw));
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_name = cp;
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_passwd = cp;
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_uid = atoi(cp);
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_gid = atoi(cp);
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_gecos = cp;
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     pw.pw_dir = cp;
     pw.pw_shell = colon;
     len = strlen(colon);
     if (len > 0 && colon[len - 1] == '\n')
        colon[len - 1] = '\0';
-    return(&pw);
+    return &pw;
 }
 
 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;
     }
@@ -168,20 +159,23 @@ ts_getpwnam(name)
        fclose(pwf);
        pwf = NULL;
     }
-    return(pw);
+    return pw;
 }
 
 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;
     }
@@ -189,30 +183,33 @@ ts_getpwuid(uid)
        fclose(pwf);
        pwf = NULL;
     }
-    return(pw);
+    return pw;
 }
 
 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 +219,7 @@ ts_endgrent()
 }
 
 struct group *
-ts_getgrent()
+getgrent()
 {
     static struct group gr;
     static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
@@ -231,19 +228,19 @@ ts_getgrent()
     int n;
 
     if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
-       return(NULL);
+       return NULL;
 
     zero_bytes(&gr, sizeof(gr));
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     gr.gr_name = cp;
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     gr.gr_passwd = cp;
     if ((colon = strchr(cp = colon, ':')) == NULL)
-       return(NULL);
+       return NULL;
     *colon++ = '\0';
     gr.gr_gid = atoi(cp);
     len = strlen(colon);
@@ -259,20 +256,23 @@ ts_getgrent()
        gr.gr_mem[n++] = NULL;
     } else
        gr.gr_mem = NULL;
-    return(&gr);
+    return &gr;
 }
 
 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;
     }
@@ -280,20 +280,23 @@ ts_getgrnam(name)
        fclose(grf);
        grf = NULL;
     }
-    return(gr);
+    return gr;
 }
 
 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;
     }
@@ -301,5 +304,5 @@ ts_getgrgid(gid)
        fclose(grf);
        grf = NULL;
     }
-    return(gr);
+    return gr;
 }