Coverage Report

Created: 2023-11-19 07:16

/src/haproxy/src/cfgparse.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Configuration parser
3
 *
4
 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version
9
 * 2 of the License, or (at your option) any later version.
10
 *
11
 */
12
13
/* This is to have crypt() and sched_setaffinity() defined on Linux */
14
#define _GNU_SOURCE
15
16
#ifdef USE_LIBCRYPT
17
#ifdef USE_CRYPT_H
18
/* some platforms such as Solaris need this */
19
#include <crypt.h>
20
#endif
21
#endif /* USE_LIBCRYPT */
22
23
#include <dirent.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <netdb.h>
28
#include <ctype.h>
29
#include <pwd.h>
30
#include <grp.h>
31
#include <errno.h>
32
#ifdef USE_CPU_AFFINITY
33
#include <sched.h>
34
#endif
35
#include <sys/types.h>
36
#include <sys/stat.h>
37
#include <unistd.h>
38
39
#include <haproxy/acl.h>
40
#include <haproxy/action.h>
41
#include <haproxy/api.h>
42
#include <haproxy/arg.h>
43
#include <haproxy/auth.h>
44
#include <haproxy/backend.h>
45
#include <haproxy/capture.h>
46
#include <haproxy/cfgcond.h>
47
#include <haproxy/cfgparse.h>
48
#include <haproxy/channel.h>
49
#include <haproxy/check.h>
50
#include <haproxy/chunk.h>
51
#include <haproxy/clock.h>
52
#ifdef USE_CPU_AFFINITY
53
#include <haproxy/cpuset.h>
54
#endif
55
#include <haproxy/connection.h>
56
#include <haproxy/errors.h>
57
#include <haproxy/filters.h>
58
#include <haproxy/frontend.h>
59
#include <haproxy/global.h>
60
#include <haproxy/http_ana.h>
61
#include <haproxy/http_rules.h>
62
#include <haproxy/lb_chash.h>
63
#include <haproxy/lb_fas.h>
64
#include <haproxy/lb_fwlc.h>
65
#include <haproxy/lb_fwrr.h>
66
#include <haproxy/lb_map.h>
67
#include <haproxy/listener.h>
68
#include <haproxy/log.h>
69
#include <haproxy/sink.h>
70
#include <haproxy/mailers.h>
71
#include <haproxy/namespace.h>
72
#include <haproxy/quic_sock.h>
73
#include <haproxy/obj_type-t.h>
74
#include <haproxy/openssl-compat.h>
75
#include <haproxy/peers-t.h>
76
#include <haproxy/peers.h>
77
#include <haproxy/pool.h>
78
#include <haproxy/protocol.h>
79
#include <haproxy/proxy.h>
80
#include <haproxy/resolvers.h>
81
#include <haproxy/sample.h>
82
#include <haproxy/server.h>
83
#include <haproxy/session.h>
84
#include <haproxy/stats-t.h>
85
#include <haproxy/stick_table.h>
86
#include <haproxy/stream.h>
87
#include <haproxy/task.h>
88
#include <haproxy/tcp_rules.h>
89
#include <haproxy/tcpcheck.h>
90
#include <haproxy/thread.h>
91
#include <haproxy/tools.h>
92
#include <haproxy/uri_auth-t.h>
93
94
95
/* Used to chain configuration sections definitions. This list
96
 * stores struct cfg_section
97
 */
98
struct list sections = LIST_HEAD_INIT(sections);
99
100
struct list postparsers = LIST_HEAD_INIT(postparsers);
101
102
extern struct proxy *mworker_proxy;
103
104
/* curproxy is only valid during parsing and will be NULL afterwards. */
105
struct proxy *curproxy = NULL;
106
107
char *cursection = NULL;
108
int cfg_maxpconn = 0;                   /* # of simultaneous connections per proxy (-N) */
109
int cfg_maxconn = 0;      /* # of simultaneous connections, (-n) */
110
char *cfg_scope = NULL;                 /* the current scope during the configuration parsing */
111
int non_global_section_parsed = 0;
112
113
/* how to handle default paths */
114
static enum default_path_mode {
115
  DEFAULT_PATH_CURRENT = 0,  /* "current": paths are relative to CWD (this is the default) */
116
  DEFAULT_PATH_CONFIG,       /* "config": paths are relative to config file */
117
  DEFAULT_PATH_PARENT,       /* "parent": paths are relative to config file's ".." */
118
  DEFAULT_PATH_ORIGIN,       /* "origin": paths are relative to default_path_origin */
119
} default_path_mode;
120
121
static char initial_cwd[PATH_MAX];
122
static char current_cwd[PATH_MAX];
123
124
/* List head of all known configuration keywords */
125
struct cfg_kw_list cfg_keywords = {
126
  .list = LIST_HEAD_INIT(cfg_keywords.list)
127
};
128
129
/*
130
 * converts <str> to a list of listeners which are dynamically allocated.
131
 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
132
 *  - <addr> can be empty or "*" to indicate INADDR_ANY ;
133
 *  - <port> is a numerical port from 1 to 65535 ;
134
 *  - <end> indicates to use the range from <port> to <end> instead (inclusive).
135
 * This can be repeated as many times as necessary, separated by a coma.
136
 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
137
 * not NULL, it must be a valid pointer to either NULL or a freeable area that
138
 * will be replaced with an error message.
139
 */
140
int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
141
0
{
142
0
  struct protocol *proto;
143
0
  char *next, *dupstr;
144
0
  int port, end;
145
146
0
  next = dupstr = strdup(str);
147
148
0
  while (next && *next) {
149
0
    struct sockaddr_storage *ss2;
150
0
    int fd = -1;
151
152
0
    str = next;
153
    /* 1) look for the end of the first address */
154
0
    if ((next = strchr(str, ',')) != NULL) {
155
0
      *next++ = 0;
156
0
    }
157
158
0
    ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, NULL, err,
159
0
                       (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
160
0
                       NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
161
0
                              PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
162
0
    if (!ss2)
163
0
      goto fail;
164
165
0
    if (ss2->ss_family == AF_CUST_REV_SRV) {
166
      /* Check if a previous non reverse HTTP present is
167
       * already defined. If DGRAM or STREAM is set, this
168
       * indicates that we are currently parsing the second
169
       * or more address.
170
       */
171
0
      if (bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM) &&
172
0
          !(bind_conf->options & BC_O_REVERSE_HTTP)) {
173
0
        memprintf(err, "Cannot mix reverse HTTP bind with others.\n");
174
0
        goto fail;
175
0
      }
176
177
0
      bind_conf->reverse_srvname = strdup(str + strlen("rhttp@"));
178
0
      if (!bind_conf->reverse_srvname) {
179
0
        memprintf(err, "Cannot allocate reverse HTTP bind.\n");
180
0
        goto fail;
181
0
      }
182
183
0
      bind_conf->options |= BC_O_REVERSE_HTTP;
184
0
    }
185
0
    else if (bind_conf->options & BC_O_REVERSE_HTTP) {
186
      /* Standard address mixed with a previous reverse HTTP one. */
187
0
      memprintf(err, "Cannot mix reverse HTTP bind with others.\n");
188
0
      goto fail;
189
0
    }
190
191
    /* OK the address looks correct */
192
0
    if (proto->proto_type == PROTO_TYPE_DGRAM)
193
0
      bind_conf->options |= BC_O_USE_SOCK_DGRAM;
194
0
    else
195
0
      bind_conf->options |= BC_O_USE_SOCK_STREAM;
196
197
0
    if (proto->xprt_type == PROTO_TYPE_DGRAM)
198
0
      bind_conf->options |= BC_O_USE_XPRT_DGRAM;
199
0
    else
200
0
      bind_conf->options |= BC_O_USE_XPRT_STREAM;
201
202
0
    if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
203
0
      memprintf(err, "%s for address '%s'.\n", *err, str);
204
0
      goto fail;
205
0
    }
206
0
  } /* end while(next) */
207
0
  free(dupstr);
208
0
  return 1;
209
0
 fail:
210
0
  free(dupstr);
211
0
  return 0;
212
0
}
213
214
/*
215
 * converts <str> to a list of datagram-oriented listeners which are dynamically
216
 * allocated.
217
 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
218
 *  - <addr> can be empty or "*" to indicate INADDR_ANY ;
219
 *  - <port> is a numerical port from 1 to 65535 ;
220
 *  - <end> indicates to use the range from <port> to <end> instead (inclusive).
221
 * This can be repeated as many times as necessary, separated by a coma.
222
 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
223
 * not NULL, it must be a valid pointer to either NULL or a freeable area that
224
 * will be replaced with an error message.
225
 */
226
int str2receiver(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
227
0
{
228
0
  struct protocol *proto;
229
0
  char *next, *dupstr;
230
0
  int port, end;
231
232
0
  next = dupstr = strdup(str);
233
234
0
  while (next && *next) {
235
0
    struct sockaddr_storage *ss2;
236
0
    int fd = -1;
237
238
0
    str = next;
239
    /* 1) look for the end of the first address */
240
0
    if ((next = strchr(str, ',')) != NULL) {
241
0
      *next++ = 0;
242
0
    }
243
244
0
    ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, NULL, err,
245
0
                       curproxy == global.cli_fe ? NULL : global.unix_bind.prefix,
246
0
                       NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
247
0
                              PA_O_SOCKET_FD | PA_O_DGRAM | PA_O_XPRT);
248
0
    if (!ss2)
249
0
      goto fail;
250
251
    /* OK the address looks correct */
252
0
    if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
253
0
      memprintf(err, "%s for address '%s'.\n", *err, str);
254
0
      goto fail;
255
0
    }
256
0
  } /* end while(next) */
257
0
  free(dupstr);
258
0
  return 1;
259
0
 fail:
260
0
  free(dupstr);
261
0
  return 0;
262
0
}
263
264
/*
265
 * Sends a warning if proxy <proxy> does not have at least one of the
266
 * capabilities in <cap>. An optional <hint> may be added at the end
267
 * of the warning to help the user. Returns 1 if a warning was emitted
268
 * or 0 if the condition is valid.
269
 */
270
int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, const char *arg, const char *hint)
271
0
{
272
0
  char *msg;
273
274
0
  switch (cap) {
275
0
  case PR_CAP_BE: msg = "no backend"; break;
276
0
  case PR_CAP_FE: msg = "no frontend"; break;
277
0
  case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
278
0
  default: msg = "not enough"; break;
279
0
  }
280
281
0
  if (!(proxy->cap & cap)) {
282
0
    ha_warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
283
0
         file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
284
0
    return 1;
285
0
  }
286
0
  return 0;
287
0
}
288
289
/*
290
 * Sends an alert if proxy <proxy> does not have at least one of the
291
 * capabilities in <cap>. An optional <hint> may be added at the end
292
 * of the alert to help the user. Returns 1 if an alert was emitted
293
 * or 0 if the condition is valid.
294
 */
295
int failifnotcap(struct proxy *proxy, int cap, const char *file, int line, const char *arg, const char *hint)
296
0
{
297
0
  char *msg;
298
299
0
  switch (cap) {
300
0
  case PR_CAP_BE: msg = "no backend"; break;
301
0
  case PR_CAP_FE: msg = "no frontend"; break;
302
0
  case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
303
0
  default: msg = "not enough"; break;
304
0
  }
305
306
0
  if (!(proxy->cap & cap)) {
307
0
    ha_alert("parsing [%s:%d] : '%s' not allowed because %s '%s' has %s capability.%s\n",
308
0
       file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
309
0
    return 1;
310
0
  }
311
0
  return 0;
312
0
}
313
314
/*
315
 * Report an error in <msg> when there are too many arguments. This version is
316
 * intended to be used by keyword parsers so that the message will be included
317
 * into the general error message. The index is the current keyword in args.
318
 * Return 0 if the number of argument is correct, otherwise build a message and
319
 * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
320
 * message may also be null, it will simply not be produced (useful to check only).
321
 * <msg> and <err_code> are only affected on error.
322
 */
323
int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
324
0
{
325
0
  int i;
326
327
0
  if (!*args[index + maxarg + 1])
328
0
    return 0;
329
330
0
  if (msg) {
331
0
    *msg = NULL;
332
0
    memprintf(msg, "%s", args[0]);
333
0
    for (i = 1; i <= index; i++)
334
0
      memprintf(msg, "%s %s", *msg, args[i]);
335
336
0
    memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
337
0
  }
338
0
  if (err_code)
339
0
    *err_code |= ERR_ALERT | ERR_FATAL;
340
341
0
  return 1;
342
0
}
343
344
/*
345
 * same as too_many_args_idx with a 0 index
346
 */
347
int too_many_args(int maxarg, char **args, char **msg, int *err_code)
348
0
{
349
0
  return too_many_args_idx(maxarg, 0, args, msg, err_code);
350
0
}
351
352
/*
353
 * Report a fatal Alert when there is too much arguments
354
 * The index is the current keyword in args
355
 * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
356
 * Fill err_code with an ERR_ALERT and an ERR_FATAL
357
 */
358
int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
359
0
{
360
0
  char *kw = NULL;
361
0
  int i;
362
363
0
  if (!*args[index + maxarg + 1])
364
0
    return 0;
365
366
0
  memprintf(&kw, "%s", args[0]);
367
0
  for (i = 1; i <= index; i++) {
368
0
    memprintf(&kw, "%s %s", kw, args[i]);
369
0
  }
370
371
0
  ha_alert("parsing [%s:%d] : '%s' cannot handle unexpected argument '%s'.\n", file, linenum, kw, args[index + maxarg + 1]);
372
0
  free(kw);
373
0
  *err_code |= ERR_ALERT | ERR_FATAL;
374
0
  return 1;
375
0
}
376
377
/*
378
 * same as alertif_too_many_args_idx with a 0 index
379
 */
380
int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
381
0
{
382
0
  return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
383
0
}
384
385
386
/* Report it if a request ACL condition uses some keywords that are incompatible
387
 * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
388
 * its result can be or'ed with err_code. Note that <cond> may be NULL and then
389
 * will be ignored.
390
 */
391
int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
392
0
{
393
0
  const struct acl *acl;
394
0
  const char *kw;
395
396
0
  if (!cond)
397
0
    return 0;
398
399
0
  acl = acl_cond_conflicts(cond, where);
400
0
  if (acl) {
401
0
    if (acl->name && *acl->name)
402
0
      ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
403
0
           file, line, acl->name, sample_ckp_names(where));
404
0
    else
405
0
      ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
406
0
           file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
407
0
    return ERR_WARN;
408
0
  }
409
0
  if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
410
0
    return 0;
411
412
0
  if (acl->name && *acl->name)
413
0
    ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
414
0
         file, line, acl->name, kw, sample_ckp_names(where));
415
0
  else
416
0
    ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
417
0
         file, line, kw, sample_ckp_names(where));
418
0
  return ERR_WARN;
419
0
}
420
421
/* Report it if an ACL uses a L6 sample fetch from an HTTP proxy.  It returns
422
 * either 0 or ERR_WARN so that its result can be or'ed with err_code. Note that
423
 * <cond> may be NULL and then will be ignored.
424
*/
425
int warnif_tcp_http_cond(const struct proxy *px, const struct acl_cond *cond)
426
0
{
427
0
  if (!cond || px->mode != PR_MODE_HTTP)
428
0
    return 0;
429
430
0
  if (cond->use & (SMP_USE_L6REQ|SMP_USE_L6RES)) {
431
0
    ha_warning("Proxy '%s': L6 sample fetches ignored on HTTP proxies (declared at %s:%d).\n",
432
0
         px->id, cond->file, cond->line);
433
0
    return ERR_WARN;
434
0
  }
435
0
  return 0;
436
0
}
437
438
/* try to find in <list> the word that looks closest to <word> by counting
439
 * transitions between letters, digits and other characters. Will return the
440
 * best matching word if found, otherwise NULL. An optional array of extra
441
 * words to compare may be passed in <extra>, but it must then be terminated
442
 * by a NULL entry. If unused it may be NULL.
443
 */
444
const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra)
445
0
{
446
0
  uint8_t word_sig[1024]; // 0..25=letter, 26=digit, 27=other, 28=begin, 29=end
447
0
  uint8_t list_sig[1024];
448
0
  const struct cfg_kw_list *kwl;
449
0
  int index;
450
0
  const char *best_ptr = NULL;
451
0
  int dist, best_dist = INT_MAX;
452
453
0
  make_word_fingerprint(word_sig, word);
454
0
  list_for_each_entry(kwl, list, list) {
455
0
    for (index = 0; kwl->kw[index].kw != NULL; index++) {
456
0
      if (kwl->kw[index].section != section)
457
0
        continue;
458
459
0
      make_word_fingerprint(list_sig, kwl->kw[index].kw);
460
0
      dist = word_fingerprint_distance(word_sig, list_sig);
461
0
      if (dist < best_dist) {
462
0
        best_dist = dist;
463
0
        best_ptr = kwl->kw[index].kw;
464
0
      }
465
0
    }
466
0
  }
467
468
0
  while (extra && *extra) {
469
0
    make_word_fingerprint(list_sig, *extra);
470
0
    dist = word_fingerprint_distance(word_sig, list_sig);
471
0
    if (dist < best_dist) {
472
0
      best_dist = dist;
473
0
      best_ptr = *extra;
474
0
    }
475
0
    extra++;
476
0
  }
477
478
0
  if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
479
0
    best_ptr = NULL;
480
0
  return best_ptr;
481
0
}
482
483
/* Parse a string representing a process number or a set of processes. It must
484
 * be "all", "odd", "even", a number between 1 and <max> or a range with
485
 * two such numbers delimited by a dash ('-'). On success, it returns
486
 * 0. otherwise it returns 1 with an error message in <err>.
487
 *
488
 * Note: this function can also be used to parse a thread number or a set of
489
 * threads.
490
 */
491
int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
492
0
{
493
0
  if (autoinc) {
494
0
    *autoinc = 0;
495
0
    if (strncmp(arg, "auto:", 5) == 0) {
496
0
      arg += 5;
497
0
      *autoinc = 1;
498
0
    }
499
0
  }
500
501
0
  if (strcmp(arg, "all") == 0)
502
0
    *proc |= ~0UL;
503
0
  else if (strcmp(arg, "odd") == 0)
504
0
    *proc |= ~0UL/3UL; /* 0x555....555 */
505
0
  else if (strcmp(arg, "even") == 0)
506
0
    *proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
507
0
  else {
508
0
    const char *p, *dash = NULL;
509
0
    unsigned int low, high;
510
511
0
    for (p = arg; *p; p++) {
512
0
      if (*p == '-' && !dash)
513
0
        dash = p;
514
0
      else if (!isdigit((unsigned char)*p)) {
515
0
        memprintf(err, "'%s' is not a valid number/range.", arg);
516
0
        return -1;
517
0
      }
518
0
    }
519
520
0
    low = high = str2uic(arg);
521
0
    if (dash)
522
0
      high = ((!*(dash+1)) ? max : str2uic(dash + 1));
523
524
0
    if (high < low) {
525
0
      unsigned int swap = low;
526
0
      low  = high;
527
0
      high = swap;
528
0
    }
529
530
0
    if (low < 1 || low > max || high > max) {
531
0
      memprintf(err, "'%s' is not a valid number/range."
532
0
          " It supports numbers from 1 to %d.\n",
533
0
          arg, max);
534
0
      return 1;
535
0
    }
536
537
0
    for (;low <= high; low++)
538
0
      *proc |= 1UL << (low-1);
539
0
  }
540
0
  *proc &= ~0UL >> (LONGBITS - max);
541
542
0
  return 0;
543
0
}
544
545
/* Allocate and initialize the frontend of a "peers" section found in
546
 * file <file> at line <linenum> with <id> as ID.
547
 * Return 0 if succeeded, -1 if not.
548
 * Note that this function may be called from "default-server"
549
 * or "peer" lines.
550
 */
551
static int init_peers_frontend(const char *file, int linenum,
552
                               const char *id, struct peers *peers)
553
0
{
554
0
  struct proxy *p;
555
556
0
  if (peers->peers_fe) {
557
0
    p = peers->peers_fe;
558
0
    goto out;
559
0
  }
560
561
0
  p = calloc(1, sizeof *p);
562
0
  if (!p) {
563
0
    ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
564
0
    return -1;
565
0
  }
566
567
0
  init_new_proxy(p);
568
0
  peers_setup_frontend(p);
569
0
  p->parent = peers;
570
  /* Finally store this frontend. */
571
0
  peers->peers_fe = p;
572
573
0
 out:
574
0
  if (id && !p->id)
575
0
    p->id = strdup(id);
576
0
  free(p->conf.file);
577
0
  p->conf.args.file = p->conf.file = strdup(file);
578
0
  if (linenum != -1)
579
0
    p->conf.args.line = p->conf.line = linenum;
580
581
0
  return 0;
582
0
}
583
584
/* Only change ->file, ->line and ->arg struct bind_conf member values
585
 * if already present.
586
 */
587
static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
588
                                              const char *file, int line,
589
                                              const char *arg, struct xprt_ops *xprt)
590
0
{
591
0
  struct bind_conf *bind_conf;
592
593
0
  if (!LIST_ISEMPTY(&p->conf.bind)) {
594
0
    bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
595
    /*
596
     * We keep bind_conf->file and bind_conf->line unchanged
597
     * to make them available for error messages
598
     */
599
0
    if (arg) {
600
0
      free(bind_conf->arg);
601
0
      bind_conf->arg = strdup(arg);
602
0
    }
603
0
  }
604
0
  else {
605
0
    bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
606
0
  }
607
608
0
  return bind_conf;
609
0
}
610
611
/*
612
 * Allocate a new struct peer parsed at line <linenum> in file <file>
613
 * to be added to <peers>.
614
 * Returns the new allocated structure if succeeded, NULL if not.
615
 */
616
static struct peer *cfg_peers_add_peer(struct peers *peers,
617
                                       const char *file, int linenum,
618
                                       const char *id, int local)
619
0
{
620
0
  struct peer *p;
621
622
0
  p = calloc(1, sizeof *p);
623
0
  if (!p) {
624
0
    ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
625
0
    return NULL;
626
0
  }
627
628
  /* the peers are linked backwards first */
629
0
  peers->count++;
630
0
  p->peers = peers;
631
0
  p->next = peers->remote;
632
0
  peers->remote = p;
633
0
  p->conf.file = strdup(file);
634
0
  p->conf.line = linenum;
635
0
  p->last_change = ns_to_sec(now_ns);
636
0
  p->xprt  = xprt_get(XPRT_RAW);
637
0
  p->sock_init_arg = NULL;
638
0
  HA_SPIN_INIT(&p->lock);
639
0
  if (id)
640
0
    p->id = strdup(id);
641
0
  if (local) {
642
0
    p->local = 1;
643
0
    peers->local = p;
644
0
  }
645
646
0
  return p;
647
0
}
648
649
/*
650
 * Parse a line in a <listen>, <frontend> or <backend> section.
651
 * Returns the error code, 0 if OK, or any combination of :
652
 *  - ERR_ABORT: must abort ASAP
653
 *  - ERR_FATAL: we can continue parsing but not start the service
654
 *  - ERR_WARN: a warning has been emitted
655
 *  - ERR_ALERT: an alert has been emitted
656
 * Only the two first ones can stop processing, the two others are just
657
 * indicators.
658
 */
