1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
/*
* Copyright (c) 2011-2014 Balabit
* Copyright (c) 2011-2014 Gergely Nagy <algernon@balabit.hu>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "afsmtp.h"
#include "afsmtp-parser.h"
#include "plugin.h"
#include "messages.h"
#include "stats/stats-registry.h"
#include "logqueue.h"
#include "plugin-types.h"
#include "logthrdest/logthrdestdrv.h"
#include <libesmtp.h>
#include <signal.h>
typedef struct
{
gchar *name;
LogTemplate *template_obj;
} AFSMTPHeader;
typedef struct
{
gchar *phrase;
LogTemplate *template_obj;
afsmtp_rcpt_type_t type;
} AFSMTPRecipient;
typedef struct
{
LogThreadedDestDriver super;
/* Shared between main/writer; only read by the writer, never
written */
gchar *host;
gint port;
AFSMTPRecipient *mail_from;
GList *rcpt_tos;
GList *headers;
LogTemplate *subject_template;
LogTemplate *body_template;
/* Writer-only stuff */
GString *str;
LogTemplateOptions template_options;
} AFSMTPDriver;
typedef struct
{
gboolean success;
AFSMTPDriver *driver;
} LogRcptStatusData;
static gchar *
afsmtp_wash_string (gchar *str)
{
gint i;
for (i = 0; i < strlen (str); i++)
if (str[i] == '\n' ||
str[i] == '\r')
str[i] = ' ';
return str;
}
LogTemplateOptions *
afsmtp_dd_get_template_options(LogDriver *d)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
return &self->template_options;
}
/*
* Configuration
*/
void
afsmtp_dd_set_host(LogDriver *d, const gchar *host)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
g_free(self->host);
self->host = g_strdup (host);
}
void
afsmtp_dd_set_port(LogDriver *d, gint port)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
self->port = (int)port;
}
void
afsmtp_dd_set_subject(LogDriver *d, LogTemplate *subject)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
log_template_unref(self->subject_template);
self->subject_template = log_template_ref(subject);
}
void
afsmtp_dd_set_from(LogDriver *d, LogTemplate *phrase, LogTemplate *mbox)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
g_free(self->mail_from->phrase);
self->mail_from->phrase = afsmtp_wash_string(g_strdup(phrase->template_str));
log_template_unref(self->mail_from->template_obj);
self->mail_from->template_obj = log_template_ref(mbox);
}
void
afsmtp_dd_add_rcpt(LogDriver *d, afsmtp_rcpt_type_t type, LogTemplate *phrase,
LogTemplate *mbox)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
AFSMTPRecipient *rcpt;
rcpt = g_new0(AFSMTPRecipient, 1);
rcpt->phrase = afsmtp_wash_string(g_strdup(phrase->template_str));
log_template_unref(rcpt->template_obj);
rcpt->template_obj = log_template_ref(mbox);
rcpt->type = type;
self->rcpt_tos = g_list_append(self->rcpt_tos, rcpt);
}
void
afsmtp_dd_set_body(LogDriver *d, LogTemplate *body)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
log_template_unref(self->body_template);
self->body_template = log_template_ref(body);
}
gboolean
afsmtp_dd_add_header(LogDriver *d, const gchar *header, LogTemplate *value)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
AFSMTPHeader *h;
if (!g_ascii_strcasecmp(header, "to") ||
!g_ascii_strcasecmp(header, "cc") ||
!g_ascii_strcasecmp(header, "bcc") ||
!g_ascii_strcasecmp(header, "from") ||
!g_ascii_strcasecmp(header, "sender") ||
!g_ascii_strcasecmp(header, "reply-to") ||
!g_ascii_strcasecmp(header, "date"))
return FALSE;
h = g_new0(AFSMTPHeader, 1);
h->name = g_strdup(header);
log_template_unref(h->template_obj);
h->template_obj = log_template_ref(value);
self->headers = g_list_append(self->headers, h);
return TRUE;
}
/*
* Utilities
*/
void
ignore_sigpipe (void)
{
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGPIPE, &sa, NULL);
}
static const gchar *
afsmtp_dd_format_stats_key(LogThreadedDestDriver *d, StatsClusterKeyBuilder *kb)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("driver", "smtp"));
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("host", self->host));
gchar num[64];
g_snprintf(num, sizeof(num), "%u", self->port);
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("port", num));
return NULL;
}
static const gchar *
afsmtp_dd_format_persist_name(const LogPipe *s)
{
const AFSMTPDriver *self = (const AFSMTPDriver *)s;
static gchar persist_name[1024];
if (s->persist_name)
g_snprintf(persist_name, sizeof(persist_name), "smtp.%s", s->persist_name);
else
g_snprintf(persist_name, sizeof(persist_name), "smtp(%s,%u)", self->host, self->port);
return persist_name;
}
/*
* Worker thread
*/
static void
_smtp_message_add_recipient_header(smtp_message_t self, AFSMTPRecipient *rcpt, AFSMTPDriver *driver)
{
const gchar *hdr;
switch (rcpt->type)
{
case AFSMTP_RCPT_TYPE_TO:
hdr = "To";
break;
case AFSMTP_RCPT_TYPE_CC:
hdr = "Cc";
break;
case AFSMTP_RCPT_TYPE_REPLY_TO:
hdr = "Reply-To";
break;
default:
return;
}
smtp_set_header(self, hdr, rcpt->phrase, afsmtp_wash_string (driver->str->str));
smtp_set_header_option(self, hdr, Hdr_OVERRIDE, 1);
}
static void
_smtp_message_add_recipient_from_template(smtp_message_t self, AFSMTPDriver *driver, LogTemplate *template_obj,
LogMessage *msg)
{
LogTemplateEvalOptions options = {&driver->template_options, LTZ_SEND,
driver->super.worker.instance.seq_num, NULL, LM_VT_STRING
};
log_template_format(template_obj, msg, &options, driver->str);
smtp_add_recipient(self, afsmtp_wash_string (driver->str->str));
}
static void
afsmtp_dd_msg_add_recipient(AFSMTPRecipient *rcpt, gpointer user_data)
{
AFSMTPDriver *self = ((gpointer *)user_data)[0];
LogMessage *msg = ((gpointer *)user_data)[1];
smtp_message_t message = ((gpointer *)user_data)[2];
_smtp_message_add_recipient_from_template(message, self, rcpt->template_obj, msg);
_smtp_message_add_recipient_header(message, rcpt, self);
}
static void
afsmtp_dd_msg_add_header(AFSMTPHeader *hdr, gpointer user_data)
{
AFSMTPDriver *self = ((gpointer *)user_data)[0];
LogMessage *msg = ((gpointer *)user_data)[1];
smtp_message_t message = ((gpointer *)user_data)[2];
LogTemplateEvalOptions options = {&self->template_options, LTZ_LOCAL,
self->super.worker.instance.seq_num, NULL, LM_VT_STRING
};
log_template_format(hdr->template_obj, msg, &options, self->str);
smtp_set_header(message, hdr->name, afsmtp_wash_string (self->str->str), NULL);
smtp_set_header_option(message, hdr->name, Hdr_OVERRIDE, 1);
}
static void
afsmtp_dd_log_rcpt_status(smtp_recipient_t rcpt, const char *mailbox,
gpointer user_data)
{
const smtp_status_t *status;
LogRcptStatusData *status_data = (LogRcptStatusData *)user_data;
status = smtp_recipient_status(rcpt);
if (status->code != 250)
{
status_data->success = FALSE;
msg_error("SMTP recipient result",
evt_tag_str("driver", status_data->driver->super.super.super.id),
evt_tag_str("recipient", mailbox),
evt_tag_int("code", status->code),
evt_tag_str("text", status->text));
}
else
{
msg_debug("SMTP recipient result",
evt_tag_str("driver", status_data->driver->super.super.super.id),
evt_tag_str("recipient", mailbox),
evt_tag_int("code", status->code),
evt_tag_str("text", status->text));
}
}
static void
afsmtp_dd_cb_event(smtp_session_t session, int event, AFSMTPDriver *self)
{
switch (event)
{
case SMTP_EV_CONNECT:
msg_verbose("Connected to SMTP server",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("host", self->host),
evt_tag_int("port", self->port));
break;
case SMTP_EV_MAILSTATUS:
case SMTP_EV_RCPTSTATUS:
case SMTP_EV_MESSAGEDATA:
case SMTP_EV_MESSAGESENT:
/* Ignore */
break;
case SMTP_EV_DISCONNECT:
msg_verbose("Disconnected from SMTP server",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("host", self->host),
evt_tag_int("port", self->port));
break;
default:
msg_verbose("Unknown SMTP event",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_int("event_id", event));
break;
}
}
static void
afsmtp_dd_cb_monitor(const gchar *buf, gint buflen, gint writing,
AFSMTPDriver *self)
{
switch (writing)
{
case SMTP_CB_READING:
msg_debug ("SMTP Session: SERVER",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_mem("message", buf, buflen));
break;
case SMTP_CB_WRITING:
msg_debug("SMTP Session: CLIENT",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_mem("message", buf, buflen));
break;
case SMTP_CB_HEADERS:
msg_debug("SMTP Session: HEADERS",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_mem("data", buf, buflen));
break;
default:
break;
}
}
static smtp_session_t
__build_session(AFSMTPDriver *self, LogMessage *msg)
{
smtp_session_t session;
session = smtp_create_session();
g_string_printf(self->str, "%s:%d", self->host, self->port);
smtp_set_server(session, self->str->str);
smtp_set_eventcb(session, (smtp_eventcb_t) afsmtp_dd_cb_event, (void *) self);
smtp_set_monitorcb(session, (smtp_monitorcb_t) afsmtp_dd_cb_monitor, (void *) self, 1);
return session;
}
static smtp_message_t
__build_message(AFSMTPDriver *self, LogMessage *msg, smtp_session_t session)
{
smtp_message_t message;
gpointer args[] = { self, NULL, NULL };
message = smtp_add_message(session);
LogTemplateEvalOptions options = {&self->template_options, LTZ_SEND,
self->super.worker.instance.seq_num, NULL, LM_VT_STRING
};
log_template_format(self->mail_from->template_obj, msg, &options, self->str);
smtp_set_reverse_path(message, afsmtp_wash_string(self->str->str));
/* Defaults */
smtp_set_header(message, "To", NULL, NULL);
smtp_set_header(message, "From", NULL, NULL);
LogTemplateEvalOptions eval_options = {&self->template_options, LTZ_SEND,
self->super.worker.instance.seq_num, NULL, LM_VT_STRING
};
log_template_format(self->subject_template, msg, &eval_options, self->str);
smtp_set_header(message, "Subject", afsmtp_wash_string(self->str->str));
smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1);
/* Add recipients */
args[1] = msg;
args[2] = message;
g_list_foreach(self->rcpt_tos, (GFunc) afsmtp_dd_msg_add_recipient, args);
/* Add custom header (overrides anything set before, or in the
body). */
g_list_foreach(self->headers, (GFunc) afsmtp_dd_msg_add_header, args);
/* Set the body.
*
* We add a header to the body, otherwise libesmtp will not
* recognise headers, and will append them to the end of the body.
*/
g_string_assign(self->str, "X-Mailer: syslog-ng " SYSLOG_NG_VERSION "\r\n\r\n");
log_template_append_format(self->body_template, msg, &eval_options, self->str);
smtp_set_message_str(message, self->str->str);
return message;
}
static gboolean
__check_transfer_status(AFSMTPDriver *self, smtp_message_t message)
{
LogRcptStatusData status_data;
const smtp_status_t *status = smtp_message_transfer_status(message);
status_data.success = TRUE;
status_data.driver = self;
if (status->code != 250)
{
status_data.success = FALSE;
msg_error("Failed to send message",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_int("code", status->code),
evt_tag_str("text", status->text));
}
else
{
msg_debug("SMTP result",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_int("code", status->code),
evt_tag_str("text", status->text));
smtp_enumerate_recipients(message, afsmtp_dd_log_rcpt_status, &status_data);
}
return status_data.success;
}
static gboolean
__send_message(AFSMTPDriver *self, smtp_session_t session)
{
gboolean success = smtp_start_session(session);
if (!(success))
{
gchar error[1024] = {0};
smtp_strerror(smtp_errno(), error, sizeof (error) - 1);
msg_error("SMTP server error, suspending",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("error", error),
evt_tag_int("time_reopen", self->super.time_reopen));
success = FALSE;
}
return success;
}
static LogThreadedResult
afsmtp_worker_insert(LogThreadedDestDriver *s, LogMessage *msg)
{
AFSMTPDriver *self = (AFSMTPDriver *)s;
if (msg->flags & LF_MARK)
{
msg_debug("Mark messages are dropped by SMTP destination",
evt_tag_str("driver", self->super.super.super.id));
return LTR_SUCCESS;
}
smtp_session_t session = __build_session(self, msg);
smtp_message_t message = __build_message(self, msg, session);
gboolean message_sent = __send_message(self, session);
gboolean success = message_sent && __check_transfer_status(self, message);
smtp_destroy_session(session);
if (!success)
{
if (!message_sent)
return LTR_NOT_CONNECTED;
else
return LTR_ERROR;
}
return LTR_SUCCESS;
}
static void
afsmtp_worker_thread_init(LogThreadedDestDriver *d)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
self->str = g_string_sized_new(1024);
ignore_sigpipe();
}
static void
afsmtp_worker_thread_deinit(LogThreadedDestDriver *d)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
g_string_free(self->str, TRUE);
}
/*
* Main thread
*/
static gboolean
__check_rcpt_tos(AFSMTPDriver *self)
{
gboolean result = FALSE;
GList *l = self->rcpt_tos;
while (l)
{
AFSMTPRecipient *rcpt = (AFSMTPRecipient *)l->data;
gboolean rcpt_type_accepted = rcpt->type == AFSMTP_RCPT_TYPE_BCC ||
rcpt->type == AFSMTP_RCPT_TYPE_CC ||
rcpt->type == AFSMTP_RCPT_TYPE_TO;
if (rcpt->template_obj && rcpt_type_accepted)
{
result = TRUE;
break;
}
l = l->next;
}
return result;
}
static gboolean
__check_required_options(AFSMTPDriver *self)
{
if (!self->mail_from->template_obj)
{
msg_error("Error: from or sender option is required",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
if (!__check_rcpt_tos(self))
{
msg_error("Error: to or bcc option is required",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
if (!self->subject_template)
{
msg_error("Error: subject is required option",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
if (!self->body_template)
{
msg_error("Error: body is required option",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
return TRUE;
}
static gboolean
afsmtp_dd_init(LogPipe *s)
{
AFSMTPDriver *self = (AFSMTPDriver *)s;
GlobalConfig *cfg = log_pipe_get_config(s);
msg_verbose("Initializing SMTP destination",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("host", self->host),
evt_tag_int("port", self->port));
if (!__check_required_options(self))
return FALSE;
if (!log_threaded_dest_driver_init_method(s))
return FALSE;
log_template_options_init(&self->template_options, cfg);
return TRUE;
}
static void
afsmtp_dd_free(LogPipe *d)
{
AFSMTPDriver *self = (AFSMTPDriver *)d;
GList *l;
g_free(self->host);
g_free(self->mail_from->phrase);
log_template_unref(self->mail_from->template_obj);
g_free(self->mail_from);
log_template_unref(self->subject_template);
log_template_unref(self->body_template);
l = self->rcpt_tos;
while (l)
{
AFSMTPRecipient *rcpt = (AFSMTPRecipient *)l->data;
g_free(rcpt->phrase);
log_template_unref(rcpt->template_obj);
g_free(rcpt);
l = g_list_delete_link(l, l);
}
l = self->headers;
while (l)
{
AFSMTPHeader *hdr = (AFSMTPHeader *)l->data;
g_free(hdr->name);
log_template_unref(hdr->template_obj);
g_free(hdr);
l = g_list_delete_link(l, l);
}
log_template_options_destroy(&self->template_options);
log_threaded_dest_driver_free(d);
}
/*
* Plugin glue.
*/
LogDriver *
afsmtp_dd_new(GlobalConfig *cfg)
{
AFSMTPDriver *self = g_new0(AFSMTPDriver, 1);
log_threaded_dest_driver_init_instance(&self->super, cfg);
self->super.super.super.super.init = afsmtp_dd_init;
self->super.super.super.super.free_fn = afsmtp_dd_free;
self->super.super.super.super.generate_persist_name = afsmtp_dd_format_persist_name;
self->super.worker.thread_init = afsmtp_worker_thread_init;
self->super.worker.thread_deinit = afsmtp_worker_thread_deinit;
self->super.worker.insert = afsmtp_worker_insert;
self->super.format_stats_key = afsmtp_dd_format_stats_key;
self->super.stats_source = stats_register_type("smtp");
afsmtp_dd_set_host((LogDriver *)self, "127.0.0.1");
afsmtp_dd_set_port((LogDriver *)self, 25);
self->mail_from = g_new0(AFSMTPRecipient, 1);
log_template_options_defaults(&self->template_options);
return (LogDriver *)self;
}
extern CfgParser afsmtp_dd_parser;
static Plugin afsmtp_plugin =
{
.type = LL_CONTEXT_DESTINATION,
.name = "smtp",
.parser = &afsmtp_parser,
};
gboolean
afsmtp_module_init(PluginContext *context, CfgArgs *args)
{
plugin_register(context, &afsmtp_plugin, 1);
return TRUE;
}
const ModuleInfo module_info =
{
.canonical_name = "afsmtp",
.version = SYSLOG_NG_VERSION,
.description = "The afsmtp module provides SMTP destination support for syslog-ng.",
.core_revision = SYSLOG_NG_SOURCE_REVISION,
.plugins = &afsmtp_plugin,
.plugins_len = 1,
};
|