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.

87 lines
1.7KB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use JSON;
  5. my $WORKING_DIR = "/home/gashapwn/lyadmin/";
  6. my $ACCOUNT_DIR = "req/";
  7. my $FULL_PATH = "$WORKING_DIR$ACCOUNT_DIR";
  8. my $CONF_PATH = $WORKING_DIR."lyadmin.conf.json";
  9. my $SHELL_ENUM;
  10. open FILE, $CONF_PATH or die "could not open file $CONF_PATH";
  11. {
  12. my $conf_str;
  13. my $conf_obj;
  14. local $/=undef;
  15. $conf_str = <FILE>;
  16. chomp $conf_str;
  17. $conf_obj = decode_json($conf_str);
  18. $SHELL_ENUM = $conf_obj->{"shell"};
  19. };
  20. close FILE;
  21. my @g;
  22. sub create($){
  23. my $id = $_[0];
  24. my $fn1 = $FULL_PATH.$id.".ident";
  25. my $username;
  26. my $shell_pref;
  27. my $user_email;
  28. my $pub_key;
  29. open FILE, $fn1 or die "could not open file $fn1";
  30. $username = <FILE>;
  31. chomp $username;
  32. $user_email = <FILE>;
  33. chomp $user_email;
  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. $pub_key = <FILE>;
  43. chomp $pub_key;
  44. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  45. printf("%s has an INVALID username\n", $id);
  46. die ("oh no");
  47. }
  48. {
  49. my $cmd;
  50. $cmd = "useradd -m -s " . $shell_pref . " " . $username;
  51. printf("Y/N is this command OK?: %s\n", $cmd);
  52. if(!(<STDIN> =~ /^y/i)){
  53. die "invalid characters?!!";
  54. }
  55. system($cmd);
  56. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  57. system("chmod 711 /home/$username");
  58. system("mv $fn1 $fn1.done");
  59. system("echo $username >> user_list.txt");
  60. }
  61. close FILE;
  62. }
  63. @g = glob("$FULL_PATH*");
  64. @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
  65. for my $fn (@g){
  66. create($fn);
  67. }