-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathuseradd.c
2707 lines (2473 loc) · 71.2 KB
/
useradd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
* SPDX-FileCopyrightText: 2000 - 2006, Tomasz Kłoczko
* SPDX-FileCopyrightText: 2007 - 2012, Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
#ident "$Id$"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#ifdef ENABLE_LASTLOG
#include <lastlog.h>
#endif /* ENABLE_LASTLOG */
#include <libgen.h>
#include <pwd.h>
#include <signal.h>
#ifdef ACCT_TOOLS_SETUID
#ifdef USE_PAM
#include "pam_defs.h"
#endif /* USE_PAM */
#endif /* ACCT_TOOLS_SETUID */
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "alloc/x/xmalloc.h"
#include "atoi/a2i/a2s.h"
#include "atoi/getnum.h"
#include "chkname.h"
#include "defines.h"
#include "faillog.h"
#include "getdef.h"
#include "groupio.h"
#include "nscd.h"
#include "prototypes.h"
#include "pwauth.h"
#include "pwio.h"
#include "run_part.h"
#ifdef SHADOWGRP
#include "sgroupio.h"
#endif
#include "shadowio.h"
#ifdef WITH_SELINUX
#include <selinux/selinux.h>
#endif /* WITH_SELINUX */
#ifdef ENABLE_SUBIDS
#include "subordinateio.h"
#endif /* ENABLE_SUBIDS */
#ifdef WITH_TCB
#include "tcbfuncs.h"
#endif
#include "shadowlog.h"
#include "sssd.h"
#include "string/memset/memzero.h"
#include "string/sprintf/snprintf.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/strcaseeq.h"
#include "string/strcmp/streq.h"
#include "string/strdup/xstrdup.h"
#include "string/strtok/stpsep.h"
#ifndef SKEL_DIR
#define SKEL_DIR "/etc/skel"
#endif
#ifndef USRSKELDIR
#define USRSKELDIR "/usr/etc/skel"
#endif
#ifndef USER_DEFAULTS_FILE
#define USER_DEFAULTS_FILE "/etc/default/useradd"
#define NEW_USER_FILE "/etc/default/nuaddXXXXXX"
#endif
/*
* Needed for MkLinux DR1/2/2.1 - J.
*/
#ifndef LASTLOG_FILE
#define LASTLOG_FILE "/var/log/lastlog"
#endif
/*
* Global variables
*/
static const char Prog[] = "useradd";
/*
* These defaults are used if there is no defaults file.
*/
static gid_t def_group = 1000;
static const char *def_groups = "";
static const char *def_gname = "other";
static const char *def_home = "/home";
static const char *def_shell = "/bin/bash";
static const char *def_template = SKEL_DIR;
static const char *def_usrtemplate = USRSKELDIR;
static const char *def_create_mail_spool = "yes";
static const char *def_log_init = "yes";
static long def_inactive = -1;
static const char *def_expire = "";
#define VALID(s) (strcspn (s, ":\n") == strlen (s))
static const char *user_name = "";
static const char *user_pass = "!";
static uid_t user_id;
static gid_t user_gid;
static const char *user_comment = "";
static const char *user_home = "";
static const char *user_shell = "";
static const char *create_mail_spool = "";
static const char *prefix = "";
static const char *prefix_user_home = NULL;
#ifdef WITH_SELINUX
static /*@notnull@*/const char *user_selinux = "";
static const char *user_selinux_range = NULL;
#endif /* WITH_SELINUX */
static long user_expire = -1;
static bool is_shadow_pwd;
#ifdef SHADOWGRP
static bool is_shadow_grp;
static bool sgr_locked = false;
#endif
#ifdef ENABLE_SUBIDS
static bool is_sub_uid = false;
static bool is_sub_gid = false;
static bool sub_uid_locked = false;
static bool sub_gid_locked = false;
static uid_t sub_uid_start; /* New subordinate uid range */
static gid_t sub_gid_start; /* New subordinate gid range */
#endif /* ENABLE_SUBIDS */
static bool pw_locked = false;
static bool gr_locked = false;
static bool spw_locked = false;
static char **user_groups; /* NULL-terminated list */
static long sys_ngroups;
static bool do_grp_update = false; /* group files need to be updated */
extern int allow_bad_names;
static bool
bflg = false, /* new default root of home directory */
cflg = false, /* comment (GECOS) field for new account */
dflg = false, /* home directory for new account */
Dflg = false, /* set/show new user default values */
eflg = false, /* days since 1970-01-01 when account is locked */
fflg = false, /* days until account with expired password is locked */
#ifdef ENABLE_SUBIDS
Fflg = false, /* update /etc/subuid and /etc/subgid even if -r option is given */
#endif
gflg = false, /* primary group ID for new account */
Gflg = false, /* secondary group set for new account */
kflg = false, /* specify a directory to fill new user directory */
lflg = false, /* do not add user to lastlog/faillog databases */
mflg = false, /* create user's home directory if it doesn't exist */
Mflg = false, /* do not create user's home directory even if CREATE_HOME is set */
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) */
oflg = false, /* permit non-unique user ID to be specified with -u */
rflg = false, /* create a system account */
sflg = false, /* shell program for new account */
subvolflg = false, /* create subvolume home on BTRFS */
uflg = false, /* specify user ID for new account */
Uflg = false; /* create a group having the same name as the user */
#ifdef WITH_SELINUX
#define Zflg (!streq(user_selinux, ""))
#endif /* WITH_SELINUX */
static bool home_added = false;
/*
* exit status values
*/
/*@-exitarg@*/
#define E_SUCCESS 0 /* success */
#define E_PW_UPDATE 1 /* can't update password file */
#define E_USAGE 2 /* invalid command syntax */
#define E_BAD_ARG 3 /* invalid argument to option */
#define E_UID_IN_USE 4 /* UID already in use (and no -o) */
#define E_NOTFOUND 6 /* specified group doesn't exist */
#define E_NAME_IN_USE 9 /* username or group name already in use */
#define E_GRP_UPDATE 10 /* can't update group file */
#define E_HOMEDIR 12 /* can't create home directory */
#define E_MAILBOXFILE 13 /* can't create mailbox file */
#define E_SE_UPDATE 14 /* can't update SELinux user mapping */
#ifdef ENABLE_SUBIDS
#define E_SUB_UID_UPDATE 16 /* can't update the subordinate uid file */
#define E_SUB_GID_UPDATE 18 /* can't update the subordinate gid file */
#endif /* ENABLE_SUBIDS */
#define E_BAD_NAME 19 /* Bad login name */
#define DGROUP "GROUP"
#define DGROUPS "GROUPS"
#define DHOME "HOME"
#define DSHELL "SHELL"
#define DINACT "INACTIVE"
#define DEXPIRE "EXPIRE"
#define DSKEL "SKEL"
#define DUSRSKEL "USRSKEL"
#define DCREATE_MAIL_SPOOL "CREATE_MAIL_SPOOL"
#define DLOG_INIT "LOG_INIT"
/* local function prototypes */
NORETURN static void fail_exit (int);
static void get_defaults (void);
static void show_defaults (void);
static int set_defaults (void);
static int get_groups (char *);
static struct group * get_local_group (char * grp_name);
NORETURN static void usage (int status);
static void new_pwent (struct passwd *);
static void new_spent (struct spwd *);
static void grp_update (void);
static void process_flags (int argc, char **argv);
static void close_files (void);
static void close_group_files (void);
static void unlock_group_files (void);
static void open_files (void);
static void open_group_files (void);
static void open_shadow (void);
static void faillog_reset (uid_t);
#ifdef ENABLE_LASTLOG
static void lastlog_reset (uid_t);
#endif /* ENABLE_LASTLOG */
static void tallylog_reset (const char *);
static void usr_update (unsigned long subuid_count, unsigned long subgid_count);
static void create_home (void);
static void create_mail (void);
static void check_uid_range(int rflg, uid_t user_id);
static FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);
/*
* fail_exit - undo as much as possible
*/
static void fail_exit (int code)
{
#ifdef WITH_AUDIT
int type;
#endif
if (home_added && rmdir(prefix_user_home) != 0) {
fprintf(stderr,
_("%s: %s was created, but could not be removed\n"),
Prog, prefix_user_home);
SYSLOG((LOG_ERR, "failed to remove %s", prefix_user_home));
}
if (spw_locked && spw_unlock() == 0) {
fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname());
SYSLOG((LOG_ERR, "failed to unlock %s", spw_dbname()));
/* continue */
}
if (pw_locked && pw_unlock() == 0) {
fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname());
SYSLOG((LOG_ERR, "failed to unlock %s", pw_dbname()));
/* continue */
}
if (gr_locked && gr_unlock() == 0) {
fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname());
SYSLOG((LOG_ERR, "failed to unlock %s", gr_dbname()));
/* continue */
}
#ifdef SHADOWGRP
if (sgr_locked && sgr_unlock() == 0) {
fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname());
SYSLOG((LOG_ERR, "failed to unlock %s", sgr_dbname()));
/* continue */
}
#endif
#ifdef ENABLE_SUBIDS
if (sub_uid_locked && sub_uid_unlock() == 0) {
fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, sub_uid_dbname());
SYSLOG((LOG_ERR, "failed to unlock %s", sub_uid_dbname()));
/* continue */
}
if (sub_gid_locked && sub_gid_unlock() == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sub_gid_dbname());
SYSLOG ((LOG_ERR, "failed to unlock %s", sub_gid_dbname()));
/* continue */
}
#endif /* ENABLE_SUBIDS */
#ifdef WITH_AUDIT
if (code == E_PW_UPDATE || code >= E_GRP_UPDATE)
type = AUDIT_USER_MGMT;
else
type = AUDIT_ADD_USER;
audit_logger (type, Prog,
"add-user",
user_name, AUDIT_NO_ID, SHADOW_AUDIT_FAILURE);
#endif
SYSLOG((LOG_INFO, "failed adding user '%s', exit code: %d", user_name, code));
exit(code);
}
/*
* get_defaults - read the defaults file
*
* get_defaults() reads the defaults file for this command. It sets the
* various values from the file, or uses built-in default values if the
* file does not exist.
*/
static void
get_defaults(void)
{
FILE *fp;
char *default_file = USER_DEFAULTS_FILE;
char buf[1024];
char *cp;
const char *ccp;
if (prefix[0]) {
if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
return;
}
/*
* Open the defaults file for reading.
*/
fp = fopen (default_file, "r");
if (NULL == fp) {
goto getdef_err;
}
/*
* Read the file a line at a time. Only the lines that have relevant
* values are used, everything else can be ignored.
*/
while (fgets (buf, sizeof buf, fp) == buf) {
stpsep(buf, "\n");
cp = stpsep(buf, "=");
if (NULL == cp)
continue;
/*
* Primary GROUP identifier
*/
if (streq(buf, DGROUP)) {
const struct group *grp = prefix_getgr_nam_gid (cp);
if (NULL == grp) {
fprintf (stderr,
_("%s: group '%s' does not exist\n"),
Prog, cp);
fprintf (stderr,
_("%s: the %s= configuration in %s will be ignored\n"),
Prog, DGROUP, default_file);
} else {
def_group = grp->gr_gid;
def_gname = xstrdup (grp->gr_name);
}
}
ccp = cp;
if (streq(buf, DGROUPS)) {
if (get_groups (cp) != 0) {
fprintf (stderr,
_("%s: the '%s=' configuration in %s has an invalid group, ignoring the bad group\n"),
Prog, DGROUPS, default_file);
}
if (user_groups[0] != NULL) {
do_grp_update = true;
def_groups = xstrdup (cp);
}
}
/*
* Default HOME filesystem
*/
else if (streq(buf, DHOME)) {
def_home = xstrdup(ccp);
}
/*
* Default Login Shell command
*/
else if (streq(buf, DSHELL)) {
def_shell = xstrdup(ccp);
}
/*
* Default Password Inactive value
*/
else if (streq(buf, DINACT)) {
if (a2sl(&def_inactive, ccp, NULL, 0, -1, LONG_MAX) == -1) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Prog, ccp);
fprintf (stderr,
_("%s: the %s= configuration in %s will be ignored\n"),
Prog, DINACT, default_file);
def_inactive = -1;
}
}
/*
* Default account expiration date
*/
else if (streq(buf, DEXPIRE)) {
def_expire = xstrdup(ccp);
}
/*
* Default Skeleton information
*/
else if (streq(buf, DSKEL)) {
if (streq(ccp, ""))
ccp = SKEL_DIR;
if (prefix[0]) {
char *dt;
xasprintf(&dt, "%s/%s", prefix, ccp);
def_template = dt;
} else {
def_template = xstrdup(ccp);
}
}
/*
* Default Usr Skeleton information
*/
else if (streq(buf, DUSRSKEL)) {
if (streq(ccp, ""))
ccp = USRSKELDIR;
if (prefix[0]) {
char *dut;
xasprintf(&dut, "%s/%s", prefix, ccp);
def_usrtemplate = dut;
} else {
def_usrtemplate = xstrdup(ccp);
}
}
/*
* Create by default user mail spool or not ?
*/
else if (streq(buf, DCREATE_MAIL_SPOOL)) {
if (streq(ccp, ""))
ccp = "no";
def_create_mail_spool = xstrdup(ccp);
}
/*
* By default do we add the user to the lastlog and faillog databases ?
*/
else if (streq(buf, DLOG_INIT)) {
if (streq(ccp, ""))
ccp = def_log_init;
def_log_init = xstrdup(ccp);
}
}
(void) fclose (fp);
getdef_err:
if (prefix[0]) {
free(default_file);
}
}
/*
* show_defaults - show the contents of the defaults file
*
* show_defaults() displays the values that are used from the default
* file and the built-in values.
*/
static void show_defaults (void)
{
printf ("GROUP=%u\n", (unsigned int) def_group);
printf ("GROUPS=%s\n", def_groups);
printf ("HOME=%s\n", def_home);
printf ("INACTIVE=%ld\n", def_inactive);
printf ("EXPIRE=%s\n", def_expire);
printf ("SHELL=%s\n", def_shell);
printf ("SKEL=%s\n", def_template);
printf ("USRSKEL=%s\n", def_usrtemplate);
printf ("CREATE_MAIL_SPOOL=%s\n", def_create_mail_spool);
printf ("LOG_INIT=%s\n", def_log_init);
}
/*
* set_defaults - write new defaults file
*
* set_defaults() re-writes the defaults file using the values that
* are currently set. Duplicated lines are pruned, missing lines are
* added, and unrecognized lines are copied as is.
*/
static int
set_defaults(void)
{
int ret = -1;
bool out_group = false;
bool out_groups = false;
bool out_home = false;
bool out_inactive = false;
bool out_expire = false;
bool out_shell = false;
bool out_skel = false;
bool out_usrskel = false;
bool out_create_mail_spool = false;
bool out_log_init = false;
char buf[1024];
char *new_file = NULL;
char *new_file_dup = NULL;
char *default_file = USER_DEFAULTS_FILE;
char *cp;
FILE *ifp;
FILE *ofp;
if (asprintf(&new_file, "%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE) == -1)
{
fprintf(stderr, _("%s: cannot create new defaults file: %s\n"),
Prog, strerror(errno));
return -1;
}
if (prefix[0]) {
if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
{
fprintf(stderr,
_("%s: cannot create new defaults file: %s\n"),
Prog, strerror(errno));
goto err_free_new;
}
}
new_file_dup = strdup(new_file);
if (new_file_dup == NULL) {
fprintf (stderr,
_("%s: cannot create directory for defaults file\n"),
Prog);
goto err_free_def;
}
ret = mkdir(dirname(new_file_dup), 0755);
free(new_file_dup);
if (-1 == ret && EEXIST != errno) {
fprintf (stderr,
_("%s: cannot create directory for defaults file\n"),
Prog);
goto err_free_def;
}
/*
* Create a temporary file to copy the new output to.
*/
ofp = fmkomstemp(new_file, 0, 0644);
if (NULL == ofp) {
fprintf (stderr,
_("%s: cannot open new defaults file\n"),
Prog);
goto err_free_def;
}
/*
* Open the existing defaults file and copy the lines to the
* temporary file, using any new values. Each line is checked
* to insure that it is not output more than once.
*/
ifp = fopen (default_file, "r");
if (NULL == ifp) {
fprintf (ofp, "# useradd defaults file\n");
goto skip;
}
while (fgets (buf, sizeof buf, ifp) == buf) {
char *val;
if (stpsep(buf, "\n") == NULL) {
/* A line which does not end with \n is only valid
* at the end of the file.
*/
if (feof (ifp) == 0) {
fprintf (stderr,
_("%s: line too long in %s: %s..."),
Prog, default_file, buf);
fclose(ifp);
fclose(ofp);
goto err_free_def;
}
}
val = stpsep(buf, "=");
if (val == NULL) {
fprintf(ofp, "%s\n", buf);
} else if (!out_group && streq(buf, DGROUP)) {
fprintf(ofp, DGROUP "=%u\n", (unsigned int) def_group);
out_group = true;
} else if (!out_groups && streq(buf, DGROUPS)) {
fprintf(ofp, DGROUPS "=%s\n", def_groups);
out_groups = true;
} else if (!out_home && streq(buf, DHOME)) {
fprintf(ofp, DHOME "=%s\n", def_home);
out_home = true;
} else if (!out_inactive && streq(buf, DINACT)) {
fprintf(ofp, DINACT "=%ld\n", def_inactive);
out_inactive = true;
} else if (!out_expire && streq(buf, DEXPIRE)) {
fprintf(ofp, DEXPIRE "=%s\n", def_expire);
out_expire = true;
} else if (!out_shell && streq(buf, DSHELL)) {
fprintf(ofp, DSHELL "=%s\n", def_shell);
out_shell = true;
} else if (!out_skel && streq(buf, DSKEL)) {
fprintf(ofp, DSKEL "=%s\n", def_template);
out_skel = true;
} else if (!out_usrskel && streq(buf, DUSRSKEL)) {
fprintf(ofp, DUSRSKEL "=%s\n", def_usrtemplate);
out_usrskel = true;
} else if (!out_create_mail_spool
&& streq(buf, DCREATE_MAIL_SPOOL))
{
fprintf(ofp,
DCREATE_MAIL_SPOOL "=%s\n",
def_create_mail_spool);
out_create_mail_spool = true;
} else if (!out_log_init && streq(buf, DLOG_INIT)) {
fprintf(ofp, DLOG_INIT "=%s\n", def_log_init);
out_log_init = true;
} else {
fprintf(ofp, "%s=%s\n", buf, val);
}
}
(void) fclose (ifp);
skip:
/*
* Check each line to insure that every line was output. This
* causes new values to be added to a file which did not previously
* have an entry for that value.
*/
if (!out_group)
fprintf (ofp, DGROUP "=%u\n", (unsigned int) def_group);
if (!out_groups)
fprintf (ofp, DGROUPS "=%s\n", def_groups);
if (!out_home)
fprintf (ofp, DHOME "=%s\n", def_home);
if (!out_inactive)
fprintf (ofp, DINACT "=%ld\n", def_inactive);
if (!out_expire)
fprintf (ofp, DEXPIRE "=%s\n", def_expire);
if (!out_shell)
fprintf (ofp, DSHELL "=%s\n", def_shell);
if (!out_skel)
fprintf (ofp, DSKEL "=%s\n", def_template);
if (!out_usrskel)
fprintf (ofp, DUSRSKEL "=%s\n", def_usrtemplate);
if (!out_create_mail_spool)
fprintf (ofp, DCREATE_MAIL_SPOOL "=%s\n", def_create_mail_spool);
if (!out_log_init)
fprintf (ofp, DLOG_INIT "=%s\n", def_log_init);
/*
* Flush and close the file. Check for errors to make certain
* the new file is intact.
*/
(void) fflush (ofp);
if ( (ferror (ofp) != 0)
|| (fsync (fileno (ofp)) != 0)
|| (fclose (ofp) != 0))
{
unlink (new_file);
goto err_free_def;
}
/*
* Rename the current default file to its backup name.
*/
assert(SNPRINTF(buf, "%s-", default_file) != -1);
unlink (buf);
if ((link (default_file, buf) != 0) && (ENOENT != errno)) {
int err = errno;
fprintf (stderr,
_("%s: Cannot create backup file (%s): %s\n"),
Prog, buf, strerror (err));
unlink (new_file);
goto err_free_def;
}
/*
* Rename the new default file to its correct name.
*/
if (rename (new_file, default_file) != 0) {
int err = errno;
fprintf (stderr,
_("%s: rename: %s: %s\n"),
Prog, new_file, strerror (err));
goto err_free_def;
}
#ifdef WITH_AUDIT
audit_logger (AUDIT_USYS_CONFIG, Prog,
"changing-useradd-defaults",
NULL, AUDIT_NO_ID,
SHADOW_AUDIT_SUCCESS);
#endif
SYSLOG ((LOG_INFO,
"useradd defaults: GROUP=%u, HOME=%s, SHELL=%s, INACTIVE=%ld, "
"EXPIRE=%s, SKEL=%s, CREATE_MAIL_SPOOL=%s, LOG_INIT=%s",
(unsigned int) def_group, def_home, def_shell,
def_inactive, def_expire, def_template,
def_create_mail_spool, def_log_init));
ret = 0;
err_free_def:
if (prefix[0])
free(default_file);
err_free_new:
free(new_file);
return ret;
}
/*
* get_groups - convert a list of group names to an array of group IDs
*
* get_groups() takes a comma-separated list of group names and
* converts it to a NULL-terminated array. Any unknown group
* names are reported as errors.
*/
static int get_groups (char *list)
{
struct group *grp;
bool errors = false;
int ngroups = 0;
/*
* Free previous group list before creating a new one.
*/
int i = 0;
while (NULL != user_groups[i]) {
free(user_groups[i]);
user_groups[i++] = NULL;
}
if (streq(list, "")) {
return 0;
}
/*
* Open the group files
*/
open_group_files ();
/*
* So long as there is some data to be converted, strip off
* each name and look it up. A mix of numerical and string
* values for group identifiers is permitted.
*/
while (NULL != list) {
char *g;
/*
* Strip off a single name from the list
*/
g = strsep(&list, ",");
/*
* Names starting with digits are treated as numerical
* GID values, otherwise the string is looked up as is.
*/
grp = get_local_group(g);
/*
* There must be a match, either by GID value or by
* string name.
* FIXME: It should exist according to gr_locate,
* otherwise, we can't change its members
*/
if (NULL == grp) {
fprintf (stderr,
_("%s: group '%s' does not exist\n"),
Prog, g);
errors = true;
}
/*
* If the group doesn't exist, don't dump core...
* Instead, try the next one. --marekm
*/
if (NULL == grp) {
continue;
}
if (ngroups == sys_ngroups) {
fprintf (stderr,
_("%s: too many groups specified (max %d).\n"),
Prog, ngroups);
gr_free(grp);
break;
}
/*
* Add the group name to the user's list of groups.
*/
user_groups[ngroups++] = xstrdup (grp->gr_name);
gr_free (grp);
}
close_group_files ();
unlock_group_files ();
user_groups[ngroups] = NULL;
/*
* Any errors in finding group names are fatal
*/
if (errors) {
return -1;
}
return 0;
}
/*
* get_local_group - checks if a given group name exists locally
*
* get_local_group() checks if a given group name exists locally.
* If the name exists the group information is returned, otherwise NULL is
* returned.
*/
static struct group * get_local_group(char * grp_name)
{
gid_t gid;
struct group *result_grp = NULL;
const struct group *grp;
if (get_gid(grp_name, &gid) == 0)
grp = gr_locate_gid(gid);
else
grp = gr_locate(grp_name);
if (grp != NULL) {
result_grp = __gr_dup (grp);
if (NULL == result_grp) {
fprintf (stderr,
_("%s: Out of memory. Cannot find group '%s'.\n"),
Prog, grp_name);
fail_exit (E_GRP_UPDATE);
}
}
return result_grp;
}
/*
* usage - display usage message and exit
*/
static void usage (int status)
{
FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
(void) fprintf (usageout,
_("Usage: %s [options] LOGIN\n"
" %s -D\n"
" %s -D [options]\n"
"\n"
"Options:\n"),
Prog, Prog, Prog);
(void) fputs (_(" --badname do not check for bad names\n"), usageout);
(void) fputs (_(" -b, --base-dir BASE_DIR base directory for the home directory of the\n"
" new account\n"), usageout);
#ifdef WITH_BTRFS
(void) fputs (_(" --btrfs-subvolume-home use BTRFS subvolume for home directory\n"), usageout);
#endif
(void) fputs (_(" -c, --comment COMMENT GECOS field of the new account\n"), usageout);
(void) fputs (_(" -d, --home-dir HOME_DIR home directory of the new account\n"), usageout);
(void) fputs (_(" -D, --defaults print or change default useradd configuration\n"), usageout);
(void) fputs (_(" -e, --expiredate EXPIRE_DATE expiration date of the new account\n"), usageout);
(void) fputs (_(" -f, --inactive INACTIVE password inactivity period of the new account\n"), usageout);
#ifdef ENABLE_SUBIDS
(void) fputs (_(" -F, --add-subids-for-system add entries to sub[ud]id even when adding a system user\n"), usageout);
#endif
(void) fputs (_(" -g, --gid GROUP name or ID of the primary group of the new\n"
" account\n"), usageout);
(void) fputs (_(" -G, --groups GROUPS list of supplementary groups of the new\n"
" account\n"), usageout);
(void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
(void) fputs (_(" -k, --skel SKEL_DIR use this alternative skeleton directory\n"), usageout);
(void) fputs (_(" -K, --key KEY=VALUE override /etc/login.defs defaults\n"), usageout);
#ifdef ENABLE_LASTLOG
(void) fputs (_(" -l, --no-log-init do not add the user to the lastlog and\n"
" faillog databases\n"), usageout);
#endif /* ENABLE_LASTLOG */
(void) fputs (_(" -m, --create-home create the user's home directory\n"), usageout);
(void) fputs (_(" -M, --no-create-home do not create the user's home directory\n"), usageout);
(void) fputs (_(" -N, --no-user-group do not create a group with the same name as\n"
" the user\n"), usageout);
(void) fputs (_(" -o, --non-unique allow to create users with duplicate\n"
" (non-unique) UID\n"), usageout);
(void) fputs (_(" -p, --password PASSWORD encrypted password of the new account\n"), usageout);
(void) fputs (_(" -r, --system create a system account\n"), usageout);
(void) fputs (_(" -R, --root CHROOT_DIR directory to chroot into\n"), usageout);
(void) fputs (_(" -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files\n"), usageout);
(void) fputs (_(" -s, --shell SHELL login shell of the new account\n"), usageout);
(void) fputs (_(" -u, --uid UID user ID of the new account\n"), usageout);
(void) fputs (_(" -U, --user-group create a group with the same name as the user\n"), usageout);
#ifdef WITH_SELINUX
(void) fputs (_(" -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping\n"), usageout);
(void) fputs (_(" --selinux-range SERANGE use a specific MLS range for the SELinux user mapping\n"), usageout);
#endif /* WITH_SELINUX */
(void) fputs ("\n", usageout);
exit (status);
}
/*
* new_pwent - initialize the values in a password file entry
*
* new_pwent() takes all of the values that have been entered and
* fills in a (struct passwd) with them.
*/
static void new_pwent (struct passwd *pwent)
{
memzero (pwent, sizeof *pwent);
pwent->pw_name = (char *) user_name;
if (is_shadow_pwd) {
pwent->pw_passwd = (char *) SHADOW_PASSWD_STRING;
} else {
pwent->pw_passwd = (char *) user_pass;
}
pwent->pw_uid = user_id;
pwent->pw_gid = user_gid;
pwent->pw_gecos = (char *) user_comment;
pwent->pw_dir = (char *) user_home;
pwent->pw_shell = (char *) user_shell;
}
/*
* new_spent - initialize the values in a shadow password file entry
*
* new_spent() takes all of the values that have been entered and
* fills in a (struct spwd) with them.
*/
static void new_spent (struct spwd *spent)
{
memzero (spent, sizeof *spent);
spent->sp_namp = (char *) user_name;
spent->sp_pwdp = (char *) user_pass;
spent->sp_lstchg = gettime () / DAY;
if (0 == spent->sp_lstchg) {
/* Better disable aging than requiring a password change */
spent->sp_lstchg = -1;
}
if (!rflg) {
spent->sp_min = getdef_num ("PASS_MIN_DAYS", -1);
spent->sp_max = getdef_num ("PASS_MAX_DAYS", -1);
spent->sp_warn = getdef_num ("PASS_WARN_AGE", -1);
spent->sp_inact = def_inactive;
spent->sp_expire = user_expire;
} else {
spent->sp_min = -1;
spent->sp_max = -1;
spent->sp_warn = -1;
spent->sp_inact = -1;
spent->sp_expire = -1;
}
spent->sp_flag = SHADOW_SP_FLAG_UNSET;
}
/*
* grp_update - add user to secondary group set
*
* grp_update() takes the secondary group set given in user_groups
* and adds the user to each group given by that set.
*
* The group files are opened and locked in open_files().
*
* close_files() should be called afterwards to commit the changes
* and unlocking the group files.
*/
static void grp_update (void)
{
const struct group *grp;
struct group *ngrp;
#ifdef SHADOWGRP
const struct sgrp *sgrp;