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