~ubuntu-branches/ubuntu/natty/amavisd-new/natty

« back to all changes in this revision

Viewing changes to helper-progs/amavis-milter-based-on-1.1.2.3.2.36.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Perrier
  • Date: 2007-02-24 19:27:53 UTC
  • mfrom: (3.1.7 feisty)
  • Revision ID: james.westby@ubuntu.com-20070224192753-fvvima53q1jrp34x
Tags: 1:2.4.2-6.1
* Non-maintainer upload to fix pending l10n issues.
* Debconf translations
  - Remove extra debian/po/de.po~
  - Convert all translation files to UTF-8
  - Russian. Closes: #405243
  - Spanish. Closes: #408734
  - Italian. Closes: #409831
* Add very simple LSB headers to init scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Based on RCSid("$Id: amavis-milter.c,v 1.1.2.3.2.36 2003/03/07 17:07:19 lhecking Exp $")
3
 
 * sendmail/milter client for amavis
4
 
 * amavisd version
5
 
 *
6
 
 * Author: Geoff Winkless <gwinkless@users.sourceforge.net>
7
 
 * Additional work and patches by:
8
 
 *   Gregory Ade
9
 
 *   Anne Bennett
10
 
 *   Thomas Biege
11
 
 *   Pierre-Yves Bonnetain
12
 
 *   Lars Hecking
13
 
 *   Rainer Link
14
 
 *   Dale Perkel
15
 
 *   Julio Sanchez
16
 
 *
17
 
 */
18
 
 
19
 
/*
20
 
 * Add some copyright notice here ...
21
 
 */
22
 
 
23
 
#include "config.h"
24
 
 
25
 
#define BUFFLEN 255
26
 
/* Must be the same as the buffer length for recv() in amavisd */
27
 
#define SOCKBUFLEN 8192
28
 
 
29
 
#include <time.h>
30
 
#include <sys/types.h>
31
 
#include <sys/wait.h>
32
 
#include <sys/stat.h>
33
 
#include <sys/utsname.h>
34
 
#include <fcntl.h>
35
 
#include <stdio.h>
36
 
#include <stdlib.h>
37
 
#include <string.h>
38
 
#include <sysexits.h>
39
 
#include <unistd.h>
40
 
#include <errno.h>
41
 
#include <netinet/in.h>
42
 
#include <sys/socket.h>
43
 
#include <sys/un.h>
44
 
#include <stdarg.h>
45
 
#include <limits.h>
46
 
 
47
 
#ifdef HAVE_SM_GEN_H
48
 
# include "sm/gen.h"
49
 
#endif
50
 
#include "libmilter/mfapi.h"
51
 
 
52
 
#ifndef HAVE_SM_GEN_BOOL_TYPE
53
 
typedef int bool;
54
 
#endif
55
 
 
56
 
#define TEMPLATE "/amavis-milter-XXXXXXXX"
57
 
#define DEVNULL "/dev/null"
58
 
 
59
 
/* Extracted from the code for better configurability
60
 
 * These will be set by configure/make eventually */
61
 
#ifndef X_HEADER_TAG
62
 
# define X_HEADER_TAG "X-Virus-Scanned"
63
 
#endif
64
 
#ifndef X_HEADER_LINE
65
 
# define X_HEADER_LINE "by amavisd-milter (http://www.amavis.org/)"
66
 
#endif
67
 
 
68
 
typedef struct llstrct {
69
 
    char *str;
70
 
    struct llstrct *next;
71
 
} ll;
72
 
 
73
 
struct mlfiPriv {
74
 
    struct in_addr client_addr;
75
 
    char *mlfi_fname;
76
 
    FILE *mlfi_fp;
77
 
    char *mlfi_envfrom;
78
 
    ll mlfi_envto;
79
 
    ll *mlfi_thisenvto;
80
 
    int mlfi_numto;
81
 
};
82
 
 
83
 
#define DBG_NONE    0
84
 
#define DBG_INFO    1
85
 
#define DBG_WARN    2
86
 
#define DBG_FATAL   4
87
 
 
88
 
#define DBG_ALL     (DBG_FATAL | DBG_WARN | DBG_INFO)
89
 
 
90
 
/* Don't debug by default - use -d N option to switch it on */
91
 
static int debuglevel = DBG_NONE;
92
 
static const char *mydebugfile = RUNTIME_DIR "/amavis.client";
93
 
 
94
 
static int enable_x_header = 1;
95
 
 
96
 
static size_t mystrlcpy(char *, const char *, size_t);
97
 
static void mydebug(const int, const char *, ...);
98
 
static char *mymktempdir(char *);
99
 
static void freeenvto(ll *);
100
 
static sfsistat clearpriv(SMFICTX *, sfsistat, int);
101
 
static int allocmem(SMFICTX *);
102
 
static sfsistat mlfi_connect(SMFICTX *, char *, _SOCK_ADDR *);
103
 
static sfsistat mlfi_envto(SMFICTX *, char **);
104
 
static sfsistat mlfi_envfrom(SMFICTX *, char **);
105
 
