wildduck/README.md

252 lines
17 KiB
Markdown
Raw Normal View History

2017-07-22 15:39:35 +08:00
# Wild Duck Mail Server
2017-03-06 05:45:50 +08:00
2017-07-22 15:39:35 +08:00
Wild Duck is a distributed IMAP/POP3 mail server. _Distributed_ means that Wild Duck uses a distributed database (sharded + replicated MongoDB) as a backend for storing all data, including emails. Wild Duck instances are stateless, any user can connect to any Wild Duck instance. Wild Duck uses a write ahead log to keep IMAP sessions between different instances in sync.
2017-03-06 22:29:25 +08:00
2017-07-22 15:39:35 +08:00
Wild Duck tries to follow Gmail in architectural design. If there's a decision to be made then usually the answer is to do whatever Gmail has done.
2017-03-11 22:02:41 +08:00
2017-06-04 16:22:46 +08:00
> **NB!** Wild Duck is currently in **beta**. Use it on your own responsibility.
2017-03-06 05:45:50 +08:00
2017-08-02 04:26:58 +08:00
![](https://cldup.com/TZoTfxPugm.png)
- Web interface at https://wildduck.email that uses Wild Duck API
2017-07-22 15:47:27 +08:00
## Requirements
* *MongoDB* to store all data
* *Redis* for pubsub and counters
2017-07-25 03:11:01 +08:00
* *Node.js* at least version 6.0.0
2017-07-22 15:47:27 +08:00
**Optional requirements**
* Build tools to install optional dependencies that need compiling
2017-07-22 15:50:49 +08:00
Wild Duck can be installed on any Node.js compatible platform.
2017-06-04 04:38:18 +08:00
## Usage
Assuming you have MongoDB and Redis running somewhere.
### Step 1\. Get the code from github
```
2017-07-22 15:47:27 +08:00
$ git clone git://github.com/nodemailer/wildduck.git
2017-06-04 04:38:18 +08:00
$ cd wildduck
```
### Step 2\. Install dependencies
Install dependencies from npm
```
$ npm install --production
```
2017-07-16 19:38:38 +08:00
### Step 3\. Run the server
2017-06-04 04:38:18 +08:00
2017-07-16 19:37:33 +08:00
To use the [default config](./config/default.toml) file, run the following:
2017-06-04 04:38:18 +08:00
```
2017-07-16 19:37:33 +08:00
node server.js
2017-06-04 04:38:18 +08:00
```
2017-07-16 19:37:33 +08:00
Or if you want to override default configuration options with your own, run the following (custom config file is merged with the default, so specify only these values that you want to change):
2017-06-04 04:38:18 +08:00
```
2017-07-16 19:37:33 +08:00
node server.js --config=/etc/wildduck.toml
2017-06-04 04:38:18 +08:00
```
2017-07-22 15:47:27 +08:00
> For additional config options, see the _wild-config_ [documentation](https://github.com/nodemailer/wild-config).
2017-07-16 19:37:33 +08:00
2017-07-16 19:38:38 +08:00
### Step 4\. Create an user account
2017-06-04 04:38:18 +08:00
2017-07-25 03:11:01 +08:00
See [API Docs](https://github.com/nodemailer/wildduck/wiki/API-Docs#add-a-new-user) for details about creating new user accounts
2017-06-04 04:38:18 +08:00
2017-07-16 19:38:38 +08:00
### Step 5\. Use an IMAP/POP3 client to log in
2017-06-04 04:40:39 +08:00
2017-07-16 19:38:38 +08:00
Any IMAP or POP3 client will do. Use the credentials from step 4\. to log in.
2017-06-04 04:40:39 +08:00
## Goals of the Project
2017-03-06 05:45:50 +08:00
2017-04-09 17:33:10 +08:00
1. Build a scalable and distributed IMAP/POP3 server that uses clustered database instead of single machine file system as mail store
2. Allow using internationalized email addresses
2017-03-08 02:08:59 +08:00
3. Provide Gmail-like features like pushing sent messages automatically to Sent Mail folder or notifying about messages moved to Junk folder so these could be marked as spam
2017-04-03 21:59:04 +08:00
4. Provide parsed mailbox and message data over HTTP. This should make creating webmail interfaces super easy, no need to parse RFC822 messages to get text content or attachments
2017-03-06 05:45:50 +08:00
2017-07-25 03:08:07 +08:00
# HTTP API
Users, mailboxes and messages can be managed with HTTP requests against Wild Duck API
2017-07-25 03:11:01 +08:00
### [API Docs](https://github.com/nodemailer/wildduck/wiki/API-Docs)
2017-07-25 03:08:07 +08:00
# FAQ
2017-07-16 14:02:41 +08:00
### Does it work?
Yes, it does. You can run the server and get working IMAP and POP3 servers for mail store, LMTP server for pushing messages to the mail store and HTTP API server to create new users. All handled by Node.js, MongoDB and Redis, no additional dependencies needed. Provided services can be disabled and enabled one by one so, for example you could process just IMAP in one host and LMTP in another.
### What are the killer features?
1. **Stateless.** Start as many instances as you want. You can start multiple Wild Duck instances in different machines and as long as they share the same MongoDB and Redis settings, users can connect to any instances. This is very different from the traditional IMAP servers where a single user always needs to connect (or be proxied) to the same IMAP server. Wild Duck keeps all required state information in MongoDB, so it does not matter which IMAP instance you use.
2017-07-19 16:22:07 +08:00
2. **Centralized authentication** which allows modern features like 2FA, application specific passwords, authentication scopes, revoking authentication tokens, audit logging and even profile files to auto configure Apple email clients without providing master password
2017-07-16 14:02:41 +08:00
3. **Works on any OS including Windows.** At least if you get MongoDB and Redis running first.
4. Focus on **internationalization**, ie. supporting email addresses with non-ascii characters
5. **De-duplication of attachments.** If the same attachment is referenced by different messages then only a single copy of the attachment is stored. Attachment is stored in the encoded form (eg. encoded in base64) to not break any signatures so the resulting encoding must match as well.
6. Access messages both using **IMAP and HTTP API**. The latter serves parsed data, so no need to fetch RFC822 messages and parse out html, plaintext content or attachments. It is super easy to create a webmail interface on top of this.
2017-07-19 18:38:21 +08:00
7. Built in **address labels**: _username+label@example.com_ is delivered to _username@example.com_
2017-07-31 16:21:05 +08:00
8. Dots in usernames and addresses are informational only. username@example.com is the same as user.name@example.com
9. **HTTP Event Source** to push modifications in user email account to browser for super snappy webmail clients
10. **Super easy to tweak.** The entire codebase is pure JavaScript, so there's nothing to compile or anything platform specific. If you need to tweak something then change the code, restart the app and you're ready to go. If it works on one machine then most probably it works in every other machine as well.
11. **Better disk usage**. Attachment de-duplication and MongoDB compression yield in about 40% smaller disk usage as the sum of all stored email sizes.
2017-07-19 18:38:21 +08:00
2017-07-19 19:51:43 +08:00
**Demo video for HTTP push**
2017-07-19 18:38:21 +08:00
[![Stream Push Demo](https://img.youtube.com/vi/KFoO8x0mEpw/0.jpg)](https://www.youtube.com/watch?v=KFoO8x0mEpw)
2017-07-16 14:02:41 +08:00
### Isn't it bad to use a database as a mail store?
2017-07-22 15:59:42 +08:00
Yes, historically it has [been considered a bad practice](http://www.memoryhole.net/~kyle/databaseemail.html) to store emails in a database. And for a good reason. The data model of relational databases like MySQL does not work well with tree like structures (email mime tree) or large blobs (email source).
2017-07-16 14:02:41 +08:00
Notice the word "relational"? In fact document stores like MongoDB work very well with emails. Document store is great for storing tree-like structures and while GridFS is not as good as "real" object storage, it is good enough for storing the raw parts of the message. Additionally there's nothing too GridFS specific, so (at least in theory) it could be replaced with any object store.
2017-07-22 16:33:57 +08:00
Here's a list of alternative email servers that also use a database for storing email messages:
2017-07-19 16:22:07 +08:00
- [DBMail](http://www.dbmail.org/) (MySQL, IMAP)
- [Archiveopteryx](http://archiveopteryx.org/) (PostgreSQL, IMAP)
- [ElasticInbox](http://www.elasticinbox.com/) (Cassandra, POP3)
2017-07-16 14:02:41 +08:00
### How does it work?
Whenever a message is received Wild Duck parses it into a tree-like structure based on the MIME tree and stores this tree to MongoDB. Attachments are removed from the tree and stored separately in GridStore. If a message needs to be loaded then Wild Duck fetches the tree structure first and, if needed, loads attachments from GridStore and then compiles it back into the original RFC822 message. The result should be identical to the original messages unless the original message used unix newlines, these might be partially replaced with windows newlines.
2017-07-19 16:22:07 +08:00
Wild Duck tries to keep minimal state for sessions (basically just a list of currently known UIDs and latest MODSEQ value) to be able to distribute sessions between different hosts. Whenever a mailbox is opened the entire message list is loaded as an array of UID values. The first UID in the array element points to the message nr. 1 in IMAP, second one points to message nr. 2 etc.
2017-07-16 14:02:41 +08:00
Actual update data (information about new and deleted messages, flag updates and such) is stored to a journal log and an update beacon is propagated through Redis pub/sub whenever something happens. If a session detects that there have been some changes in the current mailbox and it is possible to notify the user about it (eg. a NOOP call was made), journaled log is loaded from the database and applied to the UID array one action at a time. Once all journaled updates have applied then the result should match the latest state. If it is not possible to notify the user (eg a FETCH call was made), then journal log is not loaded and the user continues to see the old state.
## E-Mail Protocol support
2017-03-06 22:24:57 +08:00
2017-03-08 02:08:59 +08:00
Wild Duck IMAP server supports the following IMAP standards:
2017-03-06 22:31:49 +08:00
2017-03-30 23:25:59 +08:00
- The entire **IMAP4rev1** suite with some minor differences from the spec. See below for [IMAP Protocol Differences](#imap-protocol-differences) for a complete list
2017-04-05 01:39:32 +08:00
- **IDLE** ([RFC2177](https://tools.ietf.org/html/rfc2177)) notfies about new and deleted messages and also about flag updates
- **CONDSTORE** ([RFC4551](https://tools.ietf.org/html/rfc4551)) and **ENABLE** ([RFC5161](https://tools.ietf.org/html/rfc5161)) supports most of the spec, except metadata stuff which is ignored
- **STARTTLS** ([RFC2595](https://tools.ietf.org/html/rfc2595))
- **NAMESPACE** ([RFC2342](https://tools.ietf.org/html/rfc2342)) minimal support, just lists the single user namespace with hierarchy separator
- **UNSELECT** ([RFC3691](https://tools.ietf.org/html/rfc3691))
- **UIDPLUS** ([RFC4315](https://tools.ietf.org/html/rfc4315))
- **SPECIAL-USE** ([RFC6154](https://tools.ietf.org/html/rfc6154))
- **ID** ([RFC2971](https://tools.ietf.org/html/rfc2971))
- **MOVE** ([RFC6851](https://tools.ietf.org/html/rfc6851))
- **AUTHENTICATE PLAIN** ([RFC4959](https://tools.ietf.org/html/rfc4959)) and **SASL-IR**
- **APPENDLIMIT** ([RFC7889](https://tools.ietf.org/html/rfc7889)) maximum global allowed message size is advertised in CAPABILITY listing
2017-07-16 14:02:41 +08:00
- **UTF8=ACCEPT** ([RFC6855](https://tools.ietf.org/html/rfc6855)) this also means that Wild Duck natively supports unicode email usernames. For example [андрис@уайлддак.орг](mailto:андрис@уайлддак.орг) is a valid email address that is hosted by a test instance of Wild Duck
2017-07-22 16:33:57 +08:00
- **QUOTA** ([RFC2087](https://tools.ietf.org/html/rfc2087)) Quota size is global for an account, using a single quota root. Be aware that quota size does not mean actual byte storage in disk, it is calculated as the sum of the [RFC822](https://tools.ietf.org/html/rfc822) sources of stored messages.
2017-04-05 01:39:32 +08:00
- **COMPRESS=DEFLATE** ([RFC4978](https://tools.ietf.org/html/rfc4978)) Compress traffic between the client and the server
2017-03-08 02:08:59 +08:00
2017-07-19 16:22:07 +08:00
Wild Duck more or less passes the [ImapTest](https://www.imapwiki.org/ImapTest/TestFeatures) Stress Testing run. Common errors that arise in the test are unknown labels (Wild Duck doesn't send unsolicited `FLAGS` updates even though it does send unsolicited `FETCH FLAGS` updates) and sometimes NO for `STORE` (messages deleted in one session can not be updated in another).
2017-07-22 16:33:57 +08:00
In comparison Wild Duck is slower in processing single user than Dovecot. Especially when fetching messages, which is expected as Dovecot is reading directly from filesystem while Wild Duck is recomposing messages from different parts.
Raw read/write speed for a single user is usually not relevant anyway as fetching entire mailbox content is not something that happens often. Wild Duck offers better parallelization through MongoDB sharding, so more users should not mean slower response times. It is also more important to offer fast synchronization speeds between clients (eg. notifications about new email and such) where Wild Duck excels due to the write ahead log and the ability to push this log to clients.
2017-04-01 02:41:53 +08:00
2017-04-08 03:38:52 +08:00
### POP3 Support
2017-04-09 17:33:10 +08:00
In addition to the required POP3 commands ([RFC1939](https://tools.ietf.org/html/rfc1939)) Wild Duck supports the following extensions:
2017-04-08 03:38:52 +08:00
2017-04-24 19:51:11 +08:00
- **UIDL**
- **USER**
- **PASS**
- **SASL PLAIN**
- **PIPELINING**
- **TOP**
2017-04-09 17:33:10 +08:00
2017-04-09 17:57:10 +08:00
#### POP3 command behaviors
2017-04-24 19:51:11 +08:00
All changes to messages like deleting messages or marking messages as seen are stored in storage only in the UPDATE stage (eg. after calling QUIT). Until then the changes are preserved in memory only. This also means that if a message is downloaded but QUIT is not issued then the message does not get marked as _Seen_.
2017-04-10 02:29:42 +08:00
2017-04-09 17:57:10 +08:00
##### LIST
2017-04-09 17:33:10 +08:00
POP3 listing displays the newest 250 messages in INBOX (configurable)
2017-04-09 18:00:19 +08:00
##### UIDL
2017-04-24 19:51:11 +08:00
Wild Duck uses message `_id` value (24 byte hex) as the unique ID. If a message is moved from one mailbox to another then it might _re-appear_ in the listing.
2017-04-09 18:00:19 +08:00
2017-04-09 17:57:10 +08:00
##### RETR
2017-04-09 17:33:10 +08:00
2017-04-24 19:51:11 +08:00
If a messages is downloaded by a client this message gets marked as _Seen_
2017-04-09 17:33:10 +08:00
2017-04-09 17:57:10 +08:00
##### DELE
2017-04-09 17:33:10 +08:00
If a messages is deleted by a client this message gets marked as Seen and moved to Trash folder
2017-04-08 03:38:52 +08:00
## Message filtering
2017-04-17 20:58:46 +08:00
Wild Duck has built-in message filtering in LMTP server. This is somewhat similar to Sieve even though the filters are not scripts.
2017-07-25 03:11:01 +08:00
Filters can be managed via the [Wild Duck API](https://github.com/nodemailer/wildduck/wiki/API-Docs).
2017-04-17 20:58:46 +08:00
## Sharding
Shard the following collections by these keys:
```javascript
sh.enableSharding('wildduck');
sh.shardCollection('wildduck.messages', { mailbox: 1, uid: 1 });
sh.shardCollection('wildduck.threads', { user: 'hashed' });
sh.shardCollection('wildduck.attachments.files', { 'metadata.h': 'hashed' });
sh.shardCollection('wildduck.attachments.chunks', { files_id: 'hashed' });
```
2017-07-16 14:02:41 +08:00
> Attachments collections might reside in a different database than default. Modify sharding namespaces accordingly (and do not forget to enable sharding for the attachments database)
## IMAP Protocol Differences
This is a list of known differences from the IMAP specification. Listed differences are either intentional or are bugs that became features.
1. `\Recent` flags is not implemented and most probably never will be (RFC3501 2.3.2.)
2. `RENAME` does not touch subfolders which is against the spec (RFC3501 6.3.5\. _If the name has inferior hierarchical names, then the inferior hierarchical names MUST also be renamed._). Wild Duck stores all folders using flat hierarchy, the "/" separator is fake and only used for listing mailboxes
3. Unsolicited `FLAGS` responses (RFC3501 7.2.6.) and `PERMANENTFLAGS` are not sent (except for as part of `SELECT` and `EXAMINE` responses). Wild Duck notifies about flag updates only with unsolicited FETCH updates.
4. Wild Duck responds with `NO` for `STORE` if matching messages were deleted in another session
5. `CHARSET` argument for the `SEARCH` command is ignored (RFC3501 6.4.4.)
6. Metadata arguments for `SEARCH MODSEQ` are ignored (RFC7162 3.1.5.). You can define `<entry-name>` and `<entry-type-req>` values but these are not used for anything
2017-04-04 15:20:25 +08:00
7. `SEARCH TEXT` and `SEARCH BODY` both use MongoDB [$text index](https://docs.mongodb.com/v3.4/reference/operator/query/text/) against decoded plaintext version of the message. RFC3501 assumes that it should be a string match either against full message (`TEXT`) or body section (`BODY`).
2017-04-05 01:39:32 +08:00
8. What happens when FETCH is called for messages that were deleted in another session? _Not sure, need to check_
Any other differences are most probably real bugs and unintentional.
## Testing
Create an email account and use your IMAP client to connect to it. To send mail to this account, run the example script:
```
node examples/push-mail.js username@example.com
```
2017-04-13 16:35:39 +08:00
This should "deliver" a new message to the INBOX of _username@example.com_ by using the built-in LMTP maildrop interface. If your email client is connected then you should promptly see the new message.
2017-04-05 01:39:32 +08:00
## Outbound SMTP
2017-08-02 16:28:51 +08:00
Use [WildDuck MTA](https://github.com/nodemailer/wildduck-mta) (which under the hood is
[ZoneMTA](https://github.com/zone-eu/zone-mta) with the [ZoneMTA-WildDuck](https://github.com/nodemailer/zonemta-wildduck) plugin).
This gives you an outbound SMTP server that uses Wild Duck accounts for authentication. The plugin authenticates user credentials and also rewrites headers if needed (if the header From: address does not match user address or aliases then it is rewritten). Additionally a copy of the sent message is uploaded to the Sent Mail folder. Local delivery is done directly to Wild Duck LMTP.
2017-04-05 01:39:32 +08:00
2017-06-04 04:41:40 +08:00
## Inbound SMTP
2017-04-13 16:35:39 +08:00
2017-08-02 15:46:32 +08:00
Use [Haraka](http://haraka.github.io/) with [queue/lmtp](http://haraka.github.io/manual/plugins/queue/lmtp.html) plugin to deliver messages to Wild Duck and [haraka-plugins-wildduck](https://github.com/nodemailer/haraka-plugin-wildduck) to validate recipient addresses and quota usage against the Wild Duck users database.
2017-04-13 16:35:39 +08:00
2017-07-16 14:02:41 +08:00
## Future considerations
- Optimize FETCH queries to load only partial data for BODY subparts
- Parse incoming message into the mime tree as a stream. Currently the entire message is buffered in memory before being parsed.
2017-07-22 15:41:39 +08:00
- Maybe allow some kind of message manipulation through plugins
2017-07-16 14:02:41 +08:00
- Wild Duck does not plan to be the most feature-rich IMAP client in the world. Most IMAP extensions are useless because there aren't too many clients that are able to benefit from these extensions. There are a few extensions though that would make sense to be added to Wild Duck:
2017-07-17 21:32:31 +08:00
2017-07-16 14:02:41 +08:00
- IMAP4 non-synchronizing literals, LITERAL- ([RFC7888](https://tools.ietf.org/html/rfc7888)). Synchronized literals are needed for APPEND to check mailbox quota, small values could go with the non-synchronizing version.
- LIST-STATUS ([RFC5819](https://tools.ietf.org/html/rfc5819))
- _What else?_ (definitely not NOTIFY nor QRESYNC)
2017-03-06 05:45:50 +08:00
## License
Wild Duck Mail Agent is licensed under the [European Union Public License 1.1](http://ec.europa.eu/idabc/eupl.html).