init.sh 3.9 KB

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