]>
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
32 $verbose = 1; # verbose = [0-2]
33 $defaultusepassword = "yes"; # use password authentication for new users
34 $defaultenableaccount = "yes"; # enable the account by default
35 $defaultemptypassword = "no"; # don't create an empty password
36 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
37 $dotdir_bak = $dotdir;
38 $send_message = "/etc/adduser.message"; # send message to new user
39 $send_message_bak = '/etc/adduser.message';
40 $config = "/etc/adduser.conf"; # config file for adduser
41 $config_read = 1; # read config file
42 $logfile = "/var/log/adduser"; # logfile
43 $home = "/home"; # default HOME
44 $etc_shells = "/etc/shells";
45 $etc_passwd = "/etc/master.passwd";
46 $group = "/etc/group";
47 $pwd_mkdb = "pwd_mkdb -p"; # program for building passwd database
50 # List of directories where shells located
51 @path = ('/bin', '/usr/bin', '/usr/local/bin');
52 # common shells, first element has higher priority
53 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
55 $defaultshell = 'sh'; # defaultshell if not empty
57 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
60 $uid_start = 1000; # new users get this uid
61 $uid_end = 32000; # max. uid
65 $username = ''; # $username{username} = uid
66 $uid = ''; # $uid{uid} = username
67 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
69 $password = ''; # password for new users
70 $usepassword = ''; # use password-based auth
71 $useemptypassword = ''; # use an empty password
72 $enableaccount = ''; # enable or disable account password at creation
75 $groupname =''; # $groupname{groupname} = gid
76 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
77 $gid = ''; # $gid{gid} = groupname; gid form group db
78 @group_comments; # Comments in the group file
81 $shell = ''; # $shell{`basename sh`} = sh
83 umask 022; # don't give login group write access
85 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
89 @user_variable_list = ''; # user variables in /etc/adduser.conf
90 $do_not_delete = '## DO NOT DELETE THIS LINE!';
93 # read shell database, see also: shells(5)
98 print "Check $etc_shells\n" if $verbose;
99 open(S
, $etc_shells) || die "$etc_shells:$!\n";
103 s/^\s*//; s/\s+.*//; # chop
106 $shell{&basename
($sh)} = $sh;
108 warn "Shell: $sh not executable!\n";
114 # Allow /nonexistent and /bin/date as a valid shell for system utils
115 push(@list, "/nonexistent");
116 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
117 $shell{"no"} = "/nonexistent";
119 push(@list, "/bin/date");
120 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
121 $shell{"date"} = "/bin/date";
126 # add new shells if possible
128 local($sh,$dir,@list);
130 return 1 unless $verbose;
132 foreach $sh (@shellpref) {
135 # shell $sh is not defined as login shell
136 foreach $dir (@path) {
139 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
140 push(@list, "$dir/$sh");
141 push(@shellpref, "$sh");
142 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
149 &append_file
($etc_shells, @list) if $#list >= 0;
152 # choose your favourite shell and return the shell
154 local($e,$i,$new_shell);
157 $sh = &shell_default_valid
($defaultshell);
158 return $sh unless $verbose;
160 $new_shell = &confirm_list
("Enter your default shell:", 0,
161 $sh, sort(keys %shell));
162 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
163 $changes++ if $new_shell ne $sh;
167 sub shell_default_valid
{
171 return $sh if $shell{$sh};
173 foreach $e (@shellpref) {
175 last if defined($shell{$s});
178 warn "Shell ``$sh'' is undefined, use ``$s''\n";
182 # return default home partition (e.g. "/home")
183 # create base directory if nesseccary
186 $home = &stripdir
($home);
189 return $h if !$verbose && $h eq &home_partition_valid
($h);
192 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
194 last if $h eq &home_partition_valid
($h);
197 $changes++ if $h ne $home;
201 sub home_partition_valid
{
206 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
210 warn "Please use absolute path for home: ``$h''.\a\n";
215 warn "$h exists, but is not a directory or symlink!\n"
216 unless -d
$h || -l
$h;
217 warn "$h is not writable!\n"
221 # create home partition
222 return $h if &mkdir_home
($h);
227 # check for valid passwddb
229 system("$pwd_mkdb -C $etc_passwd");
230 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
235 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
237 print "Check $etc_passwd\n" if $verbose;
238 open(P
, "$etc_passwd") || die "$passwd: $!\n";
242 push(@passwd_backup, $_);
247 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
249 print "$p_username already exists with uid: $username{$p_username}!\n"
250 if $username{$p_username} && $verbose;
251 $username{$p_username} = $p_uid;
252 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
253 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
254 print "User $p_username: illegal shell: ``$sh''\n"
255 if ($verbose && $sh &&
256 !$shell{&basename
($sh)} &&
257 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
258 $sh !~ /\/(pppd
|sliplogin
|nologin
|nonexistent
)$/);
259 $uid{$p_uid} = $p_username;
260 $pwgid{$p_gid} = $p_username;
267 local($g_groupname,$pw,$g_gid, $memb);
269 print "Check $group\n" if $verbose;
270 open(G
, "$group") || die "$group: $!\n";
273 push(@group_backup, $_);
276 # Save comments to restore later
278 push(@group_comments, $_);
282 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
284 $groupmembers{$g_gid} = $memb;
285 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
286 if $groupname{$g_groupname} && $verbose;
287 $groupname{$g_groupname} = $g_gid;
288 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
289 if $gid{$g_gid} && $verbose;
290 $gid{$g_gid} = $g_groupname;
295 # check gids /etc/passwd <-> /etc/group
297 local($c_gid, $c_username, @list);
299 foreach $c_gid (keys %pwgid) {
301 $c_username = $pwgid{$c_gid};
302 warn "User ``$c_username'' has gid $c_gid but a group with this " .
303 "gid does not exist.\n" if $verbose;
309 # main loop for creating new users
317 $name = &confirm_list
("Enter username", 1, "a-z0-9_-", "");
318 if (length($name) > 16) {
319 warn "Username is longer than 16 chars\a\n";
322 last if (&new_users_name_valid
($name) eq $name);
327 sub new_users_name_valid
{
330 if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/ || $name eq "a-z0-9_-") {
331 warn "Wrong username. " .
332 "Please use only lowercase characters or digits\a\n";
334 } elsif ($username{$name}) {
335 warn "Username ``$name'' already exists!\a\n"; return 0;
341 sub new_users_fullname
{
346 $fullname = &confirm_list
("Enter full name", 1, "", "");
347 last if $fullname eq &new_users_fullname_valid
($fullname);
349 $fullname = $name unless $fullname;
353 sub new_users_fullname_valid
{
354 local($fullname) = @_;
356 return $fullname if $fullname !~ /:/;
358 warn "``:'' is not allowed!\a\n";
362 # return shell (full path) for user
363 sub new_users_shell
{
366 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
370 # return home (full path) for user
371 # Note that the home path defaults to $home/$name for batch
377 $userhome = &confirm_list
("Enter home directory (full path)", 1, "$home/$name", "");
378 last if $userhome =~ /^\//;
379 warn qq{Home directory "$userhome" is not a full path\a\n};
384 # return free uid and gid
387 local($u_id, $g_id) = &next_id
($name);
388 local($u_id_tmp, $e);
391 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
392 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
394 if ($uid{$u_id_tmp}) {
395 warn "Uid ``$u_id_tmp'' in use!\a\n";
396 $uid_start = $u_id_tmp;
397 ($u_id, $g_id) = &next_id
($name);
400 warn "Wrong uid.\a\n";
404 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
406 $uid_start = $u_id_tmp;
407 return &next_id
($name);
410 # return login class for user
411 sub new_users_class
{
415 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
416 $class = "" if $class eq "default";
422 local($gid, $name) = @_;
425 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
427 $groupmembers_bak{$gid} = $groupmembers{$gid};
428 $groupmembers{$gid} .= "," if $groupmembers{$gid};
429 $groupmembers{$gid} .= "$name";
436 sub new_users_grplogin
{
437 local($name, $defaultgroup, $new_users_ok) = @_;
438 local($group_login, $group);
441 $group = $defaultgroup if $defaultgroup ne $group_uniq;
445 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
447 # restore old groupmembers, user was not accept
448 foreach $e (keys %groupmembers_bak) {
449 $groupmembers{$e} = $groupmembers_bak{$e};
454 $group_login = &confirm_list
("Login group", 1, $group,
456 last if $group_login eq $group;
457 last if $group_login eq $name;
458 last if defined $groupname{$group_login};
459 if ($group_login eq $group_uniq) {
460 $group_login = $name; last;
463 if (defined $gid{$group_login}) {
464 # convert numeric groupname (gid) to groupname
465 $group_login = $gid{$group_login};
468 warn "Group does not exist!\a\n";
471 #if (defined($groupname{$group_login})) {
472 # &add_group($groupname{$group_login}, $name);
475 return ($group_login, $group_uniq) if $group_login eq $name;
476 return ($group_login, $group_login);
479 # return other groups (string)
480 sub new_users_groups
{
481 local($name, $other_groups) = @_;
483 "Login group is ``$group_login''. Invite $name into other groups:";
485 local($new_groups,$groups);
487 $other_groups = "no" unless $other_groups;
490 $groups = &confirm_list
($string, 1, $other_groups,
491 ("no", $other_groups, "guest"));
493 return "" if $groups eq "no";
495 ($flag, $new_groups) = &new_users_groups_valid
($groups);
498 $new_groups =~ s/\s*$//;
502 sub new_users_groups_valid
{
504 local($e, $new_groups);
507 foreach $e (split(/[,\s]+/, $groups)) {
508 # convert numbers to groupname
509 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
512 if (defined($groupname{$e})) {
513 if ($e eq $group_login) {
514 # do not add user to a group if this group
515 # is also the login group.
516 } elsif (&add_group
($groupname{$e}, $name)) {
517 $new_groups .= "$e ";
519 warn "$name is already member of group ``$e''\n";
522 warn "Group ``$e'' does not exist\a\n"; $flag++;
525 return ($flag, $new_groups);
531 # Note that we either show "password disabled" or
532 # "****" .. we don't show "empty password" since
533 # the whole point of starring out the password in
534 # the first place is to stop people looking over your
535 # shoulder and seeing the password.. -- adrian
536 if ($usepassword eq "no") {
537 $newpasswd = "Password disabled";
538 } elsif ($enableaccount eq "no") {
539 $newpasswd = "Password disabled";
550 Gid: $g_id ($group_login)
552 Groups: $group_login $new_groups
557 return &confirm_yn
("OK?", "yes");
560 # make password database
561 sub new_users_pwdmkdb
{
562 local($last) = shift;
563 local($name) = shift;
565 system("$pwd_mkdb -u $name $etc_passwd");
568 warn "``$pwd_mkdb'' failed\n";
573 # update group database
574 sub new_users_group_update
{
578 if (!defined($groupname{$group_login}) &&
579 !defined($gid{$groupname{$group_login}})) {
580 push(@group_backup, "$group_login:*:$g_id:");
581 $groupname{$group_login} = $g_id;
582 $gid{$g_id} = $group_login;
583 # $groupmembers{$g_id} = $group_login;
586 if ($new_groups || defined($groupname{$group_login}) ||
587 defined($gid{$groupname{$group_login}}) &&
588 $gid{$groupname{$group_login}} ne "+") {
589 # new user is member of some groups
590 # new login group is already in name space
591 rename($group, "$group.bak");
592 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
594 # Restore comments from the top of the group file
595 @a = @group_comments;
596 foreach $e (sort {$a <=> $b} (keys %gid)) {
597 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
599 &append_file
($group, @a);
601 &append_file
($group, "$group_login:*:$g_id:");
606 sub new_users_passwd_update
{
607 # update passwd/group variables
608 push(@passwd_backup, $new_entry);
609 $username{$name} = $u_id;
611 $pwgid{$g_id} = $name;
614 # send message to new user
615 sub new_users_sendmessage
{
616 return 1 if $send_message eq "no";
619 &confirm_list
("Send message to ``$name'' and:",
620 1, "no", ("root", "second_mail_address", "no"));
622 $cc = "" if $cc eq "no";
624 foreach $e (@message_buffer) {
629 local(@message_buffer_append) = ();
630 if (!&confirm_yn
("Add anything to default message", "no")) {
631 print "Use ``.'' or ^D alone on a line to finish your message.\n";
632 push(@message_buffer_append, "\n");
633 while($read = <STDIN
>) {
634 last if $read eq "\.\n";
635 push(@message_buffer_append, $read);
639 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
640 if (&confirm_yn
("Send message", "yes"));
644 local($to, @message) = @_;
647 if (!open(M
, "| mail -s Welcome $to")) {
648 warn "Cannot send mail to: $to!\n";
651 foreach $e (@message) {
652 print M
eval "\"$e\"";
659 sub new_users_password
{
664 system("stty -echo");
665 $password = &confirm_list
("Enter password", 1, "", "");
668 if ($password ne "") {
669 system("stty -echo");
670 $newpass = &confirm_list
("Enter password again", 1, "", "");
673 last if $password eq $newpass;
674 print "They didn't match, please try again\n";
676 elsif (&confirm_yn
("Use an empty password?", "yes")) {
684 sub new_users_use_password
{
685 local ($p) = $defaultusepassword;
686 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
687 return "yes" if (($defaultusepassword eq "yes" && $p) ||
688 ($defaultusepassword eq "no" && !$p));
689 return "no"; # otherwise
692 sub new_users_enable_account
{
693 local ($p) = $defaultenableaccount;
694 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
695 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
696 ($defaultenableaccount eq "no" && !$p));
697 return "no"; # otherwise
700 sub new_users_empty_password
{
701 local ($p) = $defaultemptypassword;
702 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
703 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
704 ($defaultemptypassword eq "no" && !$p));
705 return "no"; # otherwise
710 print "\n" if $verbose;
711 print "Ok, let's go.\n" .
712 "Don't worry about mistakes. I will give you the chance later to " .
713 "correct any input.\n" if $verbose;
716 # fullname: Full name
718 # userhome: home path for user
722 # group_login: groupname of g_id
723 # new_groups: some other groups
724 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
726 local($groupmembers_bak, $cryptpwd);
727 local($new_users_ok) = 1;
731 $new_groups = "no" unless $groupname{$new_groups};
734 $name = &new_users_name
;
735 $fullname = &new_users_fullname
($name);
736 $sh = &new_users_shell
;
737 $userhome = &new_users_home
($name);
738 ($u_id, $g_id) = &new_users_id
($name);
739 $class = &new_users_class
($defaultclass);
740 ($group_login, $defaultgroup) =
741 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
742 # do not use uniq username and login group
743 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
747 # If $usepasswd is 0, we use a * as a password
748 # If $usepasswd is 1, then
749 # if $enableaccount is 0, we prepend * as a password
750 # else if $enableaccount is 1 we don't prepend anything
751 # if $useemptypassword is 0 we ask for a password,
752 # else we use a blank one
754 # The logic is tasty, I'll give you that, but its flexible and
755 # it'll stop people shooting themselves in the foot.
757 $new_groups = &new_users_groups
($name, $new_groups);
759 $usepassword = &new_users_use_password
;
760 if ($usepassword eq "no") {
761 # note that the assignments to enableaccount and
762 # useemptypassword functionally do the same as
763 # usepasswd == "no". Just for consistency.
764 $password = ""; # no password!
765 $enableaccount = "no"; # doesn't matter here
766 $useemptypassword = "yes"; # doesn't matter here
768 $useemptypassword = &new_users_empty_password
;
769 if ($useemptypassword eq "no") {
770 $password = &new_users_password
;
772 $enableaccount = &new_users_enable_account
;
779 $cryptpwd = crypt($password, &salt
) if $password ne "";
781 if ($usepassword eq "no") {
784 # cryptpwd is valid before this if mess, so if
785 # blankpasswd is no we don't blank the cryptpwd
786 if ($useemptypassword eq "yes") {
789 if ($enableaccount eq "no") {
790 $cryptpwd = "*" . $cryptpwd;
794 $new_entry = "$name\:" . "$cryptpwd" .
795 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
796 &append_file
($etc_passwd, "$new_entry");
797 &new_users_pwdmkdb
("$new_entry", $name);
798 &new_users_group_update
;
799 &new_users_passwd_update
; print "Added user ``$name''\n";
800 &new_users_sendmessage
;
801 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
802 &home_create
($userhome, $name, $group_login);
806 if (!&confirm_yn
("Add another user?", "yes")) {
807 print "Goodbye!\n" if $verbose;
810 print "\n" if !$verbose;
814 # ask for password usage
815 sub password_default
{
816 local($p) = $defaultusepassword;
818 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
819 $changes++ unless $p;
821 return "yes" if (($defaultusepassword eq "yes" && $p) ||
822 ($defaultusepassword eq "no" && !$p));
823 return "no"; # otherwise
826 # ask for account enable usage
827 sub enable_account_default
{
828 local ($p) = $defaultenableaccount;
830 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
831 $changes++ unless $p;
833 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
834 ($defaultenableaccount eq "no" && !$p));
835 return "no"; # otherwise
838 # ask for empty password
839 sub enable_empty_password
{
840 local ($p) = $defaultemptypassword;
842 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
843 $changes++ unless $p;
845 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
846 ($defaultemptypassword eq "no" && !$p));
847 return "no"; # otherwise
852 die "You are not root!\n" if $< && !$test;
865 [-message message_file]
868 [-s|-silent|-q|-quiet]
872 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
873 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
881 local($e, $last, @array);
883 foreach $e (sort @list) {
884 push(@array, $e) unless $e eq $last;
890 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
892 local($salt); # initialization
894 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
896 warn "calculate salt\n" if $verbose > 1;
898 for ($i = 0; $i < 27; $i++) {
899 srand(time + $rand + $$);
900 $rand = rand(25*29*17 + $rand);
901 $salt .= $itoa64[$rand & $#itoa64];
903 warn "Salt is: $salt\n" if $verbose > 1;
917 print "Use option ``-silent'' if you don't want to see " .
918 "all warnings and questions.\n\n";
920 print "Use option ``-verbose'' if you want to see more warnings and " .
921 "questions \nor try to repair bugs.\n\n";
926 sub parse_arguments
{
929 while ($_ = $argv[0], /^-/) {
932 if (/^--?(v|verbose)$/) { $verbose = 1 }
933 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
934 elsif (/^--?(debug)$/) { $verbose = 2 }
935 elsif (/^--?(h|help|\?)$/) { &usage
}
936 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
937 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
938 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
939 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
940 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
941 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
942 elsif (/^--?(check_only)$/) { $check_only = 1 }
943 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
945 elsif (/^--?(batch)$/) {
946 warn "The -batch option is not supported anymore.\n",
947 "Please use the pw(8) command line tool!\n";
951 elsif (/^--?(config_create)$/) { &create_conf
; }
952 elsif (/^--?(noconfig)$/) { $config_read = 0; }
955 #&usage if $#argv < 0;
967 $name = &stripdir
($name);
968 $name =~ s
|/+[^/]+$||;
969 $name = "/" unless $name; # dirname of / is /
973 # return 1 if $file is a readable file or link
975 local($file, $verb) = @_;
978 if (-f
$file || -l
$file) {
980 warn "$file unreadable\n" if $verbose;
982 warn "$file is not a plain file or link\n" if $verbose;
988 # create configuration files and exit
991 if ($send_message ne 'no') {
992 &message_create
($send_message);
994 &message_create
($send_message_bak);
1000 # log for new user in /var/log/adduser
1002 local($string) = @_;
1005 return 1 if $logfile eq "no";
1007 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1011 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
1013 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1016 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1019 # create HOME directory, copy dotfiles from $dotdir to $HOME
1021 local($homedir, $name, $group) = @_;
1024 if (-e
"$homedir") {
1025 warn "HOME Directory ``$homedir'' already exist\a\n";
1029 # if the home directory prefix doesn't exist, create it
1030 # First, split the directory into a list; then remove the user's dir
1031 @dir = split('/', $homedir); pop(@dir);
1032 # Put back together & strip to get directory prefix
1033 $rootdir = &stripdir
(join('/', @dir));
1035 if (!&mkdirhier
("$rootdir")) {
1036 # warn already displayed
1040 if ($dotdir eq 'no') {
1041 if (!mkdir("$homedir", 0755)) {
1042 warn "$dir: $!\n"; return 0;
1044 system 'chown', "$name:$group", $homedir;
1048 # copy files from $dotdir to $homedir
1049 # rename 'dot.foo' files to '.foo'
1050 print "Copy files from $dotdir to $homedir\n" if $verbose;
1051 system("cp -R $dotdir $homedir");
1052 system("chmod -R u+wrX,go-w $homedir");
1053 system("chown -R $name:$group $homedir");
1056 opendir(D
, $homedir);
1057 foreach $file (readdir(D
)) {
1058 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
1059 $file =~ s/^dot\././;
1060 rename("$homedir/dot$file", "$homedir/$file");
1062 chmod(0600, "$homedir/$file")
1063 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1064 chmod(0700, "$homedir/$file")
1065 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1071 # makes a directory hierarchy
1074 $dir = &stripdir
($dir);
1075 local($user_partition) = "/usr";
1076 local($dirname) = &dirname
($dir);
1078 -e
$dirname || &mkdirhier
($dirname);
1080 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1081 # home partition is on root partition
1082 # create home partition on $user_partition and make
1083 # a symlink from $dir to $user_partition/`basename $dir`
1084 # For instance: /home -> /usr/home
1086 local($basename) = &basename
($dir);
1087 local($d) = "$user_partition/$basename";
1091 warn "Oops, $d already exist\n" if $verbose;
1093 print "Create $d\n" if $verbose;
1094 if (!mkdir("$d", 0755)) {
1095 warn "$d: $!\a\n"; return 0;
1099 unlink($dir); # symlink to nonexist file
1100 print "Create symlink: $dir -> $d\n" if $verbose;
1101 if (!symlink("$d", $dir)) {
1102 warn "Symlink $d: $!\a\n"; return 0;
1105 print "Create $dir\n" if $verbose;
1106 if (!mkdir("$dir", 0755)) {
1107 warn "Directory ``$dir'': $!\a\n"; return 0;
1117 $dir = &stripdir
($dir);
1119 foreach $d (split('/', $dir)) {
1123 print "Create $dir\n" if $verbose;
1124 if (!mkdir("$dir", 0755)) {
1125 warn "$dir: $!\n"; return 0;
1134 # F.i.: //usr///home// -> /usr/home
1138 $dir =~ s
|/+|/|g
; # delete double '/'
1139 $dir =~ s
|/$||; # delete '/' at end
1140 return $dir if $dir ne "";
1144 # Read one of the elements from @list. $confirm is default.
1145 # If !$allow accept only elements from @list.
1147 local($message, $allow, $confirm, @list) = @_;
1148 local($read, $c, $print);
1150 $print = "$message" if $message;
1151 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1153 $print .= join($", &uniq(@list)); #"
1154 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1156 print "\n" if (length($print) + length($confirm)) > 60;
1157 print "[$confirm]: ";
1159 chop($read = <STDIN>);
1162 return $confirm if $read eq "";
1163 return "$read" if $allow;
1165 foreach $c (@list) {
1166 return $read if $c eq $read;
1168 warn "$read: is not allowed!\a\n";
1169 return &confirm_list($message, $allow, $confirm, @list);
1172 # YES or NO question
1173 # return 1 if &confirm("message", "yes") and answer is yes
1174 # or if &confirm("message", "no") an answer is no
1177 local($message, $confirm) = @_;
1178 local($yes) = '^(yes
|YES
|y
|Y
)$';
1179 local($no) = '^(no|NO
|n
|N
)$';
1182 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1187 print "$message (y/n) [$confirm]: ";
1188 chop($read = <STDIN>);
1191 return 1 unless $read;
1193 if (($confirm eq "y" && $read =~ "$yes") ||
1194 ($confirm eq "n" && $read =~ "$no")) {
1198 if ($read !~ "$yes" && $read !~ "$no") {
1199 warn "Wrong value. Enter again!\a\n";
1200 return &confirm_yn($message, $confirm);
1205 # test if $dotdir exist
1206 # return "no" if $dotdir not exist or dotfiles should not copied
1207 sub dotdir_default {
1208 local($dir) = $dotdir;
1210 return &dotdir_default_valid($dir) unless $verbose;
1212 $dir = &confirm_list("Copy dotfiles from:", 1,
1213 $dir, ("no", $dotdir_bak, $dir));
1214 last if $dir eq &dotdir_default_valid($dir);
1216 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1218 $changes++ if $dir ne $dotdir;
1222 sub dotdir_default_valid {
1225 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1226 return $dir if $dir eq "no";
1227 warn "Dotdir ``$dir'' is not a directory\a\n";
1231 # ask for messages to new users
1232 sub message_default {
1233 local($file) = $send_message;
1234 local(@d) = ($file, $send_message_bak, "no");
1237 $file = &confirm_list("Send message from file:", 1, $file, @d);
1238 last if $file eq "no";
1239 last if &filetest($file, 1);
1241 # maybe create message file
1242 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1243 last if &filetest($file, 0);
1244 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1248 if ($file eq "no" || !&filetest($file, 0)) {
1249 warn "Do not send message\n" if $verbose;
1252 &message_read($file);
1255 $changes++ if $file ne $send_message && $verbose;
1259 # create message file
1260 sub message_create {
1263 rename($file, "$file.bak");
1264 if (!open(M, "> $file")) {
1265 warn "Messagefile ``$file'': $!\n"; return 0;
1269 # Message file for adduser(8)
1271 # default variables: \$name, \$fullname, \$password
1272 # other variables: see /etc/adduser.conf after
1273 # line ``$do_not_delete''
1278 your account ``\$name'' was created.
1281 See also chpass(1), finger(1), passwd(1)
1287 # read message file into buffer
1290 @message_buffer = '';
1292 if (!open(R
, "$file")) {
1293 warn "File ``$file'':$!\n"; return 0;
1296 push(@message_buffer, $_) unless /^\s*#/;
1301 # write @list to $file with file-locking
1303 local($file,@list) = @_;
1305 local($LOCK_EX) = 2;
1306 local($LOCK_NB) = 4;
1307 local($LOCK_UN) = 8;
1309 open(F
, ">> $file") || die "$file: $!\n";
1310 print "Lock $file.\n" if $verbose > 1;
1311 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1312 warn "Cannot lock file: $file\a\n";
1313 die "Sorry, give up\n"
1314 unless &confirm_yn
("Try again?", "yes");
1316 print F
join("\n", @list) . "\n";
1318 print "Unlock $file.\n" if $verbose > 1;
1322 # return free uid+gid
1323 # uid == gid if possible
1327 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1328 # looking for next free uid
1329 while($uid{$uid_start}) {
1331 $uid_start = 1000 if $uid_start >= $uid_end;
1332 print "$uid_start\n" if $verbose > 1;
1335 local($gid_start) = $uid_start;
1336 # group for user (username==groupname) already exist
1337 if ($groupname{$group}) {
1338 $gid_start = $groupname{$group};
1340 # gid is in use, looking for another gid.
1341 # Note: uid an gid are not equal
1342 elsif ($gid{$uid_start}) {
1343 while($gid{$gid_start} || $uid{$gid_start}) {
1345 $gid_start = $uid_end if $gid_start < 100;
1348 return ($uid_start, $gid_start);
1354 local($user_flag) = 0;
1356 # don't read config file
1357 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1359 if(!open(C
, "$config")) {
1360 warn "$config: $!\n"; return 0;
1364 # user defined variables
1365 /^$do_not_delete/ && $user_flag++;
1366 # found @array or $variable
1367 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1371 # lines with '^##' are not saved
1372 push(@user_variable_list, $_)
1373 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1375 #warn "X @user_variable_list X\n";
1382 local($silent) = @_;
1385 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1389 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1392 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1396 rename($config, "$config.bak");
1397 open(C
, "> $config") || die "$config: $!\n";
1399 # prepare some variables
1400 $send_message = "no" unless $send_message;
1401 $defaultusepassword = "no" unless $defaultusepassword;
1402 $defaultenableaccount = "yes" unless $defaultenableaccount;
1403 $defaultemptypassword = "no" unless $defaultemptypassword;
1404 local($shpref) = "'" . join("', '", @shellpref) . "'";
1405 local($shpath) = "'" . join("', '", @path) . "'";
1406 local($user_var) = join('', @user_variable_list);
1410 # $config - automatic generated by adduser(8)
1412 # Note: adduser read *and* write this file.
1413 # You may change values, but don't add new things before the
1414 # line ``$do_not_delete''
1420 # use password-based authentication for new users
1421 # defaultusepassword = "yes" | "no"
1422 defaultusepassword = "$defaultusepassword"
1424 # enable account password at creation
1425 # (the password will be prepended with a star if the account isn't enabled)
1426 # defaultenableaccount = "yes" | "no"
1427 defaultenableaccount = "$defaultenableaccount"
1429 # allow blank passwords
1430 # defaultemptypassword = "yes" | "no"
1431 defaultemptypassword = "$defaultemptypassword"
1433 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1436 # send this file to new user ("/etc/adduser.message" or "no")
1437 send_message = "$send_message"
1439 # config file for adduser ("/etc/adduser.conf")
1442 # logfile ("/var/log/adduser" or "no")
1443 logfile = "$logfile"
1445 # default HOME directory ("/home")
1448 # List of directories where shells located
1449 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1452 # common shell list, first element has higher priority
1453 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1454 shellpref = ($shpref)
1456 # defaultshell if not empty ("bash")
1457 defaultshell = "$defaultshell"
1459 # defaultgroup ('USER' for same as username or any other valid group)
1460 defaultgroup = $defaultgroup
1462 # defaultclass if not empty
1463 defaultclass = "$defaultclass"
1465 # new users get this uid (1000)
1466 uid_start = "$uid_start"
1469 ## your own variables, see /etc/adduser.message
1480 $test = 0; # test mode, only for development
1483 &check_root
; # you must be root to run this script!
1484 &variables
; # initialize variables
1485 &config_read
(@ARGV); # read variables form config-file
1486 &parse_arguments
(@ARGV); # parse arguments
1494 &passwd_check
; # check for valid passwdb
1495 &shells_read
; # read /etc/shells
1496 &passwd_read
; # read /etc/master.passwd
1497 &group_read
; # read /etc/group
1498 &group_check
; # check for incon*
1499 exit 0 if $check_only; # only check consistence and exit
1504 &shells_add
; # maybe add some new shells
1505 $defaultshell = &shell_default
; # enter default shell
1506 $home = &home_partition
($home); # find HOME partition
1507 $dotdir = &dotdir_default
; # check $dotdir
1508 $send_message = &message_default
; # send message to new user
1509 $defaultusepassword = &password_default
; # maybe use password
1510 if ($defaultusepassword eq "no") {
1512 print "Creating accounts with a locked password.\n";
1514 $defaultenableaccount = "no";
1515 $defaultemptypassword = "yes";
1517 $defaultenableaccount = &enable_account_default
; # enable or disable account
1518 $defaultemptypassword = &enable_empty_password
; # use empty password or not
1520 &config_write
(!$verbose); # write variables in file
1522 # main loop for creating new users
1523 &new_users
; # add new users