masqmail
view src/accept.c @ 106:1f0d63713a1c
masqmail now *always* removes Bcc: headers
this is the most simple way and taken by other MTAs (courier, postfix) too
author | meillo@marmaro.de |
---|---|
date | Tue, 29 Jun 2010 10:37:18 +0200 |
parents | 47ee3fbcecd2 |
children | db52c64acaa2 |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
20 #include "readsock.h"
22 gchar *prot_names[] = {
23 "local",
24 "bsmtp",
25 "smtp",
26 "esmtp",
27 "pop3",
28 "apop",
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 else
55 return ret;
56 }
58 /* accept message from anywhere.
59 A locally originating message is indicated by msg->recieved_host == NULL
61 If the flags ACC_DEL_RCPTS is set, recipients in the msg->rcpt_list is
62 copied and items occuring in it will be removed from the newly constructed
63 (from To/Cc/Bcc headers if ACC_RCPT_TO is set) rcpt_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_NODOT_TERM))) {
84 if (line[1] == '\n') {
85 g_free(line);
86 break;
87 }
88 line1++;
89 }
91 if (len <= 0) {
92 if ((len == -1) && ((flags & ACC_NODOT_TERM) || (flags & ACC_NODOT_RELAX))) {
93 /* we got an EOF, and the last line was not terminated by a CR */
94 gint len1 = strlen(line1);
95 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
96 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
97 line1[len1] = '\n';
98 line1[len1 + 1] = '\0';
99 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
100 data_size += strlen(line1);
101 line_cnt++;
102 }
103 }
104 break;
105 } else {
106 g_free(line);
107 if (len == -1) {
108 return AERR_EOF;
109 } else if (len == -2) {
110 /* should not happen any more */
111 return AERR_OVERFLOW;
112 } else if (len == -3) {
113 return AERR_TIMEOUT;
114 } else {
115 /* does not happen */
116 DEBUG(5) debugf("read_sockline returned %d\n", len);
117 return AERR_UNKNOWN;
118 }
119 }
120 } else {
121 if (in_headers) {
123 /* some pop servers send the 'From ' line, skip it: */
124 if (msg->hdr_list == NULL)
125 if (strncmp(line1, "From ", 5) == 0)
126 continue;
128 if (line1[0] == ' ' || line1[0] == '\t') {
129 /* continuation of 'folded' header: */
130 if (hdr) {
131 hdr->header = g_strconcat(hdr->header, line1, NULL);
132 }
134 } else if (line1[0] == '\n') {
135 /* an empty line marks end of headers */
136 in_headers = FALSE;
137 } else {
138 /* in all other cases we expect another header */
139 if ((hdr = get_header(line1)))
140 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
141 else {
142 /* if get_header() returns NULL, no header was recognized,
143 so this seems to be the first data line of a broken mailer
144 which does not send an empty line after the headers */
145 in_headers = FALSE;
146 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
147 }
148 }
149 } else {
150 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
151 data_size += strlen(line1);
152 line_cnt++;
153 }
154 }
155 }
157 if (msg->data_list != NULL)
158 msg->data_list = g_list_reverse(msg->data_list);
159 else
160 /* make sure data list is not NULL: */
161 msg->data_list = g_list_append(NULL, g_strdup(""));
163 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
164 /* we get here after we succesfully received the mail data */
166 msg->data_size = data_size;
167 msg->received_time = time(NULL);
169 return AERR_OK;
170 }
172 accept_error
173 accept_message_prepare(message * msg, guint flags)
174 {
175 struct passwd *passwd = NULL;
176 GList *non_rcpt_list = NULL;
177 time_t rec_time = time(NULL);
179 DEBUG(5) debugf("accept_message_prepare()\n");
181 /* create unique message id */
182 msg->uid = g_malloc(14);
184 string_base62(msg->uid, rec_time, 6);
185 msg->uid[6] = '-';
186 string_base62(&(msg->uid[7]), getpid(), 3);
187 msg->uid[10] = '-';
188 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
189 msg->uid[13] = 0;
191 /* if local, get password entry */
192 if (msg->received_host == NULL) {
193 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
194 msg->ident = g_strdup(passwd->pw_name);
195 }
197 /* set return path if local */
198 if (msg->return_path == NULL && msg->received_host == NULL) {
199 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
200 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
201 msg->return_path = create_address(path, TRUE);
202 g_free(path);
203 }
205 /* -t option */
206 if (flags & ACC_DEL_RCPTS) {
207 non_rcpt_list = msg->rcpt_list;
208 msg->rcpt_list = NULL;
209 }
211 /* scan headers */
212 {
213 gboolean has_id = FALSE;
214 gboolean has_date = FALSE;
215 gboolean has_sender = FALSE;
216 gboolean has_from = FALSE;
217 gboolean has_to_or_cc = FALSE;
218 GList *hdr_node, *hdr_node_next;
219 header *hdr;
221 for (hdr_node = g_list_first(msg->hdr_list);
222 hdr_node != NULL; hdr_node = hdr_node_next) {
223 hdr_node_next = g_list_next(hdr_node);
224 hdr = ((header *) (hdr_node->data));
225 DEBUG(5) debugf("scanning headers: %s", hdr->header);
226 switch (hdr->id) {
227 case HEAD_MESSAGE_ID:
228 has_id = TRUE;
229 break;
230 case HEAD_DATE:
231 has_date = TRUE;
232 break;
233 case HEAD_FROM:
234 has_from = TRUE;
235 break;
236 case HEAD_SENDER:
237 has_sender = TRUE;
238 break;
239 case HEAD_TO:
240 case HEAD_CC:
241 has_to_or_cc = TRUE;
242 /* fall through */
243 case HEAD_BCC:
244 if (flags & ACC_RCPT_FROM_HEAD) {
245 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
246 if (hdr->value) {
247 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
248 }
249 }
250 if (hdr->id == HEAD_BCC) {
251 DEBUG(3) debugf("removing 'Bcc' header\n");
252 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
253 g_list_free_1(hdr_node);
254 destroy_header(hdr);
255 }
256 break;
257 case HEAD_ENVELOPE_TO:
258 if (flags & ACC_SAVE_ENVELOPE_TO) {
259 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
260 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
261 "X-Orig-Envelope-to: %s", hdr->value));
262 }
263 DEBUG(3) debugf("removing 'Envelope-To' header\n");
264 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
265 g_list_free_1(hdr_node);
266 destroy_header(hdr);
267 break;
268 case HEAD_RETURN_PATH:
269 if (flags & ACC_MAIL_FROM_HEAD) {
270 /* usually POP3 accept */
271 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
272 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
273 }
274 DEBUG(3) debugf("removing 'Return-Path' header\n");
275 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
276 g_list_free_1(hdr_node);
277 destroy_header(hdr);
278 break;
279 default:
280 break; /* make compiler happy */
281 }
282 }
284 if (msg->return_path == NULL) {
285 /* this can happen for pop3 accept only and if no Return-path: header was given */
286 GList *hdr_list;
287 header *hdr;
289 DEBUG(3) debugf("return_path == NULL\n");
291 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
292 if (!hdr_list)
293 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
294 if (hdr_list) {
295 gchar *addr;
296 hdr = (header *) (g_list_first(hdr_list)->data);
298 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
300 addr = g_strdup(hdr->value);
301 g_strchomp(addr);
303 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
304 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
305 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
306 "X-Warning: return path set from %s address\n",
307 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
308 }
309 g_free(addr);
310 }
311 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
312 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
313 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
314 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
315 "X-Warning: real return path is unknown\n"));
316 }
317 }
319 if (flags & ACC_DEL_RCPTS) {
320 GList *rcpt_node;
321 foreach(non_rcpt_list, rcpt_node) {
322 address *rcpt = (address *) (rcpt_node->data);
323 GList *node;
324 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
325 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
326 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
327 destroy_address((address *) (node->data));
328 g_list_free_1(node);
329 }
330 }
331 }
333 /* here we should have our recipients, fail if not: */
334 if (msg->rcpt_list == NULL) {
335 logwrite(LOG_WARNING, "no recipients found in message\n");
336 return AERR_NORCPT;
337 }
339 if (!(has_sender || has_from)) {
340 DEBUG(3) debugf("adding 'From' header\n");
341 msg->hdr_list = g_list_append(msg->hdr_list,
342 msg->full_sender_name
343 ?
344 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
345 msg->return_path->local_part, msg->return_path->domain)
346 :
347 create_header(HEAD_FROM, "From: <%s@%s>\n",
348 msg->return_path->local_part, msg->return_path->domain)
349 );
350 }
351 if (!has_to_or_cc) {
352 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
353 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
354 }
355 if (!has_date) {
356 DEBUG(3) debugf("adding 'Date:' header\n");
357 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
358 }
359 if (!has_id) {
360 DEBUG(3) debugf("adding 'Message-ID:' header\n");
361 msg->hdr_list = g_list_append(msg->hdr_list,
362 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
363 }
364 }
366 /* Received header: */
367 /* At this point because we have to know the rcpts for the 'for' part */
368 gchar *for_string = NULL;
369 header *hdr = NULL;
371 DEBUG(3) debugf("adding 'Received:' header\n");
373 if (g_list_length(msg->rcpt_list) == 1) {
374 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
375 for_string = g_strdup_printf(" for %s", addr_string(addr));
376 }
378 if (msg->received_host == NULL) {
379 /* received locally */
380 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
381 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
382 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
383 } else {
384 /* received from remote */
385 #ifdef ENABLE_IDENT
386 DEBUG(5) debugf("adding 'Received:' header (5)\n");
387 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
388 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
389 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
390 rec_timestamp());
391 #else
392 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
393 msg->received_host, conf.host_name, prot_names[msg->received_prot],
394 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
395 #endif
396 }
397 header_fold(hdr);
398 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
400 if (for_string)
401 g_free(for_string);
403 /* write message to spool: */
404 /* accept is no longer responsible for this
405 if (!spool_write(msg, TRUE))
406 return AERR_NOSPOOL;
407 */
408 return AERR_OK;
409 }
411 accept_error
412 accept_message(FILE * in, message * msg, guint flags)
413 {
414 accept_error err;
416 err = accept_message_stream(in, msg, flags);
417 if (err == AERR_OK)
418 err = accept_message_prepare(msg, flags);
420 return err;
421 }