static sfsistat mlfi_header(SMFICTX *, char *, char *);
106
 
static sfsistat mlfi_eoh(SMFICTX *);
107
 
static sfsistat mlfi_body(SMFICTX *, u_char *, size_t);
108
 
static sfsistat mlfi_eom(SMFICTX *);
109
 
static sfsistat mlfi_close(SMFICTX *);
110
 
static sfsistat mlfi_abort(SMFICTX *);
111
 
static sfsistat mlfi_cleanup(SMFICTX *, bool);
112
 
 
113
 
static struct utsname myuts;
114
 
 
115
 
static size_t
116
 
mystrlcpy(char *dst, const char *src, size_t size)
117
 
{
118
 
    size_t src_l = strlen(src);
119
 
    if(src_l < size) {
120
 
        memcpy(dst, src, src_l + 1);
121
 
    } else if(size) {
122
 
        memcpy(dst, src, size - 1);
123
 
        dst[size - 1] = '\0';
124
 
    }
125
 
    return src_l;
126
 
}
127
 
 
128
 
static void
129
 
mydebug(const int level, const char *fmt, ...)
130
 
{
131
 
    FILE *f = NULL;
132
 
    time_t tmpt;
133
 
    char *timestamp;
134
 
    int rv;
135
 
    va_list ap;
136
 
 
137
 
    /* Only bother to do something if we're told to with -d <n> */
138
 
    if (!(level & debuglevel))
139
 
        return;
140
 
 
141
 
    /* Set up debug file */
142
 
    if (mydebugfile && (f = fopen(mydebugfile, "a")) == NULL) {
143
 
        fprintf(stderr, "error opening '%s': %s\n", mydebugfile, strerror(errno));
144
 
        return;
145
 
    }
146
 
 
147
 
    tmpt = time(NULL);
148
 
    timestamp = ctime(&tmpt);
149
 
    /* A 26 character string according ctime(3c)
150
 
     * we cut off the trailing \n\0 */
151
 
    timestamp[24] = 0;
152
 
    rv = fprintf(f, "%s %s amavis(client)[%ld]: ", timestamp,
153
 
                 (myuts.nodename ? myuts.nodename : "localhost"), (long) getpid());
154
 
    if (rv < 0)
155
 
        perror("error writing (fprintf) to debug file");
156
 
 
157
 
    va_start(ap, fmt);
158
 
    rv = vfprintf(f, fmt, ap);
159
 
    va_end(ap);
160
 
    if (rv < 0)
161
 
        perror("error writing (vfprintf) to debug file");
162
 
 
163
 
    rv = fputc('\n', f);
164
 
    if (rv < 0)
165
 
        perror("error writing (fputc) to debug file");
166
 
    rv = fclose(f);
167
 
    if (rv < 0)
168
 
        perror("error closing debug file f");
169
 
}
170
 
 
171
 
static char *
172
 
mymktempdir(char *s)
173
 
{
174
 
    char dirname[BUFFLEN];
175
 
    char *stt;
176
 
    int count = 0;
177
 
 
178
 
    mystrlcpy(dirname, s, sizeof(dirname));
179
 
 
180
 
#ifdef HAVE_MKDTEMP
181
 
    return mkdtemp(s);
182
 
#else
183
 
    /* magic number alert */
184
 
    while (count++ < 20) {
185
 
# ifdef HAVE_MKTEMP
186
 
        stt = mktemp(s);
187
 
# else
188
 
        stt = strrchr(dirname, '-') + 1;
189
 
        if (stt) {
190
 
            /* more magic number alert */
191
 
            snprintf(stt, sizeof(dirname) - 1 - (stt - dirname), "%08d", lrand48() / 215);
192
 
            /* This assumes that s in the calling function is the same size
193
 
             * as the local dirname! Beware of malicious coders ;-) */
194
 
            mystrlcpy(s, dirname, sizeof(dirname));
195
 
            stt = dirname;
196
 
        } else {
197
 
            /* invalid template */
198
 
            return NULL;
199
 
        }
200
 
# endif
201
 
        if (stt) {
202
 
            if (!mkdir(s, S_IRWXU)) {
203
 
                return s;
204
 
            } else {
205
 
                continue;
206
 
            }
207
 
        }
208
 
    }
209
 
    return NULL;
210
 
#endif /* HAVE_MKDTEMP */
211
 
}
212
 
 
213
 
#define MLFIPRIV        ((struct mlfiPriv *) smfi_getpriv(ctx))
214
 
 
215
 
static void
216
 
freeenvto(ll * envto)
217
 
{
218
 
    ll *new;
219
 
 
220
 
    while (envto) {
221
 
        new = envto->next;
222
 
        if (envto->str) {
223
 
            free(envto->str);
224
 
            envto->str = NULL;
225
 
        }
226
 
        if (envto)
227
 
            free(envto);
228
 
        envto = new;
229
 
    }
230
 
}
231
 
 
232
 
static sfsistat
233
 
clearpriv(SMFICTX *ctx, sfsistat retme, int clearall)
234
 
