summaryrefslogtreecommitdiff
path: root/localmksslcerts
blob: 75538681a6e22628a98e31eec1085031dffed624 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/localmksslcerts
  4. # Copyright 2001-2004 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmksslcerts,v 1.13 2004-12-28 20:56:20 jonas Exp $
  7. #
  8. # Generate certificates for mail (and other) servers
  9. # Based on uw-imapd-ssl post-install script
  10. #
  11. # TODO: Use getopts
  12. # TODO: Add symlink from CA certificate to cacert.pem if non-existing
  13. # TODO: Default CA certificate is cacert.pem if --cacert not set
  14. set -e
  15. prg=$(basename $0)
  16. copyright="(C) 2001-2004 Jonas Smedegaard <dr@jones.dk>"
  17. usage() {
  18. echo "$prg, $copyright
  19. Usage: $prg [--fqdn <FQDN>] [...] --daemon <daemon> [...] [--force]
  20. or: $prg <daemon> [<daemon>...] [-f]
  21. Options:
  22. --fqdn <FQDN> Fully Qualified Domain Name for this host.
  23. --cn <country> Country Name (2 letter code)
  24. --state <state> State or Province Name (full name)
  25. --loc <locality> Locality Name (eg, city)
  26. --org <organisation> Organisation/company
  27. --ou <department> Organisational unit/department
  28. --daemon <daemon> Daemon(s) in need for a certificate
  29. (certificate is generated for each daemon)
  30. --issuer <issuer> Email address of entity issuing certificate
  31. --cert Use certified host certificate
  32. --cacert <file> Where to store host certificate if missing
  33. --makeca Create CA certificate if missing
  34. -f, --force Force overwriting existing certificate(s)
  35. -h, --help This help text
  36. If issuer is not given, \"postmaster@<localdomain>\" is used."
  37. exit 1
  38. }
  39. # Set some defaults
  40. CWD=`pwd`
  41. PATH=$PATH:/usr/bin/ssl
  42. DAYS2EXPIRE=365
  43. fqdn=''
  44. cn=''
  45. state=''
  46. loc=''
  47. org=''
  48. ou=''
  49. daemon=''
  50. daemons=''
  51. issuer=''
  52. cert=''
  53. cacert=''
  54. makeca=''
  55. force=''
  56. args=''
  57. while [ $# -gt 0 ]; do
  58. doubleshift=''
  59. case $1 in
  60. --fqdn) fqdn="$2"; doubleshift=1;;
  61. --cn) cn="$2"; doubleshift=1;;
  62. --state) state="$2"; doubleshift=1;;
  63. --loc) loc="$2"; doubleshift=1;;
  64. --org) org="$2"; doubleshift=1;;
  65. --ou) ou="$2"; doubleshift=1;;
  66. --daemon) daemons="$daemons$2 "; doubleshift=1;;
  67. --issuer) issuer="$2"; doubleshift=1;;
  68. --cert) cert=1;;
  69. --cacert) cacert="$2"; doubleshift=1;;
  70. --makeca) makeca=1;;
  71. --force|-f) force=1;;
  72. -*) usage;;
  73. *) args="$args$1 ";;
  74. esac
  75. if [ -n "$doubleshift" ];then
  76. if [ $# -gt 1 ]; then
  77. shift
  78. else
  79. echo "ERROR: Parameter for option \"$1\" missing!"
  80. usage
  81. fi
  82. fi
  83. shift
  84. done
  85. set -- $args
  86. if [ -z "$issuer" ]; then
  87. DOMAINNAME="`hostname -d`"
  88. ISSUER="postmaster@$DOMAINNAME"
  89. fi
  90. if [ -z "$fqdn" ]; then
  91. if [ $# -gt 0 ]; then
  92. fqdn="`hostname -f`"
  93. else
  94. echo "Too few parameters!"
  95. usage
  96. fi
  97. fi
  98. for val in org ou; do
  99. if eval [ -z "\$$val" ]; then
  100. eval "$val=\"$fqdn\""
  101. fi
  102. done
  103. for val in cn state loc; do
  104. if eval [ -z "\$$val" ]; then
  105. eval "$val=\".\""
  106. fi
  107. done
  108. if [ -n "$cert" ]; then
  109. if [ ! -f /etc/ssl/certs/$fqdn.pem -o ! -f /etc/ssl/private/$fqdn.pem ]; then
  110. if [ -z "$cacert" ]; then
  111. echo "ERROR: Host certificate for \"$fqdn\" missing!"
  112. exit 1
  113. fi
  114. if [ ! -r /etc/ssl/certs/$cacert.pem -o ! -r /etc/ssl/private/$cacert.pem ]; then
  115. if [ -n "$makeca" ]; then
  116. # Generate private key for CA certificate
  117. cd /etc/ssl/private
  118. #FIXME: Make strength configurable
  119. openssl genrsa -des3 -out $cacert.pem 1024
  120. chown root:root $cacert.pem
  121. chmod 0400 $cacert.pem
  122. # Generate and pre-filled certification request
  123. cd /etc/ssl/certs
  124. #FIXME: Make validity configurable
  125. openssl req -new \
  126. -key /etc/ssl/private/$cacert.pem \
  127. -x509 -days 1095 \
  128. -out $cacert.pem
  129. # Add hash to certified public certificate and cleanup
  130. ln -sf $cacert.pem `openssl x509 -noout -hash -in $cacert.pem`.0
  131. else
  132. echo "ERROR: CAcert (certifying authority certificate) missing!"
  133. exit 1
  134. fi
  135. fi
  136. echo "Generating host certificate for \"$fqdn\"..."
  137. for file in /etc/ssl/private/$fqdn.pem /etc/ssl/certs/$fqdn.csr /etc/ssl/certs/$fqdn.pem; do
  138. if [ -e $file ]; then
  139. if [ -n "$force" ]; then
  140. rm -f $file
  141. else
  142. echo "ERROR: File $file already exists!"
  143. exit 1
  144. fi
  145. fi
  146. done
  147. # Generate private key for host certificate
  148. cd /etc/ssl/private
  149. openssl genrsa -out $fqdn.pem
  150. chown root:root $fqdn.pem
  151. chmod 0600 $fqdn.pem
  152. # Generate and pre-filled certification request
  153. cd /etc/ssl/certs
  154. openssl req -new \
  155. -key /etc/ssl/private/$fqdn.pem \
  156. -out $fqdn.csr > /dev/null 2>&1 <<+
  157. $cn
  158. $state
  159. $loc
  160. $org
  161. $ou
  162. $fqdn
  163. $issuer
  164. .
  165. .
  166. +
  167. # Generate public ccertificate from certification request
  168. openssl x509 -req \
  169. -days $DAYS2EXPIRE \
  170. -CA /etc/ssl/certs/$cacert.pem \
  171. -CAkey /etc/ssl/private/$cacert.pem \
  172. -CAcreateserial -out $fqdn.pem -in $fqdn.csr
  173. # Add hash to certified public certificate and cleanup
  174. ln -sf $fqdn.pem `openssl x509 -noout -hash -in $fqdn.pem`.0
  175. rm $fqdn.csr
  176. fi
  177. fi
  178. cd /etc/ssl/certs
  179. for daemon in $daemons $@; do
  180. if [ -f $daemon.pem ]; then
  181. if [ -n "$force" ]; then
  182. rm -f `openssl x509 -noout -hash < $daemon.pem`.0
  183. rm -f $daemon.pem
  184. else
  185. echo "Ignoring certificate (/etc/ssl/certs/$daemon.pem already exists...)"
  186. continue
  187. fi
  188. fi
  189. if [ -n "$cert" ]; then
  190. echo "Attaching $daemon to certified certificate for $fqdn."
  191. ln -sf $fqdn.pem $daemon.pem
  192. (
  193. cd /etc/ssl/private
  194. ln -sf $fqdn.pem $daemon.pem
  195. )
  196. else
  197. echo -n "Generating self-certifying $daemon certificate..."
  198. openssl req -new -x509 -nodes \
  199. -days $DAYS2EXPIRE \
  200. -keyout $daemon.pem \
  201. -out $daemon.pem > /dev/null 2>&1 <<+
  202. $cn
  203. $state
  204. $loc
  205. $org
  206. $ou
  207. $fqdn
  208. $issuer
  209. +
  210. ln -sf $daemon.pem `openssl x509 -noout -hash -in $daemon.pem`.0
  211. chown root:root $daemon.pem
  212. chmod 0640 $daemon.pem
  213. echo "Done!"
  214. fi
  215. done
  216. cd $CWD