From 0bf424496ff707551a96e09d08d43fa6415d14c9 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 4 Jul 2015 15:27:04 +0000 Subject: Validate input of pw usermod -h and pwusermod -H Push the code that set the password into a separate function to improve readability Add regression tests about pw usermod -h and pw usermod -H --- pw/pw.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 30fb55b..b9bd9d0 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -137,6 +137,7 @@ main(int argc, char *argv[]) relocated = nis = false; memset(&conf, 0, sizeof(conf)); strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath)); + conf.fd = -1; LIST_INIT(&arglist); @@ -280,6 +281,35 @@ main(int argc, char *argv[]) errx(EX_USAGE, "Bad id '%s': %s", optarg, errstr); break; + case 'H': + if (conf.fd != -1) + errx(EX_USAGE, "'-h' and '-H' are mutually " + "exclusive options"); + conf.precrypted = true; + if (strspn(optarg, "0123456789") != strlen(optarg)) + errx(EX_USAGE, "'-H' expects a file descriptor"); + + conf.fd = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL) + errx(EX_USAGE, "Bad file descriptor '%s': %s", + optarg, errstr); + break; + case 'h': + if (conf.fd != -1) + errx(EX_USAGE, "'-h' and '-H' are mutually " + "exclusive options"); + + if (strcmp(optarg, "-") == 0) + conf.fd = '-'; + else if (strspn(optarg, "0123456789") == strlen(optarg)) { + conf.fd = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL) + errx(EX_USAGE, "'-h' expects a " + "file descriptor or '-'"); + } else + errx(EX_USAGE, "'-h' expects a file " + "descriptor or '-'"); + break; case 'o': conf.checkduplicate = true; break; -- cgit v1.2.3 From b1002b3c03d08cb2ba07261fcb5363561004a077 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Thu, 9 Jul 2015 14:14:44 +0000 Subject: Do not try to set password on group if the group is added as a consequence of of creating a user (regression from r285136) Reported by: Fabian Keil --- pw/pw.c | 1 + 1 file changed, 1 insertion(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index b9bd9d0..6769738 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -215,6 +215,7 @@ main(int argc, char *argv[]) if (mode == -1 || which == -1) cmdhelp(mode, which); + conf.which = which; /* * We know which mode we're in and what we're about to do, so now * let's dispatch the remaining command line args in a genric way. -- cgit v1.2.3 From ecf012ac29ba3e82e8b5e497ee1b80629ae5624f Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 11 Jul 2015 17:01:08 +0000 Subject: Move the quiet flag into the configuration structure --- pw/pw.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 6769738..951abc6 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -314,6 +314,9 @@ main(int argc, char *argv[]) case 'o': conf.checkduplicate = true; break; + case 'q': + conf.quiet = true; + break; default: addarg(&arglist, ch, optarg); break; @@ -334,7 +337,7 @@ main(int argc, char *argv[]) * We should immediately look for the -q 'quiet' switch so that we * don't bother with extraneous errors */ - if (getarg(&arglist, 'q') != NULL) + if (conf.quiet) freopen(_PATH_DEVNULL, "w", stderr); /* -- cgit v1.2.3 From cc424f0d3468b75718e420a691cb5e3b238804b6 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 11 Jul 2015 18:09:27 +0000 Subject: Make separate functions to show users and groups --- pw/pw.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 951abc6..56e4103 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -234,6 +234,9 @@ main(int argc, char *argv[]) conf.config = optarg; config = conf.config; break; + case 'F': + conf.force = true; + break; case 'N': conf.dryrun = true; break; @@ -248,6 +251,9 @@ main(int argc, char *argv[]) case 'Y': nis = true; break; + case 'a': + conf.all = true; + break; case 'g': if (which == 0) { /* for user* */ addarg(&arglist, 'g', optarg); -- cgit v1.2.3 From dc338df23912779d9d051efb40fdf73ad6289ea0 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 11 Jul 2015 19:07:47 +0000 Subject: Make a separate groupdel/userdel from the main function --- pw/pw.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 56e4103..112b1f0 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -323,6 +323,9 @@ main(int argc, char *argv[]) case 'q': conf.quiet = true; break; + case 'r': + conf.deletehome = true; + break; default: addarg(&arglist, ch, optarg); break; -- cgit v1.2.3 From 13824e6c6765b7a1d67b0ffaab60aa0612bee252 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 11 Jul 2015 21:09:50 +0000 Subject: check the gecos format early: at the moment the -c option is parsed --- pw/pw.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 112b1f0..3d4fcf2 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -254,6 +254,9 @@ main(int argc, char *argv[]) case 'a': conf.all = true; break; + case 'c': + conf.gecos = pw_checkname(optarg, 1); + break; case 'g': if (which == 0) { /* for user* */ addarg(&arglist, 'g', optarg); -- cgit v1.2.3 From 5c8793faf0c3ceee6ca534d0f69b417a3759f7af Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 12 Jul 2015 00:02:43 +0000 Subject: Make getarg return NULL if args is NULL --- pw/pw.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 3d4fcf2..532d77b 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -582,7 +582,12 @@ cmdhelp(int mode, int which) struct carg * getarg(struct cargs * _args, int ch) { - struct carg *c = LIST_FIRST(_args); + struct carg *c; + + if (_args == NULL) + return (NULL); + + c = LIST_FIRST(_args); while (c != NULL && c->ch != ch) c = LIST_NEXT(c, list); -- cgit v1.2.3 From f6975773c5ebf939f9e3f22b2b600d2c821604bc Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 12 Jul 2015 20:29:51 +0000 Subject: Rework the home directory creation and copy or the skel content to use *at functions This allows to simplify the code a bit for -R by not having to keep modifying path and also prepare the code to improve support -R in userdel While here, add regression tests for the functionality --- pw/pw.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 532d77b..d81cfb0 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -136,6 +136,7 @@ main(int argc, char *argv[]) name = NULL; relocated = nis = false; memset(&conf, 0, sizeof(conf)); + strlcpy(conf.rootdir, "/", sizeof(conf.rootdir)); strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath)); conf.fd = -1; @@ -215,6 +216,9 @@ main(int argc, char *argv[]) if (mode == -1 || which == -1) cmdhelp(mode, which); + conf.rootfd = open(conf.rootdir, O_DIRECTORY|O_CLOEXEC); + if (conf.rootfd == -1) + errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir); conf.which = which; /* * We know which mode we're in and what we're about to do, so now -- cgit v1.2.3 From 1e6401def38868bd9621db64c744d1788b51170d Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Mon, 13 Jul 2015 09:07:38 +0000 Subject: Fix logic of check duplicates that has been inverted --- pw/pw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index d81cfb0..c5427b4 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -139,6 +139,7 @@ main(int argc, char *argv[]) strlcpy(conf.rootdir, "/", sizeof(conf.rootdir)); strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath)); conf.fd = -1; + conf.checkduplicate = false; LIST_INIT(&arglist); @@ -325,7 +326,7 @@ main(int argc, char *argv[]) "descriptor or '-'"); break; case 'o': - conf.checkduplicate = true; + conf.checkduplicate = false; break; case 'q': conf.quiet = true; -- cgit v1.2.3 From a8e235f6926f727fd30a1fc881bda8f39c16ac1b Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Mon, 13 Jul 2015 09:12:05 +0000 Subject: Really fix -o --- pw/pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index c5427b4..5ad2511 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -139,7 +139,7 @@ main(int argc, char *argv[]) strlcpy(conf.rootdir, "/", sizeof(conf.rootdir)); strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath)); conf.fd = -1; - conf.checkduplicate = false; + conf.checkduplicate = true; LIST_INIT(&arglist); -- cgit v1.2.3 From d2d684f5597f30a1cc8efa767281ed1c7c9eff45 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Jul 2015 12:20:57 +0000 Subject: when -n is passed to any pw subcommand it is always expected to be considered as a name so do not try to convert it to an id if it is a numeric value PR: 31933 Reported by: ted@impulse.net Sponsored by: gandi.net --- pw/pw.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 5ad2511..3db427a 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -287,14 +287,7 @@ main(int argc, char *argv[]) errstr); break; case 'n': - if (strspn(optarg, "0123456789") != strlen(optarg)) { - name = optarg; - break; - } - id = strtonum(optarg, 0, LONG_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "Bad id '%s': %s", optarg, - errstr); + name = optarg; break; case 'H': if (conf.fd != -1) -- cgit v1.2.3 From 7fe27302ca1b955d97360f508b462470b593b0db Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Jul 2015 21:10:58 +0000 Subject: Check uid/gid used when creating a user/group are not larger than UID_MAX/GID_MAX PR: 173977 Reported by: nvass@gmx.com --- pw/pw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 3db427a..c1d9cd3 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -269,7 +269,7 @@ main(int argc, char *argv[]) } if (strspn(optarg, "0123456789") != strlen(optarg)) errx(EX_USAGE, "-g expects a number"); - id = strtonum(optarg, 0, LONG_MAX, &errstr); + id = strtonum(optarg, 0, GID_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", optarg, errstr); @@ -281,7 +281,7 @@ main(int argc, char *argv[]) addarg(&arglist, 'u', optarg); break; } - id = strtonum(optarg, 0, LONG_MAX, &errstr); + id = strtonum(optarg, 0, UID_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", optarg, errstr); -- cgit v1.2.3 From 00e5987be5ae3b7c108866ac1dba4403cc4306d5 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 29 Jul 2015 06:22:41 +0000 Subject: Create a strtounum function using the same API as strtonum This function returns uintmax_t Use this function to convert to gid_t/uid_t --- pw/pw.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index c1d9cd3..88c83db 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -199,7 +199,7 @@ main(int argc, char *argv[]) cmdhelp(mode, which); else if (which != -1 && mode != -1) { if (strspn(argv[1], "0123456789") == strlen(argv[1])) { - id = strtonum(argv[1], 0, LONG_MAX, &errstr); + id = strtounum(argv[1], 0, UID_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", argv[1], errstr); @@ -269,7 +269,7 @@ main(int argc, char *argv[]) } if (strspn(optarg, "0123456789") != strlen(optarg)) errx(EX_USAGE, "-g expects a number"); - id = strtonum(optarg, 0, GID_MAX, &errstr); + id = strtounum(optarg, 0, GID_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", optarg, errstr); @@ -281,7 +281,7 @@ main(int argc, char *argv[]) addarg(&arglist, 'u', optarg); break; } - id = strtonum(optarg, 0, UID_MAX, &errstr); + id = strtounum(optarg, 0, UID_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", optarg, errstr); -- cgit v1.2.3 From a25394718fcec2b1422b872e24e54ad5c7ae69af Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 1 Aug 2015 10:25:55 +0000 Subject: Validate expiration days and password days from commmand line and pw.conf --- pw/pw.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 88c83db..bca6715 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -262,6 +262,11 @@ main(int argc, char *argv[]) case 'c': conf.gecos = pw_checkname(optarg, 1); break; + case 'e': + conf.expire_days = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(EX_USAGE, "Invalid expired days: %s", optarg); + break; case 'g': if (which == 0) { /* for user* */ addarg(&arglist, 'g', optarg); @@ -321,6 +326,11 @@ main(int argc, char *argv[]) case 'o': conf.checkduplicate = false; break; + case 'p': + conf.password_days = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(EX_USAGE, "Invalid password days: %s", optarg); + break; case 'q': conf.quiet = true; break; -- cgit v1.2.3 From 5bb254a38fa9e233bcdb24956e1f8ccf2b19182b Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 1 Aug 2015 12:18:48 +0000 Subject: Partial revert of r286152 More work needed on the cli validation --- pw/pw.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index bca6715..88c83db 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -262,11 +262,6 @@ main(int argc, char *argv[]) case 'c': conf.gecos = pw_checkname(optarg, 1); break; - case 'e': - conf.expire_days = strtonum(optarg, 0, INT_MAX, &errstr); - if (errstr) - errx(EX_USAGE, "Invalid expired days: %s", optarg); - break; case 'g': if (which == 0) { /* for user* */ addarg(&arglist, 'g', optarg); @@ -326,11 +321,6 @@ main(int argc, char *argv[]) case 'o': conf.checkduplicate = false; break; - case 'p': - conf.password_days = strtonum(optarg, 0, INT_MAX, &errstr); - if (errstr) - errx(EX_USAGE, "Invalid password days: %s", optarg); - break; case 'q': conf.quiet = true; break; -- cgit v1.2.3 From e773096e9049d4fb494d9ea86a862cd89d62bdfc Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 2 Aug 2015 12:47:50 +0000 Subject: Rewrite parsing subcommands arguments of pw(8) Now each subcommands checks its arguments in a dedicated functions. This helps improving input validation, code readability/maintainability While here: - Add a -y option to pw userdel/usermod so it can maintain NIS servers if nispasswd is not defined in pw.conf(5) - Allow pw -r to remove directory with userdel -r - Fix bug when renaming a user which was not renaming the user name it groups it is a member of. - Only parse pw.conf(5) when needed. --- pw/pw.c | 296 ++++++++-------------------------------------------------------- 1 file changed, 35 insertions(+), 261 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index 88c83db..b13db70 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -37,9 +37,6 @@ static const char rcsid[] = #include #include "pw.h" -#if !defined(_PATH_YP) -#define _PATH_YP "/var/yp/" -#endif const char *Modes[] = { "add", "del", "mod", "show", "next", NULL}; @@ -85,55 +82,39 @@ struct pwf VPWF = vgetgrnam, }; -struct pwconf conf; - -static struct cargs arglist; +static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = { + { /* user */ + pw_user_add, + pw_user_del, + pw_user_mod, + pw_user_show, + pw_user_next, + pw_user_lock, + pw_user_unlock, + }, + { /* group */ + pw_group_add, + pw_group_del, + pw_group_mod, + pw_group_show, + pw_group_next, + } +}; -static int getindex(const char *words[], const char *word); -static void cmdhelp(int mode, int which); +struct pwconf conf; +static int getindex(const char *words[], const char *word); +static void cmdhelp(int mode, int which); int main(int argc, char *argv[]) { - int ch; - int mode = -1; - int which = -1; - long id = -1; - char *config = NULL; + int mode = -1, which = -1, tmp; struct stat st; - const char *errstr; - char arg, *name; + char arg, *arg1; bool relocated, nis; - static const char *opts[W_NUM][M_NUM] = - { - { /* user */ - "R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y", - "R:V:C:qn:u:rY", - "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY", - "R:V:C:qn:u:FPa7", - "R:V:C:q", - "R:V:C:q", - "R:V:C:q" - }, - { /* grp */ - "R:V:C:qn:g:h:H:M:opNPY", - "R:V:C:qn:g:Y", - "R:V:C:qn:d:g:l:h:H:FM:m:NPY", - "R:V:C:qn:g:FPa", - "R:V:C:q" - } - }; - - static int (*funcs[W_NUM]) (int _mode, char *_name, long _id, - struct cargs * _args) = - { /* Request handlers */ - pw_user, - pw_group - }; - - name = NULL; + arg1 = NULL; relocated = nis = false; memset(&conf, 0, sizeof(conf)); strlcpy(conf.rootdir, "/", sizeof(conf.rootdir)); @@ -141,17 +122,13 @@ main(int argc, char *argv[]) conf.fd = -1; conf.checkduplicate = true; - LIST_INIT(&arglist); - - (void)setlocale(LC_ALL, ""); + setlocale(LC_ALL, ""); /* * Break off the first couple of words to determine what exactly * we're being asked to do */ while (argc > 1) { - int tmp; - if (*argv[1] == '-') { /* * Special case, allow pw -V [args] for scripts etc. @@ -197,15 +174,9 @@ main(int argc, char *argv[]) mode = tmp % M_NUM; } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) cmdhelp(mode, which); - else if (which != -1 && mode != -1) { - if (strspn(argv[1], "0123456789") == strlen(argv[1])) { - id = strtounum(argv[1], 0, UID_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "Bad id '%s': %s", - argv[1], errstr); - } else - name = argv[1]; - } else + else if (which != -1 && mode != -1) + arg1 = argv[1]; + else errx(EX_USAGE, "unknown keyword `%s'", argv[1]); ++argv; --argc; @@ -220,193 +191,22 @@ main(int argc, char *argv[]) conf.rootfd = open(conf.rootdir, O_DIRECTORY|O_CLOEXEC); if (conf.rootfd == -1) errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir); - conf.which = which; - /* - * We know which mode we're in and what we're about to do, so now - * let's dispatch the remaining command line args in a genric way. - */ - optarg = NULL; - - while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { - switch (ch) { - case '?': - errx(EX_USAGE, "unknown switch"); - break; - case '7': - conf.v7 = true; - break; - case 'C': - conf.config = optarg; - config = conf.config; - break; - case 'F': - conf.force = true; - break; - case 'N': - conf.dryrun = true; - break; - case 'l': - if (strlen(optarg) >= MAXLOGNAME) - errx(EX_USAGE, "new name too long: %s", optarg); - conf.newname = optarg; - break; - case 'P': - conf.pretty = true; - break; - case 'Y': - nis = true; - break; - case 'a': - conf.all = true; - break; - case 'c': - conf.gecos = pw_checkname(optarg, 1); - break; - case 'g': - if (which == 0) { /* for user* */ - addarg(&arglist, 'g', optarg); - break; - } - if (strspn(optarg, "0123456789") != strlen(optarg)) - errx(EX_USAGE, "-g expects a number"); - id = strtounum(optarg, 0, GID_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "Bad id '%s': %s", optarg, - errstr); - break; - case 'u': - if (strspn(optarg, "0123456789,") != strlen(optarg)) - errx(EX_USAGE, "-u expects a number"); - if (strchr(optarg, ',') != NULL) { - addarg(&arglist, 'u', optarg); - break; - } - id = strtounum(optarg, 0, UID_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "Bad id '%s': %s", optarg, - errstr); - break; - case 'n': - name = optarg; - break; - case 'H': - if (conf.fd != -1) - errx(EX_USAGE, "'-h' and '-H' are mutually " - "exclusive options"); - conf.precrypted = true; - if (strspn(optarg, "0123456789") != strlen(optarg)) - errx(EX_USAGE, "'-H' expects a file descriptor"); - - conf.fd = strtonum(optarg, 0, INT_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "Bad file descriptor '%s': %s", - optarg, errstr); - break; - case 'h': - if (conf.fd != -1) - errx(EX_USAGE, "'-h' and '-H' are mutually " - "exclusive options"); - - if (strcmp(optarg, "-") == 0) - conf.fd = '-'; - else if (strspn(optarg, "0123456789") == strlen(optarg)) { - conf.fd = strtonum(optarg, 0, INT_MAX, &errstr); - if (errstr != NULL) - errx(EX_USAGE, "'-h' expects a " - "file descriptor or '-'"); - } else - errx(EX_USAGE, "'-h' expects a file " - "descriptor or '-'"); - break; - case 'o': - conf.checkduplicate = false; - break; - case 'q': - conf.quiet = true; - break; - case 'r': - conf.deletehome = true; - break; - default: - addarg(&arglist, ch, optarg); - break; - } - optarg = NULL; - } - - if (name != NULL && strlen(name) >= MAXLOGNAME) - errx(EX_USAGE, "name too long: %s", name); - - /* - * Must be root to attempt an update - */ - if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && !conf.dryrun) - errx(EX_NOPERM, "you must be root to run this program"); - /* - * We should immediately look for the -q 'quiet' switch so that we - * don't bother with extraneous errors - */ - if (conf.quiet) - freopen(_PATH_DEVNULL, "w", stderr); - - /* - * Set our base working path if not overridden - */ - - if (config == NULL) { /* Only override config location if -C not specified */ - asprintf(&config, "%s/pw.conf", conf.etcpath); - if (config == NULL) - errx(EX_OSERR, "out of memory"); - } - - /* - * Now, let's do the common initialisation - */ - conf.userconf = read_userconfig(config); - - ch = funcs[which] (mode, name, id, &arglist); - - /* - * If everything went ok, and we've been asked to update - * the NIS maps, then do it now - */ - if (ch == EXIT_SUCCESS && nis) { - pid_t pid; - - fflush(NULL); - if (chdir(_PATH_YP) == -1) - warn("chdir(" _PATH_YP ")"); - else if ((pid = fork()) == -1) - warn("fork()"); - else if (pid == 0) { - /* Is make anywhere else? */ - execlp("/usr/bin/make", "make", (char *)NULL); - _exit(1); - } else { - int i; - waitpid(pid, &i, 0); - if ((i = WEXITSTATUS(i)) != 0) - errx(ch, "make exited with status %d", i); - else - pw_log(conf.userconf, mode, which, "NIS maps updated"); - } - } - return ch; + return (cmdfunc[which][mode](argc, argv, arg1)); } static int getindex(const char *words[], const char *word) { - int i = 0; + int i = 0; while (words[i]) { if (strcmp(words[i], word) == 0) - return i; + return (i); i++; } - return -1; + return (-1); } @@ -456,7 +256,7 @@ cmdhelp(int mode, int which) " Setting defaults:\n" "\t-V etcdir alternate /etc location\n" "\t-R rootir alternate root directory\n" - "\t-D set user defaults\n" + "\t-D set user defaults\n" "\t-b dir default home root dir\n" "\t-e period default expiry period\n" "\t-p period default password change period\n" @@ -476,6 +276,7 @@ cmdhelp(int mode, int which) "\t-n name login name\n" "\t-u uid user id\n" "\t-Y update NIS maps\n" + "\t-y path set NIS passwd file path\n" "\t-r remove home & contents\n", "usage: pw usermod [uid|name] [switches]\n" "\t-V etcdir alternate /etc location\n" @@ -500,6 +301,7 @@ cmdhelp(int mode, int which) "\t-h fd read password on fd\n" "\t-H fd read encrypted password on fd\n" "\t-Y update NIS maps\n" + "\t-y path set NIS passwd file path\n" "\t-N no update\n", "usage: pw usershow [uid|name] [switches]\n" "\t-V etcdir alternate /etc location\n" @@ -576,31 +378,3 @@ cmdhelp(int mode, int which) } exit(EXIT_FAILURE); } - -struct carg * -getarg(struct cargs * _args, int ch) -{ - struct carg *c; - - if (_args == NULL) - return (NULL); - - c = LIST_FIRST(_args); - - while (c != NULL && c->ch != ch) - c = LIST_NEXT(c, list); - return c; -} - -struct carg * -addarg(struct cargs * _args, int ch, char *argstr) -{ - struct carg *ca = malloc(sizeof(struct carg)); - - if (ca == NULL) - errx(EX_OSERR, "out of memory"); - ca->ch = ch; - ca->val = argstr; - LIST_INSERT_HEAD(_args, ca, list); - return ca; -} -- cgit v1.2.3 From 4573132fd44c8bd71211a975ed21e9acbb58ca45 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 2 Aug 2015 13:22:46 +0000 Subject: Cleanup a bit includes --- pw/pw.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'pw/pw.c') diff --git a/pw/pw.c b/pw/pw.c index b13db70..1f42101 100644 --- a/pw/pw.c +++ b/pw/pw.c @@ -32,9 +32,10 @@ static const char rcsid[] = #include #include #include -#include -#include -#include +#include +#include +#include + #include "pw.h" const char *Modes[] = { -- cgit v1.2.3