]> git.cameronkatri.com Git - pw-darwin.git/blob - adduser/adduser.perl
Keep similar things together: Check for too long usernames
[pw-darwin.git] / adduser / adduser.perl
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28
29
30 # read variables
31 sub variables {
32 $verbose = 1; # verbose = [0-2]
33 $usernameregexp = "^[a-z0-9_][a-z0-9_-]*\$"; # configurable
34 $defaultusernameregexp = $usernameregexp; # remains constant
35 $defaultusepassword = "yes"; # use password authentication for new users
36 $defaultenableaccount = "yes"; # enable the account by default
37 $defaultemptypassword = "no"; # don't create an empty password
38 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
39 $dotdir_bak = $dotdir;
40 $send_message = "/etc/adduser.message"; # send message to new user
41 $send_message_bak = '/etc/adduser.message';
42 $config = "/etc/adduser.conf"; # config file for adduser
43 $config_read = 1; # read config file
44 $logfile = "/var/log/adduser"; # logfile
45 $home = "/home"; # default HOME
46 $etc_shells = "/etc/shells";
47 $etc_passwd = "/etc/master.passwd";
48 $group = "/etc/group";
49 @pwd_mkdb = qw(pwd_mkdb -p); # program for building passwd database
50
51
52 # List of directories where shells located
53 @path = ('/bin', '/usr/bin', '/usr/local/bin');
54 # common shells, first element has higher priority
55 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
56
57 $defaultshell = 'sh'; # defaultshell if not empty
58 $group_uniq = 'USER';
59 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
60 $defaultclass = '';
61
62 $uid_start = 1000; # new users get this uid
63 $uid_end = 32000; # max. uid
64
65 # global variables
66 # passwd
67 $username = ''; # $username{username} = uid
68 $uid = ''; # $uid{uid} = username
69 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
70
71 $password = ''; # password for new users
72 $usepassword = ''; # use password-based auth
73 $useemptypassword = ''; # use an empty password
74 $enableaccount = ''; # enable or disable account password at creation
75
76 # group
77 $groupname =''; # $groupname{groupname} = gid
78 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
79 $gid = ''; # $gid{gid} = groupname; gid form group db
80 @group_comments; # Comments in the group file
81
82 # shell
83 $shell = ''; # $shell{`basename sh`} = sh
84
85 umask 022; # don't give login group write access
86
87 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
88 @passwd_backup = '';
89 @group_backup = '';
90 @message_buffer = '';
91 @user_variable_list = ''; # user variables in /etc/adduser.conf
92 $do_not_delete = '## DO NOT DELETE THIS LINE!';
93 }
94
95 # read shell database, see also: shells(5)
96 sub shells_read {
97 local($sh);
98 local($err) = 0;
99
100 print "Check $etc_shells\n" if $verbose;
101 open(S, $etc_shells) || die "$etc_shells:$!\n";
102
103 while(<S>) {
104 if (/^\s*\//) {
105 s/^\s*//; s/\s+.*//; # chop
106 $sh = $_;
107 if (-x $sh) {
108 $shell{&basename($sh)} = $sh;
109 } else {
110 warn "Shell: $sh not executable!\n";
111 $err++;
112 }
113 }
114 }
115
116 # Allow /nonexistent and /bin/date as a valid shell for system utils
117 push(@list, "/nonexistent");
118 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
119 $shell{"no"} = "/nonexistent";
120
121 push(@list, "/bin/date");
122 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
123 $shell{"date"} = "/bin/date";
124
125 return $err;
126 }
127
128 # add new shells if possible
129 sub shells_add {
130 local($sh,$dir,@list);
131
132 return 1 unless $verbose;
133
134 foreach $sh (@shellpref) {
135 # all known shells
136 if (!$shell{$sh}) {
137 # shell $sh is not defined as login shell
138 foreach $dir (@path) {
139 if (-x "$dir/$sh") {
140 # found shell
141 if (&confirm_yn("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
142 push(@list, "$dir/$sh");
143 push(@shellpref, "$sh");
144 $shell{&basename("$dir/$sh")} = "$dir/$sh";
145 $changes++;
146 }
147 }
148 }
149 }
150 }
151 &append_file($etc_shells, @list) if $#list >= 0;
152 }
153
154 # choose your favourite shell and return the shell
155 sub shell_default {
156 local($e,$i,$new_shell);
157 local($sh);
158
159 $sh = &shell_default_valid($defaultshell);
160 return $sh unless $verbose;
161
162 $new_shell = &confirm_list("Enter your default shell:", 0,
163 $sh, sort(keys %shell));
164 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
165 $changes++ if $new_shell ne $sh;
166 return $new_shell;
167 }
168
169 sub shell_default_valid {
170 local($sh) = @_;
171 local($s,$e);
172
173 return $sh if $shell{$sh};
174
175 foreach $e (@shellpref) {
176 $s = $e;
177 last if defined($shell{$s});
178 }
179 $s = "sh" unless $s;
180 warn "Shell ``$sh'' is undefined, use ``$s''\n";
181 return $s;
182 }
183
184 # return default home partition (e.g. "/home")
185 # create base directory if nesseccary
186 sub home_partition {
187 local($home) = @_;
188 $home = &stripdir($home);
189 local($h) = $home;
190
191 return $h if !$verbose && $h eq &home_partition_valid($h);
192
193 while(1) {
194 $h = &confirm_list("Enter your default HOME partition:", 1, $home, "");
195 $h = &stripdir($h);
196 last if $h eq &home_partition_valid($h);
197 }
198
199 $changes++ if $h ne $home;
200 return $h;
201 }
202
203 sub home_partition_valid {
204 local($h) = @_;
205
206 $h = &stripdir($h);
207 # all right (I hope)
208 return $h if $h =~ "^/" && -e $h && -w $h && (-d $h || -l $h);
209
210 # Errors or todo
211 if ($h !~ "^/") {
212 warn "Please use absolute path for home: ``$h''.\a\n";
213 return 0;
214 }
215
216 if (-e $h) {
217 warn "$h exists, but is not a directory or symlink!\n"
218 unless -d $h || -l $h;
219 warn "$h is not writable!\n"
220 unless -w $h;
221 return 0;
222 } else {
223 # create home partition
224 return $h if &mkdir_home($h);
225 }
226 return 0;
227 }
228
229 # check for valid passwddb
230 sub passwd_check {
231 system(@pwd_mkdb, '-C', $etc_passwd);
232 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
233 }
234
235 # read /etc/passwd
236 sub passwd_read {
237 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
238
239 print "Check $etc_passwd\n" if $verbose;
240 open(P, "$etc_passwd") || die "$etc_passwd: $!\n";
241
242 while(<P>) {
243 chop;
244 push(@passwd_backup, $_);
245 # ignore comments
246 next if /^\s*$/;
247 next if /^\s*#/;
248
249 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
250
251 print "$p_username already exists with uid: $username{$p_username}!\n"
252 if $username{$p_username} && $verbose;
253 $username{$p_username} = $p_uid;
254 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
255 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
256 print "User $p_username: illegal shell: ``$sh''\n"
257 if ($verbose && $sh &&
258 !$shell{&basename($sh)} &&
259 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
260 $sh !~ /\/(pppd|sliplogin|nologin|nonexistent)$/);
261 $uid{$p_uid} = $p_username;
262 $pwgid{$p_gid} = $p_username;
263 }
264 close P;
265 }
266
267 # read /etc/group
268 sub group_read {
269 local($g_groupname,$pw,$g_gid, $memb);
270
271 print "Check $group\n" if $verbose;
272 open(G, "$group") || die "$group: $!\n";
273 while(<G>) {
274 chop;
275 push(@group_backup, $_);
276 # Ignore empty lines
277 next if /^\s*$/;
278 # Save comments to restore later
279 if (/^\s*\#/) {
280 push(@group_comments, $_);
281 next;
282 }
283
284 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
285
286 $groupmembers{$g_gid} = $memb;
287 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
288 if $groupname{$g_groupname} && $verbose;
289 $groupname{$g_groupname} = $g_gid;
290 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
291 if $gid{$g_gid} && $verbose;
292 $gid{$g_gid} = $g_groupname;
293 }
294 close G;
295 }
296
297 # check gids /etc/passwd <-> /etc/group
298 sub group_check {
299 local($c_gid, $c_username, @list);
300
301 foreach $c_gid (keys %pwgid) {
302 if (!$gid{$c_gid}) {
303 $c_username = $pwgid{$c_gid};
304 warn "User ``$c_username'' has gid $c_gid but a group with this " .
305 "gid does not exist.\n" if $verbose;
306 }
307 }
308 }
309
310 #
311 # main loop for creating new users
312 #
313
314 # return username
315 sub new_users_name {
316 local($name);
317
318 while(1) {
319 $name = &confirm_list("Enter username", 1, $usernameregexp, "");
320 last if (&new_users_name_valid($name));
321 }
322 return $name;
323 }
324
325 sub new_users_name_valid {
326 local($name) = @_;
327
328 if ($name eq $usernameregexp) { # user/admin just pressed <Return>
329 warn "Please enter a username\a\n";
330 return 0;
331 } elsif (length($name) > 16) {
332 warn "Username is longer than 16 characters.\a\n";
333 return 0;
334 } elsif ($name =~ /[:\n]/) {
335 warn "Username cannot contain colon or newline characters.\a\n";
336 return 0;
337 } elsif ($name !~ /$usernameregexp/) {
338 if ($usernameregexp eq $defaultusernameregexp) {
339 warn "Illegal username.\n" .
340 "Please use only lowercase Roman, decimal, underscore, " .
341 "or hyphen characters.\n" .
342 "Additionally, a username should not start with a hyphen.\a\n";
343 } else {
344 warn "Username doesn't match the regexp /$usernameregexp/\a\n";
345 }
346 return 0;
347 } elsif (defined($username{$name})) {
348 warn "Username ``$name'' already exists!\a\n"; return 0;
349 }
350 return 1;
351 }
352
353 # return full name
354 sub new_users_fullname {
355 local($name) = @_;
356 local($fullname);
357
358 while(1) {
359 $fullname = &confirm_list("Enter full name", 1, "", "");
360 last if $fullname eq &new_users_fullname_valid($fullname);
361 }
362 $fullname = $name unless $fullname;
363 return $fullname;
364 }
365
366 sub new_users_fullname_valid {
367 local($fullname) = @_;
368
369 return $fullname if $fullname !~ /:/;
370
371 warn "``:'' is not allowed!\a\n";
372 return 0;
373 }
374
375 # return shell (full path) for user
376 sub new_users_shell {
377 local($sh);
378
379 $sh = &confirm_list("Enter shell", 0, $defaultshell, keys %shell);
380 return $shell{$sh};
381 }
382
383 # return home (full path) for user
384 # Note that the home path defaults to $home/$name for batch
385 sub new_users_home {
386 local($name) = @_;
387 local($userhome);
388
389 while(1) {
390 $userhome = &confirm_list("Enter home directory (full path)", 1, "$home/$name", "");
391 last if $userhome =~ /^\//;
392 warn qq{Home directory "$userhome" is not a full path\a\n};
393 }
394 return $userhome;
395 }
396
397 # return free uid and gid
398 sub new_users_id {
399 local($name) = @_;
400 local($u_id, $g_id) = &next_id($name);
401 local($u_id_tmp, $e);
402
403 while(1) {
404 $u_id_tmp = &confirm_list("Uid", 1, $u_id, "");
405 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
406 ! $uid{$u_id_tmp};
407 if ($uid{$u_id_tmp}) {
408 warn "Uid ``$u_id_tmp'' in use!\a\n";
409 $uid_start = $u_id_tmp;
410 ($u_id, $g_id) = &next_id($name);
411 next;
412 } else {
413 warn "Wrong uid.\a\n";
414 }
415 }
416 # use calculated uid
417 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
418 # recalculate gid
419 $uid_start = $u_id_tmp;
420 return &next_id($name);
421 }
422
423 # return login class for user
424 sub new_users_class {
425 local($def) = @_;
426 local($class);
427
428 $class = &confirm_list("Enter login class:", 1, $def, ($def, "default"));
429 $class = "" if $class eq "default";
430 return $class;
431 }
432
433 # add user to group
434 sub add_group {
435 local($gid, $name) = @_;
436
437 return 0 if
438 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
439
440 $groupmembers_bak{$gid} = $groupmembers{$gid};
441 $groupmembers{$gid} .= "," if $groupmembers{$gid};
442 $groupmembers{$gid} .= "$name";
443
444 return $name;
445 }
446
447
448 # return login group
449 sub new_users_grplogin {
450 local($name, $defaultgroup, $new_users_ok) = @_;
451 local($group_login, $group);
452
453 $group = $name;
454 $group = $defaultgroup if $defaultgroup ne $group_uniq;
455
456 if ($new_users_ok) {
457 # clean up backup
458 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
459 } else {
460 # restore old groupmembers, user was not accept
461 foreach $e (keys %groupmembers_bak) {
462 $groupmembers{$e} = $groupmembers_bak{$e};
463 }
464 }
465
466 while(1) {
467 $group_login = &confirm_list("Login group", 1, $group,
468 ($name, $group));
469 last if $group_login eq $group;
470 last if $group_login eq $name;
471 last if defined $groupname{$group_login};
472 if ($group_login eq $group_uniq) {
473 $group_login = $name; last;
474 }
475
476 if (defined $gid{$group_login}) {
477 # convert numeric groupname (gid) to groupname
478 $group_login = $gid{$group_login};
479 last;
480 }
481 warn "Group does not exist!\a\n";
482 }
483
484 #if (defined($groupname{$group_login})) {
485 # &add_group($groupname{$group_login}, $name);
486 #}
487
488 return ($group_login, $group_uniq) if $group_login eq $name;
489 return ($group_login, $group_login);
490 }
491
492 # return other groups (string)
493 sub new_users_groups {
494 local($name, $other_groups) = @_;
495 local($string) =
496 "Login group is ``$group_login''. Invite $name into other groups:";
497 local($e, $flag);
498 local($new_groups,$groups);
499
500 $other_groups = "no" unless $other_groups;
501
502 while(1) {
503 $groups = &confirm_list($string, 1, $other_groups,
504 ("no", $other_groups, "guest"));
505 # no other groups
506 return "" if $groups eq "no";
507
508 ($flag, $new_groups) = &new_users_groups_valid($groups);
509 last unless $flag;
510 }
511 $new_groups =~ s/\s*$//;
512 return $new_groups;
513 }
514
515 sub new_users_groups_valid {
516 local($groups) = @_;
517 local($e, $new_groups);
518 local($flag) = 0;
519
520 foreach $e (split(/[,\s]+/, $groups)) {
521 # convert numbers to groupname
522 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
523 $e = $gid{$e};
524 }
525 if (defined($groupname{$e})) {
526 if ($e eq $group_login) {
527 # do not add user to a group if this group
528 # is also the login group.
529 } elsif (&add_group($groupname{$e}, $name)) {
530 $new_groups .= "$e ";
531 } else {
532 warn "$name is already member of group ``$e''\n";
533 }
534 } else {
535 warn "Group ``$e'' does not exist\a\n"; $flag++;
536 }
537 }
538 return ($flag, $new_groups);
539 }
540
541 # your last change
542 sub new_users_ok {
543 local ($newpasswd);
544 # Note that we either show "password disabled" or
545 # "****" .. we don't show "empty password" since
546 # the whole point of starring out the password in
547 # the first place is to stop people looking over your
548 # shoulder and seeing the password.. -- adrian
549 if ($usepassword eq "no") {
550 $newpasswd = "Password disabled";
551 } elsif ($enableaccount eq "no") {
552 $newpasswd = "Password disabled";
553 } else {
554 $newpasswd = "****";
555 }
556
557 print <<EOF;
558
559 Name: $name
560 Password: $newpasswd
561 Fullname: $fullname
562 Uid: $u_id
563 Gid: $g_id ($group_login)
564 Class: $class
565 Groups: $group_login $new_groups
566 HOME: $userhome
567 Shell: $sh
568 EOF
569
570 return &confirm_yn("OK?", "yes");
571 }
572
573 # make password database
574 sub new_users_pwdmkdb {
575 local($last) = shift;
576 local($name) = shift;
577
578 system(@pwd_mkdb, '-u', $name, $etc_passwd);
579 if ($?) {
580 warn "$last\n";
581 warn "``@pwd_mkdb'' failed\n";
582 exit($? >> 8);
583 }
584 }
585
586 # update group database
587 sub new_users_group_update {
588 local($e, @a);
589
590 # Add *new* group
591 if (!defined($groupname{$group_login}) &&
592 !defined($gid{$groupname{$group_login}})) {
593 push(@group_backup, "$group_login:*:$g_id:");
594 $groupname{$group_login} = $g_id;
595 $gid{$g_id} = $group_login;
596 # $groupmembers{$g_id} = $group_login;
597 }
598
599 if ($new_groups || defined($groupname{$group_login}) ||
600 defined($gid{$groupname{$group_login}}) &&
601 $gid{$groupname{$group_login}} ne "+") {
602 # new user is member of some groups
603 # new login group is already in name space
604 rename($group, "$group.bak");
605 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
606
607 # Restore comments from the top of the group file
608 @a = @group_comments;
609 foreach $e (sort {$a <=> $b} (keys %gid)) {
610 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
611 }
612 &append_file($group, @a);
613 } else {
614 &append_file($group, "$group_login:*:$g_id:");
615 }
616
617 }
618
619 sub new_users_passwd_update {
620 # update passwd/group variables
621 push(@passwd_backup, $new_entry);
622 $username{$name} = $u_id;
623 $uid{$u_id} = $name;
624 $pwgid{$g_id} = $name;
625 }
626
627 # send message to new user
628 sub new_users_sendmessage {
629 return 1 if $send_message eq "no";
630
631 local($cc) =
632 &confirm_list("Send message to ``$name'' and:",
633 1, "no", ("root", "second_mail_address", "no"));
634 local($e);
635 $cc = "" if $cc eq "no";
636
637 foreach $e (@message_buffer) {
638 print eval "\"$e\"";
639 }
640 print "\n";
641
642 local(@message_buffer_append) = ();
643 if (!&confirm_yn("Add anything to default message", "no")) {
644 print "Use ``.'' or ^D alone on a line to finish your message.\n";
645 push(@message_buffer_append, "\n");
646 while($read = <STDIN>) {
647 last if $read eq "\.\n";
648 push(@message_buffer_append, $read);
649 }
650 }
651
652 &sendmessage("$name $cc", (@message_buffer, @message_buffer_append))
653 if (&confirm_yn("Send message", "yes"));
654 }
655
656 sub sendmessage {
657 local($to, @message) = @_;
658 local($e);
659
660 if (!open(M, "| mail -s Welcome $to")) {
661 warn "Cannot send mail to: $to!\n";
662 return 0;
663 } else {
664 foreach $e (@message) {
665 print M eval "\"$e\"";
666 }
667 close M;
668 }
669 }
670
671
672 sub new_users_password {
673
674 local($password);
675
676 while(1) {
677 system("stty -echo");
678 $password = &confirm_list("Enter password", 1, "", "");
679 system("stty echo");
680 print "\n";
681 if ($password ne "") {
682 system("stty -echo");
683 $newpass = &confirm_list("Enter password again", 1, "", "");
684 system("stty echo");
685 print "\n";
686 last if $password eq $newpass;
687 print "They didn't match, please try again\n";
688 }
689 elsif (&confirm_yn("Use an empty password?", "yes")) {
690 last;
691 }
692 }
693
694 return $password;
695 }
696
697 sub new_users_use_password {
698 local ($p) = $defaultusepassword;
699 $p = &confirm_yn("Use password-based authentication", $defaultusepassword);
700 return "yes" if (($defaultusepassword eq "yes" && $p) ||
701 ($defaultusepassword eq "no" && !$p));
702 return "no"; # otherwise
703 }
704
705 sub new_users_enable_account {
706 local ($p) = $defaultenableaccount;
707 $p = &confirm_yn("Enable account password at creation", $defaultenableaccount);
708 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
709 ($defaultenableaccount eq "no" && !$p));
710 return "no"; # otherwise
711 }
712
713 sub new_users_empty_password {
714 local ($p) = $defaultemptypassword;
715 $p = &confirm_yn("Use an empty password", $defaultemptypassword);
716 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
717 ($defaultemptypassword eq "no" && !$p));
718 return "no"; # otherwise
719 }
720
721 sub new_users {
722
723 print "\n" if $verbose;
724 print "Ok, let's go.\n" .
725 "Don't worry about mistakes. I will give you the chance later to " .
726 "correct any input.\n" if $verbose;
727
728 # name: Username
729 # fullname: Full name
730 # sh: shell
731 # userhome: home path for user
732 # u_id: user id
733 # g_id: group id
734 # class: login class
735 # group_login: groupname of g_id
736 # new_groups: some other groups
737 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
738 local($userhome);
739 local($groupmembers_bak, $cryptpwd);
740 local($new_users_ok) = 1;
741
742
743 $new_groups = "no";
744 $new_groups = "no" unless $groupname{$new_groups};
745
746 while(1) {
747 $name = &new_users_name;
748 $fullname = &new_users_fullname($name);
749 $sh = &new_users_shell;
750 $userhome = &new_users_home($name);
751 ($u_id, $g_id) = &new_users_id($name);
752 $class = &new_users_class($defaultclass);
753 ($group_login, $defaultgroup) =
754 &new_users_grplogin($name, $defaultgroup, $new_users_ok);
755 # do not use uniq username and login group
756 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
757
758
759 # The tricky logic:
760 # If $usepasswd is 0, we use a * as a password
761 # If $usepasswd is 1, then
762 # if $enableaccount is 0, we prepend * as a password
763 # else if $enableaccount is 1 we don't prepend anything
764 # if $useemptypassword is 0 we ask for a password,
765 # else we use a blank one
766 #
767 # The logic is tasty, I'll give you that, but its flexible and
768 # it'll stop people shooting themselves in the foot.
769
770 $new_groups = &new_users_groups($name, $new_groups);
771
772 $usepassword = &new_users_use_password;
773 if ($usepassword eq "no") {
774 # note that the assignments to enableaccount and
775 # useemptypassword functionally do the same as
776 # usepasswd == "no". Just for consistency.
777 $password = ""; # no password!
778 $enableaccount = "no"; # doesn't matter here
779 $useemptypassword = "yes"; # doesn't matter here
780 } else {
781 $useemptypassword = &new_users_empty_password;
782 if ($useemptypassword eq "no") {
783 $password = &new_users_password;
784 }
785 $enableaccount = &new_users_enable_account;
786 }
787
788 if (&new_users_ok) {
789 $new_users_ok = 1;
790
791 $cryptpwd = "";
792 $cryptpwd = crypt($password, &salt) if $password ne "";
793
794 if ($usepassword eq "no") {
795 $cryptpwd = "*";
796 } else {
797 # cryptpwd is valid before this if mess, so if
798 # blankpasswd is no we don't blank the cryptpwd
799 if ($useemptypassword eq "yes") {
800 $cryptpwd = "";
801 }
802 if ($enableaccount eq "no") {
803 $cryptpwd = "*" . $cryptpwd;
804 }
805 }
806 # obscure perl bug
807 $new_entry = "$name\:" . "$cryptpwd" .
808 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
809 &append_file($etc_passwd, "$new_entry");
810 &new_users_pwdmkdb("$new_entry", $name);
811 &new_users_group_update;
812 &new_users_passwd_update; print "Added user ``$name''\n";
813 &new_users_sendmessage;
814 &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname");
815 &home_create($userhome, $name, $group_login);
816 } else {
817 $new_users_ok = 0;
818 }
819 if (!&confirm_yn("Add another user?", "yes")) {
820 print "Goodbye!\n" if $verbose;
821 last;
822 }
823 print "\n" if !$verbose;
824 }
825 }
826
827 # ask for password usage
828 sub password_default {
829 local($p) = $defaultusepassword;
830 if ($verbose) {
831 $p = &confirm_yn("Use password-based authentication", $defaultusepassword);
832 $changes++ unless $p;
833 }
834 return "yes" if (($defaultusepassword eq "yes" && $p) ||
835 ($defaultusepassword eq "no" && !$p));
836 return "no"; # otherwise
837 }
838
839 # ask for account enable usage
840 sub enable_account_default {
841 local ($p) = $defaultenableaccount;
842 if ($verbose) {
843 $p = &confirm_yn("Enable account password at creation", $defaultenableaccount);
844 $changes++ unless $p;
845 }
846 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
847 ($defaultenableaccount eq "no" && !$p));
848 return "no"; # otherwise
849 }
850
851 # ask for empty password
852 sub enable_empty_password {
853 local ($p) = $defaultemptypassword;
854 if ($verbose) {
855 $p = &confirm_yn("Use an empty password", $defaultemptypassword);
856 $changes++ unless $p;
857 }
858 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
859 ($defaultemptypassword eq "no" && !$p));
860 return "no"; # otherwise
861 }
862
863 # misc
864 sub check_root {
865 die "You are not root!\n" if $< && !$test;
866 }
867
868 sub usage {
869 warn <<USAGE;
870 usage: adduser
871 [-check_only]
872 [-class login_class]
873 [-config_create]
874 [-dotdir dotdir]
875 [-group login_group]
876 [-h|-help]
877 [-home home]
878 [-message message_file]
879 [-noconfig]
880 [-shell shell]
881 [-s|-silent|-q|-quiet]
882 [-uid uid_start]
883 [-v|-verbose]
884
885 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
886 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
887 USAGE
888 exit 1;
889 }
890
891 # uniq(1)
892 sub uniq {
893 local(@list) = @_;
894 local($e, $last, @array);
895
896 foreach $e (sort @list) {
897 push(@array, $e) unless $e eq $last;
898 $last = $e;
899 }
900 return @array;
901 }
902
903 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
904 sub salt {
905 local($salt); # initialization
906 local($i, $rand);
907 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
908
909 warn "calculate salt\n" if $verbose > 1;
910 # to64
911 for ($i = 0; $i < 27; $i++) {
912 srand(time + $rand + $$);
913 $rand = rand(25*29*17 + $rand);
914 $salt .= $itoa64[$rand & $#itoa64];
915 }
916 warn "Salt is: $salt\n" if $verbose > 1;
917
918 return $salt;
919 }
920
921
922 # print banner
923 sub copyright {
924 return;
925 }
926
927 # hints
928 sub hints {
929 if ($verbose) {
930 print "Use option ``-silent'' if you don't want to see " .
931 "all warnings and questions.\n\n";
932 } else {
933 print "Use option ``-verbose'' if you want to see more warnings and " .
934 "questions \nor try to repair bugs.\n\n";
935 }
936 }
937
938 #
939 sub parse_arguments {
940 local(@argv) = @_;
941
942 while ($_ = $argv[0], /^-/) {
943 shift @argv;
944 last if /^--$/;
945 if (/^--?(v|verbose)$/) { $verbose = 1 }
946 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
947 elsif (/^--?(debug)$/) { $verbose = 2 }
948 elsif (/^--?(h|help|\?)$/) { &usage }
949 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
950 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
951 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
952 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
953 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
954 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
955 elsif (/^--?(check_only)$/) { $check_only = 1 }
956 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
957 $sendmessage = 1; }
958 elsif (/^--?(batch)$/) {
959 warn "The -batch option is not supported anymore.\n",
960 "Please use the pw(8) command line tool!\n";
961 exit(0);
962 }
963 # see &config_read
964 elsif (/^--?(config_create)$/) { &create_conf; }
965 elsif (/^--?(noconfig)$/) { $config_read = 0; }
966 else { &usage }
967 }
968 #&usage if $#argv < 0;
969 }
970
971 sub basename {
972 local($name) = @_;
973 $name =~ s|/+$||;
974 $name =~ s|.*/+||;
975 return $name;
976 }
977
978 sub dirname {
979 local($name) = @_;
980 $name = &stripdir($name);
981 $name =~ s|/+[^/]+$||;
982 $name = "/" unless $name; # dirname of / is /
983 return $name;
984 }
985
986 # return 1 if $file is a readable file or link
987 sub filetest {
988 local($file, $verb) = @_;
989
990 if (-e $file) {
991 if (-f $file || -l $file) {
992 return 1 if -r _;
993 warn "$file unreadable\n" if $verbose;
994 } else {
995 warn "$file is not a plain file or link\n" if $verbose;
996 }
997 }
998 return 0;
999 }
1000
1001 # create configuration files and exit
1002 sub create_conf {
1003 $create_conf = 1;
1004 if ($send_message ne 'no') {
1005 &message_create($send_message);
1006 } else {
1007 &message_create($send_message_bak);
1008 }
1009 &config_write(1);
1010 exit(0);
1011 }
1012
1013 # log for new user in /var/log/adduser
1014 sub adduser_log {
1015 local($string) = @_;
1016 local($e);
1017
1018 return 1 if $logfile eq "no";
1019
1020 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1021 $year += 1900;
1022 $mon++;
1023
1024 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
1025 # '7' -> '07'
1026 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1027 }
1028
1029 &append_file($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1030 }
1031
1032 # create HOME directory, copy dotfiles from $dotdir to $HOME
1033 sub home_create {
1034 local($homedir, $name, $group) = @_;
1035 local($rootdir);
1036
1037 if (-e "$homedir") {
1038 warn "HOME Directory ``$homedir'' already exist\a\n";
1039 return 0;
1040 }
1041
1042 # if the home directory prefix doesn't exist, create it
1043 # First, split the directory into a list; then remove the user's dir
1044 @dir = split('/', $homedir); pop(@dir);
1045 # Put back together & strip to get directory prefix
1046 $rootdir = &stripdir(join('/', @dir));
1047
1048 if (!&mkdirhier("$rootdir")) {
1049 # warn already displayed
1050 return 0;
1051 }
1052
1053 if ($dotdir eq 'no') {
1054 if (!mkdir("$homedir", 0755)) {
1055 warn "$dir: $!\n"; return 0;
1056 }
1057 system 'chown', "$name:$group", $homedir;
1058 return !$?;
1059 }
1060
1061 # copy files from $dotdir to $homedir
1062 # rename 'dot.foo' files to '.foo'
1063 print "Copy files from $dotdir to $homedir\n" if $verbose;
1064 system('cp', '-R', $dotdir, $homedir);
1065 system('chmod', '-R', 'u+wrX,go-w', $homedir);
1066 system('chown', '-Rh', "$name:$group", $homedir);
1067
1068 # security
1069 opendir(D, $homedir);
1070 foreach $file (readdir(D)) {
1071 if ($file =~ /^dot\./ && -f "$homedir/$file") {
1072 $file =~ s/^dot\././;
1073 rename("$homedir/dot$file", "$homedir/$file");
1074 }
1075 chmod(0600, "$homedir/$file")
1076 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1077 chmod(0700, "$homedir/$file")
1078 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1079 }
1080 closedir D;
1081 return 1;
1082 }
1083
1084 # makes a directory hierarchy
1085 sub mkdir_home {
1086 local($dir) = @_;
1087 $dir = &stripdir($dir);
1088 local($user_partition) = "/usr";
1089 local($dirname) = &dirname($dir);
1090
1091 -e $dirname || &mkdirhier($dirname);
1092
1093 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1094 # home partition is on root partition
1095 # create home partition on $user_partition and make
1096 # a symlink from $dir to $user_partition/`basename $dir`
1097 # For instance: /home -> /usr/home
1098
1099 local($basename) = &basename($dir);
1100 local($d) = "$user_partition/$basename";
1101
1102
1103 if (-d $d) {
1104 warn "Oops, $d already exist\n" if $verbose;
1105 } else {
1106 print "Create $d\n" if $verbose;
1107 if (!mkdir("$d", 0755)) {
1108 warn "$d: $!\a\n"; return 0;
1109 }
1110 }
1111
1112 unlink($dir); # symlink to nonexist file
1113 print "Create symlink: $dir -> $d\n" if $verbose;
1114 if (!symlink("$d", $dir)) {
1115 warn "Symlink $d: $!\a\n"; return 0;
1116 }
1117 } else {
1118 print "Create $dir\n" if $verbose;
1119 if (!mkdir("$dir", 0755)) {
1120 warn "Directory ``$dir'': $!\a\n"; return 0;
1121 }
1122 }
1123 return 1;
1124 }
1125
1126 sub mkdirhier {
1127 local($dir) = @_;
1128 local($d,$p);
1129
1130 $dir = &stripdir($dir);
1131
1132 foreach $d (split('/', $dir)) {
1133 $dir = "$p/$d";
1134 $dir =~ s|^//|/|;
1135 if (! -e "$dir") {
1136 print "Create $dir\n" if $verbose;
1137 if (!mkdir("$dir", 0755)) {
1138 warn "$dir: $!\n"; return 0;
1139 }
1140 }
1141 $p .= "/$d";
1142 }
1143 return 1;
1144 }
1145
1146 # stript unused '/'
1147 # F.i.: //usr///home// -> /usr/home
1148 sub stripdir {
1149 local($dir) = @_;
1150
1151 $dir =~ s|/+|/|g; # delete double '/'
1152 $dir =~ s|/$||; # delete '/' at end
1153 return $dir if $dir ne "";
1154 return '/';
1155 }
1156
1157 # Read one of the elements from @list. $confirm is default.
1158 # If !$allow accept only elements from @list.
1159 sub confirm_list {
1160 local($message, $allow, $confirm, @list) = @_;
1161 local($read, $c, $print);
1162
1163 $print = "$message" if $message;
1164 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1165
1166 $print .= join($", &uniq(@list)); #"
1167 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1168 print "$print";
1169 print "\n" if (length($print) + length($confirm)) > 60;
1170 print "[$confirm]: ";
1171
1172 chop($read = <STDIN>);
1173 $read =~ s/^\s*//;
1174 $read =~ s/\s*$//;
1175 return $confirm if $read eq "";
1176 return "$read" if $allow;
1177
1178 foreach $c (@list) {
1179 return $read if $c eq $read;
1180 }
1181 warn "$read: is not allowed!\a\n";
1182 return &confirm_list($message, $allow, $confirm, @list);
1183 }
1184
1185 # YES or NO question
1186 # return 1 if &confirm("message", "yes") and answer is yes
1187 # or if &confirm("message", "no") an answer is no
1188 # otherwise 0
1189 sub confirm_yn {
1190 local($message, $confirm) = @_;
1191 local($yes) = '^(yes|YES|y|Y)$';
1192 local($no) = '^(no|NO|n|N)$';
1193 local($read, $c);
1194
1195 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1196 $confirm = "y";
1197 } else {
1198 $confirm = "n";
1199 }
1200 print "$message (y/n) [$confirm]: ";
1201 chop($read = <STDIN>);
1202 $read =~ s/^\s*//;
1203 $read =~ s/\s*$//;
1204 return 1 unless $read;
1205
1206 if (($confirm eq "y" && $read =~ "$yes") ||
1207 ($confirm eq "n" && $read =~ "$no")) {
1208 return 1;
1209 }
1210
1211 if ($read !~ "$yes" && $read !~ "$no") {
1212 warn "Wrong value. Enter again!\a\n";
1213 return &confirm_yn($message, $confirm);
1214 }
1215 return 0;
1216 }
1217
1218 # allow configuring usernameregexp
1219 sub usernameregexp_default {
1220 local($r) = $usernameregexp;
1221
1222 while ($verbose) {
1223 $r = &confirm_list("Usernames must match regular expression:", 1,
1224 $r, "");
1225 eval "'foo' =~ /$r/";
1226 last unless $@;
1227 warn "Invalid regular expression\a\n";
1228 }
1229 $changes++ if $r ne $usernameregexp;
1230 return $r;
1231 }
1232
1233 # test if $dotdir exist
1234 # return "no" if $dotdir not exist or dotfiles should not copied
1235 sub dotdir_default {
1236 local($dir) = $dotdir;
1237
1238 return &dotdir_default_valid($dir) unless $verbose;
1239 while($verbose) {
1240 $dir = &confirm_list("Copy dotfiles from:", 1,
1241 $dir, ("no", $dotdir_bak, $dir));
1242 last if $dir eq &dotdir_default_valid($dir);
1243 }
1244 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1245
1246 $changes++ if $dir ne $dotdir;
1247 return $dir;
1248 }
1249
1250 sub dotdir_default_valid {
1251 local($dir) = @_;
1252
1253 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1254 return $dir if $dir eq "no";
1255 warn "Dotdir ``$dir'' is not a directory\a\n";
1256 return "no";
1257 }
1258
1259 # ask for messages to new users
1260 sub message_default {
1261 local($file) = $send_message;
1262 local(@d) = ($file, $send_message_bak, "no");
1263
1264 while($verbose) {
1265 $file = &confirm_list("Send message from file:", 1, $file, @d);
1266 last if $file eq "no";
1267 last if &filetest($file, 1);
1268
1269 # maybe create message file
1270 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1271 last if &filetest($file, 0);
1272 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1273 "yes");
1274 }
1275
1276 if ($file eq "no" || !&filetest($file, 0)) {
1277 warn "Do not send message\n" if $verbose;
1278 $file = "no";
1279 } else {
1280 &message_read($file);
1281 }
1282
1283 $changes++ if $file ne $send_message && $verbose;
1284 return $file;
1285 }
1286
1287 # create message file
1288 sub message_create {
1289 local($file) = @_;
1290
1291 rename($file, "$file.bak");
1292 if (!open(M, "> $file")) {
1293 warn "Messagefile ``$file'': $!\n"; return 0;
1294 }
1295 print M <<EOF;
1296 #
1297 # Message file for adduser(8)
1298 # comment: ``#''
1299 # default variables: \$name, \$fullname, \$password
1300 # other variables: see /etc/adduser.conf after
1301 # line ``$do_not_delete''
1302 #
1303
1304 \$fullname,
1305
1306 your account ``\$name'' was created.
1307 Have fun!
1308
1309 See also chpass(1), finger(1), passwd(1)
1310 EOF
1311 close M;
1312 return 1;
1313 }
1314
1315 # read message file into buffer
1316 sub message_read {
1317 local($file) = @_;
1318 @message_buffer = '';
1319
1320 if (!open(R, "$file")) {
1321 warn "File ``$file'':$!\n"; return 0;
1322 }
1323 while(<R>) {
1324 push(@message_buffer, $_) unless /^\s*#/;
1325 }
1326 close R;
1327 }
1328
1329 # write @list to $file with file-locking
1330 sub append_file {
1331 local($file,@list) = @_;
1332 local($e);
1333 local($LOCK_EX) = 2;
1334 local($LOCK_NB) = 4;
1335 local($LOCK_UN) = 8;
1336
1337 open(F, ">> $file") || die "$file: $!\n";
1338 print "Lock $file.\n" if $verbose > 1;
1339 while(!flock(F, $LOCK_EX | $LOCK_NB)) {
1340 warn "Cannot lock file: $file\a\n";
1341 die "Sorry, give up\n"
1342 unless &confirm_yn("Try again?", "yes");
1343 }
1344 print F join("\n", @list) . "\n";
1345 close F;
1346 print "Unlock $file.\n" if $verbose > 1;
1347 flock(F, $LOCK_UN);
1348 }
1349
1350 # return free uid+gid
1351 # uid == gid if possible
1352 sub next_id {
1353 local($group) = @_;
1354
1355 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1356 # looking for next free uid
1357 while($uid{$uid_start}) {
1358 $uid_start++;
1359 $uid_start = 1000 if $uid_start >= $uid_end;
1360 print "$uid_start\n" if $verbose > 1;
1361 }
1362
1363 local($gid_start) = $uid_start;
1364 # group for user (username==groupname) already exist
1365 if ($groupname{$group}) {
1366 $gid_start = $groupname{$group};
1367 }
1368 # gid is in use, looking for another gid.
1369 # Note: uid an gid are not equal
1370 elsif ($gid{$uid_start}) {
1371 while($gid{$gid_start} || $uid{$gid_start}) {
1372 $gid_start--;
1373 $gid_start = $uid_end if $gid_start < 100;
1374 }
1375 }
1376 return ($uid_start, $gid_start);
1377 }
1378
1379 # read config file
1380 sub config_read {
1381 local($opt) = @_;
1382 local($user_flag) = 0;
1383
1384 # don't read config file
1385 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1386
1387 if(!open(C, "$config")) {
1388 warn "$config: $!\n"; return 0;
1389 }
1390
1391 while(<C>) {
1392 # user defined variables
1393 /^$do_not_delete/ && $user_flag++;
1394 # found @array or $variable
1395 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1396 eval $_;
1397 #warn "$_";
1398 }
1399 # lines with '^##' are not saved
1400 push(@user_variable_list, $_)
1401 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1402 }
1403 #warn "X @user_variable_list X\n";
1404 close C;
1405 }
1406
1407
1408 # write config file
1409 sub config_write {
1410 local($silent) = @_;
1411
1412 # nothing to do
1413 return 1 unless ($changes || ! -e $config || !$config_read || $silent);
1414
1415 if (!$silent) {
1416 if (-e $config) {
1417 return 1 if &confirm_yn("\nWrite your changes to $config?", "no");
1418 } else {
1419 return 1 unless
1420 &confirm_yn("\nWrite your configuration to $config?", "yes");
1421 }
1422 }
1423
1424 rename($config, "$config.bak");
1425 open(C, "> $config") || die "$config: $!\n";
1426
1427 # prepare some variables
1428 $send_message = "no" unless $send_message;
1429 $defaultusepassword = "no" unless $defaultusepassword;
1430 $defaultenableaccount = "yes" unless $defaultenableaccount;
1431 $defaultemptypassword = "no" unless $defaultemptypassword;
1432 local($shpref) = "'" . join("', '", @shellpref) . "'";
1433 local($shpath) = "'" . join("', '", @path) . "'";
1434 local($user_var) = join('', @user_variable_list);
1435
1436 print C <<EOF;
1437 #
1438 # $config - automatic generated by adduser(8)
1439 #
1440 # Note: adduser read *and* write this file.
1441 # You may change values, but don't add new things before the
1442 # line ``$do_not_delete''
1443 #
1444
1445 # verbose = [0-2]
1446 verbose = $verbose
1447
1448 # regular expression usernames are checked against (see perlre(1))
1449 # usernameregexp = 'regexp'
1450 usernameregexp = '$usernameregexp'
1451
1452 # use password-based authentication for new users
1453 # defaultusepassword = "yes" | "no"
1454 defaultusepassword = "$defaultusepassword"
1455
1456 # enable account password at creation
1457 # (the password will be prepended with a star if the account isn't enabled)
1458 # defaultenableaccount = "yes" | "no"
1459 defaultenableaccount = "$defaultenableaccount"
1460
1461 # allow blank passwords
1462 # defaultemptypassword = "yes" | "no"
1463 defaultemptypassword = "$defaultemptypassword"
1464
1465 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1466 dotdir = "$dotdir"
1467
1468 # send this file to new user ("/etc/adduser.message" or "no")
1469 send_message = "$send_message"
1470
1471 # config file for adduser ("/etc/adduser.conf")
1472 config = "$config"
1473
1474 # logfile ("/var/log/adduser" or "no")
1475 logfile = "$logfile"
1476
1477 # default HOME directory ("/home")
1478 home = "$home"
1479
1480 # List of directories where shells located
1481 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1482 path = ($shpath)
1483
1484 # common shell list, first element has higher priority
1485 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1486 shellpref = ($shpref)
1487
1488 # defaultshell if not empty ("bash")
1489 defaultshell = "$defaultshell"
1490
1491 # defaultgroup ('USER' for same as username or any other valid group)
1492 defaultgroup = $defaultgroup
1493
1494 # defaultclass if not empty
1495 defaultclass = "$defaultclass"
1496
1497 # new users get this uid (1000)
1498 uid_start = "$uid_start"
1499
1500 $do_not_delete
1501 ## your own variables, see /etc/adduser.message
1502 $user_var
1503
1504 ## end
1505 EOF
1506 close C;
1507 }
1508
1509 ################
1510 # main
1511 #
1512 $test = 0; # test mode, only for development
1513 $check_only = 0;
1514
1515 &check_root; # you must be root to run this script!
1516 &variables; # initialize variables
1517 &config_read(@ARGV); # read variables form config-file
1518 &parse_arguments(@ARGV); # parse arguments
1519
1520 if (!$check_only) {
1521 &copyright; &hints;
1522 }
1523
1524 # check
1525 $changes = 0;
1526 &passwd_check; # check for valid passwdb
1527 &shells_read; # read /etc/shells
1528 &passwd_read; # read /etc/master.passwd
1529 &group_read; # read /etc/group
1530 &group_check; # check for incon*
1531 exit 0 if $check_only; # only check consistence and exit
1532
1533
1534 # interactive
1535 # some questions
1536 $usernameregexp = &usernameregexp_default; # regexp to check usernames against
1537 &shells_add; # maybe add some new shells
1538 $defaultshell = &shell_default; # enter default shell
1539 $home = &home_partition($home); # find HOME partition
1540 $dotdir = &dotdir_default; # check $dotdir
1541 $send_message = &message_default; # send message to new user
1542 $defaultusepassword = &password_default; # maybe use password
1543 if ($defaultusepassword eq "no") {
1544 if ($verbose) {
1545 print "Creating accounts with a locked password.\n";
1546 }
1547 $defaultenableaccount = "no";
1548 $defaultemptypassword = "yes";
1549 } else {
1550 $defaultenableaccount = &enable_account_default; # enable or disable account
1551 $defaultemptypassword = &enable_empty_password; # use empty password or not
1552 }
1553 &config_write(!$verbose); # write variables in file
1554
1555 # main loop for creating new users
1556 &new_users; # add new users
1557
1558 #end