Simon Horman | 0d16a40 | 2015-01-30 11:22:58 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Mailer management. |
| 3 | * |
| 4 | * Copyright 2015 Horms Solutions Ltd, Simon Horman <horms@verge.net.au> |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 5 | * Copyright 2020 Willy Tarreau <w@1wt.eu> |
Simon Horman | 0d16a40 | 2015-01-30 11:22:58 +0900 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <stdlib.h> |
| 15 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
| 17 | #include <haproxy/api.h> |
| 18 | #include <haproxy/check.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 19 | #include <haproxy/errors.h> |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 20 | #include <haproxy/list.h> |
| 21 | #include <haproxy/mailers.h> |
| 22 | #include <haproxy/pool.h> |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 23 | #include <haproxy/proxy-t.h> |
| 24 | #include <haproxy/server-t.h> |
| 25 | #include <haproxy/task.h> |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 26 | #include <haproxy/tcpcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/thread.h> |
| 28 | #include <haproxy/time.h> |
| 29 | #include <haproxy/tools.h> |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 30 | |
Simon Horman | 0d16a40 | 2015-01-30 11:22:58 +0900 | [diff] [blame] | 31 | |
| 32 | struct mailers *mailers = NULL; |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 33 | |
| 34 | DECLARE_STATIC_POOL(pool_head_email_alert, "email_alert", sizeof(struct email_alert)); |
| 35 | |
| 36 | /****************************** Email alerts ******************************/ |
| 37 | /* NOTE: It may be pertinent to use an applet to handle email alerts */ |
| 38 | /* instead of a tcp-check ruleset */ |
| 39 | /**************************************************************************/ |
| 40 | void email_alert_free(struct email_alert *alert) |
| 41 | { |
| 42 | struct tcpcheck_rule *rule, *back; |
| 43 | |
| 44 | if (!alert) |
| 45 | return; |
| 46 | |
| 47 | if (alert->rules.list) { |
| 48 | list_for_each_entry_safe(rule, back, alert->rules.list, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 49 | LIST_DELETE(&rule->list); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 50 | free_tcpcheck(rule, 1); |
| 51 | } |
| 52 | free_tcpcheck_vars(&alert->rules.preset_vars); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 53 | ha_free(&alert->rules.list); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 54 | } |
| 55 | pool_free(pool_head_email_alert, alert); |
| 56 | } |
| 57 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 58 | static struct task *process_email_alert(struct task *t, void *context, unsigned int state) |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 59 | { |
| 60 | struct check *check = context; |
| 61 | struct email_alertq *q; |
| 62 | struct email_alert *alert; |
| 63 | |
| 64 | q = container_of(check, typeof(*q), check); |
| 65 | |
| 66 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 67 | while (1) { |
| 68 | if (!(check->state & CHK_ST_ENABLED)) { |
| 69 | if (LIST_ISEMPTY(&q->email_alerts)) { |
| 70 | /* All alerts processed, queue the task */ |
| 71 | t->expire = TICK_ETERNITY; |
| 72 | task_queue(t); |
| 73 | goto end; |
| 74 | } |
| 75 | |
| 76 | alert = LIST_NEXT(&q->email_alerts, typeof(alert), list); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 77 | LIST_DELETE(&alert->list); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 78 | t->expire = now_ms; |
| 79 | check->tcpcheck_rules = &alert->rules; |
| 80 | check->status = HCHK_STATUS_INI; |
| 81 | check->state |= CHK_ST_ENABLED; |
| 82 | } |
| 83 | |
| 84 | process_chk(t, context, state); |
| 85 | if (check->state & CHK_ST_INPROGRESS) |
| 86 | break; |
| 87 | |
| 88 | alert = container_of(check->tcpcheck_rules, typeof(*alert), rules); |
| 89 | email_alert_free(alert); |
| 90 | check->tcpcheck_rules = NULL; |
| 91 | check->server = NULL; |
| 92 | check->state &= ~CHK_ST_ENABLED; |
| 93 | } |
| 94 | end: |
| 95 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 96 | return t; |
| 97 | } |
| 98 | |
| 99 | /* Initializes mailer alerts for the proxy <p> using <mls> parameters. |
| 100 | * |
| 101 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 102 | * filled. |
| 103 | */ |
| 104 | int init_email_alert(struct mailers *mls, struct proxy *p, char **err) |
| 105 | { |
| 106 | struct mailer *mailer; |
| 107 | struct email_alertq *queues; |
| 108 | const char *err_str; |
| 109 | int i = 0; |
| 110 | |
| 111 | if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) { |
| 112 | memprintf(err, "out of memory while allocating mailer alerts queues"); |
| 113 | goto fail_no_queue; |
| 114 | } |
| 115 | |
| 116 | for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) { |
| 117 | struct email_alertq *q = &queues[i]; |
| 118 | struct check *check = &q->check; |
| 119 | struct task *t; |
| 120 | |
| 121 | LIST_INIT(&q->email_alerts); |
| 122 | HA_SPIN_INIT(&q->lock); |
Christopher Faulet | 58e3501 | 2022-06-08 09:17:14 +0200 | [diff] [blame] | 123 | check->obj_type = OBJ_TYPE_CHECK; |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 124 | check->inter = mls->timeout.mail; |
| 125 | check->rise = DEF_AGENT_RISETIME; |
| 126 | check->proxy = p; |
| 127 | check->fall = DEF_AGENT_FALLTIME; |
| 128 | if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) { |
| 129 | memprintf(err, "%s", err_str); |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | check->xprt = mailer->xprt; |
| 134 | check->addr = mailer->addr; |
| 135 | check->port = get_host_port(&mailer->addr); |
| 136 | |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 137 | if ((t = task_new_anywhere()) == NULL) { |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 138 | memprintf(err, "out of memory while allocating mailer alerts task"); |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | check->task = t; |
| 143 | t->process = process_email_alert; |
| 144 | t->context = check; |
| 145 | |
| 146 | /* check this in one ms */ |
| 147 | t->expire = TICK_ETERNITY; |
| 148 | check->start = now; |
| 149 | task_queue(t); |
| 150 | } |
| 151 | |
| 152 | mls->users++; |
| 153 | free(p->email_alert.mailers.name); |
| 154 | p->email_alert.mailers.m = mls; |
| 155 | p->email_alert.queues = queues; |
| 156 | return 0; |
| 157 | |
| 158 | error: |
| 159 | for (i = 0; i < mls->count; i++) { |
| 160 | struct email_alertq *q = &queues[i]; |
| 161 | struct check *check = &q->check; |
| 162 | |
| 163 | free_check(check); |
| 164 | } |
| 165 | free(queues); |
| 166 | fail_no_queue: |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | static int enqueue_one_email_alert(struct proxy *p, struct server *s, |
| 171 | struct email_alertq *q, const char *msg) |
| 172 | { |
| 173 | struct email_alert *alert; |
| 174 | struct tcpcheck_rule *tcpcheck; |
| 175 | struct check *check = &q->check; |
| 176 | |
| 177 | if ((alert = pool_alloc(pool_head_email_alert)) == NULL) |
| 178 | goto error; |
| 179 | LIST_INIT(&alert->list); |
| 180 | alert->rules.flags = TCPCHK_RULES_TCP_CHK; |
| 181 | alert->rules.list = calloc(1, sizeof(*alert->rules.list)); |
| 182 | if (!alert->rules.list) |
| 183 | goto error; |
| 184 | LIST_INIT(alert->rules.list); |
| 185 | LIST_INIT(&alert->rules.preset_vars); /* unused for email alerts */ |
| 186 | alert->srv = s; |
| 187 | |
Willy Tarreau | 3ab0a0b | 2021-03-22 21:07:52 +0100 | [diff] [blame] | 188 | if ((tcpcheck = pool_zalloc(pool_head_tcpcheck_rule)) == NULL) |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 189 | goto error; |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 190 | tcpcheck->action = TCPCHK_ACT_CONNECT; |
| 191 | tcpcheck->comment = NULL; |
| 192 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 193 | LIST_APPEND(alert->rules.list, &tcpcheck->list); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 194 | |
| 195 | if (!add_tcpcheck_expect_str(&alert->rules, "220 ")) |
| 196 | goto error; |
| 197 | |
| 198 | { |
Lukas Tribus | 1a16e4e | 2022-02-17 15:40:51 +0100 | [diff] [blame] | 199 | const char * const strs[4] = { "HELO ", p->email_alert.myhostname, "\r\n" }; |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 200 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 205 | goto error; |
| 206 | |
| 207 | { |
| 208 | const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" }; |
| 209 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 214 | goto error; |
| 215 | |
| 216 | { |
| 217 | const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" }; |
| 218 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 219 | goto error; |
| 220 | } |
| 221 | |
| 222 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 223 | goto error; |
| 224 | |
| 225 | { |
| 226 | const char * const strs[2] = { "DATA\r\n" }; |
| 227 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 228 | goto error; |
| 229 | } |
| 230 | |
| 231 | if (!add_tcpcheck_expect_str(&alert->rules, "354 ")) |
| 232 | goto error; |
| 233 | |
| 234 | { |
| 235 | struct tm tm; |
| 236 | char datestr[48]; |
| 237 | const char * const strs[18] = { |
| 238 | "From: ", p->email_alert.from, "\r\n", |
| 239 | "To: ", p->email_alert.to, "\r\n", |
| 240 | "Date: ", datestr, "\r\n", |
Willy Tarreau | 64975cf | 2021-05-09 06:45:16 +0200 | [diff] [blame] | 241 | "Subject: [HAProxy Alert] ", msg, "\r\n", |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 242 | "\r\n", |
| 243 | msg, "\r\n", |
| 244 | "\r\n", |
| 245 | ".\r\n", |
| 246 | NULL |
| 247 | }; |
| 248 | |
| 249 | get_localtime(date.tv_sec, &tm); |
| 250 | |
| 251 | if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) { |
| 252 | goto error; |
| 253 | } |
| 254 | |
| 255 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 260 | goto error; |
| 261 | |
| 262 | { |
| 263 | const char * const strs[2] = { "QUIT\r\n" }; |
| 264 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 265 | goto error; |
| 266 | } |
| 267 | |
| 268 | if (!add_tcpcheck_expect_str(&alert->rules, "221 ")) |
| 269 | goto error; |
| 270 | |
| 271 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 272 | task_wakeup(check->task, TASK_WOKEN_MSG); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 273 | LIST_APPEND(&q->email_alerts, &alert->list); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 274 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 275 | return 1; |
| 276 | |
| 277 | error: |
| 278 | email_alert_free(alert); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg) |
| 283 | { |
| 284 | int i; |
| 285 | struct mailer *mailer; |
| 286 | |
| 287 | for (i = 0, mailer = p->email_alert.mailers.m->mailer_list; |
| 288 | i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) { |
| 289 | if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) { |
| 290 | ha_alert("Email alert [%s] could not be enqueued: out of memory\n", p->id); |
| 291 | return; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Send email alert if configured. |
| 300 | */ |
| 301 | void send_email_alert(struct server *s, int level, const char *format, ...) |
| 302 | { |
| 303 | va_list argp; |
| 304 | char buf[1024]; |
| 305 | int len; |
| 306 | struct proxy *p = s->proxy; |
| 307 | |
| 308 | if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL) |
| 309 | return; |
| 310 | |
| 311 | va_start(argp, format); |
| 312 | len = vsnprintf(buf, sizeof(buf), format, argp); |
| 313 | va_end(argp); |
| 314 | |
| 315 | if (len < 0 || len >= sizeof(buf)) { |
| 316 | ha_alert("Email alert [%s] could not format message\n", p->id); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | enqueue_email_alert(p, s, buf); |
| 321 | } |