From 01b516ce7ccabe0ff31333f1faa08c03db600cdb Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Wed, 22 Apr 2020 23:02:05 +0200 Subject: [PATCH] add config option socket, rework example config and release 1.0.0 --- README.md | 11 +++---- docs/pymodmilter.conf.example | 57 ++++++++++++++++++++--------------- misc/openrc/pymodmilter.confd | 9 ------ misc/openrc/pymodmilter.initd | 3 +- pymodmilter/run.py | 13 ++++++-- pymodmilter/version.py | 2 +- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 29dd483..0059bfa 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,21 @@ Pymodmilter is depending on these python packages, but they are installed automa * [netaddr](https://github.com/drkjam/netaddr/) ## Installation -* Install pymodmilter with pip. +* Install pymodmilter with pip and copy the example configuration file. ```sh pip install pymodmilter -``` -* Copy the example configuration file. -```sh cp /usr/share/doc/pymodmilter/pymodmilter.conf.example /etc/pymodmilter.conf ``` -* Modify /etc/pymodmilter.conf according to your needs and you are ready to go. +* Modify /etc/pymodmilter.conf according to your needs. ## Configuration options Pymodmilter uses a configuration file in JSON format. The options are described below. Make a copy of the [example configuration file](https://github.com/spacefreak86/pymodmilter/blob/master/docs/pymodmilter.conf.example) in the [docs](https://github.com/spacefreak86/pymodmilter/tree/master/docs) folder to start with. Rules and modifications are processed in the given order. ### Global -The following optional global configuration options are available: +The following global configuration options are optional: +* **socket** + The socket used to communicate with the MTA. * **local_addrs** A list of hosts and network addresses which are considered local. It is used to for the condition option [local](#Conditions). This option may be overriden within a rule object. * **log** diff --git a/docs/pymodmilter.conf.example b/docs/pymodmilter.conf.example index a5d3035..00cde86 100644 --- a/docs/pymodmilter.conf.example +++ b/docs/pymodmilter.conf.example @@ -7,97 +7,106 @@ # { # Section: global - # Notes: Set default options. + # Notes: Global options. # "global": { + # Option: socket + # Type: String + # Notes: The socket used to communicate with the MTA. + # + # Examples: + # unix:/path/to/socket a named pipe + # inet:8899 listen on ANY interface + # inet:8899@localhost listen on a specific interface + # inet6:8899 listen on ANY interface + # inet6:8899@[2001:db8:1234::1] listen on a specific interface + # Value: [ SOCKET ] + "socket": "inet:8898@127.0.0.1", + # Option: local_addrs # Type: List - # Notes: Set a list of local hosts and networks. + # Notes: A list of local hosts and networks. # Value: [ LIST ] # "local_addrs": ["127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"], # Option: log # Type: Bool - # Notes: Set if processing of rules and modifications is logged. + # Notes: Enable or disable logging of rules and modifications. # Value: [ true | false ] # "log": true }, # Section: rules - # Notes: Set rules and related modifications. + # Notes: Rules and related modifications. # "rules": [ { # Option: name # Type: String - # Notes: Set the name of the rule. + # Notes: Name of the rule. # Value: [ NAME ] # "name": "MyRule", # Section: conditions - # Notes: Optionally set conditions to run the rule. - # If multiple conditions are specified, they all - # have to be true to run the rule. + # Notes: Optional conditions to process the rule. + # If multiple conditions are set, they all + # have to be true to process the rule. # "conditions": { # Option: local # Type: Bool - # Notes: Set a condition on the senders host address. - # Set to true to execute the rule only for emails originating - # from addresses defined in local_addrs and vice versa. + # Notes: Condition wheter the senders host address is listed in local_addrs. # Value: [ true | false ] # "local": false, # Option: hosts # Type: String - # Notes: Set a condition on the senders host address. - # The rule will only be executed if the list contains the - # senders host address. + # Notes: Condition wheter the senders host address is listed in this list. # Value: [ LIST ] # "hosts": [ "127.0.0.1" ], # Option: envfrom # Type: String - # Notes: Set a regular expression to match against the envelope-from address. + # Notes: Condition wheter the envelop-from address matches this regular expression. # Value: [ REGEX ] # "envfrom": "^(?!.+@mycompany\\.com).+$" }, # Section: modifications - # Notes: Set modifications for the rule. + # Notes: Modifications of the rule. # "modifications": [ { # Option: name # Type: String - # Notes: Set the name of the modification. + # Notes: Name of the modification. # Value: [ NAME ] # "name": "AddHeader", # Option: type # Type: String - # Notes: Set the modification type. + # Notes: Type of the modification. # Value: [ add_header | del_header | mod_header ] # "type": "add_header", # Option: header # Type: String - # Notes: Set the name of the new header. + # Notes: Name of the header. # Value: [ NAME ] # "header": "X-Test-Header", # Option: value # Type: String - # Notes: Set the value of the new header. + # Notes: Value of the header. # Value: [ VALUE ] # "value": "true" @@ -108,21 +117,21 @@ # Option: header # Type: String - # Notes: Set a regular expression to match against header lines (e.g. Subject: Test-Subject). + # Notes: Regular expression to match against header lines (e.g. Subject: Test-Subject). # Value: [ REGEX ] # "header": "^Subject:", # Option: search # Type: String - # Notes: Set a regular expression to match against the headers value. + # Notes: Regular expression to match against the headers value. # Values: [ VALUE ] # "search": "(?P.*)", # Option: value # Type: String - # Notes: Set the value of the header. + # Notes: New value of the header. # Values: [ VALUE ] "value": "[EXTERNAL] \\g" }, { @@ -132,7 +141,7 @@ # Option: header # Type: String - # Notes: Set a regular expression to match against header lines (e.g. Subject: Test-Subject). + # Notes: Regular expression to match against header lines (e.g. Subject: Test-Subject). # Value: [ REGEX ] # "header": "^Received:" diff --git a/misc/openrc/pymodmilter.confd b/misc/openrc/pymodmilter.confd index b4e923c..6c5e71d 100644 --- a/misc/openrc/pymodmilter.confd +++ b/misc/openrc/pymodmilter.confd @@ -1,14 +1,5 @@ # /etc/conf.d/pymodmilter: config file for /etc/init.d/pymodmilter -# Set the socket used to communicate with the MTA. -# Examples: -# unix:/path/to/socket a named pipe -# inet:8899 listen on ANY interface -# inet:8899@localhost listen on a specific interface -# inet6:8899 listen on ANY interface -# inet6:8899@[2001:db8:1234::1] listen on a specific interface -SOCKET="inet:8898@127.0.0.1" - # Start the daemon as the user. You can optionally append a group name here also. # USER="daemon" # USER="daemon:nobody" diff --git a/misc/openrc/pymodmilter.initd b/misc/openrc/pymodmilter.initd index a487a22..bb9a646 100755 --- a/misc/openrc/pymodmilter.initd +++ b/misc/openrc/pymodmilter.initd @@ -1,12 +1,11 @@ #!/sbin/openrc-run user=${USER:-daemon} -socket="${SOCKET:-}" milter_opts="${MILTER_OPTS:-}" pidfile="/run/${RC_SVCNAME}.pid" command="/usr/bin/pymodmilter" -command_args="-s ${socket} ${milter_opts}" +command_args="${milter_opts}" command_background=true start_stop_daemon_args="--user ${user}" diff --git a/pymodmilter/run.py b/pymodmilter/run.py index 3afd970..6e3c8e2 100644 --- a/pymodmilter/run.py +++ b/pymodmilter/run.py @@ -40,7 +40,7 @@ def main(): "-s", "--socket", help="Socket used to communicate with the MTA.", - required=True) + default="") parser.add_argument( "-d", "--debug", @@ -95,6 +95,15 @@ def main(): if "global" not in config: config["global"] = {} + if args.socket: + socket = args.socket + elif "socket" in config["global"]: + socket = config["global"]["socket"] + else: + raise RuntimeError( + f"listening socket is neither specified on the command line " + f"nor in the configuration file") + if "local_addrs" not in config["global"]: config["global"]["local_addrs"] = [ "127.0.0.0/8", @@ -177,7 +186,7 @@ def main(): rc = 0 try: - Milter.runmilter("pymodmilter", socketname=args.socket, timeout=30) + Milter.runmilter("pymodmilter", socketname=socket, timeout=30) except Milter.milter.error as e: logger.error(e) rc = 255 diff --git a/pymodmilter/version.py b/pymodmilter/version.py index 00ec2dc..5becc17 100644 --- a/pymodmilter/version.py +++ b/pymodmilter/version.py @@ -1 +1 @@ -__version__ = "0.0.9" +__version__ = "1.0.0"