Coverage Report

Created: 2026-10-31 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib-mail/message-part-data.c
Line
Count
Source
1
/* Copyright (c) Dovecot authors, see top-level COPYING file */
2
3
#include "lib.h"
4
#include "str.h"
5
#include "wildcard-match.h"
6
#include "array.h"
7
#include "llist.h"
8
#include "rfc822-parser.h"
9
#include "rfc2231-parser.h"
10
#include "message-address.h"
11
#include "message-header-parser.h"
12
13
#include "message-part-data.h"
14
15
const char *message_part_envelope_headers[] = {
16
  "Date", "Subject", "From", "Sender", "Reply-To",
17
  "To", "Cc", "Bcc", "In-Reply-To", "Message-ID",
18
  NULL
19
};
20
21
/*
22
 *
23
 */
24
25
bool message_part_data_is_plain_7bit(const struct message_part *part)
26
0
{
27
0
  const struct message_part_data *data = part->data;
28
29
0
  i_assert(data != NULL);
30
0
  i_assert(part->parent == NULL);
31
32
  /* if content-type is text/xxx we don't have to check any
33
     multipart stuff */
34
0
  if ((part->flags & MESSAGE_PART_FLAG_TEXT) == 0)
35
0
    return FALSE;
36
0
  if (part->next != NULL || part->children != NULL)
37
0
    return FALSE; /* shouldn't happen normally.. */
38
39
  /* must be text/plain */
40
0
  if (data->content_subtype != NULL &&
41
0
      strcasecmp(data->content_subtype, "plain") != 0)
42
0
    return FALSE;
43
44
  /* only allowed parameter is charset=us-ascii, which is also default */
45
0
  if (data->content_type_params_count == 0) {
46
    /* charset defaults to us-ascii */
47
0
  } else if (data->content_type_params_count != 1 ||
48
0
       strcasecmp(data->content_type_params[0].name, "charset") != 0 ||
49
0
       strcasecmp(data->content_type_params[0].value,
50
0
            MESSAGE_PART_DEFAULT_CHARSET) != 0)
51
0
    return FALSE;
52
53
0
  if (data->content_id != NULL ||
54
0
      data->content_description != NULL)
55
0
    return FALSE;
56
57
0
  if (data->content_transfer_encoding != NULL &&
58
0
      strcasecmp(data->content_transfer_encoding, "7bit") != 0)
59
0
    return FALSE;
60
61
  /* BODYSTRUCTURE checks: */
62
0
  if (data->content_md5 != NULL ||
63
0
      data->content_disposition != NULL ||
64
0
      data->content_language != NULL ||
65
0
      data->content_location != NULL)
66
0
    return FALSE;
67
68
0
  return TRUE;
69
0
}
70
71
/* Returns whether the parameter exists. If a value is requested through
72
   value_r, returns it there and return TRUE, otherwise sets *value_r to NULL
73
   and returns FALSE. */
74
static bool get_param_value(const char *parameter_name,
75
          const struct message_part_param *params_list,
76
          unsigned int params_list_count,
77
          const char **value_r)
78
0
{
79
0
  for (unsigned int i = 0; i < params_list_count; i++) {
80
0
    const struct message_part_param *param = &params_list[i];
81
82
0
    if (strcasecmp(param->name, parameter_name) == 0) {
83
0
      if (value_r != NULL) {
84
0
        *value_r = NULL;
85
0
        if (*param->value != '\0') {
86
0
          *value_r = param->value;
87
0
        }
88
0
        return *value_r != NULL;
89
0
      }
90
0
      return TRUE;
91
0
    }
92
0
  }
93
0
  return FALSE;
94
0
}
95
96
static bool message_part_data_find_attachment_filename(
97
  const struct message_part_data *data,
98
  bool default_return,
99
  const char **filename_r)
