]>
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 $defaultpasswd = "yes"; # use password for new users
34 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
35 $dotdir_bak = $dotdir;
36 $send_message = "/etc/adduser.message"; # send message to new user
37 $send_message_bak = '/etc/adduser.message';
38 $config = "/etc/adduser.conf"; # config file for adduser
39 $config_read = 1; # read config file
40 $logfile = "/var/log/adduser"; # logfile
41 $home = "/home"; # default HOME
42 $etc_shells = "/etc/shells";
43 $etc_passwd = "/etc/master.passwd";
44 $group = "/etc/group";
45 $pwd_mkdb = "pwd_mkdb -p"; # program for building passwd database
48 # List of directories where shells located
49 @path = ('/bin', '/usr/bin', '/usr/local/bin');
50 # common shells, first element has higher priority
51 @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
53 $defaultshell = 'sh'; # defaultshell if not empty
55 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
58 $uid_start = 1000; # new users get this uid
59 $uid_end = 32000; # max. uid
63 $username = ''; # $username{username} = uid
64 $uid = ''; # $uid{uid} = username
65 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
67 $password = ''; # password for new users
70 $groupname =''; # $groupname{groupname} = gid
71 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
72 $gid = ''; # $gid{gid} = groupname; gid form group db
75 $shell = ''; # $shell{`basename sh`} = sh
77 umask 022; # don't give login group write access
79 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
83 @user_variable_list = ''; # user variables in /etc/adduser.conf
84 $do_not_delete = '## DO NOT DELETE THIS LINE!';
87 # read shell database, see also: shells(5)
92 print "Check $etc_shells\n" if $verbose;
93 open(S
, $etc_shells) || die "$etc_shells:$!\n";
97 s/^\s*//; s/\s+.*//; # chop
100 $shell{&basename
($sh)} = $sh;
102 warn "Shell: $sh not executable!\n";
108 # Allow /nonexistent and /bin/date as a valid shell for system utils
109 push(@list, "/nonexistent");
110 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
111 $shell{"no"} = "/nonexistent";
113 push(@list, "/bin/date");
114 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
115 $shell{"date"} = "/bin/date";
120 # add new shells if possible
122 local($sh,$dir,@list);
124 return 1 unless $verbose;
126 foreach $sh (@shellpref) {
129 # shell $sh is not defined as login shell
130 foreach $dir (@path) {
133 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
134 push(@list, "$dir/$sh");
135 push(@shellpref, "$sh");
136 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
143 &append_file
($etc_shells, @list) if $#list >= 0;
146 # choose your favourite shell and return the shell
148 local($e,$i,$new_shell);
151 $sh = &shell_default_valid
($defaultshell);
152 return $sh unless $verbose;
154 $new_shell = &confirm_list
("Enter your default shell:", 0,
155 $sh, sort(keys %shell));
156 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
157 $changes++ if $new_shell ne $sh;
161 sub shell_default_valid
{
165 return $sh if $shell{$sh};
167 foreach $e (@shellpref) {
169 last if defined($shell{$s});
172 warn "Shell ``$sh'' is undefined, use ``$s''\n";
176 # return default home partition (e.g. "/home")
177 # create base directory if nesseccary
180 $home = &stripdir
($home);
183 return $h if !$verbose && $h eq &home_partition_valid
($h);
186 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
188 last if $h eq &home_partition_valid
($h);
191 $changes++ if $h ne $home;
195 sub home_partition_valid
{
200 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
204 warn "Please use absolute path for home: ``$h''.\a\n";
209 warn "$h exists, but is not a directory or symlink!\n"
210 unless -d
$h || -l
$h;
211 warn "$h is not writable!\n"
215 # create home partition
216 return $h if &mkdir_home
($h);
221 # check for valid passwddb
223 system("$pwd_mkdb -C $etc_passwd");
224 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
229 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
231 print "Check $etc_passwd\n" if $verbose;
232 open(P
, "$etc_passwd") || die "$passwd: $!\n";
236 push(@passwd_backup, $_);
241 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
243 print "$p_username already exists with uid: $username{$p_username}!\n"
244 if $username{$p_username} && $verbose;
245 $username{$p_username} = $p_uid;
246 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
247 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
248 print "User $p_username: illegal shell: ``$sh''\n"
249 if ($verbose && $sh &&
250 !$shell{&basename
($sh)} &&
251 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
252 $sh !~ /\/(pppd
|sliplogin
|nologin
|nonexistent
)$/);
253 $uid{$p_uid} = $p_username;
254 $pwgid{$p_gid} = $p_username;
261 local($g_groupname,$pw,$g_gid, $memb);
263 print "Check $group\n" if $verbose;
264 open(G
, "$group") || die "$group: $!\n";
267 push(@group_backup, $_);
272 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
274 $groupmembers{$g_gid} = $memb;
275 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
276 if $groupname{$g_groupname} && $verbose;
277 $groupname{$g_groupname} = $g_gid;
278 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
279 if $gid{$g_gid} && $verbose;
280 $gid{$g_gid} = $g_groupname;
285 # check gids /etc/passwd <-> /etc/group
287 local($c_gid, $c_username, @list);
289 foreach $c_gid (keys %pwgid) {
291 $c_username = $pwgid{$c_gid};
292 warn "User ``$c_username'' has gid $c_gid but a group with this " .
293 "gid does not exist.\n" if $verbose;
299 # main loop for creating new users
307 $name = &confirm_list
("Enter username", 1, "a-z0-9_-", "");
308 if (length($name) > 16) {
309 warn "Username is longer than 16 chars\a\n";
312 last if (&new_users_name_valid
($name) eq $name);
317 sub new_users_name_valid
{
320 if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/ || $name eq "a-z0-9_-") {
321 warn "Wrong username. " .
322 "Please use only lowercase characters or digits\a\n";
324 } elsif ($username{$name}) {
325 warn "Username ``$name'' already exists!\a\n"; return 0;
331 sub new_users_fullname
{
336 $fullname = &confirm_list
("Enter full name", 1, "", "");
337 last if $fullname eq &new_users_fullname_valid
($fullname);
339 $fullname = $name unless $fullname;
343 sub new_users_fullname_valid
{
344 local($fullname) = @_;
346 return $fullname if $fullname !~ /:/;
348 warn "``:'' is not allowed!\a\n";
352 # return shell (full path) for user
353 sub new_users_shell
{
356 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
360 # return home (full path) for user
361 # Note that the home path defaults to $home/$name for batch
367 $userhome = &confirm_list
("Enter home directory (full path)", 1, "$home/$name", "");
368 last if $userhome =~ /^\//;
369 warn qq{Home directory "$userhome" is not a full path\a\n};
374 # return free uid and gid
377 local($u_id, $g_id) = &next_id
($name);
378 local($u_id_tmp, $e);
381 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
382 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
384 if ($uid{$u_id_tmp}) {
385 warn "Uid ``$u_id_tmp'' in use!\a\n";
386 $uid_start = $u_id_tmp;
387 ($u_id, $g_id) = &next_id
($name);
390 warn "Wrong uid.\a\n";
394 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
396 $uid_start = $u_id_tmp;
397 return &next_id
($name);
400 # return login class for user
401 sub new_users_class
{
405 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
406 $class = "" if $class eq "default";
412 local($gid, $name) = @_;
415 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
417 $groupmembers_bak{$gid} = $groupmembers{$gid};
418 $groupmembers{$gid} .= "," if $groupmembers{$gid};
419 $groupmembers{$gid} .= "$name";
426 sub new_users_grplogin
{
427 local($name, $defaultgroup, $new_users_ok) = @_;
428 local($group_login, $group);
431 $group = $defaultgroup if $defaultgroup ne $group_uniq;
435 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
437 # restore old groupmembers, user was not accept
438 foreach $e (keys %groupmembers_bak) {
439 $groupmembers{$e} = $groupmembers_bak{$e};
444 $group_login = &confirm_list
("Login group", 1, $group,
446 last if $group_login eq $group;
447 last if $group_login eq $name;
448 last if defined $groupname{$group_login};
449 if ($group_login eq $group_uniq) {
450 $group_login = $name; last;
453 if (defined $gid{$group_login}) {
454 # convert numeric groupname (gid) to groupname
455 $group_login = $gid{$group_login};
458 warn "Group does not exist!\a\n";
461 #if (defined($groupname{$group_login})) {
462 # &add_group($groupname{$group_login}, $name);
465 return ($group_login, $group_uniq) if $group_login eq $name;
466 return ($group_login, $group_login);
469 # return other groups (string)
470 sub new_users_groups
{
471 local($name, $other_groups) = @_;
473 "Login group is ``$group_login''. Invite $name into other groups:";
475 local($new_groups,$groups);
477 $other_groups = "no" unless $other_groups;
480 $groups = &confirm_list
($string, 1, $other_groups,
481 ("no", $other_groups, "guest"));
483 return "" if $groups eq "no";
485 ($flag, $new_groups) = &new_users_groups_valid
($groups);
488 $new_groups =~ s/\s*$//;
492 sub new_users_groups_valid
{
494 local($e, $new_groups);
497 foreach $e (split(/[,\s]+/, $groups)) {
498 # convert numbers to groupname
499 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
502 if (defined($groupname{$e})) {
503 if ($e eq $group_login) {
504 # do not add user to a group if this group
505 # is also the login group.
506 } elsif (&add_group
($groupname{$e}, $name)) {
507 $new_groups .= "$e ";
509 warn "$name is already member of group ``$e''\n";
512 warn "Group ``$e'' does not exist\a\n"; $flag++;
515 return ($flag, $new_groups);
527 Gid: $g_id ($group_login)
529 Groups: $group_login $new_groups
534 return &confirm_yn
("OK?", "yes");
537 # make password database
538 sub new_users_pwdmkdb
{
541 system("$pwd_mkdb $etc_passwd");
544 warn "``$pwd_mkdb'' failed\n";
549 # update group database
550 sub new_users_group_update
{
554 if (!defined($groupname{$group_login}) &&
555 !defined($gid{$groupname{$group_login}})) {
556 push(@group_backup, "$group_login:*:$g_id:");
557 $groupname{$group_login} = $g_id;
558 $gid{$g_id} = $group_login;
559 # $groupmembers{$g_id} = $group_login;
562 if ($new_groups || defined($groupname{$group_login}) ||
563 defined($gid{$groupname{$group_login}}) &&
564 $gid{$groupname{$group_login}} ne "+") {
565 # new user is member of some groups
566 # new login group is already in name space
567 rename($group, "$group.bak");
568 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
569 foreach $e (sort {$a <=> $b} (keys %gid)) {
570 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
572 &append_file
($group, @a);
574 &append_file
($group, "$group_login:*:$g_id:");
579 sub new_users_passwd_update
{
580 # update passwd/group variables
581 push(@passwd_backup, $new_entry);
582 $username{$name} = $u_id;
584 $pwgid{$g_id} = $name;
587 # send message to new user
588 sub new_users_sendmessage
{
589 return 1 if $send_message eq "no";
592 &confirm_list
("Send message to ``$name'' and:",
593 1, "no", ("root", "second_mail_address", "no"));
595 $cc = "" if $cc eq "no";
597 foreach $e (@message_buffer) {
602 local(@message_buffer_append) = ();
603 if (!&confirm_yn
("Add anything to default message", "no")) {
604 print "Use ``.'' or ^D alone on a line to finish your message.\n";
605 push(@message_buffer_append, "\n");
606 while($read = <STDIN
>) {
607 last if $read eq "\.\n";
608 push(@message_buffer_append, $read);
612 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
613 if (&confirm_yn
("Send message", "yes"));
617 local($to, @message) = @_;
620 if (!open(M
, "| mail -s Welcome $to")) {
621 warn "Cannot send mail to: $to!\n";
624 foreach $e (@message) {
625 print M
eval "\"$e\"";
632 sub new_users_password
{
635 return "" if $defaultpasswd ne "yes";
640 system("stty -echo");
641 $password = &confirm_list
("Enter password", 1, "", "");
644 if ($password ne "") {
645 system("stty -echo");
646 $newpass = &confirm_list
("Enter password again", 1, "", "");
649 last if $password eq $newpass;
650 print "They didn't match, please try again\n";
652 elsif (&confirm_yn
("Use an empty password?", "yes")) {
663 print "\n" if $verbose;
664 print "Ok, let's go.\n" .
665 "Don't worry about mistakes. I will give you the chance later to " .
666 "correct any input.\n" if $verbose;
669 # fullname: Full name
671 # userhome: home path for user
675 # group_login: groupname of g_id
676 # new_groups: some other groups
677 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
679 local($groupmembers_bak, $cryptpwd);
680 local($new_users_ok) = 1;
684 $new_groups = "no" unless $groupname{$new_groups};
687 $name = &new_users_name
;
688 $fullname = &new_users_fullname
($name);
689 $sh = &new_users_shell
;
690 $userhome = &new_users_home
($name);
691 ($u_id, $g_id) = &new_users_id
($name);
692 $class = &new_users_class
($defaultclass);
693 ($group_login, $defaultgroup) =
694 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
695 # do not use uniq username and login group
696 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
698 $new_groups = &new_users_groups
($name, $new_groups);
699 $password = &new_users_password
;
706 $cryptpwd = crypt($password, &salt
) if $password ne "";
708 $new_entry = "$name\:" . "$cryptpwd" .
709 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
710 &append_file
($etc_passwd, "$new_entry");
711 &new_users_pwdmkdb
("$new_entry");
712 &new_users_group_update
;
713 &new_users_passwd_update
; print "Added user ``$name''\n";
714 &new_users_sendmessage
;
715 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
716 &home_create
($userhome, $name, $group_login);
720 if (!&confirm_yn
("Add another user?", "yes")) {
721 print "Goodbye!\n" if $verbose;
724 print "\n" if !$verbose;
728 # ask for password usage
729 sub password_default
{
730 local($p) = $defaultpasswd;
732 $p = &confirm_yn
("Use passwords", $defaultpasswd);
733 $changes++ unless $p;
735 return "yes" if (($defaultpasswd eq "yes" && $p) ||
736 ($defaultpasswd eq "no" && !$p));
737 return "no"; # otherwise
742 die "You are not root!\n" if $< && !$test;
755 [-message message_file]
758 [-s|-silent|-q|-quiet]
762 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
763 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
771 local($e, $last, @array);
773 foreach $e (sort @list) {
774 push(@array, $e) unless $e eq $last;
780 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
782 local($salt); # initialization
784 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
786 warn "calculate salt\n" if $verbose > 1;
788 for ($i = 0; $i < 8; $i++) {
789 srand(time + $rand + $$);
790 $rand = rand(25*29*17 + $rand);
791 $salt .= $itoa64[$rand & $#itoa64];
793 warn "Salt is: $salt\n" if $verbose > 1;
807 print "Use option ``-silent'' if you don't want to see " .
808 "all warnings and questions.\n\n";
810 print "Use option ``-verbose'' if you want to see more warnings and " .
811 "questions \nor try to repair bugs.\n\n";
816 sub parse_arguments
{
819 while ($_ = $argv[0], /^-/) {
822 if (/^--?(v|verbose)$/) { $verbose = 1 }
823 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
824 elsif (/^--?(debug)$/) { $verbose = 2 }
825 elsif (/^--?(h|help|\?)$/) { &usage
}
826 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
827 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
828 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
829 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
830 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
831 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
832 elsif (/^--?(check_only)$/) { $check_only = 1 }
833 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
835 elsif (/^--?(batch)$/) {
836 warn "The -batch option is not supported anymore.\n",
837 "Please use the pw(8) command line tool!\n";
841 elsif (/^--?(config_create)$/) { &create_conf
; }
842 elsif (/^--?(noconfig)$/) { $config_read = 0; }
845 #&usage if $#argv < 0;
857 $name = &stripdir
($name);
858 $name =~ s
|/+[^/]+$||;
859 $name = "/" unless $name; # dirname of / is /
863 # return 1 if $file is a readable file or link
865 local($file, $verb) = @_;
868 if (-f
$file || -l
$file) {
870 warn "$file unreadable\n" if $verbose;
872 warn "$file is not a plain file or link\n" if $verbose;
878 # create configuration files and exit
881 if ($send_message ne 'no') {
882 &message_create
($send_message);
884 &message_create
($send_message_bak);
890 # log for new user in /var/log/adduser
895 return 1 if $logfile eq "no";
897 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
901 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
903 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
906 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
909 # create HOME directory, copy dotfiles from $dotdir to $HOME
911 local($homedir, $name, $group) = @_;
915 warn "HOME Directory ``$homedir'' already exist\a\n";
919 # if the home directory prefix doesn't exist, create it
920 # First, split the directory into a list; then remove the user's dir
921 @dir = split('/', $homedir); pop(@dir);
922 # Put back together & strip to get directory prefix
923 $rootdir = &stripdir
(join('/', @dir));
925 if (!&mkdirhier
("$rootdir")) {
926 # warn already displayed
930 if ($dotdir eq 'no') {
931 if (!mkdir("$homedir", 0755)) {
932 warn "$dir: $!\n"; return 0;
934 system 'chown', "$name:$group", $homedir;
938 # copy files from $dotdir to $homedir
939 # rename 'dot.foo' files to '.foo'
940 print "Copy files from $dotdir to $homedir\n" if $verbose;
941 system("cp -R $dotdir $homedir");
942 system("chmod -R u+wrX,go-w $homedir");
943 system("chown -R $name:$group $homedir");
946 opendir(D
, $homedir);
947 foreach $file (readdir(D
)) {
948 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
949 $file =~ s/^dot\././;
950 rename("$homedir/dot$file", "$homedir/$file");
952 chmod(0600, "$homedir/$file")
953 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
954 chmod(0700, "$homedir/$file")
955 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
961 # makes a directory hierarchy
964 $dir = &stripdir
($dir);
965 local($user_partition) = "/usr";
966 local($dirname) = &dirname
($dir);
968 -e
$dirname || &mkdirhier
($dirname);
970 if (((stat($dirname))[0]) == ((stat("/"))[0])){
971 # home partition is on root partition
972 # create home partition on $user_partition and make
973 # a symlink from $dir to $user_partition/`basename $dir`
974 # For instance: /home -> /usr/home
976 local($basename) = &basename
($dir);
977 local($d) = "$user_partition/$basename";
981 warn "Oops, $d already exist\n" if $verbose;
983 print "Create $d\n" if $verbose;
984 if (!mkdir("$d", 0755)) {
985 warn "$d: $!\a\n"; return 0;
989 unlink($dir); # symlink to nonexist file
990 print "Create symlink: $dir -> $d\n" if $verbose;
991 if (!symlink("$d", $dir)) {
992 warn "Symlink $d: $!\a\n"; return 0;
995 print "Create $dir\n" if $verbose;
996 if (!mkdir("$dir", 0755)) {
997 warn "Directory ``$dir'': $!\a\n"; return 0;
1007 $dir = &stripdir
($dir);
1009 foreach $d (split('/', $dir)) {
1013 print "Create $dir\n" if $verbose;
1014 if (!mkdir("$dir", 0755)) {
1015 warn "$dir: $!\n"; return 0;
1024 # F.i.: //usr///home// -> /usr/home
1028 $dir =~ s
|/+|/|g
; # delete double '/'
1029 $dir =~ s
|/$||; # delete '/' at end
1030 return $dir if $dir ne "";
1034 # Read one of the elements from @list. $confirm is default.
1035 # If !$allow accept only elements from @list.
1037 local($message, $allow, $confirm, @list) = @_;
1038 local($read, $c, $print);
1040 $print = "$message" if $message;
1041 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1043 $print .= join($", &uniq(@list)); #"
1044 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1046 print "\n" if (length($print) + length($confirm)) > 60;
1047 print "[$confirm]: ";
1049 chop($read = <STDIN>);
1052 return $confirm if $read eq "";
1053 return "$read" if $allow;
1055 foreach $c (@list) {
1056 return $read if $c eq $read;
1058 warn "$read: is not allowed!\a\n";
1059 return &confirm_list($message, $allow, $confirm, @list);
1062 # YES or NO question
1063 # return 1 if &confirm("message", "yes") and answer is yes
1064 # or if &confirm("message", "no") an answer is no
1067 local($message, $confirm) = @_;
1068 local($yes) = '^(yes
|YES
|y
|Y
)$';
1069 local($no) = '^(no|NO
|n
|N
)$';
1072 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1077 print "$message (y/n) [$confirm]: ";
1078 chop($read = <STDIN>);
1081 return 1 unless $read;
1083 if (($confirm eq "y" && $read =~ "$yes") ||
1084 ($confirm eq "n" && $read =~ "$no")) {
1088 if ($read !~ "$yes" && $read !~ "$no") {
1089 warn "Wrong value. Enter again!\a\n";
1090 return &confirm_yn($message, $confirm);
1095 # test if $dotdir exist
1096 # return "no" if $dotdir not exist or dotfiles should not copied
1097 sub dotdir_default {
1098 local($dir) = $dotdir;
1100 return &dotdir_default_valid($dir) unless $verbose;
1102 $dir = &confirm_list("Copy dotfiles from:", 1,
1103 $dir, ("no", $dotdir_bak, $dir));
1104 last if $dir eq &dotdir_default_valid($dir);
1106 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1108 $changes++ if $dir ne $dotdir;
1112 sub dotdir_default_valid {
1115 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1116 return $dir if $dir eq "no";
1117 warn "Dotdir ``$dir'' is not a directory\a\n";
1121 # ask for messages to new users
1122 sub message_default {
1123 local($file) = $send_message;
1124 local(@d) = ($file, $send_message_bak, "no");
1127 $file = &confirm_list("Send message from file:", 1, $file, @d);
1128 last if $file eq "no";
1129 last if &filetest($file, 1);
1131 # maybe create message file
1132 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1133 last if &filetest($file, 0);
1134 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1138 if ($file eq "no" || !&filetest($file, 0)) {
1139 warn "Do not send message\n" if $verbose;
1142 &message_read($file);
1145 $changes++ if $file ne $send_message && $verbose;
1149 # create message file
1150 sub message_create {
1153 rename($file, "$file.bak");
1154 if (!open(M, "> $file")) {
1155 warn "Messagefile ``$file'': $!\n"; return 0;
1159 # Message file for adduser(8)
1161 # default variables: \$name, \$fullname, \$password
1162 # other variables: see /etc/adduser.conf after
1163 # line ``$do_not_delete''
1168 your account ``\$name'' was created.
1171 See also chpass(1), finger(1), passwd(1)
1177 # read message file into buffer
1180 @message_buffer = '';
1182 if (!open(R
, "$file")) {
1183 warn "File ``$file'':$!\n"; return 0;
1186 push(@message_buffer, $_) unless /^\s*#/;
1191 # write @list to $file with file-locking
1193 local($file,@list) = @_;
1195 local($LOCK_EX) = 2;
1196 local($LOCK_NB) = 4;
1197 local($LOCK_UN) = 8;
1199 open(F
, ">> $file") || die "$file: $!\n";
1200 print "Lock $file.\n" if $verbose > 1;
1201 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1202 warn "Cannot lock file: $file\a\n";
1203 die "Sorry, give up\n"
1204 unless &confirm_yn
("Try again?", "yes");
1206 print F
join("\n", @list) . "\n";
1208 print "Unlock $file.\n" if $verbose > 1;
1212 # return free uid+gid
1213 # uid == gid if possible
1217 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1218 # looking for next free uid
1219 while($uid{$uid_start}) {
1221 $uid_start = 1000 if $uid_start >= $uid_end;
1222 print "$uid_start\n" if $verbose > 1;
1225 local($gid_start) = $uid_start;
1226 # group for user (username==groupname) already exist
1227 if ($groupname{$group}) {
1228 $gid_start = $groupname{$group};
1230 # gid is in use, looking for another gid.
1231 # Note: uid an gid are not equal
1232 elsif ($gid{$uid_start}) {
1233 while($gid{$gid_start} || $uid{$gid_start}) {
1235 $gid_start = $uid_end if $gid_start < 100;
1238 return ($uid_start, $gid_start);
1244 local($user_flag) = 0;
1246 # don't read config file
1247 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1249 if(!open(C
, "$config")) {
1250 warn "$config: $!\n"; return 0;
1254 # user defined variables
1255 /^$do_not_delete/ && $user_flag++;
1256 # found @array or $variable
1257 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1261 # lines with '^##' are not saved
1262 push(@user_variable_list, $_)
1263 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1265 #warn "X @user_variable_list X\n";
1272 local($silent) = @_;
1275 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1279 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1282 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1286 rename($config, "$config.bak");
1287 open(C
, "> $config") || die "$config: $!\n";
1289 # prepare some variables
1290 $send_message = "no" unless $send_message;
1291 $defaultpasswd = "no" unless $defaultpasswd;
1292 local($shpref) = "'" . join("', '", @shellpref) . "'";
1293 local($shpath) = "'" . join("', '", @path) . "'";
1294 local($user_var) = join('', @user_variable_list);
1298 # $config - automatic generated by adduser(8)
1300 # Note: adduser read *and* write this file.
1301 # You may change values, but don't add new things before the
1302 # line ``$do_not_delete''
1308 # use password for new users
1309 # defaultpasswd = yes | no
1310 defaultpasswd = $defaultpasswd
1312 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1315 # send this file to new user ("/etc/adduser.message" or "no")
1316 send_message = "$send_message"
1318 # config file for adduser ("/etc/adduser.conf")
1321 # logfile ("/var/log/adduser" or "no")
1322 logfile = "$logfile"
1324 # default HOME directory ("/home")
1327 # List of directories where shells located
1328 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1331 # common shell list, first element has higher priority
1332 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1333 shellpref = ($shpref)
1335 # defaultshell if not empty ("bash")
1336 defaultshell = "$defaultshell"
1338 # defaultgroup ('USER' for same as username or any other valid group)
1339 defaultgroup = $defaultgroup
1341 # defaultclass if not empty
1342 defaultclass = "$defaultclass"
1344 # new users get this uid (1000)
1345 uid_start = "$uid_start"
1348 ## your own variables, see /etc/adduser.message
1359 $test = 0; # test mode, only for development
1362 &check_root
; # you must be root to run this script!
1363 &variables
; # initialize variables
1364 &config_read
(@ARGV); # read variables form config-file
1365 &parse_arguments
(@ARGV); # parse arguments
1373 &passwd_check
; # check for valid passwdb
1374 &shells_read
; # read /etc/shells
1375 &passwd_read
; # read /etc/master.passwd
1376 &group_read
; # read /etc/group
1377 &group_check
; # check for incon*
1378 exit 0 if $check_only; # only check consistence and exit
1383 &shells_add
; # maybe add some new shells
1384 $defaultshell = &shell_default
; # enter default shell
1385 $home = &home_partition
($home); # find HOME partition
1386 $dotdir = &dotdir_default
; # check $dotdir
1387 $send_message = &message_default
; # send message to new user
1388 $defaultpasswd = &password_default
; # maybe use password
1389 &config_write
(!$verbose); # write variables in file
1391 # main loop for creating new users
1392 &new_users
; # add new users