]>
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 $force = 0; # relax username validity check if true
34 $defaultusepassword = "yes"; # use password authentication for new users
35 $defaultenableaccount = "yes"; # enable the account by default
36 $defaultemptypassword = "no"; # don't create an empty password
37 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
38 $dotdir_bak = $dotdir;
39 $send_message = "/etc/adduser.message"; # send message to new user
40 $send_message_bak = '/etc/adduser.message';
41 $config = "/etc/adduser.conf"; # config file for adduser
42 $config_read = 1; # read config file
43 $logfile = "/var/log/adduser"; # logfile
44 $home = "/home"; # default HOME
45 $etc_shells = "/etc/shells";
46 $etc_passwd = "/etc/master.passwd";
47 $group = "/etc/group";
48 @pwd_mkdb = qw(pwd_mkdb -p); # program for building passwd database
51 # List of directories where shells located
52 @path = ('/bin', '/usr/bin', '/usr/local/bin');
53 # common shells, first element has higher priority
54 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
56 $defaultshell = 'sh'; # defaultshell if not empty
58 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
61 $uid_start = 1000; # new users get this uid
62 $uid_end = 32000; # max. uid
66 $username = ''; # $username{username} = uid
67 $uid = ''; # $uid{uid} = username
68 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
70 $password = ''; # password for new users
71 $usepassword = ''; # use password-based auth
72 $useemptypassword = ''; # use an empty password
73 $enableaccount = ''; # enable or disable account password at creation
76 $groupname =''; # $groupname{groupname} = gid
77 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
78 $gid = ''; # $gid{gid} = groupname; gid form group db
79 @group_comments; # Comments in the group file
82 $shell = ''; # $shell{`basename sh`} = sh
84 umask 022; # don't give login group write access
86 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
90 @user_variable_list = ''; # user variables in /etc/adduser.conf
91 $do_not_delete = '## DO NOT DELETE THIS LINE!';
94 # read shell database, see also: shells(5)
99 print "Check $etc_shells\n" if $verbose;
100 open(S
, $etc_shells) || die "$etc_shells:$!\n";
104 s/^\s*//; s/\s+.*//; # chop
107 $shell{&basename
($sh)} = $sh;
109 warn "Shell: $sh not executable!\n";
115 # Allow /nonexistent and /bin/date as a valid shell for system utils
116 push(@list, "/nonexistent");
117 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
118 $shell{"no"} = "/nonexistent";
120 push(@list, "/bin/date");
121 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
122 $shell{"date"} = "/bin/date";
127 # add new shells if possible
129 local($sh,$dir,@list);
131 return 1 unless $verbose;
133 foreach $sh (@shellpref) {
136 # shell $sh is not defined as login shell
137 foreach $dir (@path) {
140 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
141 push(@list, "$dir/$sh");
142 push(@shellpref, "$sh");
143 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
150 &append_file
($etc_shells, @list) if $#list >= 0;
153 # choose your favourite shell and return the shell
155 local($e,$i,$new_shell);
158 $sh = &shell_default_valid
($defaultshell);
159 return $sh unless $verbose;
161 $new_shell = &confirm_list
("Enter your default shell:", 0,
162 $sh, sort(keys %shell));
163 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
164 $changes++ if $new_shell ne $sh;
168 sub shell_default_valid
{
172 return $sh if $shell{$sh};
174 foreach $e (@shellpref) {
176 last if defined($shell{$s});
179 warn "Shell ``$sh'' is undefined, use ``$s''\n";
183 # return default home partition (e.g. "/home")
184 # create base directory if nesseccary
187 $home = &stripdir
($home);
190 return $h if !$verbose && $h eq &home_partition_valid
($h);
193 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
195 last if $h eq &home_partition_valid
($h);
198 $changes++ if $h ne $home;
202 sub home_partition_valid
{
207 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
211 warn "Please use absolute path for home: ``$h''.\a\n";
216 warn "$h exists, but is not a directory or symlink!\n"
217 unless -d
$h || -l
$h;
218 warn "$h is not writable!\n"
222 # create home partition
223 return $h if &mkdir_home
($h);
228 # check for valid passwddb
230 system(@pwd_mkdb, '-C', $etc_passwd);
231 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
236 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
238 print "Check $etc_passwd\n" if $verbose;
239 open(P
, "$etc_passwd") || die "$etc_passwd: $!\n";
243 push(@passwd_backup, $_);
248 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
250 print "$p_username already exists with uid: $username{$p_username}!\n"
251 if $username{$p_username} && $verbose;
252 $username{$p_username} = $p_uid;
253 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
254 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
255 print "User $p_username: illegal shell: ``$sh''\n"
256 if ($verbose && $sh &&
257 !$shell{&basename
($sh)} &&
258 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
259 $sh !~ /\/(pppd
|sliplogin
|nologin
|nonexistent
)$/);
260 $uid{$p_uid} = $p_username;
261 $pwgid{$p_gid} = $p_username;
268 local($g_groupname,$pw,$g_gid, $memb);
270 print "Check $group\n" if $verbose;
271 open(G
, "$group") || die "$group: $!\n";
274 push(@group_backup, $_);
277 # Save comments to restore later
279 push(@group_comments, $_);
283 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
285 $groupmembers{$g_gid} = $memb;
286 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
287 if $groupname{$g_groupname} && $verbose;
288 $groupname{$g_groupname} = $g_gid;
289 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
290 if $gid{$g_gid} && $verbose;
291 $gid{$g_gid} = $g_groupname;
296 # check gids /etc/passwd <-> /etc/group
298 local($c_gid, $c_username, @list);
300 foreach $c_gid (keys %pwgid) {
302 $c_username = $pwgid{$c_gid};
303 warn "User ``$c_username'' has gid $c_gid but a group with this " .
304 "gid does not exist.\n" if $verbose;
310 # main loop for creating new users
318 $name = &confirm_list
("Enter username", 1, "a-z0-9_-", "");
319 if (length($name) > 16) {
320 warn "Username is longer than 16 chars\a\n";
323 last if (&new_users_name_valid
($name));
328 sub new_users_name_valid
{
332 if ($name eq "a-z0-9_-") {
333 warn "Please enter a username.\a\n";
336 if ($name =~ /[:\n]/) {
337 warn "Illegal username, which would break your passwd file.\a\n";
340 if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/) {
341 warn "Caution: Username contains illegal characters.\n" .
342 "Adding this user may cause utilities " .
343 "or applications to malfunction,\n" .
344 "or even impose a security risk on your system.\a\n";
346 } elsif ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/ || $name eq "a-z0-9_-") {
347 warn "Illegal username.\n" .
348 "Please use only lowercase Roman, decimal, underscore, " .
349 "or hyphen characters.\n" .
350 "Additionally, a username should not start with a hyphen.\a\n";
353 if ($username{$name}) {
354 warn "Username ``$name'' already exists!\a\n"; return 0;
360 sub new_users_fullname
{
365 $fullname = &confirm_list
("Enter full name", 1, "", "");
366 last if $fullname eq &new_users_fullname_valid
($fullname);
368 $fullname = $name unless $fullname;
372 sub new_users_fullname_valid
{
373 local($fullname) = @_;
375 return $fullname if $fullname !~ /:/;
377 warn "``:'' is not allowed!\a\n";
381 # return shell (full path) for user
382 sub new_users_shell
{
385 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
389 # return home (full path) for user
390 # Note that the home path defaults to $home/$name for batch
396 $userhome = &confirm_list
("Enter home directory (full path)", 1, "$home/$name", "");
397 last if $userhome =~ /^\//;
398 warn qq{Home directory "$userhome" is not a full path\a\n};
403 # return free uid and gid
406 local($u_id, $g_id) = &next_id
($name);
407 local($u_id_tmp, $e);
410 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
411 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
413 if ($uid{$u_id_tmp}) {
414 warn "Uid ``$u_id_tmp'' in use!\a\n";
415 $uid_start = $u_id_tmp;
416 ($u_id, $g_id) = &next_id
($name);
419 warn "Wrong uid.\a\n";
423 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
425 $uid_start = $u_id_tmp;
426 return &next_id
($name);
429 # return login class for user
430 sub new_users_class
{
434 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
435 $class = "" if $class eq "default";
441 local($gid, $name) = @_;
444 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
446 $groupmembers_bak{$gid} = $groupmembers{$gid};
447 $groupmembers{$gid} .= "," if $groupmembers{$gid};
448 $groupmembers{$gid} .= "$name";
455 sub new_users_grplogin
{
456 local($name, $defaultgroup, $new_users_ok) = @_;
457 local($group_login, $group);
460 $group = $defaultgroup if $defaultgroup ne $group_uniq;
464 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
466 # restore old groupmembers, user was not accept
467 foreach $e (keys %groupmembers_bak) {
468 $groupmembers{$e} = $groupmembers_bak{$e};
473 $group_login = &confirm_list
("Login group", 1, $group,
475 last if $group_login eq $group;
476 last if $group_login eq $name;
477 last if defined $groupname{$group_login};
478 if ($group_login eq $group_uniq) {
479 $group_login = $name; last;
482 if (defined $gid{$group_login}) {
483 # convert numeric groupname (gid) to groupname
484 $group_login = $gid{$group_login};
487 warn "Group does not exist!\a\n";
490 #if (defined($groupname{$group_login})) {
491 # &add_group($groupname{$group_login}, $name);
494 return ($group_login, $group_uniq) if $group_login eq $name;
495 return ($group_login, $group_login);
498 # return other groups (string)
499 sub new_users_groups
{
500 local($name, $other_groups) = @_;
502 "Login group is ``$group_login''. Invite $name into other groups:";
504 local($new_groups,$groups);
506 $other_groups = "no" unless $other_groups;
509 $groups = &confirm_list
($string, 1, $other_groups,
510 ("no", $other_groups, "guest"));
512 return "" if $groups eq "no";
514 ($flag, $new_groups) = &new_users_groups_valid
($groups);
517 $new_groups =~ s/\s*$//;
521 sub new_users_groups_valid
{
523 local($e, $new_groups);
526 foreach $e (split(/[,\s]+/, $groups)) {
527 # convert numbers to groupname
528 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
531 if (defined($groupname{$e})) {
532 if ($e eq $group_login) {
533 # do not add user to a group if this group
534 # is also the login group.
535 } elsif (&add_group
($groupname{$e}, $name)) {
536 $new_groups .= "$e ";
538 warn "$name is already member of group ``$e''\n";
541 warn "Group ``$e'' does not exist\a\n"; $flag++;
544 return ($flag, $new_groups);
550 # Note that we either show "password disabled" or
551 # "****" .. we don't show "empty password" since
552 # the whole point of starring out the password in
553 # the first place is to stop people looking over your
554 # shoulder and seeing the password.. -- adrian
555 if ($usepassword eq "no") {
556 $newpasswd = "Password disabled";
557 } elsif ($enableaccount eq "no") {
558 $newpasswd = "Password disabled";
569 Gid: $g_id ($group_login)
571 Groups: $group_login $new_groups
576 return &confirm_yn
("OK?", "yes");
579 # make password database
580 sub new_users_pwdmkdb
{
581 local($last) = shift;
582 local($name) = shift;
584 system(@pwd_mkdb, '-u', $name, $etc_passwd);
587 warn "``@pwd_mkdb'' failed\n";
592 # update group database
593 sub new_users_group_update
{
597 if (!defined($groupname{$group_login}) &&
598 !defined($gid{$groupname{$group_login}})) {
599 push(@group_backup, "$group_login:*:$g_id:");
600 $groupname{$group_login} = $g_id;
601 $gid{$g_id} = $group_login;
602 # $groupmembers{$g_id} = $group_login;
605 if ($new_groups || defined($groupname{$group_login}) ||
606 defined($gid{$groupname{$group_login}}) &&
607 $gid{$groupname{$group_login}} ne "+") {
608 # new user is member of some groups
609 # new login group is already in name space
610 rename($group, "$group.bak");
611 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
613 # Restore comments from the top of the group file
614 @a = @group_comments;
615 foreach $e (sort {$a <=> $b} (keys %gid)) {
616 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
618 &append_file
($group, @a);
620 &append_file
($group, "$group_login:*:$g_id:");
625 sub new_users_passwd_update
{
626 # update passwd/group variables
627 push(@passwd_backup, $new_entry);
628 $username{$name} = $u_id;
630 $pwgid{$g_id} = $name;
633 # send message to new user
634 sub new_users_sendmessage
{
635 return 1 if $send_message eq "no";
638 &confirm_list
("Send message to ``$name'' and:",
639 1, "no", ("root", "second_mail_address", "no"));
641 $cc = "" if $cc eq "no";
643 foreach $e (@message_buffer) {
648 local(@message_buffer_append) = ();
649 if (!&confirm_yn
("Add anything to default message", "no")) {
650 print "Use ``.'' or ^D alone on a line to finish your message.\n";
651 push(@message_buffer_append, "\n");
652 while($read = <STDIN
>) {
653 last if $read eq "\.\n";
654 push(@message_buffer_append, $read);
658 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
659 if (&confirm_yn
("Send message", "yes"));
663 local($to, @message) = @_;
666 if (!open(M
, "| mail -s Welcome $to")) {
667 warn "Cannot send mail to: $to!\n";
670 foreach $e (@message) {
671 print M
eval "\"$e\"";
678 sub new_users_password
{
683 system("stty -echo");
684 $password = &confirm_list
("Enter password", 1, "", "");
687 if ($password ne "") {
688 system("stty -echo");
689 $newpass = &confirm_list
("Enter password again", 1, "", "");
692 last if $password eq $newpass;
693 print "They didn't match, please try again\n";
695 elsif (&confirm_yn
("Use an empty password?", "yes")) {
703 sub new_users_use_password
{
704 local ($p) = $defaultusepassword;
705 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
706 return "yes" if (($defaultusepassword eq "yes" && $p) ||
707 ($defaultusepassword eq "no" && !$p));
708 return "no"; # otherwise
711 sub new_users_enable_account
{
712 local ($p) = $defaultenableaccount;
713 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
714 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
715 ($defaultenableaccount eq "no" && !$p));
716 return "no"; # otherwise
719 sub new_users_empty_password
{
720 local ($p) = $defaultemptypassword;
721 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
722 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
723 ($defaultemptypassword eq "no" && !$p));
724 return "no"; # otherwise
729 print "\n" if $verbose;
730 print "Ok, let's go.\n" .
731 "Don't worry about mistakes. I will give you the chance later to " .
732 "correct any input.\n" if $verbose;
735 # fullname: Full name
737 # userhome: home path for user
741 # group_login: groupname of g_id
742 # new_groups: some other groups
743 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
745 local($groupmembers_bak, $cryptpwd);
746 local($new_users_ok) = 1;
750 $new_groups = "no" unless $groupname{$new_groups};
753 $name = &new_users_name
;
754 $fullname = &new_users_fullname
($name);
755 $sh = &new_users_shell
;
756 $userhome = &new_users_home
($name);
757 ($u_id, $g_id) = &new_users_id
($name);
758 $class = &new_users_class
($defaultclass);
759 ($group_login, $defaultgroup) =
760 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
761 # do not use uniq username and login group
762 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
766 # If $usepasswd is 0, we use a * as a password
767 # If $usepasswd is 1, then
768 # if $enableaccount is 0, we prepend * as a password
769 # else if $enableaccount is 1 we don't prepend anything
770 # if $useemptypassword is 0 we ask for a password,
771 # else we use a blank one
773 # The logic is tasty, I'll give you that, but its flexible and
774 # it'll stop people shooting themselves in the foot.
776 $new_groups = &new_users_groups
($name, $new_groups);
778 $usepassword = &new_users_use_password
;
779 if ($usepassword eq "no") {
780 # note that the assignments to enableaccount and
781 # useemptypassword functionally do the same as
782 # usepasswd == "no". Just for consistency.
783 $password = ""; # no password!
784 $enableaccount = "no"; # doesn't matter here
785 $useemptypassword = "yes"; # doesn't matter here
787 $useemptypassword = &new_users_empty_password
;
788 if ($useemptypassword eq "no") {
789 $password = &new_users_password
;
791 $enableaccount = &new_users_enable_account
;
798 $cryptpwd = crypt($password, &salt
) if $password ne "";
800 if ($usepassword eq "no") {
803 # cryptpwd is valid before this if mess, so if
804 # blankpasswd is no we don't blank the cryptpwd
805 if ($useemptypassword eq "yes") {
808 if ($enableaccount eq "no") {
809 $cryptpwd = "*" . $cryptpwd;
813 $new_entry = "$name\:" . "$cryptpwd" .
814 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
815 &append_file
($etc_passwd, "$new_entry");
816 &new_users_pwdmkdb
("$new_entry", $name);
817 &new_users_group_update
;
818 &new_users_passwd_update
; print "Added user ``$name''\n";
819 &new_users_sendmessage
;
820 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
821 &home_create
($userhome, $name, $group_login);
825 if (!&confirm_yn
("Add another user?", "yes")) {
826 print "Goodbye!\n" if $verbose;
829 print "\n" if !$verbose;
833 # ask for password usage
834 sub password_default
{
835 local($p) = $defaultusepassword;
837 $p = &confirm_yn
("Use password-based authentication", $defaultusepassword);
838 $changes++ unless $p;
840 return "yes" if (($defaultusepassword eq "yes" && $p) ||
841 ($defaultusepassword eq "no" && !$p));
842 return "no"; # otherwise
845 # ask for account enable usage
846 sub enable_account_default
{
847 local ($p) = $defaultenableaccount;
849 $p = &confirm_yn
("Enable account password at creation", $defaultenableaccount);
850 $changes++ unless $p;
852 return "yes" if (($defaultenableaccount eq "yes" && $p) ||
853 ($defaultenableaccount eq "no" && !$p));
854 return "no"; # otherwise
857 # ask for empty password
858 sub enable_empty_password
{
859 local ($p) = $defaultemptypassword;
861 $p = &confirm_yn
("Use an empty password", $defaultemptypassword);
862 $changes++ unless $p;
864 return "yes" if (($defaultemptypassword eq "yes" && $p) ||
865 ($defaultemptypassword eq "no" && !$p));
866 return "no"; # otherwise
871 die "You are not root!\n" if $< && !$test;
885 [-message message_file]
888 [-s|-silent|-q|-quiet]
892 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
893 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
901 local($e, $last, @array);
903 foreach $e (sort @list) {
904 push(@array, $e) unless $e eq $last;
910 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
912 local($salt); # initialization
914 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
916 warn "calculate salt\n" if $verbose > 1;
918 for ($i = 0; $i < 27; $i++) {
919 srand(time + $rand + $$);
920 $rand = rand(25*29*17 + $rand);
921 $salt .= $itoa64[$rand & $#itoa64];
923 warn "Salt is: $salt\n" if $verbose > 1;
937 print "Use option ``-silent'' if you don't want to see " .
938 "all warnings and questions.\n\n";
940 print "Use option ``-verbose'' if you want to see more warnings and " .
941 "questions \nor try to repair bugs.\n\n";
946 sub parse_arguments
{
949 while ($_ = $argv[0], /^-/) {
952 if (/^--?(v|verbose)$/) { $verbose = 1 }
953 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
954 elsif (/^--?(debug)$/) { $verbose = 2 }
955 elsif (/^--?(f|force)$/) { $force = 1 }
956 elsif (/^--?(h|help|\?)$/) { &usage
}
957 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
958 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
959 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
960 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
961 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
962 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
963 elsif (/^--?(check_only)$/) { $check_only = 1 }
964 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
966 elsif (/^--?(batch)$/) {
967 warn "The -batch option is not supported anymore.\n",
968 "Please use the pw(8) command line tool!\n";
972 elsif (/^--?(config_create)$/) { &create_conf
; }
973 elsif (/^--?(noconfig)$/) { $config_read = 0; }
976 #&usage if $#argv < 0;
988 $name = &stripdir
($name);
989 $name =~ s
|/+[^/]+$||;
990 $name = "/" unless $name; # dirname of / is /
994 # return 1 if $file is a readable file or link
996 local($file, $verb) = @_;
999 if (-f
$file || -l
$file) {
1001 warn "$file unreadable\n" if $verbose;
1003 warn "$file is not a plain file or link\n" if $verbose;
1009 # create configuration files and exit
1012 if ($send_message ne 'no') {
1013 &message_create
($send_message);
1015 &message_create
($send_message_bak);
1021 # log for new user in /var/log/adduser
1023 local($string) = @_;
1026 return 1 if $logfile eq "no";
1028 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1032 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
1034 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1037 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1040 # create HOME directory, copy dotfiles from $dotdir to $HOME
1042 local($homedir, $name, $group) = @_;
1045 if (-e
"$homedir") {
1046 warn "HOME Directory ``$homedir'' already exist\a\n";
1050 # if the home directory prefix doesn't exist, create it
1051 # First, split the directory into a list; then remove the user's dir
1052 @dir = split('/', $homedir); pop(@dir);
1053 # Put back together & strip to get directory prefix
1054 $rootdir = &stripdir
(join('/', @dir));
1056 if (!&mkdirhier
("$rootdir")) {
1057 # warn already displayed
1061 if ($dotdir eq 'no') {
1062 if (!mkdir("$homedir", 0755)) {
1063 warn "$dir: $!\n"; return 0;
1065 system 'chown', "$name:$group", $homedir;
1069 # copy files from $dotdir to $homedir
1070 # rename 'dot.foo' files to '.foo'
1071 print "Copy files from $dotdir to $homedir\n" if $verbose;
1072 system('cp', '-R', $dotdir, $homedir);
1073 system('chmod', '-R', 'u+wrX,go-w', $homedir);
1074 system('chown', '-R', "$name:$group", $homedir);
1077 opendir(D
, $homedir);
1078 foreach $file (readdir(D
)) {
1079 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
1080 $file =~ s/^dot\././;
1081 rename("$homedir/dot$file", "$homedir/$file");
1083 chmod(0600, "$homedir/$file")
1084 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1085 chmod(0700, "$homedir/$file")
1086 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1092 # makes a directory hierarchy
1095 $dir = &stripdir
($dir);
1096 local($user_partition) = "/usr";
1097 local($dirname) = &dirname
($dir);
1099 -e
$dirname || &mkdirhier
($dirname);
1101 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1102 # home partition is on root partition
1103 # create home partition on $user_partition and make
1104 # a symlink from $dir to $user_partition/`basename $dir`
1105 # For instance: /home -> /usr/home
1107 local($basename) = &basename
($dir);
1108 local($d) = "$user_partition/$basename";
1112 warn "Oops, $d already exist\n" if $verbose;
1114 print "Create $d\n" if $verbose;
1115 if (!mkdir("$d", 0755)) {
1116 warn "$d: $!\a\n"; return 0;
1120 unlink($dir); # symlink to nonexist file
1121 print "Create symlink: $dir -> $d\n" if $verbose;
1122 if (!symlink("$d", $dir)) {
1123 warn "Symlink $d: $!\a\n"; return 0;
1126 print "Create $dir\n" if $verbose;
1127 if (!mkdir("$dir", 0755)) {
1128 warn "Directory ``$dir'': $!\a\n"; return 0;
1138 $dir = &stripdir
($dir);
1140 foreach $d (split('/', $dir)) {
1144 print "Create $dir\n" if $verbose;
1145 if (!mkdir("$dir", 0755)) {
1146 warn "$dir: $!\n"; return 0;
1155 # F.i.: //usr///home// -> /usr/home
1159 $dir =~ s
|/+|/|g
; # delete double '/'
1160 $dir =~ s
|/$||; # delete '/' at end
1161 return $dir if $dir ne "";
1165 # Read one of the elements from @list. $confirm is default.
1166 # If !$allow accept only elements from @list.
1168 local($message, $allow, $confirm, @list) = @_;
1169 local($read, $c, $print);
1171 $print = "$message" if $message;
1172 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1174 $print .= join($", &uniq(@list)); #"
1175 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1177 print "\n" if (length($print) + length($confirm)) > 60;
1178 print "[$confirm]: ";
1180 chop($read = <STDIN>);
1183 return $confirm if $read eq "";
1184 return "$read" if $allow;
1186 foreach $c (@list) {
1187 return $read if $c eq $read;
1189 warn "$read: is not allowed!\a\n";
1190 return &confirm_list($message, $allow, $confirm, @list);
1193 # YES or NO question
1194 # return 1 if &confirm("message", "yes") and answer is yes
1195 # or if &confirm("message", "no") an answer is no
1198 local($message, $confirm) = @_;
1199 local($yes) = '^(yes
|YES
|y
|Y
)$';
1200 local($no) = '^(no|NO
|n
|N
)$';
1203 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1208 print "$message (y/n) [$confirm]: ";
1209 chop($read = <STDIN>);
1212 return 1 unless $read;
1214 if (($confirm eq "y" && $read =~ "$yes") ||
1215 ($confirm eq "n" && $read =~ "$no")) {
1219 if ($read !~ "$yes" && $read !~ "$no") {
1220 warn "Wrong value. Enter again!\a\n";
1221 return &confirm_yn($message, $confirm);
1226 # test if $dotdir exist
1227 # return "no" if $dotdir not exist or dotfiles should not copied
1228 sub dotdir_default {
1229 local($dir) = $dotdir;
1231 return &dotdir_default_valid($dir) unless $verbose;
1233 $dir = &confirm_list("Copy dotfiles from:", 1,
1234 $dir, ("no", $dotdir_bak, $dir));
1235 last if $dir eq &dotdir_default_valid($dir);
1237 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1239 $changes++ if $dir ne $dotdir;
1243 sub dotdir_default_valid {
1246 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1247 return $dir if $dir eq "no";
1248 warn "Dotdir ``$dir'' is not a directory\a\n";
1252 # ask for messages to new users
1253 sub message_default {
1254 local($file) = $send_message;
1255 local(@d) = ($file, $send_message_bak, "no");
1258 $file = &confirm_list("Send message from file:", 1, $file, @d);
1259 last if $file eq "no";
1260 last if &filetest($file, 1);
1262 # maybe create message file
1263 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1264 last if &filetest($file, 0);
1265 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1269 if ($file eq "no" || !&filetest($file, 0)) {
1270 warn "Do not send message\n" if $verbose;
1273 &message_read($file);
1276 $changes++ if $file ne $send_message && $verbose;
1280 # create message file
1281 sub message_create {
1284 rename($file, "$file.bak");
1285 if (!open(M, "> $file")) {
1286 warn "Messagefile ``$file'': $!\n"; return 0;
1290 # Message file for adduser(8)
1292 # default variables: \$name, \$fullname, \$password
1293 # other variables: see /etc/adduser.conf after
1294 # line ``$do_not_delete''
1299 your account ``\$name'' was created.
1302 See also chpass(1), finger(1), passwd(1)
1308 # read message file into buffer
1311 @message_buffer = '';
1313 if (!open(R
, "$file")) {
1314 warn "File ``$file'':$!\n"; return 0;
1317 push(@message_buffer, $_) unless /^\s*#/;
1322 # write @list to $file with file-locking
1324 local($file,@list) = @_;
1326 local($LOCK_EX) = 2;
1327 local($LOCK_NB) = 4;
1328 local($LOCK_UN) = 8;
1330 open(F
, ">> $file") || die "$file: $!\n";
1331 print "Lock $file.\n" if $verbose > 1;
1332 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1333 warn "Cannot lock file: $file\a\n";
1334 die "Sorry, give up\n"
1335 unless &confirm_yn
("Try again?", "yes");
1337 print F
join("\n", @list) . "\n";
1339 print "Unlock $file.\n" if $verbose > 1;
1343 # return free uid+gid
1344 # uid == gid if possible
1348 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1349 # looking for next free uid
1350 while($uid{$uid_start}) {
1352 $uid_start = 1000 if $uid_start >= $uid_end;
1353 print "$uid_start\n" if $verbose > 1;
1356 local($gid_start) = $uid_start;
1357 # group for user (username==groupname) already exist
1358 if ($groupname{$group}) {
1359 $gid_start = $groupname{$group};
1361 # gid is in use, looking for another gid.
1362 # Note: uid an gid are not equal
1363 elsif ($gid{$uid_start}) {
1364 while($gid{$gid_start} || $uid{$gid_start}) {
1366 $gid_start = $uid_end if $gid_start < 100;
1369 return ($uid_start, $gid_start);
1375 local($user_flag) = 0;
1377 # don't read config file
1378 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1380 if(!open(C
, "$config")) {
1381 warn "$config: $!\n"; return 0;
1385 # user defined variables
1386 /^$do_not_delete/ && $user_flag++;
1387 # found @array or $variable
1388 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1392 # lines with '^##' are not saved
1393 push(@user_variable_list, $_)
1394 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1396 #warn "X @user_variable_list X\n";
1403 local($silent) = @_;
1406 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1410 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1413 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1417 rename($config, "$config.bak");
1418 open(C
, "> $config") || die "$config: $!\n";
1420 # prepare some variables
1421 $send_message = "no" unless $send_message;
1422 $defaultusepassword = "no" unless $defaultusepassword;
1423 $defaultenableaccount = "yes" unless $defaultenableaccount;
1424 $defaultemptypassword = "no" unless $defaultemptypassword;
1425 local($shpref) = "'" . join("', '", @shellpref) . "'";
1426 local($shpath) = "'" . join("', '", @path) . "'";
1427 local($user_var) = join('', @user_variable_list);
1431 # $config - automatic generated by adduser(8)
1433 # Note: adduser read *and* write this file.
1434 # You may change values, but don't add new things before the
1435 # line ``$do_not_delete''
1441 # use password-based authentication for new users
1442 # defaultusepassword = "yes" | "no"
1443 defaultusepassword = "$defaultusepassword"
1445 # enable account password at creation
1446 # (the password will be prepended with a star if the account isn't enabled)
1447 # defaultenableaccount = "yes" | "no"
1448 defaultenableaccount = "$defaultenableaccount"
1450 # allow blank passwords
1451 # defaultemptypassword = "yes" | "no"
1452 defaultemptypassword = "$defaultemptypassword"
1454 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1457 # send this file to new user ("/etc/adduser.message" or "no")
1458 send_message = "$send_message"
1460 # config file for adduser ("/etc/adduser.conf")
1463 # logfile ("/var/log/adduser" or "no")
1464 logfile = "$logfile"
1466 # default HOME directory ("/home")
1469 # List of directories where shells located
1470 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1473 # common shell list, first element has higher priority
1474 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1475 shellpref = ($shpref)
1477 # defaultshell if not empty ("bash")
1478 defaultshell = "$defaultshell"
1480 # defaultgroup ('USER' for same as username or any other valid group)
1481 defaultgroup = $defaultgroup
1483 # defaultclass if not empty
1484 defaultclass = "$defaultclass"
1486 # new users get this uid (1000)
1487 uid_start = "$uid_start"
1490 ## your own variables, see /etc/adduser.message
1501 $test = 0; # test mode, only for development
1504 &check_root
; # you must be root to run this script!
1505 &variables
; # initialize variables
1506 &config_read
(@ARGV); # read variables form config-file
1507 &parse_arguments
(@ARGV); # parse arguments
1515 &passwd_check
; # check for valid passwdb
1516 &shells_read
; # read /etc/shells
1517 &passwd_read
; # read /etc/master.passwd
1518 &group_read
; # read /etc/group
1519 &group_check
; # check for incon*
1520 exit 0 if $check_only; # only check consistence and exit
1525 &shells_add
; # maybe add some new shells
1526 $defaultshell = &shell_default
; # enter default shell
1527 $home = &home_partition
($home); # find HOME partition
1528 $dotdir = &dotdir_default
; # check $dotdir
1529 $send_message = &message_default
; # send message to new user
1530 $defaultusepassword = &password_default
; # maybe use password
1531 if ($defaultusepassword eq "no") {
1533 print "Creating accounts with a locked password.\n";
1535 $defaultenableaccount = "no";
1536 $defaultemptypassword = "yes";
1538 $defaultenableaccount = &enable_account_default
; # enable or disable account
1539 $defaultemptypassword = &enable_empty_password
; # use empty password or not
1541 &config_write
(!$verbose); # write variables in file
1543 # main loop for creating new users
1544 &new_users
; # add new users