bootstrap-sogo.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. # Wait for MySQL to warm-up
  3. while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
  4. echo "Waiting for database to come up..."
  5. sleep 2
  6. done
  7. # Wait until port becomes free and send sig
  8. until ! nc -z sogo-mailcow 20000;
  9. do
  10. killall -TERM sogod
  11. sleep 3
  12. done
  13. # Wait for updated schema
  14. DBV_NOW=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT version FROM versions WHERE application = 'db_schema';" -BN)
  15. DBV_NEW=$(grep -oE '\$db_version = .*;' init_db.inc.php | sed 's/$db_version = //g;s/;//g' | cut -d \" -f2)
  16. while [[ "${DBV_NOW}" != "${DBV_NEW}" ]]; do
  17. echo "Waiting for schema update..."
  18. DBV_NOW=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT version FROM versions WHERE application = 'db_schema';" -BN)
  19. DBV_NEW=$(grep -oE '\$db_version = .*;' init_db.inc.php | sed 's/$db_version = //g;s/;//g' | cut -d \" -f2)
  20. sleep 5
  21. done
  22. echo "DB schema is ${DBV_NOW}"
  23. # cat /dev/urandom seems to hang here occasionally and is not recommended anyway, better use openssl
  24. RAND_PASS=$(openssl rand -base64 16 | tr -dc _A-Z-a-z-0-9)
  25. # Generate plist header with timezone data
  26. mkdir -p /var/lib/sogo/GNUstep/Defaults/
  27. cat <<EOF > /var/lib/sogo/GNUstep/Defaults/sogod.plist
  28. <?xml version="1.0" encoding="UTF-8"?>
  29. <!DOCTYPE plist PUBLIC "-//GNUstep//DTD plist 0.9//EN" "http://www.gnustep.org/plist-0_9.xml">
  30. <plist version="0.9">
  31. <dict>
  32. <key>OCSAclURL</key>
  33. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_acl</string>
  34. <key>SOGoIMAPServer</key>
  35. <string>imap://${IPV4_NETWORK}.250:143/?TLS=YES&amp;tlsVerifyMode=none</string>
  36. <key>SOGoSieveServer</key>
  37. <string>sieve://${IPV4_NETWORK}.250:4190/?TLS=YES&amp;tlsVerifyMode=none</string>
  38. <key>SOGoSMTPServer</key>
  39. <string>smtp://${IPV4_NETWORK}.253:588/?TLS=YES&amp;tlsVerifyMode=none</string>
  40. <key>SOGoTrustProxyAuthentication</key>
  41. <string>YES</string>
  42. <key>SOGoEncryptionKey</key>
  43. <string>${RAND_PASS}</string>
  44. <key>OCSCacheFolderURL</key>
  45. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_cache_folder</string>
  46. <key>OCSEMailAlarmsFolderURL</key>
  47. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_alarms_folder</string>
  48. <key>OCSFolderInfoURL</key>
  49. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_folder_info</string>
  50. <key>OCSSessionsFolderURL</key>
  51. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_sessions_folder</string>
  52. <key>OCSStoreURL</key>
  53. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_store</string>
  54. <key>SOGoProfileURL</key>
  55. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/sogo_user_profile</string>
  56. <key>SOGoTimeZone</key>
  57. <string>${TZ}</string>
  58. <key>domains</key>
  59. <dict>
  60. EOF
  61. # Generate multi-domain setup
  62. while read -r line gal
  63. do
  64. echo " <key>${line}</key>
  65. <dict>
  66. <key>SOGoMailDomain</key>
  67. <string>${line}</string>
  68. <key>SOGoUserSources</key>
  69. <array>
  70. <dict>
  71. <key>MailFieldNames</key>
  72. <array>
  73. <string>aliases</string>
  74. <string>ad_aliases</string>
  75. <string>ext_acl</string>
  76. </array>
  77. <key>KindFieldName</key>
  78. <string>kind</string>
  79. <key>DomainFieldName</key>
  80. <string>domain</string>
  81. <key>MultipleBookingsFieldName</key>
  82. <string>multiple_bookings</string>
  83. <key>listRequiresDot</key>
  84. <string>NO</string>
  85. <key>canAuthenticate</key>
  86. <string>YES</string>
  87. <key>displayName</key>
  88. <string>GAL ${line}</string>
  89. <key>id</key>
  90. <string>${line}</string>
  91. <key>isAddressBook</key>
  92. <string>${gal}</string>
  93. <key>type</key>
  94. <string>sql</string>
  95. <key>userPasswordAlgorithm</key>
  96. <string>${MAILCOW_PASS_SCHEME}</string>
  97. <key>prependPasswordScheme</key>
  98. <string>YES</string>
  99. <key>viewURL</key>
  100. <string>mysql://${DBUSER}:${DBPASS}@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock/${DBNAME}/_sogo_static_view</string>
  101. </dict>" >> /var/lib/sogo/GNUstep/Defaults/sogod.plist
  102. # Generate alternative LDAP authentication dict, when SQL authentication fails
  103. # This will nevertheless read attributes from LDAP
  104. line=${line} envsubst < /etc/sogo/plist_ldap >> /var/lib/sogo/GNUstep/Defaults/sogod.plist
  105. echo " </array>
  106. </dict>" >> /var/lib/sogo/GNUstep/Defaults/sogod.plist
  107. done < <(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT domain, CASE gal WHEN '1' THEN 'YES' ELSE 'NO' END AS gal FROM domain;" -B -N)
  108. # Generate footer
  109. echo ' </dict>
  110. </dict>
  111. </plist>' >> /var/lib/sogo/GNUstep/Defaults/sogod.plist
  112. # Fix permissions
  113. chown sogo:sogo -R /var/lib/sogo/
  114. chmod 600 /var/lib/sogo/GNUstep/Defaults/sogod.plist
  115. # Patch ACLs
  116. #if [[ ${ACL_ANYONE} == 'allow' ]]; then
  117. # #enable any or authenticated targets for ACL
  118. # if patch -R -sfN --dry-run /usr/lib/GNUstep/SOGo/Templates/UIxAclEditor.wox < /acl.diff > /dev/null; then
  119. # patch -R /usr/lib/GNUstep/SOGo/Templates/UIxAclEditor.wox < /acl.diff;
  120. # fi
  121. #else
  122. # #disable any or authenticated targets for ACL
  123. # if patch -sfN --dry-run /usr/lib/GNUstep/SOGo/Templates/UIxAclEditor.wox < /acl.diff > /dev/null; then
  124. # patch /usr/lib/GNUstep/SOGo/Templates/UIxAclEditor.wox < /acl.diff;
  125. # fi
  126. #fi
  127. # Copy logo, if any
  128. [[ -f /etc/sogo/sogo-full.svg ]] && cp /etc/sogo/sogo-full.svg /usr/lib/GNUstep/SOGo/WebServerResources/img/sogo-full.svg
  129. # Rsync web content
  130. echo "Syncing web content with named volume"
  131. rsync -a /usr/lib/GNUstep/SOGo/. /sogo_web/
  132. # Chown backup path
  133. chown -R sogo:sogo /sogo_backup
  134. exec gosu sogo /usr/sbin/sogod