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