#!/bin/bash if [[ ! -f ".configured" ]]; then exec > >(tee -i /iredmail.log) exec 2>&1 set -x : ${MYSQL_SERVER_ADDRESS:=127.0.0.1} : ${MYSQL_SERVER_PORT:=3306} : ${MYSQL_ROOT_USER:=admin_iredmail} : ${MYSQL_ROOT_PASSWD:=admin_password} : ${MYSQL_GRANT_HOST:=%} : ${MYSQLEXIST:=NO} : ${SETFQDN:=test.testdomain.ru} : ${STORAGE_BASE_DIR:=/var/vmail} : ${WEB_SERVER:=NGINX} : ${BACKEND_ORIG:=OPENLDAP} : ${BACKEND:=OPENLDAP} : ${VMAIL_DB_BIND_PASSWD:=`eval /dev/null || head -c 20)`} : ${VMAIL_DB_ADMIN_PASSWD:=`eval /dev/null || head -c 20)`} : ${MLMMJADMIN_API_AUTH_TOKEN:=`eval /dev/null || head -c 20)`} : ${NETDATA_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${LDAP_SUFFIX:=dc=testdomain,dc=ru} : ${LDAP_BINDPW:=`eval /dev/null || head -c 20)`} : ${LDAP_ADMIN_PW:=`eval /dev/null || head -c 20)`} : ${LDAP_ROOTPW:=`eval /dev/null || head -c 20)`} : ${LDAP_SEARCH_BASE:=cn=users,dc=testdomain,dc=ru} : ${LDAP_EXTERNAL_USE:=YES} : ${LDAP_EXTERNAL_SERVER:=127.0.0.1} : ${LDAP_EXTERNAL_BINDUSER:=vmail} : ${LDAP_EXTERNAL_BINDPW:=$LDAP_BINDPW} : ${LDAP_EXTERNAL_BASE:=dc=testdomain,dc=ru} : ${MYSQL_ROOT_PASSWD:=`eval /dev/null || head -c 20)`} : ${FIRST_DOMAIN:=testdomain.ru} : ${DOMAIN_ADMIN_PASSWD_PLAIN:=`eval /dev/null || head -c 20)`} : ${USE_IREDADMIN:=YES} : ${USE_ROUNDCUBE:=YES} : ${AMAVISD_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${IREDADMIN_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${RCM_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${SOGO_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${SOGO_SIEVE_MASTER_PASSWD:=`eval /dev/null || head -c 20)`} : ${IREDAPD_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${FAIL2BAN_DB_PASSWD:=`eval /dev/null || head -c 20)`} : ${USE_SYSTEMD:=NO} : ${MAILNAME:=`hostname -f`} if [[ X"${MYSQLEXIST}" == X'YES' || X"${MYSQLEXIST}" == X'yes' ]]; then : ${USE_EXISTING_MYSQL:=YES} fi if [[ X"${MYSQLEXIST}" == X'YES' || X"${MYSQLEXIST}" == X'yes' ]]; then while ! mysqladmin ping -h"${MYSQL_SERVER_ADDRESS}" --silent; do sleep 1 done fi . /lib/lsb/init-functions cat < /etc/mailname $MAILNAME EOF echo '>create initial config file' cat < /iredmail/config export STORAGE_BASE_DIR='$STORAGE_BASE_DIR' export WEB_SERVER='$WEB_SERVER' export BACKEND_ORIG='$BACKEND_ORIG' export BACKEND='$BACKEND' export VMAIL_DB_BIND_PASSWD='$VMAIL_DB_BIND_PASSWD' export VMAIL_DB_ADMIN_PASSWD='$VMAIL_DB_ADMIN_PASSWD' export MLMMJADMIN_API_AUTH_TOKEN='$MLMMJADMIN_API_AUTH_TOKEN' export NETDATA_DB_PASSWD='$NETDATA_DB_PASSWD' export LDAP_SUFFIX='$LDAP_SUFFIX' export LDAP_BINDPW='$LDAP_BINDPW' export LDAP_ADMIN_PW='$LDAP_ADMIN_PW' export LDAP_ROOTPW='$LDAP_ROOTPW' export MYSQL_ROOT_PASSWD='$MYSQL_ROOT_PASSWD' export FIRST_DOMAIN='$FIRST_DOMAIN' export DOMAIN_ADMIN_PASSWD_PLAIN='$DOMAIN_ADMIN_PASSWD_PLAIN' export USE_IREDADMIN='$USE_IREDADMIN' export USE_ROUNDCUBE='$USE_ROUNDCUBE' export AMAVISD_DB_PASSWD='$AMAVISD_DB_PASSWD' export IREDADMIN_DB_PASSWD='$IREDADMIN_DB_PASSWD' export RCM_DB_PASSWD='$RCM_DB_PASSWD' export SOGO_DB_PASSWD='$SOGO_DB_PASSWD' export SOGO_SIEVE_MASTER_PASSWD='$SOGO_DB_PASSWD' export IREDAPD_DB_PASSWD='$SOGO_DB_PASSWD' export FAIL2BAN_DB_PASSWD='$SOGO_DB_PASSWD' #EOF EOF echo '> install iredmail' # Install iredmail if [[ X"${MYSQLEXIST}" == X'YES' || X"${MYSQLEXIST}" == X'yes' ]]; then USE_EXISTING_MYSQL=YES MYSQL_SERVER_ADDRESS=$MYSQL_SERVER_ADDRESS \ MYSQL_SERVER_PORT=$MYSQL_SERVER_PORT MYSQL_ROOT_USER=$MYSQL_ROOT_USER \ MYSQL_ROOT_PASSWD=$MYSQL_ROOT_PASSWD MYSQL_GRANT_HOST=$MYSQL_GRANT_HOST \ AUTO_USE_EXISTING_CONFIG_FILE=y AUTO_INSTALL_WITHOUT_CONFIRM=y \ AUTO_CLEANUP_REMOVE_SENDMAIL=y AUTO_CLEANUP_REPLACE_MYSQL_CONFIG=y \ bash /iredmail/iRedMail.sh echo $MYSQL_SERVER_ADDRESS echo $MYSQL_GRANT_HOST else AUTO_USE_EXISTING_CONFIG_FILE=y AUTO_INSTALL_WITHOUT_CONFIRM=y \ AUTO_CLEANUP_REMOVE_SENDMAIL=y AUTO_CLEANUP_REPLACE_MYSQL_CONFIG=y \ bash /iredmail/iRedMail.sh fi cp /opt/www/iredadmin/rc_scripts/uwsgi/debian.ini /etc/uwsgi/apps-enabled/iredadmin.ini cp /opt/mlmmjadmin/rc_scripts/uwsgi/debian.ini /etc/uwsgi/apps-enabled/mlmmjadmin.ini external_ldap_config() { # Enable external AD authentication postconf -e virtual_alias_maps='' postconf -e sender_bcc_maps='' postconf -e recipient_bcc_maps='' postconf -e relay_domains='' postconf -e relay_recipient_maps='' postconf -e sender_dependent_relayhost_maps='' postconf -e smtpd_sasl_local_domain=$FIRST_DOMAIN postconf -e virtual_mailbox_domains=$FIRST_DOMAIN postconf -e transport_maps='hash:/etc/postfix/transport' postconf -e smtpd_sender_login_maps='proxy:ldap:/etc/postfix/ad_sender_login_maps.cf' postconf -e virtual_mailbox_maps='proxy:ldap:/etc/postfix/ad_virtual_mailbox_maps.cf' postconf -e virtual_alias_maps='proxy:ldap:/etc/postfix/ad_virtual_group_maps.cf' cat < /etc/postfix/transport $FIRST_DOMAIN dovecot EOF postmap hash:/etc/postfix/transport cat < /etc/postfix/ad_sender_login_maps.cf server_host = $LDAP_EXTERNAL_SERVER server_port = 389 version = 3 bind = yes start_tls = no bind_dn = $LDAP_EXTERNAL_BINDUSER bind_pw = $LDAP_EXTERNAL_BINDPW search_base = $LDAP_EXTERNAL_BASE scope = sub query_filter = (&(userPrincipalName=%s)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))) result_attribute= userPrincipalName debuglevel = 0 EOF cat < /etc/postfix/ad_virtual_mailbox_maps.cf server_host = $LDAP_EXTERNAL_SERVER server_port = 389 version = 3 bind = yes start_tls = no bind_dn = $LDAP_EXTERNAL_BINDUSER bind_pw = $LDAP_EXTERNAL_BINDPW search_base = $LDAP_EXTERNAL_BASE scope = sub query_filter = (&(objectclass=person)(userPrincipalName=%s)) result_attribute= userPrincipalName result_format = %d/%u/Maildir/ debuglevel = 0 EOF cat < /etc/postfix/ad_virtual_group_maps.cf server_host = $LDAP_EXTERNAL_SERVER server_port = 389 version = 3 bind = yes start_tls = no bind_dn = $LDAP_EXTERNAL_BINDUSER bind_pw = $LDAP_EXTERNAL_BINDPW search_base = $LDAP_EXTERNAL_BASE scope = sub query_filter = (&(objectClass=group)(mail=%s)) special_result_attribute = member #leaf_result_attribute = mail result_attribute= userPrincipalName debuglevel = 0 EOF sed -i '/check_policy_service inet:127.0.0.1:7777/d' /etc/postfix/main.cf rm -rf /etc/dovecot/dovecot-ldap.conf cat < /etc/dovecot/dovecot-ldap.conf hosts = $LDAP_EXTERNAL_SERVER:389 ldap_version = 3 auth_bind = yes dn = $LDAP_EXTERNAL_BINDUSER dnpass = $LDAP_EXTERNAL_BINDPW base = $LDAP_EXTERNAL_BASE scope = subtree deref = never # Below two are required by command 'doveadm mailbox ...' iterate_attrs = userPrincipalName=user iterate_filter = (&(userPrincipalName=*)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))) user_filter = (&(userPrincipalName=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))) pass_filter = (&(userPrincipalName=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))) pass_attrs = userPassword=password default_pass_scheme = CRYPT user_attrs = =home=/var/vmail/vmail1/%Ld/%Ln/,=mail=maildir:~/Maildir/ EOF sed -i '/Global\ LDAP\ address\ book/,$d' /opt/www/roundcubemail/config/config.inc.php cat <> /opt/www/roundcubemail/config/config.inc.php # # "sql" is personal address book stored in roundcube database. # "global_ldap_abook" is the new LDAP address book for AD, we will create it below. # \$config['autocomplete_addressbooks'] = array("sql", "global_ldap_abook"); # Enable setting below if Roundcube returns 'user@127.0.0.1' as email address #\$config['mail_domain'] = '%d'; # # Global LDAP Address Book with AD. # \$config['ldap_public']["global_ldap_abook"] = array( 'name' => 'Global Address Book', 'hosts' => array("$LDAP_EXTERNAL_SERVER"), // <- Set AD hostname or IP address here. 'port' => 389, 'use_tls' => false, // <- Set to true if you want to use LDAP over TLS. 'ldap_version' => '3', 'network_timeout' => 10, 'user_specific' => false, 'base_dn' => "$LDAP_EXTERNAL_BASE", // <- Set base dn in AD 'bind_dn' => "$LDAP_EXTERNAL_BINDUSER", // <- bind dn 'bind_pass' => "$LDAP_EXTERNAL_BINDPW", // <- bind password 'writable' => false, // <- Do not allow mail user write data back to AD. 'search_fields' => array('mail', 'cn', 'sAMAccountName', 'displayname', 'sn', 'givenName'), // mapping of contact fields to directory attributes 'fieldmap' => array( 'name' => 'cn', 'displayname' => 'displayName', 'surname' => 'sn', 'firstname' => 'givenName', 'jobtitle' => 'title', 'department' => 'department', 'company' => 'company', 'email' => 'mail:*', 'phone:work' => 'telephoneNumber', 'phone:home' => 'homePhone', 'phone:mobile' => 'mobile', 'phone:workfax' => 'facsimileTelephoneNumber', 'phone:pager' => 'pager', 'phone:other' => 'ipPhone', 'street:work' => 'streetAddress', 'zipcode:work' => 'postalCode', 'locality:work' => 'l', 'region:work' => 'st', 'country:work' => 'c', 'notes' => 'description', 'photo' => 'jpegPhoto', // Might be 'thumbnailPhoto' for // compatibility with some other // Microsoft software 'website' => 'wWWHomePage', ), 'sort' => 'cn', 'scope' => 'sub', 'filter' => "(&(|(objectclass=person)(objectclass=group))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))", 'fuzzy_search' => true, 'vlv' => false, // Enable Virtual List View to more // efficiently fetch paginated data // (if server supports it) 'sizelimit' => '0', // Enables you to limit the count of // entries fetched. Setting this to 0 // means no limit. 'timelimit' => '0', // Sets the number of seconds how long // is spend on the search. Setting this // to 0 means no limit. 'referrals' => false, // Sets the LDAP_OPT_REFERRALS option. // Mostly used in multi-domain Active // Directory setups ); EOF } if [[ X"${LDAP_EXTERNAL_USE}" == X'YES' || X"${LDAP_EXTERNAL_USE}" == X'yes' ]]; then external_ldap_config fi touch /.configured else # Start syslog-ng /usr/sbin/syslog-ng -F --no-caps & if [[ X"${MYSQLEXIST}" != X'YES' || X"${MYSQLEXIST}" != X'yes' ]]; then service mysql start fi service dovecot start service nginx start service php7.2-fpm start # Start postfix start-stop-daemon --start --exec /usr/sbin/postmulti -- -i - -x /usr/sbin/postfix quiet-quick-start service clamav-daemon start service clamav-freshclam start service spamassasin start service amavis start service mlmmjadmin_enable start service uwsgi start if [[ X"${LDAP_EXTERNAL_USE}" != X'YES' || X"${LDAP_EXTERNAL_USE}" != X'yes' ]]; then service slapd start service iredapd start fi while true; do sleep 1; done fi