2 * Copyright 1991 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #include <sys/types.h>
54 #include "prototypes.h"
63 * for E_GRP_UPDATE and E_NOSPACE (not used yet), other update requests
64 * will be implemented (as documented in the Solaris 2.x man page).
66 #define E_SUCCESS 0 /* success */
67 #define E_PW_UPDATE 1 /* can't update password file */
68 #define E_USAGE 2 /* invalid command syntax */
69 #define E_BAD_ARG 3 /* invalid argument to option */
70 #define E_UID_IN_USE 4 /* UID already in use (and no -o) */
71 /* #define E_BAD_PWFILE 5 passwd file contains errors */
72 #define E_NOTFOUND 6 /* specified user/group doesn't exist */
73 #define E_USER_BUSY 8 /* user to modify is logged in */
74 #define E_NAME_IN_USE 9 /* username already in use */
75 #define E_GRP_UPDATE 10 /* can't update group file */
76 /* #define E_NOSPACE 11 insufficient space to move home dir */
77 #define E_HOMEDIR 12 /* unable to complete home dir move */
78 #define VALID(s) (strcspn (s, ":\n") == strlen (s))
82 static char *user_name;
83 static char *user_newname;
84 static char *user_pass;
86 static uid_t user_newid;
87 static gid_t user_gid;
88 static gid_t user_newgid;
89 static char *user_comment;
90 static char *user_home;
91 static char *user_newhome;
92 static char *user_shell;
93 static long user_expire;
94 static long user_inactive;
95 static long sys_ngroups;
96 static char **user_groups; /* NULL-terminated list */
99 static char *user_newcomment; /* Audit */
100 static char *user_newshell; /* Audit */
101 static long user_newexpire; /* Audit */
102 static long user_newinactive; /* Audit */
108 aflg = 0, /* append to existing secondary group set */
109 cflg = 0, /* new comment (GECOS) field */
110 dflg = 0, /* new home directory */
111 eflg = 0, /* days since 1970-01-01 when account becomes expired */
112 fflg = 0, /* days until account with expired password is locked */
113 gflg = 0, /* new primary group ID */
114 Gflg = 0, /* new secondary group set */
115 Lflg = 0, /* lock the password */
116 lflg = 0, /* new user name */
117 mflg = 0, /* create user's home directory if it doesn't exist */
118 oflg = 0, /* permit non-unique user ID to be specified with -u */
119 pflg = 0, /* new encrypted password */
120 sflg = 0, /* new shell program */
121 uflg = 0, /* specify new user ID */
122 Uflg = 0; /* unlock the password */
124 static int is_shadow_pwd;
127 static int is_shadow_grp;
130 static int pw_locked = 0;
131 static int spw_locked = 0;
132 static int gr_locked = 0;
134 static int sgr_locked = 0;
138 /* local function prototypes */
139 static int get_groups (char *);
140 static void usage (void);
141 static void new_pwent (struct passwd *);
143 static void new_spent (struct spwd *);
144 static void fail_exit (int);
145 static void update_group (void);
148 static void update_gshadow (void);
150 static void grp_update (void);
152 static long get_number (const char *);
153 static uid_t get_id (const char *);
154 static void process_flags (int, char **);
155 static void close_files (void);
156 static void open_files (void);
157 static void usr_update (void);
158 static void move_home (void);
159 static void update_files (void);
161 #ifndef NO_MOVE_MAILBOX
162 static void move_mailbox (void);
166 * Had to move this over from useradd.c since we have groups named
167 * "56k-family"... ergh.
170 static struct group *getgr_nam_gid (const char *grname)
175 val = strtol (grname, &errptr, 10);
176 if (*grname != '\0' && *errptr == '\0' && errno != ERANGE && val >= 0)
177 return getgrgid (val);
178 return getgrnam (grname);
182 * get_groups - convert a list of group names to an array of group IDs
184 * get_groups() takes a comma-separated list of group names and
185 * converts it to a NULL-terminated array. Any unknown group names are
186 * reported as errors.
188 static int get_groups (char *list)
191 const struct group *grp;
196 * Initialize the list to be empty
198 user_groups[0] = (char *) 0;
204 * So long as there is some data to be converted, strip off each
205 * name and look it up. A mix of numerical and string values for
206 * group identifiers is permitted.
210 * Strip off a single name from the list
212 if ((cp = strchr (list, ',')))
216 * Names starting with digits are treated as numerical GID
217 * values, otherwise the string is looked up as is.
219 grp = getgr_nam_gid (list);
222 * There must be a match, either by GID value or by
226 fprintf (stderr, _("%s: unknown group %s\n"),
233 * If the group doesn't exist, don't dump core. Instead,
234 * try the next one. --marekm
241 * Don't add this group if they are an NIS group. Tell the
242 * user to go to the server for this group.
246 _("%s: group '%s' is a NIS group.\n"),
252 if (ngroups == sys_ngroups) {
255 ("%s: too many groups specified (max %d).\n"),
261 * Add the group name to the user's list of groups.
263 user_groups[ngroups++] = xstrdup (grp->gr_name);
266 user_groups[ngroups] = (char *) 0;
269 * Any errors in finding group names are fatal
278 * usage - display usage message and exit
280 static void usage (void)
282 fprintf (stderr, _("Usage: usermod [options] LOGIN\n"
285 " -c, --comment COMMENT new value of the GECOS field\n"
286 " -d, --home HOME_DIR new home directory for the user account\n"
287 " -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE\n"
288 " -f, --inactive INACTIVE set password inactive after expiration\n"
290 " -g, --gid GROUP force use GROUP as new primary group\n"
291 " -G, --groups GROUPS new list of supplementary GROUPS\n"
292 " -a, --append append the user to the supplemental GROUPS\n"
293 " mentioned by the -G option without removing\n"
294 " him/her from other groups\n"
295 " -h, --help display this help message and exit\n"
296 " -l, --login NEW_LOGIN new value of the login name\n"
297 " -L, --lock lock the user account\n"
298 " -m, --move-home move contents of the home directory to the new\n"
299 " location (use only with -d)\n"
300 " -o, --non-unique allow using duplicate (non-unique) UID\n"
301 " -p, --password PASSWORD use encrypted password for the new password\n"
302 " -s, --shell SHELL new login shell for the user account\n"
303 " -u, --uid UID new UID for the user account\n"
304 " -U, --unlock unlock the user account\n"
310 * update encrypted password string (for both shadow and non-shadow
313 static char *new_pw_passwd (char *pw_pass, const char *pw_name)
315 if (Lflg && pw_pass[0] != '!') {
316 char *buf = xmalloc (strlen (pw_pass) + 2);
319 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating passwd",
320 user_newname, user_newid, 0);
322 SYSLOG ((LOG_INFO, "lock user `%s' password", pw_name));
324 strcat (buf, pw_pass);
326 } else if (Uflg && pw_pass[0] == '!') {
330 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating password",
331 user_newname, user_newid, 0);
333 SYSLOG ((LOG_INFO, "unlock user `%s' password", pw_name));
341 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
342 user_newname, user_newid, 1);
344 SYSLOG ((LOG_INFO, "change user `%s' password", pw_name));
345 pw_pass = xstrdup (user_pass);
351 * new_pwent - initialize the values in a password file entry
353 * new_pwent() takes all of the values that have been entered and fills
354 * in a (struct passwd) with them.
356 static void new_pwent (struct passwd *pwent)
360 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing name",
361 user_newname, user_newid, 1);
363 SYSLOG ((LOG_INFO, "change user name `%s' to `%s'",
364 pwent->pw_name, user_newname));
365 pwent->pw_name = xstrdup (user_newname);
369 new_pw_passwd (pwent->pw_passwd, pwent->pw_name);
373 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing uid",
374 user_newname, user_newid, 1);
377 "change user `%s' UID from `%d' to `%d'",
378 pwent->pw_name, pwent->pw_uid, user_newid));
379 pwent->pw_uid = user_newid;
383 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
384 "changing primary group", user_newname,
388 "change user `%s' GID from `%d' to `%d'",
389 pwent->pw_name, pwent->pw_gid, user_newgid));
390 pwent->pw_gid = user_newgid;
394 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing comment",
395 user_newname, user_newid, 1);
396 pwent->pw_gecos = user_newcomment;
398 pwent->pw_gecos = user_comment;
404 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
405 "changing home directory", user_newname,
409 "change user `%s' home from `%s' to `%s'",
410 pwent->pw_name, pwent->pw_dir, user_newhome));
411 pwent->pw_dir = user_newhome;
415 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing user shell",
416 user_newname, user_newid, 1);
417 SYSLOG ((LOG_INFO, "change user `%s' shell from `%s' to `%s'",
418 pwent->pw_name, pwent->pw_shell, user_newshell));
419 pwent->pw_shell = user_newshell;
422 "change user `%s' shell from `%s' to `%s'",
423 pwent->pw_name, pwent->pw_shell, user_shell));
424 pwent->pw_shell = user_shell;
430 * new_spent - initialize the values in a shadow password file entry
432 * new_spent() takes all of the values that have been entered and fills
433 * in a (struct spwd) with them.
435 static void new_spent (struct spwd *spent)
438 spent->sp_namp = xstrdup (user_newname);
442 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
443 "changing inactive days", user_newname,
446 "change user `%s' inactive from `%ld' to `%ld'",
447 spent->sp_namp, spent->sp_inact, user_newinactive));
448 spent->sp_inact = user_newinactive;
452 "change user `%s' inactive from `%ld' to `%ld'",
453 spent->sp_namp, spent->sp_inact, user_inactive));
454 spent->sp_inact = user_inactive;
458 /* XXX - dates might be better than numbers of days. --marekm */
463 char new_exp[16], old_exp[16];
465 if (user_newexpire == -1)
468 exp_t = user_newexpire * DAY;
469 exp_tm = gmtime (&exp_t);
471 strftime (new_exp, sizeof (new_exp), "%Y-%m-%d",
474 memset (new_exp, 0, sizeof (new_exp));
475 snprintf (new_exp, sizeof (new_exp) - 1,
477 exp_tm->tm_year + 1900,
478 exp_tm->tm_mon + 1, exp_tm->tm_mday);
482 if (user_expire == -1)
485 exp_t = user_expire * DAY;
486 exp_tm = gmtime (&exp_t);
488 strftime (old_exp, sizeof (old_exp), "%Y-%m-%d",
491 memset (old_exp, 0, sizeof (old_exp));
492 snprintf (old_exp, sizeof (old_exp) - 1,
494 exp_tm->tm_year + 1900,
495 exp_tm->tm_mon + 1, exp_tm->tm_mday);
498 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
499 "changing expiration date", user_newname,
504 "change user `%s' expiration from `%ld' to `%ld'",
505 spent->sp_namp, spent->sp_expire, user_newexpire));
506 spent->sp_expire = user_newexpire;
509 "change user `%s' expiration from `%ld' to `%ld'",
510 spent->sp_namp, spent->sp_expire, user_expire));
511 spent->sp_expire = user_expire;
514 spent->sp_pwdp = new_pw_passwd (spent->sp_pwdp, spent->sp_namp);
516 spent->sp_lstchg = time ((time_t *) 0) / SCALE;
520 * fail_exit - exit with an error code after unlocking files
522 static void fail_exit (int code)
536 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "modifying account",
543 static void update_group (void)
548 const struct group *grp;
554 * Scan through the entire group file looking for the groups that
555 * the user is a member of.
557 while ((grp = gr_next ())) {
559 * See if the user specified this group as one of their
562 was_member = is_on_list (grp->gr_mem, user_name);
563 is_member = Gflg && is_on_list (user_groups, grp->gr_name);
565 if (!was_member && !is_member)
568 ngrp = __gr_dup (grp);
571 _("%s: Out of memory. Cannot update the group database.\n"),
573 fail_exit (E_GRP_UPDATE);
576 if (was_member && (!Gflg || is_member)) {
578 ngrp->gr_mem = del_list (ngrp->gr_mem,
580 ngrp->gr_mem = add_list (ngrp->gr_mem,
584 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
585 "changing group member",
586 user_newname, -1, 1);
589 "change `%s' to `%s' in group `%s'",
590 user_name, user_newname,
593 } else if (was_member && !aflg && Gflg && !is_member) {
594 ngrp->gr_mem = del_list (ngrp->gr_mem, user_name);
597 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
598 "removing group member", user_name, -1,
601 SYSLOG ((LOG_INFO, "delete `%s' from group `%s'",
602 user_name, ngrp->gr_name));
603 } else if (!was_member && Gflg && is_member) {
604 ngrp->gr_mem = add_list (ngrp->gr_mem,
605 lflg ? user_newname :
609 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
610 "adding user to group", user_name, -1, 1);
612 SYSLOG ((LOG_INFO, "add `%s' to group `%s'",
613 lflg ? user_newname : user_name,
620 if (!gr_update (ngrp)) {
622 _("%s: error adding new group entry\n"), Prog);
623 SYSLOG ((LOG_ERR, "error adding new group entry"));
624 fail_exit (E_GRP_UPDATE);
630 static void update_gshadow (void)
636 const struct sgrp *sgrp;
642 * Scan through the entire shadow group file looking for the groups
643 * that the user is a member of.
645 while ((sgrp = sgr_next ())) {
648 * See if the user was a member of this group
650 was_member = is_on_list (sgrp->sg_mem, user_name);
653 * See if the user was an administrator of this group
655 was_admin = is_on_list (sgrp->sg_adm, user_name);
658 * See if the user specified this group as one of their
661 is_member = Gflg && is_on_list (user_groups, sgrp->sg_name);
663 if (!was_member && !was_admin && !is_member)
666 nsgrp = __sgr_dup (sgrp);
669 _("%s: Out of memory. Cannot update the shadow group database.\n"),
671 fail_exit (E_GRP_UPDATE);
674 if (was_admin && lflg) {
675 nsgrp->sg_adm = del_list (nsgrp->sg_adm, user_name);
676 nsgrp->sg_adm = add_list (nsgrp->sg_adm, user_newname);
679 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
680 "changing admin name in shadow group",
684 "change admin `%s' to `%s' in shadow group `%s'",
685 user_name, user_newname, nsgrp->sg_name));
687 if (was_member && (!Gflg || is_member)) {
689 nsgrp->sg_mem = del_list (nsgrp->sg_mem,
691 nsgrp->sg_mem = add_list (nsgrp->sg_mem,
695 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
696 "changing member in shadow group",
700 "change `%s' to `%s' in shadow group `%s'",
701 user_name, user_newname,
704 } else if (was_member && !aflg && Gflg && !is_member) {
705 nsgrp->sg_mem = del_list (nsgrp->sg_mem, user_name);
708 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
709 "removing user from shadow group",
713 "delete `%s' from shadow group `%s'",
714 user_name, nsgrp->sg_name));
715 } else if (!was_member && Gflg && is_member) {
716 nsgrp->sg_mem = add_list (nsgrp->sg_mem,
717 lflg ? user_newname :
721 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
722 "adding user to shadow group",
723 user_newname, -1, 1);
725 SYSLOG ((LOG_INFO, "add `%s' to shadow group `%s'",
726 lflg ? user_newname : user_name,
735 * Update the group entry to reflect the changes.
737 if (!sgr_update (nsgrp)) {
739 _("%s: error adding new shadow group entry\n"), Prog);
740 SYSLOG ((LOG_ERR, "error adding shadow group entry"));
741 fail_exit (E_GRP_UPDATE);
745 #endif /* SHADOWGRP */
748 * grp_update - add user to secondary group set
750 * grp_update() takes the secondary group set given in user_groups and
751 * adds the user to each group given by that set.
753 static void grp_update (void)
762 static long get_number (const char *numstr)
767 val = strtol (numstr, &errptr, 10);
768 if (*errptr || errno == ERANGE) {
769 fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog,
776 static uid_t get_id (const char *uidstr)
781 val = strtol (uidstr, &errptr, 10);
782 if (*errptr || errno == ERANGE || val < 0) {
783 fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog,
791 * process_flags - perform command line argument setting
793 * process_flags() interprets the command line arguments and sets the
794 * values that the user will be created with accordingly. The values
795 * are checked for sanity.
797 static void process_flags (int argc, char **argv)
799 const struct group *grp;
800 const struct passwd *pwd;
802 const struct spwd *spwd = NULL;
805 if (argc == 1 || argv[argc - 1][0] == '-')
808 if (!(pwd = getpwnam (argv[argc - 1]))) {
809 fprintf (stderr, _("%s: user %s does not exist\n"),
810 Prog, argv[argc - 1]);
814 user_name = argv[argc - 1];
815 user_id = pwd->pw_uid;
816 user_gid = pwd->pw_gid;
817 user_comment = xstrdup (pwd->pw_gecos);
818 user_home = xstrdup (pwd->pw_dir);
819 user_shell = xstrdup (pwd->pw_shell);
821 user_newname = user_name;
822 user_newid = user_id;
823 user_newgid = user_gid;
824 user_newcomment = user_comment;
825 user_newhome = user_home;
826 user_newshell = user_shell;
831 * Now make sure it isn't an NIS user.
837 fprintf (stderr, _("%s: user %s is a NIS user\n"),
840 if (!yp_get_default_domain (&nis_domain) &&
841 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
842 fprintf (stderr, _("%s: %s is the NIS master\n"),
849 if (is_shadow_pwd && (spwd = getspnam (user_name))) {
850 user_expire = spwd->sp_expire;
851 user_inactive = spwd->sp_inact;
853 user_newexpire = user_expire;
854 user_newinactive = user_inactive;
860 * Parse the command line options.
863 static struct option long_options[] = {
864 {"append", no_argument, NULL, 'a'},
865 {"comment", required_argument, NULL, 'c'},
866 {"home", required_argument, NULL, 'd'},
867 {"expiredate", required_argument, NULL, 'e'},
868 {"inactive", required_argument, NULL, 'f'},
869 {"gid", required_argument, NULL, 'g'},
870 {"groups", required_argument, NULL, 'G'},
871 {"help", no_argument, NULL, 'h'},
872 {"login", required_argument, NULL, 'l'},
873 {"lock", no_argument, NULL, 'L'},
874 {"move-home", no_argument, NULL, 'm'},
875 {"non-unique", no_argument, NULL, 'o'},
876 {"password", required_argument, NULL, 'p'},
877 {"shell", required_argument, NULL, 's'},
878 {"uid", required_argument, NULL, 'u'},
879 {"unlock", no_argument, NULL, 'U'},
880 {NULL, 0, NULL, '\0'}
883 getopt_long (argc, argv, "ac:d:e:f:g:G:hl:Lmop:s:u:U",
884 long_options, NULL)) != -1) {
890 if (!VALID (optarg)) {
892 _("%s: invalid field '%s'\n"),
897 user_newcomment = optarg;
899 user_comment = optarg;
904 if (!VALID (optarg)) {
906 _("%s: invalid field '%s'\n"),
911 user_newhome = optarg;
916 user_newexpire = strtoday (optarg);
917 if (user_newexpire == -1) {
919 user_expire = strtoday (optarg);
920 if (user_expire == -1) {
924 ("%s: invalid date '%s'\n"),
929 user_newexpire *= DAY / SCALE;
931 user_expire *= DAY / SCALE;
943 user_newinactive = get_number (optarg);
945 user_inactive = get_number (optarg);
950 grp = getgr_nam_gid (optarg);
953 _("%s: unknown group %s\n"),
957 user_newgid = grp->gr_gid;
961 if (get_groups (optarg))
966 if (!check_user_name (optarg)) {
968 _("%s: invalid field '%s'\n"),
974 * If the name does not really change, we mustn't
975 * set the flag as this will cause rather serious
978 if (strcmp (user_name, optarg))
981 user_newname = optarg;
997 if (!VALID (optarg)) {
999 _("%s: invalid field '%s'\n"),
1004 user_newshell = optarg;
1006 user_shell = optarg;
1011 user_newid = get_id (optarg);
1025 fprintf (stderr, _("%s: no flags given\n"), Prog);
1028 if (!is_shadow_pwd && (eflg || fflg)) {
1031 ("%s: shadow passwords required for -e and -f\n"),
1036 if (optind != argc - 1)
1039 if (aflg && (!Gflg)) {
1041 _("%s: -a flag is ONLY allowed with the -G flag\n"),
1047 if ((Lflg && (pflg || Uflg)) || (pflg && Uflg)) {
1049 _("%s: the -L, -p, and -U flags are exclusive\n"),
1055 if (oflg && !uflg) {
1057 _("%s: -o flag is ONLY allowed with the -u flag\n"),
1063 if (mflg && !dflg) {
1065 _("%s: -m flag is ONLY allowed with the -d flag\n"),
1071 if (dflg && strcmp (user_home, user_newhome) == 0)
1074 if (uflg && user_id == user_newid)
1077 if (lflg && getpwnam (user_newname)) {
1078 fprintf (stderr, _("%s: user %s exists\n"), Prog, user_newname);
1079 exit (E_NAME_IN_USE);
1082 if (uflg && !oflg && getpwuid (user_newid)) {
1083 fprintf (stderr, _("%s: uid %lu is not unique\n"),
1084 Prog, (unsigned long) user_newid);
1085 exit (E_UID_IN_USE);
1090 * close_files - close all of the files that were opened
1092 * close_files() closes all of the files that were opened for this new
1093 * user. This causes any modified entries to be written out.
1095 static void close_files (void)
1098 fprintf (stderr, _("%s: cannot rewrite password file\n"), Prog);
1099 fail_exit (E_PW_UPDATE);
1101 if (is_shadow_pwd && !spw_close ()) {
1103 _("%s: cannot rewrite shadow password file\n"), Prog);
1104 fail_exit (E_PW_UPDATE);
1109 fprintf (stderr, _("%s: cannot rewrite group file\n"),
1111 fail_exit (E_GRP_UPDATE);
1114 if (is_shadow_grp && !sgr_close ()) {
1116 _("%s: cannot rewrite shadow group file\n"),
1118 fail_exit (E_GRP_UPDATE);
1136 * Close the DBM and/or flat files
1147 * open_files - lock and open the password files
1149 * open_files() opens the two password files.
1151 static void open_files (void)
1154 fprintf (stderr, _("%s: unable to lock password file\n"), Prog);
1155 fail_exit (E_PW_UPDATE);
1158 if (!pw_open (O_RDWR)) {
1159 fprintf (stderr, _("%s: unable to open password file\n"), Prog);
1160 fail_exit (E_PW_UPDATE);
1162 if (is_shadow_pwd && !spw_lock ()) {
1164 _("%s: cannot lock shadow password file\n"), Prog);
1165 fail_exit (E_PW_UPDATE);
1168 if (is_shadow_pwd && !spw_open (O_RDWR)) {
1170 _("%s: cannot open shadow password file\n"), Prog);
1171 fail_exit (E_PW_UPDATE);
1176 * Lock and open the group file. This will load all of the
1180 fprintf (stderr, _("%s: error locking group file\n"),
1182 fail_exit (E_GRP_UPDATE);
1185 if (!gr_open (O_RDWR)) {
1186 fprintf (stderr, _("%s: error opening group file\n"),
1188 fail_exit (E_GRP_UPDATE);
1191 if (is_shadow_grp && !sgr_lock ()) {
1193 _("%s: error locking shadow group file\n"),
1195 fail_exit (E_GRP_UPDATE);
1198 if (is_shadow_grp && !sgr_open (O_RDWR)) {
1200 _("%s: error opening shadow group file\n"),
1202 fail_exit (E_GRP_UPDATE);
1212 * usr_update - create the user entries
1214 * usr_update() creates the password file entries for this user and
1215 * will update the group entries if required.
1217 static void usr_update (void)
1219 struct passwd pwent;
1220 const struct passwd *pwd;
1223 const struct spwd *spwd = NULL;
1226 * Locate the entry in /etc/passwd, which MUST exist.
1228 pwd = pw_locate (user_name);
1230 fprintf (stderr, _("%s: %s not found in /etc/passwd\n"),
1232 fail_exit (E_NOTFOUND);
1239 * Locate the entry in /etc/shadow. It doesn't have to exist, and
1240 * won't be created if it doesn't.
1242 if (is_shadow_pwd && (spwd = spw_locate (user_name))) {
1247 if (lflg || uflg || gflg || cflg || dflg || sflg || pflg
1249 if (!pw_update (&pwent)) {
1251 _("%s: error changing password entry\n"),
1253 fail_exit (E_PW_UPDATE);
1255 if (lflg && !pw_remove (user_name)) {
1257 _("%s: error removing password entry\n"),
1259 fail_exit (E_PW_UPDATE);
1262 if (spwd && (lflg || eflg || fflg || pflg || Lflg || Uflg)) {
1263 if (!spw_update (&spent)) {
1266 ("%s: error adding new shadow password entry\n"),
1268 fail_exit (E_PW_UPDATE);
1270 if (lflg && !spw_remove (user_name)) {
1273 ("%s: error removing shadow password entry\n"),
1275 fail_exit (E_PW_UPDATE);
1281 * move_home - move the user's home directory
1283 * move_home() moves the user's home directory to a new location. The
1284 * files will be copied if the directory cannot simply be renamed.
1286 static void move_home (void)
1290 if (mflg && stat (user_home, &sb) == 0) {
1292 * Don't try to move it if it is not a directory
1293 * (but /dev/null for example). --marekm
1295 if (!S_ISDIR (sb.st_mode))
1298 if (access (user_newhome, F_OK) == 0) {
1299 fprintf (stderr, _("%s: directory %s exists\n"),
1300 Prog, user_newhome);
1301 fail_exit (E_HOMEDIR);
1302 } else if (rename (user_home, user_newhome)) {
1303 if (errno == EXDEV) {
1304 if (mkdir (user_newhome, sb.st_mode & 0777)) {
1307 ("%s: can't create %s\n"),
1308 Prog, user_newhome);
1310 if (chown (user_newhome, sb.st_uid, sb.st_gid)) {
1312 _("%s: can't chown %s\n"),
1313 Prog, user_newhome);
1314 rmdir (user_newhome);
1315 fail_exit (E_HOMEDIR);
1317 if (copy_tree (user_home, user_newhome,
1318 uflg ? user_newid : -1,
1319 gflg ? user_newgid : -1) == 0) {
1320 if (remove_tree (user_home) != 0 ||
1321 rmdir (user_home) != 0)
1324 ("%s: warning: failed to completely remove old home directory %s"),
1327 audit_logger (AUDIT_USER_CHAUTHTOK,
1329 "moving home directory",
1330 user_newname, user_newid,
1336 (void) remove_tree (user_newhome);
1337 (void) rmdir (user_newhome);
1341 ("%s: cannot rename directory %s to %s\n"),
1342 Prog, user_home, user_newhome);
1343 fail_exit (E_HOMEDIR);
1346 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1347 "moving home directory", user_newname, user_newid,
1353 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1354 "changing home directory owner", user_newname,
1357 chown (dflg ? user_newhome : user_home,
1358 uflg ? user_newid : user_id,
1359 gflg ? user_newgid : user_gid);
1364 * update_files - update the lastlog and faillog files
1366 static void update_files (void)
1373 * Relocate the "lastlog" entries for the user. The old entry is
1374 * left alone in case the UID was shared. It doesn't hurt anything
1375 * to just leave it be.
1377 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1) {
1378 lseek (fd, (off_t) user_id * sizeof ll, SEEK_SET);
1379 if (read (fd, (char *) &ll, sizeof ll) == sizeof ll) {
1380 lseek (fd, (off_t) user_newid * sizeof ll, SEEK_SET);
1381 write (fd, (char *) &ll, sizeof ll);
1387 * Relocate the "faillog" entries in the same manner.
1389 if ((fd = open (FAILLOG_FILE, O_RDWR)) != -1) {
1390 lseek (fd, (off_t) user_id * sizeof fl, SEEK_SET);
1391 if (read (fd, (char *) &fl, sizeof fl) == sizeof fl) {
1392 lseek (fd, (off_t) user_newid * sizeof fl, SEEK_SET);
1393 write (fd, (char *) &fl, sizeof fl);
1399 #ifndef NO_MOVE_MAILBOX
1401 * This is the new and improved code to carefully chown/rename the user's
1402 * mailbox. Maybe I am too paranoid but the mail spool dir sometimes
1403 * happens to be mode 1777 (this makes mail user agents work without
1404 * being setgid mail, but is NOT recommended; they all should be fixed
1405 * to use movemail). --marekm
1407 static void move_mailbox (void)
1409 const char *maildir;
1410 char mailfile[1024], newmailfile[1024];
1414 maildir = getdef_str ("MAIL_DIR");
1415 #ifdef MAIL_SPOOL_DIR
1416 if (!maildir && !getdef_str ("MAIL_FILE"))
1417 maildir = MAIL_SPOOL_DIR;
1423 * O_NONBLOCK is to make sure open won't hang on mandatory locks.
1424 * We do fstat/fchown to make sure there are no races (someone
1425 * replacing /var/spool/mail/luser with a hard link to /etc/passwd
1426 * between stat and chown). --marekm
1428 snprintf (mailfile, sizeof mailfile, "%s/%s", maildir, user_name);
1429 fd = open (mailfile, O_RDONLY | O_NONBLOCK, 0);
1431 /* no need for warnings if the mailbox doesn't exist */
1432 if (errno != ENOENT)
1436 if (fstat (fd, &st) < 0) {
1441 if (st.st_uid != user_id) {
1442 /* better leave it alone */
1443 fprintf (stderr, _("%s: warning: %s not owned by %s\n"),
1444 Prog, mailfile, user_name);
1449 if (fchown (fd, user_newid, (gid_t) - 1) < 0) {
1450 perror (_("failed to change mailbox owner"));
1454 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1455 "changing mail file owner", user_newname,
1464 snprintf (newmailfile, sizeof newmailfile, "%s/%s",
1465 maildir, user_newname);
1466 if (link (mailfile, newmailfile) || unlink (mailfile)) {
1467 perror (_("failed to rename mailbox"));
1471 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1472 "changing mail file name", user_newname,
1481 * main - usermod command
1483 int main (int argc, char **argv)
1486 pam_handle_t *pamh = NULL;
1487 struct passwd *pampw;
1496 * Get my name so that I can use it to report errors.
1498 Prog = Basename (argv[0]);
1500 setlocale (LC_ALL, "");
1501 bindtextdomain (PACKAGE, LOCALEDIR);
1502 textdomain (PACKAGE);
1504 sys_ngroups = sysconf (_SC_NGROUPS_MAX);
1505 user_groups = malloc ((1 + sys_ngroups) * sizeof (char *));
1506 user_groups[0] = (char *) 0;
1508 OPENLOG ("usermod");
1510 is_shadow_pwd = spw_file_present ();
1512 is_shadow_grp = sgr_file_present ();
1515 process_flags (argc, argv);
1518 retval = PAM_SUCCESS;
1520 pampw = getpwuid (getuid ());
1521 if (pampw == NULL) {
1522 retval = PAM_USER_UNKNOWN;
1525 if (retval == PAM_SUCCESS) {
1526 retval = pam_start ("usermod", pampw->pw_name, &conv, &pamh);
1529 if (retval == PAM_SUCCESS) {
1530 retval = pam_authenticate (pamh, 0);
1531 if (retval != PAM_SUCCESS) {
1532 pam_end (pamh, retval);
1536 if (retval == PAM_SUCCESS) {
1537 retval = pam_acct_mgmt (pamh, 0);
1538 if (retval != PAM_SUCCESS) {
1539 pam_end (pamh, retval);
1543 if (retval != PAM_SUCCESS) {
1544 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
1547 #endif /* USE_PAM */
1550 * Do the hard stuff - open the files, change the user entries,
1551 * change the home directory, then close and update the files.
1559 nscd_flush_cache ("passwd");
1560 nscd_flush_cache ("group");
1565 #ifndef NO_MOVE_MAILBOX
1574 * Change the UID on all of the files owned by `user_id' to
1575 * `user_newid' in the user's home directory.
1577 chown_tree (dflg ? user_newhome : user_home,
1578 user_id, user_newid,
1579 user_gid, gflg ? user_newgid : user_gid);
1583 if (retval == PAM_SUCCESS)
1584 pam_end (pamh, PAM_SUCCESS);
1585 #endif /* USE_PAM */