4
0

corosync-qnetd-certutil.sh 6.2 KB

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