From 3bf739cbe71a4f6ee5e6c65aa91b0ca793744824 Mon Sep 17 00:00:00 2001 From: gashapwn Date: Tue, 29 Dec 2020 03:44:14 +0000 Subject: [PATCH] create_user.pl - refactored create_user.pl so it can be run with STDIN instead of a file --- perl-script/create_user.pl | 48 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/perl-script/create_user.pl b/perl-script/create_user.pl index 32fefa6..509379b 100644 --- a/perl-script/create_user.pl +++ b/perl-script/create_user.pl @@ -19,32 +19,51 @@ my @g; # Given a username... prompts and creates that user sub create($){ - my $id = $_[0]; + my $id; - my $fn1 = $account_dir.$id.".ident"; + my $fn1; my $username; my $shell_pref; my $user_email; my $pub_key; + my $p0; + + # Prompts... + $p0 = [ + "Enter username: ", + "Enter pubkey: " + ]; + + $fn1 = ""; + if($_[0]){ + $id = $_[0]; + $fn1 = $account_dir.$id.".ident"; + open IN0, $fn1 or die "could not open file $fn1"; + $p0 = [ map("", @{$p0}) ]; + }else{ + *IN0 = *STDIN; + } + # read in username and validate - open FILE, $fn1 or die "could not open file $fn1"; - $username = ; + printf($p0->[0]); + $username = ; chomp $username; - + if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){ - printf("%s has an INVALID username\n", $id); + printf("%s is an INVALID username\n", $id); die ("oh no"); } # read in email - $user_email = ; + $user_email = $_[0] ? : ""; chomp $user_email; # read in shell and validate { - my $s0 = ; + my $s0; + $s0 = $_[0] ? : "SHELL_KSH"; chomp $s0; unless($SHELL_ENUM->{$s0}){ die "invalid shell setting $s0 in file $id.ident"; @@ -53,7 +72,8 @@ sub create($){ } # read in pub key - $pub_key = ; + printf($p0->[1]); + $pub_key = ; chomp $pub_key; { @@ -70,10 +90,10 @@ sub create($){ system($cmd); system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys"); system("chmod 711 /home/$username"); - system("mv $fn1 $fn1.done"); + system("test -f $fn1 && mv $fn1 $fn1.done"); system("echo $username >> $ul_path"); } - close FILE; + close IN0; } # MAIN starts here @@ -85,14 +105,15 @@ if(!(`id` =~ /uid=0/)){ # Adjusts the relative file paths based on where # the script runs from -if( `pwd` =~ /perl-script\/?\s*$/){ +if(`pwd` =~ /perl-script\/?\s*$/){ $working_dir = "../"; $account_dir = $working_dir."req/"; $conf_path = $working_dir."lyadmin.conf.json"; $ul_path = $working_dir."user_list.txt"; printf("%s\n", $conf_path); }elsif(!(join(" ", glob("./*")) =~ /perl-script/)){ - die "please run this script with ./perl-script/ as the present working directory"; + create(0); + die "done...\n"; } # Opens the conf file to read @@ -117,4 +138,3 @@ close FILE; for my $fn (@g){ create($fn); } -