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@0: /* meillo@367: ** I always forget these rfc numbers: meillo@367: ** RFC 821 (SMTP) meillo@367: ** RFC 1869 (ESMTP) meillo@367: ** RFC 1870 (ESMTP SIZE) meillo@367: ** RFC 2197 (ESMTP PIPELINE) meillo@367: ** RFC 2554 (ESMTP AUTH) meillo@0: */ meillo@0: meillo@0: meillo@10: smtp_cmd smtp_cmds[] = { meillo@15: {SMTP_HELO, "HELO",}, meillo@15: {SMTP_EHLO, "EHLO",}, meillo@15: {SMTP_MAIL_FROM, "MAIL FROM:",}, meillo@15: {SMTP_RCPT_TO, "RCPT TO:",}, meillo@15: {SMTP_DATA, "DATA",}, meillo@15: {SMTP_QUIT, "QUIT",}, meillo@15: {SMTP_RSET, "RSET",}, meillo@15: {SMTP_NOOP, "NOOP",}, meillo@15: {SMTP_HELP, "HELP"}, meillo@0: }; meillo@0: meillo@10: static smtp_cmd_id meillo@366: get_id(const gchar *line) meillo@0: { meillo@10: gint i; meillo@10: for (i = 0; i < SMTP_NUM_IDS; i++) { meillo@80: if (strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0) { meillo@10: return (smtp_cmd_id) i; meillo@80: } meillo@10: } meillo@10: return SMTP_ERROR; meillo@0: } meillo@0: meillo@117: static gboolean meillo@117: get_size(gchar *line, unsigned long *msize) { meillo@117: gchar *s = NULL; meillo@117: meillo@117: /* hope we need not to handle cases like SiZe= ...*/ meillo@117: s = strstr(line, "SIZE="); meillo@117: if (!s) { meillo@117: /* try it in lowercase too */ meillo@117: if (!(s = strstr(line, "size="))) { meillo@117: return FALSE; meillo@117: } meillo@117: } meillo@117: s += 5; meillo@117: *msize = atol(s); meillo@117: DEBUG(5) debugf("get_size(): line=%s, msize=%ld\n", line, *msize); meillo@117: meillo@117: return TRUE; meillo@117: } meillo@117: meillo@117: meillo@367: /* meillo@367: ** this is a quick hack: we expect the address to be syntactically correct meillo@367: ** and containing the mailbox only, though we first check for size in meillo@367: ** smtp_in(). meillo@367: ** Return false if address is too long. meillo@0: */ meillo@10: static gboolean meillo@366: get_address(gchar *line, gchar *addr) meillo@0: { meillo@80: gchar *p = line; meillo@80: gchar *q = addr; meillo@0: meillo@10: /* skip MAIL FROM: and RCPT TO: */ meillo@80: while (*p && (*p != ':')) { meillo@10: p++; meillo@80: } meillo@10: p++; meillo@0: meillo@10: /* skip spaces: */ meillo@80: while (*p && isspace(*p)) { meillo@10: p++; meillo@80: } meillo@0: meillo@10: /* get address: */ meillo@136: while (*p && !isspace(*p)) { meillo@136: if (q >= addr + MAX_ADDRESS-1) { meillo@136: *q = '\0'; meillo@136: return FALSE; meillo@136: } meillo@10: *(q++) = *(p++); meillo@80: } meillo@136: *q = '\0'; meillo@0: meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static smtp_connection* meillo@366: create_base(gchar *remote_host) meillo@0: { meillo@10: smtp_connection *base = g_malloc(sizeof(smtp_connection)); meillo@80: if (!base) { meillo@80: return NULL; meillo@80: } meillo@0: meillo@80: base->remote_host = g_strdup(remote_host); meillo@0: meillo@80: base->prot = PROT_SMTP; meillo@80: base->next_id = 0; meillo@80: base->helo_seen = 0; meillo@80: base->from_seen = 0; meillo@80: base->rcpt_seen = 0; meillo@80: base->msg = NULL; meillo@80: meillo@80: return base; meillo@0: } meillo@0: meillo@10: static void meillo@366: smtp_printf(FILE *out, gchar *fmt, ...) meillo@0: { meillo@10: va_list args; meillo@10: va_start(args, fmt); meillo@0: meillo@10: DEBUG(4) { meillo@10: gchar buf[256]; meillo@10: va_list args_copy; meillo@0: meillo@10: va_copy(args_copy, args); meillo@10: vsnprintf(buf, 255, fmt, args_copy); meillo@10: va_end(args_copy); meillo@0: meillo@208: debugf(">>>%s\n", buf); meillo@10: } meillo@0: meillo@10: vfprintf(out, fmt, args); meillo@10: fflush(out); meillo@0: meillo@10: va_end(args); meillo@0: } meillo@0: meillo@10: void meillo@366: smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident) meillo@0: { meillo@10: gchar *buffer; meillo@10: smtp_cmd_id cmd_id; meillo@10: message *msg = NULL; meillo@10: smtp_connection *psc; meillo@10: int len; meillo@117: unsigned long size, msize; meillo@0: meillo@10: DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host); meillo@0: meillo@10: psc = create_base(remote_host); meillo@10: psc->msg = msg; meillo@0: meillo@10: buffer = (gchar *) g_malloc(BUF_LEN); meillo@80: if (!buffer) { meillo@80: /* this check is actually unneccessary as g_malloc() meillo@80: aborts on failure */ meillo@80: return; meillo@80: } meillo@0: meillo@80: /* send greeting string, containing ESMTP: */ meillo@80: smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n", conf.host_name, VERSION); meillo@10: meillo@80: while ((len = read_sockline(in, buffer, BUF_LEN, 5 * 60, READSOCKL_CHUG)) >= 0) { meillo@80: cmd_id = get_id(buffer); meillo@10: meillo@135: if (conf.defer_all) { meillo@135: /* I need this to debug delivery failures */ meillo@127: smtp_printf(out, "421 %s service temporarily unavailable.\r\n", conf.host_name); meillo@135: destroy_message(msg); meillo@135: msg = NULL; meillo@135: return; meillo@127: } meillo@127: meillo@80: switch (cmd_id) { meillo@127: case SMTP_HELO: meillo@127: psc->prot = PROT_SMTP; meillo@127: psc->helo_seen = TRUE; meillo@127: smtp_printf(out, "250 %s pretty old mailer, huh?\r\n", conf.host_name); meillo@127: break; meillo@127: meillo@80: case SMTP_EHLO: meillo@80: psc->prot = PROT_ESMTP; meillo@80: psc->helo_seen = TRUE; meillo@127: smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n", conf.host_name); meillo@127: smtp_printf(out, "250-SIZE %d\r\n", conf.max_msg_size); meillo@127: smtp_printf(out, "250-PIPELINING\r\n"); meillo@127: smtp_printf(out, "250 HELP\r\n"); meillo@10: break; meillo@80: meillo@80: case SMTP_MAIL_FROM: meillo@80: { meillo@80: gchar buf[MAX_ADDRESS]; meillo@80: address *addr; meillo@80: meillo@80: if (!psc->helo_seen) { meillo@80: smtp_printf(out, "503 need HELO or EHLO\r\n"); meillo@80: break; meillo@80: } meillo@80: if (psc->from_seen) { meillo@80: smtp_printf(out, "503 MAIL FROM: already given.\r\n"); meillo@80: break; meillo@80: } meillo@128: if (get_size(buffer, &msize)) { meillo@128: DEBUG(5) debugf("smtp_in(): get_size: msize=%ld, conf.mms=%d\n", meillo@128: msize, conf.max_msg_size); meillo@128: if (conf.max_msg_size && (msize > conf.max_msg_size)) { meillo@128: smtp_printf(out, "552 Message size exceeds fixed limit.\r\n"); meillo@128: break; meillo@128: } meillo@128: } meillo@136: if (!get_address(buffer, buf)) { meillo@136: smtp_printf(out, "553 Address too long.\r\n"); meillo@136: break; meillo@136: } meillo@128: meillo@80: msg = create_message(); meillo@80: msg->received_host = remote_host ? g_strdup(remote_host) : NULL; meillo@80: msg->received_prot = psc->prot; meillo@80: msg->ident = ident ? g_strdup(ident) : NULL; meillo@80: /* get transfer id and increment for next one */ meillo@80: msg->transfer_id = (psc->next_id)++; meillo@80: meillo@80: if (remote_host) { meillo@80: addr = create_address(buf, TRUE); meillo@80: } else { meillo@80: addr = create_address_qualified(buf, TRUE, conf.host_name); meillo@80: } meillo@80: if (!addr) { meillo@80: smtp_printf(out, "501 %s: syntax error.\r\n", buf); meillo@80: } else if (!addr->domain) { meillo@80: smtp_printf(out, "501 return path must be qualified.\r\n", buf); meillo@80: } else { meillo@80: psc->from_seen = TRUE; meillo@80: msg->return_path = addr; meillo@80: smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address); meillo@80: } meillo@80: } meillo@10: break; meillo@80: meillo@80: case SMTP_RCPT_TO: meillo@80: { meillo@80: char buf[MAX_ADDRESS]; meillo@80: address *addr; meillo@80: meillo@80: if (!psc->helo_seen) { meillo@80: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@80: break; meillo@80: } meillo@80: if (!psc->from_seen) { meillo@80: smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n"); meillo@80: break; meillo@80: } meillo@136: if (!get_address(buffer, buf)) { meillo@136: smtp_printf(out, "553 Address too long.\r\n"); meillo@136: break; meillo@136: } meillo@80: meillo@80: if (remote_host) { meillo@80: addr = create_address(buf, TRUE); meillo@80: } else { meillo@80: addr = create_address_qualified(buf, TRUE, conf.host_name); meillo@80: } meillo@80: if (!addr) { meillo@80: smtp_printf(out, "501 %s: syntax error in address.\r\n", buf); meillo@80: break; meillo@80: } meillo@80: if (addr->local_part[0] == '|') { meillo@80: smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf); meillo@80: break; meillo@80: } meillo@80: if (!addr->domain) { meillo@305: /* TODO: ``postmaster'' may be unqualified */ meillo@80: smtp_printf(out, "501 recipient address must be qualified.\r\n", buf); meillo@80: break; meillo@80: } meillo@305: if (!(conf.do_relay || addr_is_local(msg->return_path) || addr_is_local(addr))) { meillo@80: smtp_printf(out, "550 relaying to %s denied.\r\n", addr_string(addr)); meillo@80: break; meillo@80: } meillo@80: psc->rcpt_seen = TRUE; meillo@80: msg->rcpt_list = g_list_append(msg->rcpt_list, addr); meillo@80: smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address); meillo@80: } meillo@10: break; meillo@80: meillo@80: case SMTP_DATA: meillo@80: if (!psc->helo_seen) { meillo@80: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@80: break; meillo@80: } meillo@80: if (!psc->rcpt_seen) { meillo@80: smtp_printf(out, "503 need RCPT TO: before DATA\r\n"); meillo@80: break; meillo@80: } meillo@80: accept_error err; meillo@80: meillo@80: smtp_printf(out, "354 okay, and do not forget the dot\r\n"); meillo@80: meillo@80: err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0); meillo@80: if (err != AERR_OK) { meillo@117: switch (err) { meillo@117: case AERR_TIMEOUT: meillo@117: case AERR_EOF: meillo@117: return; meillo@117: case AERR_SIZE: meillo@117: smtp_printf(out, "552 Error: message too large.\r\n"); meillo@117: return; meillo@117: default: meillo@117: /* should never happen: */ meillo@117: smtp_printf(out, "451 Unknown error\r\n"); meillo@80: return; meillo@80: } meillo@80: } meillo@80: meillo@80: meillo@80: if (!spool_write(msg, TRUE)) { meillo@80: smtp_printf(out, "451 Could not write spool file\r\n"); meillo@80: return; meillo@80: } meillo@80: pid_t pid; meillo@80: smtp_printf(out, "250 OK id=%s\r\n", msg->uid); meillo@80: meillo@80: if (remote_host != NULL) { meillo@80: logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n", msg->uid, meillo@80: msg->return_path->local_part, msg->return_path->domain, meillo@80: remote_host, prot_names[psc->prot]); meillo@80: } else { meillo@80: logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n", msg->uid, meillo@80: msg->return_path->local_part, msg->return_path->domain, meillo@80: prot_names[psc->prot]); meillo@80: } meillo@80: meillo@80: if (conf.do_queue) { meillo@80: DEBUG(1) debugf("queuing forced by configuration or option.\n"); meillo@80: } else { meillo@80: pid = fork(); meillo@347: if (pid < 0) { meillo@347: logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n", msg->uid); meillo@347: } else if (pid == 0) { meillo@347: /* FIXME: most likely inverted exit code */ meillo@80: _exit(deliver(msg)); meillo@80: } meillo@80: } meillo@80: psc->rcpt_seen = psc->from_seen = FALSE; meillo@80: destroy_message(msg); meillo@80: msg = NULL; meillo@80: break; meillo@80: meillo@80: case SMTP_QUIT: meillo@80: smtp_printf(out, "221 goodbye\r\n"); meillo@81: destroy_message(msg); meillo@81: msg = NULL; meillo@80: return; meillo@80: meillo@80: case SMTP_RSET: meillo@80: psc->from_seen = psc->rcpt_seen = FALSE; meillo@81: destroy_message(msg); meillo@81: msg = NULL; meillo@80: smtp_printf(out, "250 OK\r\n"); meillo@80: break; meillo@80: meillo@80: case SMTP_NOOP: meillo@80: smtp_printf(out, "250 OK\r\n"); meillo@80: break; meillo@80: meillo@80: case SMTP_HELP: meillo@80: { meillo@80: int i; meillo@80: meillo@80: smtp_printf(out, "214-supported commands:\r\n"); meillo@80: for (i = 0; i < SMTP_NUM_IDS - 1; i++) { meillo@80: smtp_printf(out, "214-%s\r\n", smtp_cmds[i].cmd); meillo@80: } meillo@80: smtp_printf(out, "214 %s\r\n", smtp_cmds[i].cmd); meillo@80: } meillo@80: break; meillo@80: meillo@10: default: meillo@80: smtp_printf(out, "501 command not recognized\r\n"); meillo@80: DEBUG(1) debugf("command not recognized, was '%s'\n", buffer); meillo@10: break; meillo@10: } meillo@0: } meillo@80: switch (len) { meillo@80: case -3: meillo@80: logwrite(LOG_NOTICE, "connection timed out\n"); meillo@80: break; meillo@80: case -2: meillo@80: logwrite(LOG_NOTICE, "line overflow\n"); meillo@80: break; meillo@80: case -1: meillo@80: logwrite(LOG_NOTICE, "received EOF\n"); meillo@80: break; meillo@80: default: meillo@80: break; meillo@80: } meillo@0: }