Skip to content

Commit f0bc22e

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 8101abc commit f0bc22e

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"
@@ -479,7 +481,7 @@ int main (int argc, char **argv)
479481
*/
480482
if (optind < argc) {
481483
if (!is_valid_user_name (argv[optind])) {
482-
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
484+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
483485
fail_exit (1);
484486
}
485487
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
@@ -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

@@ -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
@@ -1493,9 +1493,7 @@ static void process_flags (int argc, char **argv)
14931493

14941494
user_name = argv[optind];
14951495
if (!is_valid_user_name(user_name)) {
1496-
fprintf(stderr,
1497-
_("%s: invalid user name '%s'\n"),
1498-
Prog, user_name);
1496+
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
14991497
#ifdef WITH_AUDIT
15001498
audit_logger (AUDIT_ADD_USER, Prog,
15011499
"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>
@@ -1123,9 +1124,8 @@ process_flags(int argc, char **argv)
11231124
/*@notreached@*/break;
11241125
case 'l':
11251126
if (!is_valid_user_name(optarg)) {
1126-
fprintf(stderr,
1127-
_("%s: invalid user name '%s'\n"),
1128-
Prog, optarg);
1127+
fprintf(stderr, _("%s: user: %s\n"),
1128+
Prog, strerror(errno));
11291129
exit (E_BAD_ARG);
11301130
}
11311131
lflg = true;

0 commit comments

Comments
 (0)