blob: ff1c927cb45c3facfcba7d8241189524ae9a8084 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Configuration parser
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#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 Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <common/cfgparse.h>
22#include <common/config.h>
23#include <common/memory.h>
24#include <common/standard.h>
25#include <common/time.h>
26#include <common/uri_auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
28#include <types/capture.h>
29#include <types/global.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010030#include <types/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/polling.h>
32#include <types/proxy.h>
33#include <types/queue.h>
34
35#include <proto/backend.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010036#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/checks.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010038#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/log.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010040#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/server.h>
42#include <proto/task.h>
43
44
Willy Tarreauf3c69202006-07-09 16:42:34 +020045/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
46 * ssl-hello-chk option to ensure that the remote server speaks SSL.
47 *
48 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
49 */
50const char sslv3_client_hello_pkt[] = {
51 "\x16" /* ContentType : 0x16 = Hanshake */
52 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
53 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
54 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
55 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
56 "\x03\x00" /* Hello Version : 0x0300 = v3 */
57 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
58 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
59 "\x00" /* Session ID length : empty (no session ID) */
60 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
61 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
62 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
63 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
64 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
65 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
66 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
67 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
68 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
69 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
70 "\x00\x38" "\x00\x39" "\x00\x3A"
71 "\x01" /* Compression Length : 0x01 = 1 byte for types */
72 "\x00" /* Compression Type : 0x00 = NULL compression */
73};
74
Willy Tarreau13943ab2006-12-31 00:24:10 +010075/* some of the most common options which are also the easiest to handle */
76static const struct {
77 const char *name;
78 unsigned int val;
79 unsigned int cap;
Willy Tarreau4fee4e92007-01-06 21:09:17 +010080 unsigned int checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +010081} cfg_opts[] =
82{
83#ifdef TPROXY
84 { "transparent", PR_O_TRANSP, PR_CAP_FE },
85#endif
Willy Tarreau4fee4e92007-01-06 21:09:17 +010086 { "redispatch", PR_O_REDISP, PR_CAP_BE, 0 },
87 { "keepalive", PR_O_KEEPALIVE, PR_CAP_NONE, 0 },
Willy Tarreau4fee4e92007-01-06 21:09:17 +010088 { "httpclose", PR_O_HTTP_CLOSE, PR_CAP_FE | PR_CAP_BE, 0 },
89 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0 },
90 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0 },
91 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0 },
92 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0 },
93 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0 },
94 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0 },
95 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0 },
96 { "persist", PR_O_PERSIST, PR_CAP_BE, 0 },
97 { "forceclose", PR_O_FORCE_CLO | PR_O_HTTP_CLOSE, PR_CAP_BE, 0 },
Willy Tarreau8f922fc2007-01-06 21:11:49 +010098#ifdef CONFIG_HAP_TCPSPLICE
99 { "tcpsplice", PR_O_TCPSPLICE , PR_CAP_BE|PR_CAP_FE, LSTCHK_TCPSPLICE|LSTCHK_NETADM },
100#endif
101
Willy Tarreau13943ab2006-12-31 00:24:10 +0100102 { NULL, 0, 0 }
103};
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105
106static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
107int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
108int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
109
110/*
111 * converts <str> to a list of listeners which are dynamically allocated.
112 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
113 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
114 * - <port> is a numerical port from 1 to 65535 ;
115 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
116 * This can be repeated as many times as necessary, separated by a coma.
117 * The <tail> argument is a pointer to a current list which should be appended
118 * to the tail of the new list. The pointer to the new list is returned.
119 */
120static struct listener *str2listener(char *str, struct listener *tail)
121{
122 struct listener *l;
123 char *c, *next, *range, *dupstr;
124 int port, end;
125
126 next = dupstr = strdup(str);
127
128 while (next && *next) {
129 struct sockaddr_storage ss;
130
131 str = next;
132 /* 1) look for the end of the first address */
133 if ((next = strrchr(str, ',')) != NULL) {
134 *next++ = 0;
135 }
136
137 /* 2) look for the addr/port delimiter, it's the last colon. */
138 if ((range = strrchr(str, ':')) == NULL) {
139 Alert("Missing port number: '%s'\n", str);
140 goto fail;
141 }
142
143 *range++ = 0;
144
145 if (strrchr(str, ':') != NULL) {
146 /* IPv6 address contains ':' */
147 memset(&ss, 0, sizeof(ss));
148 ss.ss_family = AF_INET6;
149
150 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
151 Alert("Invalid server address: '%s'\n", str);
152 goto fail;
153 }
154 }
155 else {
156 memset(&ss, 0, sizeof(ss));
157 ss.ss_family = AF_INET;
158
159 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
160 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
161 }
162 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
163 struct hostent *he;
164
165 if ((he = gethostbyname(str)) == NULL) {
166 Alert("Invalid server name: '%s'\n", str);
167 goto fail;
168 }
169 else
170 ((struct sockaddr_in *)&ss)->sin_addr =
171 *(struct in_addr *) *(he->h_addr_list);
172 }
173 }
174
175 /* 3) look for the port-end delimiter */
176 if ((c = strchr(range, '-')) != NULL) {
177 *c++ = 0;
178 end = atol(c);
179 }
180 else {
181 end = atol(range);
182 }
183
184 port = atol(range);
185
186 if (port < 1 || port > 65535) {
187 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
188 goto fail;
189 }
190
191 if (end < 1 || end > 65535) {
192 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
193 goto fail;
194 }
195
196 for (; port <= end; port++) {
197 l = (struct listener *)calloc(1, sizeof(struct listener));
198 l->next = tail;
199 tail = l;
200
201 l->fd = -1;
202 l->addr = ss;
203 if (ss.ss_family == AF_INET6)
204 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
205 else
206 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
207
208 } /* end for(port) */
209 } /* end while(next) */
210 free(dupstr);
211 return tail;
212 fail:
213 free(dupstr);
214 return NULL;
215}
216
Willy Tarreau977b8e42006-12-29 14:19:17 +0100217/*
218 * Sends a warning if proxy <proxy> does not have at least one of the
219 * capabilities in <cap>. An optionnal <hint> may be added at the end
220 * of the warning to help the user. Returns 1 if a warning was emitted
221 * or 0 if the condition is valid.
222 */
223int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, char *arg, char *hint)
224{
225 char *msg;
226
227 switch (cap) {
228 case PR_CAP_BE: msg = "no backend"; break;
229 case PR_CAP_FE: msg = "no frontend"; break;
230 case PR_CAP_RS: msg = "no ruleset"; break;
231 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
232 default: msg = "not enough"; break;
233 }
234
235 if (!(proxy->cap & cap)) {
236 Warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100237 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
Willy Tarreau977b8e42006-12-29 14:19:17 +0100238 return 1;
239 }
240 return 0;
241}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242
243/*
244 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
245 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200246int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247{
248
249 if (!strcmp(args[0], "global")) { /* new section */
250 /* no option, nothing special to do */
251 return 0;
252 }
253 else if (!strcmp(args[0], "daemon")) {
254 global.mode |= MODE_DAEMON;
255 }
256 else if (!strcmp(args[0], "debug")) {
257 global.mode |= MODE_DEBUG;
258 }
259 else if (!strcmp(args[0], "noepoll")) {
260 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
261 }
262 else if (!strcmp(args[0], "nopoll")) {
263 cfg_polling_mechanism &= ~POLL_USE_POLL;
264 }
265 else if (!strcmp(args[0], "quiet")) {
266 global.mode |= MODE_QUIET;
267 }
268 else if (!strcmp(args[0], "stats")) {
269 global.mode |= MODE_STATS;
270 }
271 else if (!strcmp(args[0], "uid")) {
272 if (global.uid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200273 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 return 0;
275 }
276 if (*(args[1]) == 0) {
277 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
278 return -1;
279 }
280 global.uid = atol(args[1]);
281 }
282 else if (!strcmp(args[0], "gid")) {
283 if (global.gid != 0) {
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200284 Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 return 0;
286 }
287 if (*(args[1]) == 0) {
288 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
289 return -1;
290 }
291 global.gid = atol(args[1]);
292 }
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200293 /* user/group name handling */
294 else if (!strcmp(args[0], "user")) {
295 struct passwd *ha_user;
296 if (global.uid != 0) {
297 Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
298 return 0;
299 }
300 errno = 0;
301 ha_user = getpwnam(args[1]);
302 if (ha_user != NULL) {
303 global.uid = (int)ha_user->pw_uid;
304 }
305 else {
306 Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
307 exit(1);
308 }
309 }
310 else if (!strcmp(args[0], "group")) {
311 struct group *ha_group;
312 if (global.gid != 0) {
313 Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
314 return 0;
315 }
316 errno = 0;
317 ha_group = getgrnam(args[1]);
318 if (ha_group != NULL) {
319 global.gid = (int)ha_group->gr_gid;
320 }
321 else {
322 Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
323 exit(1);
324 }
325 }
326 /* end of user/group name handling*/
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 else if (!strcmp(args[0], "nbproc")) {
328 if (global.nbproc != 0) {
329 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
330 return 0;
331 }
332 if (*(args[1]) == 0) {
333 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
334 return -1;
335 }
336 global.nbproc = atol(args[1]);
337 }
338 else if (!strcmp(args[0], "maxconn")) {
339 if (global.maxconn != 0) {
340 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
341 return 0;
342 }
343 if (*(args[1]) == 0) {
344 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
345 return -1;
346 }
347 global.maxconn = atol(args[1]);
348#ifdef SYSTEM_MAXCONN
349 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
350 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);
351 global.maxconn = DEFAULT_MAXCONN;
352 }
353#endif /* SYSTEM_MAXCONN */
354 }
355 else if (!strcmp(args[0], "ulimit-n")) {
356 if (global.rlimit_nofile != 0) {
357 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
358 return 0;
359 }
360 if (*(args[1]) == 0) {
361 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
362 return -1;
363 }
364 global.rlimit_nofile = atol(args[1]);
365 }
366 else if (!strcmp(args[0], "chroot")) {
367 if (global.chroot != NULL) {
368 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
369 return 0;
370 }
371 if (*(args[1]) == 0) {
372 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
373 return -1;
374 }
375 global.chroot = strdup(args[1]);
376 }
377 else if (!strcmp(args[0], "pidfile")) {
378 if (global.pidfile != NULL) {
379 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
380 return 0;
381 }
382 if (*(args[1]) == 0) {
383 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
384 return -1;
385 }
386 global.pidfile = strdup(args[1]);
387 }
388 else if (!strcmp(args[0], "log")) { /* syslog server address */
389 struct sockaddr_in *sa;
390 int facility, level;
391
392 if (*(args[1]) == 0 || *(args[2]) == 0) {
393 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
394 return -1;
395 }
396
397 facility = get_log_facility(args[2]);
398 if (facility < 0) {
399 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
400 exit(1);
401 }
402
403 level = 7; /* max syslog level = debug */
404 if (*(args[3])) {
405 level = get_log_level(args[3]);
406 if (level < 0) {
407 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
408 exit(1);
409 }
410 }
411
412 sa = str2sa(args[1]);
413 if (!sa->sin_port)
414 sa->sin_port = htons(SYSLOG_PORT);
415
416 if (global.logfac1 == -1) {
417 global.logsrv1 = *sa;
418 global.logfac1 = facility;
419 global.loglev1 = level;
420 }
421 else if (global.logfac2 == -1) {
422 global.logsrv2 = *sa;
423 global.logfac2 = facility;
424 global.loglev2 = level;
425 }
426 else {
427 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
428 return -1;
429 }
430
431 }
432 else {
433 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
434 return -1;
435 }
436 return 0;
437}
438
439
440static void init_default_instance()
441{
442 memset(&defproxy, 0, sizeof(defproxy));
443 defproxy.mode = PR_MODE_TCP;
444 defproxy.state = PR_STNEW;
445 defproxy.maxconn = cfg_maxpconn;
446 defproxy.conn_retries = CONN_RETRIES;
447 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
448}
449
450/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100451 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
452 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200454int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455{
456 static struct proxy *curproxy = NULL;
457 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200458 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459 int rc;
460
Willy Tarreau977b8e42006-12-29 14:19:17 +0100461 if (!strcmp(args[0], "listen"))
462 rc = PR_CAP_LISTEN;
463 else if (!strcmp(args[0], "frontend"))
464 rc = PR_CAP_FE | PR_CAP_RS;
465 else if (!strcmp(args[0], "backend"))
466 rc = PR_CAP_BE | PR_CAP_RS;
467 else if (!strcmp(args[0], "ruleset"))
468 rc = PR_CAP_RS;
469 else
470 rc = PR_CAP_NONE;
471
472 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 if (!*args[1]) {
474 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
475 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
476 file, linenum, args[0]);
477 return -1;
478 }
479
480 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
481 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
482 return -1;
483 }
484
485 curproxy->next = proxy;
486 proxy = curproxy;
487 LIST_INIT(&curproxy->pendconns);
488
489 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100490 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491
492 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100493 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494 curproxy->listen = str2listener(args[2], curproxy->listen);
495 if (!curproxy->listen)
496 return -1;
497 global.maxsock++;
498 }
499
500 /* set default values */
501 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502 curproxy->options = defproxy.options;
Willy Tarreau7ac51f62007-03-25 16:00:04 +0200503 curproxy->except_net = defproxy.except_net;
504 curproxy->except_mask = defproxy.except_mask;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
Willy Tarreau977b8e42006-12-29 14:19:17 +0100506 if (curproxy->cap & PR_CAP_FE) {
507 curproxy->maxconn = defproxy.maxconn;
508
509 /* initialize error relocations */
510 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
511 if (defproxy.errmsg[rc].str)
512 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
513 }
514
515 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
516 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517
Willy Tarreau977b8e42006-12-29 14:19:17 +0100518 if (curproxy->cap & PR_CAP_BE) {
519 curproxy->fullconn = defproxy.fullconn;
520 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Willy Tarreau977b8e42006-12-29 14:19:17 +0100522 if (defproxy.check_req)
523 curproxy->check_req = strdup(defproxy.check_req);
524 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200525
Willy Tarreau977b8e42006-12-29 14:19:17 +0100526 if (defproxy.cookie_name)
527 curproxy->cookie_name = strdup(defproxy.cookie_name);
528 curproxy->cookie_len = defproxy.cookie_len;
529 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530
Willy Tarreau977b8e42006-12-29 14:19:17 +0100531 if (curproxy->cap & PR_CAP_RS) {
532 if (defproxy.capture_name)
533 curproxy->capture_name = strdup(defproxy.capture_name);
534 curproxy->capture_namelen = defproxy.capture_namelen;
535 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100536 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau977b8e42006-12-29 14:19:17 +0100538 if (curproxy->cap & PR_CAP_FE) {
539 curproxy->clitimeout = defproxy.clitimeout;
540 curproxy->uri_auth = defproxy.uri_auth;
541 curproxy->mon_net = defproxy.mon_net;
542 curproxy->mon_mask = defproxy.mon_mask;
543 if (defproxy.monitor_uri)
544 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
545 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100546 if (defproxy.defbe.name)
547 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100548 }
549
550 if (curproxy->cap & PR_CAP_BE) {
551 curproxy->contimeout = defproxy.contimeout;
552 curproxy->srvtimeout = defproxy.srvtimeout;
553 curproxy->source_addr = defproxy.source_addr;
554 }
555
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556 curproxy->mode = defproxy.mode;
557 curproxy->logfac1 = defproxy.logfac1;
558 curproxy->logsrv1 = defproxy.logsrv1;
559 curproxy->loglev1 = defproxy.loglev1;
560 curproxy->logfac2 = defproxy.logfac2;
561 curproxy->logsrv2 = defproxy.logsrv2;
562 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200564
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565 return 0;
566 }
567 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
568 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100569 /* FIXME-20070101: we should do this too at the end of the
570 * config parsing to free all default values.
571 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 if (defproxy.check_req) free(defproxy.check_req);
573 if (defproxy.cookie_name) free(defproxy.cookie_name);
574 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200575 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100576 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100577
578 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
579 if (defproxy.errmsg[rc].len)
580 free(defproxy.errmsg[rc].str);
581 }
582
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583 /* we cannot free uri_auth because it might already be used */
584 init_default_instance();
585 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100586 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587 return 0;
588 }
589 else if (curproxy == NULL) {
590 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
591 return -1;
592 }
593
Willy Tarreau977b8e42006-12-29 14:19:17 +0100594
595 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596 if (!strcmp(args[0], "bind")) { /* new listen addresses */
597 if (curproxy == &defproxy) {
598 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
599 return -1;
600 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100601 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
602 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603
604 if (strchr(args[1], ':') == NULL) {
605 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
606 file, linenum, args[0]);
607 return -1;
608 }
609 curproxy->listen = str2listener(args[1], curproxy->listen);
610 if (!curproxy->listen)
611 return -1;
612 global.maxsock++;
613 return 0;
614 }
615 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
616 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
617 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
618 file, linenum, args[0]);
619 return -1;
620 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100621 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
622 return 0;
623
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 /* flush useless bits */
625 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
626 return 0;
627 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200628 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100629 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
630 return 0;
631
Willy Tarreau1c47f852006-07-09 08:22:27 +0200632 if (!*args[1]) {
633 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
634 file, linenum, args[0]);
635 return -1;
636 }
637
638 if (curproxy->monitor_uri != NULL)
639 free(curproxy->monitor_uri);
640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100641 curproxy->monitor_uri_len = strlen(args[1]);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200642 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100643 memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200644 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
645
646 return 0;
647 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
649 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
650 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
651 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
652 else {
653 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
654 return -1;
655 }
656 }
657 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
658 curproxy->state = PR_STSTOPPED;
659 }
660 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
661 curproxy->state = PR_STNEW;
662 }
663 else if (!strcmp(args[0], "cookie")) { /* cookie name */
664 int cur_arg;
665 // if (curproxy == &defproxy) {
666 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
667 // return -1;
668 // }
669
Willy Tarreau977b8e42006-12-29 14:19:17 +0100670 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
671 return 0;
672
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673 if (curproxy->cookie_name != NULL) {
674 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
675 // file, linenum);
676 // return 0;
677 free(curproxy->cookie_name);
678 }
679
680 if (*(args[1]) == 0) {
681 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
682 file, linenum, args[0]);
683 return -1;
684 }
685 curproxy->cookie_name = strdup(args[1]);
686 curproxy->cookie_len = strlen(curproxy->cookie_name);
687
688 cur_arg = 2;
689 while (*(args[cur_arg])) {
690 if (!strcmp(args[cur_arg], "rewrite")) {
691 curproxy->options |= PR_O_COOK_RW;
692 }
693 else if (!strcmp(args[cur_arg], "indirect")) {
694 curproxy->options |= PR_O_COOK_IND;
695 }
696 else if (!strcmp(args[cur_arg], "insert")) {
697 curproxy->options |= PR_O_COOK_INS;
698 }
699 else if (!strcmp(args[cur_arg], "nocache")) {
700 curproxy->options |= PR_O_COOK_NOC;
701 }
702 else if (!strcmp(args[cur_arg], "postonly")) {
703 curproxy->options |= PR_O_COOK_POST;
704 }
705 else if (!strcmp(args[cur_arg], "prefix")) {
706 curproxy->options |= PR_O_COOK_PFX;
707 }
708 else {
709 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
710 file, linenum, args[0]);
711 return -1;
712 }
713 cur_arg++;
714 }
715 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
716 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
717 file, linenum);
718 return -1;
719 }
720
721 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
722 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
723 file, linenum);
724 return -1;
725 }
726 }/* end else if (!strcmp(args[0], "cookie")) */
727 else if (!strcmp(args[0], "appsession")) { /* cookie name */
728 // if (curproxy == &defproxy) {
729 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
730 // return -1;
731 // }
732
Willy Tarreau977b8e42006-12-29 14:19:17 +0100733 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
734 return 0;
735
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736 if (curproxy->appsession_name != NULL) {
737 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
738 // file, linenum);
739 // return 0;
740 free(curproxy->appsession_name);
741 }
742
743 if (*(args[5]) == 0) {
744 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
745 file, linenum, args[0]);
746 return -1;
747 }
748 have_appsession = 1;
749 curproxy->appsession_name = strdup(args[1]);
750 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
751 curproxy->appsession_len = atoi(args[3]);
752 curproxy->appsession_timeout = atoi(args[5]);
753 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
754 if (rc) {
755 Alert("Error Init Appsession Hashtable.\n");
756 return -1;
757 }
758 } /* Url App Session */
759 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100760 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
761 return 0;
762
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
764 // if (curproxy == &defproxy) {
765 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
766 // return -1;
767 // }
768
769 if (curproxy->capture_name != NULL) {
770 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
771 // file, linenum, args[0]);
772 // return 0;
773 free(curproxy->capture_name);
774 }
775
776 if (*(args[4]) == 0) {
777 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
778 file, linenum, args[0]);
779 return -1;
780 }
781 curproxy->capture_name = strdup(args[2]);
782 curproxy->capture_namelen = strlen(curproxy->capture_name);
783 curproxy->capture_len = atol(args[4]);
784 if (curproxy->capture_len >= CAPTURE_LEN) {
785 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
786 file, linenum, CAPTURE_LEN - 1);
787 curproxy->capture_len = CAPTURE_LEN - 1;
788 }
789 curproxy->to_log |= LW_COOKIE;
790 }
791 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
792 struct cap_hdr *hdr;
793
794 if (curproxy == &defproxy) {
795 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
796 return -1;
797 }
798
799 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
800 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
801 file, linenum, args[0], args[1]);
802 return -1;
803 }
804
805 hdr = calloc(sizeof(struct cap_hdr), 1);
806 hdr->next = curproxy->req_cap;
807 hdr->name = strdup(args[3]);
808 hdr->namelen = strlen(args[3]);
809 hdr->len = atol(args[5]);
810 hdr->index = curproxy->nb_req_cap++;
811 curproxy->req_cap = hdr;
812 curproxy->to_log |= LW_REQHDR;
813 }
814 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
815 struct cap_hdr *hdr;
816
817 if (curproxy == &defproxy) {
818 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
819 return -1;
820 }
821
822 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
823 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
824 file, linenum, args[0], args[1]);
825 return -1;
826 }
827 hdr = calloc(sizeof(struct cap_hdr), 1);
828 hdr->next = curproxy->rsp_cap;
829 hdr->name = strdup(args[3]);
830 hdr->namelen = strlen(args[3]);
831 hdr->len = atol(args[5]);
832 hdr->index = curproxy->nb_rsp_cap++;
833 curproxy->rsp_cap = hdr;
834 curproxy->to_log |= LW_RSPHDR;
835 }
836 else {
837 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
838 file, linenum, args[0]);
839 return -1;
840 }
841 }
842 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
843 if (curproxy->contimeout != defproxy.contimeout) {
844 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
845 return 0;
846 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100847 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
848 return 0;
849
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 if (*(args[1]) == 0) {
851 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
852 file, linenum, args[0]);
853 return -1;
854 }
855 curproxy->contimeout = atol(args[1]);
856 }
857 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
858 if (curproxy->clitimeout != defproxy.clitimeout) {
859 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
860 file, linenum, args[0]);
861 return 0;
862 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100863 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
864 return 0;
865
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 if (*(args[1]) == 0) {
867 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
868 file, linenum, args[0]);
869 return -1;
870 }
871 curproxy->clitimeout = atol(args[1]);
872 }
873 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
874 if (curproxy->srvtimeout != defproxy.srvtimeout) {
875 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
876 return 0;
877 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100878 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
879 return 0;
880
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 if (*(args[1]) == 0) {
882 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
883 file, linenum, args[0]);
884 return -1;
885 }
886 curproxy->srvtimeout = atol(args[1]);
887 }
888 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100889 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
890 return 0;
891
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892 if (*(args[1]) == 0) {
893 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
894 file, linenum, args[0]);
895 return -1;
896 }
897 curproxy->conn_retries = atol(args[1]);
898 }
899 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100900 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
901 return 0;
902
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
904 curproxy->uri_auth = NULL; /* we must detach from the default config */
905
906 if (*(args[1]) == 0) {
907 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
908 return -1;
909 } else if (!strcmp(args[1], "uri")) {
910 if (*(args[2]) == 0) {
911 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
912 return -1;
913 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
914 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
915 return -1;
916 }
917 } else if (!strcmp(args[1], "realm")) {
918 if (*(args[2]) == 0) {
919 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
920 return -1;
921 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
922 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
923 return -1;
924 }
925 } else if (!strcmp(args[1], "auth")) {
926 if (*(args[2]) == 0) {
927 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
928 return -1;
929 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
930 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
931 return -1;
932 }
933 } else if (!strcmp(args[1], "scope")) {
934 if (*(args[2]) == 0) {
935 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
936 return -1;
937 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
938 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
939 return -1;
940 }
941 } else if (!strcmp(args[1], "enable")) {
942 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
943 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
944 return -1;
945 }
946 } else {
947 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
948 file, linenum, args[0]);
949 return -1;
950 }
951 }
952 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100953 int optnum;
954
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 if (*(args[1]) == 0) {
956 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
957 return -1;
958 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100959
960 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
961 if (!strcmp(args[1], cfg_opts[optnum].name)) {
962 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
963 return 0;
964 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +0100965 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +0100966 return 0;
967 }
968 }
969
970 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200971 /* generate a complete HTTP log */
972 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
973 else if (!strcmp(args[1], "tcplog"))
974 /* generate a detailed TCP log */
975 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976 else if (!strcmp(args[1], "tcpka")) {
977 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100978 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
979 return 0;
980
981 if (curproxy->cap & PR_CAP_FE)
982 curproxy->options |= PR_O_TCP_CLI_KA;
983 if (curproxy->cap & PR_CAP_BE)
984 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985 }
986 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100987 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
988 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 /* use HTTP request to check servers' health */
990 if (curproxy->check_req != NULL) {
991 free(curproxy->check_req);
992 }
993 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200994 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995 if (!*args[2]) { /* no argument */
996 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
997 curproxy->check_len = strlen(DEF_CHECK_REQ);
998 } else if (!*args[3]) { /* one argument : URI */
999 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
1000 curproxy->check_req = (char *)malloc(reqlen);
1001 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1002 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
1003 } else { /* more arguments : METHOD URI [HTTP_VER] */
1004 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
1005 if (*args[4])
1006 reqlen += strlen(args[4]);
1007 else
1008 reqlen += strlen("HTTP/1.0");
1009
1010 curproxy->check_req = (char *)malloc(reqlen);
1011 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
1012 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
1013 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001014 }
1015 else if (!strcmp(args[1], "ssl-hello-chk")) {
1016 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +01001017 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
1018 return 0;
1019
Willy Tarreauf3c69202006-07-09 16:42:34 +02001020 if (curproxy->check_req != NULL) {
1021 free(curproxy->check_req);
1022 }
1023 curproxy->options &= ~PR_O_HTTP_CHK;
1024 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025 }
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001026 else if (!strcmp(args[1], "forwardfor")) {
1027 /* insert x-forwarded-for field, but not for the
1028 * IP address listed as an except.
1029 */
1030 if (*(args[2])) {
1031 if (!strcmp(args[2], "except")) {
1032 if (!*args[3] || !str2net(args[3], &curproxy->except_net, &curproxy->except_mask)) {
1033 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1034 file, linenum, args[0]);
1035 return -1;
1036 }
1037 /* flush useless bits */
1038 curproxy->except_net.s_addr &= curproxy->except_mask.s_addr;
1039 } else {
1040 Alert("parsing [%s:%d] : '%s' only supports optional 'except' address[/mask].\n",
1041 file, linenum, args[0]);
1042 return -1;
1043 }
1044 }
1045 curproxy->options |= PR_O_FWDFOR;
1046 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047 else {
1048 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1049 return -1;
1050 }
1051 return 0;
1052 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001053 else if (!strcmp(args[0], "default_backend")) {
1054 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
1055 return 0;
1056
1057 if (*(args[1]) == 0) {
1058 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
1059 return -1;
1060 }
1061 if (curproxy->defbe.name)
1062 free(curproxy->defbe.name);
1063 curproxy->defbe.name = strdup(args[1]);
1064 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001066 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1067 return 0;
1068
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069 /* enable reconnections to dispatch */
1070 curproxy->options |= PR_O_REDISP;
1071 }
1072#ifdef TPROXY
1073 else if (!strcmp(args[0], "transparent")) {
1074 /* enable transparent proxy connections */
1075 curproxy->options |= PR_O_TRANSP;
1076 }
1077#endif
1078 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001079 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1080 return 0;
1081
Willy Tarreaubaaee002006-06-26 02:48:02 +02001082 if (*(args[1]) == 0) {
1083 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1084 return -1;
1085 }
1086 curproxy->maxconn = atol(args[1]);
1087 }
Willy Tarreau86034312006-12-29 00:10:33 +01001088 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001089 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1090 return 0;
1091
Willy Tarreau86034312006-12-29 00:10:33 +01001092 if (*(args[1]) == 0) {
1093 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1094 return -1;
1095 }
1096 curproxy->fullconn = atol(args[1]);
1097 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1099 if (*(args[1]) == 0) {
1100 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1101 return -1;
1102 }
1103 curproxy->grace = atol(args[1]);
1104 }
1105 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1106 if (curproxy == &defproxy) {
1107 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1108 return -1;
1109 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001110 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1111 return 0;
1112
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 if (strchr(args[1], ':') == NULL) {
1114 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1115 return -1;
1116 }
1117 curproxy->dispatch_addr = *str2sa(args[1]);
1118 }
1119 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001120 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1121 return 0;
1122
Willy Tarreaubaaee002006-06-26 02:48:02 +02001123 if (*(args[1])) {
1124 if (!strcmp(args[1], "roundrobin")) {
1125 curproxy->options |= PR_O_BALANCE_RR;
1126 }
1127 else if (!strcmp(args[1], "source")) {
1128 curproxy->options |= PR_O_BALANCE_SH;
1129 }
1130 else {
1131 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1132 return -1;
1133 }
1134 }
1135 else /* if no option is set, use round-robin by default */
1136 curproxy->options |= PR_O_BALANCE_RR;
1137 }
1138 else if (!strcmp(args[0], "server")) { /* server address */
1139 int cur_arg;
1140 char *rport;
1141 char *raddr;
1142 short realport;
1143 int do_check;
1144
1145 if (curproxy == &defproxy) {
1146 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1147 return -1;
1148 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001149 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1150 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151
1152 if (!*args[2]) {
1153 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1154 file, linenum, args[0]);
1155 return -1;
1156 }
1157 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1158 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1159 return -1;
1160 }
1161
1162 /* the servers are linked backwards first */
1163 newsrv->next = curproxy->srv;
1164 curproxy->srv = newsrv;
1165 newsrv->proxy = curproxy;
1166
1167 LIST_INIT(&newsrv->pendconns);
1168 do_check = 0;
1169 newsrv->state = SRV_RUNNING; /* early server setup */
1170 newsrv->id = strdup(args[1]);
1171
1172 /* several ways to check the port component :
1173 * - IP => port=+0, relative
1174 * - IP: => port=+0, relative
1175 * - IP:N => port=N, absolute
1176 * - IP:+N => port=+N, relative
1177 * - IP:-N => port=-N, relative
1178 */
1179 raddr = strdup(args[2]);
1180 rport = strchr(raddr, ':');
1181 if (rport) {
1182 *rport++ = 0;
1183 realport = atol(rport);
1184 if (!isdigit((int)*rport))
1185 newsrv->state |= SRV_MAPPORTS;
1186 } else {
1187 realport = 0;
1188 newsrv->state |= SRV_MAPPORTS;
1189 }
1190
1191 newsrv->addr = *str2sa(raddr);
1192 newsrv->addr.sin_port = htons(realport);
1193 free(raddr);
1194
1195 newsrv->curfd = -1; /* no health-check in progress */
1196 newsrv->inter = DEF_CHKINTR;
1197 newsrv->rise = DEF_RISETIME;
1198 newsrv->fall = DEF_FALLTIME;
1199 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001200 newsrv->set_check_addr = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201 cur_arg = 3;
1202 while (*args[cur_arg]) {
1203 if (!strcmp(args[cur_arg], "cookie")) {
1204 newsrv->cookie = strdup(args[cur_arg + 1]);
1205 newsrv->cklen = strlen(args[cur_arg + 1]);
1206 cur_arg += 2;
1207 }
1208 else if (!strcmp(args[cur_arg], "rise")) {
1209 newsrv->rise = atol(args[cur_arg + 1]);
1210 newsrv->health = newsrv->rise;
1211 cur_arg += 2;
1212 }
1213 else if (!strcmp(args[cur_arg], "fall")) {
1214 newsrv->fall = atol(args[cur_arg + 1]);
1215 cur_arg += 2;
1216 }
1217 else if (!strcmp(args[cur_arg], "inter")) {
1218 newsrv->inter = atol(args[cur_arg + 1]);
1219 cur_arg += 2;
1220 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001221 else if (!strcmp(args[cur_arg], "addr")) {
1222 newsrv->check_addr = *str2sa(args[cur_arg + 1]);
1223 newsrv->set_check_addr = 1;
1224 cur_arg += 2;
1225 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226 else if (!strcmp(args[cur_arg], "port")) {
1227 newsrv->check_port = atol(args[cur_arg + 1]);
1228 cur_arg += 2;
1229 }
1230 else if (!strcmp(args[cur_arg], "backup")) {
1231 newsrv->state |= SRV_BACKUP;
1232 cur_arg ++;
1233 }
1234 else if (!strcmp(args[cur_arg], "weight")) {
1235 int w;
1236 w = atol(args[cur_arg + 1]);
1237 if (w < 1 || w > 256) {
1238 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1239 file, linenum, newsrv->id, w);
1240 return -1;
1241 }
1242 newsrv->uweight = w - 1;
1243 cur_arg += 2;
1244 }
1245 else if (!strcmp(args[cur_arg], "minconn")) {
1246 newsrv->minconn = atol(args[cur_arg + 1]);
1247 cur_arg += 2;
1248 }
1249 else if (!strcmp(args[cur_arg], "maxconn")) {
1250 newsrv->maxconn = atol(args[cur_arg + 1]);
1251 cur_arg += 2;
1252 }
1253 else if (!strcmp(args[cur_arg], "check")) {
1254 global.maxsock++;
1255 do_check = 1;
1256 cur_arg += 1;
1257 }
1258 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1259 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001260#ifdef CONFIG_HAP_CTTPROXY
1261 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1262 file, linenum, "source", "usesrc");
1263#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1265 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001266#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267 return -1;
1268 }
1269 newsrv->state |= SRV_BIND_SRC;
1270 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1271 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001272 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001273#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001274 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001275 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1276 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001277 return -1;
1278 }
1279 if (!*args[cur_arg + 1]) {
1280 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1281 file, linenum, "usesrc");
1282 return -1;
1283 }
1284 if (!strcmp(args[cur_arg + 1], "client")) {
1285 newsrv->state |= SRV_TPROXY_CLI;
1286 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1287 newsrv->state |= SRV_TPROXY_CIP;
1288 } else {
1289 newsrv->state |= SRV_TPROXY_ADDR;
1290 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1291 }
1292 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1293 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001294#else /* no CTTPROXY support */
1295 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1296 file, linenum, "usesrc");
1297 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001298#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001299 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001301#ifdef CONFIG_HAP_CTTPROXY
1302 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1303 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1304 file, linenum, "usesrc", "source");
1305 return -1;
1306 }
1307#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308 else {
1309 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
1310 file, linenum, newsrv->id);
1311 return -1;
1312 }
1313 }
1314
1315 if (do_check) {
1316 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1317 newsrv->check_port = realport; /* by default */
1318 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001319 /* not yet valid, because no port was set on
1320 * the server either. We'll check if we have
1321 * a known port on the first listener.
1322 */
1323 struct listener *l;
1324 l = curproxy->listen;
1325 if (l) {
1326 int port;
1327 port = (l->addr.ss_family == AF_INET6)
1328 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1329 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1330 newsrv->check_port = port;
1331 }
1332 }
1333 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001334 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1335 file, linenum, newsrv->id);
1336 return -1;
1337 }
1338 newsrv->state |= SRV_CHECKED;
1339 }
1340
1341 if (newsrv->state & SRV_BACKUP)
1342 curproxy->srv_bck++;
1343 else
1344 curproxy->srv_act++;
1345 }
1346 else if (!strcmp(args[0], "log")) { /* syslog server address */
1347 struct sockaddr_in *sa;
1348 int facility;
1349
1350 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1351 curproxy->logfac1 = global.logfac1;
1352 curproxy->logsrv1 = global.logsrv1;
1353 curproxy->loglev1 = global.loglev1;
1354 curproxy->logfac2 = global.logfac2;
1355 curproxy->logsrv2 = global.logsrv2;
1356 curproxy->loglev2 = global.loglev2;
1357 }
1358 else if (*(args[1]) && *(args[2])) {
1359 int level;
1360
1361 facility = get_log_facility(args[2]);
1362 if (facility < 0) {
1363 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1364 exit(1);
1365 }
1366
1367 level = 7; /* max syslog level = debug */
1368 if (*(args[3])) {
1369 level = get_log_level(args[3]);
1370 if (level < 0) {
1371 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1372 exit(1);
1373 }
1374 }
1375
1376 sa = str2sa(args[1]);
1377 if (!sa->sin_port)
1378 sa->sin_port = htons(SYSLOG_PORT);
1379
1380 if (curproxy->logfac1 == -1) {
1381 curproxy->logsrv1 = *sa;
1382 curproxy->logfac1 = facility;
1383 curproxy->loglev1 = level;
1384 }
1385 else if (curproxy->logfac2 == -1) {
1386 curproxy->logsrv2 = *sa;
1387 curproxy->logfac2 = facility;
1388 curproxy->loglev2 = level;
1389 }
1390 else {
1391 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1392 return -1;
1393 }
1394 }
1395 else {
1396 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1397 file, linenum);
1398 return -1;
1399 }
1400 }
1401 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001402 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1403 return 0;
1404
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001406#ifdef CONFIG_HAP_CTTPROXY
1407 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1408 file, linenum, "source", "usesrc");
1409#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1411 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001412#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413 return -1;
1414 }
1415
1416 curproxy->source_addr = *str2sa(args[1]);
1417 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001418 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001419#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001420 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1421 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1422 file, linenum, "usesrc");
1423 return -1;
1424 }
1425 if (!*args[3]) {
1426 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1427 file, linenum, "usesrc");
1428 return -1;
1429 }
1430
1431 if (!strcmp(args[3], "client")) {
1432 curproxy->options |= PR_O_TPXY_CLI;
1433 } else if (!strcmp(args[3], "clientip")) {
1434 curproxy->options |= PR_O_TPXY_CIP;
1435 } else {
1436 curproxy->options |= PR_O_TPXY_ADDR;
1437 curproxy->tproxy_addr = *str2sa(args[3]);
1438 }
1439 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001440#else /* no CTTPROXY support */
1441 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1442 file, linenum, "usesrc");
1443 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001444#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001445 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001446 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001447#ifdef CONFIG_HAP_CTTPROXY
1448 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1449 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1450 file, linenum, "usesrc", "source");
1451 return -1;
1452 }
1453#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001454 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1455 regex_t *preg;
1456 if (curproxy == &defproxy) {
1457 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1458 return -1;
1459 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001460 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1461 return 0;
1462
Willy Tarreaubaaee002006-06-26 02:48:02 +02001463 if (*(args[1]) == 0 || *(args[2]) == 0) {
1464 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1465 file, linenum, args[0]);
1466 return -1;
1467 }
1468
1469 preg = calloc(1, sizeof(regex_t));
1470 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1471 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1472 return -1;
1473 }
1474
1475 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1476 if (err) {
1477 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1478 file, linenum, *err);
1479 return -1;
1480 }
1481 }
1482 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1483 regex_t *preg;
1484 if (curproxy == &defproxy) {
1485 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1486 return -1;
1487 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001488 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1489 return 0;
1490
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491 if (*(args[1]) == 0) {
1492 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1493 return -1;
1494 }
1495
1496 preg = calloc(1, sizeof(regex_t));
1497 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1498 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1499 return -1;
1500 }
1501
1502 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1503 }
1504 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1505 regex_t *preg;
1506 if (curproxy == &defproxy) {
1507 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1508 return -1;
1509 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001510 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1511 return 0;
1512
Willy Tarreaubaaee002006-06-26 02:48:02 +02001513 if (*(args[1]) == 0) {
1514 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1515 return -1;
1516 }
1517
1518 preg = calloc(1, sizeof(regex_t));
1519 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1520 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1521 return -1;
1522 }
1523
1524 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1525 }
1526 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1527 regex_t *preg;
1528 if (curproxy == &defproxy) {
1529 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1530 return -1;
1531 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001532 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1533 return 0;
1534
Willy Tarreaubaaee002006-06-26 02:48:02 +02001535 if (*(args[1]) == 0) {
1536 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1537 return -1;
1538 }
1539
1540 preg = calloc(1, sizeof(regex_t));
1541 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1542 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1543 return -1;
1544 }
1545
1546 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1547 }
1548 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1549 regex_t *preg;
1550 if (curproxy == &defproxy) {
1551 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1552 return -1;
1553 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001554 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1555 return 0;
1556
Willy Tarreaubaaee002006-06-26 02:48:02 +02001557 if (*(args[1]) == 0) {
1558 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1559 return -1;
1560 }
1561
1562 preg = calloc(1, sizeof(regex_t));
1563 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1564 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1565 return -1;
1566 }
1567
1568 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1569 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001570 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1571 regex_t *preg;
1572 if (curproxy == &defproxy) {
1573 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1574 return -1;
1575 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001576 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1577 return 0;
1578
Willy Tarreaub8750a82006-09-03 09:56:00 +02001579 if (*(args[1]) == 0) {
1580 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1581 return -1;
1582 }
1583
1584 preg = calloc(1, sizeof(regex_t));
1585 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1586 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1587 return -1;
1588 }
1589
1590 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1591 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001592 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1593 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001594 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001595 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1596 return -1;
1597 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001598 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1599 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001600
Willy Tarreau977b8e42006-12-29 14:19:17 +01001601 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001602 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1603 file, linenum, args[0]);
1604 return -1;
1605 }
1606
1607 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001608 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001609 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1610 }
1611
1612 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1613 }
1614 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1615 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001616 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001617 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1618 return -1;
1619 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001620 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1621 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001622
Willy Tarreau977b8e42006-12-29 14:19:17 +01001623 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001624 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1625 file, linenum, args[0]);
1626 return -1;
1627 }
1628
1629 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001630 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001631 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1632 }
1633
1634 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1635 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1637 regex_t *preg;
1638 if (curproxy == &defproxy) {
1639 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1640 return -1;
1641 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001642 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1643 return 0;
1644
Willy Tarreaubaaee002006-06-26 02:48:02 +02001645 if (*(args[1]) == 0 || *(args[2]) == 0) {
1646 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1647 file, linenum, args[0]);
1648 return -1;
1649 }
1650
1651 preg = calloc(1, sizeof(regex_t));
1652 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1653 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1654 return -1;
1655 }
1656
1657 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1658 if (err) {
1659 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1660 file, linenum, *err);
1661 return -1;
1662 }
1663 }
1664 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1665 regex_t *preg;
1666 if (curproxy == &defproxy) {
1667 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1668 return -1;
1669 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001670 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1671 return 0;
1672
Willy Tarreaubaaee002006-06-26 02:48:02 +02001673 if (*(args[1]) == 0) {
1674 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1675 return -1;
1676 }
1677
1678 preg = calloc(1, sizeof(regex_t));
1679 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1680 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1681 return -1;
1682 }
1683
1684 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1685 }
1686 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1687 regex_t *preg;
1688 if (curproxy == &defproxy) {
1689 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1690 return -1;
1691 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001692 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1693 return 0;
1694
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695 if (*(args[1]) == 0) {
1696 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1697 return -1;
1698 }
1699
1700 preg = calloc(1, sizeof(regex_t));
1701 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1702 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1703 return -1;
1704 }
1705
1706 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1707 }
1708 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1709 regex_t *preg;
1710 if (curproxy == &defproxy) {
1711 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1712 return -1;
1713 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001714 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1715 return 0;
1716
Willy Tarreaubaaee002006-06-26 02:48:02 +02001717 if (*(args[1]) == 0) {
1718 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1719 return -1;
1720 }
1721
1722 preg = calloc(1, sizeof(regex_t));
1723 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1724 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1725 return -1;
1726 }
1727
1728 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1729 }
1730 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1731 regex_t *preg;
1732 if (curproxy == &defproxy) {
1733 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1734 return -1;
1735 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001736 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1737 return 0;
1738
Willy Tarreaubaaee002006-06-26 02:48:02 +02001739 if (*(args[1]) == 0) {
1740 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1741 return -1;
1742 }
1743
1744 preg = calloc(1, sizeof(regex_t));
1745 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1746 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1747 return -1;
1748 }
1749
1750 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1751 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001752 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1753 regex_t *preg;
1754 if (curproxy == &defproxy) {
1755 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1756 return -1;
1757 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001758 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1759 return 0;
1760
Willy Tarreaub8750a82006-09-03 09:56:00 +02001761 if (*(args[1]) == 0) {
1762 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1763 return -1;
1764 }
1765
1766 preg = calloc(1, sizeof(regex_t));
1767 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1768 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1769 return -1;
1770 }
1771
1772 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001774 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1775 if (curproxy == &defproxy) {
1776 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1777 return -1;
1778 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001779 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1780 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001781
1782 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1783 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1784 return 0;
1785 }
1786
1787 if (*(args[1]) == 0) {
1788 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1789 return -1;
1790 }
1791
1792 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1793 }
1794 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1795 regex_t *preg;
1796
1797 if (*(args[1]) == 0 || *(args[2]) == 0) {
1798 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1799 file, linenum, args[0]);
1800 return -1;
1801 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001802 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1803 return 0;
1804
Willy Tarreaubaaee002006-06-26 02:48:02 +02001805 preg = calloc(1, sizeof(regex_t));
1806 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1807 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1808 return -1;
1809 }
1810
1811 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1812 if (err) {
1813 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1814 file, linenum, *err);
1815 return -1;
1816 }
1817 }
1818 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1819 regex_t *preg;
1820 if (curproxy == &defproxy) {
1821 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1822 return -1;
1823 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001824 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1825 return 0;
1826
Willy Tarreaubaaee002006-06-26 02:48:02 +02001827 if (*(args[1]) == 0) {
1828 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1829 return -1;
1830 }
1831
1832 preg = calloc(1, sizeof(regex_t));
1833 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1834 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1835 return -1;
1836 }
1837
1838 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1839 if (err) {
1840 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1841 file, linenum, *err);
1842 return -1;
1843 }
1844 }
1845 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1846 regex_t *preg;
1847 if (curproxy == &defproxy) {
1848 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1849 return -1;
1850 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001851 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1852 return 0;
1853
Willy Tarreaubaaee002006-06-26 02:48:02 +02001854 if (*(args[1]) == 0) {
1855 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1856 return -1;
1857 }
1858
1859 preg = calloc(1, sizeof(regex_t));
1860 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1861 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1862 return -1;
1863 }
1864
1865 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1866 if (err) {
1867 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1868 file, linenum, *err);
1869 return -1;
1870 }
1871 }
1872 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1873 regex_t *preg;
1874 if (curproxy == &defproxy) {
1875 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1876 return -1;
1877 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001878 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1879 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001880
1881 if (*(args[1]) == 0 || *(args[2]) == 0) {
1882 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1883 file, linenum, args[0]);
1884 return -1;
1885 }
1886
1887 preg = calloc(1, sizeof(regex_t));
1888 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1889 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1890 return -1;
1891 }
1892
1893 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1894 if (err) {
1895 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1896 file, linenum, *err);
1897 return -1;
1898 }
1899 }
1900 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1901 regex_t *preg;
1902 if (curproxy == &defproxy) {
1903 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1904 return -1;
1905 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001906 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1907 return 0;
1908
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909 if (*(args[1]) == 0) {
1910 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1911 return -1;
1912 }
1913
1914 preg = calloc(1, sizeof(regex_t));
1915 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1916 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1917 return -1;
1918 }
1919
1920 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1921 if (err) {
1922 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1923 file, linenum, *err);
1924 return -1;
1925 }
1926 }
1927 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1928 regex_t *preg;
1929 if (curproxy == &defproxy) {
1930 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1931 return -1;
1932 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001933 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1934 return 0;
1935
Willy Tarreaubaaee002006-06-26 02:48:02 +02001936 if (*(args[1]) == 0) {
1937 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1938 return -1;
1939 }
1940
1941 preg = calloc(1, sizeof(regex_t));
1942 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1943 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1944 return -1;
1945 }
1946
1947 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1948 if (err) {
1949 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1950 file, linenum, *err);
1951 return -1;
1952 }
1953 }
1954 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1955 if (curproxy == &defproxy) {
1956 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1957 return -1;
1958 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001959 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1960 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001961
1962 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1963 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1964 return 0;
1965 }
1966
1967 if (*(args[1]) == 0) {
1968 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1969 return -1;
1970 }
1971
1972 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1973 }
1974 else if (!strcmp(args[0], "errorloc") ||
1975 !strcmp(args[0], "errorloc302") ||
1976 !strcmp(args[0], "errorloc303")) { /* error location */
1977 int errnum, errlen;
1978 char *err;
1979
1980 // if (curproxy == &defproxy) {
1981 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1982 // return -1;
1983 // }
1984
Willy Tarreau977b8e42006-12-29 14:19:17 +01001985 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
1986 return 0;
1987
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01001989 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 return -1;
1991 }
1992
1993 errnum = atol(args[1]);
1994 if (!strcmp(args[0], "errorloc303")) {
1995 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1996 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1997 } else {
1998 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1999 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2000 }
2001
Willy Tarreau0f772532006-12-23 20:51:41 +01002002 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2003 if (http_err_codes[rc] == errnum) {
2004 if (curproxy->errmsg[rc].str)
2005 free(curproxy->errmsg[rc].str);
2006 curproxy->errmsg[rc].str = err;
2007 curproxy->errmsg[rc].len = errlen;
2008 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002009 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002011
2012 if (rc >= HTTP_ERR_SIZE) {
2013 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2014 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002015 free(err);
2016 }
2017 }
2018 else {
2019 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2020 return -1;
2021 }
2022 return 0;
2023}
2024
2025
2026/*
2027 * This function reads and parses the configuration file given in the argument.
2028 * returns 0 if OK, -1 if error.
2029 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002030int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002031{
2032 char thisline[256];
2033 char *line;
2034 FILE *f;
2035 int linenum = 0;
2036 char *end;
2037 char *args[MAX_LINE_ARGS];
2038 int arg;
2039 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002040 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002041 int confsect = CFG_NONE;
2042
2043 struct proxy *curproxy = NULL;
2044 struct server *newsrv = NULL;
2045
2046 if ((f=fopen(file,"r")) == NULL)
2047 return -1;
2048
2049 init_default_instance();
2050
2051 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2052 linenum++;
2053
2054 end = line + strlen(line);
2055
2056 /* skip leading spaces */
2057 while (isspace((int)*line))
2058 line++;
2059
2060 arg = 0;
2061 args[arg] = line;
2062
2063 while (*line && arg < MAX_LINE_ARGS) {
2064 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2065 * C equivalent value. Other combinations left unchanged (eg: \1).
2066 */
2067 if (*line == '\\') {
2068 int skip = 0;
2069 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2070 *line = line[1];
2071 skip = 1;
2072 }
2073 else if (line[1] == 'r') {
2074 *line = '\r';
2075 skip = 1;
2076 }
2077 else if (line[1] == 'n') {
2078 *line = '\n';
2079 skip = 1;
2080 }
2081 else if (line[1] == 't') {
2082 *line = '\t';
2083 skip = 1;
2084 }
2085 else if (line[1] == 'x') {
2086 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2087 unsigned char hex1, hex2;
2088 hex1 = toupper(line[2]) - '0';
2089 hex2 = toupper(line[3]) - '0';
2090 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2091 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2092 *line = (hex1<<4) + hex2;
2093 skip = 3;
2094 }
2095 else {
2096 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2097 return -1;
2098 }
2099 }
2100 if (skip) {
2101 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2102 end -= skip;
2103 }
2104 line++;
2105 }
2106 else if (*line == '#' || *line == '\n' || *line == '\r') {
2107 /* end of string, end of loop */
2108 *line = 0;
2109 break;
2110 }
2111 else if (isspace((int)*line)) {
2112 /* a non-escaped space is an argument separator */
2113 *line++ = 0;
2114 while (isspace((int)*line))
2115 line++;
2116 args[++arg] = line;
2117 }
2118 else {
2119 line++;
2120 }
2121 }
2122
2123 /* empty line */
2124 if (!**args)
2125 continue;
2126
2127 /* zero out remaining args */
2128 while (++arg < MAX_LINE_ARGS) {
2129 args[arg] = line;
2130 }
2131
Willy Tarreau977b8e42006-12-29 14:19:17 +01002132 if (!strcmp(args[0], "listen") ||
2133 !strcmp(args[0], "frontend") ||
2134 !strcmp(args[0], "backend") ||
2135 !strcmp(args[0], "ruleset") ||
2136 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002137 confsect = CFG_LISTEN;
2138 else if (!strcmp(args[0], "global")) /* global config */
2139 confsect = CFG_GLOBAL;
2140 /* else it's a section keyword */
2141
2142 switch (confsect) {
2143 case CFG_LISTEN:
2144 if (cfg_parse_listen(file, linenum, args) < 0)
2145 return -1;
2146 break;
2147 case CFG_GLOBAL:
2148 if (cfg_parse_global(file, linenum, args) < 0)
2149 return -1;
2150 break;
2151 default:
2152 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2153 return -1;
2154 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002155 }
2156 fclose(f);
2157
2158 /*
2159 * Now, check for the integrity of all that we have collected.
2160 */
2161
2162 /* will be needed further to delay some tasks */
2163 tv_now(&now);
2164
2165 if ((curproxy = proxy) == NULL) {
2166 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2167 file);
2168 return -1;
2169 }
2170
2171 while (curproxy != NULL) {
Willy Tarreau97a738f2006-12-17 18:02:30 +01002172 curproxy->fiprm = curproxy->beprm = curproxy;
2173
Willy Tarreaubaaee002006-06-26 02:48:02 +02002174 if (curproxy->state == PR_STSTOPPED) {
2175 curproxy = curproxy->next;
2176 continue;
2177 }
2178
Willy Tarreau977b8e42006-12-29 14:19:17 +01002179 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2180 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 +01002181 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002182 cfgerr++;
2183 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002184 else if (curproxy->cap & PR_CAP_BE &&
2185 ((curproxy->mode != PR_MODE_HEALTH) &&
2186 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2187 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2188 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 +01002189 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002190 cfgerr++;
2191 }
2192 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2193 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002194 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002195 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002196 cfgerr++;
2197 }
2198#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2199 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002200 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002201 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002202 cfgerr++;
2203 }
2204#endif
2205 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002206 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002207 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002208 }
2209 }
2210 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2211 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002212 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002213 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 }
2215 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002216 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002217 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002218 }
2219 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002220 Warning("parsing %s : server regular expressions 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 (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002224 Warning("parsing %s : client regular expressions 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 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002227 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002228 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002229 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002230 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002231 }
2232 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2233 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2234 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2235 file, curproxy->id);
2236 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002237 }
2238 }
2239
2240 /* if a default backend was specified, let's find it */
2241 if (curproxy->defbe.name) {
2242 struct proxy *target;
2243
2244 for (target = proxy; target != NULL; target = target->next) {
2245 if (strcmp(target->id, curproxy->defbe.name) == 0)
2246 break;
2247 }
2248 if (target == NULL) {
2249 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2250 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2251 cfgerr++;
2252 } else if (target == curproxy) {
2253 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2254 cfgerr++;
2255 } else if (!(target->cap & PR_CAP_BE)) {
2256 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2257 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2258 cfgerr++;
2259 } else if (target->mode != curproxy->mode) {
2260 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2261 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2262 cfgerr++;
2263 } else {
2264 free(curproxy->defbe.name);
2265 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002266 }
2267 }
2268
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002269 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002270 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2271 /* map jump target for ACT_SETBE in req_rep chain */
2272 struct hdr_exp *exp;
2273 struct proxy *target;
2274 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2275 if (exp->action != ACT_SETBE)
2276 continue;
2277 for (target = proxy; target != NULL; target = target->next) {
2278 if (strcmp(target->id, exp->replace) == 0)
2279 break;
2280 }
2281 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002282 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002283 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002284 cfgerr++;
2285 } else if (target == curproxy) {
2286 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2287 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002288 } else if (!(target->cap & PR_CAP_BE)) {
2289 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002290 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002291 cfgerr++;
2292 } else if (target->mode != PR_MODE_HTTP) {
2293 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002294 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002295 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002296 } else {
2297 free((void *)exp->replace);
2298 exp->replace = (const char *)target;
2299 }
2300 }
2301 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002302 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002303 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2304 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2305 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002306 " | While not properly invalid, you will certainly encounter various problems\n"
2307 " | with such a configuration. To fix this, please ensure that all following\n"
2308 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002309 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002310 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002311
2312 if (curproxy->options & PR_O_SSL3_CHK) {
2313 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2314 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2315 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2316 }
2317
Willy Tarreau86034312006-12-29 00:10:33 +01002318 /* for backwards compatibility with "listen" instances, if
2319 * fullconn is not set but maxconn is set, then maxconn
2320 * is used.
2321 */
2322 if (!curproxy->fullconn)
2323 curproxy->fullconn = curproxy->maxconn;
2324
Willy Tarreaubaaee002006-06-26 02:48:02 +02002325 /* first, we will invert the servers list order */
2326 newsrv = NULL;
2327 while (curproxy->srv) {
2328 struct server *next;
2329
2330 next = curproxy->srv->next;
2331 curproxy->srv->next = newsrv;
2332 newsrv = curproxy->srv;
2333 if (!next)
2334 break;
2335 curproxy->srv = next;
2336 }
2337
2338 /* now, newsrv == curproxy->srv */
2339 if (newsrv) {
2340 struct server *srv;
2341 int pgcd;
2342 int act, bck;
2343
2344 /* We will factor the weights to reduce the table,
2345 * using Euclide's largest common divisor algorithm
2346 */
2347 pgcd = newsrv->uweight + 1;
2348 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2349 int t, w;
2350
2351 w = srv->uweight + 1;
2352 while (w) {
2353 t = pgcd % w;
2354 pgcd = w;
2355 w = t;
2356 }
2357 }
2358
2359 act = bck = 0;
2360 for (srv = newsrv; srv; srv = srv->next) {
2361 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
2362 if (srv->state & SRV_BACKUP)
2363 bck += srv->eweight + 1;
2364 else
2365 act += srv->eweight + 1;
2366 }
2367
2368 /* this is the largest map we will ever need for this servers list */
2369 if (act < bck)
2370 act = bck;
2371
2372 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2373 /* recounts servers and their weights */
2374 recount_servers(curproxy);
2375 recalc_server_map(curproxy);
2376 }
2377
2378 if (curproxy->options & PR_O_LOGASAP)
2379 curproxy->to_log &= ~LW_BYTES;
2380
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381 /*
2382 * If this server supports a maxconn parameter, it needs a dedicated
2383 * tasks to fill the emptied slots when a connection leaves.
2384 */
2385 newsrv = curproxy->srv;
2386 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002387 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002388 /* Only 'minconn' was specified, or it was higher than or equal
2389 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2390 * this will avoid further useless expensive computations.
2391 */
2392 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002393 } else if (newsrv->maxconn && !newsrv->minconn) {
2394 /* minconn was not specified, so we set it to maxconn */
2395 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002396 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2397 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002398 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002399 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002400 }
2401
2402 if (newsrv->maxconn > 0) {
2403 struct task *t;
2404
2405 if ((t = pool_alloc(task)) == NULL) {
2406 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2407 return -1;
2408 }
2409
Willy Tarreau964c9362007-01-07 00:38:00 +01002410 t->rqnext = NULL;
2411 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412 t->state = TASK_IDLE;
2413 t->process = process_srv_queue;
2414 t->context = newsrv;
2415 newsrv->queue_mgt = t;
2416
2417 /* never run it unless specifically woken up */
2418 tv_eternity(&t->expire);
2419 task_queue(t);
2420 }
2421 newsrv = newsrv->next;
2422 }
2423
2424 /* now we'll start this proxy's health checks if any */
2425 /* 1- count the checkers to run simultaneously */
2426 nbchk = 0;
2427 mininter = 0;
2428 newsrv = curproxy->srv;
2429 while (newsrv != NULL) {
2430 if (newsrv->state & SRV_CHECKED) {
2431 if (!mininter || mininter > newsrv->inter)
2432 mininter = newsrv->inter;
2433 nbchk++;
2434 }
2435 newsrv = newsrv->next;
2436 }
2437
2438 /* 2- start them as far as possible from each others while respecting
2439 * their own intervals. For this, we will start them after their own
2440 * interval added to the min interval divided by the number of servers,
2441 * weighted by the server's position in the list.
2442 */
2443 if (nbchk > 0) {
2444 struct task *t;
2445 int srvpos;
2446
2447 newsrv = curproxy->srv;
2448 srvpos = 0;
2449 while (newsrv != NULL) {
2450 /* should this server be checked ? */
2451 if (newsrv->state & SRV_CHECKED) {
2452 if ((t = pool_alloc(task)) == NULL) {
2453 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2454 return -1;
2455 }
2456
Willy Tarreau964c9362007-01-07 00:38:00 +01002457 t->wq = NULL;
2458 t->rqnext = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002459 t->state = TASK_IDLE;
2460 t->process = process_chk;
2461 t->context = newsrv;
2462
2463 /* check this every ms */
2464 tv_delayfrom(&t->expire, &now,
2465 newsrv->inter + mininter * srvpos / nbchk);
2466 task_queue(t);
2467 //task_wakeup(&rq, t);
2468 srvpos++;
2469 }
2470 newsrv = newsrv->next;
2471 }
2472 }
2473
2474 curproxy = curproxy->next;
2475 }
2476 if (cfgerr > 0) {
2477 Alert("Errors found in configuration file, aborting.\n");
2478 return -1;
2479 }
2480 else
2481 return 0;
2482}
2483
2484
2485
2486/*
2487 * Local variables:
2488 * c-indent-level: 8
2489 * c-basic-offset: 8
2490 * End:
2491 */