{
235
 
    /* release private memory and return retme */
236
 
    struct mlfiPriv *priv = MLFIPRIV;
237
 
 
238
 
    mydebug(DBG_INFO, "Clearing priv (clearall=%d)", clearall);
239
 
 
240
 
    if (priv) {
241
 
        if (priv->mlfi_fname) {
242
 
            mydebug(DBG_INFO, "clearing fname");
243
 
            free(priv->mlfi_fname);
244
 
            priv->mlfi_fname = NULL;
245
 
        }
246
 
        if (priv->mlfi_envfrom) {
247
 
            mydebug(DBG_INFO, "clearing envfrom");
248
 
            free(priv->mlfi_envfrom);
249
 
            priv->mlfi_envfrom = NULL;
250
 
        }
251
 
        if (priv->mlfi_envto.next) {
252
 
            mydebug(DBG_INFO, "clearing multi-envto");
253
 
            freeenvto(priv->mlfi_envto.next);
254
 
            priv->mlfi_envto.next = NULL;
255
 
        }
256
 
        if (priv->mlfi_envto.str) {
257
 
            mydebug(DBG_INFO, "clearing envto");
258
 
            free(priv->mlfi_envto.str);
259
 
            priv->mlfi_envto.str = NULL;
260
 
        }
261
 
 
262
 
        mydebug(DBG_INFO, "clearing priv");
263
 
        free(priv);
264
 
        priv = NULL;
265
 
        smfi_setpriv(ctx, priv);
266
 
    }
267
 
    return retme;
268
 
}
269
 
 
270
 
/*
271
 
 * allocate some private memory if not already allocated
272
 
 * returns 0 if ok, 1 if not
273
 
 */
274
 
static int
275
 
allocmem(SMFICTX * ctx)
276
 
{
277
 
    struct mlfiPriv *priv = MLFIPRIV;
278
 
 
279
 
    if (priv == NULL) {
280
 
        mydebug(DBG_INFO, "priv was null");
281
 
        priv = malloc(sizeof *priv);
282
 
        if (priv == NULL) {
283
 
            /* can't accept this message right now */
284
 
            mydebug(DBG_FATAL, "failed to malloc %d bytes for private store: %s",
285
 
                    sizeof(*priv), strerror(errno));
286
 
            return 1;
287
 
        }
288
 
        mydebug(DBG_INFO, "malloced priv - now using memset()");
289
 
        memset(priv, 0, sizeof *priv);
290
 
        mydebug(DBG_INFO, "malloced priv successfully");
291
 
        smfi_setpriv(ctx, priv);
292
 
    } else {
293
 
        mydebug(DBG_WARN, "allocmem tried but priv was already set");
294
 
        mydebug(DBG_WARN, "priv->client_addr.s_addr is %d",
295
 
                priv->client_addr.s_addr);
296
 
    }
297
 
    return 0;
298
 
}
299
 
 
300
 
static sfsistat
301
 
mlfi_connect(SMFICTX * ctx, char *hostname, _SOCK_ADDR * gen_hostaddr)
302
 
{
303
 
    struct mlfiPriv *priv;
304
 
    struct sockaddr_in *hostaddr;
305
 
 
306
 
    hostaddr = (struct sockaddr_in *) gen_hostaddr;
307
 
 
308
 
    if (hostaddr) {
309
 
        mydebug(DBG_INFO, "hostname is %s, addr is %d.%d.%d.%d",
310
 
                hostname, (hostaddr->sin_addr.s_addr) & 0xff,
311
 
                (hostaddr->sin_addr.s_addr >> 8) & 0xff,
312
 
                (hostaddr->sin_addr.s_addr >> 16) & 0xff,
313
 
                (hostaddr->sin_addr.s_addr >> 24) & 0xff);
314
 
    }
315
 
    mydebug(DBG_INFO, "checking allocmem");
316
 
    if (allocmem(ctx))
317
 
        return SMFIS_TEMPFAIL;
318
 
    priv = MLFIPRIV;
319
 
    if (hostaddr) {
320
 
        priv->client_addr.s_addr = hostaddr->sin_addr.s_addr;
321
 
    } else {
322
 
        priv->client_addr.s_addr = 0;
323
 
    }
324
 
    smfi_setpriv(ctx, priv);    /* not really needed but nice */
325
 
    return SMFIS_CONTINUE;
326
 
}
327
 
 
328
 
static sfsistat
329
 
mlfi_envto(SMFICTX * ctx, char **envto)
330
 
