File: headers.c

package info (click to toggle)
libesmtp 1.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,156 kB
  • ctags: 1,220
  • sloc: sh: 10,217; ansic: 8,355; xml: 2,007; makefile: 145
file content (909 lines) | stat: -rw-r--r-- 25,256 bytes parent folder | download | duplicates (4)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
 *  This file is part of libESMTP, a library for submission of RFC 2822
 *  formatted electronic mail messages using the SMTP protocol described
 *  in RFC 2821.
 *
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#ifdef HAVE_GETTIMEOFDAY
# include <sys/time.h>
#endif
#include <errno.h>

#include <missing.h>

#include "libesmtp-private.h"
#include "headers.h"
#include "htable.h"
#include "rfc2822date.h"
#include "api.h"

struct rfc2822_header
  {
    struct rfc2822_header *next;
    struct header_info *info;		/* Info for setting and printing */
    char *header;			/* Header name */
    void *value;			/* Header value */
  };

typedef int (*hdrset_t) (struct rfc2822_header *, va_list);
typedef void (*hdrprint_t) (smtp_message_t, struct rfc2822_header *);
typedef void (*hdrdestroy_t) (struct rfc2822_header *);

struct header_actions
  {
    const char *name;		/* Header for which the action is specified */
    unsigned int flags;		/* Header flags */
    hdrset_t set;		/* Function to set header value from API */
    hdrprint_t print;		/* Function to print the header value */
    hdrdestroy_t destroy;	/* Function to destroy the header value */
  };

struct header_info
  {
    const struct header_actions *action;
    struct rfc2822_header *hdr;	/* Pointer to most recently defined header */
    unsigned int seen : 1;	/* Header has been seen in the message */
    unsigned int override : 1;	/* LibESMTP is overriding the message */
    unsigned int prohibit : 1;	/* Header may not appear in the message */
  };

#define NELT(x)		((int) (sizeof x / sizeof x[0]))

#define OPTIONAL	0
#define SHOULD		1
#define REQUIRE		2
#define PROHIBIT	4
#define PRESERVE	8
#define LISTVALUE	16
#define MULTIPLE	32

static struct rfc2822_header *create_header (smtp_message_t message,
					    const char *header,
					    struct header_info *info);
void destroy_string (struct rfc2822_header *header);
void destroy_mbox_list (struct rfc2822_header *header);
struct header_info *find_header (smtp_message_t message,
				 const char *name, int len);
struct header_info *insert_header (smtp_message_t message, const char *name);

/* RFC 2822 headers processing */

/****************************************************************************
 * Functions for setting and printing header values
 ****************************************************************************/

static int
set_string (struct rfc2822_header *header, va_list alist)
{
  const char *value;

  assert (header != NULL);

  if (header->value != NULL)		/* Already set */
    return 0;

  value = va_arg (alist, const char *);
  if (value == NULL)
    return 0;
  header->value = strdup (value);
  return header->value != NULL;
}

static int
set_string_null (struct rfc2822_header *header, va_list alist)
{
  const char *value;

  assert (header != NULL);

  if (header->value != NULL)		/* Already set */
    return 0;

  value = va_arg (alist, const char *);
  if (value == NULL)
    return 1;
  header->value = strdup (value);
  return header->value != NULL;
}

/* Print header-name ": " header-value "\r\n" */
static void
print_string (smtp_message_t message, struct rfc2822_header *header)
{
  assert (message != NULL && header != NULL);

  /* TODO: implement line folding at white spaces */
  vconcatenate (&message->hdr_buffer, header->header, ": ",
                (header->value != NULL) ? header->value : "", "\r\n", NULL);
}

void
destroy_string (struct rfc2822_header *header)
{
  assert (header != NULL);

  if (header->value != NULL)
    free (header->value);
}

