blob: 00b490807c5af32300db1d4ccb9fe98f3846b781 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Configuration parser
3 *
Willy Tarreau0b4ed902007-03-26 00:18:40 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <netdb.h>
17#include <ctype.h>
Willy Tarreau95c20ac2007-03-25 15:39:23 +020018#include <pwd.h>
19#include <grp.h>
Willy Tarreau0b4ed902007-03-26 00:18:40 +020020#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/cfgparse.h>
23#include <common/config.h>
24#include <common/memory.h>
25#include <common/standard.h>
26#include <common/time.h>
27#include <common/uri_auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#include <types/capture.h>
30#include <types/global.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010031#include <types/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/polling.h>
33#include <types/proxy.h>
34#include <types/queue.h>
35
36#include <proto/backend.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010037#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/checks.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010039#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/log.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010041#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/server.h>
43#include <proto/task.h>
44
45
Willy Tarreauf3c69202006-07-09 16:42:34 +020046/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
47 * ssl-hello-chk option to ensure that the remote server speaks SSL.
48 *
49 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
50 */
51const char sslv3_client_hello_pkt[] = {
52 "\x16" /* ContentType : 0x16 = Hanshake */
53 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
54 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
55 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
56 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
57 "\x03\x00" /* Hello Version : 0x0300 = v3 */
58 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
59 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
60 "\x00" /* Session ID length : empty (no session ID) */
61 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
62 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
63 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
64 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
65 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
66 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
67 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
68 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
69 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
70 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
71 "\x00\x38" "\x00\x39" "\x00\x3A"
72 "\x01" /* Compression Length : 0x01 = 1 byte for types */
73 "\x00" /* Compression Type : 0x00 = NULL compression */
74};
75
Willy Tarreau13943ab2006-12-31 00:24:10 +010076/* some of the most common options which are also the easiest to handle */
77static const struct {
78 const char *name;
79 unsigned int val;
80 unsigned int cap;
Willy Tarreau4fee4e92007-01-06 21:09:17 +010081 unsigned int checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +010082} cfg_opts[] =
83{
84#ifdef TPROXY
85 { "transparent", PR_O_TRANSP, PR_CAP_FE },
86#endif
Willy Tarreau4fee4e92007-01-06 21:09:17 +010087 { "redispatch", PR_O_REDISP, PR_CAP_BE, 0 },
88 { "keepalive", PR_O_KEEPALIVE, PR_CAP_NONE, 0 },
Willy Tarreau4fee4e92007-01-06 21:09:17 +010089 { "httpclose", PR_O_HTTP_CLOSE, PR_CAP_FE | PR_CAP_BE, 0 },
90 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0 },
91 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0 },
92 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0 },
93 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0 },
94 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0 },
95 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0 },
96 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0 },
97 { "persist", PR_O_PERSIST, PR_CAP_BE, 0 },
98 { "forceclose", PR_O_FORCE_CLO | PR_O_HTTP_CLOSE, PR_CAP_BE, 0 },
Willy Tarreau8f922fc2007-01-06 21:11:49 +010099#ifdef CONFIG_HAP_TCPSPLICE
100 { "tcpsplice", PR_O_TCPSPLICE , PR_CAP_BE|PR_CAP_FE, LSTCHK_TCPSPLICE|LSTCHK_NETADM },
101#endif
102
Willy Tarreau13943ab2006-12-31 00:24:10 +0100103 { NULL, 0, 0 }
104};
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
107static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
108int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
109int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
110
111/*
112 * converts <str> to a list of listeners which are dynamically allocated.
113 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
114 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
115 * - <port> is a numerical port from 1 to 65535 ;
116 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
117 * This can be repeated as many times as necessary, separated by a coma.
118 * The <tail> argument is a pointer to a current list which should be appended
119 * to the tail of the new list. The pointer to the new list is returned.
120 */
121static struct listener *str2listener(char *str, struct listener *tail)
122{
123 struct listener *l;
124 char *c, *next, *range, *dupstr;
125 int port, end;
126
127 next = dupstr = strdup(str);
128
129 while (next && *next) {
130 struct sockaddr_storage ss;
131
132 str = next;
133 /* 1) look for the end of the first address */
134 if ((next = strrchr(str, ',')) != NULL) {
135 *next++ = 0;
136 }
137
138 /* 2) look for the addr/port delimiter, it's the last colon. */
139 if ((range = strrchr(str, ':')) == NULL) {
140 Alert("Missing port number: '%s'\n", str);
141 goto fail;
142 }
143
144 *range++ = 0;
145
146 if (strrchr(str, ':') != NULL) {
147 /* IPv6 address contains ':' */
148 memset(&ss, 0, sizeof(ss));
149 ss.ss_family = AF_INET6;
150
151 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
152 Alert("Invalid server address: '%s'\n", str);
153 goto fail;
154 }
155 }
156 else {
157 memset(&ss, 0, sizeof(ss));
158 ss.ss_family = AF_INET;
159
160 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
161 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
162 }
163 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
164 struct hostent *he;
165
166 if ((he = gethostbyname(str)) == NULL) {
167 Alert("Invalid server name: '%s'\n", str);
168 goto fail;
169 }
170 else
171 ((struct sockaddr_in *)&ss)->sin_addr =
172 *(struct in_addr *) *(he->h_addr_list);
173 }
174 }
175
176 /* 3) look for the port-end delimiter */
177 if ((c = strchr(range, '-')) != NULL) {
178 *c++ = 0;
179 end = atol(c);
180 }
181 else {
182 end = atol(range);
183 }
184
185 port = atol(range);
186
187 if (port < 1 || port > 65535) {
188 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
189 goto fail;
190 }
191
192 if (end < 1 || end > 65535) {
193 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
194 goto fail;
195 }
196
197 for (; port <= end; port++) {
198 l = (struct listener *)calloc(1, sizeof(struct listener));
199 l->next = tail;
200 tail = l;
201
202 l->fd = -1;
203 l->addr = ss;
204 if (ss.ss_family == AF_INET6)
205 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
206 else
207 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
208
209 } /* end for(port) */
210 } /* end while(next) */
211 free(dupstr);
212 return tail;
213 fail:
214 free(dupstr);
215 return NULL;
216}
217
Willy Tarreau977b8e42006-12-29 14:19:17 +0100218/*
219 * Sends a warning if proxy <proxy> does not have at least one of the
220 * capabilities in <cap>. An optionnal <hint> may be added at the end
221 * of the warning to help the user. Returns 1 if a warning was emitted
222 * or 0 if the condition is valid.
223 */
224int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, char *arg, char *hint)
225{
226 char *msg;
227
228 switch (cap) {
229 case PR_CAP_BE: msg = "no backend"; break;
230 case PR_CAP_FE: msg = "no frontend"; break;
231 case PR_CAP_RS: msg = "no ruleset"; break;
232 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
233 default: msg = "not enough"; break;
234 }
235
236 if (!(proxy->cap & cap)) {
237 Warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100238 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
Willy Tarreau977b8e42006-12-29 14:19:17 +0100239 return 1;
240 }
241 return 0;
242}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
244/*
245 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
246 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200247int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248{
249
250 if (!strcmp(args[0], "global")) { /* new section */
251 /* no option, nothing special to do */
252 return 0;
253 }
254 else if (!strcmp(args[0], "daemon")) {
255 global.mode |= MODE_DAEMON;
256 }
257 else if (!strcmp(args[0], "debug")) {
258 global.mode |= MODE_DEBUG;
259 }
260 else if (!strcmp(args[0], "noepoll")) {
261 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
262 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200263 else if (!strcmp(args[0], "nosepoll")) {
264 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
265 }
266 else if (!strcmp(args[0], "nokqueue")) {
267 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
268 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 else if (!strcmp(args[0], "nopoll")) {
270 cfg_polling_mechanism &= ~POLL_USE_POLL;
271 }
272 else if (!strcmp(args[0], "quiet")) {
273 global.mode |= MODE_QUIET;
274 }
275 else if (!strcmp(args[0], "stats")) {
276 global.mode |= MODE_STATS;
277 }
278 else if (!strcmp(args[0], "uid")) {
279 if (global.uid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200280 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281 return 0;
282 }
283 if (*(args[1]) == 0) {
284 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
285 return -1;
286 }
287 global.uid = atol(args[1]);
288 }
289 else if (!strcmp(args[0], "gid")) {
290 if (global.gid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200291 Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292 return 0;
293 }
294 if (*(args[1]) == 0) {
295 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
296 return -1;
297 }
298 global.gid = atol(args[1]);
299 }
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200300 /* user/group name handling */
301 else if (!strcmp(args[0], "user")) {
302 struct passwd *ha_user;
303 if (global.uid != 0) {
304 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
305 return 0;
306 }
307 errno = 0;
308 ha_user = getpwnam(args[1]);
309 if (ha_user != NULL) {
310 global.uid = (int)ha_user->pw_uid;
311 }
312 else {
313 Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
314 exit(1);
315 }
316 }
317 else if (!strcmp(args[0], "group")) {
318 struct group *ha_group;
319 if (global.gid != 0) {
320 Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
321 return 0;
322 }
323 errno = 0;
324 ha_group = getgrnam(args[1]);
325 if (ha_group != NULL) {
326 global.gid = (int)ha_group->gr_gid;
327 }
328 else {
329 Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
330 exit(1);
331 }
332 }
333 /* end of user/group name handling*/
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 else if (!strcmp(args[0], "nbproc")) {
335 if (global.nbproc != 0) {
336 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
337 return 0;
338 }
339 if (*(args[1]) == 0) {
340 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
341 return -1;
342 }
343 global.nbproc = atol(args[1]);
344 }
345 else if (!strcmp(args[0], "maxconn")) {
346 if (global.maxconn != 0) {
347 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
348 return 0;
349 }
350 if (*(args[1]) == 0) {
351 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
352 return -1;
353 }
354 global.maxconn = atol(args[1]);
355#ifdef SYSTEM_MAXCONN
356 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
357 Alert("parsing [%s:%d] : maxconn value %d too high for this system.\nLimiting to %d. Please use '-n' to force the value.\n", file, linenum, global.maxconn, DEFAULT_MAXCONN);
358 global.maxconn = DEFAULT_MAXCONN;
359 }
360#endif /* SYSTEM_MAXCONN */
361 }
362 else if (!strcmp(args[0], "ulimit-n")) {
363 if (global.rlimit_nofile != 0) {
364 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
365 return 0;
366 }
367 if (*(args[1]) == 0) {
368 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
369 return -1;
370 }
371 global.rlimit_nofile = atol(args[1]);
372 }
373 else if (!strcmp(args[0], "chroot")) {
374 if (global.chroot != NULL) {
375 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
376 return 0;
377 }
378 if (*(args[1]) == 0) {
379 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
380 return -1;
381 }
382 global.chroot = strdup(args[1]);
383 }
384 else if (!strcmp(args[0], "pidfile")) {
385 if (global.pidfile != NULL) {
386 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
387 return 0;
388 }
389 if (*(args[1]) == 0) {
390 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
391 return -1;
392 }
393 global.pidfile = strdup(args[1]);
394 }
395 else if (!strcmp(args[0], "log")) { /* syslog server address */
396 struct sockaddr_in *sa;
397 int facility, level;
398
399 if (*(args[1]) == 0 || *(args[2]) == 0) {
400 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
401 return -1;
402 }
403
404 facility = get_log_facility(args[2]);
405 if (facility < 0) {
406 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
407 exit(1);
408 }
409
410 level = 7; /* max syslog level = debug */
411 if (*(args[3])) {
412 level = get_log_level(args[3]);
413 if (level < 0) {
414 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
415 exit(1);
416 }
417 }
418
419 sa = str2sa(args[1]);
420 if (!sa->sin_port)
421 sa->sin_port = htons(SYSLOG_PORT);
422
423 if (global.logfac1 == -1) {
424 global.logsrv1 = *sa;
425 global.logfac1 = facility;
426 global.loglev1 = level;
427 }
428 else if (global.logfac2 == -1) {
429 global.logsrv2 = *sa;
430 global.logfac2 = facility;
431 global.loglev2 = level;
432 }
433 else {
434 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
435 return -1;
436 }
437
438 }
439 else {
440 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
441 return -1;
442 }
443 return 0;
444}
445
446
447static void init_default_instance()
448{
449 memset(&defproxy, 0, sizeof(defproxy));
450 defproxy.mode = PR_MODE_TCP;
451 defproxy.state = PR_STNEW;
452 defproxy.maxconn = cfg_maxpconn;
453 defproxy.conn_retries = CONN_RETRIES;
454 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
455}
456
457/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100458 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
459 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200461int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462{
463 static struct proxy *curproxy = NULL;
464 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200465 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 int rc;
467
Willy Tarreau977b8e42006-12-29 14:19:17 +0100468 if (!strcmp(args[0], "listen"))
469 rc = PR_CAP_LISTEN;
470 else if (!strcmp(args[0], "frontend"))
471 rc = PR_CAP_FE | PR_CAP_RS;
472 else if (!strcmp(args[0], "backend"))
473 rc = PR_CAP_BE | PR_CAP_RS;
474 else if (!strcmp(args[0], "ruleset"))
475 rc = PR_CAP_RS;
476 else
477 rc = PR_CAP_NONE;
478
479 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480 if (!*args[1]) {
481 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
482 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
483 file, linenum, args[0]);
484 return -1;
485 }
486
487 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
488 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
489 return -1;
490 }
491
492 curproxy->next = proxy;
493 proxy = curproxy;
494 LIST_INIT(&curproxy->pendconns);
495
496 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100497 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498
499 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100500 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501 curproxy->listen = str2listener(args[2], curproxy->listen);
502 if (!curproxy->listen)
503 return -1;
504 global.maxsock++;
505 }
506
507 /* set default values */
508 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509 curproxy->options = defproxy.options;
Willy Tarreau7ac51f62007-03-25 16:00:04 +0200510 curproxy->except_net = defproxy.except_net;
511 curproxy->except_mask = defproxy.except_mask;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512
Willy Tarreau977b8e42006-12-29 14:19:17 +0100513 if (curproxy->cap & PR_CAP_FE) {
514 curproxy->maxconn = defproxy.maxconn;
515
516 /* initialize error relocations */
517 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
518 if (defproxy.errmsg[rc].str)
519 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
520 }
521
522 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
523 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524
Willy Tarreau977b8e42006-12-29 14:19:17 +0100525 if (curproxy->cap & PR_CAP_BE) {
526 curproxy->fullconn = defproxy.fullconn;
527 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreau977b8e42006-12-29 14:19:17 +0100529 if (defproxy.check_req)
530 curproxy->check_req = strdup(defproxy.check_req);
531 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532
Willy Tarreau977b8e42006-12-29 14:19:17 +0100533 if (defproxy.cookie_name)
534 curproxy->cookie_name = strdup(defproxy.cookie_name);
535 curproxy->cookie_len = defproxy.cookie_len;
536 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau977b8e42006-12-29 14:19:17 +0100538 if (curproxy->cap & PR_CAP_RS) {
539 if (defproxy.capture_name)
540 curproxy->capture_name = strdup(defproxy.capture_name);
541 curproxy->capture_namelen = defproxy.capture_namelen;
542 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544
Willy Tarreau977b8e42006-12-29 14:19:17 +0100545 if (curproxy->cap & PR_CAP_FE) {
546 curproxy->clitimeout = defproxy.clitimeout;
547 curproxy->uri_auth = defproxy.uri_auth;
548 curproxy->mon_net = defproxy.mon_net;
549 curproxy->mon_mask = defproxy.mon_mask;
550 if (defproxy.monitor_uri)
551 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
552 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100553 if (defproxy.defbe.name)
554 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100555 }
556
557 if (curproxy->cap & PR_CAP_BE) {
558 curproxy->contimeout = defproxy.contimeout;
559 curproxy->srvtimeout = defproxy.srvtimeout;
560 curproxy->source_addr = defproxy.source_addr;
561 }
562
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563 curproxy->mode = defproxy.mode;
564 curproxy->logfac1 = defproxy.logfac1;
565 curproxy->logsrv1 = defproxy.logsrv1;
566 curproxy->loglev1 = defproxy.loglev1;
567 curproxy->logfac2 = defproxy.logfac2;
568 curproxy->logsrv2 = defproxy.logsrv2;
569 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200571
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 return 0;
573 }
574 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
575 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100576 /* FIXME-20070101: we should do this too at the end of the
577 * config parsing to free all default values.
578 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 if (defproxy.check_req) free(defproxy.check_req);
580 if (defproxy.cookie_name) free(defproxy.cookie_name);
581 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200582 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100583 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100584
585 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
586 if (defproxy.errmsg[rc].len)
587 free(defproxy.errmsg[rc].str);
588 }
589
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590 /* we cannot free uri_auth because it might already be used */
591 init_default_instance();
592 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100593 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594 return 0;
595 }
596 else if (curproxy == NULL) {
597 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
598 return -1;
599 }
600
Willy Tarreau977b8e42006-12-29 14:19:17 +0100601
602 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603 if (!strcmp(args[0], "bind")) { /* new listen addresses */
604 if (curproxy == &defproxy) {
605 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
606 return -1;
607 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100608 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
609 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610
611 if (strchr(args[1], ':') == NULL) {
612 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
613 file, linenum, args[0]);
614 return -1;
615 }
616 curproxy->listen = str2listener(args[1], curproxy->listen);
617 if (!curproxy->listen)
618 return -1;
619 global.maxsock++;
620 return 0;
621 }
622 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
623 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
624 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
625 file, linenum, args[0]);
626 return -1;
627 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100628 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
629 return 0;
630
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631 /* flush useless bits */
632 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
633 return 0;
634 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200635 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100636 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
637 return 0;
638
Willy Tarreau1c47f852006-07-09 08:22:27 +0200639 if (!*args[1]) {
640 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
641 file, linenum, args[0]);
642 return -1;
643 }
644
645 if (curproxy->monitor_uri != NULL)
646 free(curproxy->monitor_uri);
647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100648 curproxy->monitor_uri_len = strlen(args[1]);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200649 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100650 memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200651 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
652
653 return 0;
654 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
656 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
657 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
658 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
659 else {
660 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
661 return -1;
662 }
663 }
664 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
665 curproxy->state = PR_STSTOPPED;
666 }
667 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
668 curproxy->state = PR_STNEW;
669 }
670 else if (!strcmp(args[0], "cookie")) { /* cookie name */
671 int cur_arg;
672 // if (curproxy == &defproxy) {
673 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
674 // return -1;
675 // }
676
Willy Tarreau977b8e42006-12-29 14:19:17 +0100677 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
678 return 0;
679
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 if (curproxy->cookie_name != NULL) {
681 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
682 // file, linenum);
683 // return 0;
684 free(curproxy->cookie_name);
685 }
686
687 if (*(args[1]) == 0) {
688 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
689 file, linenum, args[0]);
690 return -1;
691 }
692 curproxy->cookie_name = strdup(args[1]);
693 curproxy->cookie_len = strlen(curproxy->cookie_name);
694
695 cur_arg = 2;
696 while (*(args[cur_arg])) {
697 if (!strcmp(args[cur_arg], "rewrite")) {
698 curproxy->options |= PR_O_COOK_RW;
699 }
700 else if (!strcmp(args[cur_arg], "indirect")) {
701 curproxy->options |= PR_O_COOK_IND;
702 }
703 else if (!strcmp(args[cur_arg], "insert")) {
704 curproxy->options |= PR_O_COOK_INS;
705 }
706 else if (!strcmp(args[cur_arg], "nocache")) {
707 curproxy->options |= PR_O_COOK_NOC;
708 }
709 else if (!strcmp(args[cur_arg], "postonly")) {
710 curproxy->options |= PR_O_COOK_POST;
711 }
712 else if (!strcmp(args[cur_arg], "prefix")) {
713 curproxy->options |= PR_O_COOK_PFX;
714 }
715 else {
716 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
717 file, linenum, args[0]);
718 return -1;
719 }
720 cur_arg++;
721 }
722 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
723 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
724 file, linenum);
725 return -1;
726 }
727
728 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
729 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
730 file, linenum);
731 return -1;
732 }
733 }/* end else if (!strcmp(args[0], "cookie")) */
734 else if (!strcmp(args[0], "appsession")) { /* cookie name */
735 // if (curproxy == &defproxy) {
736 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
737 // return -1;
738 // }
739
Willy Tarreau977b8e42006-12-29 14:19:17 +0100740 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
741 return 0;
742
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 if (curproxy->appsession_name != NULL) {
744 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
745 // file, linenum);
746 // return 0;
747 free(curproxy->appsession_name);
748 }
749
750 if (*(args[5]) == 0) {
751 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
752 file, linenum, args[0]);
753 return -1;
754 }
755 have_appsession = 1;
756 curproxy->appsession_name = strdup(args[1]);
757 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
758 curproxy->appsession_len = atoi(args[3]);
759 curproxy->appsession_timeout = atoi(args[5]);
760 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
761 if (rc) {
762 Alert("Error Init Appsession Hashtable.\n");
763 return -1;
764 }
765 } /* Url App Session */
766 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100767 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
768 return 0;
769
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
771 // if (curproxy == &defproxy) {
772 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
773 // return -1;
774 // }
775
776 if (curproxy->capture_name != NULL) {
777 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
778 // file, linenum, args[0]);
779 // return 0;
780 free(curproxy->capture_name);
781 }
782
783 if (*(args[4]) == 0) {
784 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
785 file, linenum, args[0]);
786 return -1;
787 }
788 curproxy->capture_name = strdup(args[2]);
789 curproxy->capture_namelen = strlen(curproxy->capture_name);
790 curproxy->capture_len = atol(args[4]);
791 if (curproxy->capture_len >= CAPTURE_LEN) {
792 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
793 file, linenum, CAPTURE_LEN - 1);
794 curproxy->capture_len = CAPTURE_LEN - 1;
795 }
796 curproxy->to_log |= LW_COOKIE;
797 }
798 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
799 struct cap_hdr *hdr;
800
801 if (curproxy == &defproxy) {
802 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
803 return -1;
804 }
805
806 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
807 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
808 file, linenum, args[0], args[1]);
809 return -1;
810 }
811
812 hdr = calloc(sizeof(struct cap_hdr), 1);
813 hdr->next = curproxy->req_cap;
814 hdr->name = strdup(args[3]);
815 hdr->namelen = strlen(args[3]);
816 hdr->len = atol(args[5]);
817 hdr->index = curproxy->nb_req_cap++;
818 curproxy->req_cap = hdr;
819 curproxy->to_log |= LW_REQHDR;
820 }
821 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
822 struct cap_hdr *hdr;
823
824 if (curproxy == &defproxy) {
825 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
826 return -1;
827 }
828
829 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
830 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
831 file, linenum, args[0], args[1]);
832 return -1;
833 }
834 hdr = calloc(sizeof(struct cap_hdr), 1);
835 hdr->next = curproxy->rsp_cap;
836 hdr->name = strdup(args[3]);
837 hdr->namelen = strlen(args[3]);
838 hdr->len = atol(args[5]);
839 hdr->index = curproxy->nb_rsp_cap++;
840 curproxy->rsp_cap = hdr;
841 curproxy->to_log |= LW_RSPHDR;
842 }
843 else {
844 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
845 file, linenum, args[0]);
846 return -1;
847 }
848 }
849 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
850 if (curproxy->contimeout != defproxy.contimeout) {
851 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
852 return 0;
853 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100854 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
855 return 0;
856
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 if (*(args[1]) == 0) {
858 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
859 file, linenum, args[0]);
860 return -1;
861 }
862 curproxy->contimeout = atol(args[1]);
863 }
864 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
865 if (curproxy->clitimeout != defproxy.clitimeout) {
866 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
867 file, linenum, args[0]);
868 return 0;
869 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100870 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
871 return 0;
872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 if (*(args[1]) == 0) {
874 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
875 file, linenum, args[0]);
876 return -1;
877 }
878 curproxy->clitimeout = atol(args[1]);
879 }
880 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
881 if (curproxy->srvtimeout != defproxy.srvtimeout) {
882 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
883 return 0;
884 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100885 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
886 return 0;
887
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 if (*(args[1]) == 0) {
889 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
890 file, linenum, args[0]);
891 return -1;
892 }
893 curproxy->srvtimeout = atol(args[1]);
894 }
895 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100896 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
897 return 0;
898
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899 if (*(args[1]) == 0) {
900 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
901 file, linenum, args[0]);
902 return -1;
903 }
904 curproxy->conn_retries = atol(args[1]);
905 }
906 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100907 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
908 return 0;
909
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
911 curproxy->uri_auth = NULL; /* we must detach from the default config */
912
913 if (*(args[1]) == 0) {
914 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
915 return -1;
916 } else if (!strcmp(args[1], "uri")) {
917 if (*(args[2]) == 0) {
918 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
919 return -1;
920 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
921 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
922 return -1;
923 }
924 } else if (!strcmp(args[1], "realm")) {
925 if (*(args[2]) == 0) {
926 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
927 return -1;
928 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
929 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
930 return -1;
931 }
932 } else if (!strcmp(args[1], "auth")) {
933 if (*(args[2]) == 0) {
934 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
935 return -1;
936 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
937 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
938 return -1;
939 }
940 } else if (!strcmp(args[1], "scope")) {
941 if (*(args[2]) == 0) {
942 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
943 return -1;
944 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
945 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
946 return -1;
947 }
948 } else if (!strcmp(args[1], "enable")) {
949 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
950 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
951 return -1;
952 }
953 } else {
954 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
955 file, linenum, args[0]);
956 return -1;
957 }
958 }
959 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100960 int optnum;
961
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 if (*(args[1]) == 0) {
963 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
964 return -1;
965 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100966
967 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
968 if (!strcmp(args[1], cfg_opts[optnum].name)) {
969 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
970 return 0;
971 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +0100972 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +0100973 return 0;
974 }
975 }
976
977 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200978 /* generate a complete HTTP log */
979 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
980 else if (!strcmp(args[1], "tcplog"))
981 /* generate a detailed TCP log */
982 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 else if (!strcmp(args[1], "tcpka")) {
984 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100985 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
986 return 0;
987
988 if (curproxy->cap & PR_CAP_FE)
989 curproxy->options |= PR_O_TCP_CLI_KA;
990 if (curproxy->cap & PR_CAP_BE)
991 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 }
993 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100994 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
995 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 /* use HTTP request to check servers' health */
997 if (curproxy->check_req != NULL) {
998 free(curproxy->check_req);
999 }
1000 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +02001001 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 if (!*args[2]) { /* no argument */
1003 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
1004 curproxy->check_len = strlen(DEF_CHECK_REQ);
1005 } else if (!*args[3]) { /* one argument : URI */
1006 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
1007 curproxy->check_req = (char *)malloc(reqlen);
1008 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1009 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
1010 } else { /* more arguments : METHOD URI [HTTP_VER] */
1011 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
1012 if (*args[4])
1013 reqlen += strlen(args[4]);
1014 else
1015 reqlen += strlen("HTTP/1.0");
1016
1017 curproxy->check_req = (char *)malloc(reqlen);
1018 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1019 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
1020 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001021 }
1022 else if (!strcmp(args[1], "ssl-hello-chk")) {
1023 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001024 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1025 return 0;
1026
Willy Tarreauf3c69202006-07-09 16:42:34 +02001027 if (curproxy->check_req != NULL) {
1028 free(curproxy->check_req);
1029 }
1030 curproxy->options &= ~PR_O_HTTP_CHK;
1031 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 }
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001033 else if (!strcmp(args[1], "forwardfor")) {
1034 /* insert x-forwarded-for field, but not for the
1035 * IP address listed as an except.
1036 */
1037 if (*(args[2])) {
1038 if (!strcmp(args[2], "except")) {
1039 if (!*args[3] || !str2net(args[3], &curproxy->except_net, &curproxy->except_mask)) {
1040 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1041 file, linenum, args[0]);
1042 return -1;
1043 }
1044 /* flush useless bits */
1045 curproxy->except_net.s_addr &= curproxy->except_mask.s_addr;
1046 } else {
1047 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1048 file, linenum, args[0]);
1049 return -1;
1050 }
1051 }
1052 curproxy->options |= PR_O_FWDFOR;
1053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 else {
1055 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1056 return -1;
1057 }
1058 return 0;
1059 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001060 else if (!strcmp(args[0], "default_backend")) {
1061 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
1062 return 0;
1063
1064 if (*(args[1]) == 0) {
1065 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
1066 return -1;
1067 }
1068 if (curproxy->defbe.name)
1069 free(curproxy->defbe.name);
1070 curproxy->defbe.name = strdup(args[1]);
1071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001073 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1074 return 0;
1075
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076 /* enable reconnections to dispatch */
1077 curproxy->options |= PR_O_REDISP;
1078 }
1079#ifdef TPROXY
1080 else if (!strcmp(args[0], "transparent")) {
1081 /* enable transparent proxy connections */
1082 curproxy->options |= PR_O_TRANSP;
1083 }
1084#endif
1085 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001086 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1087 return 0;
1088
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 if (*(args[1]) == 0) {
1090 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1091 return -1;
1092 }
1093 curproxy->maxconn = atol(args[1]);
1094 }
Willy Tarreau86034312006-12-29 00:10:33 +01001095 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001096 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1097 return 0;
1098
Willy Tarreau86034312006-12-29 00:10:33 +01001099 if (*(args[1]) == 0) {
1100 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1101 return -1;
1102 }
1103 curproxy->fullconn = atol(args[1]);
1104 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1106 if (*(args[1]) == 0) {
1107 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1108 return -1;
1109 }
1110 curproxy->grace = atol(args[1]);
1111 }
1112 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1113 if (curproxy == &defproxy) {
1114 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1115 return -1;
1116 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001117 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1118 return 0;
1119
Willy Tarreaubaaee002006-06-26 02:48:02 +02001120 if (strchr(args[1], ':') == NULL) {
1121 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1122 return -1;
1123 }
1124 curproxy->dispatch_addr = *str2sa(args[1]);
1125 }
1126 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001127 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1128 return 0;
1129
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130 if (*(args[1])) {
1131 if (!strcmp(args[1], "roundrobin")) {
1132 curproxy->options |= PR_O_BALANCE_RR;
1133 }
1134 else if (!strcmp(args[1], "source")) {
1135 curproxy->options |= PR_O_BALANCE_SH;
1136 }
1137 else {
1138 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1139 return -1;
1140 }
1141 }
1142 else /* if no option is set, use round-robin by default */
1143 curproxy->options |= PR_O_BALANCE_RR;
1144 }
1145 else if (!strcmp(args[0], "server")) { /* server address */
1146 int cur_arg;
1147 char *rport;
1148 char *raddr;
1149 short realport;
1150 int do_check;
1151
1152 if (curproxy == &defproxy) {
1153 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1154 return -1;
1155 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001156 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1157 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158
1159 if (!*args[2]) {
1160 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1161 file, linenum, args[0]);
1162 return -1;
1163 }
1164 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1165 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1166 return -1;
1167 }
1168
1169 /* the servers are linked backwards first */
1170 newsrv->next = curproxy->srv;
1171 curproxy->srv = newsrv;
1172 newsrv->proxy = curproxy;
1173
1174 LIST_INIT(&newsrv->pendconns);
1175 do_check = 0;
1176 newsrv->state = SRV_RUNNING; /* early server setup */
1177 newsrv->id = strdup(args[1]);
1178
1179 /* several ways to check the port component :
1180 * - IP => port=+0, relative
1181 * - IP: => port=+0, relative
1182 * - IP:N => port=N, absolute
1183 * - IP:+N => port=+N, relative
1184 * - IP:-N => port=-N, relative
1185 */
1186 raddr = strdup(args[2]);
1187 rport = strchr(raddr, ':');
1188 if (rport) {
1189 *rport++ = 0;
1190 realport = atol(rport);
1191 if (!isdigit((int)*rport))
1192 newsrv->state |= SRV_MAPPORTS;
1193 } else {
1194 realport = 0;
1195 newsrv->state |= SRV_MAPPORTS;
1196 }
1197
1198 newsrv->addr = *str2sa(raddr);
1199 newsrv->addr.sin_port = htons(realport);
1200 free(raddr);
1201
1202 newsrv->curfd = -1; /* no health-check in progress */
1203 newsrv->inter = DEF_CHKINTR;
1204 newsrv->rise = DEF_RISETIME;
1205 newsrv->fall = DEF_FALLTIME;
1206 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
Willy Tarreau417fae02007-03-25 21:16:40 +02001207 newsrv->uweight = 1;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001208
Willy Tarreaubaaee002006-06-26 02:48:02 +02001209 cur_arg = 3;
1210 while (*args[cur_arg]) {
1211 if (!strcmp(args[cur_arg], "cookie")) {
1212 newsrv->cookie = strdup(args[cur_arg + 1]);
1213 newsrv->cklen = strlen(args[cur_arg + 1]);
1214 cur_arg += 2;
1215 }
1216 else if (!strcmp(args[cur_arg], "rise")) {
1217 newsrv->rise = atol(args[cur_arg + 1]);
1218 newsrv->health = newsrv->rise;
1219 cur_arg += 2;
1220 }
1221 else if (!strcmp(args[cur_arg], "fall")) {
1222 newsrv->fall = atol(args[cur_arg + 1]);
1223 cur_arg += 2;
1224 }
1225 else if (!strcmp(args[cur_arg], "inter")) {
1226 newsrv->inter = atol(args[cur_arg + 1]);
1227 cur_arg += 2;
1228 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001229 else if (!strcmp(args[cur_arg], "addr")) {
1230 newsrv->check_addr = *str2sa(args[cur_arg + 1]);
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001231 cur_arg += 2;
1232 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 else if (!strcmp(args[cur_arg], "port")) {
1234 newsrv->check_port = atol(args[cur_arg + 1]);
1235 cur_arg += 2;
1236 }
1237 else if (!strcmp(args[cur_arg], "backup")) {
1238 newsrv->state |= SRV_BACKUP;
1239 cur_arg ++;
1240 }
1241 else if (!strcmp(args[cur_arg], "weight")) {
1242 int w;
1243 w = atol(args[cur_arg + 1]);
1244 if (w < 1 || w > 256) {
1245 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1246 file, linenum, newsrv->id, w);
1247 return -1;
1248 }
Willy Tarreau417fae02007-03-25 21:16:40 +02001249 newsrv->uweight = w;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001250 cur_arg += 2;
1251 }
1252 else if (!strcmp(args[cur_arg], "minconn")) {
1253 newsrv->minconn = atol(args[cur_arg + 1]);
1254 cur_arg += 2;
1255 }
1256 else if (!strcmp(args[cur_arg], "maxconn")) {
1257 newsrv->maxconn = atol(args[cur_arg + 1]);
1258 cur_arg += 2;
1259 }
1260 else if (!strcmp(args[cur_arg], "check")) {
1261 global.maxsock++;
1262 do_check = 1;
1263 cur_arg += 1;
1264 }
1265 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1266 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001267#ifdef CONFIG_HAP_CTTPROXY
1268 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1269 file, linenum, "source", "usesrc");
1270#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1272 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001273#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001274 return -1;
1275 }
1276 newsrv->state |= SRV_BIND_SRC;
1277 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1278 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001279 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001280#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001281 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001282 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1283 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001284 return -1;
1285 }
1286 if (!*args[cur_arg + 1]) {
1287 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1288 file, linenum, "usesrc");
1289 return -1;
1290 }
1291 if (!strcmp(args[cur_arg + 1], "client")) {
1292 newsrv->state |= SRV_TPROXY_CLI;
1293 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1294 newsrv->state |= SRV_TPROXY_CIP;
1295 } else {
1296 newsrv->state |= SRV_TPROXY_ADDR;
1297 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1298 }
1299 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1300 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001301#else /* no CTTPROXY support */
1302 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1303 file, linenum, "usesrc");
1304 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001305#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001306 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001308#ifdef CONFIG_HAP_CTTPROXY
1309 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1310 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1311 file, linenum, "usesrc", "source");
1312 return -1;
1313 }
1314#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 else {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001316 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 +02001317 file, linenum, newsrv->id);
1318 return -1;
1319 }
1320 }
1321
1322 if (do_check) {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001323 if (!newsrv->check_port && newsrv->check_addr.sin_port)
1324 newsrv->check_port = newsrv->check_addr.sin_port;
1325
Willy Tarreaubaaee002006-06-26 02:48:02 +02001326 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1327 newsrv->check_port = realport; /* by default */
1328 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001329 /* not yet valid, because no port was set on
1330 * the server either. We'll check if we have
1331 * a known port on the first listener.
1332 */
1333 struct listener *l;
1334 l = curproxy->listen;
1335 if (l) {
1336 int port;
1337 port = (l->addr.ss_family == AF_INET6)
1338 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1339 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1340 newsrv->check_port = port;
1341 }
1342 }
1343 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001344 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1345 file, linenum, newsrv->id);
1346 return -1;
1347 }
1348 newsrv->state |= SRV_CHECKED;
1349 }
1350
1351 if (newsrv->state & SRV_BACKUP)
1352 curproxy->srv_bck++;
1353 else
1354 curproxy->srv_act++;
1355 }
1356 else if (!strcmp(args[0], "log")) { /* syslog server address */
1357 struct sockaddr_in *sa;
1358 int facility;
1359
1360 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1361 curproxy->logfac1 = global.logfac1;
1362 curproxy->logsrv1 = global.logsrv1;
1363 curproxy->loglev1 = global.loglev1;
1364 curproxy->logfac2 = global.logfac2;
1365 curproxy->logsrv2 = global.logsrv2;
1366 curproxy->loglev2 = global.loglev2;
1367 }
1368 else if (*(args[1]) && *(args[2])) {
1369 int level;
1370
1371 facility = get_log_facility(args[2]);
1372 if (facility < 0) {
1373 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1374 exit(1);
1375 }
1376
1377 level = 7; /* max syslog level = debug */
1378 if (*(args[3])) {
1379 level = get_log_level(args[3]);
1380 if (level < 0) {
1381 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1382 exit(1);
1383 }
1384 }
1385
1386 sa = str2sa(args[1]);
1387 if (!sa->sin_port)
1388 sa->sin_port = htons(SYSLOG_PORT);
1389
1390 if (curproxy->logfac1 == -1) {
1391 curproxy->logsrv1 = *sa;
1392 curproxy->logfac1 = facility;
1393 curproxy->loglev1 = level;
1394 }
1395 else if (curproxy->logfac2 == -1) {
1396 curproxy->logsrv2 = *sa;
1397 curproxy->logfac2 = facility;
1398 curproxy->loglev2 = level;
1399 }
1400 else {
1401 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1402 return -1;
1403 }
1404 }
1405 else {
1406 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1407 file, linenum);
1408 return -1;
1409 }
1410 }
1411 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001412 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1413 return 0;
1414
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001416#ifdef CONFIG_HAP_CTTPROXY
1417 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1418 file, linenum, "source", "usesrc");
1419#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1421 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001422#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423 return -1;
1424 }
1425
1426 curproxy->source_addr = *str2sa(args[1]);
1427 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001428 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001429#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001430 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1431 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1432 file, linenum, "usesrc");
1433 return -1;
1434 }
1435 if (!*args[3]) {
1436 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1437 file, linenum, "usesrc");
1438 return -1;
1439 }
1440
1441 if (!strcmp(args[3], "client")) {
1442 curproxy->options |= PR_O_TPXY_CLI;
1443 } else if (!strcmp(args[3], "clientip")) {
1444 curproxy->options |= PR_O_TPXY_CIP;
1445 } else {
1446 curproxy->options |= PR_O_TPXY_ADDR;
1447 curproxy->tproxy_addr = *str2sa(args[3]);
1448 }
1449 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001450#else /* no CTTPROXY support */
1451 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1452 file, linenum, "usesrc");
1453 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001454#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001455 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001456 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001457#ifdef CONFIG_HAP_CTTPROXY
1458 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1459 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1460 file, linenum, "usesrc", "source");
1461 return -1;
1462 }
1463#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1465 regex_t *preg;
1466 if (curproxy == &defproxy) {
1467 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1468 return -1;
1469 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001470 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1471 return 0;
1472
Willy Tarreaubaaee002006-06-26 02:48:02 +02001473 if (*(args[1]) == 0 || *(args[2]) == 0) {
1474 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1475 file, linenum, args[0]);
1476 return -1;
1477 }
1478
1479 preg = calloc(1, sizeof(regex_t));
1480 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1481 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1482 return -1;
1483 }
1484
1485 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1486 if (err) {
1487 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1488 file, linenum, *err);
1489 return -1;
1490 }
1491 }
1492 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1493 regex_t *preg;
1494 if (curproxy == &defproxy) {
1495 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1496 return -1;
1497 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001498 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1499 return 0;
1500
Willy Tarreaubaaee002006-06-26 02:48:02 +02001501 if (*(args[1]) == 0) {
1502 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1503 return -1;
1504 }
1505
1506 preg = calloc(1, sizeof(regex_t));
1507 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1508 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1509 return -1;
1510 }
1511
1512 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1513 }
1514 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1515 regex_t *preg;
1516 if (curproxy == &defproxy) {
1517 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1518 return -1;
1519 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001520 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1521 return 0;
1522
Willy Tarreaubaaee002006-06-26 02:48:02 +02001523 if (*(args[1]) == 0) {
1524 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1525 return -1;
1526 }
1527
1528 preg = calloc(1, sizeof(regex_t));
1529 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1530 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1531 return -1;
1532 }
1533
1534 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1535 }
1536 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1537 regex_t *preg;
1538 if (curproxy == &defproxy) {
1539 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1540 return -1;
1541 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001542 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1543 return 0;
1544
Willy Tarreaubaaee002006-06-26 02:48:02 +02001545 if (*(args[1]) == 0) {
1546 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1547 return -1;
1548 }
1549
1550 preg = calloc(1, sizeof(regex_t));
1551 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1552 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1553 return -1;
1554 }
1555
1556 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1557 }
1558 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1559 regex_t *preg;
1560 if (curproxy == &defproxy) {
1561 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1562 return -1;
1563 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001564 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1565 return 0;
1566
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567 if (*(args[1]) == 0) {
1568 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1569 return -1;
1570 }
1571
1572 preg = calloc(1, sizeof(regex_t));
1573 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1574 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1575 return -1;
1576 }
1577
1578 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1579 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001580 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1581 regex_t *preg;
1582 if (curproxy == &defproxy) {
1583 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1584 return -1;
1585 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001586 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1587 return 0;
1588
Willy Tarreaub8750a82006-09-03 09:56:00 +02001589 if (*(args[1]) == 0) {
1590 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1591 return -1;
1592 }
1593
1594 preg = calloc(1, sizeof(regex_t));
1595 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1596 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1597 return -1;
1598 }
1599
1600 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1601 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001602 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1603 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001604 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001605 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1606 return -1;
1607 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001608 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1609 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001610
Willy Tarreau977b8e42006-12-29 14:19:17 +01001611 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001612 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1613 file, linenum, args[0]);
1614 return -1;
1615 }
1616
1617 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001618 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001619 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1620 }
1621
1622 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1623 }
1624 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1625 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001626 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001627 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1628 return -1;
1629 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001630 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1631 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001632
Willy Tarreau977b8e42006-12-29 14:19:17 +01001633 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001634 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1635 file, linenum, args[0]);
1636 return -1;
1637 }
1638
1639 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001640 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001641 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1642 }
1643
1644 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001646 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1647 regex_t *preg;
1648 if (curproxy == &defproxy) {
1649 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1650 return -1;
1651 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001652 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1653 return 0;
1654
Willy Tarreaubaaee002006-06-26 02:48:02 +02001655 if (*(args[1]) == 0 || *(args[2]) == 0) {
1656 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1657 file, linenum, args[0]);
1658 return -1;
1659 }
1660
1661 preg = calloc(1, sizeof(regex_t));
1662 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1663 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1664 return -1;
1665 }
1666
1667 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1668 if (err) {
1669 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1670 file, linenum, *err);
1671 return -1;
1672 }
1673 }
1674 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1675 regex_t *preg;
1676 if (curproxy == &defproxy) {
1677 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1678 return -1;
1679 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001680 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1681 return 0;
1682
Willy Tarreaubaaee002006-06-26 02:48:02 +02001683 if (*(args[1]) == 0) {
1684 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1685 return -1;
1686 }
1687
1688 preg = calloc(1, sizeof(regex_t));
1689 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1690 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1691 return -1;
1692 }
1693
1694 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1695 }
1696 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1697 regex_t *preg;
1698 if (curproxy == &defproxy) {
1699 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1700 return -1;
1701 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001702 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1703 return 0;
1704
Willy Tarreaubaaee002006-06-26 02:48:02 +02001705 if (*(args[1]) == 0) {
1706 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1707 return -1;
1708 }
1709
1710 preg = calloc(1, sizeof(regex_t));
1711 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1712 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1713 return -1;
1714 }
1715
1716 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1717 }
1718 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1719 regex_t *preg;
1720 if (curproxy == &defproxy) {
1721 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1722 return -1;
1723 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001724 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1725 return 0;
1726
Willy Tarreaubaaee002006-06-26 02:48:02 +02001727 if (*(args[1]) == 0) {
1728 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1729 return -1;
1730 }
1731
1732 preg = calloc(1, sizeof(regex_t));
1733 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1734 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1735 return -1;
1736 }
1737
1738 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1739 }
1740 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1741 regex_t *preg;
1742 if (curproxy == &defproxy) {
1743 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1744 return -1;
1745 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001746 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1747 return 0;
1748
Willy Tarreaubaaee002006-06-26 02:48:02 +02001749 if (*(args[1]) == 0) {
1750 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1751 return -1;
1752 }
1753
1754 preg = calloc(1, sizeof(regex_t));
1755 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1756 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1757 return -1;
1758 }
1759
1760 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1761 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001762 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1763 regex_t *preg;
1764 if (curproxy == &defproxy) {
1765 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1766 return -1;
1767 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001768 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1769 return 0;
1770
Willy Tarreaub8750a82006-09-03 09:56:00 +02001771 if (*(args[1]) == 0) {
1772 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1773 return -1;
1774 }
1775
1776 preg = calloc(1, sizeof(regex_t));
1777 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1778 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1779 return -1;
1780 }
1781
1782 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1783 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1785 if (curproxy == &defproxy) {
1786 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1787 return -1;
1788 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001789 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1790 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001791
1792 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1793 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1794 return 0;
1795 }
1796
1797 if (*(args[1]) == 0) {
1798 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1799 return -1;
1800 }
1801
1802 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1803 }
1804 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1805 regex_t *preg;
1806
1807 if (*(args[1]) == 0 || *(args[2]) == 0) {
1808 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1809 file, linenum, args[0]);
1810 return -1;
1811 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001812 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1813 return 0;
1814
Willy Tarreaubaaee002006-06-26 02:48:02 +02001815 preg = calloc(1, sizeof(regex_t));
1816 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1817 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1818 return -1;
1819 }
1820
1821 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1822 if (err) {
1823 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1824 file, linenum, *err);
1825 return -1;
1826 }
1827 }
1828 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1829 regex_t *preg;
1830 if (curproxy == &defproxy) {
1831 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1832 return -1;
1833 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001834 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1835 return 0;
1836
Willy Tarreaubaaee002006-06-26 02:48:02 +02001837 if (*(args[1]) == 0) {
1838 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1839 return -1;
1840 }
1841
1842 preg = calloc(1, sizeof(regex_t));
1843 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1844 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1845 return -1;
1846 }
1847
1848 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1849 if (err) {
1850 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1851 file, linenum, *err);
1852 return -1;
1853 }
1854 }
1855 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1856 regex_t *preg;
1857 if (curproxy == &defproxy) {
1858 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1859 return -1;
1860 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001861 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1862 return 0;
1863
Willy Tarreaubaaee002006-06-26 02:48:02 +02001864 if (*(args[1]) == 0) {
1865 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1866 return -1;
1867 }
1868
1869 preg = calloc(1, sizeof(regex_t));
1870 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1871 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1872 return -1;
1873 }
1874
1875 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1876 if (err) {
1877 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1878 file, linenum, *err);
1879 return -1;
1880 }
1881 }
1882 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1883 regex_t *preg;
1884 if (curproxy == &defproxy) {
1885 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1886 return -1;
1887 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001888 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1889 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001890
1891 if (*(args[1]) == 0 || *(args[2]) == 0) {
1892 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1893 file, linenum, args[0]);
1894 return -1;
1895 }
1896
1897 preg = calloc(1, sizeof(regex_t));
1898 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1899 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1900 return -1;
1901 }
1902
1903 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1904 if (err) {
1905 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1906 file, linenum, *err);
1907 return -1;
1908 }
1909 }
1910 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1911 regex_t *preg;
1912 if (curproxy == &defproxy) {
1913 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1914 return -1;
1915 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001916 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1917 return 0;
1918
Willy Tarreaubaaee002006-06-26 02:48:02 +02001919 if (*(args[1]) == 0) {
1920 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1921 return -1;
1922 }
1923
1924 preg = calloc(1, sizeof(regex_t));
1925 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1926 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1927 return -1;
1928 }
1929
1930 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1931 if (err) {
1932 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1933 file, linenum, *err);
1934 return -1;
1935 }
1936 }
1937 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1938 regex_t *preg;
1939 if (curproxy == &defproxy) {
1940 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1941 return -1;
1942 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001943 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1944 return 0;
1945
Willy Tarreaubaaee002006-06-26 02:48:02 +02001946 if (*(args[1]) == 0) {
1947 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1948 return -1;
1949 }
1950
1951 preg = calloc(1, sizeof(regex_t));
1952 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1953 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1954 return -1;
1955 }
1956
1957 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1958 if (err) {
1959 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1960 file, linenum, *err);
1961 return -1;
1962 }
1963 }
1964 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1965 if (curproxy == &defproxy) {
1966 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1967 return -1;
1968 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001969 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1970 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001971
1972 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1973 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1974 return 0;
1975 }
1976
1977 if (*(args[1]) == 0) {
1978 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1979 return -1;
1980 }
1981
1982 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1983 }
1984 else if (!strcmp(args[0], "errorloc") ||
1985 !strcmp(args[0], "errorloc302") ||
1986 !strcmp(args[0], "errorloc303")) { /* error location */
1987 int errnum, errlen;
1988 char *err;
1989
1990 // if (curproxy == &defproxy) {
1991 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1992 // return -1;
1993 // }
1994
Willy Tarreau977b8e42006-12-29 14:19:17 +01001995 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
1996 return 0;
1997
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01001999 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000 return -1;
2001 }
2002
2003 errnum = atol(args[1]);
2004 if (!strcmp(args[0], "errorloc303")) {
2005 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
2006 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
2007 } else {
2008 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
2009 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2010 }
2011
Willy Tarreau0f772532006-12-23 20:51:41 +01002012 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2013 if (http_err_codes[rc] == errnum) {
2014 if (curproxy->errmsg[rc].str)
2015 free(curproxy->errmsg[rc].str);
2016 curproxy->errmsg[rc].str = err;
2017 curproxy->errmsg[rc].len = errlen;
2018 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002019 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002021
2022 if (rc >= HTTP_ERR_SIZE) {
2023 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2024 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025 free(err);
2026 }
2027 }
2028 else {
2029 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2030 return -1;
2031 }
2032 return 0;
2033}
2034
2035
2036/*
2037 * This function reads and parses the configuration file given in the argument.
2038 * returns 0 if OK, -1 if error.
2039 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002040int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002041{
2042 char thisline[256];
2043 char *line;
2044 FILE *f;
2045 int linenum = 0;
2046 char *end;
2047 char *args[MAX_LINE_ARGS];
2048 int arg;
2049 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002050 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002051 int confsect = CFG_NONE;
2052
2053 struct proxy *curproxy = NULL;
2054 struct server *newsrv = NULL;
2055
2056 if ((f=fopen(file,"r")) == NULL)
2057 return -1;
2058
2059 init_default_instance();
2060
2061 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2062 linenum++;
2063
2064 end = line + strlen(line);
2065
2066 /* skip leading spaces */
2067 while (isspace((int)*line))
2068 line++;
2069
2070 arg = 0;
2071 args[arg] = line;
2072
2073 while (*line && arg < MAX_LINE_ARGS) {
2074 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2075 * C equivalent value. Other combinations left unchanged (eg: \1).
2076 */
2077 if (*line == '\\') {
2078 int skip = 0;
2079 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2080 *line = line[1];
2081 skip = 1;
2082 }
2083 else if (line[1] == 'r') {
2084 *line = '\r';
2085 skip = 1;
2086 }
2087 else if (line[1] == 'n') {
2088 *line = '\n';
2089 skip = 1;
2090 }
2091 else if (line[1] == 't') {
2092 *line = '\t';
2093 skip = 1;
2094 }
2095 else if (line[1] == 'x') {
2096 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2097 unsigned char hex1, hex2;
2098 hex1 = toupper(line[2]) - '0';
2099 hex2 = toupper(line[3]) - '0';
2100 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2101 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2102 *line = (hex1<<4) + hex2;
2103 skip = 3;
2104 }
2105 else {
2106 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2107 return -1;
2108 }
2109 }
2110 if (skip) {
2111 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2112 end -= skip;
2113 }
2114 line++;
2115 }
2116 else if (*line == '#' || *line == '\n' || *line == '\r') {
2117 /* end of string, end of loop */
2118 *line = 0;
2119 break;
2120 }
2121 else if (isspace((int)*line)) {
2122 /* a non-escaped space is an argument separator */
2123 *line++ = 0;
2124 while (isspace((int)*line))
2125 line++;
2126 args[++arg] = line;
2127 }
2128 else {
2129 line++;
2130 }
2131 }
2132
2133 /* empty line */
2134 if (!**args)
2135 continue;
2136
2137 /* zero out remaining args */
2138 while (++arg < MAX_LINE_ARGS) {
2139 args[arg] = line;
2140 }
2141
Willy Tarreau977b8e42006-12-29 14:19:17 +01002142 if (!strcmp(args[0], "listen") ||
2143 !strcmp(args[0], "frontend") ||
2144 !strcmp(args[0], "backend") ||
2145 !strcmp(args[0], "ruleset") ||
2146 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002147 confsect = CFG_LISTEN;
2148 else if (!strcmp(args[0], "global")) /* global config */
2149 confsect = CFG_GLOBAL;
2150 /* else it's a section keyword */
2151
2152 switch (confsect) {
2153 case CFG_LISTEN:
2154 if (cfg_parse_listen(file, linenum, args) < 0)
2155 return -1;
2156 break;
2157 case CFG_GLOBAL:
2158 if (cfg_parse_global(file, linenum, args) < 0)
2159 return -1;
2160 break;
2161 default:
2162 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2163 return -1;
2164 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002165 }
2166 fclose(f);
2167
2168 /*
2169 * Now, check for the integrity of all that we have collected.
2170 */
2171
2172 /* will be needed further to delay some tasks */
2173 tv_now(&now);
2174
2175 if ((curproxy = proxy) == NULL) {
2176 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2177 file);
2178 return -1;
2179 }
2180
2181 while (curproxy != NULL) {
2182 if (curproxy->state == PR_STSTOPPED) {
2183 curproxy = curproxy->next;
2184 continue;
2185 }
2186
Willy Tarreau977b8e42006-12-29 14:19:17 +01002187 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2188 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 +01002189 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002190 cfgerr++;
2191 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002192 else if (curproxy->cap & PR_CAP_BE &&
2193 ((curproxy->mode != PR_MODE_HEALTH) &&
2194 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2195 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2196 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 +01002197 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 cfgerr++;
2199 }
2200 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2201 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002202 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002203 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204 cfgerr++;
2205 }
2206#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2207 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002208 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002209 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 cfgerr++;
2211 }
2212#endif
2213 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002214 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002215 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002216 }
2217 }
2218 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2219 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002220 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002221 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002222 }
2223 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002224 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002225 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 }
2227 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002228 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002229 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230 }
2231 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002232 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002233 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002235 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002236 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002237 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002238 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002239 }
2240 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2241 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2242 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2243 file, curproxy->id);
2244 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002245 }
2246 }
2247
2248 /* if a default backend was specified, let's find it */
2249 if (curproxy->defbe.name) {
2250 struct proxy *target;
2251
2252 for (target = proxy; target != NULL; target = target->next) {
2253 if (strcmp(target->id, curproxy->defbe.name) == 0)
2254 break;
2255 }
2256 if (target == NULL) {
2257 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2258 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2259 cfgerr++;
2260 } else if (target == curproxy) {
2261 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2262 cfgerr++;
2263 } else if (!(target->cap & PR_CAP_BE)) {
2264 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2265 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2266 cfgerr++;
2267 } else if (target->mode != curproxy->mode) {
2268 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2269 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2270 cfgerr++;
2271 } else {
2272 free(curproxy->defbe.name);
2273 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 }
2275 }
2276
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002277 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002278 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2279 /* map jump target for ACT_SETBE in req_rep chain */
2280 struct hdr_exp *exp;
2281 struct proxy *target;
2282 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2283 if (exp->action != ACT_SETBE)
2284 continue;
2285 for (target = proxy; target != NULL; target = target->next) {
2286 if (strcmp(target->id, exp->replace) == 0)
2287 break;
2288 }
2289 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002290 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002291 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002292 cfgerr++;
2293 } else if (target == curproxy) {
2294 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2295 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002296 } else if (!(target->cap & PR_CAP_BE)) {
2297 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002298 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002299 cfgerr++;
2300 } else if (target->mode != PR_MODE_HTTP) {
2301 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002302 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002303 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002304 } else {
2305 free((void *)exp->replace);
2306 exp->replace = (const char *)target;
2307 }
2308 }
2309 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002310 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002311 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2312 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2313 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002314 " | While not properly invalid, you will certainly encounter various problems\n"
2315 " | with such a configuration. To fix this, please ensure that all following\n"
2316 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002317 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002318 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002319
2320 if (curproxy->options & PR_O_SSL3_CHK) {
2321 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2322 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2323 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2324 }
2325
Willy Tarreau86034312006-12-29 00:10:33 +01002326 /* for backwards compatibility with "listen" instances, if
2327 * fullconn is not set but maxconn is set, then maxconn
2328 * is used.
2329 */
2330 if (!curproxy->fullconn)
2331 curproxy->fullconn = curproxy->maxconn;
2332
Willy Tarreaubaaee002006-06-26 02:48:02 +02002333 /* first, we will invert the servers list order */
2334 newsrv = NULL;
2335 while (curproxy->srv) {
2336 struct server *next;
2337
2338 next = curproxy->srv->next;
2339 curproxy->srv->next = newsrv;
2340 newsrv = curproxy->srv;
2341 if (!next)
2342 break;
2343 curproxy->srv = next;
2344 }
2345
2346 /* now, newsrv == curproxy->srv */
2347 if (newsrv) {
2348 struct server *srv;
2349 int pgcd;
2350 int act, bck;
2351
2352 /* We will factor the weights to reduce the table,
2353 * using Euclide's largest common divisor algorithm
2354 */
Willy Tarreau417fae02007-03-25 21:16:40 +02002355 pgcd = newsrv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002356 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2357 int t, w;
2358
Willy Tarreau417fae02007-03-25 21:16:40 +02002359 w = srv->uweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002360 while (w) {
2361 t = pgcd % w;
2362 pgcd = w;
2363 w = t;
2364 }
2365 }
2366
2367 act = bck = 0;
2368 for (srv = newsrv; srv; srv = srv->next) {
Willy Tarreau417fae02007-03-25 21:16:40 +02002369 srv->eweight = srv->uweight / pgcd;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002370 if (srv->state & SRV_BACKUP)
Willy Tarreau417fae02007-03-25 21:16:40 +02002371 bck += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002372 else
Willy Tarreau417fae02007-03-25 21:16:40 +02002373 act += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002374 }
2375
2376 /* this is the largest map we will ever need for this servers list */
2377 if (act < bck)
2378 act = bck;
2379
2380 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2381 /* recounts servers and their weights */
2382 recount_servers(curproxy);
2383 recalc_server_map(curproxy);
2384 }
2385
2386 if (curproxy->options & PR_O_LOGASAP)
2387 curproxy->to_log &= ~LW_BYTES;
2388
Willy Tarreaubaaee002006-06-26 02:48:02 +02002389 /*
2390 * If this server supports a maxconn parameter, it needs a dedicated
2391 * tasks to fill the emptied slots when a connection leaves.
2392 */
2393 newsrv = curproxy->srv;
2394 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002395 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002396 /* Only 'minconn' was specified, or it was higher than or equal
2397 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2398 * this will avoid further useless expensive computations.
2399 */
2400 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002401 } else if (newsrv->maxconn && !newsrv->minconn) {
2402 /* minconn was not specified, so we set it to maxconn */
2403 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002404 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2405 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002406 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002407 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002408 }
2409
2410 if (newsrv->maxconn > 0) {
2411 struct task *t;
2412
2413 if ((t = pool_alloc(task)) == NULL) {
2414 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2415 return -1;
2416 }
2417
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002418 t->qlist.p = NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +01002419 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420 t->state = TASK_IDLE;
2421 t->process = process_srv_queue;
2422 t->context = newsrv;
2423 newsrv->queue_mgt = t;
2424
2425 /* never run it unless specifically woken up */
2426 tv_eternity(&t->expire);
2427 task_queue(t);
2428 }
2429 newsrv = newsrv->next;
2430 }
2431
2432 /* now we'll start this proxy's health checks if any */
2433 /* 1- count the checkers to run simultaneously */
2434 nbchk = 0;
2435 mininter = 0;
2436 newsrv = curproxy->srv;
2437 while (newsrv != NULL) {
2438 if (newsrv->state & SRV_CHECKED) {
2439 if (!mininter || mininter > newsrv->inter)
2440 mininter = newsrv->inter;
2441 nbchk++;
2442 }
2443 newsrv = newsrv->next;
2444 }
2445
2446 /* 2- start them as far as possible from each others while respecting
2447 * their own intervals. For this, we will start them after their own
2448 * interval added to the min interval divided by the number of servers,
2449 * weighted by the server's position in the list.
2450 */
2451 if (nbchk > 0) {
2452 struct task *t;
2453 int srvpos;
2454
2455 newsrv = curproxy->srv;
2456 srvpos = 0;
2457 while (newsrv != NULL) {
2458 /* should this server be checked ? */
2459 if (newsrv->state & SRV_CHECKED) {
2460 if ((t = pool_alloc(task)) == NULL) {
2461 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2462 return -1;
2463 }
2464
Willy Tarreau964c9362007-01-07 00:38:00 +01002465 t->wq = NULL;
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002466 t->qlist.p = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002467 t->state = TASK_IDLE;
2468 t->process = process_chk;
2469 t->context = newsrv;
2470
2471 /* check this every ms */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002472 tv_ms_add(&t->expire, &now,
2473 newsrv->inter + mininter * srvpos / nbchk);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002474 task_queue(t);
2475 //task_wakeup(&rq, t);
2476 srvpos++;
2477 }
2478 newsrv = newsrv->next;
2479 }
2480 }
2481
2482 curproxy = curproxy->next;
2483 }
2484 if (cfgerr > 0) {
2485 Alert("Errors found in configuration file, aborting.\n");
2486 return -1;
2487 }
2488 else
2489 return 0;
2490}
2491
2492
2493
2494/*
2495 * Local variables:
2496 * c-indent-level: 8
2497 * c-basic-offset: 8
2498 * End:
2499 */