{
331
 
    struct mlfiPriv *priv;
332
 
 
333
 
    if (allocmem(ctx))
334
 
        return SMFIS_TEMPFAIL;
335
 
 
336
 
    priv = MLFIPRIV;
337
 
    if (!(priv->mlfi_thisenvto)) {
338
 
        /* first one... */
339
 
        priv->mlfi_thisenvto = &(priv->mlfi_envto);
340
 
        priv->mlfi_numto = 1;
341
 
    } else {
342
 
        priv->mlfi_numto++;
343
 
        if ((priv->mlfi_thisenvto->next = malloc(sizeof(ll))) == NULL)
344
 
            return (SMFIS_TEMPFAIL);
345
 
        priv->mlfi_thisenvto = priv->mlfi_thisenvto->next;
346
 
        priv->mlfi_thisenvto->next = NULL;
347
 
    }
348
 
    if ((priv->mlfi_thisenvto->str = strdup(*envto)) == NULL)
349
 
        return (SMFIS_TEMPFAIL);
350
 
    mydebug(DBG_INFO, "added %s as recip", *envto);
351
 
    return SMFIS_CONTINUE;
352
 
}
353
 
 
354
 
static sfsistat
355
 
mlfi_envfrom(SMFICTX * ctx, char **envfrom)
356
 
{
357
 
    struct mlfiPriv *priv;
358
 
    char dirname[BUFFLEN];
359
 
    char messagecopy[BUFFLEN];
360
 
    struct stat buf, StatBuf;
361
 
 
362
 
    if (allocmem(ctx))
363
 
        return SMFIS_TEMPFAIL;
364
 
    priv = MLFIPRIV;
365
 
 
366
 
    /* open a file to store this message */
367
 
    mystrlcpy(dirname, RUNTIME_DIR, sizeof(dirname));
368
 
    strncat(dirname, TEMPLATE, sizeof(dirname) - 1 - strlen(dirname));
369
 
    if (mymktempdir(dirname) == NULL) {
370
 
        mydebug(DBG_FATAL, "Failed to create temp dir %s: %s", dirname,
371
 
                strerror(errno));
372
 
        return SMFIS_TEMPFAIL;
373
 
    }
374
 
    snprintf(messagecopy, sizeof(messagecopy), "%s/email.txt", dirname);
375
 
    mydebug(DBG_INFO, "got %s file, %s sender", messagecopy, *envfrom);
376
 
 
377
 
    priv->mlfi_fname = strdup(messagecopy);
378
 
    if (priv->mlfi_fname == NULL) {
379
 
        return SMFIS_TEMPFAIL;
380
 
    }
381
 
    priv->mlfi_envfrom = strdup(*envfrom);
382
 
    if (!priv->mlfi_envfrom) {
383
 
        free(priv->mlfi_fname);
384
 
        priv->mlfi_fname = NULL;
385
 
        return SMFIS_TEMPFAIL;
386
 
    }
387
 
    /* umask(0077); */
388
 
    if (lstat(dirname, &StatBuf) < 0) {
389
 
        mydebug(DBG_FATAL, "Error while trying lstat(%s): %s", dirname,
390
 
                strerror(errno));
391
 
        exit(EX_UNAVAILABLE);
392
 
    }
393
 
 
394
 
    /* may be too restrictive for you, but's good to avoid problems */
395
 
    if (!S_ISDIR(StatBuf.st_mode) || StatBuf.st_uid != geteuid() || StatBuf.st_gid != getegid() || !(StatBuf.st_mode & S_IRWXU)) {
396
 
        mydebug(DBG_FATAL,
397
 
                "Security Warning: %s must be a Directory and owned by "
398
 
                "User %d and Group %d\n"
399
 
                "and just read-/write-able by the User and noone else. "
400
 
                "Exit.", dirname, geteuid(), getegid());
401
 
        exit(EX_UNAVAILABLE);
402
 
    }
403
 
    /* there is still a race condition here if RUNTIME_DIR is writeable by the attacker :-\ */
404
 
 
405
 
    if ((priv->mlfi_fp = fopen(priv->mlfi_fname, "w+")) == NULL) {
406
 
        free(priv->mlfi_fname);
407
 
        priv->mlfi_fname = NULL;
408
 
        free(priv->mlfi_envfrom);
409
 
        priv->mlfi_envfrom = NULL;
410
 
        return SMFIS_TEMPFAIL;
411
 
    }
412
 
 
413
 
    stat(dirname, &buf);
414
 
#if 0
415
 
    if (buf.st_uid != geteuid() || (buf.st_mode & 0777) != 0700) {
416
 
        mydebug(DBG_FATAL,
417
 
                "Security alert -- someone changed %s. Uid is %d, mode is %o",
418
 
                dirname, buf.st_uid, 0777 & buf.st_mode);
419
 
        fclose(priv->mlfi_fp);
420
 
        unlink(priv->mlfi_fname);
421
 
        unlink(dirname);
422
 
        free(priv->mlfi_fname);
423
 
        free(priv->mlfi_envfrom);
424
 
        free(priv);
425
 
        return SMFIS_TEMPFAIL;
426
 
    }
427
 
#endif
428
 
 
429
 
    /* save the private data */
430
 
 
431
 
    smfi_setpriv(ctx, priv);
432
 
 
433
 
    /* continue processing */
434
 
    return SMFIS_CONTINUE;
435
 
}
436
 
 
437
 
static sfsistat
438
 
mlfi_header(SMFICTX *ctx, char *headerf, char *headerv)
439
 
