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
32 #ident "$Id: usermod.c,v 1.65 2006/01/18 19:55:15 kloczek Exp $"
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;
131 /* local function prototypes */
132 static int get_groups (char *);
133 static void usage (void);
134 static void new_pwent (struct passwd *);
136 static void new_spent (struct spwd *);
137 static void fail_exit (int);
138 static int update_group (void);
141 static int update_gshadow (void);
143 static int grp_update (void);
145 static long get_number (const char *);
146 static uid_t get_id (const char *);
147 static void process_flags (int, char **);
148 static void close_files (void);
149 static void open_files (void);
150 static void usr_update (void);
151 static void move_home (void);
152 static void update_files (void);
154 #ifndef NO_MOVE_MAILBOX
155 static void move_mailbox (void);
159 * Had to move this over from useradd.c since we have groups named
160 * "56k-family"... ergh.
163 static struct group *getgr_nam_gid (const char *name)
168 gid = strtoul (name, &ep, 10);
169 if (*name != '\0' && *ep == '\0') /* valid numeric GID */
170 return getgrgid (gid);
172 return getgrnam (name);
176 * get_groups - convert a list of group names to an array of group IDs
178 * get_groups() takes a comma-separated list of group names and
179 * converts it to a NULL-terminated array. Any unknown group names are
180 * reported as errors.
182 static int get_groups (char *list)
185 const struct group *grp;
190 * Initialize the list to be empty
192 user_groups[0] = (char *) 0;
198 * So long as there is some data to be converted, strip off each
199 * name and look it up. A mix of numerical and string values for
200 * group identifiers is permitted.
204 * Strip off a single name from the list
206 if ((cp = strchr (list, ',')))
210 * Names starting with digits are treated as numerical GID
211 * values, otherwise the string is looked up as is.
213 grp = getgr_nam_gid (list);
216 * There must be a match, either by GID value or by
220 fprintf (stderr, _("%s: unknown group %s\n"),
227 * If the group doesn't exist, don't dump core. Instead,
228 * try the next one. --marekm
235 * Don't add this group if they are an NIS group. Tell the
236 * user to go to the server for this group.
240 _("%s: group `%s' is a NIS group.\n"),
246 if (ngroups == sys_ngroups) {
249 ("%s: too many groups specified (max %d).\n"),
255 * Add the group name to the user's list of groups.
257 user_groups[ngroups++] = xstrdup (grp->gr_name);
260 user_groups[ngroups] = (char *) 0;
263 * Any errors in finding group names are fatal
272 * usage - display usage message and exit
274 static void usage (void)
276 fprintf (stderr, _("Usage: usermod [options] login\n"
279 " -a, --append GROUP append the user to the supplemental GROUP\n"
280 " -c, --comment COMMENT new value of the GECOS field\n"
281 " -d, --home HOME_DIR new login directory for the new user account\n"
282 " -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE\n"
283 " -f, --inactive INACTIVE set password inactive after expiration\n"
285 " -g, --gid GROUP force use GROUP as new initial login group\n"
286 " -G, --groups GROUPS list of supplementary GROUPS\n"
287 " -h, --help display this help message and exit\n"
288 " -l, --login LOGIN new value of the login name\n"
289 " -L, --lock lock the user account\n"
290 " -m, --move-home move contents of the home directory to the new\n"
291 " location (use only with -d)\n"
292 " -o, --non-unique allow using duplicate (non-unique) UID\n"
293 " -p, --password PASSWORD use encrypted password for the new password\n"
294 " -s, --shell SHELL new login shell for the user account\n"
295 " -u, --uid UID new UID for the user account\n"
296 " -U, --unlock unlock the user account\n"));
301 * update encrypted password string (for both shadow and non-shadow
304 static char *new_pw_passwd (char *pw_pass, const char *pw_name)
306 if (Lflg && pw_pass[0] != '!') {
307 char *buf = xmalloc (strlen (pw_pass) + 2);
310 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating passwd",
311 user_newname, user_newid, 0);
313 SYSLOG ((LOG_INFO, "lock user `%s' password", pw_name));
315 strcat (buf, pw_pass);
317 } else if (Uflg && pw_pass[0] == '!') {
321 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating password",
322 user_newname, user_newid, 0);
324 SYSLOG ((LOG_INFO, "unlock user `%s' password", pw_name));
332 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
333 user_newname, user_newid, 1);
335 SYSLOG ((LOG_INFO, "change user `%s' password", pw_name));
336 pw_pass = xstrdup (user_pass);
342 * new_pwent - initialize the values in a password file entry
344 * new_pwent() takes all of the values that have been entered and fills
345 * in a (struct passwd) with them.
347 static void new_pwent (struct passwd *pwent)
351 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing name",
352 user_newname, user_newid, 1);
354 SYSLOG ((LOG_INFO, "change user name `%s' to `%s'",
355 pwent->pw_name, user_newname));
356 pwent->pw_name = xstrdup (user_newname);
360 new_pw_passwd (pwent->pw_passwd, pwent->pw_name);
364 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing uid",
365 user_newname, user_newid, 1);
368 "change user `%s' UID from `%d' to `%d'",
369 pwent->pw_name, pwent->pw_uid, user_newid));
370 pwent->pw_uid = user_newid;
374 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
375 "changing primary group", user_newname,
379 "change user `%s' GID from `%d' to `%d'",
380 pwent->pw_name, pwent->pw_gid, user_newgid));
381 pwent->pw_gid = user_newgid;
385 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing comment",
386 user_newname, user_newid, 1);
387 pwent->pw_gecos = user_newcomment;
389 pwent->pw_gecos = user_comment;
395 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
396 "changing home directory", user_newname,
400 "change user `%s' home from `%s' to `%s'",
401 pwent->pw_name, pwent->pw_dir, user_newhome));
402 pwent->pw_dir = user_newhome;
406 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing user shell",
407 user_newname, user_newid, 1);
408 SYSLOG ((LOG_INFO, "change user `%s' shell from `%s' to `%s'",
409 pwent->pw_name, pwent->pw_shell, user_newshell));
410 pwent->pw_shell = user_newshell;
413 "change user `%s' shell from `%s' to `%s'",
414 pwent->pw_name, pwent->pw_shell, user_shell));
415 pwent->pw_shell = user_shell;
421 * new_spent - initialize the values in a shadow password file entry
423 * new_spent() takes all of the values that have been entered and fills
424 * in a (struct spwd) with them.
426 static void new_spent (struct spwd *spent)
429 spent->sp_namp = xstrdup (user_newname);
433 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
434 "changing inactive days", user_newname,
437 "change user `%s' inactive from `%ld' to `%ld'",
438 spent->sp_namp, spent->sp_inact, user_newinactive));
439 spent->sp_inact = user_newinactive;
443 "change user `%s' inactive from `%ld' to `%ld'",
444 spent->sp_namp, spent->sp_inact, user_inactive));
445 spent->sp_inact = user_inactive;
449 /* XXX - dates might be better than numbers of days. --marekm */
454 char new_exp[16], old_exp[16];
456 if (user_newexpire == -1)
459 exp_t = user_newexpire * DAY;
460 exp_tm = gmtime (&exp_t);
462 strftime (new_exp, sizeof (new_exp), "%Y-%m-%d",
465 memset (new_exp, 0, sizeof (new_exp));
466 snprintf (new_exp, sizeof (new_exp) - 1,
468 exp_tm->tm_year + 1900,
469 exp_tm->tm_mon + 1, exp_tm->tm_mday);
473 if (user_expire == -1)
476 exp_t = user_expire * DAY;
477 exp_tm = gmtime (&exp_t);
479 strftime (old_exp, sizeof (old_exp), "%Y-%m-%d",
482 memset (old_exp, 0, sizeof (old_exp));
483 snprintf (old_exp, sizeof (old_exp) - 1,
485 exp_tm->tm_year + 1900,
486 exp_tm->tm_mon + 1, exp_tm->tm_mday);
489 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
490 "changing expiration date", user_newname,
495 "change user `%s' expiration from `%ld' to `%ld'",
496 spent->sp_namp, spent->sp_expire, user_newexpire));
497 spent->sp_expire = user_newexpire;
500 "change user `%s' expiration from `%ld' to `%ld'",
501 spent->sp_namp, spent->sp_expire, user_expire));
502 spent->sp_expire = user_expire;
505 spent->sp_pwdp = new_pw_passwd (spent->sp_pwdp, spent->sp_namp);
507 spent->sp_lstchg = time ((time_t *) 0) / SCALE;
511 * fail_exit - exit with an error code after unlocking files
513 static void fail_exit (int code)
524 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "modifying account",
531 static int update_group (void)
536 const struct group *grp;
540 * Lock and open the group file. This will load all of the group
544 fprintf (stderr, _("%s: error locking group file\n"), Prog);
545 SYSLOG ((LOG_ERR, "error locking group file"));
548 if (!gr_open (O_RDWR)) {
549 fprintf (stderr, _("%s: error opening group file\n"), Prog);
550 SYSLOG ((LOG_ERR, "error opening group file"));
558 * Scan through the entire group file looking for the groups that
559 * the user is a member of.
561 while ((grp = gr_next ())) {
563 * See if the user specified this group as one of their
566 was_member = is_on_list (grp->gr_mem, user_name);
567 is_member = Gflg && is_on_list (user_groups, grp->gr_name);
569 if (!was_member && !is_member)
572 ngrp = __gr_dup (grp);
575 _("%s: out of memory in update_group\n"),
581 if (was_member && (!Gflg || is_member)) {
583 ngrp->gr_mem = del_list (ngrp->gr_mem,
585 ngrp->gr_mem = add_list (ngrp->gr_mem,
589 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
590 "changing group member",
591 user_newname, -1, 1);
594 "change `%s' to `%s' in group `%s'",
595 user_name, user_newname,
598 } else if (was_member && !aflg && Gflg && !is_member) {
599 ngrp->gr_mem = del_list (ngrp->gr_mem, user_name);
602 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
603 "removing group member", user_name, -1,
606 SYSLOG ((LOG_INFO, "delete `%s' from group `%s'",
607 user_name, ngrp->gr_name));
608 } else if (!was_member && Gflg && is_member) {
609 ngrp->gr_mem = add_list (ngrp->gr_mem,
610 lflg ? user_newname :
614 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
615 "adding user to group", user_name, -1, 1);
617 SYSLOG ((LOG_INFO, "add `%s' to group `%s'",
618 lflg ? user_newname : user_name,
625 if (!gr_update (ngrp)) {
627 _("%s: error adding new group entry\n"), Prog);
628 SYSLOG ((LOG_ERR, "error adding group entry"));
635 fprintf (stderr, _("%s: cannot rewrite group file\n"), Prog);
644 static int update_gshadow (void)
650 const struct sgrp *sgrp;
655 _("%s: error locking shadow group file\n"), Prog);
656 SYSLOG ((LOG_ERR, "error locking shadow group file"));
659 if (!sgr_open (O_RDWR)) {
661 _("%s: error opening shadow group file\n"), Prog);
662 SYSLOG ((LOG_ERR, "error opening shadow group file"));
670 * Scan through the entire shadow group file looking for the groups
671 * that the user is a member of.
673 while ((sgrp = sgr_next ())) {
676 * See if the user was a member of this group
678 was_member = is_on_list (sgrp->sg_mem, user_name);
681 * See if the user was an administrator of this group
683 was_admin = is_on_list (sgrp->sg_adm, user_name);
686 * See if the user specified this group as one of their
689 is_member = Gflg && is_on_list (user_groups, sgrp->sg_name);
691 if (!was_member && !was_admin && !is_member)
694 nsgrp = __sgr_dup (sgrp);
698 ("%s: out of memory in update_gshadow\n"),
704 if (was_admin && lflg) {
705 nsgrp->sg_adm = del_list (nsgrp->sg_adm, user_name);
706 nsgrp->sg_adm = add_list (nsgrp->sg_adm, user_newname);
709 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
710 "changing admin name in shadow group",
714 "change admin `%s' to `%s' in shadow group `%s'",
715 user_name, user_newname, nsgrp->sg_name));
717 if (was_member && (!Gflg || is_member)) {
719 nsgrp->sg_mem = del_list (nsgrp->sg_mem,
721 nsgrp->sg_mem = add_list (nsgrp->sg_mem,
725 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
726 "changing member in shadow group",
730 "change `%s' to `%s' in shadow group `%s'",
731 user_name, user_newname,
734 } else if (was_member && !aflg && Gflg && !is_member) {
735 nsgrp->sg_mem = del_list (nsgrp->sg_mem, user_name);
738 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
739 "removing user from shadow group",
743 "delete `%s' from shadow group `%s'",
744 user_name, nsgrp->sg_name));
745 } else if (!was_member && Gflg && is_member) {
746 nsgrp->sg_mem = add_list (nsgrp->sg_mem,
747 lflg ? user_newname :
751 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
752 "adding user to shadow group",
753 user_newname, -1, 1);
755 SYSLOG ((LOG_INFO, "add `%s' to shadow group `%s'",
756 lflg ? user_newname : user_name,
765 * Update the group entry to reflect the changes.
767 if (!sgr_update (nsgrp)) {
769 _("%s: error adding new group entry\n"), Prog);
770 SYSLOG ((LOG_ERR, "error adding shadow group entry"));
778 _("%s: cannot rewrite shadow group file\n"), Prog);
785 #endif /* SHADOWGRP */
788 * grp_update - add user to secondary group set
790 * grp_update() takes the secondary group set given in user_groups and
791 * adds the user to each group given by that set.
793 static int grp_update (void)
797 ret = update_group ();
799 if (!ret && is_shadow_grp)
800 ret = update_gshadow ();
805 static long get_number (const char *cp)
810 val = strtol (cp, &ep, 10);
811 if (*cp != '\0' && *ep == '\0') /* valid number */
814 fprintf (stderr, _("%s: invalid numeric argument `%s'\n"), Prog, cp);
818 static uid_t get_id (const char *cp)
823 val = strtoul (cp, &ep, 10);
824 if (*cp != '\0' && *ep == '\0') /* valid number */
827 fprintf (stderr, _("%s: invalid numeric argument `%s'\n"), Prog, cp);
832 * process_flags - perform command line argument setting
834 * process_flags() interprets the command line arguments and sets the
835 * values that the user will be created with accordingly. The values
836 * are checked for sanity.
838 static void process_flags (int argc, char **argv)
840 const struct group *grp;
841 const struct passwd *pwd;
843 const struct spwd *spwd = NULL;
846 if (argc == 1 || argv[argc - 1][0] == '-')
849 if (!(pwd = getpwnam (argv[argc - 1]))) {
850 fprintf (stderr, _("%s: user %s does not exist\n"),
851 Prog, argv[argc - 1]);
855 user_name = argv[argc - 1];
856 user_id = pwd->pw_uid;
857 user_gid = pwd->pw_gid;
858 user_comment = xstrdup (pwd->pw_gecos);
859 user_home = xstrdup (pwd->pw_dir);
860 user_shell = xstrdup (pwd->pw_shell);
862 user_newname = user_name;
863 user_newid = user_id;
864 user_newgid = user_gid;
865 user_newcomment = user_comment;
866 user_newhome = user_home;
867 user_newshell = user_shell;
872 * Now make sure it isn't an NIS user.
878 fprintf (stderr, _("%s: user %s is a NIS user\n"),
881 if (!yp_get_default_domain (&nis_domain) &&
882 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
883 fprintf (stderr, _("%s: %s is the NIS master\n"),
890 if (is_shadow_pwd && (spwd = getspnam (user_name))) {
891 user_expire = spwd->sp_expire;
892 user_inactive = spwd->sp_inact;
894 user_newexpire = user_expire;
895 user_newinactive = user_inactive;
901 * Parse the command line options.
904 static struct option long_options[] = {
905 {"append", required_argument, NULL, 'a'},
906 {"comment", required_argument, NULL, 'c'},
907 {"home", required_argument, NULL, 'd'},
908 {"expiredate", required_argument, NULL, 'e'},
909 {"inactive", required_argument, NULL, 'f'},
910 {"gid", required_argument, NULL, 'g'},
911 {"groups", required_argument, NULL, 'G'},
912 {"help", no_argument, NULL, 'h'},
913 {"login", required_argument, NULL, 'l'},
914 {"lock", no_argument, NULL, 'L'},
915 {"move-home", no_argument, NULL, 'm'},
916 {"non-unique", no_argument, NULL, 'o'},
917 {"password", required_argument, NULL, 'p'},
918 {"shell", required_argument, NULL, 's'},
919 {"uid", required_argument, NULL, 'u'},
920 {"unlock", no_argument, NULL, 'U'},
921 {NULL, 0, NULL, '\0'}
924 getopt_long (argc, argv, "ac:d:e:f:g:G:l:Lmop:s:u:U",
925 long_options, NULL)) != -1) {
931 if (!VALID (optarg)) {
933 _("%s: invalid field `%s'\n"),
938 user_newcomment = optarg;
940 user_comment = optarg;
945 if (!VALID (optarg)) {
947 _("%s: invalid field `%s'\n"),
952 user_newhome = optarg;
957 user_newexpire = strtoday (optarg);
958 if (user_newexpire == -1) {
960 user_expire = strtoday (optarg);
961 if (user_expire == -1) {
965 ("%s: invalid date `%s'\n"),
970 user_newexpire *= DAY / SCALE;
972 user_expire *= DAY / SCALE;
984 user_newinactive = get_number (optarg);
986 user_inactive = get_number (optarg);
991 grp = getgr_nam_gid (optarg);
994 _("%s: unknown group %s\n"),
998 user_newgid = grp->gr_gid;
1002 if (get_groups (optarg))
1007 if (!check_user_name (optarg)) {
1009 _("%s: invalid field `%s'\n"),
1015 * If the name does not really change, we mustn't
1016 * set the flag as this will cause rather serious
1019 if (strcmp (user_name, optarg))
1022 user_newname = optarg;
1050 if (!VALID (optarg)) {
1052 _("%s: invalid field `%s'\n"),
1057 user_newshell = optarg;
1059 user_shell = optarg;
1064 user_newid = get_id (optarg);
1081 fprintf (stderr, _("%s: no flags given\n"), Prog);
1084 if (!is_shadow_pwd && (eflg || fflg)) {
1087 ("%s: shadow passwords required for -e and -f\n"),
1092 if (optind != argc - 1)
1095 if (aflg && (!Gflg)) {
1097 _("%s: -a flag is ONLY allowed with the -G flag\n"),
1103 if (dflg && strcmp (user_home, user_newhome) == 0)
1106 if (uflg && user_id == user_newid)
1109 if (lflg && getpwnam (user_newname)) {
1110 fprintf (stderr, _("%s: user %s exists\n"), Prog, user_newname);
1111 exit (E_NAME_IN_USE);
1114 if (uflg && !oflg && getpwuid (user_newid)) {
1115 fprintf (stderr, _("%s: uid %lu is not unique\n"),
1116 Prog, (unsigned long) user_newid);
1117 exit (E_UID_IN_USE);
1122 * close_files - close all of the files that were opened
1124 * close_files() closes all of the files that were opened for this new
1125 * user. This causes any modified entries to be written out.
1127 static void close_files (void)
1130 fprintf (stderr, _("%s: cannot rewrite password file\n"), Prog);
1131 fail_exit (E_PW_UPDATE);
1133 if (is_shadow_pwd && !spw_close ()) {
1135 _("%s: cannot rewrite shadow password file\n"), Prog);
1136 fail_exit (E_PW_UPDATE);
1140 (void) pw_unlock ();
1143 * Close the DBM and/or flat files
1154 * open_files - lock and open the password files
1156 * open_files() opens the two password files.
1158 static void open_files (void)
1161 fprintf (stderr, _("%s: unable to lock password file\n"), Prog);
1164 if (!pw_open (O_RDWR)) {
1165 fprintf (stderr, _("%s: unable to open password file\n"), Prog);
1166 fail_exit (E_PW_UPDATE);
1168 if (is_shadow_pwd && !spw_lock ()) {
1170 _("%s: cannot lock shadow password file\n"), Prog);
1171 fail_exit (E_PW_UPDATE);
1173 if (is_shadow_pwd && !spw_open (O_RDWR)) {
1175 _("%s: cannot open shadow password file\n"), Prog);
1176 fail_exit (E_PW_UPDATE);
1181 * usr_update - create the user entries
1183 * usr_update() creates the password file entries for this user and
1184 * will update the group entries if required.
1186 static void usr_update (void)
1188 struct passwd pwent;
1189 const struct passwd *pwd;
1192 const struct spwd *spwd = NULL;
1195 * Locate the entry in /etc/passwd, which MUST exist.
1197 pwd = pw_locate (user_name);
1199 fprintf (stderr, _("%s: %s not found in /etc/passwd\n"),
1201 fail_exit (E_NOTFOUND);
1208 * Locate the entry in /etc/shadow. It doesn't have to exist, and
1209 * won't be created if it doesn't.
1211 if (is_shadow_pwd && (spwd = spw_locate (user_name))) {
1216 if (lflg || uflg || gflg || cflg || dflg || sflg || pflg
1218 if (!pw_update (&pwent)) {
1220 _("%s: error changing password entry\n"),
1222 fail_exit (E_PW_UPDATE);
1224 if (lflg && !pw_remove (user_name)) {
1226 _("%s: error removing password entry\n"),
1228 fail_exit (E_PW_UPDATE);
1231 if (spwd && (lflg || eflg || fflg || pflg || Lflg || Uflg)) {
1232 if (!spw_update (&spent)) {
1235 ("%s: error adding new shadow password entry\n"),
1237 fail_exit (E_PW_UPDATE);
1239 if (lflg && !spw_remove (user_name)) {
1242 ("%s: error removing shadow password entry\n"),
1244 fail_exit (E_PW_UPDATE);
1250 * move_home - move the user's home directory
1252 * move_home() moves the user's home directory to a new location. The
1253 * files will be copied if the directory cannot simply be renamed.
1255 static void move_home (void)
1259 if (mflg && stat (user_home, &sb) == 0) {
1261 * Don't try to move it if it is not a directory
1262 * (but /dev/null for example). --marekm
1264 if (!S_ISDIR (sb.st_mode))
1267 if (access (user_newhome, F_OK) == 0) {
1268 fprintf (stderr, _("%s: directory %s exists\n"),
1269 Prog, user_newhome);
1270 fail_exit (E_HOMEDIR);
1271 } else if (rename (user_home, user_newhome)) {
1272 if (errno == EXDEV) {
1273 if (mkdir (user_newhome, sb.st_mode & 0777)) {
1276 ("%s: can't create %s\n"),
1277 Prog, user_newhome);
1279 if (chown (user_newhome, sb.st_uid, sb.st_gid)) {
1281 _("%s: can't chown %s\n"),
1282 Prog, user_newhome);
1283 rmdir (user_newhome);
1284 fail_exit (E_HOMEDIR);
1286 if (copy_tree (user_home, user_newhome,
1287 uflg ? user_newid : -1,
1288 gflg ? user_newgid : -1) == 0) {
1289 if (remove_tree (user_home) != 0 ||
1290 rmdir (user_home) != 0)
1293 ("%s: warning: failed to completely remove old home directory %s"),
1296 audit_logger (AUDIT_USER_CHAUTHTOK,
1298 "moving home directory",
1299 user_newname, user_newid,
1305 (void) remove_tree (user_newhome);
1306 (void) rmdir (user_newhome);
1310 ("%s: cannot rename directory %s to %s\n"),
1311 Prog, user_home, user_newhome);
1312 fail_exit (E_HOMEDIR);
1315 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1316 "moving home directory", user_newname, user_newid,
1322 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1323 "changing home directory owner", user_newname,
1326 chown (dflg ? user_newhome : user_home,
1327 uflg ? user_newid : user_id,
1328 gflg ? user_newgid : user_gid);
1333 * update_files - update the lastlog and faillog files
1335 static void update_files (void)
1342 * Relocate the "lastlog" entries for the user. The old entry is
1343 * left alone in case the UID was shared. It doesn't hurt anything
1344 * to just leave it be.
1346 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1) {
1347 lseek (fd, (off_t) user_id * sizeof ll, SEEK_SET);
1348 if (read (fd, (char *) &ll, sizeof ll) == sizeof ll) {
1349 lseek (fd, (off_t) user_newid * sizeof ll, SEEK_SET);
1350 write (fd, (char *) &ll, sizeof ll);
1356 * Relocate the "faillog" entries in the same manner.
1358 if ((fd = open (FAILLOG_FILE, O_RDWR)) != -1) {
1359 lseek (fd, (off_t) user_id * sizeof fl, SEEK_SET);
1360 if (read (fd, (char *) &fl, sizeof fl) == sizeof fl) {
1361 lseek (fd, (off_t) user_newid * sizeof fl, SEEK_SET);
1362 write (fd, (char *) &fl, sizeof fl);
1368 #ifndef NO_MOVE_MAILBOX
1370 * This is the new and improved code to carefully chown/rename the user's
1371 * mailbox. Maybe I am too paranoid but the mail spool dir sometimes
1372 * happens to be mode 1777 (this makes mail user agents work without
1373 * being setgid mail, but is NOT recommended; they all should be fixed
1374 * to use movemail). --marekm
1376 static void move_mailbox (void)
1378 const char *maildir;
1379 char mailfile[1024], newmailfile[1024];
1383 maildir = getdef_str ("MAIL_DIR");
1384 #ifdef MAIL_SPOOL_DIR
1385 if (!maildir && !getdef_str ("MAIL_FILE"))
1386 maildir = MAIL_SPOOL_DIR;
1392 * O_NONBLOCK is to make sure open won't hang on mandatory locks.
1393 * We do fstat/fchown to make sure there are no races (someone
1394 * replacing /var/spool/mail/luser with a hard link to /etc/passwd
1395 * between stat and chown). --marekm
1397 snprintf (mailfile, sizeof mailfile, "%s/%s", maildir, user_name);
1398 fd = open (mailfile, O_RDONLY | O_NONBLOCK, 0);
1400 /* no need for warnings if the mailbox doesn't exist */
1401 if (errno != ENOENT)
1405 if (fstat (fd, &st) < 0) {
1410 if (st.st_uid != user_id) {
1411 /* better leave it alone */
1412 fprintf (stderr, _("%s: warning: %s not owned by %s\n"),
1413 Prog, mailfile, user_name);
1418 if (fchown (fd, user_newid, (gid_t) - 1) < 0) {
1419 perror (_("failed to change mailbox owner"));
1423 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1424 "changing mail file owner", user_newname,
1433 snprintf (newmailfile, sizeof newmailfile, "%s/%s",
1434 maildir, user_newname);
1435 if (link (mailfile, newmailfile) || unlink (mailfile)) {
1436 perror (_("failed to rename mailbox"));
1440 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
1441 "changing mail file name", user_newname,
1450 * main - usermod command
1452 int main (int argc, char **argv)
1457 pam_handle_t *pamh = NULL;
1458 struct passwd *pampw;
1467 * Get my name so that I can use it to report errors.
1469 Prog = Basename (argv[0]);
1471 setlocale (LC_ALL, "");
1472 bindtextdomain (PACKAGE, LOCALEDIR);
1473 textdomain (PACKAGE);
1475 sys_ngroups = sysconf (_SC_NGROUPS_MAX);
1476 user_groups = malloc ((1 + sys_ngroups) * sizeof (char *));
1477 user_groups[0] = (char *) 0;
1479 OPENLOG ("usermod");
1481 is_shadow_pwd = spw_file_present ();
1483 is_shadow_grp = sgr_file_present ();
1486 process_flags (argc, argv);
1489 retval = PAM_SUCCESS;
1491 pampw = getpwuid (getuid ());
1492 if (pampw == NULL) {
1493 retval = PAM_USER_UNKNOWN;
1496 if (retval == PAM_SUCCESS) {
1497 retval = pam_start ("usermod", pampw->pw_name, &conv, &pamh);
1500 if (retval == PAM_SUCCESS) {
1501 retval = pam_authenticate (pamh, 0);
1502 if (retval != PAM_SUCCESS) {
1503 pam_end (pamh, retval);
1507 if (retval == PAM_SUCCESS) {
1508 retval = pam_acct_mgmt (pamh, 0);
1509 if (retval != PAM_SUCCESS) {
1510 pam_end (pamh, retval);
1514 if (retval != PAM_SUCCESS) {
1515 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
1518 #endif /* USE_PAM */
1521 * Do the hard stuff - open the files, change the user entries,
1522 * change the home directory, then close and update the files.
1527 nscd_flush_cache ("passwd");
1528 nscd_flush_cache ("group");
1533 grp_err = grp_update ();
1538 #ifndef NO_MOVE_MAILBOX
1547 * Change the UID on all of the files owned by `user_id' to
1548 * `user_newid' in the user's home directory.
1550 chown_tree (dflg ? user_newhome : user_home,
1551 user_id, user_newid,
1552 user_gid, gflg ? user_newgid : user_gid);
1556 exit (E_GRP_UPDATE);
1559 if (retval == PAM_SUCCESS)
1560 pam_end (pamh, PAM_SUCCESS);
1561 #endif /* USE_PAM */