comparison 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
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
28 RFC 2554 (ESMTP AUTH) 28 RFC 2554 (ESMTP AUTH)
29 */ 29 */
30 30
31 #ifdef ENABLE_SMTP_SERVER 31 #ifdef ENABLE_SMTP_SERVER
32 32
33 smtp_cmd smtp_cmds[] = 33 smtp_cmd smtp_cmds[] = {
34 { 34 {SMTP_HELO, "HELO",}
35 { SMTP_HELO, "HELO", }, 35 ,
36 { SMTP_EHLO, "EHLO", }, 36 {SMTP_EHLO, "EHLO",}
37 { SMTP_MAIL_FROM, "MAIL FROM:", }, 37 ,
38 { SMTP_RCPT_TO, "RCPT TO:", }, 38 {SMTP_MAIL_FROM, "MAIL FROM:",}
39 { SMTP_DATA, "DATA", }, 39 ,
40 { SMTP_QUIT, "QUIT", }, 40 {SMTP_RCPT_TO, "RCPT TO:",}
41 { SMTP_RSET, "RSET", }, 41 ,
42 { SMTP_NOOP, "NOOP", }, 42 {SMTP_DATA, "DATA",}
43 { SMTP_HELP, "HELP" }, 43 ,
44 {SMTP_QUIT, "QUIT",}
45 ,
46 {SMTP_RSET, "RSET",}
47 ,
48 {SMTP_NOOP, "NOOP",}
49 ,
50 {SMTP_HELP, "HELP"}
51 ,
44 }; 52 };
45 53
46 static 54 static smtp_cmd_id
47 smtp_cmd_id get_id(const gchar *line) 55 get_id(const gchar * line)
48 { 56 {
49 gint i; 57 gint i;
50 for(i = 0; i < SMTP_NUM_IDS; i++){ 58 for (i = 0; i < SMTP_NUM_IDS; i++) {
51 if(strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0) 59 if (strncasecmp(smtp_cmds[i].cmd, line, strlen(smtp_cmds[i].cmd)) == 0)
52 return (smtp_cmd_id)i; 60 return (smtp_cmd_id) i;
53 } 61 }
54 return SMTP_ERROR; 62 return SMTP_ERROR;
55 } 63 }
56 64
57 /* this is a quick hack: we expect the address to be syntactically correct 65 /* this is a quick hack: we expect the address to be syntactically correct
58 and containing the mailbox only: 66 and containing the mailbox only:
59 */ 67 */
60 68
61 static 69 static gboolean
62 gboolean get_address(gchar *line, gchar *addr) 70 get_address(gchar * line, gchar * addr)
63 { 71 {
64 gchar *p = line, *q = addr; 72 gchar *p = line, *q = addr;
65 73
66 /* skip MAIL FROM: and RCPT TO: */ 74 /* skip MAIL FROM: and RCPT TO: */
67 while(*p && (*p != ':')) p++; 75 while (*p && (*p != ':'))
68 p++; 76 p++;
69 77 p++;
70 /* skip spaces: */ 78
71 while(*p && isspace(*p)) p++; 79 /* skip spaces: */
72 80 while (*p && isspace(*p))
73 /* get address: */ 81 p++;
74 while(*p && !isspace(*p) && (q < addr+MAX_ADDRESS-1)) *(q++) = *(p++); 82
75 *q = 0; 83 /* get address: */
76 84 while (*p && !isspace(*p) && (q < addr + MAX_ADDRESS - 1))
77 return TRUE; 85 *(q++) = *(p++);
78 } 86 *q = 0;
79 87
80 static 88 return TRUE;
81 smtp_connection *create_base(gchar *remote_host) 89 }
82 { 90
83 smtp_connection *base = g_malloc(sizeof(smtp_connection)); 91 static smtp_connection*
84 if(base){ 92 create_base(gchar * remote_host)
85 base->remote_host = g_strdup(remote_host); 93 {
86 94 smtp_connection *base = g_malloc(sizeof(smtp_connection));
87 base->prot = PROT_SMTP; 95 if (base) {
88 base->next_id = 0; 96 base->remote_host = g_strdup(remote_host);
89 base->helo_seen = 0; 97
90 base->from_seen = 0; 98 base->prot = PROT_SMTP;
91 base->rcpt_seen = 0; 99 base->next_id = 0;
92 base->msg = NULL; 100 base->helo_seen = 0;
93 101 base->from_seen = 0;
94 return base; 102 base->rcpt_seen = 0;
95 } 103 base->msg = NULL;
96 return NULL; 104
97 } 105 return base;
98
99 static
100 void smtp_printf(FILE *out, gchar *fmt, ...)
101 {
102 va_list args;
103 va_start(args, fmt);
104
105 DEBUG(4){
106 gchar buf[256];
107 va_list args_copy;
108
109 va_copy(args_copy, args);
110 vsnprintf(buf, 255, fmt, args_copy);
111 va_end(args_copy);
112
113 debugf(">>>%s", buf);
114 }
115
116 vfprintf(out, fmt, args); fflush(out);
117
118 va_end(args);
119 }
120
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;
128
129 DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host);
130
131 psc = create_base(remote_host);
132 psc->msg = msg;
133
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);
139
140 while((len = read_sockline(in, buffer, BUF_LEN, 5*60, READSOCKL_CHUG)) >= 0){
141 cmd_id = get_id(buffer);
142
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;
149
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 } 106 }
167 107 return NULL;
168 case SMTP_MAIL_FROM: 108 }
169 if(psc->helo_seen && !psc->from_seen){ 109
170 gchar buf[MAX_ADDRESS]; 110 static void
171 address *addr; 111 smtp_printf(FILE * out, gchar * fmt, ...)
172 112 {
173 msg = create_message(); 113 va_list args;
174 msg->received_host = remote_host ? g_strdup(remote_host) : NULL; 114 va_start(args, fmt);
175 msg->received_prot = psc->prot; 115
176 msg->ident = ident ? g_strdup(ident) : NULL; 116 DEBUG(4) {
177 /* get transfer id and increment for next one */ 117 gchar buf[256];
178 msg->transfer_id = (psc->next_id)++; 118 va_list args_copy;
179 119
180 get_address(buffer, buf); 120 va_copy(args_copy, args);
181 if((addr = remote_host ? 121 vsnprintf(buf, 255, fmt, args_copy);
182 create_address(buf, TRUE) : 122 va_end(args_copy);
183 create_address_qualified(buf, TRUE, conf.host_name))){ 123
184 if(addr->domain != NULL){ 124 debugf(">>>%s", buf);
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 } 125 }
201 break; 126
202 127 vfprintf(out, fmt, args);
203 case SMTP_RCPT_TO: 128 fflush(out);
204 129
205 if(psc->helo_seen && psc->from_seen){ 130 va_end(args);
206 char buf[MAX_ADDRESS]; 131 }
207 address *addr; 132
208 133 void
209 get_address(buffer, buf); 134 smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident)
210 if((addr = remote_host ? 135 {
211 create_address(buf, TRUE) : 136 gchar *buffer;
212 create_address_qualified(buf, TRUE, conf.host_name))){ 137 smtp_cmd_id cmd_id;
213 if(addr->local_part[0] != '|'){ 138 message *msg = NULL;
214 if(addr->domain != NULL){ 139 smtp_connection *psc;
215 gboolean do_relay = conf.do_relay; 140 int len;
216 if(!do_relay){ 141
217 if((do_relay = addr_is_local(msg->return_path))); 142 DEBUG(5) debugf("smtp_in entered, remote_host = %s\n", remote_host);
218 if(!do_relay){ 143
219 do_relay = addr_is_local(addr); 144 psc = create_base(remote_host);
220 } 145 psc->msg = msg;
146
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);
151
152 while ((len = read_sockline(in, buffer, BUF_LEN, 5 * 60, READSOCKL_CHUG)) >= 0) {
153 cmd_id = get_id(buffer);
154
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;
161
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 }
174
175 case SMTP_MAIL_FROM:
176 if (psc->helo_seen && !psc->from_seen) {
177 gchar buf[MAX_ADDRESS];
178 address *addr;
179
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)++;
186
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;
208
209 case SMTP_RCPT_TO:
210
211 if (psc->helo_seen && psc->from_seen) {
212 char buf[MAX_ADDRESS];
213 address *addr;
214
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 {
245
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;
252
253 case SMTP_DATA:
254 if (psc->helo_seen && psc->rcpt_seen) {
255 accept_error err;
256
257 smtp_printf(out, "354 okay, and do not forget the dot\r\n");
258
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);
263
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]);
270
271 if (!conf.do_queue) {
272 if ((pid = fork()) == 0) {
273
274 if (deliver(msg))
275 _exit(EXIT_SUCCESS);
276 else
277 _exit(EXIT_FAILURE);
278
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;
329
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 }
221 } 342 }
222 if(do_relay){ 343 switch (len) {
223 psc->rcpt_seen = TRUE; 344 case -3:
224 msg->rcpt_list = g_list_append(msg->rcpt_list, addr); 345 logwrite(LOG_NOTICE, "connection timed out\n");
225 smtp_printf(out, "250 OK %s is our friend.\r\n", addr->address); 346 break;
226 }else{ 347 case -2:
227 smtp_printf(out, "550 relaying to %s denied.\r\n", 348 logwrite(LOG_NOTICE, "line overflow\n");
228 addr_string(addr)); 349 break;
350 case -1:
351 logwrite(LOG_NOTICE, "received EOF\n");
352 break;
353 default:
354 break;
229 } 355 }
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{
240
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 } 356 }
246 break;
247
248 case SMTP_DATA:
249 if(psc->helo_seen && psc->rcpt_seen){
250 accept_error err;
251
252 smtp_printf(out, "354 okay, and do not forget the dot\r\n");
253
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);
259
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]);
270
271 if(!conf.do_queue){
272 if((pid = fork()) == 0){
273
274 if(deliver(msg))
275 _exit(EXIT_SUCCESS);
276 else
277 _exit(EXIT_FAILURE);
278
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;
329
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 } 357 }
358 #endif 358 #endif