* lib/prototypes.h, configure.in, libmisc/Makefile.am,
libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c, libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c: Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(), xgetgrgid(), and xgetspnam(). They allocate memory for the returned structure and are more robust to successive calls. They are implemented with the libc's getxxyyy_r() functions if available. * libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c, libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c, libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c, src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c, src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c, src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c, src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c, src/groupadd.c, src/chage.c, src/login.c, src/suauth.c, src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the usage of one of the getpwnam(), getpwuid(), getgrnam(), getgrgid(), and getspnam() functions. It was noticed on http://bugs.debian.org/341230 that chfn and chsh use a passwd structure after calling a pam function, which result in using information from the passwd structure requested by pam, not the original one. It is much easier to use the new xget... functions to avoid these issues. I've checked which call to the original get... functions could be left (reducing the scope of the structure if possible), and I've left comments to ease future reviews (e.g. /* local, no need for xgetpwnam */). Note: the getpwent/getgrent calls should probably be checked also. * src/groupdel.c, src/expiry.c: Fix typos in comments. * src/groupmod.c: Re-indent. * libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c, lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup functions (used by the xget... functions) from the <xx>io.c files to the new <xx>mem.c files. This avoid linking some utils against the SELinux library.
This commit is contained in:
@ -205,8 +205,8 @@ static struct group *getgr_nam_gid (const char *grname)
|
||||
|
||||
gid = strtol (grname, &errptr, 10);
|
||||
if (*grname != '\0' && *errptr == '\0' && errno != ERANGE && gid >= 0)
|
||||
return getgrgid (gid);
|
||||
return getgrnam (grname);
|
||||
return xgetgrgid (gid);
|
||||
return xgetgrnam (grname);
|
||||
}
|
||||
|
||||
static long get_number (const char *numstr)
|
||||
@ -252,7 +252,6 @@ static void get_defaults (void)
|
||||
FILE *fp;
|
||||
char buf[1024];
|
||||
char *cp, *ep;
|
||||
const struct group *grp;
|
||||
|
||||
/*
|
||||
* Open the defaults file for reading.
|
||||
@ -279,9 +278,11 @@ static void get_defaults (void)
|
||||
*/
|
||||
if (MATCH (buf, DGROUP)) {
|
||||
unsigned int val = (unsigned int) strtoul (cp, &ep, 10);
|
||||
const struct group *grp;
|
||||
|
||||
if (*cp != '\0' && *ep == '\0') { /* valid number */
|
||||
if (*cp != '\0' && *ep == '\0') { /* valid number */
|
||||
def_group = val;
|
||||
/* local, no need for xgetgrgid */
|
||||
if ((grp = getgrgid (def_group))) {
|
||||
def_gname = xstrdup (grp->gr_name);
|
||||
} else {
|
||||
@ -289,6 +290,7 @@ static void get_defaults (void)
|
||||
_("%s: unknown GID %s\n"),
|
||||
Prog, cp);
|
||||
}
|
||||
/* local, no need for xgetgrnam */
|
||||
} else if ((grp = getgrnam (cp))) {
|
||||
def_group = grp->gr_gid;
|
||||
def_gname = xstrdup (cp);
|
||||
@ -837,7 +839,7 @@ static void find_new_uid (void)
|
||||
#ifdef NO_GETPWENT
|
||||
pw_rewind ();
|
||||
while ((pwd = pw_next ())) {
|
||||
#else /* using getpwent() we can check against NIS users etc. */
|
||||
#else /* using getpwent() we can check against NIS users etc. */
|
||||
setpwent ();
|
||||
while ((pwd = getpwent ())) {
|
||||
#endif
|
||||
@ -881,6 +883,7 @@ static void find_new_uid (void)
|
||||
if (!pwd)
|
||||
break;
|
||||
#else
|
||||
/* local, no need for xgetpwuid */
|
||||
if (!getpwuid (user_id))
|
||||
break;
|
||||
#endif
|
||||
@ -940,7 +943,7 @@ static void find_new_gid ()
|
||||
#ifndef NO_GETGRENT /* glibc does have this, so ... */
|
||||
/* A quick test gets here: if the UID is available
|
||||
* as a GID, go ahead and use it */
|
||||
if (!getgrgid (user_id)) {
|
||||
if (!getgrgid (user_id)) { /* local, no need for xgetgrgid */
|
||||
user_gid = user_id;
|
||||
return;
|
||||
}
|
||||
@ -953,6 +956,7 @@ static void find_new_gid ()
|
||||
if (!grp)
|
||||
break;
|
||||
#else
|
||||
/* local, no need for xgetgrgid */
|
||||
if (!getgrgid (user_gid))
|
||||
break;
|
||||
#endif
|
||||
@ -1497,6 +1501,7 @@ static void usr_update (void)
|
||||
* no user with this UID exists yet (entries for shared UIDs
|
||||
* are left unchanged). --marekm
|
||||
*/
|
||||
/* local, no need for xgetpwuid */
|
||||
if (!getpwuid (user_id)) {
|
||||
faillog_reset (user_id);
|
||||
lastlog_reset (user_id);
|
||||
@ -1596,7 +1601,7 @@ static void create_mail (void)
|
||||
return;
|
||||
}
|
||||
|
||||
gr = getgrnam ("mail");
|
||||
gr = getgrnam ("mail"); /* local, no need for xgetgrnam */
|
||||
if (!gr) {
|
||||
fprintf (stderr,
|
||||
_
|
||||
@ -1622,7 +1627,6 @@ int main (int argc, char **argv)
|
||||
{
|
||||
#ifdef USE_PAM
|
||||
pam_handle_t *pamh = NULL;
|
||||
struct passwd *pampw;
|
||||
int retval;
|
||||
#endif
|
||||
|
||||
@ -1661,13 +1665,17 @@ int main (int argc, char **argv)
|
||||
#ifdef USE_PAM
|
||||
retval = PAM_SUCCESS;
|
||||
|
||||
pampw = getpwuid (getuid ());
|
||||
if (pampw == NULL) {
|
||||
retval = PAM_USER_UNKNOWN;
|
||||
}
|
||||
{
|
||||
struct passwd *pampw;
|
||||
pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
|
||||
if (pampw == NULL) {
|
||||
retval = PAM_USER_UNKNOWN;
|
||||
}
|
||||
|
||||
if (retval == PAM_SUCCESS) {
|
||||
retval = pam_start ("useradd", pampw->pw_name, &conv, &pamh);
|
||||
if (retval == PAM_SUCCESS) {
|
||||
retval = pam_start ("useradd", pampw->pw_name,
|
||||
&conv, &pamh);
|
||||
}
|
||||
}
|
||||
|
||||
if (retval == PAM_SUCCESS) {
|
||||
@ -1705,7 +1713,7 @@ int main (int argc, char **argv)
|
||||
/*
|
||||
* Start with a quick check to see if the user exists.
|
||||
*/
|
||||
if (getpwnam (user_name)) {
|
||||
if (getpwnam (user_name)) { /* local, no need for xgetpwnam */
|
||||
fprintf (stderr, _("%s: user %s exists\n"), Prog, user_name);
|
||||
#ifdef WITH_AUDIT
|
||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding user",
|
||||
@ -1721,7 +1729,7 @@ int main (int argc, char **argv)
|
||||
* --bero
|
||||
*/
|
||||
if (!gflg) {
|
||||
if (getgrnam (user_name)) {
|
||||
if (getgrnam (user_name)) { /* local, no need for xgetgrnam */
|
||||
fprintf (stderr,
|
||||
_
|
||||
("%s: group %s exists - if you want to add this user to that group, use -g.\n"),
|
||||
|
Reference in New Issue
Block a user