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.

187 lines
5.2KB

  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. # Make sure script is provisioning a fresh instance
  63. # and doesn't clobber users existing configs
  64. printf("This script is meant to be run on a fresh install\n");
  65. printf("Y/N OK to proceed?");
  66. if(!(<STDIN> =~ /^y/i)){
  67. die "provision cancelled...";
  68. }
  69. unless( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  70. printf("to provision the instance there must be a non root user with an authorized_keys file");
  71. printf("creating user...\n");
  72. create();
  73. }
  74. # install git
  75. system("pkg_add git");
  76. # Setup install dir
  77. system("mkdir $INST_DIR");
  78. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  79. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  80. chdir $INST_DIR;
  81. # clone repo
  82. system("su $SVC_ACCT -c 'git clone $GIT_REPO'");
  83. chdir $REPO_DIR;
  84. # Copy the skel directory
  85. system("mkdir ./skel/public_html/cgi");
  86. system("cp -r ./skel/* /etc/skel/");
  87. # Check /etc/passwd for the username created during
  88. # installation
  89. if( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  90. # grant doas access to admin user
  91. system("echo 'permit nopass $admin_un' > /etc/doas.conf");
  92. # setup admin user
  93. system("cp -r ./skel/* /home/$admin_un/");
  94. system("chown -R $admin_un:$admin_un /home/$admin_un");
  95. system("echo $admin_un >> ./user_list.txt");
  96. }
  97. # Setup the virtual environment
  98. system("pkg_add python3 openssl rust");
  99. printf("generating virtual enviornment...\n");
  100. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  101. printf("running pip. can take up to 3 minutes due to slow compilation.\n");
  102. system("su $SVC_ACCT -c '. ./venv/bin/activate && python3 -m pip install --upgrade pip'");
  103. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  104. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  105. system("chmod 755 /etc/rc.d/lingyind");
  106. system("rcctl enable lingyind");
  107. system("rcctl start lingyind");
  108. system("pkg_add p5-JSON");
  109. # Install apache
  110. system("pkg_add apache-httpd");
  111. printf("configuring apache\n");
  112. # enable the userdir module
  113. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  114. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  115. # Enable the CGI directory
  116. system("echo '<Directory \"/home/*/public_html/cgi/\">
  117. Require all granted
  118. Options +ExecCGI
  119. AddHandler cgi-script .cgi
  120. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  121. # Enable the CGI modules
  122. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  123. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  124. # Disable directory listing
  125. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  126. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  127. # Change the port to 5001
  128. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  129. # rev up those apache processes!
  130. system("rcctl enable apache2");
  131. system("rcctl start apache2");
  132. # Install and config haproxy
  133. system("pkg_add haproxy");
  134. printf("configuring haproxy\n");
  135. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  136. system("rcctl enable haproxy");
  137. system("rcctl start haproxy");
  138. # Disable root login
  139. system("sed -i -e 's/^[^#]*PermitRootLogin.*\$/PermitRootLogin no/' /etc/ssh/sshd_config");
  140. system("sed -i -e 's/^PasswordAuthentication.*$//' /etc/ssh/sshd_config");
  141. system("echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config");
  142. system("rcctl restart sshd");
  143. printf("\n\nInstall complete\n");
  144. printf("==================================================\n");
  145. printf("Protip: use doas instead of sudo\n");
  146. printf("root login and password login is now disabled, so dont forget\nto set a password\n");
  147. printf("and test your pub key\n");