/* Print header-name ": <" message-id ">\r\n" */
static void
print_message_id (smtp_message_t message, struct rfc2822_header *header)
{
  const char *message_id;
  char buf[64];
#ifdef HAVE_GETTIMEOFDAY
  struct timeval tv;
#endif

  assert (message != NULL && header != NULL);

  message_id = header->value;
  if (message_id == NULL)
    {
#ifdef HAVE_GETTIMEOFDAY
      if (gettimeofday (&tv, NULL) != -1) /* This shouldn't fail ... */
	snprintf (buf, sizeof buf, "%ld.%ld.%d@%s", tv.tv_sec, tv.tv_usec,
		  getpid (), message->session->localhost);
      else /* ... but if it does fall back to using time() */
#endif
      snprintf (buf, sizeof buf, "%ld.%d@%s", time (NULL),
      	        getpid (), message->session->localhost);
      message_id = buf;
    }
  /* TODO: implement line folding at white spaces */
  vconcatenate (&message->hdr_buffer,
                header->header, ": <", message_id, ">\r\n",
                NULL);
}

/****/

static int
set_date (struct rfc2822_header *header, va_list alist)
{
  const time_t *value;

  assert (header != NULL);

  if ((time_t) header->value != (time_t) 0)		/* Already set */
    return 0;

  value = va_arg (alist, const time_t *);
  header->value = (void *) *value;
  return 1;
}

/* Print header-name ": " formatted-date "\r\n" */
static void
print_date (smtp_message_t message, struct rfc2822_header *header)
{
  char buf[64];
  time_t when;

  assert (message != NULL && header != NULL);

  when = (time_t) header->value;
  if (when == (time_t) 0)
    time (&when);
  vconcatenate (&message->hdr_buffer, header->header, ": ",
                rfc2822date (buf, sizeof buf, &when), "\r\n", NULL);
}

/****/

struct mbox
  {
    struct mbox *next;
    char *mailbox;
    char *phrase;
  };

void
destroy_mbox_list (struct rfc2822_header *header)
{
  struct mbox *mbox, *next;

  assert (header != NULL);

  mbox = header->value;
  while (mbox != NULL)
    {
      next = mbox->next;
      if (mbox->phrase != NULL)
        free ((void *) mbox->phrase);
      if (mbox->mailbox != NULL)
        free ((void *) mbox->mailbox);
      free (mbox);
      mbox = next;
    }
}

static int
set_from (struct rfc2822_header *header, va_list alist)
{
  struct mbox *mbox;
  const char *mailbox;
  const char *phrase;

  assert (header != NULL);

  phrase = va_arg (alist, const char *);
  mailbox = va_arg (alist, const char *);

  /* Allow this to succeed as a special case.  Effectively requesting
     default action in print_from().   Fails if explicit values have
     already been set.  */
  if (phrase == NULL && mailbox == NULL)
    return header->value == NULL;

  mbox = malloc (sizeof (struct mbox));
  if (mbox == NULL)
    return 0;
  mbox->phrase = (phrase != NULL) ? strdup (phrase) : NULL;
  mbox->mailbox = strdup (mailbox);

  mbox->next = header->value;
  header->value = mbox;
  return 1;
}

/* Print header-name ": " mailbox "\r\n" 
      or header-name ": \"" phrase "\" <" mailbox ">\r\n" */
static void
print_from (smtp_message_t message, struct rfc2822_header *header)
{
  struct mbox *mbox;
  const char *mailbox;

  assert (message != NULL && header != NULL);

  vconcatenate (&message->hdr_buffer, header->header, ": ", NULL);
  /* TODO: implement line folding at white spaces */
  if (header->value == NULL)
    {
      mailbox = message->reverse_path_mailbox;
      vconcatenate (&message->hdr_buffer,
		    (mailbox != NULL && *mailbox != '\0') ? mailbox : "<>",
		    "\r\n", NULL);
    }
  else
    for (mbox = header->value; mbox != NULL; mbox = mbox->next)
      {
	mailbox = mbox->mailbox;
	if (mbox->phrase == NULL)
	  vconcatenate (&message->hdr_buffer,
			(mailbox != NULL && *mailbox != '\0') ? mailbox : "<>",
			NULL);
	else
	  vconcatenate (&message->hdr_buffer, "\"", mbox->phrase, "\""
			" <", (mailbox != NULL) ? mailbox : "", ">", NULL);
	vconcatenate (&message->hdr_buffer,
		      (mbox->next != NULL) ? ",\r\n    " : "\r\n", NULL);
      }
}

/* Same arguments and syntax as from: except that only one value is
   allowed.  */
