lyadmin/perl-script/create-user.pl

74 lines
1.4 KiB
Perl
Raw Normal View History

2020-11-24 14:51:43 -05:00
#!/usr/bin/perl
use warnings;
use strict;
my $WORKING_DIR = "/home/gashapwn/lyadmin/";
my $ACCOUNT_DIR = "test/";
my $FULL_PATH = "$WORKING_DIR$ACCOUNT_DIR";
2020-11-24 22:33:03 -05:00
my $SHELL_ENUM = {
"SHELL_BASH" => "/usr/local/bin/bash",
2020-11-24 22:33:03 -05:00
"SHELL_KSH" => "/bin/ksh"
};
2020-11-24 14:51:43 -05:00
my @g;
2020-11-24 23:05:19 -05:00
sub create($){
2020-11-24 22:33:03 -05:00
my $id = $_[0];
my $fn1 = $FULL_PATH.$id.".ident";
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-11-26 17:31:34 -05:00
open FILE, $fn1 or die "could not open file $fn1";
2020-11-24 22:33:03 -05:00
$username = <FILE>;
chomp $username;
$user_email = <FILE>;
chomp $user_email;
{
2020-11-26 17:31:34 -05:00
my $s0 = <FILE>;
chomp $s0;
$shell_pref = $SHELL_ENUM->{$s0};
2020-11-24 22:33:03 -05:00
}
2020-11-26 17:31:34 -05:00
$pub_key = <FILE>;
chomp $pub_key;
2020-11-24 22:33:03 -05:00
if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
printf("%s has an INVALID username\n", $id);
die ("oh no");
}
{
my $cmd;
$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)){
die "invalid characters?!!";
}
system($cmd);
2020-11-26 17:31:34 -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-11-26 17:36:27 -05:00
system("mv $fn1 $fn1.done");
2020-11-26 01:53:20 -05:00
system("echo $username >> user_list.txt");
2020-11-24 22:33:03 -05:00
}
close FILE;
}
2020-11-24 14:51:43 -05:00
@g = glob("$FULL_PATH*");
2020-11-26 17:31:34 -05:00
@g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
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
}