corosync-qnetd-certutil.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!@BASHPATH@
  2. #
  3. # Copyright (c) 2015-2018 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. 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. CERTDB_FILES=("cert9.db key4.db pkcs11.txt"
  48. "cert8.db key3.db secmod.db")
  49. usage() {
  50. echo "$0: [-i|-s] [-c certificate] [-n cluster_name]"
  51. echo
  52. echo " -i Initialize QNetd CA and generate server certificate"
  53. echo " -s Sign cluster certificate (needs cluster certificate)"
  54. echo " -c certificate CRQ certificate file name"
  55. echo " -n cluster_name Name of cluster (for -s operation)"
  56. exit 0
  57. }
  58. chown_ref_cfgdir() {
  59. if [ "$UID" == "0" ];then
  60. chown --reference="$CONFIG_DIR" "$@" 2>/dev/null || chown "$(stat -f "%u:%g" "$CONFIG_DIR")" "$@" 2>/dev/null || return $?
  61. fi
  62. }
  63. create_new_noise_file() {
  64. local noise_file="$1"
  65. if [ ! -e "$noise_file" ];then
  66. echo "Creating new noise file $noise_file"
  67. (ps -elf; date; w) | sha1sum | (read sha_sum rest; echo $sha_sum) > "$noise_file"
  68. chown_ref_cfgdir "$noise_file"
  69. chmod 0660 "$noise_file"
  70. else
  71. echo "Using existing noise file $noise_file"
  72. fi
  73. }
  74. get_serial_no() {
  75. local serial_no
  76. if ! [ -f "$SERIAL_NO_FILE" ];then
  77. echo "100" > $SERIAL_NO_FILE
  78. chown_ref_cfgdir "$SERIAL_NO_FILE"
  79. chmod 0660 "$SERIAL_NO_FILE"
  80. fi
  81. serial_no=`cat $SERIAL_NO_FILE`
  82. serial_no=$((serial_no+1))
  83. echo "$serial_no" > $SERIAL_NO_FILE
  84. echo "$serial_no"
  85. }
  86. find_certdb_files() {
  87. for cert_files_index in ${!CERTDB_FILES[@]};do
  88. cert_files=${CERTDB_FILES[$cert_files_index]}
  89. test_file=${cert_files%% *}
  90. if [ -f "$DB_DIR/$test_file" ];then
  91. echo "$cert_files"
  92. return 0
  93. fi
  94. done
  95. return 1
  96. }
  97. init_qnetd_ca() {
  98. cert_files=`find_certdb_files`
  99. if [ "$cert_files" != "" ];then
  100. echo "Certificate database ($DB_DIR) already exists. Delete it to initialize new db" >&2
  101. exit 1
  102. fi
  103. if ! [ -d "$DB_DIR" ];then
  104. echo "Creating $DB_DIR"
  105. mkdir -p "$DB_DIR"
  106. chown_ref_cfgdir "$DB_DIR"
  107. chmod 0770 "$DB_DIR"
  108. fi
  109. echo "Creating new key and cert db"
  110. echo -n "" > "$PWD_FILE"
  111. chown_ref_cfgdir "$PWD_FILE"
  112. chmod 0660 "$PWD_FILE"
  113. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  114. cert_files=`find_certdb_files`
  115. if [ "$cert_files" == "" ];then
  116. echo "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created" >&2
  117. exit 1
  118. fi
  119. for fname in $cert_files;do
  120. chown_ref_cfgdir "$DB_DIR/$fname"
  121. chmod 0660 "$DB_DIR/$fname"
  122. done
  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. chown_ref_cfgdir "$CA_EXPORT_FILE"
  133. certutil -S -n "$SERVER_NICKNAME" -s "$SERVER_SUBJECT" -c "$CA_NICKNAME" -t "u,u,u" -m "$(get_serial_no)" \
  134. -v $CRT_VALIDITY -d "$DB_DIR" -z "$NOISE_FILE" -f "$PWD_FILE"
  135. echo "QNetd CA certificate is exported as $CA_EXPORT_FILE"
  136. }
  137. sign_cluster_cert() {
  138. cert_files=`find_certdb_files`
  139. if [ "$cert_files" == "" ];then
  140. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  141. exit 1
  142. fi
  143. echo "Signing cluster certificate"
  144. certutil -C -v "$CRT_VALIDITY" -m "$(get_serial_no)" -i "$CERTIFICATE_FILE" -o "$CRT_FILE" -c "$CA_NICKNAME" -d "$DB_DIR"
  145. chown_ref_cfgdir "$CRT_FILE"
  146. echo "Certificate stored in $CRT_FILE"
  147. }
  148. OPERATION=""
  149. CERTIFICATE_FILE=""
  150. CLUSTER_NAME=""
  151. while getopts ":hisc:n:" opt; do
  152. case $opt in
  153. i)
  154. OPERATION=init_qnetd_ca
  155. ;;
  156. s)
  157. OPERATION=sign_cluster_cert
  158. ;;
  159. h)
  160. usage
  161. ;;
  162. c)
  163. CERTIFICATE_FILE="$OPTARG"
  164. ;;
  165. n)
  166. CLUSTER_NAME="$OPTARG"
  167. ;;
  168. \?)
  169. echo "Invalid option: -$OPTARG" >&2
  170. exit 1
  171. ;;
  172. :)
  173. echo "Option -$OPTARG requires an argument." >&2
  174. exit 1
  175. ;;
  176. esac
  177. done
  178. [ "$OPERATION" == "" ] && usage
  179. CRT_FILE="$DB_DIR/cluster-$CLUSTER_NAME.crt"
  180. case "$OPERATION" in
  181. "init_qnetd_ca")
  182. init_qnetd_ca
  183. ;;
  184. "sign_cluster_cert")
  185. if ! [ -e "$CERTIFICATE_FILE" ];then
  186. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  187. exit 2
  188. fi
  189. if [ "$CLUSTER_NAME" == "" ];then
  190. echo "You have to specify cluster name" >&2
  191. exit 2
  192. fi
  193. sign_cluster_cert
  194. ;;
  195. *)
  196. usage
  197. ;;
  198. esac