100
0
{
101
  /* The filename might be under Content-Disposition->filename or under
102
     Content-Type->name */
103
0
  if (get_param_value("filename", data->content_disposition_params,
104
0
          data->content_disposition_params_count, filename_r) ||
105
0
      get_param_value("name", data->content_type_params,
106
0
          data->content_type_params_count, filename_r))
107
0
    return TRUE;
108
0
  if (default_return && filename_r != NULL) {
109
    /* This is an attachment without filename or name parameter.
110
       Just return the filename as empty string, so the caller
111
       doesn't have to handle NULL filename separately. */
112
0
    *filename_r = "";
113
0
  }
114
0
  return default_return;
115
0
}
116
117
/* Returns whether the message has an attachment in accordance with the provided
118
   settings. If there is an attachment, the filename is returned in filename_r
119
   if it is not NULL. */
120
static bool message_part_get_attachment_filename(
121
  const struct message_part *part,
122
  const struct message_part_attachment_settings *settings,
123
  const char **filename_r)
124
0
{
125
0
  const struct message_part_data *data = part->data;
126
127
0
  i_assert(data != NULL);
128
129
  /* Reject the message if the Content-Type is excluded. */
130
0
  if (settings != NULL && settings->content_type_filter != NULL &&
131
0
      !message_part_has_content_types(part, settings->content_type_filter))
132
0
    return FALSE;
133
134
  /* Accept Content-Disposition: attachment, even when it does not have a
135
     filename or Content-Type->name parameter */
136
0
  if (null_strcasecmp(data->content_disposition, "attachment") == 0)
137
0
    return message_part_data_find_attachment_filename(data, TRUE, filename_r);
138
139
  /* Accept Content-Disposition: inline only when a filename or
140
     Content-Type->name is available and if inlined attachments are not
141
     excluded */
142
0
  if ((settings == NULL || !settings->exclude_inlined) &&
143
0
      null_strcasecmp(data->content_disposition, "inline") == 0)
144
0
    return message_part_data_find_attachment_filename(data, FALSE, filename_r);
145
146
0
  return FALSE;
147
0
}
148
149
bool message_part_data_get_filename(const struct message_part *part,
150
  const char **filename_r)
151
0
{
152
0
  const struct message_part_data *data = part->data;
153
154
0
  i_assert(data != NULL);
155
156
0
  return message_part_get_attachment_filename(part, NULL, filename_r);
157
0
}
158
159
/*
160
 * Header parsing
161
 */
162
163
/* Message part envelope */
164
165
enum envelope_field {
166
  ENVELOPE_FIELD_DATE = 0,
167
  ENVELOPE_FIELD_SUBJECT,
168
  ENVELOPE_FIELD_FROM,
169
  ENVELOPE_FIELD_SENDER,
170
  ENVELOPE_FIELD_REPLY_TO,
171
  ENVELOPE_FIELD_TO,
172
  ENVELOPE_FIELD_CC,
173
  ENVELOPE_FIELD_BCC,
174
  ENVELOPE_FIELD_IN_REPLY_TO,
175
  ENVELOPE_FIELD_MESSAGE_ID,
176
177
  ENVELOPE_FIELD_UNKNOWN
178
};
179
180
static enum envelope_field
181
envelope_get_field(const char *name)
182
0
{
183
0
  switch (*name) {
184
0
  case 'B':
185
0
  case 'b':
186
0
    if (strcasecmp(name, "Bcc") == 0)
187
0
      return ENVELOPE_FIELD_BCC;
188
0
    break;
189
0
  case 'C':
190
0
  case 'c':
191
0
    if (strcasecmp(name, "Cc") == 0)
192
0
      return ENVELOPE_FIELD_CC;
193
0
    break;
194
0
  case 'D':
195
0
  case 'd':
196
0
    if (strcasecmp(name, "Date") == 0)
197
0
      return ENVELOPE_FIELD_DATE;
198
0
    break;
199
0
  case 'F':
200
0
  case 'f':
201
0
    if (strcasecmp(name, "From") == 0)
202
0
      return ENVELOPE_FIELD_FROM;
203
0
    break;
204
0
  case 'I':
205
0
  case 'i':
206
0
    if (strcasecmp(name, "In-reply-to") == 0)
207
0
      return ENVELOPE_FIELD_IN_REPLY_TO;
208
0
    break;
209
0
  case 'M':
210
0
  case 'm':
211
0
    if (strcasecmp(name, "Message-id") == 0)
212
0
      return ENVELOPE_FIELD_MESSAGE_ID;
213
0
    break;
214
0
  case 'R':
215
0
  case 'r':
216
0
    if (strcasecmp(name, "Reply-to") == 0)
217
0
      return ENVELOPE_FIELD_REPLY_TO;
218
0
    break;
219
0
  case 'S':
220
0
  case 's':
221
0
    if (strcasecmp(name, "Subject") == 0)
222
0
      return ENVELOPE_FIELD_SUBJECT;
223
0
    if (strcasecmp(name, "Sender") == 0)
224
0
      return ENVELOPE_FIELD_SENDER;
225
0
    break;
226
0
  case 'T':
227
0
  case 't':
228
0
    if (strcasecmp(name, "To") == 0)
229
0
      return ENVELOPE_FIELD_TO;
230
0
    break;
231
0
  default:
232
0
    break;
233
0
  }
234
235
0
  return ENVELOPE_FIELD_UNKNOWN;
236
0
}
237
238
void message_part_envelope_parse_from_header(pool_t pool,
239
  struct message_part_envelope **data,
240
  struct message_part_data_limits *limits,
241
  struct message_header_line *hdr)
