summaryrefslogtreecommitdiff
path: root/testsuite/mockmail.c
blob: bae7381e4ca47608d914c99909f80dcf2d754b73 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
  NAME
    mockmail - mock Sendmail binary for use in test suites

  SYNOPSIS
    mockmail [-bm] [-f EMAIL] [-itv] [-oi] [EMAIL ...]

  DESCRIPTION
    Mimicks the behavior of "sendmail -bm".  Instead of delivering
    the message, dumps it to the file "mail.dump".  The location of
    the dump file can be changed by setting the MAIL_DUMP environment
    variable.

    Being a mailutils test tool, mockname is written without relying on
    the mailutils libraries.  Only libc is needed.

    Message to be "delivered" is read from the standard input.
    
    Only rudimental message parsing is performed, as necessary for
    the functioning of the -t option.  Similarly, email address parser
    is very naive.

  OPTIONS
    -bm       Ignored for compatibility with Sendmail.
    -f EMAIL  Sets sender email address.
    -i        Same as -oi.
    -t        Read recipients from the message.
    -oi       Don't expect the incoming message to be terminated with
              a dot.  This also turns off dot unstuffing.
    -v        Ignored for compatibility with Sendmail.

  ENVIRONMENT
  
    MAIL_DUMP
      Name of the dump file ("mail.dump")
      
    MAIL_DUMP_APPEND
      When set, append new entries to the mail.dump file, instead of
      overwriting it.

  DUMP FORMAT
    The message is represented as a series of records:

    MSGID: 0001
      This record is for compatibility with mockmta.
    SENDER: <string>
      Sender email address as given by the -f option, if given.
    NRCPT: <numeric>
      Number of recipients.
   
    The list of recipients follows this line.  Each record in the list is
    
    RCPT[<I>]: <string>

    where <I> is 0-based index of the recipient in recipient table.

    LENGTH: <N>
      Total length of the data section, after eventual dot-unstuffing.
      This does not include terminating dot (if such was present and
      the -oi option was not given).

      This line is followed by <N> bytes representing the material received
      from the standard input.

    Message dump is terminated by a single LF character.

  BUGS
    At most 32 recipients are allowed.
    Header and address parsing is rudimental.
    
  AUTHOR
    Sergey Poznyakoff <gray@gnu.org>
    
  LICENSE
    This program is part of GNU Mailutils testsuite.
    Copyright (C) 2020-2021 Free Software Foundation, Inc.

    Mockmta 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; either version 3, or (at your option)
    any later version.

    Mockmta 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.

*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

char *progname;
FILE *logfile;

char *from_person = NULL; /* Set the name of the `from' person */
int read_recipients = 0;  /* Read the message for recipients */
int dot = 1;              /* Message is terminated by a lone dot on a line */

#define MAXRCPT 32

enum
  {
    EX_OK,
    EX_FAILURE,
    EX_USAGE
  };

static void
terror (char const *fmt, ...)
{
  va_list ap;
  int m;
  static char *fmtbuf = NULL;
  static size_t fmtsize = 0;
  int ec = errno;
  char const *es = NULL;
  size_t len;
  
  for (m = 0; fmt[m += strcspn (fmt + m, "%")]; )
    {
      m++;
      if (fmt[m] == 'm')
	break;
    }      

  len = strlen (fmt) + 1;
  if (fmt[m])
    {
      es = strerror (ec);
      len += strlen (es) - 2;
    }
  if (len > fmtsize)
    {
      fmtsize = len;
      fmtbuf = realloc (fmtbuf, fmtsize);
      if (!fmtbuf)
	{
	  perror ("realloc");
	  exit (EX_FAILURE);
	}
    }

  if (es)
    {
      memcpy (fmtbuf, fmt, m - 1);
      memcpy (fmtbuf + m - 1, es, strlen (es) + 1);
      strcat (fmtbuf, fmt + m + 1);
    }
  else
    strcpy (fmtbuf, fmt);

  fprintf (stderr, "%s: ", progname);
  va_start (ap, fmt);
  vfprintf (stderr, fmtbuf, ap);
  va_end (ap);
  fputc ('\n', stderr);
}

