masqmail
view src/accept.c @ 16:163aa299bedb
fixed man pages
added backslashes to dashes (to satisfy Debian)
removed wrong sentence about /etc/aliases. Thanks to Alexis. (Closes Debian bug #480477)
fixed typos reported by Reuben Thomas. (Closes Debian bug #427096)
author | meillo@marmaro.de |
---|---|
date | Thu, 06 Nov 2008 09:33:32 +0100 |
parents | 49dab67fe461 |
children | 7c1635972aa7 |
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 =
132 g_strconcat(hdr->header, line1, NULL);
133 }
135 } else if (line1[0] == '\n') {
136 /* an empty line marks end of headers */
137 in_headers = FALSE;
138 } else {
139 /* in all other cases we expect another header */
140 if ((hdr = get_header(line1)))
141 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
142 else {
143 /* if get_header() returns NULL, no header was recognized,
144 so this seems to be the first data line of a broken mailer
145 which does not send an empty line after the headers */
146 in_headers = FALSE;
147 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
148 }
149 }
150 } else {
151 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
152 data_size += strlen(line1);
153 line_cnt++;
154 }
155 }
156 }
158 if (msg->data_list != NULL)
159 msg->data_list = g_list_reverse(msg->data_list);
160 else
161 /* make sure data list is not NULL: */
162 msg->data_list = g_list_append(NULL, g_strdup(""));
164 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
165 /* we get here after we succesfully received the mail data */
167 msg->data_size = data_size;
168 msg->received_time = time(NULL);
170 return AERR_OK;
171 }
173 accept_error
174 accept_message_prepare(message * msg, guint flags)
175 {
176 struct passwd *passwd = NULL;
177 GList *non_rcpt_list = NULL;
178 time_t rec_time = time(NULL);
180 DEBUG(5) debugf("accept_message_prepare()\n");
182 /* create unique message id */
183 msg->uid = g_malloc(14);
185 string_base62(msg->uid, rec_time, 6);
186 msg->uid[6] = '-';
187 string_base62(&(msg->uid[7]), getpid(), 3);
188 msg->uid[10] = '-';
189 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
190 msg->uid[13] = 0;
192 /* if local, get password entry */
193 if (msg->received_host == NULL) {
194 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
195 msg->ident = g_strdup(passwd->pw_name);
196 }
198 /* set return path if local */
199 if (msg->return_path == NULL) {
201 if (msg->received_host == NULL) {
202 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
203 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
204 msg->return_path = create_address(path, TRUE);
205 g_free(path);
206 }
207 }
209 /* -t option */
210 if (flags & ACC_DEL_RCPTS) {
211 non_rcpt_list = msg->rcpt_list;
212 msg->rcpt_list = NULL;
213 }
215 /* scan headers */
216 {
217 gboolean has_id = FALSE;
218 gboolean has_date = FALSE;
219 gboolean has_sender = FALSE;
220 gboolean has_from = FALSE;
221 gboolean has_rcpt = FALSE;
222 gboolean has_to_or_cc = FALSE;
223 GList *hdr_node, *hdr_node_next;
224 header *hdr;
226 for (hdr_node = g_list_first(msg->hdr_list);
227 hdr_node != NULL; hdr_node = hdr_node_next) {
228 hdr_node_next = g_list_next(hdr_node);
229 hdr = ((header *) (hdr_node->data));
230 DEBUG(5) debugf("scanning headers: %s", hdr->header);
231 switch (hdr->id) {
232 case HEAD_MESSAGE_ID:
233 has_id = TRUE;
234 break;
235 case HEAD_DATE:
236 has_date = TRUE;
237 break;
238 case HEAD_FROM:
239 has_from = TRUE;
240 break;
241 case HEAD_SENDER:
242 has_sender = TRUE;
243 break;
244 case HEAD_TO:
245 case HEAD_CC:
246 case HEAD_BCC:
247 has_rcpt = TRUE;
248 if (flags & ACC_RCPT_FROM_HEAD) {
249 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
250 if (hdr->value) {
251 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
252 }
253 }
254 if ((flags & ACC_DEL_BCC) && (hdr->id == HEAD_BCC)) {
255 DEBUG(3) debugf("removing 'Bcc' header\n");
256 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
257 g_list_free_1(hdr_node);
258 destroy_header(hdr);
259 } else
260 has_to_or_cc = TRUE;
261 break;
262 case HEAD_ENVELOPE_TO:
263 if (flags & ACC_SAVE_ENVELOPE_TO) {
264 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
265 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Orig-Envelope-to: %s", hdr->value));
266 }
267 DEBUG(3) debugf("removing 'Envelope-To' header\n");
268 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
269 g_list_free_1(hdr_node);
270 destroy_header(hdr);
271 break;
272 case HEAD_RETURN_PATH:
273 if (flags & ACC_MAIL_FROM_HEAD) {
274 /* usually POP3 accept */
275 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
276 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
277 }
278 DEBUG(3) debugf("removing 'Return-Path' 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 default:
284 break; /* make compiler happy */
285 }
286 }
288 if (msg->return_path == NULL) {
289 /* this can happen for pop3 accept only and if no Return-path: header was given */
290 GList *hdr_list;
291 header *hdr;
293 DEBUG(3) debugf("return_path == NULL\n");
295 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
296 if (!hdr_list)
297 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
298 if (hdr_list) {
299 gchar *addr;
300 hdr = (header *) (g_list_first(hdr_list)->data);
302 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
304 addr = g_strdup(hdr->value);
305 g_strchomp(addr);
307 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
308 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
309 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
310 "X-Warning: return path set from %s address\n",
311 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
312 }
313 g_free(addr);
314 }
315 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
316 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
317 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
318 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
319 "X-Warning: real return path is unkown\n"));
320 }
321 }
323 if (flags & ACC_DEL_RCPTS) {
324 GList *rcpt_node;
325 foreach(non_rcpt_list, rcpt_node) {
326 address *rcpt = (address *) (rcpt_node->data);
327 GList *node;
328 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
329 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
330 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
331 destroy_address((address *) (node->data));
332 g_list_free_1(node);
333 }
334 }
335 }
337 /* here we should have our recipients, fail if not: */
338 if (msg->rcpt_list == NULL) {
339 logwrite(LOG_WARNING, "no recipients found in message\n");
340 return AERR_NORCPT;
341 }
343 if (!(has_sender || has_from)) {
344 DEBUG(3) debugf("adding 'From' header\n");
345 msg->hdr_list = g_list_append(msg->hdr_list,
346 msg->full_sender_name
347 ?
348 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
349 msg->return_path->local_part, msg->return_path->domain)
350 :
351 create_header(HEAD_FROM, "From: <%s@%s>\n",
352 msg->return_path->local_part, msg->return_path->domain)
353 );
354 }
355 if ((flags & ACC_HEAD_FROM_RCPT) && !has_rcpt) {
356 GList *node;
357 DEBUG(3) debugf("adding 'To' header(s)\n");
358 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
359 msg->hdr_list = g_list_append(msg->hdr_list,
360 create_header(HEAD_TO, "To: %s\n", addr_string(msg-> return_path)));
361 }
362 }
363 if ((flags & ACC_DEL_BCC) && !has_to_or_cc) {
364 /* Bcc headers have been removed, and there are no remaining rcpt headers */
365 DEBUG(3) debugf("adding empty 'Bcc:' header\n");
366 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_BCC, "Bcc:\n"));
367 }
368 if (!has_date) {
369 DEBUG(3) debugf("adding 'Date:' header\n");
370 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
371 }
372 if (!has_id) {
373 DEBUG(3) debugf("adding 'Message-ID:' header\n");
374 msg->hdr_list = g_list_append(msg->hdr_list,
375 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
376 }
377 }
379 /* Received header: */
380 /* At this point because we have to know the rcpts for the 'for' part */
381 if (!(flags & ACC_NO_RECVD_HDR)) {
382 gchar *for_string = NULL;
383 header *hdr = NULL;
385 DEBUG(3) debugf("adding 'Received:' header\n");
387 if (g_list_length(msg->rcpt_list) == 1) {
388 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
389 for_string = g_strdup_printf(" for %s", addr_string(addr));
390 }
392 if (msg->received_host == NULL) {
393 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
394 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
395 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
396 } else {
397 #ifdef ENABLE_IDENT
398 DEBUG(5) debugf("adding 'Received:' header (5)\n");
399 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
400 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
401 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
402 rec_timestamp());
403 #else
404 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
405 msg->received_host, conf.host_name, prot_names[msg->received_prot],
406 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
407 #endif
408 }
409 header_fold(hdr);
410 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
412 if (for_string)
413 g_free(for_string);
414 }
416 /* write message to spool: */
417 /* accept is no longer responsible for this
418 if (!spool_write(msg, TRUE))
419 return AERR_NOSPOOL;
420 */
421 return AERR_OK;
422 }
424 accept_error
425 accept_message(FILE * in, message * msg, guint flags)
426 {
427 accept_error err;
429 err = accept_message_stream(in, msg, flags);
430 if (err == AERR_OK)
431 err = accept_message_prepare(msg, flags);
433 return err;
434 }