init.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. set -e
  3. set -x
  4. appSetup () {
  5. # Set variables
  6. DOMAIN=${DOMAIN:-SAMDOM.LOCAL}
  7. DOMAINPASS=${DOMAINPASS:-youshouldsetapassword}
  8. JOIN=${JOIN:-false}
  9. JOINSITE=${JOINSITE:-NONE}
  10. MULTISITE=${MULTISITE:-false}
  11. NOCOMPLEXITY=${NOCOMPLEXITY:-false}
  12. INSECURELDAP=${INSECURELDAP:-false}
  13. DNSFORWARDER=${DNSFORWARDER:-NONE}
  14. LDOMAIN=${DOMAIN,,}
  15. UDOMAIN=${DOMAIN^^}
  16. URDOMAIN=${UDOMAIN%%.*}
  17. # If multi-site, we need to connect to the VPN before joining the domain
  18. if [[ ${MULTISITE,,} == "true" ]]; then
  19. /usr/sbin/openvpn --config /docker.ovpn &
  20. VPNPID=$!
  21. echo "Sleeping 30s to ensure VPN connects ($VPNPID)";
  22. sleep 30
  23. fi
  24. # Set up samba
  25. mv /etc/krb5.conf /etc/krb5.conf.orig
  26. echo "[libdefaults]" > /etc/krb5.conf
  27. echo " dns_lookup_realm = false" >> /etc/krb5.conf
  28. echo " dns_lookup_kdc = true" >> /etc/krb5.conf
  29. echo " default_realm = ${UDOMAIN}" >> /etc/krb5.conf
  30. # If the finished file isn't there, this is brand new, we're not just moving to a new container
  31. if [[ ! -f /etc/samba/external/smb.conf ]]; then
  32. mv /etc/samba/smb.conf /etc/samba/smb.conf.orig
  33. if [[ ${JOIN,,} == "true" ]]; then
  34. if [[ ${JOINSITE} == "NONE" ]]; then
  35. samba-tool domain join ${LDOMAIN} DC -U"${URDOMAIN}\administrator" --password="${DOMAINPASS}" --dns-backend=SAMBA_INTERNAL
  36. else
  37. samba-tool domain join ${LDOMAIN} DC -U"${URDOMAIN}\administrator" --password="${DOMAINPASS}" --dns-backend=SAMBA_INTERNAL --site=${JOINSITE}
  38. fi
  39. else
  40. samba-tool domain provision --use-rfc2307 --domain=${URDOMAIN} --realm=${UDOMAIN} --server-role=dc --dns-backend=SAMBA_INTERNAL --adminpass=${DOMAINPASS}
  41. if [[ ${NOCOMPLEXITY,,} == "true" ]]; then
  42. samba-tool domain passwordsettings set --complexity=off
  43. samba-tool domain passwordsettings set --history-length=0
  44. samba-tool domain passwordsettings set --min-pwd-age=0
  45. samba-tool domain passwordsettings set --max-pwd-age=0
  46. fi
  47. fi
  48. sed -i "/\[global\]/a \
  49. \\\tidmap_ldb:use rfc2307 = yes\\n\
  50. wins support = yes\\n\
  51. template shell = /bin/bash\\n\
  52. winbind nss info = rfc2307\\n\
  53. idmap config ${URDOMAIN}: range = 10000-20000\\n\
  54. idmap config ${URDOMAIN}: backend = ad\
  55. " /etc/samba/smb.conf
  56. if [[ $DNSFORWARDER != "NONE" ]]; then
  57. sed -i "/\[global\]/a \
  58. \\\tdns forwarder = ${DNSFORWARDER}\
  59. " /etc/samba/smb.conf
  60. fi
  61. if [[ ${INSECURELDAP,,} == "true" ]]; then
  62. sed -i "/\[global\]/a \
  63. \\\tldap server require strong auth = no\
  64. " /etc/samba/smb.conf
  65. fi
  66. # Once we are set up, we'll make a file so that we know to use it if we ever spin this up again
  67. cp /etc/samba/smb.conf /etc/samba/external/smb.conf
  68. else
  69. cp /etc/samba/external/smb.conf /etc/samba/smb.conf
  70. fi
  71. # Set up supervisor
  72. echo "[supervisord]" > /etc/supervisor/conf.d/supervisord.conf
  73. echo "nodaemon=true" >> /etc/supervisor/conf.d/supervisord.conf
  74. echo "" >> /etc/supervisor/conf.d/supervisord.conf
  75. echo "[program:samba]" >> /etc/supervisor/conf.d/supervisord.conf
  76. echo "command=/usr/sbin/samba -i" >> /etc/supervisor/conf.d/supervisord.conf
  77. if [[ ${MULTISITE,,} == "true" ]]; then
  78. if [[ -n $VPNPID ]]; then
  79. kill $VPNPID
  80. fi
  81. echo "" >> /etc/supervisor/conf.d/supervisord.conf
  82. echo "[program:openvpn]" >> /etc/supervisor/conf.d/supervisord.conf
  83. echo "command=/usr/sbin/openvpn --config /docker.ovpn" >> /etc/supervisor/conf.d/supervisord.conf
  84. fi
  85. appStart
  86. }
  87. appStart () {
  88. /usr/bin/supervisord
  89. }
  90. case "$1" in
  91. start)
  92. if [[ -f /etc/samba/external/smb.conf ]]; then
  93. cp /etc/samba/external/smb.conf /etc/samba/smb.conf
  94. appStart
  95. else
  96. echo "Config file is missing."
  97. fi
  98. ;;
  99. setup)
  100. # If the supervisor conf isn't there, we're spinning up a new container
  101. if [[ -f /etc/supervisor/conf.d/supervisord.conf ]]; then
  102. appStart
  103. else
  104. appSetup
  105. fi
  106. ;;
  107. esac
  108. exit 0