2020-11-24 14:51:43 -05:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2020-11-26 19:32:04 -05:00
|
|
|
use JSON;
|
2020-11-24 14:51:43 -05:00
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# create-user.pl
|
|
|
|
#
|
|
|
|
# processes new user request files
|
2020-11-24 14:51:43 -05:00
|
|
|
|
2020-11-29 18:26:34 -05:00
|
|
|
my $working_dir = "./";
|
|
|
|
my $account_dir = $working_dir."req/";
|
|
|
|
|
2020-11-29 18:37:31 -05:00
|
|
|
my $conf_path = $working_dir."lyadmin.conf.json";
|
2020-11-29 21:36:46 -05:00
|
|
|
my $ul_path = $working_dir."user_list.txt";
|
2020-11-26 19:32:04 -05:00
|
|
|
my $SHELL_ENUM;
|
2020-11-24 14:51:43 -05:00
|
|
|
|
|
|
|
my @g;
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# Given a username... prompts and creates that user
|
2020-11-24 23:05:19 -05:00
|
|
|
sub create($){
|
2020-12-28 22:44:14 -05:00
|
|
|
my $id;
|
2020-11-24 22:33:03 -05:00
|
|
|
|
2020-12-28 22:44:14 -05:00
|
|
|
my $fn1;
|
2020-11-24 22:33:03 -05:00
|
|
|
|
|
|
|
my $username;
|
|
|
|
my $shell_pref;
|
|
|
|
my $user_email;
|
2020-11-26 17:31:34 -05:00
|
|
|
my $pub_key;
|
2020-11-24 22:33:03 -05:00
|
|
|
|
2020-12-28 22:44:14 -05:00
|
|
|
my $p0;
|
|
|
|
|
|
|
|
# Prompts...
|
|
|
|
$p0 = [
|
|
|
|
"Enter username: ",
|
|
|
|
"Enter pubkey: "
|
|
|
|
];
|
|
|
|
|
|
|
|
$fn1 = "";
|
|
|
|
if($_[0]){
|
|
|
|
$id = $_[0];
|
2021-03-02 10:35:53 -05:00
|
|
|
$fn1 = $account_dir.$id.".ident";
|
2020-12-28 22:44:14 -05:00
|
|
|
open IN0, $fn1 or die "could not open file $fn1";
|
|
|
|
$p0 = [ map("", @{$p0}) ];
|
|
|
|
}else{
|
|
|
|
*IN0 = *STDIN;
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# read in username and validate
|
2020-12-28 22:44:14 -05:00
|
|
|
printf($p0->[0]);
|
|
|
|
$username = <IN0>;
|
2020-11-24 22:33:03 -05:00
|
|
|
chomp $username;
|
2020-12-28 22:44:14 -05:00
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
|
2020-12-28 22:44:14 -05:00
|
|
|
printf("%s is an INVALID username\n", $id);
|
2020-11-30 00:05:17 -05:00
|
|
|
die ("oh no");
|
|
|
|
}
|
2020-11-26 19:32:04 -05:00
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# read in email
|
2020-12-28 22:44:14 -05:00
|
|
|
$user_email = $_[0] ? <IN0> : "";
|
2020-11-24 22:33:03 -05:00
|
|
|
chomp $user_email;
|
2020-11-30 00:05:17 -05:00
|
|
|
|
|
|
|
# read in shell and validate
|
2020-11-24 22:33:03 -05:00
|
|
|
{
|
2020-12-28 22:44:14 -05:00
|
|
|
my $s0;
|
|
|
|
$s0 = $_[0] ? <IN0> : "SHELL_KSH";
|
2020-11-26 17:31:34 -05:00
|
|
|
chomp $s0;
|
2020-11-26 19:32:04 -05:00
|
|
|
unless($SHELL_ENUM->{$s0}){
|
2020-12-28 23:11:50 -05:00
|
|
|
die "invalid shell setting $s0";
|
2020-11-26 19:32:04 -05:00
|
|
|
}
|
2020-11-26 17:31:34 -05:00
|
|
|
$shell_pref = $SHELL_ENUM->{$s0};
|
2020-11-24 22:33:03 -05:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# read in pub key
|
2020-12-28 22:44:14 -05:00
|
|
|
printf($p0->[1]);
|
|
|
|
$pub_key = <IN0>;
|
2020-11-26 17:31:34 -05:00
|
|
|
chomp $pub_key;
|
|
|
|
|
2020-11-24 22:33:03 -05:00
|
|
|
{
|
2020-11-30 00:05:17 -05:00
|
|
|
# Prompt to make sure the username looks OK
|
2020-11-24 22:33:03 -05:00
|
|
|
my $cmd;
|
2020-11-24 23:00:11 -05:00
|
|
|
$cmd = "useradd -m -s " . $shell_pref . " " . $username;
|
|
|
|
printf("Y/N is this command OK?: %s\n", $cmd);
|
|
|
|
|
2020-11-26 17:31:34 -05:00
|
|
|
if(!(<STDIN> =~ /^y/i)){
|
2020-11-24 23:00:11 -05:00
|
|
|
die "invalid characters?!!";
|
|
|
|
}
|
2020-11-30 00:05:17 -05:00
|
|
|
|
|
|
|
# create the user
|
2020-11-24 23:00:11 -05:00
|
|
|
system($cmd);
|
2020-11-27 00:49:15 -05:00
|
|
|
system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
|
2020-11-26 01:14:59 -05:00
|
|
|
system("chmod 711 /home/$username");
|
2020-12-28 23:16:03 -05:00
|
|
|
system("test $fn1 && mv $fn1 $fn1.done");
|
2020-11-29 21:36:46 -05:00
|
|
|
system("echo $username >> $ul_path");
|
2020-11-24 22:33:03 -05:00
|
|
|
}
|
2020-12-28 22:44:14 -05:00
|
|
|
close IN0;
|
2020-11-24 22:33:03 -05:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# MAIN starts here
|
|
|
|
|
|
|
|
# Checks if user is root
|
2020-11-29 18:26:34 -05:00
|
|
|
if(!(`id` =~ /uid=0/)){
|
|
|
|
die "please run this script as root";
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# Adjusts the relative file paths based on where
|
|
|
|
# the script runs from
|
2020-12-28 22:44:14 -05:00
|
|
|
if(`pwd` =~ /perl-script\/?\s*$/){
|
2020-11-29 18:37:31 -05:00
|
|
|
$working_dir = "../";
|
|
|
|
$account_dir = $working_dir."req/";
|
|
|
|
$conf_path = $working_dir."lyadmin.conf.json";
|
2020-11-29 21:36:46 -05:00
|
|
|
$ul_path = $working_dir."user_list.txt";
|
2020-11-29 18:37:31 -05:00
|
|
|
printf("%s\n", $conf_path);
|
|
|
|
}elsif(!(join(" ", glob("./*")) =~ /perl-script/)){
|
2020-12-28 23:11:50 -05:00
|
|
|
$SHELL_ENUM = {"SHELL_KSH" => "/bin/ksh"};
|
2020-12-28 22:44:14 -05:00
|
|
|
create(0);
|
2020-12-28 23:58:33 -05:00
|
|
|
printf("admin user is now configured\n");
|
|
|
|
printf("run the below command to continue the install\n");
|
2020-12-28 23:51:33 -05:00
|
|
|
printf("pkg_add wget && wget 'https://git.lain.church/gashapwn/lyadmin/raw/branch/master/perl-script/provision.pl' -O - | perl");
|
2020-12-28 23:58:33 -05:00
|
|
|
die "\n\n";
|
2020-11-29 18:37:31 -05:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# Opens the conf file to read
|
|
|
|
# shell enums
|
2020-11-29 18:37:31 -05:00
|
|
|
open FILE, $conf_path or die "could not open file $conf_path";
|
|
|
|
{
|
|
|
|
my $conf_str;
|
|
|
|
my $conf_obj;
|
|
|
|
local $/=undef;
|
|
|
|
$conf_str = <FILE>;
|
|
|
|
chomp $conf_str;
|
|
|
|
$conf_obj = decode_json($conf_str);
|
|
|
|
$SHELL_ENUM = $conf_obj->{"shell"};
|
|
|
|
};
|
|
|
|
close FILE;
|
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# Saves all new user requests to an array
|
2020-11-29 18:26:34 -05:00
|
|
|
@g = glob("$account_dir*");
|
2020-11-26 17:31:34 -05:00
|
|
|
@g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
|
2020-11-24 14:51:43 -05:00
|
|
|
|
2020-11-30 00:05:17 -05:00
|
|
|
# Iterate and create the users
|
2020-11-24 14:51:43 -05:00
|
|
|
for my $fn (@g){
|
2020-11-24 23:05:19 -05:00
|
|
|
create($fn);
|
2020-11-24 14:51:43 -05:00
|
|
|
}
|