1#!/usr/bin/perl 2# 3# $OpenBSD: adduser.perl,v 1.32 2001/09/07 20:34:11 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.32 2001/09/07 20:34:11 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 nesseccary 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}) && 622 !defined($gid{$groupname{$group_login}})) { 623 push(@group_backup, "$group_login:*:$g_id:"); 624 $groupname{$group_login} = $g_id; 625 $gid{$g_id} = $group_login; 626 # $groupmembers{$g_id} = $group_login; 627 } 628 629 if ($new_groups || defined($groupname{$group_login}) || 630 defined($gid{$groupname{$group_login}}) && 631 $gid{$groupname{$group_login}} ne "+") { 632 # new user is member of some groups 633 # new login group is already in name space 634 rename($group, "$group.bak"); 635 #warn "$group_login $groupname{$group_login} $groupmembers{$groupname{$group_login}}\n"; 636 foreach $e (sort {$a <=> $b} (keys %gid)) { 637 push(@a, "$gid{$e}:*:$e:$groupmembers{$e}"); 638 } 639 &append_file($group, @a); 640 } else { 641 &append_file($group, "$group_login:*:$g_id:"); 642 } 643 644} 645 646sub new_users_passwd_update { 647 # update passwd/group variables 648 push(@passwd_backup, $new_entry); 649 $username{$name} = $u_id; 650 $uid{$u_id} = $name; 651 $pwgid{$g_id} = $name; 652} 653 654# send message to new user 655sub new_users_sendmessage { 656 return 1 if $send_message eq "no"; 657 658 local($cc) = 659 &confirm_list("Send message to ``$name'' and:", 660 1, "no", ("root", "second_mail_address", 661 "no carbon copy")); 662 local($e); 663 $cc = "" if $cc eq "no"; 664 665 @message_buffer = (); 666 message_read ($send_message); 667 668 foreach $e (@message_buffer) { 669 print eval "\"$e\""; 670 } 671 print "\n"; 672 673 local(@message_buffer_append) = (); 674 if (!&confirm_yn("Add anything to default message", "no")) { 675 print "Use ``.'' or ^D alone on a line to finish your message.\n"; 676 push(@message_buffer_append, "\n"); 677 while($read = <STDIN>) { 678 last if $read eq "\.\n"; 679 push(@message_buffer_append, $read); 680 } 681 } 682 683 &sendmessage("$name $cc", (@message_buffer, @message_buffer_append)) 684 if (&confirm_yn("Send message", "yes")); 685} 686 687sub sendmessage { 688 local($to, @message) = @_; 689 local($e); 690 691 if (!open(M, "| mail -s Welcome $to")) { 692 warn "Cannot send mail to: $to!\n"; 693 return 0; 694 } else { 695 foreach $e (@message) { 696 print M eval "\"$e\""; 697 } 698 close M; 699 } 700} 701 702 703sub new_users_password { 704 705 # empty password 706 return "" if $defaultpasswd ne "yes"; 707 708 local($password); 709 710 while(1) { 711 system("stty", "-echo"); 712 $password = &confirm_list("Enter password", 1, "", ""); 713 system("stty", "echo"); 714 print "\n"; 715 if ($password ne "") { 716 system("stty", "-echo"); 717 $newpass = &confirm_list("Enter password again", 1, "", ""); 718 system("stty", "echo"); 719 print "\n"; 720 last if $password eq $newpass; 721 print "They didn't match, please try again\n"; 722 } 723 elsif (!&confirm_yn("Set the password so that user cannot logon?", "no")) { 724 last; 725 } 726 } 727 728 return $password; 729} 730 731 732sub new_users { 733 734 print "\n" if $verbose; 735 print "Ok, let's go.\n" . 736 "Don't worry about mistakes. I will give you the chance later to " . 737 "correct any input.\n" if $verbose; 738 739 # name: Username 740 # fullname: Full name 741 # sh: shell 742 # u_id: user id 743 # g_id: group id 744 # group_login: groupname of g_id 745 # new_groups: some other groups 746 local($name, $group_login, $fullname, $sh, $u_id, $g_id, $new_groups); 747 local($groupmembers_bak, $cryptpwd); 748 local($new_users_ok) = 1; 749 750 751 $new_groups = "no" unless $groupname{$new_groups}; 752 753 while(1) { 754 $name = &new_users_name; 755 $fullname = &new_users_fullname($name); 756 $sh = &new_users_shell; 757 ($u_id, $g_id) = &new_users_id($name); 758 ($group_login, $defaultgroup) = 759 &new_users_grplogin($name, $defaultgroup, $new_users_ok); 760 # do not use uniq username and login group 761 $g_id = $groupname{$group_login} if (defined($groupname{$group_login})); 762 763 $new_groups = &new_users_groups($name, $new_groups); 764 $password = &new_users_password; 765 766 767 if (&new_users_ok) { 768 $new_users_ok = 1; 769 770 $cryptpwd = "*"; # Locked by default 771 $cryptpwd = encrypt($password, &salt) if ($password ne ""); 772 773 # obscure perl bug 774 $new_entry = "$name\:" . "$cryptpwd" . 775 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh"; 776 &append_file($etc_passwd, "$new_entry"); 777 &new_users_pwdmkdb("$new_entry"); 778 &new_users_group_update; 779 &new_users_passwd_update; print "Added user ``$name''\n"; 780 &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname"); 781 &home_create($name, $group_login); 782 &new_users_sendmessage; 783 } else { 784 $new_users_ok = 0; 785 } 786 if (!&confirm_yn("Add another user?", "yes")) { 787 print "Goodbye!\n" if $verbose; 788 last; 789 } 790 print "\n" if !$verbose; 791 } 792} 793 794sub batch { 795 local($name, $groups, $fullname, $password) = @_; 796 local($sh); 797 798 $defaultshell = &shell_default_valid($defaultshell); 799 return 0 unless $home = &home_partition_valid($home); 800 return 0 if $dotdir ne &dotdir_default_valid($dotdir); 801 $send_message = &message_default; 802 803 return 0 if $name ne &new_users_name_valid($name); 804 $sh = $shell{$defaultshell}; 805 ($u_id, $g_id) = &next_id($name); 806 $group_login = &new_users_grplogin_batch($name, $defaultgroup); 807 return 0 unless $group_login; 808 $g_id = $groupname{$group_login} if (defined($groupname{$group_login})); 809 ($flag, $new_groups) = &new_users_groups_valid($groups); 810 return 0 if $flag; 811 812 $cryptpwd = "*"; # Locked by default 813 if ($password ne "" && $password ne "*") { 814 if($unencrypted) { $cryptpwd = encrypt($password, &salt) } 815 else { $cryptpwd = $password } 816 } 817 # obscure perl bug 818 $new_entry = "$name\:" . "$cryptpwd" . 819 "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh"; 820 &append_file($etc_passwd, "$new_entry"); 821 &new_users_pwdmkdb("$new_entry"); 822 &new_users_group_update; 823 &new_users_passwd_update; print "Added user ``$name''\n"; 824 &sendmessage($name, @message_buffer) if $send_message ne "no"; 825 &adduser_log("$name:*:$u_id:$g_id($group_login):$fullname"); 826 &home_create($name, $group_login); 827} 828 829# ask for password usage 830sub password_default { 831 local($p) = $defaultpasswd; 832 if ($verbose) { 833 $p = &confirm_yn("Prompt for passwords by default", $defaultpasswd); 834 $changes++ unless $p; 835 } 836 return "yes" if (($defaultpasswd eq "yes" && $p) || 837 ($defaultpasswd eq "no" && !$p)); 838 return "no"; # otherwise 839} 840 841# get default encryption method 842sub encryption_default { 843 local($m) = ""; 844 if ($verbose) { 845 while (&encryption_check($m) == 0) { 846 $m = &confirm_list("Default encryption method for passwords", 1, 847 $encryption_methods[0], @encryption_methods); 848 } 849 } 850 return($m); 851} 852 853# Confirm that we have a valid encryption method 854sub encryption_check { 855 local($m) = $_[0]; 856 857 foreach $i (@encryption_methods) { 858 if ($m eq $i) { return 1; } 859 } 860 861 if ($m =~ /^blowfish,(\d+)$/) { return 1; } 862 return 0; 863} 864 865# misc 866sub check_root { 867 die "You are not root!\n" if $< && !$test; 868} 869 870sub usage { 871 warn <<USAGE; 872usage: adduser 873 [-batch username [group[,group]...] [fullname] [password]] 874 [-check_only] 875 [-config_create] 876 [-dotdir dotdir] 877 [-e|-encryption method] 878 [-group login_group] 879 [-h|-help] 880 [-home home] 881 [-message message_file] 882 [-noconfig] 883 [-shell shell] 884 [-s|-silent|-q|-quiet] 885 [-uid_start uid_start] 886 [-uid_end uid_end] 887 [-unencrypted] 888 [-v|-verbose] 889 890home=$home shell=$defaultshell dotdir=$dotdir login_group=$defaultgroup 891message_file=$send_message uid_start=$uid_start uid_end=$uid_end 892USAGE 893 exit 1; 894} 895 896# uniq(1) 897sub uniq { 898 local(@list) = @_; 899 local($e, $last = "", @array); 900 901 foreach $e (sort @list) { 902 push(@array, $e) unless $e eq $last; 903 $last = $e; 904 } 905 return @array; 906} 907 908# Generate an appropriate argument to encrypt() 909# That may be a DES salt or a blowfish rotation count 910sub salt { 911 local($salt); # initialization 912 if ($encryptionmethod eq "des" || $encryptionmethod eq "old") { 913 local($i, $rand); 914 local(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63 915 916 warn "calculate salt\n" if $verbose > 1; 917 918 for ($i = 0; $i < 8; $i++) { 919 srand(time + $rand + $$); 920 $rand = rand(25*29*17 + $rand); 921 $salt .= $itoa64[$rand & $#itoa64]; 922 } 923 } elsif ($encryptionmethod eq "md5") { 924 $salt = ""; 925 } elsif ($encryptionmethod =~ /^blowfish/ ) { 926 ($encryptionmethod, $salt) = split(/\,/, $encryptionmethod); 927 if ($salt eq "") { $salt = 7; } # default rounds inf unspecified 928 } else { 929 warn "$encryptionmethod encryption method invalid\n" if ($verbose > 0); 930 warn "Falling back to blowfish,7...\n" if ($verbose > 0); 931 $encryptionmethod = "blowfish"; 932 $salt = 7; 933 } 934 935 warn "Salt is: $salt\n" if $verbose > 1; 936 937 return $salt; 938} 939 940# Encrypt a password using the selected method 941sub encrypt { 942 local($pass, $salt) = ($_[0], $_[1]); 943 local($args, $crypt); 944 945 if ($encryptionmethod eq "des" || $encryptionmethod eq "old") { 946 $args = "-s $salt"; 947 } elsif ($encryptionmethod eq "md5") { 948 $args = "-m"; 949 } elsif ($encryptionmethod eq "blowfish") { 950 $args = "-b $salt"; 951 } 952 953 open2(\*ENCRD, \*ENCWR, "/usr/bin/encrypt $args"); 954 print ENCWR "$pass\n"; 955 close ENCWR; 956 $crypt = <ENCRD>; 957 close ENCRD; 958 chomp $crypt; 959 die "encrypt failed" if (wait == -1 || $? != 0); 960 return($crypt); 961} 962 963# hints 964sub hints { 965 if ($verbose) { 966 print "Use option ``-silent'' if you don't want to see " . 967 "all warnings and questions.\n\n"; 968 } 969} 970 971# 972sub parse_arguments { 973 local(@argv) = @_; 974 975 while ($_ = $argv[0], /^-/) { 976 shift @argv; 977 last if /^--$/; 978 if (/^--?(v|verbose)$/) { $verbose = 1 } 979 elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 } 980 elsif (/^--?(debug)$/) { $verbose = 2 } 981 elsif (/^--?(h|help|\?)$/) { &usage } 982 elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv } 983 elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv } 984 elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv } 985 elsif (/^--?(uid_start)$/) { $uid_start = $argv[0]; shift @argv } 986 elsif (/^--?(uid_end)$/) { $uid_end = $argv[0]; shift @argv } 987 elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv } 988 elsif (/^--?(check_only)$/) { $check_only = 1 } 989 elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv; 990 $sendmessage = 1; } 991 elsif (/^--?(unencrypted)$/) { $unencrypted = 1 } 992 elsif (/^--?(batch)$/) { 993 @batch = splice(@argv, 0, 4); $verbose = 0; 994 die "batch: too few arguments\n" if $#batch < 0; 995 } 996 # see &config_read 997 elsif (/^--?(config_create)$/) { &hints; &create_conf; exit(0); } 998 elsif (/^--?(noconfig)$/) { $config_read = 0; } 999 elsif (/^--?(e|encryption)$/) { 1000 $encryptionmethod = $argv[0]; 1001 shift @argv; 1002 } 1003 else { &usage } 1004 } 1005 #&usage if $#argv < 0; 1006} 1007 1008sub basename { 1009 local($name) = @_; 1010 $name =~ s|/+$||; 1011 $name =~ s|.*/+||; 1012 return $name; 1013} 1014 1015sub dirname { 1016 local($name) = @_; 1017 $name = &stripdir($name); 1018 $name =~ s|/+[^/]+$||; 1019 $name = "/" unless $name; # dirname of / is / 1020 return $name; 1021} 1022 1023# return 1 if $file is a readable file or link 1024sub filetest { 1025 local($file, $verbose) = @_; 1026 1027 if (-e $file) { 1028 if (-f $file || -l $file) { 1029 return 1 if -r _; 1030 warn "$file unreadable\n" if $verbose; 1031 } else { 1032 warn "$file is not a plain file or link\n" if $verbose; 1033 } 1034 } 1035 return 0; 1036} 1037 1038# create or recreate configuration file prompting for values 1039sub create_conf { 1040 $create_conf = 1; 1041 1042 &shells_read; # Pull in /etc/shells info 1043 &shells_add; # maybe add some new shells 1044 $defaultshell = &shell_default; # enter default shell 1045 $home = &home_partition($home); # find HOME partition 1046 $dotdir = &dotdir_default; # check $dotdir 1047 $send_message = &message_default; # send message to new user 1048 $defaultpasswd = &password_default; # maybe use password 1049 $defaultencryption = &encryption_default; # Encryption method 1050 1051 if ($send_message ne 'no') { 1052 &message_create($send_message); 1053 } else { 1054 &message_create($send_message_bak); 1055 } 1056 &config_write(1); 1057} 1058 1059# log for new user in /var/log/adduser 1060sub adduser_log { 1061 local($string) = @_; 1062 local($e); 1063 1064 return 1 if $logfile eq "no"; 1065 1066 local($sec, $min, $hour, $mday, $mon, $year) = localtime; 1067 $year += 1900; 1068 $mon++; 1069 1070 foreach $e ('sec', 'min', 'hour', 'mday', 'mon') { 1071 # '7' -> '07' 1072 eval "\$$e = 0 . \$$e" if (eval "\$$e" < 10); 1073 } 1074 1075 &append_file($logfile, "$year/$mon/$mday $hour:$min:$sec $string"); 1076} 1077 1078# create HOME directory, copy dotfiles from $dotdir to $HOME 1079sub home_create { 1080 local($name, $group) = @_; 1081 local($homedir) = "$home/$name"; 1082 1083 if (-e "$homedir") { 1084 warn "HOME Directory ``$homedir'' already exists\a\n"; 1085 return 0; 1086 } 1087 1088 if ($dotdir eq 'no') { 1089 if (!mkdir("$homedir",0755)) { 1090 warn "mkdir $homedir: $!\n"; return 0; 1091 } 1092 system 'chown', "$name:$group", $homedir; 1093 return !$?; 1094 } 1095 1096 # copy files from $dotdir to $homedir 1097 # rename 'dot.foo' files to '.foo' 1098 print "Copy files from $dotdir to $homedir\n" if $verbose; 1099 system("cp", "-r", $dotdir, $homedir); 1100 system("chmod", "-R", "u+wrX,go-w", $homedir); 1101 system("chown", "-R", "$name:$group", $homedir); 1102 1103 # security 1104 opendir(D, $homedir); 1105 foreach $file (readdir(D)) { 1106 if ($file =~ /^dot\./ && -f "$homedir/$file") { 1107 $file =~ s/^dot\././; 1108 rename("$homedir/dot$file", "$homedir/$file"); 1109 } 1110 chmod(0600, "$homedir/$file") 1111 if ($file =~ /^\.(rhosts|Xauthority|kermrc|netrc)$/); 1112 chmod(0700, "$homedir/$file") 1113 if ($file =~ /^(Mail|prv|\.(iscreen|term))$/); 1114 } 1115 closedir D; 1116 return 1; 1117} 1118 1119# makes a directory hierarchy 1120sub mkdir_home { 1121 local($dir) = @_; 1122 $dir = &stripdir($dir); 1123 local($user_partition) = "/usr"; 1124 local($dirname) = &dirname($dir); 1125 1126 1127 -e $dirname || &mkdirhier($dirname); 1128 1129 if (((stat($dirname))[0]) == ((stat("/"))[0])){ 1130 # home partition is on root partition 1131 # create home partition on $user_partition and make 1132 # a symlink from $dir to $user_partition/`basename $dir` 1133 # For instance: /home -> /usr/home 1134 1135 local($basename) = &basename($dir); 1136 local($d) = "$user_partition/$basename"; 1137 1138 1139 if (-d $d) { 1140 warn "Oops, $d already exists\n" if $verbose; 1141 } else { 1142 print "Create $d\n" if $verbose; 1143 if (!mkdir("$d", 0755)) { 1144 warn "$d: $!\a\n"; return 0; 1145 } 1146 } 1147 1148 unlink($dir); # symlink to nonexist file 1149 print "Create symlink: $dir -> $d\n" if $verbose; 1150 if (!symlink("$d", $dir)) { 1151 warn "Symlink $d: $!\a\n"; return 0; 1152 } 1153 } else { 1154 print "Create $dir\n" if $verbose; 1155 if (!mkdir("$dir", 0755)) { 1156 warn "Directory ``$dir'': $!\a\n"; return 0; 1157 } 1158 } 1159 return 1; 1160} 1161 1162sub mkdirhier { 1163 local($dir) = @_; 1164 local($d,$p); 1165 1166 $dir = &stripdir($dir); 1167 1168 foreach $d (split('/', $dir)) { 1169 $dir = "$p/$d"; 1170 $dir =~ s|^//|/|; 1171 if (! -e "$dir") { 1172 print "Create $dir\n" if $verbose; 1173 if (!mkdir("$dir", 0755)) { 1174 warn "$dir: $!\n"; return 0; 1175 } 1176 } 1177 $p .= "/$d"; 1178 } 1179 return 1; 1180} 1181 1182# stript unused '/' 1183# F.i.: //usr///home// -> /usr/home 1184sub stripdir { 1185 local($dir) = @_; 1186 1187 $dir =~ s|/+|/|g; # delete double '/' 1188 $dir =~ s|/$||; # delete '/' at end 1189 return $dir if $dir ne ""; 1190 return '/'; 1191} 1192 1193# Read one of the elements from @list. $confirm is default. 1194# If !$allow accept only elements from @list. 1195sub confirm_list { 1196 local($message, $allow, $confirm, @list) = @_; 1197 local($read, $c, $print); 1198 1199 $print = "$message" if $message; 1200 $print .= " " unless $message =~ /\n$/ || $#list == 0; 1201 1202 $print .= join($", &uniq(@list)); #" 1203 $print .= " " unless $message =~ /\n$/ && $#list == 0; 1204 print "$print"; 1205 print "\n" if (length($print) + length($confirm)) > 60; 1206 print "[$confirm]: "; 1207 1208 chop($read = <STDIN>); 1209 $read =~ s/^\s*//; 1210 $read =~ s/\s*$//; 1211 return $confirm if $read eq ""; 1212 return "$read" if $allow; 1213 1214 foreach $c (@list) { 1215 return $read if $c eq $read; 1216 } 1217 warn "$read: is not allowed!\a\n"; 1218 return &confirm_list($message, $allow, $confirm, @list); 1219} 1220 1221# YES or NO question 1222# return 1 if &confirm("message", "yes") and answer is yes 1223# or if &confirm("message", "no") an answer is no 1224# otherwise 0 1225sub confirm_yn { 1226 local($message, $confirm) = @_; 1227 local($yes) = '^(yes|YES|y|Y)$'; 1228 local($no) = '^(no|NO|n|N)$'; 1229 local($read, $c); 1230 1231 if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) { 1232 $confirm = "y"; 1233 } else { 1234 $confirm = "n"; 1235 } 1236 print "$message (y/n) [$confirm]: "; 1237 chop($read = <STDIN>); 1238 $read =~ s/^\s*//; 1239 $read =~ s/\s*$//; 1240 return 1 unless $read; 1241 1242 if (($confirm eq "y" && $read =~ "$yes") || 1243 ($confirm eq "n" && $read =~ "$no")) { 1244 return 1; 1245 } 1246 1247 if ($read !~ "$yes" && $read !~ "$no") { 1248 warn "Wrong value. Enter again!\a\n"; 1249 return &confirm_yn($message, $confirm); 1250 } 1251 return 0; 1252} 1253 1254# test if $dotdir exist 1255# return "no" if $dotdir not exist or dotfiles should not copied 1256sub dotdir_default { 1257 local($dir) = $dotdir; 1258 1259 return &dotdir_default_valid($dir) unless $verbose; 1260 while($verbose) { 1261 $dir = &confirm_list("Copy dotfiles from:", 1, 1262 $dir, ("no", $dotdir_bak, $dir)); 1263 last if $dir eq &dotdir_default_valid($dir); 1264 } 1265 warn "Do not copy dotfiles.\n" if $verbose && $dir eq "no"; 1266 1267 $changes++ if $dir ne $dotdir; 1268 return $dir; 1269} 1270 1271sub dotdir_default_valid { 1272 local($dir) = @_; 1273 1274 return $dir if (-e $dir && -r _ && (-d _ || -l $dir) && $dir =~ "^/"); 1275 return $dir if $dir eq "no"; 1276 warn "Dotdir ``$dir'' is not a directory\a\n"; 1277 return "no"; 1278} 1279 1280# ask for messages to new users 1281sub message_default { 1282 local($file) = $send_message; 1283 local(@d) = ($file, $send_message_bak, "no"); 1284 1285 while($verbose) { 1286 $file = &confirm_list("Send message from file:", 1, $file, @d); 1287 last if $file eq "no"; 1288 last if &filetest($file, 1); 1289 1290 # maybe create message file 1291 &message_create($file) if &confirm_yn("Create ``$file''?", "yes"); 1292 last if &filetest($file, 0); 1293 last if !&confirm_yn("File ``$file'' does not exist, try again?", 1294 "yes"); 1295 } 1296 1297 if ($file eq "no" || !&filetest($file, 0)) { 1298 warn "Do not send message\n" if $verbose; 1299 $file = "no"; 1300 } else { 1301 &message_read($file); 1302 } 1303 1304 $changes++ if $file ne $send_message && $verbose; 1305 return $file; 1306} 1307 1308# create message file 1309sub message_create { 1310 local($file) = @_; 1311 1312 rename($file, "$file.bak"); 1313 if (!open(M, "> $file")) { 1314 warn "Messagefile ``$file'': $!\n"; return 0; 1315 } 1316 print M <<EOF; 1317# 1318# Message file for adduser(8) 1319# comment: ``#'' 1320# default variables: \$name, \$fullname, \$password 1321# other variables: see /etc/adduser.conf after 1322# line ``$do_not_delete'' 1323# 1324 1325\$fullname, 1326 1327your account ``\$name'' was created. 1328Have fun! 1329 1330See also chpass(1), finger(1), passwd(1) 1331EOF 1332 close M; 1333 return 1; 1334} 1335 1336# read message file into buffer 1337sub message_read { 1338 local($file) = @_; 1339 @message_buffer = (); 1340 1341 if (!open(R, "$file")) { 1342 warn "File ``$file'':$!\n"; return 0; 1343 } 1344 while(<R>) { 1345 push(@message_buffer, $_) unless /^\s*#/; 1346 } 1347 close R; 1348} 1349 1350# write @list to $file with file-locking 1351sub append_file { 1352 local($file,@list) = @_; 1353 local($e); 1354 1355 open(F, ">> $file") || die "$file: $!\n"; 1356 print "Lock $file.\n" if $verbose > 1; 1357 while(!flock(F, LOCK_EX | LOCK_NB)) { 1358 warn "Cannot lock file: $file\a\n"; 1359 die "Sorry, gave up\n" 1360 unless &confirm_yn("Try again?", "yes"); 1361 } 1362 print F join("\n", @list) . "\n"; 1363 print "Unlock $file.\n" if $verbose > 1; 1364 flock(F, LOCK_UN); 1365 close F; 1366} 1367 1368# return free uid+gid 1369# uid == gid if possible 1370sub next_id { 1371 local($group) = @_; 1372 1373 $uid_start = 1000 if ($uid_start <= 0 || $uid_start >= $uid_end); 1374 # looking for next free uid 1375 while($uid{$uid_start}) { 1376 $uid_start++; 1377 $uid_start = 1000 if $uid_start >= $uid_end; 1378 print "$uid_start\n" if $verbose > 1; 1379 } 1380 1381 local($gid_start) = $uid_start; 1382 # group for user (username==groupname) already exist 1383 if ($groupname{$group}) { 1384 $gid_start = $groupname{$group}; 1385 } 1386 # gid is in use, looking for another gid. 1387 # Note: uid and gid are not equal 1388 elsif ($gid{$uid_start}) { 1389 while($gid{$gid_start} || $uid{$gid_start}) { 1390 $gid_start--; 1391 $gid_start = $uid_end if $gid_start < 100; 1392 } 1393 } 1394 return ($uid_start, $gid_start); 1395} 1396 1397# read config file - typically /etc/adduser.conf 1398sub config_read { 1399 local($opt) = join " ", @_; 1400 local($user_flag) = 0; 1401 1402 # don't read config file 1403 return 1 if $opt =~ /-(noconfig|config_create)/ || !$config_read; 1404 1405 if (!-f $config) { 1406 warn("Couldn't find $config: creating a new adduser configuration file\n"); 1407 &create_conf; 1408 } 1409 1410 if (!open(C, "$config")) { 1411 warn "$config: $!\n"; return 0; 1412 } 1413 1414 while(<C>) { 1415 # user defined variables 1416 /^$do_not_delete/ && $user_flag++; 1417 # found @array or $variable 1418 if (s/^(\w+\s*=\s*\()/\@$1/ || s/^(\w+\s*=)/\$$1/) { 1419 eval $_; 1420 #warn "$_"; 1421 } 1422 next if /^$/; 1423 # lines with '^##' are not saved 1424 push(@user_variable_list, $_) 1425 if $user_flag && !/^##/ && (s/^[\$\@]// || /^[#\s]/); 1426 } 1427 #warn "X @user_variable_list X\n"; 1428 close C; 1429} 1430 1431 1432# write config file 1433sub config_write { 1434 local($silent) = @_; 1435 1436 # nothing to do 1437 return 1 unless ($changes || ! -e $config || !$config_read || $silent); 1438 1439 if (!$silent) { 1440 if (-e $config) { 1441 return 1 if &confirm_yn("\nWrite your changes to $config?", "no"); 1442 } else { 1443 return 1 unless 1444 &confirm_yn("\nWrite your configuration to $config?", "yes"); 1445 } 1446 } 1447 1448 rename($config, "$config.bak"); 1449 open(C, "> $config") || die "$config: $!\n"; 1450 1451 # prepare some variables 1452 $send_message = "no" unless $send_message; 1453 $defaultpasswd = "no" unless $defaultpasswd; 1454 local($shpref) = "'" . join("', '", @shellpref) . "'"; 1455 local($shpath) = "'" . join("', '", @path) . "'"; 1456 local($user_var) = join('', @user_variable_list); 1457 1458 print C <<EOF; 1459# 1460# $rcsid 1461# $config - automatic generated by adduser(8) 1462# 1463# Note: adduser reads *and* writes this file. 1464# You may change values, but don't add new things before the 1465# line ``$do_not_delete'' 1466# 1467 1468# verbose = [0-2] 1469verbose = $verbose 1470 1471# Get new password for new users 1472# defaultpasswd = yes | no 1473defaultpasswd = $defaultpasswd 1474 1475# Default encryption method for user passwords 1476# Methods are all those listed in passwd.conf(5) 1477encryptionmethod = "$defaultencryption" 1478 1479# copy dotfiles from this dir ("/etc/skel" or "no") 1480dotdir = "$dotdir" 1481 1482# send this file to new user ("/etc/adduser.message" or "no") 1483send_message = "$send_message" 1484 1485# config file for adduser ("/etc/adduser.conf") 1486config = "$config" 1487 1488# logfile ("/var/log/adduser" or "no") 1489logfile = "$logfile" 1490 1491# default HOME directory ("/home") 1492home = "$home" 1493 1494# List of directories where shells located 1495# path = ('/bin', '/usr/bin', '/usr/local/bin') 1496path = ($shpath) 1497 1498# common shell list, first element has higher priority 1499# shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh') 1500shellpref = ($shpref) 1501 1502# defaultshell if not empty ("bash") 1503defaultshell = "$defaultshell" 1504 1505# defaultgroup ('USER' for same as username or any other valid group) 1506defaultgroup = $defaultgroup 1507 1508# new users get this uid 1509uid_start = $uid_start 1510uid_end = $uid_end 1511 1512$do_not_delete 1513## your own variables, see /etc/adduser.message 1514EOF 1515 print C "$user_var\n" if ($user_var ne ''); 1516 print C "\n## end\n"; 1517 close C; 1518} 1519 1520# check for sane variables 1521sub variable_check { 1522 # Check uid_start & uid_end 1523 warn "WARNING: uid_start < 1000!\n" if($uid_start < 1000); 1524 die "ERROR: uid_start >= uid_end!\n" if($uid_start >= $uid_end); 1525 # unencrypted really only usable in batch mode 1526 warn "WARNING: unencrypted only effective in batch mode\n" 1527 if($#batch < 0 && $unencrypted); 1528} 1529 1530sub cleanup { 1531 local($sig) = @_; 1532 1533 print STDERR "Caught signal SIG$sig -- cleaning up.\n"; 1534 exit(0); 1535} 1536 1537END { 1538 if (-e $etc_ptmp && defined 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