blob: a3d875c87ffcd88a0bb88ced1759cbdf3abcd881 [file] [log] [blame]
Zhiyi Zhang915aa452020-10-17 17:20:58 -07001#!/usr/bin/env bash
2
tylerliub7083bd2020-10-17 20:42:14 -07003function generate_client_config() {
4echo
5echo "What is the parent CA's prefix?"
6read -r parent_ca_prefix
tylerliud33f5622020-10-17 21:54:01 -07007echo "what is the parent certificate? (use Ctrl-D to end input)"
tylerliub7083bd2020-10-17 20:42:14 -07008root_cert=$(cat | tr -d '\n')
9
10cat > ndncert-site-client.conf << ~EOF
11{
12 "ca-list":
13 [
14 {
15 "ca-prefix": "$parent_ca_prefix",
16 "ca-info": "NDN Testbed Root Trust Anchor",
17 "max-validity-period": "1296000",
18 "max-suffix-length": "3",
19 "probe-parameters":
20 [
21 {"probe-parameter-key": "pin"}
22 ],
23 "certificate": "$root_cert"
24 }
25 ]
26}
27~EOF
28echo "config file generated at ndncert-site-client.conf"
29echo
30}
31
32function generate_ca_config() {
33echo "Load the new configuration file for the CA"
34echo "Would you like to allow email challenge for this CA? [Y/N]"
35read -r allow_email_challenge
36# prepare CA configuration file
tylerliud33f5622020-10-17 21:54:01 -070037sudo cat > /usr/local/etc/ndncert/ca.conf << ~EOF
tylerliub7083bd2020-10-17 20:42:14 -070038{
39 "ca-prefix": "$1",
40 "ca-info": "NDN Trust Anchor: $1",
41 "max-validity-period": "1296000",
42 "max-suffix-length": "2",
43 "probe-parameters":
44 [
45 {"probe-parameter-key": "email"}
46 ],
47 "supported-challenges":
48 [
49~EOF
50if [ "$allow_email_challenge" = 'y' ]; then
tylerliud33f5622020-10-17 21:54:01 -070051 sudo echo '{ "challenge": "email" },' >> /usr/local/etc/ndncert/ca.conf
tylerliub7083bd2020-10-17 20:42:14 -070052elif [ "$allow_email_challenge" = 'Y' ]; then
tylerliud33f5622020-10-17 21:54:01 -070053 sudo echo '{ "challenge": "email" },' >> /usr/local/etc/ndncert/ca.conf
tylerliub7083bd2020-10-17 20:42:14 -070054fi
tylerliud33f5622020-10-17 21:54:01 -070055sudo cat >> /usr/local/etc/ndncert/ca.conf << ~EOF
tylerliub7083bd2020-10-17 20:42:14 -070056 { "challenge": "pin" }
57 ],
58 "name-assignment":
59 {
60 "param": "/email"
61 }
62}
63~EOF
Zhiyi Zhang915aa452020-10-17 17:20:58 -070064echo ""
tylerliub7083bd2020-10-17 20:42:14 -070065}
Zhiyi Zhang915aa452020-10-17 17:20:58 -070066
tylerliuf77d9552020-10-17 21:28:21 -070067deployment_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
68ndncert_dir="$(dirname "$deployment_dir")"
69current_path="$(pwd)"
70cd "$ndncert_dir"
tylerliu9f19df52020-10-17 21:11:57 -070071
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -070072echo "Do you want to (re) compile and build NDNCERT? [Y/N]"
tylerliub7083bd2020-10-17 20:42:14 -070073read -r NDNCERT_COMPILE
Zhiyi Zhang915aa452020-10-17 17:20:58 -070074echo ""
75
76case $NDNCERT_COMPILE in
77 N|n)
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -070078 echo "Okay, we'll skip compilation and build."
Zhiyi Zhang915aa452020-10-17 17:20:58 -070079 ;;
80 Y|y)
tylerliuca486302020-10-17 21:23:07 -070081 CXXFLAGS="-O2" "./waf" configure
82 "./waf"
Zhiyi Zhang915aa452020-10-17 17:20:58 -070083 ;;
84 *)
85 echo "Unknown option, build and install is cancelled"
tylerliuf77d9552020-10-17 21:28:21 -070086 cd "$current_path"
87 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -070088 ;;
89esac
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -070090echo "Need sudo to install NDNCERT CLI tools"
tylerliuca486302020-10-17 21:23:07 -070091sudo "./waf" install
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -070092echo ""
Zhiyi Zhang915aa452020-10-17 17:20:58 -070093
94echo "==================================================================="
95echo "=="
tylerliuf77d9552020-10-17 21:28:21 -070096echo "== deploying NDNCERT"
Zhiyi Zhang915aa452020-10-17 17:20:58 -070097echo "=="
98echo "==================================================================="
99echo ""
100echo "Are you sure [Y/n] ?"
tylerliuf77d9552020-10-17 21:28:21 -0700101read -r deploy
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700102
tylerliuf77d9552020-10-17 21:28:21 -0700103case $deploy in
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700104 N|n)
tylerliuf77d9552020-10-17 21:28:21 -0700105 echo "deployment cancelled"
106 cd "$current_path"
107 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700108 ;;
109 Y|y)
110 ;;
111 *)
112 echo "Unknown option, deployment cancelled"
tylerliuf77d9552020-10-17 21:28:21 -0700113 cd "$current_path"
114 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700115 ;;
116esac
117
118echo ""
119echo "==================================================================="
120echo "=="
tylerliuf77d9552020-10-17 21:28:21 -0700121echo "== deployment started"
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700122echo "=="
123echo "==================================================================="
124
tylerliub7083bd2020-10-17 20:42:14 -0700125echo "What is the CA Prefix (eg. /example) you want to deploy?"
tylerliuf77d9552020-10-17 21:28:21 -0700126read -r ca_prefix
tylerliub7083bd2020-10-17 20:42:14 -0700127echo ""
128
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700129echo "Do you want to install ndncert CA for systemd on this machine? [Y/N]"
tylerliuf77d9552020-10-17 21:28:21 -0700130read -r systemd_install
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700131echo ""
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700132
tylerliuf77d9552020-10-17 21:28:21 -0700133case $systemd_install in
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700134 N|n)
135 echo "We will not install systemd CA on this machine"
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700136 echo "Successfully finish the deployment of NDNCERT. To run NDNCERT, please use CLI ndncert-ca-server"
tylerliuf77d9552020-10-17 21:28:21 -0700137 cd "$current_path"
138 exit 0
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700139 ;;
140 Y|y)
141 echo "Copying NDNCERT-CA systemd service on this machine"
tylerliuf77d9552020-10-17 21:28:21 -0700142 sudo cp "$ndncert_dir/build/systemd/ndncert-ca.service" /etc/systemd/system
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700143 sudo chmod 644 /etc/systemd/system/ndncert-ca.service
144 ;;
145 *)
146 echo "Unknown option, deployment cancelled"
tylerliuf77d9552020-10-17 21:28:21 -0700147 cd "$current_path"
148 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700149 ;;
150esac
151
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700152echo ""
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700153echo "ndncert-ca service requires user ndn. Will check it now :D"
154if id ndn &>/dev/null; then
155 echo 'ndn user account found, GOOD!'
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700156else
157 echo 'ndn user not found; adding ndn user as root'
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700158 sudo useradd ndn
159fi
160
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700161echo ""
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700162echo "ndncert-ca service requires /var/lib/ndncert-ca. Will check or create the keychain in /var/lib/ndncert-ca"
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700163sudo mkdir -p /var/lib/ndncert-ca
164sudo chown ndn /var/lib/ndncert-ca
165echo '/var/lib/ndncert-ca is ready, GOOD!'
166
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700167echo ""
tylerliuf77d9552020-10-17 21:28:21 -0700168echo "Do you want to import an exisitng safebag for $ca_prefix ? [Y/N]"
tylerliu1666a1c2020-10-17 22:06:10 -0700169read -r use_safe_bag
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700170
tylerliu1666a1c2020-10-17 22:06:10 -0700171case $use_safe_bag in
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700172 N|n)
tylerliuf566faf2020-10-17 22:08:38 -0700173 if [ "$(HOME=/var/lib/ndncert-ca ndnsec list | grep " $ca_prefix$" > /dev/null 2>&1; echo $?)" -ne 0 ]; then
tylerliu1666a1c2020-10-17 22:06:10 -0700174 echo "Generating new NDN identity for $ca_prefix"
175 sudo HOME=/var/lib/ndncert-ca -u ndn ndnsec-keygen "$ca_prefix"
176 else
177 echo "Key detected for $ca_prefix"
178 echo "Continue..."
179 fi
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700180 ;;
181 Y|y)
182 echo "Reading the safebag."
183 echo "What is the safebag file name?"
tylerliuf77d9552020-10-17 21:28:21 -0700184 read -r safe_bag_path
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700185 echo ""
186
187 echo "What is the password of the safebag?"
tylerliuf77d9552020-10-17 21:28:21 -0700188 read -r safe_bafg_pwd
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700189 echo ""
190
tylerliuf77d9552020-10-17 21:28:21 -0700191 sudo HOME=/var/lib/ndncert-ca -u ndn ndnsec-import -i "$safe_bag_path" -P "$safe_bafg_pwd"
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700192 ;;
193 *)
194 echo "Unknown option, deployment cancelled"
tylerliuf77d9552020-10-17 21:28:21 -0700195 cd "$current_path"
196 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700197 ;;
198esac
199
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700200echo ""
tylerliub7083bd2020-10-17 20:42:14 -0700201echo "Do you want to request a certificate from a parent CA? [Y/N]"
tylerliuf77d9552020-10-17 21:28:21 -0700202read -r run_client
203case $run_client in
tylerliub7083bd2020-10-17 20:42:14 -0700204 Y|y)
205 echo "Running ndncert client"
206 generate_client_config
207 ndncert-client -c ndncert-site-client.conf
208 rm ndncert-site-client.conf
209
210 echo "What is the new certificate name?"
211 read -r new_cert_name
212 ndnsec set-default -c "$new_cert_name"
213 ;;
214 *)
215 echo "Will not request a certificate. "
216 ;;
217esac
218
tylerliuf77d9552020-10-17 21:28:21 -0700219generate_ca_config "$ca_prefix"
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700220
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700221echo "Do you want to start the service now? [Y/N]"
tylerliuf77d9552020-10-17 21:28:21 -0700222read -r start_now
223case $start_now in
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700224 N|n)
225 echo "Successfully finish the deployment of NDNCERT. You can run sudo systemctl start ndncert-ca when you want to start the service"
tylerliuf77d9552020-10-17 21:28:21 -0700226 cd "$current_path"
227 exit 0
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700228 ;;
229 Y|y)
230 echo "Starting the service ndncert-ca"
231 sudo systemctl start ndncert-ca
Zhiyi Zhangc318a7a2020-10-17 17:41:19 -0700232 sleep 2
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700233 echo "Reading the status of service ndncert-ca"
234 sudo systemctl status ndncert-ca
235 echo "Successfully finish the deployment of NDNCERT. You can run sudo systemctl status ndncert-ca when you want to check the status of the service"
tylerliuf77d9552020-10-17 21:28:21 -0700236 cd "$current_path"
237 exit 0
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700238 ;;
239 *)
240 echo "Unknown option, deployment cancelled"
tylerliuf77d9552020-10-17 21:28:21 -0700241 cd "$current_path"
242 exit 1
Zhiyi Zhang915aa452020-10-17 17:20:58 -0700243 ;;
244esac
tylerliuca486302020-10-17 21:23:07 -0700245
tylerliuf77d9552020-10-17 21:28:21 -0700246cd "$current_path"