From d45dc827a8458c6c290af0e996fb519646e34798 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Mon, 9 Dec 2024 13:04:50 +0100 Subject: [PATCH] src/: Report errors in user or groups names more consistently - 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 --- src/chfn.c | 4 +++- src/chsh.c | 4 +++- src/groupadd.c | 5 ++--- src/groupmod.c | 5 ++--- src/grpck.c | 6 ++++-- src/newgrp.c | 11 +++++------ src/newusers.c | 6 ++---- src/passwd.c | 3 ++- src/pwck.c | 4 +++- src/useradd.c | 4 +--- src/usermod.c | 6 +++--- 11 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/chfn.c b/src/chfn.c index 4c96fba28..9c490a6f6 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -11,10 +11,12 @@ #ident "$Id$" +#include #include #include #include #include +#include #include #include @@ -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]; diff --git a/src/chsh.c b/src/chsh.c index 15bfae323..34cb470a0 100644 --- a/src/chsh.c +++ b/src/chsh.c @@ -11,10 +11,12 @@ #ident "$Id$" +#include #include #include #include #include +#include #include #include "chkname.h" @@ -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]; diff --git a/src/groupadd.c b/src/groupadd.c index 9f0eb2e50..55eb15a28 100644 --- a/src/groupadd.c +++ b/src/groupadd.c @@ -12,6 +12,7 @@ #ident "$Id$" #include +#include #include #include #include @@ -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); } diff --git a/src/groupmod.c b/src/groupmod.c index 7342707d0..a54fb73bd 100644 --- a/src/groupmod.c +++ b/src/groupmod.c @@ -12,6 +12,7 @@ #ident "$Id$" #include +#include #include #include #include @@ -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); } diff --git a/src/grpck.c b/src/grpck.c index 6bac2849f..4a96f62ef 100644 --- a/src/grpck.c +++ b/src/grpck.c @@ -10,11 +10,13 @@ #include +#include #include +#include #include #include #include -#include +#include #include "chkname.h" #include "commonio.h" @@ -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)); } /* diff --git a/src/newgrp.c b/src/newgrp.c index f73508663..7fdb3a9b5 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "agetpass.h" @@ -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]; @@ -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]; diff --git a/src/newusers.c b/src/newusers.c index c89c8df75..da574dfae 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -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; } @@ -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; } diff --git a/src/passwd.c b/src/passwd.c index cc79960a5..4dae512a4 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -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]; diff --git a/src/pwck.c b/src/pwck.c index 8e5046282..3cfe39ef7 100644 --- a/src/pwck.c +++ b/src/pwck.c @@ -12,11 +12,13 @@ #ident "$Id$" +#include #include #include #include #include #include +#include #include "chkname.h" #include "commonio.h" @@ -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; } diff --git a/src/useradd.c b/src/useradd.c index 4331aa2f4..628b7f20d 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -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", diff --git a/src/usermod.c b/src/usermod.c index 08116c7d7..3f1e2d137 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -27,6 +27,7 @@ #endif /* USE_PAM */ #endif /* ACCT_TOOLS_SETUID */ #include +#include #include #include #include @@ -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;