2
0

sip-router_cert.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # This script generates a self-signed TLS/SSL certificate that can be
  6. # immediately used with the TLS module of SIP Router. The file was inspired
  7. # by a script from Debian's uw-imapd package.
  8. #
  9. #############################################################################
  10. # Configuration variables
  11. #############################################################################
  12. NAME=$MAIN_NAME
  13. if [ -z "$NAME" ] ; then NAME="sip-router"; fi;
  14. DEFAULT_DIR="/usr/local/etc/$NAME"
  15. DEFAULT_DAYS=365
  16. DEFAULT_INFO="Self-signed certificate for $NAME"
  17. DEFAULT_CERT_FILENAME="$NAME-selfsigned.pem"
  18. DEFAULT_KEY_FILENAME="$NAME-selfsigned.key"
  19. DEFAULT_OPENSSL='openssl'
  20. HOSTNAME=`hostname -s`
  21. if hostname -f >/dev/null 2>/dev/null ; then
  22. FQDN=`hostname -f`
  23. else
  24. FQDN=`hostname`
  25. fi
  26. MAILNAME=`cat /etc/mailname 2> /dev/null || echo $FQDN`
  27. # test if we have the normal or enhanced getopt
  28. getopt -T >/dev/null
  29. if [ $? = 4 ]; then
  30. LONGOPTS_SUPPORTED=1
  31. fi
  32. longopts() {
  33. if [ -z "${LONGOPTS_SUPPORTED}" ]; then
  34. exit;
  35. fi
  36. case "$1" in
  37. -h) echo ', --help';;
  38. -d) echo ', --dir' ;;
  39. -c) echo ', --certificate';;
  40. -k) echo ', --key';;
  41. -e) echo ', --expires';;
  42. -i) echo ', --info';;
  43. -o) echo ', --overwrite' ;;
  44. esac
  45. }
  46. usage() {
  47. cat <<EOF
  48. NAME
  49. $COMMAND - Generate a self-signed TLS/SSL certificate for use with $NAME.
  50. SYNOPSIS
  51. $COMMAND [options]
  52. DESCRIPTION
  53. This is a simple shell script that generates a self signed TLS/SSL
  54. certificate (and private key) for use with the tls module of $NAME. The
  55. self-signed certificate is suitable for testing and/or private setups.
  56. You are encouraged to create a proper authorized one if needed.
  57. Both certificate and key files are by default stored in the directory
  58. containing the configuration file of $NAME (unless you change it using
  59. the options below).
  60. OPTIONS
  61. -h`longopts -h`
  62. Display this help text.
  63. -d`longopts -d`
  64. The path to the directory where cert and key files will be stored.
  65. (Default value is '$DEFAULT_DIR')
  66. -c`longopts -c`
  67. The name of the file where the certificate will be stored.
  68. (Default value is '$DEFAULT_CERT_FILENAME')
  69. -k`longopts -k`
  70. The name of the file where the private key will be stored.
  71. (Default value is '$DEFAULT_KEY_FILENAME')
  72. -e`longopts -e`
  73. Number of days for which the certificate will be valid.
  74. (Default value is '$DEFAULT_DAYS')
  75. -i`longopts -i`
  76. The description text to be embedded in the certificate.
  77. (Default value is '$DEFAULT_INFO')
  78. -o`longopts -o`
  79. Overwrite certificate and key files if they exist already.
  80. (By default they will be not overwritten.)
  81. ENVIRONMENT VARIABLES
  82. OPENSSL Path to openssl command (Currently ${OPENSSL})
  83. AUTHOR
  84. Written by Jan Janak <[email protected]>
  85. REPORTING BUGS
  86. Report bugs to <[email protected]>
  87. EOF
  88. } #usage
  89. COMMAND=`basename $0`
  90. if [ -z "$DIR" ] ; then DIR=$DEFAULT_DIR; fi;
  91. if [ -z "$DAYS" ] ; then DAYS=$DEFAULT_DAYS; fi;
  92. if [ -z "$INFO" ] ; then INFO=$DEFAULT_INFO; fi;
  93. if [ -z "$CERT_FILENAME" ] ; then CERT_FILENAME=$DEFAULT_CERT_FILENAME; fi;
  94. if [ -z "$KEY_FILENAME" ] ; then KEY_FILENAME=$DEFAULT_KEY_FILENAME; fi;
  95. if [ -z "$OPENSSL" ] ; then OPENSSL=$DEFAULT_OPENSSL; fi;
  96. if [ -n "${LONGOPTS_SUPPORTED}" ]; then
  97. # enhanced version
  98. TEMP=`getopt -o hd:c:k:e:i:o --long help,dir:,certificate:,key:,expires:,info:,overwrite -n $COMMAND -- "$@"`
  99. else
  100. # basic version
  101. TEMP=`getopt hd:c:k:e:i:o "$@"`
  102. fi
  103. if [ $? != 0 ] ; then exit 1; fi
  104. eval set -- "$TEMP"
  105. while true ; do
  106. case "$1" in
  107. -h|--help) usage; exit 0 ;;
  108. -d|--dir) DIR=$2; shift 2 ;;
  109. -c|--certificate) CERT_FILENAME=$2; shift 2 ;;
  110. -k|--key) KEY_FILENAME=$2; shift 2 ;;
  111. -e|--expires) DAYS=$2; shift 2 ;;
  112. -i|--info) INFO=$2; shift 2 ;;
  113. -o|--overwrite) OVERWRITE=1; shift ;;
  114. --) shift; break ;;
  115. *) echo "Internal error"; exit 1 ;;
  116. esac
  117. done
  118. TEMP=`which $OPENSSL`
  119. if [ $? != 0 ] ; then
  120. echo "Could not find openssl command"
  121. echo "Set OPENSSL environment variable properly (see -h for more info)"
  122. exit 1
  123. fi
  124. if [ ! -d "$DIR" ] ; then
  125. echo "Directory '$DIR' does not exist."
  126. exit 1
  127. fi
  128. if [ -z "$OVERWRITE" -a \( -f "$DIR/$CERT_FILENAME" \) ] ; then
  129. echo "File '$DIR/$CERT_FILENAME' already exists, doing nothing."
  130. echo "(Use -o to override)"
  131. exit 0;
  132. fi
  133. if [ -z "$OVERWRITE" -a \( -f "$DIR/$KEY_FILENAME" \) ] ; then
  134. echo "File '$DIR/$KEY_FILENAME' already exists, doing nothing."
  135. echo "(Use -o to override)."
  136. exit 0;
  137. fi
  138. touch "$DIR/$CERT_FILENAME" > /dev/null 2>&1
  139. if [ $? != 0 ] ; then
  140. echo "Could not create file '$DIR/$CERT_FILENAME'"
  141. exit 1
  142. fi
  143. touch "$DIR/$KEY_FILENAME" > /dev/null 2>&1
  144. if [ $? != 0 ] ; then
  145. echo "Could not create file '$DIR/$KEY_FILENAME'"
  146. rm -f "$DIR/$CERT_FILE"
  147. exit 1
  148. fi
  149. echo "Creating a new $NAME self-signed certificate for '$FQDN'" \
  150. "valid for $DAYS days."
  151. openssl req -new -x509 -days "$DAYS" -nodes -out "$DIR/$CERT_FILENAME" \
  152. -keyout "$DIR/$KEY_FILENAME" > /dev/null 2>&1 <<+
  153. .
  154. .
  155. .
  156. $INFO
  157. $HOSTNAME
  158. $FQDN
  159. root@$MAILNAME
  160. +
  161. if [ $? != 0 ] ; then
  162. echo "Error while executing openssl command."
  163. rm -f "$DIR/$CERT_FILE" "$DIR/$KEY_FILE"
  164. exit 1;
  165. else
  166. echo "Private key stored in '$DIR/$KEY_FILENAME'."
  167. echo "Certificate stored in '$DIR/$CERT_FILENAME'."
  168. exit 0;
  169. fi