corosync-qnetd-certutil.sh 5.9 KB

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