blob: 5e4aa51ee036ce95b446f6dc205aa5b424a6ae0d [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;
524 return 0;
525 }
526 else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
527 /* some variables may have already been initialized earlier */
528 if (defproxy.check_req) free(defproxy.check_req);
529 if (defproxy.cookie_name) free(defproxy.cookie_name);
530 if (defproxy.capture_name) free(defproxy.capture_name);
531 if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400);
532 if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403);
533 if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408);
534 if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500);
535 if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502);
536 if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503);
537 if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504);
538 /* we cannot free uri_auth because it might already be used */
539 init_default_instance();
540 curproxy = &defproxy;
541 return 0;
542 }
543 else if (curproxy == NULL) {
544 Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
545 return -1;
546 }
547
548 if (!strcmp(args[0], "bind")) { /* new listen addresses */
549 if (curproxy == &defproxy) {
550 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
551 return -1;
552 }
553
554 if (strchr(args[1], ':') == NULL) {
555 Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
556 file, linenum, args[0]);
557 return -1;
558 }
559 curproxy->listen = str2listener(args[1], curproxy->listen);
560 if (!curproxy->listen)
561 return -1;
562 global.maxsock++;
563 return 0;
564 }
565 else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */
566 if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) {
567 Alert("parsing [%s:%d] : '%s' expects address[/mask].\n",
568 file, linenum, args[0]);
569 return -1;
570 }
571 /* flush useless bits */
572 curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr;
573 return 0;
574 }
575 else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
576 if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
577 else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
578 else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
579 else {
580 Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
581 return -1;
582 }
583 }
584 else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
585 curproxy->state = PR_STSTOPPED;
586 }
587 else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
588 curproxy->state = PR_STNEW;
589 }
590 else if (!strcmp(args[0], "cookie")) { /* cookie name */
591 int cur_arg;
592 // if (curproxy == &defproxy) {
593 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
594 // return -1;
595 // }
596
597 if (curproxy->cookie_name != NULL) {
598 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
599 // file, linenum);
600 // return 0;
601 free(curproxy->cookie_name);
602 }
603
604 if (*(args[1]) == 0) {
605 Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
606 file, linenum, args[0]);
607 return -1;
608 }
609 curproxy->cookie_name = strdup(args[1]);
610 curproxy->cookie_len = strlen(curproxy->cookie_name);
611
612 cur_arg = 2;
613 while (*(args[cur_arg])) {
614 if (!strcmp(args[cur_arg], "rewrite")) {
615 curproxy->options |= PR_O_COOK_RW;
616 }
617 else if (!strcmp(args[cur_arg], "indirect")) {
618 curproxy->options |= PR_O_COOK_IND;
619 }
620 else if (!strcmp(args[cur_arg], "insert")) {
621 curproxy->options |= PR_O_COOK_INS;
622 }
623 else if (!strcmp(args[cur_arg], "nocache")) {
624 curproxy->options |= PR_O_COOK_NOC;
625 }
626 else if (!strcmp(args[cur_arg], "postonly")) {
627 curproxy->options |= PR_O_COOK_POST;
628 }
629 else if (!strcmp(args[cur_arg], "prefix")) {
630 curproxy->options |= PR_O_COOK_PFX;
631 }
632 else {
633 Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n",
634 file, linenum, args[0]);
635 return -1;
636 }
637 cur_arg++;
638 }
639 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) {
640 Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n",
641 file, linenum);
642 return -1;
643 }
644
645 if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) {
646 Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n",
647 file, linenum);
648 return -1;
649 }
650 }/* end else if (!strcmp(args[0], "cookie")) */
651 else if (!strcmp(args[0], "appsession")) { /* cookie name */
652 // if (curproxy == &defproxy) {
653 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
654 // return -1;
655 // }
656
657 if (curproxy->appsession_name != NULL) {
658 // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
659 // file, linenum);
660 // return 0;
661 free(curproxy->appsession_name);
662 }
663
664 if (*(args[5]) == 0) {
665 Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
666 file, linenum, args[0]);
667 return -1;
668 }
669 have_appsession = 1;
670 curproxy->appsession_name = strdup(args[1]);
671 curproxy->appsession_name_len = strlen(curproxy->appsession_name);
672 curproxy->appsession_len = atoi(args[3]);
673 curproxy->appsession_timeout = atoi(args[5]);
674 rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy);
675 if (rc) {
676 Alert("Error Init Appsession Hashtable.\n");
677 return -1;
678 }
679 } /* Url App Session */
680 else if (!strcmp(args[0], "capture")) {
681 if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
682 // if (curproxy == &defproxy) {
683 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
684 // return -1;
685 // }
686
687 if (curproxy->capture_name != NULL) {
688 // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
689 // file, linenum, args[0]);
690 // return 0;
691 free(curproxy->capture_name);
692 }
693
694 if (*(args[4]) == 0) {
695 Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
696 file, linenum, args[0]);
697 return -1;
698 }
699 curproxy->capture_name = strdup(args[2]);
700 curproxy->capture_namelen = strlen(curproxy->capture_name);
701 curproxy->capture_len = atol(args[4]);
702 if (curproxy->capture_len >= CAPTURE_LEN) {
703 Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n",
704 file, linenum, CAPTURE_LEN - 1);
705 curproxy->capture_len = CAPTURE_LEN - 1;
706 }
707 curproxy->to_log |= LW_COOKIE;
708 }
709 else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) {
710 struct cap_hdr *hdr;
711
712 if (curproxy == &defproxy) {
713 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
714 return -1;
715 }
716
717 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
718 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
719 file, linenum, args[0], args[1]);
720 return -1;
721 }
722
723 hdr = calloc(sizeof(struct cap_hdr), 1);
724 hdr->next = curproxy->req_cap;
725 hdr->name = strdup(args[3]);
726 hdr->namelen = strlen(args[3]);
727 hdr->len = atol(args[5]);
728 hdr->index = curproxy->nb_req_cap++;
729 curproxy->req_cap = hdr;
730 curproxy->to_log |= LW_REQHDR;
731 }
732 else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) {
733 struct cap_hdr *hdr;
734
735 if (curproxy == &defproxy) {
736 Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]);
737 return -1;
738 }
739
740 if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) {
741 Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n",
742 file, linenum, args[0], args[1]);
743 return -1;
744 }
745 hdr = calloc(sizeof(struct cap_hdr), 1);
746 hdr->next = curproxy->rsp_cap;
747 hdr->name = strdup(args[3]);
748 hdr->namelen = strlen(args[3]);
749 hdr->len = atol(args[5]);
750 hdr->index = curproxy->nb_rsp_cap++;
751 curproxy->rsp_cap = hdr;
752 curproxy->to_log |= LW_RSPHDR;
753 }
754 else {
755 Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n",
756 file, linenum, args[0]);
757 return -1;
758 }
759 }
760 else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
761 if (curproxy->contimeout != defproxy.contimeout) {
762 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
763 return 0;
764 }
765 if (*(args[1]) == 0) {
766 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
767 file, linenum, args[0]);
768 return -1;
769 }
770 curproxy->contimeout = atol(args[1]);
771 }
772 else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
773 if (curproxy->clitimeout != defproxy.clitimeout) {
774 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
775 file, linenum, args[0]);
776 return 0;
777 }
778 if (*(args[1]) == 0) {
779 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
780 file, linenum, args[0]);
781 return -1;
782 }
783 curproxy->clitimeout = atol(args[1]);
784 }
785 else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
786 if (curproxy->srvtimeout != defproxy.srvtimeout) {
787 Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
788 return 0;
789 }
790 if (*(args[1]) == 0) {
791 Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n",
792 file, linenum, args[0]);
793 return -1;
794 }
795 curproxy->srvtimeout = atol(args[1]);
796 }
797 else if (!strcmp(args[0], "retries")) { /* connection retries */
798 if (*(args[1]) == 0) {
799 Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n",
800 file, linenum, args[0]);
801 return -1;
802 }
803 curproxy->conn_retries = atol(args[1]);
804 }
805 else if (!strcmp(args[0], "stats")) {
806 if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth)
807 curproxy->uri_auth = NULL; /* we must detach from the default config */
808
809 if (*(args[1]) == 0) {
810 Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]);
811 return -1;
812 } else if (!strcmp(args[1], "uri")) {
813 if (*(args[2]) == 0) {
814 Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum);
815 return -1;
816 } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) {
817 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
818 return -1;
819 }
820 } else if (!strcmp(args[1], "realm")) {
821 if (*(args[2]) == 0) {
822 Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum);
823 return -1;
824 } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) {
825 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
826 return -1;
827 }
828 } else if (!strcmp(args[1], "auth")) {
829 if (*(args[2]) == 0) {
830 Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum);
831 return -1;
832 } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) {
833 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
834 return -1;
835 }
836 } else if (!strcmp(args[1], "scope")) {
837 if (*(args[2]) == 0) {
838 Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum);
839 return -1;
840 } else if (!stats_add_scope(&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], "enable")) {
845 if (!stats_check_init_uri_auth(&curproxy->uri_auth)) {
846 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
847 return -1;
848 }
849 } else {
850 Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n",
851 file, linenum, args[0]);
852 return -1;
853 }
854 }
855 else if (!strcmp(args[0], "option")) {
856 if (*(args[1]) == 0) {
857 Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]);
858 return -1;
859 }
860 if (!strcmp(args[1], "redispatch"))
861 /* enable reconnections to dispatch */
862 curproxy->options |= PR_O_REDISP;
863#ifdef TPROXY
864 else if (!strcmp(args[1], "transparent"))
865 /* enable transparent proxy connections */
866 curproxy->options |= PR_O_TRANSP;
867#endif
868 else if (!strcmp(args[1], "keepalive"))
869 /* enable keep-alive */
870 curproxy->options |= PR_O_KEEPALIVE;
871 else if (!strcmp(args[1], "forwardfor"))
872 /* insert x-forwarded-for field */
873 curproxy->options |= PR_O_FWDFOR;
874 else if (!strcmp(args[1], "logasap"))
875 /* log as soon as possible, without waiting for the session to complete */
876 curproxy->options |= PR_O_LOGASAP;
877 else if (!strcmp(args[1], "abortonclose"))
878 /* abort connection if client closes during queue or connect() */
879 curproxy->options |= PR_O_ABRT_CLOSE;
880 else if (!strcmp(args[1], "httpclose"))
881 /* force connection: close in both directions in HTTP mode */
882 curproxy->options |= PR_O_HTTP_CLOSE;
883 else if (!strcmp(args[1], "forceclose"))
884 /* force connection: close in both directions in HTTP mode and enforce end of session */
885 curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE;
886 else if (!strcmp(args[1], "checkcache"))
887 /* require examination of cacheability of the 'set-cookie' field */
888 curproxy->options |= PR_O_CHK_CACHE;
889 else if (!strcmp(args[1], "httplog"))
890 /* generate a complete HTTP log */
891 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES;
892 else if (!strcmp(args[1], "tcplog"))
893 /* generate a detailed TCP log */
894 curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES;
895 else if (!strcmp(args[1], "dontlognull")) {
896 /* don't log empty requests */
897 curproxy->options |= PR_O_NULLNOLOG;
898 }
899 else if (!strcmp(args[1], "tcpka")) {
900 /* enable TCP keep-alives on client and server sessions */
901 curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA;
902 }
903 else if (!strcmp(args[1], "clitcpka")) {
904 /* enable TCP keep-alives on client sessions */
905 curproxy->options |= PR_O_TCP_CLI_KA;
906 }
907 else if (!strcmp(args[1], "srvtcpka")) {
908 /* enable TCP keep-alives on server sessions */
909 curproxy->options |= PR_O_TCP_SRV_KA;
910 }
911 else if (!strcmp(args[1], "allbackups")) {
912 /* Use all backup servers simultaneously */
913 curproxy->options |= PR_O_USE_ALL_BK;
914 }
915 else if (!strcmp(args[1], "httpchk")) {
916 /* use HTTP request to check servers' health */
917 if (curproxy->check_req != NULL) {
918 free(curproxy->check_req);
919 }
920 curproxy->options |= PR_O_HTTP_CHK;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200921 curproxy->options &= ~PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 if (!*args[2]) { /* no argument */
923 curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */
924 curproxy->check_len = strlen(DEF_CHECK_REQ);
925 } else if (!*args[3]) { /* one argument : URI */
926 int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n");
927 curproxy->check_req = (char *)malloc(reqlen);
928 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
929 "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */
930 } else { /* more arguments : METHOD URI [HTTP_VER] */
931 int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n");
932 if (*args[4])
933 reqlen += strlen(args[4]);
934 else
935 reqlen += strlen("HTTP/1.0");
936
937 curproxy->check_req = (char *)malloc(reqlen);
938 curproxy->check_len = snprintf(curproxy->check_req, reqlen,
939 "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0");
940 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200941 }
942 else if (!strcmp(args[1], "ssl-hello-chk")) {
943 /* use SSLv3 CLIENT HELLO to check servers' health */
944 if (curproxy->check_req != NULL) {
945 free(curproxy->check_req);
946 }
947 curproxy->options &= ~PR_O_HTTP_CHK;
948 curproxy->options |= PR_O_SSL3_CHK;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949 }
950 else if (!strcmp(args[1], "persist")) {
951 /* persist on using the server specified by the cookie, even when it's down */
952 curproxy->options |= PR_O_PERSIST;
953 }
954 else {
955 Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
956 return -1;
957 }
958 return 0;
959 }
960 else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
961 /* enable reconnections to dispatch */
962 curproxy->options |= PR_O_REDISP;
963 }
964#ifdef TPROXY
965 else if (!strcmp(args[0], "transparent")) {
966 /* enable transparent proxy connections */
967 curproxy->options |= PR_O_TRANSP;
968 }
969#endif
970 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
971 if (*(args[1]) == 0) {
972 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
973 return -1;
974 }
975 curproxy->maxconn = atol(args[1]);
976 }
977 else if (!strcmp(args[0], "grace")) { /* grace time (ms) */
978 if (*(args[1]) == 0) {
979 Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]);
980 return -1;
981 }
982 curproxy->grace = atol(args[1]);
983 }
984 else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
985 if (curproxy == &defproxy) {
986 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
987 return -1;
988 }
989 if (strchr(args[1], ':') == NULL) {
990 Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
991 return -1;
992 }
993 curproxy->dispatch_addr = *str2sa(args[1]);
994 }
995 else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */
996 if (*(args[1])) {
997 if (!strcmp(args[1], "roundrobin")) {
998 curproxy->options |= PR_O_BALANCE_RR;
999 }
1000 else if (!strcmp(args[1], "source")) {
1001 curproxy->options |= PR_O_BALANCE_SH;
1002 }
1003 else {
1004 Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]);
1005 return -1;
1006 }
1007 }
1008 else /* if no option is set, use round-robin by default */
1009 curproxy->options |= PR_O_BALANCE_RR;
1010 }
1011 else if (!strcmp(args[0], "server")) { /* server address */
1012 int cur_arg;
1013 char *rport;
1014 char *raddr;
1015 short realport;
1016 int do_check;
1017
1018 if (curproxy == &defproxy) {
1019 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1020 return -1;
1021 }
1022
1023 if (!*args[2]) {
1024 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1025 file, linenum, args[0]);
1026 return -1;
1027 }
1028 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
1029 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1030 return -1;
1031 }
1032
1033 /* the servers are linked backwards first */
1034 newsrv->next = curproxy->srv;
1035 curproxy->srv = newsrv;
1036 newsrv->proxy = curproxy;
1037
1038 LIST_INIT(&newsrv->pendconns);
1039 do_check = 0;
1040 newsrv->state = SRV_RUNNING; /* early server setup */
1041 newsrv->id = strdup(args[1]);
1042
1043 /* several ways to check the port component :
1044 * - IP => port=+0, relative
1045 * - IP: => port=+0, relative
1046 * - IP:N => port=N, absolute
1047 * - IP:+N => port=+N, relative
1048 * - IP:-N => port=-N, relative
1049 */
1050 raddr = strdup(args[2]);
1051 rport = strchr(raddr, ':');
1052 if (rport) {
1053 *rport++ = 0;
1054 realport = atol(rport);
1055 if (!isdigit((int)*rport))
1056 newsrv->state |= SRV_MAPPORTS;
1057 } else {
1058 realport = 0;
1059 newsrv->state |= SRV_MAPPORTS;
1060 }
1061
1062 newsrv->addr = *str2sa(raddr);
1063 newsrv->addr.sin_port = htons(realport);
1064 free(raddr);
1065
1066 newsrv->curfd = -1; /* no health-check in progress */
1067 newsrv->inter = DEF_CHKINTR;
1068 newsrv->rise = DEF_RISETIME;
1069 newsrv->fall = DEF_FALLTIME;
1070 newsrv->health = newsrv->rise; /* up, but will fall down at first failure */
1071 cur_arg = 3;
1072 while (*args[cur_arg]) {
1073 if (!strcmp(args[cur_arg], "cookie")) {
1074 newsrv->cookie = strdup(args[cur_arg + 1]);
1075 newsrv->cklen = strlen(args[cur_arg + 1]);
1076 cur_arg += 2;
1077 }
1078 else if (!strcmp(args[cur_arg], "rise")) {
1079 newsrv->rise = atol(args[cur_arg + 1]);
1080 newsrv->health = newsrv->rise;
1081 cur_arg += 2;
1082 }
1083 else if (!strcmp(args[cur_arg], "fall")) {
1084 newsrv->fall = atol(args[cur_arg + 1]);
1085 cur_arg += 2;
1086 }
1087 else if (!strcmp(args[cur_arg], "inter")) {
1088 newsrv->inter = atol(args[cur_arg + 1]);
1089 cur_arg += 2;
1090 }
1091 else if (!strcmp(args[cur_arg], "port")) {
1092 newsrv->check_port = atol(args[cur_arg + 1]);
1093 cur_arg += 2;
1094 }
1095 else if (!strcmp(args[cur_arg], "backup")) {
1096 newsrv->state |= SRV_BACKUP;
1097 cur_arg ++;
1098 }
1099 else if (!strcmp(args[cur_arg], "weight")) {
1100 int w;
1101 w = atol(args[cur_arg + 1]);
1102 if (w < 1 || w > 256) {
1103 Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n",
1104 file, linenum, newsrv->id, w);
1105 return -1;
1106 }
1107 newsrv->uweight = w - 1;
1108 cur_arg += 2;
1109 }
1110 else if (!strcmp(args[cur_arg], "minconn")) {
1111 newsrv->minconn = atol(args[cur_arg + 1]);
1112 cur_arg += 2;
1113 }
1114 else if (!strcmp(args[cur_arg], "maxconn")) {
1115 newsrv->maxconn = atol(args[cur_arg + 1]);
1116 cur_arg += 2;
1117 }
1118 else if (!strcmp(args[cur_arg], "check")) {
1119 global.maxsock++;
1120 do_check = 1;
1121 cur_arg += 1;
1122 }
1123 else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1124 if (!*args[cur_arg + 1]) {
1125 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1126 file, linenum, "source");
1127 return -1;
1128 }
1129 newsrv->state |= SRV_BIND_SRC;
1130 newsrv->source_addr = *str2sa(args[cur_arg + 1]);
1131 cur_arg += 2;
1132 }
1133 else {
1134 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
1135 file, linenum, newsrv->id);
1136 return -1;
1137 }
1138 }
1139
1140 if (do_check) {
1141 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1142 newsrv->check_port = realport; /* by default */
1143 if (!newsrv->check_port) {
1144 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1145 file, linenum, newsrv->id);
1146 return -1;
1147 }
1148 newsrv->state |= SRV_CHECKED;
1149 }
1150
1151 if (newsrv->state & SRV_BACKUP)
1152 curproxy->srv_bck++;
1153 else
1154 curproxy->srv_act++;
1155 }
1156 else if (!strcmp(args[0], "log")) { /* syslog server address */
1157 struct sockaddr_in *sa;
1158 int facility;
1159
1160 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1161 curproxy->logfac1 = global.logfac1;
1162 curproxy->logsrv1 = global.logsrv1;
1163 curproxy->loglev1 = global.loglev1;
1164 curproxy->logfac2 = global.logfac2;
1165 curproxy->logsrv2 = global.logsrv2;
1166 curproxy->loglev2 = global.loglev2;
1167 }
1168 else if (*(args[1]) && *(args[2])) {
1169 int level;
1170
1171 facility = get_log_facility(args[2]);
1172 if (facility < 0) {
1173 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1174 exit(1);
1175 }
1176
1177 level = 7; /* max syslog level = debug */
1178 if (*(args[3])) {
1179 level = get_log_level(args[3]);
1180 if (level < 0) {
1181 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1182 exit(1);
1183 }
1184 }
1185
1186 sa = str2sa(args[1]);
1187 if (!sa->sin_port)
1188 sa->sin_port = htons(SYSLOG_PORT);
1189
1190 if (curproxy->logfac1 == -1) {
1191 curproxy->logsrv1 = *sa;
1192 curproxy->logfac1 = facility;
1193 curproxy->loglev1 = level;
1194 }
1195 else if (curproxy->logfac2 == -1) {
1196 curproxy->logsrv2 = *sa;
1197 curproxy->logfac2 = facility;
1198 curproxy->loglev2 = level;
1199 }
1200 else {
1201 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1202 return -1;
1203 }
1204 }
1205 else {
1206 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1207 file, linenum);
1208 return -1;
1209 }
1210 }
1211 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
1212 if (!*args[1]) {
1213 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1214 file, linenum, "source");
1215 return -1;
1216 }
1217
1218 curproxy->source_addr = *str2sa(args[1]);
1219 curproxy->options |= PR_O_BIND_SRC;
1220 }
1221 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1222 regex_t *preg;
1223 if (curproxy == &defproxy) {
1224 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1225 return -1;
1226 }
1227
1228 if (*(args[1]) == 0 || *(args[2]) == 0) {
1229 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1230 file, linenum, args[0]);
1231 return -1;
1232 }
1233
1234 preg = calloc(1, sizeof(regex_t));
1235 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1236 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1237 return -1;
1238 }
1239
1240 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1241 if (err) {
1242 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1243 file, linenum, *err);
1244 return -1;
1245 }
1246 }
1247 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1248 regex_t *preg;
1249 if (curproxy == &defproxy) {
1250 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1251 return -1;
1252 }
1253
1254 if (*(args[1]) == 0) {
1255 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1256 return -1;
1257 }
1258
1259 preg = calloc(1, sizeof(regex_t));
1260 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1261 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1262 return -1;
1263 }
1264
1265 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1266 }
1267 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1268 regex_t *preg;
1269 if (curproxy == &defproxy) {
1270 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1271 return -1;
1272 }
1273
1274 if (*(args[1]) == 0) {
1275 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1276 return -1;
1277 }
1278
1279 preg = calloc(1, sizeof(regex_t));
1280 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1281 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1282 return -1;
1283 }
1284
1285 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1286 }
1287 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1288 regex_t *preg;
1289 if (curproxy == &defproxy) {
1290 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1291 return -1;
1292 }
1293
1294 if (*(args[1]) == 0) {
1295 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1296 return -1;
1297 }
1298
1299 preg = calloc(1, sizeof(regex_t));
1300 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1301 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1302 return -1;
1303 }
1304
1305 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1306 }
1307 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1308 regex_t *preg;
1309 if (curproxy == &defproxy) {
1310 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1311 return -1;
1312 }
1313
1314 if (*(args[1]) == 0) {
1315 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1316 return -1;
1317 }
1318
1319 preg = calloc(1, sizeof(regex_t));
1320 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1321 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1322 return -1;
1323 }
1324
1325 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1326 }
1327 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1328 regex_t *preg;
1329 if (curproxy == &defproxy) {
1330 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1331 return -1;
1332 }
1333
1334 if (*(args[1]) == 0 || *(args[2]) == 0) {
1335 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1336 file, linenum, args[0]);
1337 return -1;
1338 }
1339
1340 preg = calloc(1, sizeof(regex_t));
1341 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1342 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1343 return -1;
1344 }
1345
1346 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1347 if (err) {
1348 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1349 file, linenum, *err);
1350 return -1;
1351 }
1352 }
1353 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1354 regex_t *preg;
1355 if (curproxy == &defproxy) {
1356 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1357 return -1;
1358 }
1359
1360 if (*(args[1]) == 0) {
1361 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1362 return -1;
1363 }
1364
1365 preg = calloc(1, sizeof(regex_t));
1366 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1367 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1368 return -1;
1369 }
1370
1371 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1372 }
1373 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1374 regex_t *preg;
1375 if (curproxy == &defproxy) {
1376 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1377 return -1;
1378 }
1379
1380 if (*(args[1]) == 0) {
1381 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1382 return -1;
1383 }
1384
1385 preg = calloc(1, sizeof(regex_t));
1386 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1387 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1388 return -1;
1389 }
1390
1391 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1392 }
1393 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1394 regex_t *preg;
1395 if (curproxy == &defproxy) {
1396 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1397 return -1;
1398 }
1399
1400 if (*(args[1]) == 0) {
1401 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1402 return -1;
1403 }
1404
1405 preg = calloc(1, sizeof(regex_t));
1406 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1407 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1408 return -1;
1409 }
1410
1411 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1412 }
1413 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1414 regex_t *preg;
1415 if (curproxy == &defproxy) {
1416 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1417 return -1;
1418 }
1419
1420 if (*(args[1]) == 0) {
1421 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1422 return -1;
1423 }
1424
1425 preg = calloc(1, sizeof(regex_t));
1426 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1427 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1428 return -1;
1429 }
1430
1431 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1432 }
1433 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1434 if (curproxy == &defproxy) {
1435 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1436 return -1;
1437 }
1438
1439 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1440 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1441 return 0;
1442 }
1443
1444 if (*(args[1]) == 0) {
1445 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1446 return -1;
1447 }
1448
1449 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1450 }
1451 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1452 regex_t *preg;
1453
1454 if (*(args[1]) == 0 || *(args[2]) == 0) {
1455 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1456 file, linenum, args[0]);
1457 return -1;
1458 }
1459
1460 preg = calloc(1, sizeof(regex_t));
1461 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1462 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1463 return -1;
1464 }
1465
1466 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1467 if (err) {
1468 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1469 file, linenum, *err);
1470 return -1;
1471 }
1472 }
1473 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1474 regex_t *preg;
1475 if (curproxy == &defproxy) {
1476 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1477 return -1;
1478 }
1479
1480 if (*(args[1]) == 0) {
1481 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1482 return -1;
1483 }
1484
1485 preg = calloc(1, sizeof(regex_t));
1486 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1487 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1488 return -1;
1489 }
1490
1491 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1492 if (err) {
1493 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1494 file, linenum, *err);
1495 return -1;
1496 }
1497 }
1498 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1499 regex_t *preg;
1500 if (curproxy == &defproxy) {
1501 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1502 return -1;
1503 }
1504
1505 if (*(args[1]) == 0) {
1506 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1507 return -1;
1508 }
1509
1510 preg = calloc(1, sizeof(regex_t));
1511 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1512 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1513 return -1;
1514 }
1515
1516 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1517 if (err) {
1518 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1519 file, linenum, *err);
1520 return -1;
1521 }
1522 }
1523 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1524 regex_t *preg;
1525 if (curproxy == &defproxy) {
1526 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1527 return -1;
1528 }
1529
1530 if (*(args[1]) == 0 || *(args[2]) == 0) {
1531 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1532 file, linenum, args[0]);
1533 return -1;
1534 }
1535
1536 preg = calloc(1, sizeof(regex_t));
1537 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1538 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1539 return -1;
1540 }
1541
1542 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1543 if (err) {
1544 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1545 file, linenum, *err);
1546 return -1;
1547 }
1548 }
1549 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1550 regex_t *preg;
1551 if (curproxy == &defproxy) {
1552 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1553 return -1;
1554 }
1555
1556 if (*(args[1]) == 0) {
1557 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1558 return -1;
1559 }
1560
1561 preg = calloc(1, sizeof(regex_t));
1562 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1563 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1564 return -1;
1565 }
1566
1567 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1568 if (err) {
1569 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1570 file, linenum, *err);
1571 return -1;
1572 }
1573 }
1574 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1575 regex_t *preg;
1576 if (curproxy == &defproxy) {
1577 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1578 return -1;
1579 }
1580
1581 if (*(args[1]) == 0) {
1582 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1583 return -1;
1584 }
1585
1586 preg = calloc(1, sizeof(regex_t));
1587 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1588 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1589 return -1;
1590 }
1591
1592 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1593 if (err) {
1594 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1595 file, linenum, *err);
1596 return -1;
1597 }
1598 }
1599 else if (!strcmp(args[0], "rspadd")) { /* add response header */
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 (curproxy->nb_rspadd >= MAX_NEWHDR) {
1606 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1607 return 0;
1608 }
1609
1610 if (*(args[1]) == 0) {
1611 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1612 return -1;
1613 }
1614
1615 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1616 }
1617 else if (!strcmp(args[0], "errorloc") ||
1618 !strcmp(args[0], "errorloc302") ||
1619 !strcmp(args[0], "errorloc303")) { /* error location */
1620 int errnum, errlen;
1621 char *err;
1622
1623 // if (curproxy == &defproxy) {
1624 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1625 // return -1;
1626 // }
1627
1628 if (*(args[2]) == 0) {
1629 Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum);
1630 return -1;
1631 }
1632
1633 errnum = atol(args[1]);
1634 if (!strcmp(args[0], "errorloc303")) {
1635 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1636 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1637 } else {
1638 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1639 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
1640 }
1641
1642 if (errnum == 400) {
1643 if (curproxy->errmsg.msg400) {
1644 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1645 free(curproxy->errmsg.msg400);
1646 }
1647 curproxy->errmsg.msg400 = err;
1648 curproxy->errmsg.len400 = errlen;
1649 }
1650 else if (errnum == 403) {
1651 if (curproxy->errmsg.msg403) {
1652 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1653 free(curproxy->errmsg.msg403);
1654 }
1655 curproxy->errmsg.msg403 = err;
1656 curproxy->errmsg.len403 = errlen;
1657 }
1658 else if (errnum == 408) {
1659 if (curproxy->errmsg.msg408) {
1660 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1661 free(curproxy->errmsg.msg408);
1662 }
1663 curproxy->errmsg.msg408 = err;
1664 curproxy->errmsg.len408 = errlen;
1665 }
1666 else if (errnum == 500) {
1667 if (curproxy->errmsg.msg500) {
1668 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1669 free(curproxy->errmsg.msg500);
1670 }
1671 curproxy->errmsg.msg500 = err;
1672 curproxy->errmsg.len500 = errlen;
1673 }
1674 else if (errnum == 502) {
1675 if (curproxy->errmsg.msg502) {
1676 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1677 free(curproxy->errmsg.msg502);
1678 }
1679 curproxy->errmsg.msg502 = err;
1680 curproxy->errmsg.len502 = errlen;
1681 }
1682 else if (errnum == 503) {
1683 if (curproxy->errmsg.msg503) {
1684 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1685 free(curproxy->errmsg.msg503);
1686 }
1687 curproxy->errmsg.msg503 = err;
1688 curproxy->errmsg.len503 = errlen;
1689 }
1690 else if (errnum == 504) {
1691 if (curproxy->errmsg.msg504) {
1692 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1693 free(curproxy->errmsg.msg504);
1694 }
1695 curproxy->errmsg.msg504 = err;
1696 curproxy->errmsg.len504 = errlen;
1697 }
1698 else {
1699 Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum);
1700 free(err);
1701 }
1702 }
1703 else {
1704 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
1705 return -1;
1706 }
1707 return 0;
1708}
1709
1710
1711/*
1712 * This function reads and parses the configuration file given in the argument.
1713 * returns 0 if OK, -1 if error.
1714 */
1715int readcfgfile(char *file)
1716{
1717 char thisline[256];
1718 char *line;
1719 FILE *f;
1720 int linenum = 0;
1721 char *end;
1722 char *args[MAX_LINE_ARGS];
1723 int arg;
1724 int cfgerr = 0;
1725 int nbchk, mininter;
1726 int confsect = CFG_NONE;
1727
1728 struct proxy *curproxy = NULL;
1729 struct server *newsrv = NULL;
1730
1731 if ((f=fopen(file,"r")) == NULL)
1732 return -1;
1733
1734 init_default_instance();
1735
1736 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
1737 linenum++;
1738
1739 end = line + strlen(line);
1740
1741 /* skip leading spaces */
1742 while (isspace((int)*line))
1743 line++;
1744
1745 arg = 0;
1746 args[arg] = line;
1747
1748 while (*line && arg < MAX_LINE_ARGS) {
1749 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1750 * C equivalent value. Other combinations left unchanged (eg: \1).
1751 */
1752 if (*line == '\\') {
1753 int skip = 0;
1754 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1755 *line = line[1];
1756 skip = 1;
1757 }
1758 else if (line[1] == 'r') {
1759 *line = '\r';
1760 skip = 1;
1761 }
1762 else if (line[1] == 'n') {
1763 *line = '\n';
1764 skip = 1;
1765 }
1766 else if (line[1] == 't') {
1767 *line = '\t';
1768 skip = 1;
1769 }
1770 else if (line[1] == 'x') {
1771 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
1772 unsigned char hex1, hex2;
1773 hex1 = toupper(line[2]) - '0';
1774 hex2 = toupper(line[3]) - '0';
1775 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1776 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1777 *line = (hex1<<4) + hex2;
1778 skip = 3;
1779 }
1780 else {
1781 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
1782 return -1;
1783 }
1784 }
1785 if (skip) {
1786 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
1787 end -= skip;
1788 }
1789 line++;
1790 }
1791 else if (*line == '#' || *line == '\n' || *line == '\r') {
1792 /* end of string, end of loop */
1793 *line = 0;
1794 break;
1795 }
1796 else if (isspace((int)*line)) {
1797 /* a non-escaped space is an argument separator */
1798 *line++ = 0;
1799 while (isspace((int)*line))
1800 line++;
1801 args[++arg] = line;
1802 }
1803 else {
1804 line++;
1805 }
1806 }
1807
1808 /* empty line */
1809 if (!**args)
1810 continue;
1811
1812 /* zero out remaining args */
1813 while (++arg < MAX_LINE_ARGS) {
1814 args[arg] = line;
1815 }
1816
1817 if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */
1818 confsect = CFG_LISTEN;
1819 else if (!strcmp(args[0], "global")) /* global config */
1820 confsect = CFG_GLOBAL;
1821 /* else it's a section keyword */
1822
1823 switch (confsect) {
1824 case CFG_LISTEN:
1825 if (cfg_parse_listen(file, linenum, args) < 0)
1826 return -1;
1827 break;
1828 case CFG_GLOBAL:
1829 if (cfg_parse_global(file, linenum, args) < 0)
1830 return -1;
1831 break;
1832 default:
1833 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
1834 return -1;
1835 }
1836
1837
1838 }
1839 fclose(f);
1840
1841 /*
1842 * Now, check for the integrity of all that we have collected.
1843 */
1844
1845 /* will be needed further to delay some tasks */
1846 tv_now(&now);
1847
1848 if ((curproxy = proxy) == NULL) {
1849 Alert("parsing %s : no <listen> line. Nothing to do !\n",
1850 file);
1851 return -1;
1852 }
1853
1854 while (curproxy != NULL) {
1855 if (curproxy->state == PR_STSTOPPED) {
1856 curproxy = curproxy->next;
1857 continue;
1858 }
1859
1860 if (curproxy->listen == NULL) {
1861 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);
1862 cfgerr++;
1863 }
1864 else if ((curproxy->mode != PR_MODE_HEALTH) &&
1865 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
1866 (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) {
1867 Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n",
1868 file, curproxy->id);
1869 cfgerr++;
1870 }
1871 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
1872 if (curproxy->options & PR_O_TRANSP) {
1873 Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n",
1874 file, curproxy->id);
1875 cfgerr++;
1876 }
1877#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
1878 else if (curproxy->srv == NULL) {
1879 Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n",
1880 file, curproxy->id);
1881 cfgerr++;
1882 }
1883#endif
1884 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
1885 Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n",
1886 file, curproxy->id);
1887 }
1888 }
1889 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
1890 if (curproxy->cookie_name != NULL) {
1891 Warning("parsing %s : cookie will be ignored for listener %s.\n",
1892 file, curproxy->id);
1893 }
1894 if ((newsrv = curproxy->srv) != NULL) {
1895 Warning("parsing %s : servers will be ignored for listener %s.\n",
1896 file, curproxy->id);
1897 }
1898 if (curproxy->rsp_exp != NULL) {
1899 Warning("parsing %s : server regular expressions will be ignored for listener %s.\n",
1900 file, curproxy->id);
1901 }
1902 if (curproxy->req_exp != NULL) {
1903 Warning("parsing %s : client regular expressions will be ignored for listener %s.\n",
1904 file, curproxy->id);
1905 }
1906 }
1907 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
1908 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
1909 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
1910 file, curproxy->id);
1911 cfgerr++;
1912 }
1913 }
1914
Willy Tarreau2738a142006-07-08 17:28:09 +02001915 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
1916 (!curproxy->clitimeout || !curproxy->contimeout || !curproxy->srvtimeout)) {
1917 Warning("parsing %s : missing timeouts for listener '%s'.\n"
1918 " | While not properly invalid, you will certainly encounter various problems\n"
1919 " | with such a configuration. To fix this, please ensure that all following\n"
1920 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
1921 file, curproxy->id);
1922 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02001923
1924 if (curproxy->options & PR_O_SSL3_CHK) {
1925 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
1926 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
1927 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
1928 }
1929
Willy Tarreaubaaee002006-06-26 02:48:02 +02001930 /* first, we will invert the servers list order */
1931 newsrv = NULL;
1932 while (curproxy->srv) {
1933 struct server *next;
1934
1935 next = curproxy->srv->next;
1936 curproxy->srv->next = newsrv;
1937 newsrv = curproxy->srv;
1938 if (!next)
1939 break;
1940 curproxy->srv = next;
1941 }
1942
1943 /* now, newsrv == curproxy->srv */
1944 if (newsrv) {
1945 struct server *srv;
1946 int pgcd;
1947 int act, bck;
1948
1949 /* We will factor the weights to reduce the table,
1950 * using Euclide's largest common divisor algorithm
1951 */
1952 pgcd = newsrv->uweight + 1;
1953 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
1954 int t, w;
1955
1956 w = srv->uweight + 1;
1957 while (w) {
1958 t = pgcd % w;
1959 pgcd = w;
1960 w = t;
1961 }
1962 }
1963
1964 act = bck = 0;
1965 for (srv = newsrv; srv; srv = srv->next) {
1966 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
1967 if (srv->state & SRV_BACKUP)
1968 bck += srv->eweight + 1;
1969 else
1970 act += srv->eweight + 1;
1971 }
1972
1973 /* this is the largest map we will ever need for this servers list */
1974 if (act < bck)
1975 act = bck;
1976
1977 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
1978 /* recounts servers and their weights */
1979 recount_servers(curproxy);
1980 recalc_server_map(curproxy);
1981 }
1982
1983 if (curproxy->options & PR_O_LOGASAP)
1984 curproxy->to_log &= ~LW_BYTES;
1985
1986 if (curproxy->errmsg.msg400 == NULL) {
1987 curproxy->errmsg.msg400 = (char *)HTTP_400;
1988 curproxy->errmsg.len400 = strlen(HTTP_400);
1989 }
1990 if (curproxy->errmsg.msg403 == NULL) {
1991 curproxy->errmsg.msg403 = (char *)HTTP_403;
1992 curproxy->errmsg.len403 = strlen(HTTP_403);
1993 }
1994 if (curproxy->errmsg.msg408 == NULL) {
1995 curproxy->errmsg.msg408 = (char *)HTTP_408;
1996 curproxy->errmsg.len408 = strlen(HTTP_408);
1997 }
1998 if (curproxy->errmsg.msg500 == NULL) {
1999 curproxy->errmsg.msg500 = (char *)HTTP_500;
2000 curproxy->errmsg.len500 = strlen(HTTP_500);
2001 }
2002 if (curproxy->errmsg.msg502 == NULL) {
2003 curproxy->errmsg.msg502 = (char *)HTTP_502;
2004 curproxy->errmsg.len502 = strlen(HTTP_502);
2005 }
2006 if (curproxy->errmsg.msg503 == NULL) {
2007 curproxy->errmsg.msg503 = (char *)HTTP_503;
2008 curproxy->errmsg.len503 = strlen(HTTP_503);
2009 }
2010 if (curproxy->errmsg.msg504 == NULL) {
2011 curproxy->errmsg.msg504 = (char *)HTTP_504;
2012 curproxy->errmsg.len504 = strlen(HTTP_504);
2013 }
2014
2015 /*
2016 * If this server supports a maxconn parameter, it needs a dedicated
2017 * tasks to fill the emptied slots when a connection leaves.
2018 */
2019 newsrv = curproxy->srv;
2020 while (newsrv != NULL) {
2021 if (newsrv->minconn >= newsrv->maxconn) {
2022 /* Only 'minconn' was specified, or it was higher than or equal
2023 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2024 * this will avoid further useless expensive computations.
2025 */
2026 newsrv->maxconn = newsrv->minconn;
2027 newsrv->minconn = 0;
2028 }
2029
2030 if (newsrv->maxconn > 0) {
2031 struct task *t;
2032
2033 if ((t = pool_alloc(task)) == NULL) {
2034 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2035 return -1;
2036 }
2037
2038 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2039 t->wq = LIST_HEAD(wait_queue[1]); /* already assigned to the eternity queue */
2040 t->state = TASK_IDLE;
2041 t->process = process_srv_queue;
2042 t->context = newsrv;
2043 newsrv->queue_mgt = t;
2044
2045 /* never run it unless specifically woken up */
2046 tv_eternity(&t->expire);
2047 task_queue(t);
2048 }
2049 newsrv = newsrv->next;
2050 }
2051
2052 /* now we'll start this proxy's health checks if any */
2053 /* 1- count the checkers to run simultaneously */
2054 nbchk = 0;
2055 mininter = 0;
2056 newsrv = curproxy->srv;
2057 while (newsrv != NULL) {
2058 if (newsrv->state & SRV_CHECKED) {
2059 if (!mininter || mininter > newsrv->inter)
2060 mininter = newsrv->inter;
2061 nbchk++;
2062 }
2063 newsrv = newsrv->next;
2064 }
2065
2066 /* 2- start them as far as possible from each others while respecting
2067 * their own intervals. For this, we will start them after their own
2068 * interval added to the min interval divided by the number of servers,
2069 * weighted by the server's position in the list.
2070 */
2071 if (nbchk > 0) {
2072 struct task *t;
2073 int srvpos;
2074
2075 newsrv = curproxy->srv;
2076 srvpos = 0;
2077 while (newsrv != NULL) {
2078 /* should this server be checked ? */
2079 if (newsrv->state & SRV_CHECKED) {
2080 if ((t = pool_alloc(task)) == NULL) {
2081 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2082 return -1;
2083 }
2084
2085 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2086 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
2087 t->state = TASK_IDLE;
2088 t->process = process_chk;
2089 t->context = newsrv;
2090
2091 /* check this every ms */
2092 tv_delayfrom(&t->expire, &now,
2093 newsrv->inter + mininter * srvpos / nbchk);
2094 task_queue(t);
2095 //task_wakeup(&rq, t);
2096 srvpos++;
2097 }
2098 newsrv = newsrv->next;
2099 }
2100 }
2101
2102 curproxy = curproxy->next;
2103 }
2104 if (cfgerr > 0) {
2105 Alert("Errors found in configuration file, aborting.\n");
2106 return -1;
2107 }
2108 else
2109 return 0;
2110}
2111
2112
2113
2114/*
2115 * Local variables:
2116 * c-indent-level: 8
2117 * c-basic-offset: 8
2118 * End:
2119 */