static int
set_sender (struct rfc2822_header *header, va_list alist)
{
  struct mbox *mbox;
  const char *mailbox;
  const char *phrase;

  assert (header != NULL);

  if (header->value != NULL)
    return 0;

  phrase = va_arg (alist, const char *);
  mailbox = va_arg (alist, const char *);
  if (phrase == NULL && mailbox == NULL)
    return 0;

  mbox = malloc (sizeof (struct mbox));
  if (mbox == NULL)
    return 0;
  mbox->phrase = (phrase != NULL) ? strdup (phrase) : NULL;
  mbox->mailbox = strdup (mailbox);
  mbox->next = NULL;

  header->value = mbox;
  return 1;
}

/* TODO: do nothing if the mailbox is NULL.  Check this doesn't fool
         the protocol engine into thinking it has seen end of file. */
/* Print header-name ": " mailbox "\r\n" 
      or header-name ": \"" phrase "\" <" mailbox ">\r\n"
 */
static void
print_sender (smtp_message_t message, struct rfc2822_header *header)
{
  struct mbox *mbox;
  const char *mailbox;

  assert (message != NULL && header != NULL);

  vconcatenate (&message->hdr_buffer, header->header, ": ", NULL);
  mbox = header->value;
  mailbox = mbox->mailbox;
  if (mbox->phrase == NULL)
    vconcatenate (&message->hdr_buffer,
		  (mailbox != NULL && *mailbox != '\0') ? mailbox : "<>",
		  "\r\n", NULL);
  else
    vconcatenate (&message->hdr_buffer, "\"", mbox->phrase, "\""
		  " <", (mailbox != NULL) ? mailbox : "", ">\r\n", NULL);
}

static int
set_to (struct rfc2822_header *header, va_list alist)
{
  struct mbox *mbox;
  const char *mailbox;
  const char *phrase;

  assert (header != NULL);

  phrase = va_arg (alist, const char *);
  mailbox = va_arg (alist, const char *);
  if (phrase == NULL && mailbox == NULL)
    mbox = NULL;
  else
    {
      mbox = malloc (sizeof (struct mbox));
      if (mbox == NULL)
	return 0;
      mbox->phrase = (phrase != NULL) ? strdup (phrase) : NULL;
      mbox->mailbox = strdup (mailbox);

      mbox->next = header->value;
    }
  header->value = mbox;
  return 1;
}

static int
set_cc (struct rfc2822_header *header, va_list alist)
{
  struct mbox *mbox;
  const char *mailbox;
  const char *phrase;

  assert (header != NULL);

  phrase = va_arg (alist, const char *);
  mailbox = va_arg (alist, const char *);
  if (mailbox == NULL)
    return 0;
  mbox = malloc (sizeof (struct mbox));
  if (mbox == NULL)
    return 0;
  mbox->phrase = (phrase != NULL) ? strdup (phrase) : NULL;
  mbox->mailbox = strdup (mailbox);

  mbox->next = header->value;
  header->value = mbox;
  return 1;
}

/* Print header-name ": " mailbox "\r\n" 
      or header-name ": \"" phrase "\" <" mailbox ">\r\n"
   ad nauseum. */
static void
print_cc (smtp_message_t message, struct rfc2822_header *header)
{
  struct mbox *mbox;

  assert (message != NULL && header != NULL);

  vconcatenate (&message->hdr_buffer, header->header, ": ", NULL);
  for (mbox = header->value; mbox != NULL; mbox = mbox->next)
    {
      if (mbox->phrase == NULL)
	vconcatenate (&message->hdr_buffer, mbox->mailbox, NULL);
      else
	vconcatenate (&message->hdr_buffer,
		      "\"", mbox->phrase, "\" <", mbox->mailbox, ">",
		      NULL);
      vconcatenate (&message->hdr_buffer,
		    (mbox->next != NULL) ? ",\r\n    " : "\r\n", NULL);
    }
}

/* As above but generate a default value from the recipient list.
 */
static void
print_to (smtp_message_t message, struct rfc2822_header *header)
{
  smtp_recipient_t recipient;

  assert (header != NULL);

  if (header->value != NULL)
    {
      print_cc (message, header);
      return;
    }

  /* TODO: implement line folding at white spaces */
  vconcatenate (&message->hdr_buffer, header->header, ": ", NULL);
  for (recipient = message->recipients;
       recipient != NULL;
       recipient = recipient->next)
    vconcatenate (&message->hdr_buffer, recipient->mailbox,
		  (recipient->next != NULL) ? ",\r\n    " : "\r\n",
		  NULL);
}


