blob: fc9d956c384e9ea819c1143a8d11ed5b89444a73 [file] [log] [blame]
Willy Tarreau51cd5952020-06-05 12:25:38 +02001/*
2 * Health-checks functions.
3 *
4 * Copyright 2000-2009,2020 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
6 * Copyright 2013 Baptiste Assmann <bedis9@gmail.com>
7 * Copyright 2020 Gaetan Rivet <grive@u256.net>
8 * Copyright 2020 Christopher Faulet <cfaulet@haproxy.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 */
16
17#include <sys/resource.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21#include <netinet/in.h>
22#include <netinet/tcp.h>
23#include <arpa/inet.h>
24
25#include <ctype.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <signal.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34#include <unistd.h>
35
36#include <haproxy/action.h>
37#include <haproxy/api.h>
38#include <haproxy/cfgparse.h>
39#include <haproxy/check.h>
40#include <haproxy/chunk.h>
41#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020042#include <haproxy/errors.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020043#include <haproxy/global.h>
44#include <haproxy/h1.h>
45#include <haproxy/http.h>
46#include <haproxy/http_htx.h>
47#include <haproxy/htx.h>
48#include <haproxy/istbuf.h>
49#include <haproxy/list.h>
50#include <haproxy/log.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020051#include <haproxy/protocol.h>
52#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/regex.h>
54#include <haproxy/sample.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020055#include <haproxy/server.h>
56#include <haproxy/ssl_sock.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020057#include <haproxy/task.h>
58#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/time.h>
60#include <haproxy/tools.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020061#include <haproxy/vars.h>
62
63
64/* Global tree to share all tcp-checks */
65struct eb_root shared_tcpchecks = EB_ROOT;
66
67
68DECLARE_POOL(pool_head_tcpcheck_rule, "tcpcheck_rule", sizeof(struct tcpcheck_rule));
69
70/**************************************************************************/
71/*************** Init/deinit tcp-check rules and ruleset ******************/
72/**************************************************************************/
73/* Releases memory allocated for a log-format string */
74static void free_tcpcheck_fmt(struct list *fmt)
75{
76 struct logformat_node *lf, *lfb;
77
78 list_for_each_entry_safe(lf, lfb, fmt, list) {
79 LIST_DEL(&lf->list);
80 release_sample_expr(lf->expr);
81 free(lf->arg);
82 free(lf);
83 }
84}
85
86/* Releases memory allocated for an HTTP header used in a tcp-check send rule */
87void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr)
88{
89 if (!hdr)
90 return;
91
92 free_tcpcheck_fmt(&hdr->value);
93 istfree(&hdr->name);
94 free(hdr);
95}
96
97/* Releases memory allocated for an HTTP header list used in a tcp-check send
98 * rule
99 */
100static void free_tcpcheck_http_hdrs(struct list *hdrs)
101{
102 struct tcpcheck_http_hdr *hdr, *bhdr;
103
104 list_for_each_entry_safe(hdr, bhdr, hdrs, list) {
105 LIST_DEL(&hdr->list);
106 free_tcpcheck_http_hdr(hdr);
107 }
108}
109
110/* Releases memory allocated for a tcp-check. If in_pool is set, it means the
111 * tcp-check was allocated using a memory pool (it is used to instantiate email
112 * alerts).
113 */
114void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool)
115{
116 if (!rule)
117 return;
118
119 free(rule->comment);
120 switch (rule->action) {
121 case TCPCHK_ACT_SEND:
122 switch (rule->send.type) {
123 case TCPCHK_SEND_STRING:
124 case TCPCHK_SEND_BINARY:
125 istfree(&rule->send.data);
126 break;
127 case TCPCHK_SEND_STRING_LF:
128 case TCPCHK_SEND_BINARY_LF:
129 free_tcpcheck_fmt(&rule->send.fmt);
130 break;
131 case TCPCHK_SEND_HTTP:
132 free(rule->send.http.meth.str.area);
133 if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT))
134 istfree(&rule->send.http.uri);
135 else
136 free_tcpcheck_fmt(&rule->send.http.uri_fmt);
137 istfree(&rule->send.http.vsn);
138 free_tcpcheck_http_hdrs(&rule->send.http.hdrs);
139 if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT))
140 istfree(&rule->send.http.body);
141 else
142 free_tcpcheck_fmt(&rule->send.http.body_fmt);
143 break;
144 case TCPCHK_SEND_UNDEF:
145 break;
146 }
147 break;
148 case TCPCHK_ACT_EXPECT:
149 free_tcpcheck_fmt(&rule->expect.onerror_fmt);
150 free_tcpcheck_fmt(&rule->expect.onsuccess_fmt);
151 release_sample_expr(rule->expect.status_expr);
152 switch (rule->expect.type) {
153 case TCPCHK_EXPECT_HTTP_STATUS:
154 free(rule->expect.codes.codes);
155 break;
156 case TCPCHK_EXPECT_STRING:
157 case TCPCHK_EXPECT_BINARY:
158 case TCPCHK_EXPECT_HTTP_BODY:
159 istfree(&rule->expect.data);
160 break;
161 case TCPCHK_EXPECT_STRING_REGEX:
162 case TCPCHK_EXPECT_BINARY_REGEX:
163 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
164 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
165 regex_free(rule->expect.regex);
166 break;
167 case TCPCHK_EXPECT_STRING_LF:
168 case TCPCHK_EXPECT_BINARY_LF:
169 case TCPCHK_EXPECT_HTTP_BODY_LF:
170 free_tcpcheck_fmt(&rule->expect.fmt);
171 break;
172 case TCPCHK_EXPECT_HTTP_HEADER:
173 if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG)
174 regex_free(rule->expect.hdr.name_re);
175 else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT)
176 free_tcpcheck_fmt(&rule->expect.hdr.name_fmt);
177 else
178 istfree(&rule->expect.hdr.name);
179
180 if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG)
181 regex_free(rule->expect.hdr.value_re);
182 else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT)
183 free_tcpcheck_fmt(&rule->expect.hdr.value_fmt);
184 else if (!(rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE))
185 istfree(&rule->expect.hdr.value);
186 break;
187 case TCPCHK_EXPECT_CUSTOM:
188 case TCPCHK_EXPECT_UNDEF:
189 break;
190 }
191 break;
192 case TCPCHK_ACT_CONNECT:
193 free(rule->connect.sni);
194 free(rule->connect.alpn);
195 release_sample_expr(rule->connect.port_expr);
196 break;
197 case TCPCHK_ACT_COMMENT:
198 break;
199 case TCPCHK_ACT_ACTION_KW:
200 free(rule->action_kw.rule);
201 break;
202 }
203
204 if (in_pool)
205 pool_free(pool_head_tcpcheck_rule, rule);
206 else
207 free(rule);
208}
209
210/* Creates a tcp-check variable used in preset variables before executing a
211 * tcp-check ruleset.
212 */
213struct tcpcheck_var *create_tcpcheck_var(const struct ist name)
214{
215 struct tcpcheck_var *var = NULL;
216
217 var = calloc(1, sizeof(*var));
218 if (var == NULL)
219 return NULL;
220
221 var->name = istdup(name);
222 if (!isttest(var->name)) {
223 free(var);
224 return NULL;
225 }
226
227 LIST_INIT(&var->list);
228 return var;
229}
230
231/* Releases memory allocated for a preset tcp-check variable */
232void free_tcpcheck_var(struct tcpcheck_var *var)
233{
234 if (!var)
235 return;
236
237 istfree(&var->name);
238 if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN)
239 free(var->data.u.str.area);
240 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER)
241 free(var->data.u.meth.str.area);
242 free(var);
243}
244
245/* Releases a list of preset tcp-check variables */
246void free_tcpcheck_vars(struct list *vars)
247{
248 struct tcpcheck_var *var, *back;
249
250 list_for_each_entry_safe(var, back, vars, list) {
251 LIST_DEL(&var->list);
252 free_tcpcheck_var(var);
253 }
254}
255
256/* Duplicate a list of preset tcp-check variables */
257int dup_tcpcheck_vars(struct list *dst, struct list *src)
258{
259 struct tcpcheck_var *var, *new = NULL;
260
261 list_for_each_entry(var, src, list) {
262 new = create_tcpcheck_var(var->name);
263 if (!new)
264 goto error;
265 new->data.type = var->data.type;
266 if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) {
267 if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL)
268 goto error;
269 if (var->data.type == SMP_T_STR)
270 new->data.u.str.area[new->data.u.str.data] = 0;
271 }
272 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
273 if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL)
274 goto error;
275 new->data.u.str.area[new->data.u.str.data] = 0;
276 new->data.u.meth.meth = var->data.u.meth.meth;
277 }
278 else
279 new->data.u = var->data.u;
280 LIST_ADDQ(dst, &new->list);
281 }
282 return 1;
283
284 error:
285 free(new);
286 return 0;
287}
288
289/* Looks for a shared tcp-check ruleset given its name. */
290struct tcpcheck_ruleset *find_tcpcheck_ruleset(const char *name)
291{
292 struct tcpcheck_ruleset *rs;
293 struct ebpt_node *node;
294
295 node = ebis_lookup_len(&shared_tcpchecks, name, strlen(name));
296 if (node) {
297 rs = container_of(node, typeof(*rs), node);
298 return rs;
299 }
300 return NULL;
301}
302
303/* Creates a new shared tcp-check ruleset and insert it in shared_tcpchecks
304 * tree.
305 */
306struct tcpcheck_ruleset *create_tcpcheck_ruleset(const char *name)
307{
308 struct tcpcheck_ruleset *rs;
309
310 rs = calloc(1, sizeof(*rs));
311 if (rs == NULL)
312 return NULL;
313
314 rs->node.key = strdup(name);
315 if (rs->node.key == NULL) {
316 free(rs);
317 return NULL;
318 }
319
320 LIST_INIT(&rs->rules);
321 ebis_insert(&shared_tcpchecks, &rs->node);
322 return rs;
323}
324
325/* Releases memory allocated by a tcp-check ruleset. */
326void free_tcpcheck_ruleset(struct tcpcheck_ruleset *rs)
327{
328 struct tcpcheck_rule *r, *rb;
329
330 if (!rs)
331 return;
332
333 ebpt_delete(&rs->node);
334 free(rs->node.key);
335 list_for_each_entry_safe(r, rb, &rs->rules, list) {
336 LIST_DEL(&r->list);
337 free_tcpcheck(r, 0);
338 }
339 free(rs);
340}
341
342
343/**************************************************************************/
344/**************** Everything about tcp-checks execution *******************/
345/**************************************************************************/
346/* Returns the id of a step in a tcp-check ruleset */
347int tcpcheck_get_step_id(struct check *check, struct tcpcheck_rule *rule)
348{
349 if (!rule)
350 rule = check->current_step;
351
352 /* no last started step => first step */
353 if (!rule)
354 return 1;
355
356 /* last step is the first implicit connect */
357 if (rule->index == 0 &&
358 rule->action == TCPCHK_ACT_CONNECT &&
359 (rule->connect.options & TCPCHK_OPT_IMPLICIT))
360 return 0;
361
362 return rule->index + 1;
363}
364
365/* Returns the first non COMMENT/ACTION_KW tcp-check rule from list <list> or
366 * NULL if none was found.
367 */
368struct tcpcheck_rule *get_first_tcpcheck_rule(struct tcpcheck_rules *rules)
369{
370 struct tcpcheck_rule *r;
371
372 list_for_each_entry(r, rules->list, list) {
373 if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
374 return r;
375 }
376 return NULL;
377}
378
379/* Returns the last non COMMENT/ACTION_KW tcp-check rule from list <list> or
380 * NULL if none was found.
381 */
382static struct tcpcheck_rule *get_last_tcpcheck_rule(struct tcpcheck_rules *rules)
383{
384 struct tcpcheck_rule *r;
385
386 list_for_each_entry_rev(r, rules->list, list) {
387 if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
388 return r;
389 }
390 return NULL;
391}
392
393/* Returns the non COMMENT/ACTION_KW tcp-check rule from list <list> following
394 * <start> or NULL if non was found. If <start> is NULL, it relies on
395 * get_first_tcpcheck_rule().
396 */
397static struct tcpcheck_rule *get_next_tcpcheck_rule(struct tcpcheck_rules *rules, struct tcpcheck_rule *start)
398{
399 struct tcpcheck_rule *r;
400
401 if (!start)
402 return get_first_tcpcheck_rule(rules);
403
404 r = LIST_NEXT(&start->list, typeof(r), list);
405 list_for_each_entry_from(r, rules->list, list) {
406 if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
407 return r;
408 }
409 return NULL;
410}
411
412
413/* Creates info message when a tcp-check healthcheck fails on an expect rule */
414static void tcpcheck_expect_onerror_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule,
415 int match, struct ist info)
416{
417 struct sample *smp;
418
419 /* Follows these step to produce the info message:
420 * 1. if info field is already provided, copy it
421 * 2. if the expect rule provides an onerror log-format string,
422 * use it to produce the message
423 * 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing
424 * 4. Otherwise produce the generic tcp-check info message
425 */
426 if (istlen(info)) {
427 chunk_strncat(msg, istptr(info), istlen(info));
428 goto comment;
429 }
430 else if (!LIST_ISEMPTY(&rule->expect.onerror_fmt)) {
431 msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), &rule->expect.onerror_fmt);
432 goto comment;
433 }
434
435 if (check->type == PR_O2_TCPCHK_CHK &&
436 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_TCP_CHK)
437 goto comment;
438
439 chunk_strcat(msg, (match ? "TCPCHK matched unwanted content" : "TCPCHK did not match content"));
440 switch (rule->expect.type) {
441 case TCPCHK_EXPECT_HTTP_STATUS:
442 chunk_appendf(msg, "(status codes) at step %d", tcpcheck_get_step_id(check, rule));
443 break;
444 case TCPCHK_EXPECT_STRING:
445 case TCPCHK_EXPECT_HTTP_BODY:
446 chunk_appendf(msg, " '%.*s' at step %d", (unsigned int)istlen(rule->expect.data), istptr(rule->expect.data),
447 tcpcheck_get_step_id(check, rule));
448 break;
449 case TCPCHK_EXPECT_BINARY:
450 chunk_appendf(msg, " (binary) at step %d", tcpcheck_get_step_id(check, rule));
451 break;
452 case TCPCHK_EXPECT_STRING_REGEX:
453 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
454 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
455 chunk_appendf(msg, " (regex) at step %d", tcpcheck_get_step_id(check, rule));
456 break;
457 case TCPCHK_EXPECT_BINARY_REGEX:
458 chunk_appendf(msg, " (binary regex) at step %d", tcpcheck_get_step_id(check, rule));
459 break;
460 case TCPCHK_EXPECT_STRING_LF:
461 case TCPCHK_EXPECT_HTTP_BODY_LF:
462 chunk_appendf(msg, " (log-format string) at step %d", tcpcheck_get_step_id(check, rule));
463 break;
464 case TCPCHK_EXPECT_BINARY_LF:
465 chunk_appendf(msg, " (log-format binary) at step %d", tcpcheck_get_step_id(check, rule));
466 break;
467 case TCPCHK_EXPECT_CUSTOM:
468 chunk_appendf(msg, " (custom function) at step %d", tcpcheck_get_step_id(check, rule));
469 break;
470 case TCPCHK_EXPECT_HTTP_HEADER:
471 chunk_appendf(msg, " (header pattern) at step %d", tcpcheck_get_step_id(check, rule));
472 case TCPCHK_EXPECT_UNDEF:
473 /* Should never happen. */
474 return;
475 }
476
477 comment:
478 /* If the failing expect rule provides a comment, it is concatenated to
479 * the info message.
480 */
481 if (rule->comment) {
482 chunk_strcat(msg, " comment: ");
483 chunk_strcat(msg, rule->comment);
484 }
485
486 /* Finally, the check status code is set if the failing expect rule
487 * defines a status expression.
488 */
489 if (rule->expect.status_expr) {
490 smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
491 rule->expect.status_expr, SMP_T_STR);
492
493 if (smp && sample_casts[smp->data.type][SMP_T_SINT] &&
494 sample_casts[smp->data.type][SMP_T_SINT](smp))
495 check->code = smp->data.u.sint;
496 }
497
498 *(b_tail(msg)) = '\0';
499}
500
501/* Creates info message when a tcp-check healthcheck succeeds on an expect rule */
502static void tcpcheck_expect_onsuccess_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule,
503 struct ist info)
504{
505 struct sample *smp;
506
507 /* Follows these step to produce the info message:
508 * 1. if info field is already provided, copy it
509 * 2. if the expect rule provides an onsucces log-format string,
510 * use it to produce the message
511 * 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing
512 * 4. Otherwise produce the generic tcp-check info message
513 */
514 if (istlen(info))
515 chunk_strncat(msg, istptr(info), istlen(info));
516 if (!LIST_ISEMPTY(&rule->expect.onsuccess_fmt))
517 msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg),
518 &rule->expect.onsuccess_fmt);
519 else if (check->type == PR_O2_TCPCHK_CHK &&
520 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK)
521 chunk_strcat(msg, "(tcp-check)");
522
523 /* Finally, the check status code is set if the expect rule defines a
524 * status expression.
525 */
526 if (rule->expect.status_expr) {
527 smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
528 rule->expect.status_expr, SMP_T_STR);
529
530 if (smp && sample_casts[smp->data.type][SMP_T_SINT] &&
531 sample_casts[smp->data.type][SMP_T_SINT](smp))
532 check->code = smp->data.u.sint;
533 }
534
535 *(b_tail(msg)) = '\0';
536}
537
538/* Internal functions to parse and validate a MySQL packet in the context of an
539 * expect rule. It start to parse the input buffer at the offset <offset>. If
540 * <last_read> is set, no more data are expected.
541 */
542static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check, struct tcpcheck_rule *rule,
543 unsigned int offset, int last_read)
544{
545 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
546 enum healthcheck_status status;
547 struct buffer *msg = NULL;
548 struct ist desc = IST_NULL;
549 unsigned int err = 0, plen = 0;
550
551
552 /* 3 Bytes for the packet length and 1 byte for the sequence id */
553 if (b_data(&check->bi) < offset+4) {
554 if (!last_read)
555 goto wait_more_data;
556
557 /* invalid length or truncated response */
558 status = HCHK_STATUS_L7RSP;
559 goto error;
560 }
561
562 plen = ((unsigned char) *b_peek(&check->bi, offset)) +
563 (((unsigned char) *(b_peek(&check->bi, offset+1))) << 8) +
564 (((unsigned char) *(b_peek(&check->bi, offset+2))) << 16);
565
566 if (b_data(&check->bi) < offset+plen+4) {
567 if (!last_read)
568 goto wait_more_data;
569
570 /* invalid length or truncated response */
571 status = HCHK_STATUS_L7RSP;
572 goto error;
573 }
574
575 if (*b_peek(&check->bi, offset+4) == '\xff') {
576 /* MySQL Error packet always begin with field_count = 0xff */
577 status = HCHK_STATUS_L7STS;
578 err = ((unsigned char) *b_peek(&check->bi, offset+5)) +
579 (((unsigned char) *(b_peek(&check->bi, offset+6))) << 8);
580 desc = ist2(b_peek(&check->bi, offset+7), b_data(&check->bi) - offset - 7);
581 goto error;
582 }
583
584 if (get_next_tcpcheck_rule(check->tcpcheck_rules, rule) != NULL) {
585 /* Not the last rule, continue */
586 goto out;
587 }
588
589 /* We set the MySQL Version in description for information purpose
590 * FIXME : it can be cool to use MySQL Version for other purpose,
591 * like mark as down old MySQL server.
592 */
593 status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
594 set_server_check_status(check, status, b_peek(&check->bi, 5));
595
596 out:
597 free_trash_chunk(msg);
598 return ret;
599
600 error:
601 ret = TCPCHK_EVAL_STOP;
602 check->code = err;
603 msg = alloc_trash_chunk();
604 if (msg)
605 tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
606 set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
607 goto out;
608
609 wait_more_data:
610 ret = TCPCHK_EVAL_WAIT;
611 goto out;
612}
613
614/* Custom tcp-check expect function to parse and validate the MySQL initial
615 * handshake packet. Returns TCPCHK_EVAL_WAIT to wait for more data,
616 * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
617 * error occurred.
618 */
619enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read)
620{
621 return tcpcheck_mysql_expect_packet(check, rule, 0, last_read);
622}
623
624/* Custom tcp-check expect function to parse and validate the MySQL OK packet
625 * following the initial handshake. Returns TCPCHK_EVAL_WAIT to wait for more
626 * data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if
627 * an error occurred.
628 */
629enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read)
630{
631 unsigned int hslen = 0;
632
633 hslen = 4 + ((unsigned char) *b_head(&check->bi)) +
634 (((unsigned char) *(b_peek(&check->bi, 1))) << 8) +
635 (((unsigned char) *(b_peek(&check->bi, 2))) << 16);
636
637 return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read);
638}
639
640/* Custom tcp-check expect function to parse and validate the LDAP bind response
641 * package packet. Returns TCPCHK_EVAL_WAIT to wait for more data,
642 * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
643 * error occurred.
644 */
645enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read)
646{
647 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
648 enum healthcheck_status status;
649 struct buffer *msg = NULL;
650 struct ist desc = IST_NULL;
651 unsigned short msglen = 0;
652
653 /* Check if the server speaks LDAP (ASN.1/BER)
654 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
655 * http://tools.ietf.org/html/rfc4511
656 */
657 /* size of LDAPMessage */
658 msglen = (*(b_head(&check->bi) + 1) & 0x80) ? (*(b_head(&check->bi) + 1) & 0x7f) : 0;
659
660 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
661 * messageID: 0x02 0x01 0x01: INTEGER 1
662 * protocolOp: 0x61: bindResponse
663 */
664 if ((msglen > 2) || (memcmp(b_head(&check->bi) + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
665 status = HCHK_STATUS_L7RSP;
666 desc = ist("Not LDAPv3 protocol");
667 goto error;
668 }
669
670 /* size of bindResponse */
671 msglen += (*(b_head(&check->bi) + msglen + 6) & 0x80) ? (*(b_head(&check->bi) + msglen + 6) & 0x7f) : 0;
672
673 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
674 * ldapResult: 0x0a 0x01: ENUMERATION
675 */
676 if ((msglen > 4) || (memcmp(b_head(&check->bi) + 7 + msglen, "\x0a\x01", 2) != 0)) {
677 status = HCHK_STATUS_L7RSP;
678 desc = ist("Not LDAPv3 protocol");
679 goto error;
680 }
681
682 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
683 * resultCode
684 */
685 check->code = *(b_head(&check->bi) + msglen + 9);
686 if (check->code) {
687 status = HCHK_STATUS_L7STS;
688 desc = ist("See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
689 goto error;
690 }
691
692 status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
693 set_server_check_status(check, status, "Success");
694
695 out:
696 free_trash_chunk(msg);
697 return ret;
698
699 error:
700 ret = TCPCHK_EVAL_STOP;
701 msg = alloc_trash_chunk();
702 if (msg)
703 tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
704 set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
705 goto out;
706}
707
708/* Custom tcp-check expect function to parse and validate the SPOP hello agent
709 * frame. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE
710 * to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred.
711 */
712enum tcpcheck_eval_ret tcpcheck_spop_expect_agenthello(struct check *check, struct tcpcheck_rule *rule, int last_read)
713{
714 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
715 enum healthcheck_status status;
716 struct buffer *msg = NULL;
717 struct ist desc = IST_NULL;
718 unsigned int framesz;
719
720
721 memcpy(&framesz, b_head(&check->bi), 4);
722 framesz = ntohl(framesz);
723
724 if (!last_read && b_data(&check->bi) < (4+framesz))
725 goto wait_more_data;
726
727 memset(b_orig(&trash), 0, b_size(&trash));
728 if (spoe_handle_healthcheck_response(b_peek(&check->bi, 4), framesz, b_orig(&trash), HCHK_DESC_LEN) == -1) {
729 status = HCHK_STATUS_L7RSP;
730 desc = ist2(b_orig(&trash), strlen(b_orig(&trash)));
731 goto error;
732 }
733
734 status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
735 set_server_check_status(check, status, "SPOA server is ok");
736
737 out:
738 free_trash_chunk(msg);
739 return ret;
740
741 error:
742 ret = TCPCHK_EVAL_STOP;
743 msg = alloc_trash_chunk();
744 if (msg)
745 tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
746 set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
747 goto out;
748
749 wait_more_data:
750 ret = TCPCHK_EVAL_WAIT;
751 goto out;
752}
753
754/* Custom tcp-check expect function to parse and validate the agent-check
755 * reply. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE
756 * to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred.
757 */
758enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read)
759{
760 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_STOP;
761 enum healthcheck_status status = HCHK_STATUS_CHECKED;
762 const char *hs = NULL; /* health status */
763 const char *as = NULL; /* admin status */
764 const char *ps = NULL; /* performance status */
765 const char *cs = NULL; /* maxconn */
766 const char *err = NULL; /* first error to report */
767 const char *wrn = NULL; /* first warning to report */
768 char *cmd, *p;
769
770 /* We're getting an agent check response. The agent could
771 * have been disabled in the mean time with a long check
772 * still pending. It is important that we ignore the whole
773 * response.
774 */
775 if (!(check->state & CHK_ST_ENABLED))
776 goto out;
777
778 /* The agent supports strings made of a single line ended by the
779 * first CR ('\r') or LF ('\n'). This line is composed of words
780 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
781 * line may optionally contained a description of a state change
782 * after a sharp ('#'), which is only considered if a health state
783 * is announced.
784 *
785 * Words may be composed of :
786 * - a numeric weight suffixed by the percent character ('%').
787 * - a health status among "up", "down", "stopped", and "fail".
788 * - an admin status among "ready", "drain", "maint".
789 *
790 * These words may appear in any order. If multiple words of the
791 * same category appear, the last one wins.
792 */
793
794 p = b_head(&check->bi);
795 while (*p && *p != '\n' && *p != '\r')
796 p++;
797
798 if (!*p) {
799 if (!last_read)
800 goto wait_more_data;
801
802 /* at least inform the admin that the agent is mis-behaving */
803 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
804 goto out;
805 }
806
807 *p = 0;
808 cmd = b_head(&check->bi);
809
810 while (*cmd) {
811 /* look for next word */
812 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
813 cmd++;
814 continue;
815 }
816
817 if (*cmd == '#') {
818 /* this is the beginning of a health status description,
819 * skip the sharp and blanks.
820 */
821 cmd++;
822 while (*cmd == '\t' || *cmd == ' ')
823 cmd++;
824 break;
825 }
826
827 /* find the end of the word so that we have a null-terminated
828 * word between <cmd> and <p>.
829 */
830 p = cmd + 1;
831 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
832 p++;
833 if (*p)
834 *p++ = 0;
835
836 /* first, health statuses */
837 if (strcasecmp(cmd, "up") == 0) {
Christopher Faulet789bbdc2021-03-12 09:06:07 +0100838 check->health = check->rise + check->fall - 1;
Willy Tarreau51cd5952020-06-05 12:25:38 +0200839 status = HCHK_STATUS_L7OKD;
840 hs = cmd;
841 }
842 else if (strcasecmp(cmd, "down") == 0) {
Christopher Faulet789bbdc2021-03-12 09:06:07 +0100843 check->health = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +0200844 status = HCHK_STATUS_L7STS;
845 hs = cmd;
846 }
847 else if (strcasecmp(cmd, "stopped") == 0) {
Christopher Faulet789bbdc2021-03-12 09:06:07 +0100848 check->health = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +0200849 status = HCHK_STATUS_L7STS;
850 hs = cmd;
851 }
852 else if (strcasecmp(cmd, "fail") == 0) {
Christopher Faulet789bbdc2021-03-12 09:06:07 +0100853 check->health = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +0200854 status = HCHK_STATUS_L7STS;
855 hs = cmd;
856 }
857 /* admin statuses */
858 else if (strcasecmp(cmd, "ready") == 0) {
859 as = cmd;
860 }
861 else if (strcasecmp(cmd, "drain") == 0) {
862 as = cmd;
863 }
864 else if (strcasecmp(cmd, "maint") == 0) {
865 as = cmd;
866 }
867 /* try to parse a weight here and keep the last one */
868 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
869 ps = cmd;
870 }
871 /* try to parse a maxconn here */
872 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
873 cs = cmd;
874 }
875 else {
876 /* keep a copy of the first error */
877 if (!err)
878 err = cmd;
879 }
880 /* skip to next word */
881 cmd = p;
882 }
883 /* here, cmd points either to \0 or to the beginning of a
884 * description. Skip possible leading spaces.
885 */
886 while (*cmd == ' ' || *cmd == '\n')
887 cmd++;
888
889 /* First, update the admin status so that we avoid sending other
890 * possibly useless warnings and can also update the health if
891 * present after going back up.
892 */
893 if (as) {
894 if (strcasecmp(as, "drain") == 0)
895 srv_adm_set_drain(check->server);
896 else if (strcasecmp(as, "maint") == 0)
897 srv_adm_set_maint(check->server);
898 else
899 srv_adm_set_ready(check->server);
900 }
901
902 /* now change weights */
903 if (ps) {
904 const char *msg;
905
906 msg = server_parse_weight_change_request(check->server, ps);
907 if (!wrn || !*wrn)
908 wrn = msg;
909 }
910
911 if (cs) {
912 const char *msg;
913
914 cs += strlen("maxconn:");
915
Amaury Denoyelle43b0c622021-06-18 11:11:36 +0200916 /* This is safe to call server_parse_maxconn_change_request
917 * because the server lock is held during the check.
918 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200919 msg = server_parse_maxconn_change_request(check->server, cs);
920 if (!wrn || !*wrn)
921 wrn = msg;
922 }
923
924 /* and finally health status */
925 if (hs) {
926 /* We'll report some of the warnings and errors we have
927 * here. Down reports are critical, we leave them untouched.
928 * Lack of report, or report of 'UP' leaves the room for
929 * ERR first, then WARN.
930 */
931 const char *msg = cmd;
932 struct buffer *t;
933
934 if (!*msg || status == HCHK_STATUS_L7OKD) {
935 if (err && *err)
936 msg = err;
937 else if (wrn && *wrn)
938 msg = wrn;
939 }
940
941 t = get_trash_chunk();
942 chunk_printf(t, "via agent : %s%s%s%s",
943 hs, *msg ? " (" : "",
944 msg, *msg ? ")" : "");
945 set_server_check_status(check, status, t->area);
946 }
947 else if (err && *err) {
948 /* No status change but we'd like to report something odd.
949 * Just report the current state and copy the message.
950 */
951 chunk_printf(&trash, "agent reports an error : %s", err);
952 set_server_check_status(check, status/*check->status*/, trash.area);
953 }
954 else if (wrn && *wrn) {
955 /* No status change but we'd like to report something odd.
956 * Just report the current state and copy the message.
957 */
958 chunk_printf(&trash, "agent warns : %s", wrn);
959 set_server_check_status(check, status/*check->status*/, trash.area);
960 }
961 else
962 set_server_check_status(check, status, NULL);
963
964 out:
965 return ret;
966
967 wait_more_data:
968 ret = TCPCHK_EVAL_WAIT;
969 goto out;
970}
971
972/* Evaluates a TCPCHK_ACT_CONNECT rule. Returns TCPCHK_EVAL_WAIT to wait the
973 * connection establishment, TCPCHK_EVAL_CONTINUE to evaluate the next rule or
974 * TCPCHK_EVAL_STOP if an error occurred.
975 */
976enum tcpcheck_eval_ret tcpcheck_eval_connect(struct check *check, struct tcpcheck_rule *rule)
977{
978 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
979 struct tcpcheck_connect *connect = &rule->connect;
980 struct proxy *proxy = check->proxy;
981 struct server *s = check->server;
982 struct task *t = check->task;
983 struct conn_stream *cs;
984 struct connection *conn = NULL;
985 struct protocol *proto;
986 struct xprt_ops *xprt;
987 struct tcpcheck_rule *next;
988 int status, port;
989
Christopher Faulet444b7b12021-01-18 15:47:03 +0100990 /* For a connect action we'll create a new connection. The previous one,
991 * if any should have been killed. We don't want to leave *without* a
Willy Tarreau51cd5952020-06-05 12:25:38 +0200992 * connection if we came here from the connection layer, hence with a
993 * connection. Thus we'll proceed in the following order :
994 * 1: close but not release previous connection (handled by the caller)
Christopher Faulet444b7b12021-01-18 15:47:03 +0100995 * 2: Wait the connection is released (handled by process_chk_conn())
996 * 3: try to get a new connection
Willy Tarreau51cd5952020-06-05 12:25:38 +0200997 */
998
Christopher Fauletb51e0372020-11-25 13:47:00 +0100999 /* Always release input and output buffer when a new connect is evaluated */
1000 check_release_buf(check, &check->bi);
1001 check_release_buf(check, &check->bo);
1002
Christopher Faulet444b7b12021-01-18 15:47:03 +01001003 /* prepare new connection */
Christopher Faulet236c93b2020-07-02 09:19:54 +02001004 cs = cs_new(NULL, (s ? &s->obj_type : &proxy->obj_type));
Willy Tarreau51cd5952020-06-05 12:25:38 +02001005 if (!cs) {
1006 chunk_printf(&trash, "TCPCHK error allocating connection at step %d",
1007 tcpcheck_get_step_id(check, rule));
1008 if (rule->comment)
1009 chunk_appendf(&trash, " comment: '%s'", rule->comment);
1010 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area);
1011 ret = TCPCHK_EVAL_STOP;
1012 goto out;
1013 }
1014
Willy Tarreau51cd5952020-06-05 12:25:38 +02001015 tasklet_set_tid(check->wait_list.tasklet, tid);
1016
1017 check->cs = cs;
1018 conn = cs->conn;
1019 conn_set_owner(conn, check->sess, NULL);
1020
1021 /* Maybe there were an older connection we were waiting on */
1022 check->wait_list.events = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001023
1024 /* no client address */
Willy Tarreau9b7587a2020-10-15 07:32:10 +02001025 if (!sockaddr_alloc(&conn->dst, NULL, 0)) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001026 status = SF_ERR_RESOURCE;
1027 goto fail_check;
1028 }
1029
1030 /* connect to the connect rule addr if specified, otherwise the check
1031 * addr if specified on the server. otherwise, use the server addr (it
1032 * MUST exist at this step).
1033 */
1034 *conn->dst = (is_addr(&connect->addr)
1035 ? connect->addr
1036 : (is_addr(&check->addr) ? check->addr : s->addr));
1037 proto = protocol_by_family(conn->dst->ss_family);
1038
1039 port = 0;
1040 if (!port && connect->port)
1041 port = connect->port;
1042 if (!port && connect->port_expr) {
1043 struct sample *smp;
1044
1045 smp = sample_fetch_as_type(check->proxy, check->sess, NULL,
1046 SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
1047 connect->port_expr, SMP_T_SINT);
1048 if (smp)
1049 port = smp->data.u.sint;
1050 }
1051 if (!port && is_inet_addr(&connect->addr))
1052 port = get_host_port(&connect->addr);
1053 if (!port && check->port)
1054 port = check->port;
1055 if (!port && is_inet_addr(&check->addr))
1056 port = get_host_port(&check->addr);
1057 if (!port) {
1058 /* The server MUST exist here */
1059 port = s->svc_port;
1060 }
1061 set_host_port(conn->dst, port);
1062
1063 xprt = ((connect->options & TCPCHK_OPT_SSL)
1064 ? xprt_get(XPRT_SSL)
1065 : ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) ? check->xprt : xprt_get(XPRT_RAW)));
1066
1067 conn_prepare(conn, proto, xprt);
1068 cs_attach(cs, check, &check_conn_cb);
1069
Christopher Fauletf7177272020-10-02 13:41:55 +02001070 if ((connect->options & TCPCHK_OPT_SOCKS4) && s && (s->flags & SRV_F_SOCKS4_PROXY)) {
1071 conn->send_proxy_ofs = 1;
1072 conn->flags |= CO_FL_SOCKS4;
1073 }
1074 else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.via_socks4 && (s->flags & SRV_F_SOCKS4_PROXY)) {
1075 conn->send_proxy_ofs = 1;
1076 conn->flags |= CO_FL_SOCKS4;
1077 }
1078
1079 if (connect->options & TCPCHK_OPT_SEND_PROXY) {
1080 conn->send_proxy_ofs = 1;
1081 conn->flags |= CO_FL_SEND_PROXY;
1082 }
1083 else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
1084 conn->send_proxy_ofs = 1;
1085 conn->flags |= CO_FL_SEND_PROXY;
1086 }
1087
Willy Tarreau51cd5952020-06-05 12:25:38 +02001088 status = SF_ERR_INTERNAL;
1089 next = get_next_tcpcheck_rule(check->tcpcheck_rules, rule);
1090 if (proto && proto->connect) {
1091 int flags = 0;
1092
1093 if (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK)
1094 flags |= CONNECT_HAS_DATA;
1095 if (!next || next->action != TCPCHK_ACT_EXPECT)
1096 flags |= CONNECT_DELACK_ALWAYS;
1097 status = proto->connect(conn, flags);
1098 }
1099
1100 if (status != SF_ERR_NONE)
1101 goto fail_check;
1102
Christopher Faulet21ddc742020-07-01 15:26:14 +02001103 conn_set_private(conn);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001104 conn->ctx = cs;
1105
Willy Tarreau51cd5952020-06-05 12:25:38 +02001106#ifdef USE_OPENSSL
1107 if (connect->sni)
1108 ssl_sock_set_servername(conn, connect->sni);
1109 else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.sni)
1110 ssl_sock_set_servername(conn, s->check.sni);
1111
1112 if (connect->alpn)
1113 ssl_sock_set_alpn(conn, (unsigned char *)connect->alpn, connect->alpn_len);
1114 else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.alpn_str)
1115 ssl_sock_set_alpn(conn, (unsigned char *)s->check.alpn_str, s->check.alpn_len);
1116#endif
Willy Tarreau51cd5952020-06-05 12:25:38 +02001117
1118 if (conn_ctrl_ready(conn) && (connect->options & TCPCHK_OPT_LINGER)) {
1119 /* Some servers don't like reset on close */
1120 fdtab[cs->conn->handle.fd].linger_risk = 0;
1121 }
1122
1123 if (conn_ctrl_ready(conn) && (conn->flags & (CO_FL_SEND_PROXY | CO_FL_SOCKS4))) {
1124 if (xprt_add_hs(conn) < 0)
1125 status = SF_ERR_RESOURCE;
1126 }
1127
Willy Tarreau8e979fa2020-07-31 08:49:31 +02001128 /* The mux may be initialized now if there isn't server attached to the
1129 * check (email alerts) or if there is a mux proto specified or if there
1130 * is no alpn.
1131 */
1132 if (!s || ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && check->mux_proto) ||
1133 connect->mux_proto || (!connect->alpn && !check->alpn_str)) {
1134 const struct mux_ops *mux_ops;
1135
1136 if (connect->mux_proto)
1137 mux_ops = connect->mux_proto->mux;
1138 else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && check->mux_proto)
1139 mux_ops = check->mux_proto->mux;
1140 else {
1141 int mode = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK
1142 ? PROTO_MODE_HTTP
1143 : PROTO_MODE_TCP);
1144
1145 mux_ops = conn_get_best_mux(conn, IST_NULL, PROTO_SIDE_BE, mode);
1146 }
1147 if (mux_ops && conn_install_mux(conn, mux_ops, cs, proxy, check->sess) < 0) {
1148 status = SF_ERR_INTERNAL;
1149 goto fail_check;
1150 }
1151 }
1152
Willy Tarreau51cd5952020-06-05 12:25:38 +02001153 fail_check:
1154 /* It can return one of :
1155 * - SF_ERR_NONE if everything's OK
1156 * - SF_ERR_SRVTO if there are no more servers
1157 * - SF_ERR_SRVCL if the connection was refused by the server
1158 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1159 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1160 * - SF_ERR_INTERNAL for any other purely internal errors
1161 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
1162 * Note that we try to prevent the network stack from sending the ACK during the
1163 * connect() when a pure TCP check is used (without PROXY protocol).
1164 */
1165 switch (status) {
1166 case SF_ERR_NONE:
1167 /* we allow up to min(inter, timeout.connect) for a connection
1168 * to establish but only when timeout.check is set as it may be
1169 * to short for a full check otherwise
1170 */
1171 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1172
1173 if (proxy->timeout.check && proxy->timeout.connect) {
1174 int t_con = tick_add(now_ms, proxy->timeout.connect);
1175 t->expire = tick_first(t->expire, t_con);
1176 }
1177 break;
1178 case SF_ERR_SRVTO: /* ETIMEDOUT */
1179 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1180 case SF_ERR_PRXCOND:
1181 case SF_ERR_RESOURCE:
1182 case SF_ERR_INTERNAL:
1183 chk_report_conn_err(check, errno, 0);
1184 ret = TCPCHK_EVAL_STOP;
1185 goto out;
1186 }
1187
1188 /* don't do anything until the connection is established */
1189 if (conn->flags & CO_FL_WAIT_XPRT) {
1190 if (conn->mux) {
1191 if (next && next->action == TCPCHK_ACT_SEND)
1192 conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list);
1193 else
1194 conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list);
1195 }
1196 ret = TCPCHK_EVAL_WAIT;
1197 goto out;
1198 }
1199
1200 out:
1201 if (conn && check->result == CHK_RES_FAILED)
1202 conn->flags |= CO_FL_ERROR;
Christopher Fauletcc554532020-12-09 19:46:38 +01001203
1204 if (ret == TCPCHK_EVAL_CONTINUE && check->proxy->timeout.check)
1205 check->task->expire = tick_add_ifset(now_ms, check->proxy->timeout.check);
1206
Willy Tarreau51cd5952020-06-05 12:25:38 +02001207 return ret;
1208}
1209
1210/* Evaluates a TCPCHK_ACT_SEND rule. Returns TCPCHK_EVAL_WAIT if outgoing data
1211 * were not fully sent, TCPCHK_EVAL_CONTINUE to evaluate the next rule or
1212 * TCPCHK_EVAL_STOP if an error occurred.
1213 */
1214enum tcpcheck_eval_ret tcpcheck_eval_send(struct check *check, struct tcpcheck_rule *rule)
1215{
1216 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
1217 struct tcpcheck_send *send = &rule->send;
1218 struct conn_stream *cs = check->cs;
1219 struct connection *conn = cs_conn(cs);
1220 struct buffer *tmp = NULL;
1221 struct htx *htx = NULL;
Amaury Denoyelleaea63022020-12-22 14:08:52 +01001222 int connection_hdr = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001223
Christopher Fauletb51e0372020-11-25 13:47:00 +01001224 if (check->state & CHK_ST_OUT_ALLOC) {
1225 ret = TCPCHK_EVAL_WAIT;
1226 goto out;
1227 }
1228
1229 if (!check_get_buf(check, &check->bo)) {
1230 check->state |= CHK_ST_OUT_ALLOC;
1231 ret = TCPCHK_EVAL_WAIT;
1232 goto out;
1233 }
1234
Christopher Faulet6f45f032020-11-25 13:34:51 +01001235 /* Data already pending in the output buffer, send them now */
1236 if (b_data(&check->bo))
1237 goto do_send;
1238
Christopher Fauletb51e0372020-11-25 13:47:00 +01001239 /* Always release input buffer when a new send is evaluated */
1240 check_release_buf(check, &check->bi);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001241
1242 switch (send->type) {
1243 case TCPCHK_SEND_STRING:
1244 case TCPCHK_SEND_BINARY:
1245 if (istlen(send->data) >= b_size(&check->bo)) {
1246 chunk_printf(&trash, "tcp-check send : string too large (%u) for buffer size (%u) at step %d",
1247 (unsigned int)istlen(send->data), (unsigned int)b_size(&check->bo),
1248 tcpcheck_get_step_id(check, rule));
1249 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area);
1250 ret = TCPCHK_EVAL_STOP;
1251 goto out;
1252 }
1253 b_putist(&check->bo, send->data);
1254 break;
1255 case TCPCHK_SEND_STRING_LF:
1256 check->bo.data = sess_build_logline(check->sess, NULL, b_orig(&check->bo), b_size(&check->bo), &rule->send.fmt);
1257 if (!b_data(&check->bo))
1258 goto out;
1259 break;
1260 case TCPCHK_SEND_BINARY_LF: {
1261 int len = b_size(&check->bo);
1262
1263 tmp = alloc_trash_chunk();
1264 if (!tmp)
1265 goto error_lf;
1266 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &rule->send.fmt);
1267 if (!b_data(tmp))
1268 goto out;
1269 tmp->area[tmp->data] = '\0';
1270 if (parse_binary(b_orig(tmp), &check->bo.area, &len, NULL) == 0)
1271 goto error_lf;
1272 check->bo.data = len;
1273 break;
1274 }
1275 case TCPCHK_SEND_HTTP: {
1276 struct htx_sl *sl;
1277 struct ist meth, uri, vsn, clen, body;
1278 unsigned int slflags = 0;
1279
1280 tmp = alloc_trash_chunk();
1281 if (!tmp)
1282 goto error_htx;
1283
1284 meth = ((send->http.meth.meth == HTTP_METH_OTHER)
1285 ? ist2(send->http.meth.str.area, send->http.meth.str.data)
1286 : http_known_methods[send->http.meth.meth]);
1287 if (send->http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) {
1288 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &send->http.uri_fmt);
1289 uri = (b_data(tmp) ? ist2(b_orig(tmp), b_data(tmp)) : ist("/"));
1290 }
1291 else
1292 uri = (isttest(send->http.uri) ? send->http.uri : ist("/"));
1293 vsn = (isttest(send->http.vsn) ? send->http.vsn : ist("HTTP/1.0"));
1294
1295 if ((istlen(vsn) == 6 && *(vsn.ptr+5) == '2') ||
1296 (istlen(vsn) == 8 && (*(vsn.ptr+5) > '1' || (*(vsn.ptr+5) == '1' && *(vsn.ptr+7) >= '1'))))
1297 slflags |= HTX_SL_F_VER_11;
1298 slflags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
1299 if (!isttest(send->http.body))
1300 slflags |= HTX_SL_F_BODYLESS;
1301
1302 htx = htx_from_buf(&check->bo);
1303 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, slflags, meth, uri, vsn);
1304 if (!sl)
1305 goto error_htx;
1306 sl->info.req.meth = send->http.meth.meth;
1307 if (!http_update_host(htx, sl, uri))
1308 goto error_htx;
1309
1310 if (!LIST_ISEMPTY(&send->http.hdrs)) {
1311 struct tcpcheck_http_hdr *hdr;
1312 struct ist hdr_value;
1313
1314 list_for_each_entry(hdr, &send->http.hdrs, list) {
1315 chunk_reset(tmp);
1316 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &hdr->value);
1317 if (!b_data(tmp))
1318 continue;
1319 hdr_value = ist2(b_orig(tmp), b_data(tmp));
1320 if (!htx_add_header(htx, hdr->name, hdr_value))
1321 goto error_htx;
1322 if ((sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(hdr->name, ist("host"))) {
1323 if (!http_update_authority(htx, sl, hdr_value))
1324 goto error_htx;
1325 }
Amaury Denoyelleaea63022020-12-22 14:08:52 +01001326 if (isteqi(hdr->name, ist("connection")))
1327 connection_hdr = 1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001328 }
1329
1330 }
1331 if (check->proxy->options2 & PR_O2_CHK_SNDST) {
1332 chunk_reset(tmp);
1333 httpchk_build_status_header(check->server, tmp);
1334 if (!htx_add_header(htx, ist("X-Haproxy-Server-State"), ist2(b_orig(tmp), b_data(tmp))))
1335 goto error_htx;
1336 }
1337
1338
1339 if (send->http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) {
1340 chunk_reset(tmp);
1341 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &send->http.body_fmt);
1342 body = ist2(b_orig(tmp), b_data(tmp));
1343 }
1344 else
1345 body = send->http.body;
1346 clen = ist((!istlen(body) ? "0" : ultoa(istlen(body))));
1347
Amaury Denoyelleaea63022020-12-22 14:08:52 +01001348 if ((!connection_hdr && !htx_add_header(htx, ist("Connection"), ist("close"))) ||
Willy Tarreau51cd5952020-06-05 12:25:38 +02001349 !htx_add_header(htx, ist("Content-length"), clen))
1350 goto error_htx;
1351
1352
1353 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Faulet810df062020-07-22 16:20:34 +02001354 (istlen(body) && !htx_add_data_atonce(htx, body)))
1355 goto error_htx;
1356
1357 htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
1358 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau51cd5952020-06-05 12:25:38 +02001359 goto error_htx;
1360
1361 htx_to_buf(htx, &check->bo);
1362 break;
1363 }
1364 case TCPCHK_SEND_UNDEF:
1365 /* Should never happen. */
1366 ret = TCPCHK_EVAL_STOP;
1367 goto out;
1368 };
1369
Christopher Faulet6f45f032020-11-25 13:34:51 +01001370 do_send:
Willy Tarreau51cd5952020-06-05 12:25:38 +02001371 if (conn->mux->snd_buf(cs, &check->bo,
1372 (IS_HTX_CONN(conn) ? (htxbuf(&check->bo))->data: b_data(&check->bo)), 0) <= 0) {
1373 if ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR)) {
1374 ret = TCPCHK_EVAL_STOP;
1375 goto out;
1376 }
1377 }
1378 if ((IS_HTX_CONN(conn) && !htx_is_empty(htxbuf(&check->bo))) || b_data(&check->bo)) {
1379 cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list);
1380 ret = TCPCHK_EVAL_WAIT;
1381 goto out;
1382 }
1383
1384 out:
1385 free_trash_chunk(tmp);
Christopher Fauletb51e0372020-11-25 13:47:00 +01001386 if (!b_data(&check->bo) || ret == TCPCHK_EVAL_STOP)
1387 check_release_buf(check, &check->bo);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001388 return ret;
1389
1390 error_htx:
1391 if (htx) {
1392 htx_reset(htx);
1393 htx_to_buf(htx, &check->bo);
1394 }
1395 chunk_printf(&trash, "tcp-check send : failed to build HTTP request at step %d",
1396 tcpcheck_get_step_id(check, rule));
1397 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area);
1398 ret = TCPCHK_EVAL_STOP;
1399 goto out;
1400
1401 error_lf:
1402 chunk_printf(&trash, "tcp-check send : failed to build log-format string at step %d",
1403 tcpcheck_get_step_id(check, rule));
1404 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area);
1405 ret = TCPCHK_EVAL_STOP;
1406 goto out;
1407
1408}
1409
1410/* Try to receive data before evaluating a tcp-check expect rule. Returns
1411 * TCPCHK_EVAL_WAIT if it is already subscribed on receive events or if nothing
1412 * was received, TCPCHK_EVAL_CONTINUE to evaluate the expect rule or
1413 * TCPCHK_EVAL_STOP if an error occurred.
1414 */
1415enum tcpcheck_eval_ret tcpcheck_eval_recv(struct check *check, struct tcpcheck_rule *rule)
1416{
1417 struct conn_stream *cs = check->cs;
1418 struct connection *conn = cs_conn(cs);
1419 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
1420 size_t max, read, cur_read = 0;
1421 int is_empty;
1422 int read_poll = MAX_READ_POLL_LOOPS;
1423
1424 if (check->wait_list.events & SUB_RETRY_RECV)
1425 goto wait_more_data;
1426
1427 if (cs->flags & CS_FL_EOS)
1428 goto end_recv;
1429
Christopher Fauletb51e0372020-11-25 13:47:00 +01001430 if (check->state & CHK_ST_IN_ALLOC)
1431 goto wait_more_data;
1432
1433 if (!check_get_buf(check, &check->bi)) {
1434 check->state |= CHK_ST_IN_ALLOC;
1435 goto wait_more_data;
1436 }
1437
Willy Tarreau51cd5952020-06-05 12:25:38 +02001438 /* errors on the connection and the conn-stream were already checked */
1439
1440 /* prepare to detect if the mux needs more room */
1441 cs->flags &= ~CS_FL_WANT_ROOM;
1442
1443 while ((cs->flags & CS_FL_RCV_MORE) ||
1444 (!(conn->flags & CO_FL_ERROR) && !(cs->flags & (CS_FL_ERROR|CS_FL_EOS)))) {
1445 max = (IS_HTX_CS(cs) ? htx_free_space(htxbuf(&check->bi)) : b_room(&check->bi));
1446 read = conn->mux->rcv_buf(cs, &check->bi, max, 0);
1447 cur_read += read;
1448 if (!read ||
1449 (cs->flags & CS_FL_WANT_ROOM) ||
1450 (--read_poll <= 0) ||
1451 (read < max && read >= global.tune.recv_enough))
1452 break;
1453 }
1454
1455 end_recv:
1456 is_empty = (IS_HTX_CS(cs) ? htx_is_empty(htxbuf(&check->bi)) : !b_data(&check->bi));
1457 if (is_empty && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) {
1458 /* Report network errors only if we got no other data. Otherwise
1459 * we'll let the upper layers decide whether the response is OK
1460 * or not. It is very common that an RST sent by the server is
1461 * reported as an error just after the last data chunk.
1462 */
1463 goto stop;
1464 }
1465 if (!cur_read) {
1466 if (!(cs->flags & (CS_FL_WANT_ROOM|CS_FL_ERROR|CS_FL_EOS))) {
1467 conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list);
1468 goto wait_more_data;
1469 }
1470 if (is_empty) {
1471 int status;
1472
1473 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
1474 tcpcheck_get_step_id(check, rule));
1475 if (rule->comment)
1476 chunk_appendf(&trash, " comment: '%s'", rule->comment);
1477
1478 status = ((rule->expect.err_status != HCHK_STATUS_UNKNOWN) ? rule->expect.err_status : HCHK_STATUS_L7RSP);
1479 set_server_check_status(check, status, trash.area);
1480 goto stop;
1481 }
1482 }
1483
1484 out:
Christopher Fauletb51e0372020-11-25 13:47:00 +01001485 if (!b_data(&check->bi) || ret == TCPCHK_EVAL_STOP)
1486 check_release_buf(check, &check->bi);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001487 return ret;
1488
1489 stop:
1490 ret = TCPCHK_EVAL_STOP;
1491 goto out;
1492
1493 wait_more_data:
1494 ret = TCPCHK_EVAL_WAIT;
1495 goto out;
1496}
1497
1498/* Evaluates an HTTP TCPCHK_ACT_EXPECT rule. If <last_read> is set , no more data
1499 * are expected. Returns TCPCHK_EVAL_WAIT to wait for more data,
1500 * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
1501 * error occurred.
1502 */
1503enum tcpcheck_eval_ret tcpcheck_eval_expect_http(struct check *check, struct tcpcheck_rule *rule, int last_read)
1504{
1505 struct htx *htx = htxbuf(&check->bi);
1506 struct htx_sl *sl;
1507 struct htx_blk *blk;
1508 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
1509 struct tcpcheck_expect *expect = &rule->expect;
1510 struct buffer *msg = NULL, *tmp = NULL, *nbuf = NULL, *vbuf = NULL;
1511 enum healthcheck_status status = HCHK_STATUS_L7RSP;
1512 struct ist desc = IST_NULL;
1513 int i, match, inverse;
1514
Christopher Faulet18d98902020-12-09 19:45:07 +01001515 last_read |= (!htx_free_data_space(htx) || (htx_get_tail_type(htx) == HTX_BLK_EOM));
Willy Tarreau51cd5952020-06-05 12:25:38 +02001516
1517 if (htx->flags & HTX_FL_PARSING_ERROR) {
1518 status = HCHK_STATUS_L7RSP;
1519 goto error;
1520 }
1521
1522 if (htx_is_empty(htx)) {
1523 if (last_read) {
1524 status = HCHK_STATUS_L7RSP;
1525 goto error;
1526 }
1527 goto wait_more_data;
1528 }
1529
1530 sl = http_get_stline(htx);
1531 check->code = sl->info.res.status;
1532
1533 if (check->server &&
1534 (check->server->proxy->options & PR_O_DISABLE404) &&
1535 (check->server->next_state != SRV_ST_STOPPED) &&
1536 (check->code == 404)) {
1537 /* 404 may be accepted as "stopping" only if the server was up */
1538 goto out;
1539 }
1540
1541 inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV);
1542 /* Make GCC happy ; initialize match to a failure state. */
1543 match = inverse;
1544 status = expect->err_status;
1545
1546 switch (expect->type) {
1547 case TCPCHK_EXPECT_HTTP_STATUS:
1548 match = 0;
1549 for (i = 0; i < expect->codes.num; i++) {
1550 if (sl->info.res.status >= expect->codes.codes[i][0] &&
1551 sl->info.res.status <= expect->codes.codes[i][1]) {
1552 match = 1;
1553 break;
1554 }
1555 }
1556
1557 /* Set status and description in case of error */
1558 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS);
1559 if (LIST_ISEMPTY(&expect->onerror_fmt))
1560 desc = htx_sl_res_reason(sl);
1561 break;
1562 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
1563 match = regex_exec2(expect->regex, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
1564
1565 /* Set status and description in case of error */
1566 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS);
1567 if (LIST_ISEMPTY(&expect->onerror_fmt))
1568 desc = htx_sl_res_reason(sl);
1569 break;
1570
1571 case TCPCHK_EXPECT_HTTP_HEADER: {
1572 struct http_hdr_ctx ctx;
1573 struct ist npat, vpat, value;
1574 int full = (expect->flags & (TCPCHK_EXPT_FL_HTTP_HVAL_NONE|TCPCHK_EXPT_FL_HTTP_HVAL_FULL));
1575
1576 if (expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) {
1577 nbuf = alloc_trash_chunk();
1578 if (!nbuf) {
1579 status = HCHK_STATUS_L7RSP;
1580 desc = ist("Failed to allocate buffer to eval log-format string");
1581 goto error;
1582 }
1583 nbuf->data = sess_build_logline(check->sess, NULL, b_orig(nbuf), b_size(nbuf), &expect->hdr.name_fmt);
1584 if (!b_data(nbuf)) {
1585 status = HCHK_STATUS_L7RSP;
1586 desc = ist("log-format string evaluated to an empty string");
1587 goto error;
1588 }
1589 npat = ist2(b_orig(nbuf), b_data(nbuf));
1590 }
1591 else if (!(expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG))
1592 npat = expect->hdr.name;
1593
1594 if (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) {
1595 vbuf = alloc_trash_chunk();
1596 if (!vbuf) {
1597 status = HCHK_STATUS_L7RSP;
1598 desc = ist("Failed to allocate buffer to eval log-format string");
1599 goto error;
1600 }
1601 vbuf->data = sess_build_logline(check->sess, NULL, b_orig(vbuf), b_size(vbuf), &expect->hdr.value_fmt);
1602 if (!b_data(vbuf)) {
1603 status = HCHK_STATUS_L7RSP;
1604 desc = ist("log-format string evaluated to an empty string");
1605 goto error;
1606 }
1607 vpat = ist2(b_orig(vbuf), b_data(vbuf));
1608 }
1609 else if (!(expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG))
1610 vpat = expect->hdr.value;
1611
1612 match = 0;
1613 ctx.blk = NULL;
1614 while (1) {
1615 switch (expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_TYPE) {
1616 case TCPCHK_EXPT_FL_HTTP_HNAME_STR:
1617 if (!http_find_str_header(htx, npat, &ctx, full))
1618 goto end_of_match;
1619 break;
1620 case TCPCHK_EXPT_FL_HTTP_HNAME_BEG:
1621 if (!http_find_pfx_header(htx, npat, &ctx, full))
1622 goto end_of_match;
1623 break;
1624 case TCPCHK_EXPT_FL_HTTP_HNAME_END:
1625 if (!http_find_sfx_header(htx, npat, &ctx, full))
1626 goto end_of_match;
1627 break;
1628 case TCPCHK_EXPT_FL_HTTP_HNAME_SUB:
1629 if (!http_find_sub_header(htx, npat, &ctx, full))
1630 goto end_of_match;
1631 break;
1632 case TCPCHK_EXPT_FL_HTTP_HNAME_REG:
1633 if (!http_match_header(htx, expect->hdr.name_re, &ctx, full))
1634 goto end_of_match;
1635 break;
1636 default:
1637 /* should never happen */
1638 goto end_of_match;
1639 }
1640
1641 /* A header has matched the name pattern, let's test its
1642 * value now (always defined from there). If there is no
1643 * value pattern, it is a good match.
1644 */
1645
1646 if (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE) {
1647 match = 1;
1648 goto end_of_match;
1649 }
1650
1651 value = ctx.value;
1652 switch (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_TYPE) {
1653 case TCPCHK_EXPT_FL_HTTP_HVAL_STR:
1654 if (isteq(value, vpat)) {
1655 match = 1;
1656 goto end_of_match;
1657 }
1658 break;
1659 case TCPCHK_EXPT_FL_HTTP_HVAL_BEG:
1660 if (istlen(value) < istlen(vpat))
1661 break;
1662 value = ist2(istptr(value), istlen(vpat));
1663 if (isteq(value, vpat)) {
1664 match = 1;
1665 goto end_of_match;
1666 }
1667 break;
1668 case TCPCHK_EXPT_FL_HTTP_HVAL_END:
1669 if (istlen(value) < istlen(vpat))
1670 break;
1671 value = ist2(istptr(value) + istlen(value) - istlen(vpat), istlen(vpat));
1672 if (isteq(value, vpat)) {
1673 match = 1;
1674 goto end_of_match;
1675 }
1676 break;
1677 case TCPCHK_EXPT_FL_HTTP_HVAL_SUB:
1678 if (isttest(istist(value, vpat))) {
1679 match = 1;
1680 goto end_of_match;
1681 }
1682 break;
1683 case TCPCHK_EXPT_FL_HTTP_HVAL_REG:
1684 if (regex_exec2(expect->hdr.value_re, istptr(value), istlen(value))) {
1685 match = 1;
1686 goto end_of_match;
1687 }
1688 break;
1689 }
1690 }
1691
1692 end_of_match:
1693 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS);
1694 if (LIST_ISEMPTY(&expect->onerror_fmt))
1695 desc = htx_sl_res_reason(sl);
1696 break;
1697 }
1698
1699 case TCPCHK_EXPECT_HTTP_BODY:
1700 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
1701 case TCPCHK_EXPECT_HTTP_BODY_LF:
1702 match = 0;
1703 chunk_reset(&trash);
1704 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1705 enum htx_blk_type type = htx_get_blk_type(blk);
1706
1707 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1708 break;
1709 if (type == HTX_BLK_DATA) {
1710 if (!chunk_istcat(&trash, htx_get_blk_value(htx, blk)))
1711 break;
1712 }
1713 }
1714
1715 if (!b_data(&trash)) {
1716 if (!last_read)
1717 goto wait_more_data;
1718 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP);
1719 if (LIST_ISEMPTY(&expect->onerror_fmt))
1720 desc = ist("HTTP content check could not find a response body");
1721 goto error;
1722 }
1723
1724 if (expect->type == TCPCHK_EXPECT_HTTP_BODY_LF) {
1725 tmp = alloc_trash_chunk();
1726 if (!tmp) {
1727 status = HCHK_STATUS_L7RSP;
1728 desc = ist("Failed to allocate buffer to eval log-format string");
1729 goto error;
1730 }
1731 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &expect->fmt);
1732 if (!b_data(tmp)) {
1733 status = HCHK_STATUS_L7RSP;
1734 desc = ist("log-format string evaluated to an empty string");
1735 goto error;
1736 }
1737 }
1738
1739 if (!last_read &&
1740 ((expect->type == TCPCHK_EXPECT_HTTP_BODY && b_data(&trash) < istlen(expect->data)) ||
1741 ((expect->type == TCPCHK_EXPECT_HTTP_BODY_LF && b_data(&trash) < b_data(tmp))) ||
1742 (expect->min_recv > 0 && b_data(&trash) < expect->min_recv))) {
1743 ret = TCPCHK_EVAL_WAIT;
1744 goto out;
1745 }
1746
1747 if (expect->type ==TCPCHK_EXPECT_HTTP_BODY)
1748 match = my_memmem(b_orig(&trash), b_data(&trash), istptr(expect->data), istlen(expect->data)) != NULL;
1749 else if (expect->type ==TCPCHK_EXPECT_HTTP_BODY_LF)
1750 match = my_memmem(b_orig(&trash), b_data(&trash), b_orig(tmp), b_data(tmp)) != NULL;
1751 else
1752 match = regex_exec2(expect->regex, b_orig(&trash), b_data(&trash));
1753
Christopher Fauletac3b1552020-12-09 18:45:47 +01001754 /* Wait for more data on mismatch only if no minimum is defined (-1),
1755 * otherwise the absence of match is already conclusive.
1756 */
1757 if (!match && !last_read && (expect->min_recv == -1)) {
1758 ret = TCPCHK_EVAL_WAIT;
1759 goto out;
1760 }
1761
Willy Tarreau51cd5952020-06-05 12:25:38 +02001762 /* Set status and description in case of error */
1763 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP);
1764 if (LIST_ISEMPTY(&expect->onerror_fmt))
1765 desc = (inverse
1766 ? ist("HTTP check matched unwanted content")
1767 : ist("HTTP content check did not match"));
1768 break;
1769
1770
1771 default:
1772 /* should never happen */
1773 status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP);
1774 goto error;
1775 }
1776
Willy Tarreau51cd5952020-06-05 12:25:38 +02001777 if (!(match ^ inverse))
1778 goto error;
1779
1780 out:
1781 free_trash_chunk(tmp);
1782 free_trash_chunk(nbuf);
1783 free_trash_chunk(vbuf);
1784 free_trash_chunk(msg);
1785 return ret;
1786
1787 error:
1788 ret = TCPCHK_EVAL_STOP;
1789 msg = alloc_trash_chunk();
1790 if (msg)
1791 tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
1792 set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
1793 goto out;
1794
1795 wait_more_data:
1796 ret = TCPCHK_EVAL_WAIT;
1797 goto out;
1798}
1799
1800/* Evaluates a TCP TCPCHK_ACT_EXPECT rule. Returns TCPCHK_EVAL_WAIT to wait for
1801 * more data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP
1802 * if an error occurred.
1803 */
1804enum tcpcheck_eval_ret tcpcheck_eval_expect(struct check *check, struct tcpcheck_rule *rule, int last_read)
1805{
1806 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
1807 struct tcpcheck_expect *expect = &rule->expect;
1808 struct buffer *msg = NULL, *tmp = NULL;
1809 struct ist desc = IST_NULL;
1810 enum healthcheck_status status;
1811 int match, inverse;
1812
1813 last_read |= b_full(&check->bi);
1814
1815 /* The current expect might need more data than the previous one, check again
1816 * that the minimum amount data required to match is respected.
1817 */
1818 if (!last_read) {
1819 if ((expect->type == TCPCHK_EXPECT_STRING || expect->type == TCPCHK_EXPECT_BINARY) &&
1820 (b_data(&check->bi) < istlen(expect->data))) {
1821 ret = TCPCHK_EVAL_WAIT;
1822 goto out;
1823 }
1824 if (expect->min_recv > 0 && (b_data(&check->bi) < expect->min_recv)) {
1825 ret = TCPCHK_EVAL_WAIT;
1826 goto out;
1827 }
1828 }
1829
1830 inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV);
1831 /* Make GCC happy ; initialize match to a failure state. */
1832 match = inverse;
1833 status = ((expect->err_status != HCHK_STATUS_UNKNOWN) ? expect->err_status : HCHK_STATUS_L7RSP);
1834
1835 switch (expect->type) {
1836 case TCPCHK_EXPECT_STRING:
1837 case TCPCHK_EXPECT_BINARY:
1838 match = my_memmem(b_head(&check->bi), b_data(&check->bi), istptr(expect->data), istlen(expect->data)) != NULL;
1839 break;
1840 case TCPCHK_EXPECT_STRING_REGEX:
1841 match = regex_exec2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1));
1842 break;
1843
1844 case TCPCHK_EXPECT_BINARY_REGEX:
1845 chunk_reset(&trash);
1846 dump_binary(&trash, b_head(&check->bi), b_data(&check->bi));
1847 match = regex_exec2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1));
1848 break;
1849
1850 case TCPCHK_EXPECT_STRING_LF:
1851 case TCPCHK_EXPECT_BINARY_LF:
1852 match = 0;
1853 tmp = alloc_trash_chunk();
1854 if (!tmp) {
1855 status = HCHK_STATUS_L7RSP;
1856 desc = ist("Failed to allocate buffer to eval format string");
1857 goto error;
1858 }
1859 tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &expect->fmt);
1860 if (!b_data(tmp)) {
1861 status = HCHK_STATUS_L7RSP;
1862 desc = ist("log-format string evaluated to an empty string");
1863 goto error;
1864 }
1865 if (expect->type == TCPCHK_EXPECT_BINARY_LF) {
1866 int len = tmp->data;
1867 if (parse_binary(b_orig(tmp), &tmp->area, &len, NULL) == 0) {
1868 status = HCHK_STATUS_L7RSP;
1869 desc = ist("Failed to parse hexastring resulting of eval of a log-format string");
1870 goto error;
1871 }
1872 tmp->data = len;
1873 }
1874 if (b_data(&check->bi) < tmp->data) {
1875 if (!last_read) {
1876 ret = TCPCHK_EVAL_WAIT;
1877 goto out;
1878 }
1879 break;
1880 }
1881 match = my_memmem(b_head(&check->bi), b_data(&check->bi), b_orig(tmp), b_data(tmp)) != NULL;
1882 break;
1883
1884 case TCPCHK_EXPECT_CUSTOM:
1885 if (expect->custom)
1886 ret = expect->custom(check, rule, last_read);
1887 goto out;
1888 default:
1889 /* Should never happen. */
1890 ret = TCPCHK_EVAL_STOP;
1891 goto out;
1892 }
1893
1894
1895 /* Wait for more data on mismatch only if no minimum is defined (-1),
1896 * otherwise the absence of match is already conclusive.
1897 */
1898 if (!match && !last_read && (expect->min_recv == -1)) {
1899 ret = TCPCHK_EVAL_WAIT;
1900 goto out;
1901 }
1902
1903 /* Result as expected, next rule. */
1904 if (match ^ inverse)
1905 goto out;
1906
1907 error:
1908 /* From this point on, we matched something we did not want, this is an error state. */
1909 ret = TCPCHK_EVAL_STOP;
1910 msg = alloc_trash_chunk();
1911 if (msg)
1912 tcpcheck_expect_onerror_message(msg, check, rule, match, desc);
1913 set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
1914 free_trash_chunk(msg);
1915
1916 out:
1917 free_trash_chunk(tmp);
1918 return ret;
1919}
1920
1921/* Evaluates a TCPCHK_ACT_ACTION_KW rule. Returns TCPCHK_EVAL_CONTINUE to
1922 * evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. It never
1923 * waits.
1924 */
1925enum tcpcheck_eval_ret tcpcheck_eval_action_kw(struct check *check, struct tcpcheck_rule *rule)
1926{
1927 enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
1928 struct act_rule *act_rule;
1929 enum act_return act_ret;
1930
1931 act_rule =rule->action_kw.rule;
1932 act_ret = act_rule->action_ptr(act_rule, check->proxy, check->sess, NULL, 0);
1933 if (act_ret != ACT_RET_CONT) {
1934 chunk_printf(&trash, "TCPCHK ACTION unexpected result at step %d\n",
1935 tcpcheck_get_step_id(check, rule));
1936 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area);
1937 ret = TCPCHK_EVAL_STOP;
1938 }
1939
1940 return ret;
1941}
1942
1943/* Executes a tcp-check ruleset. Note that this is called both from the
1944 * connection's wake() callback and from the check scheduling task. It returns
1945 * 0 on normal cases, or <0 if a close() has happened on an existing connection,
1946 * presenting the risk of an fd replacement.
1947 *
1948 * Please do NOT place any return statement in this function and only leave
1949 * via the out_end_tcpcheck label after setting retcode.
1950 */
1951int tcpcheck_main(struct check *check)
1952{
1953 struct tcpcheck_rule *rule;
1954 struct conn_stream *cs = check->cs;
1955 struct connection *conn = cs_conn(cs);
1956 int must_read = 1, last_read = 0;
Christopher Faulet6f45f032020-11-25 13:34:51 +01001957 int retcode = 0;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001958 enum tcpcheck_eval_ret eval_ret;
1959
1960 /* here, we know that the check is complete or that it failed */
1961 if (check->result != CHK_RES_UNKNOWN)
1962 goto out;
1963
1964 /* Note: the conn-stream and the connection may only be undefined before
1965 * the first rule evaluation (it is always a connect rule) or when the
1966 * conn-stream allocation failed on the first connect.
1967 */
1968
1969 /* 1- check for connection error, if any */
1970 if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR))
1971 goto out_end_tcpcheck;
1972
1973 /* 2- check if we are waiting for the connection establishment. It only
1974 * happens during TCPCHK_ACT_CONNECT. */
1975 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
Christopher Faulet444b7b12021-01-18 15:47:03 +01001976 if (!cs)
1977 rule = check->current_step;
1978 else if (cs && (check->state & CHK_ST_CLOSE_CONN)) {
1979 /* We are still waiting the connection gets closed */
1980 goto out;
1981 }
1982 else {
1983 if (conn->flags & CO_FL_WAIT_XPRT) {
1984 struct tcpcheck_rule *next;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001985
Christopher Faulet444b7b12021-01-18 15:47:03 +01001986 next = get_next_tcpcheck_rule(check->tcpcheck_rules, check->current_step);
1987 if (next && next->action == TCPCHK_ACT_SEND) {
1988 if (!(check->wait_list.events & SUB_RETRY_SEND))
1989 conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001990 goto out;
Christopher Faulet444b7b12021-01-18 15:47:03 +01001991 }
1992 else {
1993 eval_ret = tcpcheck_eval_recv(check, check->current_step);
1994 if (eval_ret == TCPCHK_EVAL_STOP)
1995 goto out_end_tcpcheck;
1996 else if (eval_ret == TCPCHK_EVAL_WAIT)
1997 goto out;
1998 last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS)));
1999 must_read = 0;
2000 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02002001 }
Christopher Fauletcc554532020-12-09 19:46:38 +01002002
Christopher Faulet444b7b12021-01-18 15:47:03 +01002003 if (check->proxy->timeout.check)
2004 check->task->expire = tick_add_ifset(now_ms, check->proxy->timeout.check);
Christopher Fauletcc554532020-12-09 19:46:38 +01002005
Christopher Faulet444b7b12021-01-18 15:47:03 +01002006 rule = LIST_NEXT(&check->current_step->list, typeof(rule), list);
2007 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02002008 }
2009
Christopher Faulet6f45f032020-11-25 13:34:51 +01002010 /* 3- check if a rule must be resume. It happens if check->current_step
Willy Tarreau51cd5952020-06-05 12:25:38 +02002011 * is defined. */
2012 else if (check->current_step)
2013 rule = check->current_step;
2014
Christopher Faulet6f45f032020-11-25 13:34:51 +01002015 /* 4- It is the first evaluation. We must create a session and preset
Willy Tarreau51cd5952020-06-05 12:25:38 +02002016 * tcp-check variables */
2017 else {
2018 struct tcpcheck_var *var;
2019
2020 /* First evaluation, create a session */
2021 check->sess = session_new(&checks_fe, NULL, &check->obj_type);
2022 if (!check->sess) {
2023 chunk_printf(&trash, "TCPCHK error allocating check session");
2024 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area);
2025 goto out_end_tcpcheck;
2026 }
2027 vars_init(&check->vars, SCOPE_CHECK);
2028 rule = LIST_NEXT(check->tcpcheck_rules->list, typeof(rule), list);
2029
2030 /* Preset tcp-check variables */
2031 list_for_each_entry(var, &check->tcpcheck_rules->preset_vars, list) {
2032 struct sample smp;
2033
2034 memset(&smp, 0, sizeof(smp));
2035 smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL);
2036 smp.data = var->data;
2037 vars_set_by_name_ifexist(istptr(var->name), istlen(var->name), &smp);
2038 }
2039 }
2040
2041 /* Now evaluate the tcp-check rules */
2042
2043 list_for_each_entry_from(rule, check->tcpcheck_rules->list, list) {
2044 check->code = 0;
2045 switch (rule->action) {
2046 case TCPCHK_ACT_CONNECT:
Christopher Faulet444b7b12021-01-18 15:47:03 +01002047 /* Not the first connection, release it first */
2048 if (check->cs && check->current_step != rule) {
2049 check->state |= CHK_ST_CLOSE_CONN;
2050 retcode = -1;
2051 }
2052
Willy Tarreau51cd5952020-06-05 12:25:38 +02002053 check->current_step = rule;
2054
Christopher Faulet444b7b12021-01-18 15:47:03 +01002055 /* We are still waiting the connection gets closed */
2056 if (check->cs && (check->state & CHK_ST_CLOSE_CONN)) {
2057 eval_ret = TCPCHK_EVAL_WAIT;
2058 break;
Willy Tarreau51cd5952020-06-05 12:25:38 +02002059 }
Christopher Faulet444b7b12021-01-18 15:47:03 +01002060
Willy Tarreau51cd5952020-06-05 12:25:38 +02002061 eval_ret = tcpcheck_eval_connect(check, rule);
2062
2063 /* Refresh conn-stream and connection */
2064 cs = check->cs;
2065 conn = cs_conn(cs);
2066 must_read = 1; last_read = 0;
2067 break;
2068 case TCPCHK_ACT_SEND:
2069 check->current_step = rule;
2070 eval_ret = tcpcheck_eval_send(check, rule);
2071 must_read = 1;
2072 break;
2073 case TCPCHK_ACT_EXPECT:
2074 check->current_step = rule;
2075 if (must_read) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02002076 eval_ret = tcpcheck_eval_recv(check, rule);
2077 if (eval_ret == TCPCHK_EVAL_STOP)
2078 goto out_end_tcpcheck;
2079 else if (eval_ret == TCPCHK_EVAL_WAIT)
2080 goto out;
2081 last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS)));
2082 must_read = 0;
2083 }
2084
2085 eval_ret = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK
2086 ? tcpcheck_eval_expect_http(check, rule, last_read)
2087 : tcpcheck_eval_expect(check, rule, last_read));
2088
2089 if (eval_ret == TCPCHK_EVAL_WAIT) {
2090 check->current_step = rule->expect.head;
2091 if (!(check->wait_list.events & SUB_RETRY_RECV))
2092 conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list);
2093 }
2094 break;
2095 case TCPCHK_ACT_ACTION_KW:
2096 /* Don't update the current step */
2097 eval_ret = tcpcheck_eval_action_kw(check, rule);
2098 break;
2099 default:
2100 /* Otherwise, just go to the next one and don't update
2101 * the current step
2102 */
2103 eval_ret = TCPCHK_EVAL_CONTINUE;
2104 break;
2105 }
2106
2107 switch (eval_ret) {
2108 case TCPCHK_EVAL_CONTINUE:
2109 break;
2110 case TCPCHK_EVAL_WAIT:
2111 goto out;
2112 case TCPCHK_EVAL_STOP:
2113 goto out_end_tcpcheck;
2114 }
2115 }
2116
2117 /* All rules was evaluated */
2118 if (check->current_step) {
2119 rule = check->current_step;
2120
2121 if (rule->action == TCPCHK_ACT_EXPECT) {
2122 struct buffer *msg;
2123 enum healthcheck_status status;
2124
2125 if (check->server &&
2126 (check->server->proxy->options & PR_O_DISABLE404) &&
2127 (check->server->next_state != SRV_ST_STOPPED) &&
2128 (check->code == 404)) {
2129 set_server_check_status(check, HCHK_STATUS_L7OKCD, NULL);
2130 goto out_end_tcpcheck;
2131 }
2132
2133 msg = alloc_trash_chunk();
2134 if (msg)
2135 tcpcheck_expect_onsuccess_message(msg, check, rule, IST_NULL);
2136 status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
2137 set_server_check_status(check, status, (msg ? b_head(msg) : "(tcp-check)"));
2138 free_trash_chunk(msg);
2139 }
2140 else if (rule->action == TCPCHK_ACT_CONNECT) {
2141 const char *msg = ((rule->connect.options & TCPCHK_OPT_IMPLICIT) ? NULL : "(tcp-check)");
2142 enum healthcheck_status status = HCHK_STATUS_L4OK;
2143#ifdef USE_OPENSSL
2144 if (ssl_sock_is_ssl(conn))
2145 status = HCHK_STATUS_L6OK;
2146#endif
2147 set_server_check_status(check, status, msg);
2148 }
Christopher Fauletb4ae2472021-01-05 16:56:07 +01002149 else
2150 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
Willy Tarreau51cd5952020-06-05 12:25:38 +02002151 }
2152 else
2153 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2154
2155 out_end_tcpcheck:
2156 if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR))
2157 chk_report_conn_err(check, errno, 0);
2158
Christopher Fauletb51e0372020-11-25 13:47:00 +01002159 /* the tcpcheck is finished, release in/out buffer now */
2160 check_release_buf(check, &check->bi);
2161 check_release_buf(check, &check->bo);
2162
Willy Tarreau51cd5952020-06-05 12:25:38 +02002163 out:
2164 return retcode;
2165}
2166
2167
2168/**************************************************************************/
2169/******************* Internals to parse tcp-check rules *******************/
2170/**************************************************************************/
2171struct action_kw_list tcp_check_keywords = {
2172 .list = LIST_HEAD_INIT(tcp_check_keywords.list),
2173};
2174
2175/* Creates a tcp-check rule resulting from parsing a custom keyword. NULL is
2176 * returned on error.
2177 */
2178struct tcpcheck_rule *parse_tcpcheck_action(char **args, int cur_arg, struct proxy *px,
2179 struct list *rules, struct action_kw *kw,
2180 const char *file, int line, char **errmsg)
2181{
2182 struct tcpcheck_rule *chk = NULL;
2183 struct act_rule *actrule = NULL;
2184
2185 actrule = calloc(1, sizeof(*actrule));
2186 if (!actrule) {
2187 memprintf(errmsg, "out of memory");
2188 goto error;
2189 }
2190 actrule->kw = kw;
2191 actrule->from = ACT_F_TCP_CHK;
2192
2193 cur_arg++;
2194 if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) {
2195 memprintf(errmsg, "'%s' : %s", kw->kw, *errmsg);
2196 goto error;
2197 }
2198
2199 chk = calloc(1, sizeof(*chk));
2200 if (!chk) {
2201 memprintf(errmsg, "out of memory");
2202 goto error;
2203 }
2204 chk->action = TCPCHK_ACT_ACTION_KW;
2205 chk->action_kw.rule = actrule;
2206 return chk;
2207
2208 error:
2209 free(actrule);
2210 return NULL;
2211}
2212
2213/* Parses and creates a tcp-check connect or an http-check connect rule. NULL is
2214 * returned on error.
2215 */
2216struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct proxy *px, struct list *rules,
2217 const char *file, int line, char **errmsg)
2218{
2219 struct tcpcheck_rule *chk = NULL;
2220 struct sockaddr_storage *sk = NULL;
2221 char *comment = NULL, *sni = NULL, *alpn = NULL;
2222 struct sample_expr *port_expr = NULL;
2223 const struct mux_proto_list *mux_proto = NULL;
2224 unsigned short conn_opts = 0;
2225 long port = 0;
2226 int alpn_len = 0;
2227
2228 list_for_each_entry(chk, rules, list) {
2229 if (chk->action == TCPCHK_ACT_CONNECT)
2230 break;
2231 if (chk->action == TCPCHK_ACT_COMMENT ||
2232 chk->action == TCPCHK_ACT_ACTION_KW ||
2233 (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)))
2234 continue;
2235
2236 memprintf(errmsg, "first step MUST also be a 'connect', "
2237 "optionally preceded by a 'set-var', an 'unset-var' or a 'comment', "
2238 "when there is a 'connect' step in the tcp-check ruleset");
2239 goto error;
2240 }
2241
2242 cur_arg++;
2243 while (*(args[cur_arg])) {
2244 if (strcmp(args[cur_arg], "default") == 0)
2245 conn_opts |= TCPCHK_OPT_DEFAULT_CONNECT;
2246 else if (strcmp(args[cur_arg], "addr") == 0) {
2247 int port1, port2;
Willy Tarreau51cd5952020-06-05 12:25:38 +02002248
2249 if (!*(args[cur_arg+1])) {
2250 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[cur_arg]);
2251 goto error;
2252 }
2253
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002254 sk = str2sa_range(args[cur_arg+1], NULL, &port1, &port2, NULL, NULL,
2255 errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Willy Tarreau51cd5952020-06-05 12:25:38 +02002256 if (!sk) {
2257 memprintf(errmsg, "'%s' : %s.", args[cur_arg], *errmsg);
2258 goto error;
2259 }
2260
Willy Tarreau51cd5952020-06-05 12:25:38 +02002261 cur_arg++;
2262 }
2263 else if (strcmp(args[cur_arg], "port") == 0) {
2264 const char *p, *end;
2265
2266 if (!*(args[cur_arg+1])) {
2267 memprintf(errmsg, "'%s' expects a port number or a sample expression as argument.", args[cur_arg]);
2268 goto error;
2269 }
2270 cur_arg++;
2271
2272 port = 0;
2273 release_sample_expr(port_expr);
2274 p = args[cur_arg]; end = p + strlen(p);
2275 port = read_uint(&p, end);
2276 if (p != end) {
2277 int idx = 0;
2278
2279 px->conf.args.ctx = ARGC_SRV;
2280 port_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx,
2281 file, line, errmsg, &px->conf.args, NULL);
2282
2283 if (!port_expr) {
2284 memprintf(errmsg, "error detected while parsing port expression : %s", *errmsg);
2285 goto error;
2286 }
2287 if (!(port_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) {
2288 memprintf(errmsg, "error detected while parsing port expression : "
2289 " fetch method '%s' extracts information from '%s', "
2290 "none of which is available here.\n",
2291 args[cur_arg], sample_src_names(port_expr->fetch->use));
2292 goto error;
2293 }
2294 px->http_needed |= !!(port_expr->fetch->use & SMP_USE_HTTP_ANY);
2295 }
2296 else if (port > 65535 || port < 1) {
2297 memprintf(errmsg, "expects a valid TCP port (from range 1 to 65535) or a sample expression, got %s.",
2298 args[cur_arg]);
2299 goto error;
2300 }
2301 }
2302 else if (strcmp(args[cur_arg], "proto") == 0) {
2303 if (!*(args[cur_arg+1])) {
2304 memprintf(errmsg, "'%s' expects a MUX protocol as argument.", args[cur_arg]);
2305 goto error;
2306 }
2307 mux_proto = get_mux_proto(ist2(args[cur_arg+1], strlen(args[cur_arg+1])));
2308 if (!mux_proto) {
2309 memprintf(errmsg, "'%s' : unknown MUX protocol '%s'.", args[cur_arg], args[cur_arg+1]);
2310 goto error;
2311 }
Amaury Denoyelle34ff9ac2020-11-13 12:34:58 +01002312
2313 if (strcmp(args[0], "tcp-check") == 0 && mux_proto->mode != PROTO_MODE_TCP) {
2314 memprintf(errmsg, "'%s' : invalid MUX protocol '%s' for tcp-check", args[cur_arg], args[cur_arg+1]);
2315 goto error;
2316 }
2317 else if (strcmp(args[0], "http-check") == 0 && mux_proto->mode != PROTO_MODE_HTTP) {
2318 memprintf(errmsg, "'%s' : invalid MUX protocol '%s' for http-check", args[cur_arg], args[cur_arg+1]);
2319 goto error;
2320 }
2321
Willy Tarreau51cd5952020-06-05 12:25:38 +02002322 cur_arg++;
2323 }
2324 else if (strcmp(args[cur_arg], "comment") == 0) {
2325 if (!*(args[cur_arg+1])) {
2326 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2327 goto error;
2328 }
2329 cur_arg++;
2330 free(comment);
2331 comment = strdup(args[cur_arg]);
2332 if (!comment) {
2333 memprintf(errmsg, "out of memory");
2334 goto error;
2335 }
2336 }
2337 else if (strcmp(args[cur_arg], "send-proxy") == 0)
2338 conn_opts |= TCPCHK_OPT_SEND_PROXY;
2339 else if (strcmp(args[cur_arg], "via-socks4") == 0)
2340 conn_opts |= TCPCHK_OPT_SOCKS4;
2341 else if (strcmp(args[cur_arg], "linger") == 0)
2342 conn_opts |= TCPCHK_OPT_LINGER;
2343#ifdef USE_OPENSSL
2344 else if (strcmp(args[cur_arg], "ssl") == 0) {
2345 px->options |= PR_O_TCPCHK_SSL;
2346 conn_opts |= TCPCHK_OPT_SSL;
2347 }
2348 else if (strcmp(args[cur_arg], "sni") == 0) {
2349 if (!*(args[cur_arg+1])) {
2350 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2351 goto error;
2352 }
2353 cur_arg++;
2354 free(sni);
2355 sni = strdup(args[cur_arg]);
2356 if (!sni) {
2357 memprintf(errmsg, "out of memory");
2358 goto error;
2359 }
2360 }
2361 else if (strcmp(args[cur_arg], "alpn") == 0) {
2362#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2363 free(alpn);
2364 if (ssl_sock_parse_alpn(args[cur_arg + 1], &alpn, &alpn_len, errmsg)) {
2365 memprintf(errmsg, "'%s' : %s", args[cur_arg], *errmsg);
2366 goto error;
2367 }
2368 cur_arg++;
2369#else
2370 memprintf(errmsg, "'%s' : library does not support TLS ALPN extension.", args[cur_arg]);
2371 goto error;
2372#endif
2373 }
2374#endif /* USE_OPENSSL */
2375
2376 else {
2377 memprintf(errmsg, "expects 'comment', 'port', 'addr', 'send-proxy'"
2378#ifdef USE_OPENSSL
2379 ", 'ssl', 'sni', 'alpn'"
2380#endif /* USE_OPENSSL */
2381 " or 'via-socks4', 'linger', 'default' but got '%s' as argument.",
2382 args[cur_arg]);
2383 goto error;
2384 }
2385 cur_arg++;
2386 }
2387
2388 chk = calloc(1, sizeof(*chk));
2389 if (!chk) {
2390 memprintf(errmsg, "out of memory");
2391 goto error;
2392 }
2393 chk->action = TCPCHK_ACT_CONNECT;
2394 chk->comment = comment;
2395 chk->connect.port = port;
2396 chk->connect.options = conn_opts;
2397 chk->connect.sni = sni;
2398 chk->connect.alpn = alpn;
2399 chk->connect.alpn_len= alpn_len;
2400 chk->connect.port_expr= port_expr;
2401 chk->connect.mux_proto= mux_proto;
2402 if (sk)
2403 chk->connect.addr = *sk;
2404 return chk;
2405
2406 error:
2407 free(alpn);
2408 free(sni);
2409 free(comment);
2410 release_sample_expr(port_expr);
2411 return NULL;
2412}
2413
2414/* Parses and creates a tcp-check send rule. NULL is returned on error */
2415struct tcpcheck_rule *parse_tcpcheck_send(char **args, int cur_arg, struct proxy *px, struct list *rules,
2416 const char *file, int line, char **errmsg)
2417{
2418 struct tcpcheck_rule *chk = NULL;
2419 char *comment = NULL, *data = NULL;
2420 enum tcpcheck_send_type type = TCPCHK_SEND_UNDEF;
2421
2422 if (strcmp(args[cur_arg], "send-binary-lf") == 0)
2423 type = TCPCHK_SEND_BINARY_LF;
2424 else if (strcmp(args[cur_arg], "send-binary") == 0)
2425 type = TCPCHK_SEND_BINARY;
2426 else if (strcmp(args[cur_arg], "send-lf") == 0)
2427 type = TCPCHK_SEND_STRING_LF;
2428 else if (strcmp(args[cur_arg], "send") == 0)
2429 type = TCPCHK_SEND_STRING;
2430
2431 if (!*(args[cur_arg+1])) {
2432 memprintf(errmsg, "'%s' expects a %s as argument",
2433 (type == TCPCHK_SEND_BINARY ? "binary string": "string"), args[cur_arg]);
2434 goto error;
2435 }
2436
2437 data = args[cur_arg+1];
2438
2439 cur_arg += 2;
2440 while (*(args[cur_arg])) {
2441 if (strcmp(args[cur_arg], "comment") == 0) {
2442 if (!*(args[cur_arg+1])) {
2443 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2444 goto error;
2445 }
2446 cur_arg++;
2447 free(comment);
2448 comment = strdup(args[cur_arg]);
2449 if (!comment) {
2450 memprintf(errmsg, "out of memory");
2451 goto error;
2452 }
2453 }
2454 else {
2455 memprintf(errmsg, "expects 'comment' but got '%s' as argument.",
2456 args[cur_arg]);
2457 goto error;
2458 }
2459 cur_arg++;
2460 }
2461
2462 chk = calloc(1, sizeof(*chk));
2463 if (!chk) {
2464 memprintf(errmsg, "out of memory");
2465 goto error;
2466 }
2467 chk->action = TCPCHK_ACT_SEND;
2468 chk->comment = comment;
2469 chk->send.type = type;
2470
2471 switch (chk->send.type) {
2472 case TCPCHK_SEND_STRING:
2473 chk->send.data = ist2(strdup(data), strlen(data));
2474 if (!isttest(chk->send.data)) {
2475 memprintf(errmsg, "out of memory");
2476 goto error;
2477 }
2478 break;
2479 case TCPCHK_SEND_BINARY: {
2480 int len = chk->send.data.len;
2481 if (parse_binary(data, &chk->send.data.ptr, &len, errmsg) == 0) {
2482 memprintf(errmsg, "'%s' invalid binary string (%s).\n", data, *errmsg);
2483 goto error;
2484 }
2485 chk->send.data.len = len;
2486 break;
2487 }
2488 case TCPCHK_SEND_STRING_LF:
2489 case TCPCHK_SEND_BINARY_LF:
2490 LIST_INIT(&chk->send.fmt);
2491 px->conf.args.ctx = ARGC_SRV;
2492 if (!parse_logformat_string(data, px, &chk->send.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
2493 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", data, *errmsg);
2494 goto error;
2495 }
2496 break;
2497 case TCPCHK_SEND_HTTP:
2498 case TCPCHK_SEND_UNDEF:
2499 goto error;
2500 }
2501
2502 return chk;
2503
2504 error:
2505 free(chk);
2506 free(comment);
2507 return NULL;
2508}
2509
2510/* Parses and creates a http-check send rule. NULL is returned on error */
2511struct tcpcheck_rule *parse_tcpcheck_send_http(char **args, int cur_arg, struct proxy *px, struct list *rules,
2512 const char *file, int line, char **errmsg)
2513{
2514 struct tcpcheck_rule *chk = NULL;
2515 struct tcpcheck_http_hdr *hdr = NULL;
2516 struct http_hdr hdrs[global.tune.max_http_hdr];
2517 char *meth = NULL, *uri = NULL, *vsn = NULL;
2518 char *body = NULL, *comment = NULL;
2519 unsigned int flags = 0;
2520 int i = 0, host_hdr = -1;
2521
2522 cur_arg++;
2523 while (*(args[cur_arg])) {
2524 if (strcmp(args[cur_arg], "meth") == 0) {
2525 if (!*(args[cur_arg+1])) {
2526 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2527 goto error;
2528 }
2529 cur_arg++;
2530 meth = args[cur_arg];
2531 }
2532 else if (strcmp(args[cur_arg], "uri") == 0 || strcmp(args[cur_arg], "uri-lf") == 0) {
2533 if (!*(args[cur_arg+1])) {
2534 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2535 goto error;
2536 }
2537 flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT;
2538 if (strcmp(args[cur_arg], "uri-lf") == 0)
2539 flags |= TCPCHK_SND_HTTP_FL_URI_FMT;
2540 cur_arg++;
2541 uri = args[cur_arg];
2542 }
2543 else if (strcmp(args[cur_arg], "ver") == 0) {
2544 if (!*(args[cur_arg+1])) {
2545 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2546 goto error;
2547 }
2548 cur_arg++;
2549 vsn = args[cur_arg];
2550 }
2551 else if (strcmp(args[cur_arg], "hdr") == 0) {
2552 if (!*args[cur_arg+1] || !*args[cur_arg+2]) {
2553 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg]);
2554 goto error;
2555 }
2556
2557 if (strcasecmp(args[cur_arg+1], "host") == 0) {
2558 if (host_hdr >= 0) {
2559 memprintf(errmsg, "'%s' header already defined (previous value is '%s')",
2560 args[cur_arg+1], istptr(hdrs[host_hdr].v));
2561 goto error;
2562 }
2563 host_hdr = i;
2564 }
Amaury Denoyelleaea63022020-12-22 14:08:52 +01002565 else if (strcasecmp(args[cur_arg+1], "content-length") == 0 ||
Willy Tarreau51cd5952020-06-05 12:25:38 +02002566 strcasecmp(args[cur_arg+1], "transfer-encoding") == 0)
2567 goto skip_hdr;
2568
2569 hdrs[i].n = ist2(args[cur_arg+1], strlen(args[cur_arg+1]));
2570 hdrs[i].v = ist2(args[cur_arg+2], strlen(args[cur_arg+2]));
2571 i++;
2572 skip_hdr:
2573 cur_arg += 2;
2574 }
2575 else if (strcmp(args[cur_arg], "body") == 0 || strcmp(args[cur_arg], "body-lf") == 0) {
2576 if (!*(args[cur_arg+1])) {
2577 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2578 goto error;
2579 }
2580 flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT;
2581 if (strcmp(args[cur_arg], "body-lf") == 0)
2582 flags |= TCPCHK_SND_HTTP_FL_BODY_FMT;
2583 cur_arg++;
2584 body = args[cur_arg];
2585 }
2586 else if (strcmp(args[cur_arg], "comment") == 0) {
2587 if (!*(args[cur_arg+1])) {
2588 memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]);
2589 goto error;
2590 }
2591 cur_arg++;
2592 free(comment);
2593 comment = strdup(args[cur_arg]);
2594 if (!comment) {
2595 memprintf(errmsg, "out of memory");
2596 goto error;
2597 }
2598 }
2599 else {
2600 memprintf(errmsg, "expects 'comment', 'meth', 'uri', 'uri-lf', 'ver', 'hdr', 'body' or 'body-lf'"
2601 " but got '%s' as argument.", args[cur_arg]);
2602 goto error;
2603 }
2604 cur_arg++;
2605 }
2606
2607 hdrs[i].n = hdrs[i].v = IST_NULL;
2608
2609 chk = calloc(1, sizeof(*chk));
2610 if (!chk) {
2611 memprintf(errmsg, "out of memory");
2612 goto error;
2613 }
2614 chk->action = TCPCHK_ACT_SEND;
2615 chk->comment = comment; comment = NULL;
2616 chk->send.type = TCPCHK_SEND_HTTP;
2617 chk->send.http.flags = flags;
2618 LIST_INIT(&chk->send.http.hdrs);
2619
2620 if (meth) {
2621 chk->send.http.meth.meth = find_http_meth(meth, strlen(meth));
2622 chk->send.http.meth.str.area = strdup(meth);
2623 chk->send.http.meth.str.data = strlen(meth);
2624 if (!chk->send.http.meth.str.area) {
2625 memprintf(errmsg, "out of memory");
2626 goto error;
2627 }
2628 }
2629 if (uri) {
2630 if (chk->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) {
2631 LIST_INIT(&chk->send.http.uri_fmt);
2632 px->conf.args.ctx = ARGC_SRV;
2633 if (!parse_logformat_string(uri, px, &chk->send.http.uri_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
2634 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", uri, *errmsg);
2635 goto error;
2636 }
2637 }
2638 else {
2639 chk->send.http.uri = ist2(strdup(uri), strlen(uri));
2640 if (!isttest(chk->send.http.uri)) {
2641 memprintf(errmsg, "out of memory");
2642 goto error;
2643 }
2644 }
2645 }
2646 if (vsn) {
2647 chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn));
2648 if (!isttest(chk->send.http.vsn)) {
2649 memprintf(errmsg, "out of memory");
2650 goto error;
2651 }
2652 }
2653 for (i = 0; istlen(hdrs[i].n); i++) {
2654 hdr = calloc(1, sizeof(*hdr));
2655 if (!hdr) {
2656 memprintf(errmsg, "out of memory");
2657 goto error;
2658 }
2659 LIST_INIT(&hdr->value);
2660 hdr->name = istdup(hdrs[i].n);
2661 if (!isttest(hdr->name)) {
2662 memprintf(errmsg, "out of memory");
2663 goto error;
2664 }
2665
2666 ist0(hdrs[i].v);
2667 if (!parse_logformat_string(istptr(hdrs[i].v), px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg))
2668 goto error;
2669 LIST_ADDQ(&chk->send.http.hdrs, &hdr->list);
2670 hdr = NULL;
2671 }
2672
2673 if (body) {
2674 if (chk->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) {
2675 LIST_INIT(&chk->send.http.body_fmt);
2676 px->conf.args.ctx = ARGC_SRV;
2677 if (!parse_logformat_string(body, px, &chk->send.http.body_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
2678 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", body, *errmsg);
2679 goto error;
2680 }
2681 }
2682 else {
2683 chk->send.http.body = ist2(strdup(body), strlen(body));
2684 if (!isttest(chk->send.http.body)) {
2685 memprintf(errmsg, "out of memory");
2686 goto error;
2687 }
2688 }
2689 }
2690
2691 return chk;
2692
2693 error:
2694 free_tcpcheck_http_hdr(hdr);
2695 free_tcpcheck(chk, 0);
2696 free(comment);
2697 return NULL;
2698}
2699
2700/* Parses and creates a http-check comment rule. NULL is returned on error */
2701struct tcpcheck_rule *parse_tcpcheck_comment(char **args, int cur_arg, struct proxy *px, struct list *rules,
2702 const char *file, int line, char **errmsg)
2703{
2704 struct tcpcheck_rule *chk = NULL;
2705 char *comment = NULL;
2706
2707 if (!*(args[cur_arg+1])) {
2708 memprintf(errmsg, "expects a string as argument");
2709 goto error;
2710 }
2711 cur_arg++;
2712 comment = strdup(args[cur_arg]);
2713 if (!comment) {
2714 memprintf(errmsg, "out of memory");
2715 goto error;
2716 }
2717
2718 chk = calloc(1, sizeof(*chk));
2719 if (!chk) {
2720 memprintf(errmsg, "out of memory");
2721 goto error;
2722 }
2723 chk->action = TCPCHK_ACT_COMMENT;
2724 chk->comment = comment;
2725 return chk;
2726
2727 error:
2728 free(comment);
2729 return NULL;
2730}
2731
2732/* Parses and creates a tcp-check or an http-check expect rule. NULL is returned
2733 * on error. <proto> is set to the right protocol flags (covered by the
2734 * TCPCHK_RULES_PROTO_CHK mask).
2735 */
2736struct tcpcheck_rule *parse_tcpcheck_expect(char **args, int cur_arg, struct proxy *px,
2737 struct list *rules, unsigned int proto,
2738 const char *file, int line, char **errmsg)
2739{
2740 struct tcpcheck_rule *prev_check, *chk = NULL;
2741 struct sample_expr *status_expr = NULL;
2742 char *on_success_msg, *on_error_msg, *comment, *pattern, *npat, *vpat;
2743 enum tcpcheck_expect_type type = TCPCHK_EXPECT_UNDEF;
2744 enum healthcheck_status ok_st = HCHK_STATUS_UNKNOWN;
2745 enum healthcheck_status err_st = HCHK_STATUS_UNKNOWN;
2746 enum healthcheck_status tout_st = HCHK_STATUS_UNKNOWN;
2747 unsigned int flags = 0;
2748 long min_recv = -1;
2749 int inverse = 0;
2750
2751 on_success_msg = on_error_msg = comment = pattern = npat = vpat = NULL;
2752 if (!*(args[cur_arg+1])) {
2753 memprintf(errmsg, "expects at least a matching pattern as arguments");
2754 goto error;
2755 }
2756
2757 cur_arg++;
2758 while (*(args[cur_arg])) {
2759 int in_pattern = 0;
2760
2761 rescan:
2762 if (strcmp(args[cur_arg], "min-recv") == 0) {
2763 if (in_pattern) {
2764 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
2765 goto error;
2766 }
2767 if (!*(args[cur_arg+1])) {
2768 memprintf(errmsg, "'%s' expects a integer as argument", args[cur_arg]);
2769 goto error;
2770 }
Christopher Faulet7151a122020-11-25 17:20:57 +01002771 /* Use an signed integer here because of bufsize */
Willy Tarreau51cd5952020-06-05 12:25:38 +02002772 cur_arg++;
2773 min_recv = atol(args[cur_arg]);
2774 if (min_recv < -1 || min_recv > INT_MAX) {
2775 memprintf(errmsg, "'%s' expects -1 or an integer from 0 to INT_MAX" , args[cur_arg-1]);
2776 goto error;
2777 }
2778 }
2779 else if (*(args[cur_arg]) == '!') {
2780 in_pattern = 1;
2781 while (*(args[cur_arg]) == '!') {
2782 inverse = !inverse;
2783 args[cur_arg]++;
2784 }
2785 if (!*(args[cur_arg]))
2786 cur_arg++;
2787 goto rescan;
2788 }
2789 else if (strcmp(args[cur_arg], "string") == 0 || strcmp(args[cur_arg], "rstring") == 0) {
2790 if (type != TCPCHK_EXPECT_UNDEF) {
2791 memprintf(errmsg, "only on pattern expected");
2792 goto error;
2793 }
2794 if (proto != TCPCHK_RULES_HTTP_CHK)
2795 type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING : TCPCHK_EXPECT_STRING_REGEX);
2796 else
2797 type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_BODY : TCPCHK_EXPECT_HTTP_BODY_REGEX);
2798
2799 if (!*(args[cur_arg+1])) {
2800 memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]);
2801 goto error;
2802 }
2803 cur_arg++;
2804 pattern = args[cur_arg];
2805 }
2806 else if (strcmp(args[cur_arg], "binary") == 0 || strcmp(args[cur_arg], "rbinary") == 0) {
2807 if (proto == TCPCHK_RULES_HTTP_CHK)
2808 goto bad_http_kw;
2809 if (type != TCPCHK_EXPECT_UNDEF) {
2810 memprintf(errmsg, "only on pattern expected");
2811 goto error;
2812 }
2813 type = ((*(args[cur_arg]) == 'b') ? TCPCHK_EXPECT_BINARY : TCPCHK_EXPECT_BINARY_REGEX);
2814
2815 if (!*(args[cur_arg+1])) {
2816 memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]);
2817 goto error;
2818 }
2819 cur_arg++;
2820 pattern = args[cur_arg];
2821 }
2822 else if (strcmp(args[cur_arg], "string-lf") == 0 || strcmp(args[cur_arg], "binary-lf") == 0) {
2823 if (type != TCPCHK_EXPECT_UNDEF) {
2824 memprintf(errmsg, "only on pattern expected");
2825 goto error;
2826 }
2827 if (proto != TCPCHK_RULES_HTTP_CHK)
2828 type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING_LF : TCPCHK_EXPECT_BINARY_LF);
2829 else {
2830 if (*(args[cur_arg]) != 's')
2831 goto bad_http_kw;
2832 type = TCPCHK_EXPECT_HTTP_BODY_LF;
2833 }
2834
2835 if (!*(args[cur_arg+1])) {
2836 memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]);
2837 goto error;
2838 }
2839 cur_arg++;
2840 pattern = args[cur_arg];
2841 }
2842 else if (strcmp(args[cur_arg], "status") == 0 || strcmp(args[cur_arg], "rstatus") == 0) {
2843 if (proto != TCPCHK_RULES_HTTP_CHK)
2844 goto bad_tcp_kw;
2845 if (type != TCPCHK_EXPECT_UNDEF) {
2846 memprintf(errmsg, "only on pattern expected");
2847 goto error;
2848 }
2849 type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_STATUS : TCPCHK_EXPECT_HTTP_STATUS_REGEX);
2850
2851 if (!*(args[cur_arg+1])) {
2852 memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]);
2853 goto error;
2854 }
2855 cur_arg++;
2856 pattern = args[cur_arg];
2857 }
2858 else if (strcmp(args[cur_arg], "custom") == 0) {
2859 if (in_pattern) {
2860 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
2861 goto error;
2862 }
2863 if (type != TCPCHK_EXPECT_UNDEF) {
2864 memprintf(errmsg, "only on pattern expected");
2865 goto error;
2866 }
2867 type = TCPCHK_EXPECT_CUSTOM;
2868 }
2869 else if (strcmp(args[cur_arg], "hdr") == 0 || strcmp(args[cur_arg], "fhdr") == 0) {
2870 int orig_arg = cur_arg;
2871
2872 if (proto != TCPCHK_RULES_HTTP_CHK)
2873 goto bad_tcp_kw;
2874 if (type != TCPCHK_EXPECT_UNDEF) {
2875 memprintf(errmsg, "only on pattern expected");
2876 goto error;
2877 }
2878 type = TCPCHK_EXPECT_HTTP_HEADER;
2879
2880 if (strcmp(args[cur_arg], "fhdr") == 0)
2881 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_FULL;
2882
2883 /* Parse the name pattern, mandatory */
2884 if (!*(args[cur_arg+1]) || !*(args[cur_arg+2]) ||
2885 (strcmp(args[cur_arg+1], "name") != 0 && strcmp(args[cur_arg+1], "name-lf") != 0)) {
2886 memprintf(errmsg, "'%s' expects at the name keyword as first argument followed by a pattern",
2887 args[orig_arg]);
2888 goto error;
2889 }
2890
2891 if (strcmp(args[cur_arg+1], "name-lf") == 0)
2892 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_FMT;
2893
2894 cur_arg += 2;
2895 if (strcmp(args[cur_arg], "-m") == 0) {
2896 if (!*(args[cur_arg+1])) {
2897 memprintf(errmsg, "'%s' : '%s' expects at a matching pattern ('str', 'beg', 'end', 'sub' or 'reg')",
2898 args[orig_arg], args[cur_arg]);
2899 goto error;
2900 }
2901 if (strcmp(args[cur_arg+1], "str") == 0)
2902 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_STR;
2903 else if (strcmp(args[cur_arg+1], "beg") == 0)
2904 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_BEG;
2905 else if (strcmp(args[cur_arg+1], "end") == 0)
2906 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_END;
2907 else if (strcmp(args[cur_arg+1], "sub") == 0)
2908 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_SUB;
2909 else if (strcmp(args[cur_arg+1], "reg") == 0) {
2910 if (flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) {
2911 memprintf(errmsg, "'%s': log-format string is not supported with a regex matching method",
2912 args[orig_arg]);
2913 goto error;
2914 }
2915 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_REG;
2916 }
2917 else {
2918 memprintf(errmsg, "'%s' : '%s' only supports 'str', 'beg', 'end', 'sub' or 'reg' (got '%s')",
2919 args[orig_arg], args[cur_arg], args[cur_arg+1]);
2920 goto error;
2921 }
2922 cur_arg += 2;
2923 }
2924 else
2925 flags |= TCPCHK_EXPT_FL_HTTP_HNAME_STR;
2926 npat = args[cur_arg];
2927
2928 if (!*(args[cur_arg+1]) ||
2929 (strcmp(args[cur_arg+1], "value") != 0 && strcmp(args[cur_arg+1], "value-lf") != 0)) {
2930 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_NONE;
2931 goto next;
2932 }
2933 if (strcmp(args[cur_arg+1], "value-lf") == 0)
2934 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_FMT;
2935
2936 /* Parse the value pattern, optional */
2937 if (strcmp(args[cur_arg+2], "-m") == 0) {
2938 cur_arg += 2;
2939 if (!*(args[cur_arg+1])) {
2940 memprintf(errmsg, "'%s' : '%s' expects at a matching pattern ('str', 'beg', 'end', 'sub' or 'reg')",
2941 args[orig_arg], args[cur_arg]);
2942 goto error;
2943 }
2944 if (strcmp(args[cur_arg+1], "str") == 0)
2945 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_STR;
2946 else if (strcmp(args[cur_arg+1], "beg") == 0)
2947 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_BEG;
2948 else if (strcmp(args[cur_arg+1], "end") == 0)
2949 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_END;
2950 else if (strcmp(args[cur_arg+1], "sub") == 0)
2951 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_SUB;
2952 else if (strcmp(args[cur_arg+1], "reg") == 0) {
2953 if (flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) {
2954 memprintf(errmsg, "'%s': log-format string is not supported with a regex matching method",
2955 args[orig_arg]);
2956 goto error;
2957 }
2958 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_REG;
2959 }
2960 else {
2961 memprintf(errmsg, "'%s' : '%s' only supports 'str', 'beg', 'end', 'sub' or 'reg' (got '%s')",
2962 args[orig_arg], args[cur_arg], args[cur_arg+1]);
2963 goto error;
2964 }
2965 }
2966 else
2967 flags |= TCPCHK_EXPT_FL_HTTP_HVAL_STR;
2968
2969 if (!*(args[cur_arg+2])) {
2970 memprintf(errmsg, "'%s' expect a pattern with the value keyword", args[orig_arg]);
2971 goto error;
2972 }
2973 vpat = args[cur_arg+2];
2974 cur_arg += 2;
2975 }
2976 else if (strcmp(args[cur_arg], "comment") == 0) {
2977 if (in_pattern) {
2978 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
2979 goto error;
2980 }
2981 if (!*(args[cur_arg+1])) {
2982 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
2983 goto error;
2984 }
2985 cur_arg++;
2986 free(comment);
2987 comment = strdup(args[cur_arg]);
2988 if (!comment) {
2989 memprintf(errmsg, "out of memory");
2990 goto error;
2991 }
2992 }
2993 else if (strcmp(args[cur_arg], "on-success") == 0) {
2994 if (in_pattern) {
2995 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
2996 goto error;
2997 }
2998 if (!*(args[cur_arg+1])) {
2999 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
3000 goto error;
3001 }
3002 cur_arg++;
3003 on_success_msg = args[cur_arg];
3004 }
3005 else if (strcmp(args[cur_arg], "on-error") == 0) {
3006 if (in_pattern) {
3007 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
3008 goto error;
3009 }
3010 if (!*(args[cur_arg+1])) {
3011 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
3012 goto error;
3013 }
3014 cur_arg++;
3015 on_error_msg = args[cur_arg];
3016 }
3017 else if (strcmp(args[cur_arg], "ok-status") == 0) {
3018 if (in_pattern) {
3019 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
3020 goto error;
3021 }
3022 if (!*(args[cur_arg+1])) {
3023 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
3024 goto error;
3025 }
3026 if (strcasecmp(args[cur_arg+1], "L7OK") == 0)
3027 ok_st = HCHK_STATUS_L7OKD;
3028 else if (strcasecmp(args[cur_arg+1], "L7OKC") == 0)
3029 ok_st = HCHK_STATUS_L7OKCD;
3030 else if (strcasecmp(args[cur_arg+1], "L6OK") == 0)
3031 ok_st = HCHK_STATUS_L6OK;
3032 else if (strcasecmp(args[cur_arg+1], "L4OK") == 0)
3033 ok_st = HCHK_STATUS_L4OK;
3034 else {
3035 memprintf(errmsg, "'%s' only supports 'L4OK', 'L6OK', 'L7OK' or 'L7OKC' status (got '%s').",
3036 args[cur_arg], args[cur_arg+1]);
3037 goto error;
3038 }
3039 cur_arg++;
3040 }
3041 else if (strcmp(args[cur_arg], "error-status") == 0) {
3042 if (in_pattern) {
3043 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
3044 goto error;
3045 }
3046 if (!*(args[cur_arg+1])) {
3047 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
3048 goto error;
3049 }
3050 if (strcasecmp(args[cur_arg+1], "L7RSP") == 0)
3051 err_st = HCHK_STATUS_L7RSP;
3052 else if (strcasecmp(args[cur_arg+1], "L7STS") == 0)
3053 err_st = HCHK_STATUS_L7STS;
3054 else if (strcasecmp(args[cur_arg+1], "L6RSP") == 0)
3055 err_st = HCHK_STATUS_L6RSP;
3056 else if (strcasecmp(args[cur_arg+1], "L4CON") == 0)
3057 err_st = HCHK_STATUS_L4CON;
3058 else {
3059 memprintf(errmsg, "'%s' only supports 'L4CON', 'L6RSP', 'L7RSP' or 'L7STS' status (got '%s').",
3060 args[cur_arg], args[cur_arg+1]);
3061 goto error;
3062 }
3063 cur_arg++;
3064 }
3065 else if (strcmp(args[cur_arg], "status-code") == 0) {
3066 int idx = 0;
3067
3068 if (in_pattern) {
3069 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
3070 goto error;
3071 }
3072 if (!*(args[cur_arg+1])) {
3073 memprintf(errmsg, "'%s' expects an expression as argument", args[cur_arg]);
3074 goto error;
3075 }
3076
3077 cur_arg++;
3078 release_sample_expr(status_expr);
3079 px->conf.args.ctx = ARGC_SRV;
3080 status_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx,
3081 file, line, errmsg, &px->conf.args, NULL);
3082 if (!status_expr) {
3083 memprintf(errmsg, "error detected while parsing status-code expression : %s", *errmsg);
3084 goto error;
3085 }
3086 if (!(status_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) {
3087 memprintf(errmsg, "error detected while parsing status-code expression : "
3088 " fetch method '%s' extracts information from '%s', "
3089 "none of which is available here.\n",
3090 args[cur_arg], sample_src_names(status_expr->fetch->use));
3091 goto error;
3092 }
3093 px->http_needed |= !!(status_expr->fetch->use & SMP_USE_HTTP_ANY);
3094 }
3095 else if (strcmp(args[cur_arg], "tout-status") == 0) {
3096 if (in_pattern) {
3097 memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]);
3098 goto error;
3099 }
3100 if (!*(args[cur_arg+1])) {
3101 memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]);
3102 goto error;
3103 }
3104 if (strcasecmp(args[cur_arg+1], "L7TOUT") == 0)
3105 tout_st = HCHK_STATUS_L7TOUT;
3106 else if (strcasecmp(args[cur_arg+1], "L6TOUT") == 0)
3107 tout_st = HCHK_STATUS_L6TOUT;
3108 else if (strcasecmp(args[cur_arg+1], "L4TOUT") == 0)
3109 tout_st = HCHK_STATUS_L4TOUT;
3110 else {
3111 memprintf(errmsg, "'%s' only supports 'L4TOUT', 'L6TOUT' or 'L7TOUT' status (got '%s').",
3112 args[cur_arg], args[cur_arg+1]);
3113 goto error;
3114 }
3115 cur_arg++;
3116 }
3117 else {
3118 if (proto == TCPCHK_RULES_HTTP_CHK) {
3119 bad_http_kw:
3120 memprintf(errmsg, "'only supports min-recv, [!]string', '[!]rstring', '[!]string-lf', '[!]status', "
3121 "'[!]rstatus', [!]hdr, [!]fhdr or comment but got '%s' as argument.", args[cur_arg]);
3122 }
3123 else {
3124 bad_tcp_kw:
3125 memprintf(errmsg, "'only supports min-recv, '[!]binary', '[!]string', '[!]rstring', '[!]string-lf'"
3126 "'[!]rbinary', '[!]binary-lf' or comment but got '%s' as argument.", args[cur_arg]);
3127 }
3128 goto error;
3129 }
3130 next:
3131 cur_arg++;
3132 }
3133
3134 chk = calloc(1, sizeof(*chk));
3135 if (!chk) {
3136 memprintf(errmsg, "out of memory");
3137 goto error;
3138 }
3139 chk->action = TCPCHK_ACT_EXPECT;
3140 LIST_INIT(&chk->expect.onerror_fmt);
3141 LIST_INIT(&chk->expect.onsuccess_fmt);
3142 chk->comment = comment; comment = NULL;
3143 chk->expect.type = type;
3144 chk->expect.min_recv = min_recv;
3145 chk->expect.flags = flags | (inverse ? TCPCHK_EXPT_FL_INV : 0);
3146 chk->expect.ok_status = ok_st;
3147 chk->expect.err_status = err_st;
3148 chk->expect.tout_status = tout_st;
3149 chk->expect.status_expr = status_expr; status_expr = NULL;
3150
3151 if (on_success_msg) {
3152 px->conf.args.ctx = ARGC_SRV;
3153 if (!parse_logformat_string(on_success_msg, px, &chk->expect.onsuccess_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
3154 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_success_msg, *errmsg);
3155 goto error;
3156 }
3157 }
3158 if (on_error_msg) {
3159 px->conf.args.ctx = ARGC_SRV;
3160 if (!parse_logformat_string(on_error_msg, px, &chk->expect.onerror_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
3161 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_error_msg, *errmsg);
3162 goto error;
3163 }
3164 }
3165
3166 switch (chk->expect.type) {
3167 case TCPCHK_EXPECT_HTTP_STATUS: {
3168 const char *p = pattern;
3169 unsigned int c1,c2;
3170
3171 chk->expect.codes.codes = NULL;
3172 chk->expect.codes.num = 0;
3173 while (1) {
3174 c1 = c2 = read_uint(&p, pattern + strlen(pattern));
3175 if (*p == '-') {
3176 p++;
3177 c2 = read_uint(&p, pattern + strlen(pattern));
3178 }
3179 if (c1 > c2) {
3180 memprintf(errmsg, "invalid range of status codes '%s'", pattern);
3181 goto error;
3182 }
3183
3184 chk->expect.codes.num++;
3185 chk->expect.codes.codes = my_realloc2(chk->expect.codes.codes,
3186 chk->expect.codes.num * sizeof(*chk->expect.codes.codes));
3187 if (!chk->expect.codes.codes) {
3188 memprintf(errmsg, "out of memory");
3189 goto error;
3190 }
3191 chk->expect.codes.codes[chk->expect.codes.num-1][0] = c1;
3192 chk->expect.codes.codes[chk->expect.codes.num-1][1] = c2;
3193
3194 if (*p == '\0')
3195 break;
3196 if (*p != ',') {
3197 memprintf(errmsg, "invalid character '%c' in the list of status codes", *p);
3198 goto error;
3199 }
3200 p++;
3201 }
3202 break;
3203 }
3204 case TCPCHK_EXPECT_STRING:
3205 case TCPCHK_EXPECT_HTTP_BODY:
3206 chk->expect.data = ist2(strdup(pattern), strlen(pattern));
3207 if (!isttest(chk->expect.data)) {
3208 memprintf(errmsg, "out of memory");
3209 goto error;
3210 }
3211 break;
3212 case TCPCHK_EXPECT_BINARY: {
3213 int len = chk->expect.data.len;
3214
3215 if (parse_binary(pattern, &chk->expect.data.ptr, &len, errmsg) == 0) {
3216 memprintf(errmsg, "invalid binary string (%s)", *errmsg);
3217 goto error;
3218 }
3219 chk->expect.data.len = len;
3220 break;
3221 }
3222 case TCPCHK_EXPECT_STRING_REGEX:
3223 case TCPCHK_EXPECT_BINARY_REGEX:
3224 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
3225 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
3226 chk->expect.regex = regex_comp(pattern, 1, 0, errmsg);
3227 if (!chk->expect.regex)
3228 goto error;
3229 break;
3230
3231 case TCPCHK_EXPECT_STRING_LF:
3232 case TCPCHK_EXPECT_BINARY_LF:
3233 case TCPCHK_EXPECT_HTTP_BODY_LF:
3234 LIST_INIT(&chk->expect.fmt);
3235 px->conf.args.ctx = ARGC_SRV;
3236 if (!parse_logformat_string(pattern, px, &chk->expect.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
3237 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", pattern, *errmsg);
3238 goto error;
3239 }
3240 break;
3241
3242 case TCPCHK_EXPECT_HTTP_HEADER:
3243 if (!npat) {
3244 memprintf(errmsg, "unexpected error, undefined header name pattern");
3245 goto error;
3246 }
3247 if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG) {
3248 chk->expect.hdr.name_re = regex_comp(npat, 0, 0, errmsg);
3249 if (!chk->expect.hdr.name_re)
3250 goto error;
3251 }
3252 else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) {
3253 px->conf.args.ctx = ARGC_SRV;
3254 LIST_INIT(&chk->expect.hdr.name_fmt);
3255 if (!parse_logformat_string(npat, px, &chk->expect.hdr.name_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
3256 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", npat, *errmsg);
3257 goto error;
3258 }
3259 }
3260 else {
3261 chk->expect.hdr.name = ist2(strdup(npat), strlen(npat));
3262 if (!isttest(chk->expect.hdr.name)) {
3263 memprintf(errmsg, "out of memory");
3264 goto error;
3265 }
3266 }
3267
3268 if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE) {
3269 chk->expect.hdr.value = IST_NULL;
3270 break;
3271 }
3272
3273 if (!vpat) {
3274 memprintf(errmsg, "unexpected error, undefined header value pattern");
3275 goto error;
3276 }
3277 else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG) {
3278 chk->expect.hdr.value_re = regex_comp(vpat, 1, 0, errmsg);
3279 if (!chk->expect.hdr.value_re)
3280 goto error;
3281 }
3282 else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) {
3283 px->conf.args.ctx = ARGC_SRV;
3284 LIST_INIT(&chk->expect.hdr.value_fmt);
3285 if (!parse_logformat_string(vpat, px, &chk->expect.hdr.value_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) {
3286 memprintf(errmsg, "'%s' invalid log-format string (%s).\n", npat, *errmsg);
3287 goto error;
3288 }
3289 }
3290 else {
3291 chk->expect.hdr.value = ist2(strdup(vpat), strlen(vpat));
3292 if (!isttest(chk->expect.hdr.value)) {
3293 memprintf(errmsg, "out of memory");
3294 goto error;
3295 }
3296 }
3297
3298 break;
3299 case TCPCHK_EXPECT_CUSTOM:
3300 chk->expect.custom = NULL; /* Must be defined by the caller ! */
3301 break;
3302 case TCPCHK_EXPECT_UNDEF:
3303 memprintf(errmsg, "pattern not found");
3304 goto error;
3305 }
3306
3307 /* All tcp-check expect points back to the first inverse expect rule in
3308 * a chain of one or more expect rule, potentially itself.
3309 */
3310 chk->expect.head = chk;
3311 list_for_each_entry_rev(prev_check, rules, list) {
3312 if (prev_check->action == TCPCHK_ACT_EXPECT) {
3313 if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV)
3314 chk->expect.head = prev_check;
3315 continue;
3316 }
3317 if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW)
3318 break;
3319 }
3320 return chk;
3321
3322 error:
3323 free_tcpcheck(chk, 0);
3324 free(comment);
3325 release_sample_expr(status_expr);
3326 return NULL;
3327}
3328
3329/* Overwrites fields of the old http send rule with those of the new one. When
3330 * replaced, old values are freed and replaced by the new ones. New values are
3331 * not copied but transferred. At the end <new> should be empty and can be
3332 * safely released. This function never fails.
3333 */
3334void tcpcheck_overwrite_send_http_rule(struct tcpcheck_rule *old, struct tcpcheck_rule *new)
3335{
3336 struct logformat_node *lf, *lfb;
3337 struct tcpcheck_http_hdr *hdr, *bhdr;
3338
3339
3340 if (new->send.http.meth.str.area) {
3341 free(old->send.http.meth.str.area);
3342 old->send.http.meth.meth = new->send.http.meth.meth;
3343 old->send.http.meth.str.area = new->send.http.meth.str.area;
3344 old->send.http.meth.str.data = new->send.http.meth.str.data;
3345 new->send.http.meth.str = BUF_NULL;
3346 }
3347
3348 if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && isttest(new->send.http.uri)) {
3349 if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT))
3350 istfree(&old->send.http.uri);
3351 else
3352 free_tcpcheck_fmt(&old->send.http.uri_fmt);
3353 old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT;
3354 old->send.http.uri = new->send.http.uri;
3355 new->send.http.uri = IST_NULL;
3356 }
3357 else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && !LIST_ISEMPTY(&new->send.http.uri_fmt)) {
3358 if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT))
3359 istfree(&old->send.http.uri);
3360 else
3361 free_tcpcheck_fmt(&old->send.http.uri_fmt);
3362 old->send.http.flags |= TCPCHK_SND_HTTP_FL_URI_FMT;
3363 LIST_INIT(&old->send.http.uri_fmt);
3364 list_for_each_entry_safe(lf, lfb, &new->send.http.uri_fmt, list) {
3365 LIST_DEL(&lf->list);
3366 LIST_ADDQ(&old->send.http.uri_fmt, &lf->list);
3367 }
3368 }
3369
3370 if (isttest(new->send.http.vsn)) {
3371 istfree(&old->send.http.vsn);
3372 old->send.http.vsn = new->send.http.vsn;
3373 new->send.http.vsn = IST_NULL;
3374 }
3375
3376 free_tcpcheck_http_hdrs(&old->send.http.hdrs);
3377 list_for_each_entry_safe(hdr, bhdr, &new->send.http.hdrs, list) {
3378 LIST_DEL(&hdr->list);
3379 LIST_ADDQ(&old->send.http.hdrs, &hdr->list);
3380 }
3381
3382 if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && isttest(new->send.http.body)) {
3383 if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT))
3384 istfree(&old->send.http.body);
3385 else
3386 free_tcpcheck_fmt(&old->send.http.body_fmt);
3387 old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT;
3388 old->send.http.body = new->send.http.body;
3389 new->send.http.body = IST_NULL;
3390 }
3391 else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && !LIST_ISEMPTY(&new->send.http.body_fmt)) {
3392 if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT))
3393 istfree(&old->send.http.body);
3394 else
3395 free_tcpcheck_fmt(&old->send.http.body_fmt);
3396 old->send.http.flags |= TCPCHK_SND_HTTP_FL_BODY_FMT;
3397 LIST_INIT(&old->send.http.body_fmt);
3398 list_for_each_entry_safe(lf, lfb, &new->send.http.body_fmt, list) {
3399 LIST_DEL(&lf->list);
3400 LIST_ADDQ(&old->send.http.body_fmt, &lf->list);
3401 }
3402 }
3403}
3404
3405/* Internal function used to add an http-check rule in a list during the config
3406 * parsing step. Depending on its type, and the previously inserted rules, a
3407 * specific action may be performed or an error may be reported. This functions
3408 * returns 1 on success and 0 on error and <errmsg> is filled with the error
3409 * message.
3410 */
3411int tcpcheck_add_http_rule(struct tcpcheck_rule *chk, struct tcpcheck_rules *rules, char **errmsg)
3412{
3413 struct tcpcheck_rule *r;
3414
3415 /* the implicit send rule coming from an "option httpchk" line must be
3416 * merged with the first explici http-check send rule, if
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003417 * any. Depending on the declaration order some tests are required.
Willy Tarreau51cd5952020-06-05 12:25:38 +02003418 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003419 * Some tests are also required for other kinds of http-check rules to be
Willy Tarreau51cd5952020-06-05 12:25:38 +02003420 * sure the ruleset remains valid.
3421 */
3422
3423 if (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) {
3424 /* Tries to add an implicit http-check send rule from an "option httpchk" line.
3425 * First, the first rule is retrieved, skipping the first CONNECT, if any, and
3426 * following tests are performed :
3427 *
3428 * 1- If there is no such rule or if it is not a send rule, the implicit send
3429 * rule is pushed in front of the ruleset
3430 *
3431 * 2- If it is another implicit send rule, it is replaced with the new one.
3432 *
3433 * 3- Otherwise, it means it is an explicit send rule. In this case we merge
3434 * both, overwriting the old send rule (the explicit one) with info of the
3435 * new send rule (the implicit one).
3436 */
3437 r = get_first_tcpcheck_rule(rules);
3438 if (r && r->action == TCPCHK_ACT_CONNECT)
3439 r = get_next_tcpcheck_rule(rules, r);
3440 if (!r || r->action != TCPCHK_ACT_SEND)
3441 LIST_ADD(rules->list, &chk->list);
3442 else if (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT) {
3443 LIST_DEL(&r->list);
3444 free_tcpcheck(r, 0);
3445 LIST_ADD(rules->list, &chk->list);
3446 }
3447 else {
3448 tcpcheck_overwrite_send_http_rule(r, chk);
3449 free_tcpcheck(chk, 0);
3450 }
3451 }
3452 else {
3453 /* Tries to add an explicit http-check rule. First of all we check the typefo the
3454 * last inserted rule to be sure it is valid. Then for send rule, we try to merge it
3455 * with an existing implicit send rule, if any. At the end, if there is no error,
3456 * the rule is appended to the list.
3457 */
3458
3459 r = get_last_tcpcheck_rule(rules);
3460 if (!r || (r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)))
3461 /* no error */;
3462 else if (r->action != TCPCHK_ACT_CONNECT && chk->action == TCPCHK_ACT_SEND) {
3463 memprintf(errmsg, "unable to add http-check send rule at step %d (missing connect rule).",
3464 chk->index+1);
3465 return 0;
3466 }
3467 else if (r->action != TCPCHK_ACT_SEND && r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_EXPECT) {
3468 memprintf(errmsg, "unable to add http-check expect rule at step %d (missing send rule).",
3469 chk->index+1);
3470 return 0;
3471 }
3472 else if (r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_CONNECT) {
3473 memprintf(errmsg, "unable to add http-check connect rule at step %d (missing expect rule).",
3474 chk->index+1);
3475 return 0;
3476 }
3477
3478 if (chk->action == TCPCHK_ACT_SEND) {
3479 r = get_first_tcpcheck_rule(rules);
3480 if (r && r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) {
3481 tcpcheck_overwrite_send_http_rule(r, chk);
3482 free_tcpcheck(chk, 0);
3483 LIST_DEL(&r->list);
3484 r->send.http.flags &= ~TCPCHK_SND_HTTP_FROM_OPT;
3485 chk = r;
3486 }
3487 }
3488 LIST_ADDQ(rules->list, &chk->list);
3489 }
3490 return 1;
3491}
3492
3493/* Check tcp-check health-check configuration for the proxy <px>. */
3494static int check_proxy_tcpcheck(struct proxy *px)
3495{
3496 struct tcpcheck_rule *chk, *back;
3497 char *comment = NULL, *errmsg = NULL;
3498 enum tcpcheck_rule_type prev_action = TCPCHK_ACT_COMMENT;
3499 int ret = 0;
3500
3501 if (!(px->cap & PR_CAP_BE) || (px->options2 & PR_O2_CHK_ANY) != PR_O2_TCPCHK_CHK) {
3502 deinit_proxy_tcpcheck(px);
3503 goto out;
3504 }
3505
3506 free(px->check_command);
3507 free(px->check_path);
3508 px->check_command = px->check_path = NULL;
3509
3510 if (!px->tcpcheck_rules.list) {
3511 ha_alert("config : proxy '%s' : tcp-check configured but no ruleset defined.\n", px->id);
3512 ret |= ERR_ALERT | ERR_FATAL;
3513 goto out;
3514 }
3515
3516 /* HTTP ruleset only : */
3517 if ((px->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
3518 struct tcpcheck_rule *next;
3519
3520 /* move remaining implicit send rule from "option httpchk" line to the right place.
3521 * If such rule exists, it must be the first one. In this case, the rule is moved
3522 * after the first connect rule, if any. Otherwise, nothing is done.
3523 */
3524 chk = get_first_tcpcheck_rule(&px->tcpcheck_rules);
3525 if (chk && chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) {
3526 next = get_next_tcpcheck_rule(&px->tcpcheck_rules, chk);
3527 if (next && next->action == TCPCHK_ACT_CONNECT) {
3528 LIST_DEL(&chk->list);
3529 LIST_ADD(&next->list, &chk->list);
Christopher Faulet2eafa552021-06-25 11:37:45 +02003530 chk->index = next->index + 1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02003531 }
3532 }
3533
3534 /* add implicit expect rule if the last one is a send. It is inherited from previous
3535 * versions where the http expect rule was optional. Now it is possible to chained
3536 * send/expect rules but the last expect may still be implicit.
3537 */
3538 chk = get_last_tcpcheck_rule(&px->tcpcheck_rules);
3539 if (chk && chk->action == TCPCHK_ACT_SEND) {
3540 next = parse_tcpcheck_expect((char *[]){"http-check", "expect", "status", "200-399", ""},
3541 1, px, px->tcpcheck_rules.list, TCPCHK_RULES_HTTP_CHK,
3542 px->conf.file, px->conf.line, &errmsg);
3543 if (!next) {
3544 ha_alert("config : proxy '%s': unable to add implicit http-check expect rule "
3545 "(%s).\n", px->id, errmsg);
3546 free(errmsg);
3547 ret |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
3550 LIST_ADDQ(px->tcpcheck_rules.list, &next->list);
Christopher Faulet2eafa552021-06-25 11:37:45 +02003551 next->index = chk->index + 1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02003552 }
3553 }
3554
3555 /* For all ruleset: */
3556
3557 /* If there is no connect rule preceding all send / expect rules, an
3558 * implicit one is inserted before all others.
3559 */
3560 chk = get_first_tcpcheck_rule(&px->tcpcheck_rules);
3561 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
3562 chk = calloc(1, sizeof(*chk));
3563 if (!chk) {
3564 ha_alert("config : proxy '%s': unable to add implicit tcp-check connect rule "
3565 "(out of memory).\n", px->id);
3566 ret |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
3569 chk->action = TCPCHK_ACT_CONNECT;
3570 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
3571 LIST_ADD(px->tcpcheck_rules.list, &chk->list);
3572 }
3573
3574 /* Remove all comment rules. To do so, when a such rule is found, the
3575 * comment is assigned to the following rule(s).
3576 */
3577 list_for_each_entry_safe(chk, back, px->tcpcheck_rules.list, list) {
3578 if (chk->action != prev_action && prev_action != TCPCHK_ACT_COMMENT) {
3579 free(comment);
3580 comment = NULL;
3581 }
3582
3583 prev_action = chk->action;
3584 switch (chk->action) {
3585 case TCPCHK_ACT_COMMENT:
3586 free(comment);
3587 comment = chk->comment;
3588 LIST_DEL(&chk->list);
3589 free(chk);
3590 break;
3591 case TCPCHK_ACT_CONNECT:
3592 if (!chk->comment && comment)
3593 chk->comment = strdup(comment);
Tim Duesterhus588b3142020-05-29 14:35:51 +02003594 /* fall through */
Willy Tarreau51cd5952020-06-05 12:25:38 +02003595 case TCPCHK_ACT_ACTION_KW:
3596 free(comment);
3597 comment = NULL;
3598 break;
3599 case TCPCHK_ACT_SEND:
3600 case TCPCHK_ACT_EXPECT:
3601 if (!chk->comment && comment)
3602 chk->comment = strdup(comment);
3603 break;
3604 }
3605 }
3606 free(comment);
3607 comment = NULL;
3608
3609 out:
3610 return ret;
3611}
3612
3613void deinit_proxy_tcpcheck(struct proxy *px)
3614{
3615 free_tcpcheck_vars(&px->tcpcheck_rules.preset_vars);
3616 px->tcpcheck_rules.flags = 0;
3617 px->tcpcheck_rules.list = NULL;
3618}
3619
3620static void deinit_tcpchecks()
3621{
3622 struct tcpcheck_ruleset *rs;
3623 struct tcpcheck_rule *r, *rb;
3624 struct ebpt_node *node, *next;
3625
3626 node = ebpt_first(&shared_tcpchecks);
3627 while (node) {
3628 next = ebpt_next(node);
3629 ebpt_delete(node);
3630 free(node->key);
3631 rs = container_of(node, typeof(*rs), node);
3632 list_for_each_entry_safe(r, rb, &rs->rules, list) {
3633 LIST_DEL(&r->list);
3634 free_tcpcheck(r, 0);
3635 }
3636 free(rs);
3637 node = next;
3638 }
3639}
3640
3641int add_tcpcheck_expect_str(struct tcpcheck_rules *rules, const char *str)
3642{
3643 struct tcpcheck_rule *tcpcheck, *prev_check;
3644 struct tcpcheck_expect *expect;
3645
3646 if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL)
3647 return 0;
3648 memset(tcpcheck, 0, sizeof(*tcpcheck));
3649 tcpcheck->action = TCPCHK_ACT_EXPECT;
3650
3651 expect = &tcpcheck->expect;
3652 expect->type = TCPCHK_EXPECT_STRING;
3653 LIST_INIT(&expect->onerror_fmt);
3654 LIST_INIT(&expect->onsuccess_fmt);
3655 expect->ok_status = HCHK_STATUS_L7OKD;
3656 expect->err_status = HCHK_STATUS_L7RSP;
3657 expect->tout_status = HCHK_STATUS_L7TOUT;
3658 expect->data = ist2(strdup(str), strlen(str));
3659 if (!isttest(expect->data)) {
3660 pool_free(pool_head_tcpcheck_rule, tcpcheck);
3661 return 0;
3662 }
3663
3664 /* All tcp-check expect points back to the first inverse expect rule
3665 * in a chain of one or more expect rule, potentially itself.
3666 */
3667 tcpcheck->expect.head = tcpcheck;
3668 list_for_each_entry_rev(prev_check, rules->list, list) {
3669 if (prev_check->action == TCPCHK_ACT_EXPECT) {
3670 if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV)
3671 tcpcheck->expect.head = prev_check;
3672 continue;
3673 }
3674 if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW)
3675 break;
3676 }
3677 LIST_ADDQ(rules->list, &tcpcheck->list);
3678 return 1;
3679}
3680
3681int add_tcpcheck_send_strs(struct tcpcheck_rules *rules, const char * const *strs)
3682{
3683 struct tcpcheck_rule *tcpcheck;
3684 struct tcpcheck_send *send;
3685 const char *in;
3686 char *dst;
3687 int i;
3688
3689 if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL)
3690 return 0;
3691 memset(tcpcheck, 0, sizeof(*tcpcheck));
3692 tcpcheck->action = TCPCHK_ACT_SEND;
3693
3694 send = &tcpcheck->send;
3695 send->type = TCPCHK_SEND_STRING;
3696
3697 for (i = 0; strs[i]; i++)
3698 send->data.len += strlen(strs[i]);
3699
3700 send->data.ptr = malloc(istlen(send->data) + 1);
3701 if (!isttest(send->data)) {
3702 pool_free(pool_head_tcpcheck_rule, tcpcheck);
3703 return 0;
3704 }
3705
3706 dst = istptr(send->data);
3707 for (i = 0; strs[i]; i++)
3708 for (in = strs[i]; (*dst = *in++); dst++);
3709 *dst = 0;
3710
3711 LIST_ADDQ(rules->list, &tcpcheck->list);
3712 return 1;
3713}
3714
3715/* Parses the "tcp-check" proxy keyword */
3716static int proxy_parse_tcpcheck(char **args, int section, struct proxy *curpx,
3717 struct proxy *defpx, const char *file, int line,
3718 char **errmsg)
3719{
3720 struct tcpcheck_ruleset *rs = NULL;
3721 struct tcpcheck_rule *chk = NULL;
3722 int index, cur_arg, ret = 0;
3723
3724 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL))
3725 ret = 1;
3726
3727 /* Deduce the ruleset name from the proxy info */
3728 chunk_printf(&trash, "*tcp-check-%s_%s-%d",
3729 ((curpx == defpx) ? "defaults" : curpx->id),
3730 curpx->conf.file, curpx->conf.line);
3731
3732 rs = find_tcpcheck_ruleset(b_orig(&trash));
3733 if (rs == NULL) {
3734 rs = create_tcpcheck_ruleset(b_orig(&trash));
3735 if (rs == NULL) {
3736 memprintf(errmsg, "out of memory.\n");
3737 goto error;
3738 }
3739 }
3740
3741 index = 0;
3742 if (!LIST_ISEMPTY(&rs->rules)) {
3743 chk = LIST_PREV(&rs->rules, typeof(chk), list);
3744 index = chk->index + 1;
Christopher Faulet37e583d2021-03-12 12:00:14 +01003745 chk = NULL;
Willy Tarreau51cd5952020-06-05 12:25:38 +02003746 }
3747
3748 cur_arg = 1;
3749 if (strcmp(args[cur_arg], "connect") == 0)
3750 chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
3751 else if (strcmp(args[cur_arg], "send") == 0 || strcmp(args[cur_arg], "send-binary") == 0 ||
3752 strcmp(args[cur_arg], "send-lf") == 0 || strcmp(args[cur_arg], "send-binary-lf") == 0)
3753 chk = parse_tcpcheck_send(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
3754 else if (strcmp(args[cur_arg], "expect") == 0)
3755 chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, 0, file, line, errmsg);
3756 else if (strcmp(args[cur_arg], "comment") == 0)
3757 chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
3758 else {
3759 struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]);
3760
3761 if (!kw) {
3762 action_kw_tcp_check_build_list(&trash);
3763 memprintf(errmsg, "'%s' only supports 'comment', 'connect', 'send', 'send-binary', 'expect'"
3764 "%s%s. but got '%s'",
3765 args[0], (*trash.area ? ", " : ""), trash.area, args[1]);
3766 goto error;
3767 }
3768 chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg);
3769 }
3770
3771 if (!chk) {
3772 memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg);
3773 goto error;
3774 }
3775 ret = (ret || (*errmsg != NULL)); /* Handle warning */
3776
3777 /* No error: add the tcp-check rule in the list */
3778 chk->index = index;
3779 LIST_ADDQ(&rs->rules, &chk->list);
3780
3781 if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
3782 (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
3783 /* Use this ruleset if the proxy already has tcp-check enabled */
3784 curpx->tcpcheck_rules.list = &rs->rules;
3785 curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_TCP_RS;
3786 }
3787 else {
3788 /* mark this ruleset as unused for now */
3789 curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_TCP_RS;
3790 }
3791
3792 return ret;
3793
3794 error:
3795 free_tcpcheck(chk, 0);
3796 free_tcpcheck_ruleset(rs);
3797 return -1;
3798}
3799
3800static struct cfg_kw_list cfg_kws = {ILH, {
3801 { CFG_LISTEN, "tcp-check", proxy_parse_tcpcheck },
3802 { 0, NULL, NULL },
3803}};
3804
3805REGISTER_POST_PROXY_CHECK(check_proxy_tcpcheck);
3806REGISTER_PROXY_DEINIT(deinit_proxy_tcpcheck);
3807REGISTER_POST_DEINIT(deinit_tcpchecks);
3808INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);