blob: c5d17440f5ed98de76e2791eab52cd7450b300f4 [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 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200263 else if (!strcmp(args[0], "nosepoll")) {
264 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
265 }
266 else if (!strcmp(args[0], "nokqueue")) {
267 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
268 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 else if (!strcmp(args[0], "nopoll")) {
270 cfg_polling_mechanism &= ~POLL_USE_POLL;
271 }
272 else if (!strcmp(args[0], "quiet")) {
273 global.mode |= MODE_QUIET;
274 }
275 else if (!strcmp(args[0], "stats")) {
276 global.mode |= MODE_STATS;
277 }
278 else if (!strcmp(args[0], "uid")) {
279 if (global.uid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200280 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281 return 0;
282 }
283 if (*(args[1]) == 0) {
284 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
285 return -1;
286 }
287 global.uid = atol(args[1]);
288 }
289 else if (!strcmp(args[0], "gid")) {
290 if (global.gid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200291 Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292 return 0;
293 }
294 if (*(args[1]) == 0) {
295 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
296 return -1;
297 }
298 global.gid = atol(args[1]);
299 }
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200300 /* user/group name handling */
301 else if (!strcmp(args[0], "user")) {
302 struct passwd *ha_user;
303 if (global.uid != 0) {
304 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
305 return 0;
306 }
307 errno = 0;
308 ha_user = getpwnam(args[1]);
309 if (ha_user != NULL) {
310 global.uid = (int)ha_user->pw_uid;
311 }
312 else {
313 Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
314 exit(1);
315 }
316 }
317 else if (!strcmp(args[0], "group")) {
318 struct group *ha_group;
319 if (global.gid != 0) {
320 Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
321 return 0;
322 }
323 errno = 0;
324 ha_group = getgrnam(args[1]);
325 if (ha_group != NULL) {
326 global.gid = (int)ha_group->gr_gid;
327 }
328 else {
329 Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
330 exit(1);
331 }
332 }
333 /* end of user/group name handling*/
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 else if (!strcmp(args[0], "nbproc")) {
335 if (global.nbproc != 0) {
336 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
337 return 0;
338 }
339 if (*(args[1]) == 0) {
340 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
341 return -1;
342 }
343 global.nbproc = atol(args[1]);
344 }
345 else if (!strcmp(args[0], "maxconn")) {
346 if (global.maxconn != 0) {
347 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
348 return 0;
349 }
350 if (*(args[1]) == 0) {
351 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
352 return -1;
353 }
354 global.maxconn = atol(args[1]);
355#ifdef SYSTEM_MAXCONN
356 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
357 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);
358 global.maxconn = DEFAULT_MAXCONN;
359 }
360#endif /* SYSTEM_MAXCONN */
361 }
362 else if (!strcmp(args[0], "ulimit-n")) {
363 if (global.rlimit_nofile != 0) {
364 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
365 return 0;
366 }
367 if (*(args[1]) == 0) {
368 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
369 return -1;
370 }
371 global.rlimit_nofile = atol(args[1]);
372 }
373 else if (!strcmp(args[0], "chroot")) {
374 if (global.chroot != NULL) {
375 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
376 return 0;
377 }
378 if (*(args[1]) == 0) {
379 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
380 return -1;
381 }
382 global.chroot = strdup(args[1]);
383 }
384 else if (!strcmp(args[0], "pidfile")) {
385 if (global.pidfile != NULL) {
386 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
387 return 0;
388 }
389 if (*(args[1]) == 0) {
390 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
391 return -1;
392 }
393 global.pidfile = strdup(args[1]);
394 }
395 else if (!strcmp(args[0], "log")) { /* syslog server address */
396 struct sockaddr_in *sa;
397 int facility, level;
398
399 if (*(args[1]) == 0 || *(args[2]) == 0) {
400 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
401 return -1;
402 }
403
404 facility = get_log_facility(args[2]);
405 if (facility < 0) {
406 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
407 exit(1);
408 }
409
410 level = 7; /* max syslog level = debug */
411 if (*(args[3])) {
412 level = get_log_level(args[3]);
413 if (level < 0) {
414 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
415 exit(1);
416 }
417 }
418
419 sa = str2sa(args[1]);
420 if (!sa->sin_port)
421 sa->sin_port = htons(SYSLOG_PORT);
422
423 if (global.logfac1 == -1) {
424 global.logsrv1 = *sa;
425 global.logfac1 = facility;
426 global.loglev1 = level;
427 }
428 else if (global.logfac2 == -1) {
429 global.logsrv2 = *sa;
430 global.logfac2 = facility;
431 global.loglev2 = level;
432 }
433 else {
434 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
435 return -1;
436 }
437
438 }
439 else {
440 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
441 return -1;
442 }
443 return 0;
444}
445
446
447static void init_default_instance()
448{
449 memset(&defproxy, 0, sizeof(defproxy));
450 defproxy.mode = PR_MODE_TCP;
451 defproxy.state = PR_STNEW;
452 defproxy.maxconn = cfg_maxpconn;
453 defproxy.conn_retries = CONN_RETRIES;
454 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
455}
456
457/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100458 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
459 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200461int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462{
463 static struct proxy *curproxy = NULL;
464 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200465 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 int rc;
467
Willy Tarreau977b8e42006-12-29 14:19:17 +0100468 if (!strcmp(args[0], "listen"))
469 rc = PR_CAP_LISTEN;
470 else if (!strcmp(args[0], "frontend"))
471 rc = PR_CAP_FE | PR_CAP_RS;
472 else if (!strcmp(args[0], "backend"))
473 rc = PR_CAP_BE | PR_CAP_RS;
474 else if (!strcmp(args[0], "ruleset"))
475 rc = PR_CAP_RS;
476 else
477 rc = PR_CAP_NONE;
478
479 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480 if (!*args[1]) {
481 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
482 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
483 file, linenum, args[0]);
484 return -1;
485 }
486
487 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
488 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
489 return -1;
490 }
491
492 curproxy->next = proxy;
493 proxy = curproxy;
494 LIST_INIT(&curproxy->pendconns);
495
496 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100497 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498
499 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100500 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501 curproxy->listen = str2listener(args[2], curproxy->listen);
502 if (!curproxy->listen)
503 return -1;
504 global.maxsock++;
505 }
506
507 /* set default values */
508 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509 curproxy->options = defproxy.options;
Willy Tarreau7ac51f62007-03-25 16:00:04 +0200510 curproxy->except_net = defproxy.except_net;
511 curproxy->except_mask = defproxy.except_mask;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512
Willy Tarreau977b8e42006-12-29 14:19:17 +0100513 if (curproxy->cap & PR_CAP_FE) {
514 curproxy->maxconn = defproxy.maxconn;
515
516 /* initialize error relocations */
517 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
518 if (defproxy.errmsg[rc].str)
519 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
520 }
521
522 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
523 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524
Willy Tarreau977b8e42006-12-29 14:19:17 +0100525 if (curproxy->cap & PR_CAP_BE) {
526 curproxy->fullconn = defproxy.fullconn;
527 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreau977b8e42006-12-29 14:19:17 +0100529 if (defproxy.check_req)
530 curproxy->check_req = strdup(defproxy.check_req);
531 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532
Willy Tarreau977b8e42006-12-29 14:19:17 +0100533 if (defproxy.cookie_name)
534 curproxy->cookie_name = strdup(defproxy.cookie_name);
535 curproxy->cookie_len = defproxy.cookie_len;
536 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau977b8e42006-12-29 14:19:17 +0100538 if (curproxy->cap & PR_CAP_RS) {
539 if (defproxy.capture_name)
540 curproxy->capture_name = strdup(defproxy.capture_name);
541 curproxy->capture_namelen = defproxy.capture_namelen;
542 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544
Willy Tarreau977b8e42006-12-29 14:19:17 +0100545 if (curproxy->cap & PR_CAP_FE) {
546 curproxy->clitimeout = defproxy.clitimeout;
547 curproxy->uri_auth = defproxy.uri_auth;
548 curproxy->mon_net = defproxy.mon_net;
549 curproxy->mon_mask = defproxy.mon_mask;
550 if (defproxy.monitor_uri)
551 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
552 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100553 if (defproxy.defbe.name)
554 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100555 }
556
557 if (curproxy->cap & PR_CAP_BE) {
558 curproxy->contimeout = defproxy.contimeout;
559 curproxy->srvtimeout = defproxy.srvtimeout;
560 curproxy->source_addr = defproxy.source_addr;
561 }
562
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563 curproxy->mode = defproxy.mode;
564 curproxy->logfac1 = defproxy.logfac1;
565 curproxy->logsrv1 = defproxy.logsrv1;
566 curproxy->loglev1 = defproxy.loglev1;
567 curproxy->logfac2 = defproxy.logfac2;
568 curproxy->logsrv2 = defproxy.logsrv2;
569 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200571
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 return 0;
573 }
574 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
575 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100576 /* FIXME-20070101: we should do this too at the end of the
577 * config parsing to free all default values.
578 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 if (defproxy.check_req) free(defproxy.check_req);
580 if (defproxy.cookie_name) free(defproxy.cookie_name);
581 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200582 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100583 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100584
585 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
586 if (defproxy.errmsg[rc].len)
587 free(defproxy.errmsg[rc].str);
588 }
589
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590 /* we cannot free uri_auth because it might already be used */
591 init_default_instance();
592 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100593 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594 return 0;
595 }
596 else if (curproxy == NULL) {
597 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
598 return -1;
599 }
600
Willy Tarreau977b8e42006-12-29 14:19:17 +0100601
602 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603 if (!strcmp(args[0], "bind")) { /* new listen addresses */
604 if (curproxy == &defproxy) {
605 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
606 return -1;
607 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100608 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
609 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610
611 if (strchr(args[1], ':') == NULL) {
612 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
613 file, linenum, args[0]);
614 return -1;
615 }
616 curproxy->listen = str2listener(args[1], curproxy->listen);
617 if (!curproxy->listen)
618 return -1;
619 global.maxsock++;
620 return 0;
621 }
622 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
623 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
624 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
625 file, linenum, args[0]);
626 return -1;
627 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100628 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
629 return 0;
630
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631 /* flush useless bits */
632 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
633 return 0;
634 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200635 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100636 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
637 return 0;
638
Willy Tarreau1c47f852006-07-09 08:22:27 +0200639 if (!*args[1]) {
640 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
641 file, linenum, args[0]);
642 return -1;
643 }
644
645 if (curproxy->monitor_uri != NULL)
646 free(curproxy->monitor_uri);
647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100648 curproxy->monitor_uri_len = strlen(args[1]);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200649 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100650 memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200651 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
652
653 return 0;
654 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
656 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
657 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
658 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
659 else {
660 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
661 return -1;
662 }
663 }
664 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
665 curproxy->state = PR_STSTOPPED;
666 }
667 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
668 curproxy->state = PR_STNEW;
669 }
670 else if (!strcmp(args[0], "cookie")) { /* cookie name */
671 int cur_arg;
672 // if (curproxy == &defproxy) {
673 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
674 // return -1;
675 // }
676
Willy Tarreau977b8e42006-12-29 14:19:17 +0100677 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
678 return 0;
679
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 if (curproxy->cookie_name != NULL) {
681 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
682 // file, linenum);
683 // return 0;
684 free(curproxy->cookie_name);
685 }
686
687 if (*(args[1]) == 0) {
688 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
689 file, linenum, args[0]);
690 return -1;
691 }
692 curproxy->cookie_name = strdup(args[1]);
693 curproxy->cookie_len = strlen(curproxy->cookie_name);
694
695 cur_arg = 2;
696 while (*(args[cur_arg])) {
697 if (!strcmp(args[cur_arg], "rewrite")) {
698 curproxy->options |= PR_O_COOK_RW;
699 }
700 else if (!strcmp(args[cur_arg], "indirect")) {
701 curproxy->options |= PR_O_COOK_IND;
702 }
703 else if (!strcmp(args[cur_arg], "insert")) {
704 curproxy->options |= PR_O_COOK_INS;
705 }
706 else if (!strcmp(args[cur_arg], "nocache")) {
707 curproxy->options |= PR_O_COOK_NOC;
708 }
709 else if (!strcmp(args[cur_arg], "postonly")) {
710 curproxy->options |= PR_O_COOK_POST;
711 }
712 else if (!strcmp(args[cur_arg], "prefix")) {
713 curproxy->options |= PR_O_COOK_PFX;
714 }
715 else {
716 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
717 file, linenum, args[0]);
718 return -1;
719 }
720 cur_arg++;
721 }
722 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
723 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
724 file, linenum);
725 return -1;
726 }
727
728 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
729 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
730 file, linenum);
731 return -1;
732 }
733 }/* end else if (!strcmp(args[0], "cookie")) */
734 else if (!strcmp(args[0], "appsession")) { /* cookie name */
735 // if (curproxy == &defproxy) {
736 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
737 // return -1;
738 // }
739
Willy Tarreau977b8e42006-12-29 14:19:17 +0100740 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
741 return 0;
742
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 if (curproxy->appsession_name != NULL) {
744 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
745 // file, linenum);
746 // return 0;
747 free(curproxy->appsession_name);
748 }
749
750 if (*(args[5]) == 0) {
751 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
752 file, linenum, args[0]);
753 return -1;
754 }
755 have_appsession = 1;
756 curproxy->appsession_name = strdup(args[1]);
757 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
758 curproxy->appsession_len = atoi(args[3]);
759 curproxy->appsession_timeout = atoi(args[5]);
760 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
761 if (rc) {
762 Alert("Error Init Appsession Hashtable.\n");
763 return -1;
764 }
765 } /* Url App Session */
766 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100767 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
768 return 0;
769
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
771 // if (curproxy == &defproxy) {
772 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
773 // return -1;
774 // }
775
776 if (curproxy->capture_name != NULL) {
777 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
778 // file, linenum, args[0]);
779 // return 0;
780 free(curproxy->capture_name);
781 }
782
783 if (*(args[4]) == 0) {
784 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
785 file, linenum, args[0]);
786 return -1;
787 }
788 curproxy->capture_name = strdup(args[2]);
789 curproxy->capture_namelen = strlen(curproxy->capture_name);
790 curproxy->capture_len = atol(args[4]);
791 if (curproxy->capture_len >= CAPTURE_LEN) {
792 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
793 file, linenum, CAPTURE_LEN - 1);
794 curproxy->capture_len = CAPTURE_LEN - 1;
795 }
796 curproxy->to_log |= LW_COOKIE;
797 }
798 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
799 struct cap_hdr *hdr;
800
801 if (curproxy == &defproxy) {
802 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
803 return -1;
804 }
805
806 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
807 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
808 file, linenum, args[0], args[1]);
809 return -1;
810 }
811
812 hdr = calloc(sizeof(struct cap_hdr), 1);
813 hdr->next = curproxy->req_cap;
814 hdr->name = strdup(args[3]);
815 hdr->namelen = strlen(args[3]);
816 hdr->len = atol(args[5]);
817 hdr->index = curproxy->nb_req_cap++;
818 curproxy->req_cap = hdr;
819 curproxy->to_log |= LW_REQHDR;
820 }
821 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
822 struct cap_hdr *hdr;
823
824 if (curproxy == &defproxy) {
825 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
826 return -1;
827 }
828
829 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
830 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
831 file, linenum, args[0], args[1]);
832 return -1;
833 }
834 hdr = calloc(sizeof(struct cap_hdr), 1);
835 hdr->next = curproxy->rsp_cap;
836 hdr->name = strdup(args[3]);
837 hdr->namelen = strlen(args[3]);
838 hdr->len = atol(args[5]);
839 hdr->index = curproxy->nb_rsp_cap++;
840 curproxy->rsp_cap = hdr;
841 curproxy->to_log |= LW_RSPHDR;
842 }
843 else {
844 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
845 file, linenum, args[0]);
846 return -1;
847 }
848 }
849 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
850 if (curproxy->contimeout != defproxy.contimeout) {
851 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
852 return 0;
853 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100854 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
855 return 0;
856
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 if (*(args[1]) == 0) {
858 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
859 file, linenum, args[0]);
860 return -1;
861 }
862 curproxy->contimeout = atol(args[1]);
863 }
864 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
865 if (curproxy->clitimeout != defproxy.clitimeout) {
866 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
867 file, linenum, args[0]);
868 return 0;
869 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100870 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
871 return 0;
872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 if (*(args[1]) == 0) {
874 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
875 file, linenum, args[0]);
876 return -1;
877 }
878 curproxy->clitimeout = atol(args[1]);
879 }
880 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
881 if (curproxy->srvtimeout != defproxy.srvtimeout) {
882 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
883 return 0;
884 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100885 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
886 return 0;
887
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 if (*(args[1]) == 0) {
889 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
890 file, linenum, args[0]);
891 return -1;
892 }
893 curproxy->srvtimeout = atol(args[1]);
894 }
895 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100896 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
897 return 0;
898
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899 if (*(args[1]) == 0) {
900 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
901 file, linenum, args[0]);
902 return -1;
903 }
904 curproxy->conn_retries = atol(args[1]);
905 }
906 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100907 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
908 return 0;
909
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
911 curproxy->uri_auth = NULL; /* we must detach from the default config */
912
913 if (*(args[1]) == 0) {
914 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
915 return -1;
916 } else if (!strcmp(args[1], "uri")) {
917 if (*(args[2]) == 0) {
918 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
919 return -1;
920 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
921 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
922 return -1;
923 }
924 } else if (!strcmp(args[1], "realm")) {
925 if (*(args[2]) == 0) {
926 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
927 return -1;
928 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
929 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
930 return -1;
931 }
932 } else if (!strcmp(args[1], "auth")) {
933 if (*(args[2]) == 0) {
934 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
935 return -1;
936 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
937 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
938 return -1;
939 }
940 } else if (!strcmp(args[1], "scope")) {
941 if (*(args[2]) == 0) {
942 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
943 return -1;
944 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
945 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
946 return -1;
947 }
948 } else if (!strcmp(args[1], "enable")) {
949 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
950 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
951 return -1;
952 }
953 } else {
954 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
955 file, linenum, args[0]);
956 return -1;
957 }
958 }
959 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100960 int optnum;
961
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 if (*(args[1]) == 0) {
963 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
964 return -1;
965 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100966
967 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
968 if (!strcmp(args[1], cfg_opts[optnum].name)) {
969 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
970 return 0;
971 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +0100972 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +0100973 return 0;
974 }
975 }
976
977 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200978 /* generate a complete HTTP log */
979 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
980 else if (!strcmp(args[1], "tcplog"))
981 /* generate a detailed TCP log */
982 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 else if (!strcmp(args[1], "tcpka")) {
984 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100985 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
986 return 0;
987
988 if (curproxy->cap & PR_CAP_FE)
989 curproxy->options |= PR_O_TCP_CLI_KA;
990 if (curproxy->cap & PR_CAP_BE)
991 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 }
993 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100994 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
995 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 /* use HTTP request to check servers' health */
997 if (curproxy->check_req != NULL) {
998 free(curproxy->check_req);
999 }
1000 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +02001001 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 if (!*args[2]) { /* no argument */
1003 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
1004 curproxy->check_len = strlen(DEF_CHECK_REQ);
1005 } else if (!*args[3]) { /* one argument : URI */
1006 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
1007 curproxy->check_req = (char *)malloc(reqlen);
1008 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1009 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
1010 } else { /* more arguments : METHOD URI [HTTP_VER] */
1011 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
1012 if (*args[4])
1013 reqlen += strlen(args[4]);
1014 else
1015 reqlen += strlen("HTTP/1.0");
1016
1017 curproxy->check_req = (char *)malloc(reqlen);
1018 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1019 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
1020 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001021 }
1022 else if (!strcmp(args[1], "ssl-hello-chk")) {
1023 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001024 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1025 return 0;
1026
Willy Tarreauf3c69202006-07-09 16:42:34 +02001027 if (curproxy->check_req != NULL) {
1028 free(curproxy->check_req);
1029 }
1030 curproxy->options &= ~PR_O_HTTP_CHK;
1031 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 }
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001033 else if (!strcmp(args[1], "forwardfor")) {
1034 /* insert x-forwarded-for field, but not for the
1035 * IP address listed as an except.
1036 */
1037 if (*(args[2])) {
1038 if (!strcmp(args[2], "except")) {
1039 if (!*args[3] || !str2net(args[3], &curproxy->except_net, &curproxy->except_mask)) {
1040 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1041 file, linenum, args[0]);
1042 return -1;
1043 }
1044 /* flush useless bits */
1045 curproxy->except_net.s_addr &= curproxy->except_mask.s_addr;
1046 } else {
1047 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1048 file, linenum, args[0]);
1049 return -1;
1050 }
1051 }
1052 curproxy->options |= PR_O_FWDFOR;
1053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 else {
1055 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1056 return -1;
1057 }
1058 return 0;
1059 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001060 else if (!strcmp(args[0], "default_backend")) {
1061 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
1062 return 0;
1063
1064 if (*(args[1]) == 0) {
1065 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
1066 return -1;
1067 }
1068 if (curproxy->defbe.name)
1069 free(curproxy->defbe.name);
1070 curproxy->defbe.name = strdup(args[1]);
1071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001073 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1074 return 0;
1075
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076 /* enable reconnections to dispatch */
1077 curproxy->options |= PR_O_REDISP;
1078 }
1079#ifdef TPROXY
1080 else if (!strcmp(args[0], "transparent")) {
1081 /* enable transparent proxy connections */
1082 curproxy->options |= PR_O_TRANSP;
1083 }
1084#endif
1085 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001086 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1087 return 0;
1088
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 if (*(args[1]) == 0) {
1090 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1091 return -1;
1092 }
1093 curproxy->maxconn = atol(args[1]);
1094 }
Willy Tarreau86034312006-12-29 00:10:33 +01001095 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001096 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1097 return 0;
1098
Willy Tarreau86034312006-12-29 00:10:33 +01001099 if (*(args[1]) == 0) {
1100 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1101 return -1;
1102 }
1103 curproxy->fullconn = atol(args[1]);
1104 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1106 if (*(args[1]) == 0) {
1107 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1108 return -1;
1109 }
1110 curproxy->grace = atol(args[1]);
1111 }
1112 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1113 if (curproxy == &defproxy) {
1114 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1115 return -1;
1116 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001117 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1118 return 0;
1119
Willy Tarreaubaaee002006-06-26 02:48:02 +02001120 if (strchr(args[1], ':') == NULL) {
1121 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1122 return -1;
1123 }
1124 curproxy->dispatch_addr = *str2sa(args[1]);
1125 }
1126 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001127 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1128 return 0;
1129
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130 if (*(args[1])) {
1131 if (!strcmp(args[1], "roundrobin")) {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001132 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133 curproxy->options |= PR_O_BALANCE_RR;
1134 }
1135 else if (!strcmp(args[1], "source")) {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001136 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137 curproxy->options |= PR_O_BALANCE_SH;
1138 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001139 else if (!strcmp(args[1], "uri")) {
1140 curproxy->options &= ~PR_O_BALANCE;
1141 curproxy->options |= PR_O_BALANCE_UH;
1142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 else {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001144 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin', 'source' and 'uri' options.\n", file, linenum, args[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001145 return -1;
1146 }
1147 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001148 else {/* if no option is set, use round-robin by default */
1149 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150 curproxy->options |= PR_O_BALANCE_RR;
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001151 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152 }
1153 else if (!strcmp(args[0], "server")) { /* server address */
1154 int cur_arg;
1155 char *rport;
1156 char *raddr;
1157 short realport;
1158 int do_check;
1159
1160 if (curproxy == &defproxy) {
1161 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1162 return -1;
1163 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001164 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1165 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166
1167 if (!*args[2]) {
1168 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1169 file, linenum, args[0]);
1170 return -1;
1171 }
1172 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1173 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1174 return -1;
1175 }
1176
1177 /* the servers are linked backwards first */
1178 newsrv->next = curproxy->srv;
1179 curproxy->srv = newsrv;
1180 newsrv->proxy = curproxy;
1181
1182 LIST_INIT(&newsrv->pendconns);
1183 do_check = 0;
1184 newsrv->state = SRV_RUNNING; /* early server setup */
1185 newsrv->id = strdup(args[1]);
1186
1187 /* several ways to check the port component :
1188 * - IP => port=+0, relative
1189 * - IP: => port=+0, relative
1190 * - IP:N => port=N, absolute
1191 * - IP:+N => port=+N, relative
1192 * - IP:-N => port=-N, relative
1193 */
1194 raddr = strdup(args[2]);
1195 rport = strchr(raddr, ':');
1196 if (rport) {
1197 *rport++ = 0;
1198 realport = atol(rport);
1199 if (!isdigit((int)*rport))
1200 newsrv->state |= SRV_MAPPORTS;
1201 } else {
1202 realport = 0;
1203 newsrv->state |= SRV_MAPPORTS;
1204 }
1205
1206 newsrv->addr = *str2sa(raddr);
1207 newsrv->addr.sin_port = htons(realport);
1208 free(raddr);
1209
1210 newsrv->curfd = -1; /* no health-check in progress */
1211 newsrv->inter = DEF_CHKINTR;
1212 newsrv->rise = DEF_RISETIME;
1213 newsrv->fall = DEF_FALLTIME;
1214 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
Willy Tarreau417fae02007-03-25 21:16:40 +02001215 newsrv->uweight = 1;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001216
Willy Tarreaubaaee002006-06-26 02:48:02 +02001217 cur_arg = 3;
1218 while (*args[cur_arg]) {
1219 if (!strcmp(args[cur_arg], "cookie")) {
1220 newsrv->cookie = strdup(args[cur_arg + 1]);
1221 newsrv->cklen = strlen(args[cur_arg + 1]);
1222 cur_arg += 2;
1223 }
1224 else if (!strcmp(args[cur_arg], "rise")) {
1225 newsrv->rise = atol(args[cur_arg + 1]);
1226 newsrv->health = newsrv->rise;
1227 cur_arg += 2;
1228 }
1229 else if (!strcmp(args[cur_arg], "fall")) {
1230 newsrv->fall = atol(args[cur_arg + 1]);
1231 cur_arg += 2;
1232 }
1233 else if (!strcmp(args[cur_arg], "inter")) {
1234 newsrv->inter = atol(args[cur_arg + 1]);
1235 cur_arg += 2;
1236 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001237 else if (!strcmp(args[cur_arg], "addr")) {
1238 newsrv->check_addr = *str2sa(args[cur_arg + 1]);
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001239 cur_arg += 2;
1240 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001241 else if (!strcmp(args[cur_arg], "port")) {
1242 newsrv->check_port = atol(args[cur_arg + 1]);
1243 cur_arg += 2;
1244 }
1245 else if (!strcmp(args[cur_arg], "backup")) {
1246 newsrv->state |= SRV_BACKUP;
1247 cur_arg ++;
1248 }
1249 else if (!strcmp(args[cur_arg], "weight")) {
1250 int w;
1251 w = atol(args[cur_arg + 1]);
1252 if (w < 1 || w > 256) {
1253 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1254 file, linenum, newsrv->id, w);
1255 return -1;
1256 }
Willy Tarreau417fae02007-03-25 21:16:40 +02001257 newsrv->uweight = w;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001258 cur_arg += 2;
1259 }
1260 else if (!strcmp(args[cur_arg], "minconn")) {
1261 newsrv->minconn = atol(args[cur_arg + 1]);
1262 cur_arg += 2;
1263 }
1264 else if (!strcmp(args[cur_arg], "maxconn")) {
1265 newsrv->maxconn = atol(args[cur_arg + 1]);
1266 cur_arg += 2;
1267 }
1268 else if (!strcmp(args[cur_arg], "check")) {
1269 global.maxsock++;
1270 do_check = 1;
1271 cur_arg += 1;
1272 }
1273 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1274 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001275#ifdef CONFIG_HAP_CTTPROXY
1276 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1277 file, linenum, "source", "usesrc");
1278#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001279 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1280 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001281#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282 return -1;
1283 }
1284 newsrv->state |= SRV_BIND_SRC;
1285 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1286 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001287 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001288#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001289 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001290 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1291 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001292 return -1;
1293 }
1294 if (!*args[cur_arg + 1]) {
1295 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1296 file, linenum, "usesrc");
1297 return -1;
1298 }
1299 if (!strcmp(args[cur_arg + 1], "client")) {
1300 newsrv->state |= SRV_TPROXY_CLI;
1301 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1302 newsrv->state |= SRV_TPROXY_CIP;
1303 } else {
1304 newsrv->state |= SRV_TPROXY_ADDR;
1305 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1306 }
1307 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1308 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001309#else /* no CTTPROXY support */
1310 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1311 file, linenum, "usesrc");
1312 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001313#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001314 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001316#ifdef CONFIG_HAP_CTTPROXY
1317 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1318 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1319 file, linenum, "usesrc", "source");
1320 return -1;
1321 }
1322#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001323 else {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001324 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 +02001325 file, linenum, newsrv->id);
1326 return -1;
1327 }
1328 }
1329
1330 if (do_check) {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001331 if (!newsrv->check_port && newsrv->check_addr.sin_port)
1332 newsrv->check_port = newsrv->check_addr.sin_port;
1333
Willy Tarreaubaaee002006-06-26 02:48:02 +02001334 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1335 newsrv->check_port = realport; /* by default */
1336 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001337 /* not yet valid, because no port was set on
1338 * the server either. We'll check if we have
1339 * a known port on the first listener.
1340 */
1341 struct listener *l;
1342 l = curproxy->listen;
1343 if (l) {
1344 int port;
1345 port = (l->addr.ss_family == AF_INET6)
1346 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1347 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1348 newsrv->check_port = port;
1349 }
1350 }
1351 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001352 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1353 file, linenum, newsrv->id);
1354 return -1;
1355 }
1356 newsrv->state |= SRV_CHECKED;
1357 }
1358
1359 if (newsrv->state & SRV_BACKUP)
1360 curproxy->srv_bck++;
1361 else
1362 curproxy->srv_act++;
1363 }
1364 else if (!strcmp(args[0], "log")) { /* syslog server address */
1365 struct sockaddr_in *sa;
1366 int facility;
1367
1368 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1369 curproxy->logfac1 = global.logfac1;
1370 curproxy->logsrv1 = global.logsrv1;
1371 curproxy->loglev1 = global.loglev1;
1372 curproxy->logfac2 = global.logfac2;
1373 curproxy->logsrv2 = global.logsrv2;
1374 curproxy->loglev2 = global.loglev2;
1375 }
1376 else if (*(args[1]) && *(args[2])) {
1377 int level;
1378
1379 facility = get_log_facility(args[2]);
1380 if (facility < 0) {
1381 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1382 exit(1);
1383 }
1384
1385 level = 7; /* max syslog level = debug */
1386 if (*(args[3])) {
1387 level = get_log_level(args[3]);
1388 if (level < 0) {
1389 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1390 exit(1);
1391 }
1392 }
1393
1394 sa = str2sa(args[1]);
1395 if (!sa->sin_port)
1396 sa->sin_port = htons(SYSLOG_PORT);
1397
1398 if (curproxy->logfac1 == -1) {
1399 curproxy->logsrv1 = *sa;
1400 curproxy->logfac1 = facility;
1401 curproxy->loglev1 = level;
1402 }
1403 else if (curproxy->logfac2 == -1) {
1404 curproxy->logsrv2 = *sa;
1405 curproxy->logfac2 = facility;
1406 curproxy->loglev2 = level;
1407 }
1408 else {
1409 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1410 return -1;
1411 }
1412 }
1413 else {
1414 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1415 file, linenum);
1416 return -1;
1417 }
1418 }
1419 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001420 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1421 return 0;
1422
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001424#ifdef CONFIG_HAP_CTTPROXY
1425 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1426 file, linenum, "source", "usesrc");
1427#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1429 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001430#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001431 return -1;
1432 }
1433
1434 curproxy->source_addr = *str2sa(args[1]);
1435 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001436 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001437#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001438 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1439 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1440 file, linenum, "usesrc");
1441 return -1;
1442 }
1443 if (!*args[3]) {
1444 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1445 file, linenum, "usesrc");
1446 return -1;
1447 }
1448
1449 if (!strcmp(args[3], "client")) {
1450 curproxy->options |= PR_O_TPXY_CLI;
1451 } else if (!strcmp(args[3], "clientip")) {
1452 curproxy->options |= PR_O_TPXY_CIP;
1453 } else {
1454 curproxy->options |= PR_O_TPXY_ADDR;
1455 curproxy->tproxy_addr = *str2sa(args[3]);
1456 }
1457 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001458#else /* no CTTPROXY support */
1459 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1460 file, linenum, "usesrc");
1461 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001462#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001463 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001465#ifdef CONFIG_HAP_CTTPROXY
1466 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1467 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1468 file, linenum, "usesrc", "source");
1469 return -1;
1470 }
1471#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001472 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1473 regex_t *preg;
1474 if (curproxy == &defproxy) {
1475 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1476 return -1;
1477 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001478 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1479 return 0;
1480
Willy Tarreaubaaee002006-06-26 02:48:02 +02001481 if (*(args[1]) == 0 || *(args[2]) == 0) {
1482 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1483 file, linenum, args[0]);
1484 return -1;
1485 }
1486
1487 preg = calloc(1, sizeof(regex_t));
1488 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1489 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1490 return -1;
1491 }
1492
1493 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1494 if (err) {
1495 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1496 file, linenum, *err);
1497 return -1;
1498 }
1499 }
1500 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1501 regex_t *preg;
1502 if (curproxy == &defproxy) {
1503 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1504 return -1;
1505 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001506 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1507 return 0;
1508
Willy Tarreaubaaee002006-06-26 02:48:02 +02001509 if (*(args[1]) == 0) {
1510 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1511 return -1;
1512 }
1513
1514 preg = calloc(1, sizeof(regex_t));
1515 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1516 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1517 return -1;
1518 }
1519
1520 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1521 }
1522 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1523 regex_t *preg;
1524 if (curproxy == &defproxy) {
1525 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1526 return -1;
1527 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001528 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1529 return 0;
1530
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531 if (*(args[1]) == 0) {
1532 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1533 return -1;
1534 }
1535
1536 preg = calloc(1, sizeof(regex_t));
1537 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1538 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1539 return -1;
1540 }
1541
1542 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1543 }
1544 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1545 regex_t *preg;
1546 if (curproxy == &defproxy) {
1547 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1548 return -1;
1549 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001550 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1551 return 0;
1552
Willy Tarreaubaaee002006-06-26 02:48:02 +02001553 if (*(args[1]) == 0) {
1554 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1555 return -1;
1556 }
1557
1558 preg = calloc(1, sizeof(regex_t));
1559 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1560 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1561 return -1;
1562 }
1563
1564 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1565 }
1566 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1567 regex_t *preg;
1568 if (curproxy == &defproxy) {
1569 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1570 return -1;
1571 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001572 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1573 return 0;
1574
Willy Tarreaubaaee002006-06-26 02:48:02 +02001575 if (*(args[1]) == 0) {
1576 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1577 return -1;
1578 }
1579
1580 preg = calloc(1, sizeof(regex_t));
1581 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1582 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1583 return -1;
1584 }
1585
1586 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1587 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001588 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1589 regex_t *preg;
1590 if (curproxy == &defproxy) {
1591 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1592 return -1;
1593 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001594 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1595 return 0;
1596
Willy Tarreaub8750a82006-09-03 09:56:00 +02001597 if (*(args[1]) == 0) {
1598 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1599 return -1;
1600 }
1601
1602 preg = calloc(1, sizeof(regex_t));
1603 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1604 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1605 return -1;
1606 }
1607
1608 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1609 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001610 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1611 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001612 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001613 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1614 return -1;
1615 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001616 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1617 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001618
Willy Tarreau977b8e42006-12-29 14:19:17 +01001619 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001620 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1621 file, linenum, args[0]);
1622 return -1;
1623 }
1624
1625 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001626 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001627 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1628 }
1629
1630 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1631 }
1632 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1633 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001634 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001635 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1636 return -1;
1637 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001638 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1639 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001640
Willy Tarreau977b8e42006-12-29 14:19:17 +01001641 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001642 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1643 file, linenum, args[0]);
1644 return -1;
1645 }
1646
1647 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001648 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001649 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1650 }
1651
1652 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1653 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001654 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1655 regex_t *preg;
1656 if (curproxy == &defproxy) {
1657 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1658 return -1;
1659 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001660 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1661 return 0;
1662
Willy Tarreaubaaee002006-06-26 02:48:02 +02001663 if (*(args[1]) == 0 || *(args[2]) == 0) {
1664 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1665 file, linenum, args[0]);
1666 return -1;
1667 }
1668
1669 preg = calloc(1, sizeof(regex_t));
1670 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1671 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1672 return -1;
1673 }
1674
1675 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1676 if (err) {
1677 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1678 file, linenum, *err);
1679 return -1;
1680 }
1681 }
1682 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1683 regex_t *preg;
1684 if (curproxy == &defproxy) {
1685 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1686 return -1;
1687 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001688 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1689 return 0;
1690
Willy Tarreaubaaee002006-06-26 02:48:02 +02001691 if (*(args[1]) == 0) {
1692 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1693 return -1;
1694 }
1695
1696 preg = calloc(1, sizeof(regex_t));
1697 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1698 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1699 return -1;
1700 }
1701
1702 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1703 }
1704 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1705 regex_t *preg;
1706 if (curproxy == &defproxy) {
1707 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1708 return -1;
1709 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001710 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1711 return 0;
1712
Willy Tarreaubaaee002006-06-26 02:48:02 +02001713 if (*(args[1]) == 0) {
1714 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1715 return -1;
1716 }
1717
1718 preg = calloc(1, sizeof(regex_t));
1719 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1720 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1721 return -1;
1722 }
1723
1724 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1725 }
1726 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1727 regex_t *preg;
1728 if (curproxy == &defproxy) {
1729 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1730 return -1;
1731 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001732 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1733 return 0;
1734
Willy Tarreaubaaee002006-06-26 02:48:02 +02001735 if (*(args[1]) == 0) {
1736 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1737 return -1;
1738 }
1739
1740 preg = calloc(1, sizeof(regex_t));
1741 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1742 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1743 return -1;
1744 }
1745
1746 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1747 }
1748 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1749 regex_t *preg;
1750 if (curproxy == &defproxy) {
1751 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1752 return -1;
1753 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001754 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1755 return 0;
1756
Willy Tarreaubaaee002006-06-26 02:48:02 +02001757 if (*(args[1]) == 0) {
1758 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1759 return -1;
1760 }
1761
1762 preg = calloc(1, sizeof(regex_t));
1763 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1764 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1765 return -1;
1766 }
1767
1768 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1769 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001770 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1771 regex_t *preg;
1772 if (curproxy == &defproxy) {
1773 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1774 return -1;
1775 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001776 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1777 return 0;
1778
Willy Tarreaub8750a82006-09-03 09:56:00 +02001779 if (*(args[1]) == 0) {
1780 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1781 return -1;
1782 }
1783
1784 preg = calloc(1, sizeof(regex_t));
1785 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1786 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1787 return -1;
1788 }
1789
1790 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1791 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001792 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1793 if (curproxy == &defproxy) {
1794 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1795 return -1;
1796 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001797 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1798 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001799
1800 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1801 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1802 return 0;
1803 }
1804
1805 if (*(args[1]) == 0) {
1806 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1807 return -1;
1808 }
1809
1810 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1811 }
1812 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1813 regex_t *preg;
1814
1815 if (*(args[1]) == 0 || *(args[2]) == 0) {
1816 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1817 file, linenum, args[0]);
1818 return -1;
1819 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001820 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1821 return 0;
1822
Willy Tarreaubaaee002006-06-26 02:48:02 +02001823 preg = calloc(1, sizeof(regex_t));
1824 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1825 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1826 return -1;
1827 }
1828
1829 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1830 if (err) {
1831 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1832 file, linenum, *err);
1833 return -1;
1834 }
1835 }
1836 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1837 regex_t *preg;
1838 if (curproxy == &defproxy) {
1839 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1840 return -1;
1841 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001842 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1843 return 0;
1844
Willy Tarreaubaaee002006-06-26 02:48:02 +02001845 if (*(args[1]) == 0) {
1846 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1847 return -1;
1848 }
1849
1850 preg = calloc(1, sizeof(regex_t));
1851 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1852 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1853 return -1;
1854 }
1855
1856 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1857 if (err) {
1858 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1859 file, linenum, *err);
1860 return -1;
1861 }
1862 }
1863 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1864 regex_t *preg;
1865 if (curproxy == &defproxy) {
1866 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1867 return -1;
1868 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001869 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1870 return 0;
1871
Willy Tarreaubaaee002006-06-26 02:48:02 +02001872 if (*(args[1]) == 0) {
1873 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1874 return -1;
1875 }
1876
1877 preg = calloc(1, sizeof(regex_t));
1878 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1879 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1880 return -1;
1881 }
1882
1883 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1884 if (err) {
1885 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1886 file, linenum, *err);
1887 return -1;
1888 }
1889 }
1890 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1891 regex_t *preg;
1892 if (curproxy == &defproxy) {
1893 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1894 return -1;
1895 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001896 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1897 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001898
1899 if (*(args[1]) == 0 || *(args[2]) == 0) {
1900 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1901 file, linenum, args[0]);
1902 return -1;
1903 }
1904
1905 preg = calloc(1, sizeof(regex_t));
1906 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1907 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1908 return -1;
1909 }
1910
1911 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1912 if (err) {
1913 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1914 file, linenum, *err);
1915 return -1;
1916 }
1917 }
1918 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1919 regex_t *preg;
1920 if (curproxy == &defproxy) {
1921 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1922 return -1;
1923 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001924 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1925 return 0;
1926
Willy Tarreaubaaee002006-06-26 02:48:02 +02001927 if (*(args[1]) == 0) {
1928 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1929 return -1;
1930 }
1931
1932 preg = calloc(1, sizeof(regex_t));
1933 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1934 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1935 return -1;
1936 }
1937
1938 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1939 if (err) {
1940 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1941 file, linenum, *err);
1942 return -1;
1943 }
1944 }
1945 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1946 regex_t *preg;
1947 if (curproxy == &defproxy) {
1948 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1949 return -1;
1950 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001951 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1952 return 0;
1953
Willy Tarreaubaaee002006-06-26 02:48:02 +02001954 if (*(args[1]) == 0) {
1955 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1956 return -1;
1957 }
1958
1959 preg = calloc(1, sizeof(regex_t));
1960 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1961 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1962 return -1;
1963 }
1964
1965 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1966 if (err) {
1967 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1968 file, linenum, *err);
1969 return -1;
1970 }
1971 }
1972 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1973 if (curproxy == &defproxy) {
1974 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1975 return -1;
1976 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001977 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1978 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001979
1980 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1981 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1982 return 0;
1983 }
1984
1985 if (*(args[1]) == 0) {
1986 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1987 return -1;
1988 }
1989
1990 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1991 }
1992 else if (!strcmp(args[0], "errorloc") ||
1993 !strcmp(args[0], "errorloc302") ||
1994 !strcmp(args[0], "errorloc303")) { /* error location */
1995 int errnum, errlen;
1996 char *err;
1997
1998 // if (curproxy == &defproxy) {
1999 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2000 // return -1;
2001 // }
2002
Willy Tarreau977b8e42006-12-29 14:19:17 +01002003 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
2004 return 0;
2005
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01002007 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008 return -1;
2009 }
2010
2011 errnum = atol(args[1]);
2012 if (!strcmp(args[0], "errorloc303")) {
2013 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
2014 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
2015 } else {
2016 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
2017 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2018 }
2019
Willy Tarreau0f772532006-12-23 20:51:41 +01002020 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2021 if (http_err_codes[rc] == errnum) {
2022 if (curproxy->errmsg[rc].str)
2023 free(curproxy->errmsg[rc].str);
2024 curproxy->errmsg[rc].str = err;
2025 curproxy->errmsg[rc].len = errlen;
2026 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002027 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002029
2030 if (rc >= HTTP_ERR_SIZE) {
2031 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2032 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002033 free(err);
2034 }
2035 }
2036 else {
2037 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2038 return -1;
2039 }
2040 return 0;
2041}
2042
2043
2044/*
2045 * This function reads and parses the configuration file given in the argument.
2046 * returns 0 if OK, -1 if error.
2047 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002048int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002049{
2050 char thisline[256];
2051 char *line;
2052 FILE *f;
2053 int linenum = 0;
2054 char *end;
Willy Tarreau540abe42007-05-02 20:50:16 +02002055 char *args[MAX_LINE_ARGS + 1];
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056 int arg;
2057 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002058 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059 int confsect = CFG_NONE;
2060
2061 struct proxy *curproxy = NULL;
2062 struct server *newsrv = NULL;
2063
2064 if ((f=fopen(file,"r")) == NULL)
2065 return -1;
2066
2067 init_default_instance();
2068
2069 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2070 linenum++;
2071
2072 end = line + strlen(line);
2073
2074 /* skip leading spaces */
2075 while (isspace((int)*line))
2076 line++;
2077
2078 arg = 0;
2079 args[arg] = line;
2080
2081 while (*line && arg < MAX_LINE_ARGS) {
2082 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2083 * C equivalent value. Other combinations left unchanged (eg: \1).
2084 */
2085 if (*line == '\\') {
2086 int skip = 0;
2087 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2088 *line = line[1];
2089 skip = 1;
2090 }
2091 else if (line[1] == 'r') {
2092 *line = '\r';
2093 skip = 1;
2094 }
2095 else if (line[1] == 'n') {
2096 *line = '\n';
2097 skip = 1;
2098 }
2099 else if (line[1] == 't') {
2100 *line = '\t';
2101 skip = 1;
2102 }
2103 else if (line[1] == 'x') {
2104 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2105 unsigned char hex1, hex2;
2106 hex1 = toupper(line[2]) - '0';
2107 hex2 = toupper(line[3]) - '0';
2108 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2109 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2110 *line = (hex1<<4) + hex2;
2111 skip = 3;
2112 }
2113 else {
2114 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2115 return -1;
2116 }
2117 }
2118 if (skip) {
2119 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2120 end -= skip;
2121 }
2122 line++;
2123 }
2124 else if (*line == '#' || *line == '\n' || *line == '\r') {
2125 /* end of string, end of loop */
2126 *line = 0;
2127 break;
2128 }
2129 else if (isspace((int)*line)) {
2130 /* a non-escaped space is an argument separator */
2131 *line++ = 0;
2132 while (isspace((int)*line))
2133 line++;
2134 args[++arg] = line;
2135 }
2136 else {
2137 line++;
2138 }
2139 }
2140
2141 /* empty line */
2142 if (!**args)
2143 continue;
2144
Willy Tarreau540abe42007-05-02 20:50:16 +02002145 /* zero out remaining args and ensure that at least one entry
2146 * is zeroed out.
2147 */
2148 while (++arg <= MAX_LINE_ARGS) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002149 args[arg] = line;
2150 }
2151
Willy Tarreau977b8e42006-12-29 14:19:17 +01002152 if (!strcmp(args[0], "listen") ||
2153 !strcmp(args[0], "frontend") ||
2154 !strcmp(args[0], "backend") ||
2155 !strcmp(args[0], "ruleset") ||
2156 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002157 confsect = CFG_LISTEN;
2158 else if (!strcmp(args[0], "global")) /* global config */
2159 confsect = CFG_GLOBAL;
2160 /* else it's a section keyword */
2161
2162 switch (confsect) {
2163 case CFG_LISTEN:
2164 if (cfg_parse_listen(file, linenum, args) < 0)
2165 return -1;
2166 break;
2167 case CFG_GLOBAL:
2168 if (cfg_parse_global(file, linenum, args) < 0)
2169 return -1;
2170 break;
2171 default:
2172 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2173 return -1;
2174 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175 }
2176 fclose(f);
2177
2178 /*
2179 * Now, check for the integrity of all that we have collected.
2180 */
2181
2182 /* will be needed further to delay some tasks */
2183 tv_now(&now);
2184
2185 if ((curproxy = proxy) == NULL) {
2186 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2187 file);
2188 return -1;
2189 }
2190
2191 while (curproxy != NULL) {
2192 if (curproxy->state == PR_STSTOPPED) {
2193 curproxy = curproxy->next;
2194 continue;
2195 }
2196
Willy Tarreau977b8e42006-12-29 14:19:17 +01002197 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2198 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 +01002199 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002200 cfgerr++;
2201 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002202 else if (curproxy->cap & PR_CAP_BE &&
2203 ((curproxy->mode != PR_MODE_HEALTH) &&
2204 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2205 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2206 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 +01002207 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002208 cfgerr++;
2209 }
2210 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2211 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002212 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002213 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 cfgerr++;
2215 }
2216#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2217 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002218 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002219 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 cfgerr++;
2221 }
2222#endif
2223 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002224 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002225 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 }
2227 }
2228 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2229 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002230 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002231 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002232 }
2233 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002234 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002235 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236 }
2237 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002238 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002239 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240 }
2241 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002242 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002243 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002244 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002245 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002246 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002247 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002248 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02002249 if (curproxy->options & PR_O_BALANCE_UH) {
2250 curproxy->options &= ~PR_O_BALANCE;
2251 curproxy->options |= PR_O_BALANCE_RR;
2252
2253 Warning("parsing %s : URI hash will be ignored for %s '%s'. Falling back to round robin.\n",
2254 file, proxy_type_str(curproxy), curproxy->id);
2255 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002256 }
2257 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2258 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2259 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2260 file, curproxy->id);
2261 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002262 }
2263 }
2264
2265 /* if a default backend was specified, let's find it */
2266 if (curproxy->defbe.name) {
2267 struct proxy *target;
2268
2269 for (target = proxy; target != NULL; target = target->next) {
2270 if (strcmp(target->id, curproxy->defbe.name) == 0)
2271 break;
2272 }
2273 if (target == NULL) {
2274 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2275 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2276 cfgerr++;
2277 } else if (target == curproxy) {
2278 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2279 cfgerr++;
2280 } else if (!(target->cap & PR_CAP_BE)) {
2281 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2282 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2283 cfgerr++;
2284 } else if (target->mode != curproxy->mode) {
2285 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2286 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2287 cfgerr++;
2288 } else {
2289 free(curproxy->defbe.name);
2290 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002291 }
2292 }
2293
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002294 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002295 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2296 /* map jump target for ACT_SETBE in req_rep chain */
2297 struct hdr_exp *exp;
2298 struct proxy *target;
2299 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2300 if (exp->action != ACT_SETBE)
2301 continue;
2302 for (target = proxy; target != NULL; target = target->next) {
2303 if (strcmp(target->id, exp->replace) == 0)
2304 break;
2305 }
2306 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002307 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002308 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002309 cfgerr++;
2310 } else if (target == curproxy) {
2311 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2312 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002313 } else if (!(target->cap & PR_CAP_BE)) {
2314 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002315 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002316 cfgerr++;
2317 } else if (target->mode != PR_MODE_HTTP) {
2318 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002319 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002320 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002321 } else {
2322 free((void *)exp->replace);
2323 exp->replace = (const char *)target;
2324 }
2325 }
2326 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002327 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002328 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2329 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2330 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002331 " | While not properly invalid, you will certainly encounter various problems\n"
2332 " | with such a configuration. To fix this, please ensure that all following\n"
2333 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002334 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002335 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002336
2337 if (curproxy->options & PR_O_SSL3_CHK) {
2338 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2339 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2340 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2341 }
2342
Willy Tarreau86034312006-12-29 00:10:33 +01002343 /* for backwards compatibility with "listen" instances, if
2344 * fullconn is not set but maxconn is set, then maxconn
2345 * is used.
2346 */
2347 if (!curproxy->fullconn)
2348 curproxy->fullconn = curproxy->maxconn;
2349
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 /* first, we will invert the servers list order */
2351 newsrv = NULL;
2352 while (curproxy->srv) {
2353 struct server *next;
2354
2355 next = curproxy->srv->next;
2356 curproxy->srv->next = newsrv;
2357 newsrv = curproxy->srv;
2358 if (!next)
2359 break;
2360 curproxy->srv = next;
2361 }
2362
2363 /* now, newsrv == curproxy->srv */
2364 if (newsrv) {
2365 struct server *srv;
2366 int pgcd;
2367 int act, bck;
2368
2369 /* We will factor the weights to reduce the table,
2370 * using Euclide's largest common divisor algorithm
2371 */
Willy Tarreau417fae02007-03-25 21:16:40 +02002372 pgcd = newsrv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002373 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2374 int t, w;
2375
Willy Tarreau417fae02007-03-25 21:16:40 +02002376 w = srv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002377 while (w) {
2378 t = pgcd % w;
2379 pgcd = w;
2380 w = t;
2381 }
2382 }
2383
2384 act = bck = 0;
2385 for (srv = newsrv; srv; srv = srv->next) {
Willy Tarreau417fae02007-03-25 21:16:40 +02002386 srv->eweight = srv->uweight / pgcd;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002387 if (srv->state & SRV_BACKUP)
Willy Tarreau417fae02007-03-25 21:16:40 +02002388 bck += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002389 else
Willy Tarreau417fae02007-03-25 21:16:40 +02002390 act += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002391 }
2392
2393 /* this is the largest map we will ever need for this servers list */
2394 if (act < bck)
2395 act = bck;
2396
2397 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2398 /* recounts servers and their weights */
2399 recount_servers(curproxy);
2400 recalc_server_map(curproxy);
2401 }
2402
2403 if (curproxy->options & PR_O_LOGASAP)
2404 curproxy->to_log &= ~LW_BYTES;
2405
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406 /*
2407 * If this server supports a maxconn parameter, it needs a dedicated
2408 * tasks to fill the emptied slots when a connection leaves.
2409 */
2410 newsrv = curproxy->srv;
2411 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002412 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002413 /* Only 'minconn' was specified, or it was higher than or equal
2414 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2415 * this will avoid further useless expensive computations.
2416 */
2417 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002418 } else if (newsrv->maxconn && !newsrv->minconn) {
2419 /* minconn was not specified, so we set it to maxconn */
2420 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002421 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2422 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002423 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002424 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002425 }
2426
2427 if (newsrv->maxconn > 0) {
2428 struct task *t;
2429
2430 if ((t = pool_alloc(task)) == NULL) {
2431 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2432 return -1;
2433 }
2434
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002435 t->qlist.p = NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +01002436 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002437 t->state = TASK_IDLE;
2438 t->process = process_srv_queue;
2439 t->context = newsrv;
2440 newsrv->queue_mgt = t;
2441
2442 /* never run it unless specifically woken up */
2443 tv_eternity(&t->expire);
2444 task_queue(t);
2445 }
2446 newsrv = newsrv->next;
2447 }
2448
2449 /* now we'll start this proxy's health checks if any */
2450 /* 1- count the checkers to run simultaneously */
2451 nbchk = 0;
2452 mininter = 0;
2453 newsrv = curproxy->srv;
2454 while (newsrv != NULL) {
2455 if (newsrv->state & SRV_CHECKED) {
2456 if (!mininter || mininter > newsrv->inter)
2457 mininter = newsrv->inter;
2458 nbchk++;
2459 }
2460 newsrv = newsrv->next;
2461 }
2462
2463 /* 2- start them as far as possible from each others while respecting
2464 * their own intervals. For this, we will start them after their own
2465 * interval added to the min interval divided by the number of servers,
2466 * weighted by the server's position in the list.
2467 */
2468 if (nbchk > 0) {
2469 struct task *t;
2470 int srvpos;
2471
2472 newsrv = curproxy->srv;
2473 srvpos = 0;
2474 while (newsrv != NULL) {
2475 /* should this server be checked ? */
2476 if (newsrv->state & SRV_CHECKED) {
2477 if ((t = pool_alloc(task)) == NULL) {
2478 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2479 return -1;
2480 }
2481
Willy Tarreau964c9362007-01-07 00:38:00 +01002482 t->wq = NULL;
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002483 t->qlist.p = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002484 t->state = TASK_IDLE;
2485 t->process = process_chk;
2486 t->context = newsrv;
2487
2488 /* check this every ms */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002489 tv_ms_add(&t->expire, &now,
2490 newsrv->inter + mininter * srvpos / nbchk);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 task_queue(t);
2492 //task_wakeup(&rq, t);
2493 srvpos++;
2494 }
2495 newsrv = newsrv->next;
2496 }
2497 }
2498
2499 curproxy = curproxy->next;
2500 }
2501 if (cfgerr > 0) {
2502 Alert("Errors found in configuration file, aborting.\n");
2503 return -1;
2504 }
2505 else
2506 return 0;
2507}
2508
2509
2510
2511/*
2512 * Local variables:
2513 * c-indent-level: 8
2514 * c-basic-offset: 8
2515 * End:
2516 */