Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 3 | function generate_client_config() { |
| 4 | echo |
| 5 | echo "What is the parent CA's prefix?" |
| 6 | read -r parent_ca_prefix |
| 7 | echo "what is the parent certificate?" |
| 8 | root_cert=$(cat | tr -d '\n') |
| 9 | |
| 10 | cat > 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 |
| 28 | echo "config file generated at ndncert-site-client.conf" |
| 29 | echo |
| 30 | } |
| 31 | |
| 32 | function generate_ca_config() { |
| 33 | echo "Load the new configuration file for the CA" |
| 34 | echo "Would you like to allow email challenge for this CA? [Y/N]" |
| 35 | read -r allow_email_challenge |
| 36 | # prepare CA configuration file |
| 37 | cat > /usr/local/etc/ndncert/ca.conf << ~EOF |
| 38 | { |
| 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 |
| 50 | if [ "$allow_email_challenge" = 'y' ]; then |
| 51 | echo '{ "challenge": "email" },' >> /usr/local/etc/ndncert/ca.conf |
| 52 | elif [ "$allow_email_challenge" = 'Y' ]; then |
| 53 | echo '{ "challenge": "email" },' >> /usr/local/etc/ndncert/ca.conf |
| 54 | fi |
| 55 | cat >> /usr/local/etc/ndncert/ca.conf << ~EOF |
| 56 | { "challenge": "pin" } |
| 57 | ], |
| 58 | "name-assignment": |
| 59 | { |
| 60 | "param": "/email" |
| 61 | } |
| 62 | } |
| 63 | ~EOF |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 64 | echo "" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 65 | } |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 66 | |
tylerliu | 9f19df5 | 2020-10-17 21:11:57 -0700 | [diff] [blame] | 67 | DEPLOYMENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
tylerliu | e4bd697 | 2020-10-17 21:17:12 -0700 | [diff] [blame^] | 68 | NDNCERT_DIR="$(dirname "$DEPLOYMENT_DIR")" |
tylerliu | 9f19df5 | 2020-10-17 21:11:57 -0700 | [diff] [blame] | 69 | |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 70 | echo "Do you want to (re) compile and build NDNCERT? [Y/N]" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 71 | read -r NDNCERT_COMPILE |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 72 | echo "" |
| 73 | |
| 74 | case $NDNCERT_COMPILE in |
| 75 | N|n) |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 76 | echo "Okay, we'll skip compilation and build." |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 77 | ;; |
| 78 | Y|y) |
tylerliu | 9f19df5 | 2020-10-17 21:11:57 -0700 | [diff] [blame] | 79 | CXXFLAGS="-O2" "$NDNCERT_DIR/waf" configure |
| 80 | "$NDNCERT_DIR/waf" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 81 | ;; |
| 82 | *) |
| 83 | echo "Unknown option, build and install is cancelled" |
| 84 | exit |
| 85 | ;; |
| 86 | esac |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 87 | echo "Need sudo to install NDNCERT CLI tools" |
tylerliu | 9f19df5 | 2020-10-17 21:11:57 -0700 | [diff] [blame] | 88 | sudo "$NDNCERT_DIR/waf" install |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 89 | echo "" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 90 | |
| 91 | echo "===================================================================" |
| 92 | echo "==" |
| 93 | echo "== Deploying NDNCERT" |
| 94 | echo "==" |
| 95 | echo "===================================================================" |
| 96 | echo "" |
| 97 | echo "Are you sure [Y/n] ?" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 98 | read -r DEPLOY |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 99 | |
| 100 | case $DEPLOY in |
| 101 | N|n) |
| 102 | echo "Deployment cancelled" |
| 103 | exit |
| 104 | ;; |
| 105 | Y|y) |
| 106 | ;; |
| 107 | *) |
| 108 | echo "Unknown option, deployment cancelled" |
| 109 | exit |
| 110 | ;; |
| 111 | esac |
| 112 | |
| 113 | echo "" |
| 114 | echo "===================================================================" |
| 115 | echo "==" |
| 116 | echo "== Deployment started" |
| 117 | echo "==" |
| 118 | echo "===================================================================" |
| 119 | |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 120 | echo "What is the CA Prefix (eg. /example) you want to deploy?" |
| 121 | read -r CA_PREFIX |
| 122 | echo "" |
| 123 | |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 124 | echo "Do you want to install ndncert CA for systemd on this machine? [Y/N]" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 125 | read -r SYSTEMD_INSTALL |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 126 | echo "" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 127 | |
| 128 | case $SYSTEMD_INSTALL in |
| 129 | N|n) |
| 130 | echo "We will not install systemd CA on this machine" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 131 | echo "Successfully finish the deployment of NDNCERT. To run NDNCERT, please use CLI ndncert-ca-server" |
| 132 | exit |
| 133 | ;; |
| 134 | Y|y) |
| 135 | echo "Copying NDNCERT-CA systemd service on this machine" |
tylerliu | 9f19df5 | 2020-10-17 21:11:57 -0700 | [diff] [blame] | 136 | sudo cp "$NDNCERT_DIR/build/systemd/ndncert-ca.service" /etc/systemd/system |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 137 | sudo chmod 644 /etc/systemd/system/ndncert-ca.service |
| 138 | ;; |
| 139 | *) |
| 140 | echo "Unknown option, deployment cancelled" |
| 141 | exit |
| 142 | ;; |
| 143 | esac |
| 144 | |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 145 | echo "" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 146 | echo "ndncert-ca service requires user ndn. Will check it now :D" |
| 147 | if id ndn &>/dev/null; then |
| 148 | echo 'ndn user account found, GOOD!' |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 149 | else |
| 150 | echo 'ndn user not found; adding ndn user as root' |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 151 | sudo useradd ndn |
| 152 | fi |
| 153 | |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 154 | echo "" |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 155 | echo "ndncert-ca service requires /var/lib/ndncert-ca. Will check or create the keychain in /var/lib/ndncert-ca" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 156 | sudo mkdir -p /var/lib/ndncert-ca |
| 157 | sudo chown ndn /var/lib/ndncert-ca |
| 158 | echo '/var/lib/ndncert-ca is ready, GOOD!' |
| 159 | |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 160 | echo "" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 161 | echo "Do you want to import an exisitng safebag for $CA_PREFIX ? [Y/N]" |
| 162 | read -r USE_SAFE_BAG |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 163 | |
| 164 | case $USE_SAFE_BAG in |
| 165 | N|n) |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 166 | echo "Generating new NDN identity for $CA_PREFIX" |
| 167 | sudo HOME=/var/lib/ndncert-ca -u ndn ndnsec-keygen "$CA_PREFIX" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 168 | ;; |
| 169 | Y|y) |
| 170 | echo "Reading the safebag." |
| 171 | echo "What is the safebag file name?" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 172 | read -r SAFE_BAG_PATH |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 173 | echo "" |
| 174 | |
| 175 | echo "What is the password of the safebag?" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 176 | read -r SAFE_BAG_PWD |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 177 | echo "" |
| 178 | |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 179 | sudo HOME=/var/lib/ndncert-ca -u ndn ndnsec-import -i "$SAFE_BAG_PATH" -P "$SAFE_BAG_PWD" |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 180 | ;; |
| 181 | *) |
| 182 | echo "Unknown option, deployment cancelled" |
| 183 | exit |
| 184 | ;; |
| 185 | esac |
| 186 | |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 187 | echo "" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 188 | echo "Do you want to request a certificate from a parent CA? [Y/N]" |
| 189 | read -r RUN_CLIENT |
| 190 | case $RUN_CLIENT in |
| 191 | Y|y) |
| 192 | echo "Running ndncert client" |
| 193 | generate_client_config |
| 194 | ndncert-client -c ndncert-site-client.conf |
| 195 | rm ndncert-site-client.conf |
| 196 | |
| 197 | echo "What is the new certificate name?" |
| 198 | read -r new_cert_name |
| 199 | ndnsec set-default -c "$new_cert_name" |
| 200 | ;; |
| 201 | *) |
| 202 | echo "Will not request a certificate. " |
| 203 | ;; |
| 204 | esac |
| 205 | |
| 206 | generate_ca_config "$CA_PREFIX" |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 207 | |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 208 | echo "Do you want to start the service now? [Y/N]" |
tylerliu | b7083bd | 2020-10-17 20:42:14 -0700 | [diff] [blame] | 209 | read -r START_NOW |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 210 | case $START_NOW in |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 211 | N|n) |
| 212 | echo "Successfully finish the deployment of NDNCERT. You can run sudo systemctl start ndncert-ca when you want to start the service" |
| 213 | exit |
| 214 | ;; |
| 215 | Y|y) |
| 216 | echo "Starting the service ndncert-ca" |
| 217 | sudo systemctl start ndncert-ca |
Zhiyi Zhang | c318a7a | 2020-10-17 17:41:19 -0700 | [diff] [blame] | 218 | sleep 2 |
Zhiyi Zhang | 915aa45 | 2020-10-17 17:20:58 -0700 | [diff] [blame] | 219 | echo "Reading the status of service ndncert-ca" |
| 220 | sudo systemctl status ndncert-ca |
| 221 | 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" |
| 222 | exit |
| 223 | ;; |
| 224 | *) |
| 225 | echo "Unknown option, deployment cancelled" |
| 226 | exit |
| 227 | ;; |
| 228 | esac |