masqmail

annotate src/smtp_out.c @ 224:996b53a50f55

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