corosync-qnetd-certutil.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2015 Red Hat, Inc.
  4. #
  5. # All rights reserved.
  6. #
  7. # Author: Jan Friesse (jfriesse@redhat.com)
  8. #
  9. # This software licensed under BSD license, the text of which follows:
  10. #
  11. # Redistribution and use in source and binary forms, with or without
  12. # modification, are permitted provided that the following conditions are met:
  13. #
  14. # - Redistributions of source code must retain the above copyright notice,
  15. # this list of conditions and the following disclaimer.
  16. # - Redistributions in binary form must reproduce the above copyright notice,
  17. # this list of conditions and the following disclaimer in the documentation
  18. # and/or other materials provided with the distribution.
  19. # - Neither the name of the Red Hat, Inc. nor the names of its
  20. # contributors may be used to endorse or promote products derived from this
  21. # software without specific prior written permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. # THE POSSIBILITY OF SUCH DAMAGE.
  34. #
  35. DB_DIR=nssdb
  36. # Validity of certificate (months)
  37. CRT_VALIDITY=120
  38. CA_NICKNAME="QNet CA"
  39. SERVER_NICKNAME="QNetd Cert"
  40. CLUSTER_NICKNAME="Cluster Cert"
  41. CA_SUBJECT="CN=QNet CA"
  42. SERVER_SUBJECT="CN=Qnetd Server"
  43. PWD_FILE="$DB_DIR/pwdfile.txt"
  44. NOISE_FILE="$DB_DIR/noise.txt"
  45. SERIAL_NO_FILE="$DB_DIR/serial.txt"
  46. CA_EXPORT_FILE="$DB_DIR/qnetd-cacert.crt"
  47. CRQ_FILE="$DB_DIR/qnetd-node.crq"
  48. CRT_FILE="" # Generated from cluster name
  49. P12_FILE="$DB_DIR/qnetd-node.p12"
  50. usage() {
  51. echo "$0: [-I|-i|-m|-M|-r|-s] [-c certificate] [-n cluster_name]"
  52. echo
  53. echo " -I Initialize server CA and generate server certificate"
  54. echo " -i Initialize node CA. Needs CA certificate from server"
  55. echo ""
  56. echo " -m Import cluster certificate on node (needs pk12 certificate)"
  57. echo ""
  58. echo " -r Generate cluster certificate request"
  59. echo " -s Sign cluster certificate on qnetd server (needs cluster certificate)"
  60. echo " -M Import signed cluster certificate and export certificate with key to pk12 file"
  61. echo ""
  62. echo " -c certificate Ether CA, CRQ, CRT or pk12 certificate (operation dependant)"
  63. echo " -n cluster_name Name of cluster (for -r and -s operations)"
  64. echo ""
  65. echo "Typical usage:"
  66. echo "- Initialize database on QNetd server by running $0 -I"
  67. echo "- Copy exported QNetd CA certificate ($CA_EXPORT_FILE) to every node"
  68. echo "- On one of cluster node initialize database by running $0 -i -c `basename $CA_EXPORT_FILE`"
  69. echo "- Generate certificate request: $0 -r -n Cluster"
  70. echo "- Copy exported CRQ to QNetd server"
  71. echo "- On QNetd server sign and export cluster certificate by running $0 -s -c `basename $CRQ_FILE` -n Cluster"
  72. echo "- Copy exported CRT to node where certificate request was created"
  73. echo "- Import certificate on node where certificate request was created by running $0 -M -c qnetd-cluster-Cluster.crt"
  74. echo "- Copy output $P12_FILE to all other cluster nodes"
  75. echo "- On all other nodes in cluster:"
  76. echo " - Init database by running $0 -i -c `basename $CA_EXPORT_FILE`"
  77. echo " - Import cluster certificate and key: $0 -m -c `basename $P12_FILE`"
  78. exit 0
  79. }
  80. create_new_noise_file() {
  81. local noise_file="$1"
  82. if [ ! -e "$noise_file" ];then
  83. echo "Creating new noise file $noise_file"
  84. (ps -elf; date; w) | sha1sum | (read sha_sum rest; echo $sha_sum) > "$noise_file"
  85. chown root:root "$noise_file"
  86. chmod 400 "$noise_file"
  87. else
  88. echo "Using existing noise file $noise_file"
  89. fi
  90. }
  91. get_serial_no() {
  92. local serial_no
  93. if ! [ -f "$SERIAL_NO_FILE" ];then
  94. echo "100" > $SERIAL_NO_FILE
  95. fi
  96. serial_no=`cat $SERIAL_NO_FILE`
  97. serial_no=$((serial_no+1))
  98. echo "$serial_no" > $SERIAL_NO_FILE
  99. echo "$serial_no"
  100. }
  101. init_server_ca() {
  102. if [ -f "$DB_DIR/cert8.db" ];then
  103. echo "Certificate database already exists. Delete it to continue" >&2
  104. exit 1
  105. fi
  106. if ! [ -d "$DB_DIR" ];then
  107. echo "Creating $DB_DIR"
  108. mkdir "$DB_DIR"
  109. chown root:root "$DB_DIR"
  110. chmod 700 "$DB_DIR"
  111. fi
  112. echo "Creating new key and cert db"
  113. echo -n "" > "$PWD_FILE"
  114. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  115. chown root:root "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  116. chmod 600 "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  117. create_new_noise_file "$NOISE_FILE"
  118. echo "Creating new CA"
  119. # Create self-signed certificate (CA). Asks 3 questions (is this CA, lifetime and critical extension
  120. echo -e "y\n0\ny\n" | certutil -S -n "$CA_NICKNAME" -s "$CA_SUBJECT" -x \
  121. -t "CT,," -m `get_serial_no` -v $CRT_VALIDITY -d "$DB_DIR" \
  122. -z "$NOISE_FILE" -f "$PWD_FILE" -2
  123. # Export CA certificate in ascii
  124. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" > "$CA_EXPORT_FILE"
  125. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" -a >> "$CA_EXPORT_FILE"
  126. certutil -S -n "$SERVER_NICKNAME" -s "$SERVER_SUBJECT" -c "$CA_NICKNAME" -t "u,u,u" -m `get_serial_no` \
  127. -v $CRT_VALIDITY -d "$DB_DIR" -z "$NOISE_FILE" -f "$PWD_FILE"
  128. echo "QNetd CA is exported as $CA_EXPORT_FILE"
  129. }
  130. init_node_ca() {
  131. if [ -f "$DB_DIR/cert8.db" ];then
  132. echo "Certificate database already exists. Delete it to continue" >&2
  133. exit 1
  134. fi
  135. if ! [ -d "$DB_DIR" ];then
  136. echo "Creating $DB_DIR"
  137. mkdir "$DB_DIR"
  138. chown root:root "$DB_DIR"
  139. chmod 700 "$DB_DIR"
  140. fi
  141. echo "Creating new key and cert db"
  142. echo -n "" > "$PWD_FILE"
  143. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  144. chown root:root "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  145. chmod 600 "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  146. create_new_noise_file "$NOISE_FILE"
  147. echo "Importing CA"
  148. certutil -d "$DB_DIR" -A -t "CT,c,c" -n "$CA_NICKNAME" -f "$PWD_FILE" \
  149. -i "$CERTIFICATE_FILE"
  150. }
  151. gen_cluster_cert_req() {
  152. if ! [ -f "$DB_DIR/cert8.db" ];then
  153. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  154. exit 1
  155. fi
  156. echo "Creating new certificate request"
  157. certutil -R -s "CN=$CLUSTER_NAME" -o "$CRQ_FILE" -d "$DB_DIR" -f "$PWD_FILE" -z "$NOISE_FILE"
  158. echo "Certificate request stored in $CRQ_FILE"
  159. }
  160. sign_cluster_cert() {
  161. if ! [ -f "$DB_DIR/cert8.db" ];then
  162. echo "Certificate database doesn't exists. Use $0 -I to create it" >&2
  163. exit 1
  164. fi
  165. echo "Signing cluster certificate"
  166. certutil -C -v "$CRT_VALIDITY" -m `get_serial_no` -i "$CERTIFICATE_FILE" -o "$CRT_FILE" -c "$CA_NICKNAME" -d "$DB_DIR"
  167. echo "Certificate stored in $CRT_FILE"
  168. }
  169. import_signed_cert() {
  170. if ! [ -f "$DB_DIR/cert8.db" ];then
  171. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  172. exit 1
  173. fi
  174. echo "Importing signed cluster certificate"
  175. certutil -d "$DB_DIR" -A -t "u,u,u" -n "$CLUSTER_NICKNAME" -i "$CERTIFICATE_FILE"
  176. pk12util -d "$DB_DIR" -o "$P12_FILE" -W "" -n "$CLUSTER_NICKNAME"
  177. echo "Certificate stored in $P12_FILE"
  178. }
  179. import_pk12() {
  180. if ! [ -f "$DB_DIR/cert8.db" ];then
  181. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  182. exit 1
  183. fi
  184. echo "Importing cluster certificate and key"
  185. pk12util -i "$CERTIFICATE_FILE" -d "$DB_DIR" -W ""
  186. }
  187. OPERATION=""
  188. CERTIFICATE_FILE=""
  189. CLUSTER_NAME=""
  190. while getopts ":hIiMmrsc:n:" opt; do
  191. case $opt in
  192. r)
  193. OPERATION=gen_cluster_cert_req
  194. ;;
  195. I)
  196. OPERATION=init_server_ca
  197. ;;
  198. i)
  199. OPERATION=init_node_ca
  200. ;;
  201. s)
  202. OPERATION=sign_cluster_cert
  203. ;;
  204. m)
  205. OPERATION=import_pk12
  206. ;;
  207. M)
  208. OPERATION=import_signed_cert
  209. ;;
  210. n)
  211. CLUSTER_NAME="$OPTARG"
  212. ;;
  213. h)
  214. usage
  215. ;;
  216. c)
  217. CERTIFICATE_FILE="$OPTARG"
  218. ;;
  219. \?)
  220. echo "Invalid option: -$OPTARG" >&2
  221. exit 1
  222. ;;
  223. :)
  224. echo "Option -$OPTARG requires an argument." >&2
  225. exit 1
  226. ;;
  227. esac
  228. done
  229. case "$OPERATION" in
  230. "init_server_ca")
  231. init_server_ca
  232. ;;
  233. "init_node_ca")
  234. if ! [ -e "$CERTIFICATE_FILE" ];then
  235. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  236. exit 2
  237. fi
  238. init_node_ca
  239. ;;
  240. "gen_cluster_cert_req")
  241. if [ "$CLUSTER_NAME" == "" ];then
  242. echo "You have to specify cluster name" >&2
  243. exit 2
  244. fi
  245. gen_cluster_cert_req
  246. ;;
  247. "sign_cluster_cert")
  248. if ! [ -e "$CERTIFICATE_FILE" ];then
  249. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  250. exit 2
  251. fi
  252. if [ "$CLUSTER_NAME" == "" ];then
  253. echo "You have to specify cluster name" >&2
  254. exit 2
  255. fi
  256. CRT_FILE="$DB_DIR/qnetd-cluster-$CLUSTER_NAME.crt"
  257. sign_cluster_cert
  258. ;;
  259. "import_signed_cert")
  260. if ! [ -e "$CERTIFICATE_FILE" ];then
  261. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  262. exit 2
  263. fi
  264. import_signed_cert
  265. ;;
  266. "import_pk12")
  267. if ! [ -e "$CERTIFICATE_FILE" ];then
  268. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  269. exit 2
  270. fi
  271. import_pk12
  272. ;;
  273. *)
  274. usage
  275. ;;
  276. esac