Skip to content

Commit dcf74a5

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 4fbb7e9 commit dcf74a5

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

@@ -651,7 +653,7 @@ int main (int argc, char **argv)
651653
*/
652654
if (optind < argc) {
653655
if (!is_valid_user_name (argv[optind])) {
654-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
656+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
655657
fail_exit (E_NOPERM);
656658
}
657659
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>
@@ -256,9 +257,7 @@ static void
256257
check_new_name(void)
257258
{
258259
if (!is_valid_group_name(group_name)) {
259-
fprintf(stderr, _("%s: '%s' is not a valid group name\n"),
260-
Prog, group_name);
261-
260+
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
262261
fail_exit (E_BAD_ARG);
263262
}
264263

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 (bool *errors, bool *changed)
562564
*/
563565
if (!is_valid_group_name (grp->gr_name)) {
564566
*errors = true;
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
@@ -14,6 +14,7 @@
1414
#include <grp.h>
1515
#include <pwd.h>
1616
#include <stdio.h>
17+
#include <string.h>
1718
#include <sys/types.h>
1819

1920
#include "agetpass.h"
@@ -477,9 +478,8 @@ int main (int argc, char **argv)
477478
*/
478479
if ((argc > 0) && (argv[0][0] != '-')) {
479480
if (!is_valid_group_name (argv[0])) {
480-
fprintf (
481-
stderr, _("%s: provided group is not a valid group name\n"),
482-
Prog);
481+
fprintf(stderr, _("%s: group: %s\n"),
482+
Prog, strerror(errno));
483483
goto failure;
484484
}
485485
group = argv[0];
@@ -514,9 +514,8 @@ int main (int argc, char **argv)
514514
goto failure;
515515
} else if (argv[0] != NULL) {
516516
if (!is_valid_group_name (argv[0])) {
517-
fprintf (
518-
stderr, _("%s: provided group is not a valid group name\n"),
519-
Prog);
517+
fprintf(stderr, _("%s: group: %s\n"),
518+
Prog, strerror(errno));
520519
goto failure;
521520
}
522521
group = argv[0];

src/newusers.c

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

292292
/* Check if this is a valid group name */
293293
if (!is_valid_group_name (grent.gr_name)) {
294-
fprintf (stderr,
295-
_("%s: invalid group name '%s'\n"),
296-
Prog, grent.gr_name);
294+
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
297295
free (grent.gr_name);
298296
return -1;
299297
}
@@ -387,7 +385,7 @@ static int add_user (const char *name, uid_t uid, gid_t gid)
387385
struct passwd pwent;
388386

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

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

@@ -914,7 +915,7 @@ main(int argc, char **argv)
914915
myname = xstrdup (pw->pw_name);
915916
if (optind < argc) {
916917
if (!is_valid_user_name (argv[optind])) {
917-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
918+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
918919
fail_exit (E_NOPERM);
919920
}
920921
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 (bool *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 = true;
470472
}
471473

src/useradd.c

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

14921492
user_name = argv[optind];
14931493
if (!is_valid_user_name(user_name)) {
1494-
fprintf(stderr,
1495-
_("%s: invalid user name '%s'\n"),
1496-
Prog, user_name);
1494+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
14971495
#ifdef WITH_AUDIT
14981496
audit_logger (AUDIT_ADD_USER, Prog,
14991497
"add-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>
@@ -1122,9 +1123,8 @@ process_flags(int argc, char **argv)
11221123
/*@notreached@*/break;
11231124
case 'l':
11241125
if (!is_valid_user_name(optarg)) {
1125-
fprintf(stderr,
1126-
_("%s: invalid user name '%s'\n"),
1127-
Prog, optarg);
1126+
fprintf(stderr, _("%s: user: %s\n"),
1127+
Prog, strerror(errno));
11281128
exit (E_BAD_ARG);
11291129
}
11301130
lflg = true;

0 commit comments

Comments
 (0)