Skip to main content

Local SMTP helper

Project description

Laim (pronounced like lime, anagram of mail)

Build Status

Flexible library to handle local SMTP on servers.

Modern *nix servers use mail for a lot of reporting. Cron uses mail to notify about failing jobs, unattended-upgrades sends reports by mail, and sudo can send mail when invoked. Having a standardized way to report somewhere is great, but having to run a full-blown email infrastructure to receive these messages is hard.

Laim makes it easy to run a secure, local-only SMTP agent, where you define how messages are handled in python. Laim takes care of the hard parts of SMTP, dropping privileges and reading configuration, you just have to write the code to handle the message however you like. Post to slack? Write to log? Send text message? All three? Up to you!

Example handler that posts to slack:

import socket

import requests
from laim import Laim

class SlackHandler(Laim):

    def __init__(self):
        super().__init__()
        self.session = requests.Session()
        self.session.headers.update({
            'Authorization': 'Bearer %s' % self.config['SLACK_TOKEN']
        })
        self.channel_id = self.config['SLACK_CHANNEL_ID']
        self.hostname = socket.gethostname()

    def handle_message(self, sender, recipients, message):
        self.session.post('https://slack.com/api/chat.postMessage', json={
            'channel': self.channel_id,
            'text': '%s received mail for %s:\n%s' % (
                self.hostname, ', '.join(recipients), message.get_payload()),
        })

if __name__ == '__main__':
    handler = SlackHandler()
    handler.run()

A couple of things are worth noting here that laim helped out with.

Configuration

Laim reads the root-only readable yaml file /etc/laim/config.yml on startup, and saves the contents to self.config. This makes it easy to add arbitrary configuration you need in your handler, that will be read in a secure way.

Note that until the line calling super().__init__(), the script was running as root. After that it dropped privileges to the user 'laim'.

Async

Laim is not written for high throughput, but does do some basic queuing to make sure you can handle a message synchronously without blocking the reception of other messages. This is done by running the SMTP listener on one thread, and the handler on another. Messages to be delivered are passed to the handler on a bounded in-memory queue, which prevents arbitrarily high memory usage if the handler fails to process messages fast enough by dropping new messages (this event is logged by laim). You can configure the max size of the queue by passing max_queue_size to the Laim constructor (default is 50).

Security considerations

Laim will bind to localhost port 25 to handle SMTP, and will by itself not do any filtering of messages. Since it only binds to localhost there's no extra attack surface for an external attacker, but if an attacker has gotten non-root access to the server they can craft arbitrary messages that will be forwarded to your handler. This could be exploited to send trojans that might get executed by developers on their own machines or similar, thus remain skeptical to any suspicious messages that gets delivered by laim.

The service will start as root, but drops privileges once it has bound to the port and opened a handle to the config file.

Local testing

Apart from the test suite you can use the included ./devhandler.py to run laim on port 2525 for testing. To send mail you can modify the file in smtp-session.txt, which can be piped to netcat to send a sample mail:

$ nc localhost 2525 < smtp-session.txt

To send lots of mail to test the queuing system or simple load testing:

$ count=0; while :; do echo "Sending $count"; sed s/COUNT/$count/ smtp-session.txt | nc localhost 2525 || break; count=$((count+1)); done; echo "Sent $count mails"

Laim stops gracefully on SIGINT and SIGTERM, so you can stop the handler from a third shell and observe that it shuts down cleanly after having processed all queued messages:

$ pkill -f devhandler.py

There are some more example handlers in the examples/ directory.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

laim-0.4.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

laim-0.4.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file laim-0.4.0.tar.gz.

File metadata

  • Download URL: laim-0.4.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.1

File hashes

Hashes for laim-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7b6644d1119621e78fd3b09f854a4217bb0d17cb83b9a6743a8e70d479ca82b9
MD5 af16a9b306ac401560f1e5c685e0c029
BLAKE2b-256 bda1a9e67403811e8837040fc57e87da114bab935071c7ca952ee7324d44e0f9

See more details on using hashes here.

File details

Details for the file laim-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: laim-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.1

File hashes

Hashes for laim-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b46e3796791edeaba6b8b89f647d695b4f0e9443ade36d86c9db41564cbf3306
MD5 a03fef324580cd304aaa9dfde909e5af
BLAKE2b-256 f83d4677aadb81df3d8e4d706f8c4c0e25a70ecb0f897e62796b21b965bf4887

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page