2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 22:35:10 +00:00

Refactor code to open passwd/group file and add setpassent/setgroupent.

This makes the "stayopen" semantics match the system passwd/group
functions.  The getpwent/getgrent functions now open the database
if it is not already open.
This commit is contained in:
Todd C. Miller
2022-11-22 08:45:14 -07:00
parent 4d7823e518
commit 1c9c7bd34a
3 changed files with 90 additions and 75 deletions

View File

@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2005, 2008, 2010-2015
* Copyright (c) 2005, 2008, 2010-2015, 2022
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
@@ -67,8 +67,8 @@ testsudoers_setpwfile(const char *file)
testsudoers_endpwent();
}
void
testsudoers_setpwent(void)
static int
open_passwd(int reset)
{
if (pwf == NULL) {
pwf = fopen(pwfile, "r");
@@ -78,10 +78,27 @@ testsudoers_setpwent(void)
pwf = NULL;
}
}
} else {
if (pwf == NULL)
return 0;
} else if (reset) {
rewind(pwf);
}
pw_stayopen = 1;
return 1;
}
int
testsudoers_setpassent(int stayopen)
{
if (!open_passwd(1))
return 0;
pw_stayopen = stayopen;
return 1;
}
void
testsudoers_setpwent(void)
{
testsudoers_setpassent(0);
}
void
@@ -104,6 +121,9 @@ testsudoers_getpwent(void)
char *cp, *colon;
const char *errstr;
if (!open_passwd(0))
return NULL;
next_entry:
if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
return NULL;
@@ -151,16 +171,8 @@ testsudoers_getpwnam(const char *name)
{
struct passwd *pw;
if (pwf == NULL) {
if ((pwf = fopen(pwfile, "r")) == NULL)
return NULL;
if (fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(pwf);
return NULL;
}
} else {
rewind(pwf);
}
if (!open_passwd(1))
return NULL;
while ((pw = testsudoers_getpwent()) != NULL) {
if (strcmp(pw->pw_name, name) == 0)
break;
@@ -177,16 +189,8 @@ testsudoers_getpwuid(uid_t uid)
{
struct passwd *pw;
if (pwf == NULL) {
if ((pwf = fopen(pwfile, "r")) == NULL)
return NULL;
if (fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(pwf);
return NULL;
}
} else {
rewind(pwf);
}
if (!open_passwd(1))
return NULL;
while ((pw = testsudoers_getpwent()) != NULL) {
if (pw->pw_uid == uid)
break;
@@ -203,11 +207,11 @@ testsudoers_setgrfile(const char *file)
{
grfile = file;
if (grf != NULL)
endgrent();
testsudoers_endgrent();
}
void
testsudoers_setgrent(void)
static int
open_group(int reset)
{
if (grf == NULL) {
grf = fopen(grfile, "r");
@@ -217,10 +221,27 @@ testsudoers_setgrent(void)
grf = NULL;
}
}
} else {
if (grf == NULL)
return 0;
} else if (reset) {
rewind(grf);
}
gr_stayopen = 1;
return 1;
}
int
testsudoers_setgroupent(int stayopen)
{
if (!open_group(1))
return 0;
gr_stayopen = stayopen;
return 1;
}
void
testsudoers_setgrent(void)
{
testsudoers_setgroupent(0);
}
void
@@ -244,6 +265,9 @@ testsudoers_getgrent(void)
const char *errstr;
int n;
if (!open_group(0))
return NULL;
next_entry:
if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
return NULL;
@@ -287,16 +311,8 @@ testsudoers_getgrnam(const char *name)
{
struct group *gr;
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
} else {
rewind(grf);
}
if (!open_group(1))
return NULL;
while ((gr = testsudoers_getgrent()) != NULL) {
if (strcmp(gr->gr_name, name) == 0)
break;
@@ -313,16 +329,8 @@ testsudoers_getgrgid(gid_t gid)
{
struct group *gr;
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
} else {
rewind(grf);
}
if (!open_group(1))
return NULL;
while ((gr = testsudoers_getgrent()) != NULL) {
if (gr->gr_gid == gid)
break;