masqmail-0.2

view src/accept.c @ 3:8c55886cacd8

man pages will be maintained in troff now
author meillo@marmaro.de
date Fri, 26 Sep 2008 21:40:10 +0200
parents
children 26e34ae9a3e3
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 {
24 "local",
25 "bsmtp",
26 "smtp",
27 "esmtp",
28 "pop3",
29 "apop",
30 "(unknown)" /* should not happen, but better than crashing. */
31 };
33 static
34 gchar *string_base62(gchar *res, guint value, gchar len)
35 {
36 static gchar base62_chars[] =
37 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
38 gchar *p = res + len;
39 *p = 0;
40 while (p > res){
41 *(--p) = base62_chars[value % 62];
42 value /= 62;
43 }
44 return res;
45 }
47 static gint _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 If the flags ACC_DEL_RCPTS is set, recipients in the msg->rcpt_list is
63 copied and items occuring in it will be removed from the newly constructed
64 (from To/Cc/Bcc headers if ACC_RCPT_TO is set) rcpt_list.
65 */
67 accept_error 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 }
121 else{
122 if(in_headers){
124 /* some pop servers send the 'From ' line, skip it: */
125 if(msg->hdr_list == NULL)
126 if(strncmp(line1, "From ", 5) == 0)
127 continue;
129 if(line1[0] == ' ' || line1[0] == '\t'){
130 /* continuation of 'folded' header: */
131 if(hdr){
132 hdr->header = 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",
165 line_cnt, data_size);
166 /* we get here after we succesfully
167 received the mail data */
169 msg->data_size = data_size;
170 msg->received_time = time(NULL);
172 return AERR_OK;
173 }
175 accept_error accept_message_prepare(message *msg, guint flags)
176 {
177 struct passwd *passwd = NULL;
178 GList *non_rcpt_list = NULL;
179 time_t rec_time = time(NULL);
181 DEBUG(5) debugf("accept_message_prepare()\n");
183 /* create unique message id */
184 msg->uid = g_malloc(14);
186 string_base62(msg->uid, rec_time, 6);
187 msg->uid[6] = '-';
188 string_base62(&(msg->uid[7]), getpid(), 3);
189 msg->uid[10] = '-';
190 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
191 msg->uid[13] = 0;
193 /* if local, get password entry */
194 if(msg->received_host == NULL){
195 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
196 msg->ident = g_strdup(passwd->pw_name);
197 }
199 /* set return path if local */
200 if(msg->return_path == NULL){
202 if(msg->received_host == NULL){
203 gchar *path = g_strdup_printf("<%s@%s>",
204 passwd->pw_name, conf.host_name);
205 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
206 msg->return_path = create_address(path, TRUE);
207 g_free(path);
208 }
209 }
211 /* -t option */
212 if(flags & ACC_DEL_RCPTS){
213 non_rcpt_list = msg->rcpt_list;
214 msg->rcpt_list = NULL;
215 }
217 /* scan headers */
218 {
219 gboolean has_id = FALSE;
220 gboolean has_date = FALSE;
221 gboolean has_sender = FALSE;
222 gboolean has_from = FALSE;
223 gboolean has_rcpt = FALSE;
224 gboolean has_to_or_cc = FALSE;
225 GList *hdr_node, *hdr_node_next;
226 header *hdr;
228 for(hdr_node = g_list_first(msg->hdr_list);
229 hdr_node != NULL;
230 hdr_node = hdr_node_next){
231 hdr_node_next = g_list_next(hdr_node);
232 hdr = ((header *)(hdr_node->data));
233 DEBUG(5) debugf("scanning headers: %s", hdr->header);
234 switch(hdr->id){
235 case HEAD_MESSAGE_ID:
236 has_id = TRUE; break;
237 case HEAD_DATE:
238 has_date = TRUE; break;
239 case HEAD_FROM:
240 has_from = TRUE;
241 break;
242 case HEAD_SENDER:
243 has_sender = TRUE;
244 break;
245 case HEAD_TO:
246 case HEAD_CC:
247 case HEAD_BCC:
248 has_rcpt = TRUE;
249 if(flags & ACC_RCPT_FROM_HEAD){
250 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
251 if(hdr->value){
252 msg->rcpt_list =
253 addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
254 }
255 }
256 if((flags & ACC_DEL_BCC) && (hdr->id == HEAD_BCC)){
257 DEBUG(3) debugf("removing 'Bcc' header\n");
258 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
259 g_list_free_1(hdr_node);
260 destroy_header(hdr);
261 }else
262 has_to_or_cc = TRUE;
263 break;
264 case HEAD_ENVELOPE_TO:
265 if(flags & ACC_SAVE_ENVELOPE_TO){
266 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
267 msg->hdr_list =
268 g_list_prepend(msg->hdr_list,
269 create_header(HEAD_UNKNOWN,
270 "X-Orig-Envelope-to: %s", hdr->value));
271 }
272 DEBUG(3) debugf("removing 'Envelope-To' header\n");
273 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
274 g_list_free_1(hdr_node);
275 destroy_header(hdr);
276 break;
277 case HEAD_RETURN_PATH:
278 if(flags & ACC_MAIL_FROM_HEAD){
279 /* usually POP3 accept */
280 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
281 DEBUG(3) debugf("setting return_path to %s\n",
282 addr_string(msg->return_path));
283 }
284 DEBUG(3) debugf("removing 'Return-Path' header\n");
285 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
286 g_list_free_1(hdr_node);
287 destroy_header(hdr);
288 break;
289 default:
290 break; /* make compiler happy */
291 }
292 }
294 if(msg->return_path == NULL){
295 /* this can happen for pop3 accept only
296 and if no Return-path: header was given */
297 GList *hdr_list;
298 header *hdr;
300 DEBUG(3) debugf("return_path == NULL\n");
302 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
303 if(!hdr_list) hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
304 if(hdr_list){
305 gchar *addr;
306 hdr = (header *)(g_list_first(hdr_list)->data);
308 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
310 addr = g_strdup(hdr->value);
311 g_strchomp(addr);
313 if((msg->return_path =
314 create_address_qualified(addr, FALSE, msg->received_host))
315 != NULL){
316 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
317 msg->hdr_list =
318 g_list_append(msg->hdr_list,
319 create_header(HEAD_UNKNOWN,
320 "X-Warning: return path set from %s address\n",
321 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
322 }
323 g_free(addr);
324 }
325 if(msg->return_path == NULL){ /* no Sender: or From: or create_address_qualified failed */
326 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
327 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
328 msg->hdr_list =
329 g_list_append(msg->hdr_list,
330 create_header(HEAD_UNKNOWN,
331 "X-Warning: real return path is unkown\n"));
332 }
333 }
335 if(flags & ACC_DEL_RCPTS){
336 GList *rcpt_node;
337 foreach(non_rcpt_list, rcpt_node){
338 address *rcpt = (address *)(rcpt_node->data);
339 GList *node;
340 if((node = g_list_find_custom(msg->rcpt_list, rcpt,
341 _g_list_addr_isequal))){
342 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
344 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
345 destroy_address((address *)(node->data));
346 g_list_free_1(node);
347 }
348 }
349 }
351 /* here we should have our recipients, fail if not: */
352 if(msg->rcpt_list == NULL){
353 logwrite(LOG_WARNING, "no recipients found in message\n");
354 return AERR_NORCPT;
355 }
357 if(!(has_sender || has_from)){
358 DEBUG(3) debugf("adding 'From' header\n");
359 msg->hdr_list =
360 g_list_append(msg->hdr_list,
361 msg->full_sender_name ?
362 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n",
363 msg->full_sender_name,
364 msg->return_path->local_part,
365 msg->return_path->domain) :
366 create_header(HEAD_FROM, "From: <%s@%s>\n",
367 msg->return_path->local_part,
368 msg->return_path->domain)
369 );
370 }
371 if((flags & ACC_HEAD_FROM_RCPT) && !has_rcpt){
372 GList *node;
373 DEBUG(3) debugf("adding 'To' header(s)\n");
374 for(node = g_list_first(msg->rcpt_list);
375 node;
376 node = g_list_next(node)){
377 msg->hdr_list =
378 g_list_append(msg->hdr_list,
379 create_header(HEAD_TO, "To: %s\n", addr_string(msg->return_path)));
380 }
381 }
382 if((flags & ACC_DEL_BCC) && !has_to_or_cc){
383 /* Bcc headers have been removed, and there are no remaining rcpt headers */
384 DEBUG(3) debugf("adding empty 'Bcc:' header\n");
385 msg->hdr_list =
386 g_list_append(msg->hdr_list, create_header(HEAD_BCC, "Bcc:\n"));
387 }
388 if(!has_date){
389 DEBUG(3) debugf("adding 'Date:' header\n");
390 msg->hdr_list =
391 g_list_append(msg->hdr_list,
392 create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
393 }
394 if(!has_id){
395 DEBUG(3) debugf("adding 'Message-ID:' header\n");
396 msg->hdr_list =
397 g_list_append(msg->hdr_list,
398 create_header(HEAD_MESSAGE_ID,
399 "Message-ID: <%s@%s>\n",
400 msg->uid, conf.host_name));
401 }
402 }
404 /* Received header: */
405 /* At this point because we have to know the rcpts for the 'for' part */
406 if(!(flags & ACC_NO_RECVD_HDR)){
407 gchar *for_string = NULL;
408 header *hdr = NULL;
410 DEBUG(3) debugf("adding 'Received:' header\n");
412 if(g_list_length(msg->rcpt_list) == 1){
413 address *addr = (address *)(g_list_first(msg->rcpt_list)->data);
414 for_string = g_strdup_printf(" for %s", addr_string(addr));
415 }
417 if(msg->received_host == NULL){
418 hdr = create_header(HEAD_RECEIVED,
419 "Received: from %s by %s"
420 " with %s (%s %s) id %s%s;"
421 " %s\n",
422 passwd->pw_name, conf.host_name,
423 prot_names[msg->received_prot],
424 PACKAGE, VERSION,
425 msg->uid, for_string ? for_string : "",
426 rec_timestamp());
427 }else{
428 #ifdef ENABLE_IDENT
429 DEBUG(5) debugf("adding 'Received:' header (5)\n");
430 hdr = create_header(HEAD_RECEIVED,
431 "Received: from %s (ident=%s) by %s"
432 " with %s (%s %s) id %s%s;"
433 " %s\n",
434 msg->received_host,
435 msg->ident ? msg->ident : "unknown",
436 conf.host_name,
437 prot_names[msg->received_prot],
438 PACKAGE, VERSION,
439 msg->uid, for_string ? for_string : "",
440 rec_timestamp());
441 #else
442 hdr = create_header(HEAD_RECEIVED,
443 "Received: from %s by %s"
444 " with %s (%s %s) id %s%s;"
445 " %s\n",
446 msg->received_host,
447 conf.host_name,
448 prot_names[msg->received_prot],
449 PACKAGE, VERSION,
450 msg->uid, for_string ? for_string : "",
451 rec_timestamp());
452 #endif
453 }
454 header_fold(hdr);
455 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
457 if(for_string) g_free(for_string);
458 }
460 /* write message to spool: */
461 /* accept is no longer responsible for this
462 if(!spool_write(msg, TRUE))
463 return AERR_NOSPOOL;
464 */
465 return AERR_OK;
466 }
468 accept_error accept_message(FILE *in, message *msg, guint flags)
469 {
470 accept_error err;
472 err = accept_message_stream(in, msg, flags);
473 if(err == AERR_OK)
474 err = accept_message_prepare(msg, flags);
476 return err;
477 }