]>
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
27 # $Id: adduser.perl,v 1.30 1997/05/01 23:23:06 ache Exp $
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 # only for me (=Wolfram)
79 $home = "/home/w/tmp/adduser/home";
80 $etc_shells = "./shells";
81 $etc_passwd = "./master.passwd";
83 $pwd_mkdb = "pwd_mkdb -p -d .";
84 $config = "adduser.conf";
85 $send_message = "./adduser.message";
86 $logfile = "./log.adduser";
89 umask 022; # don't give login group write access
91 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
95 @user_variable_list = ''; # user variables in /etc/adduser.conf
96 $do_not_delete = '## DO NOT DELETE THIS LINE!';
99 # read shell database, see also: shells(5)
104 print "Check $etc_shells\n" if $verbose;
105 open(S
, $etc_shells) || die "$etc_shells:$!\n";
109 s/^\s*//; s/\s+.*//; # chop
112 $shell{&basename
($sh)} = $sh;
114 warn "Shell: $sh not executable!\n";
120 # Allow /nonexistent and /bin/date as a valid shell for system utils
121 push(@list, "/nonexistent");
122 push(@shellpref, "no");
123 $shell{"no"} = "/nonexistent";
125 push(@list, "/bin/date");
126 push(@shellpref, "date");
127 $shell{"date"} = "/bin/date";
132 # add new shells if possible
134 local($sh,$dir,@list);
136 return 1 unless $verbose;
138 foreach $sh (@shellpref) {
141 # shell $sh is not defined as login shell
142 foreach $dir (@path) {
145 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
146 push(@list, "$dir/$sh");
147 push(@shellpref, "$sh");
148 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
155 &append_file
($etc_shells, @list) if $#list >= 0;
158 # choose your favourite shell and return the shell
160 local($e,$i,$new_shell);
163 $sh = &shell_default_valid
($defaultshell);
164 return $sh unless $verbose;
166 $new_shell = &confirm_list
("Enter your default shell:", 0,
167 $sh, sort(keys %shell));
168 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
169 $changes++ if $new_shell ne $sh;
173 sub shell_default_valid
{
177 return $sh if $shell{$sh};
179 foreach $e (@shellpref) {
181 last if defined($shell{$s});
184 warn "Shell ``$sh'' is undefined, use ``$s''\n";
188 # return default home partition (f.e. "/home")
189 # create base directory if nesseccary
192 $home = &stripdir
($home);
195 return $h if !$verbose && $h eq &home_partition_valid
($h);
198 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
200 last if $h eq &home_partition_valid
($h);
203 $changes++ if $h ne $home;
207 sub home_partition_valid
{
212 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
216 warn "Please use absolute path for home: ``$h''.\a\n";
221 warn "$h exists, but is not a directory or symlink!\n"
222 unless -d
$h || -l
$h;
223 warn "$h is not writable!\n"
227 # create home partition
228 return $h if &mkdir_home
($h);
233 # check for valid passwddb
235 system("$pwd_mkdb -c $etc_passwd");
236 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
241 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
243 print "Check $etc_passwd\n" if $verbose;
244 open(P
, "$etc_passwd") || die "$passwd: $!\n";
248 push(@passwd_backup, $_);
253 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
255 print "$p_username already exists with uid: $username{$p_username}!\n"
256 if $username{$p_username} && $verbose;
257 $username{$p_username} = $p_uid;
258 print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
259 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
260 print "User $p_username: illegal shell: ``$sh''\n"
261 if ($verbose && $sh &&
262 !$shell{&basename
($sh)} &&
263 $p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
264 $sh !~ /\/(pppd
|sliplogin
)$/);
265 $uid{$p_uid} = $p_username;
266 $pwgid{$p_gid} = $p_username;
273 local($g_groupname,$pw,$g_gid, $memb);
275 print "Check $group\n" if $verbose;
276 open(G
, "$group") || die "$group: $!\n";
279 push(@group_backup, $_);
284 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
286 $groupmembers{$g_gid} = $memb;
287 warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
288 if $groupname{$g_groupname} && $verbose;
289 $groupname{$g_groupname} = $g_gid;
290 warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
291 if $gid{$g_gid} && $verbose;
292 $gid{$g_gid} = $g_groupname;
297 # check gids /etc/passwd <-> /etc/group
299 local($c_gid, $c_username, @list);
301 foreach $c_gid (keys %pwgid) {
303 $c_username = $pwgid{$c_gid};
304 warn "User ``$c_username'' has gid $c_gid but a group with this " .
305 "gid does not exist.\n" if $verbose;
311 # main loop for creating new users
319 $name = &confirm_list
("Enter username", 1, "a-z0-9_-", "");
320 if (length($name) > 16) {
321 warn "Username is longer than 16 chars\a\n";
324 last if (&new_users_name_valid
($name) eq $name);
329 sub new_users_name_valid
{
332 if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/) {
333 warn "Wrong username. " .
334 "Please use only lowercase characters or digits\a\n";
336 } elsif ($username{$name}) {
337 warn "Username ``$name'' already exists!\a\n"; return 0;
343 sub new_users_fullname
{
348 $fullname = &confirm_list
("Enter full name", 1, "", "");
349 last if $fullname eq &new_users_fullname_valid
($fullname);
351 $fullname = $name unless $fullname;
355 sub new_users_fullname_valid
{
356 local($fullname) = @_;
358 return $fullname if $fullname !~ /:/;
360 warn "``:'' is not allowed!\a\n";
364 # return shell (full path) for user
365 sub new_users_shell
{
368 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
372 # return free uid and gid
375 local($u_id, $g_id) = &next_id
($name);
376 local($u_id_tmp, $e);
379 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
380 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
382 if ($uid{$u_id_tmp}) {
383 warn "Uid ``$u_id_tmp'' in use!\a\n";
385 warn "Wrong uid.\a\n";
389 return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
391 $uid_start = $u_id_tmp;
392 return &next_id
($name);
395 # return login class for user
396 sub new_users_class
{
400 $class = &confirm_list
("Enter login class:", 1, $def, ($def, "default"));
401 $class = "" if $class eq "default";
407 local($gid, $name) = @_;
410 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
412 $groupmembers_bak{$gid} = $groupmembers{$gid};
413 $groupmembers{$gid} .= "," if $groupmembers{$gid};
414 $groupmembers{$gid} .= "$name";
421 sub new_users_grplogin
{
422 local($name, $defaultgroup, $new_users_ok) = @_;
423 local($group_login, $group);
426 $group = $defaultgroup if $defaultgroup ne $group_uniq;
430 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
432 # restore old groupmembers, user was not accept
433 foreach $e (keys %groupmembers_bak) {
434 $groupmembers{$e} = $groupmembers_bak{$e};
439 $group_login = &confirm_list
("Login group", 1, $group,
441 last if $group_login eq $group;
442 last if $group_login eq $name;
443 last if defined $groupname{$group_login};
444 if ($group_login eq $group_uniq) {
445 $group_login = $name; last;
448 if (defined $gid{$group_login}) {
449 # convert numeric groupname (gid) to groupname
450 $group_login = $gid{$group_login};
453 warn "Group does not exist!\a\n";
456 #if (defined($groupname{$group_login})) {
457 # &add_group($groupname{$group_login}, $name);
460 return ($group_login, $group_uniq) if $group_login eq $name;
461 return ($group_login, $group_login);
465 sub new_users_grplogin_batch
{
466 local($name, $defaultgroup) = @_;
467 local($group_login, $group);
469 $group_login = $name;
470 $group_login = $defaultgroup if $defaultgroup ne $group_uniq;
472 if (defined $gid{$group_login}) {
473 # convert numeric groupname (gid) to groupname
474 $group_login = $gid{$group_login};
477 # if (defined($groupname{$group_login})) {
478 # &add_group($groupname{$group_login}, $name);
482 if defined($groupname{$group_login}) || $group_login eq $name;
483 warn "Group ``$group_login'' does not exist\a\n";
487 # return other groups (string)
488 sub new_users_groups
{
489 local($name, $other_groups) = @_;
491 "Login group is ``$group_login''. Invite $name into other groups:";
493 local($new_groups,$groups);
495 $other_groups = "no" unless $other_groups;
498 $groups = &confirm_list
($string, 1, $other_groups,
499 ("no", $other_groups, "guest"));
501 return "" if $groups eq "no";
503 ($flag, $new_groups) = &new_users_groups_valid
($groups);
506 $new_groups =~ s/\s*$//;
510 sub new_users_groups_valid
{
512 local($e, $new_groups);
515 foreach $e (split(/[,\s]+/, $groups)) {
516 # convert numbers to groupname
517 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
520 if (defined($groupname{$e})) {
521 if ($e eq $group_login) {
522 # do not add user to a group if this group
523 # is also the login group.
524 } elsif (&add_group
($groupname{$e}, $name)) {
525 $new_groups .= "$e ";
527 warn "$name is already member of group ``$e''\n";
530 warn "Group ``$e'' does not exist\a\n"; $flag++;
533 return ($flag, $new_groups);
545 Gid: $g_id ($group_login)
547 Groups: $group_login $new_groups
552 return &confirm_yn
("OK?", "yes");
555 # make password database
556 sub new_users_pwdmkdb
{
559 system("$pwd_mkdb $etc_passwd");
562 warn "``$pwd_mkdb'' failed\n";
567 # update group database
568 sub new_users_group_update
{
572 if (!defined($groupname{$group_login}) &&
573 !defined($gid{$groupname{$group_login}})) {
574 push(@group_backup, "$group_login:*:$g_id:");
575 $groupname{$group_login} = $g_id;
576 $gid{$g_id} = $group_login;
577 # $groupmembers{$g_id} = $group_login;
580 if ($new_groups || defined($groupname{$group_login}) ||
581 defined($gid{$groupname{$group_login}}) &&
582 $gid{$groupname{$group_login}} ne "+") {
583 # new user is member of some groups
584 # new login group is already in name space
585 rename($group, "$group.bak");
586 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
587 foreach $e (sort {$a <=> $b} (keys %gid)) {
588 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
590 &append_file
($group, @a);
592 &append_file
($group, "$group_login:*:$g_id:");
597 sub new_users_passwd_update
{
598 # update passwd/group variables
599 push(@passwd_backup, $new_entry);
600 $username{$name} = $u_id;
602 $pwgid{$g_id} = $name;
605 # send message to new user
606 sub new_users_sendmessage
{
607 return 1 if $send_message eq "no";
610 &confirm_list
("Send message to ``$name'' and:",
611 1, "no", ("root", "second_mail_address", "no"));
613 $cc = "" if $cc eq "no";
615 foreach $e (@message_buffer) {
620 local(@message_buffer_append) = ();
621 if (!&confirm_yn
("Add anything to default message", "no")) {
622 print "Use ``.'' or ^D alone on a line to finish your message.\n";
623 push(@message_buffer_append, "\n");
624 while($read = <STDIN
>) {
625 last if $read eq "\.\n";
626 push(@message_buffer_append, $read);
630 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
631 if (&confirm_yn
("Send message", "yes"));
635 local($to, @message) = @_;
638 if (!open(M
, "| mail -s Welcome $to")) {
639 warn "Cannot send mail to: $to!\n";
642 foreach $e (@message) {
643 print M
eval "\"$e\"";
650 sub new_users_password
{
653 return "" if $defaultpasswd ne "yes";
658 system("stty -echo");
659 $password = &confirm_list
("Enter password", 1, "", "");
662 if ($password ne "") {
663 system("stty -echo");
664 $newpass = &confirm_list
("Enter password again", 1, "", "");
667 last if $password eq $newpass;
668 print "They didn't match, please try again\n";
670 elsif (&confirm_yn
("Use an empty password?", "yes")) {
681 print "\n" if $verbose;
682 print "Ok, let's go.\n" .
683 "Don't worry about mistakes. I will give you the chance later to " .
684 "correct any input.\n" if $verbose;
687 # fullname: Full name
692 # group_login: groupname of g_id
693 # new_groups: some other groups
694 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups);
695 local($groupmembers_bak, $cryptpwd);
696 local($new_users_ok) = 1;
700 $new_groups = "no" unless $groupname{$new_groups};
703 $name = &new_users_name
;
704 $fullname = &new_users_fullname
($name);
705 $sh = &new_users_shell
;
706 ($u_id, $g_id) = &new_users_id
($name);
707 $class = &new_users_class
($defaultclass);
708 ($group_login, $defaultgroup) =
709 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
710 # do not use uniq username and login group
711 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
713 $new_groups = &new_users_groups
($name, $new_groups);
714 $password = &new_users_password
;
721 $cryptpwd = crypt($password, &salt
) if $password ne "";
723 $new_entry = "$name\:" . "$cryptpwd" .
724 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$home/$name:$sh";
725 &append_file
($etc_passwd, "$new_entry");
726 &new_users_pwdmkdb
("$new_entry");
727 &new_users_group_update
;
728 &new_users_passwd_update
; print "Added user ``$name''\n";
729 &new_users_sendmessage
;
730 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
731 &home_create
($name, $group_login);
735 if (!&confirm_yn
("Add another user?", "yes")) {
736 print "Goodbye!\n" if $verbose;
739 print "\n" if !$verbose;
744 local($name, $groups, $class, $fullname, $password) = @_;
747 $defaultshell = &shell_default_valid
($defaultshell);
748 return 0 unless $home = &home_partition_valid
($home);
749 return 0 if $dotdir ne &dotdir_default_valid
($dotdir);
750 $send_message = &message_default
;
752 return 0 if $name ne &new_users_name_valid
($name);
753 $sh = $shell{$defaultshell};
754 ($u_id, $g_id) = &next_id
($name);
755 $group_login = &new_users_grplogin_batch
($name, $defaultgroup);
756 return 0 unless $group_login;
757 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
758 ($flag, $new_groups) = &new_users_groups_valid
($groups);
761 $class = $defaultclass if $class eq "";
763 $cryptpwd = crypt($password, &salt
) if $password ne "";
765 $new_entry = "$name\:" . "$cryptpwd" .
766 "\:$u_id\:$g_id\:$class\:0:0:$fullname:$home/$name:$sh";
767 &append_file
($etc_passwd, "$new_entry");
768 &new_users_pwdmkdb
("$new_entry");
769 &new_users_group_update
;
770 &new_users_passwd_update
; print "Added user ``$name''\n";
771 &sendmessage
($name, @message_buffer) if $send_message ne "no";
772 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
773 &home_create
($name, $group_login);
776 # ask for password usage
777 sub password_default
{
778 local($p) = $defaultpasswd;
780 $p = &confirm_yn
("Use passwords", $defaultpasswd);
781 $changes++ unless $p;
783 return "yes" if (($defaultpasswd eq "yes" && $p) ||
784 ($defaultpasswd eq "no" && !$p));
785 return "no"; # otherwise
790 die "You are not root!\n" if $< && !$test;
796 [-batch username [group[,group]...] [class] [fullname] [password]]
804 [-message message_file]
807 [-s|-silent|-q|-quiet]
811 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
812 login_class=$defaultclass message_file=$send_message uid_start=$uid_start
820 local($e, $last, @array);
822 foreach $e (sort @list) {
823 push(@array, $e) unless $e eq $last;
829 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
831 local($salt); # initialization
833 local(@itoa64) = ( 0 .. 9, a
.. z
, A
.. Z
); # 0 .. 63
835 warn "calculate salt\n" if $verbose > 1;
837 for ($i = 0; $i < 8; $i++) {
838 srand(time + $rand + $$);
839 $rand = rand(25*29*17 + $rand);
840 $salt .= $itoa64[$rand & $#itoa64];
842 warn "Salt is: $salt\n" if $verbose > 1;
856 print "Use option ``-silent'' if you don't want see " .
857 "all warnings & questions.\n\n";
859 print "Use option ``-verbose'' if you want see more warnings & " .
860 "questions \nor try to repair bugs.\n\n";
865 sub parse_arguments
{
868 while ($_ = $argv[0], /^-/) {
871 if (/^--?(v|verbose)$/) { $verbose = 1 }
872 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
873 elsif (/^--?(debug)$/) { $verbose = 2 }
874 elsif (/^--?(h|help|\?)$/) { &usage
}
875 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
876 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
877 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
878 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
879 elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv }
880 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
881 elsif (/^--?(check_only)$/) { $check_only = 1 }
882 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
884 elsif (/^--?(batch)$/) {
885 @batch = splice(@argv, 0, 5); $verbose = 0;
886 die "batch: too few arguments\n" if $#batch < 0;
889 elsif (/^--?(config_create)$/) { &create_conf
; }
890 elsif (/^--?(noconfig)$/) { $config_read = 0; }
893 #&usage if $#argv < 0;
905 $name = &stripdir
($name);
906 $name =~ s
|/+[^/]+$||;
907 $name = "/" unless $name; # dirname of / is /
911 # return 1 if $file is a readable file or link
913 local($file, $verb) = @_;
916 if (-f
$file || -l
$file) {
918 warn "$file unreadable\n" if $verbose;
920 warn "$file is not a plain file or link\n" if $verbose;
926 # create configuration files and exit
929 if ($send_message ne 'no') {
930 &message_create
($send_message);
932 &message_create
($send_message_bak);
938 # log for new user in /var/log/adduser
943 return 1 if $logfile eq "no";
945 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
948 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
950 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
953 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
956 # create HOME directory, copy dotfiles from $dotdir to $HOME
958 local($name, $group) = @_;
959 local($homedir) = "$home/$name";
962 warn "HOME Directory ``$homedir'' already exist\a\n";
966 if ($dotdir eq 'no') {
967 if (!mkdir("$homedir",0755)) {
968 warn "mkdir $homedir: $!\n"; return 0;
970 system 'chown', "$name:$group", $homedir;
974 # copy files from $dotdir to $homedir
975 # rename 'dot.foo' files to '.foo'
976 print "Copy files from $dotdir to $homedir\n" if $verbose;
977 system("cp -r $dotdir $homedir");
978 system("chmod -R u+wrX,go-w $homedir");
979 system("chown -R $name:$group $homedir");
982 opendir(D
, $homedir);
983 foreach $file (readdir(D
)) {
984 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
985 $file =~ s/^dot\././;
986 rename("$homedir/dot$file", "$homedir/$file");
988 chmod(0600, "$homedir/$file")
989 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
990 chmod(0700, "$homedir/$file")
991 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
997 # makes a directory hierarchy
1000 $dir = &stripdir
($dir);
1001 local($user_partition) = "/usr";
1002 local($dirname) = &dirname
($dir);
1005 -e
$dirname || &mkdirhier
($dirname);
1007 if (((stat($dirname))[0]) == ((stat("/"))[0])){
1008 # home partition is on root partition
1009 # create home partition on $user_partition and make
1010 # a symlink from $dir to $user_partition/`basename $dir`
1011 # For instance: /home -> /usr/home
1013 local($basename) = &basename
($dir);
1014 local($d) = "$user_partition/$basename";
1018 warn "Oops, $d already exist\n" if $verbose;
1020 print "Create $d\n" if $verbose;
1021 if (!mkdir("$d", 0755)) {
1022 warn "$d: $!\a\n"; return 0;
1026 unlink($dir); # symlink to nonexist file
1027 print "Create symlink: $dir -> $d\n" if $verbose;
1028 if (!symlink("$d", $dir)) {
1029 warn "Symlink $d: $!\a\n"; return 0;
1032 print "Create $dir\n" if $verbose;
1033 if (!mkdir("$dir", 0755)) {
1034 warn "Directory ``$dir'': $!\a\n"; return 0;
1044 $dir = &stripdir
($dir);
1046 foreach $d (split('/', $dir)) {
1050 print "Create $dir\n" if $verbose;
1051 if (!mkdir("$dir", 0755)) {
1052 warn "$dir: $!\n"; return 0;
1061 # F.i.: //usr///home// -> /usr/home
1065 $dir =~ s
|/+|/|g
; # delete double '/'
1066 $dir =~ s
|/$||; # delete '/' at end
1067 return $dir if $dir ne "";
1071 # Read one of the elements from @list. $confirm is default.
1072 # If !$allow accept only elements from @list.
1074 local($message, $allow, $confirm, @list) = @_;
1075 local($read, $c, $print);
1077 $print = "$message" if $message;
1078 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1080 $print .= join($", &uniq(@list)); #"
1081 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1083 print "\n" if (length($print) + length($confirm)) > 60;
1084 print "[$confirm]: ";
1086 chop($read = <STDIN>);
1089 return $confirm if $read eq "";
1090 return "$read" if $allow;
1092 foreach $c (@list) {
1093 return $read if $c eq $read;
1095 warn "$read: is not allowed!\a\n";
1096 return &confirm_list($message, $allow, $confirm, @list);
1099 # YES or NO question
1100 # return 1 if &confirm("message", "yes") and answer is yes
1101 # or if &confirm("message", "no") an answer is no
1104 local($message, $confirm) = @_;
1105 local($yes) = '^(yes
|YES
|y
|Y
)$';
1106 local($no) = '^(no|NO
|n
|N
)$';
1109 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1114 print "$message (y/n) [$confirm]: ";
1115 chop($read = <STDIN>);
1118 return 1 unless $read;
1120 if (($confirm eq "y" && $read =~ "$yes") ||
1121 ($confirm eq "n" && $read =~ "$no")) {
1125 if ($read !~ "$yes" && $read !~ "$no") {
1126 warn "Wrong value. Enter again!\a\n";
1127 return &confirm_yn($message, $confirm);
1132 # test if $dotdir exist
1133 # return "no" if $dotdir not exist or dotfiles should not copied
1134 sub dotdir_default {
1135 local($dir) = $dotdir;
1137 return &dotdir_default_valid($dir) unless $verbose;
1139 $dir = &confirm_list("Copy dotfiles from:", 1,
1140 $dir, ("no", $dotdir_bak, $dir));
1141 last if $dir eq &dotdir_default_valid($dir);
1143 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1145 $changes++ if $dir ne $dotdir;
1149 sub dotdir_default_valid {
1152 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1153 return $dir if $dir eq "no";
1154 warn "Dotdir ``$dir'' is not a directory\a\n";
1158 # ask for messages to new users
1159 sub message_default {
1160 local($file) = $send_message;
1161 local(@d) = ($file, $send_message_bak, "no");
1164 $file = &confirm_list("Send message from file:", 1, $file, @d);
1165 last if $file eq "no";
1166 last if &filetest($file, 1);
1168 # maybe create message file
1169 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1170 last if &filetest($file, 0);
1171 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1175 if ($file eq "no" || !&filetest($file, 0)) {
1176 warn "Do not send message\n" if $verbose;
1179 &message_read($file);
1182 $changes++ if $file ne $send_message && $verbose;
1186 # create message file
1187 sub message_create {
1190 rename($file, "$file.bak");
1191 if (!open(M, "> $file")) {
1192 warn "Messagefile ``$file'': $!\n"; return 0;
1196 # Message file for adduser(8)
1198 # default variables: \$name, \$fullname, \$password
1199 # other variables: see /etc/adduser.conf after
1200 # line ``$do_not_delete''
1205 your account ``\$name'' was created.
1208 See also chpass(1), finger(1), passwd(1)
1214 # read message file into buffer
1217 @message_buffer = '';
1219 if (!open(R
, "$file")) {
1220 warn "File ``$file'':$!\n"; return 0;
1223 push(@message_buffer, $_) unless /^\s*#/;
1228 # write @list to $file with file-locking
1230 local($file,@list) = @_;
1232 local($LOCK_EX) = 2;
1233 local($LOCK_NB) = 4;
1234 local($LOCK_UN) = 8;
1236 open(F
, ">> $file") || die "$file: $!\n";
1237 print "Lock $file.\n" if $verbose > 1;
1238 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1239 warn "Cannot lock file: $file\a\n";
1240 die "Sorry, give up\n"
1241 unless &confirm_yn
("Try again?", "yes");
1243 print F
join("\n", @list) . "\n";
1245 print "Unlock $file.\n" if $verbose > 1;
1249 # return free uid+gid
1250 # uid == gid if possible
1254 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1255 # looking for next free uid
1256 while($uid{$uid_start}) {
1258 $uid_start = 1000 if $uid_start >= $uid_end;
1259 print "$uid_start\n" if $verbose > 1;
1262 local($gid_start) = $uid_start;
1263 # group for user (username==groupname) already exist
1264 if ($groupname{$group}) {
1265 $gid_start = $groupname{$group};
1267 # gid is in use, looking for another gid.
1268 # Note: uid an gid are not equal
1269 elsif ($gid{$uid_start}) {
1270 while($gid{$gid_start} || $uid{$gid_start}) {
1272 $gid_start = $uid_end if $gid_start < 100;
1275 return ($uid_start, $gid_start);
1281 local($user_flag) = 0;
1283 # don't read config file
1284 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1286 if(!open(C
, "$config")) {
1287 warn "$config: $!\n"; return 0;
1291 # user defined variables
1292 /^$do_not_delete/ && $user_flag++;
1293 # found @array or $variable
1294 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1298 # lines with '^##' are not saved
1299 push(@user_variable_list, $_)
1300 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1302 #warn "X @user_variable_list X\n";
1309 local($silent) = @_;
1312 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1316 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1319 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1323 rename($config, "$config.bak");
1324 open(C
, "> $config") || die "$config: $!\n";
1326 # prepare some variables
1327 $send_message = "no" unless $send_message;
1328 $defaultpasswd = "no" unless $defaultpasswd;
1329 local($shpref) = "'" . join("', '", @shellpref) . "'";
1330 local($shpath) = "'" . join("', '", @path) . "'";
1331 local($user_var) = join('', @user_variable_list);
1335 # $config - automatic generated by adduser(8)
1337 # Note: adduser read *and* write this file.
1338 # You may change values, but don't add new things befor the
1339 # line ``$do_not_delete''
1345 # use password for new users
1346 # defaultpasswd = yes | no
1347 defaultpasswd = $defaultpasswd
1349 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1352 # send this file to new user ("/etc/adduser.message" or "no")
1353 send_message = "$send_message"
1355 # config file for adduser ("/etc/adduser.conf")
1358 # logfile ("/var/log/adduser" or "no")
1359 logfile = "$logfile"
1361 # default HOME directory ("/home")
1364 # List of directories where shells located
1365 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1368 # common shell list, first element has higher priority
1369 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1370 shellpref = ($shpref)
1372 # defaultshell if not empty ("bash")
1373 defaultshell = "$defaultshell"
1375 # defaultgroup ('USER' for same as username or any other valid group)
1376 defaultgroup = $defaultgroup
1378 # defaultclass if not empty
1379 defaultclass = "$defaultclass"
1381 # new users get this uid (1000)
1385 ## your own variables, see /etc/adduser.message
1396 $test = 0; # test mode, only for development
1399 &check_root
; # you must be root to run this script!
1400 &variables
; # initialize variables
1401 &config_read
(@ARGV); # read variables form config-file
1402 &parse_arguments
(@ARGV); # parse arguments
1404 if (!$check_only && $#batch < 0) {
1410 &passwd_check
; # check for valid passwdb
1411 &shells_read
; # read /etc/shells
1412 &passwd_read
; # read /etc/master.passwd
1413 &group_read
; # read /etc/group
1414 &group_check
; # check for incon*
1415 exit 0 if $check_only; # only check consistence and exit
1417 exit(!&batch
(@batch)) if $#batch >= 0; # batch mode
1421 &shells_add
; # maybe add some new shells
1422 $defaultshell = &shell_default
; # enter default shell
1423 $home = &home_partition
($home); # find HOME partition
1424 $dotdir = &dotdir_default
; # check $dotdir
1425 $send_message = &message_default
; # send message to new user
1426 $defaultpasswd = &password_default
; # maybe use password
1427 &config_write
(!$verbose); # write variables in file
1429 # main loop for creating new users
1430 &new_users
; # add new users