242
0
{
243
0
  struct message_part_envelope *d;
244
0
  enum envelope_field field;
245
0
  struct message_address_list *addr_p, new_addr;
246
0
  const char **str_p;
247
248
0
  if (*data == NULL) {
249
0
    *data = p_new(pool, struct message_part_envelope, 1);
250
0
  }
251
252
0
  if (hdr == NULL)
253
0
    return;
254
0
  field = envelope_get_field(hdr->name);
255
0
  if (field == ENVELOPE_FIELD_UNKNOWN)
256
0
    return;
257
258
0
  if (hdr->continues) {
259
    /* wait for full value */
260
0
    hdr->use_full_value = TRUE;
261
0
    return;
262
0
  }
263
264
0
  d = *data;
265
0
  addr_p = NULL; str_p = NULL;
266
0
  switch (field) {
267
0
  case ENVELOPE_FIELD_DATE:
268
0
    str_p = &d->date;
269
0
    break;
270
0
  case ENVELOPE_FIELD_SUBJECT:
271
0
    str_p = &d->subject;
272
0
    break;
273
0
  case ENVELOPE_FIELD_MESSAGE_ID:
274
0
    str_p = &d->message_id;
275
0
    break;
276
0
  case ENVELOPE_FIELD_IN_REPLY_TO:
277
0
    str_p = &d->in_reply_to;
278
0
    break;
279
280
0
  case ENVELOPE_FIELD_CC:
281
0
    addr_p = &d->cc;
282
0
    break;
283
0
  case ENVELOPE_FIELD_BCC:
284
0
    addr_p = &d->bcc;
285
0
    break;
286
0
  case ENVELOPE_FIELD_FROM:
287
0
    addr_p = &d->from;
288
0
    break;
289
0
  case ENVELOPE_FIELD_SENDER:
290
0
    addr_p = &d->sender;
291
0
    break;
292
0
  case ENVELOPE_FIELD_TO:
293
0
    addr_p = &d->to;
294
0
    break;
295
0
  case ENVELOPE_FIELD_REPLY_TO:
296
0
    addr_p = &d->reply_to;
297
0
    break;
298
0
  case ENVELOPE_FIELD_UNKNOWN:
299
0
    i_unreached();
300
0
  }
301
302
0
  if (addr_p != NULL) {
303
0
    message_address_parse_full(pool, hdr->full_value,
304
0
             hdr->full_value_len,
305
0
             limits->remaining_addresses,
306
0
             MESSAGE_ADDRESS_PARSE_FLAG_FILL_MISSING,
307
0
             &new_addr);
308
0
    i_assert(new_addr.count <= limits->remaining_addresses);
309
0
    limits->remaining_addresses -= new_addr.count;
310
311
    /* Merge multiple headers the same as if they were comma
312
       separated in a single line. This is better from security
313
       point of view, because attacker could intentionally write
314
       addresses in a way that e.g. the first From header is
315
       validated while MUA only shows the second From header. */
316
0
    DLLIST2_JOIN(&addr_p->head, &addr_p->tail,
317
0
           &new_addr.head, &new_addr.tail);
318
0
    addr_p->count += new_addr.count;
319
0
  } else if (str_p != NULL) {
320
0
    *str_p = message_header_strdup(pool, hdr->full_value,
321
0
                 hdr->full_value_len);
322
0
  }
323
0
}
324
325
/* Message part data */
326
327
static void
328
parse_mime_parameters(struct rfc822_parser_context *parser,
329
  pool_t pool, const struct message_part_param **params_r,
330
  unsigned int *params_count_r,
331
  struct message_part_data_limits *limits)
