From d7b388ed7381fc0d969f7eefce7a997661ac73d1 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 5 Nov 2010 23:30:20 -0700 Subject: Add batch support for newuser. --- pinyadmin/sbin/newuser | 151 ++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 63 deletions(-) (limited to 'pinyadmin/sbin/newuser') diff --git a/pinyadmin/sbin/newuser b/pinyadmin/sbin/newuser index e064f06..c61cdec 100755 --- a/pinyadmin/sbin/newuser +++ b/pinyadmin/sbin/newuser @@ -5,21 +5,7 @@ use warnings; use Email::Valid::Loose qw( ); -# If they passed any arguments, complain and exit. -if ( scalar @ARGV ) { - print "You can't pass any arguments to this script!\n"; - exit 2; -}; - -# If they didn't provide a terminal definition, then assume xterm. -# Everybody emulates xterm to at least a basic extent. -if ( not exists $ENV{"TERM"} ) { - $ENV{"TERM"} = "xterm"; - print "I don't know what terminal you're using; guessing xterm...\n"; -}; - -# Disable buffering. -$|++; +my ( $email, $username, $password ); # Configure the strictness of our email checks. my $checker = Email::Valid::Loose->new @@ -30,76 +16,115 @@ my $checker = Email::Valid::Loose->new , "-tldcheck" => 0 ); -my ( $email, $username, $password1, $password2 ); +# Check to see if we're in batch mode. +if ( scalar @ARGV == 3 and $ARGV[0] eq "--batch" ) { + + ( undef, $email, $username ) = @ARGV; -while ( 1 ) { + $email = $checker->address( $email ); - print "Email address to associate with new user: "; - chomp ( $email = ); + chomp( $password = ); - if ( $email eq "" ) { - print "You must provide an email address!\n"; - next; - }; +# Some other incorrect argument arrangement. +} elsif ( scalar @ARGV ) { + print "You can't pass any arguments to this script!\n"; + exit 2; +} else { - $email = $checker->address( $email ); - if ( not defined $email ) { - print "Please, at least pretend to provide a valid email address.\n"; - next; + # If they didn't provide a terminal definition, then assume xterm. + # Everybody emulates xterm to at least a basic extent. + if ( not exists $ENV{"TERM"} ) { + $ENV{"TERM"} = "xterm"; + print "I don't know what terminal you're using; guessing xterm...\n"; }; - last; + # Disable buffering. + $|++; -}; + my ( $password2 ); -while ( 1 ) { + while ( 1 ) { - print "Desired username: "; - chomp ( $username = ); + print "Email address to associate with new user: "; + chomp ( $email = ); - if ( $username eq "" ) { - print "You have to enter a username!\n"; - next; - }; - - if ( $username =~ /^git-|^ikiwiki-/ ) { - print "Your username cannot start with git- or ikiwiki-!\n"; - next - }; + if ( $email eq "" ) { + print "You must provide an email address!\n"; + next; + }; + + $email = $checker->address( $email ); + if ( not defined $email ) { + print "Please, at least pretend to provide a valid email address.\n"; + next; + }; + + last; - if ( $username !~ /^[a-zA-Z0-9_.][a-zA-Z0-9_.-]+$/ ) { - print( "Usernames must consist only of letters, digits, underscores, periods, and dashes, and not start with a dash. Usernames are case sensitive.\n" ); - next }; - last; + while ( 1 ) { -}; + print "Desired username: "; + chomp ( $username = ); -while ( 1 ) { + if ( $username eq "" ) { + print "You have to enter a username!\n"; + next; + }; - system( "stty", "-echo" ); - print "Desired password: "; - chomp ( $password1 = ); - print "\nRetype password: "; - chomp ( $password2 = ); - print "\n"; - system( "stty", "echo" ); + if ( $username =~ /^(git|ikiwiki)-/ ) { + print "Your username cannot start with git- or ikiwiki-!\n"; + next; + }; - if ( $password1 ne $password2 ) { - print "Provided passwords do not match; try again.\n"; - next; - }; + if ( $username !~ /^[a-zA-Z0-9_.][a-zA-Z0-9_.-]+$/ ) { + print "Usernames must consist only of letters, digits, underscores, periods, and dashes, and not start with a dash. Usernames are case sensitive.\n"; + next + }; + + last; - if ( $password1 eq "" ) { - print "You have to enter a password!\n"; - next; }; - last; + while ( 1 ) { + + system( "stty", "-echo" ); + print "Desired password: "; + chomp ( $password = ); + print "\nRetype password: "; + chomp ( $password = ); + print "\n"; + system( "stty", "echo" ); + + if ( $password ne $password2 ) { + print "Provided passwords do not match; try again.\n"; + next; + }; + + if ( $password eq "" ) { + print "You have to enter a password!\n"; + next; + }; + + last; + + }; }; +# All the correctness checks should be repeated here. There are multiple +# pathways to get to this point, but only a single path from here on down. We +# don't need friendly error messages; whatever UI got us to this point *should* +# have already caught these. +exit 3 if ( not defined $email or $email eq "" ); +exit 3 if ( not defined $username eq "" ); +exit 3 if ( $username =~ /^(git|ikiwiki)-/ ); +exit 3 if ( $username !~ /^[a-zA-Z0-9_.][a-zA-Z0-9_.-]+$/ ); +exit 3 if ( not defined $password or $password eq "" ); + +# Here on down is the actual creation code. + my @saltchars = ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' @@ -115,7 +140,7 @@ foreach my $n ( 1 .. 16 ) { $salt .= "\$"; -my $crypt = crypt( $password1, $salt ); +my $crypt = crypt( $password, $salt ); my $ret = system( "/usr/sbin/useradd", "-c", "$email", "-k", "/var/empty", "-g", "users", "-m", "-p", $crypt, "-s", "/usr/bin/pinyshell", $username ); -- cgit v1.2.3