~serge-hallyn/ubuntu/raring/shadow/shadow-userns

« back to all changes in this revision

Viewing changes to src/passwd.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-29 15:27:11 UTC
  • mfrom: (1.1.10) (49.1.2 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121129152711-yt26ue2rvo3lqy72
Tags: 1:4.1.5.1-1ubuntu2
Revert build-dependency from gettext:any to gettext, now that gettext is
Multi-Arch: foreign.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3
3
 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4
4
 * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5
 
 * Copyright (c) 2007 - 2010, Nicolas François
 
5
 * Copyright (c) 2007 - 2011, Nicolas François
6
6
 * All rights reserved.
7
7
 *
8
8
 * Redistribution and use in source and binary forms, with or without
32
32
 
33
33
#include <config.h>
34
34
 
35
 
#ident "$Id: passwd.c 3233 2010-08-22 19:36:09Z nekral-guest $"
 
35
#ident "$Id: passwd.c 3710 2012-02-13 20:32:00Z nekral-guest $"
36
36
 
37
37
#include <errno.h>
38
38
#include <fcntl.h>
159
159
 */
160
160
static /*@noreturn@*/void usage (int status)
161
161
{
162
 
        (void)
163
 
        fputs (_("Usage: passwd [options] [LOGIN]\n"
164
 
                 "\n"
165
 
                 "Options:\n"
166
 
                 "  -a, --all                     report password status on all accounts\n"
167
 
                 "  -d, --delete                  delete the password for the named account\n"
168
 
                 "  -e, --expire                  force expire the password for the named account\n"
169
 
                 "  -h, --help                    display this help message and exit\n"
170
 
                 "  -k, --keep-tokens             change password only if expired\n"
171
 
                 "  -i, --inactive INACTIVE       set password inactive after expiration\n"
172
 
                 "                                to INACTIVE\n"
173
 
                 "  -l, --lock                    lock the password of the named account\n"
174
 
                 "  -n, --mindays MIN_DAYS        set minimum number of days before password\n"
175
 
                 "                                change to MIN_DAYS\n"
176
 
                 "  -q, --quiet                   quiet mode\n"
177
 
                 "  -r, --repository REPOSITORY   change password in REPOSITORY repository\n"
178
 
                 "  -S, --status                  report password status on the named account\n"
179
 
                 "  -u, --unlock                  unlock the password of the named account\n"
180
 
                 "  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS\n"
181
 
                 "  -x, --maxdays MAX_DAYS        set maximum number of days before password\n"
182
 
                 "                                change to MAX_DAYS\n"
183
 
                 "\n"), (E_SUCCESS != status) ? stderr : stdout);
 
162
        FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
 
163
        (void) fprintf (usageout,
 
164
                        _("Usage: %s [options] [LOGIN]\n"
 
165
                          "\n"
 
166
                          "Options:\n"),
 
167
                        Prog);
 
168
        (void) fputs (_("  -a, --all                     report password status on all accounts\n"), usageout);
 
169
        (void) fputs (_("  -d, --delete                  delete the password for the named account\n"), usageout);
 
170
        (void) fputs (_("  -e, --expire                  force expire the password for the named account\n"), usageout);
 
171
        (void) fputs (_("  -h, --help                    display this help message and exit\n"), usageout);
 
172
        (void) fputs (_("  -k, --keep-tokens             change password only if expired\n"), usageout);
 
173
        (void) fputs (_("  -i, --inactive INACTIVE       set password inactive after expiration\n"
 
174
                        "                                to INACTIVE\n"), usageout);
 
175
        (void) fputs (_("  -l, --lock                    lock the password of the named account\n"), usageout);
 
176
        (void) fputs (_("  -n, --mindays MIN_DAYS        set minimum number of days before password\n"
 
177
                        "                                change to MIN_DAYS\n"), usageout);
 
178
        (void) fputs (_("  -q, --quiet                   quiet mode\n"), usageout);
 
179
        (void) fputs (_("  -r, --repository REPOSITORY   change password in REPOSITORY repository\n"), usageout);
 
180
        (void) fputs (_("  -R, --root CHROOT_DIR         directory to chroot into\n"), usageout);
 
181
        (void) fputs (_("  -S, --status                  report password status on the named account\n"), usageout);
 
182
        (void) fputs (_("  -u, --unlock                  unlock the password of the named account\n"), usageout);
 
183
        (void) fputs (_("  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS\n"), usageout);
 
184
        (void) fputs (_("  -x, --maxdays MAX_DAYS        set maximum number of days before password\n"
 
185
                        "                                change to MAX_DAYS\n"), usageout);
 
186
        (void) fputs ("\n", usageout);
184
187
        exit (status);
185
188
}
186
189
 
407
410
         * Passwords may only be changed after sp_min time is up.
408
411
         */
409
412
        if (sp->sp_lstchg > 0) {
410
 
                time_t last, ok;
411
 
                last = sp->sp_lstchg * SCALE;
412
 
                ok = last + (sp->sp_min > 0 ? sp->sp_min * SCALE : 0);
 
413
                time_t ok;
 
414
                ok = (time_t) sp->sp_lstchg * SCALE;
 
415
                if (sp->sp_min > 0) {
 
416
                        ok += (time_t) sp->sp_min * SCALE;
 
417
                }
413
418
 
414
419
                if (now < ok) {
415
420
                        (void) fprintf (stderr,
458
463
 
459
464
        sp = getspnam (pw->pw_name); /* local, no need for xgetspnam */
460
465
        if (NULL != sp) {
461
 
                (void) printf ("%s %s %s %ld %ld %ld %ld\n",
 
466
                (void) printf ("%s %s %s %lld %lld %lld %lld\n",
462
467
                               pw->pw_name,
463
468
                               pw_status (sp->sp_pwdp),
464
469
                               date_to_str (sp->sp_lstchg * SCALE),
465
 
                               (sp->sp_min * SCALE) / DAY,
466
 
                               (sp->sp_max * SCALE) / DAY,
467
 
                               (sp->sp_warn * SCALE) / DAY,
468
 
                               (sp->sp_inact * SCALE) / DAY);
 
470
                               ((long long)sp->sp_min * SCALE) / DAY,
 
471
                               ((long long)sp->sp_max * SCALE) / DAY,
 
472
                               ((long long)sp->sp_warn * SCALE) / DAY,
 
473
                               ((long long)sp->sp_inact * SCALE) / DAY);
469
474
        } else {
470
475
                (void) printf ("%s %s\n",
471
476
                               pw->pw_name, pw_status (pw->pw_passwd));
771
776
        const struct spwd *sp;  /* Shadow file entry for user   */
772
777
#endif                          /* !USE_PAM */
773
778
 
 
779
        sanitize_env ();
 
780
 
 
781
        /*
 
782
         * Get the program name. The program name is used as a prefix to
 
783
         * most error messages.
 
784
         */
 
785
        Prog = Basename (argv[0]);
 
786
 
774
787
        (void) setlocale (LC_ALL, "");
775
788
        (void) bindtextdomain (PACKAGE, LOCALEDIR);
776
789
        (void) textdomain (PACKAGE);
777
790
 
 
791
        process_root_flag ("-R", argc, argv);
 
792
 
778
793
        /*
779
794
         * The program behaves differently when executed by root than when
780
795
         * executed by a normal user.
781
796
         */
782
797
        amroot = (getuid () == 0);
783
798
 
784
 
        /*
785
 
         * Get the program name. The program name is used as a prefix to
786
 
         * most error messages.
787
 
         */
788
 
        Prog = Basename (argv[0]);
789
 
 
790
 
        sanitize_env ();
791
 
 
792
799
        OPENLOG ("passwd");
793
800
 
794
801
        {
795
802
                /*
796
803
                 * Parse the command line options.
797
804
                 */
798
 
                int option_index = 0;
799
805
                int c;
800
806
                static struct option long_options[] = {
801
 
                        {"all", no_argument, NULL, 'a'},
802
 
                        {"delete", no_argument, NULL, 'd'},
803
 
                        {"expire", no_argument, NULL, 'e'},
804
 
                        {"help", no_argument, NULL, 'h'},
805
 
                        {"inactive", required_argument, NULL, 'i'},
806
 
                        {"keep-tokens", no_argument, NULL, 'k'},
807
 
                        {"lock", no_argument, NULL, 'l'},
808
 
                        {"mindays", required_argument, NULL, 'n'},
809
 
                        {"quiet", no_argument, NULL, 'q'},
810
 
                        {"repository", required_argument, NULL, 'r'},
811
 
                        {"status", no_argument, NULL, 'S'},
812
 
                        {"unlock", no_argument, NULL, 'u'},
813
 
                        {"warndays", required_argument, NULL, 'w'},
814
 
                        {"maxdays", required_argument, NULL, 'x'},
 
807
                        {"all",         no_argument,       NULL, 'a'},
 
808
                        {"delete",      no_argument,       NULL, 'd'},
 
809
                        {"expire",      no_argument,       NULL, 'e'},
 
810
                        {"help",        no_argument,       NULL, 'h'},
 
811
                        {"inactive",    required_argument, NULL, 'i'},
 
812
                        {"keep-tokens", no_argument,       NULL, 'k'},
 
813
                        {"lock",        no_argument,       NULL, 'l'},
 
814
                        {"mindays",     required_argument, NULL, 'n'},
 
815
                        {"quiet",       no_argument,       NULL, 'q'},
 
816
                        {"repository",  required_argument, NULL, 'r'},
 
817
                        {"root",        required_argument, NULL, 'R'},
 
818
                        {"status",      no_argument,       NULL, 'S'},
 
819
                        {"unlock",      no_argument,       NULL, 'u'},
 
820
                        {"warndays",    required_argument, NULL, 'w'},
 
821
                        {"maxdays",     required_argument, NULL, 'x'},
815
822
                        {NULL, 0, NULL, '\0'}
816
823
                };
817
824
 
818
 
                while ((c = getopt_long (argc, argv, "adehi:kln:qr:Suw:x:",
819
 
                                         long_options, &option_index)) != -1) {
 
825
                while ((c = getopt_long (argc, argv, "adehi:kln:qr:R:Suw:x:",
 
826
                                         long_options, NULL)) != -1) {
820
827
                        switch (c) {
821
828
                        case 'a':
822
829
                                aflg = true;
829
836
                                eflg = true;
830
837
                                anyflag = true;
831
838
                                break;
 
839
                        case 'h':
 
840
                                usage (E_SUCCESS);
 
841
                                /*@notreached@*/break;
832
842
                        case 'i':
833
843
                                if (   (getlong (optarg, &inact) == 0)
834
844
                                    || (inact < -1)) {
872
882
                                        exit (E_BAD_ARG);
873
883
                                }
874
884
                                break;
 
885
                        case 'R': /* no-op, handled in process_root_flag () */
 
886
                                break;
875
887
                        case 'S':
876
888
                                Sflg = true;    /* ok for users */
877
889
                                break;
901
913
                                xflg = true;
902
914
                                anyflag = true;
903
915
                                break;
904
 
                        case 'h':
905
 
                                usage (E_SUCCESS);
906
 
                                /*@notreached@*/break;
907
916
                        default:
908
917
                                usage (E_BAD_ARG);
909
918
                        }
1048
1057
         */
1049
1058
        sp = getspnam (name); /* !USE_PAM, no need for xgetspnam */
1050
1059
        if (NULL == sp) {
 
1060
                if (errno == EACCES) {
 
1061
                        (void) fprintf (stderr,
 
1062
                                        _("%s: Permission denied.\n"),
 
1063
                                        Prog);
 
1064
                        exit (E_NOPERM);
 
1065
                }
1051
1066
                sp = pwd_to_spwd (pw);
1052
1067
        }
1053
1068