Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
d90a3218b2 | |||
b73ab237e3 | |||
1e045964fa | |||
b159820f6c | |||
690484a097 | |||
fe5b9e1f25 | |||
71f84cd0a9 | |||
e4752fd039 | |||
117c93deaf | |||
6d0040415b | |||
8fe42e9163 | |||
937992a72e | |||
e1a78d560d | |||
b9fbc9138c | |||
5469659f1e | |||
5f56a1256b | |||
59abf24ce5 | |||
7cb1b69744 | |||
95423ebf63 | |||
2bcc807b91 | |||
bcd250d2b0 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@
|
||||
__pycache__
|
||||
dummy.py
|
||||
build
|
||||
mail4one.pyz
|
||||
mail4one*.pyz
|
||||
deploy_configs/config.json
|
||||
|
51
Makefile
51
Makefile
@ -1,23 +1,9 @@
|
||||
shell:
|
||||
MYPYPATH=`pipenv --venv`/lib/python3.11/site-packages pipenv shell
|
||||
|
||||
test:
|
||||
pipenv run python -m unittest discover
|
||||
|
||||
docker-tests:
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm -it python:3.11-alpine sh runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm -it python:3.10-alpine sh runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm -it python:3.11 sh runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm -it python:3.10 sh runtests.sh
|
||||
|
||||
requirements.txt: Pipfile.lock
|
||||
pipenv requirements > requirements.txt
|
||||
|
||||
build: clean requirements.txt
|
||||
python3 -m pip install -r requirements.txt --target build
|
||||
# Needs python3 >= 3.9, sed, git for build
|
||||
build: clean
|
||||
python3 -m pip install -r requirements.txt --no-compile --target build
|
||||
cp -r mail4one/ build/
|
||||
sed -i "s/DEVELOMENT/$(shell scripts/get_version.sh)/" build/mail4one/version.py
|
||||
python3 -m compileall build/mail4one -f
|
||||
rm -rf build/mail4one/__pycache__
|
||||
rm -rf build/*.dist-info
|
||||
python3 -m zipapp \
|
||||
--output mail4one.pyz \
|
||||
@ -28,3 +14,32 @@ build: clean requirements.txt
|
||||
clean:
|
||||
rm -rf build
|
||||
rm -rf mail4one.pyz
|
||||
|
||||
docker-tests:
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm python:3.11-alpine sh scripts/runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm python:3.10-alpine sh scripts/runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm python:3.11 sh scripts/runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm python:3.10 sh scripts/runtests.sh
|
||||
docker run --pull=always -v `pwd`:/app -w /app --rm python:3.9 sh scripts/runtests.sh
|
||||
|
||||
# ============================================================================
|
||||
|
||||
requirements.txt: Pipfile.lock
|
||||
pipenv requirements > requirements.txt
|
||||
|
||||
format:
|
||||
black mail4one/*py
|
||||
|
||||
build-dev: requirements.txt build
|
||||
|
||||
setup:
|
||||
pipenv install
|
||||
|
||||
cleanup:
|
||||
pipenv --rm
|
||||
|
||||
shell:
|
||||
MYPYPATH=`pipenv --venv`/lib/python3.11/site-packages pipenv shell
|
||||
|
||||
test:
|
||||
pipenv run python -m unittest discover
|
||||
|
1
Pipfile
1
Pipfile
@ -8,6 +8,7 @@ name = "pypi"
|
||||
[packages]
|
||||
aiosmtpd = "*"
|
||||
python-jata = "*"
|
||||
asyncbasehttp = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3"
|
||||
|
10
Pipfile.lock
generated
10
Pipfile.lock
generated
@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "5c16e20a67c73101d465516d657e21c6c1d3f853ae16dcfe782b7f9e8ba139e5"
|
||||
"sha256": "6b224d3d72187a39e4fba357ffb4b162dc28994bd869c7960fcfd5acbb56c959"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
@ -24,6 +24,14 @@
|
||||
"index": "pypi",
|
||||
"version": "==1.4.4.post2"
|
||||
},
|
||||
"asyncbasehttp": {
|
||||
"hashes": [
|
||||
"sha256:2cea7b61113bb1e69d1c90f32919f004e7f56a7e1e2955e8a6286ce40f9da188",
|
||||
"sha256:70fb983c4df630a9b63081cb8ef6d2e06beb51c89f42a88da6c9a9c7ebf9fb89"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==1.0"
|
||||
},
|
||||
"atpublic": {
|
||||
"hashes": [
|
||||
"sha256:0f40433219e124edf115c6c363808ca6f0e1cfa7d160d86b2fb94793086d1294",
|
||||
|
88
README.md
88
README.md
@ -1,44 +1,70 @@
|
||||
# mail4one
|
||||
# Mail4one
|
||||
|
||||
Mail server for single user #asyncio #python
|
||||
Personal mail server for a single user or a small family. Written in pure python with minimal dependencies.
|
||||
Designed for dynamic alias based workflow where a different alias is used for each purpose.
|
||||
|
||||
## Features
|
||||
# Getting started
|
||||
|
||||
* smtp server with STARTTLS
|
||||
* pop3 server with TLS
|
||||
* Both running on single thread using asyncio
|
||||
* Saves mails in simple Maildir format (i.e one file per email message)
|
||||
* After opening port, drops root privileges. So the process will not running as `nobody`
|
||||
1. Get a domain name
|
||||
1. Get a VPS (or a home server). Setup firewall rules for receive on port 25, 995, 465
|
||||
1. Setup [MX record](#dns-records-receiving)
|
||||
1. [Build](#building-from-source) / Download latest release - `mail4one.pyz`
|
||||
1. Generate `config.json` from [config.sample](deploy_configs/config.sample)
|
||||
1. Run `./mail4one.pyz -c config.json`
|
||||
1. Setup systemd service and TLS certificates. See [deploy_configs](deploy_configs/) for examples
|
||||
|
||||
## How to use
|
||||
# Sending email
|
||||
|
||||
echo -n "balki is awesome+<YOUR PASSWORD>" | sha256sum
|
||||
pipenv install
|
||||
sudo $(pipenv --venv)/bin/python ./run.py --certfile /etc/letsencrypt/live/your.domain.com/fullchain.pem --keyfile /etc/letsencrypt/live/your.domain.com/privkey.pem /var/mails --password_hash <PASSWORD_HASH_FROM_ABOVE>
|
||||
Mail4one only takes care of receiving and serving email. For sending email, use an external service like below
|
||||
|
||||
## Just pop server for debugging
|
||||
* https://www.smtp2go.com/pricing/
|
||||
* https://www.mailgun.com/pricing/
|
||||
* https://sendgrid.com/free/
|
||||
|
||||
pipenv run python -m mail4one.pop3 /path/to/mails 9995 your_password
|
||||
Most of them have generous free tier which is more than enough for personal use.
|
||||
|
||||
## Nextups
|
||||
Sending email is tricky. Even if everything is correctly setup (DMARC, DKIM, SPF), popular email vendors like google, microsoft may mark emails sent from your IP as spam for no reason.
|
||||
|
||||
* Support sending emails - Also support for popular services like mailgun/sendgrid
|
||||
* Smart assistant like functionality. For e.g.
|
||||
* You don't need all emails of package deliver status. Just the latest one would be enough.
|
||||
* Some type of emails can auto expire. Old newsletters are not very helpful
|
||||
* Aggregate emails for weekend reading.
|
||||
* Small webserver
|
||||
* SPAM filtering - not that important as you can use unique addresses for each service. e.g. facebook@mydomian.com, bankac@mydomain.com, reddit@mydomain.com etc. You can easily figure out who sold your address to spammers and block it.
|
||||
# Community
|
||||
|
||||
## Goals
|
||||
* Intended to be used for one person. So won't have features that don't make sense in this context. e.g. LDAP AUTH, Mail quota, etc,
|
||||
* Supports only python3.7. No plans to support older versions
|
||||
Original source is at https://gitea.balki.me/balki/mail4one
|
||||
|
||||
## Known to work
|
||||
* Server: Google Cloud f1-micro with Ubuntu 18.04 - Always Free instance
|
||||
* Clients: thunderbird, evolution, k9mail
|
||||
* smtp: Received email from all. Didn't see any drops. Tested from gmail, protonmail, reddit and few others
|
||||
For issues, pull requests, discussions, please use github mirror: https://github.com/mail4one/mail4one
|
||||
|
||||
## Contribution
|
||||
# Documentation
|
||||
|
||||
Pull requests and issues welcome
|
||||
See files under [deploy_configs](deploy_configs/) for configuring and deploying to a standard systemd based linux system (e.g. debian, ubuntu, fedora, archlinux etc). [config.sample](deploy_configs/config.sample) has inline comments for more details. Feel free create github issue/discussions for support.
|
||||
|
||||
## DNS Records (Receiving)
|
||||
|
||||
If you want to receive email for `john@example.com` and your VPS IP address is `1.2.3.4`. Following record needs to be created
|
||||
|
||||
|Type | Name | Target | Notes |
|
||||
|------|------------------|----------------------|------------------------------------------------------|
|
||||
| A | mail.example.com | `1.2.3.4` | |
|
||||
| AAAA | mail.example.com | `abcd:1234::1234::1` | Optional, add if available |
|
||||
| MX | example.com | `mail.example.com` | |
|
||||
| MX | sub.example.com | `mail.example.com` | Optional, to receive emails like foo@sub.example.com |
|
||||
|
||||
For sending emails `DMARC`, `DKIM` and `SPF` records need to be set. Please refer to email [sending](#sending-email) provider for details.
|
||||
|
||||
# Building from source
|
||||
|
||||
Make sure to have make, git, python >= 3.9, and pip installed in your system and run below
|
||||
|
||||
make build
|
||||
|
||||
This should generate `mail4one.pyz` in current folder. This is a [executable python archive](https://docs.python.org/3/library/zipapp.html). Should be runnable as `./mail4one.pyz` or as `python3 mail4one.pyz`.
|
||||
|
||||
# Roadmap (Planned features for future)
|
||||
|
||||
* Other ways to install and update (PIP, AUR, docker etc)
|
||||
* Write dedicated documentation
|
||||
* Test with more email clients ([Thunderbird](https://www.thunderbird.net/) and [k9mail](https://k9mail.app/) are tested now)
|
||||
* IMAP support
|
||||
* Web UI for editing config
|
||||
* Support email submission from client to forward to other senders or direct delivery
|
||||
* Optional SPAM filtering
|
||||
* Optional DMARC,SPF,DKIM verification
|
||||
* Webmail Client
|
||||
* Web UI to view graphs and smart reports
|
||||
|
33
TODO.md
33
TODO.md
@ -1,33 +0,0 @@
|
||||
# TODO
|
||||
|
||||
DEV
|
||||
1. unittests
|
||||
1. Web interface
|
||||
1. Custom email processing
|
||||
1. Refactor smtp controller
|
||||
1. pip installable package
|
||||
1. Improve logging with timestamp, peer info, stats
|
||||
1. custom smtp with auth
|
||||
1. Filter domain name
|
||||
2. Support multiple subdomains and save in different folders
|
||||
1. smtp2go integration
|
||||
1. metrics
|
||||
2. imap
|
||||
3. handle connection disconnect
|
||||
4. rethink deleted items
|
||||
|
||||
MASTER
|
||||
1. tests!
|
||||
2. docker support
|
||||
3. multi domain
|
||||
4. [pop] auto delete
|
||||
5. [pop] TOP command
|
||||
6. merge with cloud_test branch
|
||||
|
||||
7. User timeout for POP
|
||||
8. unittests
|
||||
9. Web interface
|
||||
10. Custom email processing
|
||||
11. Refactor smtp controller
|
||||
12. pip installable package
|
||||
13. Listen on port 465 for smtp too
|
114
deploy_configs/README.md
Normal file
114
deploy_configs/README.md
Normal file
@ -0,0 +1,114 @@
|
||||
# Deployment command line example
|
||||
|
||||
Example terminal session for deploying. ssh to your VPS and follow along. Minor differences may be required. e.g. if you are already root, skip `sudo`. If curl is missing, use wget.
|
||||
|
||||
## Check python version
|
||||
|
||||
Python version should be a supported (as of now 3.9 and above)
|
||||
```sh
|
||||
python3 -V
|
||||
```
|
||||
|
||||
## Choose release
|
||||
```sh
|
||||
RELEASE=v1.0
|
||||
```
|
||||
|
||||
## Download App
|
||||
```sh
|
||||
curl -OL "https://gitea.balki.me/balki/mail4one/releases/download/$RELEASE/mail4one.pyz"
|
||||
chmod 555 mail4one.pyz
|
||||
```
|
||||
|
||||
## Download sample configurations
|
||||
```sh
|
||||
curl -OL "https://gitea.balki.me/balki/mail4one/raw/tag/$RELEASE/deploy_configs/mail4one.service"
|
||||
curl -OL "https://gitea.balki.me/balki/mail4one/raw/tag/$RELEASE/deploy_configs/mail4one.conf"
|
||||
curl -OL "https://gitea.balki.me/balki/mail4one/raw/tag/$RELEASE/deploy_configs/mail4one_cert_copy.sh"
|
||||
```
|
||||
|
||||
## Generate Password hash
|
||||
|
||||
This can be done in any machine. Do this once for each user. Every time a new hash is generated as a random salt is used. Even if you are using the same password for multiple clients, it is recommended to generate different hashes for each.
|
||||
|
||||
```sh
|
||||
./mail4one.pyz -g
|
||||
./mail4one.pyz -g <password> # also works but the password is saved in the shell commandline history
|
||||
```
|
||||
|
||||
## Generate config.json
|
||||
|
||||
Edit [config.sample](config.sample) in your local machine and convert to config.json (See [here](./config.sample#L5) for some tools).
|
||||
|
||||
Then copy the config.json to your vps
|
||||
```sh
|
||||
scp config.json user@vps:~/
|
||||
# or run below in vps terminal
|
||||
cat > config.json
|
||||
<paste json config from clibboard
|
||||
<Ctrl + D>
|
||||
|
||||
# move to /etc
|
||||
|
||||
# This should show number of lines in your config
|
||||
wc -l config.json
|
||||
sudo mv config.json /etc/mail4one/config.json
|
||||
```
|
||||
|
||||
## Create mail4one user
|
||||
|
||||
```sh
|
||||
sudo mkdir -p /etc/sysusers.d/
|
||||
sudo cp mail4one.conf /etc/sysusers.d/
|
||||
sudo systemctl restart systemd-sysusers
|
||||
# This should show the new user created
|
||||
id mail4one
|
||||
```
|
||||
## Copy app
|
||||
```sh
|
||||
sudo cp mail4one.pyz /usr/local/bin/mail4one
|
||||
# This should show executable permissions and should be owned by root
|
||||
ls -l /usr/local/bin/mail4one
|
||||
```
|
||||
|
||||
## Setup mail4one.service
|
||||
```sh
|
||||
sudo cp mail4one.service /etc/systemd/system/mail4one.service
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now mail4one.service
|
||||
systemctl status mail4one
|
||||
```
|
||||
Above command should fail as the TLS certificates don't exist yet.
|
||||
|
||||
## Setup TLS certificates
|
||||
Install [certbot](https://certbot.eff.org/) and run below command. Follow instructions to create TLS certificates. Usually you want certificate for domain name like `mail.example.com`
|
||||
```sh
|
||||
sudo certbot certonly
|
||||
sudo cp /etc/letsencrypt/live/mail.example.com/{fullchain,privkey}.pem /var/lib/mail4one/certs/
|
||||
sudo chown mail4one:mail4one /var/lib/mail4one/certs/{fullchain,privkey}.pem
|
||||
|
||||
# Edit mail4one_cert_copy.sh to update your domain name
|
||||
sudo cp mail4one_cert_copy.sh /etc/letsencrypt/renewal-hooks/deploy/
|
||||
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/mail4one_cert_copy.sh
|
||||
```
|
||||
## Restart service and check logs
|
||||
```sh
|
||||
sudo systemctl restart mail4one.service
|
||||
systemctl status mail4one.service
|
||||
cat /var/log/mail4one/mail4one.log
|
||||
```
|
||||
|
||||
## Testing dns and firewall
|
||||
In vps
|
||||
```sh
|
||||
mkdir test_dir
|
||||
touch test_dir/{a,b,c}
|
||||
cd test_dir
|
||||
python3 -m http.server 25
|
||||
```
|
||||
In local machine or a browser
|
||||
You should see file listing a, b, c. Repeat for port 465, 995 to make sure firewall rules and dns is working
|
||||
```sh
|
||||
curl http://mail.example.com:25
|
||||
```
|
||||
If not working, refer to VPS settings and OS firewall settings.
|
144
deploy_configs/config.sample
Normal file
144
deploy_configs/config.sample
Normal file
@ -0,0 +1,144 @@
|
||||
# NOTE: Sample config is provided in yaml format for easy editing
|
||||
# mail4one needs a json config, Please convert the config to json before passing to app
|
||||
# This is to avoid yaml dependency in the app
|
||||
#
|
||||
# Some tools to convert to json:
|
||||
# If you have `go` in your system (https://go.dev/)
|
||||
# go run github.com/mikefarah/yq/v4@latest -oj -P . config.sample > config.json
|
||||
#
|
||||
# If you have `pipx` in your system (https://pypa.github.io/pipx/)
|
||||
# pipx run yq . config.sample > config.json
|
||||
#
|
||||
# or a browser:
|
||||
# https://onlineyamltools.com/convert-yaml-to-json
|
||||
|
||||
default_tls: # Will be used by both pop and smtp servers
|
||||
# If using certbot(https://certbot.eff.org/),
|
||||
# the following files will be here /etc/letsencrypt/live/<domain name>
|
||||
# Use mail4one_cert_copy.sh to automaticallly copy on renewal
|
||||
certfile: /var/lib/mail4one/certs/fullchain.pem
|
||||
keyfile: /var/lib/mail4one/certs/privkey.pem
|
||||
|
||||
# default_host: '0.0.0.0'
|
||||
|
||||
logging:
|
||||
# Setup logrotate(https://github.com/logrotate/logrotate) if needed
|
||||
logfile: /var/log/mail4one/mail4one.log
|
||||
|
||||
mails_path: /var/lib/mail4one/mails
|
||||
|
||||
matches:
|
||||
# only <to> address is matched. (sent by smtp RCPT command)
|
||||
# address is converted to lowercase before matching
|
||||
- name: example.com
|
||||
addr_rexs:
|
||||
- .*@example.com
|
||||
|
||||
- name: promotion-spammers
|
||||
addrs:
|
||||
- twitter@example.com
|
||||
- random-app-not-used-anymore@example.com
|
||||
- flyer-walmart@example.com
|
||||
|
||||
- name: john
|
||||
addrs:
|
||||
- john.doe@example.com # Tip: Dont use this. Always use a different alias, this way there is no address for spammers
|
||||
- secret.name@example.com
|
||||
- john.facebook@example.com
|
||||
|
||||
- name: jane
|
||||
addrs:
|
||||
- jane.doe@example.com
|
||||
- jane.instagram@example.com
|
||||
|
||||
- name: jane-all
|
||||
addr_rexs:
|
||||
- jane.*@example.com
|
||||
|
||||
- name: shared
|
||||
addrs:
|
||||
- kidschool@example.com
|
||||
- mortgage@example.com
|
||||
- water@example.com
|
||||
- electricity@example.com
|
||||
- airbnb@example.com
|
||||
|
||||
boxes:
|
||||
# Mails saved in maildir format under <mails_path>/<name>/new
|
||||
- name: default_null_mbox # Means, the mails are dropped
|
||||
rules:
|
||||
- match_name: example.com
|
||||
negate: true # Anything mail that does not match '.*@example.com'
|
||||
stop_check: true # No further rules will be checked, mail is dropped
|
||||
|
||||
# Mailbox to store non-interesting emails but occasionally have a useful mail
|
||||
# Create a second account in your email client and disable notification
|
||||
- name: promotion-box
|
||||
rules:
|
||||
- match_name: promotion-spammers
|
||||
stop_check: true
|
||||
|
||||
- name: johnsmbox
|
||||
rules:
|
||||
- match_name: john
|
||||
- match_name: shared
|
||||
## To receive all emails excluding jane's personal emails
|
||||
# - match_name: jane
|
||||
# negate: true
|
||||
|
||||
- name: janesmbox
|
||||
rules:
|
||||
- match_name: jane
|
||||
- match_name: shared
|
||||
- match_name: jane-all
|
||||
|
||||
- name: all
|
||||
rules:
|
||||
# matches all emails except those are not for 'example.com', which are dropped before
|
||||
- match_name: default_match_all
|
||||
|
||||
users: # Used only by the pop server, smtp is for receiving mails only. No auth is used
|
||||
- username: johnmobile
|
||||
# Generated using below command. Will produce different hash each time (salt is random)
|
||||
# ./mail4one.pyz -g johnsecretpassword
|
||||
password_hash: AEH6JG3IZR3ASA2ORJHQ62YTR6PHFRP6PAXQ6RI2VZFXAT5M6VAATE373PGCUHBJTLIDOQV6UJKICP2JTKDE3QXP7ROJ227QYFQDAXPP4LY4TLPTEHUZG7D7X6VKWZ4BVCASYCD3SSNQ555AZPIFMDAV
|
||||
mbox: johnsmbox
|
||||
|
||||
# **NOTE** Use different username for each email client.
|
||||
# Otherwise emails may appear in only one client
|
||||
- username: johnlaptop
|
||||
password_hash: AEH6JG3IZR3ASA2ORJHQ62YTR6PHFRP6PAXQ6RI2VZFXAT5M6VAATE373PGCUHBJTLIDOQV6UJKICP2JTKDE3QXP7ROJ227QYFQDAXPP4LY4TLPTEHUZG7D7X6VKWZ4BVCASYCD3SSNQ555AZPIFMDAV
|
||||
mbox: johnsmbox
|
||||
|
||||
# Second account to not clutter main mailbox. Disable notification for this account
|
||||
- username: john-mobile-promotion
|
||||
password_hash: AGBD47ZYBA7BMUQY25YDTYQWVPJFDBTLIICKFP2IL2GI4M7AO2LIVIZXTY6N25KBRLOEC7TLXGAFW7SSQEBKCG7U3FJNKW6RZWZBS3ABSP2U53BBIOCXZNWPXJGWAQ6WFXIF7T4YQJZD5QLF2OO4JZ45
|
||||
mbox: promotion-box
|
||||
|
||||
- username: janemobile
|
||||
password_hash: AGQNPATXU7PP7LDD6DZ4HFLUUHRJDHFQKKKRLVLGOIIEHC7TPOZF7NTXGDAIGDNHF62RAH4N44DB46O3VC4TBOLE5XHY6S77YPLTWCNAHGONEOZYO6YWJ3NHLKOHFJLF6BOHNMCI3RCPWXWXQPHSFDQR
|
||||
mbox: janesmbox
|
||||
|
||||
- username: family_desktop # Catch all for backup
|
||||
password_hash: AGBD47ZYBA7BMUQY25YDTYQWVPJFDBTLIICKFP2IL2GI4M7AO2LIVIZXTY6N25KBRLOEC7TLXGAFW7SSQEBKCG7U3FJNKW6RZWZBS3ABSP2U53BBIOCXZNWPXJGWAQ6WFXIF7T4YQJZD5QLF2OO4JZ45
|
||||
mbox: all
|
||||
|
||||
servers:
|
||||
- server_type: pop
|
||||
## default values
|
||||
# port: 995
|
||||
# host: '0.0.0.0'
|
||||
# tls: default # Uses default_tls config
|
||||
- server_type: smtp
|
||||
## default values
|
||||
# port: 465
|
||||
# host: '0.0.0.0'
|
||||
# tls: default # Uses default_tls config
|
||||
# tls: disable # disable tls and receive emails in plain text only
|
||||
- server_type: smtp_starttls
|
||||
## default values
|
||||
# port: 25
|
||||
# host: '0.0.0.0'
|
||||
# tls: default # Uses default_tls config
|
||||
|
||||
# vim: ft=yaml
|
@ -1,3 +1,7 @@
|
||||
# This file should be copied to /etc/sysusers.d/mail4one.conf
|
||||
# Then either restart the system or run `systemctl restart systemd-sysusers`
|
||||
# That should create a system user 'mail4one'
|
||||
#
|
||||
# See sysusers.d(5) for details.
|
||||
|
||||
u mail4one - "Personal Mail server"
|
||||
|
@ -1,4 +1,7 @@
|
||||
# mail4one.service
|
||||
# This file should be copied to /etc/systemd/system/mail4one.service
|
||||
# Quickstart
|
||||
# systemctl daemon-reload
|
||||
# systemctl enable --now mail4one.service
|
||||
|
||||
[Unit]
|
||||
Description=Personal Mail server
|
||||
@ -7,16 +10,25 @@ After=network.target network-online.target
|
||||
Requires=network-online.target
|
||||
|
||||
[Service]
|
||||
|
||||
# This user should already exist. See mail4one.conf for creating user with sysusers
|
||||
User=mail4one
|
||||
ExecStart=/usr/local/bin/mail4one --config /etc/mail4one/config.json
|
||||
PrivateTmp=true
|
||||
ProtectSystem=full
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
|
||||
StateDirectory=mail4one
|
||||
# Below allows to bind to port < 1024. Standard ports are 25, 465, 995
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
|
||||
|
||||
StateDirectory=mail4one/certs mail4one/mails
|
||||
StateDirectoryMode=0750
|
||||
LogsDirectory=mail4one
|
||||
WorkingDirectory=/var/lib/mail4one
|
||||
|
||||
ProtectSystem=strict
|
||||
PrivateTmp=true
|
||||
ProtectHome=yes
|
||||
ProtectProc=invisible
|
||||
NoNewPrivileges=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
@ -1,6 +1,8 @@
|
||||
#!/bin/sh
|
||||
|
||||
# certbot deploy hook to copy certificates to mail4one when renewed.
|
||||
# Initial setup, Install certbot(https://certbot.eff.org/) and run `certbot certonly` as root
|
||||
#
|
||||
# This file is supposed to be copied to /etc/letsencrypt/renewal-hooks/deploy/
|
||||
# Change the mail domain to the one on MX record
|
||||
|
||||
@ -10,7 +12,7 @@ set -x
|
||||
if [ "$RENEWED_DOMAINS" = "mail.mydomain.com" ]
|
||||
then
|
||||
mkdir -p /var/lib/mail4one/certs
|
||||
chmod 500 /var/lib/mail4one/certs
|
||||
chmod 750 /var/lib/mail4one/certs
|
||||
chown mail4one:mail4one /var/lib/mail4one/certs
|
||||
cp "$RENEWED_LINEAGE/fullchain.pem" /var/lib/mail4one/certs/
|
||||
cp "$RENEWED_LINEAGE/privkey.pem" /var/lib/mail4one/certs/
|
||||
|
1
jsdev/.gitignore
vendored
Normal file
1
jsdev/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
tmp
|
44
jsdev/air.toml
Normal file
44
jsdev/air.toml
Normal file
@ -0,0 +1,44 @@
|
||||
root = "."
|
||||
testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
bin = "./tmp/main"
|
||||
cmd = "cp ./restart.sh ./tmp/main"
|
||||
delay = 0
|
||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||
exclude_file = []
|
||||
exclude_regex = ["_test.go"]
|
||||
exclude_unchanged = false
|
||||
follow_symlink = false
|
||||
full_bin = ""
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
rerun = false
|
||||
rerun_delay = 500
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
build = "yellow"
|
||||
main = "magenta"
|
||||
runner = "green"
|
||||
watcher = "cyan"
|
||||
|
||||
[log]
|
||||
main_only = false
|
||||
time = false
|
||||
|
||||
[misc]
|
||||
clean_on_exit = false
|
||||
|
||||
[screen]
|
||||
clear_on_rebuild = false
|
||||
keep_scroll = true
|
473
jsdev/index.html
Normal file
473
jsdev/index.html
Normal file
@ -0,0 +1,473 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
|
||||
<title>Mail4one Web config</title>
|
||||
<script type="application/json" id="m41config">
|
||||
{
|
||||
"matches": [
|
||||
{
|
||||
"name": "mydomain",
|
||||
"addr_rexs": [
|
||||
".*@mydomain.com",
|
||||
".*@m.mydomain.com"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "personal",
|
||||
"addrs": [
|
||||
"first.last@mydomain.com",
|
||||
"secret.name@mydomain.com"
|
||||
]
|
||||
}
|
||||
],
|
||||
"boxes": [
|
||||
{
|
||||
"name": "spam",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "mydomain",
|
||||
"negate": true,
|
||||
"stop_check": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "important",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "personal"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "all",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "default_match_all"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"users": [
|
||||
{
|
||||
"username": "mymobile",
|
||||
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
|
||||
"mbox": "important"
|
||||
},
|
||||
{
|
||||
"username": "mydesk",
|
||||
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
|
||||
"mbox": "all"
|
||||
}
|
||||
]
|
||||
}
|
||||
</script>
|
||||
<style>
|
||||
|
||||
td {
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
table {
|
||||
border: 2px solid;
|
||||
}
|
||||
|
||||
thead {
|
||||
background-color: orange;
|
||||
}
|
||||
|
||||
h1.page-title {
|
||||
background-color: orange;
|
||||
}
|
||||
|
||||
h3 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.outer {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
background: grey;
|
||||
}
|
||||
|
||||
.inner {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
border: 2px solid;
|
||||
background-color: lightyellow;
|
||||
padding: 10px;
|
||||
justify-content: space-around;
|
||||
margin: 10px;
|
||||
width: 1200px;
|
||||
}
|
||||
.m41-box {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 3fr;
|
||||
margin: 10px;
|
||||
// background-color: lightblue;
|
||||
}
|
||||
|
||||
.multiline {
|
||||
text-align: left;
|
||||
padding: 5px;
|
||||
padding-left: 15px;
|
||||
}
|
||||
|
||||
|
||||
#web-cfg-matches {
|
||||
width: 90%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#web-cfg-matches tbody tr:nth-of-type(odd) {
|
||||
background: lightblue;
|
||||
}
|
||||
|
||||
#web-cfg-matches tbody tr:nth-of-type(even) {
|
||||
background: lightgrey;
|
||||
}
|
||||
|
||||
#web-cfg-boxes {
|
||||
text-align: center;
|
||||
width: 90%;
|
||||
// display: grid;
|
||||
// grid-template-columns: 1fr ;
|
||||
//justify-content: space-around;
|
||||
// justify-content: flex-start;
|
||||
// background-color: blue;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:nth-of-type(odd) {
|
||||
background: lightblue;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:nth-of-type(even) {
|
||||
background: lightgrey;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody tr td div.dummy{
|
||||
display: none;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody tr td div button.dummy {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:first-of-type tr td.box div button.up.dummy {
|
||||
display: block;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:first-of-type tr td.box div button.up.real {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:last-of-type tr td.box div button.down.dummy {
|
||||
display: block;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:last-of-type tr td.box div button.down.real {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:only-of-type tr td.box div.real {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#web-cfg-boxes tbody:only-of-type tr td.box div.dummy {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
td.box {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.button-group {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
</style>
|
||||
<script type="application/javascript">
|
||||
"use strict"
|
||||
|
||||
// Globals
|
||||
let server_config
|
||||
let matches_table
|
||||
let match_row_template
|
||||
let boxes_table
|
||||
let box_template
|
||||
// let rule_template
|
||||
|
||||
function initGlobals() {
|
||||
|
||||
server_config = JSON.parse(document.getElementById('m41config').text)
|
||||
matches_table = document.getElementById("web-cfg-matches")
|
||||
match_row_template = document.getElementById("web-cfg-matches-row")
|
||||
boxes_table = document.getElementById("web-cfg-boxes")
|
||||
box_template = document.getElementById("web-cfg-box")
|
||||
// rule_template = document.getElementById("web-cfg-boxes-rule-li")
|
||||
}
|
||||
|
||||
function populate_match_table(matches_config) {
|
||||
|
||||
for (const { name: match_name, addrs, addr_rexs } of matches_config) {
|
||||
|
||||
const [match_type, match_values] = (() => {
|
||||
|
||||
if (addrs != undefined) {
|
||||
return ["addrs", addrs]
|
||||
} else {
|
||||
return ["addr_rexs", addr_rexs]
|
||||
}
|
||||
})()
|
||||
|
||||
|
||||
addMatchRow()
|
||||
const last_row = matches_table.tBodies[0].lastElementChild
|
||||
const [ ,name_cell, {firstElementChild: type_select}, value_cell ] = last_row.cells
|
||||
|
||||
name_cell.innerText = match_name
|
||||
type_select.value = match_type
|
||||
value_cell.innerText = match_values.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
function extract_match_table() {
|
||||
|
||||
let matches = []
|
||||
for (let row of matches_table.tBodies[0].rows) {
|
||||
|
||||
const [ ,name_cell, {firstElementChild: type_select}, value_cell ] = row.cells
|
||||
let m = {"name" : name_cell.innerText}
|
||||
switch (type_select.value) {
|
||||
case "addrs":
|
||||
m["addrs"] = value_cell.innerText.split("\n")
|
||||
break
|
||||
case "addr_rexs":
|
||||
m["addr_rexs"] = value_cell.innerText.split("\n")
|
||||
break
|
||||
}
|
||||
matches.push(m)
|
||||
}
|
||||
return matches
|
||||
}
|
||||
|
||||
function addMatchRow() {
|
||||
|
||||
let row_clone = match_row_template.content.cloneNode(true)
|
||||
matches_table.tBodies[0].appendChild(row_clone)
|
||||
}
|
||||
|
||||
function populate_boxes_list(boxes_config) {
|
||||
|
||||
for (const {name:box_name, rules} of boxes_config) {
|
||||
|
||||
addBox()
|
||||
const tbody = boxes_table.lastElementChild
|
||||
const box = tbody.firstElementChild
|
||||
// console.log(box)
|
||||
const [,
|
||||
{children: [box_text, ]},
|
||||
,
|
||||
{firstElementChild: match_select},
|
||||
{firstElementChild: negate_check},
|
||||
{firstElementChild: stop_check},
|
||||
] = box.children
|
||||
|
||||
box_text.value = box_name
|
||||
|
||||
const [first_rule, ...rest] = rules
|
||||
|
||||
const {match_name, negate = false, stop = false} = first_rule
|
||||
|
||||
match_select.value = match_name
|
||||
negate_check.checked = negate
|
||||
stop_check.checked = stop
|
||||
|
||||
for (const {match_name, negate = false, stop_check = false} of rest ) {
|
||||
|
||||
addRule(box)
|
||||
const rule = tbody.lastElementChild
|
||||
const [,
|
||||
{firstElementChild: match_select},
|
||||
{firstElementChild: negateCheck},
|
||||
{firstElementChild: stopCheck}
|
||||
] = rule.children
|
||||
match_select.value = match_name
|
||||
negate_check.checked = negate
|
||||
stop_check.checked = stop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addRule(box) {
|
||||
box.parentElement.appendChild(box.cloneNode(true))
|
||||
box.children[0].rowSpan++;
|
||||
box.children[1].rowSpan++;
|
||||
const newrule = box.parentElement.lastElementChild
|
||||
newrule.removeClass("fist-tr")
|
||||
newrule.firstElementChild.remove()
|
||||
newrule.firstElementChild.remove()
|
||||
}
|
||||
|
||||
function addBox() {
|
||||
let box_clone = box_template.content.cloneNode(true)
|
||||
boxes_table.appendChild(box_clone)
|
||||
}
|
||||
|
||||
|
||||
function moveUp(button) {
|
||||
const li = button.parentElement.parentElement.parentElement
|
||||
if (li.previousElementSibling != null) {
|
||||
li.parentNode.insertBefore(li, li.previousElementSibling)
|
||||
}
|
||||
}
|
||||
|
||||
function moveDown(button) {
|
||||
const li = button.parentElement.parentElement.parentElement
|
||||
if (li.nextElementSibling != null) {
|
||||
li.parentNode.insertBefore(li.nextElementSibling, li)
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
|
||||
initGlobals()
|
||||
populate_match_table(server_config["matches"])
|
||||
save()
|
||||
document.getElementById("before").innerText = JSON.stringify(server_config["matches"], null, 2)
|
||||
populate_boxes_list(server_config["boxes"])
|
||||
}
|
||||
|
||||
function save() {
|
||||
|
||||
const matches = extract_match_table()
|
||||
document.getElementById("after").innerText = JSON.stringify(matches, null, 2)
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body onload="main()">
|
||||
<template id="web-cfg-matches-row">
|
||||
<tr>
|
||||
<td><button onClick="this.parentElement.parentElement.remove()">✗</button></td>
|
||||
<td contentEditable></td>
|
||||
<td>
|
||||
<select>
|
||||
<option value="addrs">List of addresses</option>
|
||||
<option value="addr_rexs">List of regexes for addresses</option>
|
||||
</select>
|
||||
</td>
|
||||
<td contentEditable class="multiline"></td>
|
||||
</tr>
|
||||
</template>
|
||||
<template id="web-cfg-box">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="box">
|
||||
<div class="button-group dummy">
|
||||
<button disabled>✗</button>
|
||||
<button disabled>↑</button>
|
||||
<button disabled>↓</button>
|
||||
</div>
|
||||
<div class="button-group real">
|
||||
<button onClick="this.parentElement.parentElement.parentElement.parentElement.remove()">✗</button>
|
||||
<button onClick="moveBoxUp(this)" class="real up">↑</button>
|
||||
<button disabled class="dummy up">↑</button>
|
||||
<button onClick="moveBoxDown(this)" class="real down">↓</button>
|
||||
<button disabled class="dummy down">↓</button>
|
||||
</div>
|
||||
<button onClick="addRule(this.parentElement.parentElement)">+</button>
|
||||
</td>
|
||||
<td contentEditable>
|
||||
<input type="text" contentEditable>
|
||||
</td>
|
||||
<td class="rule">
|
||||
<div class="button-group dummy">
|
||||
<button disabled>✗</button>
|
||||
<button disabled>↑</button>
|
||||
<button disabled>↓</button>
|
||||
</div>
|
||||
<div class="button-group real">
|
||||
<button onClick="this.parentElement.parentElement.parentElement.remove()">✗</button>
|
||||
<button onClick="moveRuleUp(this)" class="real up">↑</button>
|
||||
<button disabled class="dummy up">↑</button>
|
||||
<button onClick="moveRuleDown(this)" class="real down">↓</button>
|
||||
<button disabled class="dummy down">↓</button>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<select></select>
|
||||
</td>
|
||||
<td>
|
||||
<input type=checkbox>
|
||||
</td>
|
||||
<td>
|
||||
<input type=checkbox>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</template>
|
||||
<div class="outer">
|
||||
<div class="inner">
|
||||
<h1 class="page-title">Mail4one Web config</h1>
|
||||
<div id="top-menu">
|
||||
<button onClick="save()">Previous</button>
|
||||
<button onClick="save()">Matches</button>
|
||||
<button onClick="save()">Boxes</button>
|
||||
<button onClick="save()">Users</button>
|
||||
<button onClick="save()">JSON</button>
|
||||
<button onClick="save()">Next</button>
|
||||
</div>
|
||||
<div id="match-page">
|
||||
<h3> Matches </h3>
|
||||
<table id="web-cfg-matches">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
<button onClick="addMatchRow()">+</button>
|
||||
</th>
|
||||
<th>Match</th>
|
||||
<th>Type</th>
|
||||
<th>Values</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id=box-page">
|
||||
<h3>Boxes</h3>
|
||||
|
||||
<table id="web-cfg-boxes">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><button onClick="addBox()">+</button></th>
|
||||
<th>Mailbox</th>
|
||||
<th></th>
|
||||
<th>Match</th>
|
||||
<th>Invert</th>
|
||||
<th>Stop</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
|
||||
<h3>Before</h3>
|
||||
<pre id="before"></pre>
|
||||
<hr>
|
||||
<h3>After</h3>
|
||||
<pre id="after"></pre>
|
||||
<hr>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
11
jsdev/restart.sh
Executable file
11
jsdev/restart.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
# kill "$(cat current.pid)"
|
||||
|
||||
# echo "starting python server"
|
||||
|
||||
python3 -m http.server
|
||||
|
||||
# echo $! > current.pid
|
||||
|
||||
# echo "started python server"
|
@ -1,3 +0,0 @@
|
||||
from .server import main
|
||||
|
||||
main()
|
@ -1,7 +1,7 @@
|
||||
import json
|
||||
import re
|
||||
import logging
|
||||
from typing import Callable
|
||||
from typing import Callable, Union, Optional
|
||||
from jata import Jata, MutableDefault
|
||||
|
||||
|
||||
@ -41,46 +41,47 @@ class TLSCfg(Jata):
|
||||
|
||||
|
||||
class ServerCfg(Jata):
|
||||
server_type: str
|
||||
host: str = "default"
|
||||
port: int
|
||||
# disabled: bool = False
|
||||
tls: TLSCfg | str = "default"
|
||||
tls: Union[TLSCfg, str] = "default"
|
||||
|
||||
|
||||
class PopCfg(ServerCfg):
|
||||
server_type = "pop"
|
||||
port = 995
|
||||
timeout_seconds = 60
|
||||
|
||||
|
||||
class SmtpStartTLSCfg(ServerCfg):
|
||||
smtputf8 = True
|
||||
server_type = "smtp_starttls"
|
||||
smtputf8 = True # Not used yet
|
||||
port = 25
|
||||
|
||||
|
||||
class SmtpCfg(ServerCfg):
|
||||
smtputf8 = True
|
||||
server_type = "smtp_starttls"
|
||||
smtputf8 = True # Not used yet
|
||||
port = 465
|
||||
|
||||
|
||||
class LogCfg(Jata):
|
||||
logfile = "STDOUT"
|
||||
logfile = "CONSOLE"
|
||||
level = "INFO"
|
||||
|
||||
|
||||
class Config(Jata):
|
||||
default_tls: TLSCfg | None
|
||||
default_tls: Optional[TLSCfg] = None
|
||||
default_host: str = "0.0.0.0"
|
||||
logging: LogCfg | None = None
|
||||
logging: Optional[LogCfg] = None
|
||||
|
||||
mails_path: str
|
||||
matches: list[Match]
|
||||
boxes: list[Mbox]
|
||||
users: list[User]
|
||||
|
||||
pop: PopCfg | None = None
|
||||
smtp_starttls: SmtpStartTLSCfg | None = None
|
||||
smtp: SmtpCfg | None = None
|
||||
# smtp_port_submission = 587
|
||||
servers: list[ServerCfg]
|
||||
|
||||
|
||||
CheckerFn = Callable[[str], bool]
|
||||
@ -88,7 +89,6 @@ Checker = tuple[str, CheckerFn, bool]
|
||||
|
||||
|
||||
def parse_checkers(cfg: Config) -> list[Checker]:
|
||||
|
||||
def make_match_fn(m: Match):
|
||||
if m.addrs and m.addr_rexs:
|
||||
raise Exception("Both addrs and addr_rexs is set")
|
||||
@ -96,8 +96,7 @@ def parse_checkers(cfg: Config) -> list[Checker]:
|
||||
return lambda malias: malias in m.addrs
|
||||
elif m.addr_rexs:
|
||||
compiled_res = [re.compile(reg) for reg in m.addr_rexs]
|
||||
return lambda malias: any(
|
||||
reg.match(malias) for reg in compiled_res)
|
||||
return lambda malias: any(reg.match(malias) for reg in compiled_res)
|
||||
else:
|
||||
raise Exception("Neither addrs nor addr_rexs is set")
|
||||
|
||||
@ -113,13 +112,13 @@ def parse_checkers(cfg: Config) -> list[Checker]:
|
||||
return mbox_name, match_fn, rule.stop_check
|
||||
|
||||
return [
|
||||
make_checker(mbox.name, Rule(rule)) for mbox in cfg.boxes or []
|
||||
make_checker(mbox.name, Rule(rule))
|
||||
for mbox in cfg.boxes or []
|
||||
for rule in mbox.rules
|
||||
]
|
||||
|
||||
|
||||
def get_mboxes(addr: str, checks: list[Checker]) -> list[str]:
|
||||
|
||||
def inner():
|
||||
for mbox, match_fn, stop_check in checks:
|
||||
if match_fn(addr):
|
||||
|
@ -13,6 +13,8 @@ from .pwhash import parse_hash, check_pass, PWInfo
|
||||
from asyncio import StreamReader, StreamWriter
|
||||
import random
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from .poputils import (
|
||||
InvalidCommand,
|
||||
parse_command,
|
||||
@ -44,7 +46,6 @@ class State:
|
||||
|
||||
|
||||
class SharedState:
|
||||
|
||||
def __init__(self, mails_path: Path, users: dict[str, tuple[PWInfo, str]]):
|
||||
self.mails_path = mails_path
|
||||
self.users = users
|
||||
@ -56,8 +57,7 @@ class SharedState:
|
||||
return self.counter
|
||||
|
||||
|
||||
c_shared_state: contextvars.ContextVar = contextvars.ContextVar(
|
||||
"pop_shared_state")
|
||||
c_shared_state: contextvars.ContextVar = contextvars.ContextVar("pop_shared_state")
|
||||
|
||||
|
||||
def scfg() -> SharedState:
|
||||
@ -72,7 +72,6 @@ def state() -> State:
|
||||
|
||||
|
||||
class PopLogger(logging.LoggerAdapter):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(logging.getLogger("pop3"), None)
|
||||
|
||||
@ -117,8 +116,8 @@ async def expect_cmd(*commands: Command) -> Request:
|
||||
return req
|
||||
|
||||
|
||||
def write(data) -> None:
|
||||
logger.debug(f"Server: {data}")
|
||||
def write(data: bytes) -> None:
|
||||
logger.debug(f"Server: {data!r}")
|
||||
state().writer.write(data)
|
||||
|
||||
|
||||
@ -280,8 +279,7 @@ def get_deleted_items(deleted_items_path: Path) -> set[str]:
|
||||
return set()
|
||||
|
||||
|
||||
def save_deleted_items(deleted_items_path: Path,
|
||||
deleted_items: set[str]) -> None:
|
||||
def save_deleted_items(deleted_items_path: Path, deleted_items: set[str]) -> None:
|
||||
with deleted_items_path.open(mode="w") as f:
|
||||
f.writelines(f"{did}\n" for did in deleted_items)
|
||||
|
||||
@ -298,8 +296,9 @@ async def transaction_stage() -> None:
|
||||
new_deleted_items: set[str] = await process_transactions(mails_list)
|
||||
logger.info(f"completed transactions. Deleted:{len(new_deleted_items)}")
|
||||
if new_deleted_items:
|
||||
save_deleted_items(deleted_items_path,
|
||||
existing_deleted_items.union(new_deleted_items))
|
||||
save_deleted_items(
|
||||
deleted_items_path, existing_deleted_items.union(new_deleted_items)
|
||||
)
|
||||
|
||||
logger.info(f"Saved deleted items")
|
||||
|
||||
@ -330,7 +329,6 @@ async def start_session() -> None:
|
||||
|
||||
|
||||
def parse_users(users: list[User]) -> dict[str, tuple[PWInfo, str]]:
|
||||
|
||||
def inner():
|
||||
for user in users:
|
||||
user = User(user)
|
||||
@ -340,15 +338,13 @@ def parse_users(users: list[User]) -> dict[str, tuple[PWInfo, str]]:
|
||||
return dict(inner())
|
||||
|
||||
|
||||
def make_pop_server_callback(mails_path: Path, users: list[User],
|
||||
timeout_seconds: int):
|
||||
def make_pop_server_callback(mails_path: Path, users: list[User], timeout_seconds: int):
|
||||
scfg = SharedState(mails_path=mails_path, users=parse_users(users))
|
||||
|
||||
async def session_cb(reader: StreamReader, writer: StreamWriter):
|
||||
c_shared_state.set(scfg)
|
||||
ip, _ = writer.get_extra_info("peername")
|
||||
c_state.set(
|
||||
State(reader=reader, writer=writer, ip=ip, req_id=scfg.next_id()))
|
||||
c_state.set(State(reader=reader, writer=writer, ip=ip, req_id=scfg.next_id()))
|
||||
logger.info(f"Got pop server callback")
|
||||
try:
|
||||
try:
|
||||
@ -367,7 +363,7 @@ async def create_pop_server(
|
||||
port: int,
|
||||
mails_path: Path,
|
||||
users: list[User],
|
||||
ssl_context: ssl.SSLContext | None = None,
|
||||
ssl_context: Optional[ssl.SSLContext] = None,
|
||||
timeout_seconds: int = 60,
|
||||
) -> asyncio.Server:
|
||||
logging.info(
|
||||
|
@ -86,7 +86,7 @@ def parse_command(bline: bytes) -> Request:
|
||||
if parts:
|
||||
request.arg2, *parts = parts
|
||||
if parts:
|
||||
(request.rest, ) = parts
|
||||
(request.rest,) = parts
|
||||
return request
|
||||
|
||||
|
||||
@ -130,7 +130,6 @@ def get_mail(entry: MailEntry) -> bytes:
|
||||
|
||||
|
||||
class MailList:
|
||||
|
||||
def __init__(self, entries: list[MailEntry]):
|
||||
self.entries = entries
|
||||
set_nid(self.entries)
|
||||
|
@ -16,20 +16,18 @@ VERSION = b"\x01"
|
||||
SALT_LEN = 30
|
||||
KEY_LEN = 64 # This is python default
|
||||
|
||||
# len(VERSION) + SALT_LEN + KEY_LEN should be multiple of 5 to avoid base32 padding
|
||||
|
||||
|
||||
def gen_pwhash(password: str) -> str:
|
||||
salt = os.urandom(SALT_LEN)
|
||||
sh = scrypt(password.encode(),
|
||||
salt=salt,
|
||||
n=SCRYPT_N,
|
||||
r=SCRYPT_R,
|
||||
p=SCRYPT_P,
|
||||
dklen=KEY_LEN)
|
||||
sh = scrypt(
|
||||
password.encode(), salt=salt, n=SCRYPT_N, r=SCRYPT_R, p=SCRYPT_P, dklen=KEY_LEN
|
||||
)
|
||||
return b32encode(VERSION + salt + sh).decode()
|
||||
|
||||
|
||||
class PWInfo:
|
||||
|
||||
def __init__(self, salt: bytes, sh: bytes):
|
||||
self.salt = salt
|
||||
self.scrypt_hash = sh
|
||||
@ -40,12 +38,13 @@ def parse_hash(pwhash_str: str) -> PWInfo:
|
||||
|
||||
if not len(pwhash) == 1 + SALT_LEN + KEY_LEN:
|
||||
raise Exception(
|
||||
f"Invalid hash size, {len(pwhash)} != {1 + SALT_LEN + KEY_LEN}")
|
||||
f"Invalid hash size, {len(pwhash)} != {1 + SALT_LEN + KEY_LEN}"
|
||||
)
|
||||
|
||||
if (ver := pwhash[0:1]) != VERSION:
|
||||
raise Exception(f"Invalid hash version, {ver!r} != {VERSION!r}")
|
||||
|
||||
salt, sh = pwhash[1:SALT_LEN + 1], pwhash[-KEY_LEN:]
|
||||
salt, sh = pwhash[1 : SALT_LEN + 1], pwhash[-KEY_LEN:]
|
||||
return PWInfo(salt, sh)
|
||||
|
||||
|
||||
|
@ -10,9 +10,11 @@ from getpass import getpass
|
||||
from .smtp import create_smtp_server_starttls, create_smtp_server
|
||||
from .pop3 import create_pop_server
|
||||
from .version import VERSION
|
||||
from .web_config import create_web_config_server
|
||||
|
||||
from . import config
|
||||
from . import pwhash
|
||||
from typing import Optional, Union
|
||||
|
||||
|
||||
def create_tls_context(certfile, keyfile) -> ssl.SSLContext:
|
||||
@ -22,30 +24,31 @@ def create_tls_context(certfile, keyfile) -> ssl.SSLContext:
|
||||
|
||||
|
||||
def setup_logging(cfg: config.LogCfg):
|
||||
logging_format = "%(asctime)s %(name)s %(levelname)s %(message)s @ %(filename)s:%(lineno)d"
|
||||
if cfg.logfile == "STDOUT":
|
||||
logging_format = (
|
||||
"%(asctime)s %(name)s %(levelname)s %(message)s @ %(filename)s:%(lineno)d"
|
||||
)
|
||||
if cfg.logfile == "CONSOLE":
|
||||
logging.basicConfig(level=cfg.level, format=logging_format)
|
||||
else:
|
||||
logging.basicConfig(filename=cfg.logfile,
|
||||
level=cfg.level,
|
||||
format=logging_format)
|
||||
logging.basicConfig(
|
||||
filename=cfg.logfile, level=cfg.level, format=logging_format
|
||||
)
|
||||
|
||||
|
||||
async def a_main(cfg: config.Config) -> None:
|
||||
default_tls_context: ssl.SSLContext | None = None
|
||||
default_tls_context: Optional[ssl.SSLContext] = None
|
||||
|
||||
if tls := cfg.default_tls:
|
||||
logging.info(
|
||||
f"Initializing default tls {tls.certfile=}, {tls.keyfile=}")
|
||||
logging.info(f"Initializing default tls {tls.certfile=}, {tls.keyfile=}")
|
||||
default_tls_context = create_tls_context(tls.certfile, tls.keyfile)
|
||||
|
||||
def get_tls_context(tls: config.TLSCfg | str):
|
||||
def get_tls_context(tls: Union[config.TLSCfg, str]):
|
||||
if tls == "default":
|
||||
return default_tls_context
|
||||
elif tls == "disable":
|
||||
return None
|
||||
else:
|
||||
tls_cfg = config.TLSCfg(pop.tls)
|
||||
tls_cfg = config.TLSCfg(tls)
|
||||
return create_tls_context(tls_cfg.certfile, tls_cfg.keyfile)
|
||||
|
||||
def get_host(host):
|
||||
@ -57,8 +60,13 @@ async def a_main(cfg: config.Config) -> None:
|
||||
mbox_finder = config.gen_addr_to_mboxes(cfg)
|
||||
servers: list[asyncio.Server] = []
|
||||
|
||||
if cfg.pop:
|
||||
pop = config.PopCfg(cfg.pop)
|
||||
if not cfg.servers:
|
||||
logging.warning("Nothing to do!")
|
||||
return
|
||||
|
||||
for scfg in cfg.servers:
|
||||
if scfg.server_type == "pop":
|
||||
pop = config.PopCfg(scfg)
|
||||
pop_server = await create_pop_server(
|
||||
host=get_host(pop.host),
|
||||
port=pop.port,
|
||||
@ -68,9 +76,8 @@ async def a_main(cfg: config.Config) -> None:
|
||||
timeout_seconds=pop.timeout_seconds,
|
||||
)
|
||||
servers.append(pop_server)
|
||||
|
||||
if cfg.smtp_starttls:
|
||||
stls = config.SmtpStartTLSCfg(cfg.smtp_starttls)
|
||||
elif scfg.server_type == "smtp_starttls":
|
||||
stls = config.SmtpStartTLSCfg(scfg)
|
||||
stls_context = get_tls_context(stls.tls)
|
||||
if not stls_context:
|
||||
raise Exception("starttls requires ssl_context")
|
||||
@ -82,9 +89,8 @@ async def a_main(cfg: config.Config) -> None:
|
||||
ssl_context=stls_context,
|
||||
)
|
||||
servers.append(smtp_server_starttls)
|
||||
|
||||
if cfg.smtp:
|
||||
smtp = config.SmtpCfg(cfg.smtp)
|
||||
elif scfg.server_type == "smtp":
|
||||
smtp = config.SmtpCfg(scfg)
|
||||
smtp_server = await create_smtp_server(
|
||||
host=get_host(smtp.host),
|
||||
port=smtp.port,
|
||||
@ -93,11 +99,21 @@ async def a_main(cfg: config.Config) -> None:
|
||||
ssl_context=get_tls_context(smtp.tls),
|
||||
)
|
||||
servers.append(smtp_server)
|
||||
elif scfg.server_type == "web_config":
|
||||
web = config.ServerCfg(scfg)
|
||||
web_server = await create_web_config_server(
|
||||
host=get_host(web.host),
|
||||
port=web.port,
|
||||
ssl_context=get_tls_context(web.tls),
|
||||
)
|
||||
servers.append(web_server)
|
||||
else:
|
||||
logging.error(f"Unknown server {scfg.server_type=}")
|
||||
|
||||
if servers:
|
||||
await asyncio.gather(*[server.serve_forever() for server in servers])
|
||||
else:
|
||||
logging.warn("Nothing to do!")
|
||||
logging.warning("Nothing to do!")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -7,7 +7,7 @@ import uuid
|
||||
import shutil
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
from typing import Callable, Optional
|
||||
from . import config
|
||||
from email.message import Message
|
||||
import email.policy
|
||||
@ -25,15 +25,14 @@ logger = logging.getLogger("smtp")
|
||||
|
||||
|
||||
class MyHandler(AsyncMessage):
|
||||
|
||||
def __init__(self, mails_path: Path, mbox_finder: Callable[[str],
|
||||
list[str]]):
|
||||
def __init__(self, mails_path: Path, mbox_finder: Callable[[str], list[str]]):
|
||||
super().__init__()
|
||||
self.mails_path = mails_path
|
||||
self.mbox_finder = mbox_finder
|
||||
|
||||
async def handle_DATA(self, server: SMTPServer, session: SMTPSession,
|
||||
envelope: SMTPEnvelope) -> str:
|
||||
async def handle_DATA(
|
||||
self, server: SMTPServer, session: SMTPSession, envelope: SMTPEnvelope
|
||||
) -> str:
|
||||
self.rcpt_tos = envelope.rcpt_tos
|
||||
self.peer = session.peer
|
||||
return await super().handle_DATA(server, session, envelope)
|
||||
@ -63,9 +62,9 @@ class MyHandler(AsyncMessage):
|
||||
)
|
||||
|
||||
|
||||
def protocol_factory_starttls(mails_path: Path,
|
||||
mbox_finder: Callable[[str], list[str]],
|
||||
context: ssl.SSLContext):
|
||||
def protocol_factory_starttls(
|
||||
mails_path: Path, mbox_finder: Callable[[str], list[str]], context: ssl.SSLContext
|
||||
):
|
||||
logger.info("Got smtp client cb starttls")
|
||||
try:
|
||||
handler = MyHandler(mails_path, mbox_finder)
|
||||
@ -81,8 +80,7 @@ def protocol_factory_starttls(mails_path: Path,
|
||||
return smtp
|
||||
|
||||
|
||||
def protocol_factory(mails_path: Path, mbox_finder: Callable[[str],
|
||||
list[str]]):
|
||||
def protocol_factory(mails_path: Path, mbox_finder: Callable[[str], list[str]]):
|
||||
logger.info("Got smtp client cb")
|
||||
try:
|
||||
handler = MyHandler(mails_path, mbox_finder)
|
||||
@ -105,8 +103,7 @@ async def create_smtp_server_starttls(
|
||||
)
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.create_server(
|
||||
partial(protocol_factory_starttls, mails_path, mbox_finder,
|
||||
ssl_context),
|
||||
partial(protocol_factory_starttls, mails_path, mbox_finder, ssl_context),
|
||||
host=host,
|
||||
port=port,
|
||||
start_serving=False,
|
||||
@ -118,7 +115,7 @@ async def create_smtp_server(
|
||||
port: int,
|
||||
mails_path: Path,
|
||||
mbox_finder: Callable[[str], list[str]],
|
||||
ssl_context: ssl.SSLContext | None = None,
|
||||
ssl_context: Optional[ssl.SSLContext] = None,
|
||||
) -> asyncio.Server:
|
||||
logging.info(
|
||||
f"Starting SMTP server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}"
|
||||
|
88
mail4one/template_web_config.html
Normal file
88
mail4one/template_web_config.html
Normal file
@ -0,0 +1,88 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Mail4one Web config</title>
|
||||
<script type="application/json" id="m41config">
|
||||
{
|
||||
"matches": [
|
||||
{
|
||||
"name": "mydomain",
|
||||
"addr_rexs": [
|
||||
".*@mydomain.com",
|
||||
".*@m.mydomain.com"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "personal",
|
||||
"addrs": [
|
||||
"first.last@mydomain.com",
|
||||
"secret.name@mydomain.com"
|
||||
]
|
||||
}
|
||||
],
|
||||
"boxes": [
|
||||
{
|
||||
"name": "spam",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "mydomain",
|
||||
"negate": true,
|
||||
"stop_check": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "important",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "personal"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "all",
|
||||
"rules": [
|
||||
{
|
||||
"match_name": "default_match_all"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"users": [
|
||||
{
|
||||
"username": "mymobile",
|
||||
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
|
||||
"mbox": "important"
|
||||
},
|
||||
{
|
||||
"username": "mydesk",
|
||||
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
|
||||
"mbox": "all"
|
||||
}
|
||||
]
|
||||
}
|
||||
</script>
|
||||
<script type="application/javascript">
|
||||
function main() {
|
||||
document.write("hello world")
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="main()">
|
||||
<template id="web-cfg-matches-row">
|
||||
<tr>
|
||||
<td>Name</td>
|
||||
<td>Type</td>
|
||||
<td>Values</td>
|
||||
</tr>
|
||||
</template>
|
||||
<h1>Mail4one Web config</h1>
|
||||
<table id="web-cfg-matches">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Type</th>
|
||||
<th>Values</th>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
@ -1,2 +1 @@
|
||||
|
||||
VERSION = "DEVELOMENT"
|
||||
|
80
mail4one/web_config.py
Normal file
80
mail4one/web_config.py
Normal file
@ -0,0 +1,80 @@
|
||||
import asyncio
|
||||
from asyncbasehttp import request_handler, Request, Response, RequestHandler
|
||||
from typing import Optional, Tuple
|
||||
import ssl
|
||||
import logging
|
||||
from pprint import pprint
|
||||
import http
|
||||
from base64 import b64decode
|
||||
from .pwhash import gen_pwhash, parse_hash, PWInfo, check_pass
|
||||
import pkgutil
|
||||
|
||||
def get_template() -> bytes:
|
||||
if data:= pkgutil.get_data('mail4one', 'template_web_config.html'):
|
||||
return data
|
||||
raise Exception("Failed to get template data from 'template_web_config.html'")
|
||||
|
||||
|
||||
def get_dummy_pwinfo() -> PWInfo:
|
||||
pwhash = gen_pwhash("world")
|
||||
return parse_hash(pwhash)
|
||||
|
||||
|
||||
class WebonfigHandler(RequestHandler):
|
||||
def __init__(self, username: str, pwinfo: PWInfo):
|
||||
self.username = username.encode()
|
||||
self.pwinfo = pwinfo
|
||||
self.auth_required = True
|
||||
|
||||
def do_auth(self, req: Request) -> Tuple[bool, Optional[Response]]:
|
||||
|
||||
def resp_unauthorized():
|
||||
resp = Response.no_body_response(http.HTTPStatus.UNAUTHORIZED)
|
||||
resp.add_header("WWW-Authenticate", 'Basic realm="Mail4one"')
|
||||
return resp
|
||||
|
||||
auth_header = req.headers["Authorization"]
|
||||
|
||||
if not auth_header:
|
||||
return False, resp_unauthorized()
|
||||
|
||||
if not auth_header.startswith("Basic "):
|
||||
logging.error("Authorization header malformed")
|
||||
return False, Response.no_body_response(http.HTTPStatus.BAD_REQUEST)
|
||||
|
||||
userpassb64 = auth_header[len("Basic ") :]
|
||||
try:
|
||||
userpass = b64decode(userpassb64)
|
||||
username, password = userpass.split(b":")
|
||||
except:
|
||||
logging.exception("bad request")
|
||||
return False, Response.no_body_response(http.HTTPStatus.BAD_REQUEST)
|
||||
|
||||
if username == self.username and check_pass(password.decode(), self.pwinfo):
|
||||
return True, None
|
||||
|
||||
return False, resp_unauthorized()
|
||||
|
||||
|
||||
async def process_request(self, req: Request) -> Response:
|
||||
if self.auth_required:
|
||||
ok, resp = self.do_auth(req)
|
||||
if not ok:
|
||||
if resp:
|
||||
return resp
|
||||
else: # To silence mypy
|
||||
raise Exception("Something went wrong!")
|
||||
return Response.create_ok_response(get_template())
|
||||
|
||||
|
||||
async def create_web_config_server(
|
||||
host: str, port: int, ssl_context: Optional[ssl.SSLContext]
|
||||
) -> asyncio.Server:
|
||||
logging.info(f"template: {get_template().decode()}")
|
||||
logging.info(f"Starting Webconfig server {host=}, {port=}, {ssl_context != None=}")
|
||||
return await asyncio.start_server(
|
||||
WebonfigHandler("hello", get_dummy_pwinfo()),
|
||||
host=host,
|
||||
port=port,
|
||||
ssl=ssl_context,
|
||||
)
|
@ -9,7 +9,6 @@ then
|
||||
case "$tag_val" in
|
||||
*DIRTY)
|
||||
echo "git=$commit-changes"
|
||||
exit
|
||||
;;
|
||||
v*) # Only consider tags starting with v
|
||||
echo "$tag_val"
|
||||
|
Reference in New Issue
Block a user