masqmail
view src/smtp_out.c @ 222:8cddc65765bd
added support for STARTTLS wrappers
added the route config option `instant_helo' which
causes masqmail, as SMTP client, not to wait for the
server's 220 greeting. Instead if says EHLO right at
once. You'll need this for STARTTLS wrappers that
usually eat the greeting line.
author | meillo@marmaro.de |
---|---|
date | Fri, 23 Jul 2010 10:57:53 +0200 |
parents | 10da50168dab |
children | 996b53a50f55 |
line source
1 /* smtp_out.c, Copyright (C) 1999-2001 Oliver Kurth,
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
18 /*
19 send bugs to: kurth@innominate.de
20 */
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 #include "masqmail.h"
32 #include "smtp_out.h"
33 #include "readsock.h"
35 #ifdef ENABLE_AUTH
37 #ifdef USE_LIB_CRYPTO
38 #include <openssl/hmac.h>
39 #include <openssl/md5.h>
40 #include <openssl/evp.h>
41 #else
42 #include "md5/md5.h"
43 #include "md5/hmac_md5.h"
44 #endif
46 #include "base64/base64.h"
47 #endif
49 void
50 destroy_smtpbase(smtp_base * psb)
51 {
52 fclose(psb->in);
53 fclose(psb->out);
55 close(psb->sock);
57 if (psb->helo_name)
58 g_free(psb->helo_name);
59 if (psb->buffer)
60 g_free(psb->buffer);
61 if (psb->auth_names)
62 g_strfreev(psb->auth_names);
64 if (psb->auth_name)
65 g_free(psb->auth_name);
66 if (psb->auth_login)
67 g_free(psb->auth_login);
68 if (psb->auth_secret)
69 g_free(psb->auth_secret);
70 }
72 gchar*
73 set_heloname(smtp_base * psb, gchar * default_name, gboolean do_correct)
74 {
75 struct sockaddr_in sname;
76 int len = sizeof(struct sockaddr_in);
77 struct hostent *host_entry;
79 if (do_correct) {
80 getsockname(psb->sock, (struct sockaddr *) (&sname), &len);
81 DEBUG(5) debugf("socket: name.sin_addr = %s\n", inet_ntoa(sname.sin_addr));
82 host_entry = gethostbyaddr((const char *) &(sname.sin_addr), sizeof(sname.sin_addr), AF_INET);
83 if (host_entry) {
84 psb->helo_name = g_strdup(host_entry->h_name);
85 } else {
86 /* we failed to look up our own name. Instead of giving our local hostname,
87 we may give our IP number to show the server that we are at least
88 willing to be honest. For the really picky ones. */
89 DEBUG(5) debugf("failed to look up own host name.\n");
90 psb->helo_name = g_strdup_printf("[%s]", inet_ntoa(sname.sin_addr));
91 }
92 DEBUG(5) debugf("helo_name = %s\n", psb->helo_name);
93 }
94 if (psb->helo_name == NULL) {
95 psb->helo_name = g_strdup(default_name);
96 }
97 return psb->helo_name;
98 }
100 #ifdef ENABLE_AUTH
102 gboolean
103 set_auth(smtp_base * psb, gchar * name, gchar * login, gchar * secret)
104 {
105 if ((strcasecmp(name, "CRAM-MD5") == 0) || (strcasecmp(name, "LOGIN") == 0)) {
106 psb->auth_name = g_strdup(name);
107 psb->auth_login = g_strdup(login);
108 psb->auth_secret = g_strdup(secret);
110 return TRUE;
111 }
112 return FALSE;
113 }
115 #endif
117 static smtp_base*
118 create_smtpbase(gint sock)
119 {
120 gint dup_sock;
122 smtp_base *psb = (smtp_base *) g_malloc(sizeof(smtp_base));
124 psb->sock = sock;
126 psb->use_esmtp = FALSE;
127 psb->use_size = FALSE;
128 psb->use_pipelining = FALSE;
129 psb->use_auth = FALSE;
131 psb->max_size = 0;
132 psb->auth_names = NULL;
134 psb->buffer = (gchar *) g_malloc(SMTP_BUF_LEN);
136 dup_sock = dup(sock);
137 psb->out = fdopen(sock, "w");
138 psb->in = fdopen(dup_sock, "r");
140 psb->error = smtp_ok;
142 psb->helo_name = NULL;
144 psb->auth_name = psb->auth_login = psb->auth_secret = NULL;
146 return psb;
147 }
149 static gboolean
150 read_response(smtp_base * psb, int timeout)
151 {
152 gint buf_pos = 0;
153 gchar code[5];
154 gint i, len;
156 do {
157 len = read_sockline(psb->in, &(psb->buffer[buf_pos]), SMTP_BUF_LEN - buf_pos, timeout, READSOCKL_CHUG);
158 if (len == -3) {
159 psb->error = smtp_timeout;
160 return FALSE;
161 } else if (len == -2) {
162 psb->error = smtp_syntax;
163 return FALSE;
164 } else if (len == -1) {
165 psb->error = smtp_eof;
166 return FALSE;
167 }
168 for (i = 0; i < 4; i++)
169 code[i] = psb->buffer[buf_pos + i];
170 code[i] = '\0';
171 psb->last_code = atoi(code);
173 buf_pos += len;
175 } while (code[3] == '-');
176 if (psb->buffer) {
177 DEBUG(4) debugf("S: %s\n", psb->buffer);
178 }
180 return TRUE;
181 }
183 static gboolean
184 check_response(smtp_base * psb, gboolean after_data)
185 {
186 char c = psb->buffer[0];
188 if (((c == '2') && !after_data) || ((c == '3') && after_data)) {
189 psb->error = smtp_ok;
190 DEBUG(6) debugf("response OK:'%s' after_data = %d\n", psb->buffer, (int) after_data);
191 return TRUE;
192 } else {
193 if (c == '4')
194 psb->error = smtp_trylater;
195 else if (c == '5')
196 psb->error = smtp_fail;
197 else
198 psb->error = smtp_syntax;
199 DEBUG(6) debugf("response failure:'%s' after_data = %d\n", psb->buffer, (int) after_data);
200 return FALSE;
201 }
202 }
204 static gboolean
205 check_init_response(smtp_base * psb)
206 {
207 if (check_response(psb, FALSE)) {
208 psb->use_esmtp = (strstr(psb->buffer, "ESMTP") != NULL);
210 DEBUG(4) debugf(psb->use_esmtp ? "uses esmtp\n" : "no esmtp\n");
212 return TRUE;
213 }
214 return FALSE;
215 }
217 static gchar*
218 get_response_arg(gchar * response)
219 {
220 gchar buf[SMTP_BUF_LEN];
221 gchar *p = response, *q = buf;
223 while (*p && (*p != '\n') && isspace(*p))
224 p++;
225 if (*p && (*p != '\n')) {
226 while (*p && (*p != '\n') && (*p != '\r') && (q < buf + SMTP_BUF_LEN - 1))
227 *(q++) = *(p++);
228 *q = '\0';
229 return g_strdup(buf);
230 }
231 return NULL;
232 }
234 static gboolean
235 check_helo_response(smtp_base * psb)
236 {
237 gchar *ptr;
239 if (!check_response(psb, FALSE))
240 return FALSE;
242 if (psb->last_code == 220) {
243 logwrite(LOG_NOTICE, "received a 220 greeting after sending EHLO,\n");
244 logwrite(LOG_NOTICE, "please remove `instant_helo' from your route config\n");
245 /* read the next response, cause that's the actual helo response */
246 if (!read_response(psb, SMTP_CMD_TIMEOUT) || !check_response(psb, FALSE)) {
247 return FALSE;
248 }
249 }
251 ptr = psb->buffer;
253 while (*ptr) {
254 if (strncasecmp(&(ptr[4]), "SIZE", 4) == 0) {
255 gchar *arg;
256 psb->use_size = TRUE;
257 arg = get_response_arg(&(ptr[8]));
258 if (arg) {
259 psb->max_size = atoi(arg);
260 g_free(arg);
261 }
262 }
264 if (strncasecmp(&(ptr[4]), "PIPELINING", 10) == 0)
265 psb->use_pipelining = TRUE;
267 if (strncasecmp(&(ptr[4]), "AUTH", 4) == 0) {
268 if ((ptr[8] == ' ') || (ptr[8] == '=') || (ptr[8] == '\t')) { /* not sure about '\t' */
269 gchar *arg;
270 psb->use_auth = TRUE;
271 arg = get_response_arg(&(ptr[9])); /* after several years I finally learnt to count */
272 if (arg) {
273 psb->auth_names = g_strsplit(arg, " ", 0);
274 g_free(arg);
276 DEBUG(4) {
277 gint i = 0;
278 debugf("in check_helo_response()\n");
279 while (psb->auth_names[i]) {
280 debugf(" offered AUTH %s\n", psb->auth_names[i]);
281 i++;
282 }
283 }
284 }
285 }
286 }
288 while (*ptr != '\n')
289 ptr++;
290 ptr++;
291 }
293 DEBUG(4) {
294 debugf(" %s\n", psb->use_size ? "uses SIZE" : "no size");
295 debugf(" %s\n", psb->use_pipelining ? "uses PIPELINING" : "no pipelining");
296 debugf(" %s\n", psb->use_auth ? "uses AUTH" : "no auth");
297 }
299 return TRUE;
300 }
302 static gboolean
303 smtp_helo(smtp_base * psb, gchar * helo)
304 {
305 while (TRUE) {
306 if (psb->use_esmtp) {
307 fprintf(psb->out, "EHLO %s\r\n", helo);
308 fflush(psb->out);
310 DEBUG(4) debugf("C: EHLO %s\r\n", helo);
312 } else {
313 fprintf(psb->out, "HELO %s\r\n", helo);
314 fflush(psb->out);
316 DEBUG(4) debugf("C: HELO %s\r\n", helo);
318 }
320 if (!read_response(psb, SMTP_CMD_TIMEOUT))
321 return FALSE;
323 if (check_helo_response(psb))
324 return TRUE;
325 else {
326 if (psb->error == smtp_fail) {
327 if (psb->use_esmtp) {
328 /* our guess that server understands EHLO was wrong, try again with HELO */
329 psb->use_esmtp = FALSE;
330 } else {
331 /* what sort of server ist THAT ?! give up... */
332 return FALSE;
333 }
334 } else
335 return FALSE;
336 }
337 }
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)
517 {
518 smtp_base *psb;
519 gint sock;
521 DEBUG(5) debugf("smtp_out_open_child entered, cmd = %s\n", cmd);
523 sock = child(cmd);
525 if (sock > 0) {
526 psb = create_smtpbase(sock);
527 psb->remote_host = NULL;
529 return psb;
530 }
532 return NULL;
533 }
535 gboolean
536 smtp_out_rset(smtp_base * psb)
537 {
538 gboolean ok;
540 fprintf(psb->out, "RSET\r\n");
541 fflush(psb->out);
542 DEBUG(4) debugf("C: RSET\n");
544 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
545 if (check_response(psb, FALSE))
546 return TRUE;
548 smtp_out_log_failure(psb, NULL);
550 return FALSE;
551 }
553 #ifdef ENABLE_AUTH
555 static gboolean
556 smtp_out_auth_cram_md5(smtp_base * psb)
557 {
558 gboolean ok = FALSE;
560 fprintf(psb->out, "C: AUTH CRAM-MD5\r\n");
561 fflush(psb->out);
562 DEBUG(4) debugf("AUTH CRAM-MD5\n");
563 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
564 if ((ok = check_response(psb, TRUE))) {
565 gchar *chall64 = get_response_arg(&(psb->buffer[4]));
566 gint chall_size;
567 gchar *chall = base64_decode(chall64, &chall_size);
568 guchar digest[16], *reply64, *reply;
569 gchar digest_string[33];
570 gint i;
571 #ifdef USE_LIB_CRYPTO
572 unsigned int digest_len;
573 #endif
575 DEBUG(5) debugf("smtp_out_auth_cram_md5():\n");
576 DEBUG(5) debugf(" encoded challenge = %s\n", chall64);
577 DEBUG(5) debugf(" decoded challenge = %s, size = %d\n", chall, chall_size);
578 DEBUG(5) debugf(" secret = %s\n", psb->auth_secret);
580 #ifdef USE_LIB_CRYPTO
581 HMAC(EVP_md5(), psb->auth_secret, strlen(psb->auth_secret), chall, chall_size, digest, &digest_len);
582 #else
583 hmac_md5(chall, chall_size, psb->auth_secret, strlen(psb->auth_secret), digest);
584 #endif
586 for (i = 0; i < 16; i++)
587 sprintf(&(digest_string[i + i]), "%02x", (unsigned int) (digest[i]));
588 digest_string[32] = '\0';
590 DEBUG(5) debugf(" digest = %s\n", digest_string);
592 reply = g_strdup_printf("%s %s", psb->auth_login, digest_string);
593 DEBUG(5) debugf(" unencoded reply = %s\n", reply);
595 reply64 = base64_encode(reply, strlen(reply));
596 DEBUG(5) debugf(" encoded reply = %s\n", reply64);
598 fprintf(psb->out, "%s\r\n", reply64);
599 fflush(psb->out);
600 DEBUG(6) debugf(" reply64 = %s\n", reply64);
601 DEBUG(6) debugf("C: %s\n", reply64);
603 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
604 ok = check_response(psb, FALSE);
606 g_free(reply64);
607 g_free(reply);
608 g_free(chall);
609 g_free(chall64);
610 }
611 }
612 return ok;
613 }
615 static gboolean
616 smtp_out_auth_login(smtp_base * psb)
617 {
618 gboolean ok = FALSE;
619 fprintf(psb->out, "AUTH LOGIN\r\n");
620 fflush(psb->out);
621 DEBUG(4) debugf("C: AUTH LOGIN\r\n");
622 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
623 if ((ok = check_response(psb, TRUE))) {
624 gchar *resp64;
625 guchar *resp;
626 gint resp_size;
627 gchar *reply64;
629 DEBUG(5) debugf("smtp_out_auth_login():\n");
630 resp64 = get_response_arg(&(psb->buffer[4]));
631 DEBUG(5) debugf(" encoded response = %s\n", resp64);
632 resp = base64_decode(resp64, &resp_size);
633 g_free(resp64);
634 DEBUG(5) debugf(" decoded response = %s, size = %d\n", resp, resp_size);
635 g_free(resp);
636 reply64 = base64_encode(psb->auth_login, strlen(psb->auth_login));
637 fprintf(psb->out, "%s\r\n", reply64);
638 fflush(psb->out);
639 DEBUG(6) debugf("C: %s\n", reply64);
640 g_free(reply64);
641 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
642 if ((ok = check_response(psb, TRUE))) {
643 resp64 = get_response_arg(&(psb->buffer[4]));
644 DEBUG(5) debugf(" encoded response = %s\n", resp64);
645 resp = base64_decode(resp64, &resp_size);
646 g_free(resp64);
647 DEBUG(5) debugf(" decoded response = %s, size = %d\n", resp, resp_size);
648 g_free(resp);
649 reply64 = base64_encode(psb->auth_secret, strlen(psb->auth_secret));
650 fprintf(psb->out, "%s\r\n", reply64);
651 fflush(psb->out);
652 DEBUG(6) debugf("C: %s\n", reply64);
653 g_free(reply64);
654 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
655 ok = check_response(psb, FALSE);
656 }
657 }
658 }
659 }
660 return ok;
661 }
663 gboolean
664 smtp_out_auth(smtp_base * psb)
665 {
666 gboolean ok = FALSE;
667 gint i = 0;
668 while (psb->auth_names[i]) {
669 if (strcasecmp(psb->auth_names[i], psb->auth_name) == 0)
670 break;
671 i++;
672 }
673 if (psb->auth_names[i]) {
674 if (strcasecmp(psb->auth_name, "cram-md5") == 0) {
675 smtp_out_auth_cram_md5(psb);
676 } else if (strcasecmp(psb->auth_name, "login") == 0) {
677 smtp_out_auth_login(psb);
678 } else {
679 logwrite(LOG_ERR, "auth method %s not supported\n", psb->auth_name);
680 }
681 } else {
682 logwrite(LOG_ERR, "no auth method %s found.\n", psb->auth_name);
683 }
684 return ok;
685 }
687 #endif
689 gboolean
690 smtp_out_init(smtp_base * psb, gboolean instant_helo)
691 {
692 gboolean ok;
694 logwrite(LOG_INFO, "smtp_out_init(): instant_helo:%d\n", instant_helo);
696 if (instant_helo) {
697 /* we say hello right away, hence we don't know if
698 ESMTP is supported; we just assume it */
699 psb->use_esmtp = 1;
700 } else {
701 if ((ok = read_response(psb, SMTP_INITIAL_TIMEOUT))) {
702 ok = check_init_response(psb);
703 }
704 if (!ok) {
705 smtp_out_log_failure(psb, NULL);
706 return ok;
707 }
708 }
710 if ((ok = smtp_helo(psb, psb->helo_name))) {
711 #ifdef ENABLE_AUTH
712 if (psb->auth_name && psb->use_auth) {
713 /* we completely disregard the response of server here. If
714 authentication fails, the server will complain later
715 anyway. I know, this is not polite... */
716 smtp_out_auth(psb);
717 }
718 #endif
719 }
720 if (!ok)
721 smtp_out_log_failure(psb, NULL);
722 return ok;
723 }
725 gint
726 smtp_out_msg(smtp_base * psb, message * msg, address * return_path, GList * rcpt_list, GList * hdr_list)
727 {
728 gint i, size;
729 gboolean ok = TRUE;
730 int rcpt_cnt;
731 int rcpt_accept = 0;
733 DEBUG(5) debugf("smtp_out_msg entered\n");
735 /* defaults: */
736 if (return_path == NULL)
737 return_path = msg->return_path;
738 if (hdr_list == NULL)
739 hdr_list = msg->hdr_list;
740 if (rcpt_list == NULL)
741 rcpt_list = msg->rcpt_list;
742 rcpt_cnt = g_list_length(rcpt_list);
744 size = msg_calc_size(msg, TRUE);
746 /* respect maximum size given by server: */
747 if ((psb->max_size > 0) && (size > psb->max_size)) {
748 logwrite(LOG_WARNING, "%s == host=%s message size (%d) > "
749 "fixed maximum message size of server (%d)",
750 msg->uid, psb->remote_host, size, psb->max_size);
751 psb->error = smtp_cancel;
752 ok = FALSE;
753 }
755 if (ok) {
756 /* pretend the message is a bit larger,
757 just in case the size calculation is buggy */
758 smtp_cmd_mailfrom(psb, return_path, psb->use_size ? size+SMTP_SIZE_ADD : 0);
760 if (!psb->use_pipelining) {
761 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
762 ok = check_response(psb, FALSE);
763 }
764 }
765 if (ok) {
766 GList *rcpt_node;
767 rcpt_accept = 0;
769 for (rcpt_node = g_list_first(rcpt_list); rcpt_node != NULL; rcpt_node = g_list_next(rcpt_node)) {
770 address *rcpt = (address *) (rcpt_node->data);
771 smtp_cmd_rcptto(psb, rcpt);
772 if (!psb->use_pipelining) {
773 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
774 if (check_response(psb, FALSE)) {
775 rcpt_accept++;
776 addr_mark_delivered(rcpt);
777 } else {
778 /* if server returned an error for one recp. we
779 may still try the others. But if it is a timeout, eof
780 or unexpected response, it is more serious and we should
781 give up. */
782 if ((psb->error != smtp_trylater) && (psb->error != smtp_fail)) {
783 ok = FALSE;
784 break;
785 } else {
786 logwrite(LOG_NOTICE, "%s == %s host=%s failed: %s\n",
787 msg->uid, addr_string(rcpt), psb->remote_host, psb->buffer);
788 if (psb->error == smtp_trylater) {
789 addr_mark_defered(rcpt);
790 } else {
791 addr_mark_failed(rcpt);
792 }
793 }
794 } else
795 break;
796 }
797 }
799 /* There is no point in going on if no recp.s were accpted.
800 But we can check that at this point only if not pipelining: */
801 ok = (ok && (psb->use_pipelining || (rcpt_accept > 0)));
802 if (ok) {
804 fprintf(psb->out, "DATA\r\n");
805 fflush(psb->out);
807 DEBUG(4) debugf("C: DATA\r\n");
809 if (psb->use_pipelining) {
810 /* the first pl'ed command was MAIL FROM
811 the last was DATA, whose response can be handled by the 'normal' code
812 all in between were RCPT TO:
813 */
814 /* response to MAIL FROM: */
815 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
816 if ((ok = check_response(psb, FALSE))) {
818 /* response(s) to RCPT TO:
819 this is very similar to the sequence above for no pipeline
820 */
821 for (i = 0; i < rcpt_cnt; i++) {
822 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
823 address *rcpt = g_list_nth_data(rcpt_list, i);
824 if (check_response(psb, FALSE)) {
825 rcpt_accept++;
826 addr_mark_delivered(rcpt);
827 } else {
828 /* if server returned an error 4xx or 5xx for one recp. we
829 may still try the others. But if it is a timeout, eof
830 or unexpected response, it is more serious and we
831 should give up. */
832 if ((psb->error != smtp_trylater) &&
833 (psb->error != smtp_fail)) {
834 ok = FALSE;
835 break;
836 } else {
837 logwrite(LOG_NOTICE, "%s == %s host=%s failed: %s\n", msg->uid,
838 addr_string(rcpt), psb->remote_host, psb->buffer);
839 if (psb->error == smtp_trylater) {
840 addr_mark_defered(rcpt);
841 } else {
842 addr_mark_failed(rcpt);
843 }
844 }
845 }
846 } else {
847 DEBUG(5) debugf("check_response failed after RCPT TO\n");
848 break;
849 }
850 }
851 if (rcpt_accept == 0)
852 ok = FALSE;
853 } else {
854 DEBUG(5) debugf("check_response failed after MAIL FROM\n");
855 }
856 } else {
857 DEBUG(5)
858 debugf("read_response failed after MAIL FROM\n");
859 }
860 }
862 /* if(psb->use_pipelining) */
863 /* response to the DATA cmd */
864 if (ok) {
865 if (read_response(psb, SMTP_DATA_TIMEOUT)) {
866 if (check_response(psb, TRUE)) {
867 send_header(psb, hdr_list);
868 send_data(psb, msg);
870 if (read_response(psb, SMTP_FINAL_TIMEOUT))
871 ok = check_response(psb, FALSE);
872 }
873 }
874 }
875 }
876 }
878 DEBUG(5) {
879 debugf("smtp_out_msg():\n");
880 debugf(" psb->error = %d\n", psb->error);
881 debugf(" ok = %d\n", ok);
882 debugf(" rcpt_accept = %d\n", rcpt_accept);
883 }
885 if (psb->error == smtp_ok) {
886 GList *rcpt_node;
887 for (rcpt_node = g_list_first(rcpt_list); rcpt_node; rcpt_node = g_list_next(rcpt_node)) {
888 address *rcpt = (address *) (rcpt_node->data);
889 if (addr_is_delivered(rcpt))
890 logwrite(LOG_NOTICE, "%s => %s host=%s with %s\n", msg->uid, addr_string(rcpt),
891 psb->remote_host, psb->use_esmtp ? "esmtp" : "smtp");
892 }
893 } else {
894 /* if something went wrong,
895 we have to unmark the rcpts prematurely marked as delivered
896 and mark the status */
897 smtp_out_mark_rcpts(psb, rcpt_list);
899 /* log the failure: */
900 smtp_out_log_failure(psb, msg);
901 }
902 return rcpt_accept;
903 }
905 gboolean
906 smtp_out_quit(smtp_base * psb)
907 {
908 fprintf(psb->out, "QUIT\r\n");
909 fflush(psb->out);
911 DEBUG(4) debugf("C: QUIT\n");
913 signal(SIGALRM, SIG_DFL);
915 return TRUE;
916 }
918 gint
919 smtp_deliver(gchar * host, gint port, GList * resolve_list, message * msg, address * return_path, GList * rcpt_list)
920 {
921 smtp_base *psb;
922 smtp_error err;
924 DEBUG(5) debugf("smtp_deliver entered\n");
926 if (return_path == NULL)
927 return_path = msg->return_path;
929 if ((psb = smtp_out_open(host, port, resolve_list))) {
930 set_heloname(psb, return_path->domain, TRUE);
931 /* initiate connection, send message and quit: */
932 if (smtp_out_init(psb, FALSE)) {
933 smtp_out_msg(psb, msg, return_path, rcpt_list, NULL);
934 if (psb->error == smtp_ok || (psb->error == smtp_fail) || (psb->error == smtp_trylater)
935 || (psb->error == smtp_syntax) || (psb->error == smtp_cancel))
936 smtp_out_quit(psb);
937 }
939 err = psb->error;
940 destroy_smtpbase(psb);
942 return err;
943 }
944 return -1;
945 }