masqmail

annotate src/smtp_in.c @ 80:e5090ac234cf

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