masqmail-0.2

view 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 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 {
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
47 smtp_cmd_id 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 return SMTP_ERROR;
55 }
57 /* this is a quick hack: we expect the address to be syntactically correct
58 and containing the mailbox only:
59 */
61 static
62 gboolean get_address(gchar *line, gchar *addr)
63 {
64 gchar *p = line, *q = addr;
66 /* skip MAIL FROM: and RCPT TO: */
67 while(*p && (*p != ':')) p++;
68 p++;
70 /* skip spaces: */
71 while(*p && isspace(*p)) p++;
73 /* get address: */
74 while(*p && !isspace(*p) && (q < addr+MAX_ADDRESS-1)) *(q++) = *(p++);
75 *q = 0;
77 return TRUE;
78 }
80 static
81 smtp_connection *create_base(gchar *remote_host)
82 {
83 smtp_connection *base = g_malloc(sizeof(smtp_connection));
84 if(base){
85 base->remote_host = g_strdup(remote_host);
87 base->prot = PROT_SMTP;
88 base->next_id = 0;
89 base->helo_seen = 0;
90 base->from_seen = 0;
91 base->rcpt_seen = 0;
92 base->msg = NULL;
94 return base;
95 }
96 return NULL;
97 }
99 static
100 void smtp_printf(FILE *out, gchar *fmt, ...)
101 {
102 va_list args;
103 va_start(args, fmt);
105 DEBUG(4){
106 gchar buf[256];
107 va_list args_copy;
109 va_copy(args_copy, args);
110 vsnprintf(buf, 255, fmt, args_copy);
111 va_end(args_copy);
113 debugf(">>>%s", buf);
114 }
116 vfprintf(out, fmt, args); fflush(out);
118 va_end(args);
119 }
121 void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident)
122 {
123 gchar *buffer;
124 smtp_cmd_id cmd_id;
125 message *msg = NULL;
126 smtp_connection *psc;
127 int len;
129 DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host);
131 psc = create_base(remote_host);
132 psc->msg = msg;
134 buffer = (gchar *)g_malloc(BUF_LEN);
135 if(buffer){
136 /* send greeting string, containing ESMTP: */
137 smtp_printf(out, "220 %s MasqMail %s ESMTP\r\n",
138 conf.host_name, VERSION);
140 while((len = read_sockline(in, buffer, BUF_LEN, 5*60, READSOCKL_CHUG)) >= 0){
141 cmd_id = get_id(buffer);
143 switch(cmd_id){
144 case SMTP_EHLO:
145 psc->prot = PROT_ESMTP;
146 /* fall through */
147 case SMTP_HELO:
148 psc->helo_seen = TRUE;
150 if(!conf.defer_all){ /* I need this to debug delivery failures */
151 if(psc->prot == PROT_ESMTP){
152 smtp_printf(out, "250-%s Nice to meet you with ESMTP\r\n",
153 conf.host_name);
154 /* not yet: fprintf(out, "250-SIZE\r\n"); */
155 smtp_printf(out,
156 "250-PIPELINING\r\n"
157 "250 HELP\r\n");
158 }else{
159 smtp_printf(out, "250 %s pretty old mailer, huh?\r\n",
160 conf.host_name);
161 }
162 break;
163 }else{
164 smtp_printf(out, "421 %s service temporarily unavailable.\r\n",
165 conf.host_name);
166 }
168 case SMTP_MAIL_FROM:
169 if(psc->helo_seen && !psc->from_seen){
170 gchar buf[MAX_ADDRESS];
171 address *addr;
173 msg = create_message();
174 msg->received_host = remote_host ? g_strdup(remote_host) : NULL;
175 msg->received_prot = psc->prot;
176 msg->ident = ident ? g_strdup(ident) : NULL;
177 /* get transfer id and increment for next one */
178 msg->transfer_id = (psc->next_id)++;
180 get_address(buffer, buf);
181 if((addr = remote_host ?
182 create_address(buf, TRUE) :
183 create_address_qualified(buf, TRUE, conf.host_name))){
184 if(addr->domain != NULL){
185 psc->from_seen = TRUE;
186 msg->return_path = addr;
187 smtp_printf(out, "250 OK %s is a nice guy.\r\n", addr->address);
188 }else{
189 smtp_printf(out,
190 "501 return path must be qualified.\r\n", buf);
191 }
192 }else{
193 smtp_printf(out, "501 %s: syntax error.\r\n", buf);
194 }
195 }else{
196 if(!psc->helo_seen)
197 smtp_printf(out, "503 need HELO or EHLO\r\n");
198 else
199 smtp_printf(out, "503 MAIL FROM: already given.\r\n");
200 }
201 break;
203 case SMTP_RCPT_TO:
205 if(psc->helo_seen && psc->from_seen){
206 char buf[MAX_ADDRESS];
207 address *addr;
209 get_address(buffer, buf);
210 if((addr = remote_host ?
211 create_address(buf, TRUE) :
212 create_address_qualified(buf, TRUE, conf.host_name))){
213 if(addr->local_part[0] != '|'){
214 if(addr->domain != NULL){
215 gboolean do_relay = conf.do_relay;
216 if(!do_relay){
217 if((do_relay = addr_is_local(msg->return_path)));
218 if(!do_relay){
219 do_relay = addr_is_local(addr);
220 }
221 }
222 if(do_relay){
223 psc->rcpt_seen = TRUE;
224 msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
225 smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address);
226 }else{
227 smtp_printf(out, "550 relaying to %s denied.\r\n",
228 addr_string(addr));
229 }
230 }else{
231 smtp_printf(out,
232 "501 recipient address must be qualified.\r\n", buf);
233 }
234 }else
235 smtp_printf(out, "501 %s: no pipe allowed for SMTP connections\r\n", buf);
236 }else{
237 smtp_printf(out, "501 %s: syntax error in address.\r\n", buf);
238 }
239 }else{
241 if(!psc->helo_seen)
242 smtp_printf(out, "503 need HELO or EHLO.\r\n");
243 else
244 smtp_printf(out, "503 need MAIL FROM: before RCPT TO:\r\n");
245 }
246 break;
248 case SMTP_DATA:
249 if(psc->helo_seen && psc->rcpt_seen){
250 accept_error err;
252 smtp_printf(out, "354 okay, and do not forget the dot\r\n");
254 if((err = accept_message(in, msg, conf.do_save_envelope_to ? ACC_SAVE_ENVELOPE_TO : 0))
255 == AERR_OK){
256 if(spool_write(msg, TRUE)){
257 pid_t pid;
258 smtp_printf(out, "250 OK id=%s\r\n", msg->uid);
260 if(remote_host != NULL)
261 logwrite(LOG_NOTICE, "%s <= <%s@%s> host=%s with %s\n",
262 msg->uid, msg->return_path->local_part,
263 msg->return_path->domain, remote_host,
264 prot_names[psc->prot]);
265 else
266 logwrite(LOG_NOTICE, "%s <= <%s@%s> with %s\n",
267 msg->uid, msg->return_path->local_part,
268 msg->return_path->domain,
269 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",
281 msg->uid);
282 }
283 }else{
284 DEBUG(1) debugf("queuing forced by configuration or option.\n");
285 }
286 }else{
287 smtp_printf(out, "451 Could not write spool file\r\n");
288 return;
289 }
290 }else{
291 switch(err){
292 case AERR_TIMEOUT:
293 return;
294 case AERR_EOF:
295 return;
296 default:
297 /* should never happen: */
298 smtp_printf(out, "451 Unknown error\r\n");
299 return;
300 }
301 }
302 psc->rcpt_seen = psc->from_seen = FALSE;
303 destroy_message(msg);
304 msg = NULL;
305 }else{
306 if(!psc->helo_seen)
307 smtp_printf(out, "503 need HELO or EHLO.\r\n");
308 else
309 smtp_printf(out, "503 need RCPT TO: before DATA\r\n");
310 }
311 break;
312 case SMTP_QUIT:
313 smtp_printf(out, "221 goodbye\r\n");
314 if(msg != NULL) 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