File: message.c

package info (click to toggle)
emil 2.1.0-beta9-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,160 kB
  • ctags: 587
  • sloc: ansic: 10,358; yacc: 412; makefile: 329; sh: 182
file content (487 lines) | stat: -rw-r--r-- 10,860 bytes parent folder | download | duplicates (5)
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
/*
** 
** Copyright (C) 1993 Swedish University Network (SUNET)
** 
** 
** This program is developed by UDAC, Uppsala University by commission
** of the Swedish University Network (SUNET). 
** 
** 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; either version 2 of the License, 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 FITTNESS 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 this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
** 
** 
**                                           Martin.Wendel@its.uu.se
** 				             Torbjorn.Wictorin@its.uu.se
** 
**                                           ITS	
**                                           P.O. Box 887
**                                           S-751 08 Uppsala
**                                           Sweden
** 
*/
#include "emil.h"

int do_sibling = FALSE;

/*
 * int
 * parse_message(inbuf, message)
 *
 * Parse message and divide into a hierarchical structure of message parts.
 *
 */

int
parse_message(struct message *m)
{
  struct data *inbuf;

  /* Initialize inbuf */
  inbuf = (struct data *)m->sd;

  /* Exit on empty input */
  if (inbuf->size == 0 || (inbuf->end <= inbuf->offset))
    {
#ifdef DEBUG
      if (edebug)
	fprintf(stderr, "*** Empty input (failed).\n");
#endif
      logger(LOG_ERR, "parse_message: Empty input");
      return(NOK);
    }
  /* Check header if possible */
  if (!m->headerdone && (m->sd->format != RFC822 || m->level == 0))
      if (check_header(m) != OK)
	return(NOK);
      
  switch(m->sd->format)
    {
    case MIME:
      return(parse_mime_message(m));
      break;
    case MAILTOOL:
      return(parse_sun_message(m));
      break;
    case RFC822:
      return(parse_rfc822_message(m));
      break;
    default:
      break;
    }

  return(NOK);
}


int
move_past_boundary(struct message *m, char *boundary)
{
  int linelen;
  struct data *inbuf;
  inbuf = (struct data *)m->sd;
  /* Find start boundary */
  while (is_bound(inbuf, boundary) != TRUE)
    {
      if ((linelen = getline(inbuf)) == 0)
	{
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "*   move_past_boundary failed, EOF.\n");
#endif
	return(NOK);
	}
      inbuf->offset += linelen;
      if (inbuf->end <= inbuf->offset)
	{
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "move_past_boundary: cannot find boundary\n");
#endif
	  sprintf(ebuf, "move_past_boundary: cannot find boundary");
	  logger(LOG_DEBUG, ebuf);
	  return(NOK);
	}
      inbuf->loffset += 1;
    }
  /* Move past boundary */
  inbuf->offset += getline(inbuf);
  inbuf->loffset += 1;
#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "+   Found  boundary, moved to %lu\n", inbuf->offset);
#endif
  return(OK);
}

int
check_header(struct message *m)
{
  m->headerdone = 1;
  if (load_header(m) == OK)
    /* Offset is updated with size of header! */
    {
      /* Decode the loaded header */
      decode_header(m);

      /* Set body lines, if available 
       * (body length is not trusted)
       */
      if (m->sd->bodylines != 0)
	m->sd->lineend = m->sd->loffset + m->sd->bodylines;
    }
  else
    {
      /* Load header failed */
      sprintf(ebuf, "check_header: load header failed: %lu", m->sd->loffset);
#ifdef DEBUG
      if (edebug)
	fprintf(stderr, "%s\n", ebuf);
#endif
      logger(LOG_DEBUG, ebuf);
      return(NOK);
    }
  return(OK);
}

