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