corosync-qdevice-net-certutil.sh 12 KB

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