corosync-qnetd-certutil.sh 13 KB

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