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.

180 lines
4.9KB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. # provision.pl
  5. # script to provision a tilde instance
  6. #
  7. # This script is intended to be run on a fresh
  8. # OpenBSD install
  9. #
  10. # gashapwn
  11. # Nov 2020
  12. my $GIT_REPO = 'https://git.lain.church/gashapwn/lyadmin.git';
  13. my ($REPO_DIR) = $GIT_REPO =~ /\/([^\/]*)\.git$/;
  14. my $INST_DIR = "/tilde";
  15. my $SVC_ACCT = "_lingyind";
  16. my $pwuid;
  17. my $admin_un;
  18. my $admin_home_dir;
  19. # Given a username... prompts and creates that user
  20. sub create(){
  21. my $id;
  22. my $username;
  23. my $user_email;
  24. my $pub_key;
  25. my $p0;
  26. # Prompts...
  27. $p0 = [
  28. "Enter username: ",
  29. "Enter pubkey: "
  30. ];
  31. # read in username and validate
  32. printf($p0->[0]);
  33. $username = <STDIN>;
  34. chomp $username;
  35. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  36. printf("%s is an INVALID username\n", $id);
  37. die ("oh no");
  38. }
  39. # read in pub key
  40. printf($p0->[1]);
  41. $pub_key = <STDIN>;
  42. chomp $pub_key;
  43. {
  44. # Prompt to make sure the username looks OK
  45. my $cmd;
  46. $cmd = "useradd -m " . $username;
  47. printf("Y/N is this command OK?: %s\n", $cmd);
  48. if(!(<STDIN> =~ /^y/i)){
  49. die "provision cancelled...";
  50. }
  51. # create the user
  52. system($cmd);
  53. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  54. system("chmod 711 /home/$username");
  55. }
  56. }
  57. # Make sure we're running as root
  58. $pwuid = getpwuid( $< );
  59. if($pwuid ne "root"){
  60. die "script must be run as root";
  61. }
  62. unless( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  63. printf("to provision the instance there must be a non root user with an authorized_keys file");
  64. printf("creating user...\n");
  65. create();
  66. # TODO: Getting rid of this part...
  67. # system("pkg_add p5-JSON");
  68. # `wget --quiet https://git.lain.church/gashapwn/lyadmin/raw/branch/gasha-branch/perl-script/create_user.pl -O create_admin.pl`;
  69. # printf("to provision the instance there must be a non root user with an authorized_keys file");
  70. # printf("run create_admin.pl to create an admin user\n");
  71. # die "or add with useradd and add an ssh key to ~/.ssh/authorized_keys\n";
  72. }
  73. # install git
  74. system("pkg_add git");
  75. # Setup install dir
  76. system("mkdir $INST_DIR");
  77. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  78. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  79. chdir $INST_DIR;
  80. # clone repo
  81. system("su $SVC_ACCT -c 'git clone $GIT_REPO'");
  82. chdir $REPO_DIR;
  83. # Copy the skel directory
  84. system("mkdir ./skel/public_html/cgi");
  85. system("cp -r ./skel/* /etc/skel/");
  86. # Check /etc/passwd for the username created during
  87. # installation
  88. if( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  89. # grant doas access to admin user
  90. system("echo 'permit $admin_un' > /etc/doas.conf");
  91. # setup admin user
  92. system("cp -r ./skel/* /home/$admin_un/");
  93. system("chown -R $admin_un:$admin_un /home/$admin_un");
  94. system("echo $admin_un >> ./user_list.txt");
  95. }
  96. # Setup the virtual environment
  97. system("pkg_add python3");
  98. printf("generating virtual enviornment...\n");
  99. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  100. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  101. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  102. system("chmod 755 /etc/rc.d/lingyind");
  103. system("rcctl enable lingyind");
  104. system("rcctl start lingyind");
  105. system("pkg_add p5-JSON");
  106. # Install apache
  107. system("pkg_add apache-httpd");
  108. printf("configuring apache\n");
  109. # enable the userdir module
  110. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  111. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  112. # Enable the CGI directory
  113. system("echo '<Directory \"/home/*/public_html/cgi/\">
  114. Require all granted
  115. Options +ExecCGI
  116. AddHandler cgi-script .cgi
  117. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  118. # Enable the CGI modules
  119. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  120. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  121. # Disable directory listing
  122. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  123. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  124. # Change the port to 5001
  125. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  126. # rev up those apache processes!
  127. system("rcctl enable apache2");
  128. system("rcctl start apache2");
  129. # Install and config haproxy
  130. system("pkg_add haproxy");
  131. printf("configuring haproxy\n");
  132. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  133. system("rcctl enable haproxy");
  134. system("rcctl start haproxy");
  135. printf("\n\nInstall complete\n");
  136. printf("==================================================\n");
  137. printf("Protip: use doas instead of sudoo\n");
  138. printf("dont forget\n\ncreate yourself an user with: useradd -m\n");
  139. printf("setup your ssh pub key at ~/.ssh/authorized_keys\n");