332
0
{
333
0
  const char *const *results;
334
0
  struct message_part_param *params;
335
0
  unsigned int params_count, i;
336
337
0
  rfc2231_parse(parser, &results);
338
339
0
  params_count = str_array_length(results);
340
0
  i_assert((params_count % 2) == 0);
341
0
  params_count /= 2;
342
343
0
  if (params_count > limits->remaining_mime_params)
344
0
    params_count = limits->remaining_mime_params;
345
0
  limits->remaining_mime_params -= params_count;
346
347
0
  if (params_count > 0) {
348
0
    params = p_new(pool, struct message_part_param, params_count);
349
0
    for (i = 0; i < params_count; i++) {
350
0
      params[i].name = p_strdup(pool, results[i*2+0]);
351
0
      params[i].value = p_strdup(pool, results[i*2+1]);
352
0
    }
353
0
    *params_r = params;
354
0
  }
355
356
0
  *params_count_r = params_count;
357
0
}
358
359
static void
360
parse_content_type(struct message_part_data *data,
361
  pool_t pool, struct message_header_line *hdr,
362
  struct message_part_data_limits *limits)
363
0
{
364
0
  struct rfc822_parser_context parser;
365
0
  string_t *str;
366
0
  const char *value;
367
0
  unsigned int i;
368
0
  int ret;
369
370
0
  rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
371
0
  rfc822_skip_lwsp(&parser);
372
373
0
  str = t_str_new(256);
374
0
  ret = rfc822_parse_content_type(&parser, str);
375
376
  /* Save content type and subtype */
377
0
  value = str_c(str);
378
0
  for (i = 0; value[i] != '\0'; i++) {
379
0
    if (value[i] == '/') {
380
0
      data->content_subtype = p_strdup(pool, value + i+1);
381
0
      break;
382
0
    }
383
0
  }
384
0
  str_truncate(str, i);
385
0
  data->content_type = p_strdup(pool, str_c(str));
386
0
  if (data->content_subtype == NULL) {
387
    /* The Content-Type is invalid. Don't leave it NULL so that
388
       callers can assume that if content_type != NULL,
389
       content_subtype != NULL also. */
390
0
    data->content_subtype = p_strdup(pool, "");
391
0
  }
392
393
0
  if (ret < 0) {
394
    /* Content-Type is broken, but we wanted to get it as well as
395
       we could. Don't try to read the parameters anymore though.
396
397
       We don't completely ignore a broken Content-Type, because
398
       then it would be written as text/plain. This would cause a
399
       mismatch with the message_part's MESSAGE_PART_FLAG_TEXT. */
400
0
    return;
401
0
  }
402
403
0
  parse_mime_parameters(&parser, pool,
404
0
    &data->content_type_params,
405
0
    &data->content_type_params_count, limits);
406
0
  rfc822_parser_deinit(&parser);
407
0
}
408
409
static void
410
parse_content_transfer_encoding(struct message_part_data *data,
411
  pool_t pool, struct message_header_line *hdr)
412
0
{
413
0
  struct rfc822_parser_context parser;
414
0
  string_t *str;
415
416
0
  rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
417
0
  rfc822_skip_lwsp(&parser);
418
419
0
  str = t_str_new(256);
420
0
  if (rfc822_parse_mime_token(&parser, str) >= 0 &&
421
0
      rfc822_skip_lwsp(&parser) == 0 && str_len(str) > 0) {
422
0
    data->content_transfer_encoding =
423
0
      p_strdup(pool, str_c(str));
424
0
  }
425
0
  rfc822_parser_deinit(&parser);
426
0
}
427
428
static void
429
parse_content_disposition(struct message_part_data *data,
430
  pool_t pool, struct message_header_line *hdr,
431
  struct message_part_data_limits *limits)
