masqmail

view src/accept.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents 41958685480d
children 9bc3e47b0222
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include "masqmail.h"
22 #include "readsock.h"
24 /* must match PROT_* in masqmail.h */
25 gchar *prot_names[] = {
26 "local",
27 "SMTP",
28 "ESMTP",
29 "(unknown)" /* should not happen, but better than crashing. */
30 };
32 static gchar*
33 string_base62(gchar *res, guint value, gchar len)
34 {
35 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
36 gchar *p = res + len;
37 *p = '\0';
38 while (p > res) {
39 *(--p) = base62_chars[value % 62];
40 value /= 62;
41 }
42 return res;
43 }
45 static gint
46 _g_list_addr_isequal(gconstpointer a, gconstpointer b)
47 {
48 address *addr1 = (address *) a;
49 address *addr2 = (address *) b;
50 int ret;
52 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0) {
53 return strcmp(addr1->local_part, addr2->local_part);
54 }
55 return ret;
56 }
58 /*
59 ** accept message from anywhere.
60 ** A message from local is indicated by msg->recieved_host == NULL
61 **
62 ** The -t option: With the ACC_RCPT_FROM_HEAD flag the addrs found found
63 ** in To/Cc/Bcc headers are added to the recipient list.
64 */
66 accept_error
67 accept_message_stream(FILE *in, message *msg, guint flags)
68 {
69 gchar *line, *line1;
70 int line_size = MAX_DATALINE;
71 gboolean in_headers = TRUE;
72 header *hdr = NULL;
73 gint line_cnt = 0, data_size = 0;
75 line = g_malloc(line_size);
76 line[0] = '\0';
78 while (TRUE) {
79 int len = read_sockline1(in, &line, &line_size, 5 * 60, READSOCKL_CVT_CRLF);
81 line1 = line;
83 if ((line[0] == '.') && (!(flags & ACC_DOT_IGNORE))) {
84 if (line[1] == '\n') {
85 g_free(line);
86 break;
87 }
88 line1++;
89 }
91 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
92 /* we got an EOF, and the last line was not terminated by a CR */
93 gint len1 = strlen(line1);
94 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
95 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
96 line1[len1] = '\n';
97 line1[len1 + 1] = '\0';
98 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
99 data_size += strlen(line1);
100 line_cnt++;
101 }
102 }
103 break;
105 } else if (len == -1) {
106 g_free(line);
107 return AERR_EOF;
109 } else if (len == -2) {
110 /* should not happen any more */
111 g_free(line);
112 return AERR_OVERFLOW;
114 } else if (len == -3) {
115 g_free(line);
116 return AERR_TIMEOUT;
118 } else if (len <= 0) {
119 /* does not happen */
120 g_free(line);
121 DEBUG(5) debugf("read_sockline returned %d\n", len);
122 return AERR_UNKNOWN;
124 }
126 if (in_headers) {
128 /* some pop servers send the 'From ' line, skip it: */
129 if (!msg->hdr_list && strncmp(line1, "From ", 5) == 0) {
130 continue;
131 }
133 if (line1[0] == ' ' || line1[0] == '\t') {
134 /* continuation of 'folded' header: */
135 if (hdr) {
136 char *cp;
137 cp = g_strconcat(hdr->header, line1, NULL);
138 hdr->value = cp + (hdr->value - hdr->header);
139 free(hdr->header);
140 hdr->header = cp;
141 }
143 } else if (line1[0] == '\n') {
144 /* an empty line marks end of headers */
145 in_headers = FALSE;
146 } else {
147 /* in all other cases we expect another header */
148 if ((hdr = get_header(line1))) {
149 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
150 } else {
151 /*
152 ** if get_header() returns NULL,
153 ** no header was recognized,
154 ** so this seems to be the first
155 ** data line of a broken mailer
156 ** which does not send an empty
157 ** line after the headers
158 */
159 in_headers = FALSE;
160 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
161 }
162 }
163 } else {
164 /* message body */
165 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
166 data_size += strlen(line1);
167 line_cnt++;
168 }
170 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
171 DEBUG(4) debugf("accept_message_stream(): "
172 "received %d bytes (conf.max_msg_size=%d)\n",
173 data_size, conf.max_msg_size);
174 return AERR_SIZE;
175 }
177 }
179 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
181 if (!msg->data_list) {
182 /* make sure data list is not NULL: */
183 msg->data_list = g_list_append(NULL, g_strdup(""));
184 }
185 msg->data_list = g_list_reverse(msg->data_list);
187 /* we get here after we succesfully received the mail data */
189 msg->data_size = data_size;
190 msg->received_time = time(NULL);
192 return AERR_OK;
193 }
195 accept_error
196 accept_message_prepare(message *msg, guint flags)
197 {
198 struct passwd *passwd = NULL;
199 time_t rec_time = time(NULL);
201 DEBUG(5) debugf("accept_message_prepare()\n");
203 /* create unique message id */
204 msg->uid = g_malloc(14);
205 string_base62(msg->uid, rec_time, 6);
206 msg->uid[6] = '-';
207 string_base62(msg->uid + 7, getpid(), 3);
208 msg->uid[10] = '-';
209 string_base62(msg->uid + 11, msg->transfer_id, 2);
210 msg->uid[13] = '\0';
212 /* if local, get password entry and set return path if missing */
213 if (!msg->received_host) {
214 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
215 msg->ident = g_strdup(passwd->pw_name);
216 if (!msg->return_path) {
217 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
218 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
219 msg->return_path = create_address(path, TRUE);
220 g_free(path);
221 }
222 }
224 /* scan headers */
225 {
226 gboolean has_id = FALSE;
227 gboolean has_date = FALSE;
228 gboolean has_sender = FALSE;
229 gboolean has_from = FALSE;
230 gboolean has_to_or_cc = FALSE;
231 GList *hdr_node, *hdr_node_next;
232 header *hdr;
234 for (hdr_node = g_list_first(msg->hdr_list);
235 hdr_node;
236 hdr_node = hdr_node_next) {
237 hdr_node_next = g_list_next(hdr_node);
238 hdr = ((header *) (hdr_node->data));
239 DEBUG(5) debugf("scanning headers: %s", hdr->header);
240 switch (hdr->id) {
241 case HEAD_MESSAGE_ID:
242 has_id = TRUE;
243 break;
244 case HEAD_DATE:
245 has_date = TRUE;
246 break;
247 case HEAD_FROM:
248 has_from = TRUE;
249 break;
250 case HEAD_SENDER:
251 has_sender = TRUE;
252 break;
253 case HEAD_TO:
254 case HEAD_CC:
255 has_to_or_cc = TRUE;
256 /* fall through */
257 case HEAD_BCC:
258 if (flags & ACC_RCPT_FROM_HEAD) {
259 /* -t option (see comment above) */
260 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
261 if (hdr->value) {
262 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
263 }
264 }
265 if (hdr->id == HEAD_BCC) {
266 DEBUG(3) debugf("removing 'Bcc' header\n");
267 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
268 g_list_free_1(hdr_node);
269 destroy_header(hdr);
270 }
271 break;
272 case HEAD_ENVELOPE_TO:
273 if (flags & ACC_SAVE_ENVELOPE_TO) {
274 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
275 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
276 "X-Orig-Envelope-To: %s", hdr->value));
277 }
278 DEBUG(3) debugf("removing 'Envelope-To' header\n");
279 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
280 g_list_free_1(hdr_node);
281 destroy_header(hdr);
282 break;
283 case HEAD_RETURN_PATH:
284 if (flags & ACC_MAIL_FROM_HEAD) {
285 /* usually POP3 accept */
286 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
287 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
288 }
289 DEBUG(3) debugf("removing 'Return-Path' header\n");
290 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
291 g_list_free_1(hdr_node);
292 destroy_header(hdr);
293 break;
294 default:
295 break; /* make compiler happy */
296 }
297 }
299 if (!msg->return_path) {
300 /*
301 ** TODO: do we still need this as we don't fetch
302 ** mail anymore?
303 ** This can happen for pop3 accept only and if no
304 ** Return-Path: header was given
305 */
306 GList *hdr_list;
307 header *hdr;
309 DEBUG(3) debugf("return_path == NULL\n");
311 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
312 if (!hdr_list) {
313 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
314 }
315 if (hdr_list) {
316 gchar *addr;
317 hdr = (header *) (g_list_first(hdr_list)->data);
319 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
321 addr = g_strdup(hdr->value);
322 g_strchomp(addr);
324 msg->return_path = create_address_qualified(addr, FALSE, msg->received_host);
325 if (msg->return_path) {
326 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
327 msg->hdr_list = g_list_append( msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Warning: return path set from %s address\n", hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
328 }
329 g_free(addr);
330 }
331 if (!msg->return_path) {
332 /* no Sender: or From: or
333 create_address_qualified failed */
334 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
335 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
336 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Warning: real return path is unknown\n"));
337 }
338 }
340 /* here we should have our recipients, fail if not: */
341 if (!msg->rcpt_list) {
342 logwrite(LOG_WARNING, "no recipients found in message\n");
343 return AERR_NORCPT;
344 }
346 if (!has_sender && !has_from) {
347 DEBUG(3) debugf("adding 'From:' header\n");
348 if (msg->full_sender_name) {
349 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name, msg->return_path->local_part, msg->return_path->domain));
350 } else {
351 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_FROM, "From: <%s@%s>\n", msg->return_path->local_part, msg->return_path->domain));
352 }
353 }
354 if (!has_to_or_cc) {
355 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
356 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
357 }
358 if (!has_date) {
359 DEBUG(3) debugf("adding 'Date:' header\n");
360 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
361 }
362 if (!has_id) {
363 DEBUG(3) debugf("adding 'Message-ID:' header\n");
364 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
365 }
366 }
368 /* Received header: */
369 /* At this point because we have to know the rcpts for the 'for' part */
370 /* The `for' part will only be used if exactly one rcpt is present. */
371 gchar *for_string = NULL;
372 header *hdr = NULL;
374 DEBUG(3) debugf("adding 'Received:' header\n");
376 if (g_list_length(msg->rcpt_list) == 1) {
377 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
378 for_string = g_strdup_printf("\n\tfor %s", addr_string(addr));
379 }
381 if (!msg->received_host) {
382 /* received locally */
383 hdr = create_header(HEAD_RECEIVED,
384 "Received: by %s (%s %s, from userid %d)\n\tid %s%s; %s\n",
385 conf.host_name, PACKAGE, VERSION, geteuid(),
386 msg->uid, for_string ? for_string : "", rec_timestamp());
387 } else {
388 /* received from remote */
389 DEBUG(5) debugf("adding 'Received:' header (5)\n");
390 hdr = create_header(HEAD_RECEIVED,
391 #ifdef ENABLE_IDENT
392 "Received: from %s (ident=%s)\n\tby %s with %s (%s %s)\n\tid %s%s; %s\n",
393 msg->received_host, msg->ident ? msg->ident : "unknown",
394 #else
395 "Received: from %s\n\tby %s with %s (%s %s)\n\tid %s%s; %s\n",
396 msg->received_host,
397 #endif
398 conf.host_name, prot_names[msg->received_prot], PACKAGE,
399 VERSION, msg->uid, for_string ? for_string : "",
400 rec_timestamp());
401 }
402 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
404 if (for_string)
405 g_free(for_string);
407 return AERR_OK;
408 }
410 accept_error
411 accept_message(FILE *in, message *msg, guint flags)
412 {
413 accept_error err;
415 err = accept_message_stream(in, msg, flags);
416 if (err == AERR_OK) {
417 err = accept_message_prepare(msg, flags);
418 }
420 return err;
421 }