xref: /openbsd-src/usr.sbin/adduser/adduser.perl (revision 2384d3f11e29cd4d5d0350aae4cb5c23d7431b30)
1#!/usr/bin/perl
2#
3#	$OpenBSD: adduser.perl,v 1.39 2002/07/10 19:57:31 millert Exp $
4#
5# Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# $From: adduser.perl,v 1.22 1996/12/07 21:25:12 ache Exp $
30
31use IPC::Open2;
32use Fcntl qw(:DEFAULT :flock);
33
34################
35# main
36#
37$test = 0;			# test mode, only for development
38$check_only = 0;
39
40$SIG{'INT'} = 'cleanup';
41$SIG{'QUIT'} = 'cleanup';
42$SIG{'HUP'} = 'cleanup';
43$SIG{'TERM'} = 'cleanup';
44
45&check_root;			# you must be root to run this script!
46&variables;			# initialize variables
47&config_read(@ARGV);		# read variables from config-file
48&parse_arguments(@ARGV);	# parse arguments
49
50if (!$check_only &&  $#batch < 0) {
51    &hints;
52}
53
54# check
55$changes = 0;
56&variable_check;		# check for valid variables
57&passwd_check;			# check for valid passwdb
58&shells_read;			# read /etc/shells
59&passwd_read;			# read /etc/master.passwd
60&group_read;			# read /etc/group
61&group_check;			# check for incon*
62exit 0 if $check_only;		# only check consistence and exit
63
64exit(!&batch(@batch)) if $#batch >= 0; # batch mode
65
66# Interactive:
67# main loop for creating new users
68&new_users;	     # add new users
69
70#end
71
72
73# Set adduser "default" variables internally before groking config file
74# Adduser.conf supercedes these
75sub variables {
76    $verbose = 1;		# verbose = [0-2]
77    $defaultpasswd = "yes";	# use password for new users
78    $dotdir = "/etc/skel";	# copy dotfiles from this dir
79    $dotdir_bak = $dotdir;
80    $send_message = "no"; # send message to new user
81    $send_message_bak = '/etc/adduser.message';
82    $config = "/etc/adduser.conf"; # config file for adduser
83    $config_read = 1;		# read config file
84    $logfile = "/var/log/adduser"; # logfile
85    $home = "/home";		# default HOME
86    $etc_shells = "/etc/shells";
87    $etc_passwd = "/etc/master.passwd";
88    $etc_ptmp = "/etc/ptmp";
89    $group = "/etc/group";
90    $pwd_mkdb = "pwd_mkdb -p";	# program for building passwd database
91    $encryptionmethod = "blowfish";
92    $rcsid = '$OpenBSD: adduser.perl,v 1.39 2002/07/10 19:57:31 millert Exp $';
93
94    # List of directories where shells located
95    @path = ('/bin', '/usr/bin', '/usr/local/bin');
96    # common shells, first element has higher priority
97    @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
98
99    @encryption_methods = ('blowfish', 'md5', 'des', 'old');
100
101    $defaultshell = 'sh';	# defaultshell if not empty
102    $group_uniq = 'USER';
103    $defaultgroup = $group_uniq;# login groupname, $group_uniq means username
104
105    $uid_start = 1000;		# new users get this uid
106    $uid_end   = 2147483647;	# max. uid
107
108    # global variables
109    # passwd
110    %username = ();		# $username{username} = uid
111    %uid = ();			# $uid{uid} = username
112    %pwgid = ();		# $pwgid{pwgid} = username; gid from passwd db
113
114    $password = '';		# password for new users
115
116    # group
117    %groupname = ();		# $groupname{groupname} = gid
118    %groupmembers = ();		# $groupmembers{gid} = members of group/kommalist
119    %gid = ();			# $gid{gid} = groupname;    gid form group db
120
121    # shell
122    %shell = ();		# $shell{`basename sh`} = sh
123
124    # only for me (=Wolfram)
125    if ($test) {
126	$home = "/home/w/tmp/adduser/home";
127	$etc_shells = "./shells";
128	$etc_passwd = "./master.passwd";
129	$group = "./group";
130	$pwd_mkdb = "pwd_mkdb -p -d .";
131	$config = "adduser.conf";
132	$send_message = "./adduser.message";
133	$logfile = "./log.adduser";
134    }
135
136    umask 022;			# don't give login group write access
137
138    $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
139    @passwd_backup = ();
140    @group_backup = ();
141    @message_buffer = ();
142    @user_variable_list = ();	# user variables in /etc/adduser.conf
143    $do_not_delete = '## DO NOT DELETE THIS LINE!';
144}
145
146# read shell database, see also: shells(5)
147sub shells_read {
148    local($sh);
149    local($err) = 0;
150
151    print "Reading $etc_shells\n" if $verbose;
152    open(S, $etc_shells) || die "$etc_shells: $!\n";
153
154    while(<S>) {
155	if (/^\s*\//) {
156	    s/^\s*//; s/\s+.*//; # chop
157	    $sh = $_;
158	    if (-x  $sh) {
159		$shell{&basename($sh)} = $sh;
160	    } else {
161		warn "Shell: $sh not executable!\n";
162		$err++;
163	    }
164	}
165    }
166
167    push(@list, "/sbin/nologin");
168    &shell_pref_add("nologin");
169    $shell{"nologin"} = "/sbin/nologin";
170
171    return $err;
172}
173
174# add new shells if possible
175sub shells_add {
176    local($sh,$dir,@list);
177
178    return 1 unless $verbose;
179
180    foreach $sh (@shellpref) {
181	# all known shells
182	if (!$shell{$sh}) {
183	    # shell $sh is not defined as login shell
184	    foreach $dir (@path) {
185		if (-x "$dir/$sh") {
186		    # found shell
187		    if (&confirm_yn("Found shell: $dir/$sh. Add to $etc_shells?", "yes")) {
188			push(@list, "$dir/$sh");
189			&shell_pref_add("$sh");
190			$shell{&basename("$dir/$sh")} = "$dir/$sh";
191			$changes++;
192		    }
193		}
194	    }
195	}
196    }
197    &append_file($etc_shells, @list) if $#list >= 0;
198}
199
200# add shell to preference list without duplication
201sub shell_pref_add {
202    local($new_shell) = @_;
203    local($shell);
204
205    foreach $shell (@shellpref) {
206	return if ($shell eq $new_shell);
207    }
208    push(@shellpref, $new_shell);
209}
210
211# choose your favourite shell and return the shell
212sub shell_default {
213    local($e,$i,$new_shell);
214    local($sh);
215
216    $sh = &shell_default_valid($defaultshell);
217    return $sh unless $verbose;
218
219    $new_shell = &confirm_list("Enter your default shell:", 0,
220		       $sh, sort(keys %shell));
221    print "Your default shell is: $new_shell -> $shell{$new_shell}\n";
222    $changes++ if $new_shell ne $sh;
223    return $new_shell;
224}
225
226sub shell_default_valid {
227    local($sh) = @_;
228    local($s,$e);
229
230    return $sh if $shell{$sh};
231
232    foreach $e (@shellpref) {
233	$s = $e;
234	last if defined($shell{$s});
235    }
236    $s = "sh" unless $s;
237    warn "Shell ``$sh'' is undefined, use ``$s''\n";
238    return $s;
239}
240
241# return default home partition (f.e. "/home")
242# create base directory if necessary
243sub home_partition {
244    local($home) = @_;
245    $home = &stripdir($home);
246    local($h) = $home;
247
248    return $h if !$verbose && $h eq &home_partition_valid($h);
249
250    while(1) {
251	$h = &confirm_list("Enter your default HOME partition:", 1, $home, "");
252	$h = &stripdir($h);
253	last if $h eq &home_partition_valid($h);
254    }
255
256    $changes++ if $h ne $home;
257    return $h;
258}
259
260sub home_partition_valid {
261    local($h) = @_;
262
263    $h = &stripdir($h);
264    # all right (I hope)
265    return $h if $h =~ "^/" && -e $h && -w _ && (-d _ || -l $h);
266
267    # Errors or todo
268    if ($h !~ "^/") {
269	warn "Please use absolute path for home: ``$h''.\a\n";
270	return 0;
271    }
272
273    if (-e $h) {
274	warn "$h exists, but is not a directory or symlink!\n"
275	    unless -d $h || -l $h;
276	warn "$h is not writable!\n"
277	    unless -w $h;
278	return 0;
279    } else {
280	# create home partition
281	return $h if &mkdir_home($h);
282    }
283    return 0;
284}
285
286# check for valid passwddb
287sub passwd_check {
288    system(split(/\s+/, "$pwd_mkdb -c $etc_passwd"));
289    die "\nInvalid $etc_passwd - cannot add any users!\n" if $?;
290}
291
292# read /etc/passwd
293sub passwd_read {
294    local($p_username, $pw, $p_uid, $p_gid, $sh);
295
296    print "Check $etc_passwd\n" if $verbose;
297    open(P, "$etc_passwd") || die "$etc_passwd: $!\n";
298
299    # we only use this to lock the password file
300    sysopen(PTMP, $etc_ptmp, O_RDWR|O_CREAT|O_EXCL, 0600) ||
301	die "Password file busy\n";
302
303    while(<P>) {
304	chop;
305	push(@passwd_backup, $_);
306	($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9];
307
308	print "$p_username already exists with uid: $username{$p_username}!\n"
309	    if $username{$p_username} && $verbose;
310	$username{$p_username} = $p_uid;
311	print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n"
312	    if $uid{$p_uid} && $verbose && $p_uid;    # don't warn for uid 0
313	print "User $p_username: illegal shell: ``$sh''\n"
314	    if ($verbose && $sh &&
315		!$shell{&basename($sh)} &&
316		$p_username !~ /^(news|xten|bin|nobody|uucp)$/ &&
317		$sh !~ /\/(pppd|sliplogin)$/);
318	$uid{$p_uid} = $p_username;
319	$pwgid{$p_gid} = $p_username;
320    }
321    close P;
322}
323
324# read /etc/group
325sub group_read {
326    local($g_groupname,$pw,$g_gid, $memb);
327
328    print "Check $group\n" if $verbose;
329    open(G, "$group") || die "$group: $!\n";
330    while(<G>) {
331	chop;
332	push(@group_backup, $_);
333	($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3];
334
335	$groupmembers{$g_gid} = $memb;
336	warn "Groupname exists twice: $g_groupname:$g_gid ->  $g_groupname:$groupname{$g_groupname}\n"
337	    if $groupname{$g_groupname} && $verbose;
338	$groupname{$g_groupname} = $g_gid;
339	warn "Groupid exists twice:   $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n"
340	    if $gid{$g_gid} && $verbose;
341	$gid{$g_gid} = $g_groupname;
342    }
343    close G;
344}
345
346# check gids /etc/passwd <-> /etc/group
347sub group_check {
348    local($c_gid, $c_username, @list);
349
350    foreach $c_gid (keys %pwgid) {
351	if (!$gid{$c_gid}) {
352	    $c_username = $pwgid{$c_gid};
353	    warn "User ``$c_username'' has gid $c_gid but a group with this " .
354		"gid does not exist.\n" if $verbose;
355	}
356    }
357}
358
359#
360# main loop for creating new users
361#
362
363# return username
364sub new_users_name {
365    local($name);
366
367    while(1) {
368	$name = &confirm_list("Enter username", 1, "a-z0-9_-", "");
369	if (length($name) > 31) {
370	    warn "Username is longer than 31 characters\a\n";
371	    next;
372	}
373	last if (&new_users_name_valid($name) eq $name);
374    }
375    return $name;
376}
377
378sub new_users_name_valid {
379    local($name) = @_;
380
381    if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/ || $name eq "a-z0-9_-") {
382	warn "Illegal username. " .
383	    "Please use only lowercase characters or digits\a\n";
384	return 0;
385    } elsif ($username{$name}) {
386	warn "Username ``$name'' already exists!\a\n"; return 0;
387    }
388    return $name;
389}
390
391# return full name
392sub new_users_fullname {
393    local($name) = @_;
394    local($fullname);
395
396    while(1) {
397	$fullname = &confirm_list("Enter full name", 1, "", "");
398	last if $fullname eq &new_users_fullname_valid($fullname);
399    }
400    $fullname = $name unless $fullname;
401    return $fullname;
402}
403
404sub new_users_fullname_valid {
405    local($fullname) = @_;
406
407    return $fullname if $fullname !~ /:/;
408
409    warn "``:'' is not allowed!\a\n";
410    return 0;
411}
412
413# return shell (full path) for user
414sub new_users_shell {
415    local($sh);
416
417    $sh = &confirm_list("Enter shell", 0, $defaultshell, keys %shell);
418    return $shell{$sh};
419}
420
421# return free uid and gid
422sub new_users_id {
423    local($name) = @_;
424    local($u_id, $g_id) = &next_id($name);
425    local($u_id_tmp, $e);
426
427    while(1) {
428	$u_id_tmp = &confirm_list("Uid", 1, $u_id, "");
429	last if $u_id_tmp =~ /^[0-9]+$/ && $u_id_tmp <= $uid_end &&
430		! $uid{$u_id_tmp};
431	if ($uid{$u_id_tmp}) {
432	    warn "Uid ``$u_id_tmp'' in use!\a\n";
433	} else {
434	    warn "Wrong uid.\a\n";
435	}
436    }
437    # use calculated uid
438    return ($u_id_tmp, $g_id) if $u_id_tmp eq $u_id;
439    # recalculate gid
440    $uid_start = $u_id_tmp;
441    return &next_id($name);
442}
443
444# add user to group
445sub add_group {
446    local($gid, $name) = @_;
447
448    return 0 if
449	$groupmembers{$gid} =~ /^(.*,)?$name(,.*)?$/;
450
451    $groupmembers_bak{$gid} = $groupmembers{$gid};
452    $groupmembers{$gid} .= "," if $groupmembers{$gid};
453    $groupmembers{$gid} .= "$name";
454
455    local(@l) = split(',', $groupmembers{$gid});
456    # group(5): A group cannot have more than 200 members.
457    # The maximum line length of /etc/group is 1024 characters.
458    # Longer lines will be skiped.
459    if ($#l >= 200 ||
460	length($groupmembers{$gid}) > 1024 - 50) { # 50 is for group name
461	warn "WARNING, group line ``$gid{$gid}'' is either too long or has\n" .
462	    "too many users in the group, see group(5)\a\n";
463    }
464    return $name;
465}
466
467
468# return login group
469sub new_users_grplogin {
470    local($name, $defaultgroup, $new_users_ok) = @_;
471    local($group_login, $group);
472
473    $group = $name;
474    $group = $defaultgroup if $defaultgroup ne $group_uniq;
475
476    if ($new_users_ok) {
477	# clean up backup
478	foreach $e (keys %groupmembers_bak) { delete $groupmembers_bak{$e}; }
479    } else {
480	# restore old groupmembers, user was not accept
481	foreach $e (keys %groupmembers_bak) {
482	    $groupmembers{$e} = $groupmembers_bak{$e};
483	}
484    }
485
486    while(1) {
487	$group_login = &confirm_list("Login group", 1, $group,
488				     ($name, $group));
489	last if $group_login eq $group;
490	last if $group_login eq $name;
491	last if defined $groupname{$group_login};
492	if ($group_login eq $group_uniq) {
493	    $group_login = $name; last;
494	}
495
496	if (defined $gid{$group_login}) {
497	    # convert numeric groupname (gid) to groupname
498	    $group_login = $gid{$group_login};
499	    last;
500	}
501	warn "Group does not exist!\a\n";
502    }
503
504    #if (defined($groupname{$group_login})) {
505    #	&add_group($groupname{$group_login}, $name);
506    #}
507
508    return ($group_login, $group_uniq) if $group_login eq $name;
509    return ($group_login, $group_login);
510}
511
512# return login group
513sub new_users_grplogin_batch {
514    local($name, $defaultgroup) = @_;
515    local($group_login, $group);
516
517    $group_login = $name;
518    $group_login = $defaultgroup if $defaultgroup ne $group_uniq;
519
520    if (defined $gid{$group_login}) {
521	# convert numeric groupname (gid) to groupname
522	$group_login = $gid{$group_login};
523    }
524
525    # if (defined($groupname{$group_login})) {
526    #	&add_group($groupname{$group_login}, $name);
527    # }
528
529    return $group_login
530	if defined($groupname{$group_login}) || $group_login eq $name;
531    warn "Group ``$group_login'' does not exist\a\n";
532    return 0;
533}
534
535# return other groups (string)
536sub new_users_groups {
537    local($name, $other_groups) = @_;
538    local($string) =
539	"Login group is ``$group_login''. Invite $name into other groups:";
540    local($e, $flag);
541    local($new_groups,$groups);
542
543    $other_groups = "no" unless $other_groups;
544
545    while(1) {
546	$groups = &confirm_list($string, 1, $other_groups,
547				("no", $other_groups, "guest"));
548	# no other groups
549	return "" if $groups eq "no";
550
551	($flag, $new_groups) = &new_users_groups_valid($groups);
552	last unless $flag;
553    }
554    $new_groups =~ s/\s*$//;
555    return $new_groups;
556}
557
558sub new_users_groups_valid {
559    local($groups) = @_;
560    local($e, $new_groups);
561    local($flag) = 0;
562
563    foreach $e (split(/[,\s]+/, $groups)) {
564	# convert numbers to groupname
565	if ($e =~ /^[0-9]+$/ && $gid{$e}) {
566	    $e = $gid{$e};
567	}
568	if (defined($groupname{$e})) {
569	    if ($e eq $group_login) {
570		# do not add user to a group if this group
571		# is also the login group.
572	    } elsif (&add_group($groupname{$e}, $name)) {
573		$new_groups .= "$e ";
574	    } else {
575		warn "$name is already member of group ``$e''\n";
576	    }
577	} else {
578	    warn "Group ``$e'' does not exist\a\n"; $flag++;
579	}
580    }
581    return ($flag, $new_groups);
582}
583
584# your last change
585sub new_users_ok {
586
587    print <<EOF;
588
589Name:	  $name
590Password: ****
591Fullname: $fullname
592Uid:	  $u_id
593Gid:	  $g_id ($group_login)
594Groups:	  $group_login $new_groups
595HOME:	  $home/$name
596Shell:	  $sh
597EOF
598
599    return &confirm_yn("OK?", "yes");
600}
601
602# make password database
603sub new_users_pwdmkdb {
604    local($last) = @_;
605    local($user);
606
607    $user = (split(/:/, $last))[0];
608    system(split(/\s+/, "$pwd_mkdb  -u $user $etc_passwd"));
609    if ($?) {
610	warn "$last\n";
611	warn "``$pwd_mkdb'' failed\n";
612	exit($? >> 8);
613    }
614}
615
616# update group database
617sub new_users_group_update {
618    local($e, @a);
619
620    # Add *new* group
621    if (!defined($groupname{$group_login}) && !defined($gid{$g_id})) {
622	push(@group_backup, "$group_login:*:$g_id:");
623	$groupname{$group_login} = $g_id;
624	$gid{$g_id} = $group_login;
625	# $groupmembers{$g_id} = $group_login;
626    }
627
628    if ($new_groups || defined($groupname{$group_login}) ||
629	defined($gid{$groupname{$group_login}}) &&
630		$gid{$groupname{$group_login}} ne "+") {
631	# new user is member of some groups
632	# new login group is already in name space
633	rename($group, "$group.bak");
634	#warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n";
635	foreach $e (sort {$a <=> $b} (keys %gid)) {
636	    push(@a, "$gid{$e}:*:$e:$groupmembers{$e}");
637	}
638	&append_file($group, @a);
639    } else {
640	&append_file($group, "$group_login:*:$g_id:");
641    }
642
643}
644
645sub new_users_passwd_update {
646    # update passwd/group variables
647    push(@passwd_backup, $new_entry);
648    $username{$name} = $u_id;
649    $uid{$u_id} = $name;
650    $pwgid{$g_id} = $name;
651}
652
653# send message to new user
654sub new_users_sendmessage {
655    return 1 if $send_message eq "no";
656
657    local($cc) =
658	&confirm_list("Send message to ``$name'' and:",
659		      1, "no", ("root", "second_mail_address",
660		      "no carbon copy"));
661    local($e);
662    $cc = "" if $cc eq "no";
663
664    @message_buffer = ();
665    message_read ($send_message);
666
667    foreach $e (@message_buffer) {
668	print eval "\"$e\"";
669    }
670    print "\n";
671
672    local(@message_buffer_append) = ();
673    if (!&confirm_yn("Add anything to default message", "no")) {
674	print "Use ``.'' or ^D alone on a line to finish your message.\n";
675	push(@message_buffer_append, "\n");
676	while($read = <STDIN>) {
677	    last if $read eq "\.\n";
678	    push(@message_buffer_append, $read);
679	}
680    }
681
682    &sendmessage("$name $cc", (@message_buffer, @message_buffer_append))
683	if (&confirm_yn("Send message", "yes"));
684}
685
686sub sendmessage {
687    local($to, @message) = @_;
688    local($e);
689
690    if (!open(M, "| mail -s Welcome $to")) {
691	warn "Cannot send mail to: $to!\n";
692	return 0;
693    } else {
694	foreach $e (@message) {
695	    print M eval "\"$e\"";
696	}
697	close M;
698    }
699}
700
701
702sub new_users_password {
703
704    # empty password
705    return "" if $defaultpasswd ne "yes";
706
707    local($password);
708
709    while(1) {
710	system("stty", "-echo");
711	$password = &confirm_list("Enter password", 1, "", "");
712	system("stty", "echo");
713	print "\n";
714	if ($password ne "") {
715	    system("stty", "-echo");
716	    $newpass = &confirm_list("Enter password again", 1, "", "");
717	    system("stty", "echo");
718	    print "\n";
719	    last if $password eq $newpass;
720	    print "They didn't match, please try again\n";
721	}
722	elsif (!&confirm_yn("Set the password so that user cannot logon?", "no")) {
723	    last;
724	}
725    }
726
727    return $password;
728}
729
730
731sub new_users {
732
733    print "\n" if $verbose;
734    print "Ok, let's go.\n" .
735	  "Don't worry about mistakes. I will give you the chance later to " .
736	  "correct any input.\n" if $verbose;
737
738    # name: Username
739    # fullname: Full name
740    # sh: shell
741    # u_id: user id
742    # g_id: group id
743    # group_login: groupname of g_id
744    # new_groups: some other groups
745    local($name, $group_login, $fullname, $sh, $u_id, $g_id, $new_groups);
746    local($groupmembers_bak, $cryptpwd);
747    local($new_users_ok) = 1;
748
749
750    $new_groups = "no" unless $groupname{$new_groups};
751
752    while(1) {
753	$name = &new_users_name;
754	$fullname = &new_users_fullname($name);
755	$sh = &new_users_shell;
756	($u_id, $g_id) = &new_users_id($name);
757	($group_login, $defaultgroup) =
758	    &new_users_grplogin($name, $defaultgroup, $new_users_ok);
759	# do not use uniq username and login group
760	$g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
761
762	$new_groups = &new_users_groups($name, $new_groups);
763	$password = &new_users_password;
764
765
766	if (&new_users_ok) {
767	    $new_users_ok = 1;
768
769	    $cryptpwd = "*";	# Locked by default
770	    $cryptpwd = encrypt($password, &salt) if ($password ne "");
771
772	    # obscure perl bug
773	    $new_entry = "$name\:" . "$cryptpwd" .
774		"\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
775	    &append_file($etc_passwd, "$new_entry");
776	    &new_users_pwdmkdb("$new_entry");
777	    &new_users_group_update;
778	    &new_users_passwd_update;  print "Added user ``$name''\n";
779	    &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname");
780	    &home_create($name, $group_login);
781	    &new_users_sendmessage;
782	} else {
783	    $new_users_ok = 0;
784	}
785	if (!&confirm_yn("Add another user?", "yes")) {
786	    print "Goodbye!\n" if $verbose;
787	    last;
788	}
789	print "\n" if !$verbose;
790    }
791}
792
793sub batch {
794    local($name, $groups, $fullname, $password) = @_;
795    local($sh);
796
797    $defaultshell = &shell_default_valid($defaultshell);
798    return 0 unless $home = &home_partition_valid($home);
799    return 0 if $dotdir ne &dotdir_default_valid($dotdir);
800    $send_message = &message_default;
801
802    return 0 if $name ne &new_users_name_valid($name);
803    $sh = $shell{$defaultshell};
804    ($u_id, $g_id) = &next_id($name);
805    $group_login = &new_users_grplogin_batch($name, $defaultgroup);
806    return 0 unless $group_login;
807    $g_id = $groupname{$group_login} if (defined($groupname{$group_login}));
808    ($flag, $new_groups) = &new_users_groups_valid($groups);
809    return 0 if $flag;
810
811    $cryptpwd = "*";	# Locked by default
812    if ($password ne "" && $password ne "*") {
813	if($unencrypted)	{ $cryptpwd = encrypt($password, &salt) }
814	else			{ $cryptpwd = $password }
815    }
816    # obscure perl bug
817    $new_entry = "$name\:" . "$cryptpwd" .
818	"\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh";
819    &append_file($etc_passwd, "$new_entry");
820    &new_users_pwdmkdb("$new_entry");
821    &new_users_group_update;
822    &new_users_passwd_update;  print "Added user ``$name''\n";
823    &sendmessage($name, @message_buffer) if $send_message ne "no";
824    &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname");
825    &home_create($name, $group_login);
826}
827
828# ask for password usage
829sub password_default {
830    local($p) = $defaultpasswd;
831    if ($verbose) {
832	$p = &confirm_yn("Prompt for passwords by default", $defaultpasswd);
833	$changes++ unless $p;
834    }
835    return "yes" if (($defaultpasswd eq "yes" && $p) ||
836		     ($defaultpasswd eq "no" && !$p));
837    return "no";    # otherwise
838}
839
840# get default encryption method
841sub encryption_default {
842    local($m) = "";
843    if ($verbose) {
844	while (&encryption_check($m) == 0) {
845            $m = &confirm_list("Default encryption method for passwords:", 1,
846                              $encryption_methods[0], @encryption_methods);
847	}
848    }
849    return($m);
850}
851
852# Confirm that we have a valid encryption method
853sub encryption_check {
854    local($m) = $_[0];
855
856    foreach $i (@encryption_methods) {
857        if ($m eq $i) { return 1; }
858    }
859
860    if ($m =~ /^blowfish,(\d+)$/) { return 1; }
861    return 0;
862}
863
864# misc
865sub check_root {
866    die "You are not root!\n" if $< && !$test;
867}
868
869sub usage {
870    warn <<USAGE;
871usage: adduser
872    [-batch username [group[,group]...] [fullname] [password]]
873    [-check_only]
874    [-config_create]
875    [-dotdir dotdir]
876    [-e|-encryption method]
877    [-group login_group]
878    [-h|-help]
879    [-home home]
880    [-message message_file]
881    [-noconfig]
882    [-shell shell]
883    [-s|-silent|-q|-quiet]
884    [-uid_start uid_start]
885    [-uid_end uid_end]
886    [-unencrypted]
887    [-v|-verbose]
888
889home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup
890message_file=$send_message uid_start=$uid_start uid_end=$uid_end
891USAGE
892    exit 1;
893}
894
895# uniq(1)
896sub uniq {
897    local(@list) = @_;
898    local($e, $last = "", @array);
899
900    foreach $e (sort @list) {
901	push(@array, $e) unless $e eq $last;
902	$last = $e;
903    }
904    return @array;
905}
906
907# Generate an appropriate argument to encrypt()
908# That may be a DES salt or a blowfish rotation count
909sub salt {
910    local($salt);		# initialization
911    if ($encryptionmethod eq "des" || $encryptionmethod eq "old") {
912        local($i, $rand);
913        local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63
914
915        warn "calculate salt\n" if $verbose > 1;
916
917        for ($i = 0; $i < 8; $i++) {
918	    srand(time + $rand + $$);
919	    $rand = rand(25*29*17 + $rand);
920	    $salt .=  $itoa64[$rand & $#itoa64];
921        }
922    } elsif ($encryptionmethod eq "md5") {
923        $salt = "";
924    } elsif ($encryptionmethod =~ /^blowfish/ ) {
925        ($encryptionmethod, $salt) = split(/\,/, $encryptionmethod);
926	if ($salt eq "") { $salt = 7; }	# default rounds inf unspecified
927    } else {
928        warn "$encryptionmethod encryption method invalid\n" if ($verbose > 0);
929	warn "Falling back to blowfish,7...\n" if ($verbose > 0);
930	$encryptionmethod = "blowfish";
931	$salt = 7;
932    }
933
934    warn "Salt is: $salt\n" if $verbose > 1;
935
936    return $salt;
937}
938
939# Encrypt a password using the selected method
940sub encrypt {
941    local($pass, $salt) = ($_[0], $_[1]);
942    local($args, $crypt);
943
944    if ($encryptionmethod eq "des" || $encryptionmethod eq "old") {
945        $args = "-s $salt";
946    } elsif ($encryptionmethod eq "md5") {
947        $args = "-m";
948    } elsif ($encryptionmethod eq "blowfish") {
949        $args = "-b $salt";
950    }
951
952    open2(\*ENCRD, \*ENCWR, "/usr/bin/encrypt $args");
953    print ENCWR "$pass\n";
954    close ENCWR;
955    $crypt = <ENCRD>;
956    close ENCRD;
957    chomp $crypt;
958    die "encrypt failed" if (wait == -1 || $? != 0);
959    return($crypt);
960}
961
962# hints
963sub hints {
964    if ($verbose) {
965	print "Use option ``-silent'' if you don't want to see " .
966	      "all warnings and questions.\n\n";
967    }
968}
969
970#
971sub parse_arguments {
972    local(@argv) = @_;
973
974    while ($_ = $argv[0], /^-/) {
975	shift @argv;
976	last if /^--$/;
977	if    (/^--?(v|verbose)$/)	{ $verbose = 1 }
978	elsif (/^--?(s|silent|q|quiet)$/)  { $verbose = 0 }
979	elsif (/^--?(debug)$/)	    { $verbose = 2 }
980	elsif (/^--?(h|help|\?)$/)	{ &usage }
981	elsif (/^--?(home)$/)	 { $home = $argv[0]; shift @argv }
982	elsif (/^--?(shell)$/)	 { $defaultshell = $argv[0]; shift @argv }
983	elsif (/^--?(dotdir)$/)	 { $dotdir = $argv[0]; shift @argv }
984	elsif (/^--?(uid_start)$/)	 { $uid_start = $argv[0]; shift @argv }
985	elsif (/^--?(uid_end)$/)	 { $uid_end = $argv[0]; shift @argv }
986	elsif (/^--?(group)$/)	 { $defaultgroup = $argv[0]; shift @argv }
987	elsif (/^--?(check_only)$/) { $check_only = 1 }
988	elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv;
989				   $sendmessage = 1; }
990	elsif (/^--?(unencrypted)$/)	{ $unencrypted = 1 }
991	elsif (/^--?(batch)$/)	 {
992	    @batch = splice(@argv, 0, 4); $verbose = 0;
993	    die "batch: too few arguments\n" if $#batch < 0;
994	}
995	# see &config_read
996	elsif (/^--?(config_create)$/)	{ &hints; &create_conf; exit(0); }
997	elsif (/^--?(noconfig)$/)	{ $config_read = 0; }
998	elsif (/^--?(e|encryption)$/) {
999	    $encryptionmethod = $argv[0];
1000	    shift @argv;
1001	}
1002	else			    { &usage }
1003    }
1004    #&usage if $#argv < 0;
1005}
1006
1007sub basename {
1008    local($name) = @_;
1009    $name =~ s|/+$||;
1010    $name =~ s|.*/+||;
1011    return $name;
1012}
1013
1014sub dirname {
1015    local($name) = @_;
1016    $name = &stripdir($name);
1017    $name =~ s|/+[^/]+$||;
1018    $name = "/" unless $name;	# dirname of / is /
1019    return $name;
1020}
1021
1022# return 1 if $file is a readable file or link
1023sub filetest {
1024    local($file, $verbose) = @_;
1025
1026    if (-e $file) {
1027	if (-f $file || -l $file) {
1028	    return 1 if -r _;
1029	    warn "$file unreadable\n" if $verbose;
1030	} else {
1031	    warn "$file is not a plain file or link\n" if $verbose;
1032	}
1033    }
1034    return 0;
1035}
1036
1037# create or recreate configuration file prompting for values
1038sub create_conf {
1039    $create_conf = 1;
1040
1041    &shells_read;			# Pull in /etc/shells info
1042    &shells_add;			# maybe add some new shells
1043    $defaultshell = &shell_default;	# enter default shell
1044    $home = &home_partition($home);	# find HOME partition
1045    $dotdir = &dotdir_default;		# check $dotdir
1046    $send_message = &message_default;   # send message to new user
1047    $defaultpasswd = &password_default; # maybe use password
1048    $defaultencryption = &encryption_default;	# Encryption method
1049
1050    if ($send_message ne 'no') {
1051	&message_create($send_message);
1052    } else {
1053	&message_create($send_message_bak);
1054    }
1055    &config_write(1);
1056}
1057
1058# log for new user in /var/log/adduser
1059sub adduser_log {
1060    local($string) = @_;
1061    local($e);
1062
1063    return 1 if $logfile eq "no";
1064
1065    local($sec, $min, $hour, $mday, $mon, $year) = localtime;
1066    $year += 1900;
1067    $mon++;
1068
1069    foreach $e ('sec', 'min', 'hour', 'mday', 'mon') {
1070	# '7' -> '07'
1071	eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10);
1072    }
1073
1074    &append_file($logfile, "$year/$mon/$mday $hour:$min:$sec $string");
1075}
1076
1077# create HOME directory, copy dotfiles from $dotdir to $HOME
1078sub home_create {
1079    local($name, $group) = @_;
1080    local($homedir) = "$home/$name";
1081
1082    if (-e "$homedir") {
1083	warn "HOME Directory ``$homedir'' already exists\a\n";
1084	return 0;
1085    }
1086
1087    if ($dotdir eq 'no') {
1088	if (!mkdir("$homedir",0755)) {
1089	    warn "mkdir $homedir: $!\n"; return 0;
1090	}
1091	system 'chown', "$name:$group", $homedir;
1092	return !$?;
1093    }
1094
1095    # copy files from  $dotdir to $homedir
1096    # rename 'dot.foo' files to '.foo'
1097    print "Copy files from $dotdir to $homedir\n" if $verbose;
1098    system("cp", "-R", $dotdir, $homedir);
1099    system("chmod", "-R", "u+wrX,go-w", $homedir);
1100    system("chown", "-R", "$name:$group", $homedir);
1101
1102    # security
1103    opendir(D, $homedir);
1104    foreach $file (readdir(D)) {
1105	if ($file =~ /^dot\./ && -f "$homedir/$file") {
1106	    $file =~ s/^dot\././;
1107	    rename("$homedir/dot$file", "$homedir/$file");
1108	}
1109	chmod(0600, "$homedir/$file")
1110	    if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/);
1111	chmod(0700, "$homedir/$file")
1112	    if ($file =~ /^(Mail|prv|\.(iscreen|term))$/);
1113    }
1114    closedir D;
1115    return 1;
1116}
1117
1118# makes a directory hierarchy
1119sub mkdir_home {
1120    local($dir) = @_;
1121    $dir = &stripdir($dir);
1122    local($user_partition) = "/usr";
1123    local($dirname) = &dirname($dir);
1124
1125
1126    -e $dirname || &mkdirhier($dirname);
1127
1128    if (((stat($dirname))[0]) == ((stat("/"))[0])){
1129	# home partition is on root partition
1130	# create home partition on $user_partition and make
1131	# a symlink from $dir to $user_partition/`basename $dir`
1132	# For instance: /home -> /usr/home
1133
1134	local($basename) = &basename($dir);
1135	local($d) = "$user_partition/$basename";
1136
1137
1138	if (-d $d) {
1139	    warn "Oops, $d already exists\n" if $verbose;
1140	} else {
1141	    print "Create $d\n" if $verbose;
1142	    if (!mkdir("$d", 0755)) {
1143		warn "$d: $!\a\n"; return 0;
1144	    }
1145	}
1146
1147	unlink($dir);		# symlink to nonexist file
1148	print "Create symlink: $dir -> $d\n" if $verbose;
1149	if (!symlink("$d", $dir)) {
1150	    warn "Symlink $d: $!\a\n"; return 0;
1151	}
1152    } else {
1153	print "Create $dir\n" if $verbose;
1154	if (!mkdir("$dir", 0755)) {
1155	    warn "Directory ``$dir'': $!\a\n"; return 0;
1156	}
1157    }
1158    return 1;
1159}
1160
1161sub mkdirhier {
1162    local($dir) = @_;
1163    local($d,$p);
1164
1165    $dir = &stripdir($dir);
1166
1167    foreach $d (split('/', $dir)) {
1168	$dir = "$p/$d";
1169	$dir =~ s|^//|/|;
1170	if (! -e "$dir") {
1171	    print "Create $dir\n" if $verbose;
1172	    if (!mkdir("$dir", 0755)) {
1173		warn "$dir: $!\n"; return 0;
1174	    }
1175	}
1176	$p .= "/$d";
1177    }
1178    return 1;
1179}
1180
1181# stript unused '/'
1182# F.i.: //usr///home// -> /usr/home
1183sub stripdir {
1184    local($dir) = @_;
1185
1186    $dir =~ s|/+|/|g;		# delete double '/'
1187    $dir =~ s|/$||;		# delete '/' at end
1188    return $dir if $dir ne "";
1189    return '/';
1190}
1191
1192# Read one of the elements from @list. $confirm is default.
1193# If !$allow accept only elements from @list.
1194sub confirm_list {
1195    local($message, $allow, $confirm, @list) = @_;
1196    local($read, $c, $print);
1197
1198    $print = "$message" if $message;
1199    $print .= " " unless $message =~ /\n$/ || $#list == 0;
1200
1201    $print .= join($", &uniq(@list)); #"
1202    $print .= " " unless $message =~ /\n$/ && $#list == 0;
1203    print "$print";
1204    print "\n" if (length($print) + length($confirm)) > 60;
1205    print "[$confirm]: ";
1206
1207    chop($read = <STDIN>);
1208    $read =~ s/^\s*//;
1209    $read =~ s/\s*$//;
1210    return $confirm if $read eq "";
1211    return "$read" if $allow;
1212
1213    foreach $c (@list) {
1214	return $read if $c eq $read;
1215    }
1216    warn "$read: is not allowed!\a\n";
1217    return &confirm_list($message, $allow, $confirm, @list);
1218}
1219
1220# YES or NO question
1221# return 1 if &confirm("message", "yes") and answer is yes
1222#	or if &confirm("message", "no") an answer is no
1223# otherwise 0
1224sub confirm_yn {
1225    local($message, $confirm) = @_;
1226    local($yes) = '^(yes|YES|y|Y)$';
1227    local($no) = '^(no|NO|n|N)$';
1228    local($read, $c);
1229
1230    if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
1231	$confirm = "y";
1232    } else {
1233	$confirm = "n";
1234    }
1235    print "$message (y/n) [$confirm]: ";
1236    chop($read = <STDIN>);
1237    $read =~ s/^\s*//;
1238    $read =~ s/\s*$//;
1239    return 1 unless $read;
1240
1241    if (($confirm eq "y" && $read =~ "$yes") ||
1242	($confirm eq "n" && $read =~ "$no")) {
1243	return 1;
1244    }
1245
1246    if ($read !~ "$yes" && $read !~ "$no") {
1247	warn "Wrong value. Enter again!\a\n";
1248	return &confirm_yn($message, $confirm);
1249    }
1250    return 0;
1251}
1252
1253# test if $dotdir exist
1254# return "no" if $dotdir not exist or dotfiles should not copied
1255sub dotdir_default {
1256    local($dir) = $dotdir;
1257
1258    return &dotdir_default_valid($dir) unless $verbose;
1259    while($verbose) {
1260	$dir = &confirm_list("Copy dotfiles from:", 1,
1261	    $dir, ("no", $dotdir_bak, $dir));
1262	last if $dir eq &dotdir_default_valid($dir);
1263    }
1264    warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no";
1265
1266    $changes++ if $dir ne $dotdir;
1267    return $dir;
1268}
1269
1270sub dotdir_default_valid {
1271    local($dir) = @_;
1272
1273    return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/");
1274    return $dir if $dir eq "no";
1275    warn "Dotdir ``$dir'' is not a directory\a\n";
1276    return "no";
1277}
1278
1279# ask for messages to new users
1280sub message_default {
1281    local($file) = $send_message;
1282    local(@d) = ($file, $send_message_bak, "no");
1283
1284    while($verbose) {
1285	$file = &confirm_list("Send message from file:", 1, $file, @d);
1286	last if $file eq "no";
1287	last if &filetest($file, 1);
1288
1289	# maybe create message file
1290	&message_create($file) if &confirm_yn("Create ``$file''?", "yes");
1291	last if &filetest($file, 0);
1292	last if !&confirm_yn("File ``$file'' does not exist, try again?",
1293			     "yes");
1294    }
1295
1296    if ($file eq "no" || !&filetest($file, 0)) {
1297	warn "Do not send message\n" if $verbose;
1298	$file = "no";
1299    } else {
1300	&message_read($file);
1301    }
1302
1303    $changes++ if $file ne $send_message && $verbose;
1304    return $file;
1305}
1306
1307# create message file
1308sub message_create {
1309    local($file) = @_;
1310
1311    rename($file, "$file.bak");
1312    if (!open(M, "> $file")) {
1313	warn "Messagefile ``$file'': $!\n"; return 0;
1314    }
1315    print M <<EOF;
1316#
1317# Message file for adduser(8)
1318#   comment: ``#''
1319#   default variables: \$name, \$fullname, \$password
1320#   other variables:  see /etc/adduser.conf after
1321#		     line  ``$do_not_delete''
1322#
1323
1324\$fullname,
1325
1326your account ``\$name'' was created.
1327Have fun!
1328
1329See also chpass(1), finger(1), passwd(1)
1330EOF
1331    close M;
1332    return 1;
1333}
1334
1335# read message file into buffer
1336sub message_read {
1337    local($file) = @_;
1338    @message_buffer = ();
1339
1340    if (!open(R, "$file")) {
1341	warn "File ``$file'':$!\n"; return 0;
1342    }
1343    while(<R>) {
1344	push(@message_buffer, $_) unless /^\s*#/;
1345    }
1346    close R;
1347}
1348
1349# write @list to $file with file-locking
1350sub append_file {
1351    local($file,@list) = @_;
1352    local($e);
1353
1354    open(F, ">> $file") || die "$file: $!\n";
1355    print "Lock $file.\n" if $verbose > 1;
1356    while(!flock(F, LOCK_EX | LOCK_NB)) {
1357	warn "Cannot lock file: $file\a\n";
1358	die "Sorry, gave up\n"
1359	    unless &confirm_yn("Try again?", "yes");
1360    }
1361    print F join("\n", @list) . "\n";
1362    print "Unlock $file.\n" if $verbose > 1;
1363    flock(F, LOCK_UN);
1364    close F;
1365}
1366
1367# return free uid+gid
1368# uid == gid if possible
1369sub next_id {
1370    local($group) = @_;
1371
1372    $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end);
1373    # looking for next free uid
1374    while($uid{$uid_start}) {
1375	$uid_start++;
1376	$uid_start = 1000 if $uid_start >= $uid_end;
1377	print "$uid_start\n" if $verbose > 1;
1378    }
1379
1380    local($gid_start) = $uid_start;
1381    # group for user (username==groupname) already exist
1382    if ($groupname{$group}) {
1383	$gid_start = $groupname{$group};
1384    }
1385    # gid is in use, looking for another gid.
1386    # Note: uid and gid are not equal
1387    elsif ($gid{$uid_start}) {
1388	while($gid{$gid_start} || $uid{$gid_start}) {
1389	    $gid_start--;
1390	    $gid_start = $uid_end if $gid_start < 100;
1391	}
1392    }
1393    return ($uid_start, $gid_start);
1394}
1395
1396# read config file - typically /etc/adduser.conf
1397sub config_read {
1398    local($opt) = join " ", @_;
1399    local($user_flag) = 0;
1400
1401    # don't read config file
1402    return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read;
1403
1404    if (!-f $config) {
1405        warn("Couldn't find $config: creating a new adduser configuration file\n");
1406        &create_conf;
1407    }
1408
1409    if (!open(C, "$config")) {
1410	warn "$config: $!\n"; return 0;
1411    }
1412
1413    while(<C>) {
1414	# user defined variables
1415	/^$do_not_delete/ && $user_flag++;
1416	# found @array or $variable
1417	if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) {
1418	    eval $_;
1419	    #warn "$_";
1420	}
1421	next if /^$/;
1422	# lines with '^##' are not saved
1423	push(@user_variable_list, $_)
1424	    if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/);
1425    }
1426    #warn "X @user_variable_list X\n";
1427    close C;
1428}
1429
1430
1431# write config file
1432sub config_write {
1433    local($silent) = @_;
1434
1435    # nothing to do
1436    return 1 unless ($changes || ! -e $config || !$config_read || $silent);
1437
1438    if (!$silent) {
1439	if (-e $config) {
1440	    return 1 if &confirm_yn("\nWrite your changes to $config?", "no");
1441	} else {
1442	    return 1 unless
1443		&confirm_yn("\nWrite your configuration to $config?", "yes");
1444	}
1445    }
1446
1447    rename($config, "$config.bak");
1448    open(C, "> $config") || die "$config: $!\n";
1449
1450    # prepare some variables
1451    $send_message = "no" unless $send_message;
1452    $defaultpasswd = "no" unless $defaultpasswd;
1453    local($shpref) = "'" . join("', '", @shellpref) . "'";
1454    local($shpath) = "'" . join("', '", @path) . "'";
1455    local($user_var) = join('', @user_variable_list);
1456
1457    print C <<EOF;
1458#
1459# $rcsid
1460# $config - automatic generated by adduser(8)
1461#
1462# Note: adduser reads *and* writes this file.
1463#	You may change values, but don't add new things before the
1464#	line ``$do_not_delete''
1465#
1466
1467# verbose = [0-2]
1468verbose = $verbose
1469
1470# Get new password for new users
1471# defaultpasswd =  yes | no
1472defaultpasswd = $defaultpasswd
1473
1474# Default encryption method for user passwords
1475# Methods are all those listed in passwd.conf(5)
1476encryptionmethod = "$defaultencryption"
1477
1478# copy dotfiles from this dir ("/etc/skel" or "no")
1479dotdir = "$dotdir"
1480
1481# send this file to new user ("/etc/adduser.message" or "no")
1482send_message = "$send_message"
1483
1484# config file for adduser ("/etc/adduser.conf")
1485config = "$config"
1486
1487# logfile ("/var/log/adduser" or "no")
1488logfile = "$logfile"
1489
1490# default HOME directory ("/home")
1491home = "$home"
1492
1493# List of directories where shells located
1494# path = ('/bin', '/usr/bin', '/usr/local/bin')
1495path = ($shpath)
1496
1497# common shell list, first element has higher priority
1498# shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
1499shellpref = ($shpref)
1500
1501# defaultshell if not empty ("bash")
1502defaultshell = "$defaultshell"
1503
1504# defaultgroup ('USER' for same as username or any other valid group)
1505defaultgroup = $defaultgroup
1506
1507# new users get this uid
1508uid_start = $uid_start
1509uid_end = $uid_end
1510
1511$do_not_delete
1512## your own variables, see /etc/adduser.message
1513EOF
1514    print C "$user_var\n" if ($user_var ne '');
1515    print C "\n## end\n";
1516    close C;
1517}
1518
1519# check for sane variables
1520sub variable_check {
1521	# Check uid_start & uid_end
1522	warn "WARNING: uid_start < 1000!\n" if($uid_start < 1000);
1523	die "ERROR: uid_start >= uid_end!\n" if($uid_start >= $uid_end);
1524	# unencrypted really only usable in batch mode
1525	warn "WARNING: unencrypted only effective in batch mode\n"
1526	    if($#batch < 0 && $unencrypted);
1527}
1528
1529sub cleanup {
1530    local($sig) = @_;
1531
1532    print STDERR "Caught signal SIG$sig -- cleaning up.\n";
1533    system("stty", "echo");
1534    exit(0);
1535}
1536
1537END {
1538    if (-e $etc_ptmp && defined(fileno(PTMP))) {
1539	    close PTMP;
1540	    unlink($etc_ptmp) || warn "Error: unable to remove $etc_ptmp: $!\nPlease verify that $etc_ptmp no longer exists!\n";
1541    }
1542}
1543