Skip to content

Commit

Permalink
pw: Don't silently ignore unparsed command line arguments.
Browse files Browse the repository at this point in the history
MFC after:	3 days
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D45097
  • Loading branch information
dag-erling committed May 6, 2024
1 parent a9ea647 commit c861193
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
13 changes: 11 additions & 2 deletions usr.sbin/pw/pw.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = {

struct pwconf conf;

static int mode = -1;
static int which = -1;

static int getindex(const char *words[], const char *word);
static void cmdhelp(int mode, int which);

int
main(int argc, char *argv[])
{
int mode = -1, which = -1, tmp;
int tmp;
struct stat st;
char arg, *arg1;
bool relocated, nis;
Expand Down Expand Up @@ -375,5 +378,11 @@ cmdhelp(int mode, int which)

fprintf(stderr, "%s", help[which][mode]);
}
exit(EXIT_FAILURE);
exit(EX_USAGE);
}

void
usage(void)
{
cmdhelp(mode, which);
}
2 changes: 2 additions & 0 deletions usr.sbin/pw/pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ uintmax_t strtounum(const char * __restrict, uintmax_t, uintmax_t,
const char ** __restrict);

bool grp_has_member(struct group *grp, const char *name);

void usage(void);
31 changes: 26 additions & 5 deletions usr.sbin/pw/pw_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,13 @@ pw_group_next(int argc, char **argv, char *arg1 __unused)
quiet = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -332,9 +336,13 @@ pw_group_show(int argc, char **argv, char *arg1)
all = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -391,9 +399,13 @@ pw_group_del(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -551,9 +563,13 @@ pw_group_add(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -645,9 +661,14 @@ pw_group_mod(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
cnf = get_userconfig(cfg);
Expand Down
42 changes: 35 additions & 7 deletions usr.sbin/pw/pw_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,13 @@ pw_user_next(int argc, char **argv, char *name __unused)
quiet = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -772,9 +776,13 @@ pw_user_show(int argc, char **argv, char *arg1)
v7 = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -855,9 +863,13 @@ pw_user_del(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (quiet)
freopen(_PATH_DEVNULL, "w", stderr);
Expand Down Expand Up @@ -1003,9 +1015,13 @@ pw_user_lock(int argc, char **argv, char *arg1)
/* compatibility */
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

return (pw_userlock(arg1, M_LOCK));
}
Expand All @@ -1022,9 +1038,13 @@ pw_user_unlock(int argc, char **argv, char *arg1)
/* compatibility */
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

return (pw_userlock(arg1, M_UNLOCK));
}
Expand Down Expand Up @@ -1291,9 +1311,13 @@ pw_user_add(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (geteuid() != 0 && ! dryrun)
errx(EX_NOPERM, "you must be root");
Expand Down Expand Up @@ -1604,9 +1628,13 @@ pw_user_mod(int argc, char **argv, char *arg1)
nis = true;
break;
default:
exit(EX_USAGE);
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();

if (geteuid() != 0 && ! dryrun)
errx(EX_NOPERM, "you must be root");
Expand Down

0 comments on commit c861193

Please sign in to comment.