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