]>
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 $usernameregexp = "^[a-z0-9_][a-z0-9_-]*\$"; # configurable
34 $defaultusernameregexp = $usernameregexp; # remains constant
35 $defaultusepassword = "yes"; # use password authentication for new users
36 $defaultenableaccount = "yes"; # enable the account by default
37 $defaultemptypassword = "no"; # don't create an empty password
38 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
39 $dotdir_bak = $dotdir;
40 $send_message = "/etc/adduser.message"; # send message to new user
41 $send_message_bak = '/etc/adduser.message';
42 $config = "/etc/adduser.conf"; # config file for adduser
43 $config_read = 1; # read config file
44 $logfile = "/var/log/adduser"; # logfile
45 $home = "/home"; # default HOME
46 $etc_shells = "/etc/shells";
47 $etc_passwd = "/etc/master.passwd";
48 $group = "/etc/group";
49 @pwd_mkdb = qw(pwd_mkdb -p); # program for building passwd database
52 # List of directories where shells located
53 @path = ('/bin', '/usr/bin', '/usr/local/bin');
54 # common shells, first element has higher priority
55 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
57 $defaultshell = 'sh'; # defaultshell if not empty
59 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
62 $uid_start = 1000; # new users get this uid
63 $uid_end = 32000; # max. uid
67 $username = ''; # $username{username} = uid
68 $uid = ''; # $uid{uid} = username
69 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
71 $password = ''; # password for new users
72 $usepassword = ''; # use password-based auth
73 $useemptypassword = ''; # use an empty password
74 $enableaccount = ''; # enable or disable account password at creation
77 $groupname =''; # $groupname{groupname} = gid
78 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
79 $gid = ''; # $gid{gid} = groupname; gid form group db
80 @group_comments; # Comments in the group file
83 $shell = ''; # $shell{`basename sh`} = sh
85 umask 022; # don't give login group write access
87 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
91 @user_variable_list = ''; # user variables in /etc/adduser.conf
92 $do_not_delete = '## DO NOT DELETE THIS LINE!';
95 # read shell database, see also: shells(5)
100 print "Check $etc_shells\n" if $verbose;
101 open(S
, $etc_shells) || die "$etc_shells:$!\n";
105 s/^\s*//; s/\s+.*//; # chop
108 $shell{&basename
($sh)} = $sh;
110 warn "Shell: $sh not executable!\n";
116 # Allow /nonexistent and /bin/date as a valid shell for system utils
117 push(@list, "/nonexistent");
118 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
119 $shell{"no"} = "/nonexistent";
121 push(@list, "/bin/date");
122 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
123 $shell{"date"} = "/bin/date";
128 # add new shells if possible
130 local($sh,$dir,@list);
132 return 1 unless $verbose;
134 foreach $sh (@shellpref) {
137 # shell $sh is not defined as login shell
138 foreach $dir (@path) {
141 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
142 push(@list, "$dir/$sh");
143 push(@shellpref, "$sh");
144 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
151 &append_file
($etc_shells, @list) if $#list >= 0;
154 # choose your favourite shell and return the shell
156 local($e,$i,$new_shell);
159 $sh = &shell_default_valid
($defaultshell);
160 return $sh unless $verbose;
162 $new_shell = &confirm_list
("Enter your default shell:", 0,
163 $sh, sort(keys %shell));
164 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
165 $changes++ if $new_shell ne $sh;
169 sub shell_default_valid
{
173 return $sh if $shell{$sh};
175 foreach $e (@shellpref) {
177 last if defined($shell{$s});
180 warn "Shell ``$sh'' is undefined, use ``$s''\n";
184 # return default home partition (e.g. "/home")
185 # create base directory if nesseccary
188 $home = &stripdir
($home);
191 return $h if !$verbose && $h eq &home_partition_valid
($h);
194 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
196 last if $h eq &home_partition_valid
($h);
199 $changes++ if $h ne $home;
203 sub home_partition_valid
{
208 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
212 warn "Please use absolute path for home: ``$h''.\a\n";
217 warn "$h exists, but is not a directory or symlink!\n"
218 unless -d
$h || -l
$h;
219 warn "$h is not writable!\n"
223 # create home partition
224 return $h if &mkdir_home
($h);
229 # check for valid passwddb
231 system(@pwd_mkdb, '-C', $etc_passwd);
232 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
237 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
239 print "Check $etc_passwd\n" if $verbose;
240 open(P
, "$etc_passwd") || die "$etc_passwd: $!\n";
244 push(@passwd_backup, $_);
249 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
251 print "$p_username already exists with uid: $username{$p_username}!\n"
252 if $username{$p_username} && $verbose;
253 $username{$p_username} = $p_uid;
254 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
255 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
256 print "User $p_username: illegal shell: ``$sh''\n"
257 if ($verbose && $sh &&
258 !$shell{&basename
($sh)} &&
259 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
260 $sh !~ /\/(pppd
|sliplogin
|nologin
|nonexistent
)$/);
261 $uid{$p_uid} = $p_username;
262 $pwgid{$p_gid} = $p_username;
269 local($g_groupname,$pw,$g_gid, $memb);
271 print "Check $group\n" if $verbose;
272 open(G
, "$group") || die "$group: $!\n";
275 push(@group_backup, $_);
278 # Save comments to restore later
280 push(@group_comments, $_);
284 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
286 $groupmembers{$g_gid} = $memb;
287 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
288 if $groupname{$g_groupname} && $verbose;
289 $groupname{$g_groupname} = $g_gid;
290 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
291 if $gid{$g_gid} && $verbose;
292 $gid{$g_gid} = $g_groupname;
297 # check gids /etc/passwd <-> /etc/group
299 local($c_gid, $c_username, @list);
301 foreach $c_gid (keys %pwgid) {
303 $c_username = $pwgid{$c_gid};
304 warn "User ``$c_username'' has gid $c_gid but a group with this " .
305 "gid does not exist.\n" if $verbose;
311 # main loop for creating new users
319 $name = &confirm_list
("Enter username", 1, $usernameregexp, "");
320 last if (&new_users_name_valid
($name));
325 sub new_users_name_valid
{
328 if ($name eq $usernameregexp) { # user/admin just pressed <Return>
329 warn "Please enter a username\a\n";
331 } elsif (length($name) > 16) {
332 warn "Username is longer than 16 characters.\a\n";
334 } elsif ($name =~ /[:\n]/) {
335 warn "Username cannot contain colon or newline characters.\a\n";
337 } elsif ($name !~ /$usernameregexp/) {
338 if ($usernameregexp eq $defaultusernameregexp) {
339 warn "Illegal username.\n" .
340 "Please use only lowercase Roman, decimal, underscore, " .
341 "or hyphen characters.\n" .
342 "Additionally, a username should not start with a hyphen.\a\n";
344 warn "Username doesn't match the regexp /$usernameregexp/\a\n";
347 } elsif (defined($username{$name})) {
348 warn "Username ``$name'' already exists!\a\n"; return 0;
354 sub new_users_fullname
{
359 $fullname = &confirm_list
("Enter full name", 1, "", "");
360 last if $fullname eq &new_users_fullname_valid
($fullname);
362 $fullname = $name unless $fullname;
366 sub new_users_fullname_valid
{
367 local($fullname) = @_;
369 return $fullname if $fullname !~ /:/;
371 warn "``:'' is not allowed!\a\n";
375 # return shell (full path) for user
376 sub new_users_shell
{
379 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
383 # return home (full path) for user
384 # Note that the home path defaults to $home/$name for batch
390 $userhome = &confirm_list
("Enter home directory (full path)", 1, "$home/$name", "");
391 last if $userhome =~ /^\//;
392 warn qq{Home directory "$userhome" is not a full path\a\n};
397 # return free uid and gid
400 local($u_id, $g_id) = &next_id
($name);
401 local($u_id_tmp, $e);
404 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
405 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
407 if ($uid{$u_id_tmp}) {
408 warn "Uid ``$u_id_tmp'' in use!\a\n";
409 $uid_start = $u_id_tmp;
410 ($u_id, $g_id) = &next_id
($name);
413 warn "Wrong uid.\a\n";
417 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
419 $uid_start = $u_id_tmp;
420 return &next_id
($name);
423 # return login class for user
424 sub new_users_class
{
428 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
429 $class = "" if $class eq "default";
435 local($gid, $name) = @_;
438 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
440 $groupmembers_bak{$gid} = $groupmembers{$gid};
441 $groupmembers{$gid} .= "," if $groupmembers{$gid};
442 $groupmembers{$gid} .= "$name";
449 sub new_users_grplogin
{
450 local($name, $defaultgroup, $new_users_ok) = @_;
451 local($group_login, $group);
454 $group = $defaultgroup if $defaultgroup ne $group_uniq;
458 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
460 # restore old groupmembers, user was not accept
461 foreach $e (keys %groupmembers_bak) {
462 $groupmembers{$e} = $groupmembers_bak{$e};
467 $group_login = &confirm_list
("Login group", 1, $group,
469 last if $group_login eq $group;
470 last if $group_login eq $name;
471 last if defined $groupname{$group_login};
472 if ($group_login eq $group_uniq) {
473 $group_login = $name; last;
476 if (defined $gid{$group_login}) {
477 # convert numeric groupname (gid) to groupname
478 $group_login = $gid{$group_login};
481 warn "Group does not exist!\a\n";
484 #if (defined($groupname{$group_login})) {
485 # &add_group($groupname{$group_login}, $name);
488 return ($group_login, $group_uniq) if $group_login eq $name;
489 return ($group_login, $group_login);
492 # return other groups (string)
493 sub new_users_groups
{
494 local($name, $other_groups) = @_;
496 "Login group is ``$group_login''. Invite $name into other groups:";
498 local($new_groups,$groups);
500 $other_groups = "no" unless $other_groups;
503 $groups = &confirm_list
($string, 1, $other_groups,
504 ("no", $other_groups, "guest"));
506 return "" if $groups eq "no";
508 ($flag, $new_groups) = &new_users_groups_valid
($groups);
511 $new_groups =~ s/\s*$//;
515 sub new_users_groups_valid
{
517 local($e, $new_groups);
520 foreach $e (split(/[,\s]+/, $groups)) {
521 # convert numbers to groupname
522 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
525 if (defined($groupname{$e})) {
526 if ($e eq $group_login) {
527 # do not add user to a group if this group
528 # is also the login group.
529 } elsif (&add_group
($groupname{$e}, $name)) {
530 $new_groups .= "$e ";
532 warn "$name is already member of group ``$e''\n";
535 warn "Group ``$e'' does not exist\a\n"; $flag++;
538 return ($flag, $new_groups);
544 # Note that we either show "password disabled" or
545 # "****" .. we don't show "empty password" since
546 # the whole point of starring out the password in
547 # the first place is to stop people looking over your
548 # shoulder and seeing the password.. -- adrian
549 if ($usepassword eq "no") {
550 $newpasswd = "Password disabled";
551 } elsif ($enableaccount eq "no") {
552 $newpasswd = "Password disabled";
563 Gid: $g_id ($group_login)
565 Groups: $group_login $new_groups
570 return &confirm_yn
("OK?", "yes");
573 # make password database
574 sub new_users_pwdmkdb
{
575 local($last) = shift;
576 local($name) = shift;
578 system(@pwd_mkdb, '-u', $name, $etc_passwd);
581 warn "``@pwd_mkdb'' failed\n";
586 # update group database
587 sub new_users_group_update
{
591 if (!defined($groupname{$group_login}) &&
592 !defined($gid{$groupname{$group_login}})) {
593 push(@group_backup, "$group_login:*:$g_id:");
594 $groupname{$group_login} = $g_id;
595 $gid{$g_id} = $group_login;
596 # $groupmembers{$g_id} = $group_login;
599 if ($new_groups || defined($groupname{$group_login}) ||
600 defined($gid{$groupname{$group_login}}) &&
601 $gid{$groupname{$group_login}} ne "+") {
602 # new user is member of some groups
603 # new login group is already in name space
604 rename($group, "$group.bak");
605 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
607 # Restore comments from the top of the group file
608 @a = @group_comments;
609 foreach $e (sort {$a <=> $b} (keys %gid)) {
610 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
612 &append_file
($group, @a);
614 &append_file
($group, "$group_login:*:$g_id:");
619 sub new_users_passwd_update
{
620 # update passwd/group variables
621 push(@passwd_backup, $new_entry);
622 $username{$name} = $u_id;
624 $pwgid{$g_id} = $name;
627 # send message to new user
628 sub new_users_sendmessage
{
629 return 1 if $send_message eq "no";
632 &confirm_list
("Send message to ``$name'' and:",
633 1, "no", ("root", "second_mail_address", "no"));
635 $cc = "" if $cc eq "no";
637 foreach $e (@message_buffer) {
642 local(@message_buffer_append) = ();
643 if (!&confirm_yn
("Add anything to default message", "no")) {
644 print "Use ``.'' or ^D alone on a line to finish your message.\n";
645 push(@message_buffer_append, "\n");
646 while($read = <STDIN
>) {
647 last if $read eq "\.\n";
648 push(@message_buffer_append, $read);
652 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
653 if (&confirm_yn
("Send message", "yes"));
657 local($to, @message) = @_;
660 if (!open(M
, "| mail -s Welcome $to")) {
661 warn "Cannot send mail to: $to!\n";
664 foreach $e (@message) {
665 print M
eval "\"$e\"";
672 sub new_users_password
{
677 system("stty -echo");
678 $password = &confirm_list
("Enter password", 1, "", "");
681 if ($password ne "") {
682 system("stty -echo");
683 $newpass = &confirm_list
("Enter password again", 1, "", "");
686 last if $password eq $newpass;
687 print "They didn't match, please try again\n";
689 elsif (&confirm_yn
("Use an empty password?", "yes")) {
697 sub new_users_use_password
{
698 local ($p) = $defaultusepassword;
699 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
700 return "yes" if (($defaultusepassword eq "yes" && $p) ||
701 ($defaultusepassword eq "no" && !$p));
702 return "no"; # otherwise
705 sub new_users_enable_account
{
706 local ($p) = $defaultenableaccount;
707 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
708 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
709 ($defaultenableaccount eq "no" && !$p));
710 return "no"; # otherwise
713 sub new_users_empty_password
{
714 local ($p) = $defaultemptypassword;
715 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
716 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
717 ($defaultemptypassword eq "no" && !$p));
718 return "no"; # otherwise
723 print "\n" if $verbose;
724 print "Ok, let's go.\n" .
725 "Don't worry about mistakes. I will give you the chance later to " .
726 "correct any input.\n" if $verbose;
729 # fullname: Full name
731 # userhome: home path for user
735 # group_login: groupname of g_id
736 # new_groups: some other groups
737 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
739 local($groupmembers_bak, $cryptpwd);
740 local($new_users_ok) = 1;
744 $new_groups = "no" unless $groupname{$new_groups};
747 $name = &new_users_name
;
748 $fullname = &new_users_fullname
($name);
749 $sh = &new_users_shell
;
750 $userhome = &new_users_home
($name);
751 ($u_id, $g_id) = &new_users_id
($name);
752 $class = &new_users_class
($defaultclass);
753 ($group_login, $defaultgroup) =
754 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
755 # do not use uniq username and login group
756 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
760 # If $usepasswd is 0, we use a * as a password
761 # If $usepasswd is 1, then
762 # if $enableaccount is 0, we prepend * as a password
763 # else if $enableaccount is 1 we don't prepend anything
764 # if $useemptypassword is 0 we ask for a password,
765 # else we use a blank one
767 # The logic is tasty, I'll give you that, but its flexible and
768 # it'll stop people shooting themselves in the foot.
770 $new_groups = &new_users_groups
($name, $new_groups);
772 $usepassword = &new_users_use_password
;
773 if ($usepassword eq "no") {
774 # note that the assignments to enableaccount and
775 # useemptypassword functionally do the same as
776 # usepasswd == "no". Just for consistency.
777 $password = ""; # no password!
778 $enableaccount = "no"; # doesn't matter here
779 $useemptypassword = "yes"; # doesn't matter here
781 $useemptypassword = &new_users_empty_password
;
782 if ($useemptypassword eq "no") {
783 $password = &new_users_password
;
785 $enableaccount = &new_users_enable_account
;
792 $cryptpwd = crypt($password, &salt
) if $password ne "";
794 if ($usepassword eq "no") {
797 # cryptpwd is valid before this if mess, so if
798 # blankpasswd is no we don't blank the cryptpwd
799 if ($useemptypassword eq "yes") {
802 if ($enableaccount eq "no") {
803 $cryptpwd = "*" . $cryptpwd;
807 $new_entry = "$name\:" . "$cryptpwd" .
808 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
809 &append_file
($etc_passwd, "$new_entry");
810 &new_users_pwdmkdb
("$new_entry", $name);
811 &new_users_group_update
;
812 &new_users_passwd_update
; print "Added user ``$name''\n";
813 &new_users_sendmessage
;
814 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
815 &home_create
($userhome, $name, $group_login);
819 if (!&confirm_yn
("Add another user?", "yes")) {
820 print "Goodbye!\n" if $verbose;
823 print "\n" if !$verbose;
827 # ask for password usage
828 sub password_default
{
829 local($p) = $defaultusepassword;
831 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
832 $changes++ unless $p;
834 return "yes" if (($defaultusepassword eq "yes" && $p) ||
835 ($defaultusepassword eq "no" && !$p));
836 return "no"; # otherwise
839 # ask for account enable usage
840 sub enable_account_default
{
841 local ($p) = $defaultenableaccount;
843 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
844 $changes++ unless $p;
846 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
847 ($defaultenableaccount eq "no" && !$p));
848 return "no"; # otherwise
851 # ask for empty password
852 sub enable_empty_password
{
853 local ($p) = $defaultemptypassword;
855 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
856 $changes++ unless $p;
858 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
859 ($defaultemptypassword eq "no" && !$p));
860 return "no"; # otherwise
865 die "You are not root!\n" if $< && !$test;
878 [-message message_file]
881 [-s|-silent|-q|-quiet]
885 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
886 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
894 local($e, $last, @array);
896 foreach $e (sort @list) {
897 push(@array, $e) unless $e eq $last;
903 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
905 local($salt); # initialization
907 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
909 warn "calculate salt\n" if $verbose > 1;
911 for ($i = 0; $i < 27; $i++) {
912 srand(time + $rand + $$);
913 $rand = rand(25*29*17 + $rand);
914 $salt .= $itoa64[$rand & $#itoa64];
916 warn "Salt is: $salt\n" if $verbose > 1;
930 print "Use option ``-silent'' if you don't want to see " .
931 "all warnings and questions.\n\n";
933 print "Use option ``-verbose'' if you want to see more warnings and " .
934 "questions \nor try to repair bugs.\n\n";
939 sub parse_arguments
{
942 while ($_ = $argv[0], /^-/) {
945 if (/^--?(v|verbose)$/) { $verbose = 1 }
946 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
947 elsif (/^--?(debug)$/) { $verbose = 2 }
948 elsif (/^--?(h|help|\?)$/) { &usage
}
949 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
950 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
951 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
952 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
953 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
954 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
955 elsif (/^--?(check_only)$/) { $check_only = 1 }
956 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
958 elsif (/^--?(batch)$/) {
959 warn "The -batch option is not supported anymore.\n",
960 "Please use the pw(8) command line tool!\n";
964 elsif (/^--?(config_create)$/) { &create_conf
; }
965 elsif (/^--?(noconfig)$/) { $config_read = 0; }
968 #&usage if $#argv < 0;
980 $name = &stripdir
($name);
981 $name =~ s
|/+[^/]+$||;
982 $name = "/" unless $name; # dirname of / is /
986 # return 1 if $file is a readable file or link
988 local($file, $verb) = @_;
991 if (-f
$file || -l
$file) {
993 warn "$file unreadable\n" if $verbose;
995 warn "$file is not a plain file or link\n" if $verbose;
1001 # create configuration files and exit
1004 if ($send_message ne 'no') {
1005 &message_create
($send_message);
1007 &message_create
($send_message_bak);
1013 # log for new user in /var/log/adduser
1015 local($string) = @_;
1018 return 1 if $logfile eq "no";
1020 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1024 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
1026 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1029 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1032 # create HOME directory, copy dotfiles from $dotdir to $HOME
1034 local($homedir, $name, $group) = @_;
1037 if (-e
"$homedir") {
1038 warn "HOME Directory ``$homedir'' already exist\a\n";
1042 # if the home directory prefix doesn't exist, create it
1043 # First, split the directory into a list; then remove the user's dir
1044 @dir = split('/', $homedir); pop(@dir);
1045 # Put back together & strip to get directory prefix
1046 $rootdir = &stripdir
(join('/', @dir));
1048 if (!&mkdirhier
("$rootdir")) {
1049 # warn already displayed
1053 if ($dotdir eq 'no') {
1054 if (!mkdir("$homedir", 0755)) {
1055 warn "$dir: $!\n"; return 0;
1057 system 'chown', "$name:$group", $homedir;
1061 # copy files from $dotdir to $homedir
1062 # rename 'dot.foo' files to '.foo'
1063 print "Copy files from $dotdir to $homedir\n" if $verbose;
1064 system('cp', '-R', $dotdir, $homedir);
1065 system('chmod', '-R', 'u+wrX,go-w', $homedir);
1066 system('chown', '-Rh', "$name:$group", $homedir);
1069 opendir(D
, $homedir);
1070 foreach $file (readdir(D
)) {
1071 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
1072 $file =~ s/^dot\././;
1073 rename("$homedir/dot$file", "$homedir/$file");
1075 chmod(0600, "$homedir/$file")
1076 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1077 chmod(0700, "$homedir/$file")
1078 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1084 # makes a directory hierarchy
1087 $dir = &stripdir
($dir);
1088 local($user_partition) = "/usr";
1089 local($dirname) = &dirname
($dir);
1091 -e
$dirname || &mkdirhier
($dirname);
1093 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1094 # home partition is on root partition
1095 # create home partition on $user_partition and make
1096 # a symlink from $dir to $user_partition/`basename $dir`
1097 # For instance: /home -> /usr/home
1099 local($basename) = &basename
($dir);
1100 local($d) = "$user_partition/$basename";
1104 warn "Oops, $d already exist\n" if $verbose;
1106 print "Create $d\n" if $verbose;
1107 if (!mkdir("$d", 0755)) {
1108 warn "$d: $!\a\n"; return 0;
1112 unlink($dir); # symlink to nonexist file
1113 print "Create symlink: $dir -> $d\n" if $verbose;
1114 if (!symlink("$d", $dir)) {
1115 warn "Symlink $d: $!\a\n"; return 0;
1118 print "Create $dir\n" if $verbose;
1119 if (!mkdir("$dir", 0755)) {
1120 warn "Directory ``$dir'': $!\a\n"; return 0;
1130 $dir = &stripdir
($dir);
1132 foreach $d (split('/', $dir)) {
1136 print "Create $dir\n" if $verbose;
1137 if (!mkdir("$dir", 0755)) {
1138 warn "$dir: $!\n"; return 0;
1147 # F.i.: //usr///home// -> /usr/home
1151 $dir =~ s
|/+|/|g
; # delete double '/'
1152 $dir =~ s
|/$||; # delete '/' at end
1153 return $dir if $dir ne "";
1157 # Read one of the elements from @list. $confirm is default.
1158 # If !$allow accept only elements from @list.
1160 local($message, $allow, $confirm, @list) = @_;
1161 local($read, $c, $print);
1163 $print = "$message" if $message;
1164 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1166 $print .= join($", &uniq(@list)); #"
1167 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1169 print "\n" if (length($print) + length($confirm)) > 60;
1170 print "[$confirm]: ";
1172 chop($read = <STDIN>);
1175 return $confirm if $read eq "";
1176 return "$read" if $allow;
1178 foreach $c (@list) {
1179 return $read if $c eq $read;
1181 warn "$read: is not allowed!\a\n";
1182 return &confirm_list($message, $allow, $confirm, @list);
1185 # YES or NO question
1186 # return 1 if &confirm("message", "yes") and answer is yes
1187 # or if &confirm("message", "no") an answer is no
1190 local($message, $confirm) = @_;
1191 local($yes) = '^(yes
|YES
|y
|Y
)$';
1192 local($no) = '^(no|NO
|n
|N
)$';
1195 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1200 print "$message (y/n) [$confirm]: ";
1201 chop($read = <STDIN>);
1204 return 1 unless $read;
1206 if (($confirm eq "y" && $read =~ "$yes") ||
1207 ($confirm eq "n" && $read =~ "$no")) {
1211 if ($read !~ "$yes" && $read !~ "$no") {
1212 warn "Wrong value. Enter again!\a\n";
1213 return &confirm_yn($message, $confirm);
1218 # allow configuring usernameregexp
1219 sub usernameregexp_default {
1220 local($r) = $usernameregexp;
1223 $r = &confirm_list("Usernames must match regular expression:", 1,
1225 eval "'foo
' =~ /$r/";
1227 warn "Invalid regular expression\a\n";
1229 $changes++ if $r ne $usernameregexp;
1233 # test if $dotdir exist
1234 # return "no" if $dotdir not exist or dotfiles should not copied
1235 sub dotdir_default {
1236 local($dir) = $dotdir;
1238 return &dotdir_default_valid($dir) unless $verbose;
1240 $dir = &confirm_list("Copy dotfiles from:", 1,
1241 $dir, ("no", $dotdir_bak, $dir));
1242 last if $dir eq &dotdir_default_valid($dir);
1244 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1246 $changes++ if $dir ne $dotdir;
1250 sub dotdir_default_valid {
1253 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1254 return $dir if $dir eq "no";
1255 warn "Dotdir ``$dir'' is not a directory\a\n";
1259 # ask for messages to new users
1260 sub message_default {
1261 local($file) = $send_message;
1262 local(@d) = ($file, $send_message_bak, "no");
1265 $file = &confirm_list("Send message from file:", 1, $file, @d);
1266 last if $file eq "no";
1267 last if &filetest($file, 1);
1269 # maybe create message file
1270 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1271 last if &filetest($file, 0);
1272 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1276 if ($file eq "no" || !&filetest($file, 0)) {
1277 warn "Do not send message\n" if $verbose;
1280 &message_read($file);
1283 $changes++ if $file ne $send_message && $verbose;
1287 # create message file
1288 sub message_create {
1291 rename($file, "$file.bak");
1292 if (!open(M, "> $file")) {
1293 warn "Messagefile ``$file'': $!\n"; return 0;
1297 # Message file for adduser(8)
1299 # default variables: \$name, \$fullname, \$password
1300 # other variables: see /etc/adduser.conf after
1301 # line ``$do_not_delete''
1306 your account ``\$name'' was created.
1309 See also chpass(1), finger(1), passwd(1)
1315 # read message file into buffer
1318 @message_buffer = '';
1320 if (!open(R
, "$file")) {
1321 warn "File ``$file'':$!\n"; return 0;
1324 push(@message_buffer, $_) unless /^\s*#/;
1329 # write @list to $file with file-locking
1331 local($file,@list) = @_;
1333 local($LOCK_EX) = 2;
1334 local($LOCK_NB) = 4;
1335 local($LOCK_UN) = 8;
1337 open(F
, ">> $file") || die "$file: $!\n";
1338 print "Lock $file.\n" if $verbose > 1;
1339 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1340 warn "Cannot lock file: $file\a\n";
1341 die "Sorry, give up\n"
1342 unless &confirm_yn
("Try again?", "yes");
1344 print F
join("\n", @list) . "\n";
1346 print "Unlock $file.\n" if $verbose > 1;
1350 # return free uid+gid
1351 # uid == gid if possible
1355 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1356 # looking for next free uid
1357 while($uid{$uid_start}) {
1359 $uid_start = 1000 if $uid_start >= $uid_end;
1360 print "$uid_start\n" if $verbose > 1;
1363 local($gid_start) = $uid_start;
1364 # group for user (username==groupname) already exist
1365 if ($groupname{$group}) {
1366 $gid_start = $groupname{$group};
1368 # gid is in use, looking for another gid.
1369 # Note: uid an gid are not equal
1370 elsif ($gid{$uid_start}) {
1371 while($gid{$gid_start} || $uid{$gid_start}) {
1373 $gid_start = $uid_end if $gid_start < 100;
1376 return ($uid_start, $gid_start);
1382 local($user_flag) = 0;
1384 # don't read config file
1385 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1387 if(!open(C
, "$config")) {
1388 warn "$config: $!\n"; return 0;
1392 # user defined variables
1393 /^$do_not_delete/ && $user_flag++;
1394 # found @array or $variable
1395 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1399 # lines with '^##' are not saved
1400 push(@user_variable_list, $_)
1401 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1403 #warn "X @user_variable_list X\n";
1410 local($silent) = @_;
1413 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1417 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1420 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1424 rename($config, "$config.bak");
1425 open(C
, "> $config") || die "$config: $!\n";
1427 # prepare some variables
1428 $send_message = "no" unless $send_message;
1429 $defaultusepassword = "no" unless $defaultusepassword;
1430 $defaultenableaccount = "yes" unless $defaultenableaccount;
1431 $defaultemptypassword = "no" unless $defaultemptypassword;
1432 local($shpref) = "'" . join("', '", @shellpref) . "'";
1433 local($shpath) = "'" . join("', '", @path) . "'";
1434 local($user_var) = join('', @user_variable_list);
1438 # $config - automatic generated by adduser(8)
1440 # Note: adduser read *and* write this file.
1441 # You may change values, but don't add new things before the
1442 # line ``$do_not_delete''
1448 # regular expression usernames are checked against (see perlre(1))
1449 # usernameregexp = 'regexp'
1450 usernameregexp = '$usernameregexp'
1452 # use password-based authentication for new users
1453 # defaultusepassword = "yes" | "no"
1454 defaultusepassword = "$defaultusepassword"
1456 # enable account password at creation
1457 # (the password will be prepended with a star if the account isn't enabled)
1458 # defaultenableaccount = "yes" | "no"
1459 defaultenableaccount = "$defaultenableaccount"
1461 # allow blank passwords
1462 # defaultemptypassword = "yes" | "no"
1463 defaultemptypassword = "$defaultemptypassword"
1465 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1468 # send this file to new user ("/etc/adduser.message" or "no")
1469 send_message = "$send_message"
1471 # config file for adduser ("/etc/adduser.conf")
1474 # logfile ("/var/log/adduser" or "no")
1475 logfile = "$logfile"
1477 # default HOME directory ("/home")
1480 # List of directories where shells located
1481 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1484 # common shell list, first element has higher priority
1485 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1486 shellpref = ($shpref)
1488 # defaultshell if not empty ("bash")
1489 defaultshell = "$defaultshell"
1491 # defaultgroup ('USER' for same as username or any other valid group)
1492 defaultgroup = $defaultgroup
1494 # defaultclass if not empty
1495 defaultclass = "$defaultclass"
1497 # new users get this uid (1000)
1498 uid_start = "$uid_start"
1501 ## your own variables, see /etc/adduser.message
1512 $test = 0; # test mode, only for development
1515 &check_root
; # you must be root to run this script!
1516 &variables
; # initialize variables
1517 &config_read
(@ARGV); # read variables form config-file
1518 &parse_arguments
(@ARGV); # parse arguments
1526 &passwd_check
; # check for valid passwdb
1527 &shells_read
; # read /etc/shells
1528 &passwd_read
; # read /etc/master.passwd
1529 &group_read
; # read /etc/group
1530 &group_check
; # check for incon*
1531 exit 0 if $check_only; # only check consistence and exit
1536 $usernameregexp = &usernameregexp_default
; # regexp to check usernames against
1537 &shells_add
; # maybe add some new shells
1538 $defaultshell = &shell_default
; # enter default shell
1539 $home = &home_partition
($home); # find HOME partition
1540 $dotdir = &dotdir_default
; # check $dotdir
1541 $send_message = &message_default
; # send message to new user
1542 $defaultusepassword = &password_default
; # maybe use password
1543 if ($defaultusepassword eq "no") {
1545 print "Creating accounts with a locked password.\n";
1547 $defaultenableaccount = "no";
1548 $defaultemptypassword = "yes";
1550 $defaultenableaccount = &enable_account_default
; # enable or disable account
1551 $defaultemptypassword = &enable_empty_password
; # use empty password or not
1553 &config_write
(!$verbose); # write variables in file
1555 # main loop for creating new users
1556 &new_users
; # add new users