X-Git-Url: https://git.cameronkatri.com/pw-darwin.git/blobdiff_plain/67c956f4295255e54027c4a05528dccc9d027fdb..b0c53286ae634ab410a90cd6ec0372e9961ab406:/adduser/adduser.perl?ds=inline diff --git a/adduser/adduser.perl b/adduser/adduser.perl index f13b69c..dec26df 100644 --- a/adduser/adduser.perl +++ b/adduser/adduser.perl @@ -1,7 +1,7 @@ #!/usr/bin/perl # -# Copyright (c) 1995 Wolfram Schneider. All rights reserved. -# Alle Rechte vorbehalten. Es gilt das kontinentaleuropische Urheberrecht. +# Copyright (c) 1995-1996 Wolfram Schneider . Berlin. +# All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions @@ -11,29 +11,21 @@ # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. -# 3. All advertising materials mentioning features or use of this software -# must display the following acknowledgement: -# This product includes software developed by Wolfram Schneider -# 4. The name of the author may not be used to endorse or promote products -# derived from this software without specific prior written permission # -# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# /usr/sbin/adduser - add new user(s) -# -# Email: Wolfram Schneider -# -# $Id: adduser.perl,v 1.10 1996/02/10 17:15:47 wosch Exp $ +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. # +# $Id: adduser.perl,v 1.30 1997/05/01 23:23:06 ache Exp $ + # read variables sub variables { @@ -42,7 +34,7 @@ sub variables { $dotdir = "/usr/share/skel"; # copy dotfiles from this dir $dotdir_bak = $dotdir; $send_message = "/etc/adduser.message"; # send message to new user - $send_message_bak = $send_message; + $send_message_bak = '/etc/adduser.message'; $config = "/etc/adduser.conf"; # config file for adduser $config_read = 1; # read config file $logfile = "/var/log/adduser"; # logfile @@ -61,6 +53,7 @@ sub variables { $defaultshell = 'sh'; # defaultshell if not empty $group_uniq = 'USER'; $defaultgroup = $group_uniq;# login groupname, $group_uniq means username + $defaultclass = ''; $uid_start = 1000; # new users get this uid $uid_end = 32000; # max. uid @@ -124,10 +117,10 @@ sub shells_read { } } -# Allow /nonexistant and /bin/date as a valid shell for system utils - push(@list, "/nonexistant"); +# Allow /nonexistent and /bin/date as a valid shell for system utils + push(@list, "/nonexistent"); push(@shellpref, "no"); - $shell{"no"} = "/nonexistant"; + $shell{"no"} = "/nonexistent"; push(@list, "/bin/date"); push(@shellpref, "date"); @@ -143,7 +136,7 @@ sub shells_add { return 1 unless $verbose; foreach $sh (@shellpref) { - # all knowned shells + # all known shells if (!$shell{$sh}) { # shell $sh is not defined as login shell foreach $dir (@path) { @@ -162,7 +155,7 @@ sub shells_add { &append_file($etc_shells, @list) if $#list >= 0; } -# choise your favourite shell an return the shell +# choose your favourite shell and return the shell sub shell_default { local($e,$i,$new_shell); local($sh); @@ -225,7 +218,7 @@ sub home_partition_valid { } if (-e $h) { - warn "$h exist, but is it not a directory or symlink!\n" + warn "$h exists, but is not a directory or symlink!\n" unless -d $h || -l $h; warn "$h is not writable!\n" unless -w $h; @@ -239,7 +232,7 @@ sub home_partition_valid { # check for valid passwddb sub passwd_check { - system("$pwd_mkdb $etc_passwd"); + system("$pwd_mkdb -c $etc_passwd"); die "\nInvalid $etc_passwd - cannot add any users!\n" if $?; } @@ -253,17 +246,21 @@ sub passwd_read { while(

) { chop; push(@passwd_backup, $_); + # ignore comments + next if /^\s*$/; + next if /^\s*#/; + ($p_username, $pw, $p_uid, $p_gid, $sh) = (split(/:/, $_))[0..3,9]; - print "$p_username already exist with uid: $username{$p_username}!\n" + print "$p_username already exists with uid: $username{$p_username}!\n" if $username{$p_username} && $verbose; $username{$p_username} = $p_uid; - print "User $p_username: uid $p_uid exist twice: $uid{$p_uid}\n" + print "User $p_username: uid $p_uid exists twice: $uid{$p_uid}\n" if $uid{$p_uid} && $verbose && $p_uid; # don't warn for uid 0 print "User $p_username: illegal shell: ``$sh''\n" if ($verbose && $sh && !$shell{&basename($sh)} && - $p_username !~ /^(bin|uucp|falcon|nobody)$/ && + $p_username !~ /^(news|xten|bin|nobody|uucp)$/ && $sh !~ /\/(pppd|sliplogin)$/); $uid{$p_uid} = $p_username; $pwgid{$p_gid} = $p_username; @@ -280,13 +277,17 @@ sub group_read { while() { chop; push(@group_backup, $_); + # ignore comments + next if /^\s*$/; + next if /^\s*#/; + ($g_groupname, $pw, $g_gid, $memb) = (split(/:/, $_))[0..3]; $groupmembers{$g_gid} = $memb; - warn "Groupname exist twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n" + warn "Groupname exists twice: $g_groupname:$g_gid -> $g_groupname:$groupname{$g_groupname}\n" if $groupname{$g_groupname} && $verbose; $groupname{$g_groupname} = $g_gid; - warn "Groupid exist twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n" + warn "Groupid exists twice: $g_groupname:$g_gid -> $gid{$g_gid}:$g_gid\n" if $gid{$g_gid} && $verbose; $gid{$g_gid} = $g_groupname; } @@ -315,9 +316,9 @@ sub new_users_name { local($name); while(1) { - $name = &confirm_list("Enter username", 1, "a-z0-9", ""); - if (length($name) > 8) { - warn "Username is longer than 8 chars\a\n"; + $name = &confirm_list("Enter username", 1, "a-z0-9_-", ""); + if (length($name) > 16) { + warn "Username is longer than 16 chars\a\n"; next; } last if (&new_users_name_valid($name) eq $name); @@ -328,7 +329,7 @@ sub new_users_name { sub new_users_name_valid { local($name) = @_; - if ($name !~ /^[a-z0-9]+$/) { + if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/) { warn "Wrong username. " . "Please use only lowercase characters or digits\a\n"; return 0; @@ -391,6 +392,16 @@ sub new_users_id { return &next_id($name); } +# return login class for user +sub new_users_class { + local($def) = @_; + local($class); + + $class = &confirm_list("Enter login class:", 1, $def, ($def, "default")); + $class = "" if $class eq "default"; + return $class; +} + # add user to group sub add_group { local($gid, $name) = @_; @@ -442,9 +453,9 @@ sub new_users_grplogin { warn "Group does not exist!\a\n"; } - if (defined($groupname{$group_login})) { - &add_group($groupname{$group_login}, $name); - } + #if (defined($groupname{$group_login})) { + # &add_group($groupname{$group_login}, $name); + #} return ($group_login, $group_uniq) if $group_login eq $name; return ($group_login, $group_login); @@ -463,9 +474,9 @@ sub new_users_grplogin_batch { $group_login = $gid{$group_login}; } - if (defined($groupname{$group_login})) { - &add_group($groupname{$group_login}, $name); - } + # if (defined($groupname{$group_login})) { + # &add_group($groupname{$group_login}, $name); + # } return $group_login if defined($groupname{$group_login}) || $group_login eq $name; @@ -507,7 +518,10 @@ sub new_users_groups_valid { $e = $gid{$e}; } if (defined($groupname{$e})) { - if (&add_group($groupname{$e}, $name)) { + if ($e eq $group_login) { + # do not add user to a group if this group + # is also the login group. + } elsif (&add_group($groupname{$e}, $name)) { $new_groups .= "$e "; } else { warn "$name is already member of group ``$e''\n"; @@ -525,10 +539,11 @@ sub new_users_ok { print < $crash") || die "Sorry, give up\n"; - $j = join("\n", @passwd_backup); - $j =~ s/\n//; - print R $j . "\n"; - close R; - - system("$pwd_mkdb $crash"); - die "Sorry, give up\n" if $?; - die "Successfully restore $etc_passwd. Exit.\n"; + warn "``$pwd_mkdb'' failed\n"; + exit($? >> 8); } } @@ -566,14 +571,15 @@ sub new_users_group_update { # Add *new* group if (!defined($groupname{$group_login}) && !defined($gid{$groupname{$group_login}})) { - push(@group_backup, "$group_login:*:$g_id:$group_login"); + push(@group_backup, "$group_login:*:$g_id:"); $groupname{$group_login} = $g_id; $gid{$g_id} = $group_login; - $groupmembers{$g_id} = $group_login; + # $groupmembers{$g_id} = $group_login; } if ($new_groups || defined($groupname{$group_login}) || - defined($gid{$groupname{$group_login}})) { + defined($gid{$groupname{$group_login}}) && + $gid{$groupname{$group_login}} ne "+") { # new user is member of some groups # new login group is already in name space rename($group, "$group.bak"); @@ -583,7 +589,7 @@ sub new_users_group_update { } &append_file($group, @a); } else { - &append_file($group, "$group_login:*:$g_id:$group_login"); + &append_file($group, "$group_login:*:$g_id:"); } } @@ -649,9 +655,21 @@ sub new_users_password { local($password); while(1) { + system("stty -echo"); $password = &confirm_list("Enter password", 1, "", ""); - last if $password ne ""; - last if &confirm_yn("Use an empty password?", "yes"); + system("stty echo"); + print "\n"; + if ($password ne "") { + system("stty -echo"); + $newpass = &confirm_list("Enter password again", 1, "", ""); + system("stty echo"); + print "\n"; + last if $password eq $newpass; + print "They didn't match, please try again\n"; + } + elsif (&confirm_yn("Use an empty password?", "yes")) { + last; + } } return $password; @@ -670,9 +688,10 @@ sub new_users { # sh: shell # u_id: user id # g_id: group id + # class: login class # group_login: groupname of g_id # new_groups: some other groups - local($name, $group_login, $fullname, $sh, $u_id, $g_id, $new_groups); + local($name, $group_login, $fullname, $sh, $u_id, $g_id, $class, $new_groups); local($groupmembers_bak, $cryptpwd); local($new_users_ok) = 1; @@ -685,6 +704,7 @@ sub new_users { $fullname = &new_users_fullname($name); $sh = &new_users_shell; ($u_id, $g_id) = &new_users_id($name); + $class = &new_users_class($defaultclass); ($group_login, $defaultgroup) = &new_users_grplogin($name, $defaultgroup, $new_users_ok); # do not use uniq username and login group @@ -699,9 +719,9 @@ sub new_users { $cryptpwd = ""; $cryptpwd = crypt($password, &salt) if $password ne ""; - # obskure perl bug + # obscure perl bug $new_entry = "$name\:" . "$cryptpwd" . - "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh"; + "\:$u_id\:$g_id\:$class\:0:0:$fullname:$home/$name:$sh"; &append_file($etc_passwd, "$new_entry"); &new_users_pwdmkdb("$new_entry"); &new_users_group_update; @@ -721,7 +741,7 @@ sub new_users { } sub batch { - local($name, $groups, $fullname, $password) = @_; + local($name, $groups, $class, $fullname, $password) = @_; local($sh); $defaultshell = &shell_default_valid($defaultshell); @@ -738,11 +758,12 @@ sub batch { ($flag, $new_groups) = &new_users_groups_valid($groups); return 0 if $flag; + $class = $defaultclass if $class eq ""; $cryptpwd = ""; $cryptpwd = crypt($password, &salt) if $password ne ""; - # obskure perl bug + # obscure perl bug $new_entry = "$name\:" . "$cryptpwd" . - "\:$u_id\:$g_id\::0:0:$fullname:$home/$name:$sh"; + "\:$u_id\:$g_id\:$class\:0:0:$fullname:$home/$name:$sh"; &append_file($etc_passwd, "$new_entry"); &new_users_pwdmkdb("$new_entry"); &new_users_group_update; @@ -772,8 +793,9 @@ sub check_root { sub usage { warn < -EOF + return; } # hints @@ -849,20 +869,21 @@ sub parse_arguments { shift @argv; last if /^--$/; if (/^--?(v|verbose)$/) { $verbose = 1 } - elsif (/^--?(s|silent|q|quit)$/) { $verbose = 0 } + elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 } elsif (/^--?(debug)$/) { $verbose = 2 } elsif (/^--?(h|help|\?)$/) { &usage } elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv } elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv } elsif (/^--?(dotdir)$/) { $dotdir = $argv[0]; shift @argv } elsif (/^--?(uid)$/) { $uid_start = $argv[0]; shift @argv } + elsif (/^--?(class)$/) { $defaultclass = $argv[0]; shift @argv } elsif (/^--?(group)$/) { $defaultgroup = $argv[0]; shift @argv } elsif (/^--?(check_only)$/) { $check_only = 1 } elsif (/^--?(message)$/) { $send_message = $argv[0]; shift @argv; $sendmessage = 1; } elsif (/^--?(batch)$/) { - @batch = splice(@argv, 0, 4); $verbose = 0; - die "batch: to few arguments\n" if $#batch < 0; + @batch = splice(@argv, 0, 5); $verbose = 0; + die "batch: too few arguments\n" if $#batch < 0; } # see &config_read elsif (/^--?(config_create)$/) { &create_conf; } @@ -905,7 +926,11 @@ sub filetest { # create configuration files and exit sub create_conf { $create_conf = 1; - &message_create($send_message); + if ($send_message ne 'no') { + &message_create($send_message); + } else { + &message_create($send_message_bak); + } &config_write(1); exit(0); } @@ -1170,15 +1195,15 @@ sub message_create { # # Message file for adduser(8) # comment: ``#'' -# defaultvariables: \$name, \$fullname, \$password +# default variables: \$name, \$fullname, \$password # other variables: see /etc/adduser.conf after # line ``$do_not_delete'' # \$fullname, -your account ``\$name'' was created. Your password is ``\$password''. -Please expire your password. Have fun! +your account ``\$name'' was created. +Have fun! See also chpass(1), finger(1), passwd(1) EOF @@ -1347,9 +1372,12 @@ shellpref = ($shpref) # defaultshell if not empty ("bash") defaultshell = "$defaultshell" -# defaultgroup ('USER' for same as username or any other valid group +# defaultgroup ('USER' for same as username or any other valid group) defaultgroup = $defaultgroup +# defaultclass if not empty +defaultclass = "$defaultclass" + # new users get this uid (1000) uid_start = 1000