- update build process to support multiple boot cd configurations
[bootcd.git] / conf_files / pl_boot
1 #!/bin/sh
2
3 # Run gpg once to create default options
4 GNUPGHOME=/root
5 export GNUPGHOME
6 /usr/bin/gpg --yes 2>/dev/null </dev/null
7
8 # if this file is present, cancel the boot (exit this script)
9 CANCEL_BOOT_FLAG=/tmp/CANCEL_BOOT
10
11 # how many times to fail in attempting to contact primary server
12 # before falling back to original. if the backup fails this many times
13 # too, then the process is repeated started with the primary server
14 ATTEMPTS_BEFORE_BACKUP=2
15
16 # where all the configuration files for contacting
17 # the boot server are stored
18 BOOT_DIR=/usr/boot/
19
20 # get the server we are going to be contacting
21 BOOT_SERVER=`cat $BOOT_DIR/boot_server`
22 BOOT_SERVER_PORT=`cat $BOOT_DIR/boot_server_port`
23
24 # the file to request from the boot server
25 BOOT_SERVER_PATH=`cat $BOOT_DIR/boot_server_path`
26
27 # location of the cacert for this boot server
28 BOOT_SERVER_CACERT=$BOOT_DIR/cacert.pem
29
30 # location of the gpg key ring to verify scripts
31 BOOT_SERVER_GPG_KEYRING=$BOOT_DIR/pubring.gpg
32
33 # get the backup server we are going to be contacting
34 BACKUP_BOOT_SERVER=`cat $BOOT_DIR/backup/boot_server`
35 BACKUP_BOOT_SERVER_PORT=`cat $BOOT_DIR/backup/boot_server_port`
36
37 # the file to request from the backup boot server
38 BACKUP_BOOT_SERVER_PATH=`cat $BOOT_DIR/backup/boot_server_path`
39
40 # location of the cacert for the backup boot server
41 BACKUP_BOOT_SERVER_CACERT=$BOOT_DIR/backup/cacert.pem
42
43 # location of the gpg key ring for backup server to verify scripts
44 BACKUP_BOOT_SERVER_GPG_KEYRING=$BOOT_DIR/backup/pubring.gpg
45
46 # location of a file containing this boot cd version
47 BOOT_VERSION_FILE=/pl_version
48
49 # the locations of the downloaded scripts
50 UNVERIFIED_SCRIPT=/tmp/bootscript.gpg
51 VERIFIED_SCRIPT=/tmp/bootscript
52
53
54 # --------------------------
55
56
57 # now, contact the boot server, run the script, and do it over again.
58 contact_count=0
59
60 # set to one when we are trying to contact backup server
61 on_backup_server=0
62
63 # start out contacting the primary servers
64 CONNECT_BOOT_SERVER=$BOOT_SERVER
65 CONNECT_BOOT_SERVER_PORT=$BOOT_SERVER_PORT
66 CONNECT_BOOT_SERVER_PATH=$BOOT_SERVER_PATH
67 CONNECT_BOOT_SERVER_GPG_KEYRING=$BOOT_SERVER_GPG_KEYRING
68 CONNECT_BOOT_SERVER_CACERT=$BOOT_SERVER_CACERT
69
70 while : ; do
71
72     if [[ -f $CANCEL_BOOT_FLAG ]]; then
73         echo "pl_boot: got request to cancel boot, exiting"
74         exit 0
75     fi
76
77     if [[ $contact_count -ne 0 ]]; then
78         echo "pl_boot: fetching new script in 30 seconds"
79         /bin/sleep 30
80     fi
81     
82     ((contact_count++))
83     if [[ $contact_count > $ATTEMPTS_BEFORE_BACKUP ]]; then
84
85         contact_count=0
86
87         if [[ $on_backup_server == 1 ]]; then
88             echo "pl_boot: failed to contact backup server, trying primary."
89
90             on_backup_server=0
91
92             CONNECT_BOOT_SERVER=$BOOT_SERVER
93             CONNECT_BOOT_SERVER_PORT=$BOOT_SERVER_PORT
94             CONNECT_BOOT_SERVER_PATH=$BOOT_SERVER_PATH
95             CONNECT_BOOT_SERVER_GPG_KEYRING=$BOOT_SERVER_GPG_KEYRING
96             CONNECT_BOOT_SERVER_CACERT=$BOOT_SERVER_CACERT
97         else
98             echo "pl_boot: failed to contact primary server, trying backup."
99
100             on_backup_server=1
101
102             CONNECT_BOOT_SERVER=$BACKUP_BOOT_SERVER
103             CONNECT_BOOT_SERVER_PORT=$BACKUP_BOOT_SERVER_PORT
104             CONNECT_BOOT_SERVER_PATH=$BACKUP_BOOT_SERVER_PATH
105             CONNECT_BOOT_SERVER_GPG_KEYRING=$BACKUP_BOOT_SERVER_GPG_KEYRING
106             CONNECT_BOOT_SERVER_CACERT=$BACKUP_BOOT_SERVER_CACERT
107         fi
108     fi
109
110     # assemble the curl transaction
111     CURL_CMD="/usr/bin/curl \
112         --connect-timeout 60 \
113         --max-time 600 \
114         --form version=<$BOOT_VERSION_FILE \
115         --form cmdline=</proc/cmdline \
116         --form uptime=</proc/uptime \
117         --form ifconfig=</tmp/ifconfig \
118         --form nonce=</tmp/nonce \
119         --location \
120         --output $UNVERIFIED_SCRIPT \
121         --sslv3  \
122         --silent \
123         --show-error \
124         --fail \
125         --stderr /tmp/curl_errors \
126         --cacert $CONNECT_BOOT_SERVER_CACERT \
127    https://$CONNECT_BOOT_SERVER:$CONNECT_BOOT_SERVER_PORT/$CONNECT_BOOT_SERVER_PATH"
128
129     # assemble the gpg command line
130     GPG_CMD="/usr/bin/gpg \
131         --no-default-keyring \
132         --keyring $CONNECT_BOOT_SERVER_GPG_KEYRING \
133         --output $VERIFIED_SCRIPT \
134         --always-trust \
135         --decrypt $UNVERIFIED_SCRIPT"
136
137     echo "pl_boot: generating new nonce"
138     /usr/bin/head --bytes=32 /dev/urandom | \
139         /usr/bin/od -tx1 -An --width=32 | \
140         /bin/sed 's/ //g' > /tmp/nonce
141
142     echo "pl_boot: fetching script from boot server $CONNECT_BOOT_SERVER"
143     rm -f $UNVERIFIED_SCRIPT
144     $CURL_CMD
145     curl_err=$?
146     if [ $curl_err -ne 0 ]; then
147         echo "pl_boot: curl request failed with error $curl_err:"
148         cat /tmp/curl_errors
149         echo
150         continue
151     fi 
152
153     echo "pl_boot: verifying downloaded script"
154     rm -f $VERIFIED_SCRIPT
155     $GPG_CMD 2> /tmp/gpg_errors
156     if [ $? -ne 0 ]; then
157         echo "pl_boot: failed to verify file:"
158         cat /tmp/gpg_errors
159         echo
160         continue
161     fi
162     echo "pl_boot: decrypted and verified script succesfully"
163
164     echo "pl_boot: handing control to download script"
165     rm -f $UNVERIFIED_SCRIPT
166     chmod +x $VERIFIED_SCRIPT
167     $VERIFIED_SCRIPT
168     
169     echo "pl_boot: downloaded script has returned"
170 done
171
172 echo "pl_boot: automatic boot process canceled by user"