659
int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
660
0
{
661
0
  static struct peers *curpeers = NULL;
662
0
  static int nb_shards = 0;
663
0
  struct peer *newpeer = NULL;
664
0
  const char *err;
665
0
  struct bind_conf *bind_conf;
666
0
  int err_code = 0;
667
0
  char *errmsg = NULL;
668
0
  static int bind_line, peer_line;
669
670
0
  if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
671
0
    int cur_arg;
672
0
    struct bind_conf *bind_conf;
673
0
    int ret;
674
675
0
    cur_arg = 1;
676
677
0
    if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
678
0
      err_code |= ERR_ALERT | ERR_ABORT;
679
0
      goto out;
680
0
    }
681
682
0
    bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
683
0
                                     args[1], xprt_get(XPRT_RAW));
684
0
    if (!bind_conf) {
685
0
      ha_alert("parsing [%s:%d] : '%s %s' : cannot allocate memory.\n", file, linenum, args[0], args[1]);
686
0
      err_code |= ERR_FATAL;
687
0
      goto out;
688
0
    }
689
690
0
    bind_conf->maxaccept = 1;
691
0
    bind_conf->accept = session_accept_fd;
692
0
    bind_conf->options |= BC_O_UNLIMITED; /* don't make the peers subject to global limits */
693
694
0
    if (*args[0] == 'b') {
695
0
      struct listener *l;
696
697
0
      if (peer_line) {
698
0
        ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
699
0
        err_code |= ERR_ALERT | ERR_FATAL;
700
0
        goto out;
701
0
      }
702
703
0
      if (!LIST_ISEMPTY(&bind_conf->listeners)) {
704
0
        ha_alert("parsing [%s:%d] : One listener per \"peers\" section is authorized but another is already configured at [%s:%d].\n", file, linenum, bind_conf->file, bind_conf->line);
705
0
        err_code |= ERR_FATAL;
706
0
      }
707
708
0
      if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
709
0
        if (errmsg && *errmsg) {
710
0
          indent_msg(&errmsg, 2);
711
0
          ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
712
0
        }
713
0
        else
714
0
          ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
715
0
               file, linenum, args[0], args[1], args[1]);
716
0
        err_code |= ERR_FATAL;
717
0
        goto out;
718
0
      }
719
720
      /* Only one listener supported. Compare first listener
721
       * against the last one. It must be the same one.
722
       */
723
0
      if (bind_conf->listeners.n != bind_conf->listeners.p) {
724
0
        ha_alert("parsing [%s:%d] : Only one listener per \"peers\" section is authorized. Multiple listening addresses or port range are not supported.\n", file, linenum);
725
0
        err_code |= ERR_ALERT | ERR_FATAL;
726
0
        goto out;
727
0
      }
728
      /*
729
       * Newly allocated listener is at the end of the list
730
       */
731
0
      l = LIST_ELEM(bind_conf->listeners.p, typeof(l), by_bind);
732
733
0
      global.maxsock++; /* for the listening socket */
734
735
0
      bind_line = 1;
736
0
      if (cfg_peers->local) {
737
0
        newpeer = cfg_peers->local;
738
0
      }
739
0
      else {
740
        /* This peer is local.
741
         * Note that we do not set the peer ID. This latter is initialized
742
         * when parsing "peer" or "server" line.
743
         */
744
0
        newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
745
0
        if (!newpeer) {
746
0
          err_code |= ERR_ALERT | ERR_ABORT;
747
0
          goto out;
748
0
        }
749
0
      }
750
0
      newpeer->addr = l->rx.addr;
751
0
      newpeer->proto = l->rx.proto;
752
0
      cur_arg++;
753
0
    }
754
755
0
    ret = bind_parse_args_list(bind_conf, args, cur_arg, cursection, file, linenum);
756
0
    err_code |= ret;
757
0
    if (ret != 0)
758
0
      goto out;
759
0
  }
760
0
  else if (strcmp(args[0], "default-server") == 0) {
761
0
    if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
762
0
      err_code |= ERR_ALERT | ERR_ABORT;
763
0
      goto out;
764
0
    }
765
0
    err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
766
0
                             SRV_PARSE_DEFAULT_SERVER|SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_INITIAL_RESOLVE);
767
0
  }
768
0
  else if (strcmp(args[0], "log") == 0) {
769
0
    if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
770
0
      err_code |= ERR_ALERT | ERR_ABORT;
771
0
      goto out;
772
0
    }
773
0
    if (!parse_logger(args, &curpeers->peers_fe->loggers, (kwm == KWM_NO), file, linenum, &errmsg)) {
774
0
      ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
775
0
      err_code |= ERR_ALERT | ERR_FATAL;
776
0
      goto out;
777
0
    }
778
0
  }
779
0
  else if (strcmp(args[0], "peers") == 0) { /* new peers section */
780
    /* Initialize these static variables when entering a new "peers" section*/
781
0
    bind_line = peer_line = 0;
782
0
    if (!*args[1]) {
783
0
      ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
784
0
      err_code |= ERR_ALERT | ERR_ABORT;
785
0
      goto out;
786
0
    }
787
788
0
    if (alertif_too_many_args(1, file, linenum, args, &err_code))
789
0
      goto out;
790
791
0
    err = invalid_char(args[1]);
792
0
    if (err) {
793
0
      ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
794
0
         file, linenum, *err, args[0], args[1]);
795
0
      err_code |= ERR_ALERT | ERR_ABORT;
796
0
      goto out;
797
0
    }
798
799
0
    for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
800
      /*
801
       * If there are two proxies with the same name only following
802
       * combinations are allowed:
803
       */
804
0
      if (strcmp(curpeers->id, args[1]) == 0) {
805
0
        ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
806
0
           file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
807
0
        err_code |= ERR_ALERT | ERR_FATAL;
808
0
      }
809
0
    }
810
811
0
    if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
812
0
      ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
813
0
      err_code |= ERR_ALERT | ERR_ABORT;
814
0
      goto out;
815
0
    }
816
817
0
    curpeers->next = cfg_peers;
818
0
    cfg_peers = curpeers;
819
0
    curpeers->conf.file = strdup(file);
820
0
    curpeers->conf.line = linenum;
821
0
    curpeers->last_change = ns_to_sec(now_ns);
822
0
    curpeers->id = strdup(args[1]);
823
0
    curpeers->disabled = 0;
824
0
  }
825
0
  else if (strcmp(args[0], "peer") == 0 ||
826
0
           strcmp(args[0], "server") == 0) { /* peer or server definition */
827
0
    int local_peer, peer;
828
0
    int parse_addr = 0;
829
830
0
    peer = *args[0] == 'p';
831
0
    local_peer = strcmp(args[1], localpeer) == 0;
832
    /* The local peer may have already partially been parsed on a "bind" line. */
833
0
    if (*args[0] == 'p') {
834
0
      if (bind_line) {
835
0
        ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
836
0
        err_code |= ERR_ALERT | ERR_FATAL;
837
0
        goto out;
838
0
      }
839
0
      peer_line = 1;
840
0
    }
841
0
    if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
842
      /* The local peer has already been initialized on a "bind" line.
843
       * Let's use it and store its ID.
844
       */
845
0
      newpeer = cfg_peers->local;
846
0
      newpeer->id = strdup(localpeer);
847
0
    }
848
0
    else {
849
0
      if (local_peer && cfg_peers->local) {
850
0
        ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
851
0
                 file, linenum, args[0], args[1],
852
0
         curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
853
0
        err_code |= ERR_FATAL;
854
0
        goto out;
855
0
      }
856
0
      newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
857
0
      if (!newpeer) {
858
0
        err_code |= ERR_ALERT | ERR_ABORT;
859
0
        goto out;
860
0
      }
861
0
    }
862
863
    /* Line number and peer ID are updated only if this peer is the local one. */
864
0
    if (init_peers_frontend(file,
865
0
                            newpeer->local ? linenum: -1,
866
0
                            newpeer->local ? newpeer->id : NULL,
867
0
                            curpeers) != 0) {
868
0
      err_code |= ERR_ALERT | ERR_ABORT;
869
0
      goto out;
870
0
    }
871
872
    /* This initializes curpeer->peers->peers_fe->srv.
873
     * The server address is parsed only if we are parsing a "peer" line,
874
     * or if we are parsing a "server" line and the current peer is not the local one.
875
     */
876
0
    parse_addr = (peer || !local_peer) ? SRV_PARSE_PARSE_ADDR : 0;
877
0
    err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
878
0
                             SRV_PARSE_IN_PEER_SECTION|parse_addr|SRV_PARSE_INITIAL_RESOLVE);
879
0
    if (!curpeers->peers_fe->srv) {
880
      /* Remove the newly allocated peer. */
881
0
      if (newpeer != curpeers->local) {
882
0
        struct peer *p;
883
884
0
        p = curpeers->remote;
885
0
        curpeers->remote = curpeers->remote->next;
886
0
        free(p->id);
887
0
        free(p);
888
0
      }
889
0
      goto out;
890
0
    }
891
892
0
    if (nb_shards && curpeers->peers_fe->srv->shard > nb_shards) {
893
0
      ha_warning("parsing [%s:%d] : '%s %s' : %d peer shard greater value than %d shards value is ignored.\n",
894
0
                 file, linenum, args[0], args[1], curpeers->peers_fe->srv->shard, nb_shards);
895
0
      curpeers->peers_fe->srv->shard = 0;
896
0
      err_code |= ERR_WARN;
897
0
    }
898
899
0
    if (curpeers->peers_fe->srv->init_addr_methods || curpeers->peers_fe->srv->resolvers_id ||
900
0
        curpeers->peers_fe->srv->do_check || curpeers->peers_fe->srv->do_agent) {
901
0
      ha_warning("parsing [%s:%d] : '%s %s' : init_addr, resolvers, check and agent are ignored for peers.\n", file, linenum, args[0], args[1]);
902
0
      err_code |= ERR_WARN;
903
0
    }
904
905
    /* If the peer address has just been parsed, let's copy it to <newpeer>
906
     * and initializes ->proto.
907
     */
908
0
    if (peer || !local_peer) {
909
0
      newpeer->addr = curpeers->peers_fe->srv->addr;
910
0
      newpeer->proto = protocol_lookup(newpeer->addr.ss_family, PROTO_TYPE_STREAM, 0);
911
0
    }
912
913
0
    newpeer->xprt  = xprt_get(XPRT_RAW);
914
0
    newpeer->sock_init_arg = NULL;
915
0
    HA_SPIN_INIT(&newpeer->lock);
916
917
0
    newpeer->srv = curpeers->peers_fe->srv;
918
0
    if (!newpeer->local)
919
0
      goto out;
920
921
    /* The lines above are reserved to "peer" lines. */
922
0
    if (*args[0] == 's')
923
0
      goto out;
924
925
0
    bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum, args[2], xprt_get(XPRT_RAW));
926
0
    if (!bind_conf) {
927
0
      ha_alert("parsing [%s:%d] : '%s %s' : Cannot allocate memory.\n", file, linenum, args[0], args[1]);
928
0
      err_code |= ERR_FATAL;
929
0
      goto out;
930
0
    }
931
932
0
    bind_conf->maxaccept = 1;
933
0
    bind_conf->accept = session_accept_fd;
934
0
    bind_conf->options |= BC_O_UNLIMITED; /* don't make the peers subject to global limits */
935
936
0
    if (!LIST_ISEMPTY(&bind_conf->listeners)) {
937
0
      ha_alert("parsing [%s:%d] : One listener per \"peers\" section is authorized but another is already configured at [%s:%d].\n", file, linenum, bind_conf->file, bind_conf->line);
938
0
      err_code |= ERR_FATAL;
939
0
    }
940
941
0
    if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
942
0
      if (errmsg && *errmsg) {
943
0
        indent_msg(&errmsg, 2);
944
0
        ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
945
0
      }
946
0
      else
947
0
        ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
948
0
                 file, linenum, args[0], args[1], args[2]);
949
0
      err_code |= ERR_FATAL;
950
0
      goto out;
951
0
    }
952
953
0
    global.maxsock++; /* for the listening socket */
954
0
  }
955
0
  else if (strcmp(args[0], "shards") == 0) {
956
0
    char *endptr;
957
958
0
    if (!*args[1]) {
959
0
      ha_alert("parsing [%s:%d] : '%s' : missing value\n", file, linenum, args[0]);
960
0
      err_code |= ERR_FATAL;
961
0
      goto out;
962
0
    }
963
964
0
    curpeers->nb_shards = strtol(args[1], &endptr, 10);
965
0
    if (*endptr != '\0') {
966
0
      ha_alert("parsing [%s:%d] : '%s' : expects an integer argument, found '%s'\n",
967
0
               file, linenum, args[0], args[1]);
968
0
      err_code |= ERR_FATAL;
969
0
      goto out;
970
0
    }
971
972
0
    if (!curpeers->nb_shards) {
973
0
      ha_alert("parsing [%s:%d] : '%s' : expects a strictly positive integer argument\n",
974
0
               file, linenum, args[0]);
975
0
      err_code |= ERR_FATAL;
976
0
      goto out;
977
0
    }
978
979
0
    nb_shards = curpeers->nb_shards;
980
0
  }
981
0
  else if (strcmp(args[0], "table") == 0) {
982
0
    struct stktable *t, *other;
983
0
    char *id;
984
0
    size_t prefix_len;
985
986
    /* Line number and peer ID are updated only if this peer is the local one. */
987
0
    if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
988
0
      err_code |= ERR_ALERT | ERR_ABORT;
989
0
      goto out;
990
0
    }
991
992
    /* Build the stick-table name, concatenating the "peers" section name
993
     * followed by a '/' character and the table name argument.
994
     */
995
0
    chunk_reset(&trash);
996
0
    if (!chunk_strcpy(&trash, curpeers->id)) {
997
0
      ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
998
0
               file, linenum, args[0], args[1]);
999
0
      err_code |= ERR_ALERT | ERR_FATAL;
1000
0
      goto out;
1001
0
    }
1002
1003
0
    prefix_len = trash.data;
1004
0
    if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
1005
0
      ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
1006
0
               file, linenum, args[0], args[1]);
1007
0
      err_code |= ERR_ALERT | ERR_FATAL;
1008
0
      goto out;
1009
0
    }
1010
1011
0
    t = calloc(1, sizeof *t);
1012
0
    id = strdup(trash.area);
1013
0
    if (!t || !id) {
1014
0
      ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
1015
0
               file, linenum, args[0], args[1]);
1016
0
      free(t);
1017
0
      free(id);
1018
0
      err_code |= ERR_ALERT | ERR_FATAL;
1019
0
      goto out;
1020
0
    }
1021
1022
0
    other = stktable_find_by_name(trash.area);
1023
0
    if (other) {
1024
0
      ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
1025
0
               file, linenum, args[1],
1026
0
               other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
1027
0
               other->proxy ? other->id : other->peers.p->id,
1028
0
               other->conf.file, other->conf.line);
1029
0
      err_code |= ERR_ALERT | ERR_FATAL;
1030
0
      goto out;
1031
0
    }
1032
1033
1034
0
    err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
1035
0
    if (err_code & ERR_FATAL) {
1036
0
      free(t);
1037
0
      free(id);
1038
0
      goto out;
1039
0
    }
1040
1041
0
    stktable_store_name(t);
1042
0
    t->next = stktables_list;
1043
0
    stktables_list = t;
1044
0
  }
1045
0
  else if (strcmp(args[0], "disabled") == 0) {  /* disables this peers section */
1046
0
    curpeers->disabled |= PR_FL_DISABLED;
1047
0
  }
1048
0
  else if (strcmp(args[0], "enabled") == 0) {  /* enables this peers section (used to revert a disabled default) */
1049
0
    curpeers->disabled = 0;
1050
0
  }
1051
0
  else if (*args[0] != 0) {
1052
0
    struct peers_kw_list *pkwl;
1053
0
    int index;
1054
0
    int rc = -1;
1055
1056
0
    list_for_each_entry(pkwl, &peers_keywords.list, list) {
1057
0
      for (index = 0; pkwl->kw[index].kw != NULL; index++) {
1058
0
        if (strcmp(pkwl->kw[index].kw, args[0]) == 0) {
1059
0
          rc = pkwl->kw[index].parse(args, curpeers, file, linenum, &errmsg);
1060
0
          if (rc < 0) {
1061
0
            ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1062
0
            err_code |= ERR_ALERT | ERR_FATAL;
1063
0
            goto out;
1064
0
          }
1065
0
          else if (rc > 0) {
1066
0
            ha_warning("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1067
0
            err_code |= ERR_WARN;
1068
0
            goto out;
1069
0
          }
1070
0
          goto out;
1071
0
        }
1072
0
      }
1073
0
    }
1074
1075
0
    ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1076
0
    err_code |= ERR_ALERT | ERR_FATAL;
1077
0
    goto out;
1078
0
  }
1079
1080
0
out:
1081
0
  free(errmsg);
1082
0
  return err_code;
1083
0
}
1084
1085
/*
1086
 * Parse a line in a <listen>, <frontend> or <backend> section.
1087
 * Returns the error code, 0 if OK, or any combination of :
1088
 *  - ERR_ABORT: must abort ASAP
1089
 *  - ERR_FATAL: we can continue parsing but not start the service
1090
 *  - ERR_WARN: a warning has been emitted
1091
 *  - ERR_ALERT: an alert has been emitted
1092
 * Only the two first ones can stop processing, the two others are just
1093
 * indicators.
1094
 */
1095
int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1096
0
{
1097
0
  static struct mailers *curmailers = NULL;
1098
0
  struct mailer *newmailer = NULL;
1099
0
  const char *err;
1100
0
  int err_code = 0;
1101
0
  char *errmsg = NULL;
1102
1103
0
  if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1104
0
    if (!*args[1]) {
1105
0
      ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
1106
0
      err_code |= ERR_ALERT | ERR_ABORT;
1107
0
      goto out;
1108
0
    }
1109
1110
0
    err = invalid_char(args[1]);
1111
0
    if (err) {
1112
0
      ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1113
0
         file, linenum, *err, args[0], args[1]);
1114
0
      err_code |= ERR_ALERT | ERR_ABORT;
1115
0
      goto out;
1116
0
    }
1117
1118
0
    for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1119
      /*
1120
       * If there are two proxies with the same name only following
1121
       * combinations are allowed:
1122
       */
1123
0
      if (strcmp(curmailers->id, args[1]) == 0) {
1124
0
        ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1125
0
           file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
1126
0
        err_code |= ERR_ALERT | ERR_FATAL;
1127
0
      }
1128
0
    }
1129
1130
0
    if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
1131
0
      ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1132
0
      err_code |= ERR_ALERT | ERR_ABORT;
1133
0
      goto out;
1134
0
    }
1135
1136
0
    curmailers->next = mailers;
1137
0
    mailers = curmailers;
1138
0
    curmailers->conf.file = strdup(file);
1139
0
    curmailers->conf.line = linenum;
1140
0
    curmailers->id = strdup(args[1]);
1141
0
    curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1142
      * But need enough time so that timeouts don't occur
1143
      * during tcp procssing. For now just us an arbitrary default. */
1144
0
  }
1145
0
  else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1146
0
    struct sockaddr_storage *sk;
1147
0
    int port1, port2;
1148
0
    struct protocol *proto;
1149
1150
0
    if (!*args[2]) {
1151
0
      ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1152
0
         file, linenum, args[0]);
1153
0
      err_code |= ERR_ALERT | ERR_FATAL;
1154
0
      goto out;
1155
0
    }
1156
1157
0
    err = invalid_char(args[1]);
1158
0
    if (err) {
1159
0
      ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1160
0
         file, linenum, *err, args[1]);
1161
0
      err_code |= ERR_ALERT | ERR_FATAL;
1162
0
      goto out;
1163
0
    }
1164
1165
0
    if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
1166
0
      ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1167
0
      err_code |= ERR_ALERT | ERR_ABORT;
1168
0
      goto out;
1169
0
    }
1170
1171
    /* the mailers are linked backwards first */
1172
0
    curmailers->count++;
1173
0
    newmailer->next = curmailers->mailer_list;
1174
0
    curmailers->mailer_list = newmailer;
1175
0
    newmailer->mailers = curmailers;
1176
0
    newmailer->conf.file = strdup(file);
1177
0
    newmailer->conf.line = linenum;
1178
1179
0
    newmailer->id = strdup(args[1]);
1180
1181
0
    sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto, NULL,
1182
0
                      &errmsg, NULL, NULL,
1183
0
                      PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
1184
0
    if (!sk) {
1185
0
      ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1186
0
      err_code |= ERR_ALERT | ERR_FATAL;
1187
0
      goto out;
1188
0
    }
1189
1190
0
    if (proto->sock_prot != IPPROTO_TCP) {
1191
0
      ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1192
0
         file, linenum, args[0], args[1]);
1193
0
      err_code |= ERR_ALERT | ERR_FATAL;
1194
0
      goto out;
1195
0
    }
1196
1197
0
    newmailer->addr = *sk;
1198
0
    newmailer->proto = proto;
1199
0
    newmailer->xprt  = xprt_get(XPRT_RAW);
1200
0
    newmailer->sock_init_arg = NULL;
1201
0
  }
1202
0
  else if (strcmp(args[0], "timeout") == 0) {
1203
0
    if (!*args[1]) {
1204
0
      ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1205
0
         file, linenum, args[0]);
1206
0
      err_code |= ERR_ALERT | ERR_FATAL;
1207
0
      goto out;
1208
0
    }
1209
0
    else if (strcmp(args[1], "mail") == 0) {
1210
0
      const char *res;
1211
0
      unsigned int timeout_mail;
1212
0
      if (!*args[2]) {
1213
0
        ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1214
0
           file, linenum, args[0], args[1]);
1215
0
        err_code |= ERR_ALERT | ERR_FATAL;
1216
0
        goto out;
1217
0
      }
1218
0
      res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
1219
0
      if (res == PARSE_TIME_OVER) {
1220
0
        ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1221
0
           file, linenum, args[2], args[0], args[1]);
1222
0
        err_code |= ERR_ALERT | ERR_FATAL;
1223
0
        goto out;
1224
0
      }
1225
0
      else if (res == PARSE_TIME_UNDER) {
1226
0
        ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1227
0
           file, linenum, args[2], args[0], args[1]);
