blob: 7b517d7716dad6c3311734eb36f2d6ff35136b7e [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>
18
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/cfgparse.h>
20#include <common/config.h>
21#include <common/memory.h>
22#include <common/standard.h>
23#include <common/time.h>
24#include <common/uri_auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
26#include <types/capture.h>
27#include <types/global.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010028#include <types/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/polling.h>
30#include <types/proxy.h>
31#include <types/queue.h>
32
33#include <proto/backend.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010034#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/checks.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010036#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/log.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010038#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/server.h>
40#include <proto/task.h>
41
42
Willy Tarreauf3c69202006-07-09 16:42:34 +020043/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
44 * ssl-hello-chk option to ensure that the remote server speaks SSL.
45 *
46 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
47 */
48const char sslv3_client_hello_pkt[] = {
49 "\x16" /* ContentType : 0x16 = Hanshake */
50 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
51 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
52 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
53 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
54 "\x03\x00" /* Hello Version : 0x0300 = v3 */
55 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
56 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
57 "\x00" /* Session ID length : empty (no session ID) */
58 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
59 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
60 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
61 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
62 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
63 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
64 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
65 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
66 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
67 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
68 "\x00\x38" "\x00\x39" "\x00\x3A"
69 "\x01" /* Compression Length : 0x01 = 1 byte for types */
70 "\x00" /* Compression Type : 0x00 = NULL compression */
71};
72
Willy Tarreau13943ab2006-12-31 00:24:10 +010073/* some of the most common options which are also the easiest to handle */
74static const struct {
75 const char *name;
76 unsigned int val;
77 unsigned int cap;
Willy Tarreau4fee4e92007-01-06 21:09:17 +010078 unsigned int checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +010079} cfg_opts[] =
80{
81#ifdef TPROXY
82 { "transparent", PR_O_TRANSP, PR_CAP_FE },
83#endif
Willy Tarreau4fee4e92007-01-06 21:09:17 +010084 { "redispatch", PR_O_REDISP, PR_CAP_BE, 0 },
85 { "keepalive", PR_O_KEEPALIVE, PR_CAP_NONE, 0 },
86 { "forwardfor", PR_O_FWDFOR, PR_CAP_FE | PR_CAP_BE, 0 },
87 { "httpclose", PR_O_HTTP_CLOSE, PR_CAP_FE | PR_CAP_BE, 0 },
88 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0 },
89 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0 },
90 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0 },
91 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0 },
92 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0 },
93 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0 },
94 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0 },
95 { "persist", PR_O_PERSIST, PR_CAP_BE, 0 },
96 { "forceclose", PR_O_FORCE_CLO | PR_O_HTTP_CLOSE, PR_CAP_BE, 0 },
Willy Tarreau13943ab2006-12-31 00:24:10 +010097 { NULL, 0, 0 }
98};
99
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
101static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
102int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
103int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
104
105/*
106 * converts <str> to a list of listeners which are dynamically allocated.
107 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
108 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
109 * - <port> is a numerical port from 1 to 65535 ;
110 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
111 * This can be repeated as many times as necessary, separated by a coma.
112 * The <tail> argument is a pointer to a current list which should be appended
113 * to the tail of the new list. The pointer to the new list is returned.
114 */
115static struct listener *str2listener(char *str, struct listener *tail)
116{
117 struct listener *l;
118 char *c, *next, *range, *dupstr;
119 int port, end;
120
121 next = dupstr = strdup(str);
122
123 while (next && *next) {
124 struct sockaddr_storage ss;
125
126 str = next;
127 /* 1) look for the end of the first address */
128 if ((next = strrchr(str, ',')) != NULL) {
129 *next++ = 0;
130 }
131
132 /* 2) look for the addr/port delimiter, it's the last colon. */
133 if ((range = strrchr(str, ':')) == NULL) {
134 Alert("Missing port number: '%s'\n", str);
135 goto fail;
136 }
137
138 *range++ = 0;
139
140 if (strrchr(str, ':') != NULL) {
141 /* IPv6 address contains ':' */
142 memset(&ss, 0, sizeof(ss));
143 ss.ss_family = AF_INET6;
144
145 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
146 Alert("Invalid server address: '%s'\n", str);
147 goto fail;
148 }
149 }
150 else {
151 memset(&ss, 0, sizeof(ss));
152 ss.ss_family = AF_INET;
153
154 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
155 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
156 }
157 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
158 struct hostent *he;
159
160 if ((he = gethostbyname(str)) == NULL) {
161 Alert("Invalid server name: '%s'\n", str);
162 goto fail;
163 }
164 else
165 ((struct sockaddr_in *)&ss)->sin_addr =
166 *(struct in_addr *) *(he->h_addr_list);
167 }
168 }
169
170 /* 3) look for the port-end delimiter */
171 if ((c = strchr(range, '-')) != NULL) {
172 *c++ = 0;
173 end = atol(c);
174 }
175 else {
176 end = atol(range);
177 }
178
179 port = atol(range);
180
181 if (port < 1 || port > 65535) {
182 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
183 goto fail;
184 }
185
186 if (end < 1 || end > 65535) {
187 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
188 goto fail;
189 }
190
191 for (; port <= end; port++) {
192 l = (struct listener *)calloc(1, sizeof(struct listener));
193 l->next = tail;
194 tail = l;
195
196 l->fd = -1;
197 l->addr = ss;
198 if (ss.ss_family == AF_INET6)
199 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
200 else
201 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
202
203 } /* end for(port) */
204 } /* end while(next) */
205 free(dupstr);
206 return tail;
207 fail:
208 free(dupstr);
209 return NULL;
210}
211
Willy Tarreau977b8e42006-12-29 14:19:17 +0100212/*
213 * Sends a warning if proxy <proxy> does not have at least one of the
214 * capabilities in <cap>. An optionnal <hint> may be added at the end
215 * of the warning to help the user. Returns 1 if a warning was emitted
216 * or 0 if the condition is valid.
217 */
218int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, char *arg, char *hint)
219{
220 char *msg;
221
222 switch (cap) {
223 case PR_CAP_BE: msg = "no backend"; break;
224 case PR_CAP_FE: msg = "no frontend"; break;
225 case PR_CAP_RS: msg = "no ruleset"; break;
226 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
227 default: msg = "not enough"; break;
228 }
229
230 if (!(proxy->cap & cap)) {
231 Warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100232 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
Willy Tarreau977b8e42006-12-29 14:19:17 +0100233 return 1;
234 }
235 return 0;
236}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237
238/*
239 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
240 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200241int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242{
243
244 if (!strcmp(args[0], "global")) { /* new section */
245 /* no option, nothing special to do */
246 return 0;
247 }
248 else if (!strcmp(args[0], "daemon")) {
249 global.mode |= MODE_DAEMON;
250 }
251 else if (!strcmp(args[0], "debug")) {
252 global.mode |= MODE_DEBUG;
253 }
254 else if (!strcmp(args[0], "noepoll")) {
255 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
256 }
257 else if (!strcmp(args[0], "nopoll")) {
258 cfg_polling_mechanism &= ~POLL_USE_POLL;
259 }
260 else if (!strcmp(args[0], "quiet")) {
261 global.mode |= MODE_QUIET;
262 }
263 else if (!strcmp(args[0], "stats")) {
264 global.mode |= MODE_STATS;
265 }
266 else if (!strcmp(args[0], "uid")) {
267 if (global.uid != 0) {
268 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
269 return 0;
270 }
271 if (*(args[1]) == 0) {
272 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
273 return -1;
274 }
275 global.uid = atol(args[1]);
276 }
277 else if (!strcmp(args[0], "gid")) {
278 if (global.gid != 0) {
279 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
280 return 0;
281 }
282 if (*(args[1]) == 0) {
283 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
284 return -1;
285 }
286 global.gid = atol(args[1]);
287 }
288 else if (!strcmp(args[0], "nbproc")) {
289 if (global.nbproc != 0) {
290 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
291 return 0;
292 }
293 if (*(args[1]) == 0) {
294 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
295 return -1;
296 }
297 global.nbproc = atol(args[1]);
298 }
299 else if (!strcmp(args[0], "maxconn")) {
300 if (global.maxconn != 0) {
301 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
302 return 0;
303 }
304 if (*(args[1]) == 0) {
305 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
306 return -1;
307 }
308 global.maxconn = atol(args[1]);
309#ifdef SYSTEM_MAXCONN
310 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
311 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);
312 global.maxconn = DEFAULT_MAXCONN;
313 }
314#endif /* SYSTEM_MAXCONN */
315 }
316 else if (!strcmp(args[0], "ulimit-n")) {
317 if (global.rlimit_nofile != 0) {
318 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
319 return 0;
320 }
321 if (*(args[1]) == 0) {
322 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
323 return -1;
324 }
325 global.rlimit_nofile = atol(args[1]);
326 }
327 else if (!strcmp(args[0], "chroot")) {
328 if (global.chroot != NULL) {
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 a directory as an argument.\n", file, linenum, args[0]);
334 return -1;
335 }
336 global.chroot = strdup(args[1]);
337 }
338 else if (!strcmp(args[0], "pidfile")) {
339 if (global.pidfile != NULL) {
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 a file name as an argument.\n", file, linenum, args[0]);
345 return -1;
346 }
347 global.pidfile = strdup(args[1]);
348 }
349 else if (!strcmp(args[0], "log")) { /* syslog server address */
350 struct sockaddr_in *sa;
351 int facility, level;
352
353 if (*(args[1]) == 0 || *(args[2]) == 0) {
354 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
355 return -1;
356 }
357
358 facility = get_log_facility(args[2]);
359 if (facility < 0) {
360 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
361 exit(1);
362 }
363
364 level = 7; /* max syslog level = debug */
365 if (*(args[3])) {
366 level = get_log_level(args[3]);
367 if (level < 0) {
368 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
369 exit(1);
370 }
371 }
372
373 sa = str2sa(args[1]);
374 if (!sa->sin_port)
375 sa->sin_port = htons(SYSLOG_PORT);
376
377 if (global.logfac1 == -1) {
378 global.logsrv1 = *sa;
379 global.logfac1 = facility;
380 global.loglev1 = level;
381 }
382 else if (global.logfac2 == -1) {
383 global.logsrv2 = *sa;
384 global.logfac2 = facility;
385 global.loglev2 = level;
386 }
387 else {
388 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
389 return -1;
390 }
391
392 }
393 else {
394 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
395 return -1;
396 }
397 return 0;
398}
399
400
401static void init_default_instance()
402{
403 memset(&defproxy, 0, sizeof(defproxy));
404 defproxy.mode = PR_MODE_TCP;
405 defproxy.state = PR_STNEW;
406 defproxy.maxconn = cfg_maxpconn;
407 defproxy.conn_retries = CONN_RETRIES;
408 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
409}
410
411/*
Willy Tarreau977b8e42006-12-29 14:19:17 +0100412 * Parse a line in a <listen>, <frontend>, <backend> or <ruleset> section.
413 * Returns 0 if OK, -1 if error.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200415int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416{
417 static struct proxy *curproxy = NULL;
418 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200419 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420 int rc;
421
Willy Tarreau977b8e42006-12-29 14:19:17 +0100422 if (!strcmp(args[0], "listen"))
423 rc = PR_CAP_LISTEN;
424 else if (!strcmp(args[0], "frontend"))
425 rc = PR_CAP_FE | PR_CAP_RS;
426 else if (!strcmp(args[0], "backend"))
427 rc = PR_CAP_BE | PR_CAP_RS;
428 else if (!strcmp(args[0], "ruleset"))
429 rc = PR_CAP_RS;
430 else
431 rc = PR_CAP_NONE;
432
433 if (rc != PR_CAP_NONE) { /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 if (!*args[1]) {
435 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
436 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
437 file, linenum, args[0]);
438 return -1;
439 }
440
441 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
442 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
443 return -1;
444 }
445
446 curproxy->next = proxy;
447 proxy = curproxy;
448 LIST_INIT(&curproxy->pendconns);
449
450 curproxy->id = strdup(args[1]);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100451 curproxy->cap = rc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452
453 /* parse the listener address if any */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100454 if ((curproxy->cap & PR_CAP_FE) && *args[2]) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 curproxy->listen = str2listener(args[2], curproxy->listen);
456 if (!curproxy->listen)
457 return -1;
458 global.maxsock++;
459 }
460
461 /* set default values */
462 curproxy->state = defproxy.state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 curproxy->options = defproxy.options;
464
Willy Tarreau977b8e42006-12-29 14:19:17 +0100465 if (curproxy->cap & PR_CAP_FE) {
466 curproxy->maxconn = defproxy.maxconn;
467
468 /* initialize error relocations */
469 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
470 if (defproxy.errmsg[rc].str)
471 chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
472 }
473
474 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
475 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476
Willy Tarreau977b8e42006-12-29 14:19:17 +0100477 if (curproxy->cap & PR_CAP_BE) {
478 curproxy->fullconn = defproxy.fullconn;
479 curproxy->conn_retries = defproxy.conn_retries;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480
Willy Tarreau977b8e42006-12-29 14:19:17 +0100481 if (defproxy.check_req)
482 curproxy->check_req = strdup(defproxy.check_req);
483 curproxy->check_len = defproxy.check_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau977b8e42006-12-29 14:19:17 +0100485 if (defproxy.cookie_name)
486 curproxy->cookie_name = strdup(defproxy.cookie_name);
487 curproxy->cookie_len = defproxy.cookie_len;
488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489
Willy Tarreau977b8e42006-12-29 14:19:17 +0100490 if (curproxy->cap & PR_CAP_RS) {
491 if (defproxy.capture_name)
492 curproxy->capture_name = strdup(defproxy.capture_name);
493 curproxy->capture_namelen = defproxy.capture_namelen;
494 curproxy->capture_len = defproxy.capture_len;
Willy Tarreau0f772532006-12-23 20:51:41 +0100495 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496
Willy Tarreau977b8e42006-12-29 14:19:17 +0100497 if (curproxy->cap & PR_CAP_FE) {
498 curproxy->clitimeout = defproxy.clitimeout;
499 curproxy->uri_auth = defproxy.uri_auth;
500 curproxy->mon_net = defproxy.mon_net;
501 curproxy->mon_mask = defproxy.mon_mask;
502 if (defproxy.monitor_uri)
503 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
504 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100505 if (defproxy.defbe.name)
506 curproxy->defbe.name = strdup(defproxy.defbe.name);
Willy Tarreau977b8e42006-12-29 14:19:17 +0100507 }
508
509 if (curproxy->cap & PR_CAP_BE) {
510 curproxy->contimeout = defproxy.contimeout;
511 curproxy->srvtimeout = defproxy.srvtimeout;
512 curproxy->source_addr = defproxy.source_addr;
513 }
514
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 curproxy->mode = defproxy.mode;
516 curproxy->logfac1 = defproxy.logfac1;
517 curproxy->logsrv1 = defproxy.logsrv1;
518 curproxy->loglev1 = defproxy.loglev1;
519 curproxy->logfac2 = defproxy.logfac2;
520 curproxy->logsrv2 = defproxy.logsrv2;
521 curproxy->loglev2 = defproxy.loglev2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 curproxy->grace = defproxy.grace;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200523
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 return 0;
525 }
526 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
527 /* some variables may have already been initialized earlier */
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100528 /* FIXME-20070101: we should do this too at the end of the
529 * config parsing to free all default values.
530 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531 if (defproxy.check_req) free(defproxy.check_req);
532 if (defproxy.cookie_name) free(defproxy.cookie_name);
533 if (defproxy.capture_name) free(defproxy.capture_name);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200534 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100535 if (defproxy.defbe.name) free(defproxy.defbe.name);
Willy Tarreau0f772532006-12-23 20:51:41 +0100536
537 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
538 if (defproxy.errmsg[rc].len)
539 free(defproxy.errmsg[rc].str);
540 }
541
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 /* we cannot free uri_auth because it might already be used */
543 init_default_instance();
544 curproxy = &defproxy;
Willy Tarreau977b8e42006-12-29 14:19:17 +0100545 defproxy.cap = PR_CAP_LISTEN; /* all caps for now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 return 0;
547 }
548 else if (curproxy == NULL) {
549 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
550 return -1;
551 }
552
Willy Tarreau977b8e42006-12-29 14:19:17 +0100553
554 /* Now let's parse the proxy-specific keywords */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 if (!strcmp(args[0], "bind")) { /* new listen addresses */
556 if (curproxy == &defproxy) {
557 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
558 return -1;
559 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100560 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
561 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562
563 if (strchr(args[1], ':') == NULL) {
564 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
565 file, linenum, args[0]);
566 return -1;
567 }
568 curproxy->listen = str2listener(args[1], curproxy->listen);
569 if (!curproxy->listen)
570 return -1;
571 global.maxsock++;
572 return 0;
573 }
574 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
575 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
576 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
577 file, linenum, args[0]);
578 return -1;
579 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100580 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
581 return 0;
582
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583 /* flush useless bits */
584 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
585 return 0;
586 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200587 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100588 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
589 return 0;
590
Willy Tarreau1c47f852006-07-09 08:22:27 +0200591 if (!*args[1]) {
592 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
593 file, linenum, args[0]);
594 return -1;
595 }
596
597 if (curproxy->monitor_uri != NULL)
598 free(curproxy->monitor_uri);
599
600 curproxy->monitor_uri_len = strlen(args[1]) + 2; /* include leading and trailing spaces */
601 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
602 memcpy(curproxy->monitor_uri + 1, args[1], curproxy->monitor_uri_len - 2);
603 curproxy->monitor_uri[curproxy->monitor_uri_len-1] = curproxy->monitor_uri[0] = ' ';
604 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
605
606 return 0;
607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
609 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
610 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
611 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
612 else {
613 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
614 return -1;
615 }
616 }
617 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
618 curproxy->state = PR_STSTOPPED;
619 }
620 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
621 curproxy->state = PR_STNEW;
622 }
623 else if (!strcmp(args[0], "cookie")) { /* cookie name */
624 int cur_arg;
625 // if (curproxy == &defproxy) {
626 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
627 // return -1;
628 // }
629
Willy Tarreau977b8e42006-12-29 14:19:17 +0100630 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
631 return 0;
632
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 if (curproxy->cookie_name != NULL) {
634 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
635 // file, linenum);
636 // return 0;
637 free(curproxy->cookie_name);
638 }
639
640 if (*(args[1]) == 0) {
641 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
642 file, linenum, args[0]);
643 return -1;
644 }
645 curproxy->cookie_name = strdup(args[1]);
646 curproxy->cookie_len = strlen(curproxy->cookie_name);
647
648 cur_arg = 2;
649 while (*(args[cur_arg])) {
650 if (!strcmp(args[cur_arg], "rewrite")) {
651 curproxy->options |= PR_O_COOK_RW;
652 }
653 else if (!strcmp(args[cur_arg], "indirect")) {
654 curproxy->options |= PR_O_COOK_IND;
655 }
656 else if (!strcmp(args[cur_arg], "insert")) {
657 curproxy->options |= PR_O_COOK_INS;
658 }
659 else if (!strcmp(args[cur_arg], "nocache")) {
660 curproxy->options |= PR_O_COOK_NOC;
661 }
662 else if (!strcmp(args[cur_arg], "postonly")) {
663 curproxy->options |= PR_O_COOK_POST;
664 }
665 else if (!strcmp(args[cur_arg], "prefix")) {
666 curproxy->options |= PR_O_COOK_PFX;
667 }
668 else {
669 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
670 file, linenum, args[0]);
671 return -1;
672 }
673 cur_arg++;
674 }
675 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
676 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
677 file, linenum);
678 return -1;
679 }
680
681 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
682 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
683 file, linenum);
684 return -1;
685 }
686 }/* end else if (!strcmp(args[0], "cookie")) */
687 else if (!strcmp(args[0], "appsession")) { /* cookie name */
688 // if (curproxy == &defproxy) {
689 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
690 // return -1;
691 // }
692
Willy Tarreau977b8e42006-12-29 14:19:17 +0100693 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
694 return 0;
695
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 if (curproxy->appsession_name != NULL) {
697 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
698 // file, linenum);
699 // return 0;
700 free(curproxy->appsession_name);
701 }
702
703 if (*(args[5]) == 0) {
704 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
705 file, linenum, args[0]);
706 return -1;
707 }
708 have_appsession = 1;
709 curproxy->appsession_name = strdup(args[1]);
710 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
711 curproxy->appsession_len = atoi(args[3]);
712 curproxy->appsession_timeout = atoi(args[5]);
713 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
714 if (rc) {
715 Alert("Error Init Appsession Hashtable.\n");
716 return -1;
717 }
718 } /* Url App Session */
719 else if (!strcmp(args[0], "capture")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100720 if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
721 return 0;
722
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
724 // if (curproxy == &defproxy) {
725 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
726 // return -1;
727 // }
728
729 if (curproxy->capture_name != NULL) {
730 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
731 // file, linenum, args[0]);
732 // return 0;
733 free(curproxy->capture_name);
734 }
735
736 if (*(args[4]) == 0) {
737 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
738 file, linenum, args[0]);
739 return -1;
740 }
741 curproxy->capture_name = strdup(args[2]);
742 curproxy->capture_namelen = strlen(curproxy->capture_name);
743 curproxy->capture_len = atol(args[4]);
744 if (curproxy->capture_len >= CAPTURE_LEN) {
745 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
746 file, linenum, CAPTURE_LEN - 1);
747 curproxy->capture_len = CAPTURE_LEN - 1;
748 }
749 curproxy->to_log |= LW_COOKIE;
750 }
751 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
752 struct cap_hdr *hdr;
753
754 if (curproxy == &defproxy) {
755 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
756 return -1;
757 }
758
759 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
760 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
761 file, linenum, args[0], args[1]);
762 return -1;
763 }
764
765 hdr = calloc(sizeof(struct cap_hdr), 1);
766 hdr->next = curproxy->req_cap;
767 hdr->name = strdup(args[3]);
768 hdr->namelen = strlen(args[3]);
769 hdr->len = atol(args[5]);
770 hdr->index = curproxy->nb_req_cap++;
771 curproxy->req_cap = hdr;
772 curproxy->to_log |= LW_REQHDR;
773 }
774 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
775 struct cap_hdr *hdr;
776
777 if (curproxy == &defproxy) {
778 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
779 return -1;
780 }
781
782 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
783 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
784 file, linenum, args[0], args[1]);
785 return -1;
786 }
787 hdr = calloc(sizeof(struct cap_hdr), 1);
788 hdr->next = curproxy->rsp_cap;
789 hdr->name = strdup(args[3]);
790 hdr->namelen = strlen(args[3]);
791 hdr->len = atol(args[5]);
792 hdr->index = curproxy->nb_rsp_cap++;
793 curproxy->rsp_cap = hdr;
794 curproxy->to_log |= LW_RSPHDR;
795 }
796 else {
797 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
798 file, linenum, args[0]);
799 return -1;
800 }
801 }
802 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
803 if (curproxy->contimeout != defproxy.contimeout) {
804 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
805 return 0;
806 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100807 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
808 return 0;
809
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 if (*(args[1]) == 0) {
811 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
812 file, linenum, args[0]);
813 return -1;
814 }
815 curproxy->contimeout = atol(args[1]);
816 }
817 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
818 if (curproxy->clitimeout != defproxy.clitimeout) {
819 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
820 file, linenum, args[0]);
821 return 0;
822 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100823 else if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
824 return 0;
825
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 if (*(args[1]) == 0) {
827 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
828 file, linenum, args[0]);
829 return -1;
830 }
831 curproxy->clitimeout = atol(args[1]);
832 }
833 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
834 if (curproxy->srvtimeout != defproxy.srvtimeout) {
835 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
836 return 0;
837 }
Willy Tarreau977b8e42006-12-29 14:19:17 +0100838 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
839 return 0;
840
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 if (*(args[1]) == 0) {
842 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
843 file, linenum, args[0]);
844 return -1;
845 }
846 curproxy->srvtimeout = atol(args[1]);
847 }
848 else if (!strcmp(args[0], "retries")) { /* connection retries */
Willy Tarreau977b8e42006-12-29 14:19:17 +0100849 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
850 return 0;
851
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 if (*(args[1]) == 0) {
853 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
854 file, linenum, args[0]);
855 return -1;
856 }
857 curproxy->conn_retries = atol(args[1]);
858 }
859 else if (!strcmp(args[0], "stats")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +0100860 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
861 return 0;
862
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
864 curproxy->uri_auth = NULL; /* we must detach from the default config */
865
866 if (*(args[1]) == 0) {
867 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
868 return -1;
869 } else if (!strcmp(args[1], "uri")) {
870 if (*(args[2]) == 0) {
871 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
872 return -1;
873 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
874 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
875 return -1;
876 }
877 } else if (!strcmp(args[1], "realm")) {
878 if (*(args[2]) == 0) {
879 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
880 return -1;
881 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
882 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
883 return -1;
884 }
885 } else if (!strcmp(args[1], "auth")) {
886 if (*(args[2]) == 0) {
887 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
888 return -1;
889 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
890 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
891 return -1;
892 }
893 } else if (!strcmp(args[1], "scope")) {
894 if (*(args[2]) == 0) {
895 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
896 return -1;
897 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
898 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
899 return -1;
900 }
901 } else if (!strcmp(args[1], "enable")) {
902 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
903 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
904 return -1;
905 }
906 } else {
907 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
908 file, linenum, args[0]);
909 return -1;
910 }
911 }
912 else if (!strcmp(args[0], "option")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100913 int optnum;
914
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 if (*(args[1]) == 0) {
916 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
917 return -1;
918 }
Willy Tarreau13943ab2006-12-31 00:24:10 +0100919
920 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
921 if (!strcmp(args[1], cfg_opts[optnum].name)) {
922 if (warnifnotcap(curproxy, cfg_opts[optnum].cap, file, linenum, args[1], NULL))
923 return 0;
924 curproxy->options |= cfg_opts[optnum].val;
Willy Tarreau4fee4e92007-01-06 21:09:17 +0100925 global.last_checks |= cfg_opts[optnum].checks;
Willy Tarreau13943ab2006-12-31 00:24:10 +0100926 return 0;
927 }
928 }
929
930 if (!strcmp(args[1], "httplog"))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 /* generate a complete HTTP log */
932 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
933 else if (!strcmp(args[1], "tcplog"))
934 /* generate a detailed TCP log */
935 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936 else if (!strcmp(args[1], "tcpka")) {
937 /* enable TCP keep-alives on client and server sessions */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100938 if (warnifnotcap(curproxy, PR_CAP_BE | PR_CAP_FE, file, linenum, args[1], NULL))
939 return 0;
940
941 if (curproxy->cap & PR_CAP_FE)
942 curproxy->options |= PR_O_TCP_CLI_KA;
943 if (curproxy->cap & PR_CAP_BE)
944 curproxy->options |= PR_O_TCP_SRV_KA;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
946 else if (!strcmp(args[1], "httpchk")) {
Willy Tarreau13943ab2006-12-31 00:24:10 +0100947 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
948 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949 /* use HTTP request to check servers' health */
950 if (curproxy->check_req != NULL) {
951 free(curproxy->check_req);
952 }
953 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200954 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 if (!*args[2]) { /* no argument */
956 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
957 curproxy->check_len = strlen(DEF_CHECK_REQ);
958 } else if (!*args[3]) { /* one argument : URI */
959 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
960 curproxy->check_req = (char *)malloc(reqlen);
961 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
962 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
963 } else { /* more arguments : METHOD URI [HTTP_VER] */
964 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
965 if (*args[4])
966 reqlen += strlen(args[4]);
967 else
968 reqlen += strlen("HTTP/1.0");
969
970 curproxy->check_req = (char *)malloc(reqlen);
971 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
972 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
973 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200974 }
975 else if (!strcmp(args[1], "ssl-hello-chk")) {
976 /* use SSLv3 CLIENT HELLO to check servers' health */
Willy Tarreau13943ab2006-12-31 00:24:10 +0100977 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
978 return 0;
979
Willy Tarreauf3c69202006-07-09 16:42:34 +0200980 if (curproxy->check_req != NULL) {
981 free(curproxy->check_req);
982 }
983 curproxy->options &= ~PR_O_HTTP_CHK;
984 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986 else {
987 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
988 return -1;
989 }
990 return 0;
991 }
Willy Tarreau5fdfb912007-01-01 23:11:07 +0100992 else if (!strcmp(args[0], "default_backend")) {
993 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], NULL))
994 return 0;
995
996 if (*(args[1]) == 0) {
997 Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
998 return -1;
999 }
1000 if (curproxy->defbe.name)
1001 free(curproxy->defbe.name);
1002 curproxy->defbe.name = strdup(args[1]);
1003 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01001005 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1006 return 0;
1007
Willy Tarreaubaaee002006-06-26 02:48:02 +02001008 /* enable reconnections to dispatch */
1009 curproxy->options |= PR_O_REDISP;
1010 }
1011#ifdef TPROXY
1012 else if (!strcmp(args[0], "transparent")) {
1013 /* enable transparent proxy connections */
1014 curproxy->options |= PR_O_TRANSP;
1015 }
1016#endif
1017 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001018 if (warnifnotcap(curproxy, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
1019 return 0;
1020
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 if (*(args[1]) == 0) {
1022 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1023 return -1;
1024 }
1025 curproxy->maxconn = atol(args[1]);
1026 }
Willy Tarreau86034312006-12-29 00:10:33 +01001027 else if (!strcmp(args[0], "fullconn")) { /* fullconn */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001028 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], " Maybe you want 'maxconn' instead ?"))
1029 return 0;
1030
Willy Tarreau86034312006-12-29 00:10:33 +01001031 if (*(args[1]) == 0) {
1032 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
1033 return -1;
1034 }
1035 curproxy->fullconn = atol(args[1]);
1036 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1038 if (*(args[1]) == 0) {
1039 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1040 return -1;
1041 }
1042 curproxy->grace = atol(args[1]);
1043 }
1044 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1045 if (curproxy == &defproxy) {
1046 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1047 return -1;
1048 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001049 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1050 return 0;
1051
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052 if (strchr(args[1], ':') == NULL) {
1053 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1054 return -1;
1055 }
1056 curproxy->dispatch_addr = *str2sa(args[1]);
1057 }
1058 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001059 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1060 return 0;
1061
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 if (*(args[1])) {
1063 if (!strcmp(args[1], "roundrobin")) {
1064 curproxy->options |= PR_O_BALANCE_RR;
1065 }
1066 else if (!strcmp(args[1], "source")) {
1067 curproxy->options |= PR_O_BALANCE_SH;
1068 }
1069 else {
1070 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1071 return -1;
1072 }
1073 }
1074 else /* if no option is set, use round-robin by default */
1075 curproxy->options |= PR_O_BALANCE_RR;
1076 }
1077 else if (!strcmp(args[0], "server")) { /* server address */
1078 int cur_arg;
1079 char *rport;
1080 char *raddr;
1081 short realport;
1082 int do_check;
1083
1084 if (curproxy == &defproxy) {
1085 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1086 return -1;
1087 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001088 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1089 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090
1091 if (!*args[2]) {
1092 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1093 file, linenum, args[0]);
1094 return -1;
1095 }
1096 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1097 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1098 return -1;
1099 }
1100
1101 /* the servers are linked backwards first */
1102 newsrv->next = curproxy->srv;
1103 curproxy->srv = newsrv;
1104 newsrv->proxy = curproxy;
1105
1106 LIST_INIT(&newsrv->pendconns);
1107 do_check = 0;
1108 newsrv->state = SRV_RUNNING; /* early server setup */
1109 newsrv->id = strdup(args[1]);
1110
1111 /* several ways to check the port component :
1112 * - IP => port=+0, relative
1113 * - IP: => port=+0, relative
1114 * - IP:N => port=N, absolute
1115 * - IP:+N => port=+N, relative
1116 * - IP:-N => port=-N, relative
1117 */
1118 raddr = strdup(args[2]);
1119 rport = strchr(raddr, ':');
1120 if (rport) {
1121 *rport++ = 0;
1122 realport = atol(rport);
1123 if (!isdigit((int)*rport))
1124 newsrv->state |= SRV_MAPPORTS;
1125 } else {
1126 realport = 0;
1127 newsrv->state |= SRV_MAPPORTS;
1128 }
1129
1130 newsrv->addr = *str2sa(raddr);
1131 newsrv->addr.sin_port = htons(realport);
1132 free(raddr);
1133
1134 newsrv->curfd = -1; /* no health-check in progress */
1135 newsrv->inter = DEF_CHKINTR;
1136 newsrv->rise = DEF_RISETIME;
1137 newsrv->fall = DEF_FALLTIME;
1138 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
1139 cur_arg = 3;
1140 while (*args[cur_arg]) {
1141 if (!strcmp(args[cur_arg], "cookie")) {
1142 newsrv->cookie = strdup(args[cur_arg + 1]);
1143 newsrv->cklen = strlen(args[cur_arg + 1]);
1144 cur_arg += 2;
1145 }
1146 else if (!strcmp(args[cur_arg], "rise")) {
1147 newsrv->rise = atol(args[cur_arg + 1]);
1148 newsrv->health = newsrv->rise;
1149 cur_arg += 2;
1150 }
1151 else if (!strcmp(args[cur_arg], "fall")) {
1152 newsrv->fall = atol(args[cur_arg + 1]);
1153 cur_arg += 2;
1154 }
1155 else if (!strcmp(args[cur_arg], "inter")) {
1156 newsrv->inter = atol(args[cur_arg + 1]);
1157 cur_arg += 2;
1158 }
1159 else if (!strcmp(args[cur_arg], "port")) {
1160 newsrv->check_port = atol(args[cur_arg + 1]);
1161 cur_arg += 2;
1162 }
1163 else if (!strcmp(args[cur_arg], "backup")) {
1164 newsrv->state |= SRV_BACKUP;
1165 cur_arg ++;
1166 }
1167 else if (!strcmp(args[cur_arg], "weight")) {
1168 int w;
1169 w = atol(args[cur_arg + 1]);
1170 if (w < 1 || w > 256) {
1171 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1172 file, linenum, newsrv->id, w);
1173 return -1;
1174 }
1175 newsrv->uweight = w - 1;
1176 cur_arg += 2;
1177 }
1178 else if (!strcmp(args[cur_arg], "minconn")) {
1179 newsrv->minconn = atol(args[cur_arg + 1]);
1180 cur_arg += 2;
1181 }
1182 else if (!strcmp(args[cur_arg], "maxconn")) {
1183 newsrv->maxconn = atol(args[cur_arg + 1]);
1184 cur_arg += 2;
1185 }
1186 else if (!strcmp(args[cur_arg], "check")) {
1187 global.maxsock++;
1188 do_check = 1;
1189 cur_arg += 1;
1190 }
1191 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1192 if (!*args[cur_arg + 1]) {
1193 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1194 file, linenum, "source");
1195 return -1;
1196 }
1197 newsrv->state |= SRV_BIND_SRC;
1198 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1199 cur_arg += 2;
Willy Tarreau77074d52006-11-12 23:57:19 +01001200#ifdef CONFIG_HAP_CTTPROXY
1201 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1202 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
1203 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1204 file, linenum, "usesrc");
1205 return -1;
1206 }
1207 if (!*args[cur_arg + 1]) {
1208 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1209 file, linenum, "usesrc");
1210 return -1;
1211 }
1212 if (!strcmp(args[cur_arg + 1], "client")) {
1213 newsrv->state |= SRV_TPROXY_CLI;
1214 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1215 newsrv->state |= SRV_TPROXY_CIP;
1216 } else {
1217 newsrv->state |= SRV_TPROXY_ADDR;
1218 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1219 }
1220 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1221 cur_arg += 2;
1222 }
1223#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001224 }
1225 else {
1226 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
1227 file, linenum, newsrv->id);
1228 return -1;
1229 }
1230 }
1231
1232 if (do_check) {
1233 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1234 newsrv->check_port = realport; /* by default */
1235 if (!newsrv->check_port) {
1236 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1237 file, linenum, newsrv->id);
1238 return -1;
1239 }
1240 newsrv->state |= SRV_CHECKED;
1241 }
1242
1243 if (newsrv->state & SRV_BACKUP)
1244 curproxy->srv_bck++;
1245 else
1246 curproxy->srv_act++;
1247 }
1248 else if (!strcmp(args[0], "log")) { /* syslog server address */
1249 struct sockaddr_in *sa;
1250 int facility;
1251
1252 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1253 curproxy->logfac1 = global.logfac1;
1254 curproxy->logsrv1 = global.logsrv1;
1255 curproxy->loglev1 = global.loglev1;
1256 curproxy->logfac2 = global.logfac2;
1257 curproxy->logsrv2 = global.logsrv2;
1258 curproxy->loglev2 = global.loglev2;
1259 }
1260 else if (*(args[1]) && *(args[2])) {
1261 int level;
1262
1263 facility = get_log_facility(args[2]);
1264 if (facility < 0) {
1265 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1266 exit(1);
1267 }
1268
1269 level = 7; /* max syslog level = debug */
1270 if (*(args[3])) {
1271 level = get_log_level(args[3]);
1272 if (level < 0) {
1273 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1274 exit(1);
1275 }
1276 }
1277
1278 sa = str2sa(args[1]);
1279 if (!sa->sin_port)
1280 sa->sin_port = htons(SYSLOG_PORT);
1281
1282 if (curproxy->logfac1 == -1) {
1283 curproxy->logsrv1 = *sa;
1284 curproxy->logfac1 = facility;
1285 curproxy->loglev1 = level;
1286 }
1287 else if (curproxy->logfac2 == -1) {
1288 curproxy->logsrv2 = *sa;
1289 curproxy->logfac2 = facility;
1290 curproxy->loglev2 = level;
1291 }
1292 else {
1293 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1294 return -1;
1295 }
1296 }
1297 else {
1298 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1299 file, linenum);
1300 return -1;
1301 }
1302 }
1303 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
Willy Tarreau977b8e42006-12-29 14:19:17 +01001304 if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1305 return 0;
1306
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307 if (!*args[1]) {
1308 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1309 file, linenum, "source");
1310 return -1;
1311 }
1312
1313 curproxy->source_addr = *str2sa(args[1]);
1314 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001315#ifdef CONFIG_HAP_CTTPROXY
1316 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
1317 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1318 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1319 file, linenum, "usesrc");
1320 return -1;
1321 }
1322 if (!*args[3]) {
1323 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1324 file, linenum, "usesrc");
1325 return -1;
1326 }
1327
1328 if (!strcmp(args[3], "client")) {
1329 curproxy->options |= PR_O_TPXY_CLI;
1330 } else if (!strcmp(args[3], "clientip")) {
1331 curproxy->options |= PR_O_TPXY_CIP;
1332 } else {
1333 curproxy->options |= PR_O_TPXY_ADDR;
1334 curproxy->tproxy_addr = *str2sa(args[3]);
1335 }
1336 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1337 }
1338#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339 }
1340 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1341 regex_t *preg;
1342 if (curproxy == &defproxy) {
1343 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1344 return -1;
1345 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001346 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1347 return 0;
1348
Willy Tarreaubaaee002006-06-26 02:48:02 +02001349 if (*(args[1]) == 0 || *(args[2]) == 0) {
1350 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1351 file, linenum, args[0]);
1352 return -1;
1353 }
1354
1355 preg = calloc(1, sizeof(regex_t));
1356 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1357 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1358 return -1;
1359 }
1360
1361 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1362 if (err) {
1363 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1364 file, linenum, *err);
1365 return -1;
1366 }
1367 }
1368 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1369 regex_t *preg;
1370 if (curproxy == &defproxy) {
1371 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1372 return -1;
1373 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001374 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1375 return 0;
1376
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377 if (*(args[1]) == 0) {
1378 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1379 return -1;
1380 }
1381
1382 preg = calloc(1, sizeof(regex_t));
1383 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1384 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1385 return -1;
1386 }
1387
1388 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1389 }
1390 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1391 regex_t *preg;
1392 if (curproxy == &defproxy) {
1393 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1394 return -1;
1395 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001396 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1397 return 0;
1398
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 if (*(args[1]) == 0) {
1400 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1401 return -1;
1402 }
1403
1404 preg = calloc(1, sizeof(regex_t));
1405 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1406 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1407 return -1;
1408 }
1409
1410 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1411 }
1412 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1413 regex_t *preg;
1414 if (curproxy == &defproxy) {
1415 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1416 return -1;
1417 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001418 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1419 return 0;
1420
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421 if (*(args[1]) == 0) {
1422 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1423 return -1;
1424 }
1425
1426 preg = calloc(1, sizeof(regex_t));
1427 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1428 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1429 return -1;
1430 }
1431
1432 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1433 }
1434 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1435 regex_t *preg;
1436 if (curproxy == &defproxy) {
1437 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1438 return -1;
1439 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001440 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1441 return 0;
1442
Willy Tarreaubaaee002006-06-26 02:48:02 +02001443 if (*(args[1]) == 0) {
1444 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1445 return -1;
1446 }
1447
1448 preg = calloc(1, sizeof(regex_t));
1449 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1450 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1451 return -1;
1452 }
1453
1454 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1455 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001456 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this 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 Tarreaub8750a82006-09-03 09:56:00 +02001465 if (*(args[1]) == 0) {
1466 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1467 return -1;
1468 }
1469
1470 preg = calloc(1, sizeof(regex_t));
1471 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1472 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1473 return -1;
1474 }
1475
1476 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1477 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001478 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1479 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001480 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001481 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1482 return -1;
1483 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001484 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1485 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001486
Willy Tarreau977b8e42006-12-29 14:19:17 +01001487 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001488 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1489 file, linenum, args[0]);
1490 return -1;
1491 }
1492
1493 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001494 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001495 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1496 }
1497
1498 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1499 }
1500 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1501 regex_t *preg;
Willy Tarreau977b8e42006-12-29 14:19:17 +01001502 if (curproxy == &defproxy) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001503 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1504 return -1;
1505 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001506 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1507 return 0;
Willy Tarreaua496b602006-12-17 23:15:24 +01001508
Willy Tarreau977b8e42006-12-29 14:19:17 +01001509 if (*(args[1]) == 0 || *(args[2]) == 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001510 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1511 file, linenum, args[0]);
1512 return -1;
1513 }
1514
1515 preg = calloc(1, sizeof(regex_t));
Willy Tarreau977b8e42006-12-29 14:19:17 +01001516 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
Willy Tarreaua496b602006-12-17 23:15:24 +01001517 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1518 }
1519
1520 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1521 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1523 regex_t *preg;
1524 if (curproxy == &defproxy) {
1525 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1526 return -1;
1527 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001528 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1529 return 0;
1530
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531 if (*(args[1]) == 0 || *(args[2]) == 0) {
1532 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1533 file, linenum, args[0]);
1534 return -1;
1535 }
1536
1537 preg = calloc(1, sizeof(regex_t));
1538 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1539 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1540 return -1;
1541 }
1542
1543 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1544 if (err) {
1545 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1546 file, linenum, *err);
1547 return -1;
1548 }
1549 }
1550 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
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 | REG_ICASE) != 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_REMOVE, NULL);
1571 }
1572 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
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 Tarreaubaaee002006-06-26 02:48:02 +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 | REG_ICASE) != 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_DENY, NULL);
1593 }
1594 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1595 regex_t *preg;
1596 if (curproxy == &defproxy) {
1597 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;
1602
Willy Tarreaubaaee002006-06-26 02:48:02 +02001603 if (*(args[1]) == 0) {
1604 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1605 return -1;
1606 }
1607
1608 preg = calloc(1, sizeof(regex_t));
1609 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1610 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1611 return -1;
1612 }
1613
1614 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1615 }
1616 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1617 regex_t *preg;
1618 if (curproxy == &defproxy) {
1619 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;
1624
Willy Tarreaubaaee002006-06-26 02:48:02 +02001625 if (*(args[1]) == 0) {
1626 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1627 return -1;
1628 }
1629
1630 preg = calloc(1, sizeof(regex_t));
1631 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1632 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1633 return -1;
1634 }
1635
1636 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1637 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001638 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this 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 Tarreaub8750a82006-09-03 09:56:00 +02001647 if (*(args[1]) == 0) {
1648 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1649 return -1;
1650 }
1651
1652 preg = calloc(1, sizeof(regex_t));
1653 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1654 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1655 return -1;
1656 }
1657
1658 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1659 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001660 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1661 if (curproxy == &defproxy) {
1662 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1663 return -1;
1664 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001665 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1666 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001667
1668 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1669 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1670 return 0;
1671 }
1672
1673 if (*(args[1]) == 0) {
1674 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1675 return -1;
1676 }
1677
1678 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1679 }
1680 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1681 regex_t *preg;
1682
1683 if (*(args[1]) == 0 || *(args[2]) == 0) {
1684 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1685 file, linenum, args[0]);
1686 return -1;
1687 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001688 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1689 return 0;
1690
Willy Tarreaubaaee002006-06-26 02:48:02 +02001691 preg = calloc(1, sizeof(regex_t));
1692 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1693 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1694 return -1;
1695 }
1696
1697 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1698 if (err) {
1699 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1700 file, linenum, *err);
1701 return -1;
1702 }
1703 }
1704 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1705 regex_t *preg;
1706 if (curproxy == &defproxy) {
1707 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1708 return -1;
1709 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001710 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1711 return 0;
1712
Willy Tarreaubaaee002006-06-26 02:48:02 +02001713 if (*(args[1]) == 0) {
1714 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1715 return -1;
1716 }
1717
1718 preg = calloc(1, sizeof(regex_t));
1719 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1720 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1721 return -1;
1722 }
1723
1724 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1725 if (err) {
1726 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1727 file, linenum, *err);
1728 return -1;
1729 }
1730 }
1731 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1732 regex_t *preg;
1733 if (curproxy == &defproxy) {
1734 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1735 return -1;
1736 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001737 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1738 return 0;
1739
Willy Tarreaubaaee002006-06-26 02:48:02 +02001740 if (*(args[1]) == 0) {
1741 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1742 return -1;
1743 }
1744
1745 preg = calloc(1, sizeof(regex_t));
1746 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1747 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1748 return -1;
1749 }
1750
1751 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1752 if (err) {
1753 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1754 file, linenum, *err);
1755 return -1;
1756 }
1757 }
1758 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1759 regex_t *preg;
1760 if (curproxy == &defproxy) {
1761 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1762 return -1;
1763 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001764 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1765 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001766
1767 if (*(args[1]) == 0 || *(args[2]) == 0) {
1768 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1769 file, linenum, args[0]);
1770 return -1;
1771 }
1772
1773 preg = calloc(1, sizeof(regex_t));
1774 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1775 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1776 return -1;
1777 }
1778
1779 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1780 if (err) {
1781 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1782 file, linenum, *err);
1783 return -1;
1784 }
1785 }
1786 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1787 regex_t *preg;
1788 if (curproxy == &defproxy) {
1789 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1790 return -1;
1791 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001792 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1793 return 0;
1794
Willy Tarreaubaaee002006-06-26 02:48:02 +02001795 if (*(args[1]) == 0) {
1796 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1797 return -1;
1798 }
1799
1800 preg = calloc(1, sizeof(regex_t));
1801 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1802 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1803 return -1;
1804 }
1805
1806 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1807 if (err) {
1808 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1809 file, linenum, *err);
1810 return -1;
1811 }
1812 }
1813 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1814 regex_t *preg;
1815 if (curproxy == &defproxy) {
1816 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1817 return -1;
1818 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001819 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1820 return 0;
1821
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822 if (*(args[1]) == 0) {
1823 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1824 return -1;
1825 }
1826
1827 preg = calloc(1, sizeof(regex_t));
1828 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1829 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1830 return -1;
1831 }
1832
1833 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1834 if (err) {
1835 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1836 file, linenum, *err);
1837 return -1;
1838 }
1839 }
1840 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1841 if (curproxy == &defproxy) {
1842 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1843 return -1;
1844 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01001845 else if (warnifnotcap(curproxy, PR_CAP_RS, file, linenum, args[0], NULL))
1846 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001847
1848 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1849 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1850 return 0;
1851 }
1852
1853 if (*(args[1]) == 0) {
1854 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1855 return -1;
1856 }
1857
1858 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1859 }
1860 else if (!strcmp(args[0], "errorloc") ||
1861 !strcmp(args[0], "errorloc302") ||
1862 !strcmp(args[0], "errorloc303")) { /* error location */
1863 int errnum, errlen;
1864 char *err;
1865
1866 // if (curproxy == &defproxy) {
1867 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1868 // return -1;
1869 // }
1870
Willy Tarreau977b8e42006-12-29 14:19:17 +01001871 if (warnifnotcap(curproxy, PR_CAP_FE | PR_CAP_BE, file, linenum, args[0], NULL))
1872 return 0;
1873
Willy Tarreaubaaee002006-06-26 02:48:02 +02001874 if (*(args[2]) == 0) {
Willy Tarreau0f772532006-12-23 20:51:41 +01001875 Alert("parsing [%s:%d] : <%s> expects <status_code> and <url> as arguments.\n", file, linenum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001876 return -1;
1877 }
1878
1879 errnum = atol(args[1]);
1880 if (!strcmp(args[0], "errorloc303")) {
1881 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1882 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1883 } else {
1884 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1885 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
1886 }
1887
Willy Tarreau0f772532006-12-23 20:51:41 +01001888 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1889 if (http_err_codes[rc] == errnum) {
1890 if (curproxy->errmsg[rc].str)
1891 free(curproxy->errmsg[rc].str);
1892 curproxy->errmsg[rc].str = err;
1893 curproxy->errmsg[rc].len = errlen;
1894 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001896 }
Willy Tarreau0f772532006-12-23 20:51:41 +01001897
1898 if (rc >= HTTP_ERR_SIZE) {
1899 Warning("parsing [%s:%d] : status code %d not handled, error relocation will be ignored.\n",
1900 file, linenum, errnum);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001901 free(err);
1902 }
1903 }
1904 else {
1905 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
1906 return -1;
1907 }
1908 return 0;
1909}
1910
1911
1912/*
1913 * This function reads and parses the configuration file given in the argument.
1914 * returns 0 if OK, -1 if error.
1915 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001916int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001917{
1918 char thisline[256];
1919 char *line;
1920 FILE *f;
1921 int linenum = 0;
1922 char *end;
1923 char *args[MAX_LINE_ARGS];
1924 int arg;
1925 int cfgerr = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01001926 int nbchk, mininter;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001927 int confsect = CFG_NONE;
1928
1929 struct proxy *curproxy = NULL;
1930 struct server *newsrv = NULL;
1931
1932 if ((f=fopen(file,"r")) == NULL)
1933 return -1;
1934
1935 init_default_instance();
1936
1937 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
1938 linenum++;
1939
1940 end = line + strlen(line);
1941
1942 /* skip leading spaces */
1943 while (isspace((int)*line))
1944 line++;
1945
1946 arg = 0;
1947 args[arg] = line;
1948
1949 while (*line && arg < MAX_LINE_ARGS) {
1950 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1951 * C equivalent value. Other combinations left unchanged (eg: \1).
1952 */
1953 if (*line == '\\') {
1954 int skip = 0;
1955 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1956 *line = line[1];
1957 skip = 1;
1958 }
1959 else if (line[1] == 'r') {
1960 *line = '\r';
1961 skip = 1;
1962 }
1963 else if (line[1] == 'n') {
1964 *line = '\n';
1965 skip = 1;
1966 }
1967 else if (line[1] == 't') {
1968 *line = '\t';
1969 skip = 1;
1970 }
1971 else if (line[1] == 'x') {
1972 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
1973 unsigned char hex1, hex2;
1974 hex1 = toupper(line[2]) - '0';
1975 hex2 = toupper(line[3]) - '0';
1976 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1977 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1978 *line = (hex1<<4) + hex2;
1979 skip = 3;
1980 }
1981 else {
1982 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
1983 return -1;
1984 }
1985 }
1986 if (skip) {
1987 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
1988 end -= skip;
1989 }
1990 line++;
1991 }
1992 else if (*line == '#' || *line == '\n' || *line == '\r') {
1993 /* end of string, end of loop */
1994 *line = 0;
1995 break;
1996 }
1997 else if (isspace((int)*line)) {
1998 /* a non-escaped space is an argument separator */
1999 *line++ = 0;
2000 while (isspace((int)*line))
2001 line++;
2002 args[++arg] = line;
2003 }
2004 else {
2005 line++;
2006 }
2007 }
2008
2009 /* empty line */
2010 if (!**args)
2011 continue;
2012
2013 /* zero out remaining args */
2014 while (++arg < MAX_LINE_ARGS) {
2015 args[arg] = line;
2016 }
2017
Willy Tarreau977b8e42006-12-29 14:19:17 +01002018 if (!strcmp(args[0], "listen") ||
2019 !strcmp(args[0], "frontend") ||
2020 !strcmp(args[0], "backend") ||
2021 !strcmp(args[0], "ruleset") ||
2022 !strcmp(args[0], "defaults")) /* new proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002023 confsect = CFG_LISTEN;
2024 else if (!strcmp(args[0], "global")) /* global config */
2025 confsect = CFG_GLOBAL;
2026 /* else it's a section keyword */
2027
2028 switch (confsect) {
2029 case CFG_LISTEN:
2030 if (cfg_parse_listen(file, linenum, args) < 0)
2031 return -1;
2032 break;
2033 case CFG_GLOBAL:
2034 if (cfg_parse_global(file, linenum, args) < 0)
2035 return -1;
2036 break;
2037 default:
2038 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2039 return -1;
2040 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002041 }
2042 fclose(f);
2043
2044 /*
2045 * Now, check for the integrity of all that we have collected.
2046 */
2047
2048 /* will be needed further to delay some tasks */
2049 tv_now(&now);
2050
2051 if ((curproxy = proxy) == NULL) {
2052 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2053 file);
2054 return -1;
2055 }
2056
2057 while (curproxy != NULL) {
Willy Tarreau97a738f2006-12-17 18:02:30 +01002058 curproxy->fiprm = curproxy->beprm = curproxy;
2059
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060 if (curproxy->state == PR_STSTOPPED) {
2061 curproxy = curproxy->next;
2062 continue;
2063 }
2064
Willy Tarreau977b8e42006-12-29 14:19:17 +01002065 if (curproxy->cap & PR_CAP_FE && curproxy->listen == NULL) {
2066 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 +01002067 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068 cfgerr++;
2069 }
Willy Tarreau977b8e42006-12-29 14:19:17 +01002070 else if (curproxy->cap & PR_CAP_BE &&
2071 ((curproxy->mode != PR_MODE_HEALTH) &&
2072 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2073 (*(int *)&curproxy->dispatch_addr.sin_addr == 0))) {
2074 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 +01002075 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002076 cfgerr++;
2077 }
2078 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2079 if (curproxy->options & PR_O_TRANSP) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002080 Alert("parsing %s : %s '%s' cannot use both transparent and balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002081 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 cfgerr++;
2083 }
2084#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2085 else if (curproxy->srv == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002086 Alert("parsing %s : %s '%s' needs at least 1 server in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002087 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002088 cfgerr++;
2089 }
2090#endif
2091 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002092 Warning("parsing %s : dispatch address of %s '%s' will be ignored in balance mode.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002093 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 }
2095 }
2096 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2097 if (curproxy->cookie_name != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002098 Warning("parsing %s : cookie will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002099 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002100 }
2101 if ((newsrv = curproxy->srv) != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002102 Warning("parsing %s : servers will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002103 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104 }
2105 if (curproxy->rsp_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002106 Warning("parsing %s : server regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002107 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002108 }
2109 if (curproxy->req_exp != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002110 Warning("parsing %s : client regular expressions will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002111 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002112 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002113 if (curproxy->monitor_uri != NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002114 Warning("parsing %s : monitor-uri will be ignored for %s '%s'.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002115 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1c47f852006-07-09 08:22:27 +02002116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002117 }
2118 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2119 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2120 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2121 file, curproxy->id);
2122 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002123 }
2124 }
2125
2126 /* if a default backend was specified, let's find it */
2127 if (curproxy->defbe.name) {
2128 struct proxy *target;
2129
2130 for (target = proxy; target != NULL; target = target->next) {
2131 if (strcmp(target->id, curproxy->defbe.name) == 0)
2132 break;
2133 }
2134 if (target == NULL) {
2135 Alert("parsing %s : default backend '%s' in HTTP %s '%s' was not found !\n",
2136 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2137 cfgerr++;
2138 } else if (target == curproxy) {
2139 Alert("parsing %s : loop detected for default backend %s !\n", file, curproxy->defbe.name);
2140 cfgerr++;
2141 } else if (!(target->cap & PR_CAP_BE)) {
2142 Alert("parsing %s : default backend '%s' in HTTP %s '%s' has no backend capability !\n",
2143 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2144 cfgerr++;
2145 } else if (target->mode != curproxy->mode) {
2146 Alert("parsing %s : default backend '%s' in HTTP %s '%s' is not of same mode (tcp/http) !\n",
2147 file, curproxy->defbe.name, proxy_type_str(curproxy), curproxy->id);
2148 cfgerr++;
2149 } else {
2150 free(curproxy->defbe.name);
2151 curproxy->defbe.be = target;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 }
2153 }
2154
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002155 /* find the target proxy in setbe */
Willy Tarreaua496b602006-12-17 23:15:24 +01002156 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2157 /* map jump target for ACT_SETBE in req_rep chain */
2158 struct hdr_exp *exp;
2159 struct proxy *target;
2160 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2161 if (exp->action != ACT_SETBE)
2162 continue;
2163 for (target = proxy; target != NULL; target = target->next) {
2164 if (strcmp(target->id, exp->replace) == 0)
2165 break;
2166 }
2167 if (target == NULL) {
Willy Tarreau977b8e42006-12-29 14:19:17 +01002168 Alert("parsing %s : backend '%s' in HTTP %s '%s' was not found !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002169 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaua496b602006-12-17 23:15:24 +01002170 cfgerr++;
2171 } else if (target == curproxy) {
2172 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2173 cfgerr++;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002174 } else if (!(target->cap & PR_CAP_BE)) {
2175 Alert("parsing %s : target '%s' in HTTP %s '%s' has no backend capability !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002176 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002177 cfgerr++;
2178 } else if (target->mode != PR_MODE_HTTP) {
2179 Alert("parsing %s : backend '%s' in HTTP %s '%s' is not HTTP (use 'mode http') !\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002180 file, exp->replace, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau977b8e42006-12-29 14:19:17 +01002181 cfgerr++;
Willy Tarreaua496b602006-12-17 23:15:24 +01002182 } else {
2183 free((void *)exp->replace);
2184 exp->replace = (const char *)target;
2185 }
2186 }
2187 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002188 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau977b8e42006-12-29 14:19:17 +01002189 (((curproxy->cap & PR_CAP_FE) && !curproxy->clitimeout) ||
2190 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) && (!curproxy->contimeout || !curproxy->srvtimeout)))) {
2191 Warning("parsing %s : missing timeouts for %s '%s'.\n"
Willy Tarreau2738a142006-07-08 17:28:09 +02002192 " | While not properly invalid, you will certainly encounter various problems\n"
2193 " | with such a configuration. To fix this, please ensure that all following\n"
2194 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002195 file, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau2738a142006-07-08 17:28:09 +02002196 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002197
2198 if (curproxy->options & PR_O_SSL3_CHK) {
2199 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2200 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2201 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2202 }
2203
Willy Tarreau86034312006-12-29 00:10:33 +01002204 /* for backwards compatibility with "listen" instances, if
2205 * fullconn is not set but maxconn is set, then maxconn
2206 * is used.
2207 */
2208 if (!curproxy->fullconn)
2209 curproxy->fullconn = curproxy->maxconn;
2210
Willy Tarreaubaaee002006-06-26 02:48:02 +02002211 /* first, we will invert the servers list order */
2212 newsrv = NULL;
2213 while (curproxy->srv) {
2214 struct server *next;
2215
2216 next = curproxy->srv->next;
2217 curproxy->srv->next = newsrv;
2218 newsrv = curproxy->srv;
2219 if (!next)
2220 break;
2221 curproxy->srv = next;
2222 }
2223
2224 /* now, newsrv == curproxy->srv */
2225 if (newsrv) {
2226 struct server *srv;
2227 int pgcd;
2228 int act, bck;
2229
2230 /* We will factor the weights to reduce the table,
2231 * using Euclide's largest common divisor algorithm
2232 */
2233 pgcd = newsrv->uweight + 1;
2234 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2235 int t, w;
2236
2237 w = srv->uweight + 1;
2238 while (w) {
2239 t = pgcd % w;
2240 pgcd = w;
2241 w = t;
2242 }
2243 }
2244
2245 act = bck = 0;
2246 for (srv = newsrv; srv; srv = srv->next) {
2247 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
2248 if (srv->state & SRV_BACKUP)
2249 bck += srv->eweight + 1;
2250 else
2251 act += srv->eweight + 1;
2252 }
2253
2254 /* this is the largest map we will ever need for this servers list */
2255 if (act < bck)
2256 act = bck;
2257
2258 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2259 /* recounts servers and their weights */
2260 recount_servers(curproxy);
2261 recalc_server_map(curproxy);
2262 }
2263
2264 if (curproxy->options & PR_O_LOGASAP)
2265 curproxy->to_log &= ~LW_BYTES;
2266
Willy Tarreaubaaee002006-06-26 02:48:02 +02002267 /*
2268 * If this server supports a maxconn parameter, it needs a dedicated
2269 * tasks to fill the emptied slots when a connection leaves.
2270 */
2271 newsrv = curproxy->srv;
2272 while (newsrv != NULL) {
Willy Tarreau86034312006-12-29 00:10:33 +01002273 if (newsrv->minconn > newsrv->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 /* Only 'minconn' was specified, or it was higher than or equal
2275 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2276 * this will avoid further useless expensive computations.
2277 */
2278 newsrv->maxconn = newsrv->minconn;
Willy Tarreau86034312006-12-29 00:10:33 +01002279 } else if (newsrv->maxconn && !newsrv->minconn) {
2280 /* minconn was not specified, so we set it to maxconn */
2281 newsrv->minconn = newsrv->maxconn;
Willy Tarreau977b8e42006-12-29 14:19:17 +01002282 } else if (newsrv->minconn != newsrv->maxconn && !curproxy->fullconn) {
2283 Alert("parsing %s, %s '%s' : fullconn is mandatory when minconn is set on a server.\n",
Willy Tarreau2b5652f2006-12-31 17:46:05 +01002284 file, proxy_type_str(curproxy), curproxy->id, linenum);
Willy Tarreau86034312006-12-29 00:10:33 +01002285 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002286 }
2287
2288 if (newsrv->maxconn > 0) {
2289 struct task *t;
2290
2291 if ((t = pool_alloc(task)) == NULL) {
2292 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2293 return -1;
2294 }
2295
Willy Tarreau964c9362007-01-07 00:38:00 +01002296 t->rqnext = NULL;
2297 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002298 t->state = TASK_IDLE;
2299 t->process = process_srv_queue;
2300 t->context = newsrv;
2301 newsrv->queue_mgt = t;
2302
2303 /* never run it unless specifically woken up */
2304 tv_eternity(&t->expire);
2305 task_queue(t);
2306 }
2307 newsrv = newsrv->next;
2308 }
2309
2310 /* now we'll start this proxy's health checks if any */
2311 /* 1- count the checkers to run simultaneously */
2312 nbchk = 0;
2313 mininter = 0;
2314 newsrv = curproxy->srv;
2315 while (newsrv != NULL) {
2316 if (newsrv->state & SRV_CHECKED) {
2317 if (!mininter || mininter > newsrv->inter)
2318 mininter = newsrv->inter;
2319 nbchk++;
2320 }
2321 newsrv = newsrv->next;
2322 }
2323
2324 /* 2- start them as far as possible from each others while respecting
2325 * their own intervals. For this, we will start them after their own
2326 * interval added to the min interval divided by the number of servers,
2327 * weighted by the server's position in the list.
2328 */
2329 if (nbchk > 0) {
2330 struct task *t;
2331 int srvpos;
2332
2333 newsrv = curproxy->srv;
2334 srvpos = 0;
2335 while (newsrv != NULL) {
2336 /* should this server be checked ? */
2337 if (newsrv->state & SRV_CHECKED) {
2338 if ((t = pool_alloc(task)) == NULL) {
2339 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2340 return -1;
2341 }
2342
Willy Tarreau964c9362007-01-07 00:38:00 +01002343 t->wq = NULL;
2344 t->rqnext = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002345 t->state = TASK_IDLE;
2346 t->process = process_chk;
2347 t->context = newsrv;
2348
2349 /* check this every ms */
2350 tv_delayfrom(&t->expire, &now,
2351 newsrv->inter + mininter * srvpos / nbchk);
2352 task_queue(t);
2353 //task_wakeup(&rq, t);
2354 srvpos++;
2355 }
2356 newsrv = newsrv->next;
2357 }
2358 }
2359
2360 curproxy = curproxy->next;
2361 }
2362 if (cfgerr > 0) {
2363 Alert("Errors found in configuration file, aborting.\n");
2364 return -1;
2365 }
2366 else
2367 return 0;
2368}
2369
2370
2371
2372/*
2373 * Local variables:
2374 * c-indent-level: 8
2375 * c-basic-offset: 8
2376 * End:
2377 */