/* Header actions placed here to avoid the need for many akward forward
   declarations for set_xxx/print_xxx.  */

static const struct header_actions header_actions[] =
  {
    /* This is the default header info for a simple string value.
     */
    { NULL,		OPTIONAL,
      set_string,	print_string,		destroy_string},
    /* A number of headers should be present in every message
     */
    { "Date",		REQUIRE,
      set_date,		print_date, NULL, },
    { "From",		REQUIRE | LISTVALUE,
      set_from,		print_from,		destroy_mbox_list, },
    /* Certain headers are added when a message is delivered and
       should not be present in a message being posted or which
       is in transit.  If present in the message they will be stripped
       and if specified by the API, the relevant APIs will fail. */
    { "Return-Path",	PROHIBIT, NULL, NULL, NULL, },
    /* RFC 2298 - Delivering MTA may add the Original-Recipient: header
                  using DSN ORCPT parameter and may discard
                  Original-Recipient: headers present in the message.
                  No point in sending it then. */
    { "Original-Recipient", PROHIBIT, NULL, NULL, NULL, },
    /* MIME-*: and Content-*: are MIME headers and must not be generated
       or processed by libESMTP.  Similarly, Resent-*: and Received: must
       be retained unaltered. */
    { "Content-",	PRESERVE, NULL, NULL, NULL, },
    { "MIME-",		PRESERVE, NULL, NULL, NULL, },
    { "Resent-",	PRESERVE, NULL, NULL, NULL, },
    { "Received",	PRESERVE, NULL, NULL, NULL, },
    /* Headers which are optional but which are recommended to be
       present.  Default action is to provide a default unless the
       application explicitly requests not to. */
    { "Message-Id",	SHOULD,
      set_string_null,print_message_id,		destroy_string, },
    /* Remaining headers are known to libESMTP to simplify handling them
       for the application.   All other headers are reaated as simple
       string values. */
    { "Sender",		OPTIONAL,
      set_sender,	print_sender,		destroy_mbox_list, },
    { "To",		OPTIONAL | LISTVALUE,
      set_to,		print_to,		destroy_mbox_list, },
    { "Cc",		OPTIONAL | LISTVALUE,
      set_cc,		print_cc,		destroy_mbox_list, },
    { "Bcc",		OPTIONAL | LISTVALUE,
      set_cc,		print_cc,		destroy_mbox_list, },
    { "Reply-To",	OPTIONAL | LISTVALUE,
      set_cc,		print_cc,		destroy_mbox_list, },
    /* RFC 2298 - MDN request.  Syntax is the same as the From: header and
                  default when set to NULL is the same as From: */
    { "Disposition-Notification-To", OPTIONAL,
      set_from,		print_from,		destroy_mbox_list, },
    /* TODO:
       In-Reply-To:	*(phrase / msgid)
       References:	*(phrase / msgid)
       Keywords:	#phrase

       Handle Resent- versions of
       		To Cc Bcc Message-ID Date Reply-To From Sender
     */
  };

static int
init_header_table (smtp_message_t message)
{
  int i;
  struct header_info *hi;

  assert (message != NULL);

  if (message->hdr_action != NULL)
    return -1;

  message->hdr_action = h_create ();
  if (message->hdr_action == NULL)
    return 0;
  for (i = 0; i < NELT (header_actions); i++)
    if (header_actions[i].name != NULL)
      {
	hi = h_insert (message->hdr_action, header_actions[i].name, -1,
		       sizeof (struct header_info));
	if (hi == NULL)
	  return 0;
	hi->action = &header_actions[i];

	/* REQUIREd headers must be present in the message.  SHOULD
	   means the header is optional but its presence is recommended.
	   Create a NULL valued header.  This will either be set later
	   with the API, or the print_xxx function will handle the NULL
	   value as a special case, e.g, the To: header is generated
	   from the recipient_t list. */
	if (hi->action->flags & (REQUIRE | SHOULD))
	  {
	    if (create_header (message, header_actions[i].name, hi) == NULL)
	      return 0;
	  }
      }
  return 1;
}

