]> WPIA git - infra.git/blob - bootstrap-user
fix: accept dp to work with current gigi and others
[infra.git] / bootstrap-user
1 #!/bin/bash
2 #Get domain-name and gigi-ip from the puppet config to know how to communicate with gigi
3 hostname=$(sed -n "/\$systemDomain/ { s/\$systemDomain = '\([^']*\)'/\1/; p }" environments/production/manifests/ip.pp)
4 ip=$(grep -A10 -F '$ips' environments/production/manifests/ip.pp | grep -F 'gigi =>' | head -n 1 | sed "s/.*'\([^']*\)'.*/\1/")
5
6 folder=.bootstrap-user-data
7 mkdir -p $folder
8
9 # Curl gigi using correct host-header, faking https-access and using the cookies in "cookie-jar"
10 function mcurl {
11     local url="$1"
12     shift
13     curl -s --header "X-Real-Proto: https" --header "Host: www.$hostname" -b $folder/cookie-jar "http://$ip/$url" "$@"
14 }
15
16 # get the csrf out of a webpage (arguments 1 and 2 can be used to select the correct csrf-token)
17 function csrf {
18     grep csrf | ${1:-cat}  | ${2:-cat} | sed "s/.*value='\([^']*\)'.*/\\1/"
19 }
20
21 # update the cookie-jar so that cookies received with https-only property are also sent over our fake-https-connection
22 function open-jar {
23     sed -i 's/TRUE/FALSE/g' "$1" # also use the cookie over insecure connection
24 }
25 function silent_read {
26     prompt="$1"
27     shift
28     read -rsp "$prompt" "$@"
29     printf '\n'
30 }
31 #execute a registration in gigi. If "$1" == "nopass" a password is not asked for but chosen at random.
32 function register {
33     csrf=$(mcurl register -c $folder/cookie-jar | csrf)
34     if ! [[ -f $folder/cookie-jar ]]; then
35         echo "error, could not start gigi"
36         exit 1
37     fi
38     open-jar $folder/cookie-jar
39     silent_read "First Name: " fname
40     silent_read "Last Name: " lname
41     silent_read "Year of birth: " year
42     silent_read "Month of birth: " month
43     silent_read "Day of birth: " day
44     silent_read "Email address: " email
45     while [[ "$email" == *"'"* || "$email" == *"\\"* ]]; do
46         silent_read "Email address was not valid, try again: " email
47     done
48     if [[ "$1" == "nopass" ]]; then
49         pw1="$(head -c 15 /dev/urandom | base64)"
50         pw2="$pw1"
51     else
52         silent_read "Password: " pw1
53         silent_read "Password (repeat): " pw2
54         while [[ "$pw1" != "$pw2" ]]; do
55             silent_read "Password: " pw1
56             silent_read "Password (repeat): " pw2
57         done
58     fi
59     mcurl register --data-urlencode "name-type=western" \
60           --data-urlencode "fname=$fname" \
61           --data-urlencode "lname=$lname" \
62           --data-urlencode "suffix" \
63           --data-urlencode "name" \
64           --data-urlencode "year=$year" \
65           --data-urlencode "month=$month" \
66           --data-urlencode "day=$day" \
67           --data-urlencode "residenceCountry=invalid" \
68           --data-urlencode "email=$email" \
69           --data-urlencode "pword1=$pw1" \
70           --data-urlencode "pword2=$pw2" \
71           --data-urlencode "general=1" \
72           --data-urlencode "country=1" \
73           --data-urlencode "regional=1" \
74           --data-urlencode "radius=1" \
75           --data-urlencode "tos_agree=1" \
76           --data-urlencode "dp_agree=1" \
77           --data-urlencode "process=Weiter" \
78           --data-urlencode "csrf=$csrf" > /dev/null
79 }
80 function check_error {
81     cat > $folder/page_output
82     cat $folder/page_output >> $folder/log
83     if grep -q "error-msgs" $folder/page_output; then
84         cat $folder/page_output
85         exit 1
86     fi
87 }
88
89 if ! type curl > /dev/null; then
90     echo "requires curl" >&2
91     exit 1
92 fi
93 if ! [[ "$(sudo lxc-attach -n postgres-primary -- su -c "psql -At gigi" postgres <<< "\dt")" == "No relations found." ]]; then
94     echo "gigi already has a database" >&2
95     exit 1
96 fi
97 # Manually managing gigi + nginx for now
98 sudo lxc-attach -n front-nginx systemctl stop puppet.service
99 sudo lxc-attach -n gigi systemctl stop puppet.service
100
101 #Stopping nginx so no-one external can interfere with our init procedure
102 sudo lxc-attach -n front-nginx systemctl stop nginx.service
103 sudo lxc-attach -n gigi systemctl stop gigi-proxy.{socket,service}
104 sudo lxc-attach -n gigi systemctl stop cassiopeia-client.service
105 sudo lxc-attach -n gigi gigi reset-database
106 sudo lxc-attach -n gigi systemctl start cassiopeia-client.service
107 sudo lxc-attach -n gigi systemctl start gigi-proxy.socket
108
109 rm -f $folder/cookie-jar
110 echo "So... preliminary things done. Let's start with the setup"
111 echo "We need a first administrative user, this user will be one of the bootstrappers for the WoT and seed for support."
112 register
113 adminEmail=$email
114 adminPw=$pw1
115 echo "Ok, let's define the second bootstrapper"
116 register nopass
117 secondaryEmail=$email
118 echo "You should now have been sent an activation link to the email you entered previously"
119 read -rp "The activation link: " link
120 params=${link##*\?}
121 csrf=$(mcurl "verify?$params" -c $folder/cookie-jar | csrf)
122 open-jar $folder/cookie-jar
123 echo "doing verification with $params"
124 if ! mcurl verify -d "$params&csrf=$csrf" | grep -qF "<div class='alert alert-success'>"; then
125     echo "Your e-mail address did not verify." >&2
126     exit 1
127 fi
128
129 echo "granting initial bootstrapping-rights"
130 sudo lxc-attach -n postgres-primary -- su -c "psql -d gigi" postgres <<EOF
131 INSERT INTO user_groups("user","permission","grantedby") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),'supporter',(SELECT "id" FROM "users" WHERE "email"='$adminEmail'));
132 INSERT INTO user_groups("user","permission","grantedby") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),'org-agent',(SELECT "id" FROM "users" WHERE "email"='$adminEmail'));
133 INSERT INTO notary("from","to","points","location","when","date") VALUES((SELECT "id" FROM "users" WHERE "email"='$secondaryEmail'), (SELECT "preferredName" FROM "users" WHERE "email"='$adminEmail'), 100, 'initial', CURRENT_TIMESTAMP, '$(date +%Y-%m-%d)');
134 INSERT INTO notary("from","to","points","location","when","date") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'), (SELECT "preferredName" FROM "users" WHERE "email"='$secondaryEmail'), 100, 'initial', CURRENT_TIMESTAMP, '$(date +%Y-%m-%d)');
135 INSERT INTO cats_passed("user_id", "variant_id") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),1);
136 EOF
137 sudo lxc-attach -n gigi -- systemctl stop gigi-proxy.service
138
139 csrf=$(mcurl login -c $folder/cookie-jar | csrf)
140 open-jar $folder/cookie-jar
141 mcurl login -c $folder/cookie-jar --data-urlencode "username=$adminEmail" --data-urlencode "password=$adminPw" --data-urlencode "csrf=$csrf" | check_error
142 open-jar $folder/cookie-jar
143
144 echo "Creating organisation"
145 csrf=$(mcurl "orga/new" | csrf)
146 mgmOid=$(mcurl "orga/new" -v -d "O=SomeCA&L=town&ST=state&C=AT&contact=ce%40email.org&comments=&action=new&csrf=$csrf" 2>&1 | grep "< Location: " | sed "s_.*/\([0-9]*\)[^0-9]*_\1_")
147 if ! grep -q '^[0-9]\+$' <<< $mgmOid; then
148     echo "Got an Organisation ID that is not a number: $mgmOid." >&2
149     exit 1
150 fi
151 printf "Management Organisation id is \"%s\"\n" "$mgmOid"
152
153 printf "adding org-domain for org %s: %s\n" "$mgmOid" "$hostname"
154 csrf=$(mcurl orga/$mgmOid | csrf "head -n 4" "tail -n 1")
155 domainName="$hostname"
156 mcurl orga/$mgmOid -d "domain=$domainName&addDomain=action&csrf=$csrf" | check_error
157
158 echo "using SQL to add self as orgadmin for organisation"
159 sudo lxc-attach -n postgres-primary -- su -c "psql -d gigi" postgres <<EOF
160 INSERT INTO org_admin("orgid", "memid", "creator", "master") VALUES('$mgmOid', (SELECT "id" FROM "users" WHERE "email"='$adminEmail'), (SELECT "id" FROM "users" WHERE "email"='$secondaryEmail'), 'y');
161 EOF
162
163 csrf=$(mcurl account/details -v | csrf "tail -1")
164 mcurl account/details -v -d "orgaForm=orga&org%3A$mgmOid&csrf=$csrf" | check_error
165
166 echo "Configuring pings for the domain"
167 domain=$(mcurl "account/domains" | grep "/account/domains/" | sed "s_.*/\([0-9]\+\)'.*_\1_")
168 if ! grep -q '^[0-9]\+$' <<< $domain; then
169     echo "Got a Domain ID that is not a number: $domain." >&2
170     exit 1
171 fi
172
173 csrf=$(mcurl "account/domains/$domain" | tee $folder/domain | csrf "tail -n 1")
174
175 token=$(grep pre $folder/domain | tail -n 1 | sed "s_.*>\([a-zA-Z0-9]*\)<.*_\1_")
176 name=$(grep "content available at" $folder/domain | sed "s_.*/\([a-zA-Z0-9]*\)\\.txt.*_\1_")
177
178 sudo mkdir -p /data/nginx/challenge/.well-known/someca-challenge
179 printf "%s" "$token" | sudo tee /data/nginx/challenge/.well-known/someca-challenge/$name.txt > /dev/null
180
181 openssl req -newkey rsa:4096 -subj "/CN=$domainName/OU=$token" -nodes -out $folder/self-req -keyout $folder/self-priv
182 openssl x509 -req -in $folder/self-req -signkey $folder/self-priv -out $folder/self-cert -extfile <(printf "extendedKeyUsage = clientAuth, serverAuth\n")
183
184 cp $folder/self-cert modules/gigi/files/gigi.crt
185 setfacl -m user:puppet:r $folder/self-priv
186 cp --preserve=all $folder/self-priv modules/gigi/files/gigi.key
187 sudo lxc-attach -n front-nginx -- puppet agent --test --verbose
188
189 mcurl "account/domains/$domain" -d "HTTPType=y&SSLType=y&ssl-type-0=direct&ssl-port-0=443&ssl-type-1=direct&ssl-port-1=&ssl-type-2=direct&ssl-port-2=&ssl-type-3=direct&ssl-port-3=&csrf=$csrf" | check_error
190
191 echo "Pings configured... waiting"
192 sleep 5
193 mcurl "account/domains/$domain" > $folder/domainStatus
194
195 echo "Issuing certificate for web"
196
197 function issue {
198     options=$1
199     csrf=$(mcurl "account/certs/new" | csrf "head -n 1")
200
201     openssl req -newkey rsa:4096 -subj "/CN=blabla" -nodes -out $folder/req -keyout $folder/priv
202     encoded=$(tr '\n' '?' < $folder/req | sed "s/=/%3D/g;s/+/%2B/g;s/\?/%0A/g")
203
204     mcurl account/certs/new -d "CSR=$encoded&process=Next&csrf=$csrf" | check_error
205
206     serial=$(mcurl account/certs/new -d "$options&OU=&hash_alg=SHA256&validFrom=now&validity=2y&login=1&description=&process=Issue+Certificate&csrf=$csrf" -v 2>&1 | tee $folder/certlog | grep "< Location: " | sed "s_.*/\([a-f0-9]*\)[^0-9]*_\1_")
207     echo "Certificate: $serial"
208     if [[ $serial != "" ]]; then
209         echo "installing"
210         mcurl "account/certs/$serial.crt?chain&noAnchor" > $folder/cert.crt
211         return 0;
212     else
213         return 1;
214     fi
215 }
216 if issue "profile=server-orga&CN=&SANs=dns%3Awww.$domainName%2Cdns%3Astatic.$domainName%2Cdns%3Aapi.$domainName%2Cdns%3Asecure.$domainName"; then
217     cp $folder/cert.crt modules/gigi/files/gigi.crt
218     setfacl -m user:puppet:r $folder/priv
219     cp --preserve=all $folder/priv modules/gigi/files/gigi.key
220     echo "reloading cert"
221     sudo lxc-attach -n front-nginx -- puppet agent --test --verbose
222 else
223     echo "refusing to update"
224 fi
225
226 if issue "profile=mail-orga&CN=Gigi+System&SANs=email%3Agigi@$domainName"; then
227     echo "great!"
228     keystorepw=$(head -c 15 /dev/urandom | base64)
229     openssl pkcs12 -export -name "mail" -in $folder/cert.crt -inkey $folder/priv -CAfile modules/nre/files/config/ca/root.crt -password file:<(printf '%s' "$keystorepw") | sudo tee modules/gigi/files/keystore.pkcs12 > /dev/null
230     printf '%s' "$keystorepw" | sudo tee modules/gigi/files/keystorepw > /dev/null
231 else
232     echo "refusing to update"
233 fi
234 echo "marking gigi ready"
235 echo yes | sudo lxc-attach -n gigi tee /gigi-ready > /dev/null
236 sudo lxc-attach -n gigi -- puppet agent --test --verbose
237
238 sudo lxc-attach -n front-nginx systemctl start puppet.service
239 sudo lxc-attach -n gigi systemctl start puppet.service