blob: 836dc9aa313a8e03dd961d46d0607559c5986fae [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 }
1351 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
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 || *(args[2]) == 0) {
1359 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1360 file, linenum, args[0]);
1361 return -1;
1362 }
1363
1364 preg = calloc(1, sizeof(regex_t));
1365 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1366 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1367 return -1;
1368 }
1369
1370 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1371 if (err) {
1372 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1373 file, linenum, *err);
1374 return -1;
1375 }
1376 }
1377 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1378 regex_t *preg;
1379 if (curproxy == &defproxy) {
1380 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1381 return -1;
1382 }
1383
1384 if (*(args[1]) == 0) {
1385 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1386 return -1;
1387 }
1388
1389 preg = calloc(1, sizeof(regex_t));
1390 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1391 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1392 return -1;
1393 }
1394
1395 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1396 }
1397 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this 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_DENY, NULL);
1416 }
1417 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
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_PASS, NULL);
1436 }
1437 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
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_ALLOW, NULL);
1456 }
1457 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1458 if (curproxy == &defproxy) {
1459 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1460 return -1;
1461 }
1462
1463 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1464 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1465 return 0;
1466 }
1467
1468 if (*(args[1]) == 0) {
1469 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1470 return -1;
1471 }
1472
1473 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1474 }
1475 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1476 regex_t *preg;
1477
1478 if (*(args[1]) == 0 || *(args[2]) == 0) {
1479 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1480 file, linenum, args[0]);
1481 return -1;
1482 }
1483
1484 preg = calloc(1, sizeof(regex_t));
1485 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1486 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1487 return -1;
1488 }
1489
1490 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1491 if (err) {
1492 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1493 file, linenum, *err);
1494 return -1;
1495 }
1496 }
1497 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1498 regex_t *preg;
1499 if (curproxy == &defproxy) {
1500 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1501 return -1;
1502 }
1503
1504 if (*(args[1]) == 0) {
1505 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1506 return -1;
1507 }
1508
1509 preg = calloc(1, sizeof(regex_t));
1510 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1511 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1512 return -1;
1513 }
1514
1515 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1516 if (err) {
1517 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1518 file, linenum, *err);
1519 return -1;
1520 }
1521 }
1522 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
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 }
1528
1529 if (*(args[1]) == 0) {
1530 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1531 return -1;
1532 }
1533
1534 preg = calloc(1, sizeof(regex_t));
1535 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1536 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1537 return -1;
1538 }
1539
1540 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1541 if (err) {
1542 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1543 file, linenum, *err);
1544 return -1;
1545 }
1546 }
1547 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1548 regex_t *preg;
1549 if (curproxy == &defproxy) {
1550 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1551 return -1;
1552 }
1553
1554 if (*(args[1]) == 0 || *(args[2]) == 0) {
1555 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1556 file, linenum, args[0]);
1557 return -1;
1558 }
1559
1560 preg = calloc(1, sizeof(regex_t));
1561 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1562 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1563 return -1;
1564 }
1565
1566 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1567 if (err) {
1568 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1569 file, linenum, *err);
1570 return -1;
1571 }
1572 }
1573 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1574 regex_t *preg;
1575 if (curproxy == &defproxy) {
1576 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1577 return -1;
1578 }
1579
1580 if (*(args[1]) == 0) {
1581 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1582 return -1;
1583 }
1584
1585 preg = calloc(1, sizeof(regex_t));
1586 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1587 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1588 return -1;
1589 }
1590
1591 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1592 if (err) {
1593 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1594 file, linenum, *err);
1595 return -1;
1596 }
1597 }
1598 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1599 regex_t *preg;
1600 if (curproxy == &defproxy) {
1601 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1602 return -1;
1603 }
1604
1605 if (*(args[1]) == 0) {
1606 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1607 return -1;
1608 }
1609
1610 preg = calloc(1, sizeof(regex_t));
1611 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1612 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1613 return -1;
1614 }
1615
1616 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1617 if (err) {
1618 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1619 file, linenum, *err);
1620 return -1;
1621 }
1622 }
1623 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1624 if (curproxy == &defproxy) {
1625 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1626 return -1;
1627 }
1628
1629 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1630 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1631 return 0;
1632 }
1633
1634 if (*(args[1]) == 0) {
1635 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1636 return -1;
1637 }
1638
1639 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1640 }
1641 else if (!strcmp(args[0], "errorloc") ||
1642 !strcmp(args[0], "errorloc302") ||
1643 !strcmp(args[0], "errorloc303")) { /* error location */
1644 int errnum, errlen;
1645 char *err;
1646
1647 // if (curproxy == &defproxy) {
1648 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1649 // return -1;
1650 // }
1651
1652 if (*(args[2]) == 0) {
1653 Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum);
1654 return -1;
1655 }
1656
1657 errnum = atol(args[1]);
1658 if (!strcmp(args[0], "errorloc303")) {
1659 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1660 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1661 } else {
1662 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1663 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
1664 }
1665
1666 if (errnum == 400) {
1667 if (curproxy->errmsg.msg400) {
1668 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1669 free(curproxy->errmsg.msg400);
1670 }
1671 curproxy->errmsg.msg400 = err;
1672 curproxy->errmsg.len400 = errlen;
1673 }
1674 else if (errnum == 403) {
1675 if (curproxy->errmsg.msg403) {
1676 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1677 free(curproxy->errmsg.msg403);
1678 }
1679 curproxy->errmsg.msg403 = err;
1680 curproxy->errmsg.len403 = errlen;
1681 }
1682 else if (errnum == 408) {
1683 if (curproxy->errmsg.msg408) {
1684 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1685 free(curproxy->errmsg.msg408);
1686 }
1687 curproxy->errmsg.msg408 = err;
1688 curproxy->errmsg.len408 = errlen;
1689 }
1690 else if (errnum == 500) {
1691 if (curproxy->errmsg.msg500) {
1692 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1693 free(curproxy->errmsg.msg500);
1694 }
1695 curproxy->errmsg.msg500 = err;
1696 curproxy->errmsg.len500 = errlen;
1697 }
1698 else if (errnum == 502) {
1699 if (curproxy->errmsg.msg502) {
1700 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1701 free(curproxy->errmsg.msg502);
1702 }
1703 curproxy->errmsg.msg502 = err;
1704 curproxy->errmsg.len502 = errlen;
1705 }
1706 else if (errnum == 503) {
1707 if (curproxy->errmsg.msg503) {
1708 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1709 free(curproxy->errmsg.msg503);
1710 }
1711 curproxy->errmsg.msg503 = err;
1712 curproxy->errmsg.len503 = errlen;
1713 }
1714 else if (errnum == 504) {
1715 if (curproxy->errmsg.msg504) {
1716 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1717 free(curproxy->errmsg.msg504);
1718 }
1719 curproxy->errmsg.msg504 = err;
1720 curproxy->errmsg.len504 = errlen;
1721 }
1722 else {
1723 Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum);
1724 free(err);
1725 }
1726 }
1727 else {
1728 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
1729 return -1;
1730 }
1731 return 0;
1732}
1733
1734
1735/*
1736 * This function reads and parses the configuration file given in the argument.
1737 * returns 0 if OK, -1 if error.
1738 */
1739int readcfgfile(char *file)
1740{
1741 char thisline[256];
1742 char *line;
1743 FILE *f;
1744 int linenum = 0;
1745 char *end;
1746 char *args[MAX_LINE_ARGS];
1747 int arg;
1748 int cfgerr = 0;
1749 int nbchk, mininter;
1750 int confsect = CFG_NONE;
1751
1752 struct proxy *curproxy = NULL;
1753 struct server *newsrv = NULL;
1754
1755 if ((f=fopen(file,"r")) == NULL)
1756 return -1;
1757
1758 init_default_instance();
1759
1760 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
1761 linenum++;
1762
1763 end = line + strlen(line);
1764
1765 /* skip leading spaces */
1766 while (isspace((int)*line))
1767 line++;
1768
1769 arg = 0;
1770 args[arg] = line;
1771
1772 while (*line && arg < MAX_LINE_ARGS) {
1773 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1774 * C equivalent value. Other combinations left unchanged (eg: \1).
1775 */
1776 if (*line == '\\') {
1777 int skip = 0;
1778 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1779 *line = line[1];
1780 skip = 1;
1781 }
1782 else if (line[1] == 'r') {
1783 *line = '\r';
1784 skip = 1;
1785 }
1786 else if (line[1] == 'n') {
1787 *line = '\n';
1788 skip = 1;
1789 }
1790 else if (line[1] == 't') {
1791 *line = '\t';
1792 skip = 1;
1793 }
1794 else if (line[1] == 'x') {
1795 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
1796 unsigned char hex1, hex2;
1797 hex1 = toupper(line[2]) - '0';
1798 hex2 = toupper(line[3]) - '0';
1799 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1800 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1801 *line = (hex1<<4) + hex2;
1802 skip = 3;
1803 }
1804 else {
1805 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
1806 return -1;
1807 }
1808 }
1809 if (skip) {
1810 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
1811 end -= skip;
1812 }
1813 line++;
1814 }
1815 else if (*line == '#' || *line == '\n' || *line == '\r') {
1816 /* end of string, end of loop */
1817 *line = 0;
1818 break;
1819 }
1820 else if (isspace((int)*line)) {
1821 /* a non-escaped space is an argument separator */
1822 *line++ = 0;
1823 while (isspace((int)*line))
1824 line++;
1825 args[++arg] = line;
1826 }
1827 else {
1828 line++;
1829 }
1830 }
1831
1832 /* empty line */
1833 if (!**args)
1834 continue;
1835
1836 /* zero out remaining args */
1837 while (++arg < MAX_LINE_ARGS) {
1838 args[arg] = line;
1839 }
1840
1841 if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */
1842 confsect = CFG_LISTEN;
1843 else if (!strcmp(args[0], "global")) /* global config */
1844 confsect = CFG_GLOBAL;
1845 /* else it's a section keyword */
1846
1847 switch (confsect) {
1848 case CFG_LISTEN:
1849 if (cfg_parse_listen(file, linenum, args) < 0)
1850 return -1;
1851 break;
1852 case CFG_GLOBAL:
1853 if (cfg_parse_global(file, linenum, args) < 0)
1854 return -1;
1855 break;
1856 default:
1857 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
1858 return -1;
1859 }
1860
1861
1862 }
1863 fclose(f);
1864
1865 /*
1866 * Now, check for the integrity of all that we have collected.
1867 */
1868
1869 /* will be needed further to delay some tasks */
1870 tv_now(&now);
1871
1872 if ((curproxy = proxy) == NULL) {
1873 Alert("parsing %s : no <listen> line. Nothing to do !\n",
1874 file);
1875 return -1;
1876 }
1877
1878 while (curproxy != NULL) {
1879 if (curproxy->state == PR_STSTOPPED) {
1880 curproxy = curproxy->next;
1881 continue;
1882 }
1883
1884 if (curproxy->listen == NULL) {
1885 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);
1886 cfgerr++;
1887 }
1888 else if ((curproxy->mode != PR_MODE_HEALTH) &&
1889 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
1890 (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) {
1891 Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n",
1892 file, curproxy->id);
1893 cfgerr++;
1894 }
1895 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
1896 if (curproxy->options & PR_O_TRANSP) {
1897 Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n",
1898 file, curproxy->id);
1899 cfgerr++;
1900 }
1901#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
1902 else if (curproxy->srv == NULL) {
1903 Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n",
1904 file, curproxy->id);
1905 cfgerr++;
1906 }
1907#endif
1908 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
1909 Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n",
1910 file, curproxy->id);
1911 }
1912 }
1913 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
1914 if (curproxy->cookie_name != NULL) {
1915 Warning("parsing %s : cookie will be ignored for listener %s.\n",
1916 file, curproxy->id);
1917 }
1918 if ((newsrv = curproxy->srv) != NULL) {
1919 Warning("parsing %s : servers will be ignored for listener %s.\n",
1920 file, curproxy->id);
1921 }
1922 if (curproxy->rsp_exp != NULL) {
1923 Warning("parsing %s : server regular expressions will be ignored for listener %s.\n",
1924 file, curproxy->id);
1925 }
1926 if (curproxy->req_exp != NULL) {
1927 Warning("parsing %s : client regular expressions will be ignored for listener %s.\n",
1928 file, curproxy->id);
1929 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02001930 if (curproxy->monitor_uri != NULL) {
1931 Warning("parsing %s : monitor-uri will be ignored for listener %s.\n",
1932 file, curproxy->id);
1933 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001934 }
1935 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
1936 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
1937 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
1938 file, curproxy->id);
1939 cfgerr++;
1940 }
1941 }
1942
Willy Tarreau2738a142006-07-08 17:28:09 +02001943 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
1944 (!curproxy->clitimeout || !curproxy->contimeout || !curproxy->srvtimeout)) {
1945 Warning("parsing %s : missing timeouts for listener '%s'.\n"
1946 " | While not properly invalid, you will certainly encounter various problems\n"
1947 " | with such a configuration. To fix this, please ensure that all following\n"
1948 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
1949 file, curproxy->id);
1950 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001951
1952 if (curproxy->options & PR_O_SSL3_CHK) {
1953 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
1954 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
1955 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
1956 }
1957
Willy Tarreaubaaee002006-06-26 02:48:02 +02001958 /* first, we will invert the servers list order */
1959 newsrv = NULL;
1960 while (curproxy->srv) {
1961 struct server *next;
1962
1963 next = curproxy->srv->next;
1964 curproxy->srv->next = newsrv;
1965 newsrv = curproxy->srv;
1966 if (!next)
1967 break;
1968 curproxy->srv = next;
1969 }
1970
1971 /* now, newsrv == curproxy->srv */
1972 if (newsrv) {
1973 struct server *srv;
1974 int pgcd;
1975 int act, bck;
1976
1977 /* We will factor the weights to reduce the table,
1978 * using Euclide's largest common divisor algorithm
1979 */
1980 pgcd = newsrv->uweight + 1;
1981 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
1982 int t, w;
1983
1984 w = srv->uweight + 1;
1985 while (w) {
1986 t = pgcd % w;
1987 pgcd = w;
1988 w = t;
1989 }
1990 }
1991
1992 act = bck = 0;
1993 for (srv = newsrv; srv; srv = srv->next) {
1994 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
1995 if (srv->state & SRV_BACKUP)
1996 bck += srv->eweight + 1;
1997 else
1998 act += srv->eweight + 1;
1999 }
2000
2001 /* this is the largest map we will ever need for this servers list */
2002 if (act < bck)
2003 act = bck;
2004
2005 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2006 /* recounts servers and their weights */
2007 recount_servers(curproxy);
2008 recalc_server_map(curproxy);
2009 }
2010
2011 if (curproxy->options & PR_O_LOGASAP)
2012 curproxy->to_log &= ~LW_BYTES;
2013
2014 if (curproxy->errmsg.msg400 == NULL) {
2015 curproxy->errmsg.msg400 = (char *)HTTP_400;
2016 curproxy->errmsg.len400 = strlen(HTTP_400);
2017 }
2018 if (curproxy->errmsg.msg403 == NULL) {
2019 curproxy->errmsg.msg403 = (char *)HTTP_403;
2020 curproxy->errmsg.len403 = strlen(HTTP_403);
2021 }
2022 if (curproxy->errmsg.msg408 == NULL) {
2023 curproxy->errmsg.msg408 = (char *)HTTP_408;
2024 curproxy->errmsg.len408 = strlen(HTTP_408);
2025 }
2026 if (curproxy->errmsg.msg500 == NULL) {
2027 curproxy->errmsg.msg500 = (char *)HTTP_500;
2028 curproxy->errmsg.len500 = strlen(HTTP_500);
2029 }
2030 if (curproxy->errmsg.msg502 == NULL) {
2031 curproxy->errmsg.msg502 = (char *)HTTP_502;
2032 curproxy->errmsg.len502 = strlen(HTTP_502);
2033 }
2034 if (curproxy->errmsg.msg503 == NULL) {
2035 curproxy->errmsg.msg503 = (char *)HTTP_503;
2036 curproxy->errmsg.len503 = strlen(HTTP_503);
2037 }
2038 if (curproxy->errmsg.msg504 == NULL) {
2039 curproxy->errmsg.msg504 = (char *)HTTP_504;
2040 curproxy->errmsg.len504 = strlen(HTTP_504);
2041 }
2042
2043 /*
2044 * If this server supports a maxconn parameter, it needs a dedicated
2045 * tasks to fill the emptied slots when a connection leaves.
2046 */
2047 newsrv = curproxy->srv;
2048 while (newsrv != NULL) {
2049 if (newsrv->minconn >= newsrv->maxconn) {
2050 /* Only 'minconn' was specified, or it was higher than or equal
2051 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2052 * this will avoid further useless expensive computations.
2053 */
2054 newsrv->maxconn = newsrv->minconn;
2055 newsrv->minconn = 0;
2056 }
2057
2058 if (newsrv->maxconn > 0) {
2059 struct task *t;
2060
2061 if ((t = pool_alloc(task)) == NULL) {
2062 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2063 return -1;
2064 }
2065
2066 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2067 t->wq = LIST_HEAD(wait_queue[1]); /* already assigned to the eternity queue */
2068 t->state = TASK_IDLE;
2069 t->process = process_srv_queue;
2070 t->context = newsrv;
2071 newsrv->queue_mgt = t;
2072
2073 /* never run it unless specifically woken up */
2074 tv_eternity(&t->expire);
2075 task_queue(t);
2076 }
2077 newsrv = newsrv->next;
2078 }
2079
2080 /* now we'll start this proxy's health checks if any */
2081 /* 1- count the checkers to run simultaneously */
2082 nbchk = 0;
2083 mininter = 0;
2084 newsrv = curproxy->srv;
2085 while (newsrv != NULL) {
2086 if (newsrv->state & SRV_CHECKED) {
2087 if (!mininter || mininter > newsrv->inter)
2088 mininter = newsrv->inter;
2089 nbchk++;
2090 }
2091 newsrv = newsrv->next;
2092 }
2093
2094 /* 2- start them as far as possible from each others while respecting
2095 * their own intervals. For this, we will start them after their own
2096 * interval added to the min interval divided by the number of servers,
2097 * weighted by the server's position in the list.
2098 */
2099 if (nbchk > 0) {
2100 struct task *t;
2101 int srvpos;
2102
2103 newsrv = curproxy->srv;
2104 srvpos = 0;
2105 while (newsrv != NULL) {
2106 /* should this server be checked ? */
2107 if (newsrv->state & SRV_CHECKED) {
2108 if ((t = pool_alloc(task)) == NULL) {
2109 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2110 return -1;
2111 }
2112
2113 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2114 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
2115 t->state = TASK_IDLE;
2116 t->process = process_chk;
2117 t->context = newsrv;
2118
2119 /* check this every ms */
2120 tv_delayfrom(&t->expire, &now,
2121 newsrv->inter + mininter * srvpos / nbchk);
2122 task_queue(t);
2123 //task_wakeup(&rq, t);
2124 srvpos++;
2125 }
2126 newsrv = newsrv->next;
2127 }
2128 }
2129
2130 curproxy = curproxy->next;
2131 }
2132 if (cfgerr > 0) {
2133 Alert("Errors found in configuration file, aborting.\n");
2134 return -1;
2135 }
2136 else
2137 return 0;
2138}
2139
2140
2141
2142/*
2143 * Local variables:
2144 * c-indent-level: 8
2145 * c-basic-offset: 8
2146 * End:
2147 */