From a08b5497602c225f5c7ae8e8bbf2ac55615158f9 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Oct 2014 11:20:30 +0000 Subject: When a group is renamed then the group has been invalidated for sure. In that case get the group information using the new name. Add a regression test about this bug PR: 193704 Reported by: az --- pw/pw_group.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pw/pw_group.c b/pw/pw_group.c index 391e477..4ed6ea9 100644 --- a/pw/pw_group.c +++ b/pw/pw_group.c @@ -51,6 +51,7 @@ int pw_group(struct userconf * cnf, int mode, struct cargs * args) { int rc; + struct carg *a_newname = getarg(args, 'l'); struct carg *a_name = getarg(args, 'n'); struct carg *a_gid = getarg(args, 'g'); struct carg *arg; @@ -140,8 +141,8 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) if (a_gid) grp->gr_gid = (gid_t) atoi(a_gid->val); - if ((arg = getarg(args, 'l')) != NULL) - grp->gr_name = pw_checkname((u_char *)arg->val, 0); + if (a_newname != NULL) + grp->gr_name = pw_checkname((u_char *)a_newname->val, 0); } else { if (a_name == NULL) /* Required */ errx(EX_DATAERR, "group name required"); @@ -270,8 +271,10 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) warn("group update"); return EX_IOERR; } + + arg = a_newname != NULL ? a_newname : a_name; /* grp may have been invalidated */ - if ((grp = GETGRNAM(a_name->val)) == NULL) + if ((grp = GETGRNAM(arg->val)) == NULL) errx(EX_SOFTWARE, "group disappeared during update"); pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); -- cgit v1.2.3 From 39f09169dd46aff705fef66203a371be6bfd4814 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Oct 2014 14:19:17 +0000 Subject: Fix a regression in pw usermod -G list The user was perperly adding the to different groups from "list" but was not removed from the other groups it could have belong to. While here add a regression test about this bug PR: 185666 Reported by: sub.mesa@gmail.com MFC after: 1 week --- pw/pw_user.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index efb2901..0b56b81 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -751,7 +751,25 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) */ if (mode == M_ADD || getarg(args, 'G') != NULL) { - int i; + int i, j; + /* First remove the user from all group */ + SETGRENT(); + while ((grp = GETGRENT()) != NULL) { + char group[MAXLOGNAME]; + if (grp->gr_mem == NULL) + continue; + for (i = 0; grp->gr_mem[i] != NULL; i++) { + if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) + continue; + for (j = i; grp->gr_mem[j] != NULL ; j++) + grp->gr_mem[j] = grp->gr_mem[j+1]; + strlcpy(group, grp->gr_name, MAXLOGNAME); + chggrent(group, grp); + } + } + ENDGRENT(); + + /* now add to group where needed */ for (i = 0; cnf->groups[i] != NULL; i++) { grp = GETGRNAM(cnf->groups[i]); grp = gr_add(grp, pwd->pw_name); -- cgit v1.2.3 From 77eacc73737116b101278ac7984c1a85adb55950 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Oct 2014 14:54:04 +0000 Subject: Do not delete the group wheel when bad argument is passed to pw groupdel -g Check that the -g argument is actually a number, if not report an error. This argument is converted without checking with atoi(3) later so without this check it converts any alpha entries into 0 meaning it deletes the group wheel Add a regression test about it PR: 90114 Reported by: bkoenig@cs.tu-berlin.de MFC after: 1 week --- pw/pw_group.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pw/pw_group.c b/pw/pw_group.c index 4ed6ea9..b20ce88 100644 --- a/pw/pw_group.c +++ b/pw/pw_group.c @@ -67,6 +67,11 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) NULL }; + if (a_gid != NULL) { + if (strspn(a_gid->val, "0123456789") != strlen(a_gid->val)) + errx(EX_USAGE, "-g expects a number"); + } + if (mode == M_LOCK || mode == M_UNLOCK) errx(EX_USAGE, "'lock' command is not available for groups"); -- cgit v1.2.3 From 87189f48bb3ff275026600ac63f7f684d92a7f3a Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Oct 2014 15:46:22 +0000 Subject: Ensure pw userdel -u do not try to remove root Check the uid passed is actually a number as early as possible MFC after: 1 week --- pw/pw_user.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pw/pw_user.c b/pw/pw_user.c index 0b56b81..483148a 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -321,6 +321,9 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) (a_uid = a_name)->ch = 'u'; a_name = NULL; } + } else { + if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val)) + errx(EX_USAGE, "-u expects a number"); } /* -- cgit v1.2.3 From 8c4532e1189f5ef981aff455332acbfbb082cfa9 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Oct 2014 16:27:29 +0000 Subject: Fix renaming a group via the gr_copy function Add a regression test to pw(8) because the bug was discovered via using: pw groupmod PR: 187189 Reported by: mcdouga9@egr.msu.edu Tested by: mcdouga9@egr.msu.edu Patch by: Marc de la Gueronniere --- libutil/gr_util.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libutil/gr_util.c b/libutil/gr_util.c index 6f74507..465efd9 100644 --- a/libutil/gr_util.c +++ b/libutil/gr_util.c @@ -170,14 +170,21 @@ gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr) size_t len; int eof, readlen; - sgr = gr; + if (old_gr == NULL && gr == NULL) + return(-1); + + sgr = old_gr; + /* deleting a group */ if (gr == NULL) { line = NULL; - if (old_gr == NULL) + } else { + if ((line = gr_make(gr)) == NULL) return (-1); - sgr = old_gr; - } else if ((line = gr_make(gr)) == NULL) - return (-1); + } + + /* adding a group */ + if (sgr == NULL) + sgr = gr; eof = 0; len = 0; -- cgit v1.2.3