scripts and tools to administer the lingy.in public unix / tilde
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

121 lignes
2.6KB

  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 = $_[0];
  17. my $fn1 = $account_dir.$id.".ident";
  18. my $username;
  19. my $shell_pref;
  20. my $user_email;
  21. my $pub_key;
  22. # read in username and validate
  23. open FILE, $fn1 or die "could not open file $fn1";
  24. $username = <FILE>;
  25. chomp $username;
  26. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  27. printf("%s has an INVALID username\n", $id);
  28. die ("oh no");
  29. }
  30. # read in email
  31. $user_email = <FILE>;
  32. chomp $user_email;
  33. # read in shell and validate
  34. {
  35. my $s0 = <FILE>;
  36. chomp $s0;
  37. unless($SHELL_ENUM->{$s0}){
  38. die "invalid shell setting $s0 in file $id.ident";
  39. }
  40. $shell_pref = $SHELL_ENUM->{$s0};
  41. }
  42. # read in pub key
  43. $pub_key = <FILE>;
  44. chomp $pub_key;
  45. {
  46. # Prompt to make sure the username looks OK
  47. my $cmd;
  48. $cmd = "useradd -m -s " . $shell_pref . " " . $username;
  49. printf("Y/N is this command OK?: %s\n", $cmd);
  50. if(!(<STDIN> =~ /^y/i)){
  51. die "invalid characters?!!";
  52. }
  53. # create the user
  54. system($cmd);
  55. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  56. system("chmod 711 /home/$username");
  57. system("mv $fn1 $fn1.done");
  58. system("echo $username >> $ul_path");
  59. }
  60. close FILE;
  61. }
  62. # MAIN starts here
  63. # Checks if user is root
  64. if(!(`id` =~ /uid=0/)){
  65. die "please run this script as root";
  66. }
  67. # Adjusts the relative file paths based on where
  68. # the script runs from
  69. if( `pwd` =~ /perl-script\/?\s*$/){
  70. $working_dir = "../";
  71. $account_dir = $working_dir."req/";
  72. $conf_path = $working_dir."lyadmin.conf.json";
  73. $ul_path = $working_dir."user_list.txt";
  74. printf("%s\n", $conf_path);
  75. }elsif(!(join(" ", glob("./*")) =~ /perl-script/)){
  76. die "please run this script with ./perl-script/ as the present working directory";
  77. }
  78. # Opens the conf file to read
  79. # shell enums
  80. open FILE, $conf_path or die "could not open file $conf_path";
  81. {
  82. my $conf_str;
  83. my $conf_obj;
  84. local $/=undef;
  85. $conf_str = <FILE>;
  86. chomp $conf_str;
  87. $conf_obj = decode_json($conf_str);
  88. $SHELL_ENUM = $conf_obj->{"shell"};
  89. };
  90. close FILE;
  91. # Saves all new user requests to an array
  92. @g = glob("$account_dir*");
  93. @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
  94. # Iterate and create the users
  95. for my $fn (@g){
  96. create($fn);
  97. }