meillo@367: /* meillo@367: ** MasqMail meillo@367: ** Copyright (C) 1999-2001 Oliver Kurth meillo@367: ** Copyright (C) 2010 markus schnalke meillo@367: ** meillo@367: ** This program is free software; you can redistribute it and/or modify meillo@367: ** it under the terms of the GNU General Public License as published by meillo@367: ** the Free Software Foundation; either version 2 of the License, or meillo@367: ** (at your option) any later version. meillo@367: ** meillo@367: ** This program is distributed in the hope that it will be useful, meillo@367: ** but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@367: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@367: ** GNU General Public License for more details. meillo@367: ** meillo@367: ** You should have received a copy of the GNU General Public License meillo@367: ** along with this program; if not, write to the Free Software meillo@367: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include "masqmail.h" meillo@0: #include "readsock.h" meillo@0: meillo@299: /* must match PROT_* in masqmail.h */ meillo@10: gchar *prot_names[] = { meillo@10: "local", meillo@299: "SMTP", meillo@299: "ESMTP", meillo@10: "(unknown)" /* should not happen, but better than crashing. */ meillo@0: }; meillo@0: meillo@10: static gchar* meillo@366: string_base62(gchar *res, guint value, gchar len) meillo@0: { meillo@388: static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" meillo@388: "abcdefghijklmnopqrstuvwxyz"; meillo@10: gchar *p = res + len; meillo@14: *p = '\0'; meillo@10: while (p > res) { meillo@10: *(--p) = base62_chars[value % 62]; meillo@10: value /= 62; meillo@10: } meillo@10: return res; meillo@0: } meillo@0: meillo@367: /* meillo@367: ** accept message from anywhere. meillo@367: ** A message from local is indicated by msg->recieved_host == NULL meillo@367: ** meillo@367: ** The -t option: With the ACC_RCPT_FROM_HEAD flag the addrs found found meillo@367: ** in To/Cc/Bcc headers are added to the recipient list. meillo@0: */ meillo@10: accept_error meillo@366: accept_message_stream(FILE *in, message *msg, guint flags) meillo@0: { meillo@10: gchar *line, *line1; meillo@10: int line_size = MAX_DATALINE; meillo@10: gboolean in_headers = TRUE; meillo@10: header *hdr = NULL; meillo@10: gint line_cnt = 0, data_size = 0; meillo@0: meillo@10: line = g_malloc(line_size); meillo@388: *line = '\0'; meillo@0: meillo@388: while (1) { meillo@388: int len = read_sockline1(in, &line, &line_size, 5 * 60, meillo@388: READSOCKL_CVT_CRLF); meillo@10: line1 = line; meillo@0: meillo@388: if ((*line == '.') && (!(flags & ACC_DOT_IGNORE))) { meillo@10: if (line[1] == '\n') { meillo@10: g_free(line); meillo@10: break; meillo@10: } meillo@10: line1++; meillo@10: } meillo@10: meillo@388: if (len==-1 && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) { meillo@388: /* at EOF but last line was not terminated by CR */ meillo@388: /* some MUAs allow unterminated lines */ meillo@270: gint len1 = strlen(line1); meillo@388: if (len1 > 0 && line1[len1-1] != '\n') { meillo@388: line1[len1] = '\n'; meillo@388: line1[len1+1] = '\0'; meillo@388: msg->data_list = g_list_prepend(msg->data_list, meillo@388: g_strdup(line1)); meillo@388: data_size += strlen(line1); meillo@388: line_cnt++; meillo@270: } meillo@270: break; meillo@270: meillo@270: } else if (len == -1) { meillo@270: g_free(line); meillo@270: return AERR_EOF; meillo@270: meillo@270: } else if (len == -2) { meillo@270: /* should not happen any more */ meillo@270: g_free(line); meillo@270: return AERR_OVERFLOW; meillo@270: meillo@270: } else if (len == -3) { meillo@270: g_free(line); meillo@270: return AERR_TIMEOUT; meillo@270: meillo@270: } else if (len <= 0) { meillo@270: /* does not happen */ meillo@270: g_free(line); meillo@270: DEBUG(5) debugf("read_sockline returned %d\n", len); meillo@270: return AERR_UNKNOWN; meillo@270: meillo@270: } meillo@270: meillo@270: if (in_headers) { meillo@270: /* some pop servers send the 'From ' line, skip it: */ meillo@388: if (!msg->hdr_list && strncmp(line1, "From ", 5)==0) { meillo@270: continue; meillo@270: } meillo@270: meillo@388: if (*line1 == ' ' || *line1 == '\t') { meillo@270: /* continuation of 'folded' header: */ meillo@270: if (hdr) { meillo@366: char *cp; meillo@388: cp = g_strconcat(hdr->header, line1, meillo@388: NULL); meillo@388: hdr->value = cp + (hdr->value - meillo@388: hdr->header); meillo@323: free(hdr->header); meillo@323: hdr->header = cp; meillo@270: } meillo@388: } else if (*line1 == '\n') { meillo@270: /* an empty line marks end of headers */ meillo@270: in_headers = FALSE; meillo@388: meillo@388: } else if ((hdr = get_header(line1))) { meillo@388: /* another header */ meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, hdr); meillo@10: } else { meillo@388: /* meillo@388: ** Should be another header but none was meillo@388: ** recognized, so this seems to be the first meillo@388: ** data line of a broken mailer which does meillo@388: ** not add an empty line after the headers. meillo@388: */ meillo@388: in_headers = FALSE; meillo@388: msg->data_list = g_list_prepend(msg->data_list, meillo@388: g_strdup(line1)); meillo@10: } meillo@10: } else { meillo@270: /* message body */ meillo@388: msg->data_list = g_list_prepend(msg->data_list, meillo@388: g_strdup(line1)); meillo@270: data_size += strlen(line1); meillo@270: line_cnt++; meillo@270: } meillo@10: meillo@120: if (conf.max_msg_size && (data_size > conf.max_msg_size)) { meillo@117: DEBUG(4) debugf("accept_message_stream(): " meillo@117: "received %d bytes (conf.max_msg_size=%d)\n", meillo@117: data_size, conf.max_msg_size); meillo@117: return AERR_SIZE; meillo@117: } meillo@0: } meillo@388: DEBUG(4) debugf("received %d lines of data (%d bytes)\n", meillo@388: line_cnt, data_size); meillo@270: meillo@270: if (!msg->data_list) { meillo@388: /* make sure data list is not NULL */ meillo@10: msg->data_list = g_list_append(NULL, g_strdup("")); meillo@270: } meillo@270: msg->data_list = g_list_reverse(msg->data_list); meillo@0: meillo@388: /* we have succesfully received the mail data */ meillo@0: meillo@10: msg->data_size = data_size; meillo@10: msg->received_time = time(NULL); meillo@0: meillo@10: return AERR_OK; meillo@0: } meillo@0: meillo@388: static void meillo@388: ensure_return_path(message *msg) meillo@388: { meillo@388: GList *hdr_list; meillo@388: header *hdr; meillo@388: gchar *addr; meillo@388: meillo@388: if (msg->return_path) { meillo@388: return; meillo@388: } meillo@388: meillo@388: DEBUG(3) debugf("return_path == NULL\n"); meillo@388: meillo@388: hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL); meillo@388: if (!hdr_list) { meillo@388: hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL); meillo@388: } meillo@388: if (hdr_list) { meillo@388: hdr = (header *) (g_list_first(hdr_list)->data); meillo@388: meillo@388: DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value); meillo@388: meillo@388: addr = g_strdup(hdr->value); meillo@388: g_strchomp(addr); meillo@388: msg->return_path = create_address_qualified(addr, meillo@388: FALSE, msg->received_host); meillo@388: if (msg->return_path) { meillo@388: DEBUG(3) debugf("setting return_path to %s\n", meillo@388: addr_string(msg->return_path)); meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_UNKNOWN, meillo@388: "X-Warning: return path set from %s " meillo@388: "address\n", meillo@388: (hdr->id == HEAD_SENDER) ? meillo@388: "Sender:" : "From:")); meillo@388: } meillo@388: g_free(addr); meillo@388: } meillo@388: if (!msg->return_path) { meillo@388: /* no Sender: or From: or create_address_qualified failed */ meillo@388: msg->return_path = create_address_qualified("postmaster", meillo@388: TRUE, conf.host_name); meillo@388: DEBUG(3) debugf("setting return_path to %s\n", meillo@388: addr_string(msg->return_path)); meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_UNKNOWN, meillo@388: "X-Warning: real return path is unknown\n")); meillo@388: } meillo@388: } meillo@388: meillo@388: static accept_error meillo@388: scan_headers(message *msg, guint flags) meillo@388: { meillo@388: gboolean has_id = FALSE; meillo@388: gboolean has_date = FALSE; meillo@388: gboolean has_sender = FALSE; meillo@388: gboolean has_from = FALSE; meillo@388: gboolean has_to_or_cc = FALSE; meillo@388: GList *hdr_node, *hdr_node_next; meillo@388: header *hdr; meillo@388: meillo@388: for (hdr_node = g_list_first(msg->hdr_list); hdr_node; meillo@388: hdr_node = hdr_node_next) { meillo@388: hdr_node_next = g_list_next(hdr_node); meillo@388: hdr = ((header *) (hdr_node->data)); meillo@388: DEBUG(5) debugf("scanning headers: %s", hdr->header); meillo@388: switch (hdr->id) { meillo@388: case HEAD_MESSAGE_ID: meillo@388: has_id = TRUE; meillo@388: break; meillo@388: case HEAD_DATE: meillo@388: has_date = TRUE; meillo@388: break; meillo@388: case HEAD_FROM: meillo@388: has_from = TRUE; meillo@388: break; meillo@388: case HEAD_SENDER: meillo@388: has_sender = TRUE; meillo@388: break; meillo@388: case HEAD_TO: meillo@388: case HEAD_CC: meillo@388: has_to_or_cc = TRUE; meillo@388: /* fall through */ meillo@388: case HEAD_BCC: meillo@388: if (flags & ACC_RCPT_FROM_HEAD) { meillo@388: /* -t option (see comment above) */ meillo@388: DEBUG(5) debugf("hdr->value = %s\n", meillo@388: hdr->value); meillo@388: if (hdr->value) { meillo@388: msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name); meillo@388: } meillo@388: } meillo@388: if (hdr->id == HEAD_BCC) { meillo@388: DEBUG(3) debugf("removing 'Bcc' header\n"); meillo@388: msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node); meillo@388: g_list_free_1(hdr_node); meillo@388: destroy_header(hdr); meillo@388: } meillo@388: break; meillo@388: case HEAD_ENVELOPE_TO: meillo@388: if (flags & ACC_SAVE_ENVELOPE_TO) { meillo@388: DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n"); meillo@388: msg->hdr_list = g_list_prepend(msg->hdr_list, meillo@388: create_header(HEAD_UNKNOWN, meillo@388: "X-Orig-Envelope-To: %s", meillo@388: hdr->value)); meillo@388: } meillo@388: DEBUG(3) debugf("removing 'Envelope-To' header\n"); meillo@388: msg->hdr_list = g_list_remove_link(msg->hdr_list, meillo@388: hdr_node); meillo@388: g_list_free_1(hdr_node); meillo@388: destroy_header(hdr); meillo@388: break; meillo@388: case HEAD_RETURN_PATH: meillo@388: if (flags & ACC_MAIL_FROM_HEAD) { meillo@388: /* usually POP3 accept */ meillo@388: msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host); meillo@388: DEBUG(3) debugf("setting return_path to %s\n", meillo@388: addr_string(msg->return_path)); meillo@388: } meillo@388: DEBUG(3) debugf("removing 'Return-Path' header\n"); meillo@388: msg->hdr_list = g_list_remove_link(msg->hdr_list, meillo@388: hdr_node); meillo@388: g_list_free_1(hdr_node); meillo@388: destroy_header(hdr); meillo@388: break; meillo@388: default: meillo@388: break; /* make compiler happy */ meillo@388: } meillo@388: } meillo@388: meillo@388: /* meillo@388: ** TODO: do we still need this as we don't fetch meillo@388: ** mail anymore? meillo@388: ** This can happen for pop3 accept only and if no meillo@388: ** Return-Path: header was given meillo@388: */ meillo@388: ensure_return_path(msg); meillo@388: meillo@388: /* here we should have our recipients, fail if not: */ meillo@388: if (!msg->rcpt_list) { meillo@388: logwrite(LOG_WARNING, "no recipients found in message\n"); meillo@388: return AERR_NORCPT; meillo@388: } meillo@388: meillo@388: if (!has_sender && !has_from) { meillo@388: DEBUG(3) debugf("adding 'From:' header\n"); meillo@388: if (msg->full_sender_name) { meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_FROM, meillo@388: "From: \"%s\" <%s@%s>\n", meillo@388: msg->full_sender_name, meillo@388: msg->return_path->local_part, meillo@388: msg->return_path->domain)); meillo@388: } else { meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_FROM, meillo@388: "From: <%s@%s>\n", meillo@388: msg->return_path->local_part, meillo@388: msg->return_path->domain)); meillo@388: } meillo@388: } meillo@388: if (!has_to_or_cc) { meillo@388: DEBUG(3) debugf("no To: or Cc: header, hence adding " meillo@388: "`To: undisclosed recipients:;'\n"); meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_TO, meillo@388: "To: undisclosed-recipients:;\n")); meillo@388: } meillo@388: if (!has_date) { meillo@388: DEBUG(3) debugf("adding 'Date:' header\n"); meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_DATE, "Date: %s\n", meillo@388: rec_timestamp())); meillo@388: } meillo@388: if (!has_id) { meillo@388: DEBUG(3) debugf("adding 'Message-ID:' header\n"); meillo@388: msg->hdr_list = g_list_append(msg->hdr_list, meillo@388: create_header(HEAD_MESSAGE_ID, meillo@388: "Message-ID: <%s@%s>\n", meillo@388: msg->uid, conf.host_name)); meillo@388: } meillo@388: meillo@388: return AERR_OK; meillo@388: } meillo@388: meillo@388: static void meillo@388: add_received_hdr(message *msg) meillo@388: { meillo@388: gchar *for_string = NULL; meillo@388: header *hdr = NULL; meillo@388: address *addr; meillo@388: meillo@388: DEBUG(3) debugf("adding 'Received:' header\n"); meillo@388: if (g_list_length(msg->rcpt_list) == 1) { meillo@388: /* The `for' part only if exactly one rcpt is present */ meillo@388: addr = (address *) (g_list_first(msg->rcpt_list)->data); meillo@388: for_string = g_strdup_printf("\n\tfor %s", addr_string(addr)); meillo@388: } meillo@388: if (!msg->received_host) { meillo@388: /* received locally */ meillo@388: hdr = create_header(HEAD_RECEIVED, meillo@388: "Received: by %s (%s %s, from userid %d)\n" meillo@388: "\tid %s%s; %s\n", meillo@388: conf.host_name, PACKAGE, VERSION, geteuid(), meillo@388: msg->uid, meillo@388: for_string ? for_string : "", rec_timestamp()); meillo@388: } else { meillo@388: /* received from remote */ meillo@388: DEBUG(5) debugf("adding 'Received:' header (5)\n"); meillo@388: hdr = create_header(HEAD_RECEIVED, meillo@388: "Received: from %s\n" meillo@388: "\tby %s with %s (%s %s)\n" meillo@388: "\tid %s%s; %s\n", meillo@388: msg->received_host, conf.host_name, meillo@388: prot_names[msg->received_prot], PACKAGE, meillo@388: VERSION, msg->uid, meillo@388: for_string ? for_string : "", rec_timestamp()); meillo@388: } meillo@388: msg->hdr_list = g_list_prepend(msg->hdr_list, hdr); meillo@388: if (for_string) { meillo@388: g_free(for_string); meillo@388: } meillo@388: } meillo@388: meillo@10: accept_error meillo@366: accept_message_prepare(message *msg, guint flags) meillo@0: { meillo@10: DEBUG(5) debugf("accept_message_prepare()\n"); meillo@0: meillo@388: /* generate unique message id */ meillo@10: msg->uid = g_malloc(14); meillo@388: string_base62(msg->uid, time(NULL), 6); meillo@10: msg->uid[6] = '-'; meillo@298: string_base62(msg->uid + 7, getpid(), 3); meillo@10: msg->uid[10] = '-'; meillo@298: string_base62(msg->uid + 11, msg->transfer_id, 2); meillo@298: msg->uid[13] = '\0'; meillo@0: meillo@298: /* if local, get password entry and set return path if missing */ meillo@298: if (!msg->received_host) { meillo@388: struct passwd *passwd = NULL; meillo@388: meillo@10: passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd)); meillo@10: msg->ident = g_strdup(passwd->pw_name); meillo@298: if (!msg->return_path) { meillo@388: gchar *path = g_strdup_printf("<%s@%s>", meillo@388: passwd->pw_name, conf.host_name); meillo@388: DEBUG(3) debugf("setting return_path for local " meillo@388: "accept: %s\n", path); meillo@298: msg->return_path = create_address(path, TRUE); meillo@298: g_free(path); meillo@298: } meillo@10: } meillo@0: meillo@388: if (scan_headers(msg, flags) == AERR_NORCPT) { meillo@388: return AERR_NORCPT; meillo@0: } meillo@10: meillo@388: /* after the hdrs are scanned because we need to know the rcpts */ meillo@388: add_received_hdr(msg); meillo@0: meillo@10: return AERR_OK; meillo@0: } meillo@0: meillo@10: accept_error meillo@366: accept_message(FILE *in, message *msg, guint flags) meillo@0: { meillo@10: accept_error err; meillo@0: meillo@10: err = accept_message_stream(in, msg, flags); meillo@270: if (err == AERR_OK) { meillo@10: err = accept_message_prepare(msg, flags); meillo@270: } meillo@0: meillo@10: return err; meillo@0: }