freedombone-clientcert 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Generates an email client cert for use with IMAP clients
  12. # See:
  13. # http://strange.systems/certificate-based-auth-with-dovecot-sendmail
  14. # http://help.fabasoftfolio.com/index.php?topic=doc/Installation-and-Configuration-of-Fabasoft-Folio-IMAP-Service/client-certificate-authentication.htm
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. PROJECT_NAME='freedombone'
  33. export TEXTDOMAIN=${PROJECT_NAME}-clientcert
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. USERNAME=
  36. COUNTRY_CODE="US"
  37. AREA="Free Speech Zone"
  38. LOCATION="Freedomville"
  39. ORGANISATION="Freedombone"
  40. UNIT="Freedombone Unit"
  41. EXTENSIONS=""
  42. function show_help {
  43. echo ''
  44. echo $'freedombone-clientcert -u [username]'
  45. echo ''
  46. echo $'Creates email certificates for use with IMAP clients'
  47. echo ''
  48. echo $' --help Show help'
  49. echo $' -u --username [name] Username'
  50. echo ''
  51. exit 0
  52. }
  53. while [[ $# > 1 ]]
  54. do
  55. key="$1"
  56. case $key in
  57. --help)
  58. show_help
  59. ;;
  60. -u|--username)
  61. shift
  62. USERNAME="$1"
  63. ;;
  64. *)
  65. # unknown option
  66. ;;
  67. esac
  68. shift
  69. done
  70. if [ ! $USERNAME ]; then
  71. echo $'No username specified'
  72. exit 5748
  73. fi
  74. if [ ! -d /home/$USERNAME ]; then
  75. echo $"User $USERNAME not found"
  76. exit 76239
  77. fi
  78. if [ -d /home/$USERNAME/emailcert ]; then
  79. echo $'Client certs were already for created'
  80. exit 2953
  81. fi
  82. if [ ! -f /etc/dovecot/passwd-file ]; then
  83. touch /etc/dovecot/passwd-file
  84. fi
  85. # Add a user password
  86. if ! grep -q "$USERNAME:{plain}" /etc/dovecot/passwd-file; then
  87. echo "$USERNAME:{plain}::::::nopassword" >> /etc/dovecot/passwd-file
  88. fi
  89. chmod 600 /etc/dovecot/passwd-file
  90. # create a user cert
  91. freedombone-addcert -h $USERNAME --nodh ""
  92. if [ ! -f /etc/ssl/private/$USERNAME.key ]; then
  93. echo $'User certificates were not created'
  94. rm -rf /home/$USERNAME/emailcert
  95. exit 74835
  96. fi
  97. # create a certificate request
  98. openssl req -new -sha256 -subj \
  99. "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$USERNAME" \
  100. -key /etc/ssl/private/$USERNAME.key \
  101. -out /etc/ssl/requests/$USERNAME.csr
  102. if [ ! -f /etc/ssl/requests/$USERNAME.csr ]; then
  103. echo $'Certificate request was not created'
  104. rm -rf /home/$USERNAME/emailcert
  105. exit 83520
  106. fi
  107. # sign the certificate request
  108. cd /etc/ssl
  109. openssl ca -config /etc/ssl/dovecot-ca.cnf \
  110. -in /etc/ssl/requests/$USERNAME.csr \
  111. -out /etc/ssl/certs/$USERNAME.cer
  112. if [ ! -f /etc/ssl/certs/$USERNAME.cer ]; then
  113. echo $'Authentication certificate was not created'
  114. rm -rf /home/$USERNAME/emailcert
  115. exit 343569
  116. fi
  117. # move the cert to the user's home
  118. mkdir /home/$USERNAME/emailcert
  119. mv /etc/ssl/certs/$USERNAME.cer /home/$USERNAME/emailcert
  120. cp /etc/ssl/certs/dovecot.crt /home/$USERNAME/emailcert
  121. cp /etc/ssl/certs/ca-$HOSTNAME.crt /home/$USERNAME/emailcert
  122. mv /etc/ssl/private/$USERNAME.key /home/$USERNAME/emailcert
  123. mv /etc/ssl/certs/$USERNAME.crt /home/$USERNAME/emailcert
  124. openssl pkcs12 -export -in /home/$USERNAME/emailcert/$USERNAME.cer \
  125. -out /home/$USERNAME/emailcert/$USERNAME.p12 \
  126. -inkey /home/$USERNAME/emailcert/$USERNAME.key \
  127. -certfile /home/$USERNAME/emailcert/ca-$HOSTNAME.crt \
  128. -password pass:""
  129. # make an install script
  130. echo '#!/bin/bash' > /home/$USERNAME/emailcert/install.sh
  131. echo "sudo mv ca-$HOSTNAME.crt /etc/ssl/certs" >> \
  132. /home/$USERNAME/emailcert/install.sh
  133. echo "sudo mv $USERNAME.crt /etc/ssl/certs" >> \
  134. /home/$USERNAME/emailcert/install.sh
  135. echo "sudo mv dovecot.crt /etc/ssl/certs" >> \
  136. /home/$USERNAME/emailcert/install.sh
  137. echo "sudo mv $USERNAME.key /etc/ssl/private" >> \
  138. /home/$USERNAME/emailcert/install.sh
  139. echo 'exit 0' >> /home/$USERNAME/emailcert/install.sh
  140. # set permissions for the user
  141. chmod -R 755 /home/$USERNAME/emailcert
  142. chown -R $USERNAME:$USERNAME /home/$USERNAME/emailcert
  143. chmod +x /home/$USERNAME/emailcert/install.sh
  144. shred -zu /etc/ssl/requests/$USERNAME.csr
  145. echo $'Email authentication certificate created. You can obtain it on the client with:'
  146. echo ''
  147. echo " scp -P 2222 -r $USERNAME@$HOSTNAME:/home/$USERNAME/emailcert ~/"
  148. echo ''
  149. exit 0