gencert.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. # Last update 20050401 - Christian Zoffoli <xmerlin@gentoo.org>
  3. VERSION="0.3"
  4. openssl="/usr/bin/openssl"
  5. opensslopts=""
  6. ldapconfdir="/etc/openldap/ssl"
  7. pemfile="${ldapconfdir}/ldap.pem"
  8. randfile="${ldapconfdir}/ldap.rand"
  9. cfgfile="${ldapconfdir}/ldap.cfg"
  10. function fixperms {
  11. chown root:ldap ${ldapconfdir} -R
  12. find ${ldapconfdir} -type f -exec chmod 640 \{\} \;
  13. chmod 750 ${ldapconfdir}
  14. }
  15. if [ ! -x ${openssl} ]; then
  16. exit 0
  17. fi
  18. if [ ! -d ${ldapconfdir} ]; then
  19. mkdir -p ${ldapconfdir}
  20. fi
  21. fixperms
  22. if [ -f ${pemfile} ]; then
  23. echo "${pemfile} already exist, dying"
  24. exit 0
  25. fi
  26. dd if=/dev/urandom of=$randfile count=1 2>/dev/null
  27. echo ""
  28. echo "______________________________________________________________________${T_ME}"
  29. echo ""
  30. echo "Creating self-signed certificate -- Version ${VERSION}"
  31. echo ""
  32. echo "______________________________________________________________________${T_ME}"
  33. echo ""
  34. COMMONNAME=`hostname`
  35. if [ ! -n "$COMMONNAME" ]; then
  36. COMMONNAME="www.openldap.org"
  37. fi
  38. if [ -f ${cfgfile} ]; then
  39. echo "${cfgfile} found, would you like to use it ? (y/n)"
  40. read answer
  41. case "$answer" in
  42. y|Y)
  43. opensslopts="-batch"
  44. ;;
  45. n|N)
  46. cat >${cfgfile} <<EOT
  47. [ req ]
  48. default_bits = 1024
  49. distinguished_name = req_DN
  50. RANDFILE = ${randfile}
  51. [ req_DN ]
  52. countryName = "1. Country Name (2 letter code)"
  53. countryName_default = "US"
  54. countryName_min = 2
  55. countryName_max = 2
  56. stateOrProvinceName = "2. State or Province Name (full name) "
  57. stateOrProvinceName_default = ""
  58. localityName = "3. Locality Name (eg, city) "
  59. localityName_default = ""
  60. 0.organizationName = "4. Organization Name (eg, company) "
  61. 0.organizationName_default = "LDAP Server"
  62. organizationalUnitName = "5. Organizational Unit Name (eg, section) "
  63. organizationalUnitName_default = "For testing purposes only"
  64. commonName = "6. Common Name (eg, CA name) "
  65. commonName_max = 64
  66. commonName_default = "${COMMONNAME}"
  67. emailAddress = "7. Email Address (eg, name@FQDN)"
  68. emailAddress_max = 40
  69. emailAddress_default = ""
  70. EOT
  71. ;;
  72. *)
  73. echo "Wrong answer, retry!"
  74. exit 1
  75. ;;
  76. esac
  77. fi
  78. echo ""
  79. ${openssl} req -config ${cfgfile} ${opensslopts} -new -rand ${randfile} -x509 -nodes -out ${pemfile} -keyout ${pemfile} -days 999999
  80. if [ $? -ne 0 ]; then
  81. echo "cca:Error: Failed to generate certificate " 1>&2
  82. exit 1
  83. else
  84. echo -e "\nCertificate creation done!"
  85. fi
  86. if [ -f ${randfile} ]; then
  87. rm -f ${randfile}
  88. fi
  89. if [ -f ${pemfile} ]; then
  90. fixperms
  91. fi