{
440
 
    /* write the header to the log file */
441
 
    fprintf(MLFIPRIV->mlfi_fp, "%s: %s\n", headerf, headerv);
442
 
 
443
 
    /* continue processing */
444
 
    return SMFIS_CONTINUE;
445
 
}
446
 
 
447
 
static sfsistat
448
 
mlfi_eoh(SMFICTX *ctx)
449
 
{
450
 
    /* output the blank line between the header and the body */
451
 
    fprintf(MLFIPRIV->mlfi_fp, "\n");
452
 
 
453
 
    /* continue processing */
454
 
    return SMFIS_CONTINUE;
455
 
}
456
 
 
457
 
static sfsistat
458
 
mlfi_body(SMFICTX *ctx, u_char *bodyp, size_t bodylen)
459
 
{
460
 
    /* output body block to log file */
461
 
    u_char *d = bodyp, *s = bodyp;
462
 
    u_char *lastc = bodyp + bodylen - 1;
463
 
 
464
 
    /* convert crlf to lf */
465
 
    while (s <= lastc) {
466
 
        if (s != lastc && *s == 13 && *(s+1) == 10)
467
 
            s++;
468
 
 
469
 
        *d++ = *s++;
470
 
    }
471
 
    bodylen = (size_t)(d - bodyp);
472
 
 
473
 
    if (bodylen && fwrite(bodyp, bodylen, 1, MLFIPRIV->mlfi_fp) <= 0) {
474
 
        /* write failed */
475
 
        (void) mlfi_cleanup(ctx, 0);
476
 
        return SMFIS_TEMPFAIL;
477
 
    }
478
 
 
479
 
    /* continue processing */
480
 
    return SMFIS_CONTINUE;
481
 
}
482
 
 
483
 
/* Simple "protocol" */
484
 
const char _EOT = '\3';
485
 
 
486
 
static sfsistat
487
 
mlfi_eom(SMFICTX *ctx)
488
 
{
489
 
    struct mlfiPriv *priv = MLFIPRIV;
490
 
    char buff[7];
491
 
    int sock, r;
492
 
    char *sender;
493
 
    char retval;
494
 
    struct sockaddr_un saddr;
495
 
 
496
 
    if (priv) {
497
 
        int x;
498
 
 
499
 
        mydebug(DBG_INFO, "EOM");
500
 
        /* close the file so we can run checks on it!!! */
501
 
        if (priv->mlfi_fp)
502
 
            fclose(priv->mlfi_fp);
503
 
 
504
 
        /* AFAIK, AF_UNIX is obsolete. POSIX defines AF_LOCAL */
505
 
        saddr.sun_family = AF_UNIX;
506
 
        mystrlcpy(saddr.sun_path, AMAVISD_SOCKET, sizeof(saddr.sun_path));
507
 
        mydebug(DBG_INFO, "allocate socket()");
508
 
        r = (sock = socket(PF_UNIX, SOCK_STREAM, 0));
509
 
        if (r < 0) {
510
 
            mydebug(DBG_FATAL, "failed to allocate socket: %s",
511
 
                    strerror(errno));
512
 
        }
513
 
        if (r >= 0) {
514
 
            mydebug(DBG_INFO, "connect()");
515
 
            r = connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr));
516
 
            if (r < 0)
517
 
                mydebug(DBG_FATAL, "failed to connect(): %s", strerror(errno));
518
 
        }
519
 
        if (r >= 0) {
520
 
            strrchr(priv->mlfi_fname, '/')[0] = 0;
521
 
            /* amavis-perl wants the directory, not the filename */
522
 
            mydebug(DBG_INFO, "senddir()");
523
 
            r = send(sock, priv->mlfi_fname, strlen(priv->mlfi_fname), 0);
524
 
            if (r < 0)
525
 
                mydebug(DBG_FATAL, "failed to send() directory: %s",
526
 
                        strerror(errno));
527
 
        }
528
 
        if (r >= 0) {
529
 
            r = recv(sock, &retval, 1, 0);
530
 
            if (r < 0)
531
 
                mydebug(DBG_FATAL,
532
 
                        "failed to recv() directory confirmation: %s",
533
 
                        strerror(errno));
534
 
        }
535
 
        if (r >= 0) {
536
 
            int sender_l;
537
 
            sender = (strlen(priv->mlfi_envfrom) > 0) ? priv->mlfi_envfrom : "<>";
538
 
            mydebug(DBG_INFO, "sendfrom() %s", sender);
539
 
            sender_l = strlen(sender);
540
 
            if (sender_l > SOCKBUFLEN) {
541
 
                mydebug(DBG_WARN, "Sender too long (%d), truncated to %d characters", sender_l, SOCKBUFLEN);
542
 
                sender_l = SOCKBUFLEN;
543
 
            }
544
 
            r = send(sock, sender, sender_l, 0);
545
 
            if (r < 0)
546
 
                mydebug(DBG_FATAL, "failed to send() Sender: %s", strerror(errno));
547
 
            else if (r < sender_l)
548
 
                mydebug(DBG_WARN, "failed to send() complete Sender, truncated to %d characters ", r);
549
 
        }