1228
0
        err_code |= ERR_ALERT | ERR_FATAL;
1229
0
        goto out;
1230
0
      }
1231
0
      else if (res) {
1232
0
        ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1233
0
           file, linenum, *res, args[0], args[1]);
1234
0
        err_code |= ERR_ALERT | ERR_FATAL;
1235
0
        goto out;
1236
0
      }
1237
0
      curmailers->timeout.mail = timeout_mail;
1238
0
    } else {
1239
0
      ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
1240
0
        file, linenum, args[0], args[1]);
1241
0
      err_code |= ERR_ALERT | ERR_FATAL;
1242
0
      goto out;
1243
0
    }
1244
0
  }
1245
0
  else if (*args[0] != 0) {
1246
0
    ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1247
0
    err_code |= ERR_ALERT | ERR_FATAL;
1248
0
    goto out;
1249
0
  }
1250
1251
0
out:
1252
0
  free(errmsg);
1253
0
  return err_code;
1254
0
}
1255
1256
void free_email_alert(struct proxy *p)
1257
0
{
1258
0
  ha_free(&p->email_alert.mailers.name);
1259
0
  ha_free(&p->email_alert.from);
1260
0
  ha_free(&p->email_alert.to);
1261
0
  ha_free(&p->email_alert.myhostname);
1262
0
}
1263
1264
1265
int
1266
cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1267
0
{
1268
#ifdef USE_NS
1269
  const char *err;
1270
  const char *item = args[0];
1271
1272
  if (strcmp(item, "namespace_list") == 0) {
1273
    return 0;
1274
  }
1275
  else if (strcmp(item, "namespace") == 0) {
1276
    size_t idx = 1;
1277
    const char *current;
1278
    while (*(current = args[idx++])) {
1279
      err = invalid_char(current);
1280
      if (err) {
1281
        ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1282
           file, linenum, *err, item, current);
1283
        return ERR_ALERT | ERR_FATAL;
1284
      }
1285
1286
      if (netns_store_lookup(current, strlen(current))) {
1287
        ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1288
           file, linenum, current);
1289
        return ERR_ALERT | ERR_FATAL;
1290
      }
1291
      if (!netns_store_insert(current)) {
1292
        ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1293
           file, linenum, current);
1294
        return ERR_ALERT | ERR_FATAL;
1295
      }
1296
    }
1297
  }
1298
1299
  return 0;
1300
#else
1301
0
  ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1302
0
     file, linenum);
1303
0
  return ERR_ALERT | ERR_FATAL;
1304
0
#endif
1305
0
}
1306
1307
int
1308
cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1309
0
{
1310
1311
0
  int err_code = 0;
1312
0
  const char *err;
1313
1314
0
  if (strcmp(args[0], "userlist") == 0) {   /* new userlist */
1315
0
    struct userlist *newul;
1316
1317
0
    if (!*args[1]) {
1318
0
      ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1319
0
         file, linenum, args[0]);
1320
0
      err_code |= ERR_ALERT | ERR_FATAL;
1321
0
      goto out;
1322
0
    }
1323
0
    if (alertif_too_many_args(1, file, linenum, args, &err_code))
1324
0
      goto out;
1325
1326
0
    err = invalid_char(args[1]);
1327
0
    if (err) {
1328
0
      ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1329
0
         file, linenum, *err, args[0], args[1]);
1330
0
      err_code |= ERR_ALERT | ERR_FATAL;
1331
0
      goto out;
1332
0
    }
1333
1334
0
    for (newul = userlist; newul; newul = newul->next)
1335
0
      if (strcmp(newul->name, args[1]) == 0) {
1336
0
        ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1337
0
             file, linenum, args[1]);
1338
0
        err_code |= ERR_WARN;
1339
0
        goto out;
1340
0
      }
1341
1342
0
    newul = calloc(1, sizeof(*newul));
1343
0
    if (!newul) {
1344
0
      ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1345
0
      err_code |= ERR_ALERT | ERR_ABORT;
1346
0
      goto out;
1347
0
    }
1348
1349
0
    newul->name = strdup(args[1]);
1350
0
    if (!newul->name) {
1351
0
      ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1352
0
      err_code |= ERR_ALERT | ERR_ABORT;
1353
0
      free(newul);
1354
0
      goto out;
1355
0
    }
1356
1357
0
    newul->next = userlist;
1358
0
    userlist = newul;
1359
1360
0
  } else if (strcmp(args[0], "group") == 0) {   /* new group */
1361
0
    int cur_arg;
1362
0
    const char *err;
1363
0
    struct auth_groups *ag;
1364
1365
0
    if (!*args[1]) {
1366
0
      ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1367
0
         file, linenum, args[0]);
1368
0
      err_code |= ERR_ALERT | ERR_FATAL;
1369
0
      goto out;
1370
0
    }
1371
1372
0
    err = invalid_char(args[1]);
1373
0
    if (err) {
1374
0
      ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1375
0
         file, linenum, *err, args[0], args[1]);
1376
0
      err_code |= ERR_ALERT | ERR_FATAL;
1377
0
      goto out;
1378
0
    }
1379
1380
0
    if (!userlist)
1381
0
      goto out;
1382
1383
0
    for (ag = userlist->groups; ag; ag = ag->next)
1384
0
      if (strcmp(ag->name, args[1]) == 0) {
1385
0
        ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1386
0
             file, linenum, args[1], userlist->name);
1387
0
        err_code |= ERR_ALERT;
1388
0
        goto out;
1389
0
      }
1390
1391
0
    ag = calloc(1, sizeof(*ag));
1392
0
    if (!ag) {
1393
0
      ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1394
0
      err_code |= ERR_ALERT | ERR_ABORT;
1395
0
      goto out;
1396
0
    }
1397
1398
0
    ag->name = strdup(args[1]);
1399
0
    if (!ag->name) {
1400
0
      ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1401
0
      err_code |= ERR_ALERT | ERR_ABORT;
1402
0
      free(ag);
1403
0
      goto out;
1404
0
    }
1405
1406
0
    cur_arg = 2;
1407
1408
0
    while (*args[cur_arg]) {
1409
0
      if (strcmp(args[cur_arg], "users") == 0) {
1410
0
        ag->groupusers = strdup(args[cur_arg + 1]);
1411
0
        cur_arg += 2;
1412
0
        continue;
1413
0
      } else {
1414
0
        ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1415
0
           file, linenum, args[0]);
1416
0
        err_code |= ERR_ALERT | ERR_FATAL;
1417
0
        free(ag->groupusers);
1418
0
        free(ag->name);
1419
0
        free(ag);
1420
0
        goto out;
1421
0
      }
1422
0
    }
1423
1424
0
    ag->next = userlist->groups;
1425
0
    userlist->groups = ag;
1426
1427
0
  } else if (strcmp(args[0], "user") == 0) {   /* new user */
1428
0
    struct auth_users *newuser;
1429
0
    int cur_arg;
1430
1431
0
    if (!*args[1]) {
1432
0
      ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1433
0
         file, linenum, args[0]);
1434
0
      err_code |= ERR_ALERT | ERR_FATAL;
1435
0
      goto out;
1436
0
    }
1437
0
    if (!userlist)
1438
0
      goto out;
1439
1440
0
    for (newuser = userlist->users; newuser; newuser = newuser->next)
1441
0
      if (strcmp(newuser->user, args[1]) == 0) {
1442
0
        ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1443
0
             file, linenum, args[1], userlist->name);
1444
0
        err_code |= ERR_ALERT;
1445
0
        goto out;
1446
0
      }
1447
1448
0
    newuser = calloc(1, sizeof(*newuser));
1449
0
    if (!newuser) {
1450
0
      ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1451
0
      err_code |= ERR_ALERT | ERR_ABORT;
1452
0
      goto out;
1453
0
    }
1454
1455
0
    newuser->user = strdup(args[1]);
1456
1457
0
    newuser->next = userlist->users;
1458
0
    userlist->users = newuser;
1459
1460
0
    cur_arg = 2;
1461
1462
0
    while (*args[cur_arg]) {
1463
0
      if (strcmp(args[cur_arg], "password") == 0) {
1464
#ifdef USE_LIBCRYPT
1465
        if (!crypt("", args[cur_arg + 1])) {
1466
          ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1467
             file, linenum, newuser->user);
1468
          err_code |= ERR_ALERT | ERR_FATAL;
1469
          goto out;
1470
        }
1471
#else
1472
0
        ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1473
0
             file, linenum);
1474
0
        err_code |= ERR_ALERT;
1475
0
#endif
1476
0
        newuser->pass = strdup(args[cur_arg + 1]);
1477
0
        cur_arg += 2;
1478
0
        continue;
1479
0
      } else if (strcmp(args[cur_arg], "insecure-password") == 0) {
1480
0
        newuser->pass = strdup(args[cur_arg + 1]);
1481
0
        newuser->flags |= AU_O_INSECURE;
1482
0
        cur_arg += 2;
1483
0
        continue;
1484
0
      } else if (strcmp(args[cur_arg], "groups") == 0) {
1485
0
        newuser->u.groups_names = strdup(args[cur_arg + 1]);
1486
0
        cur_arg += 2;
1487
0
        continue;
1488
0
      } else {
1489
0
        ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1490
0
           file, linenum, args[0]);
1491
0
        err_code |= ERR_ALERT | ERR_FATAL;
1492
0
        goto out;
1493
0
      }
1494
0
    }
1495
0
  } else {
1496
0
    ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "users");
1497
0
    err_code |= ERR_ALERT | ERR_FATAL;
1498
0
  }
1499
1500
0
out:
1501
0
  return err_code;
1502
0
}
1503
1504
int
1505
cfg_parse_scope(const char *file, int linenum, char *line)
1506
33.3k
{
1507
33.3k
  char *beg, *end, *scope = NULL;
1508
33.3k
  int err_code = 0;
1509
33.3k
  const char *err;
1510
1511
33.3k
  beg = line + 1;
1512
33.3k
  end = strchr(beg, ']');
1513
1514
  /* Detect end of scope declaration */
1515
33.3k
  if (!end || end == beg) {
1516
1.63k
    ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1517
1.63k
       file, linenum);
1518
1.63k
    err_code |= ERR_ALERT | ERR_FATAL;
1519
1.63k
    goto out;
1520
1.63k
  }
1521
1522
  /* Get scope name and check its validity */
1523
31.7k
  scope = my_strndup(beg, end-beg);
1524
31.7k
  err = invalid_char(scope);
1525
31.7k
  if (err) {
1526
3.79k
    ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1527
3.79k
       file, linenum, *err);
1528
3.79k
    err_code |= ERR_ALERT | ERR_ABORT;
1529
3.79k
    goto out;
1530
3.79k
  }
1531
1532
  /* Be sure to have a scope declaration alone on its line */
1533
27.9k
  line = end+1;
1534
27.9k
  while (isspace((unsigned char)*line))
1535
143
    line++;
1536
27.9k
  if (*line && *line != '#' && *line != '\n' && *line != '\r') {
1537
1.18k
    ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1538
1.18k
       file, linenum, *line);
1539
1.18k
    err_code |= ERR_ALERT | ERR_ABORT;
1540
1.18k
    goto out;
1541
1.18k
  }
1542
1543
  /* We have a valid scope declaration, save it */
1544
26.7k
  free(cfg_scope);
1545
26.7k
  cfg_scope = scope;
1546
26.7k
  scope = NULL;
1547
1548
33.3k
  out:
1549
33.3k
  free(scope);
1550
33.3k
  return err_code;
1551
26.7k
}
1552
1553
int
1554
cfg_parse_track_sc_num(unsigned int *track_sc_num,
1555
                       const char *arg, const char *end, char **errmsg)
1556
0
{
1557
0
  const char *p;
1558
0
  unsigned int num;
1559
1560
0
  p = arg;
1561
0
  num = read_uint64(&arg, end);
1562
1563
0
  if (arg != end) {
1564
0
    memprintf(errmsg, "Wrong track-sc number '%s'", p);
1565
0
    return -1;
1566
0
  }
1567
1568
0
  if (num >= global.tune.nb_stk_ctr) {
1569
0
    if (!global.tune.nb_stk_ctr)
1570
0
      memprintf(errmsg, "%u track-sc number not usable, stick-counters "
1571
0
                "are disabled by tune.stick-counters", num);
1572
0
    else
1573
0
      memprintf(errmsg, "%u track-sc number exceeding "
1574
0
                "%d (tune.stick-counters-1) value", num, global.tune.nb_stk_ctr - 1);
1575
0
    return -1;
1576
0
  }
1577
1578
0
  *track_sc_num = num;
1579
0
  return 0;
1580
0
}
1581
1582
/*
1583
 * Detect a global section after a non-global one and output a diagnostic
1584
 * warning.
1585
 */
1586
static void check_section_position(char *section_name, const char *file, int linenum)
1587
0
{
1588
0
  if (strcmp(section_name, "global") == 0) {
1589
0
    if ((global.mode & MODE_DIAG) && non_global_section_parsed == 1)
1590
0
            _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
1591
0
  }
1592
0
  else if (non_global_section_parsed == 0) {
1593
0
    non_global_section_parsed = 1;
1594
0
  }
1595
0
}
1596
1597
/* apply the current default_path setting for config file <file>, and
1598
 * optionally replace the current path to <origin> if not NULL while the
1599
 * default-path mode is set to "origin". Errors are returned into an
1600
 * allocated string passed to <err> if it's not NULL. Returns 0 on failure
1601
 * or non-zero on success.
1602
 */
1603
static int cfg_apply_default_path(const char *file, const char *origin, char **err)
1604
1.51k
{
1605
1.51k
  const char *beg, *end;
1606
1607
  /* make path start at <beg> and end before <end>, and switch it to ""
1608
   * if no slash was passed.
1609
   */
1610
1.51k
  beg = file;
1611
1.51k
  end = strrchr(beg, '/');
1612
1.51k
  if (!end)
1613
0
    end = beg;
1614
1615
1.51k
  if (!*initial_cwd) {
1616
1
    if (getcwd(initial_cwd, sizeof(initial_cwd)) == NULL) {
1617
0
      if (err)
1618
0
        memprintf(err, "Impossible to retrieve startup directory name: %s", strerror(errno));
1619
0
      return 0;
1620
0
    }
1621
1
  }
1622
1.51k
  else if (chdir(initial_cwd) == -1) {
1623
0
    if (err)
1624
0
      memprintf(err, "Impossible to get back to initial directory '%s': %s", initial_cwd, strerror(errno));
1625
0
    return 0;
1626
0
  }
1627
1628
  /* OK now we're (back) to initial_cwd */
1629
1630
1.51k
  switch (default_path_mode) {
1631
1.51k
  case DEFAULT_PATH_CURRENT:
1632
    /* current_cwd never set, nothing to do */
1633
1.51k
    return 1;
1634
1635
0
  case DEFAULT_PATH_ORIGIN:
1636
    /* current_cwd set in the config */
1637
0
    if (origin &&
1638
0
        snprintf(current_cwd, sizeof(current_cwd), "%s", origin) > sizeof(current_cwd)) {
1639
0
      if (err)
1640
0
        memprintf(err, "Absolute path too long: '%s'", origin);
1641
0
      return 0;
1642
0
    }
1643
0
    break;
1644
1645
0
  case DEFAULT_PATH_CONFIG:
1646
0
    if (end - beg >= sizeof(current_cwd)) {
1647
0
      if (err)
1648
0
        memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1649
0
      return 0;
1650
0
    }
1651
0
    memcpy(current_cwd, beg, end - beg);
1652
0
    current_cwd[end - beg] = 0;
1653
0
    break;
1654
1655
0
  case DEFAULT_PATH_PARENT:
1656
0
    if (end - beg + 3 >= sizeof(current_cwd)) {
1657
0
      if (err)
1658
0
        memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1659
0
      return 0;
1660
0
    }
1661
0
    memcpy(current_cwd, beg, end - beg);
1662
0
    if (end > beg)
1663
0
      memcpy(current_cwd + (end - beg), "/..\0", 4);
1664
0
    else
1665
0
      memcpy(current_cwd + (end - beg), "..\0", 3);
1666
0
    break;
1667
1.51k
  }
1668
1669
0
  if (*current_cwd && chdir(current_cwd) == -1) {
1670
0
    if (err)
1671
0
      memprintf(err, "Impossible to get back to directory '%s': %s", initial_cwd, strerror(errno));
1672
0
    return 0;
1673
0
  }
1674
1675
0
  return 1;
1676
0
}
1677
1678
/* parses a global "default-path" directive. */
1679
static int cfg_parse_global_def_path(char **args, int section_type, struct proxy *curpx,
1680
                                     const struct proxy *defpx, const char *file, int line,
1681
                                     char **err)
1682
0
{
1683
0
  int ret = -1;
1684
1685
  /* "current", "config", "parent", "origin <path>" */
1686
1687
0
  if (strcmp(args[1], "current") == 0)
1688
0
    default_path_mode = DEFAULT_PATH_CURRENT;
1689
0
  else if (strcmp(args[1], "config") == 0)
1690
0
    default_path_mode = DEFAULT_PATH_CONFIG;
1691
0
  else if (strcmp(args[1], "parent") == 0)
1692
0
    default_path_mode = DEFAULT_PATH_PARENT;
1693
0
  else if (strcmp(args[1], "origin") == 0)
1694
0
    default_path_mode = DEFAULT_PATH_ORIGIN;
1695
0
  else {
1696
0
    memprintf(err, "%s default-path mode '%s' for '%s', supported modes include 'current', 'config', 'parent', and 'origin'.", *args[1] ? "unsupported" : "missing", args[1], args[0]);
1697
0
    goto end;
1698
0
  }
1699
1700
0
  if (default_path_mode == DEFAULT_PATH_ORIGIN) {
1701
0
    if (!*args[2]) {
1702
0
      memprintf(err, "'%s %s' expects a directory as an argument.", args[0], args[1]);
1703
0
      goto end;
1704
0
    }
1705
0
    if (!cfg_apply_default_path(file, args[2], err)) {
1706
0
      memprintf(err, "couldn't set '%s' to origin '%s': %s.", args[0], args[2], *err);
1707
0
      goto end;
1708
0
    }
1709
0
  }
1710
0
  else if (!cfg_apply_default_path(file, NULL, err)) {
1711
0
    memprintf(err, "couldn't set '%s' to '%s': %s.", args[0], args[1], *err);
1712
0
    goto end;
1713
0
  }
1714
1715
  /* note that once applied, the path is immediately updated */
1716
1717
0
  ret = 0;
1718
0
 end:
1719
0
  return ret;
1720
0
}
1721
1722
/*
1723
 * This function reads and parses the configuration file given in the argument.
1724
 * Returns the error code, 0 if OK, -1 if the config file couldn't be opened,
1725
 * or any combination of :
1726
 *  - ERR_ABORT: must abort ASAP
1727
 *  - ERR_FATAL: we can continue parsing but not start the service
1728
 *  - ERR_WARN: a warning has been emitted
1729
 *  - ERR_ALERT: an alert has been emitted
1730
 * Only the two first ones can stop processing, the two others are just
1731
 * indicators.
1732
 */
1733
int readcfgfile(const char *file)
1734
1.51k
{
1735
1.51k
  char *thisline = NULL;
1736
1.51k
  int linesize = LINESIZE;
1737
1.51k
  FILE *f = NULL;
1738
1.51k
  int linenum = 0;
1739
1.51k
  int err_code = 0;
1740
1.51k
  struct cfg_section *cs = NULL, *pcs = NULL;
1741
1.51k
  struct cfg_section *ics;
1742
1.51k
  int readbytes = 0;
1743
1.51k
  char *outline = NULL;
1744
1.51k
  size_t outlen = 0;
1745
1.51k
  size_t outlinesize = 0;
1746
1.51k
  int fatal = 0;
1747
1.51k
  int missing_lf = -1;
1748
1.51k
  int nested_cond_lvl = 0;
1749
1.51k
  enum nested_cond_state nested_conds[MAXNESTEDCONDS];
1750
1.51k
  char *errmsg = NULL;
1751
1752
1.51k
  global.cfg_curr_line = 0;
1753
1.51k
  global.cfg_curr_file = file;
1754
1755
1.51k
  if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
1756
0
    ha_alert("Out of memory trying to allocate a buffer for a configuration line.\n");
1757
0
    err_code = -1;
1758
0
    goto err;
1759
0
  }
1760
1761
1.51k
  if ((f = fopen(file,"r")) == NULL) {
1762
0
    err_code = -1;
1763
0
    goto err;
1764
0
  }
1765
1766
  /* change to the new dir if required */
1767
1.51k
  if (!cfg_apply_default_path(file, NULL, &errmsg)) {
1768
0
    ha_alert("parsing [%s:%d]: failed to apply default-path: %s.\n", file, linenum, errmsg);
1769
0
    free(errmsg);
1770
0
    err_code = -1;
1771
0
    goto err;
1772
0
  }
1773
1774
54.9k
next_line:
1775
68.5k
  while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
1776
67.5k
    int arg, kwm = KWM_STD;
1777
67.5k
    char *end;
1778
67.5k
    char *args[MAX_LINE_ARGS + 1];
1779
67.5k
    char *line = thisline;
1780
1781
67.5k
    if (missing_lf != -1) {
1782
57
      ha_alert("parsing [%s:%d]: Stray NUL character at position %d.\n",
1783
57
               file, linenum, (missing_lf + 1));
1784
57
      err_code |= ERR_ALERT | ERR_FATAL;
1785
57
      missing_lf = -1;
1786
57
      break;
1787
57
    }
1788
1789
67.4k
    linenum++;
1790
67.4k
    global.cfg_curr_line = linenum;
1791
1792
67.4k
    if (fatal >= 50) {
1793
5
      ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
1794
5
      break;
1795
5
    }
1796
1797
67.4k
    end = line + strlen(line);
1798
1799
67.4k
    if (end-line == linesize-1 && *(end-1) != '\n') {
1800
      /* Check if we reached the limit and the last char is not \n.
1801
       * Watch out for the last line without the terminating '\n'!
1802
       */
1803
1.04k
      char *newline;
1804
1.04k
      int newlinesize = linesize * 2;
1805
1806
1.04k
      newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1807
1.04k
      if (newline == NULL) {
1808
0
        ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1809
0
           file, linenum);
1810
0
        err_code |= ERR_ALERT | ERR_FATAL;
1811
0
        fatal++;
1812
0
        linenum--;
1813
0
        continue;
1814
0
      }
1815
1816
1.04k
      readbytes = linesize - 1;
1817
1.04k
      linesize = newlinesize;
1818
1.04k
      thisline = newline;
1819
1.04k
      linenum--;
1820
1.04k
      continue;
1821
1.04k
    }
1822
1823
66.4k
    readbytes = 0;
