瀏覽代碼

create-user.pl - added comments

tags/release-00
gashapwn 3 年之前
父節點
當前提交
518339026c
共有 1 個檔案被更改,包括 26 行新增8 行删除
  1. +26
    -8
      perl-script/create-user.pl

+ 26
- 8
perl-script/create-user.pl 查看文件

@@ -4,6 +4,9 @@ use warnings;
use strict; use strict;
use JSON; use JSON;


# create-user.pl
#
# processes new user request files


my $working_dir = "./"; my $working_dir = "./";
my $account_dir = $working_dir."req/"; my $account_dir = $working_dir."req/";
@@ -14,6 +17,7 @@ my $SHELL_ENUM;


my @g; my @g;


# Given a username... prompts and creates that user
sub create($){ sub create($){
my $id = $_[0]; my $id = $_[0];
@@ -24,13 +28,21 @@ sub create($){
my $user_email; my $user_email;
my $pub_key; my $pub_key;


# read in username and validate
open FILE, $fn1 or die "could not open file $fn1"; open FILE, $fn1 or die "could not open file $fn1";
$username = <FILE>; $username = <FILE>;
chomp $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 = <FILE>; $user_email = <FILE>;
chomp $user_email; chomp $user_email;

# read in shell and validate
{ {
my $s0 = <FILE>; my $s0 = <FILE>;
chomp $s0; chomp $s0;
@@ -40,15 +52,12 @@ sub create($){
$shell_pref = $SHELL_ENUM->{$s0}; $shell_pref = $SHELL_ENUM->{$s0};
} }


# read in pub key
$pub_key = <FILE>; $pub_key = <FILE>;
chomp $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; my $cmd;
$cmd = "useradd -m -s " . $shell_pref . " " . $username; $cmd = "useradd -m -s " . $shell_pref . " " . $username;
printf("Y/N is this command OK?: %s\n", $cmd); printf("Y/N is this command OK?: %s\n", $cmd);
@@ -56,21 +65,26 @@ sub create($){
if(!(<STDIN> =~ /^y/i)){ if(!(<STDIN> =~ /^y/i)){
die "invalid characters?!!"; die "invalid characters?!!";
} }

# create the user
system($cmd); system($cmd);
system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys"); system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
system("chmod 711 /home/$username"); system("chmod 711 /home/$username");
system("mv $fn1 $fn1.done"); system("mv $fn1 $fn1.done");
system("echo $username >> $ul_path"); system("echo $username >> $ul_path");
# system("echo $username >> user_list.txt");
} }
close FILE; close FILE;
} }


# MAIN starts here

# Checks if user is root
if(!(`id` =~ /uid=0/)){ if(!(`id` =~ /uid=0/)){
die "please run this script as root"; die "please run this script as root";
} }


# Adjusts the relative file paths based on where
# the script runs from
if( `pwd` =~ /perl-script\/?\s*$/){ if( `pwd` =~ /perl-script\/?\s*$/){
$working_dir = "../"; $working_dir = "../";
$account_dir = $working_dir."req/"; $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"; 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"; open FILE, $conf_path or die "could not open file $conf_path";
{ {
my $conf_str; my $conf_str;
@@ -93,9 +109,11 @@ open FILE, $conf_path or die "could not open file $conf_path";
}; };
close FILE; close FILE;


# Saves all new user requests to an array
@g = glob("$account_dir*"); @g = glob("$account_dir*");
@g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g; @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;


# Iterate and create the users
for my $fn (@g){ for my $fn (@g){
create($fn); create($fn);
} }


Loading…
取消
儲存