blob: 0034bdab0bca8941664518d2c4c3f7f49e8ba97e [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 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200254int cfg_parse_global(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255{
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 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200427int cfg_parse_listen(const char *file, int linenum, char **args)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428{
429 static struct proxy *curproxy = NULL;
430 struct server *newsrv = NULL;
Willy Tarreaub17916e2006-10-15 15:17:57 +0200431 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432 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;
Willy Tarreau77074d52006-11-12 23:57:19 +01001156#ifdef CONFIG_HAP_CTTPROXY
1157 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1158 if (newsrv->source_addr.sin_addr.s_addr == INADDR_ANY) {
1159 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1160 file, linenum, "usesrc");
1161 return -1;
1162 }
1163 if (!*args[cur_arg + 1]) {
1164 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1165 file, linenum, "usesrc");
1166 return -1;
1167 }
1168 if (!strcmp(args[cur_arg + 1], "client")) {
1169 newsrv->state |= SRV_TPROXY_CLI;
1170 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1171 newsrv->state |= SRV_TPROXY_CIP;
1172 } else {
1173 newsrv->state |= SRV_TPROXY_ADDR;
1174 newsrv->tproxy_addr = *str2sa(args[cur_arg + 1]);
1175 }
1176 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1177 cur_arg += 2;
1178 }
1179#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001180 }
1181 else {
1182 Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', 'minconn', 'maxconn' and 'weight'.\n",
1183 file, linenum, newsrv->id);
1184 return -1;
1185 }
1186 }
1187
1188 if (do_check) {
1189 if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
1190 newsrv->check_port = realport; /* by default */
1191 if (!newsrv->check_port) {
1192 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1193 file, linenum, newsrv->id);
1194 return -1;
1195 }
1196 newsrv->state |= SRV_CHECKED;
1197 }
1198
1199 if (newsrv->state & SRV_BACKUP)
1200 curproxy->srv_bck++;
1201 else
1202 curproxy->srv_act++;
1203 }
1204 else if (!strcmp(args[0], "log")) { /* syslog server address */
1205 struct sockaddr_in *sa;
1206 int facility;
1207
1208 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
1209 curproxy->logfac1 = global.logfac1;
1210 curproxy->logsrv1 = global.logsrv1;
1211 curproxy->loglev1 = global.loglev1;
1212 curproxy->logfac2 = global.logfac2;
1213 curproxy->logsrv2 = global.logsrv2;
1214 curproxy->loglev2 = global.loglev2;
1215 }
1216 else if (*(args[1]) && *(args[2])) {
1217 int level;
1218
1219 facility = get_log_facility(args[2]);
1220 if (facility < 0) {
1221 Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]);
1222 exit(1);
1223 }
1224
1225 level = 7; /* max syslog level = debug */
1226 if (*(args[3])) {
1227 level = get_log_level(args[3]);
1228 if (level < 0) {
1229 Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]);
1230 exit(1);
1231 }
1232 }
1233
1234 sa = str2sa(args[1]);
1235 if (!sa->sin_port)
1236 sa->sin_port = htons(SYSLOG_PORT);
1237
1238 if (curproxy->logfac1 == -1) {
1239 curproxy->logsrv1 = *sa;
1240 curproxy->logfac1 = facility;
1241 curproxy->loglev1 = level;
1242 }
1243 else if (curproxy->logfac2 == -1) {
1244 curproxy->logsrv2 = *sa;
1245 curproxy->logfac2 = facility;
1246 curproxy->loglev2 = level;
1247 }
1248 else {
1249 Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum);
1250 return -1;
1251 }
1252 }
1253 else {
1254 Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n",
1255 file, linenum);
1256 return -1;
1257 }
1258 }
1259 else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
1260 if (!*args[1]) {
1261 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
1262 file, linenum, "source");
1263 return -1;
1264 }
1265
1266 curproxy->source_addr = *str2sa(args[1]);
1267 curproxy->options |= PR_O_BIND_SRC;
Willy Tarreau77074d52006-11-12 23:57:19 +01001268#ifdef CONFIG_HAP_CTTPROXY
1269 if (!strcmp(args[2], "usesrc")) { /* address to use outside */
1270 if (curproxy->source_addr.sin_addr.s_addr == INADDR_ANY) {
1271 Alert("parsing [%s:%d] : '%s' requires an explicit 'source' address.\n",
1272 file, linenum, "usesrc");
1273 return -1;
1274 }
1275 if (!*args[3]) {
1276 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', or 'clientip' as argument.\n",
1277 file, linenum, "usesrc");
1278 return -1;
1279 }
1280
1281 if (!strcmp(args[3], "client")) {
1282 curproxy->options |= PR_O_TPXY_CLI;
1283 } else if (!strcmp(args[3], "clientip")) {
1284 curproxy->options |= PR_O_TPXY_CIP;
1285 } else {
1286 curproxy->options |= PR_O_TPXY_ADDR;
1287 curproxy->tproxy_addr = *str2sa(args[3]);
1288 }
1289 global.last_checks |= LSTCHK_CTTPROXY | LSTCHK_NETADM;
1290 }
1291#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292 }
1293 else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
1294 regex_t *preg;
1295 if (curproxy == &defproxy) {
1296 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1297 return -1;
1298 }
1299
1300 if (*(args[1]) == 0 || *(args[2]) == 0) {
1301 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1302 file, linenum, args[0]);
1303 return -1;
1304 }
1305
1306 preg = calloc(1, sizeof(regex_t));
1307 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1308 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1309 return -1;
1310 }
1311
1312 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1313 if (err) {
1314 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1315 file, linenum, *err);
1316 return -1;
1317 }
1318 }
1319 else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
1320 regex_t *preg;
1321 if (curproxy == &defproxy) {
1322 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1323 return -1;
1324 }
1325
1326 if (*(args[1]) == 0) {
1327 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1328 return -1;
1329 }
1330
1331 preg = calloc(1, sizeof(regex_t));
1332 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1333 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1334 return -1;
1335 }
1336
1337 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1338 }
1339 else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
1340 regex_t *preg;
1341 if (curproxy == &defproxy) {
1342 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1343 return -1;
1344 }
1345
1346 if (*(args[1]) == 0) {
1347 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1348 return -1;
1349 }
1350
1351 preg = calloc(1, sizeof(regex_t));
1352 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1353 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1354 return -1;
1355 }
1356
1357 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1358 }
1359 else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
1360 regex_t *preg;
1361 if (curproxy == &defproxy) {
1362 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1363 return -1;
1364 }
1365
1366 if (*(args[1]) == 0) {
1367 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1368 return -1;
1369 }
1370
1371 preg = calloc(1, sizeof(regex_t));
1372 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1373 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1374 return -1;
1375 }
1376
1377 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1378 }
1379 else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
1380 regex_t *preg;
1381 if (curproxy == &defproxy) {
1382 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1383 return -1;
1384 }
1385
1386 if (*(args[1]) == 0) {
1387 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1388 return -1;
1389 }
1390
1391 preg = calloc(1, sizeof(regex_t));
1392 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1393 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1394 return -1;
1395 }
1396
1397 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1398 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001399 else if (!strcmp(args[0], "reqtarpit")) { /* tarpit a request if a header matches this regex */
1400 regex_t *preg;
1401 if (curproxy == &defproxy) {
1402 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1403 return -1;
1404 }
1405
1406 if (*(args[1]) == 0) {
1407 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1408 return -1;
1409 }
1410
1411 preg = calloc(1, sizeof(regex_t));
1412 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1413 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1414 return -1;
1415 }
1416
1417 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1418 }
Willy Tarreaua496b602006-12-17 23:15:24 +01001419 else if (!strcmp(args[0], "reqsetbe")) { /* switch the backend from a regex, respecting case */
1420 regex_t *preg;
1421 if(curproxy == &defproxy) {
1422 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1423 return -1;
1424 }
1425
1426 if(*(args[1]) == 0 || *(args[2]) == 0) {
1427 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1428 file, linenum, args[0]);
1429 return -1;
1430 }
1431
1432 preg = calloc(1, sizeof(regex_t));
1433 if(regcomp(preg, args[1], REG_EXTENDED) != 0) {
1434 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1435 }
1436
1437 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1438 }
1439 else if (!strcmp(args[0], "reqisetbe")) { /* switch the backend from a regex, ignoring case */
1440 regex_t *preg;
1441 if(curproxy == &defproxy) {
1442 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1443 return -1;
1444 }
1445
1446 if(*(args[1]) == 0 || *(args[2]) == 0) {
1447 Alert("parsing [%s:%d] : '%s' expects <search> and <target> as arguments.\n",
1448 file, linenum, args[0]);
1449 return -1;
1450 }
1451
1452 preg = calloc(1, sizeof(regex_t));
1453 if(regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1454 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1455 }
1456
1457 chain_regex(&curproxy->req_exp, preg, ACT_SETBE, strdup(args[2]));
1458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001459 else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
1460 regex_t *preg;
1461 if (curproxy == &defproxy) {
1462 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1463 return -1;
1464 }
1465
1466 if (*(args[1]) == 0 || *(args[2]) == 0) {
1467 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1468 file, linenum, args[0]);
1469 return -1;
1470 }
1471
1472 preg = calloc(1, sizeof(regex_t));
1473 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1474 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1475 return -1;
1476 }
1477
1478 err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2]));
1479 if (err) {
1480 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1481 file, linenum, *err);
1482 return -1;
1483 }
1484 }
1485 else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
1486 regex_t *preg;
1487 if (curproxy == &defproxy) {
1488 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1489 return -1;
1490 }
1491
1492 if (*(args[1]) == 0) {
1493 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1494 return -1;
1495 }
1496
1497 preg = calloc(1, sizeof(regex_t));
1498 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1499 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1500 return -1;
1501 }
1502
1503 chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL);
1504 }
1505 else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
1506 regex_t *preg;
1507 if (curproxy == &defproxy) {
1508 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1509 return -1;
1510 }
1511
1512 if (*(args[1]) == 0) {
1513 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1514 return -1;
1515 }
1516
1517 preg = calloc(1, sizeof(regex_t));
1518 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1519 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1520 return -1;
1521 }
1522
1523 chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL);
1524 }
1525 else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
1526 regex_t *preg;
1527 if (curproxy == &defproxy) {
1528 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1529 return -1;
1530 }
1531
1532 if (*(args[1]) == 0) {
1533 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1534 return -1;
1535 }
1536
1537 preg = calloc(1, sizeof(regex_t));
1538 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1539 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1540 return -1;
1541 }
1542
1543 chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL);
1544 }
1545 else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
1546 regex_t *preg;
1547 if (curproxy == &defproxy) {
1548 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1549 return -1;
1550 }
1551
1552 if (*(args[1]) == 0) {
1553 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1554 return -1;
1555 }
1556
1557 preg = calloc(1, sizeof(regex_t));
1558 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1559 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1560 return -1;
1561 }
1562
1563 chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
1564 }
Willy Tarreaub8750a82006-09-03 09:56:00 +02001565 else if (!strcmp(args[0], "reqitarpit")) { /* tarpit a request if a header matches this regex ignoring case */
1566 regex_t *preg;
1567 if (curproxy == &defproxy) {
1568 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1569 return -1;
1570 }
1571
1572 if (*(args[1]) == 0) {
1573 Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
1574 return -1;
1575 }
1576
1577 preg = calloc(1, sizeof(regex_t));
1578 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1579 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1580 return -1;
1581 }
1582
1583 chain_regex(&curproxy->req_exp, preg, ACT_TARPIT, NULL);
1584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001585 else if (!strcmp(args[0], "reqadd")) { /* add request header */
1586 if (curproxy == &defproxy) {
1587 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1588 return -1;
1589 }
1590
1591 if (curproxy->nb_reqadd >= MAX_NEWHDR) {
1592 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1593 return 0;
1594 }
1595
1596 if (*(args[1]) == 0) {
1597 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1598 return -1;
1599 }
1600
1601 curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]);
1602 }
1603 else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */
1604 regex_t *preg;
1605
1606 if (*(args[1]) == 0 || *(args[2]) == 0) {
1607 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1608 file, linenum, args[0]);
1609 return -1;
1610 }
1611
1612 preg = calloc(1, sizeof(regex_t));
1613 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1614 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1615 return -1;
1616 }
1617
1618 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1619 if (err) {
1620 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1621 file, linenum, *err);
1622 return -1;
1623 }
1624 }
1625 else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
1626 regex_t *preg;
1627 if (curproxy == &defproxy) {
1628 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1629 return -1;
1630 }
1631
1632 if (*(args[1]) == 0) {
1633 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1634 return -1;
1635 }
1636
1637 preg = calloc(1, sizeof(regex_t));
1638 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1639 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1640 return -1;
1641 }
1642
1643 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1644 if (err) {
1645 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1646 file, linenum, *err);
1647 return -1;
1648 }
1649 }
1650 else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */
1651 regex_t *preg;
1652 if (curproxy == &defproxy) {
1653 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1654 return -1;
1655 }
1656
1657 if (*(args[1]) == 0) {
1658 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1659 return -1;
1660 }
1661
1662 preg = calloc(1, sizeof(regex_t));
1663 if (regcomp(preg, args[1], REG_EXTENDED) != 0) {
1664 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1665 return -1;
1666 }
1667
1668 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1669 if (err) {
1670 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1671 file, linenum, *err);
1672 return -1;
1673 }
1674 }
1675 else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
1676 regex_t *preg;
1677 if (curproxy == &defproxy) {
1678 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1679 return -1;
1680 }
1681
1682 if (*(args[1]) == 0 || *(args[2]) == 0) {
1683 Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
1684 file, linenum, args[0]);
1685 return -1;
1686 }
1687
1688 preg = calloc(1, sizeof(regex_t));
1689 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1690 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1691 return -1;
1692 }
1693
1694 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
1695 if (err) {
1696 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1697 file, linenum, *err);
1698 return -1;
1699 }
1700 }
1701 else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
1702 regex_t *preg;
1703 if (curproxy == &defproxy) {
1704 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1705 return -1;
1706 }
1707
1708 if (*(args[1]) == 0) {
1709 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1710 return -1;
1711 }
1712
1713 preg = calloc(1, sizeof(regex_t));
1714 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1715 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1716 return -1;
1717 }
1718
1719 err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
1720 if (err) {
1721 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1722 file, linenum, *err);
1723 return -1;
1724 }
1725 }
1726 else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */
1727 regex_t *preg;
1728 if (curproxy == &defproxy) {
1729 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1730 return -1;
1731 }
1732
1733 if (*(args[1]) == 0) {
1734 Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
1735 return -1;
1736 }
1737
1738 preg = calloc(1, sizeof(regex_t));
1739 if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
1740 Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
1741 return -1;
1742 }
1743
1744 err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2]));
1745 if (err) {
1746 Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n",
1747 file, linenum, *err);
1748 return -1;
1749 }
1750 }
1751 else if (!strcmp(args[0], "rspadd")) { /* add response header */
1752 if (curproxy == &defproxy) {
1753 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1754 return -1;
1755 }
1756
1757 if (curproxy->nb_rspadd >= MAX_NEWHDR) {
1758 Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
1759 return 0;
1760 }
1761
1762 if (*(args[1]) == 0) {
1763 Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]);
1764 return -1;
1765 }
1766
1767 curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]);
1768 }
1769 else if (!strcmp(args[0], "errorloc") ||
1770 !strcmp(args[0], "errorloc302") ||
1771 !strcmp(args[0], "errorloc303")) { /* error location */
1772 int errnum, errlen;
1773 char *err;
1774
1775 // if (curproxy == &defproxy) {
1776 // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1777 // return -1;
1778 // }
1779
1780 if (*(args[2]) == 0) {
1781 Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum);
1782 return -1;
1783 }
1784
1785 errnum = atol(args[1]);
1786 if (!strcmp(args[0], "errorloc303")) {
1787 err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5);
1788 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]);
1789 } else {
1790 err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5);
1791 errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]);
1792 }
1793
1794 if (errnum == 400) {
1795 if (curproxy->errmsg.msg400) {
1796 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1797 free(curproxy->errmsg.msg400);
1798 }
1799 curproxy->errmsg.msg400 = err;
1800 curproxy->errmsg.len400 = errlen;
1801 }
1802 else if (errnum == 403) {
1803 if (curproxy->errmsg.msg403) {
1804 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1805 free(curproxy->errmsg.msg403);
1806 }
1807 curproxy->errmsg.msg403 = err;
1808 curproxy->errmsg.len403 = errlen;
1809 }
1810 else if (errnum == 408) {
1811 if (curproxy->errmsg.msg408) {
1812 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1813 free(curproxy->errmsg.msg408);
1814 }
1815 curproxy->errmsg.msg408 = err;
1816 curproxy->errmsg.len408 = errlen;
1817 }
1818 else if (errnum == 500) {
1819 if (curproxy->errmsg.msg500) {
1820 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1821 free(curproxy->errmsg.msg500);
1822 }
1823 curproxy->errmsg.msg500 = err;
1824 curproxy->errmsg.len500 = errlen;
1825 }
1826 else if (errnum == 502) {
1827 if (curproxy->errmsg.msg502) {
1828 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1829 free(curproxy->errmsg.msg502);
1830 }
1831 curproxy->errmsg.msg502 = err;
1832 curproxy->errmsg.len502 = errlen;
1833 }
1834 else if (errnum == 503) {
1835 if (curproxy->errmsg.msg503) {
1836 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1837 free(curproxy->errmsg.msg503);
1838 }
1839 curproxy->errmsg.msg503 = err;
1840 curproxy->errmsg.len503 = errlen;
1841 }
1842 else if (errnum == 504) {
1843 if (curproxy->errmsg.msg504) {
1844 //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum);
1845 free(curproxy->errmsg.msg504);
1846 }
1847 curproxy->errmsg.msg504 = err;
1848 curproxy->errmsg.len504 = errlen;
1849 }
1850 else {
1851 Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum);
1852 free(err);
1853 }
1854 }
1855 else {
1856 Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen");
1857 return -1;
1858 }
1859 return 0;
1860}
1861
1862
1863/*
1864 * This function reads and parses the configuration file given in the argument.
1865 * returns 0 if OK, -1 if error.
1866 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001867int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001868{
1869 char thisline[256];
1870 char *line;
1871 FILE *f;
1872 int linenum = 0;
1873 char *end;
1874 char *args[MAX_LINE_ARGS];
1875 int arg;
1876 int cfgerr = 0;
1877 int nbchk, mininter;
1878 int confsect = CFG_NONE;
1879
1880 struct proxy *curproxy = NULL;
1881 struct server *newsrv = NULL;
1882
1883 if ((f=fopen(file,"r")) == NULL)
1884 return -1;
1885
1886 init_default_instance();
1887
1888 while (fgets(line = thisline, sizeof(thisline), f) != NULL) {
1889 linenum++;
1890
1891 end = line + strlen(line);
1892
1893 /* skip leading spaces */
1894 while (isspace((int)*line))
1895 line++;
1896
1897 arg = 0;
1898 args[arg] = line;
1899
1900 while (*line && arg < MAX_LINE_ARGS) {
1901 /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1902 * C equivalent value. Other combinations left unchanged (eg: \1).
1903 */
1904 if (*line == '\\') {
1905 int skip = 0;
1906 if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1907 *line = line[1];
1908 skip = 1;
1909 }
1910 else if (line[1] == 'r') {
1911 *line = '\r';
1912 skip = 1;
1913 }
1914 else if (line[1] == 'n') {
1915 *line = '\n';
1916 skip = 1;
1917 }
1918 else if (line[1] == 't') {
1919 *line = '\t';
1920 skip = 1;
1921 }
1922 else if (line[1] == 'x') {
1923 if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) {
1924 unsigned char hex1, hex2;
1925 hex1 = toupper(line[2]) - '0';
1926 hex2 = toupper(line[3]) - '0';
1927 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1928 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1929 *line = (hex1<<4) + hex2;
1930 skip = 3;
1931 }
1932 else {
1933 Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
1934 return -1;
1935 }
1936 }
1937 if (skip) {
1938 memmove(line + 1, line + 1 + skip, end - (line + skip + 1));
1939 end -= skip;
1940 }
1941 line++;
1942 }
1943 else if (*line == '#' || *line == '\n' || *line == '\r') {
1944 /* end of string, end of loop */
1945 *line = 0;
1946 break;
1947 }
1948 else if (isspace((int)*line)) {
1949 /* a non-escaped space is an argument separator */
1950 *line++ = 0;
1951 while (isspace((int)*line))
1952 line++;
1953 args[++arg] = line;
1954 }
1955 else {
1956 line++;
1957 }
1958 }
1959
1960 /* empty line */
1961 if (!**args)
1962 continue;
1963
1964 /* zero out remaining args */
1965 while (++arg < MAX_LINE_ARGS) {
1966 args[arg] = line;
1967 }
1968
1969 if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */
1970 confsect = CFG_LISTEN;
1971 else if (!strcmp(args[0], "global")) /* global config */
1972 confsect = CFG_GLOBAL;
1973 /* else it's a section keyword */
1974
1975 switch (confsect) {
1976 case CFG_LISTEN:
1977 if (cfg_parse_listen(file, linenum, args) < 0)
1978 return -1;
1979 break;
1980 case CFG_GLOBAL:
1981 if (cfg_parse_global(file, linenum, args) < 0)
1982 return -1;
1983 break;
1984 default:
1985 Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]);
1986 return -1;
1987 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988 }
1989 fclose(f);
1990
1991 /*
1992 * Now, check for the integrity of all that we have collected.
1993 */
1994
1995 /* will be needed further to delay some tasks */
1996 tv_now(&now);
1997
1998 if ((curproxy = proxy) == NULL) {
1999 Alert("parsing %s : no <listen> line. Nothing to do !\n",
2000 file);
2001 return -1;
2002 }
2003
2004 while (curproxy != NULL) {
Willy Tarreau97a738f2006-12-17 18:02:30 +01002005 curproxy->fiprm = curproxy->beprm = curproxy;
2006
Willy Tarreaubaaee002006-06-26 02:48:02 +02002007 if (curproxy->state == PR_STSTOPPED) {
2008 curproxy = curproxy->next;
2009 continue;
2010 }
2011
2012 if (curproxy->listen == NULL) {
2013 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);
2014 cfgerr++;
2015 }
2016 else if ((curproxy->mode != PR_MODE_HEALTH) &&
2017 !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) &&
2018 (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) {
2019 Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n",
2020 file, curproxy->id);
2021 cfgerr++;
2022 }
2023 else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) {
2024 if (curproxy->options & PR_O_TRANSP) {
2025 Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n",
2026 file, curproxy->id);
2027 cfgerr++;
2028 }
2029#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2030 else if (curproxy->srv == NULL) {
2031 Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n",
2032 file, curproxy->id);
2033 cfgerr++;
2034 }
2035#endif
2036 else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) {
2037 Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n",
2038 file, curproxy->id);
2039 }
2040 }
2041 else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */
2042 if (curproxy->cookie_name != NULL) {
2043 Warning("parsing %s : cookie will be ignored for listener %s.\n",
2044 file, curproxy->id);
2045 }
2046 if ((newsrv = curproxy->srv) != NULL) {
2047 Warning("parsing %s : servers will be ignored for listener %s.\n",
2048 file, curproxy->id);
2049 }
2050 if (curproxy->rsp_exp != NULL) {
2051 Warning("parsing %s : server regular expressions will be ignored for listener %s.\n",
2052 file, curproxy->id);
2053 }
2054 if (curproxy->req_exp != NULL) {
2055 Warning("parsing %s : client regular expressions will be ignored for listener %s.\n",
2056 file, curproxy->id);
2057 }
Willy Tarreau1c47f852006-07-09 08:22:27 +02002058 if (curproxy->monitor_uri != NULL) {
2059 Warning("parsing %s : monitor-uri will be ignored for listener %s.\n",
2060 file, curproxy->id);
2061 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002062 }
2063 else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */
2064 if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) {
2065 Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n",
2066 file, curproxy->id);
2067 cfgerr++;
2068 }
2069 }
2070
Willy Tarreaua496b602006-12-17 23:15:24 +01002071 if (curproxy->mode == PR_MODE_HTTP && curproxy->req_exp != NULL) {
2072 /* map jump target for ACT_SETBE in req_rep chain */
2073 struct hdr_exp *exp;
2074 struct proxy *target;
2075 for (exp = curproxy->req_exp; exp != NULL; exp = exp->next) {
2076 if (exp->action != ACT_SETBE)
2077 continue;
2078 for (target = proxy; target != NULL; target = target->next) {
2079 if (strcmp(target->id, exp->replace) == 0)
2080 break;
2081 }
2082 if (target == NULL) {
2083 Alert("parsing %s : backend '%s' in HTTP proxy %s was not found !\n",
2084 file, exp->replace, curproxy->id);
2085 cfgerr++;
2086 } else if (target == curproxy) {
2087 Alert("parsing %s : loop detected for backend %s !\n", file, exp->replace);
2088 cfgerr++;
2089 } else {
2090 free((void *)exp->replace);
2091 exp->replace = (const char *)target;
2092 }
2093 }
2094 }
Willy Tarreau2738a142006-07-08 17:28:09 +02002095 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
2096 (!curproxy->clitimeout || !curproxy->contimeout || !curproxy->srvtimeout)) {
2097 Warning("parsing %s : missing timeouts for listener '%s'.\n"
2098 " | While not properly invalid, you will certainly encounter various problems\n"
2099 " | with such a configuration. To fix this, please ensure that all following\n"
2100 " | values are set to a non-zero value: clitimeout, contimeout, srvtimeout.\n",
2101 file, curproxy->id);
2102 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002103
2104 if (curproxy->options & PR_O_SSL3_CHK) {
2105 curproxy->check_len = sizeof(sslv3_client_hello_pkt);
2106 curproxy->check_req = (char *)malloc(sizeof(sslv3_client_hello_pkt));
2107 memcpy(curproxy->check_req, sslv3_client_hello_pkt, sizeof(sslv3_client_hello_pkt));
2108 }
2109
Willy Tarreaubaaee002006-06-26 02:48:02 +02002110 /* first, we will invert the servers list order */
2111 newsrv = NULL;
2112 while (curproxy->srv) {
2113 struct server *next;
2114
2115 next = curproxy->srv->next;
2116 curproxy->srv->next = newsrv;
2117 newsrv = curproxy->srv;
2118 if (!next)
2119 break;
2120 curproxy->srv = next;
2121 }
2122
2123 /* now, newsrv == curproxy->srv */
2124 if (newsrv) {
2125 struct server *srv;
2126 int pgcd;
2127 int act, bck;
2128
2129 /* We will factor the weights to reduce the table,
2130 * using Euclide's largest common divisor algorithm
2131 */
2132 pgcd = newsrv->uweight + 1;
2133 for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) {
2134 int t, w;
2135
2136 w = srv->uweight + 1;
2137 while (w) {
2138 t = pgcd % w;
2139 pgcd = w;
2140 w = t;
2141 }
2142 }
2143
2144 act = bck = 0;
2145 for (srv = newsrv; srv; srv = srv->next) {
2146 srv->eweight = ((srv->uweight + 1) / pgcd) - 1;
2147 if (srv->state & SRV_BACKUP)
2148 bck += srv->eweight + 1;
2149 else
2150 act += srv->eweight + 1;
2151 }
2152
2153 /* this is the largest map we will ever need for this servers list */
2154 if (act < bck)
2155 act = bck;
2156
2157 curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *));
2158 /* recounts servers and their weights */
2159 recount_servers(curproxy);
2160 recalc_server_map(curproxy);
2161 }
2162
2163 if (curproxy->options & PR_O_LOGASAP)
2164 curproxy->to_log &= ~LW_BYTES;
2165
2166 if (curproxy->errmsg.msg400 == NULL) {
2167 curproxy->errmsg.msg400 = (char *)HTTP_400;
2168 curproxy->errmsg.len400 = strlen(HTTP_400);
2169 }
2170 if (curproxy->errmsg.msg403 == NULL) {
2171 curproxy->errmsg.msg403 = (char *)HTTP_403;
2172 curproxy->errmsg.len403 = strlen(HTTP_403);
2173 }
2174 if (curproxy->errmsg.msg408 == NULL) {
2175 curproxy->errmsg.msg408 = (char *)HTTP_408;
2176 curproxy->errmsg.len408 = strlen(HTTP_408);
2177 }
2178 if (curproxy->errmsg.msg500 == NULL) {
2179 curproxy->errmsg.msg500 = (char *)HTTP_500;
2180 curproxy->errmsg.len500 = strlen(HTTP_500);
2181 }
2182 if (curproxy->errmsg.msg502 == NULL) {
2183 curproxy->errmsg.msg502 = (char *)HTTP_502;
2184 curproxy->errmsg.len502 = strlen(HTTP_502);
2185 }
2186 if (curproxy->errmsg.msg503 == NULL) {
2187 curproxy->errmsg.msg503 = (char *)HTTP_503;
2188 curproxy->errmsg.len503 = strlen(HTTP_503);
2189 }
2190 if (curproxy->errmsg.msg504 == NULL) {
2191 curproxy->errmsg.msg504 = (char *)HTTP_504;
2192 curproxy->errmsg.len504 = strlen(HTTP_504);
2193 }
2194
2195 /*
2196 * If this server supports a maxconn parameter, it needs a dedicated
2197 * tasks to fill the emptied slots when a connection leaves.
2198 */
2199 newsrv = curproxy->srv;
2200 while (newsrv != NULL) {
2201 if (newsrv->minconn >= newsrv->maxconn) {
2202 /* Only 'minconn' was specified, or it was higher than or equal
2203 * to 'maxconn'. Let's turn this into maxconn and clean it, as
2204 * this will avoid further useless expensive computations.
2205 */
2206 newsrv->maxconn = newsrv->minconn;
2207 newsrv->minconn = 0;
2208 }
2209
2210 if (newsrv->maxconn > 0) {
2211 struct task *t;
2212
2213 if ((t = pool_alloc(task)) == NULL) {
2214 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2215 return -1;
2216 }
2217
2218 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2219 t->wq = LIST_HEAD(wait_queue[1]); /* already assigned to the eternity queue */
2220 t->state = TASK_IDLE;
2221 t->process = process_srv_queue;
2222 t->context = newsrv;
2223 newsrv->queue_mgt = t;
2224
2225 /* never run it unless specifically woken up */
2226 tv_eternity(&t->expire);
2227 task_queue(t);
2228 }
2229 newsrv = newsrv->next;
2230 }
2231
2232 /* now we'll start this proxy's health checks if any */
2233 /* 1- count the checkers to run simultaneously */
2234 nbchk = 0;
2235 mininter = 0;
2236 newsrv = curproxy->srv;
2237 while (newsrv != NULL) {
2238 if (newsrv->state & SRV_CHECKED) {
2239 if (!mininter || mininter > newsrv->inter)
2240 mininter = newsrv->inter;
2241 nbchk++;
2242 }
2243 newsrv = newsrv->next;
2244 }
2245
2246 /* 2- start them as far as possible from each others while respecting
2247 * their own intervals. For this, we will start them after their own
2248 * interval added to the min interval divided by the number of servers,
2249 * weighted by the server's position in the list.
2250 */
2251 if (nbchk > 0) {
2252 struct task *t;
2253 int srvpos;
2254
2255 newsrv = curproxy->srv;
2256 srvpos = 0;
2257 while (newsrv != NULL) {
2258 /* should this server be checked ? */
2259 if (newsrv->state & SRV_CHECKED) {
2260 if ((t = pool_alloc(task)) == NULL) {
2261 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2262 return -1;
2263 }
2264
2265 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
2266 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
2267 t->state = TASK_IDLE;
2268 t->process = process_chk;
2269 t->context = newsrv;
2270
2271 /* check this every ms */
2272 tv_delayfrom(&t->expire, &now,
2273 newsrv->inter + mininter * srvpos / nbchk);
2274 task_queue(t);
2275 //task_wakeup(&rq, t);
2276 srvpos++;
2277 }
2278 newsrv = newsrv->next;
2279 }
2280 }
2281
2282 curproxy = curproxy->next;
2283 }
2284 if (cfgerr > 0) {
2285 Alert("Errors found in configuration file, aborting.\n");
2286 return -1;
2287 }
2288 else
2289 return 0;
2290}
2291
2292
2293
2294/*
2295 * Local variables:
2296 * c-indent-level: 8
2297 * c-basic-offset: 8
2298 * End:
2299 */