432
0
{
433
0
  struct rfc822_parser_context parser;
434
0
  string_t *str;
435
436
0
  rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
437
0
  rfc822_skip_lwsp(&parser);
438
439
0
  str = t_str_new(256);
440
0
  if (rfc822_parse_mime_token(&parser, str) < 0) {
441
0
    rfc822_parser_deinit(&parser);
442
0
    return;
443
0
  }
444
0
  data->content_disposition = p_strdup(pool, str_c(str));
445
446
0
  parse_mime_parameters(&parser, pool,
447
0
    &data->content_disposition_params,
448
0
    &data->content_disposition_params_count, limits);
449
0
  rfc822_parser_deinit(&parser);
450
0
}
451
452
static void
453
parse_content_language(struct message_part_data *data,
454
  pool_t pool, const unsigned char *value, size_t value_len,
455
  struct message_part_data_limits *limits)
456
0
{
457
0
  struct rfc822_parser_context parser;
458
0
  ARRAY_TYPE(const_string) langs;
459
0
  string_t *str;
460
461
  /* Language-Header = "Content-Language" ":" 1#Language-tag
462
     Language-Tag = Primary-tag *( "-" Subtag )
463
     Primary-tag = 1*8ALPHA
464
     Subtag = 1*8ALPHA */
465
466
0
  rfc822_parser_init(&parser, value, value_len, NULL);
467
468
0
  t_array_init(&langs, 16);
469
0
  str = t_str_new(128);
470
471
0
  rfc822_skip_lwsp(&parser);
472
0
  while (rfc822_parse_atom(&parser, str) >= 0) {
473
0
    if (limits->remaining_language_tags == 0)
474
0
      break;
475
0
    limits->remaining_language_tags--;
476
477
0
    const char *lang = p_strdup(pool, str_c(str));
478
0
    array_push_back(&langs, &lang);
479
0
    str_truncate(str, 0);
480
481
0
    if (parser.data >= parser.end || *parser.data != ',')
482
0
      break;
483
0
    parser.data++;
484
0
    rfc822_skip_lwsp(&parser);
485
0
  }
486
0
  rfc822_parser_deinit(&parser);
487
488
0
  if (array_count(&langs) > 0) {
489
0
    array_append_zero(&langs);
490
0
    data->content_language =
491
0
      p_strarray_dup(pool, array_front(&langs));
492
0
  }
493
0
}
494
495
static void
496
parse_content_header(struct message_part_data *data,
497
  pool_t pool, struct message_header_line *hdr,
498
  struct message_part_data_limits *limits)
499
0
{
500
0
  const char *name = hdr->name + strlen("Content-");
501
502
0
  if (hdr->continues) {
503
0
    hdr->use_full_value = TRUE;
504
0
    return;
505
0
  }
506
507
0
  switch (*name) {
508
0
  case 'i':
509
0
  case 'I':
510
0
    if (strcasecmp(name, "ID") == 0 && data->content_id == NULL)
511
0
      data->content_id =
512
0
        message_header_strdup(pool, hdr->full_value,
513
0
                  hdr->full_value_len);
514
0
    break;
515
516
0
  case 'm':
517
0
  case 'M':
518
0
    if (strcasecmp(name, "MD5") == 0 && data->content_md5 == NULL)
519
0
      data->content_md5 =
520
0
        message_header_strdup(pool, hdr->full_value,
521
0
                  hdr->full_value_len);
522
0
    break;
523
524
0
  case 't':
525
0
  case 'T':
526
0
    if (strcasecmp(name, "Type") == 0 && data->content_type == NULL)
527
0
      parse_content_type(data, pool, hdr, limits);
528
0
    else if (strcasecmp(name, "Transfer-Encoding") == 0 &&
529
0
       data->content_transfer_encoding == NULL)
530
0
      parse_content_transfer_encoding(data, pool, hdr);
531
0
    break;
532
533
0
  case 'l':
534
0
  case 'L':
535
0
    if (strcasecmp(name, "Language") == 0 &&
536
0
        data->content_language == NULL) {
537
0
      parse_content_language(data, pool,
538
0
        hdr->full_value, hdr->full_value_len, limits);
539
0
    } else if (strcasecmp(name, "Location") == 0 &&
540
0
         data->content_location == NULL) {
541
0
      data->content_location =
542
0
        message_header_strdup(pool, hdr->full_value,
543
0
                  hdr->full_value_len);
544
0
    }
545
0
    break;
546
547
0
  case 'd':
548
0
  case 'D':
549
0
    if (strcasecmp(name, "Description") == 0 &&
550
0
        data->content_description == NULL)
551
0
      data->content_description =
552
0
        message_header_strdup(pool, hdr->full_value,
553
0
                  hdr->full_value_len);
554
0
    else if (strcasecmp(name, "Disposition") == 0 &&
555
0
       data->content_disposition_params == NULL)
556
0
      parse_content_disposition(data, pool, hdr, limits);
557
0
    break;
558
0
  default:
559
0
    break;
560
0
  }
561
0
}
562
563
void message_part_data_parse_from_header(pool_t pool,
564
  struct message_part *part,
565
  struct message_part_data_limits *limits,
566
  struct message_header_line *hdr)
