From 7c572a56f392c33e60cf546c4fc40bbd5eb47353 Mon Sep 17 00:00:00 2001 From: "Pedro F. Giffuni" Date: Wed, 13 Aug 2014 03:44:30 +0000 Subject: Minor style tweaks. Obtained from: OpenBSD (CVS rev. 1.7) MFC after: 3 days --- libc/stdlib/strtonum.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 libc/stdlib/strtonum.c diff --git a/libc/stdlib/strtonum.c b/libc/stdlib/strtonum.c new file mode 100644 index 0000000..aa433d8 --- /dev/null +++ b/libc/stdlib/strtonum.c @@ -0,0 +1,68 @@ +/*- + * Copyright (c) 2004 Ted Unangst and Todd Miller + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 + +long long +strtonum(const char *numstr, long long minval, long long maxval, + const char **errstrp) +{ + long long ll = 0; + int error = 0; + char *ep; + struct errval { + const char *errstr; + int err; + } ev[4] = { + { NULL, 0 }, + { "invalid", EINVAL }, + { "too small", ERANGE }, + { "too large", ERANGE }, + }; + + ev[0].err = errno; + errno = 0; + if (minval > maxval) { + error = INVALID; + } else { + ll = strtoll(numstr, &ep, 10); + if (errno == EINVAL || numstr == ep || *ep != '\0') + error = INVALID; + else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) + error = TOOSMALL; + else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) + error = TOOLARGE; + } + if (errstrp != NULL) + *errstrp = ev[error].errstr; + errno = ev[error].err; + if (error) + ll = 0; + + return (ll); +} -- cgit v1.2.3 From 29b28954b6287cdbfb454e89aae39fefae091a30 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 2 Oct 2014 19:53:37 +0000 Subject: - Add a test for bug 191427 where pw(8) will go into an infinite loop Reviewed by: will MFC after: 1 month --- pw/Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pw/Makefile b/pw/Makefile index eae0b87..8c5acf9 100644 --- a/pw/Makefile +++ b/pw/Makefile @@ -11,4 +11,10 @@ WARNS?= 2 DPADD= ${LIBCRYPT} ${LIBUTIL} LDADD= -lcrypt -lutil +.include + +.if ${MK_TESTS} != "no" +SUBDIR+= tests +.endif + .include -- cgit v1.2.3 From 9a00d5bef48e2e002e065c8db0ac7ae0629b5625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 9 Oct 2014 17:39:11 +0000 Subject: Two more places where login_setcryptfmt() defaults to MD5 were missed in r252688. MFC after: 3 days --- pw/pw_user.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index 36c5d9d..efb2901 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -615,7 +615,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); lc = login_getpwclass(pwd); - if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) + if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) warn("setting crypt(3) format"); login_close(lc); pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); @@ -690,7 +690,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) } else { lc = login_getpwclass(pwd); if (lc == NULL || - login_setcryptfmt(lc, "md5", NULL) == NULL) + login_setcryptfmt(lc, "sha512", NULL) == NULL) warn("setting crypt(3) format"); login_close(lc); pwd->pw_passwd = pw_pwcrypt(line); -- cgit v1.2.3 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 From 2beb8074e6d6662d92adb15321b33c20310152bd Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 12 Nov 2014 22:27:53 +0000 Subject: Fix wrong message when using pw -V with a non existent directory Add a regression test about it PR: 194971 Submitted by: Freddy DISSAUX --- pw/pw.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pw/pw.c b/pw/pw.c index b0ac728..ff48db7 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -98,6 +98,7 @@ main(int argc, char *argv[]) int which = -1; char *config = NULL; struct userconf *cnf; + struct stat st; static const char *opts[W_NUM][M_NUM] = { @@ -143,6 +144,13 @@ main(int argc, char *argv[]) if (argv[1][1] == 'V') { optarg = &argv[1][2]; if (*optarg == '\0') { + if (stat(argv[2], &st) != 0) + errx(EX_OSFILE, \ + "no such directory `%s'", + argv[2]); + if (!S_ISDIR(st.st_mode)) + errx(EX_OSFILE, "`%s' not a " + "directory", argv[2]); optarg = argv[2]; ++argv; --argc; -- cgit v1.2.3