reviewed to ensure maximal coverage of forensics traces
[bootcd.git] / initscripts / pl_boot
1 #!/bin/bash
2
3 ### make sure this output shows up in the console
4
5 exec 2>&1
6 exec > /dev/console
7
8 ###
9 . /etc/init.d/pl_functions
10
11 . /tmp/planet.cnf
12
13 # Run gpg once to create default options
14 GNUPGHOME=/root
15 export GNUPGHOME
16 /usr/bin/gpg --yes 2>/dev/null </dev/null
17
18 # if this file is present, cancel the boot (exit this script)
19 CANCEL_BOOT_FLAG=/tmp/CANCEL_BOOT
20
21 # how many times to fail in attempting to contact primary server
22 # before falling back to original. if the backup fails this many times
23 # too, then the process is repeated started with the primary server
24 ATTEMPTS_BEFORE_BACKUP=3
25
26 # where all the configuration files for contacting
27 # the boot server are stored
28 BOOT_DIR=/usr/boot/
29
30 # get the server we are going to be contacting
31 BOOT_SERVER=$(cat $BOOT_DIR/boot_server)
32 BOOT_SERVER_PORT=$(cat $BOOT_DIR/boot_server_port)
33
34 # the file to request from the boot server
35 BOOT_SERVER_PATH=$(cat $BOOT_DIR/boot_server_path)
36
37 # location of the cacert for this boot server
38 BOOT_SERVER_CACERT=$BOOT_DIR/cacert.pem
39
40 # location of the gpg key ring to verify scripts
41 BOOT_SERVER_GPG_KEYRING=$BOOT_DIR/pubring.gpg
42
43 # get the backup server we are going to be contacting
44 BACKUP_BOOT_SERVER=$(cat $BOOT_DIR/backup/boot_server)
45 BACKUP_BOOT_SERVER_PORT=$(cat $BOOT_DIR/backup/boot_server_port)
46
47 # the file to request from the backup boot server
48 BACKUP_BOOT_SERVER_PATH=$(cat $BOOT_DIR/backup/boot_server_path)
49
50 # location of the cacert for the backup boot server
51 BACKUP_BOOT_SERVER_CACERT=$BOOT_DIR/backup/cacert.pem
52
53 # location of the gpg key ring for backup server to verify scripts
54 BACKUP_BOOT_SERVER_GPG_KEYRING=$BOOT_DIR/backup/pubring.gpg
55
56 # location of a file containing this boot cd version
57 BOOT_VERSION_FILE=/pl_version
58
59 # the locations of the downloaded scripts
60 UNVERIFIED_SCRIPT=/tmp/bootscript.gpg
61 VERIFIED_SCRIPT=/tmp/bootscript
62
63
64 # --------------------------
65
66
67 # now, contact the boot server, run the script, and do it over again.
68 contact_count=0
69
70 # set to one when we are trying to contact backup server
71 on_backup_server=0
72
73 # start out contacting the primary servers
74 CONNECT_BOOT_SERVER=$BOOT_SERVER
75 CONNECT_BOOT_SERVER_PORT=$BOOT_SERVER_PORT
76 CONNECT_BOOT_SERVER_PATH=$BOOT_SERVER_PATH
77 CONNECT_BOOT_SERVER_GPG_KEYRING=$BOOT_SERVER_GPG_KEYRING
78 CONNECT_BOOT_SERVER_CACERT=$BOOT_SERVER_CACERT
79
80 while true; do
81
82     if [[ -f $CANCEL_BOOT_FLAG ]]; then
83         verbose-message "pl_boot: got request to cancel boot, exiting"
84         exit 0
85     fi
86     
87     if [[ $contact_count -ge $ATTEMPTS_BEFORE_BACKUP ]]; then
88
89         contact_count=0
90
91         if [[ $on_backup_server == 1 ]]; then
92             verbose-message "pl_boot: failed to contact backup server, trying primary $BOOT_SERVER"
93
94             on_backup_server=0
95
96             CONNECT_BOOT_SERVER=$BOOT_SERVER
97             CONNECT_BOOT_SERVER_PORT=$BOOT_SERVER_PORT
98             CONNECT_BOOT_SERVER_PATH=$BOOT_SERVER_PATH
99             CONNECT_BOOT_SERVER_GPG_KEYRING=$BOOT_SERVER_GPG_KEYRING
100             CONNECT_BOOT_SERVER_CACERT=$BOOT_SERVER_CACERT
101         else
102             verbose-message "pl_boot: failed to contact primary server, trying backup $BACKUP_BOOT_SERVER"
103
104             on_backup_server=1
105
106             CONNECT_BOOT_SERVER=$BACKUP_BOOT_SERVER
107             CONNECT_BOOT_SERVER_PORT=$BACKUP_BOOT_SERVER_PORT
108             CONNECT_BOOT_SERVER_PATH=$BACKUP_BOOT_SERVER_PATH
109             CONNECT_BOOT_SERVER_GPG_KEYRING=$BACKUP_BOOT_SERVER_GPG_KEYRING
110             CONNECT_BOOT_SERVER_CACERT=$BACKUP_BOOT_SERVER_CACERT
111         fi
112     fi
113
114     if [[ $contact_count != 0 ]]; then
115
116         verbose-message "pl_boot: next attempt in 30s, to fetch script from server at $CONNECT_BOOT_SERVER"
117         /bin/sleep 30
118     fi
119
120     # June 2015 : using --tlsv1 instead of sslv3 that should be available
121     # on all myplc from f14 inclusive
122     # assemble the curl transaction
123     CURL_CMD="/usr/bin/curl \
124         --connect-timeout 60 \
125         --max-time 600 \
126         --form version=<$BOOT_VERSION_FILE \
127         --form cmdline=</proc/cmdline \
128         --form uptime=</proc/uptime \
129         --form ifconfig=</tmp/ifconfig \
130         --form nonce=</tmp/nonce \
131         --location \
132         --output $UNVERIFIED_SCRIPT \
133         --tlsv1  \
134         --silent \
135         --show-error \
136         --fail \
137         --stderr /tmp/curl_errors \
138         --cacert $CONNECT_BOOT_SERVER_CACERT \
139    https://$CONNECT_BOOT_SERVER:$CONNECT_BOOT_SERVER_PORT/$CONNECT_BOOT_SERVER_PATH"
140
141     # assemble the gpg command line
142     GPG_CMD="/usr/bin/gpg \
143         --no-default-keyring \
144         --keyring $CONNECT_BOOT_SERVER_GPG_KEYRING \
145         --output $VERIFIED_SCRIPT \
146         --always-trust \
147         --decrypt $UNVERIFIED_SCRIPT"
148
149     verbose-message "pl_boot: generating new nonce"
150     /usr/bin/head --bytes=32 /dev/urandom | \
151     /usr/bin/od -tx1 -An --width=32 | \
152     /bin/sed 's/ //g' > /tmp/nonce
153
154     verbose-message "pl_boot: fetching script from boot server $CONNECT_BOOT_SERVER"
155     ((contact_count++))
156     rm -f $UNVERIFIED_SCRIPT
157     $CURL_CMD
158     curl_err=$?
159     if [ $curl_err -ne 0 ]; then
160         verbose-message "pl_boot: curl request failed with error $curl_err:"
161         cat /tmp/curl_errors
162         echo
163         if [ -n "$DISCONNECTED_OPERATION" ]; then
164             mkdir /tmp/boot-media
165             mount -U "$DISCONNECTED_OPERATION" /tmp/boot-media
166             cp /tmp/boot-media/bootscript.gpg $UNVERIFIED_SCRIPT
167             umount /tmp/boot-media
168             rmdir /tmp/boot-media
169         else
170             case $curl_err in
171                 6)
172                     verbose-message "This error likely indicates a networking configuration error. "
173                     verbose-message "Please, check whether you can ping this machine.  If you can, "
174                     verbose-message "we recommend checking your DNS settings.  If you cannot, then "
175                     verbose-message "please double check your network settings registered at PLC and "
176                     verbose-message "stored on this Boot Image."
177                 ;;
178                 60)
179                     verbose-message "This error likely indicates that the hardware clock is likely not set "
180                     verbose-message "to GMT.  The result is that authentication between the local and "
181                     verbose-message "remote site fails.  Please double check this machine's system "
182                     verbose-message "clock, and set it to GMT in the BIOS. If after rebooting the same "
183                     verbose-message "error occurs, please report the situation to support@planet-lab.org "
184                     verbose-message "with as much detail as possible."
185                 ;;
186                 *)
187                     verbose-message "The best-practice approach for handling this error is not yet "
188                     verbose-message "documented.  Please report this error to support@planet-lab.org "
189                     verbose-message "with as much detail as possible."
190                 ;;
191             esac
192             # in any case display as much info as we can (see pl_functions)
193             verbose-forensics "after curl"
194             # retry
195             continue
196         fi
197     elif [ -n "$DISCONNECTED_OPERATION" ]; then
198         mkdir /tmp/boot-media
199         mount -U "$DISCONNECTED_OPERATION" /tmp/boot-media
200         cp $UNVERIFIED_SCRIPT /tmp/boot-media
201         umount /tmp/boot-media
202         rmdir /tmp/boot-media
203     fi 
204
205     verbose-message "pl_boot: verifying downloaded script"
206     rm -f $VERIFIED_SCRIPT
207     $GPG_CMD 2> /tmp/gpg_errors
208     if [ $? -ne 0 ]; then
209         verbose-message "pl_boot: failed to verify file:"
210         cat /tmp/gpg_errors
211         echo
212         continue
213     fi
214     verbose-message "pl_boot: decrypted and verified script succesfully"
215
216     verbose-message "pl_boot: handing control to download script"
217     rm -f $UNVERIFIED_SCRIPT
218     chmod +x $VERIFIED_SCRIPT
219     $VERIFIED_SCRIPT
220     
221     verbose-message "pl_boot: downloaded script has returned"
222 done
223
224 verbose-message "pl_boot: automatic boot process canceled by user"