diff --git a/perl-script/create_user.pl b/perl-script/create_user.pl index 6374319..aed401f 100644 --- a/perl-script/create_user.pl +++ b/perl-script/create_user.pl @@ -39,7 +39,7 @@ sub create($){ $fn1 = ""; if($_[0]){ $id = $_[0]; - $fn1 = $account_dir.$id.".ident"; + $fn1 = $account_dir.$id.".ident"; open IN0, $fn1 or die "could not open file $fn1"; $p0 = [ map("", @{$p0}) ]; }else{ diff --git a/perl-script/provision.pl b/perl-script/provision.pl index 106ddba..747c909 100644 --- a/perl-script/provision.pl +++ b/perl-script/provision.pl @@ -23,6 +23,55 @@ my $pwuid; my $admin_un; my $admin_home_dir; +# Given a username... prompts and creates that user +sub create(){ + my $id; + + my $username; + my $user_email; + my $pub_key; + + my $p0; + + # Prompts... + $p0 = [ + "Enter username: ", + "Enter pubkey: " + ]; + + # read in username and validate + printf($p0->[0]); + $username = ; + chomp $username; + + if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){ + printf("%s is an INVALID username\n", $id); + die ("oh no"); + } + + # read in pub key + printf($p0->[1]); + $pub_key = ; + chomp $pub_key; + + { + # Prompt to make sure the username looks OK + my $cmd; + $cmd = "useradd -m " . $username; + printf("Y/N is this command OK?: %s\n", $cmd); + + if(!( =~ /^y/i)){ + die "provision cancelled..."; + } + + # create the user + system($cmd); + system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys"); + system("chmod 711 /home/$username"); + } +} + + # Make sure we're running as root $pwuid = getpwuid( $< ); @@ -31,13 +80,18 @@ if($pwuid ne "root"){ } unless( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){ - system("pkg_add p5-JSON"); + printf("to provision the instance there must be a non root user with an authorized_keys file"); + printf("creating user...\n"); + create(); + + # TODO: Getting rid of this part... + # system("pkg_add p5-JSON"); - `wget --quiet https://git.lain.church/gashapwn/lyadmin/raw/branch/gasha-branch/perl-script/create_user.pl -O create_admin.pl`; + # `wget --quiet https://git.lain.church/gashapwn/lyadmin/raw/branch/gasha-branch/perl-script/create_user.pl -O create_admin.pl`; - printf("to provision the instance there must be a non root user with an authorized_keys file"); - printf("run create_admin.pl to create an admin user\n"); - die "or add with useradd and add an ssh key to ~/.ssh/authorized_keys\n"; + # printf("to provision the instance there must be a non root user with an authorized_keys file"); + # printf("run create_admin.pl to create an admin user\n"); + # die "or add with useradd and add an ssh key to ~/.ssh/authorized_keys\n"; } # install git