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