/*
- * 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
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);
}
struct passwd *
-ts_getpwent()
+getpwent()
{
static struct passwd pw;
static char pwbuf[LINE_MAX];
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;
}
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;
}
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);
}
struct group *
-ts_getgrent()
+getgrent()
{
static struct group gr;
static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
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);
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;
}
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;
}
fclose(grf);
grf = NULL;
}
- return(gr);
+ return gr;
}