1824
1825
66.4k
    if (end > line && *(end-1) == '\n') {
1826
      /* kill trailing LF */
1827
65.0k
      *(end - 1) = 0;
1828
65.0k
    }
1829
1.34k
    else {
1830
      /* mark this line as truncated */
1831
1.34k
      missing_lf = end - line;
1832
1.34k
    }
1833
1834
    /* skip leading spaces */
1835
66.4k
    while (isspace((unsigned char)*line))
1836
1.62k
      line++;
1837
1838
66.4k
    if (*line == '[') {/* This is the beginning if a scope */
1839
33.3k
      err_code |= cfg_parse_scope(file, linenum, line);
1840
33.3k
      goto next_line;
1841
33.3k
    }
1842
1843
34.3k
    while (1) {
1844
34.3k
      uint32_t err;
1845
34.3k
      const char *errptr;
1846
1847
34.3k
      arg = sizeof(args) / sizeof(*args);
1848
34.3k
      outlen = outlinesize;
1849
34.3k
      err = parse_line(line, outline, &outlen, args, &arg,
1850
34.3k
           PARSE_OPT_ENV | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE |
1851
34.3k
           PARSE_OPT_BKSLASH | PARSE_OPT_SHARP | PARSE_OPT_WORD_EXPAND,
1852
34.3k
           &errptr);
1853
1854
34.3k
      if (err & PARSE_ERR_QUOTE) {
1855
555
        size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1856
1857
555
        ha_alert("parsing [%s:%d]: unmatched quote at position %d:\n"
1858
555
           "  %s\n  %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1859
555
        err_code |= ERR_ALERT | ERR_FATAL;
1860
555
        fatal++;
1861
555
        goto next_line;
1862
555
      }
1863
1864
33.7k
      if (err & PARSE_ERR_BRACE) {
1865
189
        size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1866
1867
189
        ha_alert("parsing [%s:%d]: unmatched brace in environment variable name at position %d:\n"
1868
189
           "  %s\n  %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1869
189
        err_code |= ERR_ALERT | ERR_FATAL;
1870
189
        fatal++;
1871
189
        goto next_line;
1872
189
      }
1873
1874
33.6k
      if (err & PARSE_ERR_VARNAME) {
1875
391
        size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1876
1877
391
        ha_alert("parsing [%s:%d]: forbidden first char in environment variable name at position %d:\n"
1878
391
           "  %s\n  %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1879
391
        err_code |= ERR_ALERT | ERR_FATAL;
1880
391
        fatal++;
1881
391
        goto next_line;
1882
391
      }
1883
1884
33.2k
      if (err & PARSE_ERR_HEX) {
1885
126
        size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1886
1887
126
        ha_alert("parsing [%s:%d]: truncated or invalid hexadecimal sequence at position %d:\n"
1888
126
           "  %s\n  %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1889
126
        err_code |= ERR_ALERT | ERR_FATAL;
1890
126
        fatal++;
1891
126
        goto next_line;
1892
126
      }
1893
1894
33.0k
      if (err & PARSE_ERR_WRONG_EXPAND) {
1895
139
        size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1896
1897
139
        ha_alert("parsing [%s:%d]: truncated or invalid word expansion sequence at position %d:\n"
1898
139
           "  %s\n  %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1899
139
        err_code |= ERR_ALERT | ERR_FATAL;
1900
139
        fatal++;
1901
139
        goto next_line;
1902
139
      }
1903
1904
32.9k
      if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
1905
1.29k
        outlinesize = (outlen + 1023) & -1024;
1906
1.29k
        outline = my_realloc2(outline, outlinesize);
1907
1.29k
        if (outline == NULL) {
1908
0
          ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1909
0
             file, linenum);
1910
0
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1911
0
          fatal++;
1912
0
          outlinesize = 0;
1913
0
          goto err;
1914
0
        }
1915
        /* try again */
1916
1.29k
        continue;
1917
1.29k
      }
1918
1919
31.6k
      if (err & PARSE_ERR_TOOMANY) {
1920
        /* only check this *after* being sure the output is allocated */
1921
233
        ha_alert("parsing [%s:%d]: too many words, truncating after word %d, position %ld: <%s>.\n",
1922
233
           file, linenum, MAX_LINE_ARGS, (long)(args[MAX_LINE_ARGS-1] - outline + 1), args[MAX_LINE_ARGS-1]);
1923
233
        err_code |= ERR_ALERT | ERR_FATAL;
1924
233
        fatal++;
1925
233
        goto next_line;
1926
233
      }
1927
1928
      /* everything's OK */
1929
31.4k
      break;
1930
31.6k
    }
1931
1932
    /* dump cfg */
1933
31.4k
    if (global.mode & MODE_DUMP_CFG) {
1934
0
      if (args[0] != NULL) {
1935
0
        struct cfg_section *sect;
1936
0
        int is_sect = 0;
1937
0
        int i = 0;
1938
0
        uint32_t g_key = HA_ATOMIC_LOAD(&global.anon_key);
1939
1940
0
        if (global.mode & MODE_DUMP_NB_L)
1941
0
          qfprintf(stdout, "%d\t", linenum);
1942
1943
        /* if a word is in sections list, is_sect = 1 */
1944
0
        list_for_each_entry(sect, &sections, list) {
1945
0
          if (strcmp(args[0], sect->section_name) == 0) {
1946
0
            is_sect = 1;
1947
0
            break;
1948
0
          }
1949
0
        }
1950
1951
0
        if (g_key == 0) {
1952
          /* no anonymizing needed, dump the config as-is (but without comments).
1953
           * Note: tabs were lost during tokenizing, so we reinsert for non-section
1954
           * keywords.
1955
           */
1956
0
          if (!is_sect)
1957
0
            qfprintf(stdout, "\t");
1958
1959
0
          for (i = 0; i < arg; i++) {
1960
0
            qfprintf(stdout, "%s ", args[i]);
1961
0
          }
1962
0
          qfprintf(stdout, "\n");
1963
0
          continue;
1964
0
        }
1965
1966
        /* We're anonymizing */
1967
1968
0
        if (is_sect) {
1969
          /* new sections are optionally followed by an identifier */
1970
0
          if (arg >= 2) {
1971
0
            qfprintf(stdout, "%s %s\n", args[0], HA_ANON_ID(g_key, args[1]));
1972
0
          }
1973
0
          else {
1974
0
            qfprintf(stdout, "%s\n", args[0]);
1975
0
          }
1976
0
          continue;
1977
0
        }
1978
1979
        /* non-section keywords start indented */
1980
0
        qfprintf(stdout, "\t");
1981
1982
        /* some keywords deserve special treatment */
1983
0
        if (!*args[0]) {
1984
0
          qfprintf(stdout, "\n");
1985
0
        }
1986
1987
0
        else if (strcmp(args[0], "anonkey") == 0) {
1988
0
          qfprintf(stdout, "%s [...]\n", args[0]);
1989
0
        }
1990
1991
0
        else if (strcmp(args[0], "maxconn") == 0) {
1992
0
          qfprintf(stdout, "%s %s\n", args[0], args[1]);
1993
0
        }
1994
1995
0
        else if (strcmp(args[0], "stats") == 0 &&
1996
0
           (strcmp(args[1], "timeout") == 0 || strcmp(args[1], "maxconn") == 0)) {
1997
0
          qfprintf(stdout, "%s %s %s\n", args[0], args[1], args[2]);
1998
0
        }
1999
2000
0
        else if (strcmp(args[0], "stats") == 0 && strcmp(args[1], "socket") == 0) {
2001
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2002
2003
0
          if (arg > 2) {
2004
0
            qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
2005
2006
0
            if (arg > 3) {
2007
0
              qfprintf(stdout, "[...]\n");
2008
0
            }
2009
0
            else {
2010
0
              qfprintf(stdout, "\n");
2011
0
            }
2012
0
          }
2013
0
          else {
2014
0
            qfprintf(stdout, "\n");
2015
0
          }
2016
0
        }
2017
2018
0
        else if (strcmp(args[0], "timeout") == 0) {
2019
0
          qfprintf(stdout, "%s %s %s\n", args[0], args[1], args[2]);
2020
0
        }
2021
2022
0
        else if (strcmp(args[0], "mode") == 0) {
2023
0
          qfprintf(stdout, "%s %s\n", args[0], args[1]);
2024
0
        }
2025
2026
        /* It concerns user in global section and in userlist */
2027
0
        else if (strcmp(args[0], "user") == 0) {
2028
0
          qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2029
2030
0
          if (arg > 2) {
2031
0
            qfprintf(stdout, "[...]\n");
2032
0
          }
2033
0
          else {
2034
0
            qfprintf(stdout, "\n");
2035
0
          }
2036
0
        }
2037
2038
0
        else if (strcmp(args[0], "bind") == 0) {
2039
0
          qfprintf(stdout, "%s ", args[0]);
2040
0
          qfprintf(stdout, "%s ", hash_ipanon(g_key, args[1], 1));
2041
0
          if (arg > 2) {
2042
0
            qfprintf(stdout, "[...]\n");
2043
0
          }
2044
0
          else {
2045
0
            qfprintf(stdout, "\n");
2046
0
          }
2047
0
        }
2048
2049
0
        else if (strcmp(args[0], "server") == 0) {
2050
0
          qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2051
2052
0
          if (arg > 2) {
2053
0
            qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
2054
0
          }
2055
0
          if (arg > 3) {
2056
0
            qfprintf(stdout, "[...]\n");
2057
0
          }
2058
0
          else {
2059
0
            qfprintf(stdout, "\n");
2060
0
          }
2061
0
        }
2062
2063
0
        else if (strcmp(args[0], "redirect") == 0) {
2064
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2065
2066
0
          if (strcmp(args[1], "prefix") == 0 || strcmp(args[1], "location") == 0) {
2067
0
            qfprintf(stdout, "%s ", HA_ANON_PATH(g_key, args[2]));
2068
0
          }
2069
0
          else {
2070
0
            qfprintf(stdout, "%s ", args[2]);
2071
0
          }
2072
0
          if (arg > 3) {
2073
0
            qfprintf(stdout, "[...]");
2074
0
          }
2075
0
          qfprintf(stdout, "\n");
2076
0
        }
2077
2078
0
        else if (strcmp(args[0], "acl") == 0) {
2079
0
          qfprintf(stdout, "%s %s %s ", args[0], HA_ANON_ID(g_key, args[1]), args[2]);
2080
2081
0
          if (arg > 3) {
2082
0
            qfprintf(stdout, "[...]");
2083
0
          }
2084
0
          qfprintf(stdout, "\n");
2085
0
        }
2086
2087
0
        else if (strcmp(args[0], "log") == 0) {
2088
0
          qfprintf(stdout, "log ");
2089
2090
0
          if (strcmp(args[1], "global") == 0) {
2091
0
            qfprintf(stdout, "%s ", args[1]);
2092
0
          }
2093
0
          else {
2094
0
            qfprintf(stdout, "%s ", hash_ipanon(g_key, args[1], 1));
2095
0
          }
2096
0
          if (arg > 2) {
2097
0
            qfprintf(stdout, "[...]");
2098
0
          }
2099
0
          qfprintf(stdout, "\n");
2100
0
        }
2101
2102
0
        else if (strcmp(args[0], "peer") == 0) {
2103
0
          qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2104
0
          qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
2105
2106
0
          if (arg > 3) {
2107
0
            qfprintf(stdout, "[...]");
2108
0
          }
2109
0
          qfprintf(stdout, "\n");
2110
0
        }
2111
2112
0
        else if (strcmp(args[0], "use_backend") == 0) {
2113
0
          qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2114
2115
0
          if (arg > 2) {
2116
0
            qfprintf(stdout, "[...]");
2117
0
          }
2118
0
          qfprintf(stdout, "\n");
2119
0
        }
2120
2121
0
        else if (strcmp(args[0], "default_backend") == 0) {
2122
0
          qfprintf(stdout, "%s %s\n", args[0], HA_ANON_ID(g_key, args[1]));
2123
0
        }
2124
2125
0
        else if (strcmp(args[0], "source") == 0) {
2126
0
          qfprintf(stdout, "%s %s ", args[0], hash_ipanon(g_key, args[1], 1));
2127
2128
0
          if (arg > 2) {
2129
0
            qfprintf(stdout, "[...]");
2130
0
          }
2131
0
          qfprintf(stdout, "\n");
2132
0
        }
2133
2134
0
        else if (strcmp(args[0], "nameserver") == 0) {
2135
0
          qfprintf(stdout, "%s %s %s ", args[0],
2136
0
            HA_ANON_ID(g_key, args[1]), hash_ipanon(g_key, args[2], 1));
2137
0
          if (arg > 3) {
2138
0
            qfprintf(stdout, "[...]");
2139
0
          }
2140
0
          qfprintf(stdout, "\n");
2141
0
        }
2142
2143
0
        else if (strcmp(args[0], "http-request") == 0) {
2144
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2145
0
          if (arg > 2)
2146
0
            qfprintf(stdout, "[...]");
2147
0
          qfprintf(stdout, "\n");
2148
0
        }
2149
2150
0
        else if (strcmp(args[0], "http-response") == 0) {
2151
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2152
0
          if (arg > 2)
2153
0
            qfprintf(stdout, "[...]");
2154
0
          qfprintf(stdout, "\n");
2155
0
        }
2156
2157
0
        else if (strcmp(args[0], "http-after-response") == 0) {
2158
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2159
0
          if (arg > 2)
2160
0
            qfprintf(stdout, "[...]");
2161
0
          qfprintf(stdout, "\n");
2162
0
        }
2163
2164
0
        else if (strcmp(args[0], "filter") == 0) {
2165
0
          qfprintf(stdout, "%s %s ", args[0], args[1]);
2166
0
          if (arg > 2)
2167
0
            qfprintf(stdout, "[...]");
2168
0
          qfprintf(stdout, "\n");
2169
0
        }
2170
2171
0
        else if (strcmp(args[0], "errorfile") == 0) {
2172
0
          qfprintf(stdout, "%s %s %s\n", args[0], args[1], HA_ANON_PATH(g_key, args[2]));
2173
0
        }
2174
2175
0
        else if (strcmp(args[0], "cookie") == 0) {
2176
0
          qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2177
0
          if (arg > 2)
2178
0
            qfprintf(stdout, "%s ", args[2]);
2179
0
          if (arg > 3)
2180
0
            qfprintf(stdout, "[...]");
2181
0
          qfprintf(stdout, "\n");
2182
0
        }
2183
2184
0
        else if (strcmp(args[0], "stats") == 0 && strcmp(args[1], "auth") == 0) {
2185
0
          qfprintf(stdout, "%s %s %s\n", args[0], args[1], HA_ANON_STR(g_key, args[2]));
2186
0
        }
2187
2188
0
        else {
2189
          /* display up to 3 words and mask the rest which might be confidential */
2190
0
          for (i = 0; i < MIN(arg, 3); i++) {
2191
0
            qfprintf(stdout, "%s ", args[i]);
2192
0
          }
2193
0
          if (arg > 3) {
2194
0
            qfprintf(stdout, "[...]");
2195
0
          }
2196
0
          qfprintf(stdout, "\n");
2197
0
        }
2198
0
      }
2199
0
      continue;
2200
0
    }
2201
    /* end of config dump */
2202
2203
    /* empty line */
2204
31.4k
    if (!**args)
2205
10.4k
      continue;
2206
2207
    /* check for config macros */
2208
20.9k
    if (*args[0] == '.') {
2209
14.7k
      if (strcmp(args[0], ".if") == 0) {
2210
5.86k
        const char *errptr = NULL;
2211
5.86k
        char *errmsg = NULL;
2212
5.86k
        int cond;
2213
5.86k
        char *w;
2214
2215
        /* remerge all words into a single expression */
2216
8.32k
        for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ')
2217
2.45k
          ;
2218
2219
5.86k
        nested_cond_lvl++;
2220
5.86k
        if (nested_cond_lvl >= MAXNESTEDCONDS) {
2221
4
          ha_alert("parsing [%s:%d]: too many nested '.if', max is %d.\n", file, linenum, MAXNESTEDCONDS);
2222
4
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2223
4
          goto err;
2224
4
        }
2225
2226
5.86k
        if (nested_cond_lvl > 1 &&
2227
5.86k
            (nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_DROP ||
2228
4.24k
             nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_SKIP ||
2229
4.24k
             nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_DROP ||
2230
4.24k
             nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_SKIP ||
2231
4.24k
             nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELSE_DROP)) {
2232
3.40k
          nested_conds[nested_cond_lvl] = NESTED_COND_IF_SKIP;
2233
3.40k
          goto next_line;
2234
3.40k
        }
2235
2236
2.45k
        cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
2237
2.45k
        if (cond < 0) {
2238
285
          size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 76);
2239
2240
285
          ha_alert("parsing [%s:%d]: %s in '.if' at position %d:\n  .if %s\n  %*s\n",
2241
285
             file, linenum, errmsg,
2242
285
                   (int)(errptr-args[1]+1), args[1], (int)(newpos+5), "^");
2243
2244
285
          free(errmsg);
2245
285
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2246
285
          goto err;
2247
285
        }
2248
2249
2.16k
        if (cond)
2250
828
          nested_conds[nested_cond_lvl] = NESTED_COND_IF_TAKE;
2251
1.34k
        else
2252
1.34k
          nested_conds[nested_cond_lvl] = NESTED_COND_IF_DROP;
2253
2254
2.16k
        goto next_line;
2255
2.45k
      }
2256
8.88k
      else if (strcmp(args[0], ".elif") == 0) {
2257
2.57k
        const char *errptr = NULL;
2258
2.57k
        char *errmsg = NULL;
2259
2.57k
        int cond;
2260
2.57k
        char *w;
2261
2262
        /* remerge all words into a single expression */
2263
3.85k
        for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ')
2264
1.28k
          ;
2265
2266
2.57k
        if (!nested_cond_lvl) {
2267
2
          ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum);
2268
2
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2269
2
          goto err;
2270
2
        }
2271
2272
2.56k
        if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2273
2.56k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2274
1
          ha_alert("parsing [%s:%d]: '.elif' after '.else' is not permitted.\n", file, linenum);
2275
1
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2276
1
          goto err;
2277
1
        }
2278
2279
2.56k
        if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2280
2.56k
            nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2281
2.56k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
2282
2.56k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2283
1.26k
          nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_SKIP;
2284
1.26k
          goto next_line;
2285
1.26k
        }
2286
2287
1.30k
        cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
2288
1.30k
        if (cond < 0) {
2289
6
          size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 74);
2290
2291
6
          ha_alert("parsing [%s:%d]: %s in '.elif' at position %d:\n  .elif %s\n  %*s\n",
2292
6
             file, linenum, errmsg,
2293
6
                   (int)(errptr-args[1]+1), args[1], (int)(newpos+7), "^");
2294
2295
6
          free(errmsg);
2296
6
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2297
6
          goto err;
2298
6
        }
2299
2300
1.30k
        if (cond)
2301
583
          nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_TAKE;
2302
719
        else
2303
719
          nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_DROP;
2304
2305
1.30k
        goto next_line;
2306
1.30k
      }
2307
6.31k
      else if (strcmp(args[0], ".else") == 0) {
2308
1.19k
        if (*args[1]) {
2309
7
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n",
2310
7
                   file, linenum, args[1], args[0]);
2311
7
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2312
7
          break;
2313
7
        }
2314
2315
1.18k
        if (!nested_cond_lvl) {
2316
1
          ha_alert("parsing [%s:%d]: lone '.else' with no matching '.if'.\n", file, linenum);
2317
1
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2318
1
          goto err;
2319
1
        }
2320
2321
1.18k
        if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2322
1.18k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2323
0
          ha_alert("parsing [%s:%d]: '.else' after '.else' is not permitted.\n", file, linenum);
2324
0
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2325
0
          goto err;
2326
0
        }
2327
2328
1.18k
        if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2329
1.18k
            nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2330
1.18k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
2331
1.18k
            nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2332
935
          nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_DROP;
2333
935
        } else {
2334
          /* otherwise we take the "else" */
2335
251
          nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_TAKE;
2336
251
        }
2337
1.18k
        goto next_line;
2338
1.18k
      }
2339
5.11k
      else if (strcmp(args[0], ".endif") == 0) {
2340
2.35k
        if (*args[1]) {
2341
4
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n",
2342
4
                   file, linenum, args[1], args[0]);
2343
4
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2344
4
          break;
2345
4
        }
2346
2347
2.34k
        if (!nested_cond_lvl) {
2348
0
          ha_alert("parsing [%s:%d]: lone '.endif' with no matching '.if'.\n", file, linenum);
2349
0
          err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2350
0
          break;
2351
0
        }
2352
2.34k
        nested_cond_lvl--;
2353
2.34k
        goto next_line;
2354
2.34k
      }
2355
14.7k
    }
2356
2357
8.95k
    if (nested_cond_lvl &&
2358
8.95k
        (nested_conds[nested_cond_lvl] == NESTED_COND_IF_DROP ||
2359
5.30k
         nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2360
5.30k
         nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_DROP ||
2361
5.30k
         nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP ||
2362
5.30k
         nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP)) {
2363
      /* The current block is masked out by the conditions */
2364
5.05k
      goto next_line;
2365
5.05k
    }
2366
2367
    /* .warning/.error/.notice/.diag */
2368
3.89k
    if (*args[0] == '.') {
2369
1.78k
      if (strcmp(args[0], ".alert") == 0) {
2370
314
        if (*args[2]) {
2371
314
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2372
314
                     file, linenum, args[2], args[0]);
2373
314
          err_code |= ERR_ALERT | ERR_FATAL;
2374
314
          goto next_line;
2375
314
        }
2376
2377
0
        ha_alert("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2378
0
        err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2379
0
        goto err;
2380
314
      }
2381
1.47k
      else if (strcmp(args[0], ".warning") == 0) {
2382
406
        if (*args[2]) {
2383
196
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2384
196
                     file, linenum, args[2], args[0]);
2385
196
          err_code |= ERR_ALERT | ERR_FATAL;
2386
196
          goto next_line;
2387
196
        }
2388
2389
210
        ha_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2390
210
        err_code |= ERR_WARN;
2391
210
        goto next_line;
2392
406
      }
2393
1.06k
      else if (strcmp(args[0], ".notice") == 0) {
2394
390
        if (*args[2]) {
2395
191
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2396
191
                   file, linenum, args[2], args[0]);
2397
191
          err_code |= ERR_ALERT | ERR_FATAL;
2398
191
          goto next_line;
2399
191
        }
2400
2401
199
        ha_notice("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2402
199
        goto next_line;
2403
390
      }
2404
675
      else if (strcmp(args[0], ".diag") == 0) {
2405
555
        if (*args[2]) {
2406
193
          ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2407
193
                   file, linenum, args[2], args[0]);
2408
193
          err_code |= ERR_ALERT | ERR_FATAL;
2409
193
          goto next_line;
2410
193
        }
