]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/adduser.perl
3 # Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
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.
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
27 # $Id: adduser.perl,v 1.18 1996/09/14 23:22:23 wosch Exp $
32 $verbose = 1; # verbose = [0-2]
33 $defaultpasswd = "yes"; # use password for new users
34 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
35 $dotdir_bak = $dotdir;
36 $send_message = "no"; # send message to new user
37 $send_message_bak = '/etc/adduser.message';
38 $config = "/etc/adduser.conf"; # config file for adduser
39 $config_read = 1; # read config file
40 $logfile = "/var/log/adduser"; # logfile
41 $home = "/home"; # default HOME
42 $etc_shells = "/etc/shells";
43 $etc_passwd = "/etc/master.passwd";
44 $group = "/etc/group";
45 $pwd_mkdb = "pwd_mkdb -p"; # program for building passwd database
48 # List of directories where shells located
49 @path = ('/bin', '/usr/bin', '/usr/local/bin');
50 # common shells, first element has higher priority
51 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
53 $defaultshell = 'sh'; # defaultshell if not empty
55 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
57 $uid_start = 1000; # new users get this uid
58 $uid_end = 32000; # max. uid
62 $username = ''; # $username{username} = uid
63 $uid = ''; # $uid{uid} = username
64 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
66 $password = ''; # password for new users
69 $groupname =''; # $groupname{groupname} = gid
70 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
71 $gid = ''; # $gid{gid} = groupname; gid form group db
74 $shell = ''; # $shell{`basename sh`} = sh
76 # only for me (=Wolfram)
78 $home = "/home/w/tmp/adduser/home";
79 $etc_shells = "./shells";
80 $etc_passwd = "./master.passwd";
82 $pwd_mkdb = "pwd_mkdb -p -d .";
83 $config = "adduser.conf";
84 $send_message = "./adduser.message";
85 $logfile = "./log.adduser";
88 umask 022; # don't give login group write access
90 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
94 @user_variable_list = ''; # user variables in /etc/adduser.conf
95 $do_not_delete = '## DO NOT DELETE THIS LINE!';
98 # read shell database, see also: shells(5)
103 print "Check $etc_shells\n" if $verbose;
104 open(S
, $etc_shells) || die "$etc_shells:$!\n";
108 s/^\s*//; s/\s+.*//; # chop
111 $shell{&basename
($sh)} = $sh;
113 warn "Shell: $sh not executable!\n";
119 # Allow /nonexistent and /bin/date as a valid shell for system utils
120 push(@list, "/nonexistent");
121 push(@shellpref, "no");
122 $shell{"no"} = "/nonexistent";
124 push(@list, "/bin/date");
125 push(@shellpref, "date");
126 $shell{"date"} = "/bin/date";
131 # add new shells if possible
133 local($sh,$dir,@list);
135 return 1 unless $verbose;
137 foreach $sh (@shellpref) {
140 # shell $sh is not defined as login shell
141 foreach $dir (@path) {
144 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
145 push(@list, "$dir/$sh");
146 push(@shellpref, "$sh");
147 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
154 &append_file
($etc_shells, @list) if $#list >= 0;
157 # choose your favourite shell and return the shell
159 local($e,$i,$new_shell);
162 $sh = &shell_default_valid
($defaultshell);
163 return $sh unless $verbose;
165 $new_shell = &confirm_list
("Enter your default shell:", 0,
166 $sh, sort(keys %shell));
167 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
168 $changes++ if $new_shell ne $sh;
172 sub shell_default_valid
{
176 return $sh if $shell{$sh};
178 foreach $e (@shellpref) {
180 last if defined($shell{$s});
183 warn "Shell ``$sh'' is undefined, use ``$s''\n";
187 # return default home partition (f.e. "/home")
188 # create base directory if nesseccary
191 $home = &stripdir
($home);
194 return $h if !$verbose && $h eq &home_partition_valid
($h);
197 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
199 last if $h eq &home_partition_valid
($h);
202 $changes++ if $h ne $home;
206 sub home_partition_valid
{
211 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
215 warn "Please use absolute path for home: ``$h''.\a\n";
220 warn "$h exists, but is not a directory or symlink!\n"
221 unless -d
$h || -l
$h;
222 warn "$h is not writable!\n"
226 # create home partition
227 return $h if &mkdir_home
($h);
232 # check for valid passwddb
234 system("$pwd_mkdb -c $etc_passwd");
235 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
240 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
242 print "Check $etc_passwd\n" if $verbose;
243 open(P
, "$etc_passwd") || die "$passwd: $!\n";
247 push(@passwd_backup, $_);
248 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
250 print "$p_username already exists with uid: $username{$p_username}!\n"
251 if $username{$p_username} && $verbose;
252 $username{$p_username} = $p_uid;
253 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
254 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
255 print "User $p_username: illegal shell: ``$sh''\n"
256 if ($verbose && $sh &&
257 !$shell{&basename
($sh)} &&
258 $p_username !~ /^(bin|uucp|falcon|nobody)$/ &&
259 $sh !~ /\/(pppd
|sliplogin
)$/);
260 $uid{$p_uid} = $p_username;
261 $pwgid{$p_gid} = $p_username;
268 local($g_groupname,$pw,$g_gid, $memb);
270 print "Check $group\n" if $verbose;
271 open(G
, "$group") || die "$group: $!\n";
274 push(@group_backup, $_);
275 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
277 $groupmembers{$g_gid} = $memb;
278 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
279 if $groupname{$g_groupname} && $verbose;
280 $groupname{$g_groupname} = $g_gid;
281 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
282 if $gid{$g_gid} && $verbose;
283 $gid{$g_gid} = $g_groupname;
288 # check gids /etc/passwd <-> /etc/group
290 local($c_gid, $c_username, @list);
292 foreach $c_gid (keys %pwgid) {
294 $c_username = $pwgid{$c_gid};
295 warn "User ``$c_username'' has gid $c_gid but a group with this " .
296 "gid does not exist.\n" if $verbose;
302 # main loop for creating new users
310 $name = &confirm_list
("Enter username", 1, "a-z0-9", "");
311 if (length($name) > 8) {
312 warn "Username is longer than 8 chars\a\n";
315 last if (&new_users_name_valid
($name) eq $name);
320 sub new_users_name_valid
{
323 if ($name !~ /^[a-z0-9]+$/) {
324 warn "Wrong username. " .
325 "Please use only lowercase characters or digits\a\n";
327 } elsif ($username{$name}) {
328 warn "Username ``$name'' already exists!\a\n"; return 0;
334 sub new_users_fullname
{
339 $fullname = &confirm_list
("Enter full name", 1, "", "");
340 last if $fullname eq &new_users_fullname_valid
($fullname);
342 $fullname = $name unless $fullname;
346 sub new_users_fullname_valid
{
347 local($fullname) = @_;
349 return $fullname if $fullname !~ /:/;
351 warn "``:'' is not allowed!\a\n";
355 # return shell (full path) for user
356 sub new_users_shell
{
359 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
363 # return free uid and gid
366 local($u_id, $g_id) = &next_id
($name);
367 local($u_id_tmp, $e);
370 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
371 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
373 if ($uid{$u_id_tmp}) {
374 warn "Uid ``$u_id_tmp'' in use!\a\n";
376 warn "Wrong uid.\a\n";
380 return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
382 $uid_start = $u_id_tmp;
383 return &next_id
($name);
388 local($gid, $name) = @_;
391 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
393 $groupmembers_bak{$gid} = $groupmembers{$gid};
394 $groupmembers{$gid} .= "," if $groupmembers{$gid};
395 $groupmembers{$gid} .= "$name";
397 local(@l) = split(',', $groupmembers{$gid});
398 # group(5): A group cannot have more than 200 members.
399 # The maximum line length of /etc/group is 1024 characters.
400 # Longer lines will be skiped.
402 length($groupmembers{$gid}) > 1024 - 50) { # 50 is for group name
403 warn "WARNING, maybe group line ``$gid{$gid}'' is to long or to\n" .
404 "much users in group, see group(5)\a\n";
411 sub new_users_grplogin
{
412 local($name, $defaultgroup, $new_users_ok) = @_;
413 local($group_login, $group);
416 $group = $defaultgroup if $defaultgroup ne $group_uniq;
420 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
422 # restore old groupmembers, user was not accept
423 foreach $e (keys %groupmembers_bak) {
424 $groupmembers{$e} = $groupmembers_bak{$e};
429 $group_login = &confirm_list
("Login group", 1, $group,
431 last if $group_login eq $group;
432 last if $group_login eq $name;
433 last if defined $groupname{$group_login};
434 if ($group_login eq $group_uniq) {
435 $group_login = $name; last;
438 if (defined $gid{$group_login}) {
439 # convert numeric groupname (gid) to groupname
440 $group_login = $gid{$group_login};
443 warn "Group does not exist!\a\n";
446 #if (defined($groupname{$group_login})) {
447 # &add_group($groupname{$group_login}, $name);
450 return ($group_login, $group_uniq) if $group_login eq $name;
451 return ($group_login, $group_login);
455 sub new_users_grplogin_batch
{
456 local($name, $defaultgroup) = @_;
457 local($group_login, $group);
459 $group_login = $name;
460 $group_login = $defaultgroup if $defaultgroup ne $group_uniq;
462 if (defined $gid{$group_login}) {
463 # convert numeric groupname (gid) to groupname
464 $group_login = $gid{$group_login};
467 # if (defined($groupname{$group_login})) {
468 # &add_group($groupname{$group_login}, $name);
472 if defined($groupname{$group_login}) || $group_login eq $name;
473 warn "Group ``$group_login'' does not exist\a\n";
477 # return other groups (string)
478 sub new_users_groups
{
479 local($name, $other_groups) = @_;
481 "Login group is ``$group_login''. Invite $name into other groups:";
483 local($new_groups,$groups);
485 $other_groups = "no" unless $other_groups;
488 $groups = &confirm_list
($string, 1, $other_groups,
489 ("no", $other_groups, "guest"));
491 return "" if $groups eq "no";
493 ($flag, $new_groups) = &new_users_groups_valid
($groups);
496 $new_groups =~ s/\s*$//;
500 sub new_users_groups_valid
{
502 local($e, $new_groups);
505 foreach $e (split(/[,\s]+/, $groups)) {
506 # convert numbers to groupname
507 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
510 if (defined($groupname{$e})) {
511 if ($e eq $group_login) {
512 # do not add user to a group if this group
513 # is also the login group.
514 } elsif (&add_group
($groupname{$e}, $name)) {
515 $new_groups .= "$e ";
517 warn "$name is already member of group ``$e''\n";
520 warn "Group ``$e'' does not exist\a\n"; $flag++;
523 return ($flag, $new_groups);
535 Gid: $g_id ($group_login)
536 Groups: $group_login $new_groups
541 return &confirm_yn
("OK?", "yes");
544 # make password database
545 sub new_users_pwdmkdb
{
548 system("$pwd_mkdb $etc_passwd");
551 warn "``$pwd_mkdb'' failed\n";
556 # update group database
557 sub new_users_group_update
{
561 if (!defined($groupname{$group_login}) &&
562 !defined($gid{$groupname{$group_login}})) {
563 push(@group_backup, "$group_login:*:$g_id:");
564 $groupname{$group_login} = $g_id;
565 $gid{$g_id} = $group_login;
566 # $groupmembers{$g_id} = $group_login;
569 if ($new_groups || defined($groupname{$group_login}) ||
570 defined($gid{$groupname{$group_login}}) &&
571 $gid{$groupname{$group_login}} ne "+") {
572 # new user is member of some groups
573 # new login group is already in name space
574 rename($group, "$group.bak");
575 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
576 foreach $e (sort {$a <=> $b} (keys %gid)) {
577 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
579 &append_file
($group, @a);
581 &append_file
($group, "$group_login:*:$g_id:");
586 sub new_users_passwd_update
{
587 # update passwd/group variables
588 push(@passwd_backup, $new_entry);
589 $username{$name} = $u_id;
591 $pwgid{$g_id} = $name;
594 # send message to new user
595 sub new_users_sendmessage
{
596 return 1 if $send_message eq "no";
599 &confirm_list
("Send message to ``$name'' and:",
600 1, "no", ("root", "second_mail_address", "no"));
602 $cc = "" if $cc eq "no";
604 foreach $e (@message_buffer) {
609 local(@message_buffer_append) = ();
610 if (!&confirm_yn
("Add anything to default message", "no")) {
611 print "Use ``.'' or ^D alone on a line to finish your message.\n";
612 push(@message_buffer_append, "\n");
613 while($read = <STDIN
>) {
614 last if $read eq "\.\n";
615 push(@message_buffer_append, $read);
619 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
620 if (&confirm_yn
("Send message", "yes"));
624 local($to, @message) = @_;
627 if (!open(M
, "| mail -s Welcome $to")) {
628 warn "Cannot send mail to: $to!\n";
631 foreach $e (@message) {
632 print M
eval "\"$e\"";
639 sub new_users_password
{
642 return "" if $defaultpasswd ne "yes";
647 system("stty -echo");
648 $password = &confirm_list
("Enter password", 1, "", "");
651 if ($password ne "") {
652 system("stty -echo");
653 $newpass = &confirm_list
("Enter password again", 1, "", "");
656 last if $password eq $newpass;
657 print "They didn't match, please try again\n";
659 elsif (&confirm_yn
("Use an empty password?", "yes")) {
670 print "\n" if $verbose;
671 print "Ok, let's go.\n" .
672 "Don't worry about mistakes. I will give you the chance later to " .
673 "correct any input.\n" if $verbose;
676 # fullname: Full name
680 # group_login: groupname of g_id
681 # new_groups: some other groups
682 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $new_groups);
683 local($groupmembers_bak, $cryptpwd);
684 local($new_users_ok) = 1;
688 $new_groups = "no" unless $groupname{$new_groups};
691 $name = &new_users_name
;
692 $fullname = &new_users_fullname
($name);
693 $sh = &new_users_shell
;
694 ($u_id, $g_id) = &new_users_id
($name);
695 ($group_login, $defaultgroup) =
696 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
697 # do not use uniq username and login group
698 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
700 $new_groups = &new_users_groups
($name, $new_groups);
701 $password = &new_users_password
;
708 $cryptpwd = crypt($password, &salt
) if $password ne "";
710 $new_entry = "$name\:" . "$cryptpwd" .
711 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
712 &append_file
($etc_passwd, "$new_entry");
713 &new_users_pwdmkdb
("$new_entry");
714 &new_users_group_update
;
715 &new_users_passwd_update
; print "Added user ``$name''\n";
716 &new_users_sendmessage
;
717 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
718 &home_create
($name, $group_login);
722 if (!&confirm_yn
("Add another user?", "yes")) {
723 print "Goodbye!\n" if $verbose;
726 print "\n" if !$verbose;
731 local($name, $groups, $fullname, $password) = @_;
734 $defaultshell = &shell_default_valid
($defaultshell);
735 return 0 unless $home = &home_partition_valid
($home);
736 return 0 if $dotdir ne &dotdir_default_valid
($dotdir);
737 $send_message = &message_default
;
739 return 0 if $name ne &new_users_name_valid
($name);
740 $sh = $shell{$defaultshell};
741 ($u_id, $g_id) = &next_id
($name);
742 $group_login = &new_users_grplogin_batch
($name, $defaultgroup);
743 return 0 unless $group_login;
744 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
745 ($flag, $new_groups) = &new_users_groups_valid
($groups);
749 $cryptpwd = crypt($password, &salt
) if $password ne "";
751 $new_entry = "$name\:" . "$cryptpwd" .
752 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
753 &append_file
($etc_passwd, "$new_entry");
754 &new_users_pwdmkdb
("$new_entry");
755 &new_users_group_update
;
756 &new_users_passwd_update
; print "Added user ``$name''\n";
757 &sendmessage
($name, @message_buffer) if $send_message ne "no";
758 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
759 &home_create
($name, $group_login);
762 # ask for password usage
763 sub password_default
{
764 local($p) = $defaultpasswd;
766 $p = &confirm_yn
("Use passwords", $defaultpasswd);
767 $changes++ unless $p;
769 return "yes" if (($defaultpasswd eq "yes" && $p) ||
770 ($defaultpasswd eq "no" && !$p));
771 return "no"; # otherwise
776 die "You are not root!\n" if $< && !$test;
782 [-batch username [group[,group]...] [fullname] [password]]
789 [-message message_file]
792 [-s|-silent|-q|-quit]
796 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
797 message_file=$send_message uid_start=$uid_start
805 local($e, $last, @array);
807 foreach $e (sort @list) {
808 push(@array, $e) unless $e eq $last;
814 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
816 local($salt); # initialization
818 local(@itoa64) = ( 0 .. 9, a
.. z
, A
.. Z
); # 0 .. 63
820 warn "calculate salt\n" if $verbose > 1;
822 for ($i = 0; $i < 8; $i++) {
823 srand(time + $rand + $$);
824 $rand = rand(25*29*17 + $rand);
825 $salt .= $itoa64[$rand & $#itoa64];
827 warn "Salt is: $salt\n" if $verbose > 1;
841 print "Use option ``-silent'' if you don't want see " .
842 "all warnings & questions.\n\n";
844 print "Use option ``-verbose'' if you want see more warnings & " .
845 "questions \nor try to repair bugs.\n\n";
850 sub parse_arguments
{
853 while ($_ = $argv[0], /^-/) {
856 if (/^--?(v|verbose)$/) { $verbose = 1 }
857 elsif (/^--?(s|silent|q|quit)$/) { $verbose = 0 }
858 elsif (/^--?(debug)$/) { $verbose = 2 }
859 elsif (/^--?(h|help|\?)$/) { &usage
}
860 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
861 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
862 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
863 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
864 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
865 elsif (/^--?(check_only)$/) { $check_only = 1 }
866 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
868 elsif (/^--?(batch)$/) {
869 @batch = splice(@argv, 0, 4); $verbose = 0;
870 die "batch: too few arguments\n" if $#batch < 0;
873 elsif (/^--?(config_create)$/) { &create_conf
; }
874 elsif (/^--?(noconfig)$/) { $config_read = 0; }
877 #&usage if $#argv < 0;
889 $name = &stripdir
($name);
890 $name =~ s
|/+[^/]+$||;
891 $name = "/" unless $name; # dirname of / is /
895 # return 1 if $file is a readable file or link
897 local($file, $verb) = @_;
900 if (-f
$file || -l
$file) {
902 warn "$file unreadable\n" if $verbose;
904 warn "$file is not a plain file or link\n" if $verbose;
910 # create configuration files and exit
913 if ($send_message ne 'no') {
914 &message_create
($send_message);
916 &message_create
($send_message_bak);
922 # log for new user in /var/log/adduser
927 return 1 if $logfile eq "no";
929 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
932 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
934 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
937 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
940 # create HOME directory, copy dotfiles from $dotdir to $HOME
942 local($name, $group) = @_;
943 local($homedir) = "$home/$name";
946 warn "HOME Directory ``$homedir'' already exist\a\n";
950 if ($dotdir eq 'no') {
951 if (!mkdir("$homedir",0755)) {
952 warn "mkdir $homedir: $!\n"; return 0;
954 system 'chown', "$name:$group", $homedir;
958 # copy files from $dotdir to $homedir
959 # rename 'dot.foo' files to '.foo'
960 print "Copy files from $dotdir to $homedir\n" if $verbose;
961 system("cp -r $dotdir $homedir");
962 system("chmod -R u+wrX,go-w $homedir");
963 system("chown -R $name:$group $homedir");
966 opendir(D
, $homedir);
967 foreach $file (readdir(D
)) {
968 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
969 $file =~ s/^dot\././;
970 rename("$homedir/dot$file", "$homedir/$file");
972 chmod(0600, "$homedir/$file")
973 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
974 chmod(0700, "$homedir/$file")
975 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
981 # makes a directory hierarchy
984 $dir = &stripdir
($dir);
985 local($user_partition) = "/usr";
986 local($dirname) = &dirname
($dir);
989 -e
$dirname || &mkdirhier
($dirname);
991 if (((stat($dirname))[0]) == ((stat("/"))[0])){
992 # home partition is on root partition
993 # create home partition on $user_partition and make
994 # a symlink from $dir to $user_partition/`basename $dir`
995 # For instance: /home -> /usr/home
997 local($basename) = &basename
($dir);
998 local($d) = "$user_partition/$basename";
1002 warn "Oops, $d already exist\n" if $verbose;
1004 print "Create $d\n" if $verbose;
1005 if (!mkdir("$d", 0755)) {
1006 warn "$d: $!\a\n"; return 0;
1010 unlink($dir); # symlink to nonexist file
1011 print "Create symlink: $dir -> $d\n" if $verbose;
1012 if (!symlink("$d", $dir)) {
1013 warn "Symlink $d: $!\a\n"; return 0;
1016 print "Create $dir\n" if $verbose;
1017 if (!mkdir("$dir", 0755)) {
1018 warn "Directory ``$dir'': $!\a\n"; return 0;
1028 $dir = &stripdir
($dir);
1030 foreach $d (split('/', $dir)) {
1034 print "Create $dir\n" if $verbose;
1035 if (!mkdir("$dir", 0755)) {
1036 warn "$dir: $!\n"; return 0;
1045 # F.i.: //usr///home// -> /usr/home
1049 $dir =~ s
|/+|/|g
; # delete double '/'
1050 $dir =~ s
|/$||; # delete '/' at end
1051 return $dir if $dir ne "";
1055 # Read one of the elements from @list. $confirm is default.
1056 # If !$allow accept only elements from @list.
1058 local($message, $allow, $confirm, @list) = @_;
1059 local($read, $c, $print);
1061 $print = "$message" if $message;
1062 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1064 $print .= join($", &uniq(@list)); #"
1065 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1067 print "\n" if (length($print) + length($confirm)) > 60;
1068 print "[$confirm]: ";
1070 chop($read = <STDIN>);
1073 return $confirm if $read eq "";
1074 return "$read" if $allow;
1076 foreach $c (@list) {
1077 return $read if $c eq $read;
1079 warn "$read: is not allowed!\a\n";
1080 return &confirm_list($message, $allow, $confirm, @list);
1083 # YES or NO question
1084 # return 1 if &confirm("message", "yes") and answer is yes
1085 # or if &confirm("message", "no") an answer is no
1088 local($message, $confirm) = @_;
1089 local($yes) = '^(yes
|YES
|y
|Y
)$';
1090 local($no) = '^(no|NO
|n
|N
)$';
1093 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1098 print "$message (y/n) [$confirm]: ";
1099 chop($read = <STDIN>);
1102 return 1 unless $read;
1104 if (($confirm eq "y" && $read =~ "$yes") ||
1105 ($confirm eq "n" && $read =~ "$no")) {
1109 if ($read !~ "$yes" && $read !~ "$no") {
1110 warn "Wrong value. Enter again!\a\n";
1111 return &confirm_yn($message, $confirm);
1116 # test if $dotdir exist
1117 # return "no" if $dotdir not exist or dotfiles should not copied
1118 sub dotdir_default {
1119 local($dir) = $dotdir;
1121 return &dotdir_default_valid($dir) unless $verbose;
1123 $dir = &confirm_list("Copy dotfiles from:", 1,
1124 $dir, ("no", $dotdir_bak, $dir));
1125 last if $dir eq &dotdir_default_valid($dir);
1127 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1129 $changes++ if $dir ne $dotdir;
1133 sub dotdir_default_valid {
1136 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1137 return $dir if $dir eq "no";
1138 warn "Dotdir ``$dir'' is not a directory\a\n";
1142 # ask for messages to new users
1143 sub message_default {
1144 local($file) = $send_message;
1145 local(@d) = ($file, $send_message_bak, "no");
1148 $file = &confirm_list("Send message from file:", 1, $file, @d);
1149 last if $file eq "no";
1150 last if &filetest($file, 1);
1152 # maybe create message file
1153 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1154 last if &filetest($file, 0);
1155 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1159 if ($file eq "no" || !&filetest($file, 0)) {
1160 warn "Do not send message\n" if $verbose;
1163 &message_read($file);
1166 $changes++ if $file ne $send_message && $verbose;
1170 # create message file
1171 sub message_create {
1174 rename($file, "$file.bak");
1175 if (!open(M, "> $file")) {
1176 warn "Messagefile ``$file'': $!\n"; return 0;
1180 # Message file for adduser(8)
1182 # default variables: \$name, \$fullname, \$password
1183 # other variables: see /etc/adduser.conf after
1184 # line ``$do_not_delete''
1189 your account ``\$name'' was created. Your password is ``\$password''.
1190 Please expire your password. Have fun!
1192 See also chpass(1), finger(1), passwd(1)
1198 # read message file into buffer
1201 @message_buffer = '';
1203 if (!open(R
, "$file")) {
1204 warn "File ``$file'':$!\n"; return 0;
1207 push(@message_buffer, $_) unless /^\s*#/;
1212 # write @list to $file with file-locking
1214 local($file,@list) = @_;
1216 local($LOCK_EX) = 2;
1217 local($LOCK_NB) = 4;
1218 local($LOCK_UN) = 8;
1220 open(F
, ">> $file") || die "$file: $!\n";
1221 print "Lock $file.\n" if $verbose > 1;
1222 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1223 warn "Cannot lock file: $file\a\n";
1224 die "Sorry, give up\n"
1225 unless &confirm_yn
("Try again?", "yes");
1227 print F
join("\n", @list) . "\n";
1229 print "Unlock $file.\n" if $verbose > 1;
1233 # return free uid+gid
1234 # uid == gid if possible
1238 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1239 # looking for next free uid
1240 while($uid{$uid_start}) {
1242 $uid_start = 1000 if $uid_start >= $uid_end;
1243 print "$uid_start\n" if $verbose > 1;
1246 local($gid_start) = $uid_start;
1247 # group for user (username==groupname) already exist
1248 if ($groupname{$group}) {
1249 $gid_start = $groupname{$group};
1251 # gid is in use, looking for another gid.
1252 # Note: uid an gid are not equal
1253 elsif ($gid{$uid_start}) {
1254 while($gid{$gid_start} || $uid{$gid_start}) {
1256 $gid_start = $uid_end if $gid_start < 100;
1259 return ($uid_start, $gid_start);
1265 local($user_flag) = 0;
1267 # don't read config file
1268 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1270 if(!open(C
, "$config")) {
1271 warn "$config: $!\n"; return 0;
1275 # user defined variables
1276 /^$do_not_delete/ && $user_flag++;
1277 # found @array or $variable
1278 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1282 # lines with '^##' are not saved
1283 push(@user_variable_list, $_)
1284 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1286 #warn "X @user_variable_list X\n";
1293 local($silent) = @_;
1296 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1300 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1303 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1307 rename($config, "$config.bak");
1308 open(C
, "> $config") || die "$config: $!\n";
1310 # prepare some variables
1311 $send_message = "no" unless $send_message;
1312 $defaultpasswd = "no" unless $defaultpasswd;
1313 local($shpref) = "'" . join("', '", @shellpref) . "'";
1314 local($shpath) = "'" . join("', '", @path) . "'";
1315 local($user_var) = join('', @user_variable_list);
1319 # $config - automatic generated by adduser(8)
1321 # Note: adduser read *and* write this file.
1322 # You may change values, but don't add new things befor the
1323 # line ``$do_not_delete''
1329 # use password for new users
1330 # defaultpasswd = yes | no
1331 defaultpasswd = $defaultpasswd
1333 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1336 # send this file to new user ("/etc/adduser.message" or "no")
1337 send_message = "$send_message"
1339 # config file for adduser ("/etc/adduser.conf")
1342 # logfile ("/var/log/adduser" or "no")
1343 logfile = "$logfile"
1345 # default HOME directory ("/home")
1348 # List of directories where shells located
1349 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1352 # common shell list, first element has higher priority
1353 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1354 shellpref = ($shpref)
1356 # defaultshell if not empty ("bash")
1357 defaultshell = "$defaultshell"
1359 # defaultgroup ('USER' for same as username or any other valid group
1360 defaultgroup = $defaultgroup
1362 # new users get this uid (1000)
1366 ## your own variables, see /etc/adduser.message
1377 $test = 0; # test mode, only for development
1380 &check_root
; # you must be root to run this script!
1381 &variables
; # initialize variables
1382 &config_read
(@ARGV); # read variables form config-file
1383 &parse_arguments
(@ARGV); # parse arguments
1385 if (!$check_only && $#batch < 0) {
1391 &passwd_check
; # check for valid passwdb
1392 &shells_read
; # read /etc/shells
1393 &passwd_read
; # read /etc/master.passwd
1394 &group_read
; # read /etc/group
1395 &group_check
; # check for incon*
1396 exit 0 if $check_only; # only check consistence and exit
1398 exit(!&batch
(@batch)) if $#batch >= 0; # batch mode
1402 &shells_add
; # maybe add some new shells
1403 $defaultshell = &shell_default
; # enter default shell
1404 $home = &home_partition
($home); # find HOME partition
1405 $dotdir = &dotdir_default
; # check $dotdir
1406 $send_message = &message_default
; # send message to new user
1407 $defaultpasswd = &password_default
; # maybe use password
1408 &config_write
(!$verbose); # write variables in file
1410 # main loop for creating new users
1411 &new_users
; # add new users