]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/adduser.perl
3 # Copyright (c) 1995 Wolfram Schneider. All rights reserved.
4 # Alle Rechte vorbehalten. Es gilt das kontinentaleuropische Urheberrecht.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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
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.
31 # /usr/sbin/adduser - add new user(s)
33 # Email: Wolfram Schneider <wosch@cs.tu-berlin.de>
35 # $Id: adduser.perl,v 1.11 1996/05/06 06:15:19 gclarkii Exp $
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
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');
61 $defaultshell = 'sh'; # defaultshell if not empty
63 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
65 $uid_start = 1000; # new users get this uid
66 $uid_end = 32000; # max. uid
70 $username = ''; # $username{username} = uid
71 $uid = ''; # $uid{uid} = username
72 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
74 $password = ''; # password for new users
77 $groupname =''; # $groupname{groupname} = gid
78 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
79 $gid = ''; # $gid{gid} = groupname; gid form group db
82 $shell = ''; # $shell{`basename sh`} = sh
84 # only for me (=Wolfram)
86 $home = "/home/w/tmp/adduser/home";
87 $etc_shells = "./shells";
88 $etc_passwd = "./master.passwd";
90 $pwd_mkdb = "pwd_mkdb -p -d .";
91 $config = "adduser.conf";
92 $send_message = "./adduser.message";
93 $logfile = "./log.adduser";
96 umask 022; # don't give login group write access
98 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
101 @message_buffer = '';
102 @user_variable_list = ''; # user variables in /etc/adduser.conf
103 $do_not_delete = '## DO NOT DELETE THIS LINE!';
106 # read shell database, see also: shells(5)
111 print "Check $etc_shells\n" if $verbose;
112 open(S
, $etc_shells) || die "$etc_shells:$!\n";
116 s/^\s*//; s/\s+.*//; # chop
119 $shell{&basename
($sh)} = $sh;
121 warn "Shell: $sh not executable!\n";
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";
132 push(@list, "/bin/date");
133 push(@shellpref, "date");
134 $shell{"date"} = "/bin/date";
139 # add new shells if possible
141 local($sh,$dir,@list);
143 return 1 unless $verbose;
145 foreach $sh (@shellpref) {
148 # shell $sh is not defined as login shell
149 foreach $dir (@path) {
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";
162 &append_file
($etc_shells, @list) if $#list >= 0;
165 # choise your favourite shell an return the shell
167 local($e,$i,$new_shell);
170 $sh = &shell_default_valid
($defaultshell);
171 return $sh unless $verbose;
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;
180 sub shell_default_valid
{
184 return $sh if $shell{$sh};
186 foreach $e (@shellpref) {
188 last if defined($shell{$s});
191 warn "Shell ``$sh'' is undefined, use ``$s''\n";
195 # return default home partition (f.e. "/home")
196 # create base directory if nesseccary
199 $home = &stripdir
($home);
202 return $h if !$verbose && $h eq &home_partition_valid
($h);
205 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
207 last if $h eq &home_partition_valid
($h);
210 $changes++ if $h ne $home;
214 sub home_partition_valid
{
219 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
223 warn "Please use absolute path for home: ``$h''.\a\n";
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"
234 # create home partition
235 return $h if &mkdir_home
($h);
240 # check for valid passwddb
242 system("$pwd_mkdb $etc_passwd");
243 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
248 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
250 print "Check $etc_passwd\n" if $verbose;
251 open(P
, "$etc_passwd") || die "$passwd: $!\n";
255 push(@passwd_backup, $_);
256 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
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;
276 local($g_groupname,$pw,$g_gid, $memb);
278 print "Check $group\n" if $verbose;
279 open(G
, "$group") || die "$group: $!\n";
282 push(@group_backup, $_);
283 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
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;
296 # check gids /etc/passwd <-> /etc/group
298 local($c_gid, $c_username, @list);
300 foreach $c_gid (keys %pwgid) {
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;
310 # main loop for creating new users
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";
323 last if (&new_users_name_valid
($name) eq $name);
328 sub new_users_name_valid
{
331 if ($name !~ /^[a-z0-9]+$/) {
332 warn "Wrong username. " .
333 "Please use only lowercase characters or digits\a\n";
335 } elsif ($username{$name}) {
336 warn "Username ``$name'' already exists!\a\n"; return 0;
342 sub new_users_fullname
{
347 $fullname = &confirm_list
("Enter full name", 1, "", "");
348 last if $fullname eq &new_users_fullname_valid
($fullname);
350 $fullname = $name unless $fullname;
354 sub new_users_fullname_valid
{
355 local($fullname) = @_;
357 return $fullname if $fullname !~ /:/;
359 warn "``:'' is not allowed!\a\n";
363 # return shell (full path) for user
364 sub new_users_shell
{
367 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
371 # return free uid and gid
374 local($u_id, $g_id) = &next_id
($name);
375 local($u_id_tmp, $e);
378 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
379 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
381 if ($uid{$u_id_tmp}) {
382 warn "Uid ``$u_id_tmp'' in use!\a\n";
384 warn "Wrong uid.\a\n";
388 return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
390 $uid_start = $u_id_tmp;
391 return &next_id
($name);
396 local($gid, $name) = @_;
399 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
401 $groupmembers_bak{$gid} = $groupmembers{$gid};
402 $groupmembers{$gid} .= "," if $groupmembers{$gid};
403 $groupmembers{$gid} .= "$name";
410 sub new_users_grplogin
{
411 local($name, $defaultgroup, $new_users_ok) = @_;
412 local($group_login, $group);
415 $group = $defaultgroup if $defaultgroup ne $group_uniq;
419 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
421 # restore old groupmembers, user was not accept
422 foreach $e (keys %groupmembers_bak) {
423 $groupmembers{$e} = $groupmembers_bak{$e};
428 $group_login = &confirm_list
("Login group", 1, $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;
437 if (defined $gid{$group_login}) {
438 # convert numeric groupname (gid) to groupname
439 $group_login = $gid{$group_login};
442 warn "Group does not exist!\a\n";
445 if (defined($groupname{$group_login})) {
446 &add_group
($groupname{$group_login}, $name);
449 return ($group_login, $group_uniq) if $group_login eq $name;
450 return ($group_login, $group_login);
454 sub new_users_grplogin_batch
{
455 local($name, $defaultgroup) = @_;
456 local($group_login, $group);
458 $group_login = $name;
459 $group_login = $defaultgroup if $defaultgroup ne $group_uniq;
461 if (defined $gid{$group_login}) {
462 # convert numeric groupname (gid) to groupname
463 $group_login = $gid{$group_login};
466 if (defined($groupname{$group_login})) {
467 &add_group
($groupname{$group_login}, $name);
471 if defined($groupname{$group_login}) || $group_login eq $name;
472 warn "Group ``$group_login'' does not exist\a\n";
476 # return other groups (string)
477 sub new_users_groups
{
478 local($name, $other_groups) = @_;
480 "Login group is ``$group_login''. Invite $name into other groups:";
482 local($new_groups,$groups);
484 $other_groups = "no" unless $other_groups;
487 $groups = &confirm_list
($string, 1, $other_groups,
488 ("no", $other_groups, "guest"));
490 return "" if $groups eq "no";
492 ($flag, $new_groups) = &new_users_groups_valid
($groups);
495 $new_groups =~ s/\s*$//;
499 sub new_users_groups_valid
{
501 local($e, $new_groups);
504 foreach $e (split(/[,\s]+/, $groups)) {
505 # convert numbers to groupname
506 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
509 if (defined($groupname{$e})) {
510 if (&add_group
($groupname{$e}, $name)) {
511 $new_groups .= "$e ";
513 warn "$name is already member of group ``$e''\n";
516 warn "Group ``$e'' does not exist\a\n"; $flag++;
519 return ($flag, $new_groups);
531 Gid: $g_id ($group_login)
532 Groups: $group_login $new_groups
537 return &confirm_yn
("OK?", "yes");
540 # make password database
541 sub new_users_pwdmkdb
{
544 system("$pwd_mkdb $etc_passwd");
546 local($crash) = "$etc_passwd.crash$$";
548 warn "``$pwd_mkdb'' failed, try to restore ...\n";
550 open(R
, "> $crash") || die "Sorry, give up\n";
551 $j = join("\n", @passwd_backup);
556 system("$pwd_mkdb $crash");
557 die "Sorry, give up\n" if $?;
558 die "Successfully restore $etc_passwd. Exit.\n";
562 # update group database
563 sub new_users_group_update
{
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;
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}");
584 &append_file
($group, @a);
586 &append_file
($group, "$group_login:*:$g_id:$group_login");
591 sub new_users_passwd_update
{
592 # update passwd/group variables
593 push(@passwd_backup, $new_entry);
594 $username{$name} = $u_id;
596 $pwgid{$g_id} = $name;
599 # send message to new user
600 sub new_users_sendmessage
{
601 return 1 if $send_message eq "no";
604 &confirm_list
("Send message to ``$name'' and:",
605 1, "no", ("root", "second_mail_address", "no"));
607 $cc = "" if $cc eq "no";
609 foreach $e (@message_buffer) {
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);
624 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
625 if (&confirm_yn
("Send message", "yes"));
629 local($to, @message) = @_;
632 if (!open(M
, "| mail -s Welcome $to")) {
633 warn "Cannot send mail to: $to!\n";
636 foreach $e (@message) {
637 print M
eval "\"$e\"";
644 sub new_users_password
{
647 return "" if $defaultpasswd ne "yes";
652 $password = &confirm_list
("Enter password", 1, "", "");
653 last if $password ne "";
654 last if &confirm_yn
("Use an empty password?", "yes");
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;
669 # fullname: Full name
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;
681 $new_groups = "no" unless $groupname{$new_groups};
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}));
693 $new_groups = &new_users_groups
($name, $new_groups);
694 $password = &new_users_password
;
701 $cryptpwd = crypt($password, &salt
) if $password ne "";
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);
715 if (!&confirm_yn
("Add another user?", "yes")) {
716 print "Goodbye!\n" if $verbose;
719 print "\n" if !$verbose;
724 local($name, $groups, $fullname, $password) = @_;
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
;
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);
742 $cryptpwd = crypt($password, &salt
) if $password ne "";
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);
755 # ask for password usage
756 sub password_default
{
757 local($p) = $defaultpasswd;
759 $p = &confirm_yn
("Use passwords", $defaultpasswd);
760 $changes++ unless $p;
762 return "yes" if (($defaultpasswd eq "yes" && $p) ||
763 ($defaultpasswd eq "no" && !$p));
764 return "no"; # otherwise
769 die "You are not root!\n" if $< && !$test;
775 [-batch username [group[,group]...] [fullname] [password]]
782 [-message message_file]
785 [-s|-silent|-q|-quit]
789 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
790 message_file=$send_message uid_start=$uid_start
798 local($e, $last, @array);
800 foreach $e (sort @list) {
801 push(@array, $e) unless $e eq $last;
807 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
809 local($salt); # initialization
811 local(@itoa64) = ( 0 .. 9, a
.. z
, A
.. Z
); # 0 .. 63
813 warn "calculate salt\n" if $verbose > 1;
815 for ($i = 0; $i < 8; $i++) {
816 srand(time + $rand + $$);
817 $rand = rand(25*29*17 + $rand);
818 $salt .= $itoa64[$rand & $#itoa64];
820 warn "Salt is: $salt\n" if $verbose > 1;
829 (c) Copyright 1995 Wolfram Schneider <wosch@cs.tu-berlin.de>
836 print "Use option ``-silent'' if you don't want see " .
837 "all warnings & questions.\n\n";
839 print "Use option ``-verbose'' if you want see more warnings & " .
840 "questions \nor try to repair bugs.\n\n";
845 sub parse_arguments
{
848 while ($_ = $argv[0], /^-/) {
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;
863 elsif (/^--?(batch)$/) {
864 @batch = splice(@argv, 0, 4); $verbose = 0;
865 die "batch: to few arguments\n" if $#batch < 0;
868 elsif (/^--?(config_create)$/) { &create_conf
; }
869 elsif (/^--?(noconfig)$/) { $config_read = 0; }
872 #&usage if $#argv < 0;
884 $name = &stripdir
($name);
885 $name =~ s
|/+[^/]+$||;
886 $name = "/" unless $name; # dirname of / is /
890 # return 1 if $file is a readable file or link
892 local($file, $verb) = @_;
895 if (-f
$file || -l
$file) {
897 warn "$file unreadable\n" if $verbose;
899 warn "$file is not a plain file or link\n" if $verbose;
905 # create configuration files and exit
908 if ($send_message ne 'no') {
909 &message_create
($send_message);
911 &message_create
($send_message_bak);
917 # log for new user in /var/log/adduser
922 return 1 if $logfile eq "no";
924 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
927 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
929 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
932 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
935 # create HOME directory, copy dotfiles from $dotdir to $HOME
937 local($name, $group) = @_;
938 local($homedir) = "$home/$name";
941 warn "HOME Directory ``$homedir'' already exist\a\n";
945 if ($dotdir eq 'no') {
946 if (!mkdir("$homedir",0755)) {
947 warn "mkdir $homedir: $!\n"; return 0;
949 system 'chown', "$name:$group", $homedir;
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");
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");
967 chmod(0600, "$homedir/$file")
968 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
969 chmod(0700, "$homedir/$file")
970 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
976 # makes a directory hierarchy
979 $dir = &stripdir
($dir);
980 local($user_partition) = "/usr";
981 local($dirname) = &dirname
($dir);
984 -e
$dirname || &mkdirhier
($dirname);
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
992 local($basename) = &basename
($dir);
993 local($d) = "$user_partition/$basename";
997 warn "Oops, $d already exist\n" if $verbose;
999 print "Create $d\n" if $verbose;
1000 if (!mkdir("$d", 0755)) {
1001 warn "$d: $!\a\n"; return 0;
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;
1011 print "Create $dir\n" if $verbose;
1012 if (!mkdir("$dir", 0755)) {
1013 warn "Directory ``$dir'': $!\a\n"; return 0;
1023 $dir = &stripdir
($dir);
1025 foreach $d (split('/', $dir)) {
1029 print "Create $dir\n" if $verbose;
1030 if (!mkdir("$dir", 0755)) {
1031 warn "$dir: $!\n"; return 0;
1040 # F.i.: //usr///home// -> /usr/home
1044 $dir =~ s
|/+|/|g
; # delete double '/'
1045 $dir =~ s
|/$||; # delete '/' at end
1046 return $dir if $dir ne "";
1050 # Read one of the elements from @list. $confirm is default.
1051 # If !$allow accept only elements from @list.
1053 local($message, $allow, $confirm, @list) = @_;
1054 local($read, $c, $print);
1056 $print = "$message" if $message;
1057 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1059 $print .= join($", &uniq(@list)); #"
1060 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1062 print "\n" if (length($print) + length($confirm)) > 60;
1063 print "[$confirm]: ";
1065 chop($read = <STDIN>);
1068 return $confirm if $read eq "";
1069 return "$read" if $allow;
1071 foreach $c (@list) {
1072 return $read if $c eq $read;
1074 warn "$read: is not allowed!\a\n";
1075 return &confirm_list($message, $allow, $confirm, @list);
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
1083 local($message, $confirm) = @_;
1084 local($yes) = '^(yes
|YES
|y
|Y
)$';
1085 local($no) = '^(no|NO
|n
|N
)$';
1088 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1093 print "$message (y/n) [$confirm]: ";
1094 chop($read = <STDIN>);
1097 return 1 unless $read;
1099 if (($confirm eq "y" && $read =~ "$yes") ||
1100 ($confirm eq "n" && $read =~ "$no")) {
1104 if ($read !~ "$yes" && $read !~ "$no") {
1105 warn "Wrong value. Enter again!\a\n";
1106 return &confirm_yn($message, $confirm);
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;
1116 return &dotdir_default_valid($dir) unless $verbose;
1118 $dir = &confirm_list("Copy dotfiles from:", 1,
1119 $dir, ("no", $dotdir_bak, $dir));
1120 last if $dir eq &dotdir_default_valid($dir);
1122 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1124 $changes++ if $dir ne $dotdir;
1128 sub dotdir_default_valid {
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";
1137 # ask for messages to new users
1138 sub message_default {
1139 local($file) = $send_message;
1140 local(@d) = ($file, $send_message_bak, "no");
1143 $file = &confirm_list("Send message from file:", 1, $file, @d);
1144 last if $file eq "no";
1145 last if &filetest($file, 1);
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?",
1154 if ($file eq "no" || !&filetest($file, 0)) {
1155 warn "Do not send message\n" if $verbose;
1158 &message_read($file);
1161 $changes++ if $file ne $send_message && $verbose;
1165 # create message file
1166 sub message_create {
1169 rename($file, "$file.bak");
1170 if (!open(M, "> $file")) {
1171 warn "Messagefile ``$file'': $!\n"; return 0;
1175 # Message file for adduser(8)
1177 # defaultvariables: \$name, \$fullname, \$password
1178 # other variables: see /etc/adduser.conf after
1179 # line ``$do_not_delete''
1184 your account ``\$name'' was created. Your password is ``\$password''.
1185 Please expire your password. Have fun!
1187 See also chpass(1), finger(1), passwd(1)
1193 # read message file into buffer
1196 @message_buffer = '';
1198 if (!open(R
, "$file")) {
1199 warn "File ``$file'':$!\n"; return 0;
1202 push(@message_buffer, $_) unless /^\s*#/;
1207 # write @list to $file with file-locking
1209 local($file,@list) = @_;
1211 local($LOCK_EX) = 2;
1212 local($LOCK_NB) = 4;
1213 local($LOCK_UN) = 8;
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");
1222 print F
join("\n", @list) . "\n";
1224 print "Unlock $file.\n" if $verbose > 1;
1228 # return free uid+gid
1229 # uid == gid if possible
1233 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1234 # looking for next free uid
1235 while($uid{$uid_start}) {
1237 $uid_start = 1000 if $uid_start >= $uid_end;
1238 print "$uid_start\n" if $verbose > 1;
1241 local($gid_start) = $uid_start;
1242 # group for user (username==groupname) already exist
1243 if ($groupname{$group}) {
1244 $gid_start = $groupname{$group};
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}) {
1251 $gid_start = $uid_end if $gid_start < 100;
1254 return ($uid_start, $gid_start);
1260 local($user_flag) = 0;
1262 # don't read config file
1263 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1265 if(!open(C
, "$config")) {
1266 warn "$config: $!\n"; return 0;
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/) {
1277 # lines with '^##' are not saved
1278 push(@user_variable_list, $_)
1279 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1281 #warn "X @user_variable_list X\n";
1288 local($silent) = @_;
1291 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1295 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1298 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1302 rename($config, "$config.bak");
1303 open(C
, "> $config") || die "$config: $!\n";
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);
1314 # $config - automatic generated by adduser(8)
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''
1324 # use password for new users
1325 # defaultpasswd = yes | no
1326 defaultpasswd = $defaultpasswd
1328 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1331 # send this file to new user ("/etc/adduser.message" or "no")
1332 send_message = "$send_message"
1334 # config file for adduser ("/etc/adduser.conf")
1337 # logfile ("/var/log/adduser" or "no")
1338 logfile = "$logfile"
1340 # default HOME directory ("/home")
1343 # List of directories where shells located
1344 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1347 # common shell list, first element has higher priority
1348 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1349 shellpref = ($shpref)
1351 # defaultshell if not empty ("bash")
1352 defaultshell = "$defaultshell"
1354 # defaultgroup ('USER' for same as username or any other valid group
1355 defaultgroup = $defaultgroup
1357 # new users get this uid (1000)
1361 ## your own variables, see /etc/adduser.message
1372 $test = 0; # test mode, only for development
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
1380 if (!$check_only && $#batch < 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
1393 exit(!&batch
(@batch)) if $#batch >= 0; # batch mode
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
1405 # main loop for creating new users
1406 &new_users
; # add new users