corosync-qnetd-certutil.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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] [-G] [-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 " -G Do not set group write bit for new files"
  56. echo " -n cluster_name Name of cluster (for -s operation)"
  57. exit 0
  58. }
  59. chown_ref_cfgdir() {
  60. if [ "$UID" == "0" ];then
  61. chown --reference="$CONFIG_DIR" "$@" 2>/dev/null || chown "$(stat -f "%u:%g" "$CONFIG_DIR")" "$@" 2>/dev/null || return $?
  62. fi
  63. }
  64. # get_perm [directory]
  65. # Return permission based on -G and directory flag
  66. get_perm() {
  67. if [ "$1" == true ];then
  68. [ "$SET_GROUP_WRITE_BIT" == true ] && echo "0770" || echo "0750"
  69. else
  70. [ "$SET_GROUP_WRITE_BIT" == true ] && echo "0660" || echo "0640"
  71. fi
  72. }
  73. create_new_noise_file() {
  74. local noise_file="$1"
  75. if [ ! -e "$noise_file" ];then
  76. echo "Creating new noise file $noise_file"
  77. (ps -elf; date; w) | sha1sum | (read sha_sum rest; echo $sha_sum) > "$noise_file"
  78. chown_ref_cfgdir "$noise_file"
  79. chmod `get_perm` "$noise_file"
  80. else
  81. echo "Using existing noise file $noise_file"
  82. fi
  83. }
  84. get_serial_no() {
  85. local serial_no
  86. if ! [ -f "$SERIAL_NO_FILE" ];then
  87. echo "100" > $SERIAL_NO_FILE
  88. chown_ref_cfgdir "$SERIAL_NO_FILE"
  89. chmod `get_perm` "$SERIAL_NO_FILE"
  90. fi
  91. serial_no=`cat $SERIAL_NO_FILE`
  92. serial_no=$((serial_no+1))
  93. echo "$serial_no" > $SERIAL_NO_FILE
  94. echo "$serial_no"
  95. }
  96. find_certdb_files() {
  97. for cert_files_index in ${!CERTDB_FILES[@]};do
  98. cert_files=${CERTDB_FILES[$cert_files_index]}
  99. test_file=${cert_files%% *}
  100. if [ -f "$DB_DIR/$test_file" ];then
  101. echo "$cert_files"
  102. return 0
  103. fi
  104. done
  105. return 1
  106. }
  107. init_qnetd_ca() {
  108. cert_files=`find_certdb_files`
  109. if [ "$cert_files" != "" ];then
  110. echo "Certificate database ($DB_DIR) already exists. Delete it to initialize new db" >&2
  111. exit 1
  112. fi
  113. if ! [ -d "$DB_DIR" ];then
  114. echo "Creating $DB_DIR"
  115. mkdir -p "$DB_DIR"
  116. chown_ref_cfgdir "$DB_DIR"
  117. chmod `get_perm true` "$DB_DIR"
  118. fi
  119. echo "Creating new key and cert db"
  120. echo -n "" > "$PWD_FILE"
  121. chown_ref_cfgdir "$PWD_FILE"
  122. chmod `get_perm` "$PWD_FILE"
  123. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  124. cert_files=`find_certdb_files`
  125. if [ "$cert_files" == "" ];then
  126. echo "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created" >&2
  127. exit 1
  128. fi
  129. for fname in $cert_files;do
  130. chown_ref_cfgdir "$DB_DIR/$fname"
  131. chmod `get_perm` "$DB_DIR/$fname"
  132. done
  133. create_new_noise_file "$NOISE_FILE"
  134. echo "Creating new CA"
  135. # Create self-signed certificate (CA). Asks 3 questions (is this CA, lifetime and critical extension
  136. echo -e "y\n0\ny\n" | certutil -S -n "$CA_NICKNAME" -s "$CA_SUBJECT" -x \
  137. -t "CT,," -m "$(get_serial_no)" -v $CRT_VALIDITY -d "$DB_DIR" \
  138. -z "$NOISE_FILE" -f "$PWD_FILE" -2
  139. # Export CA certificate in ascii
  140. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" > "$CA_EXPORT_FILE"
  141. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" -a >> "$CA_EXPORT_FILE"
  142. chown_ref_cfgdir "$CA_EXPORT_FILE"
  143. certutil -S -n "$SERVER_NICKNAME" -s "$SERVER_SUBJECT" -c "$CA_NICKNAME" -t "u,u,u" -m "$(get_serial_no)" \
  144. -v $CRT_VALIDITY -d "$DB_DIR" -z "$NOISE_FILE" -f "$PWD_FILE"
  145. echo "QNetd CA certificate is exported as $CA_EXPORT_FILE"
  146. }
  147. sign_cluster_cert() {
  148. cert_files=`find_certdb_files`
  149. if [ "$cert_files" == "" ];then
  150. echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
  151. exit 1
  152. fi
  153. echo "Signing cluster certificate"
  154. certutil -C -v "$CRT_VALIDITY" -m "$(get_serial_no)" -i "$CERTIFICATE_FILE" -o "$CRT_FILE" -c "$CA_NICKNAME" -d "$DB_DIR"
  155. chown_ref_cfgdir "$CRT_FILE"
  156. echo "Certificate stored in $CRT_FILE"
  157. }
  158. OPERATION=""
  159. CERTIFICATE_FILE=""
  160. CLUSTER_NAME=""
  161. SET_GROUP_WRITE_BIT=true
  162. while getopts ":Ghisc:n:" opt; do
  163. case $opt in
  164. i)
  165. OPERATION=init_qnetd_ca
  166. ;;
  167. s)
  168. OPERATION=sign_cluster_cert
  169. ;;
  170. h)
  171. usage
  172. ;;
  173. c)
  174. CERTIFICATE_FILE="$OPTARG"
  175. ;;
  176. G)
  177. SET_GROUP_WRITE_BIT=false
  178. ;;
  179. n)
  180. CLUSTER_NAME="$OPTARG"
  181. ;;
  182. \?)
  183. echo "Invalid option: -$OPTARG" >&2
  184. exit 1
  185. ;;
  186. :)
  187. echo "Option -$OPTARG requires an argument." >&2
  188. exit 1
  189. ;;
  190. esac
  191. done
  192. [ "$OPERATION" == "" ] && usage
  193. CRT_FILE="$DB_DIR/cluster-$CLUSTER_NAME.crt"
  194. case "$OPERATION" in
  195. "init_qnetd_ca")
  196. init_qnetd_ca
  197. ;;
  198. "sign_cluster_cert")
  199. if ! [ -e "$CERTIFICATE_FILE" ];then
  200. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  201. exit 2
  202. fi
  203. if [ "$CLUSTER_NAME" == "" ];then
  204. echo "You have to specify cluster name" >&2
  205. exit 2
  206. fi
  207. sign_cluster_cert
  208. ;;
  209. *)
  210. usage
  211. ;;
  212. esac