masqmail

diff src/smtp_in.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/smtp_in.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,358 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 1999-2001 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include "masqmail.h"
    1.23 +#include "readsock.h"
    1.24 +
    1.25 +/*
    1.26 +  I always forget these rfc numbers:
    1.27 +  RFC 821  (SMTP)
    1.28 +  RFC 1869 (ESMTP)
    1.29 +  RFC 1870 (ESMTP SIZE)
    1.30 +  RFC 2197 (ESMTP PIPELINE)
    1.31 +  RFC 2554 (ESMTP AUTH)
    1.32 +*/
    1.33 +
    1.34 +#ifdef ENABLE_SMTP_SERVER
    1.35 +
    1.36 +smtp_cmd smtp_cmds[] =
    1.37 +{
    1.38 +  { SMTP_HELO, "HELO", },
    1.39 +  { SMTP_EHLO, "EHLO", },
    1.40 +  { SMTP_MAIL_FROM, "MAIL FROM:", },
    1.41 +  { SMTP_RCPT_TO, "RCPT TO:", },
    1.42 +  { SMTP_DATA, "DATA", },
    1.43 +  { SMTP_QUIT, "QUIT", },
    1.44 +  { SMTP_RSET, "RSET", },
    1.45 +  { SMTP_NOOP, "NOOP", },
    1.46 +  { SMTP_HELP, "HELP" },
    1.47 +};
    1.48 +
    1.49 +static
    1.50 +smtp_cmd_id get_id(const gchar *line)
    1.51 +{
    1.52 +  gint i;
    1.53 +  for(i = 0; i < SMTP_NUM_IDS; i++){
    1.54 +    if(strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0)
    1.55 +      return (smtp_cmd_id)i;
    1.56 +  }
    1.57 +  return SMTP_ERROR;
    1.58 +}
    1.59 +
    1.60 +/* this is a quick hack: we expect the address to be syntactically correct
    1.61 +   and containing the mailbox only:
    1.62 +*/
    1.63 +
    1.64 +static
    1.65 +gboolean get_address(gchar *line, gchar *addr)
    1.66 +{
    1.67 +  gchar *p = line, *q = addr;
    1.68 +
    1.69 +  /* skip MAIL FROM: and RCPT TO: */
    1.70 +  while(*p && (*p != ':')) p++;
    1.71 +  p++;
    1.72 +
    1.73 +  /* skip spaces: */
    1.74 +  while(*p && isspace(*p)) p++;
    1.75 +
    1.76 +  /* get address: */
    1.77 +  while(*p && !isspace(*p) && (q < addr+MAX_ADDRESS-1)) *(q++) = *(p++);
    1.78 +  *q = 0;
    1.79 +
    1.80 +  return TRUE;
    1.81 +}
    1.82 +
    1.83 +static
    1.84 +smtp_connection *create_base(gchar *remote_host)
    1.85 +{
    1.86 +  smtp_connection *base = g_malloc(sizeof(smtp_connection));
    1.87 +  if(base){
    1.88 +    base->remote_host = g_strdup(remote_host);
    1.89 +
    1.90 +    base->prot = PROT_SMTP;
    1.91 +    base->next_id = 0;
    1.92 +    base->helo_seen = 0;
    1.93 +    base->from_seen = 0;
    1.94 +    base->rcpt_seen = 0;
    1.95 +    base->msg = NULL;
    1.96 +
    1.97 +    return base;
    1.98 +  }
    1.99 +  return NULL;
   1.100 +}
   1.101 +
   1.102 +static
   1.103 +void smtp_printf(FILE *out, gchar *fmt, ...)
   1.104 +{
   1.105 +  va_list args;
   1.106 +  va_start(args, fmt);
   1.107 +
   1.108 +  DEBUG(4){
   1.109 +    gchar buf[256];
   1.110 +    va_list args_copy;
   1.111 +
   1.112 +    va_copy(args_copy, args);
   1.113 +    vsnprintf(buf, 255, fmt, args_copy);
   1.114 +    va_end(args_copy);
   1.115 +
   1.116 +    debugf(">>>%s", buf);
   1.117 +  }
   1.118 +
   1.119 +  vfprintf(out, fmt, args);  fflush(out);
   1.120 +
   1.121 +  va_end(args);
   1.122 +}
   1.123 +
   1.124 +void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident)
   1.125 +{
   1.126 +  gchar *buffer;
   1.127 +  smtp_cmd_id cmd_id;
   1.128 +  message *msg = NULL;
   1.129 +  smtp_connection *psc;
   1.130 +  int len;
   1.131 +
   1.132 +  DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host);
   1.133 +
   1.134 +  psc = create_base(remote_host);
   1.135 +  psc->msg = msg;
   1.136 +
   1.137 +  buffer = (gchar *)g_malloc(BUF_LEN);
   1.138 +  if(buffer){
   1.139 +    /* send greeting string, containing ESMTP: */
   1.140 +    smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n",
   1.141 +		conf.host_name, VERSION);
   1.142 +    
   1.143 +    while((len = read_sockline(in, buffer, BUF_LEN, 5*60, READSOCKL_CHUG)) >= 0){
   1.144 +      cmd_id = get_id(buffer);
   1.145 +      
   1.146 +      switch(cmd_id){
   1.147 +      case SMTP_EHLO:
   1.148 +	psc->prot = PROT_ESMTP;
   1.149 +	/* fall through */
   1.150 +      case SMTP_HELO:
   1.151 +	psc->helo_seen = TRUE;
   1.152 +
   1.153 +	if(!conf.defer_all){ /* I need this to debug delivery failures */
   1.154 +	  if(psc->prot == PROT_ESMTP){
   1.155 +	    smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n",
   1.156 +			conf.host_name);
   1.157 +	    /* not yet: fprintf(out, "250-SIZE\r\n"); */
   1.158 +	    smtp_printf(out,
   1.159 +			"250-PIPELINING\r\n"
   1.160 +			"250 HELP\r\n");
   1.161 +	  }else{
   1.162 +	    smtp_printf(out, "250 %s pretty old mailer, huh?\r\n",
   1.163 +			conf.host_name);
   1.164 +	  }
   1.165 +	  break;
   1.166 +	}else{
   1.167 +	  smtp_printf(out, "421 %s service temporarily unavailable.\r\n",
   1.168 +		      conf.host_name);
   1.169 +	}
   1.170 +
   1.171 +      case SMTP_MAIL_FROM:
   1.172 +	if(psc->helo_seen && !psc->from_seen){
   1.173 +	  gchar buf[MAX_ADDRESS];
   1.174 +	  address *addr;
   1.175 +
   1.176 +	  msg = create_message();
   1.177 +	  msg->received_host = remote_host ? g_strdup(remote_host) : NULL;
   1.178 +	  msg->received_prot = psc->prot;
   1.179 +	  msg->ident = ident ? g_strdup(ident) : NULL;
   1.180 +	  /* get transfer id and increment for next one */
   1.181 +	  msg->transfer_id = (psc->next_id)++;
   1.182 +
   1.183 +	  get_address(buffer, buf);
   1.184 +	  if((addr = remote_host ?
   1.185 +	     create_address(buf, TRUE) :
   1.186 +	      create_address_qualified(buf, TRUE, conf.host_name))){
   1.187 +	    if(addr->domain != NULL){
   1.188 +	      psc->from_seen = TRUE;
   1.189 +	      msg->return_path = addr;
   1.190 +		smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address);
   1.191 +	    }else{
   1.192 +	      smtp_printf(out,
   1.193 +			  "501 return path must be qualified.\r\n", buf);
   1.194 +	    }
   1.195 +	  }else{
   1.196 +	    smtp_printf(out, "501 %s: syntax error.\r\n", buf);
   1.197 +	  }
   1.198 +	}else{
   1.199 +	  if(!psc->helo_seen)
   1.200 +	    smtp_printf(out, "503 need HELO or EHLO\r\n");
   1.201 +	  else
   1.202 +	    smtp_printf(out, "503 MAIL FROM: already given.\r\n");
   1.203 +	}
   1.204 +	break;
   1.205 +
   1.206 +      case SMTP_RCPT_TO:
   1.207 +
   1.208 +	if(psc->helo_seen && psc->from_seen){
   1.209 +	  char buf[MAX_ADDRESS];
   1.210 +	  address *addr;
   1.211 +
   1.212 +	  get_address(buffer, buf);
   1.213 +	  if((addr = remote_host ?
   1.214 +	      create_address(buf, TRUE) :
   1.215 +	      create_address_qualified(buf, TRUE, conf.host_name))){
   1.216 +	    if(addr->local_part[0] != '|'){
   1.217 +	      if(addr->domain != NULL){
   1.218 +		gboolean do_relay = conf.do_relay;
   1.219 +		if(!do_relay){
   1.220 +		  if((do_relay = addr_is_local(msg->return_path)));
   1.221 +		  if(!do_relay){
   1.222 +		    do_relay = addr_is_local(addr);
   1.223 +		  }
   1.224 +		}
   1.225 +		if(do_relay){
   1.226 +		  psc->rcpt_seen = TRUE;
   1.227 +		  msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
   1.228 +		  smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address);
   1.229 +		}else{
   1.230 +		  smtp_printf(out, "550 relaying to %s denied.\r\n",
   1.231 +			      addr_string(addr));
   1.232 +		}
   1.233 +	      }else{
   1.234 +		smtp_printf(out,
   1.235 +			    "501 recipient address must be qualified.\r\n", buf);
   1.236 +	      }
   1.237 +	    }else
   1.238 +	      smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf);
   1.239 +	  }else{
   1.240 +	    smtp_printf(out, "501 %s: syntax error in address.\r\n", buf);
   1.241 +	  }
   1.242 +	}else{
   1.243 +
   1.244 +	  if(!psc->helo_seen)
   1.245 +	    smtp_printf(out, "503 need HELO or EHLO.\r\n");
   1.246 +	  else
   1.247 +	    smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n");
   1.248 +	}
   1.249 +	break;
   1.250 +	  
   1.251 +      case SMTP_DATA:
   1.252 +	if(psc->helo_seen && psc->rcpt_seen){
   1.253 +	  accept_error err;
   1.254 +
   1.255 +	  smtp_printf(out, "354 okay, and do not forget the dot\r\n");
   1.256 +
   1.257 +	  if((err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0))
   1.258 +	     == AERR_OK){
   1.259 +	    if(spool_write(msg, TRUE)){
   1.260 +	      pid_t pid;
   1.261 +	      smtp_printf(out, "250 OK id=%s\r\n", msg->uid);
   1.262 +	      
   1.263 +	      if(remote_host != NULL)
   1.264 +		logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n",
   1.265 +			 msg->uid, msg->return_path->local_part,
   1.266 +			 msg->return_path->domain, remote_host,
   1.267 +			 prot_names[psc->prot]);
   1.268 +	      else
   1.269 +		logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n",
   1.270 +			 msg->uid, msg->return_path->local_part,
   1.271 +			 msg->return_path->domain,
   1.272 +			 prot_names[psc->prot]);
   1.273 +	      
   1.274 +	      if(!conf.do_queue){
   1.275 +		if((pid = fork()) == 0){
   1.276 +		  
   1.277 +		  if(deliver(msg))
   1.278 +		    _exit(EXIT_SUCCESS);
   1.279 +		  else
   1.280 +		    _exit(EXIT_FAILURE);
   1.281 +		  
   1.282 +		}else if(pid < 0){
   1.283 +		  logwrite(LOG_ALERT, "could not fork for delivery, id = %s",
   1.284 +			   msg->uid);
   1.285 +		}
   1.286 +	      }else{
   1.287 +		DEBUG(1) debugf("queuing forced by configuration or option.\n");
   1.288 +	      }
   1.289 +	    }else{
   1.290 +	      smtp_printf(out, "451 Could not write spool file\r\n");
   1.291 +	      return;
   1.292 +	    }
   1.293 +	  }else{
   1.294 +	    switch(err){
   1.295 +	    case AERR_TIMEOUT:
   1.296 +	      return;
   1.297 +	    case AERR_EOF:
   1.298 +	      return;
   1.299 +	    default:
   1.300 +	      /* should never happen: */
   1.301 +	      smtp_printf(out, "451 Unknown error\r\n");
   1.302 +	      return;
   1.303 +	    }
   1.304 +	  }
   1.305 +	  psc->rcpt_seen = psc->from_seen = FALSE;
   1.306 +	  destroy_message(msg);
   1.307 +	  msg = NULL;
   1.308 +	}else{
   1.309 +	  if(!psc->helo_seen)
   1.310 +	    smtp_printf(out, "503 need HELO or EHLO.\r\n");
   1.311 +	  else
   1.312 +	    smtp_printf(out, "503 need RCPT TO: before DATA\r\n");
   1.313 +	}
   1.314 +	break;
   1.315 +      case SMTP_QUIT:
   1.316 +	smtp_printf(out, "221 goodbye\r\n");
   1.317 +	if(msg != NULL) destroy_message(msg);
   1.318 +	return;
   1.319 +      case SMTP_RSET:
   1.320 +	psc->from_seen = psc->rcpt_seen = FALSE;
   1.321 +	if(msg != NULL)
   1.322 +	  destroy_message(msg);
   1.323 +	msg = NULL;
   1.324 +	smtp_printf(out, "250 OK\r\n");
   1.325 +	break;
   1.326 +      case SMTP_NOOP:
   1.327 +	smtp_printf(out, "250 OK\r\n");
   1.328 +	break;
   1.329 +      case SMTP_HELP:
   1.330 +	{
   1.331 +	  int i;
   1.332 +
   1.333 +	  smtp_printf(out, "214-supported commands:\r\n");
   1.334 +	  for(i = 0; i < SMTP_NUM_IDS-1; i++){
   1.335 +	    smtp_printf(out, "214-%s\r\n", smtp_cmds[i].cmd);
   1.336 +	  }
   1.337 +	  smtp_printf(out, "214 %s\r\n", smtp_cmds[i].cmd);
   1.338 +	}
   1.339 +	break;
   1.340 +      default:
   1.341 +	smtp_printf(out, "501 command not recognized\r\n");
   1.342 +	DEBUG(1) debugf("command not recognized, was '%s'\n", buffer);
   1.343 +	break;
   1.344 +      }
   1.345 +    }
   1.346 +    switch(len){
   1.347 +    case -3:
   1.348 +      logwrite(LOG_NOTICE, "connection timed out\n");
   1.349 +      break;
   1.350 +    case -2:
   1.351 +      logwrite(LOG_NOTICE, "line overflow\n");
   1.352 +      break;
   1.353 +    case -1:
   1.354 +      logwrite(LOG_NOTICE, "received EOF\n");
   1.355 +      break;
   1.356 +    default:
   1.357 +      break;
   1.358 +    }
   1.359 +  }
   1.360 +}
   1.361 +#endif