4
0

corosync-qdevice-net-certutil.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!@BASHPATH@
  2. #
  3. # Copyright (c) 2015-2016 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. BASE_DIR="@COROSYSCONFDIR@/qdevice/net"
  36. DB_DIR_QNETD="@COROSYSCONFDIR@/qnetd/nssdb"
  37. DB_DIR_NODE="$BASE_DIR/nssdb"
  38. # Validity of certificate (months)
  39. CRT_VALIDITY=1200
  40. CA_NICKNAME="QNet CA"
  41. SERVER_NICKNAME="QNetd Cert"
  42. CLUSTER_NICKNAME="Cluster Cert"
  43. CA_SUBJECT="CN=QNet CA"
  44. SERVER_SUBJECT="CN=Qnetd Server"
  45. PWD_FILE_BASE="pwdfile.txt"
  46. NOISE_FILE_BASE="noise.txt"
  47. SERIAL_NO_FILE_BASE="serial.txt"
  48. CA_EXPORT_FILE="$DB_DIR_QNETD/qnetd-cacert.crt"
  49. CRQ_FILE_BASE="qdevice-net-node.crq"
  50. CRT_FILE_BASE="" # Generated from cluster name
  51. P12_FILE_BASE="qdevice-net-node.p12"
  52. QNETD_CERTUTIL_CMD="corosync-qnetd-certutil"
  53. usage() {
  54. echo "$0: [-i|-m|-M|-r|-s|-Q] [-c certificate] [-n cluster_name]"
  55. echo
  56. echo " -i Initialize node CA. Needs CA certificate from server"
  57. echo " -m Import cluster certificate on node (needs pk12 certificate)"
  58. echo " -r Generate cluster certificate request"
  59. echo " -M Import signed cluster certificate and export certificate with key to pk12 file"
  60. echo " -Q Quick start. Uses ssh/scp to initialze both qnetd and nodes."
  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 $QNETD_CERTUTIL_CMD -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 (Cluster name must match cluster_name key in the corosync.conf)"
  70. echo "- Copy exported CRQ to QNetd server"
  71. echo "- On QNetd server sign and export cluster certificate by running $QNETD_CERTUTIL_CMD -s -c `basename $CRQ_FILE_BASE` -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 cluster-Cluster.crt"
  74. echo "- Copy output $P12_FILE_BASE 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_BASE`"
  78. echo ""
  79. echo "It is also possible to use Quick start (-Q). This needs properly configured ssh."
  80. echo " $0 -Q -n Cluster qnetd_server node1 node2 ... nodeN"
  81. exit 0
  82. }
  83. create_new_noise_file() {
  84. local noise_file="$1"
  85. if [ ! -e "$noise_file" ];then
  86. echo "Creating new noise file $noise_file"
  87. (ps -elf; date; w) | sha1sum | (read sha_sum rest; echo $sha_sum) > "$noise_file"
  88. chown root:root "$noise_file"
  89. chmod 0660 "$noise_file"
  90. else
  91. echo "Using existing noise file $noise_file"
  92. fi
  93. }
  94. get_serial_no() {
  95. local serial_no
  96. if ! [ -f "$SERIAL_NO_FILE" ];then
  97. echo "100" > $SERIAL_NO_FILE
  98. chown root:root "$DB_DIR"
  99. chmod 0660 "$SERIAL_NO_FILE"
  100. fi
  101. serial_no=`cat $SERIAL_NO_FILE`
  102. serial_no=$((serial_no+1))
  103. echo "$serial_no" > $SERIAL_NO_FILE
  104. echo "$serial_no"
  105. }
  106. init_node_ca() {
  107. if [ -f "$DB_DIR/cert8.db" ];then
  108. echo "Certificate database already exists. Delete it to continue" >&2
  109. exit 1
  110. fi
  111. if ! [ -d "$DB_DIR" ];then
  112. echo "Creating $DB_DIR"
  113. mkdir -p "$DB_DIR"
  114. chown root:root "$DB_DIR"
  115. chmod 0770 "$DB_DIR"
  116. fi
  117. echo "Creating new key and cert db"
  118. echo -n "" > "$PWD_FILE"
  119. chown root:root "$PWD_FILE"
  120. chmod 0660 "$PWD_FILE"
  121. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  122. chown root:root "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  123. chmod 0660 "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  124. create_new_noise_file "$NOISE_FILE"
  125. echo "Importing CA"
  126. certutil -d "$DB_DIR" -A -t "CT,c,c" -n "$CA_NICKNAME" -f "$PWD_FILE" \
  127. -i "$CERTIFICATE_FILE"
  128. }
  129. gen_cluster_cert_req() {
  130. if ! [ -f "$DB_DIR/cert8.db" ];then
  131. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  132. exit 1
  133. fi
  134. echo "Creating new certificate request"
  135. certutil -R -s "CN=$CLUSTER_NAME" -o "$CRQ_FILE" -d "$DB_DIR" -f "$PWD_FILE" -z "$NOISE_FILE"
  136. echo "Certificate request stored in $CRQ_FILE"
  137. }
  138. import_signed_cert() {
  139. if ! [ -f "$DB_DIR/cert8.db" ];then
  140. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  141. exit 1
  142. fi
  143. echo "Importing signed cluster certificate"
  144. certutil -d "$DB_DIR" -A -t "u,u,u" -n "$CLUSTER_NICKNAME" -i "$CERTIFICATE_FILE"
  145. pk12util -d "$DB_DIR" -o "$P12_FILE" -W "" -n "$CLUSTER_NICKNAME"
  146. echo "Certificate stored in $P12_FILE"
  147. }
  148. import_pk12() {
  149. if ! [ -f "$DB_DIR/cert8.db" ];then
  150. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  151. exit 1
  152. fi
  153. echo "Importing cluster certificate and key"
  154. pk12util -i "$CERTIFICATE_FILE" -d "$DB_DIR" -W ""
  155. }
  156. quick_start() {
  157. qnetd_addr="$1"
  158. master_node="$2"
  159. other_nodes="$3"
  160. # Sanity check
  161. for i in "$master_node" $other_nodes;do
  162. if ssh root@$i "[ -d \"$DB_DIR_NODE\" ]";then
  163. echo "Node $i seems to be already initialized. Please delete $DB_DIR_NODE" >&2
  164. exit 1
  165. fi
  166. if ! ssh "root@$i" "$0" > /dev/null;then
  167. echo "Node $i doesn't have $0 installed" >&2
  168. exit 1
  169. fi
  170. done
  171. # Initialize qnetd server (it's no problem if server is already initialized)
  172. ssh "root@$qnetd_addr" "$QNETD_CERTUTIL_CMD -i"
  173. # Copy CA cert to all nodes and initialize them
  174. for node in "$master_node" $other_nodes;do
  175. scp "root@$qnetd_addr:$CA_EXPORT_FILE" "$node:/tmp"
  176. ssh "root@$node" "$0 -i -c \"/tmp/`basename $CA_EXPORT_FILE`\" && rm /tmp/`basename $CA_EXPORT_FILE`"
  177. done
  178. # Generate cert request
  179. ssh "root@$master_node" "$0 -r -n \"$CLUSTER_NAME\""
  180. # Copy exported cert request to qnetd server
  181. scp "root@$master_node:$DB_DIR_NODE/$CRQ_FILE_BASE" "root@$qnetd_addr:/tmp"
  182. # Sign and export cluster certificate
  183. ssh "root@$qnetd_addr" "$QNETD_CERTUTIL_CMD -s -c \"/tmp/$CRQ_FILE_BASE\" -n \"$CLUSTER_NAME\""
  184. # Copy exported CRT to master node
  185. scp "root@$qnetd_addr:$DB_DIR_QNETD/cluster-$CLUSTER_NAME.crt" "root@$master_node:$DB_DIR_NODE"
  186. # Import certificate
  187. ssh "root@$master_node" "$0 -M -c \"$DB_DIR_NODE/cluster-$CLUSTER_NAME.crt\""
  188. # Copy pk12 cert to all nodes and import it
  189. for node in $other_nodes;do
  190. scp "root@$master_node:$DB_DIR_NODE/$P12_FILE" "$node:$DB_DIR_NODE/$P12_FILE"
  191. ssh "root@$node" "$0 -m -c \"$DB_DIR_NODE/$P12_FILE\""
  192. done
  193. }
  194. OPERATION=""
  195. CERTIFICATE_FILE=""
  196. CLUSTER_NAME=""
  197. while getopts ":hiMmQrc:n:" opt; do
  198. case $opt in
  199. r)
  200. OPERATION=gen_cluster_cert_req
  201. ;;
  202. i)
  203. OPERATION=init_node_ca
  204. ;;
  205. m)
  206. OPERATION=import_pk12
  207. ;;
  208. M)
  209. OPERATION=import_signed_cert
  210. ;;
  211. Q)
  212. OPERATION=quick_start
  213. ;;
  214. n)
  215. CLUSTER_NAME="$OPTARG"
  216. ;;
  217. h)
  218. usage
  219. ;;
  220. c)
  221. CERTIFICATE_FILE="$OPTARG"
  222. ;;
  223. \?)
  224. echo "Invalid option: -$OPTARG" >&2
  225. exit 1
  226. ;;
  227. :)
  228. echo "Option -$OPTARG requires an argument." >&2
  229. exit 1
  230. ;;
  231. esac
  232. done
  233. case "$OPERATION" in
  234. "init_qnetd_ca")
  235. DB_DIR="$DB_DIR_QNETD"
  236. ;;
  237. "init_node_ca")
  238. DB_DIR="$DB_DIR_NODE"
  239. ;;
  240. "gen_cluster_cert_req")
  241. DB_DIR="$DB_DIR_NODE"
  242. ;;
  243. "sign_cluster_cert")
  244. DB_DIR="$DB_DIR_QNETD"
  245. ;;
  246. "import_signed_cert")
  247. DB_DIR="$DB_DIR_NODE"
  248. ;;
  249. "import_pk12")
  250. DB_DIR="$DB_DIR_NODE"
  251. ;;
  252. "quick_start")
  253. DB_DIR=""
  254. ;;
  255. *)
  256. usage
  257. ;;
  258. esac
  259. PWD_FILE="$DB_DIR/$PWD_FILE_BASE"
  260. NOISE_FILE="$DB_DIR/$NOISE_FILE_BASE"
  261. SERIAL_NO_FILE="$DB_DIR/$SERIAL_NO_FILE_BASE"
  262. CRQ_FILE="$DB_DIR/$CRQ_FILE_BASE"
  263. CRT_FILE="$DB_DIR/cluster-$CLUSTER_NAME.crt"
  264. P12_FILE="$DB_DIR/$P12_FILE_BASE"
  265. case "$OPERATION" in
  266. "init_qnetd_ca")
  267. init_qnetd_ca
  268. ;;
  269. "init_node_ca")
  270. if ! [ -e "$CERTIFICATE_FILE" ];then
  271. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  272. exit 2
  273. fi
  274. init_node_ca
  275. ;;
  276. "gen_cluster_cert_req")
  277. if [ "$CLUSTER_NAME" == "" ];then
  278. echo "You have to specify cluster name" >&2
  279. exit 2
  280. fi
  281. gen_cluster_cert_req
  282. ;;
  283. "sign_cluster_cert")
  284. if ! [ -e "$CERTIFICATE_FILE" ];then
  285. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  286. exit 2
  287. fi
  288. if [ "$CLUSTER_NAME" == "" ];then
  289. echo "You have to specify cluster name" >&2
  290. exit 2
  291. fi
  292. sign_cluster_cert
  293. ;;
  294. "import_signed_cert")
  295. if ! [ -e "$CERTIFICATE_FILE" ];then
  296. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  297. exit 2
  298. fi
  299. import_signed_cert
  300. ;;
  301. "import_pk12")
  302. if ! [ -e "$CERTIFICATE_FILE" ];then
  303. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  304. exit 2
  305. fi
  306. import_pk12
  307. ;;
  308. "quick_start")
  309. shift $((OPTIND-1))
  310. qnetd_addr="$1"
  311. shift 1
  312. master_node="$1"
  313. shift 1
  314. other_nodes="$@"
  315. if [ "$CLUSTER_NAME" == "" ];then
  316. echo "You have to specify cluster name" >&2
  317. exit 2
  318. fi
  319. if [ "$qnetd_addr" == "" ];then
  320. echo "No QNetd server address provided." >&2
  321. exit 2
  322. fi
  323. if [ "$master_node" == "" ];then
  324. echo "No nodes provided." >&2
  325. exit 2
  326. fi
  327. quick_start "$qnetd_addr" "$master_node" "$other_nodes"
  328. ;;
  329. *)
  330. usage
  331. ;;
  332. esac