2411
2412
362
        ha_diag_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2413
362
        goto next_line;
2414
555
      }
2415
120
      else {
2416
120
        ha_alert("parsing [%s:%d]: unknown directive '%s'.\n", file, linenum, args[0]);
2417
120
        err_code |= ERR_ALERT | ERR_FATAL;
2418
120
        fatal++;
2419
120
        break;
2420
120
      }
2421
1.78k
    }
2422
2423
    /* check for keyword modifiers "no" and "default" */
2424
2.11k
    if (strcmp(args[0], "no") == 0) {
2425
1.10k
      char *tmp;
2426
2427
1.10k
      kwm = KWM_NO;
2428
1.10k
      tmp = args[0];
2429
2.35k
      for (arg=0; *args[arg+1]; arg++)
2430
1.24k
        args[arg] = args[arg+1];   // shift args after inversion
2431
1.10k
      *tmp = '\0';          // fix the next arg to \0
2432
1.10k
      args[arg] = tmp;
2433
1.10k
    }
2434
1.00k
    else if (strcmp(args[0], "default") == 0) {
2435
97
      kwm = KWM_DEF;
2436
346
      for (arg=0; *args[arg+1]; arg++)
2437
249
        args[arg] = args[arg+1];   // shift args after inversion
2438
97
    }
2439
2440
2.11k
    if (kwm != KWM_STD && strcmp(args[0], "option") != 0 &&
2441
2.11k
        strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling") != 0 &&
2442
2.11k
        strcmp(args[0], "set-dumpable") != 0 && strcmp(args[0], "strict-limits") != 0 &&
2443
2.11k
        strcmp(args[0], "insecure-fork-wanted") != 0 &&
2444
2.11k
        strcmp(args[0], "numa-cpu-mapping") != 0) {
2445
712
      ha_alert("parsing [%s:%d]: negation/default currently "
2446
712
         "supported only for options, log, busy-polling, "
2447
712
         "set-dumpable, strict-limits, insecure-fork-wanted "
2448
712
         "and numa-cpu-mapping.\n", file, linenum);
2449
712
      err_code |= ERR_ALERT | ERR_FATAL;
2450
712
      fatal++;
2451
712
    }
2452
2453
    /* detect section start */
2454
2.11k
    list_for_each_entry(ics, &sections, list) {
2455
0
      if (strcmp(args[0], ics->section_name) == 0) {
2456
0
        cursection = ics->section_name;
2457
0
        pcs = cs;
2458
0
        cs = ics;
2459
0
        free(global.cfg_curr_section);
2460
0
        global.cfg_curr_section = strdup(*args[1] ? args[1] : args[0]);
2461
0
        check_section_position(args[0], file, linenum);
2462
0
        break;
2463
0
      }
2464
0
    }
2465
2466
2.11k
    if (pcs && pcs->post_section_parser) {
2467
0
      int status;
2468
2469
0
      status = pcs->post_section_parser();
2470
0
      err_code |= status;
2471
0
      if (status & ERR_FATAL)
2472
0
        fatal++;
2473
2474
0
      if (err_code & ERR_ABORT)
2475
0
        goto err;
2476
0
    }
2477
2.11k
    pcs = NULL;
2478
2479
2.11k
    if (!cs) {
2480
2.11k
      ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2481
2.11k
      err_code |= ERR_ALERT | ERR_FATAL;
2482
2.11k
      fatal++;
2483
2.11k
    } else {
2484
0
      int status;
2485
2486
0
      status = cs->section_parser(file, linenum, args, kwm);
2487
0
      err_code |= status;
2488
0
      if (status & ERR_FATAL)
2489
0
        fatal++;
2490
2491
0
      if (err_code & ERR_ABORT)
2492
0
        goto err;
2493
0
    }
2494
2.11k
  }
2495
2496
1.22k
  if (missing_lf != -1) {
2497
1.02k
    ha_alert("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d.\n",
2498
1.02k
             file, linenum, (missing_lf + 1));
2499
1.02k
    err_code |= ERR_ALERT | ERR_FATAL;
2500
1.02k
  }
2501
2502
1.22k
  ha_free(&global.cfg_curr_section);
2503
1.22k
  if (cs && cs->post_section_parser)
2504
0
    err_code |= cs->post_section_parser();
2505
2506
1.22k
  if (nested_cond_lvl) {
2507
245
    ha_alert("parsing [%s:%d]: non-terminated '.if' block.\n", file, linenum);
2508
245
    err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2509
245
  }
2510
2511
1.22k
  if (*initial_cwd && chdir(initial_cwd) == -1) {
2512
0
    ha_alert("Impossible to get back to initial directory '%s' : %s\n", initial_cwd, strerror(errno));
2513
0
    err_code |= ERR_ALERT | ERR_FATAL;
2514
0
  }
2515
2516
1.51k
err:
2517
1.51k
  ha_free(&cfg_scope);
2518
1.51k
  cursection = NULL;
2519
1.51k
  free(thisline);
2520
1.51k
  free(outline);
2521
1.51k
  global.cfg_curr_line = 0;
2522
1.51k
  global.cfg_curr_file = NULL;
2523
2524
1.51k
  if (f)
2525
1.51k
    fclose(f);
2526
2527
1.51k
  return err_code;
2528
1.22k
}
2529
2530
#if defined(USE_THREAD) && defined USE_CPU_AFFINITY
2531
#if defined(__linux__)
2532
2533
/* filter directory name of the pattern node<X> */
2534
static int numa_filter(const struct dirent *dir)
2535
{
2536
  char *endptr;
2537
2538
  /* dir name must start with "node" prefix */
2539
  if (strncmp(dir->d_name, "node", 4))
2540
    return 0;
2541
2542
  /* dir name must be at least 5 characters long */
2543
  if (!dir->d_name[4])
2544
    return 0;
2545
2546
  /* dir name must end with a numeric id */
2547
  if (strtol(&dir->d_name[4], &endptr, 10) < 0 || *endptr)
2548
    return 0;
2549
2550
  /* all tests succeeded */
2551
  return 1;
2552
}
2553
2554
/* Inspect the cpu topology of the machine on startup. If a multi-socket
2555
 * machine is detected, try to bind on the first node with active cpu. This is
2556
 * done to prevent an impact on the overall performance when the topology of
2557
 * the machine is unknown. This function is not called if one of the conditions
2558
 * is met :
2559
 * - a non-null nbthread directive is active
2560
 * - a restrictive cpu-map directive is active
2561
 * - a restrictive affinity is already applied, for example via taskset
2562
 *
2563
 * Returns the count of cpus selected. If no automatic binding was required or
2564
 * an error occurred and the topology is unknown, 0 is returned.
2565
 */
2566
static int numa_detect_topology()
2567
{
2568
  struct dirent **node_dirlist;
2569
  int node_dirlist_size;
2570
2571
  struct hap_cpuset active_cpus, node_cpu_set;
2572
  const char *parse_cpu_set_args[2];
2573
  char *err = NULL;
2574
  int grp, thr;
2575
2576
  /* node_cpu_set count is used as return value */
2577
  ha_cpuset_zero(&node_cpu_set);
2578
2579
  /* 1. count the sysfs node<X> directories */
2580
  node_dirlist = NULL;
2581
  node_dirlist_size = scandir(NUMA_DETECT_SYSTEM_SYSFS_PATH"/node", &node_dirlist, numa_filter, alphasort);
2582
  if (node_dirlist_size <= 1)
2583
    goto free_scandir_entries;
2584
2585
  /* 2. read and parse the list of currently online cpu */
2586
  if (read_line_to_trash("%s/cpu/online", NUMA_DETECT_SYSTEM_SYSFS_PATH) < 0) {
2587
    ha_notice("Cannot read online CPUs list, will not try to refine binding\n");
2588
    goto free_scandir_entries;
2589
  }
2590
2591
  parse_cpu_set_args[0] = trash.area;
2592
  parse_cpu_set_args[1] = "\0";
2593
  if (parse_cpu_set(parse_cpu_set_args, &active_cpus, &err) != 0) {
2594
    ha_notice("Cannot read online CPUs list: '%s'. Will not try to refine binding\n", err);
2595
    free(err);
2596
    goto free_scandir_entries;
2597
  }
2598
2599
  /* 3. loop through nodes dirs and find the first one with active cpus */
2600
  while (node_dirlist_size--) {
2601
    const char *node = node_dirlist[node_dirlist_size]->d_name;
2602
    ha_cpuset_zero(&node_cpu_set);
2603
2604
    if (read_line_to_trash("%s/node/%s/cpumap", NUMA_DETECT_SYSTEM_SYSFS_PATH, node) < 0) {
2605
      ha_notice("Cannot read CPUs list of '%s', will not select them to refine binding\n", node);
2606
      free(node_dirlist[node_dirlist_size]);
2607
      continue;
2608
    }
2609
2610
    parse_cpumap(trash.area, &node_cpu_set);
2611
    ha_cpuset_and(&node_cpu_set, &active_cpus);
2612
2613
    /* 5. set affinity on the first found node with active cpus */
2614
    if (!ha_cpuset_count(&node_cpu_set)) {
2615
      free(node_dirlist[node_dirlist_size]);
2616
      continue;
2617
    }
2618
2619
    ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%s' (%u active cpu(s))\n", node, ha_cpuset_count(&node_cpu_set));
2620
    for (grp = 0; grp < MAX_TGROUPS; grp++)
2621
      for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
2622
        ha_cpuset_assign(&cpu_map[grp].thread[thr], &node_cpu_set);
2623
2624
    free(node_dirlist[node_dirlist_size]);
2625
    break;
2626
  }
2627
2628
 free_scandir_entries:
2629
  while (node_dirlist_size-- > 0)
2630
    free(node_dirlist[node_dirlist_size]);
2631
  free(node_dirlist);
2632
2633
  return ha_cpuset_count(&node_cpu_set);
2634
}
2635
2636
#elif defined(__FreeBSD__)
2637
static int numa_detect_topology()
2638
{
2639
  struct hap_cpuset node_cpu_set;
2640
  int ndomains = 0, i;
2641
  size_t len = sizeof(ndomains);
2642
  int grp, thr;
2643
2644
  if (sysctlbyname("vm.ndomains", &ndomains, &len, NULL, 0) == -1) {
2645
    ha_notice("Cannot assess the number of CPUs domains\n");
2646
    return 0;
2647
  }
2648
2649
  BUG_ON(ndomains > MAXMEMDOM);
2650
  ha_cpuset_zero(&node_cpu_set);
2651
2652
  if (ndomains < 2)
2653
    goto leave;
2654
2655
  /*
2656
   * We retrieve the first active valid CPU domain
2657
   * with active cpu and binding it, we returns
2658
   * the number of cpu from the said domain
2659
   */
2660
  for (i = 0; i < ndomains; i ++) {
2661
    struct hap_cpuset dom;
2662
    ha_cpuset_zero(&dom);
2663
    if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_DOMAIN, i, sizeof(dom.cpuset), &dom.cpuset) == -1)
2664
      continue;
2665
2666
    if (!ha_cpuset_count(&dom))
2667
      continue;
2668
2669
    ha_cpuset_assign(&node_cpu_set, &dom);
2670
2671
    ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%d' (%u active cpu(s))\n", i, ha_cpuset_count(&node_cpu_set));
2672
    for (grp = 0; grp < MAX_TGROUPS; grp++)
2673
      for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
2674
        ha_cpuset_assign(&cpu_map[grp].thread[thr], &node_cpu_set);
2675
    break;
2676
  }
2677
 leave:
2678
  return ha_cpuset_count(&node_cpu_set);
2679
}
2680
2681
#else
2682
static int numa_detect_topology()
2683
{
2684
  return 0;
2685
}
2686
2687
#endif
2688
#endif /* USE_THREAD && USE_CPU_AFFINITY */
2689
2690
/*
2691
 * Returns the error code, 0 if OK, or any combination of :
2692
 *  - ERR_ABORT: must abort ASAP
2693
 *  - ERR_FATAL: we can continue parsing but not start the service
2694
 *  - ERR_WARN: a warning has been emitted
2695
 *  - ERR_ALERT: an alert has been emitted
2696
 * Only the two first ones can stop processing, the two others are just
2697
 * indicators.
2698
 */
2699
int check_config_validity()
2700
0
{
2701
0
  int cfgerr = 0;
2702
0
  struct proxy *curproxy = NULL;
2703
0
  struct proxy *init_proxies_list = NULL;
2704
0
  struct stktable *t;
2705
0
  struct server *newsrv = NULL;
2706
0
  int err_code = 0;
2707
0
  unsigned int next_pxid = 1;
2708
0
  struct bind_conf *bind_conf;
2709
0
  char *err;
2710
0
  struct cfg_postparser *postparser;
2711
0
  struct resolvers *curr_resolvers = NULL;
2712
0
  int i;
2713
2714
0
  bind_conf = NULL;
2715
  /*
2716
   * Now, check for the integrity of all that we have collected.
2717
   */
2718
2719
0
  if (!global.tune.max_http_hdr)
2720
0
    global.tune.max_http_hdr = MAX_HTTP_HDR;
2721
2722
0
  if (!global.tune.cookie_len)
2723
0
    global.tune.cookie_len = CAPTURE_LEN;
2724
2725
0
  if (!global.tune.requri_len)
2726
0
    global.tune.requri_len = REQURI_LEN;
2727
2728
0
  if (!global.nbthread) {
2729
    /* nbthread not set, thus automatic. In this case, and only if
2730
     * running on a single process, we enable the same number of
2731
     * threads as the number of CPUs the process is bound to. This
2732
     * allows to easily control the number of threads using taskset.
2733
     */
2734
0
    global.nbthread = 1;
2735
2736
#if defined(USE_THREAD)
2737
    {
2738
      int numa_cores = 0;
2739
#if defined(USE_CPU_AFFINITY)
2740
      if (global.numa_cpu_mapping && !thread_cpu_mask_forced() && !cpu_map_configured())
2741
        numa_cores = numa_detect_topology();
2742
#endif
2743
      global.nbthread = numa_cores ? numa_cores :
2744
                                     thread_cpus_enabled_at_boot;
2745
2746
      /* Note that we cannot have more than 32 or 64 threads per group */
2747
      if (!global.nbtgroups)
2748
        global.nbtgroups = 1;
2749
2750
      if (global.nbthread > MAX_THREADS_PER_GROUP * global.nbtgroups) {
2751
        ha_diag_warning("nbthread not set, found %d CPUs, limiting to %d threads (maximum is %d per thread group). Please set nbthreads and/or increase thread-groups in the global section to silence this warning.\n",
2752
             global.nbthread, MAX_THREADS_PER_GROUP * global.nbtgroups, MAX_THREADS_PER_GROUP);
2753
        global.nbthread = MAX_THREADS_PER_GROUP * global.nbtgroups;
2754
      }
2755
    }
2756
#endif
2757
0
  }
2758
2759
0
  if (!global.nbtgroups)
2760
0
    global.nbtgroups = 1;
2761
2762
0
  if (thread_map_to_groups() < 0) {
2763
0
    err_code |= ERR_ALERT | ERR_FATAL;
2764
0
    goto out;
2765
0
  }
2766
2767
0
  pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
2768
2769
0
  pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
2770
2771
  /* Post initialisation of the users and groups lists. */
2772
0
  err_code = userlist_postinit();
2773
0
  if (err_code != ERR_NONE)
2774
0
    goto out;
2775
2776
  /* first, we will invert the proxy list order */
2777
0
  curproxy = NULL;
2778
0
  while (proxies_list) {
2779
0
    struct proxy *next;
2780
2781
0
    next = proxies_list->next;
2782
0
    proxies_list->next = curproxy;
2783
0
    curproxy = proxies_list;
2784
0
    if (!next)
2785
0
      break;
2786
0
    proxies_list = next;
2787
0
  }
2788
2789
  /* starting to initialize the main proxies list */
2790
0
  init_proxies_list = proxies_list;
2791
2792
0
init_proxies_list_stage1:
2793
0
  for (curproxy = init_proxies_list; curproxy; curproxy = curproxy->next) {
2794
0
    struct switching_rule *rule;
2795
0
    struct server_rule *srule;
2796
0
    struct sticking_rule *mrule;
2797
0
    struct logger *tmplogger;
2798
0
    unsigned int next_id;
2799
2800
0
    if (!(curproxy->cap & PR_CAP_INT) && curproxy->uuid < 0) {
2801
      /* proxy ID not set, use automatic numbering with first
2802
       * spare entry starting with next_pxid. We don't assign
2803
       * numbers for internal proxies as they may depend on
2804
       * build or config options and we don't want them to
2805
       * possibly reuse existing IDs.
2806
       */
2807
0
      next_pxid = get_next_id(&used_proxy_id, next_pxid);
2808
0
      curproxy->conf.id.key = curproxy->uuid = next_pxid;
2809
0
      eb32_insert(&used_proxy_id, &curproxy->conf.id);
2810
0
    }
2811
2812
0
    if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize >= (256 << 20) && ONLY_ONCE()) {
2813
0
      ha_alert("global.tune.bufsize must be below 256 MB when HTTP is in use (current value = %d).\n",
2814
0
         global.tune.bufsize);
2815
0
      cfgerr++;
2816
0
    }
2817
2818
    /* next IDs are shifted even if the proxy is disabled, this
2819
     * guarantees that a proxy that is temporarily disabled in the
2820
     * configuration doesn't cause a renumbering. Internal proxies
2821
     * that are not assigned a static ID must never shift the IDs
2822
     * either since they may appear in any order (Lua, logs, etc).
2823
     * The GLOBAL proxy that carries the stats socket has its ID
2824
     * forced to zero.
2825
     */
2826
0
    if (curproxy->uuid >= 0)
2827
0
      next_pxid++;
2828
2829
0
    if (curproxy->flags & PR_FL_DISABLED) {
2830
      /* ensure we don't keep listeners uselessly bound. We
2831
       * can't disable their listeners yet (fdtab not
2832
       * allocated yet) but let's skip them.
2833
       */
2834
0
      if (curproxy->table) {
2835
0
        ha_free(&curproxy->table->peers.name);
2836
0
        curproxy->table->peers.p = NULL;
2837
0
      }
2838
0
      continue;
2839
0
    }
2840
2841
    /* The current proxy is referencing a default proxy. We must
2842
     * finalize its config, but only once. If the default proxy is
2843
     * ready (PR_FL_READY) it means it was already fully configured.
2844
     */
2845
0
    if (curproxy->defpx) {
2846
0
      if (!(curproxy->defpx->flags & PR_FL_READY)) {
2847
        /* check validity for 'tcp-request' layer 4/5/6/7 rules */
2848
0
        cfgerr += check_action_rules(&curproxy->defpx->tcp_req.l4_rules, curproxy->defpx, &err_code);
2849
0
        cfgerr += check_action_rules(&curproxy->defpx->tcp_req.l5_rules, curproxy->defpx, &err_code);
2850
0
        cfgerr += check_action_rules(&curproxy->defpx->tcp_req.inspect_rules, curproxy->defpx, &err_code);
2851
0
        cfgerr += check_action_rules(&curproxy->defpx->tcp_rep.inspect_rules, curproxy->defpx, &err_code);
2852
0
        cfgerr += check_action_rules(&curproxy->defpx->http_req_rules, curproxy->defpx, &err_code);
2853
0
        cfgerr += check_action_rules(&curproxy->defpx->http_res_rules, curproxy->defpx, &err_code);
2854
0
        cfgerr += check_action_rules(&curproxy->defpx->http_after_res_rules, curproxy->defpx, &err_code);
2855
2856
0
        err = NULL;
2857
0
        i = smp_resolve_args(curproxy->defpx, &err);
2858
0
        cfgerr += i;
2859
0
        if (i) {
2860
0
          indent_msg(&err, 8);
2861
0
          ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
2862
0
          ha_free(&err);
2863
0
        }
2864
0
        else
2865
0
          cfgerr += acl_find_targets(curproxy->defpx);
2866
2867
        /* default proxy is now ready. Set the right FE/BE capabilities */
2868
0
        curproxy->defpx->flags |= PR_FL_READY;
2869
0
      }
2870
0
    }
2871
2872
    /* check and reduce the bind-proc of each listener */
2873
0
    list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2874
0
      int ret;
2875
2876
      /* HTTP frontends with "h2" as ALPN/NPN will work in
2877
       * HTTP/2 and absolutely require buffers 16kB or larger.
2878
       */
2879
#ifdef USE_OPENSSL
2880
      /* no-alpn ? If so, it's the right moment to remove it */
2881
      if (bind_conf->ssl_conf.alpn_str && !bind_conf->ssl_conf.alpn_len) {
2882
        free(bind_conf->ssl_conf.alpn_str);
2883
        bind_conf->ssl_conf.alpn_str = NULL;
2884
      }
2885
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2886
      else if (!bind_conf->ssl_conf.alpn_str && !bind_conf->ssl_conf.npn_str &&
2887
         ((bind_conf->options & BC_O_USE_SSL) || bind_conf->xprt == xprt_get(XPRT_QUIC)) &&
2888
         curproxy->mode == PR_MODE_HTTP && global.tune.bufsize >= 16384) {
2889
2890
        /* Neither ALPN nor NPN were explicitly set nor disabled, we're
2891
         * in HTTP mode with an SSL or QUIC listener, we can enable ALPN.
2892
         * Note that it's in binary form.
2893
         */
2894
        if (bind_conf->xprt == xprt_get(XPRT_QUIC))
2895
          bind_conf->ssl_conf.alpn_str = strdup("\002h3");
2896
        else
2897
          bind_conf->ssl_conf.alpn_str = strdup("\002h2\010http/1.1");
2898
2899
        if (!bind_conf->ssl_conf.alpn_str) {
2900
          ha_alert("Proxy '%s': out of memory while trying to allocate a default alpn string in 'bind %s' at [%s:%d].\n",
2901
             curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
2902
          cfgerr++;
2903
          err_code |= ERR_FATAL | ERR_ALERT;
2904
          goto out;
2905
        }
2906
        bind_conf->ssl_conf.alpn_len = strlen(bind_conf->ssl_conf.alpn_str);
2907
      }
2908
#endif
2909
2910
      if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2911
#ifdef OPENSSL_NPN_NEGOTIATED
2912
        /* check NPN */
2913
        if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
2914
          ha_alert("HTTP frontend '%s' enables HTTP/2 via NPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2915
             curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
2916
          cfgerr++;
2917
        }
2918
#endif
2919
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2920
        /* check ALPN */
2921
        if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
2922
          ha_alert("HTTP frontend '%s' enables HTTP/2 via ALPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2923
             curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
2924
          cfgerr++;
2925
        }
2926
#endif
2927
      } /* HTTP && bufsize < 16384 */
2928
#endif
2929
2930
      /* finish the bind setup */
2931
0
      ret = bind_complete_thread_setup(bind_conf, &err_code);
2932
0
      if (ret != 0) {
2933
0
        cfgerr += ret;
2934
0
        if (err_code & ERR_FATAL)
2935
0
          goto out;
2936
0
      }
2937
0
    }
