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.

212 lines
6.0KB

  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 $DEV_FLAG = shift || "";
  13. my $GIT_REPO = 'https://git.lain.church/gashapwn/lyadmin.git';
  14. my $GIT_BRANCH = length($DEV_FLAG) > 0 ? "-b gasha-branch " : "";
  15. my ($REPO_DIR) = $GIT_REPO =~ /\/([^\/]*)\.git$/;
  16. my $INST_DIR = "/tilde";
  17. my $SVC_ACCT = "_lingyind";
  18. my $pwuid;
  19. my $admin_un;
  20. my $admin_home_dir;
  21. # Given a username... prompts and creates that user
  22. sub create(){
  23. my $id;
  24. my $username;
  25. my $user_email;
  26. my $pub_key;
  27. my $p0;
  28. # Prompts...
  29. $p0 = [
  30. "Enter username: ",
  31. "Enter pubkey: "
  32. ];
  33. # read in username and validate
  34. printf($p0->[0]);
  35. $username = <STDIN>;
  36. chomp $username;
  37. if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
  38. printf("%s is an INVALID username\n", $id);
  39. die ("oh no");
  40. }
  41. # read in pub key
  42. printf($p0->[1]);
  43. $pub_key = <STDIN>;
  44. chomp $pub_key;
  45. {
  46. # Prompt to make sure the username looks OK
  47. my $cmd;
  48. $cmd = "useradd -m " . $username;
  49. printf("Y/N is this command OK?: %s\n", $cmd);
  50. if(!(<STDIN> =~ /^y/i)){
  51. die "provision cancelled...";
  52. }
  53. # create the user
  54. system($cmd);
  55. system("echo '".$pub_key."' > /home/$username/.ssh/authorized_keys");
  56. system("chmod 711 /home/$username");
  57. }
  58. }
  59. # Make sure we're running as root
  60. $pwuid = getpwuid( $< );
  61. if($pwuid ne "root"){
  62. die "script must be run as root";
  63. }
  64. # Make sure script is provisioning a fresh instance
  65. # and doesn't clobber users existing configs
  66. printf("This script is meant to be run on a fresh install\n");
  67. printf("Y/N OK to proceed?");
  68. if(!(<STDIN> =~ /^y/i)){
  69. die "provision cancelled...";
  70. }
  71. unless( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  72. printf("to provision the instance there must be a non root user with an authorized_keys file");
  73. printf("creating user...\n");
  74. create();
  75. }
  76. # install git
  77. system("pkg_add git");
  78. # Setup install dir
  79. system("mkdir $INST_DIR");
  80. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  81. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  82. chdir $INST_DIR;
  83. # clone repo
  84. system("su $SVC_ACCT -c 'git clone $GIT_BRANCH$GIT_REPO'");
  85. chdir $REPO_DIR;
  86. # Copy the skel directory
  87. system("mkdir ./skel/public_html/cgi");
  88. system("cp -r ./skel/* /etc/skel/");
  89. # Check /etc/passwd for the username created during
  90. # installation
  91. if( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  92. # grant doas access to admin user
  93. system("echo 'permit nopass $admin_un' > /etc/doas.conf");
  94. # setup admin user
  95. system("cp -r ./skel/* /home/$admin_un/");
  96. system("chown -R $admin_un:$admin_un /home/$admin_un");
  97. system("echo $admin_un >> ./user_list.txt");
  98. }
  99. # Setup the virtual environment
  100. system("pkg_add python3 openssl rust bash");
  101. printf("generating virtual enviornment...\n");
  102. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  103. printf("running pip. can take up to 3 minutes due to slow compilation.\n");
  104. system("su $SVC_ACCT -c '. ./venv/bin/activate && python3 -m pip install --upgrade pip'");
  105. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  106. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  107. system("chmod 755 /etc/rc.d/lingyind");
  108. system("rcctl enable lingyind");
  109. system("rcctl start lingyind");
  110. system("pkg_add p5-JSON");
  111. # Install apache
  112. system("pkg_add apache-httpd");
  113. printf("configuring apache\n");
  114. # enable the userdir module
  115. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  116. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  117. # Enable the CGI directory
  118. system("echo '<Directory \"/home/*/public_html/cgi/\">
  119. Require all granted
  120. Options +ExecCGI
  121. AddHandler cgi-script .cgi
  122. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  123. # Enable the CGI modules
  124. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  125. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  126. # Disable directory listing
  127. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  128. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  129. # Change the port to 5001
  130. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  131. # rev up those apache processes!
  132. system("rcctl enable apache2");
  133. system("rcctl start apache2");
  134. # Install and config haproxy
  135. system("pkg_add haproxy");
  136. printf("configuring haproxy\n");
  137. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  138. system("rcctl enable haproxy");
  139. system("rcctl start haproxy");
  140. # Install and configure ngircd and delegation
  141. system("pkg_add ngircd");
  142. # irc group is used for granting permissions
  143. # to irc admins
  144. system("groupadd irc");
  145. system("usermod -G irc _ngircd");
  146. # allow doas for irc admins
  147. system("echo 'permit nopass :irc as _ngircd' >> /etc/doas.conf");
  148. # Copy over our conf file to /etc
  149. # and set permissions
  150. system("chmod 750 /etc/ngircd");
  151. system("cp ./perl-script/conf/ngircd.conf /etc/ngircd/ngircd.conf");
  152. system("chmod -R 660 /etc/ngircd/*");
  153. system("chown -R _ngircd:irc /etc/ngircd/");
  154. # copy over our admin script and set permissions
  155. system("cp ./perl-script/ngircd-ctl /usr/local/sbin/ngircd-ctl");
  156. system("chown _ngircd:irc /usr/local/sbin/ngircd-ctl");
  157. system("chmod 770 /usr/local/sbin/ngircd-ctl");
  158. # Disable root login
  159. system("sed -i -e 's/^[^#]*PermitRootLogin.*\$/PermitRootLogin no/' /etc/ssh/sshd_config");
  160. system("sed -i -e 's/^PasswordAuthentication.*$//' /etc/ssh/sshd_config");
  161. system("echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config");
  162. system("rcctl restart sshd");
  163. printf("\n\nInstall complete\n");
  164. printf("==================================================\n");
  165. printf("Protip: use doas instead of sudo\n");
  166. printf("root login and password login is now disabled, so dont forget\nto set a password\n");
  167. printf("and test your pub key\n");