]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/adduser.perl
3 # Copyright (c) 1995 Wolfram Schneider. All rights reserved.
4 # Alle Rechte vorbehalten. Es gilt das kontinentaleuropische Urheberrecht.
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.
14 # 3. All advertising materials mentioning features or use of this software
15 # must display the following acknowledgement:
16 # This product includes software developed by Wolfram Schneider
17 # 4. The name of the author may not be used to endorse or promote products
18 # derived from this software without specific prior written permission
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # /usr/sbin/adduser - add new user(s)
33 # Email: Wolfram Schneider <wosch@cs.tu-berlin.de>
35 # $Id: adduser.perl,v 1.4 1995/03/08 22:44:37 ache Exp $
40 $verbose = 1; # verbose = [0-2]
41 $defaultpasswd = "yes"; # use password for new users
42 $dotdir = "/usr/share/skel"; # copy dotfiles from this dir
43 $dotdir_bak = $dotdir;
44 $send_message = "/etc/adduser.message"; # send message to new user
45 $send_message_bak = $send_message;
46 $config = "/etc/adduser.conf"; # config file for adduser
47 $config_read = 1; # read config file
48 $logfile = "/var/log/adduser"; # logfile
49 $home = "/home"; # default HOME
50 $etc_shells = "/etc/shells";
51 $etc_passwd = "/etc/master.passwd";
52 $group = "/etc/group";
53 $pwd_mkdb = "pwd_mkdb -p"; # program for building passwd database
56 # List of directories where shells located
57 @path = ('/bin', '/usr/bin', '/usr/local/bin');
58 # common shells, first element has higher priority
59 @shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh');
61 $defaultshell = 'bash'; # defaultshell if not empty
63 $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
65 $uid_start = 1000; # new users get this uid
66 $uid_end = 32000; # max. uid
70 $username = ''; # $username{username} = uid
71 $uid = ''; # $uid{uid} = username
72 $pwgid = ''; # $pwgid{pwgid} = username; gid from passwd db
74 $password = ''; # password for new users
77 $groupname =''; # $groupname{groupname} = gid
78 $groupmembers = ''; # $groupmembers{gid} = members of group/kommalist
79 $gid = ''; # $gid{gid} = groupname; gid form group db
82 $shell = ''; # $shell{`basename sh`} = sh
84 # only for me (=Wolfram)
86 $home = "/home/w/tmp/adduser/home";
87 $etc_shells = "./shells";
88 $etc_passwd = "./master.passwd";
90 $pwd_mkdb = "pwd_mkdb -p -d .";
91 $config = "adduser.conf";
92 $send_message = "./adduser.message";
93 $logfile = "./log.adduser";
96 umask 022; # don't give login group write access
98 $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
101 @message_buffer = '';
102 @user_variable_list = ''; # user variables in /etc/adduser.conf
103 $do_not_delete = '## DO NOT DELETE THIS LINE!';
106 # read shell database, see also: shells(5)
111 print "Check $etc_shells\n" if $verbose;
112 open(S
, $etc_shells) || die "$etc_shells:$!\n";
116 s/^\s*//; s/\s+.*//; # chop
119 $shell{&basename
($sh)} = $sh;
121 warn "Shell: $sh not executable!\n";
129 # add new shells if possible
131 local($sh,$dir,@list);
133 return 1 unless $verbose;
135 foreach $sh (@shellpref) {
138 # shell $sh is not defined as login shell
139 foreach $dir (@path) {
142 if (&confirm_yn
("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
143 push(@list, "$dir/$sh");
144 push(@shellpref, "$sh");
145 $shell{&basename
("$dir/$sh")} = "$dir/$sh";
152 &append_file
($etc_shells, @list) if $#list >= 0;
155 # choise your favourite shell an return the shell
157 local($e,$i,$new_shell);
160 $sh = &shell_default_valid
($defaultshell);
161 return $sh unless $verbose;
163 $new_shell = &confirm_list
("Enter your default shell:", 0,
164 $sh, sort(keys %shell));
165 print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
166 $changes++ if $new_shell ne $sh;
170 sub shell_default_valid
{
174 return $sh if $shell{$sh};
176 foreach $e (@shellpref) {
178 last if defined($shell{$s});
181 warn "Shell ``$sh'' is undefined, use ``$s''\n";
185 # return default home partition (f.e. "/home")
186 # create base directory if nesseccary
189 $home = &stripdir
($home);
192 return $h if !$verbose && $h eq &home_partition_valid
($h);
195 $h = &confirm_list
("Enter your default HOME partition:", 1, $home, "");
197 last if $h eq &home_partition_valid
($h);
200 $changes++ if $h ne $home;
204 sub home_partition_valid
{
209 return $h if $h =~ "^/" && -e
$h && -w
$h && (-d
$h || -l
$h);
213 warn "Please use absolute path for home: ``$h''.\a\n";
218 warn "$h exist, but is it not a directory or symlink!\n"
219 unless -d
$h || -l
$h;
220 warn "$h is not writable!\n"
224 # create home partition
225 return $h if &mkdir_home
($h);
230 # check for valid passwddb
232 system("$pwd_mkdb $etc_passwd");
233 die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
238 local($p_username, $pw, $p_uid, $p_gid, $sh, %shlist);
240 print "Check $etc_passwd\n" if $verbose;
241 open(P
, "$etc_passwd") || die "$passwd: $!\n";
245 push(@passwd_backup, $_);
246 ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
248 print "$p_username already exist with uid: $username{$p_username}!\n"
249 if $username{$p_username} && $verbose;
250 $username{$p_username} = $p_uid;
251 print "User $p_username: uid $p_uid exist twice: $uid{$p_uid}\n"
252 if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0
253 print "User $p_username: illegal shell: ``$sh''\n"
254 if ($verbose && $sh &&
255 !$shell{&basename
($sh)} &&
256 $p_username !~ /^(bin|uucp|falcon|nobody)$/ &&
257 $sh !~ /\/(pppd
|sliplogin
)$/);
258 $uid{$p_uid} = $p_username;
259 $pwgid{$p_gid} = $p_username;
266 local($g_groupname,$pw,$g_gid, $memb);
268 print "Check $group\n" if $verbose;
269 open(G
, "$group") || die "$group: $!\n";
272 push(@group_backup, $_);
273 ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
275 $groupmembers{$g_gid} = $memb;
276 warn "Groupname exist twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n"
277 if $groupname{$g_groupname} && $verbose;
278 $groupname{$g_groupname} = $g_gid;
279 warn "Groupid exist twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
280 if $gid{$g_gid} && $verbose;
281 $gid{$g_gid} = $g_groupname;
286 # check gids /etc/passwd <-> /etc/group
288 local($c_gid, $c_username, @list);
290 foreach $c_gid (keys %pwgid) {
292 $c_username = $pwgid{$c_gid};
293 warn "User ``$c_username'' has gid $c_gid but a group with this " .
294 "gid does not exist.\n" if $verbose;
300 # main loop for creating new users
308 $name = &confirm_list
("Enter username", 1, "a-z0-9", "");
309 last if (&new_users_name_valid
($name) eq $name);
314 sub new_users_name_valid
{
317 if ($name !~ /^[a-z0-9]+$/) {
318 warn "Wrong username. " .
319 "Please use only lowercase characters or digits\a\n";
321 } elsif ($username{$name}) {
322 warn "Username ``$name'' already exists!\a\n"; return 0;
328 sub new_users_fullname
{
333 $fullname = &confirm_list
("Enter full name", 1, "", "");
334 last if $fullname eq &new_users_fullname_valid
($fullname);
336 $fullname = $name unless $fullname;
340 sub new_users_fullname_valid
{
341 local($fullname) = @_;
343 return $fullname if $fullname !~ /:/;
345 warn "``:'' is not allowed!\a\n";
349 # return shell (full path) for user
350 sub new_users_shell
{
353 $sh = &confirm_list
("Enter shell", 0, $defaultshell, keys %shell);
357 # return free uid and gid
360 local($u_id, $g_id) = &next_id
($name);
361 local($u_id_tmp, $e);
364 $u_id_tmp = &confirm_list
("Uid", 1, $u_id, "");
365 last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
367 if ($uid{$u_id_tmp}) {
368 warn "Uid ``$u_id_tmp'' in use!\a\n";
370 warn "Wrong uid.\a\n";
374 return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
376 $uid_start = $u_id_tmp;
377 return &next_id
($name);
382 local($gid, $name) = @_;
385 $groupmembers{$gid} =~ /^(.+,)?$name(,.+)?$/;
387 $groupmembers_bak{$gid} = $groupmembers{$gid};
388 $groupmembers{$gid} .= "," if $groupmembers{$gid};
389 $groupmembers{$gid} .= "$name";
396 sub new_users_grplogin
{
397 local($name, $defaultgroup, $new_users_ok) = @_;
398 local($group_login, $group);
401 $group = $defaultgroup if $defaultgroup ne $group_uniq;
405 foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
407 # restore old groupmembers, user was not accept
408 foreach $e (keys %groupmembers_bak) {
409 $groupmembers{$e} = $groupmembers_bak{$e};
414 $group_login = &confirm_list
("Login group", 1, $group,
416 last if $group_login eq $group;
417 last if $group_login eq $name;
418 last if defined $groupname{$group_login};
419 if ($group_login eq $group_uniq) {
420 $group_login = $name; last;
423 if (defined $gid{$group_login}) {
424 # convert numeric groupname (gid) to groupname
425 $group_login = $gid{$group_login};
428 warn "Group does not exist!\a\n";
431 if (defined($groupname{$group_login})) {
432 &add_group
($groupname{$group_login}, $name);
435 return ($group_login, $group_uniq) if $group_login eq $name;
436 return ($group_login, $group_login);
440 sub new_users_grplogin_batch
{
441 local($name, $defaultgroup) = @_;
442 local($group_login, $group);
444 $group_login = $name;
445 $group_login = $defaultgroup if $defaultgroup ne $group_uniq;
447 if (defined $gid{$group_login}) {
448 # convert numeric groupname (gid) to groupname
449 $group_login = $gid{$group_login};
452 if (defined($groupname{$group_login})) {
453 &add_group
($groupname{$group_login}, $name);
457 if defined($groupname{$group_login}) || $group_login eq $name;
458 warn "Group ``$group_login'' does not exist\a\n";
462 # return other groups (string)
463 sub new_users_groups
{
464 local($name, $other_groups) = @_;
466 "Login group is ``$group_login''. Invite $name into other groups:";
468 local($new_groups,$groups);
470 $other_groups = "no" unless $other_groups;
473 $groups = &confirm_list
($string, 1, $other_groups,
474 ("no", $other_groups, "guest"));
476 return "" if $groups eq "no";
478 ($flag, $new_groups) = &new_users_groups_valid
($groups);
481 $new_groups =~ s/\s*$//;
485 sub new_users_groups_valid
{
487 local($e, $new_groups);
490 foreach $e (split(/[,\s]+/, $groups)) {
491 # convert numbers to groupname
492 if ($e =~ /^[0-9]+$/ && $gid{$e}) {
495 if (defined($groupname{$e})) {
496 if (&add_group
($groupname{$e}, $name)) {
497 $new_groups .= "$e ";
499 warn "$name is already member of group ``$e''\n";
502 warn "Group ``$e'' does not exist\a\n"; $flag++;
505 return ($flag, $new_groups);
517 Gid: $g_id ($group_login)
518 Groups: $group_login $new_groups
523 return &confirm_yn
("Ok?", "yes");
526 # make password database
527 sub new_users_pwdmkdb
{
530 system("$pwd_mkdb $etc_passwd");
532 local($crash) = "$etc_passwd.crash$$";
534 warn "``$pwd_mkdb'' failed, try to restore ...\n";
536 open(R
, "> $crash") || die "Sorry, give up\n";
537 $j = join("\n", @passwd_backup);
542 system("$pwd_mkdb $crash");
543 die "Sorry, give up\n" if $?;
544 die "Successfully restore $etc_passwd. Exit.\n";
548 # update group database
549 sub new_users_group_update
{
553 if (!defined($groupname{$group_login}) &&
554 !defined($gid{$groupname{$group_login}})) {
555 push(@group_backup, "$group_login:*:$g_id:$group_login");
556 $groupname{$group_login} = $g_id;
557 $gid{$g_id} = $group_login;
558 $groupmembers{$g_id} = $group_login;
561 if ($new_groups || defined($groupname{$group_login}) ||
562 defined($gid{$groupname{$group_login}})) {
563 # new user is member of some groups
564 # new login group is already in name space
565 rename($group, "$group.bak");
566 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
567 foreach $e (sort {$a <=> $b} (keys %gid)) {
568 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
570 &append_file
($group, @a);
572 &append_file
($group, "$group_login:*:$g_id:$group_login");
577 sub new_users_passwd_update
{
578 # update passwd/group variables
579 push(@passwd_backup, $new_entry);
580 $username{$name} = $u_id;
582 $pwgid{$g_id} = $name;
585 # send message to new user
586 sub new_users_sendmessage
{
587 return 1 if $send_message eq "no";
590 &confirm_list
("Send message to ``$name'' and:",
591 1, "no", ("root", "second_mail_address", "no"));
593 $cc = "" if $cc eq "no";
595 foreach $e (@message_buffer) {
600 local(@message_buffer_append) = ();
601 if (!&confirm_yn
("Add somethings to message", "no")) {
602 print "Finish with beginning ``.'' or ^D\n";
603 push(@message_buffer_append, "\n");
604 while($read = <STDIN
>) {
605 last if $read eq "\.\n";
606 push(@message_buffer_append, $read);
610 &sendmessage
("$name $cc", (@message_buffer, @message_buffer_append))
611 if (&confirm_yn
("Send message", "yes"));
615 local($to, @message) = @_;
618 if (!open(M
, "| mail -s Welcome $to")) {
619 warn "Cannot send mail to: $to!\n";
622 foreach $e (@message) {
623 print M
eval "\"$e\"";
630 sub new_users_password
{
633 return "" if $defaultpasswd ne "yes";
638 $password = &confirm_list
("Enter password", 1, "", "");
639 last if $password ne "";
640 last if &confirm_yn
("Use empty password?", "yes");
649 print "\n" if $verbose;
650 print "Ok, let's go.\n" .
651 "Don't worry about mistakes. I ask you later for " .
652 "correct input.\n" if $verbose;
655 # fullname: Full name
659 # group_login: groupname of g_id
660 # new_groups: some other groups
661 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $new_groups);
662 local($groupmembers_bak, $cryptpwd);
663 local($new_users_ok) = 1;
667 $new_groups = "no" unless $groupname{$new_groups};
670 $name = &new_users_name
;
671 $fullname = &new_users_fullname
($name);
672 $sh = &new_users_shell
;
673 ($u_id, $g_id) = &new_users_id
($name);
674 ($group_login, $defaultgroup) =
675 &new_users_grplogin
($name, $defaultgroup, $new_users_ok);
676 # do not use uniq username and login group
677 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
679 $new_groups = &new_users_groups
($name, $new_groups);
680 $password = &new_users_password
;
687 $cryptpwd = crypt($password, &salt
) if $password ne "";
689 $new_entry = "$name\:" . "$cryptpwd" .
690 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
691 &append_file
($etc_passwd, "$new_entry");
692 &new_users_pwdmkdb
("$new_entry");
693 &new_users_group_update
;
694 &new_users_passwd_update
; print "Added user ``$name''\n";
695 &new_users_sendmessage
;
696 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
697 &home_create
($name, $group_login);
701 if (!&confirm_yn
("Continue with next user?", "yes")) {
702 print "Good bye.\n" if $verbose;
705 print "\n" if !$verbose;
710 local($name, $groups, $fullname, $password) = @_;
713 $defaultshell = &shell_default_valid
($defaultshell);
714 return 0 unless $home = &home_partition_valid
($home);
715 return 0 if $dotdir ne &dotdir_default_valid
($dotdir);
716 $send_message = &message_default
;
718 return 0 if $name ne &new_users_name_valid
($name);
719 $sh = $shell{$defaultshell};
720 ($u_id, $g_id) = &next_id
($name);
721 $group_login = &new_users_grplogin_batch
($name, $defaultgroup);
722 return 0 unless $group_login;
723 $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
724 ($flag, $new_groups) = &new_users_groups_valid
($groups);
728 $cryptpwd = crypt($password, &salt
) if $password ne "";
730 $new_entry = "$name\:" . "$cryptpwd" .
731 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
732 &append_file
($etc_passwd, "$new_entry");
733 &new_users_pwdmkdb
("$new_entry");
734 &new_users_group_update
;
735 &new_users_passwd_update
; print "Added user ``$name''\n";
736 &sendmessage
($name, @message_buffer) if $send_message ne "no";
737 &adduser_log
("$name:*:$u_id:$g_id($group_login):$fullname");
738 &home_create
($name, $group_login);
741 # ask for password usage
742 sub password_default
{
743 local($p) = $defaultpasswd;
745 $p = &confirm_yn
("Use passwords", $defaultpasswd);
746 $changes++ unless $p;
748 return "yes" if (($defaultpasswd eq "yes" && $p) ||
749 ($defaultpasswd eq "no" && !$p));
750 return "no"; # otherwise
755 die "You are not root!\n" if $< && !$test;
761 [-batch username [group[,group]...] [fullname] [password]]
768 [-message message_file]
771 [-s|-silent|-q|-quit]
775 home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
776 message_file=$send_message uid_start=$uid_start
784 local($e, $last, @array);
786 foreach $e (sort @list) {
787 push(@array, $e) unless $e eq $last;
793 # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
795 local($salt) = '_'; # initialization
797 local(@itoa64) = ( 0 .. 9, a
.. z
, A
.. Z
); # 0 .. 63
799 warn "calculate salt\n" if $verbose > 1;
801 for ($i = 0; $i < 8; $i++) {
802 srand(time + $rand + $$);
803 $rand = rand(25*29*17 + $rand);
804 $salt .= $itoa64[$rand & $#itoa64];
806 warn "Salt is: $salt\n" if $verbose > 1;
815 (c) Copyright 1995 Wolfram Schneider <wosch@cs.tu-berlin.de>
822 print "Use option ``-silent'' if you don't want see " .
823 "some warnings & questions.\n\n";
825 print "Use option ``-verbose'' if you want see more warnings & " .
826 "questions \nor try to repair bugs.\n\n";
831 sub parse_arguments
{
834 while ($_ = $argv[0], /^-/) {
837 if (/^--?(v|verbose)$/) { $verbose = 1 }
838 elsif (/^--?(s|silent|q|quit)$/) { $verbose = 0 }
839 elsif (/^--?(debug)$/) { $verbose = 2 }
840 elsif (/^--?(h|help|\?)$/) { &usage
}
841 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
842 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
843 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv }
844 elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv }
845 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv }
846 elsif (/^--?(check_only)$/) { $check_only = 1 }
847 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
849 elsif (/^--?(batch)$/) {
850 @batch = splice(@argv, 0, 4); $verbose = 0;
851 die "batch: to few arguments\n" if $#batch < 0;
854 elsif (/^--?(config_create)$/) { &create_conf
; }
855 elsif (/^--?(noconfig)$/) { $config_read = 0; }
858 #&usage if $#argv < 0;
870 $name = &stripdir
($name);
871 $name =~ s
|/+[^/]+$||;
872 $name = "/" unless $name; # dirname of / is /
876 # return 1 if $file is a readable file or link
878 local($file, $verb) = @_;
881 if (-f
$file || -l
$file) {
883 warn "$file unreadable\n" if $verbose;
885 warn "$file is not a plain file or link\n" if $verbose;
891 # create configuration files and exit
894 &message_create
($send_message);
899 # log for new user in /var/log/adduser
904 return 1 if $logfile eq "no";
906 local($sec, $min, $hour, $mday, $mon, $year) = localtime;
909 foreach $e ('sec', 'min', 'hour', 'mday', 'mon', 'year') {
911 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
914 &append_file
($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
917 # create HOME directory, copy dotfiles from $dotdir to $HOME
919 local($name, $group) = @_;
920 local($homedir) = "$home/$name";
923 warn "HOME Directory ``$homedir'' already exist\a\n";
927 # copy files from $dotdir to $homedir
928 # rename 'dot.foo' files to '.foo'
929 print "Copy files from $dotdir to $homedir\n" if $verbose;
930 system("cp -r $dotdir $homedir");
931 system("chmod -R u+wrX,go-w $homedir");
932 system("chown -R $name:$group $homedir");
935 opendir(D
, $homedir);
936 foreach $file (readdir(D
)) {
937 if ($file =~ /^dot\./ && -f
"$homedir/$file") {
938 $file =~ s/^dot\././;
939 rename("$homedir/dot$file", "$homedir/$file");
941 chmod(0600, "$homedir/$file")
942 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
943 chmod(0700, "$homedir/$file")
944 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
950 # makes a directory hierarchy
953 $dir = &stripdir
($dir);
954 local($user_partition) = "/usr";
955 local($dirname) = &dirname
($dir);
958 -e
$dirname || &mkdirhier
($dirname);
960 if (((stat($dirname))[0]) == ((stat("/"))[0])){
961 # home partition is on root partition
962 # create home partition on $user_partition and make
963 # a symlink from $dir to $user_partition/`basename $dir`
964 # For instance: /home -> /usr/home
966 local($basename) = &basename
($dir);
967 local($d) = "$user_partition/$basename";
971 warn "Oops, $d already exist\n" if $verbose;
973 print "Create $d\n" if $verbose;
974 if (!mkdir("$d", 0755)) {
975 warn "$d: $!\a\n"; return 0;
979 unlink($dir); # symlink to nonexist file
980 print "Create symlink: $dir -> $d\n" if $verbose;
981 if (!symlink("$d", $dir)) {
982 warn "Symlink $d: $!\a\n"; return 0;
985 print "Create $dir\n" if $verbose;
986 if (!mkdir("$dir", 0755)) {
987 warn "Directory ``$dir'': $!\a\n"; return 0;
997 $dir = &stripdir
($dir);
999 foreach $d (split('/', $dir)) {
1003 print "Create $dir\n" if $verbose;
1004 if (!mkdir("$dir", 0755)) {
1005 warn "$dir: $!\n"; return 0;
1014 # F.i.: //usr///home// -> /usr/home
1018 $dir =~ s
|/+|/|g
; # delete double '/'
1019 $dir =~ s
|/$||; # delete '/' at end
1020 return $dir if $dir ne "";
1024 # Read one of the elements from @list. $confirm is default.
1025 # If !$allow accept only elements from @list.
1027 local($message, $allow, $confirm, @list) = @_;
1028 local($read, $c, $print);
1030 $print = "$message" if $message;
1031 $print .= " " unless $message =~ /\n$/ || $#list == 0;
1033 $print .= join($", &uniq(@list)); #"
1034 $print .= " " unless $message =~ /\n$/ && $#list == 0;
1036 print "\n" if (length($print) + length($confirm)) > 60;
1037 print "[$confirm]: ";
1039 chop($read = <STDIN>);
1042 return $confirm if $read eq "";
1043 return "$read" if $allow;
1045 foreach $c (@list) {
1046 return $read if $c eq $read;
1048 warn "$read: is not allowed!\a\n";
1049 return &confirm_list($message, $allow, $confirm, @list);
1052 # YES or NO question
1053 # return 1 if &confirm("message", "yes") and answer is yes
1054 # or if &confirm("message", "no") an answer is no
1057 local($message, $confirm) = @_;
1058 local($yes) = '^(yes
|YES
|y
|Y
)$';
1059 local($no) = '^(no|NO
|n
|N
)$';
1062 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1067 print "$message (y/n) [$confirm]: ";
1068 chop($read = <STDIN>);
1071 return 1 unless $read;
1073 if (($confirm eq "y" && $read =~ "$yes") ||
1074 ($confirm eq "n" && $read =~ "$no")) {
1078 if ($read !~ "$yes" && $read !~ "$no") {
1079 warn "Wrong value. Enter again!\a\n";
1080 return &confirm_yn($message, $confirm);
1085 # test if $dotdir exist
1086 # return "no" if $dotdir not exist or dotfiles should not copied
1087 sub dotdir_default {
1088 local($dir) = $dotdir;
1090 return &dotdir_default_valid($dir) unless $verbose;
1092 $dir = &confirm_list("Copy dotfiles from:", 1,
1093 $dir, ("no", $dotdir_bak, $dir));
1094 last if $dir eq &dotdir_default_valid($dir);
1096 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1098 $changes++ if $dir ne $dotdir;
1102 sub dotdir_default_valid {
1105 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1106 return $dir if $dir eq "no";
1107 warn "Dotdir ``$dir'' is not a directory\a\n";
1111 # ask for messages to new users
1112 sub message_default {
1113 local($file) = $send_message;
1114 local(@d) = ($file, $send_message_bak, "no");
1117 $file = &confirm_list("Send message from file:", 1, $file, @d);
1118 last if $file eq "no";
1119 last if &filetest($file, 1);
1121 # maybe create message file
1122 &message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1123 last if &filetest($file, 0);
1124 last if !&confirm_yn("File ``$file'' does not exist, try again?",
1128 if ($file eq "no" || !&filetest($file, 0)) {
1129 warn "Do not send message\n" if $verbose;
1132 &message_read($file);
1135 $changes++ if $file ne $send_message && $verbose;
1139 # create message file
1140 sub message_create {
1143 rename($file, "$file.bak");
1144 if (!open(M, "> $file")) {
1145 warn "Messagefile ``$file'': $!\n"; return 0;
1149 # Message file for adduser(8)
1151 # defaultvariables: \$name, \$fullname, \$password
1152 # other variables: see /etc/adduser.conf after
1153 # line ``$do_not_delete''
1158 your account ``\$name'' was created. Your password is ``\$password''.
1159 Please expire your password. Have fun!
1161 See also chpass(1), finger(1), passwd(1)
1167 # read message file into buffer
1170 @message_buffer = '';
1172 if (!open(R
, "$file")) {
1173 warn "File ``$file'':$!\n"; return 0;
1176 push(@message_buffer, $_) unless /^\s*#/;
1181 # write @list to $file with file-locking
1183 local($file,@list) = @_;
1185 local($LOCK_EX) = 2;
1186 local($LOCK_NB) = 4;
1187 local($LOCK_UN) = 8;
1189 open(F
, ">> $file") || die "$file: $!\n";
1190 print "Lock $file.\n" if $verbose > 1;
1191 while(!flock(F
, $LOCK_EX | $LOCK_NB)) {
1192 warn "Cannot lock file: $file\a\n";
1193 die "Sorry, give up\n"
1194 unless &confirm_yn
("Try again?", "yes");
1196 print F
join("\n", @list) . "\n";
1198 print "Unlock $file.\n" if $verbose > 1;
1202 # return free uid+gid
1203 # uid == gid if possible
1207 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1208 # looking for next free uid
1209 while($uid{$uid_start}) {
1211 $uid_start = 1000 if $uid_start >= $uid_end;
1212 print "$uid_start\n" if $verbose > 1;
1215 local($gid_start) = $uid_start;
1216 # group for user (username==groupname) already exist
1217 if ($groupname{$group}) {
1218 $gid_start = $groupname{$group};
1220 # gid is in use, looking for another gid.
1221 # Note: uid an gid are not equal
1222 elsif ($gid{$uid_start}) {
1223 while($gid{$gid_start} || $uid{$gid_start}) {
1225 $gid_start = $uid_end if $gid_start < 100;
1228 return ($uid_start, $gid_start);
1234 local($user_flag) = 0;
1236 # don't read config file
1237 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1239 if(!open(C
, "$config")) {
1240 warn "$config: $!\n"; return 0;
1244 # user defined variables
1245 /^$do_not_delete/ && $user_flag++;
1246 # found @array or $variable
1247 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1251 # lines with '^##' are not saved
1252 push(@user_variable_list, $_)
1253 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1255 #warn "X @user_variable_list X\n";
1262 local($silent) = @_;
1265 return 1 unless ($changes || ! -e
$config || !$config_read || $silent);
1269 return 1 if &confirm_yn
("\nWrite your changes to $config?", "no");
1272 &confirm_yn
("\nWrite your configuration to $config?", "yes");
1276 rename($config, "$config.bak");
1277 open(C
, "> $config") || die "$config: $!\n";
1279 # prepare some variables
1280 $send_message = "no" unless $send_message;
1281 $defaultpasswd = "no" unless $defaultpasswd;
1282 local($shpref) = "'" . join("', '", @shellpref) . "'";
1283 local($shpath) = "'" . join("', '", @path) . "'";
1284 local($user_var) = join('', @user_variable_list);
1288 # $config - automatic generated by adduser(8)
1290 # Note: adduser read *and* write this file.
1291 # You may change values, but don't add new things befor the
1292 # line ``$do_not_delete''
1298 # use password for new users
1299 # defaultpasswd = yes | no
1300 defaultpasswd = $defaultpasswd
1302 # copy dotfiles from this dir ("/usr/share/skel" or "no")
1305 # send this file to new user ("/etc/adduser.message" or "no")
1306 send_message = "$send_message"
1308 # config file for adduser ("/etc/adduser.conf")
1311 # logfile ("/var/log/adduser" or "no")
1312 logfile = "$logfile"
1314 # default HOME directory ("/home")
1317 # List of directories where shells located
1318 # path = ('/bin', '/usr/bin', '/usr/local/bin')
1321 # common shell list, first element has higher priority
1322 # shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1323 shellpref = ($shpref)
1325 # defaultshell if not empty ("bash")
1326 defaultshell = "$defaultshell"
1328 # defaultgroup ('USER' for same as username or any other valid group
1329 defaultgroup = $defaultgroup
1331 # new users get this uid (1000)
1335 ## your own variables, see /etc/adduser.message
1346 $test = 0; # test mode, only for development
1349 &check_root
; # you must be root to run this script!
1350 &variables
; # initialize variables
1351 &config_read
(@ARGV); # read variables form config-file
1352 &parse_arguments
(@ARGV); # parse arguments
1354 if (!$check_only && $#batch < 0) {
1360 &passwd_check
; # check for valid passwdb
1361 &shells_read
; # read /etc/shells
1362 &passwd_read
; # read /etc/master.passwd
1363 &group_read
; # read /etc/group
1364 &group_check
; # check for incon*
1365 exit 0 if $check_only; # only check consistence and exit
1367 exit(!&batch
(@batch)) if $#batch >= 0; # batch mode
1371 &shells_add
; # maybe add some new shells
1372 $defaultshell = &shell_default
; # enter default shell
1373 $home = &home_partition
($home); # find HOME partition
1374 $dotdir = &dotdir_default
; # check $dotdir
1375 $send_message = &message_default
; # send message to new user
1376 $defaultpasswd = &password_default
; # maybe use password
1377 &config_write
(0); # write variables in file
1379 # main loop for creating new users
1380 &new_users
; # add new users