masqmail-0.2

view src/smtp_in.c @ 135:b072426cc6bb

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