139 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/sh
 | |
| 
 | |
| . ./myserver.conf
 | |
| . ./utils.sh
 | |
| 
 | |
| install_mails_services_pkg()
 | |
| {
 | |
|     pkg_add dovecot dovecot-pigeonhole opensmtpd-filter-rspamd redis-6.2.12\
 | |
|             opensmtpd-extras-6.7.1v0 opensmtpd-filter-dkimsign-0.5 rspamd-3.2 
 | |
| }
 | |
| 
 | |
| gen_mails_service_configuration()
 | |
| {
 | |
|     cp -v default_configuration/opensmtpd/smtpd.conf.example my_configuration/opensmtpd/smtpd.conf
 | |
|     sed -i "s/__DOMAIN__/$DOMAIN/g" my_configuration/opensmtpd/smtpd.conf
 | |
|     cp -v default_configuration/opensmtpd/spamd.conf.example my_configuration/opensmtpd/spamd.conf
 | |
|     cp -v default_configuration/dovecot/dovecot.conf.example my_configuration/dovecot/dovecot.conf
 | |
|     cp -v default_configuration/dovecot/local.conf.example my_configuration/dovecot/local.conf
 | |
|     sed -i "s/__DOMAIN__/$DOMAIN/g" my_configuration/dovecot/local.conf
 | |
| }
 | |
| 
 | |
| gen_dkim_keys()
 | |
| {
 | |
|     # Generate dkim key
 | |
|     openssl genrsa -out my_configuration/mail/$DOMAIN-private.key 2048
 | |
|     openssl rsa -in my_configuration/mail/$DOMAIN-private.key -pubout | \
 | |
|         sed '1s/.*/v=DKIM1;p=/;:nl;${s/-----.*//;q;};N;s/\n//g;b nl;' > default_configuration/mail/$DOMAIN-public.key
 | |
| 
 | |
| }
 | |
| 
 | |
| install_dovecot_service_antispam()
 | |
| {
 | |
|     # Add antispam utils for Dovecot
 | |
|     cd /usr/local/lib/dovecot/sieve
 | |
| 
 | |
|     cat > report-ham.sieve <<EOF
 | |
| 
 | |
| require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
 | |
| 
 | |
| if environment :matches "imap.mailbox" "*" {
 | |
| set "mailbox" "${1}";
 | |
| }
 | |
| 
 | |
| if string "${mailbox}" "Trash" {
 | |
| stop;
 | |
| }
 | |
| 
 | |
| if environment :matches "imap.user" "*" {
 | |
| set "username" "${1}";
 | |
| }
 | |
| 
 | |
| pipe :copy "sa-learn-ham.sh" [ "${username}" ];
 | |
| EOF
 | |
| 
 | |
|     cat > report-spam.sieve <<EOF
 | |
| require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
 | |
| 
 | |
| if environment :matches "imap.user" "*" {
 | |
| set "username" "${1}";
 | |
| }
 | |
| 
 | |
| pipe :copy "sa-learn-spam.sh" [ "${username}" ];
 | |
| EOF
 | |
| 
 | |
|     cat > sa-learn-ham.sh<<EOF
 | |
| #!/bin/sh
 | |
| exec /usr/local/bin/rspamc -d "${1}" learn_ham
 | |
| EOF
 | |
| 
 | |
|     cat > sa-learn-spam.sh<<EOF
 | |
| 
 | |
| #!/bin/sh
 | |
| exec /usr/local/bin/rspamc -d "${1}" learn_spam
 | |
| EOF
 | |
| 
 | |
| 
 | |
|     sievec report-ham.sieve
 | |
|     sievec report-spam.sieve
 | |
| 
 | |
|     chmod 755 sa-learn-ham.sh
 | |
|     chmod 755 sa-learn-spam.sh
 | |
| }
 | |
| 
 | |
| install_mails_services_configuration()
 | |
| {
 | |
|     cp -v my_configuration/mail/smtpd.conf /etc/mail/smtpd.conf
 | |
|     cp -v my_configuration/dovecot/dovecot.conf /etc/dovecot/
 | |
|     cp -v my_configuration/dovecot/local.conf /etc/dovecot/local.conf
 | |
|     mkdir /etc/mail/dkim/
 | |
|     cp -v my_configuration/mail/$DOMAIN-private.key /etc/mail/dkim/
 | |
|     cp -v my_configuration/mail/$DOMAIN-public.key /etc/mail/dkim/
 | |
|     chown -R _dkimsign /etc/mail/dkim/
 | |
|     touch /etc/mail/virtuals
 | |
|     touch /etc/mail/passwd
 | |
|     rm /etc/dovecot/conf.d/10-ssl.conf
 | |
| 
 | |
| }
 | |
| 
 | |
| make_system_mails_services_requirements()
 | |
| {
 | |
| 
 | |
|     useradd -c "Virtual Mail Account" -d /var/vmail -s /sbin/nologin -u 2000 -g =uid -L staff vmail
 | |
|     mkdir -p /var/vmail/$DOMAIN
 | |
|     chown -R vmail:vmail /var/vmail/
 | |
|     groupadd _maildaemons
 | |
|     usermod -G _maildaemons _dovecot
 | |
|     usermod -G _maildaemons _smtpd
 | |
| 
 | |
|     cp /etc/login.conf /etc/login.conf.orig
 | |
|     cat >> /etc/login.conf <<EOF
 | |
| dovecot:\
 | |
|     :openfiles-cur=1024:\
 | |
|     :openfiles-max=2048:\
 | |
|     :tc=daemon:
 | |
| EOF
 | |
| 
 | |
| }
 | |
| 
 | |
| make_directory_configuration()
 | |
| {
 | |
|     mkdir my_configuration/mail
 | |
|     mkdir my_configuration/dovecot
 | |
| }
 | |
| 
 | |
| if [ "$1" == "gen-config-only" ];
 | |
| then
 | |
|     gen_mails_service_configuration
 | |
|     gen_dkim_keys
 | |
| elif [ "$1" == "install" ];
 | |
| then
 | |
|     install_mails_services_pkg
 | |
|     gen_mails_service_configuration
 | |
|     gen_dkim_keys
 | |
|     install_mails_services_configuration
 | |
|     make_system_mails_services_requirements
 | |
|     rcctl enable rspamd redis
 | |
|     rcctl start rspamd redis
 | |
|     restart_mails_service
 |