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
27 # $FreeBSD: src/usr.sbin/adduser/adduser.perl,v 1.44.2.5 2002/08/31 18:29:04 dwmalone Exp $
28 # $DragonFly: src/usr.sbin/adduser/Attic/adduser.perl,v 1.2 2003/06/17 04:29:52 dillon Exp $
33 $verbose = 1; # verbose = [0-2]
34 $usernameregexp = "^[a-z0-9_][a-z0-9_-]*\$"; # configurable
35 $defaultusernameregexp = $usernameregexp; # remains constant
36 $defaultpasswd = "yes"; # use password for new users
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
73 $groupname =''; # $groupname{groupname} = gid
74 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
75 $gid = ''; # $gid{gid} = groupname; gid form group db
76 @group_comments; # Comments in the group file
79 $shell = ''; # $shell{`basename sh`} = sh
81 umask 022; # don't give login group write access
83 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
87 @user_variable_list = ''; # user variables in /etc/adduser.conf
88 $do_not_delete = '## DO NOT DELETE THIS LINE!';
91 # read shell database, see also: shells(5)
96 print "Check $etc_shells\n" if $verbose;
97 open(S, $etc_shells) || die "$etc_shells:$!\n";
101 s/^\s*//; s/\s+.*//; # chop
104 $shell{&basename($sh)} = $sh;
106 warn "Shell: $sh not executable!\n";
112 # Allow /nonexistent and /bin/date as a valid shell for system utils
113 push(@list, "/nonexistent");
114 push(@shellpref, "no") if !grep(/^no$/, @shellpref);
115 $shell{"no"} = "/nonexistent";
117 push(@list, "/bin/date");
118 push(@shellpref, "date") if !grep(/^date$/, @shellpref);
119 $shell{"date"} = "/bin/date";
124 # add new shells if possible
126 local($sh,$dir,@list);
128 return 1 unless $verbose;
130 foreach $sh (@shellpref) {
133 # shell $sh is not defined as login shell
134 foreach $dir (@path) {
137 if (&confirm_yn("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
138 push(@list, "$dir/$sh");
139 push(@shellpref, "$sh");
140 $shell{&basename("$dir/$sh")} = "$dir/$sh";
147 &append_file($etc_shells, @list) if $#list >= 0;
150 # choose your favourite shell and return the shell
152 local($e,$i,$new_shell);
155 $sh = &shell_default_valid($defaultshell);
156 return $sh unless $verbose;
158 $new_shell = &confirm_list("Enter your default shell:", 0,
159 $sh, sort(keys %shell));
160 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
161 $changes++ if $new_shell ne $sh;
165 sub shell_default_valid {
169 return $sh if $shell{$sh};
171 foreach $e (@shellpref) {
173 last if defined($shell{$s});
176 warn "Shell ``$sh'' is undefined, use ``$s''\n";
180 # return default home partition (e.g. "/home")
181 # create base directory if nesseccary
184 $home = &stripdir($home);
187 return $h if !$verbose && $h eq &home_partition_valid($h);
190 $h = &confirm_list("Enter your default HOME partition:", 1, $home, "");
192 last if $h eq &home_partition_valid($h);
195 $changes++ if $h ne $home;
199 sub home_partition_valid {
204 return $h if $h =~ "^/" && -e $h && -w $h && (-d $h || -l $h);
208 warn "Please use absolute path for home: ``$h''.\a\n";
213 warn "$h exists, but is not a directory or symlink!\n"
214 unless -d $h || -l $h;
215 warn "$h is not writable!\n"
219 # create home partition
220 return $h if &mkdir_home($h);
225 # check for valid passwddb
227 system(@pwd_mkdb, '-C', $etc_passwd);
228 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
233 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
235 print "Check $etc_passwd\n" if $verbose;
236 open(P, "$etc_passwd") || die "$etc_passwd: $!\n";
240 push(@passwd_backup, $_);
245 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
247 print "$p_username already exists with uid: $username{$p_username}!\n"
248 if $username{$p_username} && $verbose;
249 $username{$p_username} = $p_uid;
250 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
251 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
252 print "User $p_username: illegal shell: ``$sh''\n"
253 if ($verbose && $sh &&
254 !$shell{&basename($sh)} &&
255 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
256 $sh !~ /\/(pppd|sliplogin|nologin|nonexistent)$/);
257 $uid{$p_uid} = $p_username;
258 $pwgid{$p_gid} = $p_username;
265 local($g_groupname,$pw,$g_gid, $memb);
267 print "Check $group\n" if $verbose;
268 open(G, "$group") || die "$group: $!\n";
271 push(@group_backup, $_);
274 # Save comments to restore later
276 push(@group_comments, $_);
280 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
282 $groupmembers{$g_gid} = $memb;
283 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
284 if $groupname{$g_groupname} && $verbose;
285 $groupname{$g_groupname} = $g_gid;
286 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
287 if $gid{$g_gid} && $verbose;
288 $gid{$g_gid} = $g_groupname;
293 # check gids /etc/passwd <-> /etc/group
295 local($c_gid, $c_username, @list);
297 foreach $c_gid (keys %pwgid) {
299 $c_username = $pwgid{$c_gid};
300 warn "User ``$c_username'' has gid $c_gid but a group with this " .
301 "gid does not exist.\n" if $verbose;
307 # main loop for creating new users
315 $name = &confirm_list("Enter username", 1, $usernameregexp, "");
316 last if (&new_users_name_valid($name));
321 sub new_users_name_valid {
324 if ($name eq $usernameregexp) { # user/admin just pressed <Return>
325 warn "Please enter a username\a\n";
327 } elsif (length($name) > 16) {
328 warn "Username is longer than 16 characters.\a\n";
330 } elsif ($name =~ /[:\n]/) {
331 warn "Username cannot contain colon or newline characters.\a\n";
333 } elsif ($name !~ /$usernameregexp/) {
334 if ($usernameregexp eq $defaultusernameregexp) {
335 warn "Illegal username.\n" .
336 "Please use only lowercase Roman, decimal, underscore, " .
337 "or hyphen characters.\n" .
338 "Additionally, a username should not start with a hyphen.\a\n";
340 warn "Username doesn't match the regexp /$usernameregexp/\a\n";
343 } elsif (defined($username{$name})) {
344 warn "Username ``$name'' already exists!\a\n"; return 0;
350 sub new_users_fullname {
355 $fullname = &confirm_list("Enter full name", 1, "", "");
356 last if $fullname eq &new_users_fullname_valid($fullname);
358 $fullname = $name unless $fullname;
362 sub new_users_fullname_valid {
363 local($fullname) = @_;
365 return $fullname if $fullname !~ /:/;
367 warn "``:'' is not allowed!\a\n";
371 # return shell (full path) for user
372 sub new_users_shell {
375 $sh = &confirm_list("Enter shell", 0, $defaultshell, keys %shell);
379 # return home (full path) for user
380 # Note that the home path defaults to $home/$name for batch
386 $userhome = &confirm_list("Enter home directory (full path)", 1, "$home/$name", "");
387 last if $userhome =~ /^\//;
388 warn qq{Home directory "$userhome" is not a full path\a\n};
393 # return free uid and gid
396 local($u_id, $g_id) = &next_id($name);
397 local($u_id_tmp, $e);
400 $u_id_tmp = &confirm_list("Uid", 1, $u_id, "");
401 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
403 if ($uid{$u_id_tmp}) {
404 warn "Uid ``$u_id_tmp'' in use!\a\n";
405 $uid_start = $u_id_tmp;
406 ($u_id, $g_id) = &next_id($name);
409 warn "Wrong uid.\a\n";
413 # return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
415 $uid_start = $u_id_tmp;
416 return &next_id($name);
419 # return login class for user
420 sub new_users_class {
424 $class = &confirm_list("Enter login class:", 1, $def, ($def, "default"));
425 $class = "" if $class eq "default";
431 local($gid, $name) = @_;
434 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
436 $groupmembers_bak{$gid} = $groupmembers{$gid};
437 $groupmembers{$gid} .= "," if $groupmembers{$gid};
438 $groupmembers{$gid} .= "$name";
445 sub new_users_grplogin {
446 local($name, $defaultgroup, $new_users_ok) = @_;
447 local($group_login, $group);
450 $group = $defaultgroup if $defaultgroup ne $group_uniq;
454 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
456 # restore old groupmembers, user was not accept
457 foreach $e (keys %groupmembers_bak) {
458 $groupmembers{$e} = $groupmembers_bak{$e};
463 $group_login = &confirm_list("Login group", 1, $group,
465 last if $group_login eq $group;
466 last if $group_login eq $name;
467 last if defined $groupname{$group_login};
468 if ($group_login eq $group_uniq) {
469 $group_login = $name; last;
472 if (defined $gid{$group_login}) {
473 # convert numeric groupname (gid) to groupname
474 $group_login = $gid{$group_login};
477 warn "Group does not exist!\a\n";
480 #if (defined($groupname{$group_login})) {
481 # &add_group($groupname{$group_login}, $name);
484 return ($group_login, $group_uniq) if $group_login eq $name;
485 return ($group_login, $group_login);
488 # return other groups (string)
489 sub new_users_groups {
490 local($name, $other_groups) = @_;
492 "Login group is ``$group_login''. Invite $name into other groups:";
494 local($new_groups,$groups);
496 $other_groups = "no" unless $other_groups;
499 $groups = &confirm_list($string, 1, $other_groups,
500 ("no", $other_groups, "guest"));
502 return "" if $groups eq "no";
504 ($flag, $new_groups) = &new_users_groups_valid($groups);
507 $new_groups =~ s/\s*$//;
511 sub new_users_groups_valid {
513 local($e, $new_groups);
516 foreach $e (split(/[,\s]+/, $groups)) {
517 # convert numbers to groupname
518 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
521 if (defined($groupname{$e})) {
522 if ($e eq $group_login) {
523 # do not add user to a group if this group
524 # is also the login group.
525 } elsif (&add_group($groupname{$e}, $name)) {
526 $new_groups .= "$e ";
528 warn "$name is already member of group ``$e''\n";
531 warn "Group ``$e'' does not exist\a\n"; $flag++;
534 return ($flag, $new_groups);
546 Gid: $g_id ($group_login)
548 Groups: $group_login $new_groups
553 return &confirm_yn("OK?", "yes");
556 # make password database
557 sub new_users_pwdmkdb {
558 local($last) = shift;
559 local($name) = shift;
561 system(@pwd_mkdb, '-u', $name, $etc_passwd);
564 warn "``@pwd_mkdb'' failed\n";
569 # update group database
570 sub new_users_group_update {
574 if (!defined($groupname{$group_login}) &&
575 !defined($gid{$groupname{$group_login}})) {
576 push(@group_backup, "$group_login:*:$g_id:");
577 $groupname{$group_login} = $g_id;
578 $gid{$g_id} = $group_login;
579 # $groupmembers{$g_id} = $group_login;
582 if ($new_groups || defined($groupname{$group_login}) ||
583 defined($gid{$groupname{$group_login}}) &&
584 $gid{$groupname{$group_login}} ne "+") {
585 # new user is member of some groups
586 # new login group is already in name space
587 rename($group, "$group.bak");
588 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
590 # Restore comments from the top of the group file
591 @a = @group_comments;
592 foreach $e (sort {$a <=> $b} (keys %gid)) {
593 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
595 &append_file($group, @a);
597 &append_file($group, "$group_login:*:$g_id:");
602 sub new_users_passwd_update {
603 # update passwd/group variables
604 push(@passwd_backup, $new_entry);
605 $username{$name} = $u_id;
607 $pwgid{$g_id} = $name;
610 # send message to new user
611 sub new_users_sendmessage {
612 return 1 if $send_message eq "no";
615 &confirm_list("Send message to ``$name'' and:",
616 1, "no", ("root", "second_mail_address", "no"));
618 $cc = "" if $cc eq "no";
620 foreach $e (@message_buffer) {
625 local(@message_buffer_append) = ();
626 if (!&confirm_yn("Add anything to default message", "no")) {
627 print "Use ``.'' or ^D alone on a line to finish your message.\n";
628 push(@message_buffer_append, "\n");
629 while($read = <STDIN>) {
630 last if $read eq "\.\n";
631 push(@message_buffer_append, $read);
635 &sendmessage("$name $cc", (@message_buffer, @message_buffer_append))
636 if (&confirm_yn("Send message", "yes"));
640 local($to, @message) = @_;
643 if (!open(M, "| mail -s Welcome $to")) {
644 warn "Cannot send mail to: $to!\n";
647 foreach $e (@message) {
648 print M eval "\"$e\"";
655 sub new_users_password {
658 return "" if $defaultpasswd ne "yes";
663 system("stty -echo");
664 $password = &confirm_list("Enter password", 1, "", "");
667 if ($password ne "") {
668 system("stty -echo");
669 $newpass = &confirm_list("Enter password again", 1, "", "");
672 last if $password eq $newpass;
673 print "They didn't match, please try again\n";
675 elsif (&confirm_yn("Use an empty password?", "yes")) {
686 print "\n" if $verbose;
687 print "Ok, let's go.\n" .
688 "Don't worry about mistakes. I will give you the chance later to " .
689 "correct any input.\n" if $verbose;
692 # fullname: Full name
694 # userhome: home path for user
698 # group_login: groupname of g_id
699 # new_groups: some other groups
700 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
702 local($groupmembers_bak, $cryptpwd);
703 local($new_users_ok) = 1;
707 $new_groups = "no" unless $groupname{$new_groups};
710 $name = &new_users_name;
711 $fullname = &new_users_fullname($name);
712 $sh = &new_users_shell;
713 $userhome = &new_users_home($name);
714 ($u_id, $g_id) = &new_users_id($name);
715 $class = &new_users_class($defaultclass);
716 ($group_login, $defaultgroup) =
717 &new_users_grplogin($name, $defaultgroup, $new_users_ok);
718 # do not use uniq username and login group
719 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
721 $new_groups = &new_users_groups($name, $new_groups);
722 $password = &new_users_password;
729 $cryptpwd = crypt($password, &salt) if $password ne "";
731 $new_entry = "$name\:" . "$cryptpwd" .
732 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$userhome:$sh";
733 &append_file($etc_passwd, "$new_entry");
734 &new_users_pwdmkdb("$new_entry", $name);
735 &new_users_group_update;
736 &new_users_passwd_update; print "Added user ``$name''\n";
737 &new_users_sendmessage;
738 &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname");
739 &home_create($userhome, $name, $group_login);
743 if (!&confirm_yn("Add another user?", "yes")) {
744 print "Goodbye!\n" if $verbose;
747 print "\n" if !$verbose;
751 # ask for password usage
752 sub password_default {
753 local($p) = $defaultpasswd;
755 $p = &confirm_yn("Use passwords", $defaultpasswd);
756 $changes++ unless $p;
758 return "yes" if (($defaultpasswd eq "yes" && $p) ||
759 ($defaultpasswd eq "no" && !$p));
760 return "no"; # otherwise
765 die "You are not root!\n" if $< && !$test;
778 [-message message_file]
781 [-s|-silent|-q|-quiet]
785 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
786 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
794 local($e, $last, @array);
796 foreach $e (sort @list) {
797 push(@array, $e) unless $e eq $last;
803 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
805 local($salt); # initialization
807 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
809 warn "calculate salt\n" if $verbose > 1;
811 for ($i = 0; $i < 27; $i++) {
812 srand(time + $rand + $$);
813 $rand = rand(25*29*17 + $rand);
814 $salt .= $itoa64[$rand & $#itoa64];
816 warn "Salt is: $salt\n" if $verbose > 1;
830 print "Use option ``-silent'' if you don't want to see " .
831 "all warnings and questions.\n\n";
833 print "Use option ``-verbose'' if you want to see more warnings and " .
834 "questions \nor try to repair bugs.\n\n";
839 sub parse_arguments {
842 while ($_ = $argv[0], /^-/) {
845 if (/^--?(v|verbose)$/) { $verbose = 1 }
846 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
847 elsif (/^--?(debug)$/) { $verbose = 2 }
848 elsif (/^--?(h|help|\?)$/) { &usage }
849 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
850 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
851 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
852 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
853 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
854 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
855 elsif (/^--?(check_only)$/) { $check_only = 1 }
856 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
858 elsif (/^--?(batch)$/) {
859 warn "The -batch option is not supported anymore.\n",
860 "Please use the pw(8) command line tool!\n";
864 elsif (/^--?(config_create)$/) { &create_conf; }
865 elsif (/^--?(noconfig)$/) { $config_read = 0; }
868 #&usage if $#argv < 0;
880 $name = &stripdir($name);
881 $name =~ s|/+[^/]+$||;
882 $name = "/" unless $name; # dirname of / is /
886 # return 1 if $file is a readable file or link
888 local($file, $verb) = @_;
891 if (-f $file || -l $file) {
893 warn "$file unreadable\n" if $verbose;
895 warn "$file is not a plain file or link\n" if $verbose;
901 # create configuration files and exit
904 if ($send_message ne 'no') {
905 &message_create($send_message);
907 &message_create($send_message_bak);
913 # log for new user in /var/log/adduser
918 return 1 if $logfile eq "no";
920 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
924 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
926 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
929 &append_file($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
932 # create HOME directory, copy dotfiles from $dotdir to $HOME
934 local($homedir, $name, $group) = @_;
938 warn "HOME directory ``$homedir'' already exists.\a\n";
942 # if the home directory prefix doesn't exist, create it
943 # First, split the directory into a list; then remove the user's dir
944 @dir = split('/', $homedir); pop(@dir);
945 # Put back together & strip to get directory prefix
946 $rootdir = &stripdir(join('/', @dir));
948 if (!&mkdirhier("$rootdir")) {
949 # warn already displayed
953 if ($dotdir eq 'no') {
954 if (!mkdir("$homedir", 0755)) {
955 warn "$dir: $!\n"; return 0;
957 system 'chown', "$name:$group", $homedir;
961 # copy files from $dotdir to $homedir
962 # rename 'dot.foo' files to '.foo'
963 print "Copy files from $dotdir to $homedir\n" if $verbose;
964 system('cp', '-R', $dotdir, $homedir);
965 system('chmod', '-R', 'u+wrX,go-w', $homedir);
966 system('chown', '-Rh', "$name:$group", $homedir);
969 opendir(D, $homedir);
970 foreach $file (readdir(D)) {
971 if ($file =~ /^dot\./ && -f "$homedir/$file") {
972 $file =~ s/^dot\././;
973 rename("$homedir/dot$file", "$homedir/$file");
975 chmod(0600, "$homedir/$file")
976 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
977 chmod(0700, "$homedir/$file")
978 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
984 # makes a directory hierarchy
987 $dir = &stripdir($dir);
988 local($user_partition) = "/usr";
989 local($dirname) = &dirname($dir);
991 -e $dirname || &mkdirhier($dirname);
993 if (((stat($dirname))[0]) == ((stat("/"))[0])){
994 # home partition is on root partition
995 # create home partition on $user_partition and make
996 # a symlink from $dir to $user_partition/`basename $dir`
997 # For instance: /home -> /usr/home
999 local($basename) = &basename($dir);
1000 local($d) = "$user_partition/$basename";
1004 warn "Oops, $d already exists.\n" if $verbose;
1006 print "Create $d\n" if $verbose;
1007 if (!mkdir("$d", 0755)) {
1008 warn "$d: $!\a\n"; return 0;
1012 unlink($dir); # symlink to nonexist file
1013 print "Create symlink: $dir -> $d\n" if $verbose;
1014 if (!symlink("$d", $dir)) {
1015 warn "Symlink $d: $!\a\n"; return 0;
1018 print "Create $dir\n" if $verbose;
1019 if (!mkdir("$dir", 0755)) {
1020 warn "Directory ``$dir'': $!\a\n"; return 0;
1030 $dir = &stripdir($dir);
1032 foreach $d (split('/', $dir)) {
1036 print "Create $dir\n" if $verbose;
1037 if (!mkdir("$dir", 0755)) {
1038 warn "$dir: $!\n"; return 0;
1047 # F.i.: //usr///home// -> /usr/home
1051 $dir =~ s|/+|/|g; # delete double '/'
1052 $dir =~ s|/$||; # delete '/' at end
1053 return $dir if $dir ne "";
1057 # Read one of the elements from @list. $confirm is default.
1058 # If !$allow accept only elements from @list.
1060 local($message, $allow, $confirm, @list) = @_;
1061 local($read, $c, $print);
1063 $print = "$message" if $message;
1064 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1066 $print .= join($", &uniq(@list)); #"
1067 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1069 print "\n" if (length($print) + length($confirm)) > 60;
1070 print "[$confirm]: ";
1072 chop($read = <STDIN>);
1075 return $confirm if $read eq "";
1076 return "$read" if $allow;
1078 foreach $c (@list) {
1079 return $read if $c eq $read;
1081 warn "$read: is not allowed!\a\n";
1082 return &confirm_list($message, $allow, $confirm, @list);
1085 # YES or NO question
1086 # return 1 if &confirm("message", "yes") and answer is yes
1087 # or if &confirm("message", "no") an answer is no
1090 local($message, $confirm) = @_;
1091 local($yes) = '^(yes|YES|y|Y)$';
1092 local($no) = '^(no|NO|n|N)$';
1095 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1100 print "$message (y/n) [$confirm]: ";
1101 chop($read = <STDIN>);
1104 return 1 unless $read;
1106 if (($confirm eq "y" && $read =~ "$yes") ||
1107 ($confirm eq "n" && $read =~ "$no")) {
1111 if ($read !~ "$yes" && $read !~ "$no") {
1112 warn "Wrong value. Enter again!\a\n";
1113 return &confirm_yn($message, $confirm);
1118 # allow configuring usernameregexp
1119 sub usernameregexp_default {
1120 local($r) = $usernameregexp;
1123 $r = &confirm_list("Usernames must match regular expression:", 1,
1125 eval "'foo' =~ /$r/";
1127 warn "Invalid regular expression\a\n";
1129 $changes++ if $r ne $usernameregexp;
1133 # test if $dotdir exist
1134 # return "no" if $dotdir not exist or dotfiles should not copied
1135 sub dotdir_default {
1136 local($dir) = $dotdir;
1138 return &dotdir_default_valid($dir) unless $verbose;
1140 $dir = &confirm_list("Copy dotfiles from:", 1,
1141 $dir, ("no", $dotdir_bak, $dir));
1142 last if $dir eq &dotdir_default_valid($dir);
1144 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1146 $changes++ if $dir ne $dotdir;
1150 sub dotdir_default_valid {
1153 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1154 return $dir if $dir eq "no";
1155 warn "Dotdir ``$dir'' is not a directory\a\n";
1159 # ask for messages to new users
1160 sub message_default {
1161 local($file) = $send_message;
1162 local(@d) = ($file, $send_message_bak, "no");
1165 $file = &confirm_list("Send message from file:", 1, $file, @d);
1166 last if $file eq "no";
1167 last if &filetest($file, 1);
1169 # maybe create message file
1170 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1171 last if &filetest($file, 0);
1172 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1176 if ($file eq "no" || !&filetest($file, 0)) {
1177 warn "Do not send message\n" if $verbose;
1180 &message_read($file);
1183 $changes++ if $file ne $send_message && $verbose;
1187 # create message file
1188 sub message_create {
1191 rename($file, "$file.bak");
1192 if (!open(M, "> $file")) {
1193 warn "Messagefile ``$file'': $!\n"; return 0;
1197 # Message file for adduser(8)
1199 # default variables: \$name, \$fullname, \$password
1200 # other variables: see /etc/adduser.conf after
1201 # line ``$do_not_delete''
1206 your account ``\$name'' was created.
1209 See also chpass(1), finger(1), passwd(1)
1215 # read message file into buffer
1218 @message_buffer = '';
1220 if (!open(R, "$file")) {
1221 warn "File ``$file'':$!\n"; return 0;
1224 push(@message_buffer, $_) unless /^\s*#/;
1229 # write @list to $file with file-locking
1231 local($file,@list) = @_;
1233 local($LOCK_EX) = 2;
1234 local($LOCK_NB) = 4;
1235 local($LOCK_UN) = 8;
1237 open(F, ">> $file") || die "$file: $!\n";
1238 print "Lock $file.\n" if $verbose > 1;
1239 while(!flock(F, $LOCK_EX | $LOCK_NB)) {
1240 warn "Cannot lock file: $file\a\n";
1241 die "Sorry, give up\n"
1242 unless &confirm_yn("Try again?", "yes");
1244 print F join("\n", @list) . "\n";
1246 print "Unlock $file.\n" if $verbose > 1;
1250 # return free uid+gid
1251 # uid == gid if possible
1255 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1256 # looking for next free uid
1257 while($uid{$uid_start}) {
1259 $uid_start = 1000 if $uid_start >= $uid_end;
1260 print "$uid_start\n" if $verbose > 1;
1263 local($gid_start) = $uid_start;
1264 # group for user (username==groupname) already exist
1265 if ($groupname{$group}) {
1266 $gid_start = $groupname{$group};
1268 # gid is in use, looking for another gid.
1269 # Note: uid an gid are not equal
1270 elsif ($gid{$uid_start}) {
1271 while($gid{$gid_start} || $uid{$gid_start}) {
1273 $gid_start = $uid_end if $gid_start < 100;
1276 return ($uid_start, $gid_start);
1282 local($user_flag) = 0;
1284 # don't read config file
1285 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1287 if(!open(C, "$config")) {
1288 warn "$config: $!\n"; return 0;
1292 # user defined variables
1293 /^$do_not_delete/ && $user_flag++;
1294 # found @array or $variable
1295 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1299 # lines with '^##' are not saved
1300 push(@user_variable_list, $_)
1301 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1303 #warn "X @user_variable_list X\n";
1310 local($silent) = @_;
1313 return 1 unless ($changes || ! -e $config || !$config_read || $silent);
1317 return 1 if &confirm_yn("\nWrite your changes to $config?", "no");
1320 &confirm_yn("\nWrite your configuration to $config?", "yes");
1324 rename($config, "$config.bak");
1325 open(C, "> $config") || die "$config: $!\n";
1327 # prepare some variables
1328 $send_message = "no" unless $send_message;
1329 $defaultpasswd = "no" unless $defaultpasswd;
1330 local($shpref) = "'" . join("', '", @shellpref) . "'";
1331 local($shpath) = "'" . join("', '", @path) . "'";
1332 local($user_var) = join('', @user_variable_list);
1336 # $config - automatic generated by adduser(8)
1338 # Note: adduser read *and* write this file.
1339 # You may change values, but don't add new things before the
1340 # line ``$do_not_delete''
1346 # regular expression usernames are checked against (see perlre(1))
1347 # usernameregexp = 'regexp'
1348 usernameregexp = '$usernameregexp'
1350 # use password for new users
1351 # defaultpasswd = yes | no
1352 defaultpasswd = $defaultpasswd
1354 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1357 # send this file to new user ("/etc/adduser.message" or "no")
1358 send_message = "$send_message"
1360 # config file for adduser ("/etc/adduser.conf")
1363 # logfile ("/var/log/adduser" or "no")
1364 logfile = "$logfile"
1366 # default HOME directory ("/home")
1369 # List of directories where shells located
1370 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1373 # common shell list, first element has higher priority
1374 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1375 shellpref = ($shpref)
1377 # defaultshell if not empty ("bash")
1378 defaultshell = "$defaultshell"
1380 # defaultgroup ('USER' for same as username or any other valid group)
1381 defaultgroup = $defaultgroup
1383 # defaultclass if not empty
1384 defaultclass = "$defaultclass"
1386 # new users get this uid (1000)
1387 uid_start = "$uid_start"
1390 ## your own variables, see /etc/adduser.message
1401 $test = 0; # test mode, only for development
1404 &check_root; # you must be root to run this script!
1405 &variables; # initialize variables
1406 &config_read(@ARGV); # read variables form config-file
1407 &parse_arguments(@ARGV); # parse arguments
1415 &passwd_check; # check for valid passwdb
1416 &shells_read; # read /etc/shells
1417 &passwd_read; # read /etc/master.passwd
1418 &group_read; # read /etc/group
1419 &group_check; # check for incon*
1420 exit 0 if $check_only; # only check consistence and exit
1425 $usernameregexp = &usernameregexp_default; # regexp to check usernames against
1426 &shells_add; # maybe add some new shells
1427 $defaultshell = &shell_default; # enter default shell
1428 $home = &home_partition($home); # find HOME partition
1429 $dotdir = &dotdir_default; # check $dotdir
1430 $send_message = &message_default; # send message to new user
1431 $defaultpasswd = &password_default; # maybe use password
1432 &config_write(!$verbose); # write variables in file
1434 # main loop for creating new users
1435 &new_users; # add new users