2938
2939
0
    switch (curproxy->mode) {
2940
0
    case PR_MODE_TCP:
2941
0
      cfgerr += proxy_cfg_ensure_no_http(curproxy);
2942
0
      cfgerr += proxy_cfg_ensure_no_log(curproxy);
2943
0
      break;
2944
2945
0
    case PR_MODE_HTTP:
2946
0
      cfgerr += proxy_cfg_ensure_no_log(curproxy);
2947
0
      curproxy->http_needed = 1;
2948
0
      break;
2949
2950
0
    case PR_MODE_CLI:
2951
0
      cfgerr += proxy_cfg_ensure_no_http(curproxy);
2952
0
      cfgerr += proxy_cfg_ensure_no_log(curproxy);
2953
0
      break;
2954
2955
0
    case PR_MODE_SYSLOG:
2956
      /* this mode is initialized as the classic tcp proxy */
2957
0
      cfgerr += proxy_cfg_ensure_no_http(curproxy);
2958
0
      break;
2959
2960
0
    case PR_MODE_PEERS:
2961
0
    case PR_MODES:
2962
      /* should not happen, bug gcc warn missing switch statement */
2963
0
      ha_alert("%s '%s' cannot initialize this proxy mode (peers) in this way. NOTE: PLEASE REPORT THIS TO DEVELOPERS AS YOU'RE NOT SUPPOSED TO BE ABLE TO CREATE A CONFIGURATION TRIGGERING THIS!\n",
2964
0
         proxy_type_str(curproxy), curproxy->id);
2965
0
      cfgerr++;
2966
0
      break;
2967
0
    }
2968
2969
0
    if (!(curproxy->cap & PR_CAP_INT) && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
2970
0
      ha_warning("%s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
2971
0
           proxy_type_str(curproxy), curproxy->id);
2972
0
      err_code |= ERR_WARN;
2973
0
    }
2974
2975
0
    if (curproxy->cap & PR_CAP_BE) {
2976
0
      if (curproxy->lbprm.algo & BE_LB_KIND) {
2977
0
        if (curproxy->options & PR_O_TRANSP) {
2978
0
          ha_alert("%s '%s' cannot use both transparent and balance mode.\n",
2979
0
             proxy_type_str(curproxy), curproxy->id);
2980
0
          cfgerr++;
2981
0
        }
2982
#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2983
        else if (curproxy->srv == NULL) {
2984
          ha_alert("%s '%s' needs at least 1 server in balance mode.\n",
2985
             proxy_type_str(curproxy), curproxy->id);
2986
          cfgerr++;
2987
        }
2988
#endif
2989
0
        else if (curproxy->options & PR_O_DISPATCH) {
2990
0
          ha_warning("dispatch address of %s '%s' will be ignored in balance mode.\n",
2991
0
               proxy_type_str(curproxy), curproxy->id);
2992
0
          err_code |= ERR_WARN;
2993
0
        }
2994
0
      }
2995
0
      else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH))) {
2996
        /* If no LB algo is set in a backend, and we're not in
2997
         * transparent mode, dispatch mode nor proxy mode, we
2998
         * want to use balance roundrobin by default.
2999
         */
3000
0
        curproxy->lbprm.algo &= ~BE_LB_ALGO;
3001
0
        curproxy->lbprm.algo |= BE_LB_ALGO_RR;
3002
0
      }
3003
0
    }
3004
3005
0
    if (curproxy->options & PR_O_DISPATCH)
3006
0
      curproxy->options &= ~PR_O_TRANSP;
3007
0
    else if (curproxy->options & PR_O_TRANSP)
3008
0
      curproxy->options &= ~PR_O_DISPATCH;
3009
3010
0
    if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_HTTP_RS)) {
3011
0
      ha_warning("%s '%s' uses http-check rules without 'option httpchk', so the rules are ignored.\n",
3012
0
           proxy_type_str(curproxy), curproxy->id);
3013
0
      err_code |= ERR_WARN;
3014
0
    }
3015
3016
0
    if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
3017
0
        (curproxy->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_HTTP_CHK) {
3018
0
      if (curproxy->options & PR_O_DISABLE404) {
3019
0
        ha_warning("'%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
3020
0
             "disable-on-404", proxy_type_str(curproxy), curproxy->id);
3021
0
        err_code |= ERR_WARN;
3022
0
        curproxy->options &= ~PR_O_DISABLE404;
3023
0
      }
3024
0
      if (curproxy->options2 & PR_O2_CHK_SNDST) {
3025
0
        ha_warning("'%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
3026
0
             "send-state", proxy_type_str(curproxy), curproxy->id);
3027
0
        err_code |= ERR_WARN;
3028
0
        curproxy->options &= ~PR_O2_CHK_SNDST;
3029
0
      }
3030
0
    }
3031
3032
0
    if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
3033
0
      if (!global.external_check) {
3034
0
        ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
3035
0
           curproxy->id, "option external-check");
3036
0
        cfgerr++;
3037
0
      }
3038
0
      if (!curproxy->check_command) {
3039
0
        ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
3040
0
           curproxy->id, "option external-check");
3041
0
        cfgerr++;
3042
0
      }
3043
0
      if (!(global.tune.options & GTUNE_INSECURE_FORK)) {
3044
0
        ha_warning("Proxy '%s' : 'insecure-fork-wanted' not enabled in the global section, '%s' will likely fail.\n",
3045
0
           curproxy->id, "option external-check");
3046
0
        err_code |= ERR_WARN;
3047
0
      }
3048
0
    }
3049
3050
0
    if (curproxy->email_alert.set) {
3051
0
        if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
3052
0
          ha_warning("'email-alert' will be ignored for %s '%s' (the presence any of "
3053
0
               "'email-alert from', 'email-alert level' 'email-alert mailers', "
3054
0
               "'email-alert myhostname', or 'email-alert to' "
3055
0
               "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
3056
0
               "to be present).\n",
3057
0
               proxy_type_str(curproxy), curproxy->id);
3058
0
          err_code |= ERR_WARN;
3059
0
          free_email_alert(curproxy);
3060
0
        }
3061
0
        if (!curproxy->email_alert.myhostname)
3062
0
          curproxy->email_alert.myhostname = strdup(hostname);
3063
0
    }
3064
3065
0
    if (curproxy->check_command) {
3066
0
      int clear = 0;
3067
0
      if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
3068
0
        ha_warning("'%s' will be ignored for %s '%s' (requires 'option external-check').\n",
3069
0
             "external-check command", proxy_type_str(curproxy), curproxy->id);
3070
0
        err_code |= ERR_WARN;
3071
0
        clear = 1;
3072
0
      }
3073
0
      if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
3074
0
        ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
3075
0
           curproxy->id, "external-check command");
3076
0
        cfgerr++;
3077
0
      }
3078
0
      if (clear) {
3079
0
        ha_free(&curproxy->check_command);
3080
0
      }
3081
0
    }
3082
3083
0
    if (curproxy->check_path) {
3084
0
      if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
3085
0
        ha_warning("'%s' will be ignored for %s '%s' (requires 'option external-check').\n",
3086
0
             "external-check path", proxy_type_str(curproxy), curproxy->id);
3087
0
        err_code |= ERR_WARN;
3088
0
        ha_free(&curproxy->check_path);
3089
0
      }
3090
0
    }
3091
3092
    /* if a default backend was specified, let's find it */
3093
0
    if (curproxy->defbe.name) {
3094
0
      struct proxy *target;
3095
3096
0
      target = proxy_be_by_name(curproxy->defbe.name);
3097
0
      if (!target) {
3098
0
        ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
3099
0
           curproxy->id, curproxy->defbe.name);
3100
0
        cfgerr++;
3101
0
      } else if (target == curproxy) {
3102
0
        ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
3103
0
           curproxy->id, curproxy->defbe.name);
3104
0
        cfgerr++;
3105
0
      } else if (target->mode != curproxy->mode &&
3106
0
           !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
3107
3108
0
        ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
3109
0
           proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
3110
0
           curproxy->conf.file, curproxy->conf.line,
3111
0
           proxy_mode_str(target->mode), proxy_type_str(target), target->id,
3112
0
           target->conf.file, target->conf.line);
3113
0
        cfgerr++;
3114
0
      } else {
3115
0
        free(curproxy->defbe.name);
3116
0
        curproxy->defbe.be = target;
3117
        /* Emit a warning if this proxy also has some servers */
3118
0
        if (curproxy->srv) {
3119
0
          ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
3120
0
               curproxy->id);
3121
0
          err_code |= ERR_WARN;
3122
0
        }
3123
0
      }
3124
0
    }
3125
3126
    /* find the target proxy for 'use_backend' rules */
3127
0
    list_for_each_entry(rule, &curproxy->switching_rules, list) {
3128
0
      struct proxy *target;
3129
0
      struct logformat_node *node;
3130
0
      char *pxname;
3131
3132
      /* Try to parse the string as a log format expression. If the result
3133
       * of the parsing is only one entry containing a simple string, then
3134
       * it's a standard string corresponding to a static rule, thus the
3135
       * parsing is cancelled and be.name is restored to be resolved.
3136
       */
3137
0
      pxname = rule->be.name;
3138
0
      LIST_INIT(&rule->be.expr);
3139
0
      curproxy->conf.args.ctx = ARGC_UBK;
3140
0
      curproxy->conf.args.file = rule->file;
3141
0
      curproxy->conf.args.line = rule->line;
3142
0
      err = NULL;
3143
0
      if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
3144
0
        ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
3145
0
           rule->file, rule->line, pxname, err);
3146
0
        free(err);
3147
0
        cfgerr++;
3148
0
        continue;
3149
0
      }
3150
0
      node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
3151
3152
0
      if (!LIST_ISEMPTY(&rule->be.expr)) {
3153
0
        if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
3154
0
          rule->dynamic = 1;
3155
0
          free(pxname);
3156
0
          continue;
3157
0
        }
3158
        /* Only one element in the list, a simple string: free the expression and
3159
         * fall back to static rule
3160
         */
3161
0
        LIST_DELETE(&node->list);
3162
0
        free(node->arg);
3163
0
        free(node);
3164
0
      }
3165
3166
0
      rule->dynamic = 0;
3167
0
      rule->be.name = pxname;
3168
3169
0
      target = proxy_be_by_name(rule->be.name);
3170
0
      if (!target) {
3171
0
        ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
3172
0
           curproxy->id, rule->be.name);
3173
0
        cfgerr++;
3174
0
      } else if (target == curproxy) {
3175
0
        ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
3176
0
           curproxy->id, rule->be.name);
3177
0
        cfgerr++;
3178
0
      } else if (target->mode != curproxy->mode &&
3179
0
           !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
3180
3181
0
        ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
3182
0
           proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
3183
0
           curproxy->conf.file, curproxy->conf.line,
3184
0
           proxy_mode_str(target->mode), proxy_type_str(target), target->id,
3185
0
           target->conf.file, target->conf.line);
3186
0
        cfgerr++;
3187
0
      } else {
3188
0
        ha_free(&rule->be.name);
3189
0
        rule->be.backend = target;
3190
0
      }
3191
0
      err_code |= warnif_tcp_http_cond(curproxy, rule->cond);
3192
0
    }
3193
3194
    /* find the target server for 'use_server' rules */
3195
0
    list_for_each_entry(srule, &curproxy->server_rules, list) {
3196
0
      struct server *target;
3197
0
      struct logformat_node *node;
3198
0
      char *server_name;
3199
3200
      /* We try to parse the string as a log format expression. If the result of the parsing
3201
       * is only one entry containing a single string, then it's a standard string corresponding
3202
       * to a static rule, thus the parsing is cancelled and we fall back to setting srv.ptr.
3203
       */
3204
0
      server_name = srule->srv.name;
3205
0
      LIST_INIT(&srule->expr);
3206
0
      curproxy->conf.args.ctx = ARGC_USRV;
3207
0
      err = NULL;
3208
0
      if (!parse_logformat_string(server_name, curproxy, &srule->expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
3209
0
        ha_alert("Parsing [%s:%d]; use-server rule failed to parse log-format '%s' : %s.\n",
3210
0
            srule->file, srule->line, server_name, err);
3211
0
        free(err);
3212
0
        cfgerr++;
3213
0
        continue;
3214
0
      }
3215
0
      node = LIST_NEXT(&srule->expr, struct logformat_node *, list);
3216
3217
0
      if (!LIST_ISEMPTY(&srule->expr)) {
3218
0
        if (node->type != LOG_FMT_TEXT || node->list.n != &srule->expr) {
3219
0
          srule->dynamic = 1;
3220
0
          free(server_name);
3221
0
          continue;
3222
0
        }
3223
        /* Only one element in the list, a simple string: free the expression and
3224
         * fall back to static rule
3225
         */
3226
0
        LIST_DELETE(&node->list);
3227
0
        free(node->arg);
3228
0
        free(node);
3229
0
      }
3230
3231
0
      srule->dynamic = 0;
3232
0
      srule->srv.name = server_name;
3233
0
      target = findserver(curproxy, srule->srv.name);
3234
0
      err_code |= warnif_tcp_http_cond(curproxy, srule->cond);
3235
3236
0
      if (!target) {
3237
0
        ha_alert("%s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
3238
0
           proxy_type_str(curproxy), curproxy->id, srule->srv.name);
3239
0
        cfgerr++;
3240
0
        continue;
3241
0
      }
3242
0
      ha_free(&srule->srv.name);
3243
0
      srule->srv.ptr = target;
3244
0
      target->flags |= SRV_F_NON_PURGEABLE;
3245
0
    }
3246
3247
    /* find the target table for 'stick' rules */
3248
0
    list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
3249
0
      curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
3250
0
      if (mrule->flags & STK_IS_STORE)
3251
0
        curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
3252
3253
0
      if (!resolve_stick_rule(curproxy, mrule))
3254
0
        cfgerr++;
3255
3256
0
      err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
3257
0
    }
3258
3259
    /* find the target table for 'store response' rules */
3260
0
    list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
3261
0
      curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
3262
3263
0
      if (!resolve_stick_rule(curproxy, mrule))
3264
0
        cfgerr++;
3265
0
    }
3266
3267
    /* check validity for 'tcp-request' layer 4/5/6/7 rules */
3268
0
    cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
3269
0
    cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
3270
0
    cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
3271
0
    cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
3272
0
    cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
3273
0
    cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
3274
0
    cfgerr += check_action_rules(&curproxy->http_after_res_rules, curproxy, &err_code);
3275
3276
    /* Warn is a switch-mode http is used on a TCP listener with servers but no backend */
3277
0
    if (!curproxy->defbe.name && LIST_ISEMPTY(&curproxy->switching_rules) && curproxy->srv) {
3278
0
      if ((curproxy->options & PR_O_HTTP_UPG) && curproxy->mode == PR_MODE_TCP)
3279
0
        ha_warning("Proxy '%s' : 'switch-mode http' configured for a %s %s with no backend. "
3280
0
             "Incoming connections upgraded to HTTP cannot be routed to TCP servers\n",
3281
0
             curproxy->id, proxy_mode_str(curproxy->mode), proxy_type_str(curproxy));
3282
0
    }
3283
3284
0
    if (curproxy->table && curproxy->table->peers.name) {
3285
0
      struct peers *curpeers;
3286
3287
0
      for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
3288
0
        if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
3289
0
          ha_free(&curproxy->table->peers.name);
3290
0
          curproxy->table->peers.p = curpeers;
3291
0
          break;
3292
0
        }
3293
0
      }
3294
3295
0
      if (!curpeers) {
3296
0
        ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
3297
0
           curproxy->id, curproxy->table->peers.name);
3298
0
        ha_free(&curproxy->table->peers.name);
3299
0
        curproxy->table->peers.p = NULL;
3300
0
        cfgerr++;
3301
0
      }
3302
0
      else if (curpeers->disabled) {
3303
        /* silently disable this peers section */
3304
0
        curproxy->table->peers.p = NULL;
3305
0
      }
3306
0
      else if (!curpeers->peers_fe) {
3307
0
        ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
3308
0
           curproxy->id, localpeer, curpeers->id);
3309
0
        curproxy->table->peers.p = NULL;
3310
0
        cfgerr++;
3311
0
      }
3312
0
    }
3313
3314
3315
0
    if (curproxy->email_alert.mailers.name) {
3316
0
      struct mailers *curmailers = mailers;
3317
3318
0
      for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
3319
0
        if (strcmp(curmailers->id, curproxy->email_alert.mailers.name) == 0)
3320
0
          break;
3321
0
      }
3322
0
      if (!curmailers) {
3323
0
        ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
3324
0
           curproxy->id, curproxy->email_alert.mailers.name);
3325
0
        free_email_alert(curproxy);
3326
0
        cfgerr++;
3327
0
      }
3328
0
      else {
3329
0
        err = NULL;
3330
0
        if (init_email_alert(curmailers, curproxy, &err)) {
3331
0
          ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
3332
0
          free(err);
3333
0
          cfgerr++;
3334
0
        }
3335
0
      }
3336
0
    }
3337
3338
0
    if (curproxy->uri_auth && !(curproxy->uri_auth->flags & STAT_CONVDONE) &&
3339
0
        !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
3340
0
        (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
3341
0
      ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
3342
0
         "proxy", curproxy->id);
3343
0
      cfgerr++;
3344
0
      goto out_uri_auth_compat;
3345
0
    }
3346
3347
0
    if (curproxy->uri_auth && curproxy->uri_auth->userlist &&
3348
0
        (!(curproxy->uri_auth->flags & STAT_CONVDONE) ||
3349
0
         LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules))) {
3350
0
      const char *uri_auth_compat_req[10];
3351
0
      struct act_rule *rule;
3352
0
      i = 0;
3353
3354
      /* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
3355
0
      uri_auth_compat_req[i++] = "auth";
3356
3357
0
      if (curproxy->uri_auth->auth_realm) {
3358
0
        uri_auth_compat_req[i++] = "realm";
3359
0
        uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
3360
0
      }
3361
3362
0
      uri_auth_compat_req[i++] = "unless";
3363
0
      uri_auth_compat_req[i++] = "{";
3364
0
      uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
3365
0
      uri_auth_compat_req[i++] = "}";
3366
0
      uri_auth_compat_req[i++] = "";
3367
3368
0
      rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
3369
0
      if (!rule) {
3370
0
        cfgerr++;
3371
0
        break;
3372
0
      }
3373
3374
0
      LIST_APPEND(&curproxy->uri_auth->http_req_rules, &rule->list);
3375
3376
0
      if (curproxy->uri_auth->auth_realm) {
3377
0
        ha_free(&curproxy->uri_auth->auth_realm);
3378
0
      }
3379
0
      curproxy->uri_auth->flags |= STAT_CONVDONE;
3380
0
    }
3381
0
out_uri_auth_compat:
3382
3383
    /* check whether we have a logger that uses RFC5424 log format */
3384
0
    list_for_each_entry(tmplogger, &curproxy->loggers, list) {
3385
0
      if (tmplogger->format == LOG_FORMAT_RFC5424) {
3386
0
        if (!curproxy->conf.logformat_sd_string) {
3387
          /* set the default logformat_sd_string */
3388
0
          curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
3389
0
        }
3390
0
        break;
3391
0
      }
3392
0
    }
3393
3394
    /* compile the log format */
3395
0
    if (!(curproxy->cap & PR_CAP_FE)) {
3396
0
      if (curproxy->conf.logformat_string != default_http_log_format &&
3397
0
          curproxy->conf.logformat_string != default_tcp_log_format &&
3398
0
          curproxy->conf.logformat_string != clf_http_log_format)
3399
0
        free(curproxy->conf.logformat_string);
3400
0
      curproxy->conf.logformat_string = NULL;
3401
0
      ha_free(&curproxy->conf.lfs_file);
3402
0
      curproxy->conf.lfs_line = 0;
3403
3404
0
      if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
3405
0
        free(curproxy->conf.logformat_sd_string);
3406
0
      curproxy->conf.logformat_sd_string = NULL;
3407
0
      ha_free(&curproxy->conf.lfsd_file);
3408
0
      curproxy->conf.lfsd_line = 0;
3409
0
    }
3410
3411
0
    if (curproxy->conf.logformat_string) {
3412
0
      curproxy->conf.args.ctx = ARGC_LOG;
3413
0
      curproxy->conf.args.file = curproxy->conf.lfs_file;
3414
0
      curproxy->conf.args.line = curproxy->conf.lfs_line;
3415
0
      err = NULL;
3416
0
      if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
3417
0
                                  LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
3418
0
                                  SMP_VAL_FE_LOG_END, &err)) {
3419
0
        ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
3420
0
           curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
3421
0
        free(err);
3422
0
        cfgerr++;
3423
0
      }
3424
0
      curproxy->conf.args.file = NULL;
3425
0
      curproxy->conf.args.line = 0;
3426
0
    }
3427
3428
0
    if (curproxy->conf.logformat_sd_string) {
3429
0
      curproxy->conf.args.ctx = ARGC_LOGSD;
3430
0
      curproxy->conf.args.file = curproxy->conf.lfsd_file;
3431
0
      curproxy->conf.args.line = curproxy->conf.lfsd_line;
3432
0
      err = NULL;
3433
0
      if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd,
3434
0
                                  LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
3435
0
                                  SMP_VAL_FE_LOG_END, &err)) {
3436
0
        ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3437
0
           curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
3438
0
        free(err);
3439
0
        cfgerr++;
3440
0
      } else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
3441
0
        ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3442
0
           curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
3443
0
        free(err);
3444
0
        cfgerr++;
3445
0
      }
3446
0
      curproxy->conf.args.file = NULL;
3447
0
      curproxy->conf.args.line = 0;
3448
0
    }
3449
3450
0
    if (curproxy->conf.uniqueid_format_string) {
3451
0
      int where = 0;
3452
3453
0
      curproxy->conf.args.ctx = ARGC_UIF;
3454
0
      curproxy->conf.args.file = curproxy->conf.uif_file;
3455
0
      curproxy->conf.args.line = curproxy->conf.uif_line;
3456
0
      err = NULL;
3457
0
      if (curproxy->cap & PR_CAP_FE)
3458
0
        where |= SMP_VAL_FE_HRQ_HDR;
3459
0
      if (curproxy->cap & PR_CAP_BE)
3460
0
        where |= SMP_VAL_BE_HRQ_HDR;
3461
0
      if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id,
3462
0
                                  LOG_OPT_HTTP|LOG_OPT_MERGE_SPACES, where, &err)) {
3463
0
        ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
3464
0
           curproxy->conf.uif_file, curproxy->conf.uif_line, err);
