meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: 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@0: I always forget these rfc numbers: meillo@0: RFC 821 (SMTP) meillo@0: RFC 1869 (ESMTP) meillo@0: RFC 1870 (ESMTP SIZE) meillo@0: RFC 2197 (ESMTP PIPELINE) meillo@0: RFC 2554 (ESMTP AUTH) meillo@0: */ meillo@0: meillo@0: #ifdef ENABLE_SMTP_SERVER 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@10: get_id(const gchar * line) meillo@0: { meillo@10: gint i; meillo@10: for (i = 0; i < SMTP_NUM_IDS; i++) { meillo@10: if (strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0) meillo@10: return (smtp_cmd_id) i; meillo@10: } meillo@10: return SMTP_ERROR; meillo@0: } meillo@0: meillo@0: /* this is a quick hack: we expect the address to be syntactically correct meillo@0: and containing the mailbox only: meillo@0: */ meillo@10: static gboolean meillo@10: get_address(gchar * line, gchar * addr) meillo@0: { meillo@10: gchar *p = line, *q = addr; meillo@0: meillo@10: /* skip MAIL FROM: and RCPT TO: */ meillo@10: while (*p && (*p != ':')) meillo@10: p++; meillo@10: p++; meillo@0: meillo@10: /* skip spaces: */ meillo@10: while (*p && isspace(*p)) meillo@10: p++; meillo@0: meillo@10: /* get address: */ meillo@10: while (*p && !isspace(*p) && (q < addr + MAX_ADDRESS - 1)) meillo@10: *(q++) = *(p++); meillo@10: *q = 0; meillo@0: meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static smtp_connection* meillo@10: create_base(gchar * remote_host) meillo@0: { meillo@10: smtp_connection *base = g_malloc(sizeof(smtp_connection)); meillo@10: if (base) { meillo@10: base->remote_host = g_strdup(remote_host); meillo@0: meillo@10: base->prot = PROT_SMTP; meillo@10: base->next_id = 0; meillo@10: base->helo_seen = 0; meillo@10: base->from_seen = 0; meillo@10: base->rcpt_seen = 0; meillo@10: base->msg = NULL; meillo@0: meillo@10: return base; meillo@10: } meillo@10: return NULL; meillo@0: } meillo@0: meillo@10: static void meillo@10: 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@10: debugf(">>>%s", 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@10: 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@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@10: if (buffer) { meillo@10: /* send greeting string, containing ESMTP: */ meillo@10: smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n", conf.host_name, VERSION); meillo@0: meillo@10: while ((len = read_sockline(in, buffer, BUF_LEN, 5 * 60, READSOCKL_CHUG)) >= 0) { meillo@10: cmd_id = get_id(buffer); meillo@10: meillo@10: switch (cmd_id) { meillo@10: case SMTP_EHLO: meillo@10: psc->prot = PROT_ESMTP; meillo@10: /* fall through */ meillo@10: case SMTP_HELO: meillo@10: psc->helo_seen = TRUE; meillo@10: meillo@10: if (!conf.defer_all) { /* I need this to debug delivery failures */ meillo@10: if (psc->prot == PROT_ESMTP) { meillo@10: smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n", conf.host_name); meillo@10: /* not yet: fprintf(out, "250-SIZE\r\n"); */ meillo@10: smtp_printf(out, "250-PIPELINING\r\n" "250 HELP\r\n"); meillo@10: } else { meillo@10: smtp_printf(out, "250 %s pretty old mailer, huh?\r\n", conf.host_name); meillo@10: } meillo@10: break; meillo@10: } else { meillo@10: smtp_printf(out, "421 %s service temporarily unavailable.\r\n", conf.host_name); meillo@10: } meillo@10: meillo@10: case SMTP_MAIL_FROM: meillo@10: if (psc->helo_seen && !psc->from_seen) { meillo@10: gchar buf[MAX_ADDRESS]; meillo@10: address *addr; meillo@10: meillo@10: msg = create_message(); meillo@10: msg->received_host = remote_host ? g_strdup(remote_host) : NULL; meillo@10: msg->received_prot = psc->prot; meillo@10: msg->ident = ident ? g_strdup(ident) : NULL; meillo@10: /* get transfer id and increment for next one */ meillo@10: msg->transfer_id = (psc->next_id)++; meillo@10: meillo@10: get_address(buffer, buf); meillo@10: if ((addr = remote_host meillo@10: ? create_address(buf, TRUE) meillo@10: : create_address_qualified(buf, TRUE, conf.host_name))) { meillo@10: if (addr->domain != NULL) { meillo@10: psc->from_seen = TRUE; meillo@10: msg->return_path = addr; meillo@10: smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address); meillo@10: } else { meillo@10: smtp_printf(out, "501 return path must be qualified.\r\n", buf); meillo@10: } meillo@10: } else { meillo@10: smtp_printf(out, "501 %s: syntax error.\r\n", buf); meillo@10: } meillo@10: } else { meillo@10: if (!psc->helo_seen) meillo@10: smtp_printf(out, "503 need HELO or EHLO\r\n"); meillo@10: else meillo@10: smtp_printf(out, "503 MAIL FROM: already given.\r\n"); meillo@10: } meillo@10: break; meillo@10: meillo@10: case SMTP_RCPT_TO: meillo@10: meillo@10: if (psc->helo_seen && psc->from_seen) { meillo@10: char buf[MAX_ADDRESS]; meillo@10: address *addr; meillo@10: meillo@10: get_address(buffer, buf); meillo@10: if ((addr = remote_host meillo@10: ? create_address(buf, TRUE) meillo@10: : create_address_qualified(buf, TRUE, conf.host_name))) { meillo@10: if (addr->local_part[0] != '|') { meillo@10: if (addr->domain != NULL) { meillo@10: gboolean do_relay = conf.do_relay; meillo@10: if (!do_relay) { meillo@10: if ((do_relay = addr_is_local(msg->return_path))) { meillo@10: } meillo@10: if (!do_relay) { meillo@10: do_relay = addr_is_local(addr); meillo@10: } meillo@10: } meillo@10: if (do_relay) { meillo@10: psc->rcpt_seen = TRUE; meillo@10: msg->rcpt_list = g_list_append(msg->rcpt_list, addr); meillo@10: smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address); meillo@10: } else { meillo@10: smtp_printf(out, "550 relaying to %s denied.\r\n", addr_string(addr)); meillo@10: } meillo@10: } else { meillo@10: smtp_printf(out, "501 recipient address must be qualified.\r\n", buf); meillo@10: } meillo@10: } else meillo@10: smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf); meillo@10: } else { meillo@10: smtp_printf(out, "501 %s: syntax error in address.\r\n", buf); meillo@10: } meillo@10: } else { meillo@10: meillo@10: if (!psc->helo_seen) meillo@10: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@10: else meillo@10: smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n"); meillo@10: } meillo@10: break; meillo@10: meillo@10: case SMTP_DATA: meillo@10: if (psc->helo_seen && psc->rcpt_seen) { meillo@10: accept_error err; meillo@10: meillo@10: smtp_printf(out, "354 okay, and do not forget the dot\r\n"); meillo@10: meillo@10: if ((err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0)) == AERR_OK) { meillo@10: if (spool_write(msg, TRUE)) { meillo@10: pid_t pid; meillo@10: smtp_printf(out, "250 OK id=%s\r\n", msg->uid); meillo@10: meillo@10: if (remote_host != NULL) meillo@10: logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n", msg->uid, msg->return_path->local_part, meillo@10: msg->return_path->domain, remote_host, prot_names[psc->prot]); meillo@10: else meillo@10: logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n", msg->uid, msg->return_path->local_part, meillo@10: msg->return_path->domain, prot_names[psc->prot]); meillo@10: meillo@10: if (!conf.do_queue) { meillo@10: if ((pid = fork()) == 0) { meillo@10: meillo@10: if (deliver(msg)) meillo@10: _exit(EXIT_SUCCESS); meillo@10: else meillo@10: _exit(EXIT_FAILURE); meillo@10: meillo@10: } else if (pid < 0) { meillo@10: logwrite(LOG_ALERT, "could not fork for delivery, id = %s", msg->uid); meillo@10: } meillo@10: } else { meillo@10: DEBUG(1) debugf("queuing forced by configuration or option.\n"); meillo@10: } meillo@10: } else { meillo@10: smtp_printf(out, "451 Could not write spool file\r\n"); meillo@10: return; meillo@10: } meillo@10: } else { meillo@10: switch (err) { meillo@10: case AERR_TIMEOUT: meillo@10: return; meillo@10: case AERR_EOF: meillo@10: return; meillo@10: default: meillo@10: /* should never happen: */ meillo@10: smtp_printf(out, "451 Unknown error\r\n"); meillo@10: return; meillo@10: } meillo@10: } meillo@10: psc->rcpt_seen = psc->from_seen = FALSE; meillo@10: destroy_message(msg); meillo@10: msg = NULL; meillo@10: } else { meillo@10: if (!psc->helo_seen) meillo@10: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@10: else meillo@10: smtp_printf(out, "503 need RCPT TO: before DATA\r\n"); meillo@10: } meillo@10: break; meillo@10: case SMTP_QUIT: meillo@10: smtp_printf(out, "221 goodbye\r\n"); meillo@10: if (msg != NULL) meillo@10: destroy_message(msg); meillo@10: return; meillo@10: case SMTP_RSET: meillo@10: psc->from_seen = psc->rcpt_seen = FALSE; meillo@10: if (msg != NULL) meillo@10: destroy_message(msg); meillo@10: msg = NULL; meillo@10: smtp_printf(out, "250 OK\r\n"); meillo@10: break; meillo@10: case SMTP_NOOP: meillo@10: smtp_printf(out, "250 OK\r\n"); meillo@10: break; meillo@10: case SMTP_HELP: meillo@10: { meillo@10: int i; meillo@10: meillo@10: smtp_printf(out, "214-supported commands:\r\n"); meillo@10: for (i = 0; i < SMTP_NUM_IDS - 1; i++) { meillo@10: smtp_printf(out, "214-%s\r\n", smtp_cmds[i].cmd); meillo@10: } meillo@10: smtp_printf(out, "214 %s\r\n", smtp_cmds[i].cmd); meillo@10: } meillo@10: break; meillo@10: default: meillo@10: smtp_printf(out, "501 command not recognized\r\n"); meillo@10: DEBUG(1) debugf("command not recognized, was '%s'\n", buffer); meillo@10: break; meillo@10: } meillo@10: } meillo@10: switch (len) { meillo@10: case -3: meillo@10: logwrite(LOG_NOTICE, "connection timed out\n"); meillo@10: break; meillo@10: case -2: meillo@10: logwrite(LOG_NOTICE, "line overflow\n"); meillo@10: break; meillo@10: case -1: meillo@10: logwrite(LOG_NOTICE, "received EOF\n"); meillo@10: break; meillo@10: default: meillo@10: break; meillo@10: } meillo@0: } meillo@0: } meillo@0: #endif