Skip to content

Commit

Permalink
src/: Report errors in user or groups names more consistently
Browse files Browse the repository at this point in the history
-  Don't print the user name; if it's bad, it might be dangerous.
-  Print the string "user" or "group" before the error message.
-  Print the errno string to be consistent.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Dec 9, 2024
1 parent f3dff43 commit ca3d0f2
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/chfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <getopt.h>

Expand Down Expand Up @@ -646,7 +648,7 @@ int main (int argc, char **argv)
*/
if (optind < argc) {
if (!is_valid_user_name (argv[optind])) {
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
fail_exit (E_NOPERM);
}
user = argv[optind];
Expand Down
4 changes: 3 additions & 1 deletion src/chsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "chkname.h"
Expand Down Expand Up @@ -503,7 +505,7 @@ int main (int argc, char **argv)
*/
if (optind < argc) {
if (!is_valid_user_name (argv[optind])) {
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
fail_exit (1);
}
user = argv[optind];
Expand Down
5 changes: 2 additions & 3 deletions src/groupadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id$"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
Expand Down Expand Up @@ -247,9 +248,7 @@ static void
check_new_name(void)
{
if (!is_valid_group_name(group_name)) {
fprintf(stderr, _("%s: '%s' is not a valid group name\n"),
Prog, group_name);

fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
exit(E_BAD_ARG);
}

Expand Down
5 changes: 2 additions & 3 deletions src/groupmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id$"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
Expand Down Expand Up @@ -381,9 +382,7 @@ check_new_name(void)
}

if (!is_valid_group_name(group_newname)) {
fprintf(stderr,
_("%s: invalid group name '%s'\n"),
Prog, group_newname);
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
exit(E_BAD_ARG);
}

Expand Down
6 changes: 4 additions & 2 deletions src/grpck.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>

#include "chkname.h"
#include "commonio.h"
Expand Down Expand Up @@ -562,7 +564,7 @@ static void check_grp_file (int *errors, bool *changed)
*/
if (!is_valid_group_name (grp->gr_name)) {
*errors += 1;
printf (_("invalid group name '%s'\n"), grp->gr_name);
printf(_("group: %s\n"), strerror(errno));
}

/*
Expand Down
11 changes: 5 additions & 6 deletions src/newgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "agetpass.h"
Expand Down Expand Up @@ -486,9 +487,8 @@ int main (int argc, char **argv)
*/
if ((argc > 0) && (argv[0][0] != '-')) {
if (!is_valid_group_name (argv[0])) {
fprintf (
stderr, _("%s: provided group is not a valid group name\n"),
Prog);
fprintf(stderr, _("%s: group: %s\n"),
Prog, strerror(errno));
goto failure;
}
group = argv[0];
Expand Down Expand Up @@ -523,9 +523,8 @@ int main (int argc, char **argv)
goto failure;
} else if (argv[0] != NULL) {
if (!is_valid_group_name (argv[0])) {
fprintf (
stderr, _("%s: provided group is not a valid group name\n"),
Prog);
fprintf(stderr, _("%s: group: %s\n"),
Prog, strerror(errno));
goto failure;
}
group = argv[0];
Expand Down
6 changes: 2 additions & 4 deletions src/newusers.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)

/* Check if this is a valid group name */
if (!is_valid_group_name (grent.gr_name)) {
fprintf (stderr,
_("%s: invalid group name '%s'\n"),
Prog, grent.gr_name);
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
free (grent.gr_name);
return -1;
}
Expand Down Expand Up @@ -385,7 +383,7 @@ static int add_user (const char *name, uid_t uid, gid_t gid)
struct passwd pwent;

if (!is_valid_user_name(name)) {
fprintf(stderr, _("%s: invalid user name '%s'\n"), Prog, name);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
return -1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

Expand Down Expand Up @@ -913,7 +914,7 @@ main(int argc, char **argv)
myname = xstrdup (pw->pw_name);
if (optind < argc) {
if (!is_valid_user_name (argv[optind])) {
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
fail_exit (E_NOPERM);
}
name = argv[optind];
Expand Down
4 changes: 3 additions & 1 deletion src/pwck.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>

#include "chkname.h"
#include "commonio.h"
Expand Down Expand Up @@ -465,7 +467,7 @@ static void check_pw_file (int *errors, bool *changed)
}

if (!is_valid_user_name(pwd->pw_name)) {
printf(_("invalid user name '%s'\n"), pwd->pw_name);
printf(_("user: %s\n"), strerror(errno));
*errors += 1;
}

Expand Down
4 changes: 1 addition & 3 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,7 @@ static void process_flags (int argc, char **argv)

user_name = argv[optind];
if (!is_valid_user_name(user_name)) {
fprintf(stderr,
_("%s: invalid user name '%s'\n"),
Prog, user_name);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
#ifdef WITH_AUDIT
audit_logger (AUDIT_ADD_USER, Prog,
"adding user",
Expand Down
6 changes: 3 additions & 3 deletions src/usermod.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#endif /* USE_PAM */
#endif /* ACCT_TOOLS_SETUID */
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -1111,9 +1112,8 @@ process_flags(int argc, char **argv)
/*@notreached@*/break;
case 'l':
if (!is_valid_user_name(optarg)) {
fprintf(stderr,
_("%s: invalid user name '%s'\n"),
Prog, optarg);
fprintf(stderr, _("%s: user: %s\n"),
Prog, strerror(errno));
exit (E_BAD_ARG);
}
lflg = true;
Expand Down

0 comments on commit ca3d0f2

Please sign in to comment.