static void
nomemory (void)
{
  terror ("out of memory");
  exit (EX_FAILURE);
}

static inline int
is_blank (int c)
{
  return c == ' ' || c == '\t';
}

enum { H_INIT, H_HEADER, H_NL, B_INIT, B_NL, B_DOT };

struct header_map
{
  struct header_map *prev, *next;  /* List of elements */
  size_t hstart;        /* Header start offset */
  size_t nlen;          /* Header name length */
  size_t vstart;        /* Value start offset */
  size_t end;           /* Header end offset (points past the final LF) */
};

struct message
{
  char *buf_ptr;
  size_t buf_len;
  size_t buf_size;
  size_t line_len;
  
  char *rcpt[MAXRCPT];
  int nrcpt;

  size_t header_len;
  struct header_map *header_head, *header_tail;
  
  int state;
};

static void
msg_header_add (struct message *msg)
{
  struct header_map *hmap;
  char *hdr;
  size_t i;
  
  hmap = calloc (1, sizeof hmap[0]);
  if (!hmap)
    nomemory ();
  hmap->hstart = msg->buf_len - msg->line_len;
  hdr = msg->buf_ptr + hmap->hstart;
  for (i = msg->line_len - 1; i > 0; i--)
    if (!is_blank (hdr[i-1]))
      break;
  hmap->nlen = i;
  hmap->vstart = msg->buf_len;
  hmap->next = NULL;
  hmap->prev = msg->header_tail;
  if (msg->header_tail)
    msg->header_tail->next = hmap;
  else
    msg->header_head = hmap;
  msg->header_tail = hmap;
}

static void
msg_header_update (struct message *msg)
{
  struct header_map *hmap = msg->header_tail;
  hmap->end = msg->buf_len;
  while (hmap->vstart < hmap->end && is_blank (msg->buf_ptr[hmap->vstart]))
    hmap->vstart++;
}

static void
msg_add_rcpt (struct message *msg, char const *email)
{
  if (msg->nrcpt == MAXRCPT)
    {
      terror ("too many recipients");
      exit (EX_USAGE);
    }
  if ((msg->rcpt[msg->nrcpt] = strdup (email)) == NULL)
    nomemory ();
  msg->nrcpt++;
}

static void
msg_add_char (struct message *msg, int c)
{
  while (msg->buf_len + 1 > msg->buf_size)
    {
      char *p;
      size_t n = msg->buf_size;
      if (!msg->buf_ptr)
	{
	  n = 64;
	}
      else
	{
	  if ((size_t)-1 / 3 * 2 <= n)
	    nomemory ();
	  n += (n + 1) / 2;
	}
      p = realloc (msg->buf_ptr, n);
      if (!p)
	nomemory ();
      msg->buf_ptr = p;
      msg->buf_size = n;
    }
  msg->buf_ptr[msg->buf_len++] = c;
  if (c == '\n')
    msg->line_len = 0;
  else
    msg->line_len++;
}

