masqmail

view src/smtp_out.c @ 332:63efd381e27b

refactoring, partly also related to set_euidgid()
author markus schnalke <meillo@marmaro.de>
date Sat, 27 Aug 2011 16:21:17 +0200
parents c74adb7c4f50
children 02bc0331e390
line source
1 /* smtp_out.c
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.
9 *
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.
14 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
20 /*
21 I always forget these rfc numbers:
22 RFC 821 (SMTP)
23 RFC 1869 (ESMTP)
24 RFC 1870 (ESMTP SIZE)
25 RFC 2197 (ESMTP PIPELINE)
26 RFC 2554 (ESMTP AUTH)
27 */
29 #include "masqmail.h"
30 #include "smtp_out.h"
31 #include "readsock.h"
33 #ifdef ENABLE_AUTH
35 #ifdef USE_LIB_CRYPTO
36 #include <openssl/hmac.h>
37 #include <openssl/md5.h>
38 #include <openssl/evp.h>
39 #else
40 #include "md5/md5.h"
41 #include "md5/hmac_md5.h"
42 #endif
44 #include "base64/base64.h"
45 #endif
47 void
48 destroy_smtpbase(smtp_base * psb)
49 {
50 fclose(psb->in);
51 fclose(psb->out);
53 close(psb->sock);
55 if (psb->helo_name)
56 g_free(psb->helo_name);
57 if (psb->buffer)
58 g_free(psb->buffer);
59 if (psb->auth_names)
60 g_strfreev(psb->auth_names);
62 if (psb->auth_name)
63 g_free(psb->auth_name);
64 if (psb->auth_login)
65 g_free(psb->auth_login);
66 if (psb->auth_secret)
67 g_free(psb->auth_secret);
68 }
70 gchar*
71 set_heloname(smtp_base * psb, gchar * default_name, gboolean do_correct)
72 {
73 struct sockaddr_in sname;
74 int len = sizeof(struct sockaddr_in);
75 struct hostent *host_entry;
77 if (do_correct) {
78 getsockname(psb->sock, (struct sockaddr *) (&sname), &len);
79 DEBUG(5) debugf("socket: name.sin_addr = %s\n", inet_ntoa(sname.sin_addr));
80 host_entry = gethostbyaddr((const char *) &(sname.sin_addr), sizeof(sname.sin_addr), AF_INET);
81 if (host_entry) {
82 psb->helo_name = g_strdup(host_entry->h_name);
83 } else {
84 /* we failed to look up our own name. Instead of giving our local hostname,
85 we may give our IP number to show the server that we are at least
86 willing to be honest. For the really picky ones. */
87 DEBUG(5) debugf("failed to look up own host name.\n");
88 psb->helo_name = g_strdup_printf("[%s]", inet_ntoa(sname.sin_addr));
89 }
90 DEBUG(5) debugf("helo_name = %s\n", psb->helo_name);
91 }
92 if (psb->helo_name == NULL) {
93 psb->helo_name = g_strdup(default_name);
94 }
95 return psb->helo_name;
96 }
98 #ifdef ENABLE_AUTH
100 gboolean
101 set_auth(smtp_base * psb, gchar * name, gchar * login, gchar * secret)
102 {
103 if ((strcasecmp(name, "CRAM-MD5") == 0) || (strcasecmp(name, "LOGIN") == 0)) {
104 psb->auth_name = g_strdup(name);
105 psb->auth_login = g_strdup(login);
106 psb->auth_secret = g_strdup(secret);
108 return TRUE;
109 }
110 return FALSE;
111 }
113 #endif
115 static smtp_base*
116 create_smtpbase(gint sock)
117 {
118 gint dup_sock;
120 smtp_base *psb = (smtp_base *) g_malloc(sizeof(smtp_base));
122 psb->sock = sock;
124 psb->use_size = FALSE;
125 psb->use_pipelining = FALSE;
126 psb->use_auth = FALSE;
128 psb->max_size = 0;
129 psb->auth_names = NULL;
131 psb->buffer = (gchar *) g_malloc(SMTP_BUF_LEN);
133 dup_sock = dup(sock);
134 psb->out = fdopen(sock, "w");
135 psb->in = fdopen(dup_sock, "r");
137 psb->error = smtp_ok;
139 psb->helo_name = NULL;
141 psb->auth_name = psb->auth_login = psb->auth_secret = NULL;
143 return psb;
144 }
146 static gboolean
147 read_response(smtp_base * psb, int timeout)
148 {
149 gint buf_pos = 0;
150 gchar code[5];
151 gint i, len;
153 do {
154 len = read_sockline(psb->in, &(psb->buffer[buf_pos]), SMTP_BUF_LEN - buf_pos, timeout, READSOCKL_CHUG);
155 if (len == -3) {
156 psb->error = smtp_timeout;
157 return FALSE;
158 } else if (len == -2) {
159 psb->error = smtp_syntax;
160 return FALSE;
161 } else if (len == -1) {
162 psb->error = smtp_eof;
163 return FALSE;
164 }
165 for (i = 0; i < 4; i++)
166 code[i] = psb->buffer[buf_pos + i];
167 code[i] = '\0';
168 psb->last_code = atoi(code);
170 buf_pos += len;
172 } while (code[3] == '-');
173 if (psb->buffer) {
174 DEBUG(4) debugf("S: %s\n", psb->buffer);
175 }
177 return TRUE;
178 }
180 static gboolean
181 check_response(smtp_base * psb, gboolean after_data)
182 {
183 char c = psb->buffer[0];
185 if (((c == '2') && !after_data) || ((c == '3') && after_data)) {
186 psb->error = smtp_ok;
187 DEBUG(6) debugf("response OK:'%s' after_data = %d\n", psb->buffer, (int) after_data);
188 return TRUE;
189 } else {
190 if (c == '4')
191 psb->error = smtp_trylater;
192 else if (c == '5')
193 psb->error = smtp_fail;
194 else
195 psb->error = smtp_syntax;
196 DEBUG(6) debugf("response failure:'%s' after_data = %d\n", psb->buffer, (int) after_data);
197 return FALSE;
198 }
199 }
201 static gchar*
202 get_response_arg(gchar * response)
203 {
204 gchar buf[SMTP_BUF_LEN];
205 gchar *p = response, *q = buf;
207 while (*p && (*p != '\n') && isspace(*p))
208 p++;
209 if (*p && (*p != '\n')) {
210 while (*p && (*p != '\n') && (*p != '\r') && (q < buf + SMTP_BUF_LEN - 1))
211 *(q++) = *(p++);
212 *q = '\0';
213 return g_strdup(buf);
214 }
215 return NULL;
216 }
218 static gboolean
219 check_helo_response(smtp_base * psb)
220 {
221 gchar *ptr;
223 if (!check_response(psb, FALSE))
224 return FALSE;
226 if (psb->last_code == 220) {
227 logwrite(LOG_NOTICE, "received a 220 greeting after sending EHLO,\n");
228 logwrite(LOG_NOTICE, "please remove `instant_helo' from your route config\n");
229 /* read the next response, cause that's the actual helo response */
230 if (!read_response(psb, SMTP_CMD_TIMEOUT) || !check_response(psb, FALSE)) {
231 return FALSE;
232 }
233 }
235 ptr = psb->buffer;
237 while (*ptr) {
238 if (strncasecmp(&(ptr[4]), "SIZE", 4) == 0) {
239 gchar *arg;
240 psb->use_size = TRUE;
241 arg = get_response_arg(&(ptr[8]));
242 if (arg) {
243 psb->max_size = atoi(arg);
244 g_free(arg);
245 }
246 }
248 if (strncasecmp(&(ptr[4]), "PIPELINING", 10) == 0)
249 psb->use_pipelining = TRUE;
251 if (strncasecmp(&(ptr[4]), "AUTH", 4) == 0) {
252 if ((ptr[8] == ' ') || (ptr[8] == '=') || (ptr[8] == '\t')) { /* not sure about '\t' */
253 gchar *arg;
254 psb->use_auth = TRUE;
255 arg = get_response_arg(&(ptr[9])); /* after several years I finally learnt to count */
256 if (arg) {
257 psb->auth_names = g_strsplit(arg, " ", 0);
258 g_free(arg);
260 DEBUG(4) {
261 gint i = 0;
262 debugf("in check_helo_response()\n");
263 while (psb->auth_names[i]) {
264 debugf(" offered AUTH %s\n", psb->auth_names[i]);
265 i++;
266 }
267 }
268 }
269 }
270 }
272 while (*ptr != '\n')
273 ptr++;
274 ptr++;
275 }
277 DEBUG(4) {
278 debugf(" %s\n", psb->use_size ? "uses SIZE" : "no size");
279 debugf(" %s\n", psb->use_pipelining ? "uses PIPELINING" : "no pipelining");
280 debugf(" %s\n", psb->use_auth ? "uses AUTH" : "no auth");
281 }
283 return TRUE;
284 }
286 /*
287 We first try EHLO, but if it fails HELO in a second fall back try.
288 This is what is requested by RFC 2821 (sec 3.2):
290 Once the server has sent the welcoming message and
291 the client has received it, the client normally sends
292 the EHLO command to the server, [...]
293 For a particular connection attempt, if the server
294 returns a "command not recognized" response to EHLO,
295 the client SHOULD be able to fall back and send HELO.
297 Up to and including version 0.3.0 masqmail used ESMTP only if the
298 string ``ESMTP'' appeared within the server's greeting message. This
299 made it impossible to use AUTH with servers that would send odd
300 greeting messages.
301 */
302 static gboolean
303 smtp_helo(smtp_base * psb, gchar * helo)
304 {
305 fprintf(psb->out, "EHLO %s\r\n", helo);
306 fflush(psb->out);
307 DEBUG(4) debugf("C: EHLO %s\r\n", helo);
309 if (!read_response(psb, SMTP_CMD_TIMEOUT)) {
310 return FALSE;
311 }
312 if (check_helo_response(psb)) {
313 DEBUG(4) debugf("uses esmtp\n");
314 return TRUE;
315 }
317 if (psb->error != smtp_fail) {
318 return FALSE;
319 }
321 /* our guess that server understands EHLO could have been wrong,
322 try again with HELO */
324 fprintf(psb->out, "HELO %s\r\n", helo);
325 fflush(psb->out);
326 DEBUG(4) debugf("C: HELO %s\r\n", helo);
328 if (!read_response(psb, SMTP_CMD_TIMEOUT)) {
329 return FALSE;
330 }
331 if (check_helo_response(psb)) {
332 DEBUG(4) debugf("uses smtp\n");
333 return TRUE;
334 }
336 /* what sort of server ist THAT ?! give up... */
337 return FALSE;
338 }
340 static void
341 smtp_cmd_mailfrom(smtp_base * psb, address * return_path, guint size)
342 {
343 if (psb->use_size) {
344 fprintf(psb->out, "MAIL FROM:%s SIZE=%d\r\n", addr_string(return_path), size);
345 fflush(psb->out);
347 DEBUG(4) debugf("C: MAIL FROM:%s SIZE=%d\r\n", addr_string(return_path), size);
349 } else {
350 fprintf(psb->out, "MAIL FROM:%s\r\n", addr_string(return_path));
351 fflush(psb->out);
353 DEBUG(4) debugf("C: MAIL FROM:%s\r\n", addr_string(return_path));
354 }
355 }
357 static void
358 smtp_cmd_rcptto(smtp_base * psb, address * rcpt)
359 {
360 fprintf(psb->out, "RCPT TO:%s\r\n", addr_string(rcpt));
361 fflush(psb->out);
362 DEBUG(4) debugf("C: RCPT TO:%s\n", addr_string(rcpt));
363 }
365 static void
366 send_data_line(smtp_base * psb, gchar * data)
367 {
368 /* According to RFC 821 each line should be terminated with CRLF.
369 Since a dot on a line itself marks the end of data, each line
370 beginning with a dot is prepended with another dot.
371 */
372 gchar *ptr;
373 gboolean new_line = TRUE; /* previous versions assumed that each item was exactly one line.
374 This is no longer the case */
376 ptr = data;
377 while (*ptr) {
378 int c = (int) (*ptr);
379 if (c == '.' && new_line) {
380 /* dot-stuffing */
381 putc('.', psb->out);
382 }
383 if (c == '\n') {
384 /* CRLF line terminators */
385 putc('\r', psb->out);
386 putc('\n', psb->out);
387 new_line = TRUE;
388 } else {
389 putc(c, psb->out);
390 new_line = FALSE;
391 }
392 ptr++;
393 }
394 }
396 static void
397 send_header(smtp_base * psb, GList * hdr_list)
398 {
399 GList *node;
400 gint num_hdrs = 0;
402 /* header */
403 if (hdr_list) {
404 foreach(hdr_list, node) {
405 if (node->data) {
406 header *hdr = (header *) (node->data);
407 if (hdr->header) {
408 send_data_line(psb, hdr->header);
409 num_hdrs++;
410 }
411 }
412 }
413 }
415 /* empty line separating headers from data: */
416 putc('\r', psb->out);
417 putc('\n', psb->out);
419 DEBUG(4) debugf("sent %d headers\n", num_hdrs);
420 }
422 static void
423 send_data(smtp_base * psb, message * msg)
424 {
425 GList *node;
426 gint num_lines = 0;
428 /* data */
429 if (msg->data_list) {
430 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
431 if (node->data) {
432 send_data_line(psb, node->data);
433 num_lines++;
434 }
435 }
436 }
438 DEBUG(4) debugf("sent %d lines of data\n", num_lines);
440 fprintf(psb->out, ".\r\n");
441 fflush(psb->out);
442 DEBUG(4) debugf("C: .\n");
443 }
445 void
446 smtp_out_mark_rcpts(smtp_base * psb, GList * rcpt_list)
447 {
448 GList *rcpt_node;
449 for (rcpt_node = g_list_first(rcpt_list); rcpt_node; rcpt_node = g_list_next(rcpt_node)) {
450 address *rcpt = (address *) (rcpt_node->data);
452 addr_unmark_delivered(rcpt);
454 if ((psb->error == smtp_trylater) || (psb->error == smtp_timeout) || (psb->error == smtp_eof)) {
455 addr_mark_defered(rcpt);
456 } else {
457 addr_mark_failed(rcpt);
458 }
459 }
460 }
462 void
463 smtp_out_log_failure(smtp_base * psb, message * msg)
464 {
465 gchar *err_str;
467 if (psb->error == smtp_timeout)
468 err_str = g_strdup("connection timed out.");
469 else if (psb->error == smtp_eof)
470 err_str = g_strdup("connection terminated prematurely.");
471 else if (psb->error == smtp_syntax)
472 err_str = g_strdup_printf("got unexpected response: %s", psb->buffer);
473 else if (psb->error == smtp_cancel)
474 err_str = g_strdup("delivery was canceled.\n");
475 else
476 /* error message should still be in the buffer */
477 err_str = g_strdup_printf("failed: %s\n", psb->buffer);
479 if (msg == NULL)
480 logwrite(LOG_NOTICE, "host=%s %s\n", psb->remote_host, err_str);
481 else
482 logwrite(LOG_NOTICE, "%s == host=%s %s\n", msg->uid, psb->remote_host, err_str);
484 g_free(err_str);
485 }
487 smtp_base*
488 smtp_out_open(gchar * host, gint port, GList * resolve_list)
489 {
490 smtp_base *psb;
491 gint sock;
492 mxip_addr *addr;
494 DEBUG(5) debugf("smtp_out_open entered, host = %s\n", host);
496 if ((addr = connect_resolvelist(&sock, host, port, resolve_list))) {
497 /* create structure to hold status data: */
498 psb = create_smtpbase(sock);
499 psb->remote_host = addr->name;
501 DEBUG(5) {
502 struct sockaddr_in name;
503 int len = sizeof(struct sockaddr);
504 getsockname(sock, (struct sockaddr *) (&name), &len);
505 debugf("socket: name.sin_addr = %s\n", inet_ntoa(name.sin_addr));
506 }
507 return psb;
508 } else {
509 DEBUG(5) debugf("connect_resolvelist failed: %s %s\n", strerror(errno), hstrerror(h_errno));
510 }
512 return NULL;
513 }
515 smtp_base*
516 smtp_out_open_child(gchar * cmd, char* host)
517 {
518 smtp_base *psb;
519 gint sock;
521 DEBUG(5) debugf("smtp_out_open_child entered, cmd = %s\n", cmd);
522 psb->remote_host = host;
523 sock = child(cmd);
524 if (sock > 0) {
525 psb = create_smtpbase(sock);
526 psb->remote_host = NULL;
528 return psb;
529 }
531 return NULL;
532 }
534 gboolean
535 smtp_out_rset(smtp_base * psb)
536 {
537 gboolean ok;
539 fprintf(psb->out, "RSET\r\n");
540 fflush(psb->out);
541 DEBUG(4) debugf("C: RSET\n");
543 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
544 if (check_response(psb, FALSE))
545 return TRUE;
547 smtp_out_log_failure(psb, NULL);
549 return FALSE;
550 }
552 #ifdef ENABLE_AUTH
554 static gboolean
555 smtp_out_auth_cram_md5(smtp_base * psb)
556 {
557 gboolean ok = FALSE;
559 fprintf(psb->out, "C: AUTH CRAM-MD5\r\n");
560 fflush(psb->out);
561 DEBUG(4) debugf("AUTH CRAM-MD5\n");
562 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
563 if ((ok = check_response(psb, TRUE))) {
564 gchar *chall64 = get_response_arg(&(psb->buffer[4]));
565 gint chall_size;
566 gchar *chall = base64_decode(chall64, &chall_size);
567 guchar digest[16], *reply64, *reply;
568 gchar digest_string[33];
569 gint i;
570 #ifdef USE_LIB_CRYPTO
571 unsigned int digest_len;
572 #endif
574 DEBUG(5) debugf("smtp_out_auth_cram_md5():\n");
575 DEBUG(5) debugf(" encoded challenge = %s\n", chall64);
576 DEBUG(5) debugf(" decoded challenge = %s, size = %d\n", chall, chall_size);
577 DEBUG(5) debugf(" secret = %s\n", psb->auth_secret);
579 #ifdef USE_LIB_CRYPTO
580 HMAC(EVP_md5(), psb->auth_secret, strlen(psb->auth_secret), chall, chall_size, digest, &digest_len);
581 #else
582 hmac_md5(chall, chall_size, psb->auth_secret, strlen(psb->auth_secret), digest);
583 #endif
585 for (i = 0; i < 16; i++)
586 sprintf(&(digest_string[i + i]), "%02x", (unsigned int) (digest[i]));
587 digest_string[32] = '\0';
589 DEBUG(5) debugf(" digest = %s\n", digest_string);
591 reply = g_strdup_printf("%s %s", psb->auth_login, digest_string);
592 DEBUG(5) debugf(" unencoded reply = %s\n", reply);
594 reply64 = base64_encode(reply, strlen(reply));
595 DEBUG(5) debugf(" encoded reply = %s\n", reply64);
597 fprintf(psb->out, "%s\r\n", reply64);
598 fflush(psb->out);
599 DEBUG(6) debugf(" reply64 = %s\n", reply64);
600 DEBUG(6) debugf("C: %s\n", reply64);
602 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
603 ok = check_response(psb, FALSE);
605 g_free(reply64);
606 g_free(reply);
607 g_free(chall);
608 g_free(chall64);
609 }
610 }
611 return ok;
612 }
614 static gboolean
615 smtp_out_auth_login(smtp_base * psb)
616 {
617 gboolean ok = FALSE;
618 fprintf(psb->out, "AUTH LOGIN\r\n");
619 fflush(psb->out);
620 DEBUG(4) debugf("C: AUTH LOGIN\r\n");
621 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
622 if ((ok = check_response(psb, TRUE))) {
623 gchar *resp64;
624 guchar *resp;
625 gint resp_size;
626 gchar *reply64;
628 DEBUG(5) debugf("smtp_out_auth_login():\n");
629 resp64 = get_response_arg(&(psb->buffer[4]));
630 DEBUG(5) debugf(" encoded response = `%s'\n", resp64);
631 resp = base64_decode(resp64, &resp_size);
632 g_free(resp64);
633 DEBUG(5) debugf(" decoded response = `%s', size = %d\n", resp, resp_size);
634 g_free(resp);
635 reply64 = base64_encode(psb->auth_login, strlen(psb->auth_login));
636 fprintf(psb->out, "%s\r\n", reply64);
637 fflush(psb->out);
638 DEBUG(6) debugf("C: %s\n", reply64);
639 g_free(reply64);
640 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
641 if ((ok = check_response(psb, TRUE))) {
642 resp64 = get_response_arg(&(psb->buffer[4]));
643 DEBUG(5) debugf(" encoded response = `%s'\n", resp64);
644 resp = base64_decode(resp64, &resp_size);
645 g_free(resp64);
646 DEBUG(5) debugf(" decoded response = `%s', size = %d\n", resp, resp_size);
647 g_free(resp);
648 reply64 = base64_encode(psb->auth_secret, strlen(psb->auth_secret));
649 fprintf(psb->out, "%s\r\n", reply64);
650 fflush(psb->out);
651 DEBUG(6) debugf("C: %s\n", reply64);
652 g_free(reply64);
653 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
654 ok = check_response(psb, FALSE);
655 }
656 }
657 }
658 }
659 return ok;
660 }
662 gboolean
663 smtp_out_auth(smtp_base * psb)
664 {
665 gboolean ok = FALSE;
666 gint i = 0;
667 while (psb->auth_names[i]) {
668 if (strcasecmp(psb->auth_names[i], psb->auth_name) == 0)
669 break;
670 i++;
671 }
672 if (psb->auth_names[i]) {
673 if (strcasecmp(psb->auth_name, "cram-md5") == 0) {
674 smtp_out_auth_cram_md5(psb);
675 } else if (strcasecmp(psb->auth_name, "login") == 0) {
676 smtp_out_auth_login(psb);
677 } else {
678 logwrite(LOG_ERR, "auth method %s not supported\n", psb->auth_name);
679 }
680 } else {
681 logwrite(LOG_ERR, "no auth method %s found.\n", psb->auth_name);
682 }
683 return ok;
684 }
686 #endif
688 gboolean
689 smtp_out_init(smtp_base * psb, gboolean instant_helo)
690 {
691 gboolean ok;
693 logwrite(LOG_INFO, "smtp_out_init(): instant_helo:%d\n", instant_helo);
695 if (!instant_helo) {
696 if ((ok = read_response(psb, SMTP_INITIAL_TIMEOUT))) {
697 ok = check_response(psb, FALSE);
698 }
699 if (!ok) {
700 smtp_out_log_failure(psb, NULL);
701 return ok;
702 }
703 }
705 if ((ok = smtp_helo(psb, psb->helo_name))) {
706 #ifdef ENABLE_AUTH
707 if (psb->auth_name && psb->use_auth) {
708 /* we completely disregard the response of server here. If
709 authentication fails, the server will complain later
710 anyway. I know, this is not polite... */
711 smtp_out_auth(psb);
712 }
713 #endif
714 }
715 if (!ok)
716 smtp_out_log_failure(psb, NULL);
717 return ok;
718 }
720 gint
721 smtp_out_msg(smtp_base * psb, message * msg, address * return_path, GList * rcpt_list, GList * hdr_list)
722 {
723 gint i, size;
724 gboolean ok = TRUE;
725 int rcpt_cnt;
726 int rcpt_accept = 0;
728 DEBUG(5) debugf("smtp_out_msg entered\n");
730 /* defaults: */
731 if (return_path == NULL)
732 return_path = msg->return_path;
733 if (hdr_list == NULL)
734 hdr_list = msg->hdr_list;
735 if (rcpt_list == NULL)
736 rcpt_list = msg->rcpt_list;
737 rcpt_cnt = g_list_length(rcpt_list);
739 size = msg_calc_size(msg, TRUE);
741 /* respect maximum size given by server: */
742 if ((psb->max_size > 0) && (size > psb->max_size)) {
743 logwrite(LOG_WARNING, "%s == host=%s message size (%d) > "
744 "fixed maximum message size of server (%d)",
745 msg->uid, psb->remote_host, size, psb->max_size);
746 psb->error = smtp_cancel;
747 ok = FALSE;
748 }
750 if (ok) {
751 /* pretend the message is a bit larger,
752 just in case the size calculation is buggy */
753 smtp_cmd_mailfrom(psb, return_path, psb->use_size ? size+SMTP_SIZE_ADD : 0);
755 if (!psb->use_pipelining) {
756 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
757 ok = check_response(psb, FALSE);
758 }
759 }
760 if (ok) {
761 GList *rcpt_node;
762 rcpt_accept = 0;
764 for (rcpt_node = g_list_first(rcpt_list); rcpt_node != NULL; rcpt_node = g_list_next(rcpt_node)) {
765 address *rcpt = (address *) (rcpt_node->data);
766 smtp_cmd_rcptto(psb, rcpt);
767 if (!psb->use_pipelining) {
768 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
769 if (check_response(psb, FALSE)) {
770 rcpt_accept++;
771 addr_mark_delivered(rcpt);
772 } else {
773 /* if server returned an error for one recp. we
774 may still try the others. But if it is a timeout, eof
775 or unexpected response, it is more serious and we should
776 give up. */
777 if ((psb->error != smtp_trylater) && (psb->error != smtp_fail)) {
778 ok = FALSE;
779 break;
780 } else {
781 logwrite(LOG_NOTICE, "%s == %s host=%s failed: %s\n",
782 msg->uid, addr_string(rcpt), psb->remote_host, psb->buffer);
783 if (psb->error == smtp_trylater) {
784 addr_mark_defered(rcpt);
785 } else {
786 addr_mark_failed(rcpt);
787 }
788 }
789 } else
790 break;
791 }
792 }
794 /* There is no point in going on if no recp.s were accpted.
795 But we can check that at this point only if not pipelining: */
796 ok = (ok && (psb->use_pipelining || (rcpt_accept > 0)));
797 if (ok) {
799 fprintf(psb->out, "DATA\r\n");
800 fflush(psb->out);
802 DEBUG(4) debugf("C: DATA\r\n");
804 if (psb->use_pipelining) {
805 /* the first pl'ed command was MAIL FROM
806 the last was DATA, whose response can be handled by the 'normal' code
807 all in between were RCPT TO:
808 */
809 /* response to MAIL FROM: */
810 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
811 if ((ok = check_response(psb, FALSE))) {
813 /* response(s) to RCPT TO:
814 this is very similar to the sequence above for no pipeline
815 */
816 for (i = 0; i < rcpt_cnt; i++) {
817 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
818 address *rcpt = g_list_nth_data(rcpt_list, i);
819 if (check_response(psb, FALSE)) {
820 rcpt_accept++;
821 addr_mark_delivered(rcpt);
822 } else {
823 /* if server returned an error 4xx or 5xx for one recp. we
824 may still try the others. But if it is a timeout, eof
825 or unexpected response, it is more serious and we
826 should give up. */
827 if ((psb->error != smtp_trylater) &&
828 (psb->error != smtp_fail)) {
829 ok = FALSE;
830 break;
831 } else {
832 logwrite(LOG_NOTICE, "%s == %s host=%s failed: %s\n", msg->uid,
833 addr_string(rcpt), psb->remote_host, psb->buffer);
834 if (psb->error == smtp_trylater) {
835 addr_mark_defered(rcpt);
836 } else {
837 addr_mark_failed(rcpt);
838 }
839 }
840 }
841 } else {
842 DEBUG(5) debugf("check_response failed after RCPT TO\n");
843 break;
844 }
845 }
846 if (rcpt_accept == 0)
847 ok = FALSE;
848 } else {
849 DEBUG(5) debugf("check_response failed after MAIL FROM\n");
850 }
851 } else {
852 DEBUG(5)
853 debugf("read_response failed after MAIL FROM\n");
854 }
855 }
857 /* if(psb->use_pipelining) */
858 /* response to the DATA cmd */
859 if (ok) {
860 if (read_response(psb, SMTP_DATA_TIMEOUT)) {
861 if (check_response(psb, TRUE)) {
862 send_header(psb, hdr_list);
863 send_data(psb, msg);
865 if (read_response(psb, SMTP_FINAL_TIMEOUT))
866 ok = check_response(psb, FALSE);
867 }
868 }
869 }
870 }
871 }
873 DEBUG(5) {
874 debugf("smtp_out_msg():\n");
875 debugf(" psb->error = %d\n", psb->error);
876 debugf(" ok = %d\n", ok);
877 debugf(" rcpt_accept = %d\n", rcpt_accept);
878 }
880 if (psb->error == smtp_ok) {
881 GList *rcpt_node;
882 for (rcpt_node = g_list_first(rcpt_list); rcpt_node; rcpt_node = g_list_next(rcpt_node)) {
883 address *rcpt = (address *) (rcpt_node->data);
884 if (addr_is_delivered(rcpt))
885 logwrite(LOG_NOTICE, "%s => %s host=%s\n",
886 msg->uid, addr_string(rcpt), psb->remote_host);
887 }
888 } else {
889 /* if something went wrong,
890 we have to unmark the rcpts prematurely marked as delivered
891 and mark the status */
892 smtp_out_mark_rcpts(psb, rcpt_list);
894 /* log the failure: */
895 smtp_out_log_failure(psb, msg);
896 }
897 return rcpt_accept;
898 }
900 gboolean
901 smtp_out_quit(smtp_base * psb)
902 {
903 fprintf(psb->out, "QUIT\r\n");
904 fflush(psb->out);
906 DEBUG(4) debugf("C: QUIT\n");
908 signal(SIGALRM, SIG_DFL);
910 return TRUE;
911 }
913 gint
914 smtp_deliver(gchar * host, gint port, GList * resolve_list, message * msg, address * return_path, GList * rcpt_list)
915 {
916 smtp_base *psb;
917 smtp_error err;
919 DEBUG(5) debugf("smtp_deliver entered\n");
921 if (return_path == NULL)
922 return_path = msg->return_path;
924 if ((psb = smtp_out_open(host, port, resolve_list))) {
925 set_heloname(psb, return_path->domain, TRUE);
926 /* initiate connection, send message and quit: */
927 if (smtp_out_init(psb, FALSE)) {
928 smtp_out_msg(psb, msg, return_path, rcpt_list, NULL);
929 if (psb->error == smtp_ok || (psb->error == smtp_fail) || (psb->error == smtp_trylater)
930 || (psb->error == smtp_syntax) || (psb->error == smtp_cancel))
931 smtp_out_quit(psb);
932 }
934 err = psb->error;
935 destroy_smtpbase(psb);
937 return err;
938 }
939 return -1;
940 }