567
0
{
568
0
  struct message_part_data *part_data;
569
0
  struct message_part_envelope *envelope;
570
0
  bool parent_rfc822;
571
572
0
  if (hdr == NULL) {
573
0
    if (part->data == NULL) {
574
      /* no Content-* headers. add an empty context
575
         structure anyway. */
576
0
      part->data = p_new(pool, struct message_part_data, 1);
577
0
    } else if ((part->flags & MESSAGE_PART_FLAG_IS_MIME) == 0) {
578
      /* If there was no Mime-Version, forget all
579
         the Content-stuff */
580
0
      part_data = part->data;
581
0
      envelope = part_data->envelope;
582
583
0
      i_zero(part_data);
584
0
      part_data->envelope = envelope;
585
0
    }
586
0
    return;
587
0
  }
588
589
0
  if (hdr->eoh)
590
0
    return;
591
592
0
  parent_rfc822 = part->parent != NULL &&
593
0
    (part->parent->flags & MESSAGE_PART_FLAG_MESSAGE_RFC822) != 0;
594
0
  if (!parent_rfc822 && !str_begins_icase_with(hdr->name, "Content-"))
595
0
    return;
596
597
0
  if (part->data == NULL) {
598
    /* initialize message part data */
599
0
    part->data = p_new(pool, struct message_part_data, 1);
600
0
  }
601
0
  part_data = part->data;
602
603
0
  if (str_begins_icase_with(hdr->name, "Content-")) {
604
0
    T_BEGIN {
605
0
      parse_content_header(part_data, pool, hdr, limits);
606
0
    } T_END;
607
0
  }
608
609
0
  if (parent_rfc822) {
610
    /* message/rfc822, we need the envelope */
611
0
    message_part_envelope_parse_from_header(pool, &part_data->envelope,
612
0
              limits, hdr);
613
0
  }
614
0
}
615
616
bool message_part_has_content_types(const struct message_part *part,
617
            const char *const *types)
618
0
{
619
0
  struct message_part_data *data = part->data;
620
0
  bool ret = TRUE;
621
0
  const char *const *ptr;
622
0
  const char *content_type;
623
624
0
  i_assert(data != NULL);
625
626
0
  if (data->content_type == NULL)
627
0
    return FALSE;
628
0
  else if (data->content_subtype == NULL)
629
0
    content_type = t_strdup_printf("%s/", data->content_type);
630
0
  else
631
0
    content_type = t_strdup_printf("%s/%s", data->content_type,
632
0
              data->content_subtype);
633
0
  for(ptr = types; *ptr != NULL; ptr++) {
634
0
    bool exclude = (**ptr == '!');
635
0
    if (wildcard_match_icase(content_type, (*ptr)+(exclude?1:0)))
636
0
      ret = !exclude;
637
0
  }
638
639
0
  return ret;
640
0
}
641
642
bool message_part_is_attachment(
643
  const struct message_part *part,
644
  const struct message_part_attachment_settings *settings)
645
0
{
646
0
  return message_part_get_attachment_filename(part, settings, NULL);
647
0
}