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/")
6 folder=.bootstrap-user-data
9 # Curl gigi using correct host-header, faking https-access and using the cookies in "cookie-jar"
13 curl -s --header "X-Real-Proto: https" --header "Host: www.$hostname" -b $folder/cookie-jar "http://$ip/$url" "$@"
16 # get the csrf out of a webpage (arguments 1 and 2 can be used to select the correct csrf-token)
18 grep csrf | ${1:-cat} | ${2:-cat} | sed "s/.*value='\([^']*\)'.*/\\1/"
21 # update the cookie-jar so that cookies received with https-only property are also sent over our fake-https-connection
23 sed -i 's/TRUE/FALSE/g' "$1" # also use the cookie over insecure connection
25 function silent_read {
28 read -rsp "$prompt" "$@"
31 #execute a registration in gigi. If "$1" == "nopass" a password is not asked for but chosen at random.
33 csrf=$(mcurl register -c $folder/cookie-jar | csrf)
34 open-jar $folder/cookie-jar
35 silent_read "First Name: " fname
36 silent_read "Last Name: " lname
37 silent_read "Year of birth: " year
38 silent_read "Month of birth: " month
39 silent_read "Day of birth: " day
40 silent_read "Email address: " email
41 while [[ "$email" == *"'"* || "$email" == *"\\"* ]]; do
42 silent_read "Email address was not valid, try again: " email
44 if [[ "$1" == "nopass" ]]; then
45 pw1="$(head -c 15 /dev/urandom | base64)"
48 silent_read "Password: " pw1
49 silent_read "Password (repeat): " pw2
50 while [[ "$pw1" != "$pw2" ]]; do
51 silent_read "Password: " pw1
52 silent_read "Password (repeat): " pw2
55 mcurl register --data-urlencode "name-type=western" \
56 --data-urlencode "fname=$fname" \
57 --data-urlencode "lname=$lname" \
58 --data-urlencode "suffix" \
59 --data-urlencode "name" \
60 --data-urlencode "year=$year" \
61 --data-urlencode "month=$month" \
62 --data-urlencode "day=$day" \
63 --data-urlencode "residenceCountry=invalid" \
64 --data-urlencode "email=$email" \
65 --data-urlencode "pword1=$pw1" \
66 --data-urlencode "pword2=$pw2" \
67 --data-urlencode "general=1" \
68 --data-urlencode "country=1" \
69 --data-urlencode "regional=1" \
70 --data-urlencode "radius=1" \
71 --data-urlencode "tos_agree=1" \
72 --data-urlencode "process=Weiter" \
73 --data-urlencode "csrf=$csrf" > /dev/null
75 if ! type curl > /dev/null; then
76 echo "requires curl" >&2
79 if ! [[ "$(sudo lxc-attach -n postgres-primary -- su -c "psql -At gigi" postgres <<< "\dt")" == "No relations found." ]]; then
80 echo "gigi already has a database" >&2
83 # Manually managing gigi + nginx for now
84 sudo lxc-attach -n front-nginx systemctl stop puppet.service
85 sudo lxc-attach -n gigi systemctl stop puppet.service
87 #Stopping nginx so no-one external can interfere with our init procedure
88 sudo lxc-attach -n front-nginx systemctl stop nginx.service
89 sudo lxc-attach -n gigi systemctl stop gigi-proxy.{socket,service}
90 sudo lxc-attach -n gigi systemctl stop cassiopeia-client.service
91 sudo lxc-attach -n gigi gigi reset-database
92 sudo lxc-attach -n gigi systemctl start cassiopeia-client.service
93 sudo lxc-attach -n gigi systemctl start gigi-proxy.socket
95 rm -f $folder/cookie-jar
96 echo "So... preliminary things done. Let's start with the setup"
97 echo "We need a first administrative user, this user will be one of the bootstrappers for the WoT and seed for support."
101 echo "Ok, let's define the second bootstrapper"
103 secondaryEmail=$email
104 echo "You should now have been sent an activation link to the email you entered previously"
105 read -rp "The activation link: " link
107 csrf=$(mcurl "verify?$params" -c $folder/cookie-jar | csrf)
108 open-jar $folder/cookie-jar
109 echo "doing verification with $params"
110 if ! mcurl verify -d "$params&csrf=$csrf" | grep -qF "<div class='alert alert-success'>"; then
111 echo "Your e-mail address did not verify." >&2
115 echo "granting initial bootstrapping-rights"
116 sudo lxc-attach -n postgres-primary -- su -c "psql -d gigi" postgres <<EOF
117 INSERT INTO user_groups("user","permission","grantedby") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),'supporter',(SELECT "id" FROM "users" WHERE "email"='$adminEmail'));
118 INSERT INTO user_groups("user","permission","grantedby") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),'orgassurer',(SELECT "id" FROM "users" WHERE "email"='$adminEmail'));
119 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)');
120 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)');
121 INSERT INTO cats_passed("user_id", "variant_id") VALUES((SELECT "id" FROM "users" WHERE "email"='$adminEmail'),1);
123 sudo lxc-attach -n gigi -- systemctl stop gigi-proxy.service
125 csrf=$(mcurl login -c $folder/cookie-jar | csrf)
126 open-jar $folder/cookie-jar
127 mcurl login -c $folder/cookie-jar --data-urlencode "username=$adminEmail" --data-urlencode "password=$adminPw" --data-urlencode "csrf=$csrf" &>/dev/null
128 open-jar $folder/cookie-jar
130 echo "Creating organisation"
131 csrf=$(mcurl "orga/new" | csrf)
132 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_")
133 if ! grep -q '^[0-9]\+$' <<< $mgmOid; then
134 echo "Got an Organisation ID that is not a number: $mgmOid." >&2
137 printf "Management Organisation id is \"%s\"\n" "$mgmOid"
139 echo "add self as orgadmin for organisation"
140 csrf=$(mcurl orga/$mgmOid | csrf "head -n 2" "tail -n 1")
141 mcurl orga/$mgmOid --data-urlencode "email=$adminEmail" --data-urlencode "master=y" --data-urlencode "do_affiliate=Add" --data-urlencode "csrf=$csrf" &>/dev/null
142 echo "adding org-domain"
143 csrf=$(mcurl orga/$mgmOid | csrf "head -n 4" "tail -n 1")
144 domainName="$hostname"
145 mcurl orga/$mgmOid -d "domain=$domainName&addDomain=action&csrf=$csrf" &> /dev/null
147 csrf=$(mcurl account/details | csrf "tail -1")
148 mcurl account/details -d "orgaForm=orga&org%3A$mgmOid&csrf=$csrf" &> /dev/null
150 echo "Configuring pings for the domain"
151 domain=$(mcurl "account/domains" | grep "/account/domains/" | sed "s_.*/\([0-9]\+\)'.*_\1_")
152 if ! grep -q '^[0-9]\+$' <<< $domain; then
153 echo "Got a Domain ID that is not a number: $domain." >&2
157 csrf=$(mcurl "account/domains/$domain" | tee $folder/domain | csrf "tail -n 1")
159 token=$(grep pre $folder/domain | tail -n 1 | sed "s_.*>\([a-zA-Z0-9]*\)<.*_\1_")
160 name=$(grep "content available under" $folder/domain | sed "s_.*/cacert-\([a-zA-Z0-9]*\)\\.txt.*_\1_")
162 sudo mkdir -p /data/nginx/challenge
163 printf "%s" "$token" | sudo tee /data/nginx/challenge/cacert-$name.txt > /dev/null
165 openssl req -newkey rsa:4096 -subj "/CN=$domainName/OU=$token" -nodes -out $folder/self-req -keyout $folder/self-priv
166 openssl x509 -req -in $folder/self-req -signkey $folder/self-priv -out $folder/self-cert -extfile <(printf "extendedKeyUsage = clientAuth, serverAuth\n")
168 cp $folder/self-cert modules/gigi/files/gigi.crt
169 setfacl -m user:puppet:r $folder/self-priv
170 cp --preserve=all $folder/self-priv modules/gigi/files/gigi.key
171 sudo lxc-attach -n front-nginx -- puppet agent --test --verbose
173 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" > /dev/null
175 echo "Pings configured... waiting"
177 mcurl "account/domains/$domain" > $folder/domainStatus
179 echo "Issuing certificate for web"
183 csrf=$(mcurl "account/certs/new" | csrf "head -n 1")
185 openssl req -newkey rsa:4096 -subj "/CN=blabla" -nodes -out $folder/req -keyout $folder/priv
186 encoded=$(tr '\n' '?' < $folder/req | sed "s/=/%3D/g;s/+/%2B/g;s/\?/%0A/g")
188 mcurl account/certs/new -d "CSR=$encoded&process=Next&csrf=$csrf" > /dev/null
190 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_")
191 echo "Certificate: $serial"
192 if [[ $serial != "" ]]; then
194 mcurl "account/certs/$serial.crt?chain&noAnchor" > $folder/cert.crt
200 if issue "profile=server-orga&CN=&SANs=dns%3Awww.$domainName%2Cdns%3Astatic.$domainName%2Cdns%3Aapi.$domainName%2Cdns%3Asecure.$domainName"; then
201 cp $folder/cert.crt modules/gigi/files/gigi.crt
202 setfacl -m user:puppet:r $folder/priv
203 cp --preserve=all $folder/priv modules/gigi/files/gigi.key
204 echo "reloading cert"
205 sudo lxc-attach -n front-nginx -- puppet agent --test --verbose
207 echo "refusing to update"
210 if issue "profile=mail-orga&CN=Gigi+System&SANs=email%3Agigi@$domainName"; then
212 keystorepw=$(head -c 15 /dev/urandom | base64)
213 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
214 printf '%s' "$keystorepw" | sudo tee modules/gigi/files/keystorepw > /dev/null
216 echo "refusing to update"
218 echo "marking gigi ready"
219 echo yes | sudo lxc-attach -n gigi tee /gigi-ready > /dev/null
220 sudo lxc-attach -n gigi -- puppet agent --test --verbose
222 sudo lxc-attach -n front-nginx systemctl start puppet.service
223 sudo lxc-attach -n gigi systemctl start puppet.service