From 518339026cbef414a757136d6ef862097accd2c3 Mon Sep 17 00:00:00 2001 From: gashapwn Date: Mon, 30 Nov 2020 05:05:17 +0000 Subject: [PATCH] create-user.pl - added comments --- perl-script/create-user.pl | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/perl-script/create-user.pl b/perl-script/create-user.pl index 5196ba1..32fefa6 100644 --- a/perl-script/create-user.pl +++ b/perl-script/create-user.pl @@ -4,6 +4,9 @@ use warnings; use strict; use JSON; +# create-user.pl +# +# processes new user request files my $working_dir = "./"; my $account_dir = $working_dir."req/"; @@ -14,6 +17,7 @@ my $SHELL_ENUM; my @g; +# Given a username... prompts and creates that user sub create($){ my $id = $_[0]; @@ -24,13 +28,21 @@ sub create($){ my $user_email; my $pub_key; + # read in username and validate open FILE, $fn1 or die "could not open file $fn1"; $username = ; chomp $username; + + if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){ + printf("%s has an INVALID username\n", $id); + die ("oh no"); + } + # read in email $user_email = ; chomp $user_email; - + + # read in shell and validate { my $s0 = ; chomp $s0; @@ -40,15 +52,12 @@ sub create($){ $shell_pref = $SHELL_ENUM->{$s0}; } + # read in pub key $pub_key = ; chomp $pub_key; - if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){ - printf("%s has an INVALID username\n", $id); - die ("oh no"); - } - { + # Prompt to make sure the username looks OK my $cmd; $cmd = "useradd -m -s " . $shell_pref . " " . $username; printf("Y/N is this command OK?: %s\n", $cmd); @@ -56,21 +65,26 @@ sub create($){ if(!( =~ /^y/i)){ die "invalid characters?!!"; } - + + # create the user system($cmd); system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys"); system("chmod 711 /home/$username"); system("mv $fn1 $fn1.done"); system("echo $username >> $ul_path"); - # system("echo $username >> user_list.txt"); } close FILE; } +# MAIN starts here + +# Checks if user is root if(!(`id` =~ /uid=0/)){ die "please run this script as root"; } +# Adjusts the relative file paths based on where +# the script runs from if( `pwd` =~ /perl-script\/?\s*$/){ $working_dir = "../"; $account_dir = $working_dir."req/"; @@ -81,6 +95,8 @@ if( `pwd` =~ /perl-script\/?\s*$/){ die "please run this script with ./perl-script/ as the present working directory"; } +# Opens the conf file to read +# shell enums open FILE, $conf_path or die "could not open file $conf_path"; { my $conf_str; @@ -93,9 +109,11 @@ open FILE, $conf_path or die "could not open file $conf_path"; }; close FILE; +# Saves all new user requests to an array @g = glob("$account_dir*"); @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g; +# Iterate and create the users for my $fn (@g){ create($fn); }