int
check_encoding(struct message *m)
{
  struct data *inbuf;
  int status;

  inbuf = m->sd;

  /* Check for encoding */
  switch (inbuf->encoding)
    {
    case 0:
      inbuf->encoding = E7BIT;
      status = OK;
      break;
    case EBINHEX:
      if (decode_binhex(m) == NOK)
	{
	  sprintf(ebuf, "WARNING: parse_message: BinHex decode failed :%lu", 
		  inbuf->loffset);
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "%s\n", ebuf);
#endif
	  logger(LOG_WARNING, ebuf);
	  status = NOK;
	}
      else
	status = OK;

      break;
    case EUUENCODE:
      if (decode_uuencode(m) == NOK)
	{
	  sprintf(ebuf, "WARNING: parse_message: UUencode decode failed :%lu", 
		  inbuf->loffset);
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "%s\n", ebuf);
#endif
	  logger(LOG_WARNING, ebuf);
	  status = NOK;
	}
      else
	status = OK;
      
      break;
    case EBASE64:
      if (decode_base64(m) == NOK)
	{
	  sprintf(ebuf, "WARNING: parse_message: Base64 decode failed :%lu", 
		  inbuf->loffset);
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "%s\n", ebuf);
#endif
	  logger(LOG_WARNING, ebuf);
	  status = NOK;
	}
      else
	status = OK;
      if (inbuf->bodyend == 0)
	inbuf->bodyend = inbuf->end;
      break;
    case EQP:
      if (decode_quoted_printable(m) == NOK)
	{
	  sprintf(ebuf, "WARNING: parse_message: Quoted-Printable decode failed :%lu", 
		  inbuf->loffset);
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "%s\n", ebuf);
#endif
	  logger(LOG_WARNING, ebuf);
	  status = NOK;
	}
      else
	status = OK;
      if (inbuf->bodyend == 0)
	inbuf->bodyend = inbuf->end;
      break;
    default:
      status = OK;
      break;
    }
  return(status);
}
int
boundary_check(struct message *m)
{
  struct message *parent;
  int encoding;
  int check;
  
  encoding = m->sd->encoding;
  check = m->sd->check;
  parent = m->parent;
#ifdef DEBUG
  if (edebug)
  fprintf(stderr, "checking part %d with check %d and encoding %d\nat offset %lu\n",
	  m->sd->count, check, encoding, m->sd->offset);
#endif
  /* Check a multipart, look for start boundary */
  if (parent != NULL && parent->sd->startbound != NULL && 
      (strlen(parent->sd->startbound)) != 0)
    {
      if (is_bound(m->sd, parent->sd->startbound))
	{
	  return(E7BIT); /* Just a precaution */
	}
    }      

  /* Check for uuencode */
  if (check & EUUENCODE)
    {
      if (encoding & EUUENCODE)
	return(0);
    /* Check for binhex and uuencode */
      if (strncmp((m->sd->contents + m->sd->offset), "begin ", 6) == 0)
	{
	  return(EUUENCODE);
	}
    }
  /* Check for BinHex */
  if (check & EBINHEX)
    {
      if (encoding & EBINHEX)
	return(0);
    /* Check for BinHex */
      if (strncasecmp((m->sd->contents + m->sd->offset), "(This file ", 11) == 0)
	{
	  return(EBINHEX);
	}
    }
  if (encoding == 0)
    return(E7BIT);
  return(0);
}


/*
 *
 */
int
is_bound(struct data *d, char *boundary)
{
  int blen;
  if (boundary != NULL && 
      (blen = strlen(boundary)) != 0)
    {
	/*
	printf("is_bound: '%20.20s'\n          '%20.20'\n",
		d->contents+d->offset,
		boundary);
	*/
      
      if (d->offset + blen < d->end &&
	  strncmp((d->contents + d->offset), boundary, blen) == 0)
	{
	  return(TRUE);
	}
    }      
  return(FALSE);
}


/*
 *
 */
