corosync-qnetd-certutil.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!@BASHPATH@
  2. #
  3. # Copyright (c) 2015-2026 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. CONFIG_DIR="@COROSYSCONFDIR@/qnetd"
  36. DB_DIR="$CONFIG_DIR/nssdb"
  37. # Validity of certificate (months)
  38. CRT_VALIDITY=1200
  39. CA_NICKNAME="QNet CA"
  40. SERVER_NICKNAME="QNetd Cert"
  41. SERVER_SUBJECT="CN=Qnetd Server"
  42. PWD_FILE="$DB_DIR/pwdfile.txt"
  43. NOISE_FILE="$DB_DIR/noise.txt"
  44. SERIAL_NO_FILE="$DB_DIR/serial.txt"
  45. CA_EXPORT_FILE="$DB_DIR/qnetd-cacert.crt"
  46. CERTDB_FILES=("cert9.db key4.db pkcs11.txt"
  47. "cert8.db key3.db secmod.db")
  48. # errx exit_code message
  49. errx() {
  50. echo "$2" >&2
  51. exit "$1"
  52. }
  53. usage() {
  54. echo "$0: [-I|-i|-s] [-C ca_nickname] [-c certificate] [-G] [-g keysize] [-k keytype] [-n cluster_name] [-q paramset] [-S serv_nickname]"
  55. echo
  56. echo " -I Generate and add another QNetd CA and new server certificate"
  57. echo " -i Initialize QNetd CA and generate server certificate"
  58. echo " -s Sign cluster certificate (needs cluster certificate)"
  59. echo " -C ca_nickname NSS database QNetd CA certificate nickname"
  60. echo " -c certificate CRQ certificate file name"
  61. echo " -G Do not set group write bit for new files"
  62. echo " -g keysize Key size in bits - passed directly to certutil as -g parameter"
  63. echo " -k keytype Type of key - passed directly to certutil as -k parameter"
  64. echo " -n cluster_name Name of cluster (for -s operation)"
  65. echo " -q paramset Parameter set (curve name, ml-dsa set) - passed directly to certutil as -q parameter"
  66. echo " -S serv_nickname NSS database QNetd server certificate nickname"
  67. exit 0
  68. }
  69. chown_ref_cfgdir() {
  70. if [ "$UID" == "0" ];then
  71. chown --reference="$CONFIG_DIR" "$@" 2>/dev/null || chown "$(stat -f "%u:%g" "$CONFIG_DIR")" "$@" 2>/dev/null || return $?
  72. fi
  73. }
  74. # get_perm [directory]
  75. # Return permission based on -G and directory flag
  76. get_perm() {
  77. directory=${1:-}
  78. if [ "$directory" == true ];then
  79. [ "$SET_GROUP_WRITE_BIT" == true ] && echo "0770" || echo "0750"
  80. else
  81. [ "$SET_GROUP_WRITE_BIT" == true ] && echo "0660" || echo "0640"
  82. fi
  83. }
  84. create_new_noise_file() {
  85. local noise_file="$1"
  86. if [ ! -e "$noise_file" ];then
  87. echo "Creating new noise file $noise_file"
  88. (ps -elf; date; w) | sha1sum | (read sha_sum rest; echo $sha_sum) > "$noise_file"
  89. chown_ref_cfgdir "$noise_file"
  90. chmod "$(get_perm)" "$noise_file"
  91. else
  92. echo "Using existing noise file $noise_file"
  93. fi
  94. }
  95. get_serial_no() {
  96. local serial_no
  97. if ! [ -f "$SERIAL_NO_FILE" ];then
  98. echo "100" > $SERIAL_NO_FILE
  99. chown_ref_cfgdir "$SERIAL_NO_FILE"
  100. chmod "$(get_perm)" "$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. find_certdb_files() {
  108. for cert_files_index in "${!CERTDB_FILES[@]}";do
  109. cert_files=${CERTDB_FILES[$cert_files_index]}
  110. test_file=${cert_files%% *}
  111. if [ -f "$DB_DIR/$test_file" ];then
  112. echo "$cert_files"
  113. return 0
  114. fi
  115. done
  116. }
  117. get_certutil_key_params() {
  118. CERTUTIL_PARAMS=""
  119. if [ ! -z "$COROSYNC_QNETD_CERTUTIL_KEY_SIZE" ];then
  120. CERTUTIL_PARAMS="$CERTUTIL_PARAMS -g $COROSYNC_QNETD_CERTUTIL_KEY_SIZE"
  121. fi
  122. if [ ! -z "$COROSYNC_QNETD_CERTUTIL_KEY_TYPE" ];then
  123. CERTUTIL_PARAMS="$CERTUTIL_PARAMS -k $COROSYNC_QNETD_CERTUTIL_KEY_TYPE"
  124. fi
  125. if [ ! -z "$COROSYNC_QNETD_CERTUTIL_PARAMSET" ];then
  126. CERTUTIL_PARAMS="$CERTUTIL_PARAMS -q $COROSYNC_QNETD_CERTUTIL_PARAMSET"
  127. fi
  128. echo "$CERTUTIL_PARAMS"
  129. }
  130. nss_nickname_exists() {
  131. certutil -L -d "$DB_DIR" -n "$1" >/dev/null 2>&1
  132. }
  133. add_qnetd_ca() {
  134. cert_files=`find_certdb_files`
  135. if [ "$cert_files" == "" ];then
  136. errx 1 "Certificate database doesn't exist. Use $0 -i to create it"
  137. fi
  138. if nss_nickname_exists "$CA_NICKNAME";then
  139. errx 1 "Certificate nickname \"$CA_NICKNAME\" already exists in the database."
  140. fi
  141. if nss_nickname_exists "$SERVER_NICKNAME";then
  142. errx 1 "Certificate nickname \"$SERVER_NICKNAME\" already exists in the database."
  143. fi
  144. echo "Creating new CA"
  145. # Create self-signed certificate (CA). Asks 3 questions (is this CA, lifetime and critical extension
  146. echo -e "y\n0\ny\n" | certutil -S -n "$CA_NICKNAME" -s "$CA_SUBJECT" -x \
  147. -t "CT,," -m "$(get_serial_no)" -v $CRT_VALIDITY -d "$DB_DIR" \
  148. -z "$NOISE_FILE" -f "$PWD_FILE" -2 $(get_certutil_key_params)
  149. # Export CA certificate in ascii
  150. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" > "$CA_EXPORT_FILE"
  151. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" -a >> "$CA_EXPORT_FILE"
  152. chown_ref_cfgdir "$CA_EXPORT_FILE"
  153. certutil -S -n "$SERVER_NICKNAME" -s "$SERVER_SUBJECT" -c "$CA_NICKNAME" -t "u,u,u" -m "$(get_serial_no)" \
  154. -v $CRT_VALIDITY -d "$DB_DIR" -z "$NOISE_FILE" -f "$PWD_FILE" $(get_certutil_key_params)
  155. echo "QNetd CA certificate is exported as $CA_EXPORT_FILE"
  156. }
  157. init_qnetd_ca() {
  158. cert_files=`find_certdb_files`
  159. if [ "$cert_files" != "" ];then
  160. errx 1 "Certificate database ($DB_DIR) already exists. Delete it to initialize new db"
  161. fi
  162. if ! [ -d "$DB_DIR" ];then
  163. echo "Creating $DB_DIR"
  164. mkdir -p "$DB_DIR"
  165. chown_ref_cfgdir "$DB_DIR"
  166. chmod "$(get_perm true)" "$DB_DIR"
  167. fi
  168. echo "Creating new key and cert db"
  169. echo -n "" > "$PWD_FILE"
  170. chown_ref_cfgdir "$PWD_FILE"
  171. chmod "$(get_perm)" "$PWD_FILE"
  172. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  173. cert_files=`find_certdb_files`
  174. if [ "$cert_files" == "" ];then
  175. errx 1 "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created"
  176. fi
  177. for fname in $cert_files;do
  178. chown_ref_cfgdir "$DB_DIR/$fname"
  179. chmod "$(get_perm)" "$DB_DIR/$fname"
  180. done
  181. create_new_noise_file "$NOISE_FILE"
  182. add_qnetd_ca
  183. }
  184. sign_cluster_cert() {
  185. cert_files=`find_certdb_files`
  186. if [ "$cert_files" == "" ];then
  187. errx 1 "Certificate database doesn't exists. Use $0 -i to create it"
  188. fi
  189. echo "Signing cluster certificate"
  190. certutil -C -v "$CRT_VALIDITY" -m "$(get_serial_no)" -i "$CERTIFICATE_FILE" -o "$CRT_FILE" -c "$CA_NICKNAME" -d "$DB_DIR"
  191. chown_ref_cfgdir "$CRT_FILE"
  192. echo "Certificate stored in $CRT_FILE"
  193. }
  194. # Initialize options that may be overwritten by the configuration file
  195. COROSYNC_QNETD_CERTUTIL_KEY_SIZE=""
  196. COROSYNC_QNETD_CERTUTIL_KEY_TYPE=""
  197. COROSYNC_QNETD_CERTUTIL_PARAMSET=""
  198. # Import configuration file if it exists
  199. if [ -f "@INITCONFIGDIR@/corosync-qnetd" ];then
  200. . "@INITCONFIGDIR@/corosync-qnetd"
  201. fi
  202. # Strict mode
  203. set -euo pipefail
  204. trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
  205. OPERATION=""
  206. CERTIFICATE_FILE=""
  207. CLUSTER_NAME=""
  208. SET_GROUP_WRITE_BIT=true
  209. while getopts ":IGhisC:c:g:k:n:q:S:" opt; do
  210. case $opt in
  211. I)
  212. OPERATION=add_qnetd_ca
  213. ;;
  214. i)
  215. OPERATION=init_qnetd_ca
  216. ;;
  217. s)
  218. OPERATION=sign_cluster_cert
  219. ;;
  220. h)
  221. usage
  222. ;;
  223. C)
  224. CA_NICKNAME="$OPTARG"
  225. CA_EXPORT_FILE="$DB_DIR/qnetd-cacert-$CA_NICKNAME.crt"
  226. ;;
  227. c)
  228. CERTIFICATE_FILE="$OPTARG"
  229. ;;
  230. G)
  231. SET_GROUP_WRITE_BIT=false
  232. ;;
  233. g)
  234. COROSYNC_QNETD_CERTUTIL_KEY_SIZE="$OPTARG"
  235. ;;
  236. k)
  237. COROSYNC_QNETD_CERTUTIL_KEY_TYPE="$OPTARG"
  238. ;;
  239. n)
  240. CLUSTER_NAME="$OPTARG"
  241. ;;
  242. q)
  243. COROSYNC_QNETD_CERTUTIL_PARAMSET="$OPTARG"
  244. ;;
  245. S)
  246. SERVER_NICKNAME="$OPTARG"
  247. ;;
  248. \?)
  249. errx 1 "Invalid option: -$OPTARG"
  250. ;;
  251. :)
  252. errx 1 "Option -$OPTARG requires an argument."
  253. ;;
  254. esac
  255. done
  256. [ "$OPERATION" == "" ] && usage
  257. CRT_FILE="$DB_DIR/cluster-$CLUSTER_NAME.crt"
  258. CA_SUBJECT="CN=$CA_NICKNAME"
  259. case "$OPERATION" in
  260. "add_qnetd_ca")
  261. add_qnetd_ca
  262. ;;
  263. "init_qnetd_ca")
  264. init_qnetd_ca
  265. ;;
  266. "sign_cluster_cert")
  267. if ! [ -e "$CERTIFICATE_FILE" ];then
  268. errx 2 "Can't open certificate file $CERTIFICATE_FILE"
  269. fi
  270. if [ "$CLUSTER_NAME" == "" ];then
  271. errx 2 "You have to specify cluster name"
  272. fi
  273. sign_cluster_cert
  274. ;;
  275. *)
  276. usage
  277. ;;
  278. esac