]> granicus.if.org Git - shadow/blob - src/useradd.c
* src/useradd.c: Avoid redefinition of SHELL. Use DSHELL instead.
[shadow] / src / useradd.c
1 /*
2  * Copyright (c) 1991 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <assert.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <getopt.h>
42 #include <grp.h>
43 #include <lastlog.h>
44 #include <pwd.h>
45 #ifdef ACCT_TOOLS_SETUID
46 #ifdef USE_PAM
47 #include "pam_defs.h"
48 #endif                          /* USE_PAM */
49 #endif                          /* ACCT_TOOLS_SETUID */
50 #include <stdio.h>
51 #include <string.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <time.h>
55 #include "chkname.h"
56 #include "defines.h"
57 #include "faillog.h"
58 #include "getdef.h"
59 #include "groupio.h"
60 #include "nscd.h"
61 #include "prototypes.h"
62 #include "pwauth.h"
63 #include "pwio.h"
64 #ifdef  SHADOWGRP
65 #include "sgroupio.h"
66 #endif
67 #include "shadowio.h"
68
69 #ifndef SKEL_DIR
70 #define SKEL_DIR "/etc/skel"
71 #endif
72 #ifndef USER_DEFAULTS_FILE
73 #define USER_DEFAULTS_FILE "/etc/default/useradd"
74 #define NEW_USER_FILE "/etc/default/nuaddXXXXXX"
75 #endif
76 /*
77  * Needed for MkLinux DR1/2/2.1 - J.
78  */
79 #ifndef LASTLOG_FILE
80 #define LASTLOG_FILE "/var/log/lastlog"
81 #endif
82 /*
83  * Global variables
84  */
85 char *Prog;
86
87 /*
88  * These defaults are used if there is no defaults file.
89  */
90 static gid_t def_group = 100;
91 static const char *def_gname = "other";
92 static const char *def_home = "/home";
93 static const char *def_shell = "";
94 static const char *def_template = SKEL_DIR;
95 static const char *def_create_mail_spool = "no";
96
97 static long def_inactive = -1;
98 static const char *def_expire = "";
99
100 static char def_file[] = USER_DEFAULTS_FILE;
101
102 #define VALID(s)        (strcspn (s, ":\n") == strlen (s))
103
104 static const char *user_name = "";
105 static const char *user_pass = "!";
106 static uid_t user_id;
107 static gid_t user_gid;
108 static const char *user_comment = "";
109 static const char *user_home = "";
110 static const char *user_shell = "";
111 static const char *create_mail_spool = "";
112 #ifdef WITH_SELINUX
113 static const char *user_selinux = "";
114 #endif
115
116 static long user_expire = -1;
117 static bool is_shadow_pwd;
118
119 #ifdef SHADOWGRP
120 static bool is_shadow_grp;
121 static bool sgr_locked = false;
122 #endif
123 static bool pw_locked = false;
124 static bool gr_locked = false;
125 static bool spw_locked = false;
126 static char **user_groups;      /* NULL-terminated list */
127 static long sys_ngroups;
128 static bool do_grp_update = false;      /* group files need to be updated */
129
130 static bool
131     bflg = false,               /* new default root of home directory */
132     cflg = false,               /* comment (GECOS) field for new account */
133     dflg = false,               /* home directory for new account */
134     Dflg = false,               /* set/show new user default values */
135     eflg = false,               /* days since 1970-01-01 when account is locked */
136     fflg = false,               /* days until account with expired password is locked */
137     gflg = false,               /* primary group ID for new account */
138     Gflg = false,               /* secondary group set for new account */
139     kflg = false,               /* specify a directory to fill new user directory */
140     lflg = false,               /* do not add user to lastlog/faillog databases */
141     mflg = false,               /* create user's home directory if it doesn't exist */
142     Mflg = false,               /* do not create user's home directory even if CREATE_HOME is set */
143     Nflg = false,               /* do not create a group having the same name as the user, but add the user to def_group (or the group specified with -g) */
144     oflg = false,               /* permit non-unique user ID to be specified with -u */
145     rflg = false,               /* create a system account */
146     sflg = false,               /* shell program for new account */
147     uflg = false,               /* specify user ID for new account */
148     Uflg = false,               /* create a group having the same name as the user */
149     Zflg = false;               /* new selinux user */
150
151 static bool home_added = false;
152
153 /*
154  * exit status values
155  */
156 /*@-exitarg@*/
157 #define E_SUCCESS       0       /* success */
158 #define E_PW_UPDATE     1       /* can't update password file */
159 #define E_USAGE         2       /* invalid command syntax */
160 #define E_BAD_ARG       3       /* invalid argument to option */
161 #define E_UID_IN_USE    4       /* UID already in use (and no -o) */
162 #define E_NOTFOUND      6       /* specified group doesn't exist */
163 #define E_NAME_IN_USE   9       /* username already in use */
164 #define E_GRP_UPDATE    10      /* can't update group file */
165 #define E_HOMEDIR       12      /* can't create home directory */
166 #define E_MAIL_SPOOL    13      /* can't create mail spool */
167
168 #define DGROUP                  "GROUP="
169 #define DHOME                   "HOME="
170 #define DSHELL                  "SHELL="
171 #define DINACT                  "INACTIVE="
172 #define DEXPIRE                 "EXPIRE="
173 #define DSKEL                   "SKEL="
174 #define DCREATE_MAIL_SPOOL      "CREATE_MAIL_SPOOL="
175
176 /* local function prototypes */
177 static void fail_exit (int);
178 static void get_defaults (void);
179 static void show_defaults (void);
180 static int set_defaults (void);
181 static int get_groups (char *);
182 static void usage (int status);
183 static void new_pwent (struct passwd *);
184 #ifdef WITH_SELINUX
185 static void selinux_update_mapping (void);
186 #endif
187
188 static long scale_age (long);
189 static void new_spent (struct spwd *);
190 static void grp_update (void);
191
192 static void process_flags (int argc, char **argv);
193 static void close_files (void);
194 static void open_files (void);
195 static void faillog_reset (uid_t);
196 static void lastlog_reset (uid_t);
197 static void usr_update (void);
198 static void create_home (void);
199 static void create_mail (void);
200
201 /*
202  * fail_exit - undo as much as possible
203  */
204 static void fail_exit (int code)
205 {
206         if (home_added) {
207                 rmdir (user_home);
208         }
209
210         if (spw_locked) {
211                 if (spw_unlock () == 0) {
212                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
213                         SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
214 #ifdef WITH_AUDIT
215                         audit_logger (AUDIT_ADD_USER, Prog,
216                                       "unlocking shadow file",
217                                       user_name, AUDIT_NO_ID,
218                                       SHADOW_AUDIT_FAILURE);
219 #endif
220                         /* continue */
221                 }
222         }
223         if (pw_locked) {
224                 if (pw_unlock () == 0) {
225                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
226                         SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
227 #ifdef WITH_AUDIT
228                         audit_logger (AUDIT_ADD_USER, Prog,
229                                       "unlocking passwd file",
230                                       user_name, AUDIT_NO_ID,
231                                       SHADOW_AUDIT_FAILURE);
232 #endif
233                         /* continue */
234                 }
235         }
236         if (gr_locked) {
237                 if (gr_unlock () == 0) {
238                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
239                         SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
240 #ifdef WITH_AUDIT
241                         audit_logger (AUDIT_ADD_USER, Prog,
242                                       "unlocking group file",
243                                       user_name, AUDIT_NO_ID,
244                                       SHADOW_AUDIT_FAILURE);
245 #endif
246                         /* continue */
247                 }
248         }
249 #ifdef  SHADOWGRP
250         if (sgr_locked) {
251                 if (sgr_unlock () == 0) {
252                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
253                         SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
254 #ifdef WITH_AUDIT
255                         audit_logger (AUDIT_ADD_USER, Prog,
256                                       "unlocking gshadow file",
257                                       user_name, AUDIT_NO_ID,
258                                       SHADOW_AUDIT_FAILURE);
259 #endif
260                         /* continue */
261                 }
262         }
263 #endif
264
265 #ifdef WITH_AUDIT
266         audit_logger (AUDIT_ADD_USER, Prog,
267                       "adding user",
268                       user_name, AUDIT_NO_ID,
269                       SHADOW_AUDIT_FAILURE);
270 #endif
271         SYSLOG ((LOG_INFO, "failed adding user '%s', data deleted", user_name));
272         exit (code);
273 }
274
275 #define MATCH(x,y) (strncmp((x),(y),strlen(y)) == 0)
276
277 /*
278  * get_defaults - read the defaults file
279  *
280  *      get_defaults() reads the defaults file for this command. It sets the
281  *      various values from the file, or uses built-in default values if the
282  *      file does not exist.
283  */
284 static void get_defaults (void)
285 {
286         FILE *fp;
287         char buf[1024];
288         char *cp;
289
290         /*
291          * Open the defaults file for reading.
292          */
293
294         fp = fopen (def_file, "r");
295         if (NULL == fp) {
296                 return;
297         }
298
299         /*
300          * Read the file a line at a time. Only the lines that have relevant
301          * values are used, everything else can be ignored.
302          */
303         while (fgets (buf, (int) sizeof buf, fp) == buf) {
304                 cp = strrchr (buf, '\n');
305                 if (NULL != cp) {
306                         *cp = '\0';
307                 }
308
309                 cp = strchr (buf, '=');
310                 if (NULL == cp) {
311                         continue;
312                 }
313
314                 cp++;
315
316                 /*
317                  * Primary GROUP identifier
318                  */
319                 if (MATCH (buf, DGROUP)) {
320                         const struct group *grp = getgr_nam_gid (cp);
321                         if (NULL == grp) {
322                                 fprintf (stderr,
323                                          _("%s: group '%s' does not exist\n"),
324                                          Prog, cp);
325                                 fprintf (stderr,
326                                          _("%s: the %s configuration in %s will be ignored\n"),
327                                          Prog, DGROUP, def_file);
328                         } else {
329                                 def_group = grp->gr_gid;
330                                 def_gname = xstrdup (grp->gr_name);
331                         }
332                 }
333
334                 /*
335                  * Default HOME filesystem
336                  */
337                 else if (MATCH (buf, DHOME)) {
338                         def_home = xstrdup (cp);
339                 }
340
341                 /*
342                  * Default Login Shell command
343                  */
344                 else if (MATCH (buf, DSHELL)) {
345                         def_shell = xstrdup (cp);
346                 }
347
348                 /*
349                  * Default Password Inactive value
350                  */
351                 else if (MATCH (buf, DINACT)) {
352                         if (   (getlong (cp, &def_inactive) == 0)
353                             || (def_inactive < -1)) {
354                                 fprintf (stderr,
355                                          _("%s: invalid numeric argument '%s'\n"),
356                                          Prog, optarg);
357                                 fprintf (stderr,
358                                          _("%s: the %s configuration in %s will be ignored\n"),
359                                          Prog, DINACT, def_file);
360                                 def_inactive = -1;
361                         }
362                 }
363
364                 /*
365                  * Default account expiration date
366                  */
367                 else if (MATCH (buf, DEXPIRE)) {
368                         def_expire = xstrdup (cp);
369                 }
370
371                 /*
372                  * Default Skeleton information
373                  */
374                 else if (MATCH (buf, DSKEL)) {
375                         if ('\0' == *cp) {
376                                 cp = SKEL_DIR;  /* XXX warning: const */
377                         }
378
379                         def_template = xstrdup (cp);
380                 }
381
382                 /*
383                  * Create by default user mail spool or not ?
384                  */
385                 else if (MATCH (buf, DCREATE_MAIL_SPOOL)) {
386                         if (*cp == '\0') {
387                                 cp = DCREATE_MAIL_SPOOL;        /* XXX warning: const */
388                         }
389
390                         def_create_mail_spool = xstrdup (cp);
391                 }
392         }
393         (void) fclose (fp);
394 }
395
396 /*
397  * show_defaults - show the contents of the defaults file
398  *
399  *      show_defaults() displays the values that are used from the default
400  *      file and the built-in values.
401  */
402 static void show_defaults (void)
403 {
404         printf ("GROUP=%u\n", (unsigned int) def_group);
405         printf ("HOME=%s\n", def_home);
406         printf ("INACTIVE=%ld\n", def_inactive);
407         printf ("EXPIRE=%s\n", def_expire);
408         printf ("SHELL=%s\n", def_shell);
409         printf ("SKEL=%s\n", def_template);
410         printf ("CREATE_MAIL_SPOOL=%s\n", def_create_mail_spool);
411 }
412
413 /*
414  * set_defaults - write new defaults file
415  *
416  *      set_defaults() re-writes the defaults file using the values that
417  *      are currently set. Duplicated lines are pruned, missing lines are
418  *      added, and unrecognized lines are copied as is.
419  */
420 static int set_defaults (void)
421 {
422         FILE *ifp;
423         FILE *ofp;
424         char buf[1024];
425         static char new_file[] = NEW_USER_FILE;
426         char *cp;
427         int ofd;
428         int wlen;
429         bool out_group = false;
430         bool out_home = false;
431         bool out_inactive = false;
432         bool out_expire = false;
433         bool out_shell = false;
434         bool out_skel = false;
435         bool out_create_mail_spool = false;
436
437         /*
438          * Create a temporary file to copy the new output to.
439          */
440         ofd = mkstemp (new_file);
441         if (-1 == ofd) {
442                 fprintf (stderr,
443                          _("%s: cannot create new defaults file\n"),
444                          Prog);
445                 return -1;
446         }
447
448         ofp = fdopen (ofd, "w");
449         if (NULL == ofp) {
450                 fprintf (stderr,
451                          _("%s: cannot open new defaults file\n"),
452                          Prog);
453                 return -1;
454         }
455
456         /*
457          * Open the existing defaults file and copy the lines to the
458          * temporary file, using any new values. Each line is checked
459          * to insure that it is not output more than once.
460          */
461         ifp = fopen (def_file, "r");
462         if (NULL == ifp) {
463                 fprintf (ofp, "# useradd defaults file\n");
464                 goto skip;
465         }
466
467         while (fgets (buf, (int) sizeof buf, ifp) == buf) {
468                 cp = strrchr (buf, '\n');
469                 if (NULL != cp) {
470                         *cp = '\0';
471                 } else {
472                         /* A line which does not end with \n is only valid
473                          * at the end of the file.
474                          */
475                         if (feof (ifp) == 0) {
476                                 fprintf (stderr,
477                                          _("%s: line too long in %s: %s..."),
478                                          Prog, def_file, buf);
479                                 return -1;
480                         }
481                 }
482
483                 if (!out_group && MATCH (buf, DGROUP)) {
484                         fprintf (ofp, DGROUP "%u\n", (unsigned int) def_group);
485                         out_group = true;
486                 } else if (!out_home && MATCH (buf, DHOME)) {
487                         fprintf (ofp, DHOME "%s\n", def_home);
488                         out_home = true;
489                 } else if (!out_inactive && MATCH (buf, DINACT)) {
490                         fprintf (ofp, DINACT "%ld\n", def_inactive);
491                         out_inactive = true;
492                 } else if (!out_expire && MATCH (buf, DEXPIRE)) {
493                         fprintf (ofp, DEXPIRE "%s\n", def_expire);
494                         out_expire = true;
495                 } else if (!out_shell && MATCH (buf, DSHELL)) {
496                         fprintf (ofp, DSHELL "%s\n", def_shell);
497                         out_shell = true;
498                 } else if (!out_skel && MATCH (buf, DSKEL)) {
499                         fprintf (ofp, DSKEL "%s\n", def_template);
500                         out_skel = true;
501                 } else if (!out_create_mail_spool
502                            && MATCH (buf, DCREATE_MAIL_SPOOL)) {
503                         fprintf (ofp,
504                                  DCREATE_MAIL_SPOOL "%s\n",
505                                  def_create_mail_spool);
506                         out_create_mail_spool = true;
507                 } else
508                         fprintf (ofp, "%s\n", buf);
509         }
510         (void) fclose (ifp);
511
512       skip:
513         /*
514          * Check each line to insure that every line was output. This
515          * causes new values to be added to a file which did not previously
516          * have an entry for that value.
517          */
518         if (!out_group)
519                 fprintf (ofp, DGROUP "%u\n", (unsigned int) def_group);
520         if (!out_home)
521                 fprintf (ofp, DHOME "%s\n", def_home);
522         if (!out_inactive)
523                 fprintf (ofp, DINACT "%ld\n", def_inactive);
524         if (!out_expire)
525                 fprintf (ofp, DEXPIRE "%s\n", def_expire);
526         if (!out_shell)
527                 fprintf (ofp, DSHELL "%s\n", def_shell);
528         if (!out_skel)
529                 fprintf (ofp, DSKEL "%s\n", def_template);
530
531         if (!out_create_mail_spool)
532                 fprintf (ofp, DCREATE_MAIL_SPOOL "%s\n", def_create_mail_spool);
533
534         /*
535          * Flush and close the file. Check for errors to make certain
536          * the new file is intact.
537          */
538         (void) fflush (ofp);
539         if (   (ferror (ofp) != 0)
540             || (fsync (fileno (ofp)) != 0)
541             || (fclose (ofp) != 0)) {
542                 unlink (new_file);
543                 return -1;
544         }
545
546         /*
547          * Rename the current default file to its backup name.
548          */
549         wlen = snprintf (buf, sizeof buf, "%s-", def_file);
550         assert (wlen < (int) sizeof buf);
551         if ((rename (def_file, buf) != 0) && (ENOENT != errno)) {
552                 int err = errno;
553                 fprintf (stderr,
554                          _("%s: rename: %s: %s"),
555                          Prog, def_file, strerror (err));
556                 unlink (new_file);
557                 return -1;
558         }
559
560         /*
561          * Rename the new default file to its correct name.
562          */
563         if (rename (new_file, def_file) != 0) {
564                 int err = errno;
565                 fprintf (stderr,
566                          _("%s: rename: %s: %s"),
567                          Prog, new_file, strerror (err));
568                 return -1;
569         }
570 #ifdef WITH_AUDIT
571         audit_logger (AUDIT_USYS_CONFIG, Prog,
572                       "changing useradd defaults",
573                       NULL, AUDIT_NO_ID,
574                       SHADOW_AUDIT_SUCCESS);
575 #endif
576         SYSLOG ((LOG_INFO,
577                  "useradd defaults: GROUP=%u, HOME=%s, SHELL=%s, INACTIVE=%ld, "
578                  "EXPIRE=%s, SKEL=%s, CREATE_MAIL_SPOOL=%s",
579                  (unsigned int) def_group, def_home, def_shell,
580                  def_inactive, def_expire, def_template,
581                  def_create_mail_spool));
582         return 0;
583 }
584
585 /*
586  * get_groups - convert a list of group names to an array of group IDs
587  *
588  *      get_groups() takes a comma-separated list of group names and
589  *      converts it to a NULL-terminated array. Any unknown group
590  *      names are reported as errors.
591  */
592 static int get_groups (char *list)
593 {
594         char *cp;
595         const struct group *grp;
596         int errors = 0;
597         int ngroups = 0;
598
599         if ('\0' == *list) {
600                 return 0;
601         }
602
603         /*
604          * So long as there is some data to be converted, strip off
605          * each name and look it up. A mix of numerical and string
606          * values for group identifiers is permitted.
607          */
608         do {
609                 /*
610                  * Strip off a single name from the list
611                  */
612                 cp = strchr (list, ',');
613                 if (NULL != cp) {
614                         *cp++ = '\0';
615                 }
616
617                 /*
618                  * Names starting with digits are treated as numerical
619                  * GID values, otherwise the string is looked up as is.
620                  */
621                 grp = getgr_nam_gid (list);
622
623                 /*
624                  * There must be a match, either by GID value or by
625                  * string name.
626                  */
627                 if (NULL == grp) {
628                         fprintf (stderr,
629                                  _("%s: group '%s' does not exist\n"),
630                                  Prog, list);
631                         errors++;
632                 }
633                 list = cp;
634
635                 /*
636                  * If the group doesn't exist, don't dump core...
637                  * Instead, try the next one.  --marekm
638                  */
639                 if (NULL == grp) {
640                         continue;
641                 }
642
643 #ifdef  USE_NIS
644                 /*
645                  * Don't add this group if they are an NIS group. Tell
646                  * the user to go to the server for this group.
647                  */
648                 if (__isgrNIS ()) {
649                         fprintf (stderr,
650                                  _("%s: group '%s' is a NIS group.\n"),
651                                  Prog, grp->gr_name);
652                         continue;
653                 }
654 #endif
655
656                 if (ngroups == sys_ngroups) {
657                         fprintf (stderr,
658                                  _("%s: too many groups specified (max %d).\n"),
659                                  Prog, ngroups);
660                         break;
661                 }
662
663                 /*
664                  * Add the group name to the user's list of groups.
665                  */
666                 user_groups[ngroups++] = xstrdup (grp->gr_name);
667         } while (NULL != list);
668
669         user_groups[ngroups] = (char *) 0;
670
671         /*
672          * Any errors in finding group names are fatal
673          */
674         if (0 != errors) {
675                 return -1;
676         }
677
678         return 0;
679 }
680
681 /*
682  * usage - display usage message and exit
683  */
684 static void usage (int status)
685 {
686         FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
687         (void) fprintf (usageout,
688                         _("Usage: %s [options] LOGIN\n"
689                           "\n"
690                           "Options:\n"),
691                         Prog);
692         (void) fputs (_("  -b, --base-dir BASE_DIR       base directory for the home directory of the\n"
693                         "                                new account\n"), usageout);
694         (void) fputs (_("  -c, --comment COMMENT         GECOS field of the new account\n"), usageout);
695         (void) fputs (_("  -d, --home-dir HOME_DIR       home directory of the new account\n"), usageout);
696         (void) fputs (_("  -D, --defaults                print or change default useradd configuration\n"), usageout);
697         (void) fputs (_("  -e, --expiredate EXPIRE_DATE  expiration date of the new account\n"), usageout);
698         (void) fputs (_("  -f, --inactive INACTIVE       password inactivity period of the new account\n"), usageout);
699         (void) fputs (_("  -g, --gid GROUP               name or ID of the primary group of the new\n"
700                         "                                account\n"), usageout);
701         (void) fputs (_("  -G, --groups GROUPS           list of supplementary groups of the new\n"
702                         "                                account\n"), usageout);
703         (void) fputs (_("  -h, --help                    display this help message and exit\n"), usageout);
704         (void) fputs (_("  -k, --skel SKEL_DIR           use this alternative skeleton directory\n"), usageout);
705         (void) fputs (_("  -K, --key KEY=VALUE           override /etc/login.defs defaults\n"), usageout);
706         (void) fputs (_("  -l, --no-log-init             do not add the user to the lastlog and\n"
707                         "                                faillog databases\n"), usageout);
708         (void) fputs (_("  -m, --create-home             create the user's home directory\n"), usageout);
709         (void) fputs (_("  -M, --no-create-home          do not create the user's home directory\n"), usageout);
710         (void) fputs (_("  -N, --no-user-group           do not create a group with the same name as\n"
711                         "                                the user\n"), usageout);
712         (void) fputs (_("  -o, --non-unique              allow to create users with duplicate\n"
713                         "                                (non-unique) UID\n"), usageout);
714         (void) fputs (_("  -p, --password PASSWORD       encrypted password of the new account\n"), usageout);
715         (void) fputs (_("  -r, --system                  create a system account\n"), usageout);
716         (void) fputs (_("  -s, --shell SHELL             login shell of the new account\n"), usageout);
717         (void) fputs (_("  -u, --uid UID                 user ID of the new account\n"), usageout);
718         (void) fputs (_("  -U, --user-group              create a group with the same name as the user\n"), usageout);
719 #ifdef WITH_SELINUX
720         (void) fputs (_("  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping\n"), usageout);
721 #endif
722         (void) fputs ("\n", usageout);
723         exit (status);
724 }
725
726 /*
727  * new_pwent - initialize the values in a password file entry
728  *
729  *      new_pwent() takes all of the values that have been entered and
730  *      fills in a (struct passwd) with them.
731  */
732 static void new_pwent (struct passwd *pwent)
733 {
734         memzero (pwent, sizeof *pwent);
735         pwent->pw_name = (char *) user_name;
736         if (is_shadow_pwd) {
737                 pwent->pw_passwd = (char *) SHADOW_PASSWD_STRING;
738         } else {
739                 pwent->pw_passwd = (char *) user_pass;
740         }
741
742         pwent->pw_uid = user_id;
743         pwent->pw_gid = user_gid;
744         pwent->pw_gecos = (char *) user_comment;
745         pwent->pw_dir = (char *) user_home;
746         pwent->pw_shell = (char *) user_shell;
747 }
748
749 static long scale_age (long x)
750 {
751         if (x <= 0) {
752                 return x;
753         }
754
755         return x * (DAY / SCALE);
756 }
757
758 /*
759  * new_spent - initialize the values in a shadow password file entry
760  *
761  *      new_spent() takes all of the values that have been entered and
762  *      fills in a (struct spwd) with them.
763  */
764 static void new_spent (struct spwd *spent)
765 {
766         memzero (spent, sizeof *spent);
767         spent->sp_namp = (char *) user_name;
768         spent->sp_pwdp = (char *) user_pass;
769         spent->sp_lstchg = (long) time ((time_t *) 0) / SCALE;
770         if (0 == spent->sp_lstchg) {
771                 /* Better disable aging than requiring a password change */
772                 spent->sp_lstchg = -1;
773         }
774         if (!rflg) {
775                 spent->sp_min = scale_age (getdef_num ("PASS_MIN_DAYS", -1));
776                 spent->sp_max = scale_age (getdef_num ("PASS_MAX_DAYS", -1));
777                 spent->sp_warn = scale_age (getdef_num ("PASS_WARN_AGE", -1));
778                 spent->sp_inact = scale_age (def_inactive);
779                 spent->sp_expire = scale_age (user_expire);
780         } else {
781                 spent->sp_min = scale_age (-1);
782                 spent->sp_max = scale_age (-1);
783                 spent->sp_warn = scale_age (-1);
784                 spent->sp_inact = scale_age (-1);
785                 spent->sp_expire = scale_age (-1);
786         }
787         spent->sp_flag = SHADOW_SP_FLAG_UNSET;
788 }
789
790 /*
791  * grp_update - add user to secondary group set
792  *
793  *      grp_update() takes the secondary group set given in user_groups
794  *      and adds the user to each group given by that set.
795  *
796  *      The group files are opened and locked in open_files().
797  *
798  *      close_files() should be called afterwards to commit the changes
799  *      and unlocking the group files.
800  */
801 static void grp_update (void)
802 {
803         const struct group *grp;
804         struct group *ngrp;
805
806 #ifdef  SHADOWGRP
807         const struct sgrp *sgrp;
808         struct sgrp *nsgrp;
809 #endif
810
811         /*
812          * Scan through the entire group file looking for the groups that
813          * the user is a member of.
814          */
815         for (gr_rewind (), grp = gr_next (); NULL != grp; grp = gr_next ()) {
816
817                 /*
818                  * See if the user specified this group as one of their
819                  * concurrent groups.
820                  */
821                 if (!is_on_list (user_groups, grp->gr_name)) {
822                         continue;
823                 }
824
825                 /*
826                  * Make a copy - gr_update() will free() everything
827                  * from the old entry, and we need it later.
828                  */
829                 ngrp = __gr_dup (grp);
830                 if (NULL == ngrp) {
831                         fprintf (stderr,
832                                  _("%s: Out of memory. Cannot update %s.\n"),
833                                  Prog, gr_dbname ());
834                         SYSLOG ((LOG_ERR, "failed to prepare the new %s entry '%s'", gr_dbname (), user_name));
835 #ifdef WITH_AUDIT
836                         audit_logger (AUDIT_ADD_USER, Prog,
837                                       "adding user to group",
838                                       user_name, AUDIT_NO_ID,
839                                       SHADOW_AUDIT_FAILURE);
840 #endif
841                         fail_exit (E_GRP_UPDATE);       /* XXX */
842                 }
843
844                 /* 
845                  * Add the username to the list of group members and
846                  * update the group entry to reflect the change.
847                  */
848                 ngrp->gr_mem = add_list (ngrp->gr_mem, user_name);
849                 if (gr_update (ngrp) == 0) {
850                         fprintf (stderr,
851                                  _("%s: failed to prepare the new %s entry '%s'\n"),
852                                  Prog, gr_dbname (), ngrp->gr_name);
853                         SYSLOG ((LOG_ERR, "failed to prepare the new %s entry '%s'", gr_dbname (), user_name));
854 #ifdef WITH_AUDIT
855                         audit_logger (AUDIT_ADD_USER, Prog,
856                                       "adding user to group",
857                                       user_name, AUDIT_NO_ID,
858                                       SHADOW_AUDIT_FAILURE);
859 #endif
860                         fail_exit (E_GRP_UPDATE);
861                 }
862 #ifdef WITH_AUDIT
863                 audit_logger (AUDIT_ADD_USER, Prog,
864                               "adding user to group",
865                               user_name, AUDIT_NO_ID,
866                               SHADOW_AUDIT_SUCCESS);
867 #endif
868                 SYSLOG ((LOG_INFO,
869                          "add '%s' to group '%s'",
870                          user_name, ngrp->gr_name));
871         }
872
873 #ifdef  SHADOWGRP
874         if (!is_shadow_grp)
875                 return;
876
877         /*
878          * Scan through the entire shadow group file looking for the groups
879          * that the user is a member of. The administrative list isn't
880          * modified.
881          */
882         for (sgr_rewind (), sgrp = sgr_next (); NULL != sgrp; sgrp = sgr_next ()) {
883
884                 /*
885                  * See if the user specified this group as one of their
886                  * concurrent groups.
887                  */
888                 if (gr_locate (sgrp->sg_name) == NULL) {
889                         continue;
890                 }
891
892                 if (!is_on_list (user_groups, sgrp->sg_name)) {
893                         continue;
894                 }
895
896                 /*
897                  * Make a copy - sgr_update() will free() everything
898                  * from the old entry, and we need it later.
899                  */
900                 nsgrp = __sgr_dup (sgrp);
901                 if (NULL == nsgrp) {
902                         fprintf (stderr,
903                                  _("%s: Out of memory. Cannot update %s.\n"),
904                                  Prog, sgr_dbname ());
905                         SYSLOG ((LOG_ERR, "failed to prepare the new %s entry '%s'", sgr_dbname (), user_name));
906 #ifdef WITH_AUDIT
907                         audit_logger (AUDIT_ADD_USER, Prog,
908                                       "adding user to shadow group",
909                                       user_name, AUDIT_NO_ID,
910                                       SHADOW_AUDIT_FAILURE);
911 #endif
912                         fail_exit (E_GRP_UPDATE);       /* XXX */
913                 }
914
915                 /* 
916                  * Add the username to the list of group members and
917                  * update the group entry to reflect the change.
918                  */
919                 nsgrp->sg_mem = add_list (nsgrp->sg_mem, user_name);
920                 if (sgr_update (nsgrp) == 0) {
921                         fprintf (stderr,
922                                  _("%s: failed to prepare the new %s entry '%s'\n"),
923                                  Prog, sgr_dbname (), nsgrp->sg_name);
924                         SYSLOG ((LOG_ERR, "failed to prepare the new %s entry '%s'", sgr_dbname (), user_name));
925 #ifdef WITH_AUDIT
926                         audit_logger (AUDIT_ADD_USER, Prog,
927                                       "adding user to shadow group",
928                                       user_name, AUDIT_NO_ID,
929                                       SHADOW_AUDIT_FAILURE);
930 #endif
931                         fail_exit (E_GRP_UPDATE);
932                 }
933 #ifdef WITH_AUDIT
934                 audit_logger (AUDIT_ADD_USER, Prog,
935                               "adding user to shadow group",
936                               user_name, AUDIT_NO_ID,
937                               SHADOW_AUDIT_SUCCESS);
938 #endif
939                 SYSLOG ((LOG_INFO,
940                          "add '%s' to shadow group '%s'",
941                          user_name, nsgrp->sg_name));
942         }
943 #endif                          /* SHADOWGRP */
944 }
945
946 /*
947  * process_flags - perform command line argument setting
948  *
949  *      process_flags() interprets the command line arguments and sets
950  *      the values that the user will be created with accordingly. The
951  *      values are checked for sanity.
952  */
953 static void process_flags (int argc, char **argv)
954 {
955         const struct group *grp;
956         bool anyflag = false;
957         char *cp;
958
959         {
960                 /*
961                  * Parse the command line options.
962                  */
963                 int c;
964                 static struct option long_options[] = {
965                         {"base-dir", required_argument, NULL, 'b'},
966                         {"comment", required_argument, NULL, 'c'},
967                         {"home-dir", required_argument, NULL, 'd'},
968                         {"defaults", no_argument, NULL, 'D'},
969                         {"expiredate", required_argument, NULL, 'e'},
970                         {"inactive", required_argument, NULL, 'f'},
971                         {"gid", required_argument, NULL, 'g'},
972                         {"groups", required_argument, NULL, 'G'},
973                         {"help", no_argument, NULL, 'h'},
974                         {"skel", required_argument, NULL, 'k'},
975                         {"key", required_argument, NULL, 'K'},
976                         {"create-home", no_argument, NULL, 'm'},
977                         {"no-create-home", no_argument, NULL, 'M'},
978                         {"no-log-init", no_argument, NULL, 'l'},
979                         {"no-user-group", no_argument, NULL, 'N'},
980                         {"non-unique", no_argument, NULL, 'o'},
981                         {"password", required_argument, NULL, 'p'},
982                         {"system", no_argument, NULL, 'r'},
983                         {"shell", required_argument, NULL, 's'},
984 #ifdef WITH_SELINUX
985                         {"selinux-user", required_argument, NULL, 'Z'},
986 #endif
987                         {"uid", required_argument, NULL, 'u'},
988                         {"user-group", no_argument, NULL, 'U'},
989                         {NULL, 0, NULL, '\0'}
990                 };
991                 while ((c = getopt_long (argc, argv,
992 #ifdef WITH_SELINUX
993                                          "b:c:d:De:f:g:G:hk:K:lmMNop:rs:u:UZ:",
994 #else
995                                          "b:c:d:De:f:g:G:hk:K:lmMNop:rs:u:U",
996 #endif
997                                          long_options, NULL)) != -1) {
998                         switch (c) {
999                         case 'b':
1000                                 if (   ( !VALID (optarg) )
1001                                     || ( optarg[0] != '/' )) {
1002                                         fprintf (stderr,
1003                                                  _("%s: invalid base directory '%s'\n"),
1004                                                  Prog, optarg);
1005                                         exit (E_BAD_ARG);
1006                                 }
1007                                 def_home = optarg;
1008                                 bflg = true;
1009                                 break;
1010                         case 'c':
1011                                 if (!VALID (optarg)) {
1012                                         fprintf (stderr,
1013                                                  _("%s: invalid comment '%s'\n"),
1014                                                  Prog, optarg);
1015                                         exit (E_BAD_ARG);
1016                                 }
1017                                 user_comment = optarg;
1018                                 cflg = true;
1019                                 break;
1020                         case 'd':
1021                                 if (   ( !VALID (optarg) )
1022                                     || ( optarg[0] != '/' )) {
1023                                         fprintf (stderr,
1024                                                  _("%s: invalid home directory '%s'\n"),
1025                                                  Prog, optarg);
1026                                         exit (E_BAD_ARG);
1027                                 }
1028                                 user_home = optarg;
1029                                 dflg = true;
1030                                 break;
1031                         case 'D':
1032                                 if (anyflag) {
1033                                         usage (E_USAGE);
1034                                 }
1035                                 Dflg = true;
1036                                 break;
1037                         case 'e':
1038                                 if ('\0' != *optarg) {
1039                                         user_expire = strtoday (optarg);
1040                                         if (user_expire == -1) {
1041                                                 fprintf (stderr,
1042                                                          _("%s: invalid date '%s'\n"),
1043                                                          Prog, optarg);
1044                                                 exit (E_BAD_ARG);
1045                                         }
1046                                 } else {
1047                                         user_expire = -1;
1048                                 }
1049
1050                                 /*
1051                                  * -e "" is allowed - it's a no-op without /etc/shadow
1052                                  */
1053                                 if (('\0' != *optarg) && !is_shadow_pwd) {
1054                                         fprintf (stderr,
1055                                                  _("%s: shadow passwords required for -e\n"),
1056                                                  Prog);
1057                                         exit (E_USAGE);
1058                                 }
1059                                 if (Dflg) {
1060                                         def_expire = optarg;
1061                                 }
1062                                 eflg = true;
1063                                 break;
1064                         case 'f':
1065                                 if (   (getlong (optarg, &def_inactive) == 0)
1066                                     || (def_inactive < -1)) {
1067                                         fprintf (stderr,
1068                                                  _("%s: invalid numeric argument '%s'\n"),
1069                                                  Prog, optarg);
1070                                         usage (E_USAGE);
1071                                 }
1072                                 /*
1073                                  * -f -1 is allowed
1074                                  * it's a no-op without /etc/shadow
1075                                  */
1076                                 if ((-1 != def_inactive) && !is_shadow_pwd) {
1077                                         fprintf (stderr,
1078                                                  _("%s: shadow passwords required for -f\n"),
1079                                                  Prog);
1080                                         exit (E_USAGE);
1081                                 }
1082                                 fflg = true;
1083                                 break;
1084                         case 'g':
1085                                 grp = getgr_nam_gid (optarg);
1086                                 if (NULL == grp) {
1087                                         fprintf (stderr,
1088                                                  _("%s: group '%s' does not exist\n"),
1089                                                  Prog, optarg);
1090                                         exit (E_NOTFOUND);
1091                                 }
1092                                 if (Dflg) {
1093                                         def_group = grp->gr_gid;
1094                                         def_gname = optarg;
1095                                 } else {
1096                                         user_gid = grp->gr_gid;
1097                                 }
1098                                 gflg = true;
1099                                 break;
1100                         case 'G':
1101                                 if (get_groups (optarg) != 0) {
1102                                         exit (E_NOTFOUND);
1103                                 }
1104                                 if (NULL != user_groups[0]) {
1105                                         do_grp_update = true;
1106                                 }
1107                                 Gflg = true;
1108                                 break;
1109                         case 'h':
1110                                 usage (E_SUCCESS);
1111                                 break;
1112                         case 'k':
1113                                 def_template = optarg;
1114                                 kflg = true;
1115                                 break;
1116                         case 'K':
1117                                 /*
1118                                  * override login.defs defaults (-K name=value)
1119                                  * example: -K UID_MIN=100 -K UID_MAX=499
1120                                  * note: -K UID_MIN=10,UID_MAX=499 doesn't work yet
1121                                  */
1122                                 cp = strchr (optarg, '=');
1123                                 if (NULL == cp) {
1124                                         fprintf (stderr,
1125                                                  _("%s: -K requires KEY=VALUE\n"),
1126                                                  Prog);
1127                                         exit (E_BAD_ARG);
1128                                 }
1129                                 /* terminate name, point to value */
1130                                 *cp = '\0';
1131                                 cp++;
1132                                 if (putdef_str (optarg, cp) < 0) {
1133                                         exit (E_BAD_ARG);
1134                                 }
1135                                 break;
1136                         case 'l':
1137                                 lflg = true;
1138                                 break;
1139                         case 'm':
1140                                 mflg = true;
1141                                 break;
1142                         case 'M':
1143                                 Mflg = true;
1144                                 break;
1145                         case 'N':
1146                                 Nflg = true;
1147                                 break;
1148                         case 'o':
1149                                 oflg = true;
1150                                 break;
1151                         case 'p':       /* set encrypted password */
1152                                 if (!VALID (optarg)) {
1153                                         fprintf (stderr,
1154                                                  _("%s: invalid field '%s'\n"),
1155                                                  Prog, optarg);
1156                                         exit (E_BAD_ARG);
1157                                 }
1158                                 user_pass = optarg;
1159                                 break;
1160                         case 'r':
1161                                 rflg = true;
1162                                 break;
1163                         case 's':
1164                                 if (   ( !VALID (optarg) )
1165                                     || (   ('\0' != optarg[0])
1166                                         && ('/'  != optarg[0])
1167                                         && ('*'  != optarg[0]) )) {
1168                                         fprintf (stderr,
1169                                                  _("%s: invalid shell '%s'\n"),
1170                                                  Prog, optarg);
1171                                         exit (E_BAD_ARG);
1172                                 }
1173                                 user_shell = optarg;
1174                                 def_shell = optarg;
1175                                 sflg = true;
1176                                 break;
1177                         case 'u':
1178                                 if (   (get_uid (optarg, &user_id) == 0)
1179                                     || (user_id == (gid_t)-1)) {
1180                                         fprintf (stderr,
1181                                                  _("%s: invalid user ID '%s'\n"),
1182                                                  Prog, optarg);
1183                                         exit (E_BAD_ARG);
1184                                 }
1185                                 uflg = true;
1186                                 break;
1187                         case 'U':
1188                                 Uflg = true;
1189                                 break;
1190 #ifdef WITH_SELINUX
1191                         case 'Z':
1192                                 if (is_selinux_enabled () > 0) {
1193                                         user_selinux = optarg;
1194                                         Zflg = true;
1195                                 } else {
1196                                         fprintf (stderr,
1197                                                  _("%s: -Z requires SELinux enabled kernel\n"),
1198                                                  Prog);
1199
1200                                         exit (E_BAD_ARG);
1201                                 }
1202                                 break;
1203 #endif
1204                         default:
1205                                 usage (E_USAGE);
1206                         }
1207                         anyflag = true;
1208                 }
1209         }
1210
1211         if (!gflg && !Nflg && !Uflg) {
1212                 /* Get the settings from login.defs */
1213                 Uflg = getdef_bool ("USERGROUPS_ENAB");
1214         }
1215
1216         /*
1217          * Certain options are only valid in combination with others.
1218          * Check it here so that they can be specified in any order.
1219          */
1220         if (oflg && !uflg) {
1221                 fprintf (stderr,
1222                          _("%s: %s flag is only allowed with the %s flag\n"),
1223                          Prog, "-o", "-u");
1224                 usage (E_USAGE);
1225         }
1226         if (kflg && !mflg) {
1227                 fprintf (stderr,
1228                          _("%s: %s flag is only allowed with the %s flag\n"),
1229                          Prog, "-k", "-m");
1230                 usage (E_USAGE);
1231         }
1232         if (Uflg && gflg) {
1233                 fprintf (stderr,
1234                          _("%s: options %s and %s conflict\n"),
1235                          Prog, "-U", "-g");
1236                 usage (E_USAGE);
1237         }
1238         if (Uflg && Nflg) {
1239                 fprintf (stderr,
1240                          _("%s: options %s and %s conflict\n"),
1241                          Prog, "-U", "-N");
1242                 usage (E_USAGE);
1243         }
1244         if (mflg && Mflg) {
1245                 fprintf (stderr,
1246                          _("%s: options %s and %s conflict\n"),
1247                          Prog, "-m", "-M");
1248                 usage (E_USAGE);
1249         }
1250
1251         /*
1252          * Either -D or username is required. Defaults can be set with -D
1253          * for the -b, -e, -f, -g, -s options only.
1254          */
1255         if (Dflg) {
1256                 if (optind != argc) {
1257                         usage (E_USAGE);
1258                 }
1259
1260                 if (uflg || oflg || Gflg || dflg || cflg || mflg) {
1261                         usage (E_USAGE);
1262                 }
1263         } else {
1264                 if (optind != argc - 1) {
1265                         usage (E_USAGE);
1266                 }
1267
1268                 user_name = argv[optind];
1269                 if (!is_valid_user_name (user_name)) {
1270                         fprintf (stderr,
1271                                  _("%s: invalid user name '%s'\n"),
1272                                  Prog, user_name);
1273 #ifdef WITH_AUDIT
1274                         audit_logger (AUDIT_ADD_USER, Prog,
1275                                       "adding user",
1276                                       user_name, AUDIT_NO_ID,
1277                                       SHADOW_AUDIT_FAILURE);
1278 #endif
1279                         exit (E_BAD_ARG);
1280                 }
1281                 if (!dflg) {
1282                         char *uh;
1283                         size_t len = strlen (def_home) + strlen (user_name) + 2;
1284                         int wlen;
1285
1286                         uh = xmalloc (len);
1287                         wlen = snprintf (uh, len, "%s/%s", def_home, user_name);
1288                         assert (wlen == (int) len -1);
1289
1290                         user_home = uh;
1291                 }
1292         }
1293
1294         if (!eflg) {
1295                 user_expire = strtoday (def_expire);
1296         }
1297
1298         if (!gflg) {
1299                 user_gid = def_group;
1300         }
1301
1302         if (!sflg) {
1303                 user_shell = def_shell;
1304         }
1305
1306         create_mail_spool = def_create_mail_spool;
1307
1308         if (!rflg) {
1309                 /* for system accounts defaults are ignored and we
1310                  * do not create a home dir */
1311                 if (getdef_bool("CREATE_HOME")) {
1312                         mflg = true;
1313                 }
1314         }
1315
1316         if (Mflg) {
1317                 /* absolutely sure that we do not create home dirs */
1318                 mflg = false;
1319         }
1320 }
1321
1322 /*
1323  * close_files - close all of the files that were opened
1324  *
1325  *      close_files() closes all of the files that were opened for this
1326  *      new user. This causes any modified entries to be written out.
1327  */
1328 static void close_files (void)
1329 {
1330         if (pw_close () == 0) {
1331                 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, pw_dbname ());
1332                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
1333                 fail_exit (E_PW_UPDATE);
1334         }
1335         if (is_shadow_pwd && (spw_close () == 0)) {
1336                 fprintf (stderr,
1337                          _("%s: failure while writing changes to %s\n"), Prog, spw_dbname ());
1338                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", spw_dbname ()));
1339                 fail_exit (E_PW_UPDATE);
1340         }
1341         if (do_grp_update) {
1342                 if (gr_close () == 0) {
1343                         fprintf (stderr,
1344                                  _("%s: failure while writing changes to %s\n"), Prog, gr_dbname ());
1345                         SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
1346                         fail_exit (E_GRP_UPDATE);
1347                 }
1348 #ifdef  SHADOWGRP
1349                 if (is_shadow_grp && (sgr_close () == 0)) {
1350                         fprintf (stderr,
1351                                  _("%s: failure while writing changes to %s\n"),
1352                                  Prog, sgr_dbname ());
1353                         SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
1354                         fail_exit (E_GRP_UPDATE);
1355                 }
1356 #endif
1357         }
1358         if (is_shadow_pwd) {
1359                 if (spw_unlock () == 0) {
1360                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
1361                         SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
1362 #ifdef WITH_AUDIT
1363                         audit_logger (AUDIT_ADD_USER, Prog,
1364                                       "unlocking shadow file",
1365                                       user_name, AUDIT_NO_ID,
1366                                       SHADOW_AUDIT_FAILURE);
1367 #endif
1368                         /* continue */
1369                 }
1370                 spw_locked = false;
1371         }
1372         if (pw_unlock () == 0) {
1373                 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
1374                 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
1375 #ifdef WITH_AUDIT
1376                 audit_logger (AUDIT_ADD_USER, Prog,
1377                               "unlocking passwd file",
1378                               user_name, AUDIT_NO_ID,
1379                               SHADOW_AUDIT_FAILURE);
1380 #endif
1381                 /* continue */
1382         }
1383         pw_locked = false;
1384         if (gr_unlock () == 0) {
1385                 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
1386                 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
1387 #ifdef WITH_AUDIT
1388                 audit_logger (AUDIT_ADD_USER, Prog,
1389                               "unlocking group file",
1390                               user_name, AUDIT_NO_ID,
1391                               SHADOW_AUDIT_FAILURE);
1392 #endif
1393                 /* continue */
1394         }
1395         gr_locked = false;
1396 #ifdef  SHADOWGRP
1397         if (is_shadow_grp) {
1398                 if (sgr_unlock () == 0) {
1399                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
1400                         SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
1401 #ifdef WITH_AUDIT
1402                         audit_logger (AUDIT_ADD_USER, Prog,
1403                                       "unlocking gshadow file",
1404                                       user_name, AUDIT_NO_ID,
1405                                       SHADOW_AUDIT_FAILURE);
1406 #endif
1407                         /* continue */
1408                 }
1409                 sgr_locked = false;
1410         }
1411 #endif
1412 }
1413
1414 /*
1415  * open_files - lock and open the password files
1416  *
1417  *      open_files() opens the two password files.
1418  */
1419 static void open_files (void)
1420 {
1421         if (pw_lock () == 0) {
1422                 fprintf (stderr,
1423                          _("%s: cannot lock %s; try again later.\n"),
1424                          Prog, pw_dbname ());
1425                 exit (E_PW_UPDATE);
1426         }
1427         pw_locked = true;
1428         if (pw_open (O_RDWR) == 0) {
1429                 fprintf (stderr, _("%s: cannot open %s\n"), Prog, pw_dbname ());
1430                 fail_exit (E_PW_UPDATE);
1431         }
1432         if (is_shadow_pwd) {
1433                 if (spw_lock () == 0) {
1434                         fprintf (stderr,
1435                                  _("%s: cannot lock %s; try again later.\n"),
1436                                  Prog, spw_dbname ());
1437                         fail_exit (E_PW_UPDATE);
1438                 }
1439                 spw_locked = true;
1440                 if (spw_open (O_RDWR) == 0) {
1441                         fprintf (stderr,
1442                                  _("%s: cannot open %s\n"),
1443                                  Prog, spw_dbname ());
1444                         fail_exit (E_PW_UPDATE);
1445                 }
1446         }
1447
1448         /*
1449          * Lock and open the group file.
1450          */
1451         if (gr_lock () == 0) {
1452                 fprintf (stderr,
1453                          _("%s: cannot lock %s; try again later.\n"),
1454                          Prog, gr_dbname ());
1455                 fail_exit (E_GRP_UPDATE);
1456         }
1457         gr_locked = true;
1458         if (gr_open (O_RDWR) == 0) {
1459                 fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
1460                 fail_exit (E_GRP_UPDATE);
1461         }
1462 #ifdef  SHADOWGRP
1463         if (is_shadow_grp) {
1464                 if (sgr_lock () == 0) {
1465                         fprintf (stderr,
1466                                  _("%s: cannot lock %s; try again later.\n"),
1467                                  Prog, sgr_dbname ());
1468                         fail_exit (E_GRP_UPDATE);
1469                 }
1470                 sgr_locked = true;
1471                 if (sgr_open (O_RDWR) == 0) {
1472                         fprintf (stderr,
1473                                  _("%s: cannot open %s\n"),
1474                                  Prog, sgr_dbname ());
1475                         fail_exit (E_GRP_UPDATE);
1476                 }
1477         }
1478 #endif
1479 }
1480
1481 static char *empty_list = NULL;
1482
1483 /*
1484  * new_grent - initialize the values in a group file entry
1485  *
1486  *      new_grent() takes all of the values that have been entered and fills
1487  *      in a (struct group) with them.
1488  */
1489
1490 static void new_grent (struct group *grent)
1491 {
1492         memzero (grent, sizeof *grent);
1493         grent->gr_name = (char *) user_name;
1494         grent->gr_passwd = SHADOW_PASSWD_STRING;        /* XXX warning: const */
1495         grent->gr_gid = user_gid;
1496         grent->gr_mem = &empty_list;
1497 }
1498
1499 #ifdef  SHADOWGRP
1500 /*
1501  * new_sgent - initialize the values in a shadow group file entry
1502  *
1503  *      new_sgent() takes all of the values that have been entered and fills
1504  *      in a (struct sgrp) with them.
1505  */
1506
1507 static void new_sgent (struct sgrp *sgent)
1508 {
1509         memzero (sgent, sizeof *sgent);
1510         sgent->sg_name = (char *) user_name;
1511         sgent->sg_passwd = "!"; /* XXX warning: const */
1512         sgent->sg_adm = &empty_list;
1513         sgent->sg_mem = &empty_list;
1514 }
1515 #endif                          /* SHADOWGRP */
1516
1517
1518 /*
1519  * grp_add - add new group file entries
1520  *
1521  *      grp_add() writes the new records to the group files.
1522  */
1523
1524 static void grp_add (void)
1525 {
1526         struct group grp;
1527
1528 #ifdef  SHADOWGRP
1529         struct sgrp sgrp;
1530 #endif                          /* SHADOWGRP */
1531
1532         /*
1533          * Create the initial entries for this new group.
1534          */
1535         new_grent (&grp);
1536 #ifdef  SHADOWGRP
1537         new_sgent (&sgrp);
1538 #endif                          /* SHADOWGRP */
1539
1540         /*
1541          * Write out the new group file entry.
1542          */
1543         if (gr_update (&grp) == 0) {
1544                 fprintf (stderr,
1545                          _("%s: failed to prepare the new %s entry '%s'\n"),
1546                          Prog, gr_dbname (), grp.gr_name);
1547 #ifdef WITH_AUDIT
1548                 audit_logger (AUDIT_ADD_GROUP, Prog,
1549                               "adding group",
1550                               grp.gr_name, AUDIT_NO_ID,
1551                               SHADOW_AUDIT_FAILURE);
1552 #endif
1553                 fail_exit (E_GRP_UPDATE);
1554         }
1555 #ifdef  SHADOWGRP
1556         /*
1557          * Write out the new shadow group entries as well.
1558          */
1559         if (is_shadow_grp && (sgr_update (&sgrp) == 0)) {
1560                 fprintf (stderr,
1561                          _("%s: failed to prepare the new %s entry '%s'\n"),
1562                          Prog, sgr_dbname (), sgrp.sg_name);
1563 #ifdef WITH_AUDIT
1564                 audit_logger (AUDIT_ADD_GROUP, Prog,
1565                               "adding group",
1566                               grp.gr_name, AUDIT_NO_ID,
1567                               SHADOW_AUDIT_FAILURE);
1568 #endif
1569                 fail_exit (E_GRP_UPDATE);
1570         }
1571 #endif                          /* SHADOWGRP */
1572         SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u", user_name, user_gid));
1573 #ifdef WITH_AUDIT
1574         audit_logger (AUDIT_ADD_GROUP, Prog,
1575                       "adding group",
1576                       grp.gr_name, AUDIT_NO_ID,
1577                       SHADOW_AUDIT_SUCCESS);
1578 #endif
1579         do_grp_update = true;
1580 }
1581
1582 static void faillog_reset (uid_t uid)
1583 {
1584         struct faillog fl;
1585         int fd;
1586         off_t offset_uid = (off_t) (sizeof fl) * uid;
1587
1588         if (access (FAILLOG_FILE, F_OK) != 0) {
1589                 return;
1590         }
1591
1592         memzero (&fl, sizeof (fl));
1593
1594         fd = open (FAILLOG_FILE, O_RDWR);
1595         if (   (-1 == fd)
1596             || (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
1597             || (write (fd, &fl, sizeof (fl)) != (ssize_t) sizeof (fl))
1598             || (fsync (fd) != 0)
1599             || (close (fd) != 0)) {
1600                 fprintf (stderr,
1601                          _("%s: failed to reset the faillog entry of UID %lu: %s\n"),
1602                          Prog, (unsigned long) uid, strerror (errno));
1603                 SYSLOG ((LOG_WARN, "failed to reset the faillog entry of UID %lu", (unsigned long) uid));
1604                 /* continue */
1605         }
1606 }
1607
1608 static void lastlog_reset (uid_t uid)
1609 {
1610         struct lastlog ll;
1611         int fd;
1612         off_t offset_uid = (off_t) (sizeof ll) * uid;
1613
1614         if (access (LASTLOG_FILE, F_OK) != 0) {
1615                 return;
1616         }
1617
1618         memzero (&ll, sizeof (ll));
1619
1620         fd = open (LASTLOG_FILE, O_RDWR);
1621         if (   (-1 == fd)
1622             || (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
1623             || (write (fd, &ll, sizeof (ll)) != (ssize_t) sizeof (ll))
1624             || (fsync (fd) != 0)
1625             || (close (fd) != 0)) {
1626                 fprintf (stderr,
1627                          _("%s: failed to reset the lastlog entry of UID %lu: %s\n"),
1628                          Prog, (unsigned long) uid, strerror (errno));
1629                 SYSLOG ((LOG_WARN, "failed to reset the lastlog entry of UID %lu", (unsigned long) uid));
1630                 /* continue */
1631         }
1632 }
1633
1634 /*
1635  * usr_update - create the user entries
1636  *
1637  *      usr_update() creates the password file entries for this user
1638  *      and will update the group entries if required.
1639  */
1640 static void usr_update (void)
1641 {
1642         struct passwd pwent;
1643         struct spwd spent;
1644
1645         /*
1646          * Fill in the password structure with any new fields, making
1647          * copies of strings.
1648          */
1649         new_pwent (&pwent);
1650         new_spent (&spent);
1651
1652         /*
1653          * Create a syslog entry. We need to do this now in case anything
1654          * happens so we know what we were trying to accomplish.
1655          */
1656         SYSLOG ((LOG_INFO,
1657                  "new user: name=%s, UID=%u, GID=%u, home=%s, shell=%s",
1658                  user_name, (unsigned int) user_id,
1659                  (unsigned int) user_gid, user_home, user_shell));
1660
1661         /*
1662          * Initialize faillog and lastlog entries for this UID in case
1663          * it belongs to a previously deleted user. We do it only if
1664          * no user with this UID exists yet (entries for shared UIDs
1665          * are left unchanged).  --marekm
1666          */
1667         /* local, no need for xgetpwuid */
1668         if ((!lflg) && (getpwuid (user_id) == NULL)) {
1669                 faillog_reset (user_id);
1670                 lastlog_reset (user_id);
1671         }
1672
1673         /*
1674          * Put the new (struct passwd) in the table.
1675          */
1676         if (pw_update (&pwent) == 0) {
1677                 fprintf (stderr,
1678                          _("%s: failed to prepare the new %s entry '%s'\n"),
1679                          Prog, pw_dbname (), pwent.pw_name);
1680                 fail_exit (E_PW_UPDATE);
1681         }
1682
1683         /*
1684          * Put the new (struct spwd) in the table.
1685          */
1686         if (is_shadow_pwd && (spw_update (&spent) == 0)) {
1687                 fprintf (stderr,
1688                          _("%s: failed to prepare the new %s entry '%s'\n"),
1689                          Prog, spw_dbname (), spent.sp_namp);
1690 #ifdef WITH_AUDIT
1691                 audit_logger (AUDIT_ADD_USER, Prog,
1692                               "adding shadow password",
1693                               user_name, (unsigned int) user_id,
1694                               SHADOW_AUDIT_FAILURE);
1695 #endif
1696                 fail_exit (E_PW_UPDATE);
1697         }
1698 #ifdef WITH_AUDIT
1699         audit_logger (AUDIT_ADD_USER, Prog,
1700                       "adding user",
1701                       user_name, (unsigned int) user_id,
1702                       SHADOW_AUDIT_SUCCESS);
1703 #endif
1704
1705         /*
1706          * Do any group file updates for this user.
1707          */
1708         if (do_grp_update) {
1709                 grp_update ();
1710         }
1711 }
1712
1713 #ifdef WITH_SELINUX
1714 static void selinux_update_mapping (void) {
1715         if (is_selinux_enabled () <= 0) return;
1716
1717         if (*user_selinux) { /* must be done after passwd write() */
1718                 const char *argv[7];
1719                 argv[0] = "/usr/sbin/semanage";
1720                 argv[1] = "login";
1721                 argv[2] = "-a";
1722                 argv[3] = "-s";
1723                 argv[4] = user_selinux;
1724                 argv[5] = user_name;
1725                 argv[6] = NULL;
1726                 if (safe_system (argv[0], argv, NULL, 0)) {
1727                         fprintf (stderr,
1728                                  _("%s: warning: the user name %s to %s SELinux user mapping failed.\n"),
1729                                  Prog, user_name, user_selinux);
1730 #ifdef WITH_AUDIT
1731                         audit_logger (AUDIT_ADD_USER, Prog,
1732                                       "adding SELinux user mapping",
1733                                       user_name, (unsigned int) user_id, 0);
1734 #endif
1735                 }
1736         }
1737 }
1738 #endif
1739 /*
1740  * create_home - create the user's home directory
1741  *
1742  *      create_home() creates the user's home directory if it does not
1743  *      already exist. It will be created mode 755 owned by the user
1744  *      with the user's default group.
1745  */
1746 static void create_home (void)
1747 {
1748         if (access (user_home, F_OK) != 0) {
1749 #ifdef WITH_SELINUX
1750                 selinux_file_context (user_home);
1751 #endif
1752                 /* XXX - create missing parent directories.  --marekm */
1753                 if (mkdir (user_home, 0) != 0) {
1754                         fprintf (stderr,
1755                                  _("%s: cannot create directory %s\n"),
1756                                  Prog, user_home);
1757 #ifdef WITH_AUDIT
1758                         audit_logger (AUDIT_ADD_USER, Prog,
1759                                       "adding home directory",
1760                                       user_name, (unsigned int) user_id,
1761                                       SHADOW_AUDIT_FAILURE);
1762 #endif
1763                         fail_exit (E_HOMEDIR);
1764                 }
1765                 chown (user_home, user_id, user_gid);
1766                 chmod (user_home,
1767                        0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
1768                 home_added = true;
1769 #ifdef WITH_AUDIT
1770                 audit_logger (AUDIT_ADD_USER, Prog,
1771                               "adding home directory",
1772                               user_name, (unsigned int) user_id,
1773                               SHADOW_AUDIT_SUCCESS);
1774 #endif
1775 #ifdef WITH_SELINUX
1776                 /* Reset SELinux to create files with default contexts */
1777                 setfscreatecon (NULL);
1778 #endif
1779         }
1780 }
1781
1782 /*
1783  * create_mail - create the user's mail spool
1784  *
1785  *      create_mail() creates the user's mail spool if it does not already
1786  *      exist. It will be created mode 660 owned by the user and group
1787  *      'mail'
1788  */
1789 static void create_mail (void)
1790 {
1791         char *spool, *file;
1792         int fd;
1793         struct group *gr;
1794         gid_t gid;
1795         mode_t mode;
1796
1797         if (strcasecmp (create_mail_spool, "yes") == 0) {
1798                 spool = getdef_str ("MAIL_DIR");
1799                 if (NULL == spool) {
1800                         spool = "/var/mail";
1801                 }
1802                 file = alloca (strlen (spool) + strlen (user_name) + 2);
1803                 sprintf (file, "%s/%s", spool, user_name);
1804                 fd = open (file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0);
1805                 if (fd < 0) {
1806                         perror (_("Creating mailbox file"));
1807                         return;
1808                 }
1809
1810                 gr = getgrnam ("mail"); /* local, no need for xgetgrnam */
1811                 if (NULL == gr) {
1812                         fputs (_("Group 'mail' not found. Creating the user mailbox file with 0600 mode.\n"),
1813                                stderr);
1814                         gid = user_gid;
1815                         mode = 0600;
1816                 } else {
1817                         gid = gr->gr_gid;
1818                         mode = 0660;
1819                 }
1820
1821                 if (   (fchown (fd, user_id, gid) != 0)
1822                     || (fchmod (fd, mode) != 0)) {
1823                         perror (_("Setting mailbox file permissions"));
1824                 }
1825
1826                 fsync (fd);
1827                 close (fd);
1828         }
1829 }
1830
1831 /*
1832  * main - useradd command
1833  */
1834 int main (int argc, char **argv)
1835 {
1836 #ifdef ACCT_TOOLS_SETUID
1837 #ifdef USE_PAM
1838         pam_handle_t *pamh = NULL;
1839         int retval;
1840 #endif                          /* USE_PAM */
1841 #endif                          /* ACCT_TOOLS_SETUID */
1842
1843 #ifdef WITH_AUDIT
1844         audit_help_open ();
1845 #endif
1846
1847         /*
1848          * Get my name so that I can use it to report errors.
1849          */
1850         Prog = Basename (argv[0]);
1851
1852         (void) setlocale (LC_ALL, "");
1853         (void) bindtextdomain (PACKAGE, LOCALEDIR);
1854         (void) textdomain (PACKAGE);
1855
1856         OPENLOG ("useradd");
1857
1858         sys_ngroups = sysconf (_SC_NGROUPS_MAX);
1859         user_groups = (char **) xmalloc ((1 + sys_ngroups) * sizeof (char *));
1860         /*
1861          * Initialize the list to be empty
1862          */
1863         user_groups[0] = (char *) 0;
1864
1865
1866         is_shadow_pwd = spw_file_present ();
1867 #ifdef SHADOWGRP
1868         is_shadow_grp = sgr_file_present ();
1869 #endif
1870
1871         get_defaults ();
1872
1873         process_flags (argc, argv);
1874
1875 #ifdef ACCT_TOOLS_SETUID
1876 #ifdef USE_PAM
1877         {
1878                 struct passwd *pampw;
1879                 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
1880                 if (pampw == NULL) {
1881                         fprintf (stderr,
1882                                  _("%s: Cannot determine your user name.\n"),
1883                                  Prog);
1884                         fail_exit (1);
1885                 }
1886
1887                 retval = pam_start ("useradd", pampw->pw_name, &conv, &pamh);
1888         }
1889
1890         if (PAM_SUCCESS == retval) {
1891                 retval = pam_authenticate (pamh, 0);
1892         }
1893
1894         if (PAM_SUCCESS == retval) {
1895                 retval = pam_acct_mgmt (pamh, 0);
1896         }
1897
1898         if (NULL != pamh) {
1899                 (void) pam_end (pamh, retval);
1900         }
1901         if (PAM_SUCCESS != retval) {
1902                 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
1903                 fail_exit (1);
1904         }
1905 #endif                          /* USE_PAM */
1906 #endif                          /* ACCT_TOOLS_SETUID */
1907
1908         /*
1909          * See if we are messing with the defaults file, or creating
1910          * a new user.
1911          */
1912         if (Dflg) {
1913                 if (gflg || bflg || fflg || eflg || sflg) {
1914                         exit ((set_defaults () != 0) ? 1 : 0);
1915                 }
1916
1917                 show_defaults ();
1918                 exit (E_SUCCESS);
1919         }
1920
1921         /*
1922          * Start with a quick check to see if the user exists.
1923          */
1924         if (getpwnam (user_name) != NULL) { /* local, no need for xgetpwnam */
1925                 fprintf (stderr, _("%s: user '%s' already exists\n"), Prog, user_name);
1926 #ifdef WITH_AUDIT
1927                 audit_logger (AUDIT_ADD_USER, Prog,
1928                               "adding user",
1929                               user_name, AUDIT_NO_ID,
1930                               SHADOW_AUDIT_FAILURE);
1931 #endif
1932                 fail_exit (E_NAME_IN_USE);
1933         }
1934
1935         /*
1936          * Don't blindly overwrite a group when a user is added...
1937          * If you already have a group username, and want to add the user
1938          * to that group, use useradd -g username username.
1939          * --bero
1940          */
1941         if (Uflg) {
1942                 /* local, no need for xgetgrnam */
1943                 if (getgrnam (user_name) != NULL) {
1944                         fprintf (stderr,
1945                                  _("%s: group %s exists - if you want to add this user to that group, use -g.\n"),
1946                                  Prog, user_name);
1947 #ifdef WITH_AUDIT
1948                         audit_logger (AUDIT_ADD_USER, Prog,
1949                                       "adding group",
1950                                       user_name, AUDIT_NO_ID,
1951                                       SHADOW_AUDIT_FAILURE);
1952 #endif
1953                         fail_exit (E_NAME_IN_USE);
1954                 }
1955         }
1956
1957         /*
1958          * Do the hard stuff:
1959          * - open the files,
1960          * - create the user entries,
1961          * - create the home directory,
1962          * - create user mail spool,
1963          * - flush nscd caches for passwd and group services,
1964          * - then close and update the files.
1965          */
1966         open_files ();
1967
1968         if (!oflg) {
1969                 /* first, seek for a valid uid to use for this user.
1970                  * We do this because later we can use the uid we found as
1971                  * gid too ... --gafton */
1972                 if (!uflg) {
1973                         if (find_new_uid (rflg, &user_id, NULL) < 0) {
1974                                 fprintf (stderr, _("%s: can't create user\n"), Prog);
1975                                 fail_exit (E_UID_IN_USE);
1976                         }
1977                 } else {
1978                         if (getpwuid (user_id) != NULL) {
1979                                 fprintf (stderr,
1980                                          _("%s: UID %lu is not unique\n"),
1981                                          Prog, (unsigned long) user_id);
1982 #ifdef WITH_AUDIT
1983                                 audit_logger (AUDIT_ADD_USER, Prog,
1984                                               "adding user",
1985                                               user_name, (unsigned int) user_id,
1986                                               SHADOW_AUDIT_FAILURE);
1987 #endif
1988                                 fail_exit (E_UID_IN_USE);
1989                         }
1990                 }
1991         }
1992
1993         /* do we have to add a group for that user? This is why we need to
1994          * open the group files in the open_files() function  --gafton */
1995         if (Uflg) {
1996                 if (find_new_gid (rflg, &user_gid, &user_id) < 0) {
1997                         fprintf (stderr,
1998                                  _("%s: can't create group\n"),
1999                                  Prog);
2000                         fail_exit (4);
2001                 }
2002                 grp_add ();
2003         }
2004
2005         usr_update ();
2006
2007         if (mflg) {
2008                 create_home ();
2009                 if (home_added) {
2010                         copy_tree (def_template, user_home, user_id, user_gid);
2011                 } else {
2012                         fprintf (stderr,
2013                                  _("%s: warning: the home directory already exists.\n"
2014                                    "Not copying any file from skel directory into it.\n"),
2015                                  Prog);
2016                 }
2017
2018         }
2019
2020         /* Do not create mail directory for system accounts */
2021         if( !rflg ) {
2022                 create_mail ();
2023         }
2024
2025         close_files ();
2026
2027 #ifdef WITH_SELINUX
2028         selinux_update_mapping ();
2029 #endif
2030
2031         nscd_flush_cache ("passwd");
2032         nscd_flush_cache ("group");
2033
2034         return E_SUCCESS;
2035 }
2036