]> diplodocus.org Git - nmh/blob - sbr/message_id.c
Fix invalid pointer arithmetic.
[nmh] / sbr / message_id.c
1 /* message_id.c -- construct the body of a Message-ID or Content-ID
2 * header field
3 *
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include "m_rand.h"
11 #include "message_id.h"
12 #include <sys/time.h> /* for gettimeofday() */
13 #include "base64.h"
14
15
16 static enum {
17 NMH_MESSAGE_ID_LOCALNAME,
18 NMH_MESSAGE_ID_RANDOM
19 } message_id_style = NMH_MESSAGE_ID_LOCALNAME;
20 static char message_id_[BUFSIZ];
21
22
23 /* Convert name of message id style to integer value and store it. */
24 int
25 save_message_id_style (const char *value) {
26 if (! strcasecmp (value, "localname")) {
27 message_id_style = NMH_MESSAGE_ID_LOCALNAME;
28 return 0;
29 }
30 if (! strcasecmp (value, "random")) {
31 message_id_style = NMH_MESSAGE_ID_RANDOM;
32 return 0;
33 }
34 return 1;
35 }
36
37
38 char *
39 message_id (time_t tclock, int content_id) {
40 switch (message_id_style) {
41 case NMH_MESSAGE_ID_LOCALNAME: {
42 #define P(fmt) \
43 snprintf(message_id_, sizeof message_id_, \
44 (fmt), (int)getpid(), (long)tclock, LocalName(1))
45
46 if (content_id)
47 P("<%d.%ld.%%d@%s>");
48 else
49 P("<%d.%ld@%s>");
50 #undef P
51 break;
52 }
53
54 case NMH_MESSAGE_ID_RANDOM: {
55 /* Use a sequence of digits divisible by 3 because that will
56 expand to base64 without any waste. Must be shorter than 58,
57 see below. */
58 unsigned char rnd[9];
59 /* The part after the '@' is divided into thirds. The base64
60 encoded string will be 4/3 the size of rnd. */
61 size_t one_third = sizeof rnd * 4/3/3;
62
63 if (m_rand (rnd, sizeof rnd) == 0) {
64 struct timeval now;
65 /* All we really need is 4 * [sizeof rnd/3] + 2, as long as
66 the base64 encoding stays shorter than 76 bytes so embedded
67 newlines aren't necessary. But use double the sizeof rnd
68 just to be safe. */
69 unsigned char rnd_base64[2 * sizeof rnd];
70 unsigned char *cp;
71 int i;
72
73 writeBase64 (rnd, sizeof rnd, rnd_base64);
74
75 for (i = strlen ((const char *) rnd_base64) - 1;
76 i > 0 && iscntrl (rnd_base64[i]);
77 --i) {
78 /* Remove trailing newline. rnd_base64 had better be
79 shorter than 76 characters, so don't bother to look for
80 embedded newlines. */
81 rnd_base64[i] = '\0';
82 }
83
84 /* Try to make the base64 string look a little more like a
85 hostname by replacing + with - and / with _. */
86 for (cp = rnd_base64; *cp; ++cp) {
87 if (*cp == '+') {
88 *cp = '-';
89 } else if (*cp == '/') {
90 *cp = '_';
91 }
92 }
93
94 /* gettimeofday() and getpid() shouldn't fail on POSIX platforms. */
95 gettimeofday (&now, 0);
96
97 /* The format string inserts a couple of dots, for the benefit
98 of spam filters that want to see a message id with a final
99 part that resembles a hostname. */
100 #define P(fmt) \
101 snprintf(message_id_, sizeof message_id_, (fmt), \
102 getpid(), (long)now.tv_sec, (long)now.tv_usec, \
103 (int)one_third, rnd_base64, \
104 (int)one_third, &rnd_base64[one_third], \
105 (int)one_third, &rnd_base64[2*one_third])
106
107 if (content_id)
108 P("<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>");
109 else
110 P("<%d-%ld.%06ld@%.*s.%.*s.%.*s>");
111 #undef P
112 }
113
114 break;
115 }
116 }
117
118 return message_id_;
119 }