masqmail-0.2

annotate src/smtp_in.c @ 5:3cafdebd1479

removed remaining reference to docs in Makefile
author meillo@marmaro.de
date Fri, 26 Sep 2008 22:15:26 +0200
parents
children 26e34ae9a3e3
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include "readsock.h"
meillo@0 21
meillo@0 22 /*
meillo@0 23 I always forget these rfc numbers:
meillo@0 24 RFC 821 (SMTP)
meillo@0 25 RFC 1869 (ESMTP)
meillo@0 26 RFC 1870 (ESMTP SIZE)
meillo@0 27 RFC 2197 (ESMTP PIPELINE)
meillo@0 28 RFC 2554 (ESMTP AUTH)
meillo@0 29 */
meillo@0 30
meillo@0 31 #ifdef ENABLE_SMTP_SERVER
meillo@0 32
meillo@0 33 smtp_cmd smtp_cmds[] =
meillo@0 34 {
meillo@0 35 { SMTP_HELO, "HELO", },
meillo@0 36 { SMTP_EHLO, "EHLO", },
meillo@0 37 { SMTP_MAIL_FROM, "MAIL FROM:", },
meillo@0 38 { SMTP_RCPT_TO, "RCPT TO:", },
meillo@0 39 { SMTP_DATA, "DATA", },
meillo@0 40 { SMTP_QUIT, "QUIT", },
meillo@0 41 { SMTP_RSET, "RSET", },
meillo@0 42 { SMTP_NOOP, "NOOP", },
meillo@0 43 { SMTP_HELP, "HELP" },
meillo@0 44 };
meillo@0 45
meillo@0 46 static
meillo@0 47 smtp_cmd_id get_id(const gchar *line)
meillo@0 48 {
meillo@0 49 gint i;
meillo@0 50 for(i = 0; i < SMTP_NUM_IDS; i++){
meillo@0 51 if(strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0)
meillo@0 52 return (smtp_cmd_id)i;
meillo@0 53 }
meillo@0 54 return SMTP_ERROR;
meillo@0 55 }
meillo@0 56
meillo@0 57 /* this is a quick hack: we expect the address to be syntactically correct
meillo@0 58 and containing the mailbox only:
meillo@0 59 */
meillo@0 60
meillo@0 61 static
meillo@0 62 gboolean get_address(gchar *line, gchar *addr)
meillo@0 63 {
meillo@0 64 gchar *p = line, *q = addr;
meillo@0 65
meillo@0 66 /* skip MAIL FROM: and RCPT TO: */
meillo@0 67 while(*p && (*p != ':')) p++;
meillo@0 68 p++;
meillo@0 69
meillo@0 70 /* skip spaces: */
meillo@0 71 while(*p && isspace(*p)) p++;
meillo@0 72
meillo@0 73 /* get address: */
meillo@0 74 while(*p && !isspace(*p) && (q < addr+MAX_ADDRESS-1)) *(q++) = *(p++);
meillo@0 75 *q = 0;
meillo@0 76
meillo@0 77 return TRUE;
meillo@0 78 }
meillo@0 79
meillo@0 80 static
meillo@0 81 smtp_connection *create_base(gchar *remote_host)
meillo@0 82 {
meillo@0 83 smtp_connection *base = g_malloc(sizeof(smtp_connection));
meillo@0 84 if(base){
meillo@0 85 base->remote_host = g_strdup(remote_host);
meillo@0 86
meillo@0 87 base->prot = PROT_SMTP;
meillo@0 88 base->next_id = 0;
meillo@0 89 base->helo_seen = 0;
meillo@0 90 base->from_seen = 0;
meillo@0 91 base->rcpt_seen = 0;
meillo@0 92 base->msg = NULL;
meillo@0 93
meillo@0 94 return base;
meillo@0 95 }
meillo@0 96 return NULL;
meillo@0 97 }
meillo@0 98
meillo@0 99 static
meillo@0 100 void smtp_printf(FILE *out, gchar *fmt, ...)
meillo@0 101 {
meillo@0 102 va_list args;
meillo@0 103 va_start(args, fmt);
meillo@0 104
meillo@0 105 DEBUG(4){
meillo@0 106 gchar buf[256];
meillo@0 107 va_list args_copy;
meillo@0 108
meillo@0 109 va_copy(args_copy, args);
meillo@0 110 vsnprintf(buf, 255, fmt, args_copy);
meillo@0 111 va_end(args_copy);
meillo@0 112
meillo@0 113 debugf(">>>%s", buf);
meillo@0 114 }
meillo@0 115
meillo@0 116 vfprintf(out, fmt, args); fflush(out);
meillo@0 117
meillo@0 118 va_end(args);
meillo@0 119 }
meillo@0 120
meillo@0 121 void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident)
meillo@0 122 {
meillo@0 123 gchar *buffer;
meillo@0 124 smtp_cmd_id cmd_id;
meillo@0 125 message *msg = NULL;
meillo@0 126 smtp_connection *psc;
meillo@0 127 int len;
meillo@0 128
meillo@0 129 DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host);
meillo@0 130
meillo@0 131 psc = create_base(remote_host);
meillo@0 132 psc->msg = msg;
meillo@0 133
meillo@0 134 buffer = (gchar *)g_malloc(BUF_LEN);
meillo@0 135 if(buffer){
meillo@0 136 /* send greeting string, containing ESMTP: */
meillo@0 137 smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n",
meillo@0 138 conf.host_name, VERSION);
meillo@0 139
meillo@0 140 while((len = read_sockline(in, buffer, BUF_LEN, 5*60, READSOCKL_CHUG)) >= 0){
meillo@0 141 cmd_id = get_id(buffer);
meillo@0 142
meillo@0 143 switch(cmd_id){
meillo@0 144 case SMTP_EHLO:
meillo@0 145 psc->prot = PROT_ESMTP;
meillo@0 146 /* fall through */
meillo@0 147 case SMTP_HELO:
meillo@0 148 psc->helo_seen = TRUE;
meillo@0 149
meillo@0 150 if(!conf.defer_all){ /* I need this to debug delivery failures */
meillo@0 151 if(psc->prot == PROT_ESMTP){
meillo@0 152 smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n",
meillo@0 153 conf.host_name);
meillo@0 154 /* not yet: fprintf(out, "250-SIZE\r\n"); */
meillo@0 155 smtp_printf(out,
meillo@0 156 "250-PIPELINING\r\n"
meillo@0 157 "250 HELP\r\n");
meillo@0 158 }else{
meillo@0 159 smtp_printf(out, "250 %s pretty old mailer, huh?\r\n",
meillo@0 160 conf.host_name);
meillo@0 161 }
meillo@0 162 break;
meillo@0 163 }else{
meillo@0 164 smtp_printf(out, "421 %s service temporarily unavailable.\r\n",
meillo@0 165 conf.host_name);
meillo@0 166 }
meillo@0 167
meillo@0 168 case SMTP_MAIL_FROM:
meillo@0 169 if(psc->helo_seen && !psc->from_seen){
meillo@0 170 gchar buf[MAX_ADDRESS];
meillo@0 171 address *addr;
meillo@0 172
meillo@0 173 msg = create_message();
meillo@0 174 msg->received_host = remote_host ? g_strdup(remote_host) : NULL;
meillo@0 175 msg->received_prot = psc->prot;
meillo@0 176 msg->ident = ident ? g_strdup(ident) : NULL;
meillo@0 177 /* get transfer id and increment for next one */
meillo@0 178 msg->transfer_id = (psc->next_id)++;
meillo@0 179
meillo@0 180 get_address(buffer, buf);
meillo@0 181 if((addr = remote_host ?
meillo@0 182 create_address(buf, TRUE) :
meillo@0 183 create_address_qualified(buf, TRUE, conf.host_name))){
meillo@0 184 if(addr->domain != NULL){
meillo@0 185 psc->from_seen = TRUE;
meillo@0 186 msg->return_path = addr;
meillo@0 187 smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address);
meillo@0 188 }else{
meillo@0 189 smtp_printf(out,
meillo@0 190 "501 return path must be qualified.\r\n", buf);
meillo@0 191 }
meillo@0 192 }else{
meillo@0 193 smtp_printf(out, "501 %s: syntax error.\r\n", buf);
meillo@0 194 }
meillo@0 195 }else{
meillo@0 196 if(!psc->helo_seen)
meillo@0 197 smtp_printf(out, "503 need HELO or EHLO\r\n");
meillo@0 198 else
meillo@0 199 smtp_printf(out, "503 MAIL FROM: already given.\r\n");
meillo@0 200 }
meillo@0 201 break;
meillo@0 202
meillo@0 203 case SMTP_RCPT_TO:
meillo@0 204
meillo@0 205 if(psc->helo_seen && psc->from_seen){
meillo@0 206 char buf[MAX_ADDRESS];
meillo@0 207 address *addr;
meillo@0 208
meillo@0 209 get_address(buffer, buf);
meillo@0 210 if((addr = remote_host ?
meillo@0 211 create_address(buf, TRUE) :
meillo@0 212 create_address_qualified(buf, TRUE, conf.host_name))){
meillo@0 213 if(addr->local_part[0] != '|'){
meillo@0 214 if(addr->domain != NULL){
meillo@0 215 gboolean do_relay = conf.do_relay;
meillo@0 216 if(!do_relay){
meillo@0 217 if((do_relay = addr_is_local(msg->return_path)));
meillo@0 218 if(!do_relay){
meillo@0 219 do_relay = addr_is_local(addr);
meillo@0 220 }
meillo@0 221 }
meillo@0 222 if(do_relay){
meillo@0 223 psc->rcpt_seen = TRUE;
meillo@0 224 msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
meillo@0 225 smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address);
meillo@0 226 }else{
meillo@0 227 smtp_printf(out, "550 relaying to %s denied.\r\n",
meillo@0 228 addr_string(addr));
meillo@0 229 }
meillo@0 230 }else{
meillo@0 231 smtp_printf(out,
meillo@0 232 "501 recipient address must be qualified.\r\n", buf);
meillo@0 233 }
meillo@0 234 }else
meillo@0 235 smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf);
meillo@0 236 }else{
meillo@0 237 smtp_printf(out, "501 %s: syntax error in address.\r\n", buf);
meillo@0 238 }
meillo@0 239 }else{
meillo@0 240
meillo@0 241 if(!psc->helo_seen)
meillo@0 242 smtp_printf(out, "503 need HELO or EHLO.\r\n");
meillo@0 243 else
meillo@0 244 smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n");
meillo@0 245 }
meillo@0 246 break;
meillo@0 247
meillo@0 248 case SMTP_DATA:
meillo@0 249 if(psc->helo_seen && psc->rcpt_seen){
meillo@0 250 accept_error err;
meillo@0 251
meillo@0 252 smtp_printf(out, "354 okay, and do not forget the dot\r\n");
meillo@0 253
meillo@0 254 if((err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0))
meillo@0 255 == AERR_OK){
meillo@0 256 if(spool_write(msg, TRUE)){
meillo@0 257 pid_t pid;
meillo@0 258 smtp_printf(out, "250 OK id=%s\r\n", msg->uid);
meillo@0 259
meillo@0 260 if(remote_host != NULL)
meillo@0 261 logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n",
meillo@0 262 msg->uid, msg->return_path->local_part,
meillo@0 263 msg->return_path->domain, remote_host,
meillo@0 264 prot_names[psc->prot]);
meillo@0 265 else
meillo@0 266 logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n",
meillo@0 267 msg->uid, msg->return_path->local_part,
meillo@0 268 msg->return_path->domain,
meillo@0 269 prot_names[psc->prot]);
meillo@0 270
meillo@0 271 if(!conf.do_queue){
meillo@0 272 if((pid = fork()) == 0){
meillo@0 273
meillo@0 274 if(deliver(msg))
meillo@0 275 _exit(EXIT_SUCCESS);
meillo@0 276 else
meillo@0 277 _exit(EXIT_FAILURE);
meillo@0 278
meillo@0 279 }else if(pid < 0){
meillo@0 280 logwrite(LOG_ALERT, "could not fork for delivery, id = %s",
meillo@0 281 msg->uid);
meillo@0 282 }
meillo@0 283 }else{
meillo@0 284 DEBUG(1) debugf("queuing forced by configuration or option.\n");
meillo@0 285 }
meillo@0 286 }else{
meillo@0 287 smtp_printf(out, "451 Could not write spool file\r\n");
meillo@0 288 return;
meillo@0 289 }
meillo@0 290 }else{
meillo@0 291 switch(err){
meillo@0 292 case AERR_TIMEOUT:
meillo@0 293 return;
meillo@0 294 case AERR_EOF:
meillo@0 295 return;
meillo@0 296 default:
meillo@0 297 /* should never happen: */
meillo@0 298 smtp_printf(out, "451 Unknown error\r\n");
meillo@0 299 return;
meillo@0 300 }
meillo@0 301 }
meillo@0 302 psc->rcpt_seen = psc->from_seen = FALSE;
meillo@0 303 destroy_message(msg);
meillo@0 304 msg = NULL;
meillo@0 305 }else{
meillo@0 306 if(!psc->helo_seen)
meillo@0 307 smtp_printf(out, "503 need HELO or EHLO.\r\n");
meillo@0 308 else
meillo@0 309 smtp_printf(out, "503 need RCPT TO: before DATA\r\n");
meillo@0 310 }
meillo@0 311 break;
meillo@0 312 case SMTP_QUIT:
meillo@0 313 smtp_printf(out, "221 goodbye\r\n");
meillo@0 314 if(msg != NULL) destroy_message(msg);
meillo@0 315 return;
meillo@0 316 case SMTP_RSET:
meillo@0 317 psc->from_seen = psc->rcpt_seen = FALSE;
meillo@0 318 if(msg != NULL)
meillo@0 319 destroy_message(msg);
meillo@0 320 msg = NULL;
meillo@0 321 smtp_printf(out, "250 OK\r\n");
meillo@0 322 break;
meillo@0 323 case SMTP_NOOP:
meillo@0 324 smtp_printf(out, "250 OK\r\n");
meillo@0 325 break;
meillo@0 326 case SMTP_HELP:
meillo@0 327 {
meillo@0 328 int i;
meillo@0 329
meillo@0 330 smtp_printf(out, "214-supported commands:\r\n");
meillo@0 331 for(i = 0; i < SMTP_NUM_IDS-1; i++){
meillo@0 332 smtp_printf(out, "214-%s\r\n", smtp_cmds[i].cmd);
meillo@0 333 }
meillo@0 334 smtp_printf(out, "214 %s\r\n", smtp_cmds[i].cmd);
meillo@0 335 }
meillo@0 336 break;
meillo@0 337 default:
meillo@0 338 smtp_printf(out, "501 command not recognized\r\n");
meillo@0 339 DEBUG(1) debugf("command not recognized, was '%s'\n", buffer);
meillo@0 340 break;
meillo@0 341 }
meillo@0 342 }
meillo@0 343 switch(len){
meillo@0 344 case -3:
meillo@0 345 logwrite(LOG_NOTICE, "connection timed out\n");
meillo@0 346 break;
meillo@0 347 case -2:
meillo@0 348 logwrite(LOG_NOTICE, "line overflow\n");
meillo@0 349 break;
meillo@0 350 case -1:
meillo@0 351 logwrite(LOG_NOTICE, "received EOF\n");
meillo@0 352 break;
meillo@0 353 default:
meillo@0 354 break;
meillo@0 355 }
meillo@0 356 }
meillo@0 357 }
meillo@0 358 #endif