blob: cfb2e7e00a783e7f50c4ae9f611964903f7fc41f [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 Tarreau0f03c6f2007-03-25 20:46:19 +02001200
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]);
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001223 cur_arg += 2;
1224 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001225 else if (!strcmp(args[cur_arg], "port")) {
1226 newsrv->check_port = atol(args[cur_arg + 1]);
1227 cur_arg += 2;
1228 }
1229 else if (!strcmp(args[cur_arg], "backup")) {
1230 newsrv->state |= SRV_BACKUP;
1231 cur_arg ++;
1232 }
1233 else if (!strcmp(args[cur_arg], "weight")) {
1234 int w;
1235 w = atol(args[cur_arg + 1]);
1236 if (w < 1 || w > 256) {
1237 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1238 file, linenum, newsrv->id, w);
1239 return -1;
1240 }
1241 newsrv->uweight = w - 1;
1242 cur_arg += 2;
1243 }
1244 else if (!strcmp(args[cur_arg], "minconn")) {
1245 newsrv->minconn = atol(args[cur_arg + 1]);
1246 cur_arg += 2;
1247 }
1248 else if (!strcmp(args[cur_arg], "maxconn")) {
1249 newsrv->maxconn = atol(args[cur_arg + 1]);
1250 cur_arg += 2;
1251 }
1252 else if (!strcmp(args[cur_arg], "check")) {
1253 global.maxsock++;
1254 do_check = 1;
1255 cur_arg += 1;
1256 }
1257 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1258 if (!*args[cur_arg + 1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001259#ifdef CONFIG_HAP_CTTPROXY
1260 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1261 file, linenum, "source", "usesrc");
1262#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1264 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001265#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 return -1;
1267 }
1268 newsrv->state |= SRV_BIND_SRC;
1269 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1270 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001271 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001272#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001273 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001274 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1275 file, linenum, "usesrc", "source");
Willy Tarreau77074d52006-11-12 23:57:19 +01001276 return -1;
1277 }
1278 if (!*args[cur_arg + 1]) {
1279 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1280 file, linenum, "usesrc");
1281 return -1;
1282 }
1283 if (!strcmp(args[cur_arg + 1], "client")) {
1284 newsrv->state |= SRV_TPROXY_CLI;
1285 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1286 newsrv->state |= SRV_TPROXY_CIP;
1287 } else {
1288 newsrv->state |= SRV_TPROXY_ADDR;
1289 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1290 }
1291 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1292 cur_arg += 2;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001293#else /* no CTTPROXY support */
1294 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1295 file, linenum, "usesrc");
1296 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001297#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001298 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001300#ifdef CONFIG_HAP_CTTPROXY
1301 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1302 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1303 file, linenum, "usesrc", "source");
1304 return -1;
1305 }
1306#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307 else {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001308 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'addr', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309 file, linenum, newsrv->id);
1310 return -1;
1311 }
1312 }
1313
1314 if (do_check) {
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001315 if (!newsrv->check_port && newsrv->check_addr.sin_port)
1316 newsrv->check_port = newsrv->check_addr.sin_port;
1317
Willy Tarreaubaaee002006-06-26 02:48:02 +02001318 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1319 newsrv->check_port = realport; /* by default */
1320 if (!newsrv->check_port) {
Willy Tarreauef00b502007-01-07 02:40:09 +01001321 /* not yet valid, because no port was set on
1322 * the server either. We'll check if we have
1323 * a known port on the first listener.
1324 */
1325 struct listener *l;
1326 l = curproxy->listen;
1327 if (l) {
1328 int port;
1329 port = (l->addr.ss_family == AF_INET6)
1330 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1331 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1332 newsrv->check_port = port;
1333 }
1334 }
1335 if (!newsrv->check_port) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001336 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1337 file, linenum, newsrv->id);
1338 return -1;
1339 }
1340 newsrv->state |= SRV_CHECKED;
1341 }
1342
1343 if (newsrv->state & SRV_BACKUP)
1344 curproxy->srv_bck++;
1345 else
1346 curproxy->srv_act++;
1347 }
1348 else if (!strcmp(args[0], "log")) { /* syslog server address */
1349 struct sockaddr_in *sa;
1350 int facility;
1351
1352 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1353 curproxy->logfac1 = global.logfac1;
1354 curproxy->logsrv1 = global.logsrv1;
1355 curproxy->loglev1 = global.loglev1;
1356 curproxy->logfac2 = global.logfac2;
1357 curproxy->logsrv2 = global.logsrv2;
1358 curproxy->loglev2 = global.loglev2;
1359 }
1360 else if (*(args[1]) && *(args[2])) {
1361 int level;
1362
1363 facility = get_log_facility(args[2]);
1364 if (facility < 0) {
1365 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1366 exit(1);
1367 }
1368
1369 level = 7; /* max syslog level = debug */
1370 if (*(args[3])) {
1371 level = get_log_level(args[3]);
1372 if (level < 0) {
1373 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1374 exit(1);
1375 }
1376 }
1377
1378 sa = str2sa(args[1]);
1379 if (!sa->sin_port)
1380 sa->sin_port = htons(SYSLOG_PORT);
1381
1382 if (curproxy->logfac1 == -1) {
1383 curproxy->logsrv1 = *sa;
1384 curproxy->logfac1 = facility;
1385 curproxy->loglev1 = level;
1386 }
1387 else if (curproxy->logfac2 == -1) {
1388 curproxy->logsrv2 = *sa;
1389 curproxy->logfac2 = facility;
1390 curproxy->loglev2 = level;
1391 }
1392 else {
1393 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1394 return -1;
1395 }
1396 }
1397 else {
1398 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1399 file, linenum);
1400 return -1;
1401 }
1402 }
1403 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001404 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1405 return 0;
1406
Willy Tarreaubaaee002006-06-26 02:48:02 +02001407 if (!*args[1]) {
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001408#ifdef CONFIG_HAP_CTTPROXY
1409 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], and optional '%s' <addr> as argument.\n",
1410 file, linenum, "source", "usesrc");
1411#else
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1413 file, linenum, "source");
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001414#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415 return -1;
1416 }
1417
1418 curproxy->source_addr = *str2sa(args[1]);
1419 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001420 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001421#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau77074d52006-11-12 23:57:19 +01001422 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1423 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1424 file, linenum, "usesrc");
1425 return -1;
1426 }
1427 if (!*args[3]) {
1428 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1429 file, linenum, "usesrc");
1430 return -1;
1431 }
1432
1433 if (!strcmp(args[3], "client")) {
1434 curproxy->options |= PR_O_TPXY_CLI;
1435 } else if (!strcmp(args[3], "clientip")) {
1436 curproxy->options |= PR_O_TPXY_CIP;
1437 } else {
1438 curproxy->options |= PR_O_TPXY_ADDR;
1439 curproxy->tproxy_addr = *str2sa(args[3]);
1440 }
1441 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001442#else /* no CTTPROXY support */
1443 Alert("parsing [%s:%d] : '%s' not allowed here because support for cttproxy was not compiled in.\n",
1444 file, linenum, "usesrc");
1445 return -1;
Willy Tarreau77074d52006-11-12 23:57:19 +01001446#endif
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001447 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001448 }
Willy Tarreau8d9246d2007-03-24 12:47:24 +01001449#ifdef CONFIG_HAP_CTTPROXY
1450 else if (!strcmp(args[0], "usesrc")) { /* address to use outside: needs "source" first */
1451 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1452 file, linenum, "usesrc", "source");
1453 return -1;
1454 }
1455#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001456 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1457 regex_t *preg;
1458 if (curproxy == &defproxy) {
1459 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1460 return -1;
1461 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001462 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1463 return 0;
1464
Willy Tarreaubaaee002006-06-26 02:48:02 +02001465 if (*(args[1]) == 0 || *(args[2]) == 0) {
1466 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1467 file, linenum, args[0]);
1468 return -1;
1469 }
1470
1471 preg = calloc(1, sizeof(regex_t));
1472 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1473 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1474 return -1;
1475 }
1476
1477 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1478 if (err) {
1479 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1480 file, linenum, *err);
1481 return -1;
1482 }
1483 }
1484 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1485 regex_t *preg;
1486 if (curproxy == &defproxy) {
1487 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1488 return -1;
1489 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001490 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1491 return 0;
1492
Willy Tarreaubaaee002006-06-26 02:48:02 +02001493 if (*(args[1]) == 0) {
1494 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1495 return -1;
1496 }
1497
1498 preg = calloc(1, sizeof(regex_t));
1499 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1500 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1501 return -1;
1502 }
1503
1504 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1505 }
1506 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1507 regex_t *preg;
1508 if (curproxy == &defproxy) {
1509 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1510 return -1;
1511 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001512 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1513 return 0;
1514
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 if (*(args[1]) == 0) {
1516 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1517 return -1;
1518 }
1519
1520 preg = calloc(1, sizeof(regex_t));
1521 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1522 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1523 return -1;
1524 }
1525
1526 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1527 }
1528 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1529 regex_t *preg;
1530 if (curproxy == &defproxy) {
1531 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1532 return -1;
1533 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001534 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1535 return 0;
1536
Willy Tarreaubaaee002006-06-26 02:48:02 +02001537 if (*(args[1]) == 0) {
1538 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1539 return -1;
1540 }
1541
1542 preg = calloc(1, sizeof(regex_t));
1543 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1544 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1545 return -1;
1546 }
1547
1548 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1549 }
1550 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1551 regex_t *preg;
1552 if (curproxy == &defproxy) {
1553 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1554 return -1;
1555 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001556 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1557 return 0;
1558
Willy Tarreaubaaee002006-06-26 02:48:02 +02001559 if (*(args[1]) == 0) {
1560 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1561 return -1;
1562 }
1563
1564 preg = calloc(1, sizeof(regex_t));
1565 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1566 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1567 return -1;
1568 }
1569
1570 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1571 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001572 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1573 regex_t *preg;
1574 if (curproxy == &defproxy) {
1575 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1576 return -1;
1577 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001578 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1579 return 0;
1580
Willy Tarreaub8750a82006-09-03 09:56:00 +02001581 if (*(args[1]) == 0) {
1582 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1583 return -1;
1584 }
1585
1586 preg = calloc(1, sizeof(regex_t));
1587 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1588 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1589 return -1;
1590 }
1591
1592 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1593 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001594 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1595 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001596 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001597 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1598 return -1;
1599 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001600 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1601 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001602
Willy Tarreau977b8e42006-12-29 14:19:17 +01001603 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001604 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1605 file, linenum, args[0]);
1606 return -1;
1607 }
1608
1609 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001610 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001611 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1612 }
1613
1614 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1615 }
1616 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1617 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001618 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001619 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1620 return -1;
1621 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001622 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1623 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001624
Willy Tarreau977b8e42006-12-29 14:19:17 +01001625 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001626 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1627 file, linenum, args[0]);
1628 return -1;
1629 }
1630
1631 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001632 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001633 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1634 }
1635
1636 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001638 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1639 regex_t *preg;
1640 if (curproxy == &defproxy) {
1641 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1642 return -1;
1643 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001644 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1645 return 0;
1646
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 if (*(args[1]) == 0 || *(args[2]) == 0) {
1648 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1649 file, linenum, args[0]);
1650 return -1;
1651 }
1652
1653 preg = calloc(1, sizeof(regex_t));
1654 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1655 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1656 return -1;
1657 }
1658
1659 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1660 if (err) {
1661 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1662 file, linenum, *err);
1663 return -1;
1664 }
1665 }
1666 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1667 regex_t *preg;
1668 if (curproxy == &defproxy) {
1669 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1670 return -1;
1671 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001672 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1673 return 0;
1674
Willy Tarreaubaaee002006-06-26 02:48:02 +02001675 if (*(args[1]) == 0) {
1676 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1677 return -1;
1678 }
1679
1680 preg = calloc(1, sizeof(regex_t));
1681 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1682 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1683 return -1;
1684 }
1685
1686 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1687 }
1688 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1689 regex_t *preg;
1690 if (curproxy == &defproxy) {
1691 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1692 return -1;
1693 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001694 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1695 return 0;
1696
Willy Tarreaubaaee002006-06-26 02:48:02 +02001697 if (*(args[1]) == 0) {
1698 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1699 return -1;
1700 }
1701
1702 preg = calloc(1, sizeof(regex_t));
1703 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1704 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1705 return -1;
1706 }
1707
1708 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1709 }
1710 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1711 regex_t *preg;
1712 if (curproxy == &defproxy) {
1713 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1714 return -1;
1715 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001716 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1717 return 0;
1718
Willy Tarreaubaaee002006-06-26 02:48:02 +02001719 if (*(args[1]) == 0) {
1720 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1721 return -1;
1722 }
1723
1724 preg = calloc(1, sizeof(regex_t));
1725 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1726 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1727 return -1;
1728 }
1729
1730 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1731 }
1732 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1733 regex_t *preg;
1734 if (curproxy == &defproxy) {
1735 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1736 return -1;
1737 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001738 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1739 return 0;
1740
Willy Tarreaubaaee002006-06-26 02:48:02 +02001741 if (*(args[1]) == 0) {
1742 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1743 return -1;
1744 }
1745
1746 preg = calloc(1, sizeof(regex_t));
1747 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1748 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1749 return -1;
1750 }
1751
1752 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1753 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001754 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1755 regex_t *preg;
1756 if (curproxy == &defproxy) {
1757 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1758 return -1;
1759 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001760 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1761 return 0;
1762
Willy Tarreaub8750a82006-09-03 09:56:00 +02001763 if (*(args[1]) == 0) {
1764 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1765 return -1;
1766 }
1767
1768 preg = calloc(1, sizeof(regex_t));
1769 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1770 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1771 return -1;
1772 }
1773
1774 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1775 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001776 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1777 if (curproxy == &defproxy) {
1778 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1779 return -1;
1780 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001781 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1782 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001783
1784 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1785 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1786 return 0;
1787 }
1788
1789 if (*(args[1]) == 0) {
1790 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1791 return -1;
1792 }
1793
1794 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1795 }
1796 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1797 regex_t *preg;
1798
1799 if (*(args[1]) == 0 || *(args[2]) == 0) {
1800 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1801 file, linenum, args[0]);
1802 return -1;
1803 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001804 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1805 return 0;
1806
Willy Tarreaubaaee002006-06-26 02:48:02 +02001807 preg = calloc(1, sizeof(regex_t));
1808 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1809 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1810 return -1;
1811 }
1812
1813 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1814 if (err) {
1815 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1816 file, linenum, *err);
1817 return -1;
1818 }
1819 }
1820 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1821 regex_t *preg;
1822 if (curproxy == &defproxy) {
1823 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1824 return -1;
1825 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001826 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1827 return 0;
1828
Willy Tarreaubaaee002006-06-26 02:48:02 +02001829 if (*(args[1]) == 0) {
1830 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1831 return -1;
1832 }
1833
1834 preg = calloc(1, sizeof(regex_t));
1835 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1836 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1837 return -1;
1838 }
1839
1840 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1841 if (err) {
1842 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1843 file, linenum, *err);
1844 return -1;
1845 }
1846 }
1847 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1848 regex_t *preg;
1849 if (curproxy == &defproxy) {
1850 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1851 return -1;
1852 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001853 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1854 return 0;
1855
Willy Tarreaubaaee002006-06-26 02:48:02 +02001856 if (*(args[1]) == 0) {
1857 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1858 return -1;
1859 }
1860
1861 preg = calloc(1, sizeof(regex_t));
1862 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1863 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1864 return -1;
1865 }
1866
1867 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1868 if (err) {
1869 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1870 file, linenum, *err);
1871 return -1;
1872 }
1873 }
1874 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1875 regex_t *preg;
1876 if (curproxy == &defproxy) {
1877 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1878 return -1;
1879 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001880 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1881 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001882
1883 if (*(args[1]) == 0 || *(args[2]) == 0) {
1884 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1885 file, linenum, args[0]);
1886 return -1;
1887 }
1888
1889 preg = calloc(1, sizeof(regex_t));
1890 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1891 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1892 return -1;
1893 }
1894
1895 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1896 if (err) {
1897 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1898 file, linenum, *err);
1899 return -1;
1900 }
1901 }
1902 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1903 regex_t *preg;
1904 if (curproxy == &defproxy) {
1905 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1906 return -1;
1907 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001908 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1909 return 0;
1910
Willy Tarreaubaaee002006-06-26 02:48:02 +02001911 if (*(args[1]) == 0) {
1912 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1913 return -1;
1914 }
1915
1916 preg = calloc(1, sizeof(regex_t));
1917 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1918 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1919 return -1;
1920 }
1921
1922 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1923 if (err) {
1924 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1925 file, linenum, *err);
1926 return -1;
1927 }
1928 }
1929 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1930 regex_t *preg;
1931 if (curproxy == &defproxy) {
1932 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1933 return -1;
1934 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001935 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1936 return 0;
1937
Willy Tarreaubaaee002006-06-26 02:48:02 +02001938 if (*(args[1]) == 0) {
1939 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1940 return -1;
1941 }
1942
1943 preg = calloc(1, sizeof(regex_t));
1944 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1945 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1946 return -1;
1947 }
1948
1949 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1950 if (err) {
1951 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1952 file, linenum, *err);
1953 return -1;
1954 }
1955 }
1956 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1957 if (curproxy == &defproxy) {
1958 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1959 return -1;
1960 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001961 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1962 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001963
1964 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1965 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1966 return 0;
1967 }
1968
1969 if (*(args[1]) == 0) {
1970 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1971 return -1;
1972 }
1973
1974 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1975 }
1976 else if (!strcmp(args[0], "errorloc") ||
1977 !strcmp(args[0], "errorloc302") ||
1978 !strcmp(args[0], "errorloc303")) { /* error location */
1979 int errnum, errlen;
1980 char *err;
1981
1982 // if (curproxy == &defproxy) {
1983 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1984 // return -1;
1985 // }
1986
Willy Tarreau977b8e42006-12-29 14:19:17 +01001987 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
1988 return 0;
1989
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01001991 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992 return -1;
1993 }
1994
1995 errnum = atol(args[1]);
1996 if (!strcmp(args[0], "errorloc303")) {
1997 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1998 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1999 } else {
2000 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
2001 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
2002 }
2003
Willy Tarreau0f772532006-12-23 20:51:41 +01002004 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2005 if (http_err_codes[rc] == errnum) {
2006 if (curproxy->errmsg[rc].str)
2007 free(curproxy->errmsg[rc].str);
2008 curproxy->errmsg[rc].str = err;
2009 curproxy->errmsg[rc].len = errlen;
2010 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002012 }
Willy Tarreau0f772532006-12-23 20:51:41 +01002013
2014 if (rc >= HTTP_ERR_SIZE) {
2015 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
2016 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017 free(err);
2018 }
2019 }
2020 else {
2021 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
2022 return -1;
2023 }
2024 return 0;
2025}
2026
2027
2028/*
2029 * This function reads and parses the configuration file given in the argument.
2030 * returns 0 if OK, -1 if error.
2031 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02002032int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002033{
2034 char thisline[256];
2035 char *line;
2036 FILE *f;
2037 int linenum = 0;
2038 char *end;
2039 char *args[MAX_LINE_ARGS];
2040 int arg;
2041 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002042 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002043 int confsect = CFG_NONE;
2044
2045 struct proxy *curproxy = NULL;
2046 struct server *newsrv = NULL;
2047
2048 if ((f=fopen(file,"r")) == NULL)
2049 return -1;
2050
2051 init_default_instance();
2052
2053 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
2054 linenum++;
2055
2056 end = line + strlen(line);
2057
2058 /* skip leading spaces */
2059 while (isspace((int)*line))
2060 line++;
2061
2062 arg = 0;
2063 args[arg] = line;
2064
2065 while (*line && arg < MAX_LINE_ARGS) {
2066 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
2067 * C equivalent value. Other combinations left unchanged (eg: \1).
2068 */
2069 if (*line == '\\') {
2070 int skip = 0;
2071 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
2072 *line = line[1];
2073 skip = 1;
2074 }
2075 else if (line[1] == 'r') {
2076 *line = '\r';
2077 skip = 1;
2078 }
2079 else if (line[1] == 'n') {
2080 *line = '\n';
2081 skip = 1;
2082 }
2083 else if (line[1] == 't') {
2084 *line = '\t';
2085 skip = 1;
2086 }
2087 else if (line[1] == 'x') {
2088 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
2089 unsigned char hex1, hex2;
2090 hex1 = toupper(line[2]) - '0';
2091 hex2 = toupper(line[3]) - '0';
2092 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
2093 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
2094 *line = (hex1<<4) + hex2;
2095 skip = 3;
2096 }
2097 else {
2098 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2099 return -1;
2100 }
2101 }
2102 if (skip) {
2103 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
2104 end -= skip;
2105 }
2106 line++;
2107 }
2108 else if (*line == '#' || *line == '\n' || *line == '\r') {
2109 /* end of string, end of loop */
2110 *line = 0;
2111 break;
2112 }
2113 else if (isspace((int)*line)) {
2114 /* a non-escaped space is an argument separator */
2115 *line++ = 0;
2116 while (isspace((int)*line))
2117 line++;
2118 args[++arg] = line;
2119 }
2120 else {
2121 line++;
2122 }
2123 }
2124
2125 /* empty line */
2126 if (!**args)
2127 continue;
2128
2129 /* zero out remaining args */
2130 while (++arg < MAX_LINE_ARGS) {
2131 args[arg] = line;
2132 }
2133
Willy Tarreau977b8e42006-12-29 14:19:17 +01002134 if (!strcmp(args[0], "listen") ||
2135 !strcmp(args[0], "frontend") ||
2136 !strcmp(args[0], "backend") ||
2137 !strcmp(args[0], "ruleset") ||
2138 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002139 confsect = CFG_LISTEN;
2140 else if (!strcmp(args[0], "global")) /* global config */
2141 confsect = CFG_GLOBAL;
2142 /* else it's a section keyword */
2143
2144 switch (confsect) {
2145 case CFG_LISTEN:
2146 if (cfg_parse_listen(file, linenum, args) < 0)
2147 return -1;
2148 break;
2149 case CFG_GLOBAL:
2150 if (cfg_parse_global(file, linenum, args) < 0)
2151 return -1;
2152 break;
2153 default:
2154 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2155 return -1;
2156 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002157 }
2158 fclose(f);
2159
2160 /*
2161 * Now, check for the integrity of all that we have collected.
2162 */
2163
2164 /* will be needed further to delay some tasks */
2165 tv_now(&now);
2166
2167 if ((curproxy = proxy) == NULL) {
2168 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2169 file);
2170 return -1;
2171 }
2172
2173 while (curproxy != NULL) {
Willy Tarreau97a738f2006-12-17 18:02:30 +01002174 curproxy->fiprm = curproxy->beprm = curproxy;
2175
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 if (curproxy->state == PR_STSTOPPED) {
2177 curproxy = curproxy->next;
2178 continue;
2179 }
2180
Willy Tarreau977b8e42006-12-29 14:19:17 +01002181 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2182 Alert("parsing %s : %s '%s' has no listen address. Please either specify a valid address on the <listen> line, or use the <bind> keyword.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002183 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002184 cfgerr++;
2185 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002186 else if (curproxy->cap & PR_CAP_BE &&
2187 ((curproxy->mode != PR_MODE_HEALTH) &&
2188 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2189 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2190 Alert("parsing %s : %s '%s' has no dispatch address and is not in transparent or balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002191 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192 cfgerr++;
2193 }
2194 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2195 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002196 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002197 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 cfgerr++;
2199 }
2200#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2201 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002202 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002203 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204 cfgerr++;
2205 }
2206#endif
2207 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002208 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002209 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 }
2211 }
2212 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2213 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002214 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002215 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002216 }
2217 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002218 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002219 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 }
2221 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002222 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002223 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 }
2225 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002226 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002227 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002229 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002230 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002231 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002232 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002233 }
2234 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2235 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2236 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2237 file, curproxy->id);
2238 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002239 }
2240 }
2241
2242 /* if a default backend was specified, let's find it */
2243 if (curproxy->defbe.name) {
2244 struct proxy *target;
2245
2246 for (target = proxy; target != NULL; target = target->next) {
2247 if (strcmp(target->id, curproxy->defbe.name) == 0)
2248 break;
2249 }
2250 if (target == NULL) {
2251 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2252 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2253 cfgerr++;
2254 } else if (target == curproxy) {
2255 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2256 cfgerr++;
2257 } else if (!(target->cap & PR_CAP_BE)) {
2258 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2259 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2260 cfgerr++;
2261 } else if (target->mode != curproxy->mode) {
2262 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2263 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2264 cfgerr++;
2265 } else {
2266 free(curproxy->defbe.name);
2267 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 }
2269 }
2270
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002271 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002272 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2273 /* map jump target for ACT_SETBE in req_rep chain */
2274 struct hdr_exp *exp;
2275 struct proxy *target;
2276 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2277 if (exp->action != ACT_SETBE)
2278 continue;
2279 for (target = proxy; target != NULL; target = target->next) {
2280 if (strcmp(target->id, exp->replace) == 0)
2281 break;
2282 }
2283 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002284 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002285 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002286 cfgerr++;
2287 } else if (target == curproxy) {
2288 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2289 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002290 } else if (!(target->cap & PR_CAP_BE)) {
2291 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002292 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002293 cfgerr++;
2294 } else if (target->mode != PR_MODE_HTTP) {
2295 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002296 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002297 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002298 } else {
2299 free((void *)exp->replace);
2300 exp->replace = (const char *)target;
2301 }
2302 }
2303 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002304 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002305 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2306 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2307 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002308 " | While not properly invalid, you will certainly encounter various problems\n"
2309 " | with such a configuration. To fix this, please ensure that all following\n"
2310 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002311 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002312 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002313
2314 if (curproxy->options & PR_O_SSL3_CHK) {
2315 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2316 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2317 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2318 }
2319
Willy Tarreau86034312006-12-29 00:10:33 +01002320 /* for backwards compatibility with "listen" instances, if
2321 * fullconn is not set but maxconn is set, then maxconn
2322 * is used.
2323 */
2324 if (!curproxy->fullconn)
2325 curproxy->fullconn = curproxy->maxconn;
2326
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 /* first, we will invert the servers list order */
2328 newsrv = NULL;
2329 while (curproxy->srv) {
2330 struct server *next;
2331
2332 next = curproxy->srv->next;
2333 curproxy->srv->next = newsrv;
2334 newsrv = curproxy->srv;
2335 if (!next)
2336 break;
2337 curproxy->srv = next;
2338 }
2339
2340 /* now, newsrv == curproxy->srv */
2341 if (newsrv) {
2342 struct server *srv;
2343 int pgcd;
2344 int act, bck;
2345
2346 /* We will factor the weights to reduce the table,
2347 * using Euclide's largest common divisor algorithm
2348 */
2349 pgcd = newsrv->uweight + 1;
2350 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2351 int t, w;
2352
2353 w = srv->uweight + 1;
2354 while (w) {
2355 t = pgcd % w;
2356 pgcd = w;
2357 w = t;
2358 }
2359 }
2360
2361 act = bck = 0;
2362 for (srv = newsrv; srv; srv = srv->next) {
2363 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
2364 if (srv->state & SRV_BACKUP)
2365 bck += srv->eweight + 1;
2366 else
2367 act += srv->eweight + 1;
2368 }
2369
2370 /* this is the largest map we will ever need for this servers list */
2371 if (act < bck)
2372 act = bck;
2373
2374 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2375 /* recounts servers and their weights */
2376 recount_servers(curproxy);
2377 recalc_server_map(curproxy);
2378 }
2379
2380 if (curproxy->options & PR_O_LOGASAP)
2381 curproxy->to_log &= ~LW_BYTES;
2382
Willy Tarreaubaaee002006-06-26 02:48:02 +02002383 /*
2384 * If this server supports a maxconn parameter, it needs a dedicated
2385 * tasks to fill the emptied slots when a connection leaves.
2386 */
2387 newsrv = curproxy->srv;
2388 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002389 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002390 /* Only 'minconn' was specified, or it was higher than or equal
2391 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2392 * this will avoid further useless expensive computations.
2393 */
2394 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002395 } else if (newsrv->maxconn && !newsrv->minconn) {
2396 /* minconn was not specified, so we set it to maxconn */
2397 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002398 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2399 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002400 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002401 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 }
2403
2404 if (newsrv->maxconn > 0) {
2405 struct task *t;
2406
2407 if ((t = pool_alloc(task)) == NULL) {
2408 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2409 return -1;
2410 }
2411
Willy Tarreau964c9362007-01-07 00:38:00 +01002412 t->rqnext = NULL;
2413 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002414 t->state = TASK_IDLE;
2415 t->process = process_srv_queue;
2416 t->context = newsrv;
2417 newsrv->queue_mgt = t;
2418
2419 /* never run it unless specifically woken up */
2420 tv_eternity(&t->expire);
2421 task_queue(t);
2422 }
2423 newsrv = newsrv->next;
2424 }
2425
2426 /* now we'll start this proxy's health checks if any */
2427 /* 1- count the checkers to run simultaneously */
2428 nbchk = 0;
2429 mininter = 0;
2430 newsrv = curproxy->srv;
2431 while (newsrv != NULL) {
2432 if (newsrv->state & SRV_CHECKED) {
2433 if (!mininter || mininter > newsrv->inter)
2434 mininter = newsrv->inter;
2435 nbchk++;
2436 }
2437 newsrv = newsrv->next;
2438 }
2439
2440 /* 2- start them as far as possible from each others while respecting
2441 * their own intervals. For this, we will start them after their own
2442 * interval added to the min interval divided by the number of servers,
2443 * weighted by the server's position in the list.
2444 */
2445 if (nbchk > 0) {
2446 struct task *t;
2447 int srvpos;
2448
2449 newsrv = curproxy->srv;
2450 srvpos = 0;
2451 while (newsrv != NULL) {
2452 /* should this server be checked ? */
2453 if (newsrv->state & SRV_CHECKED) {
2454 if ((t = pool_alloc(task)) == NULL) {
2455 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2456 return -1;
2457 }
2458
Willy Tarreau964c9362007-01-07 00:38:00 +01002459 t->wq = NULL;
2460 t->rqnext = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002461 t->state = TASK_IDLE;
2462 t->process = process_chk;
2463 t->context = newsrv;
2464
2465 /* check this every ms */
2466 tv_delayfrom(&t->expire, &now,
2467 newsrv->inter + mininter * srvpos / nbchk);
2468 task_queue(t);
2469 //task_wakeup(&rq, t);
2470 srvpos++;
2471 }
2472 newsrv = newsrv->next;
2473 }
2474 }
2475
2476 curproxy = curproxy->next;
2477 }
2478 if (cfgerr > 0) {
2479 Alert("Errors found in configuration file, aborting.\n");
2480 return -1;
2481 }
2482 else
2483 return 0;
2484}
2485
2486
2487
2488/*
2489 * Local variables:
2490 * c-indent-level: 8
2491 * c-basic-offset: 8
2492 * End:
2493 */