masqmail

view src/conf.c @ 392:c5fd796ea06e

Heavy refactoring in parts of conf.c. init_conf() parse_boolean() parse_list_file() Re-arrangement of code. parse_address_glob_list() Removed unneccessary parameter. parse_list() parse_interface(): Use strtok()/strchr() instead of doing is all by hand. Removed limitation of fixed size buffer. eat_comments() Use a state machine. eat_line_trailing() eat_spaces() read_lval() Better structured code. read_conf() read_route() Removed magic numbers. Made all list type in the config files accept pathname entries, except for `permanent_routes' and `query_routes.' for which this is impossible.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 18:07:55 +0100
parents a408411ff8df
children 5e728dd64c1b
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include <pwd.h>
22 #include <grp.h>
24 #include "masqmail.h"
26 masqmail_conf conf;
28 void
29 init_conf()
30 {
31 struct passwd *passwd;
32 struct group *group;
34 if (!(passwd = getpwnam(DEF_MAIL_USER))) {
35 fprintf(stderr, "user %s not found! (terminating)\n",
36 DEF_MAIL_USER);
37 exit(1);
38 }
39 if (!(group = getgrnam(DEF_MAIL_GROUP))) {
40 fprintf(stderr, "group %s not found! (terminating)\n",
41 DEF_MAIL_GROUP);
42 exit(1);
43 }
44 memset(&conf, 0, sizeof(masqmail_conf));
45 conf.orig_uid = getuid();
46 conf.orig_gid = getgid();
47 conf.mail_uid = passwd->pw_uid;
48 conf.mail_gid = group->gr_gid;
49 }
51 static gchar *true_strings[] = {
52 "yes", "on", "true", NULL
53 };
55 static gchar *false_strings[] = {
56 "no", "off", "false", NULL
57 };
59 static gboolean
60 parse_boolean(gchar *rval)
61 {
62 gchar **str;
64 DEBUG(9) fprintf(stderr, "parse_boolean: %s\n", rval);
65 for (str = true_strings; *str; str++) {
66 if (strncasecmp(*str, rval, strlen(*str))==0) {
67 return TRUE;
68 }
69 }
70 for (str = false_strings; *str; str++) {
71 if (strncasecmp(*str, rval, strlen(*str))==0) {
72 return FALSE;
73 }
74 }
75 fprintf(stderr, "cannot parse value '%s'\n", rval);
76 exit(1);
77 }
79 /*
80 ** make a list from the lines of a file
81 */
82 static GList*
83 parse_list_file(const gchar *fname)
84 {
85 GList *list = NULL;
86 FILE *fptr;
87 gchar buf[256];
89 if (!(fptr = fopen(fname, "rt"))) {
90 logwrite(LOG_ALERT, "could not open %s for reading: %s\n",
91 fname, strerror(errno));
92 exit(1);
93 }
94 while (!fgets(buf, sizeof buf, fptr)) {
95 g_strstrip(buf);
96 if (!*buf || *buf == '#') {
97 continue;
98 }
99 DEBUG(9) fprintf(stderr, "parse_list_file: item = %s\n", buf);
100 list = g_list_append(list, g_strdup(buf));
101 }
102 fclose(fptr);
104 return list;
105 }
107 /*
108 ** given a semicolon separated string, this function makes a GList out of it.
109 */
110 static GList*
111 parse_list(gchar *line, gboolean read_file)
112 {
113 GList *list = NULL;
114 gchar *tok;
116 DEBUG(9) fprintf(stderr, "parsing list %s, file?:%d\n",
117 line, read_file);
118 for (tok = strtok(strdup(line), ";"); tok; tok = strtok(NULL, ";")) {
119 DEBUG(9) fprintf(stderr, "item = %s\n", tok);
120 if (read_file && *tok == '/') {
121 /* item is a filename, include its contents */
122 list = g_list_concat(list, parse_list_file(tok));
123 } else {
124 /* just a normal item */
125 list = g_list_append(list, g_strdup(tok));
126 }
127 }
128 return list;
129 }
131 /*
132 ** Split the addrs at '@' into local_part and domain. Without an '@'
133 ** everything is local_part. Create and return a list of address structs.
134 ** This funktion is used for lists of addrs containing globbing chars
135 ** (* and ?). We don't need valid RFC821 addresses here, just patterns
136 ** to match against.
137 */
138 static GList*
139 parse_address_glob_list(gchar *line)
140 {
141 GList *plain_list = parse_list(line, TRUE);
142 GList *node;
143 GList *list = NULL;
145 foreach(plain_list, node) {
146 gchar *item = (gchar *) (node->data);
147 char *at;
148 char *p;
149 address *addr = calloc(1, sizeof(address));
151 for (p=item+strlen(item)-1; isspace(*p) || *p=='>'; p--) {
152 *p = '\0';
153 }
154 for (p=item; isspace(*p) || *p=='<'; p++) {
155 }
157 addr->address = strdup(p);
158 at = strrchr(p, '@');
159 if (at) {
160 *at = '\0';
161 addr->local_part = strdup(p);
162 addr->domain = strdup(at+1);
163 } else {
164 addr->local_part = strdup(p);
165 /* No `@', thus any domain is okay. */
166 addr->domain = "*";
167 }
168 list = g_list_append(list, addr);
169 DEBUG(6) debugf("parse_address_glob_list: "
170 "read pattern `%s' `%s'\n",
171 addr->local_part, addr->domain);
172 g_free(item);
173 }
174 g_list_free(plain_list);
175 return list;
176 }
178 static GList*
179 parse_resolve_list(gchar *line)
180 {
181 GList *list;
182 GList *list_node;
183 GList *res_list = NULL;
184 gchar *item;
186 list = parse_list(line, TRUE);
187 if (!list) {
188 return NULL;
189 }
190 foreach(list, list_node) {
191 item = (gchar *) list_node->data;
192 if (strcmp(item, "byname")==0) {
193 res_list = g_list_append(res_list, resolve_byname);
194 #ifdef ENABLE_RESOLVER
195 } else if (strcmp(item, "dns_a")==0) {
196 res_list = g_list_append(res_list, resolve_dns_a);
197 } else if (strcmp(item, "dns_mx")==0) {
198 res_list = g_list_append(res_list, resolve_dns_mx);
199 #endif
200 } else {
201 logwrite(LOG_ALERT, "unknown resolver %s\n", item);
202 exit(1);
203 }
204 g_free(item);
205 }
206 g_list_free(list);
207 return res_list;
208 }
210 static interface*
211 parse_interface(gchar *line, gint def_port)
212 {
213 gchar *cp;
214 interface *iface = g_malloc(sizeof(interface));
216 DEBUG(9) fprintf(stderr, "parse_interface: %s\n", line);
217 if ((cp = strchr(line, ':'))) {
218 *cp = '\0';
219 }
220 iface->address = g_strdup(line);
221 iface->port = (cp) ? atoi(++cp) : def_port;
222 DEBUG(9) fprintf(stderr,"found: address:port=%s:%u\n",
223 iface->address, iface->port);
224 return iface;
225 }
227 static gboolean
228 eat_comments(FILE *in)
229 {
230 gint c;
231 int incomment = 0;
233 while ((c = fgetc(in)) != EOF) {
234 if (incomment) {
235 /* eat until end of line */
236 if (c == '\n') {
237 incomment = 0;
238 continue;
239 } else {
240 continue;
241 }
242 } else {
243 /* eat whitespace and watch for comments */
244 if (isspace(c)) {
245 continue;
246 } else if (c == '#') {
247 incomment = 1;
248 continue;
249 } else {
250 /* found something (that's not our business) */
251 ungetc(c, in);
252 return TRUE;
253 }
254 }
255 }
256 return FALSE;
257 }
259 /*
260 ** after parsing, eat trailing characters until and including the Newline
261 */
262 static gboolean
263 eat_line_trailing(FILE *in)
264 {
265 gint c;
267 while ((c = fgetc(in)) != EOF) {
268 if (c == '\n') {
269 return TRUE;
270 }
271 }
272 return FALSE;
273 }
275 static gboolean
276 eat_spaces(FILE *in)
277 {
278 gint c;
280 while ((c = fgetc(in)) != EOF) {
281 if (!isspace(c)) {
282 ungetc(c, in);
283 return TRUE;
284 }
285 }
286 return FALSE;
287 }
289 static gboolean
290 read_lval(FILE *in, gchar *buf, gint size)
291 {
292 gint c;
293 gchar *ptr = buf;
295 DEBUG(9) fprintf(stderr, "read_lval()\n");
296 if (!eat_spaces(in)) {
297 return FALSE;
298 }
300 c = fgetc(in);
301 DEBUG(9) fprintf(stderr, "read_lval() 2\n");
302 while (1) {
303 if ((c = fgetc(in)) == EOF) {
304 fprintf(stderr, "unexpected EOF after %s\n", buf);
305 return FALSE;
306 }
307 if (ptr >= buf+size-1) {
308 fprintf(stderr, "lval too long\n");
309 break;
310 }
311 if (!isalnum(c) && c != '_' && c != '-' && c != '.') {
312 break;
313 }
314 *ptr++ = c;
315 }
316 *ptr = '\0';
317 ungetc(c, in);
318 eat_spaces(in);
319 DEBUG(9) fprintf(stderr, "lval = %s\n", buf);
320 return *buf != '\0';
321 }
323 static gboolean
324 read_rval(FILE *in, gchar *buf, gint size)
325 {
326 gint c;
327 gchar *ptr = buf;
329 DEBUG(9) fprintf(stderr, "read_rval()\n");
330 if (!eat_spaces(in)) {
331 return FALSE;
332 }
334 c = fgetc(in);
335 if (c != '\"') {
336 while ((isalnum(c) || c == '_' || c == '-' || c == '.'
337 || c == '/' || c == '@' || c == ';' || c == ':')
338 && (ptr < buf + size - 1)
339 && (c != EOF)) {
340 *ptr = c;
341 ptr++;
342 c = fgetc(in);
343 }
344 *ptr = '\0';
345 ungetc(c, in);
346 } else {
347 gboolean escape = FALSE;
348 c = fgetc(in);
349 while (((c != '\"') || escape) && (ptr < buf + size - 1)) {
350 if (c != '\n') { /* ignore line breaks */
351 if ((c == '\\') && (!escape)) {
352 escape = TRUE;
353 } else {
354 *ptr = c;
355 ptr++;
356 escape = FALSE;
357 }
358 }
359 c = fgetc(in);
360 }
361 *ptr = '\0';
362 }
364 eat_line_trailing(in);
366 DEBUG(9) fprintf(stderr, "rval = %s\n", buf);
368 return TRUE;
369 }
371 static gboolean
372 read_statement(FILE *in, gchar *lval, gint lsize, gchar *rval, gint rsize)
373 {
374 gint c;
376 DEBUG(9) fprintf(stderr, "read_statement()\n");
378 /* eat comments and empty lines: */
379 if (!eat_comments(in))
380 return FALSE;
382 if (!read_lval(in, lval, lsize)) {
383 return FALSE;
384 }
386 DEBUG(9) fprintf(stderr, " lval = %s\n", lval);
387 if ((c = fgetc(in) == '=')) {
388 if (read_rval(in, rval, rsize)) {
389 DEBUG(9) fprintf(stderr, " rval = %s\n", rval);
390 return TRUE;
391 }
392 } else {
393 DEBUG(9) fprintf(stderr," '=' expected after %s, char was '%c'\n", lval, c);
394 fprintf(stderr, "'=' expected after %s, char was '%c'\n", lval, c);
395 }
396 return FALSE;
397 }
399 gboolean
400 read_conf(gchar *filename)
401 {
402 FILE *in;
404 conf.log_max_pri = 7;
405 conf.do_relay = TRUE;
406 conf.localpartcmp = strcmp;
407 conf.max_defer_time = 86400 * 4; /* 4 days */
408 conf.max_msg_size = 0; /* no limit on msg size */
409 conf.spool_dir = SPOOL_DIR;
410 conf.mail_dir = "/var/mail";
411 conf.listen_addresses = g_list_append(NULL, parse_interface("localhost", 25));
413 if ((in = fopen(filename, "r")) == NULL) {
414 logwrite(LOG_ALERT, "could not open config file %s: %s\n", filename, strerror(errno));
415 return FALSE;
416 }
418 gchar lval[256], rval[2048];
419 while (read_statement(in, lval, sizeof lval, rval, sizeof rval)) {
420 DEBUG(9) fprintf(stderr,"read_conf(): lval=%s\n", lval);
421 if (strcmp(lval, "debug_level") == 0)
422 conf.debug_level = atoi(rval);
423 else if (strcmp(lval, "run_as_user") == 0) {
424 if (!conf.run_as_user) /* you should not be able to reset that flag */
425 conf.run_as_user = parse_boolean(rval);
426 } else if (strcmp(lval, "use_syslog") == 0)
427 conf.use_syslog = parse_boolean(rval);
428 else if (strcmp(lval, "mail_dir") == 0)
429 conf.mail_dir = g_strdup(rval);
430 else if (strcmp(lval, "lock_dir") == 0)
431 conf.lock_dir = g_strdup(rval);
432 else if (strcmp(lval, "spool_dir") == 0)
433 conf.spool_dir = g_strdup(rval);
434 else if (strcmp(lval, "log_dir") == 0)
435 conf.log_dir = g_strdup(rval);
436 else if (strcmp(lval, "host_name") == 0) {
437 if (rval[0] != '/')
438 conf.host_name = g_strdup(rval);
439 else {
440 char buf[256];
441 FILE *fptr = fopen(rval, "rt");
442 if (!fptr) {
443 logwrite(LOG_ALERT, "could not open %s: %s\n", rval, strerror(errno));
444 return FALSE;
445 }
446 fgets(buf, 255, fptr);
447 g_strchomp(buf);
448 conf.host_name = g_strdup(buf);
449 fclose(fptr);
450 }
451 } else if (strcmp(lval, "local_hosts") == 0)
452 conf.local_hosts = parse_list(rval, TRUE);
453 else if (strcmp(lval, "local_addresses") == 0)
454 conf.local_addresses = parse_list(rval, TRUE);
455 else if (strcmp(lval, "not_local_addresses") == 0)
456 conf.not_local_addresses = parse_list(rval, TRUE);
457 else if (strcmp(lval, "do_save_envelope_to") == 0)
458 conf.do_save_envelope_to = parse_boolean(rval);
459 else if (strcmp(lval, "defer_all") == 0)
460 conf.defer_all = parse_boolean(rval);
461 else if (strcmp(lval, "do_relay") == 0)
462 conf.do_relay = parse_boolean(rval);
463 else if (strcmp(lval, "alias_file") == 0) {
464 conf.alias_file = g_strdup(rval);
465 } else if (strcmp(lval, "globalias_file") == 0) {
466 conf.globalias_file = g_strdup(rval);
467 } else if (strcmp(lval, "caseless_matching") == 0) {
468 conf.localpartcmp = parse_boolean(rval) ? strcasecmp : strcmp;
469 } else if (strcmp(lval, "mbox_default") == 0) {
470 conf.mbox_default = g_strdup(rval);
471 } else if (strcmp(lval, "mbox_users") == 0) {
472 conf.mbox_users = parse_list(rval, TRUE);
473 } else if (strcmp(lval, "mda_users") == 0) {
474 conf.mda_users = parse_list(rval, TRUE);
475 } else if (strcmp(lval, "mda") == 0) {
476 conf.mda = g_strdup(rval);
477 } else if (strcmp(lval, "mda_fromline") == 0) {
478 conf.mda_fromline = parse_boolean(rval);
479 } else if (strcmp(lval, "mda_fromhack") == 0) {
480 conf.mda_fromhack = parse_boolean(rval);
481 } else if (strcmp(lval, "pipe_fromline") == 0) {
482 conf.pipe_fromline = parse_boolean(rval);
483 } else if (strcmp(lval, "pipe_fromhack") == 0) {
484 conf.pipe_fromhack = parse_boolean(rval);
485 } else if (strcmp(lval, "listen_addresses") == 0) {
486 GList *node;
487 GList *tmp_list = parse_list(rval, TRUE);
489 conf.listen_addresses = NULL;
490 foreach(tmp_list, node) {
491 conf.listen_addresses = g_list_append(conf.listen_addresses, parse_interface((gchar *) (node-> data), 25));
492 g_free(node->data);
493 }
494 g_list_free(tmp_list);
495 } else if (strncmp(lval, "query_routes.", 13) == 0) {
496 GList *file_list = parse_list(rval, FALSE);
497 table_pair *pair = create_pair(lval+13, file_list);
498 conf.query_routes = g_list_append(conf.query_routes, pair);
499 } else if (strcmp(lval, "permanent_routes") == 0) {
500 conf.perma_routes = parse_list(rval, FALSE);
501 } else if (strcmp(lval, "online_query") == 0)
502 conf.online_query = g_strdup(rval);
503 else if (strcmp(lval, "do_queue") == 0)
504 conf.do_queue = parse_boolean(rval);
505 else if (strcmp(lval, "errmsg_file") == 0)
506 conf.errmsg_file = g_strdup(rval);
507 else if (strcmp(lval, "warnmsg_file") == 0)
508 conf.warnmsg_file = g_strdup(rval);
509 else if (strcmp(lval, "warn_intervals") == 0)
510 conf.warn_intervals = parse_list(rval, TRUE);
511 else if (strcmp(lval, "max_defer_time") == 0) {
512 gint ival = time_interval(rval);
513 if (ival < 0)
514 logwrite(LOG_WARNING, "invalid time interval for 'max_defer_time': %s\n", rval);
515 else
516 conf.max_defer_time = ival;
517 } else if (strcmp(lval, "log_user") == 0)
518 conf.log_user = g_strdup(rval);
519 else if(strcmp(lval, "max_msg_size") == 0) {
520 conf.max_msg_size = atol(rval);
521 DEBUG(9) fprintf(stderr,"rval=%s, conf.max_msg_size=%ld\n",
522 rval, conf.max_msg_size);
523 }
524 else
525 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
526 }
527 fclose(in);
529 if (!conf.host_name) {
530 logwrite(LOG_ALERT, "`host_name' MUST be set in masqmail.conf. See man page\n");
531 return FALSE;
532 }
534 if (conf.errmsg_file == NULL)
535 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
536 if (conf.warnmsg_file == NULL)
537 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
539 if (conf.lock_dir == NULL)
540 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
542 if (conf.mbox_default == NULL)
543 conf.mbox_default = g_strdup("mbox");
545 if (conf.warn_intervals == NULL)
546 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", TRUE);
548 if (!conf.local_hosts) {
549 char *shortname = strdup(conf.host_name);
550 char *p = strchr(shortname, '.');
551 if (p) {
552 *p = '\0';
553 }
554 /* don't care if shortname and conf.host_name are the same */
555 char *local_hosts_str = g_strdup_printf("localhost;%s;%s",
556 shortname, conf.host_name);
557 conf.local_hosts = parse_list(local_hosts_str, TRUE);
558 free(shortname);
559 free(local_hosts_str);
560 }
563 return TRUE;
564 }
566 connect_route*
567 read_route(gchar *filename, gboolean is_perma)
568 {
569 gboolean ok = FALSE;
570 FILE *in;
572 connect_route *route = g_malloc(sizeof(connect_route));
573 memset(route, 0, sizeof(connect_route));
575 DEBUG(5) debugf("read_route, filename = %s\n", filename);
577 route->filename = g_strdup(filename);
578 route->name = route->filename; /* quick hack */
580 route->expand_h_sender_address = TRUE;
582 route->is_perma = is_perma;
584 route->do_pipelining = TRUE;
586 if ((in = fopen(route->filename, "r")) == NULL) {
587 logwrite(LOG_ALERT, "could not open route file %s: %s\n", route->filename, strerror(errno));
588 g_free(route);
589 return NULL;
590 }
592 gchar lval[256], rval[2048];
593 while (read_statement(in, lval, sizeof lval, rval, sizeof rval)) {
594 if (strcmp(lval, "mail_host") == 0)
595 route->mail_host = parse_interface(rval, 25);
596 else if (strcmp(lval, "helo_name") == 0)
597 route->helo_name = g_strdup(rval);
598 else if (strcmp(lval, "wrapper") == 0)
599 route->wrapper = g_strdup(rval);
600 else if (strcmp(lval, "connect_error_fail") == 0)
601 route->connect_error_fail = parse_boolean(rval);
602 else if (strcmp(lval, "do_correct_helo") == 0)
603 route->do_correct_helo = parse_boolean(rval);
604 else if (strcmp(lval, "instant_helo") == 0)
605 route->instant_helo = parse_boolean(rval);
606 else if (strcmp(lval, "do_pipelining") == 0)
607 route->do_pipelining = parse_boolean(rval);
609 else if (strcmp(lval, "allowed_senders") == 0)
610 route->allowed_senders = parse_address_glob_list(rval);
611 else if (strcmp(lval, "denied_senders") == 0)
612 route->denied_senders = parse_address_glob_list(rval);
613 else if (strcmp(lval, "allowed_recipients") == 0)
614 route->allowed_recipients = parse_address_glob_list(rval);
615 else if (strcmp(lval, "denied_recipients") == 0)
616 route->denied_recipients = parse_address_glob_list(rval);
618 else if (strcmp(lval, "set_h_from_domain") == 0)
619 route->set_h_from_domain = g_strdup(rval);
620 else if (strcmp(lval, "set_h_reply_to_domain") == 0)
621 route->set_h_reply_to_domain = g_strdup(rval);
622 else if (strcmp(lval, "set_return_path_domain") == 0)
623 route->set_return_path_domain = g_strdup(rval);
624 else if (strcmp(lval, "map_return_path_addresses") == 0) {
625 GList *node, *list;
627 list = parse_list(rval, TRUE);
628 foreach(list, node) {
629 gchar *item = (gchar *) (node->data);
630 table_pair *pair = parse_table_pair(item, ':');
631 address *addr = create_address((gchar *) (pair->value), TRUE);
632 g_free(pair->value);
633 pair->value = (gpointer *) addr;
634 route->map_return_path_addresses = g_list_append(route->map_return_path_addresses, pair);
635 g_free(item);
636 }
637 g_list_free(list);
638 } else if (strcmp(lval, "map_h_from_addresses") == 0) {
639 GList *list, *node;
641 list = parse_list(rval, TRUE);
642 foreach(list, node) {
643 gchar *item = (gchar *) (node->data);
644 table_pair *pair = parse_table_pair(item, ':');
645 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
646 g_free(item);
647 }
648 g_list_free(list);
649 } else if (strcmp(lval, "map_h_reply_to_addresses") == 0) {
650 GList *list, *node;
652 list = parse_list(rval, TRUE);
653 foreach(list, node) {
654 gchar *item = (gchar *) (node->data);
655 table_pair *pair = parse_table_pair(item, ':');
656 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
657 g_free(item);
658 }
659 g_list_free(list);
660 } else if (strcmp(lval, "map_h_mail_followup_to_addresses") == 0) {
661 GList *list, *node;
663 list = parse_list(rval, TRUE);
664 foreach(list, node) {
665 gchar *item = (gchar *) (node->data);
666 table_pair *pair = parse_table_pair(item, ':');
667 route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
668 g_free(item);
669 }
670 g_list_free(list);
671 } else if (strcmp(lval, "expand_h_sender_domain") == 0) {
672 route->expand_h_sender_domain = parse_boolean(rval);
673 } else if (strcmp(lval, "expand_h_sender_address") == 0) {
674 route->expand_h_sender_address = parse_boolean(rval);
675 } else if (strcmp(lval, "resolve_list") == 0)
676 route->resolve_list = parse_resolve_list(rval);
677 else if (strcmp(lval, "do_ssl") == 0) {
678 /* we ignore this. This option is used by sqilconf */
679 ;
680 }
681 #ifdef ENABLE_AUTH
682 else if (strcmp(lval, "auth_name") == 0) {
683 route->auth_name = g_strdup(rval);
684 } else if (strcmp(lval, "auth_login") == 0) {
685 route->auth_login = g_strdup(rval);
686 } else if (strcmp(lval, "auth_secret") == 0) {
687 route->auth_secret = g_strdup(rval);
688 }
689 #else
690 else if ((strcmp(lval, "auth_name") == 0)
691 || (strcmp(lval, "auth_login") == 0)
692 || (strcmp(lval, "auth_secret") == 0)) {
693 logwrite(LOG_WARNING, "%s ignored: not compiled with auth support.\n", lval);
694 }
695 #endif
696 else if (strcmp(lval, "pipe") == 0) {
697 route->pipe = g_strdup(rval);
698 } else if (strcmp(lval, "pipe_fromline") == 0) {
699 route->pipe_fromline = parse_boolean(rval);
700 } else if (strcmp(lval, "pipe_fromhack") == 0) {
701 route->pipe_fromhack = parse_boolean(rval);
702 } else if (strcmp(lval, "last_route") == 0) {
703 route->last_route = parse_boolean(rval);
704 } else
705 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
706 }
708 if (!route->resolve_list) {
709 #ifdef ENABLE_RESOLVER
710 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_mx);
711 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_a);
712 #endif
713 route->resolve_list = g_list_append(route->resolve_list, resolve_byname);
714 }
715 fclose(in);
716 ok = TRUE;
718 /* warn user about misconfigurations: */
719 if ((route->map_h_from_addresses != NULL) && (route->set_h_from_domain != NULL)) {
720 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides 'set_h_from_domain'\n");
721 g_free(route->set_h_from_domain);
722 route->set_h_from_domain = NULL;
723 }
724 if ((route->map_h_reply_to_addresses != NULL) && (route->set_h_reply_to_domain != NULL)) {
725 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides 'set_h_reply_to_domain'\n");
726 g_free(route->set_h_reply_to_domain);
727 route->set_h_reply_to_domain = NULL;
728 }
730 if (!ok) {
731 g_free(route);
732 route = NULL;
733 }
735 return route;
736 }
738 static void
739 _g_list_free_all(GList *list)
740 {
741 GList *node;
742 if (list) {
743 foreach(list, node)
744 g_free(node->data);
745 g_list_free(list);
746 }
747 }
749 void
750 destroy_route(connect_route *r)
751 {
752 if (r->filename)
753 g_free(r->filename);
754 if (r->mail_host) {
755 g_free(r->mail_host->address);
756 g_free(r->mail_host);
757 }
758 if (r->wrapper)
759 g_free(r->wrapper);
760 if (r->helo_name)
761 g_free(r->helo_name);
762 _g_list_free_all(r->allowed_senders);
763 _g_list_free_all(r->denied_senders);
764 _g_list_free_all(r->allowed_recipients);
765 _g_list_free_all(r->denied_recipients);
766 if (r->set_h_from_domain)
767 g_free(r->set_h_from_domain);
768 if (r->set_h_reply_to_domain)
769 g_free(r->set_h_reply_to_domain);
770 if (r->set_return_path_domain)
771 g_free(r->set_return_path_domain);
772 if (r->map_h_reply_to_addresses)
773 destroy_table(r->map_h_reply_to_addresses);
774 if (r->resolve_list)
775 g_list_free(r->resolve_list);
776 #ifdef ENABLE_AUTH
777 if (r->auth_name)
778 g_free(r->auth_name);
779 if (r->auth_login)
780 g_free(r->auth_login);
781 if (r->auth_secret)
782 g_free(r->auth_secret);
783 #endif
784 if (r->pipe)
785 g_free(r->pipe);
786 g_free(r);
787 }
789 GList*
790 read_route_list(GList *rf_list, gboolean is_perma)
791 {
792 GList *list = NULL;
793 GList *node;
794 uid_t saved_uid, saved_gid;
796 if (!conf.run_as_user) {
797 set_euidgid(0, 0, &saved_uid, &saved_gid);
798 }
800 foreach(rf_list, node) {
801 gchar *fname = (gchar *) (node->data);
802 connect_route *route = read_route(fname, is_perma);
803 if (route)
804 list = g_list_append(list, route);
805 else
806 logwrite(LOG_ALERT, "could not read route configuration %s\n", fname);
807 }
809 /* set uid and gid back */
810 if (!conf.run_as_user) {
811 set_euidgid(saved_uid, saved_gid, NULL, NULL);
812 }
814 return list;
815 }
817 void
818 destroy_route_list(GList *list)
819 {
820 GList *node;
822 foreach(list, node) {
823 connect_route *route = (connect_route *) (node->data);
824 destroy_route(route);
825 }
826 g_list_free(list);
827 }