summaryrefslogtreecommitdiffstats
path: root/wallace/__init__.py
blob: 0230c6472791e6fe60b0776d05e9f181352d766e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import asyncore
import os
from smtpd import SMTPChannel
import sys
import tempfile
import threading
import time
import traceback

import pykolab
from pykolab.translate import _

log = pykolab.getLogger('pykolab.wallace')
conf = pykolab.getConf()

class WallaceDaemon(object):
    def __init__(self):
        daemon_group = conf.add_cli_parser_option_group(_("Daemon Options"))

        daemon_group.add_option(
                "--fork",
                dest    = "fork_mode",
                action  = "store_true",
                default = False,
                help    = _("Fork to the background.")
            )

        daemon_group.add_option(
                "-b", "--bind",
                dest    = "wallace_bind_address",
                action  = "store",
                default = "localhost",
                help    = _("Bind address for Wallace.")
            )

        daemon_group.add_option(
                "-p", "--port",
                dest    = "wallace_port",
                action  = "store",
                default = 10026,
                help    = _("Port that Wallace is supposed to use.")
            )

        conf.finalize_conf()

        import modules
        modules.__init__()

    def process_message(self, peer, mailfrom, rcpttos, data):
        """
            We have retrieved the message.

            - Dispatch to virus-scanning and anti-spam filtering?
            - Apply access policies;
                - Maximum number of recipients,
                - kolabAllowSMTPSender,
                - kolabAllowSMTPRecipient,
                - Rule-based matching against white- and/or blacklist
                - ...
            - Accounting
            - Data Loss Prevention
        """
        inheaders = 1

        (fp, filename) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/")

        os.write(fp, data)
        os.close(fp)

        while threading.active_count() > 25:
            log.debug(
                    _("Number of threads currently running: %d") % (
                            threading.active_count()
                        ),
                    level=8
                )

            time.sleep(1)

        log.debug(
                _("Continuing with %d threads currently running") % (
                        threading.active_count()
                    ),
                level=8
            )

        # TODO: Apply throttling
        log.debug(_("Creating thread for message in %s") % (filename), level=8)

        thread = threading.Thread(target=self.thread_run, args=[ filename ])
        thread.start()

    def thread_run(self, filename, *args, **kw):
        while threading.active_count() > 25:
            log.debug(
                    _("Number of threads currently running: %d") % (
                            threading.active_count()
                        ),
                    level=8
                )

            time.sleep(10)

        log.debug(
                _("Continuing with %d threads currently running") % (
                        threading.active_count()
                    ),
                level=8
            )

        log.debug(
                _("Running thread %s for message file %s") % (
                        threading.current_thread().name,
                        filename
                    ),
                level=8
            )

        if kw.has_key('module'):
            log.debug(
                    _("This message was already in module %s, delegating " + \
                        "specifically to that module") % (
                            kw['module']
                        ),
                    level=8
                )

            if kw.has_key('stage'):
                log.debug(
                        _("It was also in a certain stage: %s, letting " + \
                            "module %s know that") % (
                                kw['stage'],
                                kw['module']
                            ),
                        level=8
                    )

                log.debug(_("Executing module %s") % (kw['module']), level=8)

                modules.execute(kw['module'], filename, stage=kw['stage'])

                return

            log.debug(_("Executing module %s") % (kw['module']), level=8)
            modules.execute(kw['module'], filename, stage=kw['stage'])

            return

        wallace_modules = conf.get_list('wallace', 'modules')
        if wallace_modules == None:
            wallace_modules = []

        for module in wallace_modules:
            log.debug(_("Executing module %s") % (module), level=8)
            modules.execute(module, filename)

    def run(self):
        """
            Run the SASL authentication daemon.
        """

        exitcode = 0

        try:
            pid = 1
            if conf.fork_mode:
                self.thread_count += 1
                pid = os.fork()

            if pid == 0:
                log.remove_stdout_handler()

            self.do_wallace()

        except SystemExit, e:
            exitcode = e
        except KeyboardInterrupt:
            exitcode = 1
            log.info(_("Interrupted by user"))
        except AttributeError, e:
            exitcode = 1
            traceback.print_exc()
            print >> sys.stderr, _("Traceback occurred, please report a " + \
                "bug at http://bugzilla.kolabsys.com")

        except TypeError, e:
            exitcode = 1
            traceback.print_exc()
            log.error(_("Type Error: %s") % e)
        except:
            exitcode = 2
            traceback.print_exc()
            print >> sys.stderr, _("Traceback occurred, please report a " + \
                "bug at http://bugzilla.kolabsys.com")

        sys.exit(exitcode)

    def pickup_defer(self):
        wallace_modules = conf.get_list('wallace', 'modules')

        if wallace_modules == None:
            wallace_modules = []

        base_path = '/var/spool/pykolab/wallace/'

        while 1:
            file_count = 0

            log.debug(_("Picking up deferred messages for wallace"), level=8)

            defer_path = os.path.join(base_path, 'DEFER')

            if os.path.isdir(defer_path):
                for root, directory, files in os.walk(defer_path):
                    for filename in files:
                        filepath = os.path.join(root, filename)

                        file_count += 1

                        for module in wallace_modules:
                            modules.execute(module, filepath)

                        time.sleep(1)

            time.sleep(1)

            for module in wallace_modules:
                log.debug(
                        _("Picking up deferred messages for module %s") % (
                                module
                            ),
                        level=8
                    )

                module_defer_path = os.path.join(base_path, module, 'DEFER')

                if os.path.isdir(module_defer_path):
                    for root, directory, files in os.walk(module_defer_path):
                        for filename in files:
                            filepath = os.path.join(root, filename)

                            file_count += 1

                            modules.execute(module, filepath)

                            time.sleep(1)

                time.sleep(1)

            # Sleep longer if last time around we didn't find any deferred
            # message files
            if file_count > 0:
                log.debug(_("Sleeping for 1 second"), level=8)
                time.sleep(1)
            else:
                log.debug(_("Sleeping for 1800 seconds"), level=8)
                time.sleep(1800)


    def do_wallace(self):
        import binascii
        import socket
        import struct

        #s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        ## TODO: The wallace socket path could be a setting.
        #try:
            #os.remove('/var/run/kolab/wallace')
        #except:
            ## TODO: Do the "could not remove, could not start dance"
            #pass

        bound = False
        while not bound:
            try:
                s.bind((conf.wallace_bind_address, conf.wallace_port))
                bound = True
            except Exception, e:
                log.warning(
                        _("Could not bind to socket on port %d on bind " + \
                            "address %s") % (
                                conf.wallace_port,
                                conf.wallace_bind_address
                            )
                    )

                try:
                    s.shutdown(1)
                except Exception, e:
                    log.warning(_("Could not shut down socket"))

                s.close()

                time.sleep(1)

        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        #os.chmod('/var/run/kolab/wallace', 0777)
        #os.chgrp('/var/run/wallace/mux', 'kolab')
        #os.chown('/var/run/wallace/mux', 'kolab')

        s.listen(5)

        # Mind you to include the trailing slash
        pickup_path = '/var/spool/pykolab/wallace/'
        for root, directory, files in os.walk(pickup_path):
            for filename in files:
                filepath = os.path.join(root, filename)

                if not root == pickup_path:
                    module = os.path.dirname(root).replace(pickup_path, '')

                    # Compare uppercase status (specifically, DEFER) with
                    # lowercase (plugin names).
                    #
                    # The messages in DEFER are supposed to be picked up by
                    # another thread, whereas the messages in other directories
                    # are pending being handled by their respective plugins.
                    #
                    # TODO: Handle messages in spool directories for which a
                    # plugin had been enabled, but is not enabled any longer.
                    #

                    if module.lower() == "defer":
                        # Wallace was unable to deliver to re-injection smtpd.
                        # Skip it, another thread is picking up the defers.
                        continue

                    stage = root.replace(pickup_path, '').split('/')
                    if len(stage) < 2:
                        stage = None
                    else:
                        stage = stage[1]

                    if stage.lower() == "hold":
                        continue

                    # Do not handle messages in a defer state.
                    if stage.lower() == "defer":
                        continue

                    log.debug(
                            _("Number of threads currently running: %d") % (
                                    threading.active_count()
                                ),
                            level=8
                        )

                    thread = threading.Thread(
                            target = self.thread_run,
                            args = [ filepath ],
                            kwargs = {
                                    "module": '%s' % (module),
                                    "stage": '%s' % (stage)
                                }
                        )

                    thread.start()
                    time.sleep(0.5)

                    continue

                log.debug(
                        _("Picking up spooled email file %s") % (
                                filepath
                            ),
                        level=8
                    )

                log.debug(
                        _("Number of threads currently running: %d") % (
                                threading.active_count()
                            ),
                        level=8
                    )

                thread = threading.Thread(
                        target=self.thread_run,
                        args=[ filepath ]
                    )

                thread.start()
                time.sleep(0.5)

        pid = os.fork()

        if pid == 0:
            self.pickup_defer()
        else:

            try:
                while 1:
                    pair = s.accept()
                    log.info(_("Accepted connection"))
                    if not pair == None:
                        connection, address = pair
                        #print "Accepted connection from %r" % (address)
                        channel = SMTPChannel(self, connection, address)
                        asyncore.loop()
            except Exception, e:
                traceback.print_exc()
                s.shutdown(1)
                s.close()