init.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. set -e
  3. appSetup () {
  4. # Set variables
  5. DOMAIN=${DOMAIN:-SAMDOM.LOCAL}
  6. DOMAINPASS=${DOMAINPASS:-youshouldsetapassword^123}
  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. FIRSTRUN=false
  38. if [[ ! -f /etc/samba/external/smb.conf ]]; then
  39. FIRSTRUN=true
  40. mv /etc/samba/smb.conf /etc/samba/smb.conf.orig
  41. if [[ ${JOIN,,} == "true" ]]; then
  42. if [[ ${JOINSITE} == "NONE" ]]; then
  43. samba-tool domain join ${LDOMAIN} DC -U"${URDOMAIN}\administrator" --password="${DOMAINPASS}" --dns-backend=SAMBA_INTERNAL
  44. else
  45. samba-tool domain join ${LDOMAIN} DC -U"${URDOMAIN}\administrator" --password="${DOMAINPASS}" --dns-backend=SAMBA_INTERNAL --site=${JOINSITE}
  46. fi
  47. else
  48. samba-tool domain provision --use-rfc2307 --domain=${URDOMAIN} --realm=${UDOMAIN} --server-role=dc --dns-backend=SAMBA_INTERNAL --adminpass=${DOMAINPASS} ${HOSTIP_OPTION}
  49. if [[ ${NOCOMPLEXITY,,} == "true" ]]; then
  50. samba-tool domain passwordsettings set --complexity=off
  51. samba-tool domain passwordsettings set --history-length=0
  52. samba-tool domain passwordsettings set --min-pwd-age=0
  53. samba-tool domain passwordsettings set --max-pwd-age=0
  54. fi
  55. fi
  56. sed -i "/\[global\]/a \
  57. \\\tidmap_ldb:use rfc2307 = yes\\n\
  58. wins support = yes\\n\
  59. template shell = /bin/bash\\n\
  60. template homedir = /home/%U\\n\
  61. idmap config ${URDOMAIN} : schema_mode = rfc2307\\n\
  62. idmap config ${URDOMAIN} : unix_nss_info = yes\\n\
  63. idmap config ${URDOMAIN} : backend = ad\
  64. " /etc/samba/smb.conf
  65. sed -i "s/LOCALDC/${URDOMAIN}DC/g" /etc/samba/smb.conf
  66. if [[ $DNSFORWARDER != "NONE" ]]; then
  67. sed -i "/\[global\]/a \
  68. \\\tdns forwarder = ${DNSFORWARDER}\
  69. " /etc/samba/smb.conf
  70. fi
  71. if [[ ${INSECURELDAP,,} == "true" ]]; then
  72. sed -i "/\[global\]/a \
  73. \\\tldap server require strong auth = no\
  74. " /etc/samba/smb.conf
  75. fi
  76. # Once we are set up, we'll make a file so that we know to use it if we ever spin this up again
  77. cp -f /etc/samba/smb.conf /etc/samba/external/smb.conf
  78. else
  79. cp -f /etc/samba/external/smb.conf /etc/samba/smb.conf
  80. fi
  81. # Set up supervisor
  82. echo "[supervisord]" > /etc/supervisor/conf.d/supervisord.conf
  83. echo "nodaemon=true" >> /etc/supervisor/conf.d/supervisord.conf
  84. echo "" >> /etc/supervisor/conf.d/supervisord.conf
  85. echo "[program:ntpd]" >> /etc/supervisor/conf.d/supervisord.conf
  86. echo "command=/usr/sbin/ntpd -c /etc/ntpd.conf -n" >> /etc/supervisor/conf.d/supervisord.conf
  87. echo "[program:samba]" >> /etc/supervisor/conf.d/supervisord.conf
  88. echo "command=/usr/sbin/samba -i" >> /etc/supervisor/conf.d/supervisord.conf
  89. if [[ ${MULTISITE,,} == "true" ]]; then
  90. if [[ -n $VPNPID ]]; then
  91. kill $VPNPID
  92. fi
  93. echo "" >> /etc/supervisor/conf.d/supervisord.conf
  94. echo "[program:openvpn]" >> /etc/supervisor/conf.d/supervisord.conf
  95. echo "command=/usr/sbin/openvpn --config /docker.ovpn" >> /etc/supervisor/conf.d/supervisord.conf
  96. fi
  97. echo "server 127.127.1.0" > /etc/ntpd.conf
  98. echo "fudge 127.127.1.0 stratum 10" >> /etc/ntpd.conf
  99. echo "server 0.pool.ntp.org iburst prefer" >> /etc/ntpd.conf
  100. echo "server 1.pool.ntp.org iburst prefer" >> /etc/ntpd.conf
  101. echo "server 2.pool.ntp.org iburst prefer" >> /etc/ntpd.conf
  102. echo "driftfile /var/lib/ntp/ntp.drift" >> /etc/ntpd.conf
  103. echo "logfile /var/log/ntp" >> /etc/ntpd.conf
  104. echo "ntpsigndsocket /usr/local/samba/var/lib/ntp_signd/" >> /etc/ntpd.conf
  105. echo "restrict default kod nomodify notrap nopeer mssntp" >> /etc/ntpd.conf
  106. echo "restrict 127.0.0.1" >> /etc/ntpd.conf
  107. echo "restrict 0.pool.ntp.org mask 255.255.255.255 nomodify notrap nopeer noquery" >> /etc/ntpd.conf
  108. echo "restrict 1.pool.ntp.org mask 255.255.255.255 nomodify notrap nopeer noquery" >> /etc/ntpd.conf
  109. echo "restrict 2.pool.ntp.org mask 255.255.255.255 nomodify notrap nopeer noquery" >> /etc/ntpd.conf
  110. echo "tinker panic 0" >> /etc/ntpd.conf
  111. appStart ${FIRSTRUN}
  112. }
  113. fixDomainUsersGroup () {
  114. GIDNUMBER=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=domain users" | { grep ^gidNumber: || true; })
  115. if [ -z "${GIDNUMBER}" ]; then
  116. echo "dn: CN=Domain Users,CN=Users,DC=corp,DC=example,DC=com
  117. changetype: modify
  118. add: gidNumber
  119. gidNumber: 3000000" | ldbmodify -H /var/lib/samba/private/sam.ldb
  120. net cache flush
  121. fi
  122. }
  123. appStart () {
  124. /usr/bin/supervisord > /var/log/supervisor/supervisor.log 2>&1 &
  125. if [ "${1}" = "true" ]; then
  126. echo "Sleeping 10 before checking on Domain Users of gid 3000000"
  127. sleep 10
  128. fixDomainUsersGroup
  129. fi
  130. while [ ! -f /var/log/supervisor/supervisor.log ]; do
  131. echo "Waiting for log files..."
  132. sleep 1
  133. done
  134. sleep 3
  135. tail -F /var/log/supervisor/*.log
  136. }
  137. appSetup
  138. exit 0