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