comparison src/smtp_out.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents 41958685480d
children f122535c589e
comparison
equal deleted inserted replaced
366:41958685480d 367:b27f66555ba8
1 /* smtp_out.c
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
4
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 */
19
20 /* 1 /*
21 I always forget these rfc numbers: 2 ** smtp_out.c
22 RFC 821 (SMTP) 3 ** Copyright (C) 1999-2001 Oliver Kurth
23 RFC 1869 (ESMTP) 4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
24 RFC 1870 (ESMTP SIZE) 5 **
25 RFC 2197 (ESMTP PIPELINE) 6 ** This program is free software; you can redistribute it and/or modify
26 RFC 2554 (ESMTP AUTH) 7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22 ** I always forget these rfc numbers:
23 ** RFC 821 (SMTP)
24 ** RFC 1869 (ESMTP)
25 ** RFC 1870 (ESMTP SIZE)
26 ** RFC 2197 (ESMTP PIPELINE)
27 ** RFC 2554 (ESMTP AUTH)
27 */ 28 */
28 29
29 #include "masqmail.h" 30 #include "masqmail.h"
30 #include "smtp_out.h" 31 #include "smtp_out.h"
31 #include "readsock.h" 32 #include "readsock.h"
71 DEBUG(5) debugf("socket: name.sin_addr = %s\n", inet_ntoa(sname.sin_addr)); 72 DEBUG(5) debugf("socket: name.sin_addr = %s\n", inet_ntoa(sname.sin_addr));
72 host_entry = gethostbyaddr((const char *) &(sname.sin_addr), sizeof(sname.sin_addr), AF_INET); 73 host_entry = gethostbyaddr((const char *) &(sname.sin_addr), sizeof(sname.sin_addr), AF_INET);
73 if (host_entry) { 74 if (host_entry) {
74 psb->helo_name = g_strdup(host_entry->h_name); 75 psb->helo_name = g_strdup(host_entry->h_name);
75 } else { 76 } else {
76 /* we failed to look up our own name. Instead of giving our local hostname, 77 /*
77 we may give our IP number to show the server that we are at least 78 ** we failed to look up our own name. Instead of
78 willing to be honest. For the really picky ones. */ 79 ** giving our local hostname, we may give our IP
80 ** number to show the server that we are at least
81 ** willing to be honest. For the really picky ones.
82 */
79 DEBUG(5) debugf("failed to look up own host name.\n"); 83 DEBUG(5) debugf("failed to look up own host name.\n");
80 psb->helo_name = g_strdup_printf("[%s]", inet_ntoa(sname.sin_addr)); 84 psb->helo_name = g_strdup_printf("[%s]", inet_ntoa(sname.sin_addr));
81 } 85 }
82 DEBUG(5) debugf("helo_name = %s\n", psb->helo_name); 86 DEBUG(5) debugf("helo_name = %s\n", psb->helo_name);
83 } 87 }
274 278
275 return TRUE; 279 return TRUE;
276 } 280 }
277 281
278 /* 282 /*
279 We first try EHLO, but if it fails HELO in a second fall back try. 283 ** We first try EHLO, but if it fails HELO in a second fall back try.
280 This is what is requested by RFC 2821 (sec 3.2): 284 ** This is what is requested by RFC 2821 (sec 3.2):
281 285 **
282 Once the server has sent the welcoming message and 286 ** Once the server has sent the welcoming message and
283 the client has received it, the client normally sends 287 ** the client has received it, the client normally sends
284 the EHLO command to the server, [...] 288 ** the EHLO command to the server, [...]
285 For a particular connection attempt, if the server 289 ** For a particular connection attempt, if the server
286 returns a "command not recognized" response to EHLO, 290 ** returns a "command not recognized" response to EHLO,
287 the client SHOULD be able to fall back and send HELO. 291 ** the client SHOULD be able to fall back and send HELO.
288 292 **
289 Up to and including version 0.3.0 masqmail used ESMTP only if the 293 ** Up to and including version 0.3.0 masqmail used ESMTP only if the
290 string ``ESMTP'' appeared within the server's greeting message. This 294 ** string ``ESMTP'' appeared within the server's greeting message. This
291 made it impossible to use AUTH with servers that would send odd 295 ** made it impossible to use AUTH with servers that would send odd
292 greeting messages. 296 ** greeting messages.
293 */ 297 */
294 static gboolean 298 static gboolean
295 smtp_helo(smtp_base *psb, gchar *helo) 299 smtp_helo(smtp_base *psb, gchar *helo)
296 { 300 {
297 fprintf(psb->out, "EHLO %s\r\n", helo); 301 fprintf(psb->out, "EHLO %s\r\n", helo);
308 312
309 if (psb->error != smtp_fail) { 313 if (psb->error != smtp_fail) {
310 return FALSE; 314 return FALSE;
311 } 315 }
312 316
313 /* our guess that server understands EHLO could have been wrong, 317 /*
314 try again with HELO */ 318 ** our guess that server understands EHLO could have been wrong,
319 ** try again with HELO
320 */
315 321
316 fprintf(psb->out, "HELO %s\r\n", helo); 322 fprintf(psb->out, "HELO %s\r\n", helo);
317 fflush(psb->out); 323 fflush(psb->out);
318 DEBUG(4) debugf("C: HELO %s\r\n", helo); 324 DEBUG(4) debugf("C: HELO %s\r\n", helo);
319 325
355 } 361 }
356 362
357 static void 363 static void
358 send_data_line(smtp_base *psb, gchar *data) 364 send_data_line(smtp_base *psb, gchar *data)
359 { 365 {
360 /* According to RFC 821 each line should be terminated with CRLF. 366 /*
361 Since a dot on a line itself marks the end of data, each line 367 ** According to RFC 821 each line should be terminated with CRLF.
362 beginning with a dot is prepended with another dot. 368 ** Since a dot on a line itself marks the end of data, each line
363 */ 369 ** beginning with a dot is prepended with another dot.
370 */
364 gchar *ptr; 371 gchar *ptr;
365 gboolean new_line = TRUE; /* previous versions assumed that each item was exactly one line. 372 gboolean new_line = TRUE; /* previous versions assumed that each item was exactly one line. This is no longer the case */
366 This is no longer the case */
367 373
368 ptr = data; 374 ptr = data;
369 while (*ptr) { 375 while (*ptr) {
370 int c = (int) (*ptr); 376 int c = (int) (*ptr);
371 if (c == '.' && new_line) { 377 if (c == '.' && new_line) {
700 smtp_out_log_failure(psb, NULL); 706 smtp_out_log_failure(psb, NULL);
701 return ok; 707 return ok;
702 } 708 }
703 709
704 gint 710 gint
705 smtp_out_msg(smtp_base *psb, message *msg, address *return_path, GList *rcpt_list, GList *hdr_list) 711 smtp_out_msg(smtp_base *psb, message *msg, address *return_path,
712 GList *rcpt_list, GList *hdr_list)
706 { 713 {
707 gint i, size; 714 gint i, size;
708 gboolean ok = TRUE; 715 gboolean ok = TRUE;
709 int rcpt_cnt; 716 int rcpt_cnt;
710 int rcpt_accept = 0; 717 int rcpt_accept = 0;
730 psb->error = smtp_cancel; 737 psb->error = smtp_cancel;
731 ok = FALSE; 738 ok = FALSE;
732 } 739 }
733 740
734 if (ok) { 741 if (ok) {
735 /* pretend the message is a bit larger, 742 /*
736 just in case the size calculation is buggy */ 743 ** pretend the message is a bit larger,
744 ** just in case the size calculation is buggy
745 */
737 smtp_cmd_mailfrom(psb, return_path, psb->use_size ? size+SMTP_SIZE_ADD : 0); 746 smtp_cmd_mailfrom(psb, return_path, psb->use_size ? size+SMTP_SIZE_ADD : 0);
738 747
739 if (!psb->use_pipelining) { 748 if (!psb->use_pipelining) {
740 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) 749 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT)))
741 ok = check_response(psb, FALSE); 750 ok = check_response(psb, FALSE);
773 } else 782 } else
774 break; 783 break;
775 } 784 }
776 } 785 }
777 786
778 /* There is no point in going on if no recp.s were accpted. 787 /*
779 But we can check that at this point only if not pipelining: */ 788 ** There is no point in going on if no recp.s were accpted.
789 ** But we can check that at this point only if not pipelining:
790 */
780 ok = (ok && (psb->use_pipelining || (rcpt_accept > 0))); 791 ok = (ok && (psb->use_pipelining || (rcpt_accept > 0)));
781 if (ok) { 792 if (ok) {
782 793
783 fprintf(psb->out, "DATA\r\n"); 794 fprintf(psb->out, "DATA\r\n");
784 fflush(psb->out); 795 fflush(psb->out);
785 796
786 DEBUG(4) debugf("C: DATA\r\n"); 797 DEBUG(4) debugf("C: DATA\r\n");
787 798
788 if (psb->use_pipelining) { 799 if (psb->use_pipelining) {
789 /* the first pl'ed command was MAIL FROM 800 /*
790 the last was DATA, whose response can be handled by the 'normal' code 801 ** the first pl'ed command was MAIL FROM
791 all in between were RCPT TO: 802 ** the last was DATA, whose response can be
792 */ 803 ** handled by the 'normal' code all in
804 ** between were RCPT TO:
805 */
793 /* response to MAIL FROM: */ 806 /* response to MAIL FROM: */
794 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) { 807 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
795 if ((ok = check_response(psb, FALSE))) { 808 if ((ok = check_response(psb, FALSE))) {
796 809
797 /* response(s) to RCPT TO: 810 /*
798 this is very similar to the sequence above for no pipeline 811 ** response(s) to RCPT TO:
799 */ 812 ** this is very similar to
813 ** the sequence above for no
814 ** pipeline
815 */
800 for (i = 0; i < rcpt_cnt; i++) { 816 for (i = 0; i < rcpt_cnt; i++) {
801 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) { 817 if ((ok = read_response(psb, SMTP_CMD_TIMEOUT))) {
802 address *rcpt = g_list_nth_data(rcpt_list, i); 818 address *rcpt = g_list_nth_data(rcpt_list, i);
803 if (check_response(psb, FALSE)) { 819 if (check_response(psb, FALSE)) {
804 rcpt_accept++; 820 rcpt_accept++;
805 addr_mark_delivered(rcpt); 821 addr_mark_delivered(rcpt);
806 } else { 822 } else {
807 /* if server returned an error 4xx or 5xx for one recp. we 823 /*
808 may still try the others. But if it is a timeout, eof 824 ** if server returned an error 4xx or 5xx for one recp. we
809 or unexpected response, it is more serious and we 825 ** may still try the others. But if it is a timeout, eof
810 should give up. */ 826 ** or unexpected response, it is more serious and we
827 ** should give up.
828 */
811 if ((psb->error != smtp_trylater) && 829 if ((psb->error != smtp_trylater) &&
812 (psb->error != smtp_fail)) { 830 (psb->error != smtp_fail)) {
813 ok = FALSE; 831 ok = FALSE;
814 break; 832 break;
815 } else { 833 } else {
868 if (addr_is_delivered(rcpt)) 886 if (addr_is_delivered(rcpt))
869 logwrite(LOG_NOTICE, "%s => %s host=%s\n", 887 logwrite(LOG_NOTICE, "%s => %s host=%s\n",
870 msg->uid, addr_string(rcpt), psb->remote_host); 888 msg->uid, addr_string(rcpt), psb->remote_host);
871 } 889 }
872 } else { 890 } else {
873 /* if something went wrong, 891 /*
874 we have to unmark the rcpts prematurely marked as delivered 892 ** if something went wrong,
875 and mark the status */ 893 ** we have to unmark the rcpts prematurely marked as
894 ** delivered and mark the status
895 */
876 smtp_out_mark_rcpts(psb, rcpt_list); 896 smtp_out_mark_rcpts(psb, rcpt_list);
877 897
878 /* log the failure: */ 898 /* log the failure: */
879 smtp_out_log_failure(psb, msg); 899 smtp_out_log_failure(psb, msg);
880 } 900 }
893 913
894 return TRUE; 914 return TRUE;
895 } 915 }
896 916
897 gint 917 gint
898 smtp_deliver(gchar *host, gint port, GList *resolve_list, message *msg, address *return_path, GList *rcpt_list) 918 smtp_deliver(gchar *host, gint port, GList *resolve_list, message *msg,
919 address *return_path, GList *rcpt_list)
899 { 920 {
900 smtp_base *psb; 921 smtp_base *psb;
901 smtp_error err; 922 smtp_error err;
902 923
903 DEBUG(5) debugf("smtp_deliver entered\n"); 924 DEBUG(5) debugf("smtp_deliver entered\n");