]>
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 if (length($name) > 16) {
321 warn "Username is longer than 16 chars\a\n";
324 last if (&new_users_name_valid
($name));
329 sub new_users_name_valid
{
332 if ($name eq $usernameregexp) { # user/admin just pressed <Return>
333 warn "Please enter a username\a\n";
335 } elsif ($name =~ /[:\n]/) {
336 warn "Username cannot contain colon or newline characters.\a\n";
338 } elsif ($name !~ /$usernameregexp/) {
339 if ($usernameregexp eq $defaultusernameregexp) {
340 warn "Illegal username.\n" .
341 "Please use only lowercase Roman, decimal, underscore, " .
342 "or hyphen characters.\n" .
343 "Additionally, a username should not start with a hyphen.\a\n";
345 warn "Username doesn't match the regexp /$usernameregexp/\a\n";
348 } elsif ($username{$name}) {
349 warn "Username ``$name'' already exists!\a\n"; return 0;
355 sub new_users_fullname
{
360 $fullname = &confirm_list
("Enter full name", 1, "", "");
361 last if $fullname eq &new_users_fullname_valid
($fullname);
363 $fullname = $name unless $fullname;
367 sub new_users_fullname_valid
{
368 local($fullname) = @_;
370 return $fullname if $fullname !~ /:/;
372 warn "``:'' is not allowed!\a\n";
376 # return shell (full path) for user
377 sub new_users_shell
{
380 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
384 # return home (full path) for user
385 # Note that the home path defaults to $home/$name for batch
391 $userhome = &confirm_list
("Enter home directory (full path)", 1, "$home/$name", "");
392 last if $userhome =~ /^\//;
393 warn qq{Home directory "$userhome" is not a full path\a\n};
398 # return free uid and gid
401 local($u_id, $g_id) = &next_id
($name);
402 local($u_id_tmp, $e);
405 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
406 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
408 if ($uid{$u_id_tmp}) {
409 warn "Uid ``$u_id_tmp'' in use!\a\n";
410 $uid_start = $u_id_tmp;
411 ($u_id, $g_id) = &next_id
($name);
414 warn "Wrong uid.\a\n";
418 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
420 $uid_start = $u_id_tmp;
421 return &next_id
($name);
424 # return login class for user
425 sub new_users_class
{
429 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
430 $class = "" if $class eq "default";
436 local($gid, $name) = @_;
439 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
441 $groupmembers_bak{$gid} = $groupmembers{$gid};
442 $groupmembers{$gid} .= "," if $groupmembers{$gid};
443 $groupmembers{$gid} .= "$name";
450 sub new_users_grplogin
{
451 local($name, $defaultgroup, $new_users_ok) = @_;
452 local($group_login, $group);
455 $group = $defaultgroup if $defaultgroup ne $group_uniq;
459 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
461 # restore old groupmembers, user was not accept
462 foreach $e (keys %groupmembers_bak) {
463 $groupmembers{$e} = $groupmembers_bak{$e};
468 $group_login = &confirm_list
("Login group", 1, $group,
470 last if $group_login eq $group;
471 last if $group_login eq $name;
472 last if defined $groupname{$group_login};
473 if ($group_login eq $group_uniq) {
474 $group_login = $name; last;
477 if (defined $gid{$group_login}) {
478 # convert numeric groupname (gid) to groupname
479 $group_login = $gid{$group_login};
482 warn "Group does not exist!\a\n";
485 #if (defined($groupname{$group_login})) {
486 # &add_group($groupname{$group_login}, $name);
489 return ($group_login, $group_uniq) if $group_login eq $name;
490 return ($group_login, $group_login);
493 # return other groups (string)
494 sub new_users_groups
{
495 local($name, $other_groups) = @_;
497 "Login group is ``$group_login''. Invite $name into other groups:";
499 local($new_groups,$groups);
501 $other_groups = "no" unless $other_groups;
504 $groups = &confirm_list
($string, 1, $other_groups,
505 ("no", $other_groups, "guest"));
507 return "" if $groups eq "no";
509 ($flag, $new_groups) = &new_users_groups_valid
($groups);
512 $new_groups =~ s/\s*$//;
516 sub new_users_groups_valid
{
518 local($e, $new_groups);
521 foreach $e (split(/[,\s]+/, $groups)) {
522 # convert numbers to groupname
523 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
526 if (defined($groupname{$e})) {
527 if ($e eq $group_login) {
528 # do not add user to a group if this group
529 # is also the login group.
530 } elsif (&add_group
($groupname{$e}, $name)) {
531 $new_groups .= "$e ";
533 warn "$name is already member of group ``$e''\n";
536 warn "Group ``$e'' does not exist\a\n"; $flag++;
539 return ($flag, $new_groups);
545 # Note that we either show "password disabled" or
546 # "****" .. we don't show "empty password" since
547 # the whole point of starring out the password in
548 # the first place is to stop people looking over your
549 # shoulder and seeing the password.. -- adrian
550 if ($usepassword eq "no") {
551 $newpasswd = "Password disabled";
552 } elsif ($enableaccount eq "no") {
553 $newpasswd = "Password disabled";
564 Gid: $g_id ($group_login)
566 Groups: $group_login $new_groups
571 return &confirm_yn
("OK?", "yes");
574 # make password database
575 sub new_users_pwdmkdb
{
576 local($last) = shift;
577 local($name) = shift;
579 system(@pwd_mkdb, '-u', $name, $etc_passwd);
582 warn "``@pwd_mkdb'' failed\n";
587 # update group database
588 sub new_users_group_update
{
592 if (!defined($groupname{$group_login}) &&
593 !defined($gid{$groupname{$group_login}})) {
594 push(@group_backup, "$group_login:*:$g_id:");
595 $groupname{$group_login} = $g_id;
596 $gid{$g_id} = $group_login;
597 # $groupmembers{$g_id} = $group_login;
600 if ($new_groups || defined($groupname{$group_login}) ||
601 defined($gid{$groupname{$group_login}}) &&
602 $gid{$groupname{$group_login}} ne "+") {
603 # new user is member of some groups
604 # new login group is already in name space
605 rename($group, "$group.bak");
606 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
608 # Restore comments from the top of the group file
609 @a = @group_comments;
610 foreach $e (sort {$a <=> $b} (keys %gid)) {
611 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
613 &append_file
($group, @a);
615 &append_file
($group, "$group_login:*:$g_id:");
620 sub new_users_passwd_update
{
621 # update passwd/group variables
622 push(@passwd_backup, $new_entry);
623 $username{$name} = $u_id;
625 $pwgid{$g_id} = $name;
628 # send message to new user
629 sub new_users_sendmessage
{
630 return 1 if $send_message eq "no";
633 &confirm_list
("Send message to ``$name'' and:",
634 1, "no", ("root", "second_mail_address", "no"));
636 $cc = "" if $cc eq "no";
638 foreach $e (@message_buffer) {
643 local(@message_buffer_append) = ();
644 if (!&confirm_yn
("Add anything to default message", "no")) {
645 print "Use ``.'' or ^D alone on a line to finish your message.\n";
646 push(@message_buffer_append, "\n");
647 while($read = <STDIN
>) {
648 last if $read eq "\.\n";
649 push(@message_buffer_append, $read);
653 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
654 if (&confirm_yn
("Send message", "yes"));
658 local($to, @message) = @_;
661 if (!open(M
, "| mail -s Welcome $to")) {
662 warn "Cannot send mail to: $to!\n";
665 foreach $e (@message) {
666 print M
eval "\"$e\"";
673 sub new_users_password
{
678 system("stty -echo");
679 $password = &confirm_list
("Enter password", 1, "", "");
682 if ($password ne "") {
683 system("stty -echo");
684 $newpass = &confirm_list
("Enter password again", 1, "", "");
687 last if $password eq $newpass;
688 print "They didn't match, please try again\n";
690 elsif (&confirm_yn
("Use an empty password?", "yes")) {
698 sub new_users_use_password
{
699 local ($p) = $defaultusepassword;
700 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
701 return "yes" if (($defaultusepassword eq "yes" && $p) ||
702 ($defaultusepassword eq "no" && !$p));
703 return "no"; # otherwise
706 sub new_users_enable_account
{
707 local ($p) = $defaultenableaccount;
708 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
709 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
710 ($defaultenableaccount eq "no" && !$p));
711 return "no"; # otherwise
714 sub new_users_empty_password
{
715 local ($p) = $defaultemptypassword;
716 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
717 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
718 ($defaultemptypassword eq "no" && !$p));
719 return "no"; # otherwise
724 print "\n" if $verbose;
725 print "Ok, let's go.\n" .
726 "Don't worry about mistakes. I will give you the chance later to " .
727 "correct any input.\n" if $verbose;
730 # fullname: Full name
732 # userhome: home path for user
736 # group_login: groupname of g_id
737 # new_groups: some other groups
738 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
740 local($groupmembers_bak, $cryptpwd);
741 local($new_users_ok) = 1;
745 $new_groups = "no" unless $groupname{$new_groups};
748 $name = &new_users_name
;
749 $fullname = &new_users_fullname
($name);
750 $sh = &new_users_shell
;
751 $userhome = &new_users_home
($name);
752 ($u_id, $g_id) = &new_users_id
($name);
753 $class = &new_users_class
($defaultclass);
754 ($group_login, $defaultgroup) =
755 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
756 # do not use uniq username and login group
757 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
761 # If $usepasswd is 0, we use a * as a password
762 # If $usepasswd is 1, then
763 # if $enableaccount is 0, we prepend * as a password
764 # else if $enableaccount is 1 we don't prepend anything
765 # if $useemptypassword is 0 we ask for a password,
766 # else we use a blank one
768 # The logic is tasty, I'll give you that, but its flexible and
769 # it'll stop people shooting themselves in the foot.
771 $new_groups = &new_users_groups
($name, $new_groups);
773 $usepassword = &new_users_use_password
;
774 if ($usepassword eq "no") {
775 # note that the assignments to enableaccount and
776 # useemptypassword functionally do the same as
777 # usepasswd == "no". Just for consistency.
778 $password = ""; # no password!
779 $enableaccount = "no"; # doesn't matter here
780 $useemptypassword = "yes"; # doesn't matter here
782 $useemptypassword = &new_users_empty_password
;
783 if ($useemptypassword eq "no") {
784 $password = &new_users_password
;
786 $enableaccount = &new_users_enable_account
;
793 $cryptpwd = crypt($password, &salt
) if $password ne "";
795 if ($usepassword eq "no") {
798 # cryptpwd is valid before this if mess, so if
799 # blankpasswd is no we don't blank the cryptpwd
800 if ($useemptypassword eq "yes") {
803 if ($enableaccount eq "no") {
804 $cryptpwd = "*" . $cryptpwd;
808 $new_entry = "$name\:" . "$cryptpwd" .
809 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
810 &append_file
($etc_passwd, "$new_entry");
811 &new_users_pwdmkdb
("$new_entry", $name);
812 &new_users_group_update
;
813 &new_users_passwd_update
; print "Added user ``$name''\n";
814 &new_users_sendmessage
;
815 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
816 &home_create
($userhome, $name, $group_login);
820 if (!&confirm_yn
("Add another user?", "yes")) {
821 print "Goodbye!\n" if $verbose;
824 print "\n" if !$verbose;
828 # ask for password usage
829 sub password_default
{
830 local($p) = $defaultusepassword;
832 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
833 $changes++ unless $p;
835 return "yes" if (($defaultusepassword eq "yes" && $p) ||
836 ($defaultusepassword eq "no" && !$p));
837 return "no"; # otherwise
840 # ask for account enable usage
841 sub enable_account_default
{
842 local ($p) = $defaultenableaccount;
844 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
845 $changes++ unless $p;
847 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
848 ($defaultenableaccount eq "no" && !$p));
849 return "no"; # otherwise
852 # ask for empty password
853 sub enable_empty_password
{
854 local ($p) = $defaultemptypassword;
856 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
857 $changes++ unless $p;
859 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
860 ($defaultemptypassword eq "no" && !$p));
861 return "no"; # otherwise
866 die "You are not root!\n" if $< && !$test;
879 [-message message_file]
882 [-s|-silent|-q|-quiet]
886 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
887 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
895 local($e, $last, @array);
897 foreach $e (sort @list) {
898 push(@array, $e) unless $e eq $last;
904 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
906 local($salt); # initialization
908 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
910 warn "calculate salt\n" if $verbose > 1;
912 for ($i = 0; $i < 27; $i++) {
913 srand(time + $rand + $$);
914 $rand = rand(25*29*17 + $rand);
915 $salt .= $itoa64[$rand & $#itoa64];
917 warn "Salt is: $salt\n" if $verbose > 1;
931 print "Use option ``-silent'' if you don't want to see " .
932 "all warnings and questions.\n\n";
934 print "Use option ``-verbose'' if you want to see more warnings and " .
935 "questions \nor try to repair bugs.\n\n";
940 sub parse_arguments
{
943 while ($_ = $argv[0], /^-/) {
946 if (/^--?(v|verbose)$/) { $verbose = 1 }
947 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
948 elsif (/^--?(debug)$/) { $verbose = 2 }
949 elsif (/^--?(h|help|\?)$/) { &usage
}
950 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
951 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
952 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
953 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
954 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
955 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
956 elsif (/^--?(check_only)$/) { $check_only = 1 }
957 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
959 elsif (/^--?(batch)$/) {
960 warn "The -batch option is not supported anymore.\n",
961 "Please use the pw(8) command line tool!\n";
965 elsif (/^--?(config_create)$/) { &create_conf
; }
966 elsif (/^--?(noconfig)$/) { $config_read = 0; }
969 #&usage if $#argv < 0;
981 $name = &stripdir
($name);
982 $name =~ s
|/+[^/]+$||;
983 $name = "/" unless $name; # dirname of / is /
987 # return 1 if $file is a readable file or link
989 local($file, $verb) = @_;
992 if (-f
$file || -l
$file) {
994 warn "$file unreadable\n" if $verbose;
996 warn "$file is not a plain file or link\n" if $verbose;
1002 # create configuration files and exit
1005 if ($send_message ne 'no') {
1006 &message_create
($send_message);
1008 &message_create
($send_message_bak);
1014 # log for new user in /var/log/adduser
1016 local($string) = @_;
1019 return 1 if $logfile eq "no";
1021 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1025 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
1027 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1030 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1033 # create HOME directory, copy dotfiles from $dotdir to $HOME
1035 local($homedir, $name, $group) = @_;
1038 if (-e
"$homedir") {
1039 warn "HOME Directory ``$homedir'' already exist\a\n";
1043 # if the home directory prefix doesn't exist, create it
1044 # First, split the directory into a list; then remove the user's dir
1045 @dir = split('/', $homedir); pop(@dir);
1046 # Put back together & strip to get directory prefix
1047 $rootdir = &stripdir
(join('/', @dir));
1049 if (!&mkdirhier
("$rootdir")) {
1050 # warn already displayed
1054 if ($dotdir eq 'no') {
1055 if (!mkdir("$homedir", 0755)) {
1056 warn "$dir: $!\n"; return 0;
1058 system 'chown', "$name:$group", $homedir;
1062 # copy files from $dotdir to $homedir
1063 # rename 'dot.foo' files to '.foo'
1064 print "Copy files from $dotdir to $homedir\n" if $verbose;
1065 system('cp', '-R', $dotdir, $homedir);
1066 system('chmod', '-R', 'u+wrX,go-w', $homedir);
1067 system('chown', '-Rh', "$name:$group", $homedir);
1070 opendir(D
, $homedir);
1071 foreach $file (readdir(D
)) {
1072 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
1073 $file =~ s/^dot\././;
1074 rename("$homedir/dot$file", "$homedir/$file");
1076 chmod(0600, "$homedir/$file")
1077 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1078 chmod(0700, "$homedir/$file")
1079 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1085 # makes a directory hierarchy
1088 $dir = &stripdir
($dir);
1089 local($user_partition) = "/usr";
1090 local($dirname) = &dirname
($dir);
1092 -e
$dirname || &mkdirhier
($dirname);
1094 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1095 # home partition is on root partition
1096 # create home partition on $user_partition and make
1097 # a symlink from $dir to $user_partition/`basename $dir`
1098 # For instance: /home -> /usr/home
1100 local($basename) = &basename
($dir);
1101 local($d) = "$user_partition/$basename";
1105 warn "Oops, $d already exist\n" if $verbose;
1107 print "Create $d\n" if $verbose;
1108 if (!mkdir("$d", 0755)) {
1109 warn "$d: $!\a\n"; return 0;
1113 unlink($dir); # symlink to nonexist file
1114 print "Create symlink: $dir -> $d\n" if $verbose;
1115 if (!symlink("$d", $dir)) {
1116 warn "Symlink $d: $!\a\n"; return 0;
1119 print "Create $dir\n" if $verbose;
1120 if (!mkdir("$dir", 0755)) {
1121 warn "Directory ``$dir'': $!\a\n"; return 0;
1131 $dir = &stripdir
($dir);
1133 foreach $d (split('/', $dir)) {
1137 print "Create $dir\n" if $verbose;
1138 if (!mkdir("$dir", 0755)) {
1139 warn "$dir: $!\n"; return 0;
1148 # F.i.: //usr///home// -> /usr/home
1152 $dir =~ s
|/+|/|g
; # delete double '/'
1153 $dir =~ s
|/$||; # delete '/' at end
1154 return $dir if $dir ne "";
1158 # Read one of the elements from @list. $confirm is default.
1159 # If !$allow accept only elements from @list.
1161 local($message, $allow, $confirm, @list) = @_;
1162 local($read, $c, $print);
1164 $print = "$message" if $message;
1165 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1167 $print .= join($", &uniq(@list)); #"
1168 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1170 print "\n" if (length($print) + length($confirm)) > 60;
1171 print "[$confirm]: ";
1173 chop($read = <STDIN>);
1176 return $confirm if $read eq "";
1177 return "$read" if $allow;
1179 foreach $c (@list) {
1180 return $read if $c eq $read;
1182 warn "$read: is not allowed!\a\n";
1183 return &confirm_list($message, $allow, $confirm, @list);
1186 # YES or NO question
1187 # return 1 if &confirm("message", "yes") and answer is yes
1188 # or if &confirm("message", "no") an answer is no
1191 local($message, $confirm) = @_;
1192 local($yes) = '^(yes
|YES
|y
|Y
)$';
1193 local($no) = '^(no|NO
|n
|N
)$';
1196 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1201 print "$message (y/n) [$confirm]: ";
1202 chop($read = <STDIN>);
1205 return 1 unless $read;
1207 if (($confirm eq "y" && $read =~ "$yes") ||
1208 ($confirm eq "n" && $read =~ "$no")) {
1212 if ($read !~ "$yes" && $read !~ "$no") {
1213 warn "Wrong value. Enter again!\a\n";
1214 return &confirm_yn($message, $confirm);
1219 # allow configuring usernameregexp
1220 sub usernameregexp_default {
1221 local($r) = $usernameregexp;
1224 $r = &confirm_list("Usernames must match regular expression:", 1,
1226 eval "'foo
' =~ /$r/";
1228 warn "Invalid regular expression\a\n";
1230 $changes++ if $r ne $usernameregexp;
1234 # test if $dotdir exist
1235 # return "no" if $dotdir not exist or dotfiles should not copied
1236 sub dotdir_default {
1237 local($dir) = $dotdir;
1239 return &dotdir_default_valid($dir) unless $verbose;
1241 $dir = &confirm_list("Copy dotfiles from:", 1,
1242 $dir, ("no", $dotdir_bak, $dir));
1243 last if $dir eq &dotdir_default_valid($dir);
1245 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1247 $changes++ if $dir ne $dotdir;
1251 sub dotdir_default_valid {
1254 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1255 return $dir if $dir eq "no";
1256 warn "Dotdir ``$dir'' is not a directory\a\n";
1260 # ask for messages to new users
1261 sub message_default {
1262 local($file) = $send_message;
1263 local(@d) = ($file, $send_message_bak, "no");
1266 $file = &confirm_list("Send message from file:", 1, $file, @d);
1267 last if $file eq "no";
1268 last if &filetest($file, 1);
1270 # maybe create message file
1271 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1272 last if &filetest($file, 0);
1273 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1277 if ($file eq "no" || !&filetest($file, 0)) {
1278 warn "Do not send message\n" if $verbose;
1281 &message_read($file);
1284 $changes++ if $file ne $send_message && $verbose;
1288 # create message file
1289 sub message_create {
1292 rename($file, "$file.bak");
1293 if (!open(M, "> $file")) {
1294 warn "Messagefile ``$file'': $!\n"; return 0;
1298 # Message file for adduser(8)
1300 # default variables: \$name, \$fullname, \$password
1301 # other variables: see /etc/adduser.conf after
1302 # line ``$do_not_delete''
1307 your account ``\$name'' was created.
1310 See also chpass(1), finger(1), passwd(1)
1316 # read message file into buffer
1319 @message_buffer = '';
1321 if (!open(R
, "$file")) {
1322 warn "File ``$file'':$!\n"; return 0;
1325 push(@message_buffer, $_) unless /^\s*#/;
1330 # write @list to $file with file-locking
1332 local($file,@list) = @_;
1334 local($LOCK_EX) = 2;
1335 local($LOCK_NB) = 4;
1336 local($LOCK_UN) = 8;
1338 open(F
, ">> $file") || die "$file: $!\n";
1339 print "Lock $file.\n" if $verbose > 1;
1340 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1341 warn "Cannot lock file: $file\a\n";
1342 die "Sorry, give up\n"
1343 unless &confirm_yn
("Try again?", "yes");
1345 print F
join("\n", @list) . "\n";
1347 print "Unlock $file.\n" if $verbose > 1;
1351 # return free uid+gid
1352 # uid == gid if possible
1356 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1357 # looking for next free uid
1358 while($uid{$uid_start}) {
1360 $uid_start = 1000 if $uid_start >= $uid_end;
1361 print "$uid_start\n" if $verbose > 1;
1364 local($gid_start) = $uid_start;
1365 # group for user (username==groupname) already exist
1366 if ($groupname{$group}) {
1367 $gid_start = $groupname{$group};
1369 # gid is in use, looking for another gid.
1370 # Note: uid an gid are not equal
1371 elsif ($gid{$uid_start}) {
1372 while($gid{$gid_start} || $uid{$gid_start}) {
1374 $gid_start = $uid_end if $gid_start < 100;
1377 return ($uid_start, $gid_start);
1383 local($user_flag) = 0;
1385 # don't read config file
1386 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1388 if(!open(C
, "$config")) {
1389 warn "$config: $!\n"; return 0;
1393 # user defined variables
1394 /^$do_not_delete/ && $user_flag++;
1395 # found @array or $variable
1396 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1400 # lines with '^##' are not saved
1401 push(@user_variable_list, $_)
1402 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1404 #warn "X @user_variable_list X\n";
1411 local($silent) = @_;
1414 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1418 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1421 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1425 rename($config, "$config.bak");
1426 open(C
, "> $config") || die "$config: $!\n";
1428 # prepare some variables
1429 $send_message = "no" unless $send_message;
1430 $defaultusepassword = "no" unless $defaultusepassword;
1431 $defaultenableaccount = "yes" unless $defaultenableaccount;
1432 $defaultemptypassword = "no" unless $defaultemptypassword;
1433 local($shpref) = "'" . join("', '", @shellpref) . "'";
1434 local($shpath) = "'" . join("', '", @path) . "'";
1435 local($user_var) = join('', @user_variable_list);
1439 # $config - automatic generated by adduser(8)
1441 # Note: adduser read *and* write this file.
1442 # You may change values, but don't add new things before the
1443 # line ``$do_not_delete''
1449 # regular expression usernames are checked against (see perlre(1))
1450 # usernameregexp = 'regexp'
1451 usernameregexp = '$usernameregexp'
1453 # use password-based authentication for new users
1454 # defaultusepassword = "yes" | "no"
1455 defaultusepassword = "$defaultusepassword"
1457 # enable account password at creation
1458 # (the password will be prepended with a star if the account isn't enabled)
1459 # defaultenableaccount = "yes" | "no"
1460 defaultenableaccount = "$defaultenableaccount"
1462 # allow blank passwords
1463 # defaultemptypassword = "yes" | "no"
1464 defaultemptypassword = "$defaultemptypassword"
1466 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1469 # send this file to new user ("/etc/adduser.message" or "no")
1470 send_message = "$send_message"
1472 # config file for adduser ("/etc/adduser.conf")
1475 # logfile ("/var/log/adduser" or "no")
1476 logfile = "$logfile"
1478 # default HOME directory ("/home")
1481 # List of directories where shells located
1482 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1485 # common shell list, first element has higher priority
1486 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1487 shellpref = ($shpref)
1489 # defaultshell if not empty ("bash")
1490 defaultshell = "$defaultshell"
1492 # defaultgroup ('USER' for same as username or any other valid group)
1493 defaultgroup = $defaultgroup
1495 # defaultclass if not empty
1496 defaultclass = "$defaultclass"
1498 # new users get this uid (1000)
1499 uid_start = "$uid_start"
1502 ## your own variables, see /etc/adduser.message
1513 $test = 0; # test mode, only for development
1516 &check_root
; # you must be root to run this script!
1517 &variables
; # initialize variables
1518 &config_read
(@ARGV); # read variables form config-file
1519 &parse_arguments
(@ARGV); # parse arguments
1527 &passwd_check
; # check for valid passwdb
1528 &shells_read
; # read /etc/shells
1529 &passwd_read
; # read /etc/master.passwd
1530 &group_read
; # read /etc/group
1531 &group_check
; # check for incon*
1532 exit 0 if $check_only; # only check consistence and exit
1537 $usernameregexp = &usernameregexp_default
; # regexp to check usernames against
1538 &shells_add
; # maybe add some new shells
1539 $defaultshell = &shell_default
; # enter default shell
1540 $home = &home_partition
($home); # find HOME partition
1541 $dotdir = &dotdir_default
; # check $dotdir
1542 $send_message = &message_default
; # send message to new user
1543 $defaultusepassword = &password_default
; # maybe use password
1544 if ($defaultusepassword eq "no") {
1546 print "Creating accounts with a locked password.\n";
1548 $defaultenableaccount = "no";
1549 $defaultemptypassword = "yes";
1551 $defaultenableaccount = &enable_account_default
; # enable or disable account
1552 $defaultemptypassword = &enable_empty_password
; # use empty password or not
1554 &config_write
(!$verbose); # write variables in file
1556 # main loop for creating new users
1557 &new_users
; # add new users