masqmail

annotate src/smtp_out.c @ 365:934a223e4ee8

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