blob: 35fd6596e82a94aeb15d90796557f8f1949f3eed [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Configuration parser
3 *
Willy Tarreau0b4ed902007-03-26 00:18:40 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <netdb.h>
17#include <ctype.h>
Willy Tarreau95c20ac2007-03-25 15:39:23 +020018#include <pwd.h>
19#include <grp.h>
Willy Tarreau0b4ed902007-03-26 00:18:40 +020020#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/cfgparse.h>
23#include <common/config.h>
24#include <common/memory.h>
25#include <common/standard.h>
26#include <common/time.h>
27#include <common/uri_auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#include <types/capture.h>
30#include <types/global.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010031#include <types/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/polling.h>
33#include <types/proxy.h>
34#include <types/queue.h>
35
36#include <proto/backend.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010037#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/checks.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010039#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/log.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010041#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/server.h>
43#include <proto/task.h>
44
45
Willy Tarreauf3c69202006-07-09 16:42:34 +020046/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
47 * ssl-hello-chk option to ensure that the remote server speaks SSL.
48 *
49 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
50 */
51const char sslv3_client_hello_pkt[] = {
52 "\x16" /* ContentType : 0x16 = Hanshake */
53 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
54 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
55 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
56 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
57 "\x03\x00" /* Hello Version : 0x0300 = v3 */
58 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
59 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
60 "\x00" /* Session ID length : empty (no session ID) */
61 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
62 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
63 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
64 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
65 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
66 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
67 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
68 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
69 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
70 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
71 "\x00\x38" "\x00\x39" "\x00\x3A"
72 "\x01" /* Compression Length : 0x01 = 1 byte for types */
73 "\x00" /* Compression Type : 0x00 = NULL compression */
74};
75
Willy Tarreau13943ab2006-12-31 00:24:10 +010076/* some of the most common options which are also the easiest to handle */
77static const struct {
78 const char *name;
79 unsigned int val;
80 unsigned int cap;
Willy Tarreau4fee4e92007-01-06 21:09:17 +010081 unsigned int checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +010082} cfg_opts[] =
83{
84#ifdef TPROXY
85 { "transparent", PR_O_TRANSP, PR_CAP_FE },
86#endif
Willy Tarreau4fee4e92007-01-06 21:09:17 +010087 { "redispatch", PR_O_REDISP, PR_CAP_BE, 0 },
88 { "keepalive", PR_O_KEEPALIVE, PR_CAP_NONE, 0 },
Willy Tarreau4fee4e92007-01-06 21:09:17 +010089 { "httpclose", PR_O_HTTP_CLOSE, PR_CAP_FE | PR_CAP_BE, 0 },
90 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0 },
91 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0 },
92 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0 },
93 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0 },
94 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0 },
95 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0 },
96 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0 },
97 { "persist", PR_O_PERSIST, PR_CAP_BE, 0 },
98 { "forceclose", PR_O_FORCE_CLO | PR_O_HTTP_CLOSE, PR_CAP_BE, 0 },
Willy Tarreau8f922fc2007-01-06 21:11:49 +010099#ifdef CONFIG_HAP_TCPSPLICE
100 { "tcpsplice", PR_O_TCPSPLICE , PR_CAP_BE|PR_CAP_FE, LSTCHK_TCPSPLICE|LSTCHK_NETADM },
101#endif
102
Willy Tarreau13943ab2006-12-31 00:24:10 +0100103 { NULL, 0, 0 }
104};
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
107static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
108int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
109int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
110
111/*
112 * converts <str> to a list of listeners which are dynamically allocated.
113 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
114 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
115 * - <port> is a numerical port from 1 to 65535 ;
116 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
117 * This can be repeated as many times as necessary, separated by a coma.
118 * The <tail> argument is a pointer to a current list which should be appended
119 * to the tail of the new list. The pointer to the new list is returned.
120 */
121static struct listener *str2listener(char *str, struct listener *tail)
122{
123 struct listener *l;
124 char *c, *next, *range, *dupstr;
125 int port, end;
126
127 next = dupstr = strdup(str);
128
129 while (next && *next) {
130 struct sockaddr_storage ss;
131
132 str = next;
133 /* 1) look for the end of the first address */
134 if ((next = strrchr(str, ',')) != NULL) {
135 *next++ = 0;
136 }
137
138 /* 2) look for the addr/port delimiter, it's the last colon. */
139 if ((range = strrchr(str, ':')) == NULL) {
140 Alert("Missing port number: '%s'\n", str);
141 goto fail;
142 }
143
144 *range++ = 0;
145
146 if (strrchr(str, ':') != NULL) {
147 /* IPv6 address contains ':' */
148 memset(&ss, 0, sizeof(ss));
149 ss.ss_family = AF_INET6;
150
151 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
152 Alert("Invalid server address: '%s'\n", str);
153 goto fail;
154 }
155 }
156 else {
157 memset(&ss, 0, sizeof(ss));
158 ss.ss_family = AF_INET;
159
160 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
161 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
162 }
163 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
164 struct hostent *he;
165
166 if ((he = gethostbyname(str)) == NULL) {
167 Alert("Invalid server name: '%s'\n", str);
168 goto fail;
169 }
170 else
171 ((struct sockaddr_in *)&ss)->sin_addr =
172 *(struct in_addr *) *(he->h_addr_list);
173 }
174 }
175
176 /* 3) look for the port-end delimiter */
177 if ((c = strchr(range, '-')) != NULL) {
178 *c++ = 0;
179 end = atol(c);
180 }
181 else {
182 end = atol(range);
183 }
184
185 port = atol(range);
186
187 if (port < 1 || port > 65535) {
188 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
189 goto fail;
190 }
191
192 if (end < 1 || end > 65535) {
193 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
194 goto fail;
195 }
196
197 for (; port <= end; port++) {
198 l = (struct listener *)calloc(1, sizeof(struct listener));
199 l->next = tail;
200 tail = l;
201
202 l->fd = -1;
203 l->addr = ss;
204 if (ss.ss_family == AF_INET6)
205 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
206 else
207 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
208
209 } /* end for(port) */
210 } /* end while(next) */
211 free(dupstr);
212 return tail;
213 fail:
214 free(dupstr);
215 return NULL;
216}
217
Willy Tarreau977b8e42006-12-29 14:19:17 +0100218/*
219 * Sends a warning if proxy <proxy> does not have at least one of the
220 * capabilities in <cap>. An optionnal <hint> may be added at the end
221 * of the warning to help the user. Returns 1 if a warning was emitted
222 * or 0 if the condition is valid.
223 */
224int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, char *arg, char *hint)
225{
226 char *msg;
227
228 switch (cap) {
229 case PR_CAP_BE: msg = "no backend"; break;
230 case PR_CAP_FE: msg = "no frontend"; break;
231 case PR_CAP_RS: msg = "no ruleset"; break;
232 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
233 default: msg = "not enough"; break;
234 }
235
236 if (!(proxy->cap & cap)) {
237 Warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100238 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
Willy Tarreau977b8e42006-12-29 14:19:17 +0100239 return 1;
240 }
241 return 0;
242}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
244/*
245 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
246 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200247int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248{
249
250 if (!strcmp(args[0], "global")) { /* new section */
251 /* no option, nothing special to do */
252 return 0;
253 }
254 else if (!strcmp(args[0], "daemon")) {
255 global.mode |= MODE_DAEMON;
256 }
257 else if (!strcmp(args[0], "debug")) {
258 global.mode |= MODE_DEBUG;
259 }
260 else if (!strcmp(args[0], "noepoll")) {
261 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
262 }
263 else if (!strcmp(args[0], "nopoll")) {
264 cfg_polling_mechanism &= ~POLL_USE_POLL;
265 }
266 else if (!strcmp(args[0], "quiet")) {
267 global.mode |= MODE_QUIET;
268 }
269 else if (!strcmp(args[0], "stats")) {
270 global.mode |= MODE_STATS;
271 }
272 else if (!strcmp(args[0], "uid")) {
273 if (global.uid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200274 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275 return 0;
276 }
277 if (*(args[1]) == 0) {
278 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
279 return -1;
280 }
281 global.uid = atol(args[1]);
282 }
283 else if (!strcmp(args[0], "gid")) {
284 if (global.gid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200285 Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 return 0;
287 }
288 if (*(args[1]) == 0) {
289 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
290 return -1;
291 }
292 global.gid = atol(args[1]);
293 }
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200294 /* user/group name handling */
295 else if (!strcmp(args[0], "user")) {
296 struct passwd *ha_user;
297 if (global.uid != 0) {
298 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
299 return 0;
300 }
301 errno = 0;
302 ha_user = getpwnam(args[1]);
303 if (ha_user != NULL) {
304 global.uid = (int)ha_user->pw_uid;
305 }
306 else {
307 Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
308 exit(1);
309 }
310 }
311 else if (!strcmp(args[0], "group")) {
312 struct group *ha_group;
313 if (global.gid != 0) {
314 Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
315 return 0;
316 }
317 errno = 0;
318 ha_group = getgrnam(args[1]);
319 if (ha_group != NULL) {
320 global.gid = (int)ha_group->gr_gid;
321 }
322 else {
323 Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
324 exit(1);
325 }
326 }
327 /* end of user/group name handling*/
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 else if (!strcmp(args[0], "nbproc")) {
329 if (global.nbproc != 0) {
330 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
331 return 0;
332 }
333 if (*(args[1]) == 0) {
334 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
335 return -1;
336 }
337 global.nbproc = atol(args[1]);
338 }
339 else if (!strcmp(args[0], "maxconn")) {
340 if (global.maxconn != 0) {
341 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
342 return 0;
343 }
344 if (*(args[1]) == 0) {
345 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
346 return -1;
347 }
348 global.maxconn = atol(args[1]);
349#ifdef SYSTEM_MAXCONN
350 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
351 Alert("parsing [%s:%d] : maxconn value %d too high for this system.\nLimiting to %d. Please use '-n' to force the value.\n", file, linenum, global.maxconn, DEFAULT_MAXCONN);
352 global.maxconn = DEFAULT_MAXCONN;
353 }
354#endif /* SYSTEM_MAXCONN */
355 }
356 else if (!strcmp(args[0], "ulimit-n")) {
357 if (global.rlimit_nofile != 0) {
358 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
359 return 0;
360 }
361 if (*(args[1]) == 0) {
362 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
363 return -1;
364 }
365 global.rlimit_nofile = atol(args[1]);
366 }
367 else if (!strcmp(args[0], "chroot")) {
368 if (global.chroot != NULL) {
369 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
370 return 0;
371 }
372 if (*(args[1]) == 0) {
373 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
374 return -1;
375 }
376 global.chroot = strdup(args[1]);
377 }
378 else if (!strcmp(args[0], "pidfile")) {
379 if (global.pidfile != NULL) {
380 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
381 return 0;
382 }
383 if (*(args[1]) == 0) {
384 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
385 return -1;
386 }
387 global.pidfile = strdup(args[1]);
388 }
389 else if (!strcmp(args[0], "log")) { /* syslog server address */
390 struct sockaddr_in *sa;
391 int facility, level;
392
393 if (*(args[1]) == 0 || *(args[2]) == 0) {
394 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
395 return -1;
396 }
397
398 facility = get_log_facility(args[2]);
399 if (facility < 0) {
400 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
401 exit(1);
402 }
403
404 level = 7; /* max syslog level = debug */
405 if (*(args[3])) {
406 level = get_log_level(args[3]);
407 if (level < 0) {
408 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
409 exit(1);
410 }
411 }
412
413 sa = str2sa(args[1]);
414 if (!sa->sin_port)
415 sa->sin_port = htons(SYSLOG_PORT);
416
417 if (global.logfac1 == -1) {
418 global.logsrv1 = *sa;
419 global.logfac1 = facility;
420 global.loglev1 = level;
421 }
422 else if (global.logfac2 == -1) {
423 global.logsrv2 = *sa;
424 global.logfac2 = facility;
425 global.loglev2 = level;
426 }
427 else {
428 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
429 return -1;
430 }
431
432 }
433 else {
434 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
435 return -1;
436 }
437 return 0;
438}
439
440
441static void init_default_instance()
442{
443 memset(&defproxy, 0, sizeof(defproxy));
444 defproxy.mode = PR_MODE_TCP;
445 defproxy.state = PR_STNEW;
446 defproxy.maxconn = cfg_maxpconn;
447 defproxy.conn_retries = CONN_RETRIES;
448 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
449}
450
451/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100452 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
453 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200455int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456{
457 static struct proxy *curproxy = NULL;
458 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200459 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 int rc;
461
Willy Tarreau977b8e42006-12-29 14:19:17 +0100462 if (!strcmp(args[0], "listen"))
463 rc = PR_CAP_LISTEN;
464 else if (!strcmp(args[0], "frontend"))
465 rc = PR_CAP_FE | PR_CAP_RS;
466 else if (!strcmp(args[0], "backend"))
467 rc = PR_CAP_BE | PR_CAP_RS;
468 else if (!strcmp(args[0], "ruleset"))
469 rc = PR_CAP_RS;
470 else
471 rc = PR_CAP_NONE;
472
473 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 if (!*args[1]) {
475 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
476 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
477 file, linenum, args[0]);
478 return -1;
479 }
480
481 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
482 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
483 return -1;
484 }
485
486 curproxy->next = proxy;
487 proxy = curproxy;
488 LIST_INIT(&curproxy->pendconns);
489
490 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100491 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492
493 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100494 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495 curproxy->listen = str2listener(args[2], curproxy->listen);
496 if (!curproxy->listen)
497 return -1;
498 global.maxsock++;
499 }
500
501 /* set default values */
502 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503 curproxy->options = defproxy.options;
Willy Tarreau7ac51f62007-03-25 16:00:04 +0200504 curproxy->except_net = defproxy.except_net;
505 curproxy->except_mask = defproxy.except_mask;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreau977b8e42006-12-29 14:19:17 +0100507 if (curproxy->cap & PR_CAP_FE) {
508 curproxy->maxconn = defproxy.maxconn;
509
510 /* initialize error relocations */
511 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
512 if (defproxy.errmsg[rc].str)
513 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
514 }
515
516 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
517 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518
Willy Tarreau977b8e42006-12-29 14:19:17 +0100519 if (curproxy->cap & PR_CAP_BE) {
520 curproxy->fullconn = defproxy.fullconn;
521 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau977b8e42006-12-29 14:19:17 +0100523 if (defproxy.check_req)
524 curproxy->check_req = strdup(defproxy.check_req);
525 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
Willy Tarreau977b8e42006-12-29 14:19:17 +0100527 if (defproxy.cookie_name)
528 curproxy->cookie_name = strdup(defproxy.cookie_name);
529 curproxy->cookie_len = defproxy.cookie_len;
530 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531
Willy Tarreau977b8e42006-12-29 14:19:17 +0100532 if (curproxy->cap & PR_CAP_RS) {
533 if (defproxy.capture_name)
534 curproxy->capture_name = strdup(defproxy.capture_name);
535 curproxy->capture_namelen = defproxy.capture_namelen;
536 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100537 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538
Willy Tarreau977b8e42006-12-29 14:19:17 +0100539 if (curproxy->cap & PR_CAP_FE) {
540 curproxy->clitimeout = defproxy.clitimeout;
541 curproxy->uri_auth = defproxy.uri_auth;
542 curproxy->mon_net = defproxy.mon_net;
543 curproxy->mon_mask = defproxy.mon_mask;
544 if (defproxy.monitor_uri)
545 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
546 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100547 if (defproxy.defbe.name)
548 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100549 }
550
551 if (curproxy->cap & PR_CAP_BE) {
552 curproxy->contimeout = defproxy.contimeout;
553 curproxy->srvtimeout = defproxy.srvtimeout;
554 curproxy->source_addr = defproxy.source_addr;
555 }
556
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 curproxy->mode = defproxy.mode;
558 curproxy->logfac1 = defproxy.logfac1;
559 curproxy->logsrv1 = defproxy.logsrv1;
560 curproxy->loglev1 = defproxy.loglev1;
561 curproxy->logfac2 = defproxy.logfac2;
562 curproxy->logsrv2 = defproxy.logsrv2;
563 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200565
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566 return 0;
567 }
568 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
569 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100570 /* FIXME-20070101: we should do this too at the end of the
571 * config parsing to free all default values.
572 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 if (defproxy.check_req) free(defproxy.check_req);
574 if (defproxy.cookie_name) free(defproxy.cookie_name);
575 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200576 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100577 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100578
579 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
580 if (defproxy.errmsg[rc].len)
581 free(defproxy.errmsg[rc].str);
582 }
583
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584 /* we cannot free uri_auth because it might already be used */
585 init_default_instance();
586 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100587 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588 return 0;
589 }
590 else if (curproxy == NULL) {
591 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
592 return -1;
593 }
594
Willy Tarreau977b8e42006-12-29 14:19:17 +0100595
596 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597 if (!strcmp(args[0], "bind")) { /* new listen addresses */
598 if (curproxy == &defproxy) {
599 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
600 return -1;
601 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100602 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
603 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604
605 if (strchr(args[1], ':') == NULL) {
606 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
607 file, linenum, args[0]);
608 return -1;
609 }
610 curproxy->listen = str2listener(args[1], curproxy->listen);
611 if (!curproxy->listen)
612 return -1;
613 global.maxsock++;
614 return 0;
615 }
616 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
617 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
618 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
619 file, linenum, args[0]);
620 return -1;
621 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100622 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
623 return 0;
624
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 /* flush useless bits */
626 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
627 return 0;
628 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200629 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100630 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
631 return 0;
632
Willy Tarreau1c47f852006-07-09 08:22:27 +0200633 if (!*args[1]) {
634 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
635 file, linenum, args[0]);
636 return -1;
637 }
638
639 if (curproxy->monitor_uri != NULL)
640 free(curproxy->monitor_uri);
641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100642 curproxy->monitor_uri_len = strlen(args[1]);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200643 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100644 memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200645 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
646
647 return 0;
648 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
650 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
651 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
652 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
653 else {
654 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
655 return -1;
656 }
657 }
658 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
659 curproxy->state = PR_STSTOPPED;
660 }
661 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
662 curproxy->state = PR_STNEW;
663 }
664 else if (!strcmp(args[0], "cookie")) { /* cookie name */
665 int cur_arg;
666 // if (curproxy == &defproxy) {
667 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
668 // return -1;
669 // }
670
Willy Tarreau977b8e42006-12-29 14:19:17 +0100671 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
672 return 0;
673
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 if (curproxy->cookie_name != NULL) {
675 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
676 // file, linenum);
677 // return 0;
678 free(curproxy->cookie_name);
679 }
680
681 if (*(args[1]) == 0) {
682 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
683 file, linenum, args[0]);
684 return -1;
685 }
686 curproxy->cookie_name = strdup(args[1]);
687 curproxy->cookie_len = strlen(curproxy->cookie_name);
688
689 cur_arg = 2;
690 while (*(args[cur_arg])) {
691 if (!strcmp(args[cur_arg], "rewrite")) {
692 curproxy->options |= PR_O_COOK_RW;
693 }
694 else if (!strcmp(args[cur_arg], "indirect")) {
695 curproxy->options |= PR_O_COOK_IND;
696 }
697 else if (!strcmp(args[cur_arg], "insert")) {
698 curproxy->options |= PR_O_COOK_INS;
699 }
700 else if (!strcmp(args[cur_arg], "nocache")) {
701 curproxy->options |= PR_O_COOK_NOC;
702 }
703 else if (!strcmp(args[cur_arg], "postonly")) {
704 curproxy->options |= PR_O_COOK_POST;
705 }
706 else if (!strcmp(args[cur_arg], "prefix")) {
707 curproxy->options |= PR_O_COOK_PFX;
708 }
709 else {
710 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
711 file, linenum, args[0]);
712 return -1;
713 }
714 cur_arg++;
715 }
716 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
717 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
718 file, linenum);
719 return -1;
720 }
721
722 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
723 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
724 file, linenum);
725 return -1;
726 }
727 }/* end else if (!strcmp(args[0], "cookie")) */
728 else if (!strcmp(args[0], "appsession")) { /* cookie name */
729 // if (curproxy == &defproxy) {
730 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
731 // return -1;
732 // }
733
Willy Tarreau977b8e42006-12-29 14:19:17 +0100734 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
735 return 0;
736
Willy Tarreaubaaee002006-06-26 02:48:02 +0200737 if (curproxy->appsession_name != NULL) {
738 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
739 // file, linenum);
740 // return 0;
741 free(curproxy->appsession_name);
742 }
743
744 if (*(args[5]) == 0) {
745 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
746 file, linenum, args[0]);
747 return -1;
748 }
749 have_appsession = 1;
750 curproxy->appsession_name = strdup(args[1]);
751 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
752 curproxy->appsession_len = atoi(args[3]);
753 curproxy->appsession_timeout = atoi(args[5]);
754 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
755 if (rc) {
756 Alert("Error Init Appsession Hashtable.\n");
757 return -1;
758 }
759 } /* Url App Session */
760 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100761 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
762 return 0;
763
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
765 // if (curproxy == &defproxy) {
766 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
767 // return -1;
768 // }
769
770 if (curproxy->capture_name != NULL) {
771 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
772 // file, linenum, args[0]);
773 // return 0;
774 free(curproxy->capture_name);
775 }
776
777 if (*(args[4]) == 0) {
778 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
779 file, linenum, args[0]);
780 return -1;
781 }
782 curproxy->capture_name = strdup(args[2]);
783 curproxy->capture_namelen = strlen(curproxy->capture_name);
784 curproxy->capture_len = atol(args[4]);
785 if (curproxy->capture_len >= CAPTURE_LEN) {
786 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
787 file, linenum, CAPTURE_LEN - 1);
788 curproxy->capture_len = CAPTURE_LEN - 1;
789 }
790 curproxy->to_log |= LW_COOKIE;
791 }
792 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
793 struct cap_hdr *hdr;
794
795 if (curproxy == &defproxy) {
796 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
797 return -1;
798 }
799
800 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
801 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
802 file, linenum, args[0], args[1]);
803 return -1;
804 }
805
806 hdr = calloc(sizeof(struct cap_hdr), 1);
807 hdr->next = curproxy->req_cap;
808 hdr->name = strdup(args[3]);
809 hdr->namelen = strlen(args[3]);
810 hdr->len = atol(args[5]);
811 hdr->index = curproxy->nb_req_cap++;
812 curproxy->req_cap = hdr;
813 curproxy->to_log |= LW_REQHDR;
814 }
815 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
816 struct cap_hdr *hdr;
817
818 if (curproxy == &defproxy) {
819 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
820 return -1;
821 }
822
823 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
824 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
825 file, linenum, args[0], args[1]);
826 return -1;
827 }
828 hdr = calloc(sizeof(struct cap_hdr), 1);
829 hdr->next = curproxy->rsp_cap;
830 hdr->name = strdup(args[3]);
831 hdr->namelen = strlen(args[3]);
832 hdr->len = atol(args[5]);
833 hdr->index = curproxy->nb_rsp_cap++;
834 curproxy->rsp_cap = hdr;
835 curproxy->to_log |= LW_RSPHDR;
836 }
837 else {
838 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
839 file, linenum, args[0]);
840 return -1;
841 }
842 }
843 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
844 if (curproxy->contimeout != defproxy.contimeout) {
845 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
846 return 0;
847 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100848 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
849 return 0;
850
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 if (*(args[1]) == 0) {
852 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
853 file, linenum, args[0]);
854 return -1;
855 }
856 curproxy->contimeout = atol(args[1]);
857 }
858 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
859 if (curproxy->clitimeout != defproxy.clitimeout) {
860 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
861 file, linenum, args[0]);
862 return 0;
863 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100864 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
865 return 0;
866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 if (*(args[1]) == 0) {
868 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
869 file, linenum, args[0]);
870 return -1;
871 }
872 curproxy->clitimeout = atol(args[1]);
873 }
874 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
875 if (curproxy->srvtimeout != defproxy.srvtimeout) {
876 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
877 return 0;
878 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100879 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
880 return 0;
881
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 if (*(args[1]) == 0) {
883 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
884 file, linenum, args[0]);
885 return -1;
886 }
887 curproxy->srvtimeout = atol(args[1]);
888 }
889 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100890 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
891 return 0;
892
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 if (*(args[1]) == 0) {
894 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
895 file, linenum, args[0]);
896 return -1;
897 }
898 curproxy->conn_retries = atol(args[1]);
899 }
900 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100901 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
902 return 0;
903
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
905 curproxy->uri_auth = NULL; /* we must detach from the default config */
906
907 if (*(args[1]) == 0) {
908 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
909 return -1;
910 } else if (!strcmp(args[1], "uri")) {
911 if (*(args[2]) == 0) {
912 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
913 return -1;
914 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
915 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
916 return -1;
917 }
918 } else if (!strcmp(args[1], "realm")) {
919 if (*(args[2]) == 0) {
920 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
921 return -1;
922 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
923 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
924 return -1;
925 }
926 } else if (!strcmp(args[1], "auth")) {
927 if (*(args[2]) == 0) {
928 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
929 return -1;
930 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
931 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
932 return -1;
933 }
934 } else if (!strcmp(args[1], "scope")) {
935 if (*(args[2]) == 0) {
936 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
937 return -1;
938 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
939 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
940 return -1;
941 }
942 } else if (!strcmp(args[1], "enable")) {
943 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
944 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
945 return -1;
946 }
947 } else {
948 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
949 file, linenum, args[0]);
950 return -1;
951 }
952 }
953 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100954 int optnum;
955
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956 if (*(args[1]) == 0) {
957 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
958 return -1;
959 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100960
961 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
962 if (!strcmp(args[1], cfg_opts[optnum].name)) {
963 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
964 return 0;
965 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +0100966 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +0100967 return 0;
968 }
969 }
970
971 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 /* generate a complete HTTP log */
973 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
974 else if (!strcmp(args[1], "tcplog"))
975 /* generate a detailed TCP log */
976 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977 else if (!strcmp(args[1], "tcpka")) {
978 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100979 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
980 return 0;
981
982 if (curproxy->cap & PR_CAP_FE)
983 curproxy->options |= PR_O_TCP_CLI_KA;
984 if (curproxy->cap & PR_CAP_BE)
985 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986 }
987 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100988 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
989 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200990 /* use HTTP request to check servers' health */
991 if (curproxy->check_req != NULL) {
992 free(curproxy->check_req);
993 }
994 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200995 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 if (!*args[2]) { /* no argument */
997 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
998 curproxy->check_len = strlen(DEF_CHECK_REQ);
999 } else if (!*args[3]) { /* one argument : URI */
1000 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
1001 curproxy->check_req = (char *)malloc(reqlen);
1002 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1003 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
1004 } else { /* more arguments : METHOD URI [HTTP_VER] */
1005 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
1006 if (*args[4])
1007 reqlen += strlen(args[4]);
1008 else
1009 reqlen += strlen("HTTP/1.0");
1010
1011 curproxy->check_req = (char *)malloc(reqlen);
1012 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1013 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
1014 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001015 }
1016 else if (!strcmp(args[1], "ssl-hello-chk")) {
1017 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001018 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1019 return 0;
1020
Willy Tarreauf3c69202006-07-09 16:42:34 +02001021 if (curproxy->check_req != NULL) {
1022 free(curproxy->check_req);
1023 }
1024 curproxy->options &= ~PR_O_HTTP_CHK;
1025 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026 }
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001027 else if (!strcmp(args[1], "forwardfor")) {
1028 /* insert x-forwarded-for field, but not for the
1029 * IP address listed as an except.
1030 */
1031 if (*(args[2])) {
1032 if (!strcmp(args[2], "except")) {
1033 if (!*args[3] || !str2net(args[3], &curproxy->except_net, &curproxy->except_mask)) {
1034 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1035 file, linenum, args[0]);
1036 return -1;
1037 }
1038 /* flush useless bits */
1039 curproxy->except_net.s_addr &= curproxy->except_mask.s_addr;
1040 } else {
1041 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1042 file, linenum, args[0]);
1043 return -1;
1044 }
1045 }
1046 curproxy->options |= PR_O_FWDFOR;
1047 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001048 else {
1049 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1050 return -1;
1051 }
1052 return 0;
1053 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001054 else if (!strcmp(args[0], "default_backend")) {
1055 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
1056 return 0;
1057
1058 if (*(args[1]) == 0) {
1059 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
1060 return -1;
1061 }
1062 if (curproxy->defbe.name)
1063 free(curproxy->defbe.name);
1064 curproxy->defbe.name = strdup(args[1]);
1065 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001067 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1068 return 0;
1069
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 /* enable reconnections to dispatch */
1071 curproxy->options |= PR_O_REDISP;
1072 }
1073#ifdef TPROXY
1074 else if (!strcmp(args[0], "transparent")) {
1075 /* enable transparent proxy connections */
1076 curproxy->options |= PR_O_TRANSP;
1077 }
1078#endif
1079 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001080 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1081 return 0;
1082
Willy Tarreaubaaee002006-06-26 02:48:02 +02001083 if (*(args[1]) == 0) {
1084 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1085 return -1;
1086 }
1087 curproxy->maxconn = atol(args[1]);
1088 }
Willy Tarreau86034312006-12-29 00:10:33 +01001089 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001090 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1091 return 0;
1092
Willy Tarreau86034312006-12-29 00:10:33 +01001093 if (*(args[1]) == 0) {
1094 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1095 return -1;
1096 }
1097 curproxy->fullconn = atol(args[1]);
1098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1100 if (*(args[1]) == 0) {
1101 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1102 return -1;
1103 }
1104 curproxy->grace = atol(args[1]);
1105 }
1106 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1107 if (curproxy == &defproxy) {
1108 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1109 return -1;
1110 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001111 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1112 return 0;
1113
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114 if (strchr(args[1], ':') == NULL) {
1115 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1116 return -1;
1117 }
1118 curproxy->dispatch_addr = *str2sa(args[1]);
1119 }
1120 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001121 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1122 return 0;
1123
Willy Tarreaubaaee002006-06-26 02:48:02 +02001124 if (*(args[1])) {
1125 if (!strcmp(args[1], "roundrobin")) {
1126 curproxy->options |= PR_O_BALANCE_RR;
1127 }
1128 else if (!strcmp(args[1], "source")) {
1129 curproxy->options |= PR_O_BALANCE_SH;
1130 }
1131 else {
1132 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1133 return -1;
1134 }
1135 }
1136 else /* if no option is set, use round-robin by default */
1137 curproxy->options |= PR_O_BALANCE_RR;
1138 }
1139 else if (!strcmp(args[0], "server")) { /* server address */
1140 int cur_arg;
1141 char *rport;
1142 char *raddr;
1143 short realport;
1144 int do_check;
1145
1146 if (curproxy == &defproxy) {
1147 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1148 return -1;
1149 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001150 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1151 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152
1153 if (!*args[2]) {
1154 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1155 file, linenum, args[0]);
1156 return -1;
1157 }
1158 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1159 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1160 return -1;
1161 }
1162
1163 /* the servers are linked backwards first */
1164 newsrv->next = curproxy->srv;
1165 curproxy->srv = newsrv;
1166 newsrv->proxy = curproxy;
1167
1168 LIST_INIT(&newsrv->pendconns);
1169 do_check = 0;
1170 newsrv->state = SRV_RUNNING; /* early server setup */
1171 newsrv->id = strdup(args[1]);
1172
1173 /* several ways to check the port component :
1174 * - IP => port=+0, relative
1175 * - IP: => port=+0, relative
1176 * - IP:N => port=N, absolute
1177 * - IP:+N => port=+N, relative
1178 * - IP:-N => port=-N, relative
1179 */
1180 raddr = strdup(args[2]);
1181 rport = strchr(raddr, ':');
1182 if (rport) {
1183 *rport++ = 0;
1184 realport = atol(rport);
1185 if (!isdigit((int)*rport))
1186 newsrv->state |= SRV_MAPPORTS;
1187 } else {
1188 realport = 0;
1189 newsrv->state |= SRV_MAPPORTS;
1190 }
1191
1192 newsrv->addr = *str2sa(raddr);
1193 newsrv->addr.sin_port = htons(realport);
1194 free(raddr);
1195
1196 newsrv->curfd = -1; /* no health-check in progress */
1197 newsrv->inter = DEF_CHKINTR;
1198 newsrv->rise = DEF_RISETIME;
1199 newsrv->fall = DEF_FALLTIME;
1200 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
Willy Tarreau417fae02007-03-25 21:16:40 +02001201 newsrv->uweight = 1;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001202
Willy Tarreaubaaee002006-06-26 02:48:02 +02001203 cur_arg = 3;
1204 while (*args[cur_arg]) {
1205 if (!strcmp(args[cur_arg], "cookie")) {
1206 newsrv->cookie = strdup(args[cur_arg + 1]);
1207 newsrv->cklen = strlen(args[cur_arg + 1]);
1208 cur_arg += 2;
1209 }
1210 else if (!strcmp(args[cur_arg], "rise")) {
1211 newsrv->rise = atol(args[cur_arg + 1]);
1212 newsrv->health = newsrv->rise;
1213 cur_arg += 2;
1214 }
1215 else if (!strcmp(args[cur_arg], "fall")) {
1216 newsrv->fall = atol(args[cur_arg + 1]);
1217 cur_arg += 2;
1218 }
1219 else if (!strcmp(args[cur_arg], "inter")) {
1220 newsrv->inter = atol(args[cur_arg + 1]);
1221 cur_arg += 2;
1222 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001223 else if (!strcmp(args[cur_arg], "addr")) {
1224 newsrv->check_addr = *str2sa(args[cur_arg + 1]);
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001225 cur_arg += 2;
1226 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 else if (!strcmp(args[cur_arg], "port")) {
1228 newsrv->check_port = atol(args[cur_arg + 1]);
1229 cur_arg += 2;
1230 }
1231 else if (!strcmp(args[cur_arg], "backup")) {
1232 newsrv->state |= SRV_BACKUP;
1233 cur_arg ++;
1234 }
1235 else if (!strcmp(args[cur_arg], "weight")) {
1236 int w;
1237 w = atol(args[cur_arg + 1]);
1238 if (w < 1 || w > 256) {
1239 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1240 file, linenum, newsrv->id, w);
1241 return -1;
1242 }
Willy Tarreau417fae02007-03-25 21:16:40 +02001243 newsrv->uweight = w;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 cur_arg += 2;
1245 }
1246 else if (!strcmp(args[cur_arg], "minconn")) {
1247 newsrv->minconn = atol(args[cur_arg + 1]);
1248 cur_arg += 2;
1249 }
1250 else if (!strcmp(args[cur_arg], "maxconn")) {
1251 newsrv->maxconn = atol(args[cur_arg + 1]);
1252 cur_arg += 2;
1253 }
1254 else if (!strcmp(args[cur_arg], "check")) {
1255 global.maxsock++;
1256 do_check = 1;
1257 cur_arg += 1;
1258 }
1259 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1260 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001261#ifdef CONFIG_HAP_CTTPROXY
1262 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1263 file, linenum, "source", "usesrc");
1264#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1266 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001267#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268 return -1;
1269 }
1270 newsrv->state |= SRV_BIND_SRC;
1271 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1272 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001273 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001274#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001275 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001276 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1277 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001278 return -1;
1279 }
1280 if (!*args[cur_arg + 1]) {
1281 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1282 file, linenum, "usesrc");
1283 return -1;
1284 }
1285 if (!strcmp(args[cur_arg + 1], "client")) {
1286 newsrv->state |= SRV_TPROXY_CLI;
1287 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1288 newsrv->state |= SRV_TPROXY_CIP;
1289 } else {
1290 newsrv->state |= SRV_TPROXY_ADDR;
1291 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1292 }
1293 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1294 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001295#else /* no CTTPROXY support */
1296 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1297 file, linenum, "usesrc");
1298 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001299#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001300 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001302#ifdef CONFIG_HAP_CTTPROXY
1303 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1304 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1305 file, linenum, "usesrc", "source");
1306 return -1;
1307 }
1308#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309 else {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001310 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'addr', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311 file, linenum, newsrv->id);
1312 return -1;
1313 }
1314 }
1315
1316 if (do_check) {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001317 if (!newsrv->check_port && newsrv->check_addr.sin_port)
1318 newsrv->check_port = newsrv->check_addr.sin_port;
1319
Willy Tarreaubaaee002006-06-26 02:48:02 +02001320 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1321 newsrv->check_port = realport; /* by default */
1322 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001323 /* not yet valid, because no port was set on
1324 * the server either. We'll check if we have
1325 * a known port on the first listener.
1326 */
1327 struct listener *l;
1328 l = curproxy->listen;
1329 if (l) {
1330 int port;
1331 port = (l->addr.ss_family == AF_INET6)
1332 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1333 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1334 newsrv->check_port = port;
1335 }
1336 }
1337 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1339 file, linenum, newsrv->id);
1340 return -1;
1341 }
1342 newsrv->state |= SRV_CHECKED;
1343 }
1344
1345 if (newsrv->state & SRV_BACKUP)
1346 curproxy->srv_bck++;
1347 else
1348 curproxy->srv_act++;
1349 }
1350 else if (!strcmp(args[0], "log")) { /* syslog server address */
1351 struct sockaddr_in *sa;
1352 int facility;
1353
1354 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1355 curproxy->logfac1 = global.logfac1;
1356 curproxy->logsrv1 = global.logsrv1;
1357 curproxy->loglev1 = global.loglev1;
1358 curproxy->logfac2 = global.logfac2;
1359 curproxy->logsrv2 = global.logsrv2;
1360 curproxy->loglev2 = global.loglev2;
1361 }
1362 else if (*(args[1]) && *(args[2])) {
1363 int level;
1364
1365 facility = get_log_facility(args[2]);
1366 if (facility < 0) {
1367 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1368 exit(1);
1369 }
1370
1371 level = 7; /* max syslog level = debug */
1372 if (*(args[3])) {
1373 level = get_log_level(args[3]);
1374 if (level < 0) {
1375 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1376 exit(1);
1377 }
1378 }
1379
1380 sa = str2sa(args[1]);
1381 if (!sa->sin_port)
1382 sa->sin_port = htons(SYSLOG_PORT);
1383
1384 if (curproxy->logfac1 == -1) {
1385 curproxy->logsrv1 = *sa;
1386 curproxy->logfac1 = facility;
1387 curproxy->loglev1 = level;
1388 }
1389 else if (curproxy->logfac2 == -1) {
1390 curproxy->logsrv2 = *sa;
1391 curproxy->logfac2 = facility;
1392 curproxy->loglev2 = level;
1393 }
1394 else {
1395 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1396 return -1;
1397 }
1398 }
1399 else {
1400 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1401 file, linenum);
1402 return -1;
1403 }
1404 }
1405 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001406 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1407 return 0;
1408
Willy Tarreaubaaee002006-06-26 02:48:02 +02001409 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001410#ifdef CONFIG_HAP_CTTPROXY
1411 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1412 file, linenum, "source", "usesrc");
1413#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001414 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1415 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001416#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417 return -1;
1418 }
1419
1420 curproxy->source_addr = *str2sa(args[1]);
1421 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001422 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001423#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001424 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1425 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1426 file, linenum, "usesrc");
1427 return -1;
1428 }
1429 if (!*args[3]) {
1430 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1431 file, linenum, "usesrc");
1432 return -1;
1433 }
1434
1435 if (!strcmp(args[3], "client")) {
1436 curproxy->options |= PR_O_TPXY_CLI;
1437 } else if (!strcmp(args[3], "clientip")) {
1438 curproxy->options |= PR_O_TPXY_CIP;
1439 } else {
1440 curproxy->options |= PR_O_TPXY_ADDR;
1441 curproxy->tproxy_addr = *str2sa(args[3]);
1442 }
1443 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001444#else /* no CTTPROXY support */
1445 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1446 file, linenum, "usesrc");
1447 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001448#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001449 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001450 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001451#ifdef CONFIG_HAP_CTTPROXY
1452 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1453 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1454 file, linenum, "usesrc", "source");
1455 return -1;
1456 }
1457#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001458 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1459 regex_t *preg;
1460 if (curproxy == &defproxy) {
1461 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1462 return -1;
1463 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001464 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1465 return 0;
1466
Willy Tarreaubaaee002006-06-26 02:48:02 +02001467 if (*(args[1]) == 0 || *(args[2]) == 0) {
1468 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1469 file, linenum, args[0]);
1470 return -1;
1471 }
1472
1473 preg = calloc(1, sizeof(regex_t));
1474 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1475 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1476 return -1;
1477 }
1478
1479 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1480 if (err) {
1481 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1482 file, linenum, *err);
1483 return -1;
1484 }
1485 }
1486 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1487 regex_t *preg;
1488 if (curproxy == &defproxy) {
1489 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1490 return -1;
1491 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001492 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1493 return 0;
1494
Willy Tarreaubaaee002006-06-26 02:48:02 +02001495 if (*(args[1]) == 0) {
1496 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1497 return -1;
1498 }
1499
1500 preg = calloc(1, sizeof(regex_t));
1501 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1502 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1503 return -1;
1504 }
1505
1506 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1507 }
1508 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1509 regex_t *preg;
1510 if (curproxy == &defproxy) {
1511 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1512 return -1;
1513 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001514 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1515 return 0;
1516
Willy Tarreaubaaee002006-06-26 02:48:02 +02001517 if (*(args[1]) == 0) {
1518 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1519 return -1;
1520 }
1521
1522 preg = calloc(1, sizeof(regex_t));
1523 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1524 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1525 return -1;
1526 }
1527
1528 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1529 }
1530 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1531 regex_t *preg;
1532 if (curproxy == &defproxy) {
1533 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1534 return -1;
1535 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001536 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1537 return 0;
1538
Willy Tarreaubaaee002006-06-26 02:48:02 +02001539 if (*(args[1]) == 0) {
1540 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1541 return -1;
1542 }
1543
1544 preg = calloc(1, sizeof(regex_t));
1545 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1546 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1547 return -1;
1548 }
1549
1550 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1551 }
1552 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1553 regex_t *preg;
1554 if (curproxy == &defproxy) {
1555 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1556 return -1;
1557 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001558 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1559 return 0;
1560
Willy Tarreaubaaee002006-06-26 02:48:02 +02001561 if (*(args[1]) == 0) {
1562 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1563 return -1;
1564 }
1565
1566 preg = calloc(1, sizeof(regex_t));
1567 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1568 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1569 return -1;
1570 }
1571
1572 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1573 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001574 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1575 regex_t *preg;
1576 if (curproxy == &defproxy) {
1577 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1578 return -1;
1579 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001580 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1581 return 0;
1582
Willy Tarreaub8750a82006-09-03 09:56:00 +02001583 if (*(args[1]) == 0) {
1584 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1585 return -1;
1586 }
1587
1588 preg = calloc(1, sizeof(regex_t));
1589 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1590 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1591 return -1;
1592 }
1593
1594 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1595 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001596 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1597 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001598 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001599 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1600 return -1;
1601 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001602 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1603 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001604
Willy Tarreau977b8e42006-12-29 14:19:17 +01001605 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001606 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1607 file, linenum, args[0]);
1608 return -1;
1609 }
1610
1611 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001612 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001613 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1614 }
1615
1616 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1617 }
1618 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1619 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001620 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001621 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1622 return -1;
1623 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001624 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1625 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001626
Willy Tarreau977b8e42006-12-29 14:19:17 +01001627 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001628 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1629 file, linenum, args[0]);
1630 return -1;
1631 }
1632
1633 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001634 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001635 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1636 }
1637
1638 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001640 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1641 regex_t *preg;
1642 if (curproxy == &defproxy) {
1643 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1644 return -1;
1645 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001646 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1647 return 0;
1648
Willy Tarreaubaaee002006-06-26 02:48:02 +02001649 if (*(args[1]) == 0 || *(args[2]) == 0) {
1650 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1651 file, linenum, args[0]);
1652 return -1;
1653 }
1654
1655 preg = calloc(1, sizeof(regex_t));
1656 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1657 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1658 return -1;
1659 }
1660
1661 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1662 if (err) {
1663 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1664 file, linenum, *err);
1665 return -1;
1666 }
1667 }
1668 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1669 regex_t *preg;
1670 if (curproxy == &defproxy) {
1671 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1672 return -1;
1673 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001674 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1675 return 0;
1676
Willy Tarreaubaaee002006-06-26 02:48:02 +02001677 if (*(args[1]) == 0) {
1678 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1679 return -1;
1680 }
1681
1682 preg = calloc(1, sizeof(regex_t));
1683 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1684 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1685 return -1;
1686 }
1687
1688 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1689 }
1690 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1691 regex_t *preg;
1692 if (curproxy == &defproxy) {
1693 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1694 return -1;
1695 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001696 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1697 return 0;
1698
Willy Tarreaubaaee002006-06-26 02:48:02 +02001699 if (*(args[1]) == 0) {
1700 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1701 return -1;
1702 }
1703
1704 preg = calloc(1, sizeof(regex_t));
1705 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1706 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1707 return -1;
1708 }
1709
1710 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1711 }
1712 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1713 regex_t *preg;
1714 if (curproxy == &defproxy) {
1715 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1716 return -1;
1717 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001718 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1719 return 0;
1720
Willy Tarreaubaaee002006-06-26 02:48:02 +02001721 if (*(args[1]) == 0) {
1722 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1723 return -1;
1724 }
1725
1726 preg = calloc(1, sizeof(regex_t));
1727 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1728 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1729 return -1;
1730 }
1731
1732 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1733 }
1734 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1735 regex_t *preg;
1736 if (curproxy == &defproxy) {
1737 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1738 return -1;
1739 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001740 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1741 return 0;
1742
Willy Tarreaubaaee002006-06-26 02:48:02 +02001743 if (*(args[1]) == 0) {
1744 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1745 return -1;
1746 }
1747
1748 preg = calloc(1, sizeof(regex_t));
1749 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1750 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1751 return -1;
1752 }
1753
1754 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1755 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001756 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1757 regex_t *preg;
1758 if (curproxy == &defproxy) {
1759 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1760 return -1;
1761 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001762 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1763 return 0;
1764
Willy Tarreaub8750a82006-09-03 09:56:00 +02001765 if (*(args[1]) == 0) {
1766 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1767 return -1;
1768 }
1769
1770 preg = calloc(1, sizeof(regex_t));
1771 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1772 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1773 return -1;
1774 }
1775
1776 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1777 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001778 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1779 if (curproxy == &defproxy) {
1780 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1781 return -1;
1782 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001783 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1784 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001785
1786 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1787 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1788 return 0;
1789 }
1790
1791 if (*(args[1]) == 0) {
1792 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1793 return -1;
1794 }
1795
1796 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1797 }
1798 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1799 regex_t *preg;
1800
1801 if (*(args[1]) == 0 || *(args[2]) == 0) {
1802 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1803 file, linenum, args[0]);
1804 return -1;
1805 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001806 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1807 return 0;
1808
Willy Tarreaubaaee002006-06-26 02:48:02 +02001809 preg = calloc(1, sizeof(regex_t));
1810 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1811 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1812 return -1;
1813 }
1814
1815 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1816 if (err) {
1817 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1818 file, linenum, *err);
1819 return -1;
1820 }
1821 }
1822 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1823 regex_t *preg;
1824 if (curproxy == &defproxy) {
1825 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1826 return -1;
1827 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001828 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1829 return 0;
1830
Willy Tarreaubaaee002006-06-26 02:48:02 +02001831 if (*(args[1]) == 0) {
1832 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1833 return -1;
1834 }
1835
1836 preg = calloc(1, sizeof(regex_t));
1837 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1838 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1839 return -1;
1840 }
1841
1842 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1843 if (err) {
1844 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1845 file, linenum, *err);
1846 return -1;
1847 }
1848 }
1849 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1850 regex_t *preg;
1851 if (curproxy == &defproxy) {
1852 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1853 return -1;
1854 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001855 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1856 return 0;
1857
Willy Tarreaubaaee002006-06-26 02:48:02 +02001858 if (*(args[1]) == 0) {
1859 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1860 return -1;
1861 }
1862
1863 preg = calloc(1, sizeof(regex_t));
1864 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1865 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1866 return -1;
1867 }
1868
1869 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1870 if (err) {
1871 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1872 file, linenum, *err);
1873 return -1;
1874 }
1875 }
1876 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1877 regex_t *preg;
1878 if (curproxy == &defproxy) {
1879 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1880 return -1;
1881 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001882 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1883 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001884
1885 if (*(args[1]) == 0 || *(args[2]) == 0) {
1886 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1887 file, linenum, args[0]);
1888 return -1;
1889 }
1890
1891 preg = calloc(1, sizeof(regex_t));
1892 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1893 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1894 return -1;
1895 }
1896
1897 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1898 if (err) {
1899 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1900 file, linenum, *err);
1901 return -1;
1902 }
1903 }
1904 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1905 regex_t *preg;
1906 if (curproxy == &defproxy) {
1907 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1908 return -1;
1909 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001910 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1911 return 0;
1912
Willy Tarreaubaaee002006-06-26 02:48:02 +02001913 if (*(args[1]) == 0) {
1914 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1915 return -1;
1916 }
1917
1918 preg = calloc(1, sizeof(regex_t));
1919 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1920 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1921 return -1;
1922 }
1923
1924 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1925 if (err) {
1926 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1927 file, linenum, *err);
1928 return -1;
1929 }
1930 }
1931 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1932 regex_t *preg;
1933 if (curproxy == &defproxy) {
1934 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1935 return -1;
1936 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001937 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1938 return 0;
1939
Willy Tarreaubaaee002006-06-26 02:48:02 +02001940 if (*(args[1]) == 0) {
1941 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1942 return -1;
1943 }
1944
1945 preg = calloc(1, sizeof(regex_t));
1946 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1947 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1948 return -1;
1949 }
1950
1951 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1952 if (err) {
1953 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1954 file, linenum, *err);
1955 return -1;
1956 }
1957 }
1958 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1959 if (curproxy == &defproxy) {
1960 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1961 return -1;
1962 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001963 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1964 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001965
1966 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1967 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1968 return 0;
1969 }
1970
1971 if (*(args[1]) == 0) {
1972 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1973 return -1;
1974 }
1975
1976 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1977 }
1978 else if (!strcmp(args[0], "errorloc") ||
1979 !strcmp(args[0], "errorloc302") ||
1980 !strcmp(args[0], "errorloc303")) { /* error location */
1981 int errnum, errlen;
1982 char *err;
1983
1984 // if (curproxy == &defproxy) {
1985 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1986 // return -1;
1987 // }
1988
Willy Tarreau977b8e42006-12-29 14:19:17 +01001989 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
1990 return 0;
1991
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01001993 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001994 return -1;
1995 }
1996
1997 errnum = atol(args[1]);
1998 if (!strcmp(args[0], "errorloc303")) {
1999 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
2000 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
2001 } else {
2002 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
2003 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2004 }
2005
Willy Tarreau0f772532006-12-23 20:51:41 +01002006 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2007 if (http_err_codes[rc] == errnum) {
2008 if (curproxy->errmsg[rc].str)
2009 free(curproxy->errmsg[rc].str);
2010 curproxy->errmsg[rc].str = err;
2011 curproxy->errmsg[rc].len = errlen;
2012 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002013 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002014 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002015
2016 if (rc >= HTTP_ERR_SIZE) {
2017 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2018 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002019 free(err);
2020 }
2021 }
2022 else {
2023 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2024 return -1;
2025 }
2026 return 0;
2027}
2028
2029
2030/*
2031 * This function reads and parses the configuration file given in the argument.
2032 * returns 0 if OK, -1 if error.
2033 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002034int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035{
2036 char thisline[256];
2037 char *line;
2038 FILE *f;
2039 int linenum = 0;
2040 char *end;
2041 char *args[MAX_LINE_ARGS];
2042 int arg;
2043 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002044 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002045 int confsect = CFG_NONE;
2046
2047 struct proxy *curproxy = NULL;
2048 struct server *newsrv = NULL;
2049
2050 if ((f=fopen(file,"r")) == NULL)
2051 return -1;
2052
2053 init_default_instance();
2054
2055 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2056 linenum++;
2057
2058 end = line + strlen(line);
2059
2060 /* skip leading spaces */
2061 while (isspace((int)*line))
2062 line++;
2063
2064 arg = 0;
2065 args[arg] = line;
2066
2067 while (*line && arg < MAX_LINE_ARGS) {
2068 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2069 * C equivalent value. Other combinations left unchanged (eg: \1).
2070 */
2071 if (*line == '\\') {
2072 int skip = 0;
2073 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2074 *line = line[1];
2075 skip = 1;
2076 }
2077 else if (line[1] == 'r') {
2078 *line = '\r';
2079 skip = 1;
2080 }
2081 else if (line[1] == 'n') {
2082 *line = '\n';
2083 skip = 1;
2084 }
2085 else if (line[1] == 't') {
2086 *line = '\t';
2087 skip = 1;
2088 }
2089 else if (line[1] == 'x') {
2090 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2091 unsigned char hex1, hex2;
2092 hex1 = toupper(line[2]) - '0';
2093 hex2 = toupper(line[3]) - '0';
2094 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2095 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2096 *line = (hex1<<4) + hex2;
2097 skip = 3;
2098 }
2099 else {
2100 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2101 return -1;
2102 }
2103 }
2104 if (skip) {
2105 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2106 end -= skip;
2107 }
2108 line++;
2109 }
2110 else if (*line == '#' || *line == '\n' || *line == '\r') {
2111 /* end of string, end of loop */
2112 *line = 0;
2113 break;
2114 }
2115 else if (isspace((int)*line)) {
2116 /* a non-escaped space is an argument separator */
2117 *line++ = 0;
2118 while (isspace((int)*line))
2119 line++;
2120 args[++arg] = line;
2121 }
2122 else {
2123 line++;
2124 }
2125 }
2126
2127 /* empty line */
2128 if (!**args)
2129 continue;
2130
2131 /* zero out remaining args */
2132 while (++arg < MAX_LINE_ARGS) {
2133 args[arg] = line;
2134 }
2135
Willy Tarreau977b8e42006-12-29 14:19:17 +01002136 if (!strcmp(args[0], "listen") ||
2137 !strcmp(args[0], "frontend") ||
2138 !strcmp(args[0], "backend") ||
2139 !strcmp(args[0], "ruleset") ||
2140 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 confsect = CFG_LISTEN;
2142 else if (!strcmp(args[0], "global")) /* global config */
2143 confsect = CFG_GLOBAL;
2144 /* else it's a section keyword */
2145
2146 switch (confsect) {
2147 case CFG_LISTEN:
2148 if (cfg_parse_listen(file, linenum, args) < 0)
2149 return -1;
2150 break;
2151 case CFG_GLOBAL:
2152 if (cfg_parse_global(file, linenum, args) < 0)
2153 return -1;
2154 break;
2155 default:
2156 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2157 return -1;
2158 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 }
2160 fclose(f);
2161
2162 /*
2163 * Now, check for the integrity of all that we have collected.
2164 */
2165
2166 /* will be needed further to delay some tasks */
2167 tv_now(&now);
2168
2169 if ((curproxy = proxy) == NULL) {
2170 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2171 file);
2172 return -1;
2173 }
2174
2175 while (curproxy != NULL) {
2176 if (curproxy->state == PR_STSTOPPED) {
2177 curproxy = curproxy->next;
2178 continue;
2179 }
2180
Willy Tarreau977b8e42006-12-29 14:19:17 +01002181 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2182 Alert("parsing %s : %s '%s' has no listen address. Please either specify a valid address on the <listen> line, or use the <bind> keyword.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002183 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002184 cfgerr++;
2185 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002186 else if (curproxy->cap & PR_CAP_BE &&
2187 ((curproxy->mode != PR_MODE_HEALTH) &&
2188 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2189 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2190 Alert("parsing %s : %s '%s' has no dispatch address and is not in transparent or balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002191 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192 cfgerr++;
2193 }
2194 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2195 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002196 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002197 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 cfgerr++;
2199 }
2200#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2201 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002202 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002203 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204 cfgerr++;
2205 }
2206#endif
2207 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002208 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002209 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 }
2211 }
2212 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2213 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002214 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002215 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002216 }
2217 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002218 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002219 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 }
2221 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002222 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002223 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 }
2225 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002226 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002227 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002229 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002230 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002231 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002232 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002233 }
2234 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2235 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2236 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2237 file, curproxy->id);
2238 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002239 }
2240 }
2241
2242 /* if a default backend was specified, let's find it */
2243 if (curproxy->defbe.name) {
2244 struct proxy *target;
2245
2246 for (target = proxy; target != NULL; target = target->next) {
2247 if (strcmp(target->id, curproxy->defbe.name) == 0)
2248 break;
2249 }
2250 if (target == NULL) {
2251 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2252 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2253 cfgerr++;
2254 } else if (target == curproxy) {
2255 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2256 cfgerr++;
2257 } else if (!(target->cap & PR_CAP_BE)) {
2258 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2259 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2260 cfgerr++;
2261 } else if (target->mode != curproxy->mode) {
2262 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2263 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2264 cfgerr++;
2265 } else {
2266 free(curproxy->defbe.name);
2267 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 }
2269 }
2270
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002271 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002272 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2273 /* map jump target for ACT_SETBE in req_rep chain */
2274 struct hdr_exp *exp;
2275 struct proxy *target;
2276 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2277 if (exp->action != ACT_SETBE)
2278 continue;
2279 for (target = proxy; target != NULL; target = target->next) {
2280 if (strcmp(target->id, exp->replace) == 0)
2281 break;
2282 }
2283 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002284 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002285 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002286 cfgerr++;
2287 } else if (target == curproxy) {
2288 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2289 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002290 } else if (!(target->cap & PR_CAP_BE)) {
2291 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002292 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002293 cfgerr++;
2294 } else if (target->mode != PR_MODE_HTTP) {
2295 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002296 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002297 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002298 } else {
2299 free((void *)exp->replace);
2300 exp->replace = (const char *)target;
2301 }
2302 }
2303 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002304 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002305 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2306 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2307 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002308 " | While not properly invalid, you will certainly encounter various problems\n"
2309 " | with such a configuration. To fix this, please ensure that all following\n"
2310 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002311 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002312 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002313
2314 if (curproxy->options & PR_O_SSL3_CHK) {
2315 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2316 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2317 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2318 }
2319
Willy Tarreau86034312006-12-29 00:10:33 +01002320 /* for backwards compatibility with "listen" instances, if
2321 * fullconn is not set but maxconn is set, then maxconn
2322 * is used.
2323 */
2324 if (!curproxy->fullconn)
2325 curproxy->fullconn = curproxy->maxconn;
2326
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 /* first, we will invert the servers list order */
2328 newsrv = NULL;
2329 while (curproxy->srv) {
2330 struct server *next;
2331
2332 next = curproxy->srv->next;
2333 curproxy->srv->next = newsrv;
2334 newsrv = curproxy->srv;
2335 if (!next)
2336 break;
2337 curproxy->srv = next;
2338 }
2339
2340 /* now, newsrv == curproxy->srv */
2341 if (newsrv) {
2342 struct server *srv;
2343 int pgcd;
2344 int act, bck;
2345
2346 /* We will factor the weights to reduce the table,
2347 * using Euclide's largest common divisor algorithm
2348 */
Willy Tarreau417fae02007-03-25 21:16:40 +02002349 pgcd = newsrv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2351 int t, w;
2352
Willy Tarreau417fae02007-03-25 21:16:40 +02002353 w = srv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002354 while (w) {
2355 t = pgcd % w;
2356 pgcd = w;
2357 w = t;
2358 }
2359 }
2360
2361 act = bck = 0;
2362 for (srv = newsrv; srv; srv = srv->next) {
Willy Tarreau417fae02007-03-25 21:16:40 +02002363 srv->eweight = srv->uweight / pgcd;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002364 if (srv->state & SRV_BACKUP)
Willy Tarreau417fae02007-03-25 21:16:40 +02002365 bck += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002366 else
Willy Tarreau417fae02007-03-25 21:16:40 +02002367 act += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 }
2369
2370 /* this is the largest map we will ever need for this servers list */
2371 if (act < bck)
2372 act = bck;
2373
2374 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2375 /* recounts servers and their weights */
2376 recount_servers(curproxy);
2377 recalc_server_map(curproxy);
2378 }
2379
2380 if (curproxy->options & PR_O_LOGASAP)
2381 curproxy->to_log &= ~LW_BYTES;
2382
Willy Tarreaubaaee002006-06-26 02:48:02 +02002383 /*
2384 * If this server supports a maxconn parameter, it needs a dedicated
2385 * tasks to fill the emptied slots when a connection leaves.
2386 */
2387 newsrv = curproxy->srv;
2388 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002389 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002390 /* Only 'minconn' was specified, or it was higher than or equal
2391 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2392 * this will avoid further useless expensive computations.
2393 */
2394 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002395 } else if (newsrv->maxconn && !newsrv->minconn) {
2396 /* minconn was not specified, so we set it to maxconn */
2397 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002398 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2399 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002400 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002401 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 }
2403
2404 if (newsrv->maxconn > 0) {
2405 struct task *t;
2406
2407 if ((t = pool_alloc(task)) == NULL) {
2408 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2409 return -1;
2410 }
2411
Willy Tarreau964c9362007-01-07 00:38:00 +01002412 t->rqnext = NULL;
2413 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002414 t->state = TASK_IDLE;
2415 t->process = process_srv_queue;
2416 t->context = newsrv;
2417 newsrv->queue_mgt = t;
2418
2419 /* never run it unless specifically woken up */
2420 tv_eternity(&t->expire);
2421 task_queue(t);
2422 }
2423 newsrv = newsrv->next;
2424 }
2425
2426 /* now we'll start this proxy's health checks if any */
2427 /* 1- count the checkers to run simultaneously */
2428 nbchk = 0;
2429 mininter = 0;
2430 newsrv = curproxy->srv;
2431 while (newsrv != NULL) {
2432 if (newsrv->state & SRV_CHECKED) {
2433 if (!mininter || mininter > newsrv->inter)
2434 mininter = newsrv->inter;
2435 nbchk++;
2436 }
2437 newsrv = newsrv->next;
2438 }
2439
2440 /* 2- start them as far as possible from each others while respecting
2441 * their own intervals. For this, we will start them after their own
2442 * interval added to the min interval divided by the number of servers,
2443 * weighted by the server's position in the list.
2444 */
2445 if (nbchk > 0) {
2446 struct task *t;
2447 int srvpos;
2448
2449 newsrv = curproxy->srv;
2450 srvpos = 0;
2451 while (newsrv != NULL) {
2452 /* should this server be checked ? */
2453 if (newsrv->state & SRV_CHECKED) {
2454 if ((t = pool_alloc(task)) == NULL) {
2455 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2456 return -1;
2457 }
2458
Willy Tarreau964c9362007-01-07 00:38:00 +01002459 t->wq = NULL;
2460 t->rqnext = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002461 t->state = TASK_IDLE;
2462 t->process = process_chk;
2463 t->context = newsrv;
2464
2465 /* check this every ms */
2466 tv_delayfrom(&t->expire, &now,
2467 newsrv->inter + mininter * srvpos / nbchk);
2468 task_queue(t);
2469 //task_wakeup(&rq, t);
2470 srvpos++;
2471 }
2472 newsrv = newsrv->next;
2473 }
2474 }
2475
2476 curproxy = curproxy->next;
2477 }
2478 if (cfgerr > 0) {
2479 Alert("Errors found in configuration file, aborting.\n");
2480 return -1;
2481 }
2482 else
2483 return 0;
2484}
2485
2486
2487
2488/*
2489 * Local variables:
2490 * c-indent-level: 8
2491 * c-basic-offset: 8
2492 * End:
2493 */