scripts and tools to administer the lingy.in public unix / tilde
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

103 рядки
2.1KB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use JSON;
  5. my $working_dir = "./";
  6. my $account_dir = $working_dir."req/";
  7. my $conf_path = $working_dir."lyadmin.conf.json";
  8. my $ul_path = $working_dir."user_list.txt";
  9. my $SHELL_ENUM;
  10. my @g;
  11. sub create($){
  12. my $id = $_[0];
  13. my $fn1 = $account_dir.$id.".ident";
  14. my $username;
  15. my $shell_pref;
  16. my $user_email;
  17. my $pub_key;
  18. open FILE, $fn1 or die "could not open file $fn1";
  19. $username = <FILE>;
  20. chomp $username;
  21. $user_email = <FILE>;
  22. chomp $user_email;
  23. {
  24. my $s0 = <FILE>;
  25. chomp $s0;
  26. unless($SHELL_ENUM->{$s0}){
  27. die "invalid shell setting $s0 in file $id.ident";
  28. }
  29. $shell_pref = $SHELL_ENUM->{$s0};
  30. }
  31. $pub_key = <FILE>;
  32. chomp $pub_key;
  33. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  34. printf("%s has an INVALID username\n", $id);
  35. die ("oh no");
  36. }
  37. {
  38. my $cmd;
  39. $cmd = "useradd -m -s " . $shell_pref . " " . $username;
  40. printf("Y/N is this command OK?: %s\n", $cmd);
  41. if(!(<STDIN> =~ /^y/i)){
  42. die "invalid characters?!!";
  43. }
  44. system($cmd);
  45. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  46. system("chmod 711 /home/$username");
  47. system("mv $fn1 $fn1.done");
  48. system("echo $username >> $ul_path");
  49. # system("echo $username >> user_list.txt");
  50. }
  51. close FILE;
  52. }
  53. if(!(`id` =~ /uid=0/)){
  54. die "please run this script as root";
  55. }
  56. if( `pwd` =~ /perl-script\/?\s*$/){
  57. $working_dir = "../";
  58. $account_dir = $working_dir."req/";
  59. $conf_path = $working_dir."lyadmin.conf.json";
  60. $ul_path = $working_dir."user_list.txt";
  61. printf("%s\n", $conf_path);
  62. }elsif(!(join(" ", glob("./*")) =~ /perl-script/)){
  63. die "please run this script with ./perl-script/ as the present working directory";
  64. }
  65. open FILE, $conf_path or die "could not open file $conf_path";
  66. {
  67. my $conf_str;
  68. my $conf_obj;
  69. local $/=undef;
  70. $conf_str = <FILE>;
  71. chomp $conf_str;
  72. $conf_obj = decode_json($conf_str);
  73. $SHELL_ENUM = $conf_obj->{"shell"};
  74. };
  75. close FILE;
  76. @g = glob("$account_dir*");
  77. @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
  78. for my $fn (@g){
  79. create($fn);
  80. }