3465
0
        free(err);
3466
0
        cfgerr++;
3467
0
      }
3468
0
      curproxy->conf.args.file = NULL;
3469
0
      curproxy->conf.args.line = 0;
3470
0
    }
3471
3472
0
    if (curproxy->conf.error_logformat_string) {
3473
0
      curproxy->conf.args.ctx = ARGC_LOG;
3474
0
      curproxy->conf.args.file = curproxy->conf.elfs_file;
3475
0
      curproxy->conf.args.line = curproxy->conf.elfs_line;
3476
0
      err = NULL;
3477
0
      if (!parse_logformat_string(curproxy->conf.error_logformat_string, curproxy, &curproxy->logformat_error,
3478
0
                                  LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
3479
0
                                  SMP_VAL_FE_LOG_END, &err)) {
3480
0
        ha_alert("Parsing [%s:%d]: failed to parse error-log-format : %s.\n",
3481
0
           curproxy->conf.elfs_file, curproxy->conf.elfs_line, err);
3482
0
        free(err);
3483
0
        cfgerr++;
3484
0
      }
3485
0
      curproxy->conf.args.file = NULL;
3486
0
      curproxy->conf.args.line = 0;
3487
0
    }
3488
3489
    /* "balance hash" needs to compile its expression
3490
     * (log backends will handle this in proxy log postcheck)
3491
     */
3492
0
    if (curproxy->mode != PR_MODE_SYSLOG &&
3493
0
        (curproxy->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_SMP) {
3494
0
      int idx = 0;
3495
0
      const char *args[] = {
3496
0
        curproxy->lbprm.arg_str,
3497
0
        NULL,
3498
0
      };
3499
3500
0
      err = NULL;
3501
0
      curproxy->conf.args.ctx = ARGC_USRV; // same context as use_server.
3502
0
      curproxy->lbprm.expr =
3503
0
        sample_parse_expr((char **)args, &idx,
3504
0
              curproxy->conf.file, curproxy->conf.line,
3505
0
              &err, &curproxy->conf.args, NULL);
3506
3507
0
      if (!curproxy->lbprm.expr) {
3508
0
        ha_alert("%s '%s' [%s:%d]: failed to parse 'balance hash' expression '%s' in : %s.\n",
3509
0
           proxy_type_str(curproxy), curproxy->id,
3510
0
           curproxy->conf.file, curproxy->conf.line,
3511
0
           curproxy->lbprm.arg_str, err);
3512
0
        ha_free(&err);
3513
0
        cfgerr++;
3514
0
      }
3515
0
      else if (!(curproxy->lbprm.expr->fetch->val & SMP_VAL_BE_SET_SRV)) {
3516
0
        ha_alert("%s '%s' [%s:%d]: error detected while parsing 'balance hash' expression '%s' "
3517
0
           "which requires information from %s, which is not available here.\n",
3518
0
           proxy_type_str(curproxy), curproxy->id,
3519
0
           curproxy->conf.file, curproxy->conf.line,
3520
0
           curproxy->lbprm.arg_str, sample_src_names(curproxy->lbprm.expr->fetch->use));
3521
0
        cfgerr++;
3522
0
      }
3523
0
      else if (curproxy->mode == PR_MODE_HTTP && (curproxy->lbprm.expr->fetch->use & SMP_USE_L6REQ)) {
3524
0
        ha_warning("%s '%s' [%s:%d]: L6 sample fetch <%s> will be ignored in 'balance hash' expression in HTTP mode.\n",
3525
0
             proxy_type_str(curproxy), curproxy->id,
3526
0
             curproxy->conf.file, curproxy->conf.line,
3527
0
             curproxy->lbprm.arg_str);
3528
0
      }
3529
0
      else
3530
0
        curproxy->http_needed |= !!(curproxy->lbprm.expr->fetch->use & SMP_USE_HTTP_ANY);
3531
0
    }
3532
3533
    /* only now we can check if some args remain unresolved.
3534
     * This must be done after the users and groups resolution.
3535
     */
3536
0
    err = NULL;
3537
0
    i = smp_resolve_args(curproxy, &err);
3538
0
    cfgerr += i;
3539
0
    if (i) {
3540
0
      indent_msg(&err, 8);
3541
0
      ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
3542
0
      ha_free(&err);
3543
0
    } else
3544
0
      cfgerr += acl_find_targets(curproxy);
3545
3546
0
    if (!(curproxy->cap & PR_CAP_INT) && (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
3547
0
        (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
3548
0
         ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
3549
0
          (!curproxy->timeout.connect ||
3550
0
           (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
3551
0
      ha_warning("missing timeouts for %s '%s'.\n"
3552
0
           "   | While not properly invalid, you will certainly encounter various problems\n"
3553
0
           "   | with such a configuration. To fix this, please ensure that all following\n"
3554
0
           "   | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
3555
0
           proxy_type_str(curproxy), curproxy->id);
3556
0
      err_code |= ERR_WARN;
3557
0
    }
3558
3559
    /* Historically, the tarpit and queue timeouts were inherited from contimeout.
3560
     * We must still support older configurations, so let's find out whether those
3561
     * parameters have been set or must be copied from contimeouts.
3562
     */
3563
0
    if (!curproxy->timeout.tarpit)
3564
0
      curproxy->timeout.tarpit = curproxy->timeout.connect;
3565
0
    if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
3566
0
      curproxy->timeout.queue = curproxy->timeout.connect;
3567
3568
0
    if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_TCP_RS)) {
3569
0
      ha_warning("%s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
3570
0
           proxy_type_str(curproxy), curproxy->id);
3571
0
      err_code |= ERR_WARN;
3572
0
    }
3573
3574
    /* ensure that cookie capture length is not too large */
3575
0
    if (curproxy->capture_len >= global.tune.cookie_len) {
3576
0
      ha_warning("truncating capture length to %d bytes for %s '%s'.\n",
3577
0
           global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
3578
0
      err_code |= ERR_WARN;
3579
0
      curproxy->capture_len = global.tune.cookie_len - 1;
3580
0
    }
3581
3582
    /* The small pools required for the capture lists */
3583
0
    if (curproxy->nb_req_cap) {
3584
0
      curproxy->req_cap_pool = create_pool("ptrcap",
3585
0
                                           curproxy->nb_req_cap * sizeof(char *),
3586
0
                                           MEM_F_SHARED);
3587
0
    }
3588
3589
0
    if (curproxy->nb_rsp_cap) {
3590
0
      curproxy->rsp_cap_pool = create_pool("ptrcap",
3591
0
                                           curproxy->nb_rsp_cap * sizeof(char *),
3592
0
                                           MEM_F_SHARED);
3593
0
    }
3594
3595
0
    switch (curproxy->load_server_state_from_file) {
3596
0
      case PR_SRV_STATE_FILE_UNSPEC:
3597
0
        curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
3598
0
        break;
3599
0
      case PR_SRV_STATE_FILE_GLOBAL:
3600
0
        if (!global.server_state_file) {
3601
0
          ha_warning("backend '%s' configured to load server state file from global section 'server-state-file' directive. Unfortunately, 'server-state-file' is not set!\n",
3602
0
               curproxy->id);
3603
0
          err_code |= ERR_WARN;
3604
0
        }
3605
0
        break;
3606
0
    }
3607
3608
    /* first, we will invert the servers list order */
3609
0
    newsrv = NULL;
3610
0
    while (curproxy->srv) {
3611
0
      struct server *next;
3612
3613
0
      next = curproxy->srv->next;
3614
0
      curproxy->srv->next = newsrv;
3615
0
      newsrv = curproxy->srv;
3616
0
      if (!next)
3617
0
        break;
3618
0
      curproxy->srv = next;
3619
0
    }
3620
3621
    /* Check that no server name conflicts. This causes trouble in the stats.
3622
     * We only emit a warning for the first conflict affecting each server,
3623
     * in order to avoid combinatory explosion if all servers have the same
3624
     * name. We do that only for servers which do not have an explicit ID,
3625
     * because these IDs were made also for distinguishing them and we don't
3626
     * want to annoy people who correctly manage them.
3627
     */
3628
0
    for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3629
0
      struct server *other_srv;
3630
3631
0
      if (newsrv->puid)
3632
0
        continue;
3633
3634
0
      for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3635
0
        if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
3636
0
          ha_alert("parsing [%s:%d] : %s '%s', another server named '%s' was already defined at line %d, please use distinct names.\n",
3637
0
               newsrv->conf.file, newsrv->conf.line,
3638
0
               proxy_type_str(curproxy), curproxy->id,
3639
0
               newsrv->id, other_srv->conf.line);
3640
0
          cfgerr++;
3641
0
          break;
3642
0
        }
3643
0
      }
3644
0
    }
3645
3646
    /* assign automatic UIDs to servers which don't have one yet */
3647
0
    next_id = 1;
3648
0
    newsrv = curproxy->srv;
3649
0
    while (newsrv != NULL) {
3650
0
      if (!newsrv->puid) {
3651
        /* server ID not set, use automatic numbering with first
3652
         * spare entry starting with next_svid.
3653
         */
3654
0
        next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3655
0
        newsrv->conf.id.key = newsrv->puid = next_id;
3656
0
        eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3657
0
      }
3658
0
      newsrv->conf.name.key = newsrv->id;
3659
0
      ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
3660
3661
0
      next_id++;
3662
0
      newsrv = newsrv->next;
3663
0
    }
3664
3665
0
    curproxy->lbprm.wmult = 1; /* default weight multiplier */
3666
0
    curproxy->lbprm.wdiv  = 1; /* default weight divider */
3667
3668
    /*
3669
     * If this server supports a maxconn parameter, it needs a dedicated
3670
     * tasks to fill the emptied slots when a connection leaves.
3671
     * Also, resolve deferred tracking dependency if needed.
3672
     */
3673
0
    newsrv = curproxy->srv;
3674
0
    while (newsrv != NULL) {
3675
0
      set_usermsgs_ctx(newsrv->conf.file, newsrv->conf.line, &newsrv->obj_type);
3676
3677
0
      srv_minmax_conn_apply(newsrv);
3678
3679
      /* this will also properly set the transport layer for
3680
       * prod and checks
3681
       * if default-server have use_ssl, prerare ssl init
3682
       * without activating it */
3683
0
      if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 ||
3684
0
          (newsrv->proxy->options & PR_O_TCPCHK_SSL) ||
3685
0
          ((newsrv->flags & SRV_F_DEFSRV_USE_SSL) && newsrv->use_ssl != 1)) {
3686
0
        if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3687
0
          cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3688
0
      }
3689
3690
0
      if ((newsrv->flags & SRV_F_FASTOPEN) &&
3691
0
          ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3692
0
           (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
3693
0
        ha_warning("server has tfo activated, the backend should be configured with at least 'conn-failure', 'empty-response' and 'response-timeout' or we wouldn't be able to retry the connection on failure.\n");
3694
3695
0
      if (newsrv->trackit) {
3696
0
        if (srv_apply_track(newsrv, curproxy)) {
3697
0
          ++cfgerr;
3698
0
          goto next_srv;
3699
0
        }
3700
0
      }
3701
3702
0
    next_srv:
3703
0
      reset_usermsgs_ctx();
3704
0
      newsrv = newsrv->next;
3705
0
    }
3706
3707
    /*
3708
     * Try to generate dynamic cookies for servers now.
3709
     * It couldn't be done earlier, since at the time we parsed
3710
     * the server line, we may not have known yet that we
3711
     * should use dynamic cookies, or the secret key may not
3712
     * have been provided yet.
3713
     */
3714
0
    if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3715
0
      newsrv = curproxy->srv;
3716
0
      while (newsrv != NULL) {
3717
0
        srv_set_dyncookie(newsrv);
3718
0
        newsrv = newsrv->next;
3719
0
      }
3720
3721
0
    }
3722
    /* We have to initialize the server lookup mechanism depending
3723
     * on what LB algorithm was chosen.
3724
     */
3725
3726
0
    if (curproxy->mode == PR_MODE_SYSLOG) {
3727
      /* log load-balancing requires special init that is performed
3728
       * during log-postparsing step
3729
       */
3730
0
      goto skip_server_lb_init;
3731
0
    }
3732
0
    curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3733
0
    switch (curproxy->lbprm.algo & BE_LB_KIND) {
3734
0
    case BE_LB_KIND_RR:
3735
0
      if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3736
0
        curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3737
0
        init_server_map(curproxy);
3738
0
      } else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3739
0
        curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3740
0
        if (chash_init_server_tree(curproxy) < 0) {
3741
0
          cfgerr++;
3742
0
        }
3743
0
      } else {
3744
0
        curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3745
0
        fwrr_init_server_groups(curproxy);
3746
0
      }
3747
0
      break;
3748
3749
0
    case BE_LB_KIND_CB:
3750
0
      if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3751
0
        curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3752
0
        fwlc_init_server_tree(curproxy);
3753
0
      } else {
3754
0
        curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3755
0
        fas_init_server_tree(curproxy);
3756
0
      }
3757
0
      break;
3758
3759
0
    case BE_LB_KIND_HI:
3760
0
      if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3761
0
        curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3762
0
        if (chash_init_server_tree(curproxy) < 0) {
3763
0
          cfgerr++;
3764
0
        }
3765
0
      } else {
3766
0
        curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3767
0
        init_server_map(curproxy);
3768
0
      }
3769
0
      break;
3770
0
    }
3771
0
 skip_server_lb_init:
3772
0
    HA_RWLOCK_INIT(&curproxy->lbprm.lock);
3773
3774
0
    if (curproxy->options & PR_O_LOGASAP)
3775
0
      curproxy->to_log &= ~LW_BYTES;
3776
3777
0
    if (!(curproxy->cap & PR_CAP_INT) && (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
3778
0
        (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->loggers) &&
3779
0
        (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
3780
0
      ha_warning("log format ignored for %s '%s' since it has no log address.\n",
3781
0
           proxy_type_str(curproxy), curproxy->id);
3782
0
      err_code |= ERR_WARN;
3783
0
    }
3784
3785
0
    if (curproxy->mode != PR_MODE_HTTP && !(curproxy->options & PR_O_HTTP_UPG)) {
3786
0
      int optnum;
3787
3788
0
      if (curproxy->uri_auth) {
3789
0
        ha_warning("'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
3790
0
             proxy_type_str(curproxy), curproxy->id);
3791
0
        err_code |= ERR_WARN;
3792
0
        curproxy->uri_auth = NULL;
3793
0
      }
3794
3795
0
      if (curproxy->capture_name) {
3796
0
        ha_warning("'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
3797
0
             proxy_type_str(curproxy), curproxy->id);
3798
0
        err_code |= ERR_WARN;
3799
0
      }
3800
3801
0
      if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
3802
0
        ha_warning("'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
3803
0
             proxy_type_str(curproxy), curproxy->id);
3804
0
        err_code |= ERR_WARN;
3805
0
      }
3806
3807
0
      if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
3808
0
        ha_warning("'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3809
0
             proxy_type_str(curproxy), curproxy->id);
3810
0
        err_code |= ERR_WARN;
3811
0
      }
3812
3813
0
      if (!LIST_ISEMPTY(&curproxy->http_after_res_rules)) {
3814
0
        ha_warning("'http-after-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3815
0
             proxy_type_str(curproxy), curproxy->id);
3816
0
        err_code |= ERR_WARN;
3817
0
      }
3818
3819
0
      if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
3820
0
        ha_warning("'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
3821
0
             proxy_type_str(curproxy), curproxy->id);
3822
0
        err_code |= ERR_WARN;
3823
0
      }
3824
3825
0
      for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3826
0
        if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3827
0
            (curproxy->cap & cfg_opts[optnum].cap) &&
3828
0
            (curproxy->options & cfg_opts[optnum].val)) {
3829
0
          ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3830
0
               cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
3831
0
          err_code |= ERR_WARN;
3832
0
          curproxy->options &= ~cfg_opts[optnum].val;
3833
0
        }
3834
0
      }
3835
3836
0
      for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3837
0
        if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3838
0
            (curproxy->cap & cfg_opts2[optnum].cap) &&
3839
0
            (curproxy->options2 & cfg_opts2[optnum].val)) {
3840
0
          ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3841
0
               cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
3842
0
          err_code |= ERR_WARN;
3843
0
          curproxy->options2 &= ~cfg_opts2[optnum].val;
3844
0
        }
3845
0
      }
3846
3847
0
#if defined(CONFIG_HAP_TRANSPARENT)
3848
0
      if (curproxy->conn_src.bind_hdr_occ) {
3849
0
        curproxy->conn_src.bind_hdr_occ = 0;
3850
0
        ha_warning("%s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
3851
0
             proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
3852
0
        err_code |= ERR_WARN;
3853
0
      }
3854
0
#endif
3855
0
    }
3856
3857
    /*
3858
     * ensure that we're not cross-dressing a TCP server into HTTP.
3859
     */
3860
0
    newsrv = curproxy->srv;
3861
0
    while (newsrv != NULL) {
3862
0
      if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
3863
0
        ha_alert("%s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
3864
0
           proxy_type_str(curproxy), curproxy->id);
3865
0
        cfgerr++;
3866
0
      }
3867
3868
0
      if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
3869
0
        ha_warning("%s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
3870
0
             proxy_type_str(curproxy), curproxy->id, newsrv->id);
3871
0
        err_code |= ERR_WARN;
3872
0
      }
3873
3874
0
      if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
3875
0
        ha_warning("%s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
3876
0
             proxy_type_str(curproxy), curproxy->id, newsrv->id);
3877
0
        err_code |= ERR_WARN;
3878
0
      }
3879
3880
0
#if defined(CONFIG_HAP_TRANSPARENT)
3881
0
      if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
3882
0
        newsrv->conn_src.bind_hdr_occ = 0;
3883
0
        ha_warning("%s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
3884
0
             proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
3885
0
        err_code |= ERR_WARN;
3886
0
      }
3887
0
#endif
3888
3889
0
      if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
3890
0
        curproxy->options &= ~PR_O_REUSE_MASK;
3891
3892
0
      if ((curproxy->mode != PR_MODE_HTTP) && newsrv->flags & SRV_F_REVERSE) {
3893
0
        ha_alert("%s '%s' : server %s uses reverse addressing which can only be used with HTTP mode.\n",
3894
0
             proxy_type_str(curproxy), curproxy->id, newsrv->id);
3895
0
        cfgerr++;
3896
0
        err_code |= ERR_FATAL | ERR_ALERT;
3897
0
        goto out;
3898
0
      }
3899
3900
0
      newsrv = newsrv->next;
3901
0
    }
3902
3903
    /* Check filter configuration, if any */
3904
0
    cfgerr += flt_check(curproxy);
3905
3906
0
    if (curproxy->cap & PR_CAP_FE) {
3907
0
      if (!curproxy->accept)
3908
0
        curproxy->accept = frontend_accept;
3909
3910
0
      if (!LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules) ||
3911
0
          (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_req.inspect_rules)))
3912
0
        curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
3913
3914
0
      if (curproxy->mode == PR_MODE_HTTP) {
3915
0
        curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
3916
0
        curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
3917
0
      }
3918
3919
0
      if (curproxy->mode == PR_MODE_CLI) {
3920
0
        curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
3921
0
        curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
3922
0
      }
3923
3924
      /* both TCP and HTTP must check switching rules */
3925
0
      curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
3926
3927
      /* Add filters analyzers if needed */
3928
0
      if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
3929
0
        curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3930
0
        curproxy->fe_rsp_ana |= AN_RES_FLT_START_FE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
3931
0
      }
3932
0
    }
3933
3934
0
    if (curproxy->cap & PR_CAP_BE) {
3935
0
      if (!LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules) ||
3936
0
          (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_req.inspect_rules)))
3937
0
        curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
3938
3939
0
      if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules) ||
3940
0
          (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_rep.inspect_rules)))
3941
0
                                curproxy->be_rsp_ana |= AN_RES_INSPECT;
3942
3943
0
      if (curproxy->mode == PR_MODE_HTTP) {
3944
0
        curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
3945
0
        curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
3946
0
      }
3947
3948
      /* If the backend does requires RDP cookie persistence, we have to
3949
       * enable the corresponding analyser.
3950
       */
3951
0
      if (curproxy->options2 & PR_O2_RDPC_PRST)
3952
0
        curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
3953
3954
      /* Add filters analyzers if needed */
3955
0
      if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
3956
0
        curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3957
0
        curproxy->be_rsp_ana |= AN_RES_FLT_START_BE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
3958
0
      }
3959
0
    }
3960
3961
    /* Check the mux protocols, if any, for each listener and server
3962
     * attached to the current proxy */
3963
0
    list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3964
0
      int mode = conn_pr_mode_to_proto_mode(curproxy->mode);
3965
0
      const struct mux_proto_list *mux_ent;
3966
3967
0
      if (!bind_conf->mux_proto) {
3968
        /* No protocol was specified. If we're using QUIC at the transport
3969
         * layer, we'll instantiate it as a mux as well. If QUIC is not
3970
         * compiled in, this will remain NULL.
3971
         */
3972
0
        if (bind_conf->xprt && bind_conf->xprt == xprt_get(XPRT_QUIC))
3973
0
          bind_conf->mux_proto = get_mux_proto(ist("quic"));
3974
0
      }
3975
3976
0
      if (!bind_conf->mux_proto)
3977
0
        continue;
3978
3979
      /* it is possible that an incorrect mux was referenced
3980
       * due to the proxy's mode not being taken into account
3981
       * on first pass. Let's adjust it now.
3982
       */
3983
0
      mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
3984
3985
0
      if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
3986
0
        ha_alert("%s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
3987
0
           proxy_type_str(curproxy), curproxy->id,
3988
0
           (int)bind_conf->mux_proto->token.len,
3989
0
           bind_conf->mux_proto->token.ptr,
3990
0
           bind_conf->arg, bind_conf->file, bind_conf->line);
3991
0
        cfgerr++;
3992
0
      } else {
3993
0
        if ((mux_ent->mux->flags & MX_FL_FRAMED) && !(bind_conf->options & BC_O_USE_SOCK_DGRAM)) {
3994
0
          ha_alert("%s '%s' : frame-based MUX protocol '%.*s' is incompatible with stream transport of 'bind %s' at [%s:%d].\n",
3995
0
             proxy_type_str(curproxy), curproxy->id,
3996
0
             (int)bind_conf->mux_proto->token.len,
3997
0
             bind_conf->mux_proto->token.ptr,
3998
0
             bind_conf->arg, bind_conf->file, bind_conf->line);
3999
0
          cfgerr++;
4000
0
        }
4001
0
        else if (!(mux_ent->mux->flags & MX_FL_FRAMED) && !(bind_conf->options & BC_O_USE_SOCK_STREAM)) {
4002
0
          ha_alert("%s '%s' : stream-based MUX protocol '%.*s' is incompatible with framed transport of 'bind %s' at [%s:%d].\n",
4003
0
             proxy_type_str(curproxy), curproxy->id,
4004
0
             (int)bind_conf->mux_proto->token.len,
4005
0
             bind_conf->mux_proto->token.ptr,
4006
0
             bind_conf->arg, bind_conf->file, bind_conf->line);
4007
0
          cfgerr++;
4008
0
        }
4009
0
      }
