blob: 882c0e4671840fe694b3fc271bf0014a13578d12 [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>
28#include <types/polling.h>
29#include <types/proxy.h>
30#include <types/queue.h>
31
32#include <proto/backend.h>
33#include <proto/checks.h>
34#include <proto/log.h>
35#include <proto/server.h>
36#include <proto/task.h>
37
38
39const char *HTTP_302 =
40 "HTTP/1.0 302 Found\r\n"
41 "Cache-Control: no-cache\r\n"
42 "Connection: close\r\n"
43 "Location: "; /* not terminated since it will be concatenated with the URL */
44
45/* same as 302 except that the browser MUST retry with the GET method */
46const char *HTTP_303 =
47 "HTTP/1.0 303 See Other\r\n"
48 "Cache-Control: no-cache\r\n"
49 "Connection: close\r\n"
50 "Location: "; /* not terminated since it will be concatenated with the URL */
51
52const char *HTTP_400 =
53 "HTTP/1.0 400 Bad request\r\n"
54 "Cache-Control: no-cache\r\n"
55 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020056 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 "\r\n"
58 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n";
59
60const char *HTTP_403 =
61 "HTTP/1.0 403 Forbidden\r\n"
62 "Cache-Control: no-cache\r\n"
63 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020064 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 "\r\n"
66 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n";
67
68const char *HTTP_408 =
69 "HTTP/1.0 408 Request Time-out\r\n"
70 "Cache-Control: no-cache\r\n"
71 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020072 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 "\r\n"
74 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n";
75
76const char *HTTP_500 =
77 "HTTP/1.0 500 Server Error\r\n"
78 "Cache-Control: no-cache\r\n"
79 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020080 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 "\r\n"
82 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
83
84const char *HTTP_502 =
85 "HTTP/1.0 502 Bad Gateway\r\n"
86 "Cache-Control: no-cache\r\n"
87 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020088 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020089 "\r\n"
90 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n";
91
92const char *HTTP_503 =
93 "HTTP/1.0 503 Service Unavailable\r\n"
94 "Cache-Control: no-cache\r\n"
95 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020096 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 "\r\n"
98 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n";
99
100const char *HTTP_504 =
101 "HTTP/1.0 504 Gateway Time-out\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200104 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 "\r\n"
106 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n";
107
Willy Tarreauf3c69202006-07-09 16:42:34 +0200108/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
109 * ssl-hello-chk option to ensure that the remote server speaks SSL.
110 *
111 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
112 */
113const char sslv3_client_hello_pkt[] = {
114 "\x16" /* ContentType : 0x16 = Hanshake */
115 "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
116 "\x00\x79" /* ContentLength : 0x79 bytes after this one */
117 "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
118 "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
119 "\x03\x00" /* Hello Version : 0x0300 = v3 */
120 "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
121 "HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random : must be exactly 28 bytes */
122 "\x00" /* Session ID length : empty (no session ID) */
123 "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
124 "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
125 "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
126 "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
127 "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
128 "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
129 "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
130 "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
131 "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
132 "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
133 "\x00\x38" "\x00\x39" "\x00\x3A"
134 "\x01" /* Compression Length : 0x01 = 1 byte for types */
135 "\x00" /* Compression Type : 0x00 = NULL compression */
136};
137
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138
139static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
140int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */
141int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
142
143/*
144 * converts <str> to a list of listeners which are dynamically allocated.
145 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
146 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
147 * - <port> is a numerical port from 1 to 65535 ;
148 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
149 * This can be repeated as many times as necessary, separated by a coma.
150 * The <tail> argument is a pointer to a current list which should be appended
151 * to the tail of the new list. The pointer to the new list is returned.
152 */
153static struct listener *str2listener(char *str, struct listener *tail)
154{
155 struct listener *l;
156 char *c, *next, *range, *dupstr;
157 int port, end;
158
159 next = dupstr = strdup(str);
160
161 while (next && *next) {
162 struct sockaddr_storage ss;
163
164 str = next;
165 /* 1) look for the end of the first address */
166 if ((next = strrchr(str, ',')) != NULL) {
167 *next++ = 0;
168 }
169
170 /* 2) look for the addr/port delimiter, it's the last colon. */
171 if ((range = strrchr(str, ':')) == NULL) {
172 Alert("Missing port number: '%s'\n", str);
173 goto fail;
174 }
175
176 *range++ = 0;
177
178 if (strrchr(str, ':') != NULL) {
179 /* IPv6 address contains ':' */
180 memset(&ss, 0, sizeof(ss));
181 ss.ss_family = AF_INET6;
182
183 if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) {
184 Alert("Invalid server address: '%s'\n", str);
185 goto fail;
186 }
187 }
188 else {
189 memset(&ss, 0, sizeof(ss));
190 ss.ss_family = AF_INET;
191
192 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
193 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
194 }
195 else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) {
196 struct hostent *he;
197
198 if ((he = gethostbyname(str)) == NULL) {
199 Alert("Invalid server name: '%s'\n", str);
200 goto fail;
201 }
202 else
203 ((struct sockaddr_in *)&ss)->sin_addr =
204 *(struct in_addr *) *(he->h_addr_list);
205 }
206 }
207
208 /* 3) look for the port-end delimiter */
209 if ((c = strchr(range, '-')) != NULL) {
210 *c++ = 0;
211 end = atol(c);
212 }
213 else {
214 end = atol(range);
215 }
216
217 port = atol(range);
218
219 if (port < 1 || port > 65535) {
220 Alert("Invalid port '%d' specified for address '%s'.\n", port, str);
221 goto fail;
222 }
223
224 if (end < 1 || end > 65535) {
225 Alert("Invalid port '%d' specified for address '%s'.\n", end, str);
226 goto fail;
227 }
228
229 for (; port <= end; port++) {
230 l = (struct listener *)calloc(1, sizeof(struct listener));
231 l->next = tail;
232 tail = l;
233
234 l->fd = -1;
235 l->addr = ss;
236 if (ss.ss_family == AF_INET6)
237 ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port);
238 else
239 ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port);
240
241 } /* end for(port) */
242 } /* end while(next) */
243 free(dupstr);
244 return tail;
245 fail:
246 free(dupstr);
247 return NULL;
248}
249
250
251/*
252 * parse a line in a <global> section. Returns 0 if OK, -1 if error.
253 */
254int cfg_parse_global(char *file, int linenum, char **args)
255{
256
257 if (!strcmp(args[0], "global")) { /* new section */
258 /* no option, nothing special to do */
259 return 0;
260 }
261 else if (!strcmp(args[0], "daemon")) {
262 global.mode |= MODE_DAEMON;
263 }
264 else if (!strcmp(args[0], "debug")) {
265 global.mode |= MODE_DEBUG;
266 }
267 else if (!strcmp(args[0], "noepoll")) {
268 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
269 }
270 else if (!strcmp(args[0], "nopoll")) {
271 cfg_polling_mechanism &= ~POLL_USE_POLL;
272 }
273 else if (!strcmp(args[0], "quiet")) {
274 global.mode |= MODE_QUIET;
275 }
276 else if (!strcmp(args[0], "stats")) {
277 global.mode |= MODE_STATS;
278 }
279 else if (!strcmp(args[0], "uid")) {
280 if (global.uid != 0) {
281 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
282 return 0;
283 }
284 if (*(args[1]) == 0) {
285 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
286 return -1;
287 }
288 global.uid = atol(args[1]);
289 }
290 else if (!strcmp(args[0], "gid")) {
291 if (global.gid != 0) {
292 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
293 return 0;
294 }
295 if (*(args[1]) == 0) {
296 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
297 return -1;
298 }
299 global.gid = atol(args[1]);
300 }
301 else if (!strcmp(args[0], "nbproc")) {
302 if (global.nbproc != 0) {
303 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
304 return 0;
305 }
306 if (*(args[1]) == 0) {
307 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
308 return -1;
309 }
310 global.nbproc = atol(args[1]);
311 }
312 else if (!strcmp(args[0], "maxconn")) {
313 if (global.maxconn != 0) {
314 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
315 return 0;
316 }
317 if (*(args[1]) == 0) {
318 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
319 return -1;
320 }
321 global.maxconn = atol(args[1]);
322#ifdef SYSTEM_MAXCONN
323 if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) {
324 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);
325 global.maxconn = DEFAULT_MAXCONN;
326 }
327#endif /* SYSTEM_MAXCONN */
328 }
329 else if (!strcmp(args[0], "ulimit-n")) {
330 if (global.rlimit_nofile != 0) {
331 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
332 return 0;
333 }
334 if (*(args[1]) == 0) {
335 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
336 return -1;
337 }
338 global.rlimit_nofile = atol(args[1]);
339 }
340 else if (!strcmp(args[0], "chroot")) {
341 if (global.chroot != NULL) {
342 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
343 return 0;
344 }
345 if (*(args[1]) == 0) {
346 Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
347 return -1;
348 }
349 global.chroot = strdup(args[1]);
350 }
351 else if (!strcmp(args[0], "pidfile")) {
352 if (global.pidfile != NULL) {
353 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
354 return 0;
355 }
356 if (*(args[1]) == 0) {
357 Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
358 return -1;
359 }
360 global.pidfile = strdup(args[1]);
361 }
362 else if (!strcmp(args[0], "log")) { /* syslog server address */
363 struct sockaddr_in *sa;
364 int facility, level;
365
366 if (*(args[1]) == 0 || *(args[2]) == 0) {
367 Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]);
368 return -1;
369 }
370
371 facility = get_log_facility(args[2]);
372 if (facility < 0) {
373 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
374 exit(1);
375 }
376
377 level = 7; /* max syslog level = debug */
378 if (*(args[3])) {
379 level = get_log_level(args[3]);
380 if (level < 0) {
381 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
382 exit(1);
383 }
384 }
385
386 sa = str2sa(args[1]);
387 if (!sa->sin_port)
388 sa->sin_port = htons(SYSLOG_PORT);
389
390 if (global.logfac1 == -1) {
391 global.logsrv1 = *sa;
392 global.logfac1 = facility;
393 global.loglev1 = level;
394 }
395 else if (global.logfac2 == -1) {
396 global.logsrv2 = *sa;
397 global.logfac2 = facility;
398 global.loglev2 = level;
399 }
400 else {
401 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
402 return -1;
403 }
404
405 }
406 else {
407 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
408 return -1;
409 }
410 return 0;
411}
412
413
414static void init_default_instance()
415{
416 memset(&defproxy, 0, sizeof(defproxy));
417 defproxy.mode = PR_MODE_TCP;
418 defproxy.state = PR_STNEW;
419 defproxy.maxconn = cfg_maxpconn;
420 defproxy.conn_retries = CONN_RETRIES;
421 defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
422}
423
424/*
425 * parse a line in a <listen> section. Returns 0 if OK, -1 if error.
426 */
427int cfg_parse_listen(char *file, int linenum, char **args)
428{
429 static struct proxy *curproxy = NULL;
430 struct server *newsrv = NULL;
431 char *err;
432 int rc;
433
434 if (!strcmp(args[0], "listen")) { /* new proxy */
435 if (!*args[1]) {
436 Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
437 " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
438 file, linenum, args[0]);
439 return -1;
440 }
441
442 if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
443 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
444 return -1;
445 }
446
447 curproxy->next = proxy;
448 proxy = curproxy;
449 LIST_INIT(&curproxy->pendconns);
450
451 curproxy->id = strdup(args[1]);
452
453 /* parse the listener address if any */
454 if (*args[2]) {
455 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;
463 curproxy->maxconn = defproxy.maxconn;
464 curproxy->conn_retries = defproxy.conn_retries;
465 curproxy->options = defproxy.options;
466
467 if (defproxy.check_req)
468 curproxy->check_req = strdup(defproxy.check_req);
469 curproxy->check_len = defproxy.check_len;
470
471 if (defproxy.cookie_name)
472 curproxy->cookie_name = strdup(defproxy.cookie_name);
473 curproxy->cookie_len = defproxy.cookie_len;
474
475 if (defproxy.capture_name)
476 curproxy->capture_name = strdup(defproxy.capture_name);
477 curproxy->capture_namelen = defproxy.capture_namelen;
478 curproxy->capture_len = defproxy.capture_len;
479
480 if (defproxy.errmsg.msg400)
481 curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400);
482 curproxy->errmsg.len400 = defproxy.errmsg.len400;
483
484 if (defproxy.errmsg.msg403)
485 curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403);
486 curproxy->errmsg.len403 = defproxy.errmsg.len403;
487
488 if (defproxy.errmsg.msg408)
489 curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408);
490 curproxy->errmsg.len408 = defproxy.errmsg.len408;
491
492 if (defproxy.errmsg.msg500)
493 curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500);
494 curproxy->errmsg.len500 = defproxy.errmsg.len500;
495
496 if (defproxy.errmsg.msg502)
497 curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502);
498 curproxy->errmsg.len502 = defproxy.errmsg.len502;
499
500 if (defproxy.errmsg.msg503)
501 curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503);
502 curproxy->errmsg.len503 = defproxy.errmsg.len503;
503
504 if (defproxy.errmsg.msg504)
505 curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504);
506 curproxy->errmsg.len504 = defproxy.errmsg.len504;
507
508 curproxy->clitimeout = defproxy.clitimeout;
509 curproxy->contimeout = defproxy.contimeout;
510 curproxy->srvtimeout = defproxy.srvtimeout;
511 curproxy->mode = defproxy.mode;
512 curproxy->logfac1 = defproxy.logfac1;
513 curproxy->logsrv1 = defproxy.logsrv1;
514 curproxy->loglev1 = defproxy.loglev1;
515 curproxy->logfac2 = defproxy.logfac2;
516 curproxy->logsrv2 = defproxy.logsrv2;
517 curproxy->loglev2 = defproxy.loglev2;
518 curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
519 curproxy->grace = defproxy.grace;
520 curproxy->uri_auth = defproxy.uri_auth;
521 curproxy->source_addr = defproxy.source_addr;
522 curproxy->mon_net = defproxy.mon_net;
523 curproxy->mon_mask = defproxy.mon_mask;
Willy Tarreau1c47f852006-07-09 08:22:27 +0200524
525 if (defproxy.monitor_uri)
526 curproxy->monitor_uri = strdup(defproxy.monitor_uri);
527 curproxy->monitor_uri_len = defproxy.monitor_uri_len;
528
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529 return 0;
530 }
531 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
532 /* some variables may have already been initialized earlier */
533 if (defproxy.check_req) free(defproxy.check_req);
534 if (defproxy.cookie_name) free(defproxy.cookie_name);
535 if (defproxy.capture_name) free(defproxy.capture_name);
536 if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400);
537 if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403);
538 if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408);
539 if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500);
540 if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502);
541 if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503);
542 if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504);
Willy Tarreau1c47f852006-07-09 08:22:27 +0200543 if (defproxy.monitor_uri) free(defproxy.monitor_uri);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 /* we cannot free uri_auth because it might already be used */
545 init_default_instance();
546 curproxy = &defproxy;
547 return 0;
548 }
549 else if (curproxy == NULL) {
550 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
551 return -1;
552 }
553
554 if (!strcmp(args[0], "bind")) { /* new listen addresses */
555 if (curproxy == &defproxy) {
556 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
557 return -1;
558 }
559
560 if (strchr(args[1], ':') == NULL) {
561 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
562 file, linenum, args[0]);
563 return -1;
564 }
565 curproxy->listen = str2listener(args[1], curproxy->listen);
566 if (!curproxy->listen)
567 return -1;
568 global.maxsock++;
569 return 0;
570 }
571 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
572 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
573 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
574 file, linenum, args[0]);
575 return -1;
576 }
577 /* flush useless bits */
578 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
579 return 0;
580 }
Willy Tarreau1c47f852006-07-09 08:22:27 +0200581 else if (!strcmp(args[0], "monitor-uri")) { /* set the URI to intercept */
582 if (!*args[1]) {
583 Alert("parsing [%s:%d] : '%s' expects an URI.\n",
584 file, linenum, args[0]);
585 return -1;
586 }
587
588 if (curproxy->monitor_uri != NULL)
589 free(curproxy->monitor_uri);
590
591 curproxy->monitor_uri_len = strlen(args[1]) + 2; /* include leading and trailing spaces */
592 curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
593 memcpy(curproxy->monitor_uri + 1, args[1], curproxy->monitor_uri_len - 2);
594 curproxy->monitor_uri[curproxy->monitor_uri_len-1] = curproxy->monitor_uri[0] = ' ';
595 curproxy->monitor_uri[curproxy->monitor_uri_len] = '\0';
596
597 return 0;
598 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
600 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
601 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
602 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
603 else {
604 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
605 return -1;
606 }
607 }
608 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
609 curproxy->state = PR_STSTOPPED;
610 }
611 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
612 curproxy->state = PR_STNEW;
613 }
614 else if (!strcmp(args[0], "cookie")) { /* cookie name */
615 int cur_arg;
616 // if (curproxy == &defproxy) {
617 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
618 // return -1;
619 // }
620
621 if (curproxy->cookie_name != NULL) {
622 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
623 // file, linenum);
624 // return 0;
625 free(curproxy->cookie_name);
626 }
627
628 if (*(args[1]) == 0) {
629 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
630 file, linenum, args[0]);
631 return -1;
632 }
633 curproxy->cookie_name = strdup(args[1]);
634 curproxy->cookie_len = strlen(curproxy->cookie_name);
635
636 cur_arg = 2;
637 while (*(args[cur_arg])) {
638 if (!strcmp(args[cur_arg], "rewrite")) {
639 curproxy->options |= PR_O_COOK_RW;
640 }
641 else if (!strcmp(args[cur_arg], "indirect")) {
642 curproxy->options |= PR_O_COOK_IND;
643 }
644 else if (!strcmp(args[cur_arg], "insert")) {
645 curproxy->options |= PR_O_COOK_INS;
646 }
647 else if (!strcmp(args[cur_arg], "nocache")) {
648 curproxy->options |= PR_O_COOK_NOC;
649 }
650 else if (!strcmp(args[cur_arg], "postonly")) {
651 curproxy->options |= PR_O_COOK_POST;
652 }
653 else if (!strcmp(args[cur_arg], "prefix")) {
654 curproxy->options |= PR_O_COOK_PFX;
655 }
656 else {
657 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
658 file, linenum, args[0]);
659 return -1;
660 }
661 cur_arg++;
662 }
663 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
664 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
665 file, linenum);
666 return -1;
667 }
668
669 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
670 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
671 file, linenum);
672 return -1;
673 }
674 }/* end else if (!strcmp(args[0], "cookie")) */
675 else if (!strcmp(args[0], "appsession")) { /* cookie name */
676 // if (curproxy == &defproxy) {
677 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
678 // return -1;
679 // }
680
681 if (curproxy->appsession_name != NULL) {
682 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
683 // file, linenum);
684 // return 0;
685 free(curproxy->appsession_name);
686 }
687
688 if (*(args[5]) == 0) {
689 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
690 file, linenum, args[0]);
691 return -1;
692 }
693 have_appsession = 1;
694 curproxy->appsession_name = strdup(args[1]);
695 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
696 curproxy->appsession_len = atoi(args[3]);
697 curproxy->appsession_timeout = atoi(args[5]);
698 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
699 if (rc) {
700 Alert("Error Init Appsession Hashtable.\n");
701 return -1;
702 }
703 } /* Url App Session */
704 else if (!strcmp(args[0], "capture")) {
705 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
706 // if (curproxy == &defproxy) {
707 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
708 // return -1;
709 // }
710
711 if (curproxy->capture_name != NULL) {
712 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
713 // file, linenum, args[0]);
714 // return 0;
715 free(curproxy->capture_name);
716 }
717
718 if (*(args[4]) == 0) {
719 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
720 file, linenum, args[0]);
721 return -1;
722 }
723 curproxy->capture_name = strdup(args[2]);
724 curproxy->capture_namelen = strlen(curproxy->capture_name);
725 curproxy->capture_len = atol(args[4]);
726 if (curproxy->capture_len >= CAPTURE_LEN) {
727 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
728 file, linenum, CAPTURE_LEN - 1);
729 curproxy->capture_len = CAPTURE_LEN - 1;
730 }
731 curproxy->to_log |= LW_COOKIE;
732 }
733 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
734 struct cap_hdr *hdr;
735
736 if (curproxy == &defproxy) {
737 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
738 return -1;
739 }
740
741 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
742 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
743 file, linenum, args[0], args[1]);
744 return -1;
745 }
746
747 hdr = calloc(sizeof(struct cap_hdr), 1);
748 hdr->next = curproxy->req_cap;
749 hdr->name = strdup(args[3]);
750 hdr->namelen = strlen(args[3]);
751 hdr->len = atol(args[5]);
752 hdr->index = curproxy->nb_req_cap++;
753 curproxy->req_cap = hdr;
754 curproxy->to_log |= LW_REQHDR;
755 }
756 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
757 struct cap_hdr *hdr;
758
759 if (curproxy == &defproxy) {
760 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
761 return -1;
762 }
763
764 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
765 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
766 file, linenum, args[0], args[1]);
767 return -1;
768 }
769 hdr = calloc(sizeof(struct cap_hdr), 1);
770 hdr->next = curproxy->rsp_cap;
771 hdr->name = strdup(args[3]);
772 hdr->namelen = strlen(args[3]);
773 hdr->len = atol(args[5]);
774 hdr->index = curproxy->nb_rsp_cap++;
775 curproxy->rsp_cap = hdr;
776 curproxy->to_log |= LW_RSPHDR;
777 }
778 else {
779 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
780 file, linenum, args[0]);
781 return -1;
782 }
783 }
784 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
785 if (curproxy->contimeout != defproxy.contimeout) {
786 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
787 return 0;
788 }
789 if (*(args[1]) == 0) {
790 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
791 file, linenum, args[0]);
792 return -1;
793 }
794 curproxy->contimeout = atol(args[1]);
795 }
796 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
797 if (curproxy->clitimeout != defproxy.clitimeout) {
798 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
799 file, linenum, args[0]);
800 return 0;
801 }
802 if (*(args[1]) == 0) {
803 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
804 file, linenum, args[0]);
805 return -1;
806 }
807 curproxy->clitimeout = atol(args[1]);
808 }
809 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
810 if (curproxy->srvtimeout != defproxy.srvtimeout) {
811 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
812 return 0;
813 }
814 if (*(args[1]) == 0) {
815 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
816 file, linenum, args[0]);
817 return -1;
818 }
819 curproxy->srvtimeout = atol(args[1]);
820 }
821 else if (!strcmp(args[0], "retries")) { /* connection retries */
822 if (*(args[1]) == 0) {
823 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
824 file, linenum, args[0]);
825 return -1;
826 }
827 curproxy->conn_retries = atol(args[1]);
828 }
829 else if (!strcmp(args[0], "stats")) {
830 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
831 curproxy->uri_auth = NULL; /* we must detach from the default config */
832
833 if (*(args[1]) == 0) {
834 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
835 return -1;
836 } else if (!strcmp(args[1], "uri")) {
837 if (*(args[2]) == 0) {
838 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
839 return -1;
840 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
841 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
842 return -1;
843 }
844 } else if (!strcmp(args[1], "realm")) {
845 if (*(args[2]) == 0) {
846 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
847 return -1;
848 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
849 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
850 return -1;
851 }
852 } else if (!strcmp(args[1], "auth")) {
853 if (*(args[2]) == 0) {
854 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
855 return -1;
856 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
857 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
858 return -1;
859 }
860 } else if (!strcmp(args[1], "scope")) {
861 if (*(args[2]) == 0) {
862 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
863 return -1;
864 } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) {
865 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
866 return -1;
867 }
868 } else if (!strcmp(args[1], "enable")) {
869 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
870 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
871 return -1;
872 }
873 } else {
874 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
875 file, linenum, args[0]);
876 return -1;
877 }
878 }
879 else if (!strcmp(args[0], "option")) {
880 if (*(args[1]) == 0) {
881 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
882 return -1;
883 }
884 if (!strcmp(args[1], "redispatch"))
885 /* enable reconnections to dispatch */
886 curproxy->options |= PR_O_REDISP;
887#ifdef TPROXY
888 else if (!strcmp(args[1], "transparent"))
889 /* enable transparent proxy connections */
890 curproxy->options |= PR_O_TRANSP;
891#endif
892 else if (!strcmp(args[1], "keepalive"))
893 /* enable keep-alive */
894 curproxy->options |= PR_O_KEEPALIVE;
895 else if (!strcmp(args[1], "forwardfor"))
896 /* insert x-forwarded-for field */
897 curproxy->options |= PR_O_FWDFOR;
898 else if (!strcmp(args[1], "logasap"))
899 /* log as soon as possible, without waiting for the session to complete */
900 curproxy->options |= PR_O_LOGASAP;
901 else if (!strcmp(args[1], "abortonclose"))
902 /* abort connection if client closes during queue or connect() */
903 curproxy->options |= PR_O_ABRT_CLOSE;
904 else if (!strcmp(args[1], "httpclose"))
905 /* force connection: close in both directions in HTTP mode */
906 curproxy->options |= PR_O_HTTP_CLOSE;
907 else if (!strcmp(args[1], "forceclose"))
908 /* force connection: close in both directions in HTTP mode and enforce end of session */
909 curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE;
910 else if (!strcmp(args[1], "checkcache"))
911 /* require examination of cacheability of the 'set-cookie' field */
912 curproxy->options |= PR_O_CHK_CACHE;
913 else if (!strcmp(args[1], "httplog"))
914 /* generate a complete HTTP log */
915 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
916 else if (!strcmp(args[1], "tcplog"))
917 /* generate a detailed TCP log */
918 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
919 else if (!strcmp(args[1], "dontlognull")) {
920 /* don't log empty requests */
921 curproxy->options |= PR_O_NULLNOLOG;
922 }
923 else if (!strcmp(args[1], "tcpka")) {
924 /* enable TCP keep-alives on client and server sessions */
925 curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA;
926 }
927 else if (!strcmp(args[1], "clitcpka")) {
928 /* enable TCP keep-alives on client sessions */
929 curproxy->options |= PR_O_TCP_CLI_KA;
930 }
931 else if (!strcmp(args[1], "srvtcpka")) {
932 /* enable TCP keep-alives on server sessions */
933 curproxy->options |= PR_O_TCP_SRV_KA;
934 }
935 else if (!strcmp(args[1], "allbackups")) {
936 /* Use all backup servers simultaneously */
937 curproxy->options |= PR_O_USE_ALL_BK;
938 }
939 else if (!strcmp(args[1], "httpchk")) {
940 /* use HTTP request to check servers' health */
941 if (curproxy->check_req != NULL) {
942 free(curproxy->check_req);
943 }
944 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200945 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946 if (!*args[2]) { /* no argument */
947 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
948 curproxy->check_len = strlen(DEF_CHECK_REQ);
949 } else if (!*args[3]) { /* one argument : URI */
950 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
951 curproxy->check_req = (char *)malloc(reqlen);
952 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
953 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
954 } else { /* more arguments : METHOD URI [HTTP_VER] */
955 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
956 if (*args[4])
957 reqlen += strlen(args[4]);
958 else
959 reqlen += strlen("HTTP/1.0");
960
961 curproxy->check_req = (char *)malloc(reqlen);
962 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
963 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
964 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200965 }
966 else if (!strcmp(args[1], "ssl-hello-chk")) {
967 /* use SSLv3 CLIENT HELLO to check servers' health */
968 if (curproxy->check_req != NULL) {
969 free(curproxy->check_req);
970 }
971 curproxy->options &= ~PR_O_HTTP_CHK;
972 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973 }
974 else if (!strcmp(args[1], "persist")) {
975 /* persist on using the server specified by the cookie, even when it's down */
976 curproxy->options |= PR_O_PERSIST;
977 }
978 else {
979 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
980 return -1;
981 }
982 return 0;
983 }
984 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
985 /* enable reconnections to dispatch */
986 curproxy->options |= PR_O_REDISP;
987 }
988#ifdef TPROXY
989 else if (!strcmp(args[0], "transparent")) {
990 /* enable transparent proxy connections */
991 curproxy->options |= PR_O_TRANSP;
992 }
993#endif
994 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
995 if (*(args[1]) == 0) {
996 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
997 return -1;
998 }
999 curproxy->maxconn = atol(args[1]);
1000 }
1001 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
1002 if (*(args[1]) == 0) {
1003 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
1004 return -1;
1005 }
1006 curproxy->grace = atol(args[1]);
1007 }
1008 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
1009 if (curproxy == &defproxy) {
1010 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1011 return -1;
1012 }
1013 if (strchr(args[1], ':') == NULL) {
1014 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
1015 return -1;
1016 }
1017 curproxy->dispatch_addr = *str2sa(args[1]);
1018 }
1019 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
1020 if (*(args[1])) {
1021 if (!strcmp(args[1], "roundrobin")) {
1022 curproxy->options |= PR_O_BALANCE_RR;
1023 }
1024 else if (!strcmp(args[1], "source")) {
1025 curproxy->options |= PR_O_BALANCE_SH;
1026 }
1027 else {
1028 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1029 return -1;
1030 }
1031 }
1032 else /* if no option is set, use round-robin by default */
1033 curproxy->options |= PR_O_BALANCE_RR;
1034 }
1035 else if (!strcmp(args[0], "server")) { /* server address */
1036 int cur_arg;
1037 char *rport;
1038 char *raddr;
1039 short realport;
1040 int do_check;
1041
1042 if (curproxy == &defproxy) {
1043 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1044 return -1;
1045 }
1046
1047 if (!*args[2]) {
1048 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1049 file, linenum, args[0]);
1050 return -1;
1051 }
1052 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1053 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1054 return -1;
1055 }
1056
1057 /* the servers are linked backwards first */
1058 newsrv->next = curproxy->srv;
1059 curproxy->srv = newsrv;
1060 newsrv->proxy = curproxy;
1061
1062 LIST_INIT(&newsrv->pendconns);
1063 do_check = 0;
1064 newsrv->state = SRV_RUNNING; /* early server setup */
1065 newsrv->id = strdup(args[1]);
1066
1067 /* several ways to check the port component :
1068 * - IP => port=+0, relative
1069 * - IP: => port=+0, relative
1070 * - IP:N => port=N, absolute
1071 * - IP:+N => port=+N, relative
1072 * - IP:-N => port=-N, relative
1073 */
1074 raddr = strdup(args[2]);
1075 rport = strchr(raddr, ':');
1076 if (rport) {
1077 *rport++ = 0;
1078 realport = atol(rport);
1079 if (!isdigit((int)*rport))
1080 newsrv->state |= SRV_MAPPORTS;
1081 } else {
1082 realport = 0;
1083 newsrv->state |= SRV_MAPPORTS;
1084 }
1085
1086 newsrv->addr = *str2sa(raddr);
1087 newsrv->addr.sin_port = htons(realport);
1088 free(raddr);
1089
1090 newsrv->curfd = -1; /* no health-check in progress */
1091 newsrv->inter = DEF_CHKINTR;
1092 newsrv->rise = DEF_RISETIME;
1093 newsrv->fall = DEF_FALLTIME;
1094 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
1095 cur_arg = 3;
1096 while (*args[cur_arg]) {
1097 if (!strcmp(args[cur_arg], "cookie")) {
1098 newsrv->cookie = strdup(args[cur_arg + 1]);
1099 newsrv->cklen = strlen(args[cur_arg + 1]);
1100 cur_arg += 2;
1101 }
1102 else if (!strcmp(args[cur_arg], "rise")) {
1103 newsrv->rise = atol(args[cur_arg + 1]);
1104 newsrv->health = newsrv->rise;
1105 cur_arg += 2;
1106 }
1107 else if (!strcmp(args[cur_arg], "fall")) {
1108 newsrv->fall = atol(args[cur_arg + 1]);
1109 cur_arg += 2;
1110 }
1111 else if (!strcmp(args[cur_arg], "inter")) {
1112 newsrv->inter = atol(args[cur_arg + 1]);
1113 cur_arg += 2;
1114 }
1115 else if (!strcmp(args[cur_arg], "port")) {
1116 newsrv->check_port = atol(args[cur_arg + 1]);
1117 cur_arg += 2;
1118 }
1119 else if (!strcmp(args[cur_arg], "backup")) {
1120 newsrv->state |= SRV_BACKUP;
1121 cur_arg ++;
1122 }
1123 else if (!strcmp(args[cur_arg], "weight")) {
1124 int w;
1125 w = atol(args[cur_arg + 1]);
1126 if (w < 1 || w > 256) {
1127 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1128 file, linenum, newsrv->id, w);
1129 return -1;
1130 }
1131 newsrv->uweight = w - 1;
1132 cur_arg += 2;
1133 }
1134 else if (!strcmp(args[cur_arg], "minconn")) {
1135 newsrv->minconn = atol(args[cur_arg + 1]);
1136 cur_arg += 2;
1137 }
1138 else if (!strcmp(args[cur_arg], "maxconn")) {
1139 newsrv->maxconn = atol(args[cur_arg + 1]);
1140 cur_arg += 2;
1141 }
1142 else if (!strcmp(args[cur_arg], "check")) {
1143 global.maxsock++;
1144 do_check = 1;
1145 cur_arg += 1;
1146 }
1147 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1148 if (!*args[cur_arg + 1]) {
1149 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1150 file, linenum, "source");
1151 return -1;
1152 }
1153 newsrv->state |= SRV_BIND_SRC;
1154 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1155 cur_arg += 2;
1156 }
1157 else {
1158 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
1159 file, linenum, newsrv->id);
1160 return -1;
1161 }
1162 }
1163
1164 if (do_check) {
1165 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1166 newsrv->check_port = realport; /* by default */
1167 if (!newsrv->check_port) {
1168 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1169 file, linenum, newsrv->id);
1170 return -1;
1171 }
1172 newsrv->state |= SRV_CHECKED;
1173 }
1174
1175 if (newsrv->state & SRV_BACKUP)
1176 curproxy->srv_bck++;
1177 else
1178 curproxy->srv_act++;
1179 }
1180 else if (!strcmp(args[0], "log")) { /* syslog server address */
1181 struct sockaddr_in *sa;
1182 int facility;
1183
1184 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1185 curproxy->logfac1 = global.logfac1;
1186 curproxy->logsrv1 = global.logsrv1;
1187 curproxy->loglev1 = global.loglev1;
1188 curproxy->logfac2 = global.logfac2;
1189 curproxy->logsrv2 = global.logsrv2;
1190 curproxy->loglev2 = global.loglev2;
1191 }
1192 else if (*(args[1]) && *(args[2])) {
1193 int level;
1194
1195 facility = get_log_facility(args[2]);
1196 if (facility < 0) {
1197 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1198 exit(1);
1199 }
1200
1201 level = 7; /* max syslog level = debug */
1202 if (*(args[3])) {
1203 level = get_log_level(args[3]);
1204 if (level < 0) {
1205 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1206 exit(1);
1207 }
1208 }
1209
1210 sa = str2sa(args[1]);
1211 if (!sa->sin_port)
1212 sa->sin_port = htons(SYSLOG_PORT);
1213
1214 if (curproxy->logfac1 == -1) {
1215 curproxy->logsrv1 = *sa;
1216 curproxy->logfac1 = facility;
1217 curproxy->loglev1 = level;
1218 }
1219 else if (curproxy->logfac2 == -1) {
1220 curproxy->logsrv2 = *sa;
1221 curproxy->logfac2 = facility;
1222 curproxy->loglev2 = level;
1223 }
1224 else {
1225 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1226 return -1;
1227 }
1228 }
1229 else {
1230 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1231 file, linenum);
1232 return -1;
1233 }
1234 }
1235 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
1236 if (!*args[1]) {
1237 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1238 file, linenum, "source");
1239 return -1;
1240 }
1241
1242 curproxy->source_addr = *str2sa(args[1]);
1243 curproxy->options |= PR_O_BIND_SRC;
1244 }
1245 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1246 regex_t *preg;
1247 if (curproxy == &defproxy) {
1248 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1249 return -1;
1250 }
1251
1252 if (*(args[1]) == 0 || *(args[2]) == 0) {
1253 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1254 file, linenum, args[0]);
1255 return -1;
1256 }
1257
1258 preg = calloc(1, sizeof(regex_t));
1259 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1260 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1261 return -1;
1262 }
1263
1264 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1265 if (err) {
1266 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1267 file, linenum, *err);
1268 return -1;
1269 }
1270 }
1271 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1272 regex_t *preg;
1273 if (curproxy == &defproxy) {
1274 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1275 return -1;
1276 }
1277
1278 if (*(args[1]) == 0) {
1279 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1280 return -1;
1281 }
1282
1283 preg = calloc(1, sizeof(regex_t));
1284 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1285 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1286 return -1;
1287 }
1288
1289 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1290 }
1291 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1292 regex_t *preg;
1293 if (curproxy == &defproxy) {
1294 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1295 return -1;
1296 }
1297
1298 if (*(args[1]) == 0) {
1299 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1300 return -1;
1301 }
1302
1303 preg = calloc(1, sizeof(regex_t));
1304 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1305 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1306 return -1;
1307 }
1308
1309 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1310 }
1311 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1312 regex_t *preg;
1313 if (curproxy == &defproxy) {
1314 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1315 return -1;
1316 }
1317
1318 if (*(args[1]) == 0) {
1319 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1320 return -1;
1321 }
1322
1323 preg = calloc(1, sizeof(regex_t));
1324 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1325 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1326 return -1;
1327 }
1328
1329 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1330 }
1331 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1332 regex_t *preg;
1333 if (curproxy == &defproxy) {
1334 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1335 return -1;
1336 }
1337
1338 if (*(args[1]) == 0) {
1339 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1340 return -1;
1341 }
1342
1343 preg = calloc(1, sizeof(regex_t));
1344 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1345 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1346 return -1;
1347 }
1348
1349 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1350 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001351 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1352 regex_t *preg;
1353 if (curproxy == &defproxy) {
1354 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1355 return -1;
1356 }
1357
1358 if (*(args[1]) == 0) {
1359 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1360 return -1;
1361 }
1362
1363 preg = calloc(1, sizeof(regex_t));
1364 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1365 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1366 return -1;
1367 }
1368
1369 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1370 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1372 regex_t *preg;
1373 if (curproxy == &defproxy) {
1374 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1375 return -1;
1376 }
1377
1378 if (*(args[1]) == 0 || *(args[2]) == 0) {
1379 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1380 file, linenum, args[0]);
1381 return -1;
1382 }
1383
1384 preg = calloc(1, sizeof(regex_t));
1385 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1386 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1387 return -1;
1388 }
1389
1390 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1391 if (err) {
1392 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1393 file, linenum, *err);
1394 return -1;
1395 }
1396 }
1397 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1398 regex_t *preg;
1399 if (curproxy == &defproxy) {
1400 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1401 return -1;
1402 }
1403
1404 if (*(args[1]) == 0) {
1405 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1406 return -1;
1407 }
1408
1409 preg = calloc(1, sizeof(regex_t));
1410 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1411 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1412 return -1;
1413 }
1414
1415 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1416 }
1417 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1418 regex_t *preg;
1419 if (curproxy == &defproxy) {
1420 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1421 return -1;
1422 }
1423
1424 if (*(args[1]) == 0) {
1425 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1426 return -1;
1427 }
1428
1429 preg = calloc(1, sizeof(regex_t));
1430 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1431 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1432 return -1;
1433 }
1434
1435 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1436 }
1437 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1438 regex_t *preg;
1439 if (curproxy == &defproxy) {
1440 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1441 return -1;
1442 }
1443
1444 if (*(args[1]) == 0) {
1445 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1446 return -1;
1447 }
1448
1449 preg = calloc(1, sizeof(regex_t));
1450 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1451 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1452 return -1;
1453 }
1454
1455 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1456 }
1457 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1458 regex_t *preg;
1459 if (curproxy == &defproxy) {
1460 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1461 return -1;
1462 }
1463
1464 if (*(args[1]) == 0) {
1465 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1466 return -1;
1467 }
1468
1469 preg = calloc(1, sizeof(regex_t));
1470 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1471 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1472 return -1;
1473 }
1474
1475 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1476 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001477 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1478 regex_t *preg;
1479 if (curproxy == &defproxy) {
1480 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1481 return -1;
1482 }
1483
1484 if (*(args[1]) == 0) {
1485 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1486 return -1;
1487 }
1488
1489 preg = calloc(1, sizeof(regex_t));
1490 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1491 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1492 return -1;
1493 }
1494
1495 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1496 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001497 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1498 if (curproxy == &defproxy) {
1499 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1500 return -1;
1501 }
1502
1503 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1504 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1505 return 0;
1506 }
1507
1508 if (*(args[1]) == 0) {
1509 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1510 return -1;
1511 }
1512
1513 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1514 }
1515 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1516 regex_t *preg;
1517
1518 if (*(args[1]) == 0 || *(args[2]) == 0) {
1519 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1520 file, linenum, args[0]);
1521 return -1;
1522 }
1523
1524 preg = calloc(1, sizeof(regex_t));
1525 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1526 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1527 return -1;
1528 }
1529
1530 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1531 if (err) {
1532 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1533 file, linenum, *err);
1534 return -1;
1535 }
1536 }
1537 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1538 regex_t *preg;
1539 if (curproxy == &defproxy) {
1540 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1541 return -1;
1542 }
1543
1544 if (*(args[1]) == 0) {
1545 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1546 return -1;
1547 }
1548
1549 preg = calloc(1, sizeof(regex_t));
1550 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1551 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1552 return -1;
1553 }
1554
1555 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1556 if (err) {
1557 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1558 file, linenum, *err);
1559 return -1;
1560 }
1561 }
1562 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1563 regex_t *preg;
1564 if (curproxy == &defproxy) {
1565 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1566 return -1;
1567 }
1568
1569 if (*(args[1]) == 0) {
1570 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1571 return -1;
1572 }
1573
1574 preg = calloc(1, sizeof(regex_t));
1575 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1576 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1577 return -1;
1578 }
1579
1580 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1581 if (err) {
1582 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1583 file, linenum, *err);
1584 return -1;
1585 }
1586 }
1587 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1588 regex_t *preg;
1589 if (curproxy == &defproxy) {
1590 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1591 return -1;
1592 }
1593
1594 if (*(args[1]) == 0 || *(args[2]) == 0) {
1595 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1596 file, linenum, args[0]);
1597 return -1;
1598 }
1599
1600 preg = calloc(1, sizeof(regex_t));
1601 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1602 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1603 return -1;
1604 }
1605
1606 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1607 if (err) {
1608 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1609 file, linenum, *err);
1610 return -1;
1611 }
1612 }
1613 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1614 regex_t *preg;
1615 if (curproxy == &defproxy) {
1616 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1617 return -1;
1618 }
1619
1620 if (*(args[1]) == 0) {
1621 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1622 return -1;
1623 }
1624
1625 preg = calloc(1, sizeof(regex_t));
1626 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1627 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1628 return -1;
1629 }
1630
1631 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1632 if (err) {
1633 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1634 file, linenum, *err);
1635 return -1;
1636 }
1637 }
1638 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1639 regex_t *preg;
1640 if (curproxy == &defproxy) {
1641 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1642 return -1;
1643 }
1644
1645 if (*(args[1]) == 0) {
1646 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1647 return -1;
1648 }
1649
1650 preg = calloc(1, sizeof(regex_t));
1651 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1652 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1653 return -1;
1654 }
1655
1656 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1657 if (err) {
1658 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1659 file, linenum, *err);
1660 return -1;
1661 }
1662 }
1663 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1664 if (curproxy == &defproxy) {
1665 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1666 return -1;
1667 }
1668
1669 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1670 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1671 return 0;
1672 }
1673
1674 if (*(args[1]) == 0) {
1675 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1676 return -1;
1677 }
1678
1679 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1680 }
1681 else if (!strcmp(args[0], "errorloc") ||
1682 !strcmp(args[0], "errorloc302") ||
1683 !strcmp(args[0], "errorloc303")) { /* error location */
1684 int errnum, errlen;
1685 char *err;
1686
1687 // if (curproxy == &defproxy) {
1688 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1689 // return -1;
1690 // }
1691
1692 if (*(args[2]) == 0) {
1693 Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum);
1694 return -1;
1695 }
1696
1697 errnum = atol(args[1]);
1698 if (!strcmp(args[0], "errorloc303")) {
1699 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1700 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1701 } else {
1702 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1703 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
1704 }
1705
1706 if (errnum == 400) {
1707 if (curproxy->errmsg.msg400) {
1708 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1709 free(curproxy->errmsg.msg400);
1710 }
1711 curproxy->errmsg.msg400 = err;
1712 curproxy->errmsg.len400 = errlen;
1713 }
1714 else if (errnum == 403) {
1715 if (curproxy->errmsg.msg403) {
1716 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1717 free(curproxy->errmsg.msg403);
1718 }
1719 curproxy->errmsg.msg403 = err;
1720 curproxy->errmsg.len403 = errlen;
1721 }
1722 else if (errnum == 408) {
1723 if (curproxy->errmsg.msg408) {
1724 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1725 free(curproxy->errmsg.msg408);
1726 }
1727 curproxy->errmsg.msg408 = err;
1728 curproxy->errmsg.len408 = errlen;
1729 }
1730 else if (errnum == 500) {
1731 if (curproxy->errmsg.msg500) {
1732 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1733 free(curproxy->errmsg.msg500);
1734 }
1735 curproxy->errmsg.msg500 = err;
1736 curproxy->errmsg.len500 = errlen;
1737 }
1738 else if (errnum == 502) {
1739 if (curproxy->errmsg.msg502) {
1740 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1741 free(curproxy->errmsg.msg502);
1742 }
1743 curproxy->errmsg.msg502 = err;
1744 curproxy->errmsg.len502 = errlen;
1745 }
1746 else if (errnum == 503) {
1747 if (curproxy->errmsg.msg503) {
1748 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1749 free(curproxy->errmsg.msg503);
1750 }
1751 curproxy->errmsg.msg503 = err;
1752 curproxy->errmsg.len503 = errlen;
1753 }
1754 else if (errnum == 504) {
1755 if (curproxy->errmsg.msg504) {
1756 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1757 free(curproxy->errmsg.msg504);
1758 }
1759 curproxy->errmsg.msg504 = err;
1760 curproxy->errmsg.len504 = errlen;
1761 }
1762 else {
1763 Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum);
1764 free(err);
1765 }
1766 }
1767 else {
1768 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
1769 return -1;
1770 }
1771 return 0;
1772}
1773
1774
1775/*
1776 * This function reads and parses the configuration file given in the argument.
1777 * returns 0 if OK, -1 if error.
1778 */
1779int readcfgfile(char *file)
1780{
1781 char thisline[256];
1782 char *line;
1783 FILE *f;
1784 int linenum = 0;
1785 char *end;
1786 char *args[MAX_LINE_ARGS];
1787 int arg;
1788 int cfgerr = 0;
1789 int nbchk, mininter;
1790 int confsect = CFG_NONE;
1791
1792 struct proxy *curproxy = NULL;
1793 struct server *newsrv = NULL;
1794
1795 if ((f=fopen(file,"r")) == NULL)
1796 return -1;
1797
1798 init_default_instance();
1799
1800 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
1801 linenum++;
1802
1803 end = line + strlen(line);
1804
1805 /* skip leading spaces */
1806 while (isspace((int)*line))
1807 line++;
1808
1809 arg = 0;
1810 args[arg] = line;
1811
1812 while (*line && arg < MAX_LINE_ARGS) {
1813 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1814 * C equivalent value. Other combinations left unchanged (eg: \1).
1815 */
1816 if (*line == '\\') {
1817 int skip = 0;
1818 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1819 *line = line[1];
1820 skip = 1;
1821 }
1822 else if (line[1] == 'r') {
1823 *line = '\r';
1824 skip = 1;
1825 }
1826 else if (line[1] == 'n') {
1827 *line = '\n';
1828 skip = 1;
1829 }
1830 else if (line[1] == 't') {
1831 *line = '\t';
1832 skip = 1;
1833 }
1834 else if (line[1] == 'x') {
1835 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
1836 unsigned char hex1, hex2;
1837 hex1 = toupper(line[2]) - '0';
1838 hex2 = toupper(line[3]) - '0';
1839 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1840 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1841 *line = (hex1<<4) + hex2;
1842 skip = 3;
1843 }
1844 else {
1845 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
1846 return -1;
1847 }
1848 }
1849 if (skip) {
1850 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
1851 end -= skip;
1852 }
1853 line++;
1854 }
1855 else if (*line == '#' || *line == '\n' || *line == '\r') {
1856 /* end of string, end of loop */
1857 *line = 0;
1858 break;
1859 }
1860 else if (isspace((int)*line)) {
1861 /* a non-escaped space is an argument separator */
1862 *line++ = 0;
1863 while (isspace((int)*line))
1864 line++;
1865 args[++arg] = line;
1866 }
1867 else {
1868 line++;
1869 }
1870 }
1871
1872 /* empty line */
1873 if (!**args)
1874 continue;
1875
1876 /* zero out remaining args */
1877 while (++arg < MAX_LINE_ARGS) {
1878 args[arg] = line;
1879 }
1880
1881 if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */
1882 confsect = CFG_LISTEN;
1883 else if (!strcmp(args[0], "global")) /* global config */
1884 confsect = CFG_GLOBAL;
1885 /* else it's a section keyword */
1886
1887 switch (confsect) {
1888 case CFG_LISTEN:
1889 if (cfg_parse_listen(file, linenum, args) < 0)
1890 return -1;
1891 break;
1892 case CFG_GLOBAL:
1893 if (cfg_parse_global(file, linenum, args) < 0)
1894 return -1;
1895 break;
1896 default:
1897 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
1898 return -1;
1899 }
1900
1901
1902 }
1903 fclose(f);
1904
1905 /*
1906 * Now, check for the integrity of all that we have collected.
1907 */
1908
1909 /* will be needed further to delay some tasks */
1910 tv_now(&now);
1911
1912 if ((curproxy = proxy) == NULL) {
1913 Alert("parsing %s : no <listen> line. Nothing to do !\n",
1914 file);
1915 return -1;
1916 }
1917
1918 while (curproxy != NULL) {
1919 if (curproxy->state == PR_STSTOPPED) {
1920 curproxy = curproxy->next;
1921 continue;
1922 }
1923
1924 if (curproxy->listen == NULL) {
1925 Alert("parsing %s : listener %s has no listen address. Please either specify a valid address on the <listen> line, or use the <bind> keyword.\n", file, curproxy->id);
1926 cfgerr++;
1927 }
1928 else if ((curproxy->mode != PR_MODE_HEALTH) &&
1929 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
1930 (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) {
1931 Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n",
1932 file, curproxy->id);
1933 cfgerr++;
1934 }
1935 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
1936 if (curproxy->options & PR_O_TRANSP) {
1937 Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n",
1938 file, curproxy->id);
1939 cfgerr++;
1940 }
1941#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
1942 else if (curproxy->srv == NULL) {
1943 Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n",
1944 file, curproxy->id);
1945 cfgerr++;
1946 }
1947#endif
1948 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
1949 Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n",
1950 file, curproxy->id);
1951 }
1952 }
1953 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
1954 if (curproxy->cookie_name != NULL) {
1955 Warning("parsing %s : cookie will be ignored for listener %s.\n",
1956 file, curproxy->id);
1957 }
1958 if ((newsrv = curproxy->srv) != NULL) {
1959 Warning("parsing %s : servers will be ignored for listener %s.\n",
1960 file, curproxy->id);
1961 }
1962 if (curproxy->rsp_exp != NULL) {
1963 Warning("parsing %s : server regular expressions will be ignored for listener %s.\n",
1964 file, curproxy->id);
1965 }
1966 if (curproxy->req_exp != NULL) {
1967 Warning("parsing %s : client regular expressions will be ignored for listener %s.\n",
1968 file, curproxy->id);
1969 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02001970 if (curproxy->monitor_uri != NULL) {
1971 Warning("parsing %s : monitor-uri will be ignored for listener %s.\n",
1972 file, curproxy->id);
1973 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001974 }
1975 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
1976 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
1977 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
1978 file, curproxy->id);
1979 cfgerr++;
1980 }
1981 }
1982
Willy Tarreau2738a142006-07-08 17:28:09 +02001983 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
1984 (!curproxy->clitimeout || !curproxy->contimeout || !curproxy->srvtimeout)) {
1985 Warning("parsing %s : missing timeouts for listener '%s'.\n"
1986 " | While not properly invalid, you will certainly encounter various problems\n"
1987 " | with such a configuration. To fix this, please ensure that all following\n"
1988 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
1989 file, curproxy->id);
1990 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001991
1992 if (curproxy->options & PR_O_SSL3_CHK) {
1993 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
1994 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
1995 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
1996 }
1997
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998 /* first, we will invert the servers list order */
1999 newsrv = NULL;
2000 while (curproxy->srv) {
2001 struct server *next;
2002
2003 next = curproxy->srv->next;
2004 curproxy->srv->next = newsrv;
2005 newsrv = curproxy->srv;
2006 if (!next)
2007 break;
2008 curproxy->srv = next;
2009 }
2010
2011 /* now, newsrv == curproxy->srv */
2012 if (newsrv) {
2013 struct server *srv;
2014 int pgcd;
2015 int act, bck;
2016
2017 /* We will factor the weights to reduce the table,
2018 * using Euclide's largest common divisor algorithm
2019 */
2020 pgcd = newsrv->uweight + 1;
2021 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2022 int t, w;
2023
2024 w = srv->uweight + 1;
2025 while (w) {
2026 t = pgcd % w;
2027 pgcd = w;
2028 w = t;
2029 }
2030 }
2031
2032 act = bck = 0;
2033 for (srv = newsrv; srv; srv = srv->next) {
2034 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
2035 if (srv->state & SRV_BACKUP)
2036 bck += srv->eweight + 1;
2037 else
2038 act += srv->eweight + 1;
2039 }
2040
2041 /* this is the largest map we will ever need for this servers list */
2042 if (act < bck)
2043 act = bck;
2044
2045 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2046 /* recounts servers and their weights */
2047 recount_servers(curproxy);
2048 recalc_server_map(curproxy);
2049 }
2050
2051 if (curproxy->options & PR_O_LOGASAP)
2052 curproxy->to_log &= ~LW_BYTES;
2053
2054 if (curproxy->errmsg.msg400 == NULL) {
2055 curproxy->errmsg.msg400 = (char *)HTTP_400;
2056 curproxy->errmsg.len400 = strlen(HTTP_400);
2057 }
2058 if (curproxy->errmsg.msg403 == NULL) {
2059 curproxy->errmsg.msg403 = (char *)HTTP_403;
2060 curproxy->errmsg.len403 = strlen(HTTP_403);
2061 }
2062 if (curproxy->errmsg.msg408 == NULL) {
2063 curproxy->errmsg.msg408 = (char *)HTTP_408;
2064 curproxy->errmsg.len408 = strlen(HTTP_408);
2065 }
2066 if (curproxy->errmsg.msg500 == NULL) {
2067 curproxy->errmsg.msg500 = (char *)HTTP_500;
2068 curproxy->errmsg.len500 = strlen(HTTP_500);
2069 }
2070 if (curproxy->errmsg.msg502 == NULL) {
2071 curproxy->errmsg.msg502 = (char *)HTTP_502;
2072 curproxy->errmsg.len502 = strlen(HTTP_502);
2073 }
2074 if (curproxy->errmsg.msg503 == NULL) {
2075 curproxy->errmsg.msg503 = (char *)HTTP_503;
2076 curproxy->errmsg.len503 = strlen(HTTP_503);
2077 }
2078 if (curproxy->errmsg.msg504 == NULL) {
2079 curproxy->errmsg.msg504 = (char *)HTTP_504;
2080 curproxy->errmsg.len504 = strlen(HTTP_504);
2081 }
2082
2083 /*
2084 * If this server supports a maxconn parameter, it needs a dedicated
2085 * tasks to fill the emptied slots when a connection leaves.
2086 */
2087 newsrv = curproxy->srv;
2088 while (newsrv != NULL) {
2089 if (newsrv->minconn >= newsrv->maxconn) {
2090 /* Only 'minconn' was specified, or it was higher than or equal
2091 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2092 * this will avoid further useless expensive computations.
2093 */
2094 newsrv->maxconn = newsrv->minconn;
2095 newsrv->minconn = 0;
2096 }
2097
2098 if (newsrv->maxconn > 0) {
2099 struct task *t;
2100
2101 if ((t = pool_alloc(task)) == NULL) {
2102 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2103 return -1;
2104 }
2105
2106 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2107 t->wq = LIST_HEAD(wait_queue[1]); /* already assigned to the eternity queue */
2108 t->state = TASK_IDLE;
2109 t->process = process_srv_queue;
2110 t->context = newsrv;
2111 newsrv->queue_mgt = t;
2112
2113 /* never run it unless specifically woken up */
2114 tv_eternity(&t->expire);
2115 task_queue(t);
2116 }
2117 newsrv = newsrv->next;
2118 }
2119
2120 /* now we'll start this proxy's health checks if any */
2121 /* 1- count the checkers to run simultaneously */
2122 nbchk = 0;
2123 mininter = 0;
2124 newsrv = curproxy->srv;
2125 while (newsrv != NULL) {
2126 if (newsrv->state & SRV_CHECKED) {
2127 if (!mininter || mininter > newsrv->inter)
2128 mininter = newsrv->inter;
2129 nbchk++;
2130 }
2131 newsrv = newsrv->next;
2132 }
2133
2134 /* 2- start them as far as possible from each others while respecting
2135 * their own intervals. For this, we will start them after their own
2136 * interval added to the min interval divided by the number of servers,
2137 * weighted by the server's position in the list.
2138 */
2139 if (nbchk > 0) {
2140 struct task *t;
2141 int srvpos;
2142
2143 newsrv = curproxy->srv;
2144 srvpos = 0;
2145 while (newsrv != NULL) {
2146 /* should this server be checked ? */
2147 if (newsrv->state & SRV_CHECKED) {
2148 if ((t = pool_alloc(task)) == NULL) {
2149 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2150 return -1;
2151 }
2152
2153 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2154 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
2155 t->state = TASK_IDLE;
2156 t->process = process_chk;
2157 t->context = newsrv;
2158
2159 /* check this every ms */
2160 tv_delayfrom(&t->expire, &now,
2161 newsrv->inter + mininter * srvpos / nbchk);
2162 task_queue(t);
2163 //task_wakeup(&rq, t);
2164 srvpos++;
2165 }
2166 newsrv = newsrv->next;
2167 }
2168 }
2169
2170 curproxy = curproxy->next;
2171 }
2172 if (cfgerr > 0) {
2173 Alert("Errors found in configuration file, aborting.\n");
2174 return -1;
2175 }
2176 else
2177 return 0;
2178}
2179
2180
2181
2182/*
2183 * Local variables:
2184 * c-indent-level: 8
2185 * c-basic-offset: 8
2186 * End:
2187 */