corosync-qnetd-certutil.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. CLUSTER_NICKNAME="Cluster Cert"
  42. CA_SUBJECT="CN=QNet CA"
  43. SERVER_SUBJECT="CN=Qnetd Server"
  44. PWD_FILE="$DB_DIR/pwdfile.txt"
  45. NOISE_FILE="$DB_DIR/noise.txt"
  46. SERIAL_NO_FILE="$DB_DIR/serial.txt"
  47. CA_EXPORT_FILE="$DB_DIR/qnetd-cacert.crt"
  48. CRT_FILE_BASE="" # Generated from cluster name
  49. usage() {
  50. echo "$0: [-i|-s] [-c certificate]"
  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 600 "$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 600 "$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. init_qnetd_ca() {
  87. if [ -f "$DB_DIR/cert8.db" ];then
  88. echo "Certificate database ($DB_DIR) already exists. Delete it to initialize new db" >&2
  89. exit 1
  90. fi
  91. if ! [ -d "$DB_DIR" ];then
  92. echo "Creating $DB_DIR"
  93. mkdir -p "$DB_DIR"
  94. chown_ref_cfgdir "$DB_DIR"
  95. chmod 700 "$DB_DIR"
  96. fi
  97. echo "Creating new key and cert db"
  98. echo -n "" > "$PWD_FILE"
  99. chown_ref_cfgdir "$PWD_FILE"
  100. chmod 600 "$PWD_FILE"
  101. certutil -N -d "$DB_DIR" -f "$PWD_FILE"
  102. chown_ref_cfgdir "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  103. chmod 600 "$DB_DIR/key3.db" "$DB_DIR/cert8.db" "$DB_DIR/secmod.db"
  104. create_new_noise_file "$NOISE_FILE"
  105. echo "Creating new CA"
  106. # Create self-signed certificate (CA). Asks 3 questions (is this CA, lifetime and critical extension
  107. echo -e "y\n0\ny\n" | certutil -S -n "$CA_NICKNAME" -s "$CA_SUBJECT" -x \
  108. -t "CT,," -m `get_serial_no` -v $CRT_VALIDITY -d "$DB_DIR" \
  109. -z "$NOISE_FILE" -f "$PWD_FILE" -2
  110. # Export CA certificate in ascii
  111. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" > "$CA_EXPORT_FILE"
  112. certutil -L -d "$DB_DIR" -n "$CA_NICKNAME" -a >> "$CA_EXPORT_FILE"
  113. chown_ref_cfgdir "$CA_EXPORT_FILE"
  114. certutil -S -n "$SERVER_NICKNAME" -s "$SERVER_SUBJECT" -c "$CA_NICKNAME" -t "u,u,u" -m `get_serial_no` \
  115. -v $CRT_VALIDITY -d "$DB_DIR" -z "$NOISE_FILE" -f "$PWD_FILE"
  116. echo "QNetd CA certificate is exported as $CA_EXPORT_FILE"
  117. }
  118. sign_cluster_cert() {
  119. if ! [ -f "$DB_DIR/cert8.db" ];then
  120. echo "Certificate database doesn't exists. Use $0 -I to create it" >&2
  121. exit 1
  122. fi
  123. echo "Signing cluster certificate"
  124. certutil -C -v "$CRT_VALIDITY" -m `get_serial_no` -i "$CERTIFICATE_FILE" -o "$CRT_FILE" -c "$CA_NICKNAME" -d "$DB_DIR"
  125. chown_ref_cfgdir "$CRT_FILE"
  126. echo "Certificate stored in $CRT_FILE"
  127. }
  128. OPERATION=""
  129. CERTIFICATE_FILE=""
  130. CLUSTER_NAME=""
  131. while getopts ":hisc:n:" opt; do
  132. case $opt in
  133. i)
  134. OPERATION=init_qnetd_ca
  135. ;;
  136. s)
  137. OPERATION=sign_cluster_cert
  138. ;;
  139. h)
  140. usage
  141. ;;
  142. c)
  143. CERTIFICATE_FILE="$OPTARG"
  144. ;;
  145. n)
  146. CLUSTER_NAME="$OPTARG"
  147. ;;
  148. \?)
  149. echo "Invalid option: -$OPTARG" >&2
  150. exit 1
  151. ;;
  152. :)
  153. echo "Option -$OPTARG requires an argument." >&2
  154. exit 1
  155. ;;
  156. esac
  157. done
  158. [ "$OPERATION" == "" ] && usage
  159. CRT_FILE="$DB_DIR/cluster-$CLUSTER_NAME.crt"
  160. case "$OPERATION" in
  161. "init_qnetd_ca")
  162. init_qnetd_ca
  163. ;;
  164. "sign_cluster_cert")
  165. if ! [ -e "$CERTIFICATE_FILE" ];then
  166. echo "Can't open certificate file $CERTIFICATE_FILE" >&2
  167. exit 2
  168. fi
  169. if [ "$CLUSTER_NAME" == "" ];then
  170. echo "You have to specify cluster name" >&2
  171. exit 2
  172. fi
  173. sign_cluster_cert
  174. ;;
  175. *)
  176. usage
  177. ;;
  178. esac