From 06ba5073c126ca90a01fb390c55b5d030900541b Mon Sep 17 00:00:00 2001
From: Dariusz Janny <janny@man.poznan.pl>
Date: Fri, 22 Jan 2021 03:55:56 +0100
Subject: [PATCH] [edugain/faas#18] rebase faas-smtp and use Makefile

---
 .gitignore                                   |  1 +
 Dockerfile                                   | 48 ++++++++--
 Makefile                                     | 33 +++++++
 README.md                                    | 18 +++-
 conf/etc/aliases                             | 96 ++++++++++++++++++++
 scripts/docker/run.sh => conf/etc/entrypoint | 16 +++-
 conf/faas-smtp.cnf                           | 13 +++
 conf/faas-smtp.compose.yml                   | 14 ---
 conf/faas-smtp.env                           |  6 --
 scripts/helpers/build.sh                     |  3 -
 scripts/helpers/run.sh                       |  3 -
 test/send.pl                                 | 31 +++++++
 12 files changed, 246 insertions(+), 36 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 conf/etc/aliases
 rename scripts/docker/run.sh => conf/etc/entrypoint (60%)
 create mode 100644 conf/faas-smtp.cnf
 delete mode 100644 conf/faas-smtp.compose.yml
 delete mode 100644 conf/faas-smtp.env
 delete mode 100755 scripts/helpers/build.sh
 delete mode 100755 scripts/helpers/run.sh
 create mode 100644 test/send.pl

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d8fe4fa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.project
diff --git a/Dockerfile b/Dockerfile
index 2867522..7f63f0e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,9 +1,45 @@
-FROM alpine:3.12.3
+FROM debian:10.7
 
-RUN apk add --no-cache  bash postfix supervisor rsyslog busybox-extras
+ARG FAAS_SMTP_IMAGE
+ARG FAAS_SMTP_NAME
+ARG FAAS_SMTP_VERSION
 
-COPY       /conf/etc/supervisord.conf     /etc/supervisord.conf
-COPY       /scripts/docker/run.sh     /run.sh
+RUN echo $FAAS_SMTP_IMAGE > /faas-docker-image
+RUN echo $FAAS_SMTP_NAME > /faas-docker-name
+RUN echo $FAAS_SMTP_VERSION > /faas-docker-version
+
+
+RUN  echo postfix postfix/main_mailer_type string "'Internet Site'" | debconf-set-selections && \
+    echo postfix postfix/mynetworks string "127.0.0.1/32" | debconf-set-selections && \
+    echo postfix postfix/mailname string $FAAS_SMTP_MYHOSTNAME | debconf-set-selections && \
+    apt-get -q -q update && \
+    apt-get install -y --no-install-recommends apt-utils && \
+    apt-get --yes install mailutils && \
+    apt-get --yes install postfix && \
+    apt-get --yes install telnet dnsutils  && \
+    apt-get --yes install supervisor && \
+    apt-get --yes --no-install-recommends install rsyslog && \
+    apt-get clean
+
+
+RUN \
+    postconf -e mydestination='$myhostname, localhost.$mydomain, localhost' && \
+    postconf -e smtpd_banner='$myhostname ESMTP $mail_name' && \
+    postconf -e unknown_local_recipient_reject_code='550' && \
+    postconf -e debug_peer_level='2' && \
+    postconf -e mail_owner='postfix' && \
+    postconf -e debug_peer_list='problem.domain' && \
+    postconf -# myhostname && \
+    postconf -# relayhost && \
+    postconf -# smtpd_relay_restrictions && \
+    postconf -# inet_protocols
+
+
+COPY ./conf/etc/supervisord.conf /etc/supervisord.conf
+COPY ./conf/etc/entrypoint /etc/entrypoint
+COPY ./conf/etc/aliases /etc/aliases
+
+EXPOSE 25/tcp
+
+ENTRYPOINT ["/etc/entrypoint"]
 
-EXPOSE     587
-CMD        [ "/bin/sh", "-c", "/run.sh" ]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..21e04a1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,33 @@
+# import config file
+config ?= conf/faas-smtp.cnf
+include $(config)
+export $(shell sed 's/=.*//' $(config))
+
+
+.DEFAULT_GOAL := help
+
+build: # build container
+	docker build -t ${FAAS_SMTP_IMAGE} --build-arg FAAS_SMTP_NAME=${FAAS_SMTP_NAME} --build-arg FAAS_SMTP_IMAGE=${FAAS_SMTP_IMAGE} --build-arg FAAS_SMTP_VERSION=${FAAS_SMTP_VERSION} .
+
+build-nc: # build container without caching
+	docker build --no-cache -t ${FAAS_SMTP_IMAGE} --build-arg FAAS_SMTP_NAME=${FAAS_SMTP_NAME} --build-arg FAAS_SMTP_IMAGE=${FAAS_SMTP_IMAGE} --build-arg FAAS_SMTP_VERSION=${FAAS_SMTP_VERSION} .
+
+run: # run container
+	docker run -i -t --detach --rm --env-file=$(config) -p=${FAAS_SMTP_PORT}:25 --name="${FAAS_SMTP_NAME}"  ${FAAS_SMTP_IMAGE}
+
+run-nd: # run container in no-deamon mode
+	docker run -i -t --rm --env-file=$(config) -p=${FAAS_SMTP_PORT}:25 --name="${FAAS_SMTP_NAME}" ${FAAS_SMTP_IMAGE}
+
+up: build run # build and run container
+
+logs: # print docker logs
+	docker logs ${FAAS_SMTP_NAME}
+
+stop: # stop container
+	docker stop ${FAAS_SMTP_NAME}
+
+version: # print current component version
+	@echo ${FAAS_SMTP_VERSION}
+
+help: # this help
+	@awk 'BEGIN {FS = ":.*?# "} /^[a-zA-Z_-]+:.*?# / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
diff --git a/README.md b/README.md
index 5615752..a2fd677 100644
--- a/README.md
+++ b/README.md
@@ -2,20 +2,32 @@
 
 ## structure
 `_conf/`_ - directory with configuration files
-`scripts/` - directory with utils and scripts
+
+`test/` - directory with tests and utils
 
 ## build image
 To build `faas-smtp` docker image 
 `
-cd scripts/helpers && ./build.sh
+make build
 `
 
 
 ## run image
 To run `faas-smtp` docker image
 `
-cd scripts/helpers && ./run.sh
+make run
 `
 
+## additional commands
+```
+build                          build container
+build-nc                       build container without caching
+run                            run container
+run-nd                         run container in no-deamon mode
+up                             build and run container
+logs                           print docker logs
+stop                           stop container
+version                        print current component version
+```
 
 
diff --git a/conf/etc/aliases b/conf/etc/aliases
new file mode 100644
index 0000000..d881891
--- /dev/null
+++ b/conf/etc/aliases
@@ -0,0 +1,96 @@
+#
+#  Aliases in this file will NOT be expanded in the header from
+#  Mail, but WILL be visible over networks or from /bin/mail.
+#
+#	>>>>>>>>>>	The program "newaliases" must be run after
+#	>> NOTE >>	this file is updated for any changes to
+#	>>>>>>>>>>	show through to sendmail.
+#
+
+# Basic system aliases -- these MUST be present.
+mailer-daemon:	postmaster
+postmaster:	root
+
+# General redirections for pseudo accounts.
+bin:		root
+daemon:		root
+adm:		root
+lp:		root
+sync:		root
+shutdown:	root
+halt:		root
+mail:		root
+news:		root
+uucp:		root
+operator:	root
+games:		root
+gopher:		root
+ftp:		root
+nobody:		root
+radiusd:	root
+nut:		root
+dbus:		root
+vcsa:		root
+canna:		root
+wnn:		root
+rpm:		root
+nscd:		root
+pcap:		root
+apache:		root
+webalizer:	root
+dovecot:	root
+fax:		root
+quagga:		root
+radvd:		root
+pvm:		root
+amanda:		root
+privoxy:	root
+ident:		root
+named:		root
+xfs:		root
+gdm:		root
+mailnull:	root
+postgres:	root
+sshd:		root
+smmsp:		root
+postfix:	root
+netdump:	root
+ldap:		root
+squid:		root
+ntp:		root
+mysql:		root
+desktop:	root
+rpcuser:	root
+rpc:		root
+nfsnobody:	root
+
+ingres:		root
+system:		root
+toor:		root
+manager:	root
+dumper:		root
+abuse:		root
+
+newsadm:	news
+newsadmin:	news
+usenet:		news
+ftpadm:		ftp
+ftpadmin:	ftp
+ftp-adm:	ftp
+ftp-admin:	ftp
+www:		webmaster
+webmaster:	root
+noc:		root
+security:	root
+hostmaster:	root
+info:		postmaster
+marketing:	postmaster
+sales:		postmaster
+support:	postmaster
+munin:		root
+
+# trap decode to catch security attacks
+decode:		root
+
+# Person who should get root's mail
+root:		__FAAS_SMTP_ROOT_ALIASES_LIST__
diff --git a/scripts/docker/run.sh b/conf/etc/entrypoint
similarity index 60%
rename from scripts/docker/run.sh
rename to conf/etc/entrypoint
index e5c7362..f376003 100755
--- a/scripts/docker/run.sh
+++ b/conf/etc/entrypoint
@@ -1,9 +1,19 @@
 #! /bin/bash
 
-# set -x
+
+# copy files to postfix jail location
+cp -f /etc/services /var/spool/postfix/etc/services
+cp -f /etc/resolv.conf /var/spool/postfix/etc/resolv.conf
+
+# updating postfix mail aliases
+sed -i "s/__FAAS_SMTP_ROOT_ALIASES_LIST__/$FAAS_SMTP_ROOT_ALIASES_LIST/g" /etc/aliases
+postalias /etc/aliases
+newaliases
 
 sed -i '/imklog/s/^/#/' /etc/rsyslog.conf
 
+
+
 function update_postfix_config_value() {
     local key=${1}
     local value=${2}
@@ -17,9 +27,13 @@ function update_postfix_config_value() {
 
 [ -z "${FAAS_SMTP_MYHOSTNAME}" ] && echo "FAAS_SMTP_MYHOSTNAME is not set" && exit 1
 [ -z "${FAAS_SMTP_INET_INTERFACES}" ] && echo "FAAS_SMTP_INET_INTERFACES is not set" && exit 1
+[ -z "${FAAS_SMTP_INET_PROTOCOLS}" ] && echo "FAAS_SMTP_INET_PROTOCOLS is not set" && exit 1
 
 update_postfix_config_value "myhostname" ${FAAS_SMTP_MYHOSTNAME}
 update_postfix_config_value "inet_interfaces" ${FAAS_SMTP_INET_INTERFACES}
+update_postfix_config_value "inet_protocols" ${FAAS_SMTP_INET_PROTOCOLS}
+update_postfix_config_value "mynetworks" "${FAAS_SMTP_POSTFIX_MYNETWORKS}"
+
 
 rm -f /var/spool/postfix/pid/master.pid
 exec supervisord -c /etc/supervisord.conf
diff --git a/conf/faas-smtp.cnf b/conf/faas-smtp.cnf
new file mode 100644
index 0000000..5a680d7
--- /dev/null
+++ b/conf/faas-smtp.cnf
@@ -0,0 +1,13 @@
+FAAS_SMTP_REPO=faas
+FAAS_SMTP_NAME=faas-smtp
+FAAS_SMTP_VERSION=1.0-SNAPSHOT
+FAAS_SMTP_IMAGE=${FAAS_SMTP_REPO}/${FAAS_SMTP_NAME}:${FAAS_SMTP_VERSION}
+
+FAAS_SMTP_MYHOSTNAME=test.faas.geant.net
+FAAS_SMTP_INET_INTERFACES=all
+FAAS_SMTP_INET_PROTOCOLS=all
+
+FAAS_SMTP_PORT=9025
+
+FAAS_SMTP_ROOT_ALIASES_LIST=janny@man.poznan.pl
+FAAS_SMTP_POSTFIX_MYNETWORKS=127.0.0.1/32 192.168.0.0/16 172.16.0.0/12 172.17.0.0/16 10.0.0.0/8
\ No newline at end of file
diff --git a/conf/faas-smtp.compose.yml b/conf/faas-smtp.compose.yml
deleted file mode 100644
index 7bbf43f..0000000
--- a/conf/faas-smtp.compose.yml
+++ /dev/null
@@ -1,14 +0,0 @@
-version: '3.1'
-
-services:
-  smtp:
-    image: ${FAAS_SMTP_IMAGE}
-    build:
-        context: ../
-        dockerfile: Dockerfile
-    restart: always
-    environment:
-    - FAAS_SMTP_MYHOSTNAME=${FAAS_SMTP_MYHOSTNAME}
-    - FAAS_SMTP_INET_INTERFACES=${FAAS_SMTP_INET_INTERFACES}
-    ports:
-        - "${FAAS_SMTP_PORT}:25"
diff --git a/conf/faas-smtp.env b/conf/faas-smtp.env
deleted file mode 100644
index 50a3ec6..0000000
--- a/conf/faas-smtp.env
+++ /dev/null
@@ -1,6 +0,0 @@
-FAAS_SMTP_IMAGE=faas/faas-smtp:1.0-SNAPSHOT
-
-FAAS_SMTP_MYHOSTNAME=test.faas.geant.net
-FAAS_SMTP_INET_INTERFACES=all
-
-FAAS_SMTP_PORT=2025
diff --git a/scripts/helpers/build.sh b/scripts/helpers/build.sh
deleted file mode 100755
index 5865c48..0000000
--- a/scripts/helpers/build.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/bash
-
-docker-compose --env-file ../../conf/faas-smtp.env -f ../../conf/faas-smtp.compose.yml build
diff --git a/scripts/helpers/run.sh b/scripts/helpers/run.sh
deleted file mode 100755
index 1ad7967..0000000
--- a/scripts/helpers/run.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/bash
-
-docker-compose --env-file ../../conf/faas-smtp.env -f ../../conf/faas-smtp.compose.yml up
diff --git a/test/send.pl b/test/send.pl
new file mode 100644
index 0000000..d148eff
--- /dev/null
+++ b/test/send.pl
@@ -0,0 +1,31 @@
+#! /usr/bin/perl
+
+use warnings;
+use strict;
+use Net::SMTP;
+
+my $smtpserver = 'localhost';
+my $smtpport = 9025;
+my $smtpuser   = 'postfix';
+my $smtppassword = '';
+
+#my $to = 'janny@man.poznan.pl';
+my $to = 'dariusz.janny@gmail.com';
+my $from = 'root@test.faas.geant.net';
+my $subject = 'subject no 2';
+
+my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 10, Debug => 1);
+die "Could not connect to server!\n" unless $smtp;
+
+# $smtp->auth($smtpuser, $smtppassword);
+$smtp ->mail($from);
+$smtp->to($to);
+$smtp->data();
+$smtp->datasend("From: $from\n");
+$smtp->datasend("To: $to\n");
+$smtp->datasend("Subject: $subject\n");
+$smtp->datasend("\n");
+$smtp->datasend("abc test\n");
+$smtp->dataend();
+
+$smtp->quit;
-- 
GitLab