void
destroy_header_table (smtp_message_t message)
{
  struct rfc2822_header *header, *next;

  assert (message != NULL);

  /* Take out the linked list */
  for (header = message->headers; header!= NULL; header = next)
    {
      next = header->next;
      if (header->info->action->destroy != NULL)
	(*header->info->action->destroy) (header);
      free (header->header);
      free (header);
    }

  /* Take out the hash table */
  if (message->hdr_action != NULL)
    {
      h_destroy (message->hdr_action, NULL, NULL);
      message->hdr_action = NULL;
    }

  message->headers = message->end_headers = NULL;
}

struct header_info *
find_header (smtp_message_t message, const char *name, int len)
{
  struct header_info *info;
  const char *p;

  assert (message != NULL && name != NULL);

  if (len < 0)
    len = strlen (name);
  if (len == 0)
    return NULL;
  info = h_search (message->hdr_action, name, len);
  if (info == NULL && (p = memchr (name, '-', len)) != NULL)
    info = h_search (message->hdr_action, name, p - name + 1);
  return info;
}

struct header_info *
insert_header (smtp_message_t message, const char *name)
{
  struct header_info *info;

  assert (message != NULL && name != NULL);

  info = h_insert (message->hdr_action, name, -1, sizeof (struct header_info));
  if (info == NULL)
    return NULL;
  info->action = &header_actions[0];
  return info;
}

static struct rfc2822_header *
create_header (smtp_message_t message, const char *header,
               struct header_info *info)
{
  struct rfc2822_header *hdr;

  assert (message != NULL && header != NULL && info != NULL);

  if ((hdr = malloc (sizeof (struct rfc2822_header))) == NULL)
    return NULL;

  memset (hdr, 0, sizeof (struct rfc2822_header));
  hdr->header = strdup (header);
  hdr->info = info;
  info->hdr = hdr;
  APPEND_LIST (message->headers, message->end_headers, hdr);
  return hdr;
}

/****************************************************************************
 * Header processing
 ****************************************************************************/

/* Called just before copying the messge from the application.
   Resets the seen flag for headers libESMTP is interested in */

static void
reset_headercb (const char *name __attribute__ ((unused)),
                void *data, void *arg __attribute__ ((unused)))
{
  struct header_info *info = data;

  assert (info != NULL);

  info->seen = 0;
}

int
reset_header_table (smtp_message_t message)
{
  int status;

  assert (message != NULL);

  if ((status = init_header_table (message)) < 0)
    h_enumerate (message->hdr_action, reset_headercb, NULL);
  return status;
}

/* This is called to process headers present in the application supplied
   message.  */
const char *
process_header (smtp_message_t message, const char *header, int *len)
{
  const char *p;
  struct header_info *info;
  const struct header_actions *action;
  hdrprint_t print;

  assert (message != NULL && header != NULL && len != NULL);

  if (*len > 0
      && (p = memchr (header, ':', *len)) != NULL
      && (info = find_header (message, header, p - header)) != NULL)
    {
      if ((action = info->action) != NULL)
        {
	  /* RFC 2822 states that headers may only appear once in a
	     message with the exception of a few special headers.
	     This restriction is enforced here. */
          if (info->seen && !(action->flags & (MULTIPLE | PRESERVE)))
            header = NULL;
          if (info->prohibit || (action->flags & PROHIBIT))
            header = NULL;

	  /* When libESMTP is overriding headers in the message with
	     ones supplied in the API, the substitution is done here
	     to preserve the original ordering of the headers.  */
	  if (header != NULL && info->override)
            {
	      if ((print = action->print) == NULL)
		print = print_string;
	      cat_reset (&message->hdr_buffer, 0);
	      (*print) (message, info->hdr);
	      header = cat_buffer (&message->hdr_buffer, len);
            }
        }
      else if (info->seen)
	header = NULL;
      info->seen = 1;
    }
  return header;
}

/* This is called to supply headers not present in the application supplied
   message.  */
