masqmail-0.2

view src/smtp_in.c @ 10:26e34ae9a3e3

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