/src/pigeonhole/src/lib-sieve/util/rfc2822.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | /* NOTE: much of the functionality implemented here should eventually appear |
4 | | * somewhere in Dovecot itself. |
5 | | */ |
6 | | |
7 | | #include "lib.h" |
8 | | #include "str.h" |
9 | | #include "unichar.h" |
10 | | |
11 | | #include "rfc2822.h" |
12 | | |
13 | | #include "message-header-encode.h" |
14 | | |
15 | | #include <stdio.h> |
16 | | #include <ctype.h> |
17 | | |
18 | | bool rfc2822_header_field_name_verify(const char *field_name, unsigned int len) |
19 | 0 | { |
20 | 0 | const char *p = field_name; |
21 | 0 | const char *pend = p + len; |
22 | | |
23 | | /* field-name = 1*ftext |
24 | | ftext = %d33-57 / ; Any character except |
25 | | %d59-126 ; controls, SP, and |
26 | | ; ":". |
27 | | */ |
28 | |
|
29 | 0 | while (p < pend) { |
30 | 0 | if (*p < 33 || *p == ':') |
31 | 0 | return FALSE; |
32 | | |
33 | 0 | p++; |
34 | 0 | } |
35 | 0 | return TRUE; |
36 | 0 | } |
37 | | |
38 | | bool rfc2822_header_field_body_verify(const char *field_body, unsigned int len, |
39 | | bool allow_crlf, bool allow_utf8) |
40 | 0 | { |
41 | 0 | const unsigned char *p = (const unsigned char *)field_body; |
42 | 0 | const unsigned char *pend = p + len; |
43 | 0 | bool is8bit = FALSE; |
44 | | |
45 | | /* RFC5322: |
46 | | |
47 | | unstructured = (*([FWS] VCHAR) *WSP) |
48 | | VCHAR = %x21-7E |
49 | | FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space |
50 | | WSP = SP / HTAB ; White space |
51 | | */ |
52 | |
|
53 | 0 | while (p < pend) { |
54 | 0 | if (*p < 0x20) { |
55 | 0 | if (*p == '\r' || *p == '\n') { |
56 | 0 | if (!allow_crlf) |
57 | 0 | return FALSE; |
58 | 0 | } else if (*p != '\t') { |
59 | 0 | return FALSE; |
60 | 0 | } |
61 | 0 | } |
62 | 0 | if (!is8bit && *p > 127) { |
63 | 0 | if (!allow_utf8) |
64 | 0 | return FALSE; |
65 | | |
66 | 0 | is8bit = TRUE; |
67 | 0 | } |
68 | 0 | p++; |
69 | 0 | } |
70 | | |
71 | 0 | if (is8bit && !uni_utf8_str_is_valid(field_body)) |
72 | 0 | return FALSE; |
73 | 0 | return TRUE; |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * |
78 | | */ |
79 | | |
80 | | const char *rfc2822_header_field_name_sanitize(const char *name) |
81 | 0 | { |
82 | 0 | char *result = t_strdup_noconst(name); |
83 | 0 | char *p; |
84 | | |
85 | | /* Make the whole name lower case ... */ |
86 | 0 | result = str_lcase(result); |
87 | | |
88 | | /* ... except for the first letter and those that follow '-' */ |
89 | 0 | p = result; |
90 | 0 | *p = i_toupper(*p); |
91 | 0 | while (*p != '\0') { |
92 | 0 | if (*p == '-') { |
93 | 0 | p++; |
94 | |
|
95 | 0 | if (*p != '\0') |
96 | 0 | *p = i_toupper(*p); |
97 | 0 | continue; |
98 | 0 | } |
99 | 0 | p++; |
100 | 0 | } |
101 | 0 | return result; |
102 | 0 | } |
103 | | |
104 | | /* |
105 | | * Message construction |
106 | | */ |
107 | | |
108 | | /* FIXME: This should be collected into a Dovecot API for composing internet |
109 | | * mail messages. |
110 | | */ |
111 | | |
112 | | unsigned int |
113 | | rfc2822_header_append(string_t *header, const char *name, const char *body, |
114 | | bool crlf, uoff_t *body_offset_r) |
115 | 0 | { |
116 | 0 | static const unsigned int max_line = 80; |
117 | |
|
118 | 0 | const char *bp = body; /* Pointer */ |
119 | 0 | const char *sp = body; /* Start pointer */ |
120 | 0 | const char *wp = NULL; /* Whitespace pointer */ |
121 | 0 | const char *nlp = NULL; /* New-line pointer */ |
122 | 0 | unsigned int line_len = strlen(name); |
123 | 0 | unsigned int lines = 0; |
124 | | |
125 | | /* Write header field name first */ |
126 | 0 | str_append(header, name); |
127 | 0 | str_append(header, ": "); |
128 | |
|
129 | 0 | if (body_offset_r != NULL) |
130 | 0 | *body_offset_r = str_len(header); |
131 | |
|
132 | 0 | line_len += 2; |
133 | | |
134 | | /* Add field body; fold it if necessary and account for existing |
135 | | folding. |
136 | | */ |
137 | 0 | while (*bp != '\0') { |
138 | 0 | bool ws_first = TRUE; |
139 | |
|
140 | 0 | while (*bp != '\0' && nlp == NULL && |
141 | 0 | (wp == NULL || line_len < max_line)) { |
142 | 0 | if (*bp == ' ' || *bp == '\t') { |
143 | 0 | if (ws_first) |
144 | 0 | wp = bp; |
145 | 0 | ws_first = FALSE; |
146 | 0 | } else if (*bp == '\r' || *bp == '\n') { |
147 | 0 | if (ws_first) |
148 | 0 | nlp = bp; |
149 | 0 | else |
150 | 0 | nlp = wp; |
151 | 0 | break; |
152 | 0 | } else { |
153 | 0 | ws_first = TRUE; |
154 | 0 | } |
155 | | |
156 | 0 | bp++; line_len++; |
157 | 0 | } |
158 | |
|
159 | 0 | if (*bp == '\0') |
160 | 0 | break; |
161 | | |
162 | | /* Existing newline ? */ |
163 | 0 | if (nlp != NULL) { |
164 | | /* Replace any consecutive newline and whitespace for |
165 | | consistency. */ |
166 | 0 | while (*bp == ' ' || *bp == '\t' || *bp == '\r' || |
167 | 0 | *bp == '\n') |
168 | 0 | bp++; |
169 | |
|
170 | 0 | str_append_data(header, sp, nlp-sp); |
171 | |
|
172 | 0 | if (crlf) |
173 | 0 | str_append(header, "\r\n"); |
174 | 0 | else |
175 | 0 | str_append(header, "\n"); |
176 | |
|
177 | 0 | while (*bp == ' ' || *bp == '\t') |
178 | 0 | bp++; |
179 | 0 | if (*bp != '\0') { |
180 | | /* Continued line; replace leading whitespace |
181 | | with single TAB. */ |
182 | 0 | str_append_c(header, '\t'); |
183 | 0 | } |
184 | |
|
185 | 0 | sp = bp; |
186 | 0 | } else { |
187 | | /* Insert newline at last whitespace within the max_line |
188 | | limit. */ |
189 | 0 | i_assert(wp >= sp); |
190 | 0 | str_append_data(header, sp, wp-sp); |
191 | | |
192 | | /* Force continued line; drop any existing whitespace. |
193 | | */ |
194 | 0 | while (*wp == ' ' || *wp == '\t') |
195 | 0 | wp++; |
196 | |
|
197 | 0 | if (crlf) |
198 | 0 | str_append(header, "\r\n"); |
199 | 0 | else |
200 | 0 | str_append(header, "\n"); |
201 | | |
202 | | /* Insert single TAB instead of the original |
203 | | whitespace. */ |
204 | 0 | str_append_c(header, '\t'); |
205 | |
|
206 | 0 | sp = wp; |
207 | 0 | if (sp > bp) |
208 | 0 | bp = sp; |
209 | 0 | } |
210 | | |
211 | 0 | lines++; |
212 | |
|
213 | 0 | line_len = bp - sp; |
214 | 0 | wp = NULL; |
215 | 0 | nlp = NULL; |
216 | 0 | } |
217 | | |
218 | 0 | if (bp != sp || lines == 0) { |
219 | 0 | str_append_data(header, sp, bp-sp); |
220 | 0 | if (crlf) |
221 | 0 | str_append(header, "\r\n"); |
222 | 0 | else |
223 | 0 | str_append(header, "\n"); |
224 | 0 | lines++; |
225 | 0 | } |
226 | |
|
227 | 0 | return lines; |
228 | 0 | } |
229 | | |
230 | | void rfc2822_header_printf(string_t *header, const char *name, |
231 | | const char *fmt, ...) |
232 | 0 | { |
233 | 0 | const char *body; |
234 | 0 | va_list args; |
235 | |
|
236 | 0 | va_start(args, fmt); |
237 | 0 | body = t_strdup_vprintf(fmt, args); |
238 | 0 | va_end(args); |
239 | |
|
240 | 0 | rfc2822_header_write(header, name, body); |
241 | 0 | } |
242 | | |
243 | | void rfc2822_header_utf8_printf(string_t *header, const char *name, |
244 | | const char *fmt, ...) |
245 | 0 | { |
246 | 0 | string_t *body = t_str_new(256); |
247 | 0 | va_list args; |
248 | |
|
249 | 0 | va_start(args, fmt); |
250 | 0 | message_header_encode(t_strdup_vprintf(fmt, args), body); |
251 | 0 | va_end(args); |
252 | |
|
253 | 0 | rfc2822_header_write(header, name, str_c(body)); |
254 | 0 | } |
255 | | |
256 | | |
257 | | void rfc2822_header_write_address(string_t *header, const char *name, |
258 | | const char *address) |
259 | 0 | { |
260 | 0 | bool has_8bit = FALSE; |
261 | 0 | const char *p; |
262 | |
|
263 | 0 | for (p = address; *p != '\0'; p++) { |
264 | 0 | if ((*p & 0x80) != 0) |
265 | 0 | has_8bit = TRUE; |
266 | 0 | } |
267 | |
|
268 | 0 | if (!has_8bit) { |
269 | 0 | rfc2822_header_write(header, name, address); |
270 | 0 | } else { |
271 | 0 | string_t *body = t_str_new(256); |
272 | 0 | message_header_encode(address, body); |
273 | 0 | rfc2822_header_write(header, name, str_c(body)); |
274 | 0 | } |
275 | 0 | } |