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.

91 lines
1.7KB

  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 $SHELL_ENUM;
  9. open FILE, $CONF_PATH or die "could not open file $CONF_PATH";
  10. {
  11. my $conf_str;
  12. my $conf_obj;
  13. local $/=undef;
  14. $conf_str = <FILE>;
  15. chomp $conf_str;
  16. $conf_obj = decode_json($conf_str);
  17. $SHELL_ENUM = $conf_obj->{"shell"};
  18. };
  19. close FILE;
  20. my @g;
  21. sub create($){
  22. my $id = $_[0];
  23. my $fn1 = $account_dir.$id.".ident";
  24. my $username;
  25. my $shell_pref;
  26. my $user_email;
  27. my $pub_key;
  28. open FILE, $fn1 or die "could not open file $fn1";
  29. $username = <FILE>;
  30. chomp $username;
  31. $user_email = <FILE>;
  32. chomp $user_email;
  33. {
  34. my $s0 = <FILE>;
  35. chomp $s0;
  36. unless($SHELL_ENUM->{$s0}){
  37. die "invalid shell setting $s0 in file $id.ident";
  38. }
  39. $shell_pref = $SHELL_ENUM->{$s0};
  40. }
  41. $pub_key = <FILE>;
  42. chomp $pub_key;
  43. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  44. printf("%s has an INVALID username\n", $id);
  45. die ("oh no");
  46. }
  47. {
  48. my $cmd;
  49. $cmd = "useradd -m -s " . $shell_pref . " " . $username;
  50. printf("Y/N is this command OK?: %s\n", $cmd);
  51. if(!(<STDIN> =~ /^y/i)){
  52. die "invalid characters?!!";
  53. }
  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 >> user_list.txt");
  59. }
  60. close FILE;
  61. }
  62. if(!(`id` =~ /uid=0/)){
  63. die "please run this script as root";
  64. }
  65. @g = glob("$account_dir*");
  66. @g = map { s/.*\/([^\/]*).ident$/$1/; $_ } grep {$_ =~ /ident$/} @g;
  67. for my $fn (@g){
  68. create($fn);
  69. }