scripts and tools to administer the lingy.in public unix / tilde
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.1KB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use JSON;
  5. # create-user.pl
  6. #
  7. # processes new user request files
  8. my $working_dir = "./";
  9. my $account_dir = $working_dir."req/";
  10. my $conf_path = $working_dir."lyadmin.conf.json";
  11. my $ul_path = $working_dir."user_list.txt";
  12. my $SHELL_ENUM;
  13. my @g;
  14. # Given a username... prompts and creates that user
  15. sub create($){
  16. my $id;
  17. my $fn1;
  18. my $username;
  19. my $shell_pref;
  20. my $user_email;
  21. my $pub_key;
  22. my $p0;
  23. # Prompts...
  24. $p0 = [
  25. "Enter username: ",
  26. "Enter pubkey: "
  27. ];
  28. $fn1 = "";
  29. if($_[0]){
  30. $id = $_[0];
  31. $fn1 = $account_dir.$id.".ident";
  32. open IN0, $fn1 or die "could not open file $fn1";
  33. $p0 = [ map("", @{$p0}) ];
  34. }else{
  35. *IN0 = *STDIN;
  36. }
  37. # read in username and validate
  38. printf($p0->[0]);
  39. $username = <IN0>;
  40. chomp $username;
  41. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  42. printf("%s is an INVALID username\n", $id);
  43. die ("oh no");
  44. }
  45. # read in email
  46. $user_email = $_[0] ? <IN0> : "";
  47. chomp $user_email;
  48. # read in shell and validate
  49. {
  50. my $s0;
  51. $s0 = $_[0] ? <IN0> : "SHELL_KSH";
  52. chomp $s0;
  53. unless($SHELL_ENUM->{$s0}){
  54. die "invalid shell setting $s0";
  55. }
  56. $shell_pref = $SHELL_ENUM->{$s0};
  57. }
  58. # read in pub key
  59. printf($p0->[1]);
  60. $pub_key = <IN0>;
  61. chomp $pub_key;
  62. {
  63. # Prompt to make sure the username looks OK
  64. my $cmd;
  65. $cmd = "useradd -m -s " . $shell_pref . " " . $username;
  66. printf("Y/N is this command OK?: %s\n", $cmd);
  67. if(!(<STDIN> =~ /^y/i)){
  68. die "invalid characters?!!";
  69. }
  70. # create the user
  71. system($cmd);
  72. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  73. system("chmod 711 /home/$username");
  74. system("test $fn1 && mv $fn1 $fn1.done");
  75. system("echo $username >> $ul_path");
  76. }
  77. close IN0;
  78. }
  79. # MAIN starts here
  80. # Checks if user is root
  81. if(!(`id` =~ /uid=0/)){
  82. die "please run this script as root";
  83. }
  84. # Adjusts the relative file paths based on where
  85. # the script runs from
  86. if(`pwd` =~ /perl-script\/?\s*$/){
  87. $working_dir = "../";
  88. $account_dir = $working_dir."req/";
  89. $conf_path = $working_dir."lyadmin.conf.json";
  90. $ul_path = $working_dir."user_list.txt";
  91. printf("%s\n", $conf_path);
  92. }elsif(!(join(" ", glob("./*")) =~ /perl-script/)){
  93. $SHELL_ENUM = {"SHELL_KSH" => "/bin/ksh"};
  94. create(0);
  95. printf("admin user is now configured\n");
  96. printf("run the below command to continue the install\n");
  97. printf("pkg_add wget && wget 'https://git.lain.church/gashapwn/lyadmin/raw/branch/master/perl-script/provision.pl' -O - | perl");
  98. die "\n\n";
  99. }
  100. # Opens the conf file to read
  101. # shell enums
  102. open FILE, $conf_path or die "could not open file $conf_path";
  103. {
  104. my $conf_str;
  105. my $conf_obj;
  106. local $/=undef;
  107. $conf_str = <FILE>;
  108. chomp $conf_str;
  109. $conf_obj = decode_json($conf_str);
  110. $SHELL_ENUM = $conf_obj->{"shell"};
  111. };
  112. close FILE;
  113. # Saves all new user requests to an array
  114. @g = glob("$account_dir*");
  115. @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
  116. # Iterate and create the users
  117. for my $fn (@g){
  118. create($fn);
  119. }