int
main (int argc, char **argv)
{
  struct message msg;
  int c;
  char *filename;
  struct header_map *hmap;
  
  progname = argv[0];
  
  while ((c = getopt (argc, argv, "b:f:ito:v")) != EOF)
    {
      switch (c)
	{
	case 'b':
	  if (strcmp (optarg, "m"))
	    {
	      terror ("-b%s not supported", optarg);
	      exit (EX_USAGE);
	    }
	  break;
	  
	case 'f':
	  from_person = optarg;
	  break;

	case 'i':
	  dot = 0;
	  break;
	  
	case 't':
	  read_recipients = 1;
	  break;

	case 'o':
	  switch (optarg[0])
	    {
	    case 'i':
	      dot = 0;
	      break;
	  
	    default:
	      /* IGNORED */ ;
	    }
	  break;

	case 'v':
	  break;
	  
	default:
	  exit (EX_USAGE);
	}
    }

  memset (&msg, 0, sizeof (msg));
  
  for (c = optind; c < argc; c++)
    msg_add_rcpt (&msg, argv[c]);

  msg.state = H_INIT;

  while ((c = getchar ()) != EOF)
    {
      msg_add_char (&msg, c);
      switch (msg.state)
	{
	case H_INIT:
	  if (c == ':')
	    {
	      msg_header_add (&msg);
	      msg.state = H_HEADER;
	    }
	  break;

	case H_HEADER:
	  if (c == '\n')
	    msg.state = H_NL;
	  break;

	case H_NL:
	  if (is_blank (c))
	    msg.state = H_HEADER;
	  else
	    {
	      msg_header_update (&msg);
	      if (c == '\n')
		{
		  msg.header_len = msg.buf_len;
		  msg.state = B_INIT;
		}
	      else
		msg.state = H_INIT;
	    }
	  break;

	case B_INIT:
	  if (c == '\n')
	    msg.state = B_NL;
	  break;

	case B_NL:
	  if (c == '.')
	    msg.state = B_DOT;
	  else
	    msg.state = B_INIT;
	  break;

	case B_DOT:
	  if (c == '\n')
	    {
	      if (dot)
		goto end;
	      msg.state = B_NL;
	    }
	  else
	    {
	      if (c == '.' && dot)
		/* unstuff */
		msg.buf_len--;
	      msg.state = B_INIT;
	    }
	  break;
	}
    }
 end:

  switch (msg.state)
    {
    case H_INIT:
    case H_HEADER:
      terror ("malformed message");
      break;
      
    case B_DOT:
      msg.buf_len -= 2;
      break;

    default:
      if (dot)
	terror ("missing terminating dot");
    }

  if (read_recipients)
    {
      for (hmap = msg.header_head; hmap; hmap = hmap->next)
	{
	  char *p = msg.buf_ptr + hmap->hstart;
	  if ((hmap->nlen == 2 && strncasecmp (p, "to", 2) == 0) ||
	      (hmap->nlen == 2 && strncasecmp (p, "cc", 2) == 0) ||
	      (hmap->nlen == 3 && strncasecmp (p, "bcc", 3) == 0))
	    {
	      char *cp;
	      size_t i, j;
	      size_t len = hmap->end - hmap->vstart;
	      int unwrap = 0;
	      
	      p = msg.buf_ptr + hmap->vstart;
	      
	      /* Allocate temporary value copy */
	      cp = malloc (len + 1);
	      if (!p)
		nomemory ();
	      /* Unwrap the value */
	      for (i = j = 0; i < len; i++)
		{
		  if (p[i] == '\n')
		    unwrap = 1;
		  else if (unwrap)
		    {
		      unwrap = 0;
		      continue;
		    }
		  else
		    cp[j++] = p[i];
		}
	      cp[j] = 0;
	      
	      /* A *very* naive and simplified address parser */
	      for (p = strtok (cp, ","); p; p = strtok (NULL, ","))
		{
		  char *q = strchr (p, '<');
		  if (q)
		    {
		      p = strchr (q, '>');
		      if (p)
			p[1] = 0;
		      msg_add_rcpt (&msg, q);
		    }
		  else
		    msg_add_rcpt (&msg, p);
		}
	      free (cp);
	    }
	}
    }

  if (msg.nrcpt == 0)
    terror ("no recipients");
  
  filename = getenv ("MAIL_DUMP");
  if (!filename)
    filename = "mail.dump";
    
  logfile = fopen (filename, getenv ("MAIL_DUMP_APPEND") ? "a" : "w");
  if (!logfile)
    {
      terror ("can't open dump file %s: %m", filename);
      exit (EX_FAILURE);
    }

  fprintf (logfile, "MSGID: %04d\n", 1);
  if (from_person)
    fprintf (logfile, "SENDER: %s\n", from_person);
  fprintf (logfile, "NRCPT: %d\n", msg.nrcpt);
  for (c = 0; c < msg.nrcpt; c++)
    fprintf (logfile, "RCPT[%d]: %s\n", c, msg.rcpt[c]);
  fprintf (logfile, "LENGTH: %lu\n", (unsigned long)msg.buf_len);
  fwrite (msg.buf_ptr, msg.buf_len, 1, logfile);
  fputc ('\n', logfile);
  fclose (logfile);
  exit (EX_OK);
}

Return to:

Send suggestions and report system problems to the System administrator.