<commit_before>#!/bin/sh

password=$(<PASSWORD>)
mysql -u root -h localhost -e "
GRANT SELECT ON itpkg.email_users TO 'email'@'localhost' IDENTIFIED BY '$password';
GRANT SELECT ON itpkg.email_domains TO 'email'@'localhost' IDENTIFIED BY '$password';
GRANT SELECT ON itpkg.email_aliases TO 'email'@'localhost' IDENTIFIED BY '$password';
FLUSH PRIVILEGES;"

cd /etc/postfix
mv main.cf main.cf.orig
cat > main.cf  << "EOF"
smtpd_banner = $myhostname ESMTP $mail_name (IT-PACKAGE)
biff = no
append_dot_mydomain = no
#delay_warning_time = 4h
readme_directory = no

smtpd_tls_cert_file=/etc/dovecot/dovecot.pem
smtpd_tls_key_file=/etc/dovecot/private/dovecot.pem
smtpd_use_tls=yes
smtpd_tls_auth_only = yes

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

smtpd_recipient_restrictions =
	permit_sasl_authenticated,
	permit_mynetworks,
	reject_unauth_destination

myhostname = localhost.localdomain
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_mailbox_domains = mysql:/etc/postfix/virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/virtual-alias-maps.cf
EOF

cat > virtual-mailbox-domains.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT 1 FROM email_domains WHERE name='%s'
EOF
cat > virtual-mailbox-maps.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT 1 FROM email_users WHERE email='%s'
EOF
cat > virtual-alias-maps.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT destination FROM virtual_aliases WHERE source='%s'
EOF

mv master.cf master.cf.orig
cat > master.cf << "EOF"
smtp      inet  n       -       -       -       -       smtpd
submission inet n       -       -       -       -       smtpd
smtps     inet  n       -       -       -       -       smtpd
EOF
cat >> /etc/services << "EOF"
smtps 465/tcp
smtps 465/udp
EOF


sed -i -e "s/PASSWORD/$password/g" /etc/postfix/virtual-*s.cf
systemctl restart postfix
systemctl restart dovecot<commit_msg>dovecot setup<commit_after>#!/bin/sh

password=$(pwgen 16)
mysql -u root -h localhost -e "
GRANT SELECT ON itpkg.email_users TO 'email'@'localhost' IDENTIFIED BY '$password';
GRANT SELECT ON itpkg.email_domains TO 'email'@'localhost' IDENTIFIED BY '$password';
GRANT SELECT ON itpkg.email_aliases TO 'email'@'localhost' IDENTIFIED BY '$password';
FLUSH PRIVILEGES;"

cd /etc/postfix
mv main.cf main.cf.orig
cat > main.cf  << "EOF"
smtpd_banner = $myhostname ESMTP $mail_name (IT-PACKAGE)
biff = no
append_dot_mydomain = no
#delay_warning_time = 4h
readme_directory = no

smtpd_tls_cert_file=/etc/dovecot/server.crt
smtpd_tls_key_file=/etc/dovecot/server.key
smtpd_use_tls=yes
smtpd_tls_auth_only = yes

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

smtpd_recipient_restrictions =
	permit_sasl_authenticated,
	permit_mynetworks,
	reject_unauth_destination

myhostname = localhost.localdomain
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_mailbox_domains = mysql:/etc/postfix/virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/virtual-alias-maps.cf
EOF

cat > virtual-mailbox-domains.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT 1 FROM email_domains WHERE name='%s'
EOF
cat > virtual-mailbox-maps.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT 1 FROM email_users WHERE email='%s'
EOF
cat > virtual-alias-maps.cf  << "EOF"
user = email
password = PASSWORD
hosts = 127.0.0.1
dbname = itpkg
query = SELECT destination FROM email_aliases WHERE source='%s'
EOF

mv master.cf master.cf.orig
cat > master.cf << "EOF"
smtp      inet  n       -       -       -       -       smtpd
submission inet n       -       -       -       -       smtpd
smtps     inet  n       -       -       -       -       smtpd
EOF

cd /etc/dovecot
cat > dovecot.conf << "EOF"
protocols = imap lmtp

mail_location = maildir:/var/mail/vhosts/%d/%n
mail_privileged_group = mail
disable_plaintext_auth = yes
auth_mechanisms = plain login

ssl_cert = </etc/dovecot/server.crt
ssl_key = </etc/dovecot/server.key
ssl = required

passdb {
  driver = sql
  args = /etc/dovecot/sql.conf.ext
}
userdb {
  driver = static
  args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}

service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0600
    user = postfix
    group = postfix
  }
}

service imap-login {
  inet_listener imap {
    port = 0
  }
}

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

  unix_listener auth-userdb {
    mode = 0600
    user = vmail
  }

  user = dovecot
}

service auth-worker {
  user = vmail
}
EOF

cat > sql.conf.ext << "EOF"
driver = mysql
connect = host=127.0.0.1 dbname=itpkg user=email password=PASSWORD
default_pass_scheme = SHA512-CRYPT
password_query = SELECT email as user, password FROM email_users WHERE email='%u';
EOF

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out server.key
chmod 400 server.key
openssl req -new -key server.key -out server.csr -subj "/C=US/ST=California/L=Goleta/O=itpkg/CN=itpkg.com"
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
chmod 444 server.crt

chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot



cat >> /etc/services << "EOF"
smtps 465/tcp
EOF

sed -i -e "s/PASSWORD/$password/g" /etc/postfix/virtual-*s.cf /etc/dovecot/sql.conf.ext
mkdir -p /var/mail/vhosts
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/mail
passwd -l <PASSWORD>
chown -R vmail:vmail /var/mail/vhosts


systemctl restart postfix
systemctl restart dovecot
{'fraction_non_alphanumeric': 0.11078171321579029, 'fraction_numerical': 0.02215634264315806, 'mean_word_length': 3.678832116788321, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 11, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '982654', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2425, 'n_words': 555}