summaryrefslogtreecommitdiff
path: root/localmksslcerts
blob: c13355b9d350d91761495b13705e0a9db29eeeb9 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/localmkmailcerts
  4. # Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmksslcerts,v 1.4 2002-03-29 17:36:08 jonas Exp $
  7. #
  8. # Generate certificates for mail servers
  9. # Based on uw-imapd-ssl post-install script
  10. #
  11. prg=$(basename $0)
  12. copyright="(C) 2001-2002 Jonas Smedegaard <dr@jones.dk>"
  13. usage() {
  14. echo "$prg, $copyright
  15. Usage: $prg --fqdn <FQDN> [--issuer <issuer>] --daemon <daemon> [...] [--force]
  16. or: $prg <FQDN> <daemon> [<daemon>...] [-f]
  17. Options:
  18. --fqdn Fully Qualified Domain Name for this host.
  19. --daemon Daemon(s) in need for a certificate
  20. (separate certificate is generated for each daemon)
  21. --issuer Email address of the person responsible for the certificate
  22. -f, --force Force overwriting existing certificate
  23. -h, --help This help text
  24. If issuer is not given, \"postmaster@<localdomain>\" is used."
  25. exit 1
  26. }
  27. # Set some defaults
  28. CWD=`pwd`
  29. PATH=$PATH:/usr/bin/ssl
  30. COUNTRY='.'
  31. STATE='.'
  32. LOCALITY='.'
  33. DAYS2EXPIRE=365
  34. fqdn=''
  35. daemons=''
  36. issuer=''
  37. force=''
  38. args=''
  39. while [ $# -gt 0 ]; do
  40. doubleshift=''
  41. case $1 in
  42. --fqdn) fqdn="$2"; doubleshift=1;;
  43. --daemon) daemons="$daemons$2 "; doubleshift=1;;
  44. --issuer) issuer="$2"; doubleshift=1;;
  45. --force|-f) force=1;;
  46. -*) usage;;
  47. *) args="$args$1 ";;
  48. esac
  49. if [ -n "$doubleshift" -a $# -gt 1 ]; then
  50. shift
  51. else
  52. echo "Missing parameter for option \"$1\"!"
  53. usage
  54. fi
  55. shift
  56. done
  57. set -- $args
  58. if [ -z "$issuer" ]; then
  59. DOMAINNAME=`hostname -d`
  60. ISSUER="postmaster@$DOMAINNAME"
  61. fi
  62. if [ -z "$fqdn" -a $# -gt 0 ]; then
  63. fqdn=$1
  64. shift
  65. else
  66. echo "Too few parameters!"
  67. usage
  68. fi
  69. set -- $daemons $args
  70. cd /etc/ssl/certs
  71. for daemon in $@; do
  72. if [ -f $daemon.pem ]; then
  73. if [ -n $force ]; then
  74. rm -f `openssl x509 -noout -hash < $daemon.pem`.0
  75. rm -f $daemon.pem
  76. else
  77. echo "You already have /etc/ssl/certs/$daemon.pem - exiting...!"
  78. exit 1
  79. fi
  80. echo -n "Generating $daemon certificate..."
  81. openssl req -new -x509 -nodes -out $daemon.pem -keyout $daemon.pem -days $DAYS2EXPIRE > /dev/null 2>&1 <<+
  82. $COUNTRY
  83. $STATE
  84. $LOCALITY
  85. $fqdn
  86. $fqdn
  87. $fqdn
  88. $issuer
  89. +
  90. ln -sf $daemon.pem `openssl x509 -noout -hash < $daemon.pem`.0
  91. echo "Done!"
  92. chown root.root /etc/ssl/certs/$daemon.pem
  93. chmod 0640 /etc/ssl/certs/$daemon.pem
  94. done
  95. cd $CWD