4010
4011
      /* update the mux */
4012
0
      bind_conf->mux_proto = mux_ent;
4013
0
    }
4014
0
    for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
4015
0
      int mode = conn_pr_mode_to_proto_mode(curproxy->mode);
4016
0
      const struct mux_proto_list *mux_ent;
4017
4018
0
      if (!newsrv->mux_proto)
4019
0
        continue;
4020
4021
      /* it is possible that an incorrect mux was referenced
4022
       * due to the proxy's mode not being taken into account
4023
       * on first pass. Let's adjust it now.
4024
       */
4025
0
      mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
4026
4027
0
      if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
4028
0
        ha_alert("%s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
4029
0
           proxy_type_str(curproxy), curproxy->id,
4030
0
           (int)newsrv->mux_proto->token.len,
4031
0
           newsrv->mux_proto->token.ptr,
4032
0
           newsrv->id, newsrv->conf.file, newsrv->conf.line);
4033
0
        cfgerr++;
4034
0
      }
4035
4036
      /* update the mux */
4037
0
      newsrv->mux_proto = mux_ent;
4038
0
    }
4039
4040
    /* Allocate default tcp-check rules for proxies without
4041
     * explicit rules.
4042
     */
4043
0
    if (curproxy->cap & PR_CAP_BE) {
4044
0
      if (!(curproxy->options2 & PR_O2_CHK_ANY)) {
4045
0
        struct tcpcheck_ruleset *rs = NULL;
4046
0
        struct tcpcheck_rules *rules = &curproxy->tcpcheck_rules;
4047
4048
0
        curproxy->options2 |= PR_O2_TCPCHK_CHK;
4049
4050
0
        rs = find_tcpcheck_ruleset("*tcp-check");
4051
0
        if (!rs) {
4052
0
          rs = create_tcpcheck_ruleset("*tcp-check");
4053
0
          if (rs == NULL) {
4054
0
            ha_alert("config: %s '%s': out of memory.\n",
4055
0
               proxy_type_str(curproxy), curproxy->id);
4056
0
            cfgerr++;
4057
0
          }
4058
0
        }
4059
4060
0
        free_tcpcheck_vars(&rules->preset_vars);
4061
0
        rules->list = &rs->rules;
4062
0
        rules->flags = 0;
4063
0
      }
4064
0
    }
4065
0
  }
4066
4067
  /*
4068
   * We have just initialized the main proxies list
4069
   * we must also configure the log-forward proxies list
4070
   */
4071
0
  if (init_proxies_list == proxies_list) {
4072
0
    init_proxies_list = cfg_log_forward;
4073
    /* check if list is not null to avoid infinite loop */
4074
0
    if (init_proxies_list)
4075
0
      goto init_proxies_list_stage1;
4076
0
  }
4077
4078
0
  if (init_proxies_list == cfg_log_forward) {
4079
0
    init_proxies_list = sink_proxies_list;
4080
    /* check if list is not null to avoid infinite loop */
4081
0
    if (init_proxies_list)
4082
0
      goto init_proxies_list_stage1;
4083
0
  }
4084
4085
  /***********************************************************/
4086
  /* At this point, target names have already been resolved. */
4087
  /***********************************************************/
4088
4089
  /* we must finish to initialize certain things on the servers */
4090
4091
0
  list_for_each_entry(newsrv, &servers_list, global_list) {
4092
    /* initialize idle conns lists */
4093
0
    if (srv_init_per_thr(newsrv) == -1) {
4094
0
      ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
4095
0
               newsrv->conf.file, newsrv->conf.line, newsrv->id);
4096
0
      cfgerr++;
4097
0
      continue;
4098
0
    }
4099
4100
0
    if (newsrv->max_idle_conns != 0) {
4101
0
      newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
4102
0
      if (!newsrv->curr_idle_thr) {
4103
0
        ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
4104
0
                 newsrv->conf.file, newsrv->conf.line, newsrv->id);
4105
0
        cfgerr++;
4106
0
        continue;
4107
0
      }
4108
4109
0
    }
4110
0
  }
4111
4112
0
  idle_conn_task = task_new_anywhere();
4113
0
  if (!idle_conn_task) {
4114
0
    ha_alert("parsing : failed to allocate global idle connection task.\n");
4115
0
    cfgerr++;
4116
0
  }
4117
0
  else {
4118
0
    idle_conn_task->process = srv_cleanup_idle_conns;
4119
0
    idle_conn_task->context = NULL;
4120
4121
0
    for (i = 0; i < global.nbthread; i++) {
4122
0
      idle_conns[i].cleanup_task = task_new_on(i);
4123
0
      if (!idle_conns[i].cleanup_task) {
4124
0
        ha_alert("parsing : failed to allocate idle connection tasks for thread '%d'.\n", i);
4125
0
        cfgerr++;
4126
0
        break;
4127
0
      }
4128
4129
0
      idle_conns[i].cleanup_task->process = srv_cleanup_toremove_conns;
4130
0
      idle_conns[i].cleanup_task->context = NULL;
4131
0
      HA_SPIN_INIT(&idle_conns[i].idle_conns_lock);
4132
0
      MT_LIST_INIT(&idle_conns[i].toremove_conns);
4133
0
    }
4134
0
  }
4135
4136
  /* perform the final checks before creating tasks */
4137
4138
  /* starting to initialize the main proxies list */
4139
0
  init_proxies_list = proxies_list;
4140
4141
0
init_proxies_list_stage2:
4142
0
  for (curproxy = init_proxies_list; curproxy; curproxy = curproxy->next) {
4143
0
    struct listener *listener;
4144
0
    unsigned int next_id;
4145
4146
    /* Configure SSL for each bind line.
4147
     * Note: if configuration fails at some point, the ->ctx member
4148
     * remains NULL so that listeners can later detach.
4149
     */
4150
0
    list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
4151
0
      if (bind_conf->xprt->prepare_bind_conf &&
4152
0
          bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
4153
0
        cfgerr++;
4154
0
      bind_conf->analysers |= curproxy->fe_req_ana;
4155
0
      if (!bind_conf->maxaccept)
4156
0
        bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
4157
0
      bind_conf->accept = session_accept_fd;
4158
0
      if (curproxy->options & PR_O_TCP_NOLING)
4159
0
        bind_conf->options |= BC_O_NOLINGER;
4160
4161
      /* smart accept mode is automatic in HTTP mode */
4162
0
      if ((curproxy->options2 & PR_O2_SMARTACC) ||
4163
0
          ((curproxy->mode == PR_MODE_HTTP || (bind_conf->options & BC_O_USE_SSL)) &&
4164
0
           !(curproxy->no_options2 & PR_O2_SMARTACC)))
4165
0
        bind_conf->options |= BC_O_NOQUICKACK;
4166
0
    }
4167
4168
    /* adjust this proxy's listeners */
4169
0
    bind_conf = NULL;
4170
0
    next_id = 1;
4171
0
    list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
4172
0
      if (!listener->luid) {
4173
        /* listener ID not set, use automatic numbering with first
4174
         * spare entry starting with next_luid.
4175
         */
4176
0
        next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
4177
0
        listener->conf.id.key = listener->luid = next_id;
4178
0
        eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
4179
0
      }
4180
0
      next_id++;
4181
4182
      /* enable separate counters */
4183
0
      if (curproxy->options2 & PR_O2_SOCKSTAT) {
4184
0
        listener->counters = calloc(1, sizeof(*listener->counters));
4185
0
        if (!listener->name)
4186
0
          memprintf(&listener->name, "sock-%d", listener->luid);
4187
0
      }
4188
4189
#ifdef USE_QUIC
4190
      if (listener->bind_conf->xprt == xprt_get(XPRT_QUIC)) {
4191
        /* quic_conn are counted against maxconn. */
4192
        listener->bind_conf->options |= BC_O_XPRT_MAXCONN;
4193
        listener->rx.quic_curr_handshake = 0;
4194
        listener->rx.quic_curr_accept = 0;
4195
4196
# ifdef USE_QUIC_OPENSSL_COMPAT
4197
        /* store the last checked bind_conf in bind_conf */
4198
        if (!(global.tune.options & GTUNE_NO_QUIC) &&
4199
            !(global.tune.options & GTUNE_LIMITED_QUIC) &&
4200
            listener->bind_conf != bind_conf) {
4201
          bind_conf = listener->bind_conf;
4202
          ha_alert("Binding [%s:%d] for %s %s: this SSL library does not support the "
4203
             "QUIC protocol. A limited compatibility layer may be enabled using "
4204
             "the \"limited-quic\" global option if desired.\n",
4205
             listener->bind_conf->file, listener->bind_conf->line,
4206
             proxy_type_str(curproxy), curproxy->id);
4207
          cfgerr++;
4208
        }
4209
# endif
4210
4211
        li_init_per_thr(listener);
4212
      }
4213
#endif
4214
0
    }
4215
4216
    /* Release unused SSL configs */
4217
0
    list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
4218
0
      if (!(bind_conf->options & BC_O_USE_SSL) && bind_conf->xprt->destroy_bind_conf)
4219
0
        bind_conf->xprt->destroy_bind_conf(bind_conf);
4220
0
    }
4221
4222
    /* create the task associated with the proxy */
4223
0
    curproxy->task = task_new_anywhere();
4224
0
    if (curproxy->task) {
4225
0
      curproxy->task->context = curproxy;
4226
0
      curproxy->task->process = manage_proxy;
4227
0
      curproxy->flags |= PR_FL_READY;
4228
0
    } else {
4229
0
      ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
4230
0
         curproxy->id);
4231
0
      cfgerr++;
4232
0
    }
4233
0
  }
4234
4235
  /*
4236
   * We have just initialized the main proxies list
4237
   * we must also configure the log-forward proxies list
4238
   */
4239
0
  if (init_proxies_list == proxies_list) {
4240
0
    init_proxies_list = cfg_log_forward;
4241
    /* check if list is not null to avoid infinite loop */
4242
0
    if (init_proxies_list)
4243
0
      goto init_proxies_list_stage2;
4244
0
  }
4245
4246
  /*
4247
   * Recount currently required checks.
4248
   */
4249
4250
0
  for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
4251
0
    int optnum;
4252
4253
0
    for (optnum = 0; cfg_opts[optnum].name; optnum++)
4254
0
      if (curproxy->options & cfg_opts[optnum].val)
4255
0
        global.last_checks |= cfg_opts[optnum].checks;
4256
4257
0
    for (optnum = 0; cfg_opts2[optnum].name; optnum++)
4258
0
      if (curproxy->options2 & cfg_opts2[optnum].val)
4259
0
        global.last_checks |= cfg_opts2[optnum].checks;
4260
0
  }
4261
4262
0
  if (cfg_peers) {
4263
0
    struct peers *curpeers = cfg_peers, **last;
4264
0
    struct peer *p, *pb;
4265
4266
    /* Remove all peers sections which don't have a valid listener,
4267
     * which are not used by any table, or which are bound to more
4268
     * than one process.
4269
     */
4270
0
    last = &cfg_peers;
4271
0
    while (*last) {
4272
0
      struct peer *peer;
4273
0
      struct stktable *t;
4274
0
      curpeers = *last;
4275
4276
0
      if (curpeers->disabled) {
4277
        /* the "disabled" keyword was present */
4278
0
        if (curpeers->peers_fe)
4279
0
          stop_proxy(curpeers->peers_fe);
4280
0
        curpeers->peers_fe = NULL;
4281
0
      }
4282
0
      else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
4283
0
        ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
4284
0
             curpeers->id, localpeer);
4285
0
        if (curpeers->peers_fe)
4286
0
          stop_proxy(curpeers->peers_fe);
4287
0
        curpeers->peers_fe = NULL;
4288
0
      }
4289
0
      else {
4290
        /* Initializes the transport layer of the server part of all the peers belonging to
4291
         * <curpeers> section if required.
4292
         * Note that ->srv is used by the local peer of a new process to connect to the local peer
4293
         * of an old process.
4294
         */
4295
0
        curpeers->peers_fe->flags |= PR_FL_READY;
4296
0
        p = curpeers->remote;
4297
0
        while (p) {
4298
0
          struct peer *other_peer;
4299
4300
0
          for (other_peer = curpeers->remote; other_peer && other_peer != p; other_peer = other_peer->next) {
4301
0
            if (strcmp(other_peer->id, p->id) == 0) {
4302
0
              ha_alert("Peer section '%s' [%s:%d]: another peer named '%s' was already defined at line %s:%d, please use distinct names.\n",
4303
0
                 curpeers->peers_fe->id,
4304
0
                 p->conf.file, p->conf.line,
4305
0
                 other_peer->id, other_peer->conf.file, other_peer->conf.line);
4306
0
              cfgerr++;
4307
0
              break;
4308
0
            }
4309
0
          }
4310
4311
0
          if (p->srv) {
4312
0
            if (p->srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
4313
0
              cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
4314
0
          }
4315
0
          p = p->next;
4316
0
        }
4317
        /* Configure the SSL bindings of the local peer if required. */
4318
0
        if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
4319
0
          struct list *l;
4320
0
          struct bind_conf *bind_conf;
4321
0
          int ret;
4322
4323
0
          l = &curpeers->peers_fe->conf.bind;
4324
0
          bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
4325
4326
0
          if (curpeers->local->srv) {
4327
0
            if (curpeers->local->srv->use_ssl == 1 && !(bind_conf->options & BC_O_USE_SSL)) {
4328
0
              ha_warning("Peers section '%s': local peer have a non-SSL listener and a SSL server configured at line %s:%d.\n",
4329
0
                   curpeers->peers_fe->id, curpeers->local->conf.file, curpeers->local->conf.line);
4330
0
            }
4331
0
            else if (curpeers->local->srv->use_ssl != 1 && (bind_conf->options & BC_O_USE_SSL)) {
4332
0
              ha_warning("Peers section '%s': local peer have a SSL listener and a non-SSL server configured at line %s:%d.\n",
4333
0
                   curpeers->peers_fe->id, curpeers->local->conf.file, curpeers->local->conf.line);
4334
0
            }
4335
0
          }
4336
4337
          /* finish the bind setup */
4338
0
          ret = bind_complete_thread_setup(bind_conf, &err_code);
4339
0
          if (ret != 0) {
4340
0
            cfgerr += ret;
4341
0
            if (err_code & ERR_FATAL)
4342
0
              goto out;
4343
0
          }
4344
4345
0
          if (bind_conf->xprt->prepare_bind_conf &&
4346
0
            bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
4347
0
            cfgerr++;
4348
0
        }
4349
0
        if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
4350
0
          ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
4351
0
             curpeers->id);
4352
0
          cfgerr++;
4353
0
          break;
4354
0
        }
4355
0
        last = &curpeers->next;
4356
4357
        /* Ignore the peer shard greater than the number of peer shard for this section.
4358
         * Also ignore the peer shard of the local peer.
4359
         */
4360
0
        for (peer = curpeers->remote; peer; peer = peer->next) {
4361
0
          if (peer == curpeers->local) {
4362
0
            if (peer->srv->shard) {
4363
0
              ha_warning("Peers section '%s': shard ignored for '%s' local peer\n",
4364
0
                     curpeers->id, peer->id);
4365
0
              peer->srv->shard = 0;
4366
0
            }
4367
0
          }
4368
0
          else if (peer->srv->shard > curpeers->nb_shards) {
4369
0
            ha_warning("Peers section '%s': shard ignored for '%s' local peer because "
4370
0
                   "%d shard value is greater than the section number of shards (%d)\n",
4371
0
                   curpeers->id, peer->id, peer->srv->shard, curpeers->nb_shards);
4372
0
            peer->srv->shard = 0;
4373
0
          }
4374
0
        }
4375
4376
0
        continue;
4377
0
      }
4378
4379
      /* clean what has been detected above */
4380
0
      p = curpeers->remote;
4381
0
      while (p) {
4382
0
        pb = p->next;
4383
0
        free(p->id);
4384
0
        free(p);
4385
0
        p = pb;
4386
0
      }
4387
4388
      /* Destroy and unlink this curpeers section.
4389
       * Note: curpeers is backed up into *last.
4390
       */
4391
0
      free(curpeers->id);
4392
0
      curpeers = curpeers->next;
4393
      /* Reset any refereance to this peers section in the list of stick-tables */
4394
0
      for (t = stktables_list; t; t = t->next) {
4395
0
        if (t->peers.p && t->peers.p == *last)
4396
0
          t->peers.p = NULL;
4397
0
      }
4398
0
      free(*last);
4399
0
      *last = curpeers;
4400
0
    }
4401
0
  }
4402
4403
0
  for (t = stktables_list; t; t = t->next) {
4404
0
    if (t->proxy)
4405
0
      continue;
4406
0
    err = NULL;
4407
0
    if (!stktable_init(t, &err)) {
4408
0
      ha_alert("Parsing [%s:%d]: failed to initialize '%s' stick-table: %s.\n", t->conf.file, t->conf.line, t->id, err);
4409
0
      ha_free(&err);
4410
0
      cfgerr++;
4411
0
    }
4412
0
  }
4413
4414
  /* initialize stick-tables on backend capable proxies. This must not
4415
   * be done earlier because the data size may be discovered while parsing
4416
   * other proxies.
4417
   */
4418
0
  for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
4419
0
    if ((curproxy->flags & PR_FL_DISABLED) || !curproxy->table)
4420
0
      continue;
4421
4422
0
    err = NULL;
4423
0
    if (!stktable_init(curproxy->table, &err)) {
4424
0
      ha_alert("Proxy '%s': failed to initialize stick-table: %s.\n", curproxy->id, err);
4425
0
      ha_free(&err);
4426
0
      cfgerr++;
4427
0
    }
4428
0
  }
4429
4430
0
  if (mailers) {
4431
0
    struct mailers *curmailers = mailers, **last;
4432
0
    struct mailer *m, *mb;
4433
4434
    /* Remove all mailers sections which don't have a valid listener.
4435
     * This can happen when a mailers section is never referenced.
4436
     */
4437
0
    last = &mailers;
4438
0
    while (*last) {
4439
0
      curmailers = *last;
4440
0
      if (curmailers->users) {
4441
0
        last = &curmailers->next;
4442
0
        continue;
4443
0
      }
4444
4445
0
      ha_warning("Removing incomplete section 'mailers %s'.\n",
4446
0
           curmailers->id);
4447
4448
0
      m = curmailers->mailer_list;
4449
0
      while (m) {
4450
0
        mb = m->next;
4451
0
        free(m->id);
4452
0
        free(m);
4453
0
        m = mb;
4454
0
      }
4455
4456
      /* Destroy and unlink this curmailers section.
4457
       * Note: curmailers is backed up into *last.
4458
       */
4459
0
      free(curmailers->id);
4460
0
      curmailers = curmailers->next;
4461
0
      free(*last);
4462
0
      *last = curmailers;
4463
0
    }
4464
0
  }
4465
4466
  /* Update server_state_file_name to backend name if backend is supposed to use
4467
   * a server-state file locally defined and none has been provided */
4468
0
  for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
4469
0
    if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
4470
0
        curproxy->server_state_file_name == NULL)
4471
0
      curproxy->server_state_file_name = strdup(curproxy->id);
4472
0
  }
4473
4474
0
  list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
4475
0
    if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
4476
0
      ha_warning("resolvers '%s' [%s:%d] has no nameservers configured!\n",
4477
0
           curr_resolvers->id, curr_resolvers->conf.file,
4478
0
           curr_resolvers->conf.line);
4479
0
      err_code |= ERR_WARN;
4480
0
    }
4481
0
  }
4482
4483
0
  list_for_each_entry(postparser, &postparsers, list) {
4484
0
    if (postparser->func)
4485
0
      cfgerr += postparser->func();
4486
0
  }
4487
4488
0
  if (cfgerr > 0)
4489
0
    err_code |= ERR_ALERT | ERR_FATAL;
4490
0
 out:
4491
0
  return err_code;
4492
0
}
4493
4494
/*
4495
 * Registers the CFG keyword list <kwl> as a list of valid keywords for next
4496
 * parsing sessions.
4497
 */
4498
void cfg_register_keywords(struct cfg_kw_list *kwl)
4499
0
{
4500
0
  LIST_APPEND(&cfg_keywords.list, &kwl->list);
4501
0
}
4502
4503
/*
4504
 * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
4505
 */
4506
void cfg_unregister_keywords(struct cfg_kw_list *kwl)
4507
0
{
4508
0
  LIST_DELETE(&kwl->list);
4509
0
  LIST_INIT(&kwl->list);
4510
0
}
4511
4512
/* this function register new section in the haproxy configuration file.
4513
 * <section_name> is the name of this new section and <section_parser>
4514
 * is the called parser. If two section declaration have the same name,
4515
 * only the first declared is used.
4516
 */
4517
int cfg_register_section(char *section_name,
4518
                         int (*section_parser)(const char *, int, char **, int),
4519
                         int (*post_section_parser)())
4520
0
{
4521
0
  struct cfg_section *cs;
4522
4523
0
  list_for_each_entry(cs, &sections, list) {
4524
0
    if (strcmp(cs->section_name, section_name) == 0) {
4525
0
      ha_alert("register section '%s': already registered.\n", section_name);
4526
0
      return 0;
4527
0
    }
4528
0
  }
4529
4530
0
  cs = calloc(1, sizeof(*cs));
4531
0
  if (!cs) {
4532
0
    ha_alert("register section '%s': out of memory.\n", section_name);
4533
0
    return 0;
4534
0
  }
4535
4536
0
  cs->section_name = section_name;
4537
0
  cs->section_parser = section_parser;
4538
0
  cs->post_section_parser = post_section_parser;
4539
4540
0
  LIST_APPEND(&sections, &cs->list);
4541
4542
0
  return 1;
4543
0
}
4544
4545
/* this function register a new function which will be called once the haproxy
4546
 * configuration file has been parsed. It's useful to check dependencies
4547
 * between sections or to resolve items once everything is parsed.
4548
 */
4549
int cfg_register_postparser(char *name, int (*func)())
4550
0
{
4551
0
  struct cfg_postparser *cp;
4552
4553
0
  cp = calloc(1, sizeof(*cp));
4554
0
  if (!cp) {
4555