Skip to content

Commit 8577a42

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 fca682b commit 8577a42

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

@@ -652,7 +654,7 @@ int main (int argc, char **argv)
652654
*/
653655
if (optind < argc) {
654656
if (!is_valid_user_name (argv[optind])) {
655-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
657+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
656658
fail_exit (E_NOPERM);
657659
}
658660
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"
@@ -480,7 +482,7 @@ int main (int argc, char **argv)
480482
*/
481483
if (optind < argc) {
482484
if (!is_valid_user_name (argv[optind])) {
483-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
485+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
484486
fail_exit (1);
485487
}
486488
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"
@@ -563,7 +565,7 @@ static void check_grp_file (bool *errors, bool *changed)
563565
*/
564566
if (!is_valid_group_name (grp->gr_name)) {
565567
*errors = true;
566-
printf (_("invalid group name '%s'\n"), grp->gr_name);
568+
printf(_("group: %s\n"), strerror(errno));
567569
}
568570

569571
/*

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"
@@ -478,9 +479,8 @@ int main (int argc, char **argv)
478479
*/
479480
if ((argc > 0) && (argv[0][0] != '-')) {
480481
if (!is_valid_group_name (argv[0])) {
481-
fprintf (
482-
stderr, _("%s: provided group is not a valid group name\n"),
483-
Prog);
482+
fprintf(stderr, _("%s: group: %s\n"),
483+
Prog, strerror(errno));
484484
goto failure;
485485
}
486486
group = argv[0];
@@ -515,9 +515,8 @@ int main (int argc, char **argv)
515515
goto failure;
516516
} else if (argv[0] != NULL) {
517517
if (!is_valid_group_name (argv[0])) {
518-
fprintf (
519-
stderr, _("%s: provided group is not a valid group name\n"),
520-
Prog);
518+
fprintf(stderr, _("%s: group: %s\n"),
519+
Prog, strerror(errno));
521520
goto failure;
522521
}
523522
group = argv[0];

src/newusers.c

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

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

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

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

@@ -915,7 +916,7 @@ main(int argc, char **argv)
915916
myname = xstrdup (pw->pw_name);
916917
if (optind < argc) {
917918
if (!is_valid_user_name (argv[optind])) {
918-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
919+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
919920
fail_exit (E_NOPERM);
920921
}
921922
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"
@@ -466,7 +468,7 @@ static void check_pw_file (bool *errors, bool *changed)
466468
}
467469

468470
if (!is_valid_user_name(pwd->pw_name)) {
469-
printf(_("invalid user name '%s'\n"), pwd->pw_name);
471+
printf(_("user: %s\n"), strerror(errno));
470472
*errors = true;
471473
}
472474

src/useradd.c

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

14881488
user_name = argv[optind];
14891489
if (!is_valid_user_name(user_name)) {
1490-
fprintf(stderr,
1491-
_("%s: invalid user name '%s'\n"),
1492-
Prog, user_name);
1490+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
14931491
#ifdef WITH_AUDIT
14941492
audit_logger (AUDIT_ADD_USER, Prog,
14951493
"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>
@@ -1120,9 +1121,8 @@ process_flags(int argc, char **argv)
11201121
/*@notreached@*/break;
11211122
case 'l':
11221123
if (!is_valid_user_name(optarg)) {
1123-
fprintf(stderr,
1124-
_("%s: invalid user name '%s'\n"),
1125-
Prog, optarg);
1124+
fprintf(stderr, _("%s: user: %s\n"),
1125+
Prog, strerror(errno));
11261126
exit (E_BAD_ARG);
11271127
}
11281128
lflg = true;

0 commit comments

Comments
 (0)