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@0: smtp_cmd smtp_cmds[] = meillo@0: { meillo@0: { SMTP_HELO, "HELO", }, meillo@0: { SMTP_EHLO, "EHLO", }, meillo@0: { SMTP_MAIL_FROM, "MAIL FROM:", }, meillo@0: { SMTP_RCPT_TO, "RCPT TO:", }, meillo@0: { SMTP_DATA, "DATA", }, meillo@0: { SMTP_QUIT, "QUIT", }, meillo@0: { SMTP_RSET, "RSET", }, meillo@0: { SMTP_NOOP, "NOOP", }, meillo@0: { SMTP_HELP, "HELP" }, meillo@0: }; meillo@0: meillo@0: static meillo@0: smtp_cmd_id get_id(const gchar *line) meillo@0: { meillo@0: gint i; meillo@0: for(i = 0; i < SMTP_NUM_IDS; i++){ meillo@0: if(strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0) meillo@0: return (smtp_cmd_id)i; meillo@0: } meillo@0: 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@0: meillo@0: static meillo@0: gboolean get_address(gchar *line, gchar *addr) meillo@0: { meillo@0: gchar *p = line, *q = addr; meillo@0: meillo@0: /* skip MAIL FROM: and RCPT TO: */ meillo@0: while(*p && (*p != ':')) p++; meillo@0: p++; meillo@0: meillo@0: /* skip spaces: */ meillo@0: while(*p && isspace(*p)) p++; meillo@0: meillo@0: /* get address: */ meillo@0: while(*p && !isspace(*p) && (q < addr+MAX_ADDRESS-1)) *(q++) = *(p++); meillo@0: *q = 0; meillo@0: meillo@0: return TRUE; meillo@0: } meillo@0: meillo@0: static meillo@0: smtp_connection *create_base(gchar *remote_host) meillo@0: { meillo@0: smtp_connection *base = g_malloc(sizeof(smtp_connection)); meillo@0: if(base){ meillo@0: base->remote_host = g_strdup(remote_host); meillo@0: meillo@0: base->prot = PROT_SMTP; meillo@0: base->next_id = 0; meillo@0: base->helo_seen = 0; meillo@0: base->from_seen = 0; meillo@0: base->rcpt_seen = 0; meillo@0: base->msg = NULL; meillo@0: meillo@0: return base; meillo@0: } meillo@0: return NULL; meillo@0: } meillo@0: meillo@0: static meillo@0: void smtp_printf(FILE *out, gchar *fmt, ...) meillo@0: { meillo@0: va_list args; meillo@0: va_start(args, fmt); meillo@0: meillo@0: DEBUG(4){ meillo@0: gchar buf[256]; meillo@0: va_list args_copy; meillo@0: meillo@0: va_copy(args_copy, args); meillo@0: vsnprintf(buf, 255, fmt, args_copy); meillo@0: va_end(args_copy); meillo@0: meillo@0: debugf(">>>%s", buf); meillo@0: } meillo@0: meillo@0: vfprintf(out, fmt, args); fflush(out); meillo@0: meillo@0: va_end(args); meillo@0: } meillo@0: meillo@0: void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident) meillo@0: { meillo@0: gchar *buffer; meillo@0: smtp_cmd_id cmd_id; meillo@0: message *msg = NULL; meillo@0: smtp_connection *psc; meillo@0: int len; meillo@0: meillo@0: DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host); meillo@0: meillo@0: psc = create_base(remote_host); meillo@0: psc->msg = msg; meillo@0: meillo@0: buffer = (gchar *)g_malloc(BUF_LEN); meillo@0: if(buffer){ meillo@0: /* send greeting string, containing ESMTP: */ meillo@0: smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n", meillo@0: conf.host_name, VERSION); meillo@0: meillo@0: while((len = read_sockline(in, buffer, BUF_LEN, 5*60, READSOCKL_CHUG)) >= 0){ meillo@0: cmd_id = get_id(buffer); meillo@0: meillo@0: switch(cmd_id){ meillo@0: case SMTP_EHLO: meillo@0: psc->prot = PROT_ESMTP; meillo@0: /* fall through */ meillo@0: case SMTP_HELO: meillo@0: psc->helo_seen = TRUE; meillo@0: meillo@0: if(!conf.defer_all){ /* I need this to debug delivery failures */ meillo@0: if(psc->prot == PROT_ESMTP){ meillo@0: smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n", meillo@0: conf.host_name); meillo@0: /* not yet: fprintf(out, "250-SIZE\r\n"); */ meillo@0: smtp_printf(out, meillo@0: "250-PIPELINING\r\n" meillo@0: "250 HELP\r\n"); meillo@0: }else{ meillo@0: smtp_printf(out, "250 %s pretty old mailer, huh?\r\n", meillo@0: conf.host_name); meillo@0: } meillo@0: break; meillo@0: }else{ meillo@0: smtp_printf(out, "421 %s service temporarily unavailable.\r\n", meillo@0: conf.host_name); meillo@0: } meillo@0: meillo@0: case SMTP_MAIL_FROM: meillo@0: if(psc->helo_seen && !psc->from_seen){ meillo@0: gchar buf[MAX_ADDRESS]; meillo@0: address *addr; meillo@0: meillo@0: msg = create_message(); meillo@0: msg->received_host = remote_host ? g_strdup(remote_host) : NULL; meillo@0: msg->received_prot = psc->prot; meillo@0: msg->ident = ident ? g_strdup(ident) : NULL; meillo@0: /* get transfer id and increment for next one */ meillo@0: msg->transfer_id = (psc->next_id)++; meillo@0: meillo@0: get_address(buffer, buf); meillo@0: if((addr = remote_host ? meillo@0: create_address(buf, TRUE) : meillo@0: create_address_qualified(buf, TRUE, conf.host_name))){ meillo@0: if(addr->domain != NULL){ meillo@0: psc->from_seen = TRUE; meillo@0: msg->return_path = addr; meillo@0: smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address); meillo@0: }else{ meillo@0: smtp_printf(out, meillo@0: "501 return path must be qualified.\r\n", buf); meillo@0: } meillo@0: }else{ meillo@0: smtp_printf(out, "501 %s: syntax error.\r\n", buf); meillo@0: } meillo@0: }else{ meillo@0: if(!psc->helo_seen) meillo@0: smtp_printf(out, "503 need HELO or EHLO\r\n"); meillo@0: else meillo@0: smtp_printf(out, "503 MAIL FROM: already given.\r\n"); meillo@0: } meillo@0: break; meillo@0: meillo@0: case SMTP_RCPT_TO: meillo@0: meillo@0: if(psc->helo_seen && psc->from_seen){ meillo@0: char buf[MAX_ADDRESS]; meillo@0: address *addr; meillo@0: meillo@0: get_address(buffer, buf); meillo@0: if((addr = remote_host ? meillo@0: create_address(buf, TRUE) : meillo@0: create_address_qualified(buf, TRUE, conf.host_name))){ meillo@0: if(addr->local_part[0] != '|'){ meillo@0: if(addr->domain != NULL){ meillo@0: gboolean do_relay = conf.do_relay; meillo@0: if(!do_relay){ meillo@0: if((do_relay = addr_is_local(msg->return_path))); meillo@0: if(!do_relay){ meillo@0: do_relay = addr_is_local(addr); meillo@0: } meillo@0: } meillo@0: if(do_relay){ meillo@0: psc->rcpt_seen = TRUE; meillo@0: msg->rcpt_list = g_list_append(msg->rcpt_list, addr); meillo@0: smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address); meillo@0: }else{ meillo@0: smtp_printf(out, "550 relaying to %s denied.\r\n", meillo@0: addr_string(addr)); meillo@0: } meillo@0: }else{ meillo@0: smtp_printf(out, meillo@0: "501 recipient address must be qualified.\r\n", buf); meillo@0: } meillo@0: }else meillo@0: smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf); meillo@0: }else{ meillo@0: smtp_printf(out, "501 %s: syntax error in address.\r\n", buf); meillo@0: } meillo@0: }else{ meillo@0: meillo@0: if(!psc->helo_seen) meillo@0: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@0: else meillo@0: smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n"); meillo@0: } meillo@0: break; meillo@0: meillo@0: case SMTP_DATA: meillo@0: if(psc->helo_seen && psc->rcpt_seen){ meillo@0: accept_error err; meillo@0: meillo@0: smtp_printf(out, "354 okay, and do not forget the dot\r\n"); meillo@0: meillo@0: if((err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0)) meillo@0: == AERR_OK){ meillo@0: if(spool_write(msg, TRUE)){ meillo@0: pid_t pid; meillo@0: smtp_printf(out, "250 OK id=%s\r\n", msg->uid); meillo@0: meillo@0: if(remote_host != NULL) meillo@0: logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n", meillo@0: msg->uid, msg->return_path->local_part, meillo@0: msg->return_path->domain, remote_host, meillo@0: prot_names[psc->prot]); meillo@0: else meillo@0: logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n", meillo@0: msg->uid, msg->return_path->local_part, meillo@0: msg->return_path->domain, meillo@0: prot_names[psc->prot]); meillo@0: meillo@0: if(!conf.do_queue){ meillo@0: if((pid = fork()) == 0){ meillo@0: meillo@0: if(deliver(msg)) meillo@0: _exit(EXIT_SUCCESS); meillo@0: else meillo@0: _exit(EXIT_FAILURE); meillo@0: meillo@0: }else if(pid < 0){ meillo@0: logwrite(LOG_ALERT, "could not fork for delivery, id = %s", meillo@0: msg->uid); meillo@0: } meillo@0: }else{ meillo@0: DEBUG(1) debugf("queuing forced by configuration or option.\n"); meillo@0: } meillo@0: }else{ meillo@0: smtp_printf(out, "451 Could not write spool file\r\n"); meillo@0: return; meillo@0: } meillo@0: }else{ meillo@0: switch(err){ meillo@0: case AERR_TIMEOUT: meillo@0: return; meillo@0: case AERR_EOF: meillo@0: return; meillo@0: default: meillo@0: /* should never happen: */ meillo@0: smtp_printf(out, "451 Unknown error\r\n"); meillo@0: return; meillo@0: } meillo@0: } meillo@0: psc->rcpt_seen = psc->from_seen = FALSE; meillo@0: destroy_message(msg); meillo@0: msg = NULL; meillo@0: }else{ meillo@0: if(!psc->helo_seen) meillo@0: smtp_printf(out, "503 need HELO or EHLO.\r\n"); meillo@0: else meillo@0: smtp_printf(out, "503 need RCPT TO: before DATA\r\n"); meillo@0: } meillo@0: break; meillo@0: case SMTP_QUIT: meillo@0: smtp_printf(out, "221 goodbye\r\n"); meillo@0: if(msg != NULL) destroy_message(msg); meillo@0: return; meillo@0: case SMTP_RSET: meillo@0: psc->from_seen = psc->rcpt_seen = FALSE; meillo@0: if(msg != NULL) meillo@0: destroy_message(msg); meillo@0: msg = NULL; meillo@0: smtp_printf(out, "250 OK\r\n"); meillo@0: break; meillo@0: case SMTP_NOOP: meillo@0: smtp_printf(out, "250 OK\r\n"); meillo@0: break; meillo@0: case SMTP_HELP: meillo@0: { meillo@0: int i; meillo@0: meillo@0: smtp_printf(out, "214-supported commands:\r\n"); meillo@0: for(i = 0; i < SMTP_NUM_IDS-1; i++){ meillo@0: smtp_printf(out, "214-%s\r\n", smtp_cmds[i].cmd); meillo@0: } meillo@0: smtp_printf(out, "214 %s\r\n", smtp_cmds[i].cmd); meillo@0: } meillo@0: break; meillo@0: default: meillo@0: smtp_printf(out, "501 command not recognized\r\n"); meillo@0: DEBUG(1) debugf("command not recognized, was '%s'\n", buffer); meillo@0: break; meillo@0: } meillo@0: } meillo@0: switch(len){ meillo@0: case -3: meillo@0: logwrite(LOG_NOTICE, "connection timed out\n"); meillo@0: break; meillo@0: case -2: meillo@0: logwrite(LOG_NOTICE, "line overflow\n"); meillo@0: break; meillo@0: case -1: meillo@0: logwrite(LOG_NOTICE, "received EOF\n"); meillo@0: break; meillo@0: default: meillo@0: break; meillo@0: } meillo@0: } meillo@0: } meillo@0: #endif