550
 
        if (r >= 0) {
551
 
            r = recv(sock, &retval, 1, 0);
552
 
            if (r < 0)
553
 
                mydebug(DBG_FATAL, "failed to recv() ok for Sender info: %s",
554
 
                        strerror(errno));
555
 
        }
556
 
        if (r >= 0) {
557
 
            priv->mlfi_thisenvto = &(priv->mlfi_envto);
558
 
            for (x = 0; (r >= 0) && (x < priv->mlfi_numto); x++) {
559
 
                int recipient_l;
560
 
                mydebug(DBG_INFO, "sendto() %s", priv->mlfi_thisenvto->str);
561
 
                recipient_l = strlen(priv->mlfi_thisenvto->str);
562
 
                if (recipient_l > SOCKBUFLEN) {
563
 
                    mydebug(DBG_WARN, "Recipient too long (%d), truncated to %d characters", recipient_l,SOCKBUFLEN);
564
 
                    recipient_l = SOCKBUFLEN;
565
 
                }
566
 
                r = send(sock, priv->mlfi_thisenvto->str, recipient_l, 0);
567
 
                if (r < 0)
568
 
                    mydebug(DBG_FATAL, "failed to send() Recipient: %s",
569
 
                            strerror(errno));
570
 
                else {
571
 
                    if (r < recipient_l)
572
 
                        mydebug(DBG_WARN, "failed to send() complete Recipient, truncated to %d characters ", r);
573
 
                    r = recv(sock, &retval, 1, 0);
574
 
                    if (r < 0)
575
 
                        mydebug(DBG_FATAL,
576
 
                                "failed to recv() ok for recip info: %s",
577
 
                                strerror(errno));
578
 
                    priv->mlfi_thisenvto = priv->mlfi_thisenvto->next;
579
 
                }
580
 
            }
581
 
        }
582
 
        if (r >= 0) {
583
 
            mydebug(DBG_INFO, "sendEOT()");
584
 
            r = send(sock, &_EOT, 1, 0);
585
 
            /* send "end of args" msg */
586
 
            if (r < 0) {
587
 
                mydebug(DBG_FATAL, "failed to send() EOT: %s", strerror(errno));
588
 
            } else {
589
 
                r = recv(sock, buff, 6, 0);
590
 
                if (r < 0)
591
 
                    mydebug(DBG_FATAL, "Failed to recv() final result: %s",
592
 
                            strerror(errno));
593
 
                else if (r == 0)
594
 
                    mydebug(DBG_FATAL, "Failed to recv() final result: empty status string");
595
 
                /* get back final result */
596
 
            }
597
 
        }
598
 
        close(sock);
599
 
        mydebug(DBG_INFO, "finished conversation\n");
600
 
        if (r < 0) {
601
 
            return clearpriv(ctx, SMFIS_TEMPFAIL, 0);
602
 
            /* some point of the communication failed miserably - so give up */
603
 
        }
604
 
 
605
 
        /* Protect against empty return string */
606
 
        if (*buff)
607
 
            retval = atoi(buff);
608
 
        else
609
 
            retval = 1;
610
 
 
611
 
        mydebug(DBG_INFO, "retval is %d", retval);
612
 
        if (retval == 99) {
613
 
            /* there was a virus
614
 
             * discard it so it doesn't go bouncing around!! */
615
 
            mydebug(DBG_WARN, "discarding mail");
616
 
            return clearpriv(ctx, SMFIS_DISCARD, 0);
617
 
        }
618
 
        if (retval == EX_UNAVAILABLE) { /* REJECT handling */
619
 
                                       /* by Didi Rieder and Mark Martinec */
620
 
            mydebug(DBG_WARN, "rejecting mail");
621
 
            smfi_setreply(ctx, "550", "5.7.1", "Message content rejected");
622
 
            return clearpriv(ctx, SMFIS_REJECT, 0);
623
 
        }
624
 
        if (retval == 0) {
625
 
            if (enable_x_header) {
626
 
                if (smfi_chgheader(ctx, X_HEADER_TAG, 1, X_HEADER_LINE) == MI_FAILURE) {
627
 
                    mydebug(DBG_INFO, "adding header");
628
 
                    smfi_addheader(ctx, X_HEADER_TAG, X_HEADER_LINE);
629
 
                } else
630
 
                    mydebug(DBG_INFO, "header already present");
631
 
            }
632
 
            mydebug(DBG_WARN, "returning ACCEPT");
633
 
            return clearpriv(ctx, SMFIS_ACCEPT, 0);
634
 
        }
635
 
        /* if we got any exit status but 0, we didn't check the file...
636
 
         * so don't add the header. We return TEMPFAIL instead */
637
 
 
638
 
        mydebug(DBG_WARN, "returning TEMPFAIL");
639
 
        return clearpriv(ctx, SMFIS_TEMPFAIL, 0);
640
 
    }
641
 
    mydebug(DBG_WARN, "couldn't scan - no priv object");
