Guest User

https://hashbang.sh

a guest
May 29th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.02 KB | None | 0 0
  1. #!/bin/sh
  2. # This script first and foremost attempts to be POSIX compliant.
  3. # Secondly, it attempts to be compatible with as many shell implementations as
  4. # possible to provide an easy gateway for new users.
  5.  
  6. # If we're using bash, we do this
  7. if [ "x$BASH" != "x" ]; then
  8.     shopt -s extglob
  9.     set -o posix
  10.     # Bail out if any curl's fail
  11.     set -o pipefail
  12. fi
  13.  
  14. bail() {
  15.     echo " "
  16.     echo " If you think this is a bug, please report it to ";
  17.     echo " -> https://github.com/hashbang/hashbang.sh/issues/";
  18.     echo " ";
  19.     echo " The installer will not continue from here...";
  20.     echo " ";
  21.     exit 1
  22. }
  23.  
  24. # check if can write to file
  25. checkutil() {
  26.     echo -n " * Checking for $1..."
  27.     if which "$1"; then
  28.         printf "ok!\n";
  29.         return 0;
  30.     else
  31.         printf "not found!\n";
  32.         return 1;
  33.     fi
  34. }
  35.  
  36. # This function can be called with two parameters:
  37. #
  38. # First is obligatory, and is the "question posed".
  39. # For instance, one may ask "is pizza your favorite meal?", to which the
  40. # responder may answer Y (yes) or N (no).
  41. #
  42. # Second parameter is optional, and can be either Y or N.
  43. # The reasoning behind this is to have a default answer to the question,
  44. # resulting in the responder being able to simple press [enter] and skip
  45. # pressing Y or N, giving the default answer instead.
  46. ask() {
  47.     while true; do
  48.         prompt=""
  49.         default=""
  50.  
  51.         if [ "${2}" = "Y" ]; then
  52.             prompt="Y/n"
  53.             default=Y
  54.         elif [ "${2}" = "N" ]; then
  55.             prompt="y/N"
  56.             default=N
  57.         else
  58.             prompt="y/n"
  59.             default=
  60.         fi
  61.  
  62.         # Ask the question
  63.         echo " "
  64.         printf "%s [%s] " "$1" "$prompt"
  65.         read REPLY
  66.  
  67.         # Default?
  68.         if [ -z "$REPLY" ]; then
  69.             REPLY=$default
  70.         fi
  71.  
  72.         # Check if the reply is valid
  73.         case "$REPLY" in
  74.             Y*|y*) return 0 ;;
  75.             N*|n*) return 1 ;;
  76.         esac
  77.  
  78.     done
  79.     echo " "
  80. }
  81.  
  82. # generate ssh kypair
  83. makekey() {
  84.     ( checkutil ssh-keygen && checkutil ssh ) || exit 1
  85.     if [ ! -e "$1" ]; then
  86.         ssh-keygen -t rsa -C "#! $username" -f "$1"
  87.         if [ ! $? ]; then
  88.             echo " Unable to make key with that location"
  89.         else
  90.             chmod 600 "$1"
  91.             echo " Successfully generated key"
  92.             break
  93.         fi
  94.     else
  95.         if ask " Unable to generate key, do you want to delete the file?" N; then
  96.             rm -f "$1"
  97.             if [ ! $? ]; then
  98.                 echo " Unable to delete file, resetting"
  99.             else
  100.                 echo " File deleted"
  101.                 ssh-keygen -t rsa -C "#! $username" -f "$1"
  102.                 if [ ! $? ]; then
  103.                     echo " Unable to generate key, resetting"
  104.                 fi
  105.             fi
  106.         else
  107.             echo " Unable to make key with that path, resetting"
  108.         fi
  109.     fi
  110. }
  111.  
  112. # Fetch host data for later.
  113. # If this fails there is no point in proceeding
  114. host_data=$(mktemp)
  115. curl -sH 'Accept:text/plain' https://hashbang.sh/server/stats > $host_data
  116. echo >> $host_data
  117.  
  118. clear;
  119. echo "   _  _    __ ";
  120. echo " _| || |_ |  |  Welcome to #!. This network has three rules:";
  121. echo "|_  __  _||  | ";
  122. echo " _| || |_ |  |  1. When people need help, teach. Don't do it for them";
  123. echo "|_  __  _||__|  2. Don't use our resources for closed source projects";
  124. echo "  |_||_|  (__)  3. Be excellent to each other";
  125. echo " ";
  126. echo " We are a diverse community of people who love teaching and learning.";
  127. echo " Putting a #! at the beginning of a \"script\" style program tells a ";
  128. echo " computer that it needs to \"do something\" or \"execute\" this file.";
  129. echo " Likewise, we are a community of people that like to \"do stuff\".";
  130. echo " ";
  131. echo " If you like technology, and you want to learn to write your first";
  132. echo " program, learn to use Linux, or even take on interesting challenges";
  133. echo " with some of the best in the industry, you are in the right place.";
  134. echo " ";
  135. echo " The following will set you up with a \"shell\" account on one of our";
  136. echo " shared systems. From here you can run IRC chat clients to talk to us,";
  137. echo " access to personal file storage and web hosting, and a wide range of";
  138. echo " development tools. ";
  139. echo " ";
  140. echo " Everything should work perfectly, unless it doesn't";
  141. echo " ";
  142. echo " Please report any issues here: ";
  143. echo "   -> https://github.com/hashbang/hashbang.sh/issues/";
  144. echo " ";
  145. printf " If you agree with the above and wish to continue, hit [Enter] ";
  146. read _
  147. clear
  148.  
  149. echo " ";
  150. echo " ";
  151. printf -- ' %72s\n' | tr ' ' -;
  152. echo " ";
  153.  
  154. echo " First, your system must be properly configured with the required";
  155. echo " utilities and executables.";
  156. echo " We will perform a short check for those now.";
  157. echo " NOTE: If you see this message, it is likely because something is";
  158. echo " not installed. Check the list below, and install any";
  159. echo " missing applications.";
  160.  
  161. checkutil expr || exit 1
  162. checkutil gpg || exit 1
  163. ( checkutil ssh-keygen && checkutil ssh ) || exit 1
  164. checkutil curl || exit 1
  165. clear;
  166.  
  167. echo " ";
  168. echo " ";
  169. printf -- ' %72s\n' | tr ' ' -;
  170. echo " ";
  171.  
  172.  
  173. echo " To create your account we first need a username.";
  174. echo " ";
  175. echo " A valid username must:";
  176. echo "  * be between between 1-31 characters long";
  177. echo "  * consist of only 0-9 and a-z (lowercase only)";
  178. echo "  * begin with a letter";
  179. echo " ";
  180. echo " Traditional unix usernames are first initial, optional middle initial,";
  181. echo " and the first 6 characters of the last name, but feel free to use ";
  182. echo " whatever you want";
  183. echo " ";
  184.  
  185. while [ "x$username" = "x" ]; do
  186.     printf " Username: ";
  187.     read input;
  188.     if echo "$input" | grep -E "^[a-z][a-z0-9]{0,30}$" >/dev/null; then
  189.         username="$input"
  190.     else
  191.         echo " ";
  192.         echo " \"$input\" is not a valid username."
  193.         echo " Please read the instructions and try again"
  194.         echo " ";
  195.     fi
  196. done
  197.  
  198. echo " ";
  199. printf -- ' %72s\n' | tr ' ' -;
  200. echo " ";
  201. echo " Now we will need an SSH Public Key."
  202. echo " ";
  203. echo " SSH Keys are a type of public/private key system that let you identify";
  204. echo " yourself to systems like this one without ever sending your password ";
  205. echo " over the internet, and thus by nature we won't even know what it is";
  206.  
  207. for keytype in id_rsa id_dsa id_ecdsa id_ed25519; do
  208.     if [ -e ~/.ssh/${keytype}.pub ] && [ -e ~/.ssh/${keytype} ]; then
  209.         if ask " We found a public key in [ ~/.ssh/${keytype}.pub ]. Use this key?" Y; then
  210.             private_keyfile="~/.ssh/${keytype}"
  211.             public_key="$(cat ~/.ssh/${keytype}.pub)"
  212.             break
  213.         fi
  214.     fi
  215. done
  216.  
  217. if [ "x$public_key" = "x" ]; then
  218.     echo " No SSH key for login to server found, attempting to generate one"
  219.     while true; do
  220.         echo " "
  221.         echo -n " Path to new or existing connection key (~/.ssh/id_rsa): ";
  222.         read private_keyfile
  223.         if [ "x$private_keyfile" = "x" ]; then
  224.             private_keyfile="$HOME/.ssh/id_rsa"
  225.         fi
  226.         private_keyfile=$(echo "$private_keyfile" | sed "s@~@$HOME@")
  227.         echo " "
  228.         if [ ! -e "$private_keyfile" ] && [ ! -e "$private_keyfile.pub" ]; then
  229.             if ask " Do you want us to generate a key for you?" Y; then
  230.                 if [ -e "$private_keyfile" ]; then
  231.                     if ask " File exists: $private_keyfile - delete?" Y; then
  232.                         rm "$private_keyfile"
  233.                         if [ -e "${private_keyfile}.pub" ]; then
  234.                             rm "${private_keyfile}.pub"
  235.                         fi
  236.                         makekey "$private_keyfile"
  237.                     fi
  238.                 else
  239.                     makekey "$private_keyfile"
  240.                 fi
  241.                 public_key=$(cat "${privat_keyfile}.pub")
  242.             fi
  243.         elif [ ! -e "$private_keyfile" ] && [ -e "${private_keyfile}.pub" ]; then
  244.             if ask " Found public keyfile, missing private. Do you wish to continue?" N; then
  245.                 echo " Using public key ${private_keyfile}.pub"
  246.                 break
  247.             else
  248.                 echo " Resetting"
  249.             fi
  250.         elif [ ! -e "${private_keyfile}.pub" ]; then
  251.             echo " Unable to find public key ${private_keyfile}.pub"
  252.         else
  253.             echo " Using public key ${private_keyfile}.pub"
  254.             break
  255.         fi
  256.     done
  257.     public_key=$(cat "${private_keyfile}.pub")
  258. fi
  259.  
  260. n=0
  261. echo
  262. printf -- ' %72s\n' | tr ' ' -;
  263. echo
  264. echo " Please choose a server to create your account on."
  265. echo
  266. printf -- ' %72s\n' | tr ' ' -;
  267. printf "  %-1s | %-4s | %-36s | %-8s | %-8s\n" \
  268.     "#" "Host" "Location" "Users" "Latency"
  269. printf -- ' %72s\n' | tr ' ' -;
  270. while IFS="|" read host ip location current_users max_users; do
  271.     host=$(echo $host | sed 's/\([a-z0-9]\+\)\..*/\1/g')
  272.     latency=$(ping -c1 ${host}.hashbang.sh | head -n2 | tail -n1 | sed 's/.*=//g')
  273.     n=$((n+1))
  274.     printf "  %-1s | %-4s | %-36s | %8s | %-8s\n" \
  275.         "$n" \
  276.         "$host" \
  277.         "$location" \
  278.         "$current_users/$max_users" \
  279.         "$latency"
  280. done < $host_data
  281. printf -- ' %72s\n' | tr ' ' -;
  282.  
  283. echo
  284. while true; do
  285.     echo -n " Enter Number 1-$n : "
  286.     read choice
  287.     number=$(echo "$choice" | awk '$0 ~/[^0-9]/ { print "no" }')
  288.     if [ "$number" != "no" ] && \
  289.        [ "$choice" -ge 1 ] && \
  290.        [ "$choice" -le $n ]; then
  291.         break;
  292.     fi
  293. done
  294. host=$(cat $host_data | head -n $choice | tail -n1 | cut -d '|' -f1)
  295.  
  296. if [ "x$public_key" != "x" -a "x$username" != "x" ]; then
  297.     echo " ";
  298.     printf -- ' %72s\n' | tr ' ' -;
  299.     echo " ";
  300.     echo " We are going to create an account with the following information";
  301.     echo " ";
  302.     echo " Username: $username";
  303.     echo " Public Key: ${private_keyfile}.pub";
  304.     echo " Host: $host";
  305.     echo " ";
  306.     if ask " Does this look correct?" Y ; then
  307.  
  308.         echo " ";
  309.         echo -n " Creating your account... ";
  310.         headers=$(mktemp)
  311.         format="{\"user\":\"$username\",\"key\":\"$public_key\",\"host\":\"$host\"}"
  312.         output="$(curl -H 'Content-Type: application/json' -d '$format' https://hashbang.sh/user/create -D $headers )"
  313.         status="$(cat $headers | awk '{print \$1}' | head -n 1)"
  314.         if [ "$status" -eq 200 ]; then
  315.             echo " Account Created!"
  316.         else
  317.             echo " Account creation failed: $(echo $output | sed -e 's/.*\"message\": \?\"\([^"]\+\)\".*/\1/')";
  318.             bail
  319.         fi
  320.  
  321.         if ask " Would you like to add trusted/signed keys for our servers to your .ssh/known_hosts?" Y ; then
  322.             echo " Downloading GPG keys"
  323.             echo " "
  324.             gpg --recv-keys 0xD2C4C74D8FAA96F5
  325.             echo " "
  326.             echo " Downloading key list"
  327.             echo " "
  328.             data="$(curl -s https://hashbang.sh/static/known_hosts.asc)"
  329.             printf %s "$data" | gpg --verify
  330.             if [ ! $? -eq 0 ]; then
  331.                 echo " "
  332.                 echo " Unable to verify keys"
  333.                 bail
  334.             fi
  335.             printf %s "$data" | grep "hashbang.sh" >> ~/.ssh/known_hosts
  336.             echo " "
  337.             echo " Key scanned and saved"
  338.         fi
  339.  
  340.         if ask " Would you like an alias (shortcut) added to your .ssh/config?" Y ; then
  341.             printf "\nHost hashbang\n  HostName ${host}\n  IdentitiesOnly yes\n  User %s\n  IdentityFile %s\n" \
  342.                             "$username" "$private_keyfile" \
  343.             >> ~/.ssh/config
  344.             echo " You can now connect any time by entering the command:";
  345.             echo " ";
  346.             echo " > ssh hashbang";
  347.         else
  348.             echo " You can now connect any time by entering the command:";
  349.             echo " ";
  350.             echo " > ssh ${username}@${host}.hashbang.sh";
  351.         fi
  352.  
  353.     else
  354.         echo " Please re-run script to make corrections.";
  355.         bail
  356.     fi
  357.  
  358.     if ask " Do you want us to log you in now?" Y; then
  359.         if [ -e $private_keyfile ]; then
  360.             ssh ${username}@${host}.hashbang.sh -i "$private_keyfile"
  361.         else
  362.             ssh ${username}@${host}.hashbang.sh
  363.         fi
  364.     fi
  365. fi
  366.  
  367. # exit [n]. if [n] is not specified, then exit shall use the return code of the
  368. # last command.
  369. exit 0
Add Comment
Please, Sign In to add comment