masqmail-0.2

view src/accept.c @ 174:087e99c7702a

added my copyright to files I worked on
author meillo@marmaro.de
date Fri, 23 Jul 2010 10:16:53 +0200
parents cd59a5b4d3dd
children ec3fe72a3e99
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include "masqmail.h"
21 #include "readsock.h"
23 gchar *prot_names[] = {
24 "local",
25 "bsmtp",
26 "smtp",
27 "esmtp",
28 "pop3",
29 "apop",
30 "(unknown)" /* should not happen, but better than crashing. */
31 };
33 static gchar*
34 string_base62(gchar * res, guint value, gchar len)
35 {
36 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
37 gchar *p = res + len;
38 *p = '\0';
39 while (p > res) {
40 *(--p) = base62_chars[value % 62];
41 value /= 62;
42 }
43 return res;
44 }
46 static gint
47 _g_list_addr_isequal(gconstpointer a, gconstpointer b)
48 {
49 address *addr1 = (address *) a;
50 address *addr2 = (address *) b;
51 int ret;
53 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0)
54 return strcmp(addr1->local_part, addr2->local_part);
55 else
56 return ret;
57 }
59 /* accept message from anywhere.
60 A locally originating message is indicated by msg->recieved_host == NULL
62 The -t option:
63 The ACC_RCPT_FROM_HEAD flag adds the recipients found in To/Cc/Bcc
64 headers to the recipients list.
65 The ACC_DEL_RCPTS flag makes only sense if the ACC_RCPT_FROM_HEAD
66 flag is given too. With ACC_DEL_RCPTS the recipients given on the
67 command line are removed from the ones given in headers.
68 */
70 accept_error
71 accept_message_stream(FILE * in, message * msg, guint flags)
72 {
73 gchar *line, *line1;
74 int line_size = MAX_DATALINE;
75 gboolean in_headers = TRUE;
76 header *hdr = NULL;
77 gint line_cnt = 0, data_size = 0;
79 line = g_malloc(line_size);
80 line[0] = '\0';
82 while (TRUE) {
83 int len = read_sockline1(in, &line, &line_size, 5 * 60, READSOCKL_CVT_CRLF);
85 line1 = line;
87 if ((line[0] == '.') && (!(flags & ACC_DOT_IGNORE))) {
88 if (line[1] == '\n') {
89 g_free(line);
90 break;
91 }
92 line1++;
93 }
95 if (len <= 0) {
96 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
97 /* we got an EOF, and the last line was not terminated by a CR */
98 gint len1 = strlen(line1);
99 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
100 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
101 line1[len1] = '\n';
102 line1[len1 + 1] = '\0';
103 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
104 data_size += strlen(line1);
105 line_cnt++;
106 }
107 }
108 break;
109 } else {
110 g_free(line);
111 if (len == -1) {
112 return AERR_EOF;
113 } else if (len == -2) {
114 /* should not happen any more */
115 return AERR_OVERFLOW;
116 } else if (len == -3) {
117 return AERR_TIMEOUT;
118 } else {
119 /* does not happen */
120 DEBUG(5) debugf("read_sockline returned %d\n", len);
121 return AERR_UNKNOWN;
122 }
123 }
124 } else {
125 if (in_headers) {
127 /* some pop servers send the 'From ' line, skip it: */
128 if (msg->hdr_list == NULL)
129 if (strncmp(line1, "From ", 5) == 0)
130 continue;
132 if (line1[0] == ' ' || line1[0] == '\t') {
133 /* continuation of 'folded' header: */
134 if (hdr) {
135 hdr->header = g_strconcat(hdr->header, line1, NULL);
136 }
138 } else if (line1[0] == '\n') {
139 /* an empty line marks end of headers */
140 in_headers = FALSE;
141 } else {
142 /* in all other cases we expect another header */
143 if ((hdr = get_header(line1)))
144 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
145 else {
146 /* if get_header() returns NULL, no header was recognized,
147 so this seems to be the first data line of a broken mailer
148 which does not send an empty line after the headers */
149 in_headers = FALSE;
150 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
151 }
152 }
153 } else {
154 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
155 data_size += strlen(line1);
156 line_cnt++;
157 }
158 }
159 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
160 DEBUG(4) debugf("accept_message_stream(): "
161 "received %d bytes (conf.max_msg_size=%d)\n",
162 data_size, conf.max_msg_size);
163 return AERR_SIZE;
164 }
166 }
168 if (msg->data_list != NULL)
169 msg->data_list = g_list_reverse(msg->data_list);
170 else
171 /* make sure data list is not NULL: */
172 msg->data_list = g_list_append(NULL, g_strdup(""));
174 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
175 /* we get here after we succesfully received the mail data */
177 msg->data_size = data_size;
178 msg->received_time = time(NULL);
180 return AERR_OK;
181 }
183 accept_error
184 accept_message_prepare(message * msg, guint flags)
185 {
186 struct passwd *passwd = NULL;
187 GList *non_rcpt_list = NULL;
188 time_t rec_time = time(NULL);
190 DEBUG(5) debugf("accept_message_prepare()\n");
192 /* create unique message id */
193 msg->uid = g_malloc(14);
195 string_base62(msg->uid, rec_time, 6);
196 msg->uid[6] = '-';
197 string_base62(&(msg->uid[7]), getpid(), 3);
198 msg->uid[10] = '-';
199 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
200 msg->uid[13] = 0;
202 /* if local, get password entry */
203 if (msg->received_host == NULL) {
204 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
205 msg->ident = g_strdup(passwd->pw_name);
206 }
208 /* set return path if local */
209 if (msg->return_path == NULL && msg->received_host == NULL) {
210 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
211 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
212 msg->return_path = create_address(path, TRUE);
213 g_free(path);
214 }
216 /* -t option (see comment above) */
217 if (flags & ACC_DEL_RCPTS) {
218 non_rcpt_list = msg->rcpt_list;
219 msg->rcpt_list = NULL;
220 }
222 /* scan headers */
223 {
224 gboolean has_id = FALSE;
225 gboolean has_date = FALSE;
226 gboolean has_sender = FALSE;
227 gboolean has_from = FALSE;
228 gboolean has_to_or_cc = FALSE;
229 GList *hdr_node, *hdr_node_next;
230 header *hdr;
232 for (hdr_node = g_list_first(msg->hdr_list);
233 hdr_node != NULL; hdr_node = hdr_node_next) {
234 hdr_node_next = g_list_next(hdr_node);
235 hdr = ((header *) (hdr_node->data));
236 DEBUG(5) debugf("scanning headers: %s", hdr->header);
237 switch (hdr->id) {
238 case HEAD_MESSAGE_ID:
239 has_id = TRUE;
240 break;
241 case HEAD_DATE:
242 has_date = TRUE;
243 break;
244 case HEAD_FROM:
245 has_from = TRUE;
246 break;
247 case HEAD_SENDER:
248 has_sender = TRUE;
249 break;
250 case HEAD_TO:
251 case HEAD_CC:
252 has_to_or_cc = TRUE;
253 /* fall through */
254 case HEAD_BCC:
255 if (flags & ACC_RCPT_FROM_HEAD) {
256 /* -t option (see comment above) */
257 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
258 if (hdr->value) {
259 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
260 }
261 }
262 if (hdr->id == HEAD_BCC) {
263 DEBUG(3) debugf("removing 'Bcc' 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 }
268 break;
269 case HEAD_ENVELOPE_TO:
270 if (flags & ACC_SAVE_ENVELOPE_TO) {
271 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
272 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
273 "X-Orig-Envelope-to: %s", hdr->value));
274 }
275 DEBUG(3) debugf("removing 'Envelope-To' header\n");
276 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
277 g_list_free_1(hdr_node);
278 destroy_header(hdr);
279 break;
280 case HEAD_RETURN_PATH:
281 if (flags & ACC_MAIL_FROM_HEAD) {
282 /* usually POP3 accept */
283 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
284 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
285 }
286 DEBUG(3) debugf("removing 'Return-Path' header\n");
287 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
288 g_list_free_1(hdr_node);
289 destroy_header(hdr);
290 break;
291 default:
292 break; /* make compiler happy */
293 }
294 }
296 if (msg->return_path == NULL) {
297 /* this can happen for pop3 accept only and if no Return-path: header was given */
298 GList *hdr_list;
299 header *hdr;
301 DEBUG(3) debugf("return_path == NULL\n");
303 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
304 if (!hdr_list)
305 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
306 if (hdr_list) {
307 gchar *addr;
308 hdr = (header *) (g_list_first(hdr_list)->data);
310 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
312 addr = g_strdup(hdr->value);
313 g_strchomp(addr);
315 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
316 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
317 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
318 "X-Warning: return path set from %s address\n",
319 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
320 }
321 g_free(addr);
322 }
323 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
324 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
325 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
326 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
327 "X-Warning: real return path is unknown\n"));
328 }
329 }
331 if (flags & ACC_DEL_RCPTS) {
332 /* remove the recipients given on the command line from the ones given in headers
333 -t option (see comment above) */
334 GList *rcpt_node;
335 foreach(non_rcpt_list, rcpt_node) {
336 address *rcpt = (address *) (rcpt_node->data);
337 GList *node;
338 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
339 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
340 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
341 destroy_address((address *) (node->data));
342 g_list_free_1(node);
343 }
344 }
345 }
347 /* here we should have our recipients, fail if not: */
348 if (msg->rcpt_list == NULL) {
349 logwrite(LOG_WARNING, "no recipients found in message\n");
350 return AERR_NORCPT;
351 }
353 if (!(has_sender || has_from)) {
354 DEBUG(3) debugf("adding 'From' header\n");
355 msg->hdr_list = g_list_append(msg->hdr_list,
356 msg->full_sender_name
357 ?
358 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
359 msg->return_path->local_part, msg->return_path->domain)
360 :
361 create_header(HEAD_FROM, "From: <%s@%s>\n",
362 msg->return_path->local_part, msg->return_path->domain)
363 );
364 }
365 if (!has_to_or_cc) {
366 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
367 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
368 }
369 if (!has_date) {
370 DEBUG(3) debugf("adding 'Date:' header\n");
371 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
372 }
373 if (!has_id) {
374 DEBUG(3) debugf("adding 'Message-ID:' header\n");
375 msg->hdr_list = g_list_append(msg->hdr_list,
376 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
377 }
378 }
380 /* Received header: */
381 /* At this point because we have to know the rcpts for the 'for' part */
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 /* received locally */
394 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
395 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
396 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
397 } else {
398 /* received from remote */
399 #ifdef ENABLE_IDENT
400 DEBUG(5) debugf("adding 'Received:' header (5)\n");
401 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
402 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
403 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
404 rec_timestamp());
405 #else
406 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
407 msg->received_host, conf.host_name, prot_names[msg->received_prot],
408 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
409 #endif
410 }
411 header_fold(hdr);
412 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
414 if (for_string)
415 g_free(for_string);
417 /* write message to spool: */
418 /* accept is no longer responsible for this
419 if (!spool_write(msg, TRUE))
420 return AERR_NOSPOOL;
421 */
422 return AERR_OK;
423 }
425 accept_error
426 accept_message(FILE * in, message * msg, guint flags)
427 {
428 accept_error err;
430 err = accept_message_stream(in, msg, flags);
431 if (err == AERR_OK)
432 err = accept_message_prepare(msg, flags);
434 return err;
435 }