spelling mistake in message output
[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 # where all the configuration files for contacting
12 # the boot server are stored
13 BOOT_DIR=/usr/boot/
14
15 # get the server we are going to be contacting
16 BOOT_SERVER=`cat $BOOT_DIR/boot_server`
17 BOOT_SERVER_PORT=`cat $BOOT_DIR/boot_server_port`
18
19 # the file to request from the boot server
20 SERVER_BOOT_DIR=`cat $BOOT_DIR/boot_server_path`
21
22 # location of the cacert for this boot server
23 BOOT_CACERT=$BOOT_DIR/cacert.pem
24
25 # location of the gpg key ring to verify scripts
26 BOOT_GPG_KEYRING=$BOOT_DIR/pubring.gpg
27
28 # location of a file containing this boot cd version
29 BOOT_VERSION_FILE=/pl_version
30
31 # the locations of the downloaded scripts
32 UNVERIFIED_SCRIPT=/tmp/bootscript.gpg
33 VERIFIED_SCRIPT=/tmp/bootscript
34
35 # asemble the curl transaction
36 CURL_CMD="/usr/bin/curl \
37    --connect-timeout 60 \
38    --max-time 600 \
39    --form version=<$BOOT_VERSION_FILE \
40    --form cmdline=</proc/cmdline \
41    --form uptime=</proc/uptime \
42    --form ifconfig=</tmp/ifconfig \
43    --form cpuinfo=</proc/cpuinfo \
44    --form meminfo=</proc/meminfo \
45    --form nonce=</tmp/nonce \
46    --location \
47    --output $UNVERIFIED_SCRIPT \
48    --sslv3  \
49    --silent \
50    --show-error \
51    --fail \
52    --stderr /tmp/curl_errors \
53    --cacert $BOOT_CACERT \
54    https://$BOOT_SERVER:$BOOT_SERVER_PORT/$SERVER_BOOT_DIR"
55
56
57 # assemble the gpg command line
58 GPG_CMD="/usr/bin/gpg \
59     --no-default-keyring \
60     --keyring $BOOT_GPG_KEYRING \
61     --output $VERIFIED_SCRIPT \
62     --always-trust \
63     --decrypt $UNVERIFIED_SCRIPT"
64
65
66 # now, contact the boot server, run the script, and do it over again.
67 first=1
68 while true; do
69
70     if [[ -f $CANCEL_BOOT_FLAG ]]; then
71         echo "pl_boot: got request to cancel boot, exiting"
72         exit 0
73     fi
74
75     if [[ $first -eq 0 ]]; then
76         echo "pl_boot: fetching new script in 30 seconds"
77         /bin/sleep 30
78     fi
79     first=0
80
81     echo "pl_boot: generating new nonce"
82     /usr/bin/head --bytes=32 /dev/urandom | \
83         /usr/bin/od -tx1 -An --width=32 | \
84         /bin/sed 's/ //g' > /tmp/nonce
85
86     echo "pl_boot: fetching script from boot server $BOOT_SERVER"
87     rm -f $UNVERIFIED_SCRIPT
88     $CURL_CMD
89     curl_err=$?
90     if [ $curl_err -ne 0 ]; then
91         echo "pl_boot: curl request failed with error $curl_err:"
92         cat /tmp/curl_errors
93         echo
94         continue
95     fi 
96
97     echo "pl_boot: verifying downloaded script"
98     rm -f $VERIFIED_SCRIPT
99     $GPG_CMD 2> /tmp/gpg_errors
100     if [ $? -ne 0 ]; then
101         echo "pl_boot: failed to verify file:"
102         cat /tmp/gpg_errors
103         echo
104         continue
105     fi
106     echo "pl_boot: decrypted and verified script succesfully"
107
108     echo "pl_boot: handing control to download script"
109     rm -f $UNVERIFIED_SCRIPT
110     chmod +x $VERIFIED_SCRIPT
111     $VERIFIED_SCRIPT
112     
113     echo "pl_boot: downloaded script has returned"
114 done
115
116 echo "pl_boot: automatic boot process canceled by user"