642
 
    return clearpriv(ctx, SMFIS_TEMPFAIL, 0);   /* no priv object */
643
 
}
644
 
 
645
 
static sfsistat
646
 
mlfi_close(SMFICTX *ctx)
647
 
{
648
 
    return clearpriv(ctx, SMFIS_ACCEPT, 1);
649
 
}
650
 
 
651
 
static sfsistat
652
 
mlfi_abort(SMFICTX *ctx)
653
 
{
654
 
    return mlfi_cleanup(ctx, 0);
655
 
}
656
 
 
657
 
static sfsistat
658
 
mlfi_cleanup(SMFICTX *ctx, bool ok)
659
 
{
660
 
    sfsistat rstat = SMFIS_CONTINUE;
661
 
    struct mlfiPriv *priv = MLFIPRIV;
662
 
    char *p;
663
 
    char host[512];
664
 
 
665
 
    if (!priv)
666
 
        return rstat;
667
 
 
668
 
    /* close the archive file */
669
 
    if (priv->mlfi_fp != NULL && fclose(priv->mlfi_fp) == EOF) {
670
 
        /* failed; we have to wait until later */
671
 
        rstat = SMFIS_TEMPFAIL;
672
 
        (void) unlink(priv->mlfi_fname);
673
 
    } else if (ok) {
674
 
        /* add a header to the message announcing our presence */
675
 
        if (gethostname(host, sizeof(host) - 1) < 0)
676
 
            mystrlcpy(host, "localhost", sizeof host);
677
 
        p = strrchr(priv->mlfi_fname, '/');
678
 
        if (p == NULL)
679
 
            p = priv->mlfi_fname;
680
 
        else
681
 
            p++;
682
 
    } else {
683
 
        /* message was aborted -- delete the archive file */
684
 
        (void) unlink(priv->mlfi_fname);
685
 
        *(strrchr(priv->mlfi_fname, '/')) = 0;
686
 
        rmdir(priv->mlfi_fname);
687
 
    }
688
 
 
689
 
    /* return status */
690
 
    mydebug(DBG_INFO, "cleanup called");
691
 
    return clearpriv(ctx, rstat, 0);
692
 
}
693
 
 
694
 
 
695
 
struct smfiDesc smfilter = {
696
 
    "amavis-milter",            /* filter name */
697
 
    SMFI_VERSION,               /* version code -- do not change */
698
 
    SMFIF_ADDHDRS|SMFIF_CHGHDRS,/* flags */
699
 
    mlfi_connect,               /* connection info filter */
700
 
    NULL,                       /* SMTP HELO command filter */
701
 
    mlfi_envfrom,               /* envelope sender filter */
702
 
    mlfi_envto,                 /* envelope recipient filter */
703
 
    mlfi_header,                /* header filter */
704
 
    mlfi_eoh,                   /* end of header */
705
 
    mlfi_body,                  /* body block filter */
706
 
    mlfi_eom,                   /* end of message */
707
 
    mlfi_abort,                 /* message aborted */
708
 
    mlfi_close                  /* connection cleanup */
709
 
};
710
 
 
711
 
 
712
 
int
713
 
main(int argc, char *argv[])
714
 