struct message *
copy_mstruct(struct message *m, int force)
{
  struct data *cbuf, *inbuf;
  struct message *cm;
#ifdef DEBUG
  if (edebug)
  fprintf(stderr, "+ Copying structure\n");
#endif
  inbuf = m->sd;

  if (force < 2)
    {
#ifdef DEBUG
      if (edebug)
	fprintf(stderr, "+   Creating new data structure\n");
#endif
      /* Copy data structure */
      cbuf = (struct data *) Yalloc(sizeof(struct data));
      cbuf->contents = inbuf->contents; /* data */
      cbuf->offset = inbuf->offset;     /* and offset */
      cbuf->check = inbuf->check;
      cbuf->size = inbuf->size;
      cbuf->count = inbuf->count + 1;
      cbuf->loffset = inbuf->loffset;
      cbuf->charset = inbuf->charset;
      cbuf->format = inbuf->format;
    }
  else
    cbuf = m->sd;


  /* Copy message structure */
  if (force < 1 && m->sd->format == RFC822)
    {
#ifdef DEBUG
      if (edebug)
      fprintf(stderr, "+   Living with old message structure\n");
#endif
      m->sd->next = cbuf;
      m->sd = m->sd->next;
      m->td = m->sd;
#ifdef DEBUG
      if (edebug)
      fprintf(stderr, "Reached offset %lu\n", m->sd->offset);
#endif
      return(m);
    }
  else
    {
#ifdef DEBUG
      if (edebug)
      fprintf(stderr, "+   Copying message structure\n");
#endif
      cm = (struct message *) Yalloc(sizeof(struct message));
      cm->sdl = cbuf;
      cm->sd = cm->sdl;
      cm->td = cbuf;
      return(cm);
    }
}


/*
 * int
 * getline(inbuf)
 *
 * Move pointer to next line of inbuf. Return line length.
 */

int
getline(struct data *inbuf)
{
  unsigned char *tmp;
  if (inbuf == NULL || inbuf->contents == NULL ||
      inbuf->offset >= inbuf->end)
    return(0);
  tmp = index(inbuf->contents + inbuf->offset, '\n');
  if (tmp == NULL || (tmp - inbuf->contents) >= inbuf->end)
    return(inbuf->end - inbuf->offset);
  else
    return(tmp - inbuf->contents + 1 - inbuf->offset);
}


int
set_end_by_boundary(struct message *m, char *boundary)
{
  int linelen, offset, loffset;
  struct data *inbuf;
  inbuf = (struct data *)m->sd;

#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "*  set_end_by_boundary\n");
#endif
  offset = inbuf->offset;
  loffset = inbuf->loffset;
  while (is_bound(inbuf, boundary) != TRUE)
    {
      if ((linelen = getline(inbuf)) == 0)
	{
	  if (m->parent)
	    inbuf->bodyend = m->parent->sd->bodyend;
	  else
	    inbuf->bodyend = inbuf->offset;
	  inbuf->lineend = inbuf->loffset;
	  inbuf->offset = offset;
	  inbuf->loffset = loffset;
	  sprintf(ebuf, "set_end_by_boundary: cannot find boundary, setting end to %lu\n", inbuf->bodyend);
	  logger(LOG_DEBUG, ebuf);
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, "set_end_by_boundary: cannot find boundary, setting end to %lu\n", inbuf->bodyend);
#endif
	  return(NOK);
	}
      inbuf->offset += linelen;
      inbuf->loffset += 1;
    }
  /* Set bodyend to just before the end boundary */
  inbuf->bodyend = inbuf->offset;
  inbuf->lineend = inbuf->loffset;
  if (m->sd->format == MIME)
    inbuf->bodyend -= 1;
  inbuf->offset = offset;
  inbuf->loffset = loffset;
#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "+   Found boundary, setting end at %lu.\n", inbuf->bodyend);
#endif
  return(OK);
}

int
set_end_by_lines(struct message *m, int lines)
{
  int linelen, offset, loffset, lineend;
  struct data *inbuf;
  inbuf = (struct data *)m->sd;

#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "*  set_end_by_lines\n");
#endif
  offset = inbuf->offset;
  loffset = inbuf->loffset;
  lineend = loffset + lines;
  while ((linelen = getline(inbuf)) != 0 && lineend > inbuf->loffset)
	{
	  inbuf->offset += linelen;
	  inbuf->loffset += 1;
	}
  
  /* Set bodyend to just before the end boundary */
  inbuf->bodyend = inbuf->offset;
  inbuf->lineend = inbuf->loffset;
  inbuf->offset = offset;
  inbuf->loffset = loffset;
  return(OK);
}