blob: 41d706611865085e51d6dbf6eb66c5958d6b036a [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
Willy Tarreaueb0c6142007-05-07 00:53:22 +020036#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/backend.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010038#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/checks.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010040#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/log.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010042#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/server.h>
44#include <proto/task.h>
45
46
Willy Tarreauf3c69202006-07-09 16:42:34 +020047/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
48 * ssl-hello-chk option to ensure that the remote server speaks SSL.
49 *
50 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
51 */
52const char sslv3_client_hello_pkt[] = {
53 "\x16" /* ContentType : 0x16 = Hanshake */
54 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
55 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
56 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
57 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
58 "\x03\x00" /* Hello Version : 0x0300 = v3 */
59 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
60 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
61 "\x00" /* Session ID length : empty (no session ID) */
62 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
63 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
64 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
65 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
66 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
67 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
68 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
69 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
70 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
71 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
72 "\x00\x38" "\x00\x39" "\x00\x3A"
73 "\x01" /* Compression Length : 0x01 = 1 byte for types */
74 "\x00" /* Compression Type : 0x00 = NULL compression */
75};
76
Willy Tarreau13943ab2006-12-31 00:24:10 +010077/* some of the most common options which are also the easiest to handle */
78static const struct {
79 const char *name;
80 unsigned int val;
81 unsigned int cap;
Willy Tarreau4fee4e92007-01-06 21:09:17 +010082 unsigned int checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +010083} cfg_opts[] =
84{
85#ifdef TPROXY
86 { "transparent", PR_O_TRANSP, PR_CAP_FE },
87#endif
Willy Tarreau4fee4e92007-01-06 21:09:17 +010088 { "redispatch", PR_O_REDISP, PR_CAP_BE, 0 },
89 { "keepalive", PR_O_KEEPALIVE, PR_CAP_NONE, 0 },
Willy Tarreau4fee4e92007-01-06 21:09:17 +010090 { "httpclose", PR_O_HTTP_CLOSE, PR_CAP_FE | PR_CAP_BE, 0 },
91 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0 },
92 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0 },
93 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0 },
94 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0 },
95 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0 },
96 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0 },
97 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0 },
98 { "persist", PR_O_PERSIST, PR_CAP_BE, 0 },
99 { "forceclose", PR_O_FORCE_CLO | PR_O_HTTP_CLOSE, PR_CAP_BE, 0 },
Willy Tarreau8f922fc2007-01-06 21:11:49 +0100100#ifdef CONFIG_HAP_TCPSPLICE
101 { "tcpsplice", PR_O_TCPSPLICE , PR_CAP_BE|PR_CAP_FE, LSTCHK_TCPSPLICE|LSTCHK_NETADM },
102#endif
103
Willy Tarreau13943ab2006-12-31 00:24:10 +0100104 { NULL, 0, 0 }
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
108static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
109int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
110int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
111
112/*
113 * converts <str> to a list of listeners which are dynamically allocated.
114 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
115 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
116 * - <port> is a numerical port from 1 to 65535 ;
117 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
118 * This can be repeated as many times as necessary, separated by a coma.
119 * The <tail> argument is a pointer to a current list which should be appended
120 * to the tail of the new list. The pointer to the new list is returned.
121 */
122static struct listener *str2listener(char *str, struct listener *tail)
123{
124 struct listener *l;
125 char *c, *next, *range, *dupstr;
126 int port, end;
127
128 next = dupstr = strdup(str);
129
130 while (next && *next) {
131 struct sockaddr_storage ss;
132
133 str = next;
134 /* 1) look for the end of the first address */
135 if ((next = strrchr(str, ',')) != NULL) {
136 *next++ = 0;
137 }
138
139 /* 2) look for the addr/port delimiter, it's the last colon. */
140 if ((range = strrchr(str, ':')) == NULL) {
141 Alert("Missing port number: '%s'\n", str);
142 goto fail;
143 }
144
145 *range++ = 0;
146
147 if (strrchr(str, ':') != NULL) {
148 /* IPv6 address contains ':' */
149 memset(&ss, 0, sizeof(ss));
150 ss.ss_family = AF_INET6;
151
152 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
153 Alert("Invalid server address: '%s'\n", str);
154 goto fail;
155 }
156 }
157 else {
158 memset(&ss, 0, sizeof(ss));
159 ss.ss_family = AF_INET;
160
161 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
162 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
163 }
164 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
165 struct hostent *he;
166
167 if ((he = gethostbyname(str)) == NULL) {
168 Alert("Invalid server name: '%s'\n", str);
169 goto fail;
170 }
171 else
172 ((struct sockaddr_in *)&ss)->sin_addr =
173 *(struct in_addr *) *(he->h_addr_list);
174 }
175 }
176
177 /* 3) look for the port-end delimiter */
178 if ((c = strchr(range, '-')) != NULL) {
179 *c++ = 0;
180 end = atol(c);
181 }
182 else {
183 end = atol(range);
184 }
185
186 port = atol(range);
187
188 if (port < 1 || port > 65535) {
189 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
190 goto fail;
191 }
192
193 if (end < 1 || end > 65535) {
194 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
195 goto fail;
196 }
197
198 for (; port <= end; port++) {
199 l = (struct listener *)calloc(1, sizeof(struct listener));
200 l->next = tail;
201 tail = l;
202
203 l->fd = -1;
204 l->addr = ss;
205 if (ss.ss_family == AF_INET6)
206 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
207 else
208 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
209
210 } /* end for(port) */
211 } /* end while(next) */
212 free(dupstr);
213 return tail;
214 fail:
215 free(dupstr);
216 return NULL;
217}
218
Willy Tarreau977b8e42006-12-29 14:19:17 +0100219/*
220 * Sends a warning if proxy <proxy> does not have at least one of the
221 * capabilities in <cap>. An optionnal <hint> may be added at the end
222 * of the warning to help the user. Returns 1 if a warning was emitted
223 * or 0 if the condition is valid.
224 */
225int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, char *arg, char *hint)
226{
227 char *msg;
228
229 switch (cap) {
230 case PR_CAP_BE: msg = "no backend"; break;
231 case PR_CAP_FE: msg = "no frontend"; break;
232 case PR_CAP_RS: msg = "no ruleset"; break;
233 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
234 default: msg = "not enough"; break;
235 }
236
237 if (!(proxy->cap & cap)) {
238 Warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100239 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
Willy Tarreau977b8e42006-12-29 14:19:17 +0100240 return 1;
241 }
242 return 0;
243}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244
245/*
246 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
247 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200248int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249{
250
251 if (!strcmp(args[0], "global")) { /* new section */
252 /* no option, nothing special to do */
253 return 0;
254 }
255 else if (!strcmp(args[0], "daemon")) {
256 global.mode |= MODE_DAEMON;
257 }
258 else if (!strcmp(args[0], "debug")) {
259 global.mode |= MODE_DEBUG;
260 }
261 else if (!strcmp(args[0], "noepoll")) {
262 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
263 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200264 else if (!strcmp(args[0], "nosepoll")) {
265 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
266 }
267 else if (!strcmp(args[0], "nokqueue")) {
268 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
269 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 else if (!strcmp(args[0], "nopoll")) {
271 cfg_polling_mechanism &= ~POLL_USE_POLL;
272 }
273 else if (!strcmp(args[0], "quiet")) {
274 global.mode |= MODE_QUIET;
275 }
276 else if (!strcmp(args[0], "stats")) {
277 global.mode |= MODE_STATS;
278 }
279 else if (!strcmp(args[0], "uid")) {
280 if (global.uid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200281 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 return 0;
283 }
284 if (*(args[1]) == 0) {
285 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
286 return -1;
287 }
288 global.uid = atol(args[1]);
289 }
290 else if (!strcmp(args[0], "gid")) {
291 if (global.gid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200292 Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 return 0;
294 }
295 if (*(args[1]) == 0) {
296 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
297 return -1;
298 }
299 global.gid = atol(args[1]);
300 }
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200301 /* user/group name handling */
302 else if (!strcmp(args[0], "user")) {
303 struct passwd *ha_user;
304 if (global.uid != 0) {
305 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
306 return 0;
307 }
308 errno = 0;
309 ha_user = getpwnam(args[1]);
310 if (ha_user != NULL) {
311 global.uid = (int)ha_user->pw_uid;
312 }
313 else {
314 Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
315 exit(1);
316 }
317 }
318 else if (!strcmp(args[0], "group")) {
319 struct group *ha_group;
320 if (global.gid != 0) {
321 Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
322 return 0;
323 }
324 errno = 0;
325 ha_group = getgrnam(args[1]);
326 if (ha_group != NULL) {
327 global.gid = (int)ha_group->gr_gid;
328 }
329 else {
330 Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
331 exit(1);
332 }
333 }
334 /* end of user/group name handling*/
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 else if (!strcmp(args[0], "nbproc")) {
336 if (global.nbproc != 0) {
337 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
338 return 0;
339 }
340 if (*(args[1]) == 0) {
341 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
342 return -1;
343 }
344 global.nbproc = atol(args[1]);
345 }
346 else if (!strcmp(args[0], "maxconn")) {
347 if (global.maxconn != 0) {
348 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
349 return 0;
350 }
351 if (*(args[1]) == 0) {
352 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
353 return -1;
354 }
355 global.maxconn = atol(args[1]);
356#ifdef SYSTEM_MAXCONN
357 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
358 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);
359 global.maxconn = DEFAULT_MAXCONN;
360 }
361#endif /* SYSTEM_MAXCONN */
362 }
363 else if (!strcmp(args[0], "ulimit-n")) {
364 if (global.rlimit_nofile != 0) {
365 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
366 return 0;
367 }
368 if (*(args[1]) == 0) {
369 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
370 return -1;
371 }
372 global.rlimit_nofile = atol(args[1]);
373 }
374 else if (!strcmp(args[0], "chroot")) {
375 if (global.chroot != NULL) {
376 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
377 return 0;
378 }
379 if (*(args[1]) == 0) {
380 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
381 return -1;
382 }
383 global.chroot = strdup(args[1]);
384 }
385 else if (!strcmp(args[0], "pidfile")) {
386 if (global.pidfile != NULL) {
387 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
388 return 0;
389 }
390 if (*(args[1]) == 0) {
391 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
392 return -1;
393 }
394 global.pidfile = strdup(args[1]);
395 }
396 else if (!strcmp(args[0], "log")) { /* syslog server address */
397 struct sockaddr_in *sa;
398 int facility, level;
399
400 if (*(args[1]) == 0 || *(args[2]) == 0) {
401 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
402 return -1;
403 }
404
405 facility = get_log_facility(args[2]);
406 if (facility < 0) {
407 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
408 exit(1);
409 }
410
411 level = 7; /* max syslog level = debug */
412 if (*(args[3])) {
413 level = get_log_level(args[3]);
414 if (level < 0) {
415 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
416 exit(1);
417 }
418 }
419
420 sa = str2sa(args[1]);
421 if (!sa->sin_port)
422 sa->sin_port = htons(SYSLOG_PORT);
423
424 if (global.logfac1 == -1) {
425 global.logsrv1 = *sa;
426 global.logfac1 = facility;
427 global.loglev1 = level;
428 }
429 else if (global.logfac2 == -1) {
430 global.logsrv2 = *sa;
431 global.logfac2 = facility;
432 global.loglev2 = level;
433 }
434 else {
435 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
436 return -1;
437 }
438
439 }
440 else {
441 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
442 return -1;
443 }
444 return 0;
445}
446
447
448static void init_default_instance()
449{
450 memset(&defproxy, 0, sizeof(defproxy));
451 defproxy.mode = PR_MODE_TCP;
452 defproxy.state = PR_STNEW;
453 defproxy.maxconn = cfg_maxpconn;
454 defproxy.conn_retries = CONN_RETRIES;
455 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
456}
457
458/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100459 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
460 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200462int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463{
464 static struct proxy *curproxy = NULL;
465 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200466 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467 int rc;
468
Willy Tarreau977b8e42006-12-29 14:19:17 +0100469 if (!strcmp(args[0], "listen"))
470 rc = PR_CAP_LISTEN;
471 else if (!strcmp(args[0], "frontend"))
472 rc = PR_CAP_FE | PR_CAP_RS;
473 else if (!strcmp(args[0], "backend"))
474 rc = PR_CAP_BE | PR_CAP_RS;
475 else if (!strcmp(args[0], "ruleset"))
476 rc = PR_CAP_RS;
477 else
478 rc = PR_CAP_NONE;
479
480 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481 if (!*args[1]) {
482 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
483 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
484 file, linenum, args[0]);
485 return -1;
486 }
487
488 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
489 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
490 return -1;
491 }
492
493 curproxy->next = proxy;
494 proxy = curproxy;
495 LIST_INIT(&curproxy->pendconns);
Willy Tarreaueb0c6142007-05-07 00:53:22 +0200496 LIST_INIT(&curproxy->acl);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +0200497 LIST_INIT(&curproxy->block_cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498
499 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100500 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501
502 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100503 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 curproxy->listen = str2listener(args[2], curproxy->listen);
505 if (!curproxy->listen)
506 return -1;
507 global.maxsock++;
508 }
509
510 /* set default values */
511 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512 curproxy->options = defproxy.options;
Willy Tarreau7ac51f62007-03-25 16:00:04 +0200513 curproxy->except_net = defproxy.except_net;
514 curproxy->except_mask = defproxy.except_mask;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreau977b8e42006-12-29 14:19:17 +0100516 if (curproxy->cap & PR_CAP_FE) {
517 curproxy->maxconn = defproxy.maxconn;
518
519 /* initialize error relocations */
520 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
521 if (defproxy.errmsg[rc].str)
522 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
523 }
524
525 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
526 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527
Willy Tarreau977b8e42006-12-29 14:19:17 +0100528 if (curproxy->cap & PR_CAP_BE) {
529 curproxy->fullconn = defproxy.fullconn;
530 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531
Willy Tarreau977b8e42006-12-29 14:19:17 +0100532 if (defproxy.check_req)
533 curproxy->check_req = strdup(defproxy.check_req);
534 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535
Willy Tarreau977b8e42006-12-29 14:19:17 +0100536 if (defproxy.cookie_name)
537 curproxy->cookie_name = strdup(defproxy.cookie_name);
538 curproxy->cookie_len = defproxy.cookie_len;
539 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540
Willy Tarreau977b8e42006-12-29 14:19:17 +0100541 if (curproxy->cap & PR_CAP_RS) {
542 if (defproxy.capture_name)
543 curproxy->capture_name = strdup(defproxy.capture_name);
544 curproxy->capture_namelen = defproxy.capture_namelen;
545 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100546 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547
Willy Tarreau977b8e42006-12-29 14:19:17 +0100548 if (curproxy->cap & PR_CAP_FE) {
549 curproxy->clitimeout = defproxy.clitimeout;
550 curproxy->uri_auth = defproxy.uri_auth;
551 curproxy->mon_net = defproxy.mon_net;
552 curproxy->mon_mask = defproxy.mon_mask;
553 if (defproxy.monitor_uri)
554 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
555 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100556 if (defproxy.defbe.name)
557 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100558 }
559
560 if (curproxy->cap & PR_CAP_BE) {
561 curproxy->contimeout = defproxy.contimeout;
562 curproxy->srvtimeout = defproxy.srvtimeout;
563 curproxy->source_addr = defproxy.source_addr;
564 }
565
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566 curproxy->mode = defproxy.mode;
567 curproxy->logfac1 = defproxy.logfac1;
568 curproxy->logsrv1 = defproxy.logsrv1;
569 curproxy->loglev1 = defproxy.loglev1;
570 curproxy->logfac2 = defproxy.logfac2;
571 curproxy->logsrv2 = defproxy.logsrv2;
572 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200574
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575 return 0;
576 }
577 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
578 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100579 /* FIXME-20070101: we should do this too at the end of the
580 * config parsing to free all default values.
581 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 if (defproxy.check_req) free(defproxy.check_req);
583 if (defproxy.cookie_name) free(defproxy.cookie_name);
584 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200585 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100586 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100587
588 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
589 if (defproxy.errmsg[rc].len)
590 free(defproxy.errmsg[rc].str);
591 }
592
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 /* we cannot free uri_auth because it might already be used */
594 init_default_instance();
595 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100596 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597 return 0;
598 }
599 else if (curproxy == NULL) {
600 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
601 return -1;
602 }
603
Willy Tarreau977b8e42006-12-29 14:19:17 +0100604
605 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606 if (!strcmp(args[0], "bind")) { /* new listen addresses */
607 if (curproxy == &defproxy) {
608 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
609 return -1;
610 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100611 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
612 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
614 if (strchr(args[1], ':') == NULL) {
615 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
616 file, linenum, args[0]);
617 return -1;
618 }
619 curproxy->listen = str2listener(args[1], curproxy->listen);
620 if (!curproxy->listen)
621 return -1;
622 global.maxsock++;
623 return 0;
624 }
625 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
626 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
627 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
628 file, linenum, args[0]);
629 return -1;
630 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100631 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
632 return 0;
633
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 /* flush useless bits */
635 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
636 return 0;
637 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200638 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100639 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
640 return 0;
641
Willy Tarreau1c47f852006-07-09 08:22:27 +0200642 if (!*args[1]) {
643 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
644 file, linenum, args[0]);
645 return -1;
646 }
647
648 if (curproxy->monitor_uri != NULL)
649 free(curproxy->monitor_uri);
650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100651 curproxy->monitor_uri_len = strlen(args[1]);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200652 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653 memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200654 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
655
656 return 0;
657 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200658 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
659 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
660 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
661 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
662 else {
663 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
664 return -1;
665 }
666 }
667 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
668 curproxy->state = PR_STSTOPPED;
669 }
670 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
671 curproxy->state = PR_STNEW;
672 }
Willy Tarreaueb0c6142007-05-07 00:53:22 +0200673 else if (!strcmp(args[0], "acl")) { /* add an ACL */
674 if (parse_acl((const char **)args + 1, &curproxy->acl) == NULL) {
675 Alert("parsing [%s:%d] : error detected while parsing ACL '%s'.\n",
676 file, linenum, args[1]);
677 return -1;
678 }
679 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 else if (!strcmp(args[0], "cookie")) { /* cookie name */
681 int cur_arg;
682 // if (curproxy == &defproxy) {
683 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
684 // return -1;
685 // }
686
Willy Tarreau977b8e42006-12-29 14:19:17 +0100687 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
688 return 0;
689
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 if (curproxy->cookie_name != NULL) {
691 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
692 // file, linenum);
693 // return 0;
694 free(curproxy->cookie_name);
695 }
696
697 if (*(args[1]) == 0) {
698 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
699 file, linenum, args[0]);
700 return -1;
701 }
702 curproxy->cookie_name = strdup(args[1]);
703 curproxy->cookie_len = strlen(curproxy->cookie_name);
704
705 cur_arg = 2;
706 while (*(args[cur_arg])) {
707 if (!strcmp(args[cur_arg], "rewrite")) {
708 curproxy->options |= PR_O_COOK_RW;
709 }
710 else if (!strcmp(args[cur_arg], "indirect")) {
711 curproxy->options |= PR_O_COOK_IND;
712 }
713 else if (!strcmp(args[cur_arg], "insert")) {
714 curproxy->options |= PR_O_COOK_INS;
715 }
716 else if (!strcmp(args[cur_arg], "nocache")) {
717 curproxy->options |= PR_O_COOK_NOC;
718 }
719 else if (!strcmp(args[cur_arg], "postonly")) {
720 curproxy->options |= PR_O_COOK_POST;
721 }
722 else if (!strcmp(args[cur_arg], "prefix")) {
723 curproxy->options |= PR_O_COOK_PFX;
724 }
725 else {
726 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
727 file, linenum, args[0]);
728 return -1;
729 }
730 cur_arg++;
731 }
732 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
733 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
734 file, linenum);
735 return -1;
736 }
737
738 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
739 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
740 file, linenum);
741 return -1;
742 }
743 }/* end else if (!strcmp(args[0], "cookie")) */
744 else if (!strcmp(args[0], "appsession")) { /* cookie name */
745 // if (curproxy == &defproxy) {
746 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
747 // return -1;
748 // }
749
Willy Tarreau977b8e42006-12-29 14:19:17 +0100750 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
751 return 0;
752
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 if (curproxy->appsession_name != NULL) {
754 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
755 // file, linenum);
756 // return 0;
757 free(curproxy->appsession_name);
758 }
759
760 if (*(args[5]) == 0) {
761 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
762 file, linenum, args[0]);
763 return -1;
764 }
765 have_appsession = 1;
766 curproxy->appsession_name = strdup(args[1]);
767 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
768 curproxy->appsession_len = atoi(args[3]);
769 curproxy->appsession_timeout = atoi(args[5]);
770 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
771 if (rc) {
772 Alert("Error Init Appsession Hashtable.\n");
773 return -1;
774 }
775 } /* Url App Session */
776 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100777 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
778 return 0;
779
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
781 // if (curproxy == &defproxy) {
782 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
783 // return -1;
784 // }
785
786 if (curproxy->capture_name != NULL) {
787 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
788 // file, linenum, args[0]);
789 // return 0;
790 free(curproxy->capture_name);
791 }
792
793 if (*(args[4]) == 0) {
794 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
795 file, linenum, args[0]);
796 return -1;
797 }
798 curproxy->capture_name = strdup(args[2]);
799 curproxy->capture_namelen = strlen(curproxy->capture_name);
800 curproxy->capture_len = atol(args[4]);
801 if (curproxy->capture_len >= CAPTURE_LEN) {
802 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
803 file, linenum, CAPTURE_LEN - 1);
804 curproxy->capture_len = CAPTURE_LEN - 1;
805 }
806 curproxy->to_log |= LW_COOKIE;
807 }
808 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
809 struct cap_hdr *hdr;
810
811 if (curproxy == &defproxy) {
812 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
813 return -1;
814 }
815
816 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
817 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
818 file, linenum, args[0], args[1]);
819 return -1;
820 }
821
822 hdr = calloc(sizeof(struct cap_hdr), 1);
823 hdr->next = curproxy->req_cap;
824 hdr->name = strdup(args[3]);
825 hdr->namelen = strlen(args[3]);
826 hdr->len = atol(args[5]);
827 hdr->index = curproxy->nb_req_cap++;
828 curproxy->req_cap = hdr;
829 curproxy->to_log |= LW_REQHDR;
830 }
831 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
832 struct cap_hdr *hdr;
833
834 if (curproxy == &defproxy) {
835 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
836 return -1;
837 }
838
839 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
840 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
841 file, linenum, args[0], args[1]);
842 return -1;
843 }
844 hdr = calloc(sizeof(struct cap_hdr), 1);
845 hdr->next = curproxy->rsp_cap;
846 hdr->name = strdup(args[3]);
847 hdr->namelen = strlen(args[3]);
848 hdr->len = atol(args[5]);
849 hdr->index = curproxy->nb_rsp_cap++;
850 curproxy->rsp_cap = hdr;
851 curproxy->to_log |= LW_RSPHDR;
852 }
853 else {
854 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
855 file, linenum, args[0]);
856 return -1;
857 }
858 }
859 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
860 if (curproxy->contimeout != defproxy.contimeout) {
861 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
862 return 0;
863 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100864 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
865 return 0;
866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 if (*(args[1]) == 0) {
868 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
869 file, linenum, args[0]);
870 return -1;
871 }
872 curproxy->contimeout = atol(args[1]);
873 }
874 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
875 if (curproxy->clitimeout != defproxy.clitimeout) {
876 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
877 file, linenum, args[0]);
878 return 0;
879 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100880 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
881 return 0;
882
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883 if (*(args[1]) == 0) {
884 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
885 file, linenum, args[0]);
886 return -1;
887 }
888 curproxy->clitimeout = atol(args[1]);
889 }
890 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
891 if (curproxy->srvtimeout != defproxy.srvtimeout) {
892 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
893 return 0;
894 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100895 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
896 return 0;
897
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898 if (*(args[1]) == 0) {
899 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
900 file, linenum, args[0]);
901 return -1;
902 }
903 curproxy->srvtimeout = atol(args[1]);
904 }
905 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100906 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
907 return 0;
908
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909 if (*(args[1]) == 0) {
910 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
911 file, linenum, args[0]);
912 return -1;
913 }
914 curproxy->conn_retries = atol(args[1]);
915 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +0200916 else if (!strcmp(args[0], "block")) { /* early blocking based on ACLs */
917 int pol = ACL_COND_NONE;
918 struct acl_cond *cond;
919
920 if (!strcmp(args[1], "if"))
921 pol = ACL_COND_IF;
922 else if (!strcmp(args[1], "unless"))
923 pol = ACL_COND_UNLESS;
924
925 if (pol == ACL_COND_NONE) {
926 Alert("parsing [%s:%d] : '%s' requires either 'if' or 'unless' followed by a condition.\n",
927 file, linenum, args[0]);
928 return -1;
929 }
930
931 if ((cond = parse_acl_cond((const char **)args + 2, &curproxy->acl, pol)) == NULL) {
932 Alert("parsing [%s:%d] : error detected while parsing blocking condition.\n",
933 file, linenum);
934 return -1;
935 }
936 LIST_ADDQ(&curproxy->block_cond, &cond->list);
937 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100939 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
940 return 0;
941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
943 curproxy->uri_auth = NULL; /* we must detach from the default config */
944
945 if (*(args[1]) == 0) {
946 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
947 return -1;
948 } else if (!strcmp(args[1], "uri")) {
949 if (*(args[2]) == 0) {
950 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
951 return -1;
952 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
953 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
954 return -1;
955 }
956 } else if (!strcmp(args[1], "realm")) {
957 if (*(args[2]) == 0) {
958 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
959 return -1;
960 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
961 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
962 return -1;
963 }
964 } else if (!strcmp(args[1], "auth")) {
965 if (*(args[2]) == 0) {
966 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
967 return -1;
968 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
969 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
970 return -1;
971 }
972 } else if (!strcmp(args[1], "scope")) {
973 if (*(args[2]) == 0) {
974 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
975 return -1;
976 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
977 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
978 return -1;
979 }
980 } else if (!strcmp(args[1], "enable")) {
981 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
982 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
983 return -1;
984 }
985 } else {
986 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
987 file, linenum, args[0]);
988 return -1;
989 }
990 }
991 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100992 int optnum;
993
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994 if (*(args[1]) == 0) {
995 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
996 return -1;
997 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100998
999 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
1000 if (!strcmp(args[1], cfg_opts[optnum].name)) {
1001 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
1002 return 0;
1003 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +01001004 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +01001005 return 0;
1006 }
1007 }
1008
1009 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010 /* generate a complete HTTP log */
1011 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
1012 else if (!strcmp(args[1], "tcplog"))
1013 /* generate a detailed TCP log */
1014 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 else if (!strcmp(args[1], "tcpka")) {
1016 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001017 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
1018 return 0;
1019
1020 if (curproxy->cap & PR_CAP_FE)
1021 curproxy->options |= PR_O_TCP_CLI_KA;
1022 if (curproxy->cap & PR_CAP_BE)
1023 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024 }
1025 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +01001026 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1027 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 /* use HTTP request to check servers' health */
1029 if (curproxy->check_req != NULL) {
1030 free(curproxy->check_req);
1031 }
1032 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +02001033 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 if (!*args[2]) { /* no argument */
1035 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
1036 curproxy->check_len = strlen(DEF_CHECK_REQ);
1037 } else if (!*args[3]) { /* one argument : URI */
1038 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
1039 curproxy->check_req = (char *)malloc(reqlen);
1040 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1041 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
1042 } else { /* more arguments : METHOD URI [HTTP_VER] */
1043 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
1044 if (*args[4])
1045 reqlen += strlen(args[4]);
1046 else
1047 reqlen += strlen("HTTP/1.0");
1048
1049 curproxy->check_req = (char *)malloc(reqlen);
1050 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1051 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
1052 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001053 }
1054 else if (!strcmp(args[1], "ssl-hello-chk")) {
1055 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001056 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1057 return 0;
1058
Willy Tarreauf3c69202006-07-09 16:42:34 +02001059 if (curproxy->check_req != NULL) {
1060 free(curproxy->check_req);
1061 }
1062 curproxy->options &= ~PR_O_HTTP_CHK;
1063 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064 }
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001065 else if (!strcmp(args[1], "forwardfor")) {
1066 /* insert x-forwarded-for field, but not for the
1067 * IP address listed as an except.
1068 */
1069 if (*(args[2])) {
1070 if (!strcmp(args[2], "except")) {
1071 if (!*args[3] || !str2net(args[3], &curproxy->except_net, &curproxy->except_mask)) {
1072 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1073 file, linenum, args[0]);
1074 return -1;
1075 }
1076 /* flush useless bits */
1077 curproxy->except_net.s_addr &= curproxy->except_mask.s_addr;
1078 } else {
1079 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1080 file, linenum, args[0]);
1081 return -1;
1082 }
1083 }
1084 curproxy->options |= PR_O_FWDFOR;
1085 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086 else {
1087 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1088 return -1;
1089 }
1090 return 0;
1091 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001092 else if (!strcmp(args[0], "default_backend")) {
1093 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
1094 return 0;
1095
1096 if (*(args[1]) == 0) {
1097 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
1098 return -1;
1099 }
1100 if (curproxy->defbe.name)
1101 free(curproxy->defbe.name);
1102 curproxy->defbe.name = strdup(args[1]);
1103 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001105 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1106 return 0;
1107
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 /* enable reconnections to dispatch */
1109 curproxy->options |= PR_O_REDISP;
1110 }
1111#ifdef TPROXY
1112 else if (!strcmp(args[0], "transparent")) {
1113 /* enable transparent proxy connections */
1114 curproxy->options |= PR_O_TRANSP;
1115 }
1116#endif
1117 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001118 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1119 return 0;
1120
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121 if (*(args[1]) == 0) {
1122 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1123 return -1;
1124 }
1125 curproxy->maxconn = atol(args[1]);
1126 }
Willy Tarreau86034312006-12-29 00:10:33 +01001127 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001128 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1129 return 0;
1130
Willy Tarreau86034312006-12-29 00:10:33 +01001131 if (*(args[1]) == 0) {
1132 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1133 return -1;
1134 }
1135 curproxy->fullconn = atol(args[1]);
1136 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1138 if (*(args[1]) == 0) {
1139 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1140 return -1;
1141 }
1142 curproxy->grace = atol(args[1]);
1143 }
1144 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1145 if (curproxy == &defproxy) {
1146 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1147 return -1;
1148 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001149 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1150 return 0;
1151
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152 if (strchr(args[1], ':') == NULL) {
1153 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1154 return -1;
1155 }
1156 curproxy->dispatch_addr = *str2sa(args[1]);
1157 }
1158 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001159 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1160 return 0;
1161
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162 if (*(args[1])) {
1163 if (!strcmp(args[1], "roundrobin")) {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001164 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 curproxy->options |= PR_O_BALANCE_RR;
1166 }
1167 else if (!strcmp(args[1], "source")) {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001168 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169 curproxy->options |= PR_O_BALANCE_SH;
1170 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001171 else if (!strcmp(args[1], "uri")) {
1172 curproxy->options &= ~PR_O_BALANCE;
1173 curproxy->options |= PR_O_BALANCE_UH;
1174 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175 else {
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001176 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin', 'source' and 'uri' options.\n", file, linenum, args[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177 return -1;
1178 }
1179 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001180 else {/* if no option is set, use round-robin by default */
1181 curproxy->options &= ~PR_O_BALANCE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001182 curproxy->options |= PR_O_BALANCE_RR;
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001183 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184 }
1185 else if (!strcmp(args[0], "server")) { /* server address */
1186 int cur_arg;
1187 char *rport;
1188 char *raddr;
1189 short realport;
1190 int do_check;
1191
1192 if (curproxy == &defproxy) {
1193 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1194 return -1;
1195 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001196 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1197 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198
1199 if (!*args[2]) {
1200 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1201 file, linenum, args[0]);
1202 return -1;
1203 }
1204 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1205 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1206 return -1;
1207 }
1208
1209 /* the servers are linked backwards first */
1210 newsrv->next = curproxy->srv;
1211 curproxy->srv = newsrv;
1212 newsrv->proxy = curproxy;
1213
1214 LIST_INIT(&newsrv->pendconns);
1215 do_check = 0;
1216 newsrv->state = SRV_RUNNING; /* early server setup */
1217 newsrv->id = strdup(args[1]);
1218
1219 /* several ways to check the port component :
1220 * - IP => port=+0, relative
1221 * - IP: => port=+0, relative
1222 * - IP:N => port=N, absolute
1223 * - IP:+N => port=+N, relative
1224 * - IP:-N => port=-N, relative
1225 */
1226 raddr = strdup(args[2]);
1227 rport = strchr(raddr, ':');
1228 if (rport) {
1229 *rport++ = 0;
1230 realport = atol(rport);
1231 if (!isdigit((int)*rport))
1232 newsrv->state |= SRV_MAPPORTS;
1233 } else {
1234 realport = 0;
1235 newsrv->state |= SRV_MAPPORTS;
1236 }
1237
1238 newsrv->addr = *str2sa(raddr);
1239 newsrv->addr.sin_port = htons(realport);
1240 free(raddr);
1241
1242 newsrv->curfd = -1; /* no health-check in progress */
1243 newsrv->inter = DEF_CHKINTR;
1244 newsrv->rise = DEF_RISETIME;
1245 newsrv->fall = DEF_FALLTIME;
1246 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
Willy Tarreau417fae02007-03-25 21:16:40 +02001247 newsrv->uweight = 1;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001248
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249 cur_arg = 3;
1250 while (*args[cur_arg]) {
1251 if (!strcmp(args[cur_arg], "cookie")) {
1252 newsrv->cookie = strdup(args[cur_arg + 1]);
1253 newsrv->cklen = strlen(args[cur_arg + 1]);
1254 cur_arg += 2;
1255 }
1256 else if (!strcmp(args[cur_arg], "rise")) {
1257 newsrv->rise = atol(args[cur_arg + 1]);
1258 newsrv->health = newsrv->rise;
1259 cur_arg += 2;
1260 }
1261 else if (!strcmp(args[cur_arg], "fall")) {
1262 newsrv->fall = atol(args[cur_arg + 1]);
1263 cur_arg += 2;
1264 }
1265 else if (!strcmp(args[cur_arg], "inter")) {
1266 newsrv->inter = atol(args[cur_arg + 1]);
1267 cur_arg += 2;
1268 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001269 else if (!strcmp(args[cur_arg], "addr")) {
1270 newsrv->check_addr = *str2sa(args[cur_arg + 1]);
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001271 cur_arg += 2;
1272 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 else if (!strcmp(args[cur_arg], "port")) {
1274 newsrv->check_port = atol(args[cur_arg + 1]);
1275 cur_arg += 2;
1276 }
1277 else if (!strcmp(args[cur_arg], "backup")) {
1278 newsrv->state |= SRV_BACKUP;
1279 cur_arg ++;
1280 }
1281 else if (!strcmp(args[cur_arg], "weight")) {
1282 int w;
1283 w = atol(args[cur_arg + 1]);
1284 if (w < 1 || w > 256) {
1285 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1286 file, linenum, newsrv->id, w);
1287 return -1;
1288 }
Willy Tarreau417fae02007-03-25 21:16:40 +02001289 newsrv->uweight = w;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001290 cur_arg += 2;
1291 }
1292 else if (!strcmp(args[cur_arg], "minconn")) {
1293 newsrv->minconn = atol(args[cur_arg + 1]);
1294 cur_arg += 2;
1295 }
1296 else if (!strcmp(args[cur_arg], "maxconn")) {
1297 newsrv->maxconn = atol(args[cur_arg + 1]);
1298 cur_arg += 2;
1299 }
1300 else if (!strcmp(args[cur_arg], "check")) {
1301 global.maxsock++;
1302 do_check = 1;
1303 cur_arg += 1;
1304 }
1305 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1306 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001307#ifdef CONFIG_HAP_CTTPROXY
1308 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1309 file, linenum, "source", "usesrc");
1310#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1312 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001313#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001314 return -1;
1315 }
1316 newsrv->state |= SRV_BIND_SRC;
1317 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1318 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001319 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001320#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001321 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001322 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1323 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001324 return -1;
1325 }
1326 if (!*args[cur_arg + 1]) {
1327 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1328 file, linenum, "usesrc");
1329 return -1;
1330 }
1331 if (!strcmp(args[cur_arg + 1], "client")) {
1332 newsrv->state |= SRV_TPROXY_CLI;
1333 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1334 newsrv->state |= SRV_TPROXY_CIP;
1335 } else {
1336 newsrv->state |= SRV_TPROXY_ADDR;
1337 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1338 }
1339 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1340 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001341#else /* no CTTPROXY support */
1342 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1343 file, linenum, "usesrc");
1344 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001345#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001346 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001348#ifdef CONFIG_HAP_CTTPROXY
1349 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1350 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1351 file, linenum, "usesrc", "source");
1352 return -1;
1353 }
1354#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001355 else {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001356 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 +02001357 file, linenum, newsrv->id);
1358 return -1;
1359 }
1360 }
1361
1362 if (do_check) {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001363 if (!newsrv->check_port && newsrv->check_addr.sin_port)
1364 newsrv->check_port = newsrv->check_addr.sin_port;
1365
Willy Tarreaubaaee002006-06-26 02:48:02 +02001366 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1367 newsrv->check_port = realport; /* by default */
1368 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001369 /* not yet valid, because no port was set on
1370 * the server either. We'll check if we have
1371 * a known port on the first listener.
1372 */
1373 struct listener *l;
1374 l = curproxy->listen;
1375 if (l) {
1376 int port;
1377 port = (l->addr.ss_family == AF_INET6)
1378 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1379 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1380 newsrv->check_port = port;
1381 }
1382 }
1383 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1385 file, linenum, newsrv->id);
1386 return -1;
1387 }
1388 newsrv->state |= SRV_CHECKED;
1389 }
1390
1391 if (newsrv->state & SRV_BACKUP)
1392 curproxy->srv_bck++;
1393 else
1394 curproxy->srv_act++;
1395 }
1396 else if (!strcmp(args[0], "log")) { /* syslog server address */
1397 struct sockaddr_in *sa;
1398 int facility;
1399
1400 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1401 curproxy->logfac1 = global.logfac1;
1402 curproxy->logsrv1 = global.logsrv1;
1403 curproxy->loglev1 = global.loglev1;
1404 curproxy->logfac2 = global.logfac2;
1405 curproxy->logsrv2 = global.logsrv2;
1406 curproxy->loglev2 = global.loglev2;
1407 }
1408 else if (*(args[1]) && *(args[2])) {
1409 int level;
1410
1411 facility = get_log_facility(args[2]);
1412 if (facility < 0) {
1413 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1414 exit(1);
1415 }
1416
1417 level = 7; /* max syslog level = debug */
1418 if (*(args[3])) {
1419 level = get_log_level(args[3]);
1420 if (level < 0) {
1421 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1422 exit(1);
1423 }
1424 }
1425
1426 sa = str2sa(args[1]);
1427 if (!sa->sin_port)
1428 sa->sin_port = htons(SYSLOG_PORT);
1429
1430 if (curproxy->logfac1 == -1) {
1431 curproxy->logsrv1 = *sa;
1432 curproxy->logfac1 = facility;
1433 curproxy->loglev1 = level;
1434 }
1435 else if (curproxy->logfac2 == -1) {
1436 curproxy->logsrv2 = *sa;
1437 curproxy->logfac2 = facility;
1438 curproxy->loglev2 = level;
1439 }
1440 else {
1441 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1442 return -1;
1443 }
1444 }
1445 else {
1446 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1447 file, linenum);
1448 return -1;
1449 }
1450 }
1451 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001452 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1453 return 0;
1454
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001456#ifdef CONFIG_HAP_CTTPROXY
1457 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1458 file, linenum, "source", "usesrc");
1459#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1461 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001462#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001463 return -1;
1464 }
1465
1466 curproxy->source_addr = *str2sa(args[1]);
1467 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001468 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001469#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001470 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1471 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1472 file, linenum, "usesrc");
1473 return -1;
1474 }
1475 if (!*args[3]) {
1476 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1477 file, linenum, "usesrc");
1478 return -1;
1479 }
1480
1481 if (!strcmp(args[3], "client")) {
1482 curproxy->options |= PR_O_TPXY_CLI;
1483 } else if (!strcmp(args[3], "clientip")) {
1484 curproxy->options |= PR_O_TPXY_CIP;
1485 } else {
1486 curproxy->options |= PR_O_TPXY_ADDR;
1487 curproxy->tproxy_addr = *str2sa(args[3]);
1488 }
1489 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001490#else /* no CTTPROXY support */
1491 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1492 file, linenum, "usesrc");
1493 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001494#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001495 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001496 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001497#ifdef CONFIG_HAP_CTTPROXY
1498 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1499 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1500 file, linenum, "usesrc", "source");
1501 return -1;
1502 }
1503#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1505 regex_t *preg;
1506 if (curproxy == &defproxy) {
1507 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1508 return -1;
1509 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001510 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1511 return 0;
1512
Willy Tarreaubaaee002006-06-26 02:48:02 +02001513 if (*(args[1]) == 0 || *(args[2]) == 0) {
1514 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1515 file, linenum, args[0]);
1516 return -1;
1517 }
1518
1519 preg = calloc(1, sizeof(regex_t));
1520 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1521 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1522 return -1;
1523 }
1524
1525 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1526 if (err) {
1527 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1528 file, linenum, *err);
1529 return -1;
1530 }
1531 }
1532 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1533 regex_t *preg;
1534 if (curproxy == &defproxy) {
1535 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1536 return -1;
1537 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001538 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1539 return 0;
1540
Willy Tarreaubaaee002006-06-26 02:48:02 +02001541 if (*(args[1]) == 0) {
1542 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1543 return -1;
1544 }
1545
1546 preg = calloc(1, sizeof(regex_t));
1547 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1548 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1549 return -1;
1550 }
1551
1552 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1553 }
1554 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1555 regex_t *preg;
1556 if (curproxy == &defproxy) {
1557 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1558 return -1;
1559 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001560 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1561 return 0;
1562
Willy Tarreaubaaee002006-06-26 02:48:02 +02001563 if (*(args[1]) == 0) {
1564 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1565 return -1;
1566 }
1567
1568 preg = calloc(1, sizeof(regex_t));
1569 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1570 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1571 return -1;
1572 }
1573
1574 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1575 }
1576 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1577 regex_t *preg;
1578 if (curproxy == &defproxy) {
1579 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1580 return -1;
1581 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001582 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1583 return 0;
1584
Willy Tarreaubaaee002006-06-26 02:48:02 +02001585 if (*(args[1]) == 0) {
1586 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1587 return -1;
1588 }
1589
1590 preg = calloc(1, sizeof(regex_t));
1591 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1592 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1593 return -1;
1594 }
1595
1596 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1597 }
1598 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1599 regex_t *preg;
1600 if (curproxy == &defproxy) {
1601 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1602 return -1;
1603 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001604 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1605 return 0;
1606
Willy Tarreaubaaee002006-06-26 02:48:02 +02001607 if (*(args[1]) == 0) {
1608 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1609 return -1;
1610 }
1611
1612 preg = calloc(1, sizeof(regex_t));
1613 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1614 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1615 return -1;
1616 }
1617
1618 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1619 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001620 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1621 regex_t *preg;
1622 if (curproxy == &defproxy) {
1623 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1624 return -1;
1625 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001626 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1627 return 0;
1628
Willy Tarreaub8750a82006-09-03 09:56:00 +02001629 if (*(args[1]) == 0) {
1630 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1631 return -1;
1632 }
1633
1634 preg = calloc(1, sizeof(regex_t));
1635 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1636 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1637 return -1;
1638 }
1639
1640 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1641 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001642 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1643 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001644 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001645 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1646 return -1;
1647 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001648 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1649 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001650
Willy Tarreau977b8e42006-12-29 14:19:17 +01001651 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001652 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1653 file, linenum, args[0]);
1654 return -1;
1655 }
1656
1657 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001658 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001659 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1660 }
1661
1662 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1663 }
1664 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1665 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001666 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001667 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1668 return -1;
1669 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001670 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1671 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001672
Willy Tarreau977b8e42006-12-29 14:19:17 +01001673 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001674 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1675 file, linenum, args[0]);
1676 return -1;
1677 }
1678
1679 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001680 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001681 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1682 }
1683
1684 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1685 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001686 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1687 regex_t *preg;
1688 if (curproxy == &defproxy) {
1689 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1690 return -1;
1691 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001692 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1693 return 0;
1694
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695 if (*(args[1]) == 0 || *(args[2]) == 0) {
1696 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1697 file, linenum, args[0]);
1698 return -1;
1699 }
1700
1701 preg = calloc(1, sizeof(regex_t));
1702 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1703 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1704 return -1;
1705 }
1706
1707 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1708 if (err) {
1709 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1710 file, linenum, *err);
1711 return -1;
1712 }
1713 }
1714 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1715 regex_t *preg;
1716 if (curproxy == &defproxy) {
1717 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1718 return -1;
1719 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001720 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1721 return 0;
1722
Willy Tarreaubaaee002006-06-26 02:48:02 +02001723 if (*(args[1]) == 0) {
1724 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1725 return -1;
1726 }
1727
1728 preg = calloc(1, sizeof(regex_t));
1729 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1730 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1731 return -1;
1732 }
1733
1734 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1735 }
1736 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1737 regex_t *preg;
1738 if (curproxy == &defproxy) {
1739 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1740 return -1;
1741 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001742 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1743 return 0;
1744
Willy Tarreaubaaee002006-06-26 02:48:02 +02001745 if (*(args[1]) == 0) {
1746 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1747 return -1;
1748 }
1749
1750 preg = calloc(1, sizeof(regex_t));
1751 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1752 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1753 return -1;
1754 }
1755
1756 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1757 }
1758 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1759 regex_t *preg;
1760 if (curproxy == &defproxy) {
1761 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1762 return -1;
1763 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001764 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1765 return 0;
1766
Willy Tarreaubaaee002006-06-26 02:48:02 +02001767 if (*(args[1]) == 0) {
1768 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1769 return -1;
1770 }
1771
1772 preg = calloc(1, sizeof(regex_t));
1773 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1774 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1775 return -1;
1776 }
1777
1778 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1779 }
1780 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1781 regex_t *preg;
1782 if (curproxy == &defproxy) {
1783 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1784 return -1;
1785 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001786 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1787 return 0;
1788
Willy Tarreaubaaee002006-06-26 02:48:02 +02001789 if (*(args[1]) == 0) {
1790 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1791 return -1;
1792 }
1793
1794 preg = calloc(1, sizeof(regex_t));
1795 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1796 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1797 return -1;
1798 }
1799
1800 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1801 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001802 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1803 regex_t *preg;
1804 if (curproxy == &defproxy) {
1805 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1806 return -1;
1807 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001808 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1809 return 0;
1810
Willy Tarreaub8750a82006-09-03 09:56:00 +02001811 if (*(args[1]) == 0) {
1812 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1813 return -1;
1814 }
1815
1816 preg = calloc(1, sizeof(regex_t));
1817 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1818 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1819 return -1;
1820 }
1821
1822 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1823 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001824 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1825 if (curproxy == &defproxy) {
1826 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1827 return -1;
1828 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001829 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1830 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001831
1832 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1833 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1834 return 0;
1835 }
1836
1837 if (*(args[1]) == 0) {
1838 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1839 return -1;
1840 }
1841
1842 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1843 }
1844 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1845 regex_t *preg;
1846
1847 if (*(args[1]) == 0 || *(args[2]) == 0) {
1848 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1849 file, linenum, args[0]);
1850 return -1;
1851 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001852 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1853 return 0;
1854
Willy Tarreaubaaee002006-06-26 02:48:02 +02001855 preg = calloc(1, sizeof(regex_t));
1856 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1857 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1858 return -1;
1859 }
1860
1861 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1862 if (err) {
1863 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1864 file, linenum, *err);
1865 return -1;
1866 }
1867 }
1868 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1869 regex_t *preg;
1870 if (curproxy == &defproxy) {
1871 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1872 return -1;
1873 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001874 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1875 return 0;
1876
Willy Tarreaubaaee002006-06-26 02:48:02 +02001877 if (*(args[1]) == 0) {
1878 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1879 return -1;
1880 }
1881
1882 preg = calloc(1, sizeof(regex_t));
1883 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1884 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1885 return -1;
1886 }
1887
1888 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1889 if (err) {
1890 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1891 file, linenum, *err);
1892 return -1;
1893 }
1894 }
1895 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1896 regex_t *preg;
1897 if (curproxy == &defproxy) {
1898 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1899 return -1;
1900 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001901 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1902 return 0;
1903
Willy Tarreaubaaee002006-06-26 02:48:02 +02001904 if (*(args[1]) == 0) {
1905 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1906 return -1;
1907 }
1908
1909 preg = calloc(1, sizeof(regex_t));
1910 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1911 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1912 return -1;
1913 }
1914
1915 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1916 if (err) {
1917 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1918 file, linenum, *err);
1919 return -1;
1920 }
1921 }
1922 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1923 regex_t *preg;
1924 if (curproxy == &defproxy) {
1925 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1926 return -1;
1927 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001928 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1929 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001930
1931 if (*(args[1]) == 0 || *(args[2]) == 0) {
1932 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1933 file, linenum, args[0]);
1934 return -1;
1935 }
1936
1937 preg = calloc(1, sizeof(regex_t));
1938 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1939 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1940 return -1;
1941 }
1942
1943 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1944 if (err) {
1945 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1946 file, linenum, *err);
1947 return -1;
1948 }
1949 }
1950 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1951 regex_t *preg;
1952 if (curproxy == &defproxy) {
1953 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1954 return -1;
1955 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001956 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1957 return 0;
1958
Willy Tarreaubaaee002006-06-26 02:48:02 +02001959 if (*(args[1]) == 0) {
1960 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1961 return -1;
1962 }
1963
1964 preg = calloc(1, sizeof(regex_t));
1965 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1966 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1967 return -1;
1968 }
1969
1970 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1971 if (err) {
1972 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1973 file, linenum, *err);
1974 return -1;
1975 }
1976 }
1977 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1978 regex_t *preg;
1979 if (curproxy == &defproxy) {
1980 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1981 return -1;
1982 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001983 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1984 return 0;
1985
Willy Tarreaubaaee002006-06-26 02:48:02 +02001986 if (*(args[1]) == 0) {
1987 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1988 return -1;
1989 }
1990
1991 preg = calloc(1, sizeof(regex_t));
1992 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1993 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1994 return -1;
1995 }
1996
1997 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1998 if (err) {
1999 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
2000 file, linenum, *err);
2001 return -1;
2002 }
2003 }
2004 else if (!strcmp(args[0], "rspadd")) { /* add response header */
2005 if (curproxy == &defproxy) {
2006 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2007 return -1;
2008 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002009 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
2010 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011
2012 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
2013 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
2014 return 0;
2015 }
2016
2017 if (*(args[1]) == 0) {
2018 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
2019 return -1;
2020 }
2021
2022 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
2023 }
2024 else if (!strcmp(args[0], "errorloc") ||
2025 !strcmp(args[0], "errorloc302") ||
2026 !strcmp(args[0], "errorloc303")) { /* error location */
2027 int errnum, errlen;
2028 char *err;
2029
2030 // if (curproxy == &defproxy) {
2031 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2032 // return -1;
2033 // }
2034
Willy Tarreau977b8e42006-12-29 14:19:17 +01002035 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
2036 return 0;
2037
Willy Tarreaubaaee002006-06-26 02:48:02 +02002038 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01002039 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002040 return -1;
2041 }
2042
2043 errnum = atol(args[1]);
2044 if (!strcmp(args[0], "errorloc303")) {
2045 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
2046 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
2047 } else {
2048 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
2049 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2050 }
2051
Willy Tarreau0f772532006-12-23 20:51:41 +01002052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2053 if (http_err_codes[rc] == errnum) {
2054 if (curproxy->errmsg[rc].str)
2055 free(curproxy->errmsg[rc].str);
2056 curproxy->errmsg[rc].str = err;
2057 curproxy->errmsg[rc].len = errlen;
2058 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002061
2062 if (rc >= HTTP_ERR_SIZE) {
2063 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2064 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002065 free(err);
2066 }
2067 }
2068 else {
2069 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2070 return -1;
2071 }
2072 return 0;
2073}
2074
2075
2076/*
2077 * This function reads and parses the configuration file given in the argument.
2078 * returns 0 if OK, -1 if error.
2079 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002080int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002081{
2082 char thisline[256];
2083 char *line;
2084 FILE *f;
2085 int linenum = 0;
2086 char *end;
Willy Tarreau540abe42007-05-02 20:50:16 +02002087 char *args[MAX_LINE_ARGS + 1];
Willy Tarreaubaaee002006-06-26 02:48:02 +02002088 int arg;
2089 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002090 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002091 int confsect = CFG_NONE;
2092
2093 struct proxy *curproxy = NULL;
2094 struct server *newsrv = NULL;
2095
2096 if ((f=fopen(file,"r")) == NULL)
2097 return -1;
2098
2099 init_default_instance();
2100
2101 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2102 linenum++;
2103
2104 end = line + strlen(line);
2105
2106 /* skip leading spaces */
2107 while (isspace((int)*line))
2108 line++;
2109
2110 arg = 0;
2111 args[arg] = line;
2112
2113 while (*line && arg < MAX_LINE_ARGS) {
2114 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2115 * C equivalent value. Other combinations left unchanged (eg: \1).
2116 */
2117 if (*line == '\\') {
2118 int skip = 0;
2119 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2120 *line = line[1];
2121 skip = 1;
2122 }
2123 else if (line[1] == 'r') {
2124 *line = '\r';
2125 skip = 1;
2126 }
2127 else if (line[1] == 'n') {
2128 *line = '\n';
2129 skip = 1;
2130 }
2131 else if (line[1] == 't') {
2132 *line = '\t';
2133 skip = 1;
2134 }
2135 else if (line[1] == 'x') {
2136 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2137 unsigned char hex1, hex2;
2138 hex1 = toupper(line[2]) - '0';
2139 hex2 = toupper(line[3]) - '0';
2140 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2141 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2142 *line = (hex1<<4) + hex2;
2143 skip = 3;
2144 }
2145 else {
2146 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2147 return -1;
2148 }
2149 }
2150 if (skip) {
2151 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2152 end -= skip;
2153 }
2154 line++;
2155 }
2156 else if (*line == '#' || *line == '\n' || *line == '\r') {
2157 /* end of string, end of loop */
2158 *line = 0;
2159 break;
2160 }
2161 else if (isspace((int)*line)) {
2162 /* a non-escaped space is an argument separator */
2163 *line++ = 0;
2164 while (isspace((int)*line))
2165 line++;
2166 args[++arg] = line;
2167 }
2168 else {
2169 line++;
2170 }
2171 }
2172
2173 /* empty line */
2174 if (!**args)
2175 continue;
2176
Willy Tarreau540abe42007-05-02 20:50:16 +02002177 /* zero out remaining args and ensure that at least one entry
2178 * is zeroed out.
2179 */
2180 while (++arg <= MAX_LINE_ARGS) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181 args[arg] = line;
2182 }
2183
Willy Tarreau977b8e42006-12-29 14:19:17 +01002184 if (!strcmp(args[0], "listen") ||
2185 !strcmp(args[0], "frontend") ||
2186 !strcmp(args[0], "backend") ||
2187 !strcmp(args[0], "ruleset") ||
2188 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 confsect = CFG_LISTEN;
2190 else if (!strcmp(args[0], "global")) /* global config */
2191 confsect = CFG_GLOBAL;
2192 /* else it's a section keyword */
2193
2194 switch (confsect) {
2195 case CFG_LISTEN:
2196 if (cfg_parse_listen(file, linenum, args) < 0)
2197 return -1;
2198 break;
2199 case CFG_GLOBAL:
2200 if (cfg_parse_global(file, linenum, args) < 0)
2201 return -1;
2202 break;
2203 default:
2204 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2205 return -1;
2206 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 }
2208 fclose(f);
2209
2210 /*
2211 * Now, check for the integrity of all that we have collected.
2212 */
2213
2214 /* will be needed further to delay some tasks */
2215 tv_now(&now);
2216
2217 if ((curproxy = proxy) == NULL) {
2218 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2219 file);
2220 return -1;
2221 }
2222
2223 while (curproxy != NULL) {
2224 if (curproxy->state == PR_STSTOPPED) {
2225 curproxy = curproxy->next;
2226 continue;
2227 }
2228
Willy Tarreau977b8e42006-12-29 14:19:17 +01002229 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2230 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 +01002231 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002232 cfgerr++;
2233 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002234 else if (curproxy->cap & PR_CAP_BE &&
2235 ((curproxy->mode != PR_MODE_HEALTH) &&
2236 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2237 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2238 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 +01002239 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240 cfgerr++;
2241 }
2242 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2243 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002244 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002245 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246 cfgerr++;
2247 }
2248#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2249 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002250 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002251 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002252 cfgerr++;
2253 }
2254#endif
2255 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002256 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002257 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258 }
2259 }
2260 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2261 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002262 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002263 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002264 }
2265 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002266 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002267 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 }
2269 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002270 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002271 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002272 }
2273 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002274 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002275 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002276 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002277 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002278 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002279 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002280 }
Willy Tarreau2fcb5002007-05-08 13:35:26 +02002281 if (curproxy->options & PR_O_BALANCE_UH) {
2282 curproxy->options &= ~PR_O_BALANCE;
2283 curproxy->options |= PR_O_BALANCE_RR;
2284
2285 Warning("parsing %s : URI hash will be ignored for %s '%s'. Falling back to round robin.\n",
2286 file, proxy_type_str(curproxy), curproxy->id);
2287 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002288 }
2289 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2290 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2291 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2292 file, curproxy->id);
2293 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002294 }
2295 }
2296
2297 /* if a default backend was specified, let's find it */
2298 if (curproxy->defbe.name) {
2299 struct proxy *target;
2300
2301 for (target = proxy; target != NULL; target = target->next) {
2302 if (strcmp(target->id, curproxy->defbe.name) == 0)
2303 break;
2304 }
2305 if (target == NULL) {
2306 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2307 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2308 cfgerr++;
2309 } else if (target == curproxy) {
2310 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2311 cfgerr++;
2312 } else if (!(target->cap & PR_CAP_BE)) {
2313 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2314 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2315 cfgerr++;
2316 } else if (target->mode != curproxy->mode) {
2317 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2318 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2319 cfgerr++;
2320 } else {
2321 free(curproxy->defbe.name);
2322 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002323 }
2324 }
2325
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002326 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002327 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2328 /* map jump target for ACT_SETBE in req_rep chain */
2329 struct hdr_exp *exp;
2330 struct proxy *target;
2331 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2332 if (exp->action != ACT_SETBE)
2333 continue;
2334 for (target = proxy; target != NULL; target = target->next) {
2335 if (strcmp(target->id, exp->replace) == 0)
2336 break;
2337 }
2338 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002339 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002340 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002341 cfgerr++;
2342 } else if (target == curproxy) {
2343 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2344 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002345 } else if (!(target->cap & PR_CAP_BE)) {
2346 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002347 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002348 cfgerr++;
2349 } else if (target->mode != PR_MODE_HTTP) {
2350 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002351 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002352 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002353 } else {
2354 free((void *)exp->replace);
2355 exp->replace = (const char *)target;
2356 }
2357 }
2358 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002359 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002360 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2361 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2362 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002363 " | While not properly invalid, you will certainly encounter various problems\n"
2364 " | with such a configuration. To fix this, please ensure that all following\n"
2365 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002366 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002367 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002368
2369 if (curproxy->options & PR_O_SSL3_CHK) {
2370 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2371 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2372 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2373 }
2374
Willy Tarreau86034312006-12-29 00:10:33 +01002375 /* for backwards compatibility with "listen" instances, if
2376 * fullconn is not set but maxconn is set, then maxconn
2377 * is used.
2378 */
2379 if (!curproxy->fullconn)
2380 curproxy->fullconn = curproxy->maxconn;
2381
Willy Tarreaubaaee002006-06-26 02:48:02 +02002382 /* first, we will invert the servers list order */
2383 newsrv = NULL;
2384 while (curproxy->srv) {
2385 struct server *next;
2386
2387 next = curproxy->srv->next;
2388 curproxy->srv->next = newsrv;
2389 newsrv = curproxy->srv;
2390 if (!next)
2391 break;
2392 curproxy->srv = next;
2393 }
2394
2395 /* now, newsrv == curproxy->srv */
2396 if (newsrv) {
2397 struct server *srv;
2398 int pgcd;
2399 int act, bck;
2400
2401 /* We will factor the weights to reduce the table,
2402 * using Euclide's largest common divisor algorithm
2403 */
Willy Tarreau417fae02007-03-25 21:16:40 +02002404 pgcd = newsrv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002405 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2406 int t, w;
2407
Willy Tarreau417fae02007-03-25 21:16:40 +02002408 w = srv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002409 while (w) {
2410 t = pgcd % w;
2411 pgcd = w;
2412 w = t;
2413 }
2414 }
2415
2416 act = bck = 0;
2417 for (srv = newsrv; srv; srv = srv->next) {
Willy Tarreau417fae02007-03-25 21:16:40 +02002418 srv->eweight = srv->uweight / pgcd;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002419 if (srv->state & SRV_BACKUP)
Willy Tarreau417fae02007-03-25 21:16:40 +02002420 bck += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421 else
Willy Tarreau417fae02007-03-25 21:16:40 +02002422 act += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423 }
2424
2425 /* this is the largest map we will ever need for this servers list */
2426 if (act < bck)
2427 act = bck;
2428
2429 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2430 /* recounts servers and their weights */
2431 recount_servers(curproxy);
2432 recalc_server_map(curproxy);
2433 }
2434
2435 if (curproxy->options & PR_O_LOGASAP)
2436 curproxy->to_log &= ~LW_BYTES;
2437
Willy Tarreaubaaee002006-06-26 02:48:02 +02002438 /*
2439 * If this server supports a maxconn parameter, it needs a dedicated
2440 * tasks to fill the emptied slots when a connection leaves.
2441 */
2442 newsrv = curproxy->srv;
2443 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002444 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 /* Only 'minconn' was specified, or it was higher than or equal
2446 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2447 * this will avoid further useless expensive computations.
2448 */
2449 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002450 } else if (newsrv->maxconn && !newsrv->minconn) {
2451 /* minconn was not specified, so we set it to maxconn */
2452 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002453 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2454 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002455 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002456 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002457 }
2458
2459 if (newsrv->maxconn > 0) {
2460 struct task *t;
2461
2462 if ((t = pool_alloc(task)) == NULL) {
2463 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2464 return -1;
2465 }
2466
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002467 t->qlist.p = NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +01002468 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002469 t->state = TASK_IDLE;
2470 t->process = process_srv_queue;
2471 t->context = newsrv;
2472 newsrv->queue_mgt = t;
2473
2474 /* never run it unless specifically woken up */
2475 tv_eternity(&t->expire);
2476 task_queue(t);
2477 }
2478 newsrv = newsrv->next;
2479 }
2480
2481 /* now we'll start this proxy's health checks if any */
2482 /* 1- count the checkers to run simultaneously */
2483 nbchk = 0;
2484 mininter = 0;
2485 newsrv = curproxy->srv;
2486 while (newsrv != NULL) {
2487 if (newsrv->state & SRV_CHECKED) {
2488 if (!mininter || mininter > newsrv->inter)
2489 mininter = newsrv->inter;
2490 nbchk++;
2491 }
2492 newsrv = newsrv->next;
2493 }
2494
2495 /* 2- start them as far as possible from each others while respecting
2496 * their own intervals. For this, we will start them after their own
2497 * interval added to the min interval divided by the number of servers,
2498 * weighted by the server's position in the list.
2499 */
2500 if (nbchk > 0) {
2501 struct task *t;
2502 int srvpos;
2503
2504 newsrv = curproxy->srv;
2505 srvpos = 0;
2506 while (newsrv != NULL) {
2507 /* should this server be checked ? */
2508 if (newsrv->state & SRV_CHECKED) {
2509 if ((t = pool_alloc(task)) == NULL) {
2510 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2511 return -1;
2512 }
2513
Willy Tarreau964c9362007-01-07 00:38:00 +01002514 t->wq = NULL;
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002515 t->qlist.p = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002516 t->state = TASK_IDLE;
2517 t->process = process_chk;
2518 t->context = newsrv;
2519
2520 /* check this every ms */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002521 tv_ms_add(&t->expire, &now,
2522 newsrv->inter + mininter * srvpos / nbchk);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002523 task_queue(t);
2524 //task_wakeup(&rq, t);
2525 srvpos++;
2526 }
2527 newsrv = newsrv->next;
2528 }
2529 }
2530
2531 curproxy = curproxy->next;
2532 }
2533 if (cfgerr > 0) {
2534 Alert("Errors found in configuration file, aborting.\n");
2535 return -1;
2536 }
2537 else
2538 return 0;
2539}
2540
2541
2542
2543/*
2544 * Local variables:
2545 * c-indent-level: 8
2546 * c-basic-offset: 8
2547 * End:
2548 */