Skip to content

Commit 769bf0a

Browse files
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 <[email protected]>
1 parent 398a1bb commit 769bf0a

File tree

11 files changed

+30
-28
lines changed

11 files changed

+30
-28
lines changed

src/chfn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
#ident "$Id$"
1313

14+
#include <errno.h>
1415
#include <fcntl.h>
1516
#include <pwd.h>
1617
#include <signal.h>
1718
#include <stdio.h>
19+
#include <string.h>
1820
#include <sys/types.h>
1921
#include <getopt.h>
2022

@@ -646,7 +648,7 @@ int main (int argc, char **argv)
646648
*/
647649
if (optind < argc) {
648650
if (!is_valid_user_name (argv[optind])) {
649-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
651+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
650652
fail_exit (E_NOPERM);
651653
}
652654
user = argv[optind];

src/chsh.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
#ident "$Id$"
1313

14+
#include <errno.h>
1415
#include <fcntl.h>
1516
#include <getopt.h>
1617
#include <pwd.h>
1718
#include <stdio.h>
19+
#include <string.h>
1820
#include <sys/types.h>
1921

2022
#include "chkname.h"
@@ -503,7 +505,7 @@ int main (int argc, char **argv)
503505
*/
504506
if (optind < argc) {
505507
if (!is_valid_user_name (argv[optind])) {
506-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
508+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
507509
fail_exit (1);
508510
}
509511
user = argv[optind];

src/groupadd.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ident "$Id$"
1313

1414
#include <ctype.h>
15+
#include <errno.h>
1516
#include <fcntl.h>
1617
#include <getopt.h>
1718
#include <grp.h>
@@ -247,9 +248,7 @@ static void
247248
check_new_name(void)
248249
{
249250
if (!is_valid_group_name(group_name)) {
250-
fprintf(stderr, _("%s: '%s' is not a valid group name\n"),
251-
Prog, group_name);
252-
251+
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
253252
exit(E_BAD_ARG);
254253
}
255254

src/groupmod.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ident "$Id$"
1313

1414
#include <ctype.h>
15+
#include <errno.h>
1516
#include <fcntl.h>
1617
#include <getopt.h>
1718
#include <grp.h>
@@ -381,9 +382,7 @@ check_new_name(void)
381382
}
382383

383384
if (!is_valid_group_name(group_newname)) {
384-
fprintf(stderr,
385-
_("%s: invalid group name '%s'\n"),
386-
Prog, group_newname);
385+
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
387386
exit(E_BAD_ARG);
388387
}
389388

src/grpck.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
#include <config.h>
1212

13+
#include <errno.h>
1314
#include <fcntl.h>
15+
#include <getopt.h>
1416
#include <grp.h>
1517
#include <pwd.h>
1618
#include <stdio.h>
17-
#include <getopt.h>
19+
#include <string.h>
1820

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

568570
/*

src/newgrp.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <grp.h>
1616
#include <pwd.h>
1717
#include <stdio.h>
18+
#include <string.h>
1819
#include <assert.h>
1920

2021
#include "agetpass.h"
@@ -486,9 +487,8 @@ int main (int argc, char **argv)
486487
*/
487488
if ((argc > 0) && (argv[0][0] != '-')) {
488489
if (!is_valid_group_name (argv[0])) {
489-
fprintf (
490-
stderr, _("%s: provided group is not a valid group name\n"),
491-
Prog);
490+
fprintf(stderr, _("%s: group: %s\n"),
491+
Prog, strerror(errno));
492492
goto failure;
493493
}
494494
group = argv[0];
@@ -523,9 +523,8 @@ int main (int argc, char **argv)
523523
goto failure;
524524
} else if (argv[0] != NULL) {
525525
if (!is_valid_group_name (argv[0])) {
526-
fprintf (
527-
stderr, _("%s: provided group is not a valid group name\n"),
528-
Prog);
526+
fprintf(stderr, _("%s: group: %s\n"),
527+
Prog, strerror(errno));
529528
goto failure;
530529
}
531530
group = argv[0];

src/newusers.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
289289

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

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

src/passwd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <pwd.h>
1818
#include <signal.h>
1919
#include <stdio.h>
20+
#include <string.h>
2021
#include <sys/types.h>
2122
#include <time.h>
2223

@@ -913,7 +914,7 @@ main(int argc, char **argv)
913914
myname = xstrdup (pw->pw_name);
914915
if (optind < argc) {
915916
if (!is_valid_user_name (argv[optind])) {
916-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
917+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
917918
fail_exit (E_NOPERM);
918919
}
919920
name = argv[optind];

src/pwck.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
#ident "$Id$"
1414

15+
#include <errno.h>
1516
#include <fcntl.h>
1617
#include <getopt.h>
1718
#include <grp.h>
1819
#include <pwd.h>
1920
#include <stdio.h>
21+
#include <string.h>
2022

2123
#include "chkname.h"
2224
#include "commonio.h"
@@ -465,7 +467,7 @@ static void check_pw_file (int *errors, bool *changed)
465467
}
466468

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

src/useradd.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,9 +1528,7 @@ static void process_flags (int argc, char **argv)
15281528

15291529
user_name = argv[optind];
15301530
if (!is_valid_user_name(user_name)) {
1531-
fprintf(stderr,
1532-
_("%s: invalid user name '%s'\n"),
1533-
Prog, user_name);
1531+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
15341532
#ifdef WITH_AUDIT
15351533
audit_logger (AUDIT_ADD_USER, Prog,
15361534
"adding user",

src/usermod.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#endif /* USE_PAM */
2828
#endif /* ACCT_TOOLS_SETUID */
2929
#include <stdio.h>
30+
#include <string.h>
3031
#include <strings.h>
3132
#include <sys/stat.h>
3233
#include <sys/types.h>
@@ -1111,9 +1112,8 @@ process_flags(int argc, char **argv)
11111112
/*@notreached@*/break;
11121113
case 'l':
11131114
if (!is_valid_user_name(optarg)) {
1114-
fprintf(stderr,
1115-
_("%s: invalid user name '%s'\n"),
1116-
Prog, optarg);
1115+
fprintf(stderr, _("%s: user: %s\n"),
1116+
Prog, strerror(errno));
11171117
exit (E_BAD_ARG);
11181118
}
11191119
lflg = true;

0 commit comments

Comments
 (0)