masqmail

view src/conf.c @ 393:5e728dd64c1b

Various refactoring and code layouting in the second half of conf.c.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 18:48:19 +0100
parents c5fd796ea06e
children c8e3d1a79313
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;
381 }
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 fprintf(stderr, "'=' expected after %s, char was '%c'\n",
389 lval, c);
390 }
391 if (!read_rval(in, rval, rsize)) {
392 return FALSE;
393 }
394 DEBUG(9) fprintf(stderr, " rval = %s\n", rval);
395 return TRUE;
396 }
398 gboolean
399 read_conf(gchar *filename)
400 {
401 FILE *in;
402 gchar lval[256], rval[2048];
403 GList *listen_addrs_tmp = NULL;
405 conf.log_max_pri = 7;
406 conf.do_relay = TRUE;
407 conf.localpartcmp = strcmp;
408 conf.max_defer_time = 86400 * 4; /* 4 days */
409 conf.max_msg_size = 0; /* no limit on msg size */
410 conf.spool_dir = SPOOL_DIR;
411 conf.mail_dir = "/var/mail";
413 if (!(in = fopen(filename, "r"))) {
414 logwrite(LOG_ALERT, "could not open config file %s: %s\n",
415 filename, strerror(errno));
416 return FALSE;
417 }
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) {
425 /* you should not be able to reset that flag */
426 conf.run_as_user = parse_boolean(rval);
427 }
428 } else if (strcmp(lval, "use_syslog")==0) {
429 conf.use_syslog = parse_boolean(rval);
430 } else if (strcmp(lval, "mail_dir")==0) {
431 conf.mail_dir = g_strdup(rval);
432 } else if (strcmp(lval, "lock_dir")==0) {
433 conf.lock_dir = g_strdup(rval);
434 } else if (strcmp(lval, "spool_dir")==0) {
435 conf.spool_dir = g_strdup(rval);
436 } else if (strcmp(lval, "log_dir")==0) {
437 conf.log_dir = g_strdup(rval);
438 } else if (strcmp(lval, "host_name")==0) {
439 if (rval[0] != '/') {
440 conf.host_name = g_strdup(rval);
441 } else {
442 char buf[256];
443 FILE *fptr = fopen(rval, "rt");
444 if (!fptr) {
445 logwrite(LOG_ALERT, "could not open "
446 "%s: %s\n", rval,
447 strerror(errno));
448 return FALSE;
449 }
450 fgets(buf, sizeof buf, fptr);
451 g_strstrip(buf);
452 conf.host_name = g_strdup(buf);
453 fclose(fptr);
454 }
455 } else if (strcmp(lval, "local_hosts")==0) {
456 conf.local_hosts = parse_list(rval, TRUE);
457 } else if (strcmp(lval, "local_addresses")==0) {
458 conf.local_addresses = parse_list(rval, TRUE);
459 } else if (strcmp(lval, "not_local_addresses")==0) {
460 conf.not_local_addresses = parse_list(rval, TRUE);
461 } else if (strcmp(lval, "do_save_envelope_to")==0) {
462 conf.do_save_envelope_to = parse_boolean(rval);
463 } else if (strcmp(lval, "defer_all")==0) {
464 conf.defer_all = parse_boolean(rval);
465 } else if (strcmp(lval, "do_relay")==0) {
466 conf.do_relay = parse_boolean(rval);
467 } else if (strcmp(lval, "alias_file")==0) {
468 conf.alias_file = g_strdup(rval);
469 } else if (strcmp(lval, "globalias_file")==0) {
470 conf.globalias_file = g_strdup(rval);
471 } else if (strcmp(lval, "caseless_matching")==0) {
472 conf.localpartcmp = parse_boolean(rval) ?
473 strcasecmp : strcmp;
474 } else if (strcmp(lval, "mbox_default")==0) {
475 conf.mbox_default = g_strdup(rval);
476 } else if (strcmp(lval, "mbox_users")==0) {
477 conf.mbox_users = parse_list(rval, TRUE);
478 } else if (strcmp(lval, "mda_users")==0) {
479 conf.mda_users = parse_list(rval, TRUE);
480 } else if (strcmp(lval, "mda")==0) {
481 conf.mda = g_strdup(rval);
482 } else if (strcmp(lval, "mda_fromline")==0) {
483 conf.mda_fromline = parse_boolean(rval);
484 } else if (strcmp(lval, "mda_fromhack")==0) {
485 conf.mda_fromhack = parse_boolean(rval);
486 } else if (strcmp(lval, "pipe_fromline")==0) {
487 conf.pipe_fromline = parse_boolean(rval);
488 } else if (strcmp(lval, "pipe_fromhack")==0) {
489 conf.pipe_fromhack = parse_boolean(rval);
490 } else if (strcmp(lval, "listen_addresses")==0) {
491 listen_addrs_tmp = parse_list(rval, TRUE);
492 } else if (strncmp(lval, "query_routes.", 13)==0) {
493 GList *file_list = parse_list(rval, FALSE);
494 table_pair *pair = create_pair(lval+13, file_list);
495 conf.query_routes = g_list_append(conf.query_routes,
496 pair);
497 } else if (strcmp(lval, "permanent_routes")==0) {
498 conf.perma_routes = parse_list(rval, FALSE);
499 } else if (strcmp(lval, "online_query")==0) {
500 conf.online_query = g_strdup(rval);
501 } else if (strcmp(lval, "do_queue")==0) {
502 conf.do_queue = parse_boolean(rval);
503 } else if (strcmp(lval, "errmsg_file")==0) {
504 conf.errmsg_file = g_strdup(rval);
505 } else if (strcmp(lval, "warnmsg_file")==0) {
506 conf.warnmsg_file = g_strdup(rval);
507 } else if (strcmp(lval, "warn_intervals")==0) {
508 conf.warn_intervals = parse_list(rval, TRUE);
509 } else if (strcmp(lval, "max_defer_time")==0) {
510 gint ival = time_interval(rval);
511 if (ival < 0) {
512 logwrite(LOG_WARNING, "invalid time interval "
513 "for 'max_defer_time': %s\n",
514 rval);
515 } else {
516 conf.max_defer_time = ival;
517 }
518 } else if (strcmp(lval, "log_user")==0) {
519 conf.log_user = g_strdup(rval);
520 } else if(strcmp(lval, "max_msg_size")==0) {
521 conf.max_msg_size = atol(rval);
522 DEBUG(9) fprintf(stderr,
523 "rval=%s, conf.max_msg_size=%ld\n",
524 rval, conf.max_msg_size);
525 } else {
526 logwrite(LOG_WARNING, "var '%s' unknown: ignored\n",
527 lval);
528 }
529 }
530 fclose(in);
532 if (!conf.host_name) {
533 logwrite(LOG_ALERT, "`host_name' MUST be set in "
534 "masqmail.conf. See man page\n");
535 return FALSE;
536 }
537 if (!conf.errmsg_file) {
538 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
539 }
540 if (!conf.warnmsg_file) {
541 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
542 }
543 if (!conf.lock_dir) {
544 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
545 }
546 if (!conf.mbox_default) {
547 conf.mbox_default = g_strdup("mbox");
548 }
549 if (!conf.warn_intervals) {
550 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", TRUE);
551 }
552 if (!conf.local_hosts) {
553 char *shortname = strdup(conf.host_name);
554 char *p = strchr(shortname, '.');
555 if (p) {
556 *p = '\0';
557 }
558 /* don't care if shortname and conf.host_name are the same */
559 char *local_hosts_str = g_strdup_printf("localhost;%s;%s",
560 shortname, conf.host_name);
561 conf.local_hosts = parse_list(local_hosts_str, TRUE);
562 free(shortname);
563 free(local_hosts_str);
564 }
565 if (!listen_addrs_tmp) {
566 conf.listen_addresses = g_list_append(NULL,
567 parse_interface("localhost", 25));
568 } else {
569 GList *node;
571 foreach(listen_addrs_tmp, node) {
572 conf.listen_addresses =
573 g_list_append(conf.listen_addresses,
574 parse_interface((gchar *) node->data,
575 25));
576 g_free(node->data);
577 }
578 g_list_free(listen_addrs_tmp);
579 }
581 return TRUE;
582 }
584 connect_route*
585 read_route(gchar *filename, gboolean is_perma)
586 {
587 FILE *in;
588 connect_route *route;
589 gchar lval[256], rval[2048];
591 DEBUG(5) debugf("read_route, filename = %s\n", filename);
593 if (!(in = fopen(filename, "r"))) {
594 logwrite(LOG_ALERT, "could not open route file %s: %s\n",
595 filename, strerror(errno));
596 return NULL;
597 }
599 route = g_malloc(sizeof(connect_route));
600 memset(route, 0, sizeof(connect_route));
601 route->filename = g_strdup(filename);
602 route->name = route->filename; /* quick hack */
603 route->expand_h_sender_address = TRUE;
604 route->is_perma = is_perma;
605 route->do_pipelining = TRUE;
607 while (read_statement(in, lval, sizeof lval, rval, sizeof rval)) {
608 if (strcmp(lval, "mail_host")==0) {
609 route->mail_host = parse_interface(rval, 25);
610 } else if (strcmp(lval, "helo_name")==0) {
611 route->helo_name = g_strdup(rval);
612 } else if (strcmp(lval, "wrapper")==0) {
613 route->wrapper = g_strdup(rval);
614 } else if (strcmp(lval, "connect_error_fail")==0) {
615 route->connect_error_fail = parse_boolean(rval);
616 } else if (strcmp(lval, "do_correct_helo")==0) {
617 route->do_correct_helo = parse_boolean(rval);
618 } else if (strcmp(lval, "instant_helo")==0) {
619 route->instant_helo = parse_boolean(rval);
620 } else if (strcmp(lval, "do_pipelining")==0) {
621 route->do_pipelining = parse_boolean(rval);
623 } else if (strcmp(lval, "allowed_senders")==0) {
624 route->allowed_senders = parse_address_glob_list(rval);
625 } else if (strcmp(lval, "denied_senders")==0) {
626 route->denied_senders = parse_address_glob_list(rval);
627 } else if (strcmp(lval, "allowed_recipients")==0) {
628 route->allowed_recipients = parse_address_glob_list(rval);
629 } else if (strcmp(lval, "denied_recipients")==0) {
630 route->denied_recipients = parse_address_glob_list(rval);
632 } else if (strcmp(lval, "set_h_from_domain")==0) {
633 route->set_h_from_domain = g_strdup(rval);
634 } else if (strcmp(lval, "set_h_reply_to_domain")==0) {
635 route->set_h_reply_to_domain = g_strdup(rval);
636 } else if (strcmp(lval, "set_return_path_domain")==0) {
637 route->set_return_path_domain = g_strdup(rval);
638 } else if (strcmp(lval, "map_return_path_addresses")==0) {
639 GList *node, *list;
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 address *addr = create_address(
646 (gchar *) (pair->value), TRUE);
647 g_free(pair->value);
648 pair->value = (gpointer *) addr;
649 route->map_return_path_addresses = g_list_append( route->map_return_path_addresses, pair);
650 g_free(item);
651 }
652 g_list_free(list);
653 } else if (strcmp(lval, "map_h_from_addresses")==0) {
654 GList *list, *node;
656 list = parse_list(rval, TRUE);
657 foreach(list, node) {
658 gchar *item = (gchar *) (node->data);
659 table_pair *pair = parse_table_pair(item, ':');
660 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
661 g_free(item);
662 }
663 g_list_free(list);
664 } else if (strcmp(lval, "map_h_reply_to_addresses")==0) {
665 GList *list, *node;
667 list = parse_list(rval, TRUE);
668 foreach(list, node) {
669 gchar *item = (gchar *) (node->data);
670 table_pair *pair = parse_table_pair(item, ':');
671 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
672 g_free(item);
673 }
674 g_list_free(list);
675 } else if (strcmp(lval, "map_h_mail_followup_to_addresses")==0) {
676 GList *list, *node;
678 list = parse_list(rval, TRUE);
679 foreach(list, node) {
680 gchar *item = (gchar *) (node->data);
681 table_pair *pair = parse_table_pair(item, ':');
682 route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
683 g_free(item);
684 }
685 g_list_free(list);
686 } else if (strcmp(lval, "expand_h_sender_domain")==0) {
687 route->expand_h_sender_domain = parse_boolean(rval);
688 } else if (strcmp(lval, "expand_h_sender_address")==0) {
689 route->expand_h_sender_address = parse_boolean(rval);
690 } else if (strcmp(lval, "resolve_list")==0) {
691 route->resolve_list = parse_resolve_list(rval);
692 } else if (strcmp(lval, "do_ssl")==0) {
693 /* we ignore this. This option is used by sqilconf */
694 ;
695 #ifdef ENABLE_AUTH
696 } else if (strcmp(lval, "auth_name")==0) {
697 route->auth_name = g_strdup(rval);
698 } else if (strcmp(lval, "auth_login")==0) {
699 route->auth_login = g_strdup(rval);
700 } else if (strcmp(lval, "auth_secret")==0) {
701 route->auth_secret = g_strdup(rval);
702 #else
703 } else if ((strcmp(lval, "auth_name")==0) ||
704 (strcmp(lval, "auth_login")==0) ||
705 (strcmp(lval, "auth_secret")==0)) {
706 logwrite(LOG_WARNING, "%s ignored: not compiled with "
707 "auth support.\n", lval);
708 }
709 #endif
710 } else if (strcmp(lval, "pipe")==0) {
711 route->pipe = g_strdup(rval);
712 } else if (strcmp(lval, "pipe_fromline")==0) {
713 route->pipe_fromline = parse_boolean(rval);
714 } else if (strcmp(lval, "pipe_fromhack")==0) {
715 route->pipe_fromhack = parse_boolean(rval);
716 } else if (strcmp(lval, "last_route")==0) {
717 route->last_route = parse_boolean(rval);
718 } else {
719 logwrite(LOG_WARNING, "var '%s' unknown: ignored\n",
720 lval);
721 }
722 }
724 if (!route->resolve_list) {
725 #ifdef ENABLE_RESOLVER
726 route->resolve_list = g_list_append(route->resolve_list,
727 resolve_dns_mx);
728 route->resolve_list = g_list_append(route->resolve_list,
729 resolve_dns_a);
730 #endif
731 route->resolve_list = g_list_append(route->resolve_list,
732 resolve_byname);
733 }
734 fclose(in);
736 /* warn user about mis-configurations: */
737 if (route->map_h_from_addresses && route->set_h_from_domain) {
738 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides "
739 "'set_h_from_domain'\n");
740 g_free(route->set_h_from_domain);
741 route->set_h_from_domain = NULL;
742 }
743 if (route->map_h_reply_to_addresses && route->set_h_reply_to_domain) {
744 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides "
745 "'set_h_reply_to_domain'\n");
746 g_free(route->set_h_reply_to_domain);
747 route->set_h_reply_to_domain = NULL;
748 }
750 return route;
751 }
753 static void
754 _g_list_free_all(GList *list)
755 {
756 GList *node;
757 if (!list) {
758 return;
759 }
760 foreach(list, node) {
761 g_free(node->data);
762 }
763 g_list_free(list);
764 }
766 void
767 destroy_route(connect_route *r)
768 {
769 if (r->filename) {
770 g_free(r->filename);
771 }
772 if (r->mail_host) {
773 g_free(r->mail_host->address);
774 g_free(r->mail_host);
775 }
776 if (r->wrapper) {
777 g_free(r->wrapper);
778 }
779 if (r->helo_name) {
780 g_free(r->helo_name);
781 }
782 _g_list_free_all(r->allowed_senders);
783 _g_list_free_all(r->denied_senders);
784 _g_list_free_all(r->allowed_recipients);
785 _g_list_free_all(r->denied_recipients);
786 if (r->set_h_from_domain) {
787 g_free(r->set_h_from_domain);
788 }
789 if (r->set_h_reply_to_domain) {
790 g_free(r->set_h_reply_to_domain);
791 }
792 if (r->set_return_path_domain) {
793 g_free(r->set_return_path_domain);
794 }
795 if (r->map_h_reply_to_addresses) {
796 destroy_table(r->map_h_reply_to_addresses);
797 }
798 if (r->resolve_list) {
799 g_list_free(r->resolve_list);
800 }
801 #ifdef ENABLE_AUTH
802 if (r->auth_name) {
803 g_free(r->auth_name);
804 }
805 if (r->auth_login) {
806 g_free(r->auth_login);
807 }
808 if (r->auth_secret) {
809 g_free(r->auth_secret);
810 }
811 #endif
812 if (r->pipe) {
813 g_free(r->pipe);
814 }
815 g_free(r);
816 }
818 GList*
819 read_route_list(GList *rf_list, gboolean is_perma)
820 {
821 GList *list = NULL;
822 GList *node;
823 uid_t saved_uid, saved_gid;
825 if (!conf.run_as_user) {
826 set_euidgid(0, 0, &saved_uid, &saved_gid);
827 }
828 foreach(rf_list, node) {
829 gchar *fname = (gchar *) (node->data);
830 connect_route *route = read_route(fname, is_perma);
831 if (route) {
832 list = g_list_append(list, route);
833 } else {
834 logwrite(LOG_ALERT, "could not read route "
835 "configuration %s\n", fname);
836 }
837 }
838 /* set uid and gid back */
839 if (!conf.run_as_user) {
840 set_euidgid(saved_uid, saved_gid, NULL, NULL);
841 }
842 return list;
843 }
845 void
846 destroy_route_list(GList *list)
847 {
848 GList *node;
850 foreach(list, node) {
851 connect_route *route = (connect_route *) (node->data);
852 destroy_route(route);
853 }
854 g_list_free(list);
855 }