const char *
missing_header (smtp_message_t message, int *len)
{
  struct header_info *info;
  hdrprint_t print;

  assert (message != NULL && len != NULL);

  /* Move on to the next header */
  if (message->current_header == NULL)
    message->current_header = message->headers;
  else
    message->current_header = message->current_header->next;

  /* Look for the next header that is actually required */
  print = NULL;
  while (message->current_header != NULL)
    {
      info = message->current_header->info;
      if (info == NULL)		/* shouldn't happen */
        break;
      if (!info->seen)
        {
	  if (info->action != NULL)
	    print = info->action->print;
	  break;
        }
      message->current_header = message->current_header->next;
    }
  if (message->current_header == NULL)
    {
      /* Free the buffer created by concatenate() and return NULL to
         mark the end of the headers */
      cat_free (&message->hdr_buffer);
      return NULL;
    }

  if (print == NULL)
    print = print_string;
  
  cat_reset (&message->hdr_buffer, 0);
  (*print) (message, message->current_header);
  return cat_buffer (&message->hdr_buffer, len);
}

/****************************************************************************
 * Header API
 ****************************************************************************/

int
smtp_set_header (smtp_message_t message, const char *header, ...)
{
  va_list alist;
  struct rfc2822_header *hdr;
  struct header_info *info;
  hdrset_t set;

  SMTPAPI_CHECK_ARGS (message != NULL && header != NULL, 0);

  if (!init_header_table (message))
    {
      set_errno (ENOMEM);
      return 0;
    }

  info = find_header (message, header, -1);
  if (info == NULL && (info = insert_header (message, header)) == NULL)
    {
      set_errno (ENOMEM);
      return 0;
    }

  /* Cannot specify a value for headers that must pass unchanged (MIME)
     or which may not appear in a posted message (Return-Path:).  */
  if (info->prohibit || (info->action->flags & (PROHIBIT | PRESERVE)))
    {
      set_error (SMTP_ERR_INVAL);
      return 0;
    }

  set = info->action->set;
  if (set == NULL)
    {
      set_error (SMTP_ERR_INVAL);
      return 0;
    }

  if (info->hdr == NULL)
    hdr = create_header (message, header, info);
  else if (info->hdr->value == NULL)
    hdr = info->hdr;
  else
    {
      /* Header has a previous value.  If multiple headers are permitted,
         create a new value.  If the header has a list value, the value
         is appended to the iost.  If neither condition applies, this
         is an error. */
      if (info->action->flags & MULTIPLE)
	hdr = create_header (message, header, info);
      else if (info->action->flags & LISTVALUE)
	hdr = info->hdr;
      else
        {
	  set_error (SMTP_ERR_INVAL);
	  return 0;
        }
  }

  /* Set its value */
  va_start (alist, header);
  (*set) (hdr, alist);
  va_end (alist);

  return 1;
}

int
smtp_set_header_option (smtp_message_t message, const char *header,
			enum header_option option, ...)
{
  va_list alist;
  struct header_info *info;

  SMTPAPI_CHECK_ARGS (message != NULL && header != NULL, 0);

  if (!init_header_table (message))
    {
      set_errno (ENOMEM);
      return 0;
    }

  info = find_header (message, header, -1);
  if (info == NULL && (info = insert_header (message, header)) == NULL)
    {
      set_errno (ENOMEM);
      return 0;
    }

  /* Don't permit options to be set on headers that must pass intact or
     which are prohibited. */
  if (info->action->flags & (PROHIBIT | PRESERVE))
    {
      set_error (SMTP_ERR_INVAL);
      return 0;
    }

  /* There is an odd quirk when setting options.  Setting an option for
     the OPTIONAL headers known to libESMTP causes default values to be
     generated automatically when not found in the message, so long as
     there is no other reason to prevent them appearing in the message! */

  /* Don't allow the user to set override on prohibited headers. */
  if (option == Hdr_OVERRIDE && !info->prohibit)
    {
      va_start (alist, option);
      info->override = !!va_arg (alist, int);
      va_end (alist);
      return 1;
    }
  /* Don't allow the user to prohibit required headers. */
  if (option == Hdr_PROHIBIT && !(info->action->flags & REQUIRE))
    {
      va_start (alist, option);
      info->prohibit = !!va_arg (alist, int);
      va_end (alist);
      return 1;
    }

  set_error (SMTP_ERR_INVAL);
  return 0;
}

int
smtp_set_resent_headers (smtp_message_t message, int onoff)
{
  SMTPAPI_CHECK_ARGS (message != NULL, 0);

  /* TODO: place holder, implement real functionality here.
           For now, succeed if the onoff argument is zero. */
  SMTPAPI_CHECK_ARGS (onoff == 0, 0);

  return 1;
}