masqmail

view src/conf.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents 41958685480d
children 5781ba87df95
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 memset(&conf, 0, sizeof(masqmail_conf));
36 conf.orig_uid = getuid();
37 conf.orig_gid = getgid();
39 if ((passwd = getpwnam(DEF_MAIL_USER)))
40 conf.mail_uid = passwd->pw_uid;
41 else {
42 fprintf(stderr, "user %s not found! (terminating)\n", DEF_MAIL_USER);
43 exit(1);
44 }
45 if ((group = getgrnam(DEF_MAIL_GROUP)))
46 conf.mail_gid = group->gr_gid;
47 else {
48 fprintf(stderr, "group %s not found! (terminating)\n", DEF_MAIL_GROUP);
49 exit(1);
50 }
51 }
53 static gchar *true_strings[] = {
54 "yes", "on", "true", NULL
55 };
57 static gchar *false_strings[] = {
58 "no", "off", "false", NULL
59 };
61 static gboolean
62 parse_boolean(gchar *rval)
63 {
64 gchar **str;
66 DEBUG(9) fprintf(stderr, "parse_boolean: %s\n", rval);
68 str = true_strings;
69 while (*str) {
70 if (strncasecmp(*str, rval, strlen(*str)) == 0)
71 return TRUE;
72 str++;
73 }
75 str = false_strings;
76 while (*str) {
77 if (strncasecmp(*str, rval, strlen(*str)) == 0)
78 return FALSE;
79 str++;
80 }
82 fprintf(stderr, "cannot parse value '%s'\n", rval);
83 exit(1);
84 }
86 /* make a list from each line in a file */
87 static GList*
88 parse_list_file(gchar *fname)
89 {
90 GList *list = NULL;
91 FILE *fptr;
93 if ((fptr = fopen(fname, "rt")) == NULL) {
94 logwrite(LOG_ALERT, "could not open %s for reading: %s\n", fname, strerror(errno));
95 exit(1);
96 }
98 gchar buf[256];
100 while (!feof(fptr)) {
101 fgets(buf, 255, fptr);
102 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
103 g_strchomp(buf);
104 DEBUG(9) fprintf(stderr,"parse_list_file: item = %s\n", buf);
105 list = g_list_append(list, g_strdup(buf));
106 }
107 }
108 fclose(fptr);
110 return list;
111 }
113 /* given a semicolon separated string, this function makes a GList out of it. */
114 GList*
115 parse_list(gchar *line, gboolean read_file)
116 {
117 GList *list = NULL;
118 gchar buf[256];
119 gchar *p, *q;
121 DEBUG(9) fprintf(stderr, "parsing list %s, file?:%d\n", line, read_file);
123 p = line;
124 while (*p != '\0') {
125 q = buf;
127 while (*p && (*p != ';') && (q < buf + 255))
128 *(q++) = *(p++);
129 *q = '\0';
131 if ((buf[0] == '/') && (read_file))
132 /* item is a filename, include its contents */
133 list = g_list_concat(list, parse_list_file(buf));
134 else
135 /* just a normal item */
136 list = g_list_append(list, g_strdup(buf));
138 DEBUG(9) fprintf(stderr, "item = %s\n", buf);
140 if (*p)
141 p++;
142 }
143 return list;
144 }
146 /*
147 ** Split the addrs at '@' into local_part and domain. Without an '@'
148 ** everything is local_part. Create address structs, which are put into a
149 ** list and returned. This funktion is used for lists of addrs containing
150 ** globbing chars (* and ?). We don't need valid RFC821 addresses here,
151 ** just patterns to match against.
152 */
153 static GList*
154 parse_address_glob_list(gchar *line, gboolean read_file)
155 {
156 GList *plain_list = parse_list(line, read_file);
157 GList *node;
158 GList *list = NULL;
160 foreach(plain_list, node) {
161 gchar *item = (gchar *) (node->data);
162 char *at;
163 char *p;
164 address *addr = calloc(1, sizeof(address));
166 for (p=item+strlen(item)-1; isspace(*p) || *p=='>'; p--) {
167 *p = '\0';
168 }
169 for (p=item; isspace(*p) || *p=='<'; p++) {
170 }
172 addr->address = strdup(p);
173 at = strrchr(p, '@');
174 if (at) {
175 *at = '\0';
176 addr->local_part = strdup(p);
177 addr->domain = strdup(at+1);
178 } else {
179 addr->local_part = strdup(p);
180 /* No `@', thus any domain is okay. */
181 addr->domain = "*";
182 }
183 list = g_list_append(list, addr);
184 DEBUG(6) debugf("parse_address_glob_list: read pattern `%s' `%s'\n",
185 addr->local_part, addr->domain);
186 g_free(item);
187 }
188 g_list_free(plain_list);
189 return list;
190 }
192 static GList*
193 parse_resolve_list(gchar *line)
194 {
195 GList *list;
196 GList *list_node;
197 GList *res_list = NULL;
199 list = parse_list(line, FALSE);
200 if (!list) {
201 return NULL;
202 }
204 foreach(list, list_node) {
205 gchar *item = (gchar *) (list_node->data);
206 if (strcmp(item, "byname") == 0) {
207 res_list = g_list_append(res_list, resolve_byname);
208 #ifdef ENABLE_RESOLVER
209 } else if (strcmp(item, "dns_a") == 0) {
210 res_list = g_list_append(res_list, resolve_dns_a);
211 } else if (strcmp(item, "dns_mx") == 0) {
212 res_list = g_list_append(res_list, resolve_dns_mx);
213 #endif
214 } else {
215 logwrite(LOG_ALERT, "unknown resolver %s\n", item);
216 exit(1);
217 }
218 g_free(item);
219 }
220 g_list_free(list);
221 return res_list;
222 }
224 static interface*
225 parse_interface(gchar *line, gint def_port)
226 {
227 gchar buf[256];
228 gchar *p, *q;
229 interface *iface;
231 DEBUG(9) fprintf(stderr, "parse_interface: %s\n", line);
233 p = line;
234 q = buf;
235 while ((*p != '\0') && (*p != ':') && (q < buf + 255))
236 *(q++) = *(p++);
237 *q = '\0';
239 iface = g_malloc(sizeof(interface));
240 iface->address = g_strdup(buf);
242 if (*p) {
243 p++;
244 iface->port = atoi(p);
245 } else
246 iface->port = def_port;
247 DEBUG(9) fprintf(stderr,"rval=%s, address:port=%s:%i\n",line, iface->address, iface->port);
249 return iface;
250 }
252 #ifdef ENABLE_IDENT /* so far used for that only */
253 static struct in_addr*
254 parse_network(gchar *line, gint def_port)
255 {
256 gchar buf[256];
257 gchar *p, *q;
258 struct in_addr addr, mask_addr, net_addr, *p_net_addr;
259 guint n;
261 DEBUG(9) fprintf(stderr, "parse_network: %s\n", line);
263 p = line;
264 q = buf;
265 while (*p && (*p != '/') && (q < buf + 255))
266 *(q++) = *(p++);
267 *q = '\0';
269 if ((addr.s_addr = inet_addr(buf)) == INADDR_NONE) {
270 fprintf(stderr, "'%s' is not a valid address (must be ip)\n", buf);
271 exit(1);
272 }
274 if (*p) {
275 guint i;
276 p++;
277 i = atoi(p);
278 if ((i >= 0) && (i <= 32))
279 n = i ? ~((1 << (32 - i)) - 1) : 0;
280 else {
281 fprintf(stderr, "'%d' is not a valid net mask (must be >= 0 and <= 32)\n", i);
282 exit(1);
283 }
284 } else
285 n = 0;
287 mask_addr.s_addr = htonl(n);
288 net_addr.s_addr = mask_addr.s_addr & addr.s_addr;
290 p_net_addr = g_malloc(sizeof(struct in_addr));
291 p_net_addr->s_addr = net_addr.s_addr;
292 return p_net_addr;
293 }
294 #endif
296 static gboolean
297 eat_comments(FILE *in)
298 {
299 gint c;
301 for (c = fgetc(in); (c == '#' || isspace(c)) && c != EOF;
302 c = fgetc(in)) {
303 if (c == '#') {
304 gint c;
305 for (c = fgetc(in); (c != '\n') && (c != EOF); c = fgetc(in));
306 }
307 }
308 if (c == EOF)
309 return FALSE;
310 ungetc(c, in);
311 return TRUE;
312 }
314 /* after parsing, eat trailing character until LF */
315 static gboolean
316 eat_line_trailing(FILE *in)
317 {
318 gint c;
320 for (c = fgetc(in); c != EOF && c != '\n'; c = fgetc(in));
321 if (c == EOF)
322 return FALSE;
323 return TRUE;
324 }
326 static gboolean
327 eat_spaces(FILE *in)
328 {
329 gint c;
331 for (c = fgetc(in); c != EOF && isspace(c); c = fgetc(in)) {
332 /* empty */
333 }
334 if (c == EOF)
335 return FALSE;
336 ungetc(c, in);
337 return TRUE;
338 }
340 static gboolean
341 read_lval(FILE *in, gchar *buf, gint size)
342 {
343 gint c;
344 gchar *ptr = buf;
346 DEBUG(9) fprintf(stderr, "read_lval()\n");
348 if (!eat_spaces(in))
349 return FALSE;
351 c = fgetc(in);
352 DEBUG(9) fprintf(stderr, "read_lval() 2\n");
353 while ((isalnum(c) || c == '_' || c == '-' || c == '.')
354 && (ptr < buf + size - 1)
355 && (c != EOF)) {
356 *ptr = c;
357 ptr++;
358 c = fgetc(in);
359 }
360 *ptr = '\0';
361 ungetc(c, in);
363 if (c == EOF) {
364 fprintf(stderr, "unexpected EOF after %s\n", buf);
365 return FALSE;
366 } else if (ptr >= buf + size - 1) {
367 fprintf(stderr, "lval too long\n");
368 }
370 eat_spaces(in);
372 DEBUG(9) fprintf(stderr, "lval = %s\n", buf);
374 return buf[0] != '\0';
375 }
377 static gboolean
378 read_rval(FILE *in, gchar *buf, gint size)
379 {
380 gint c;
381 gchar *ptr = buf;
383 DEBUG(9) fprintf(stderr, "read_rval()\n");
385 if (!eat_spaces(in))
386 return FALSE;
388 c = fgetc(in);
389 if (c != '\"') {
390 while ((isalnum(c) || c == '_' || c == '-' || c == '.'
391 || c == '/' || c == '@' || c == ';' || c == ':')
392 && (ptr < buf + size - 1)
393 && (c != EOF)) {
394 *ptr = c;
395 ptr++;
396 c = fgetc(in);
397 }
398 *ptr = '\0';
399 ungetc(c, in);
400 } else {
401 gboolean escape = FALSE;
402 c = fgetc(in);
403 while (((c != '\"') || escape) && (ptr < buf + size - 1)) {
404 if (c != '\n') { /* ignore line breaks */
405 if ((c == '\\') && (!escape)) {
406 escape = TRUE;
407 } else {
408 *ptr = c;
409 ptr++;
410 escape = FALSE;
411 }
412 }
413 c = fgetc(in);
414 }
415 *ptr = '\0';
416 }
418 eat_line_trailing(in);
420 DEBUG(9) fprintf(stderr, "rval = %s\n", buf);
422 return TRUE;
423 }
425 static gboolean
426 read_statement(FILE *in, gchar *lval, gint lsize, gchar *rval, gint rsize)
427 {
428 gint c;
430 DEBUG(9) fprintf(stderr, "read_statement()\n");
432 /* eat comments and empty lines: */
433 if (!eat_comments(in))
434 return FALSE;
436 if (!read_lval(in, lval, lsize)) {
437 return FALSE;
438 }
440 DEBUG(9) fprintf(stderr, " lval = %s\n", lval);
441 if ((c = fgetc(in) == '=')) {
442 if (read_rval(in, rval, rsize)) {
443 DEBUG(9) fprintf(stderr, " rval = %s\n", rval);
444 return TRUE;
445 }
446 } else {
447 DEBUG(9) fprintf(stderr," '=' expected after %s, char was '%c'\n", lval, c);
448 fprintf(stderr, "'=' expected after %s, char was '%c'\n", lval, c);
449 }
450 return FALSE;
451 }
453 gboolean
454 read_conf(gchar *filename)
455 {
456 FILE *in;
458 conf.log_max_pri = 7;
459 conf.do_relay = TRUE;
460 conf.localpartcmp = strcmp;
461 conf.max_defer_time = 86400 * 4; /* 4 days */
462 conf.max_msg_size = 0; /* no limit on msg size */
463 conf.spool_dir = SPOOL_DIR;
464 conf.mail_dir = "/var/mail";
465 conf.listen_addresses = g_list_append(NULL, parse_interface("localhost", 25));
467 if ((in = fopen(filename, "r")) == NULL) {
468 logwrite(LOG_ALERT, "could not open config file %s: %s\n", filename, strerror(errno));
469 return FALSE;
470 }
472 gchar lval[256], rval[2048];
473 while (read_statement(in, lval, 256, rval, 2048)) {
474 DEBUG(9) fprintf(stderr,"read_conf(): lval=%s\n", lval);
475 if (strcmp(lval, "debug_level") == 0)
476 conf.debug_level = atoi(rval);
477 else if (strcmp(lval, "run_as_user") == 0) {
478 if (!conf.run_as_user) /* you should not be able to reset that flag */
479 conf.run_as_user = parse_boolean(rval);
480 } else if (strcmp(lval, "use_syslog") == 0)
481 conf.use_syslog = parse_boolean(rval);
482 else if (strcmp(lval, "mail_dir") == 0)
483 conf.mail_dir = g_strdup(rval);
484 else if (strcmp(lval, "lock_dir") == 0)
485 conf.lock_dir = g_strdup(rval);
486 else if (strcmp(lval, "spool_dir") == 0)
487 conf.spool_dir = g_strdup(rval);
488 else if (strcmp(lval, "log_dir") == 0)
489 conf.log_dir = g_strdup(rval);
490 else if (strcmp(lval, "host_name") == 0) {
491 if (rval[0] != '/')
492 conf.host_name = g_strdup(rval);
493 else {
494 char buf[256];
495 FILE *fptr = fopen(rval, "rt");
496 if (!fptr) {
497 logwrite(LOG_ALERT, "could not open %s: %s\n", rval, strerror(errno));
498 return FALSE;
499 }
500 fgets(buf, 255, fptr);
501 g_strchomp(buf);
502 conf.host_name = g_strdup(buf);
503 fclose(fptr);
504 }
505 } else if (strcmp(lval, "local_hosts") == 0)
506 conf.local_hosts = parse_list(rval, FALSE);
507 else if (strcmp(lval, "local_addresses") == 0)
508 conf.local_addresses = parse_list(rval, TRUE);
509 else if (strcmp(lval, "not_local_addresses") == 0)
510 conf.not_local_addresses = parse_list(rval, TRUE);
511 else if (strcmp(lval, "do_save_envelope_to") == 0)
512 conf.do_save_envelope_to = parse_boolean(rval);
513 else if (strcmp(lval, "defer_all") == 0)
514 conf.defer_all = parse_boolean(rval);
515 else if (strcmp(lval, "do_relay") == 0)
516 conf.do_relay = parse_boolean(rval);
517 else if (strcmp(lval, "alias_file") == 0) {
518 conf.alias_file = g_strdup(rval);
519 } else if (strcmp(lval, "caseless_matching") == 0) {
520 conf.localpartcmp = parse_boolean(rval) ? strcasecmp : strcmp;
521 } else if (strcmp(lval, "mbox_default") == 0) {
522 conf.mbox_default = g_strdup(rval);
523 } else if (strcmp(lval, "mbox_users") == 0) {
524 conf.mbox_users = parse_list(rval, TRUE);
525 } else if (strcmp(lval, "mda_users") == 0) {
526 conf.mda_users = parse_list(rval, TRUE);
527 } else if (strcmp(lval, "mda") == 0) {
528 conf.mda = g_strdup(rval);
529 } else if (strcmp(lval, "mda_fromline") == 0) {
530 conf.mda_fromline = parse_boolean(rval);
531 } else if (strcmp(lval, "mda_fromhack") == 0) {
532 conf.mda_fromhack = parse_boolean(rval);
533 } else if (strcmp(lval, "pipe_fromline") == 0) {
534 conf.pipe_fromline = parse_boolean(rval);
535 } else if (strcmp(lval, "pipe_fromhack") == 0) {
536 conf.pipe_fromhack = parse_boolean(rval);
537 } else if (strcmp(lval, "listen_addresses") == 0) {
538 GList *node;
539 GList *tmp_list = parse_list(rval, FALSE);
541 conf.listen_addresses = NULL;
542 foreach(tmp_list, node) {
543 conf.listen_addresses = g_list_append(conf.listen_addresses, parse_interface((gchar *) (node-> data), 25));
544 g_free(node->data);
545 }
546 g_list_free(tmp_list);
547 } else if (strcmp(lval, "ident_trusted_nets") == 0) {
548 #ifdef ENABLE_IDENT
549 GList *node;
550 GList *tmp_list = parse_list(rval, FALSE);
552 conf.ident_trusted_nets = NULL;
553 foreach(tmp_list, node) {
554 conf.ident_trusted_nets = g_list_append(conf.ident_trusted_nets, parse_network((gchar *) (node->data), 25));
555 g_free(node->data);
556 }
557 g_list_free(tmp_list);
558 #else
559 logwrite(LOG_WARNING, "%s ignored: not compiled with ident support\n", lval);
560 #endif
561 } else if (strncmp(lval, "query_routes.", 13) == 0) {
562 GList *file_list = parse_list(rval, FALSE);
563 table_pair *pair = create_pair(lval+13, file_list);
564 conf.query_routes = g_list_append(conf.query_routes, pair);
565 } else if (strcmp(lval, "permanent_routes") == 0) {
566 conf.perma_routes = parse_list(rval, FALSE);
567 } else if (strcmp(lval, "online_query") == 0)
568 conf.online_query = g_strdup(rval);
569 else if (strcmp(lval, "do_queue") == 0)
570 conf.do_queue = parse_boolean(rval);
571 else if (strcmp(lval, "errmsg_file") == 0)
572 conf.errmsg_file = g_strdup(rval);
573 else if (strcmp(lval, "warnmsg_file") == 0)
574 conf.warnmsg_file = g_strdup(rval);
575 else if (strcmp(lval, "warn_intervals") == 0)
576 conf.warn_intervals = parse_list(rval, FALSE);
577 else if (strcmp(lval, "max_defer_time") == 0) {
578 gint ival = time_interval(rval);
579 if (ival < 0)
580 logwrite(LOG_WARNING, "invalid time interval for 'max_defer_time': %s\n", rval);
581 else
582 conf.max_defer_time = ival;
583 } else if (strcmp(lval, "log_user") == 0)
584 conf.log_user = g_strdup(rval);
585 else if(strcmp(lval, "max_msg_size") == 0) {
586 conf.max_msg_size = atol(rval);
587 DEBUG(9) fprintf(stderr,"rval=%s, conf.max_msg_size=%ld\n",
588 rval, conf.max_msg_size);
589 }
590 else
591 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
592 }
593 fclose(in);
595 if (!conf.host_name) {
596 logwrite(LOG_ALERT, "`host_name' MUST be set in masqmail.conf. See man page\n");
597 return FALSE;
598 }
600 if (conf.errmsg_file == NULL)
601 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
602 if (conf.warnmsg_file == NULL)
603 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
605 if (conf.lock_dir == NULL)
606 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
608 if (conf.mbox_default == NULL)
609 conf.mbox_default = g_strdup("mbox");
611 if (conf.warn_intervals == NULL)
612 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", FALSE);
614 if (!conf.local_hosts) {
615 char *shortname = strdup(conf.host_name);
616 char *p = strchr(shortname, '.');
617 if (p) {
618 *p = '\0';
619 }
620 /* we don't care if shortname and conf.host_name are the same */
621 char *local_hosts_str = g_strdup_printf("localhost;%s;%s", shortname, conf.host_name);
622 conf.local_hosts = parse_list(local_hosts_str, FALSE);
623 free(shortname);
624 free(local_hosts_str);
625 }
628 return TRUE;
629 }
631 connect_route*
632 read_route(gchar *filename, gboolean is_perma)
633 {
634 gboolean ok = FALSE;
635 FILE *in;
637 connect_route *route = g_malloc(sizeof(connect_route));
638 memset(route, 0, sizeof(connect_route));
640 DEBUG(5) debugf("read_route, filename = %s\n", filename);
642 route->filename = g_strdup(filename);
643 route->name = route->filename; /* quick hack */
645 route->expand_h_sender_address = TRUE;
647 route->is_perma = is_perma;
649 route->do_pipelining = TRUE;
651 if ((in = fopen(route->filename, "r")) == NULL) {
652 logwrite(LOG_ALERT, "could not open route file %s: %s\n", route->filename, strerror(errno));
653 g_free(route);
654 return NULL;
655 }
657 gchar lval[256], rval[2048];
658 while (read_statement(in, lval, 256, rval, 2048)) {
659 if (strcmp(lval, "mail_host") == 0)
660 route->mail_host = parse_interface(rval, 25);
661 else if (strcmp(lval, "helo_name") == 0)
662 route->helo_name = g_strdup(rval);
663 else if (strcmp(lval, "wrapper") == 0)
664 route->wrapper = g_strdup(rval);
665 else if (strcmp(lval, "connect_error_fail") == 0)
666 route->connect_error_fail = parse_boolean(rval);
667 else if (strcmp(lval, "do_correct_helo") == 0)
668 route->do_correct_helo = parse_boolean(rval);
669 else if (strcmp(lval, "instant_helo") == 0)
670 route->instant_helo = parse_boolean(rval);
671 else if (strcmp(lval, "do_pipelining") == 0)
672 route->do_pipelining = parse_boolean(rval);
674 else if (strcmp(lval, "allowed_senders") == 0)
675 route->allowed_senders = parse_address_glob_list(rval, TRUE);
676 else if (strcmp(lval, "denied_senders") == 0)
677 route->denied_senders = parse_address_glob_list(rval, TRUE);
678 else if (strcmp(lval, "allowed_recipients") == 0)
679 route->allowed_recipients = parse_address_glob_list(rval, TRUE);
680 else if (strcmp(lval, "denied_recipients") == 0)
681 route->denied_recipients = parse_address_glob_list(rval, TRUE);
683 else if (strcmp(lval, "set_h_from_domain") == 0)
684 route->set_h_from_domain = g_strdup(rval);
685 else if (strcmp(lval, "set_h_reply_to_domain") == 0)
686 route->set_h_reply_to_domain = g_strdup(rval);
687 else if (strcmp(lval, "set_return_path_domain") == 0)
688 route->set_return_path_domain = g_strdup(rval);
689 else if (strcmp(lval, "map_return_path_addresses") == 0) {
690 GList *node, *list;
692 list = parse_list(rval, TRUE);
693 foreach(list, node) {
694 gchar *item = (gchar *) (node->data);
695 table_pair *pair = parse_table_pair(item, ':');
696 address *addr = create_address((gchar *) (pair->value), TRUE);
697 g_free(pair->value);
698 pair->value = (gpointer *) addr;
699 route->map_return_path_addresses = g_list_append(route->map_return_path_addresses, pair);
700 g_free(item);
701 }
702 g_list_free(list);
703 } else if (strcmp(lval, "map_h_from_addresses") == 0) {
704 GList *list, *node;
706 list = parse_list(rval, TRUE);
707 foreach(list, node) {
708 gchar *item = (gchar *) (node->data);
709 table_pair *pair = parse_table_pair(item, ':');
710 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
711 g_free(item);
712 }
713 g_list_free(list);
714 } else if (strcmp(lval, "map_h_reply_to_addresses") == 0) {
715 GList *list, *node;
717 list = parse_list(rval, TRUE);
718 foreach(list, node) {
719 gchar *item = (gchar *) (node->data);
720 table_pair *pair = parse_table_pair(item, ':');
721 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
722 g_free(item);
723 }
724 g_list_free(list);
725 } else if (strcmp(lval, "map_h_mail_followup_to_addresses") == 0) {
726 GList *list, *node;
728 list = parse_list(rval, TRUE);
729 foreach(list, node) {
730 gchar *item = (gchar *) (node->data);
731 table_pair *pair = parse_table_pair(item, ':');
732 route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
733 g_free(item);
734 }
735 g_list_free(list);
736 } else if (strcmp(lval, "expand_h_sender_domain") == 0) {
737 route->expand_h_sender_domain = parse_boolean(rval);
738 } else if (strcmp(lval, "expand_h_sender_address") == 0) {
739 route->expand_h_sender_address = parse_boolean(rval);
740 } else if (strcmp(lval, "resolve_list") == 0)
741 route->resolve_list = parse_resolve_list(rval);
742 else if (strcmp(lval, "do_ssl") == 0) {
743 /* we ignore this. This option is used by sqilconf */
744 ;
745 }
746 #ifdef ENABLE_AUTH
747 else if (strcmp(lval, "auth_name") == 0) {
748 route->auth_name = g_strdup(rval);
749 } else if (strcmp(lval, "auth_login") == 0) {
750 route->auth_login = g_strdup(rval);
751 } else if (strcmp(lval, "auth_secret") == 0) {
752 route->auth_secret = g_strdup(rval);
753 }
754 #else
755 else if ((strcmp(lval, "auth_name") == 0)
756 || (strcmp(lval, "auth_login") == 0)
757 || (strcmp(lval, "auth_secret") == 0)) {
758 logwrite(LOG_WARNING, "%s ignored: not compiled with auth support.\n", lval);
759 }
760 #endif
761 else if (strcmp(lval, "pipe") == 0) {
762 route->pipe = g_strdup(rval);
763 } else if (strcmp(lval, "pipe_fromline") == 0) {
764 route->pipe_fromline = parse_boolean(rval);
765 } else if (strcmp(lval, "pipe_fromhack") == 0) {
766 route->pipe_fromhack = parse_boolean(rval);
767 } else if (strcmp(lval, "last_route") == 0) {
768 route->last_route = parse_boolean(rval);
769 } else
770 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
771 }
773 if (!route->resolve_list) {
774 #ifdef ENABLE_RESOLVER
775 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_mx);
776 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_a);
777 #endif
778 route->resolve_list = g_list_append(route->resolve_list, resolve_byname);
779 }
780 fclose(in);
781 ok = TRUE;
783 /* warn user about misconfigurations: */
784 if ((route->map_h_from_addresses != NULL) && (route->set_h_from_domain != NULL)) {
785 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides 'set_h_from_domain'\n");
786 g_free(route->set_h_from_domain);
787 route->set_h_from_domain = NULL;
788 }
789 if ((route->map_h_reply_to_addresses != NULL) && (route->set_h_reply_to_domain != NULL)) {
790 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides 'set_h_reply_to_domain'\n");
791 g_free(route->set_h_reply_to_domain);
792 route->set_h_reply_to_domain = NULL;
793 }
795 if (!ok) {
796 g_free(route);
797 route = NULL;
798 }
800 return route;
801 }
803 static void
804 _g_list_free_all(GList *list)
805 {
806 GList *node;
807 if (list) {
808 foreach(list, node)
809 g_free(node->data);
810 g_list_free(list);
811 }
812 }
814 void
815 destroy_route(connect_route *r)
816 {
817 if (r->filename)
818 g_free(r->filename);
819 if (r->mail_host) {
820 g_free(r->mail_host->address);
821 g_free(r->mail_host);
822 }
823 if (r->wrapper)
824 g_free(r->wrapper);
825 if (r->helo_name)
826 g_free(r->helo_name);
827 _g_list_free_all(r->allowed_senders);
828 _g_list_free_all(r->denied_senders);
829 _g_list_free_all(r->allowed_recipients);
830 _g_list_free_all(r->denied_recipients);
831 if (r->set_h_from_domain)
832 g_free(r->set_h_from_domain);
833 if (r->set_h_reply_to_domain)
834 g_free(r->set_h_reply_to_domain);
835 if (r->set_return_path_domain)
836 g_free(r->set_return_path_domain);
837 if (r->map_h_reply_to_addresses)
838 destroy_table(r->map_h_reply_to_addresses);
839 if (r->resolve_list)
840 g_list_free(r->resolve_list);
841 #ifdef ENABLE_AUTH
842 if (r->auth_name)
843 g_free(r->auth_name);
844 if (r->auth_login)
845 g_free(r->auth_login);
846 if (r->auth_secret)
847 g_free(r->auth_secret);
848 #endif
849 if (r->pipe)
850 g_free(r->pipe);
851 g_free(r);
852 }
854 GList*
855 read_route_list(GList *rf_list, gboolean is_perma)
856 {
857 GList *list = NULL;
858 GList *node;
859 uid_t saved_uid, saved_gid;
861 if (!conf.run_as_user) {
862 set_euidgid(0, 0, &saved_uid, &saved_gid);
863 }
865 foreach(rf_list, node) {
866 gchar *fname = (gchar *) (node->data);
867 connect_route *route = read_route(fname, is_perma);
868 if (route)
869 list = g_list_append(list, route);
870 else
871 logwrite(LOG_ALERT, "could not read route configuration %s\n", fname);
872 }
874 /* set uid and gid back */
875 if (!conf.run_as_user) {
876 set_euidgid(saved_uid, saved_gid, NULL, NULL);
877 }
879 return list;
880 }
882 void
883 destroy_route_list(GList *list)
884 {
885 GList *node;
887 foreach(list, node) {
888 connect_route *route = (connect_route *) (node->data);
889 destroy_route(route);
890 }
891 g_list_free(list);
892 }