{
715
 
    int c, i;
716
 
    const char *args = "p:d:Dx";
717
 
    pid_t pid;
718
 
    int AM_DAEMON = 0;
719
 
    int devnull;
720
 
 
721
 
#if !defined(HAVE_MKDTEMP) && !defined(HAVE_MKTEMP)
722
 
    int mypid = getpid();
723
 
 
724
 
    srand48(time(NULL) ^ (mypid + (mypid << 15)));
725
 
#endif
726
 
 
727
 
/*  umask(0077); */
728
 
    umask(0007);
729
 
 
730
 
    /* Process command line options */
731
 
    while ((c = getopt(argc, argv, args)) != -1) {
732
 
        switch (c) {
733
 
        case 'p':
734
 
            if (optarg == NULL || *optarg == '\0') {
735
 
                mydebug(DBG_FATAL, "Illegal conn: %s", optarg);
736
 
                exit(EX_USAGE);
737
 
            }
738
 
            /* Unlink any existing file that might be in place of
739
 
             * the socket we want to create.  This might not exactly
740
 
             * be safe, or friendly, but I'll deal with that later.
741
 
             * Be nice and issue a warning if we have a problem, but
742
 
             * other than that, ignore it. */
743
 
            if ((unlink(optarg)) < 0) {
744
 
                mydebug(DBG_WARN, "unlink(): %s: %s", optarg, strerror(errno));
745
 
            }
746
 
            (void) smfi_setconn(optarg);
747
 
            break;
748
 
        case 'd':
749
 
            switch (atoi(optarg)) {
750
 
            case 3:
751
 
                debuglevel = DBG_INFO;
752
 
            case 2:
753
 
                debuglevel = debuglevel | DBG_WARN;
754
 
            case 1:
755
 
                debuglevel = debuglevel | DBG_FATAL;
756
 
                break;
757
 
            case 0:
758
 
                debuglevel = DBG_NONE;
759
 
                break;
760
 
            default:
761
 
                debuglevel = DBG_ALL;
762
 
                break;
763
 
            }
764
 
            break;
765
 
        case 'D':
766
 
            AM_DAEMON = 1;
767
 
            break;
768
 
        case 'x':
769
 
            enable_x_header++;
770
 
            break;
771
 
        default:
772
 
            break;
773
 
        }
774
 
    }
775
 
    if (smfi_register(smfilter) == MI_FAILURE) {
776
 
        mydebug(DBG_FATAL, "smfi_register failed");
777
 
        exit(EX_UNAVAILABLE);
778
 
    }
779
 
 
780
 
    /* See if we're supposed to become a daemonized process */
781
 
    if (AM_DAEMON == 1) {
782
 
 
783
 
        /* 2001/11/09 Anne Bennett: daemonize properly.
784
 
         * OK, let's be a real daemon.  Taken from page 417
785
 
         * of Stevens' "Advanced Programming in the UNIX Environment".
786
 
         */
787
 
 
788
 
        /* Step 1: Fork and have parent exit.  This not only
789
 
         * backgrounds us but makes sure we are not a process group
790
 
         * leader.
791
 
         */
792
 
 
793
 
        /* Fork ourselves into the background, and see if it worked */
794
 
        if ((pid = fork()) > 0) {
795
 
 
796
 
            mydebug(DBG_INFO, "amavis-milter forked into background");
797
 
            /* We are the parent; exit. */
798
 
            exit(0);
799
 
 
800
 
        } else if (pid == -1) {
801
 
 
802
 
            mydebug(DBG_FATAL, "fork() returned error: %s", strerror(errno));
803
 
            exit(EX_UNAVAILABLE);
804
 
 
805
 
        }
806
 
 
807
 
        /* OK, we're backgrounded.
808
 
         * Step 2: Call setsid to create a new session.  This makes
809
 
         * sure among other things that we have no controlling
810
 
         * terminal.
811
 
         */
812
 
        if ( setsid() < (pid_t)0 ) {
813
 
            mydebug(DBG_FATAL, "setsid() returned error: %s", strerror(errno));
814
 
            exit(EX_UNAVAILABLE);
815
 
        }
816
 
 
817
 
        /* Step 3: Set the working directory appropriately. */
818
 
        if (chdir("/") < 0 ) {
819
 
            mydebug(DBG_FATAL, "chdir(/) returned error: %s", strerror(errno));
820
 
            exit(EX_UNAVAILABLE);
821
 
        }
822
 
 
823
 
        /* Step 4: Close all file descriptors. */
824
 
        for (i = 0; i < _POSIX_OPEN_MAX ; i++) {
825
 
            close(i);
826
 
        }
827
 
 
828
 
        /* Open /dev/null read-only (fd 0 = STDIN) */
829
 
        if ((devnull = open(DEVNULL, O_RDONLY, 0)) < 0) {
830
 
             mydebug(DBG_FATAL, "Could not open %s as STDIN: %s", DEVNULL,
831
 
                     strerror(errno));
832
 
             exit(EX_UNAVAILABLE);
833
 
        }
834
 
        if (devnull != 0) {
835
 
             mydebug(DBG_FATAL, "Got wrong file descriptor as STDIN: %s != 0",
836
 
                     DEVNULL);
837
 
             exit(EX_UNAVAILABLE);
838
 
        }
839
 
 
840
 
        /* Open /dev/null write-only (fd 1 = STDOUT) */
841
 
        if ((devnull = open(DEVNULL, O_WRONLY, 0)) < 0) {
842
 
             mydebug(DBG_FATAL, "Could not open %s as STDOUT: %s", DEVNULL,
843
 
                     strerror(errno));
844
 
             exit(EX_UNAVAILABLE);
845
 
        }
846
 
        if (devnull != 1) {
847
 
             mydebug(DBG_FATAL, "Got wrong file descriptor as STDOUT: %s != 1",
848
 
                     DEVNULL);
849
 
             exit(EX_UNAVAILABLE);
850
 
        }
851
 
 
852
 
        /* Open /dev/null write-only (fd 2 = STDERR) */
853
 
        if ((devnull = open(DEVNULL, O_WRONLY, 0)) < 0) {
854
 
             mydebug(DBG_FATAL, "Could not open %s as STDERR: %s", DEVNULL,
855
 
                     strerror(errno));
856
 
             exit(EX_UNAVAILABLE);
857
 
        }
858
 
        if (devnull != 2) {
859
 
             mydebug(DBG_FATAL, "Got wrong file descriptor as STDERR: %s != 2",
860
 
                     DEVNULL);
861
 
             exit(EX_UNAVAILABLE);
862
 
        }
863
 
 
864
 
    }
865
 
 
866
 
    /* hand control over to libmilter */
867
 
    mydebug(DBG_INFO, "Starting, handing off to smfi_main");
868
 
    return smfi_main();
869
 
}
870
 
 
871
 
/* eof */