masqmail

view src/conf.c @ 26:607221b0dac1

added myself as maintainer; updated URL of the website
author meillo@marmaro.de
date Thu, 06 May 2010 11:44:43 +0200
parents 49dab67fe461
children 71605f542ecc
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <pwd.h>
20 #include <grp.h>
22 #include "masqmail.h"
24 masqmail_conf conf;
26 void
27 init_conf()
28 {
29 struct passwd *passwd;
30 struct group *group;
32 memset(&conf, 0, sizeof(masqmail_conf));
34 conf.orig_uid = getuid();
35 conf.orig_gid = getgid();
37 if ((passwd = getpwnam(DEF_MAIL_USER)))
38 conf.mail_uid = passwd->pw_uid;
39 else {
40 fprintf(stderr, "user %s not found! (terminating)\n", DEF_MAIL_USER);
41 exit(EXIT_FAILURE);
42 }
43 if ((group = getgrnam(DEF_MAIL_GROUP)))
44 conf.mail_gid = group->gr_gid;
45 else {
46 fprintf(stderr, "group %s not found! (terminating)\n", DEF_MAIL_GROUP);
47 exit(EXIT_FAILURE);
48 }
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(6) fprintf(stderr, "parse_boolean: %s\n", rval);
66 str = true_strings;
67 while (*str) {
68 if (strncasecmp(*str, rval, strlen(*str)) == 0)
69 return TRUE;
70 str++;
71 }
73 str = false_strings;
74 while (*str) {
75 if (strncasecmp(*str, rval, strlen(*str)) == 0)
76 return FALSE;
77 str++;
78 }
80 fprintf(stderr, "cannot parse value '%s'\n", rval);
81 exit(EXIT_FAILURE);
82 }
84 /* make a list from each line in a file */
85 static GList*
86 parse_list_file(gchar * fname)
87 {
88 GList *list = NULL;
89 FILE *fptr;
91 if ((fptr = fopen(fname, "rt"))) {
92 gchar buf[256];
94 while (!feof(fptr)) {
95 fgets(buf, 255, fptr);
96 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
97 g_strchomp(buf);
98 list = g_list_append(list, g_strdup(buf));
99 }
100 }
101 fclose(fptr);
102 } else {
103 logwrite(LOG_ALERT, "could not open %s for reading: %s\n", fname, strerror(errno));
104 exit(EXIT_FAILURE);
105 }
107 return list;
108 }
110 /* given a semicolon separated string, this function makes a GList out of it. */
111 GList*
112 parse_list(gchar * line, gboolean read_file)
113 {
114 GList *list = NULL;
115 gchar buf[256];
116 gchar *p, *q;
118 DEBUG(6) fprintf(stderr, "parsing list %s\n", line);
120 p = line;
121 while (*p != '\0') {
122 q = buf;
124 while (*p && (*p != ';') && (q < buf + 255))
125 *(q++) = *(p++);
126 *q = '\0';
128 if ((buf[0] == '/') && (read_file))
129 /* item is a filename, include its contents */
130 list = g_list_concat(list, parse_list_file(buf));
131 else
132 /* just a normal item */
133 list = g_list_append(list, g_strdup(buf));
135 DEBUG(6) printf("item = %s\n", buf);
137 if (*p)
138 p++;
139 }
140 return list;
141 }
143 static GList*
144 parse_address_list(gchar * line, gboolean read_file)
145 {
146 GList *plain_list = parse_list(line, read_file);
147 GList *node;
148 GList *list = NULL;
150 foreach(plain_list, node) {
151 gchar *item = (gchar *) (node->data);
152 address *addr = create_address(item, TRUE);
153 if (addr)
154 list = g_list_append(list, addr);
155 g_free(item);
156 }
157 g_list_free(plain_list);
159 return list;
160 }
162 static GList*
163 parse_resolve_list(gchar * line)
164 {
165 GList *list;
166 GList *list_node;
167 GList *res_list = NULL;
169 list = parse_list(line, FALSE);
170 if (list) {
171 foreach(list, list_node) {
172 gchar *item = (gchar *) (list_node->data);
173 if (strcmp(item, "byname") == 0) {
174 res_list = g_list_append(res_list, resolve_byname);
175 #ifdef ENABLE_RESOLVER
176 } else if (strcmp(item, "dns_a") == 0) {
177 res_list = g_list_append(res_list, resolve_dns_a);
178 } else if (strcmp(item, "dns_mx") == 0) {
179 res_list = g_list_append(res_list, resolve_dns_mx);
180 #endif
181 } else {
182 logwrite(LOG_ALERT, "unknown resolver %s\n", item);
183 exit(EXIT_FAILURE);
184 }
185 g_free(item);
186 }
187 g_list_free(list);
188 }
189 return res_list;
190 }
192 static interface*
193 parse_interface(gchar * line, gint def_port)
194 {
195 gchar buf[256];
196 gchar *p, *q;
197 interface *iface;
199 DEBUG(6) fprintf(stderr, "parse_interface: %s\n", line);
201 p = line;
202 q = buf;
203 while ((*p != '\0') && (*p != ':') && (q < buf + 255))
204 *(q++) = *(p++);
205 *q = '\0';
207 iface = g_malloc(sizeof(interface));
208 iface->address = g_strdup(buf);
210 if (*p) {
211 p++;
212 iface->port = atoi(p);
213 } else
214 iface->port = def_port;
216 return iface;
217 }
219 #ifdef ENABLE_IDENT /* so far used for that only */
220 static struct in_addr*
221 parse_network(gchar * line, gint def_port)
222 {
223 gchar buf[256];
224 gchar *p, *q;
225 struct in_addr addr, mask_addr, net_addr, *p_net_addr;
226 guint n;
228 DEBUG(6) fprintf(stderr, "parse_network: %s\n", line);
230 p = line;
231 q = buf;
232 while ((*p != '\0') && (*p != '/') && (q < buf + 255))
233 *(q++) = *(p++);
234 *q = '\0';
236 if ((addr.s_addr = inet_addr(buf)) != INADDR_NONE) {
237 if (*p) {
238 guint i;
239 p++;
240 i = atoi(p);
241 if ((i >= 0) && (i <= 32))
242 n = i ? ~((1 << (32 - i)) - 1) : 0;
243 else {
244 fprintf(stderr, "'%d' is not a valid net mask (must be >= 0 and <= 32)\n", i);
245 exit(EXIT_FAILURE);
246 }
247 } else
248 n = 0;
250 mask_addr.s_addr = htonl(n);
251 net_addr.s_addr = mask_addr.s_addr & addr.s_addr;
252 } else {
253 fprintf(stderr, "'%s' is not a valid address (must be ip)\n", buf);
254 exit(EXIT_FAILURE);
255 }
257 p_net_addr = g_malloc(sizeof(struct in_addr));
258 p_net_addr->s_addr = net_addr.s_addr;
259 return p_net_addr;
260 }
261 #endif
263 static gboolean
264 eat_comments(FILE * in)
265 {
266 gint c;
268 for (c = fgetc(in); (c == '#' || isspace(c)) && c != EOF;
269 c = fgetc(in)) {
270 if (c == '#') {
271 gint c;
272 for (c = fgetc(in); (c != '\n') && (c != EOF); c = fgetc(in));
273 }
274 }
275 if (c == EOF)
276 return FALSE;
277 ungetc(c, in);
278 return TRUE;
279 }
281 /* after parsing, eat trailing character until LF */
282 static gboolean
283 eat_line_trailing(FILE * in)
284 {
285 gint c;
287 for (c = fgetc(in); c != EOF && c != '\n'; c = fgetc(in));
288 if (c == EOF)
289 return FALSE;
290 return TRUE;
291 }
293 static gboolean
294 eat_spaces(FILE * in)
295 {
296 gint c;
298 for (c = fgetc(in); c != EOF && isspace(c); c = fgetc(in));
299 if (c == EOF)
300 return FALSE;
301 ungetc(c, in);
302 return TRUE;
303 }
305 static gboolean
306 read_lval(FILE * in, gchar * buf, gint size)
307 {
308 gint c;
309 gchar *ptr = buf;
311 DEBUG(6) fprintf(stderr, "read_lval()\n");
313 if (!eat_spaces(in))
314 return FALSE;
316 c = fgetc(in);
317 DEBUG(6) fprintf(stderr, "read_lval() 2\n");
318 while ((isalnum(c) || c == '_' || c == '-' || c == '.')
319 && (ptr < buf + size - 1)
320 && (c != EOF)) {
321 *ptr = c;
322 ptr++;
323 c = fgetc(in);
324 }
325 *ptr = '\0';
326 ungetc(c, in);
328 if (c == EOF) {
329 fprintf(stderr, "unexpected EOF after %s\n", buf);
330 return FALSE;
331 } else if (ptr >= buf + size - 1) {
332 fprintf(stderr, "lval too long\n");
333 }
335 eat_spaces(in);
337 DEBUG(6) fprintf(stderr, "lval = %s\n", buf);
339 return buf[0] != '\0';
340 }
342 static gboolean
343 read_rval(FILE * in, gchar * buf, gint size)
344 {
345 gint c;
346 gchar *ptr = buf;
348 DEBUG(6) fprintf(stderr, "read_rval()\n");
350 if (!eat_spaces(in))
351 return FALSE;
353 c = fgetc(in);
354 if (c != '\"') {
355 while ((isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/' || c == '@' || c == ';')
356 && (ptr < buf + size - 1)
357 && (c != EOF)) {
358 *ptr = c;
359 ptr++;
360 c = fgetc(in);
361 }
362 *ptr = '\0';
363 ungetc(c, in);
364 } else {
365 gboolean escape = FALSE;
366 c = fgetc(in);
367 while (((c != '\"') || escape) && (ptr < buf + size - 1)) {
368 if (c != '\n') { /* ignore line breaks */
369 if ((c == '\\') && (!escape)) {
370 escape = TRUE;
371 } else {
372 *ptr = c;
373 ptr++;
374 escape = FALSE;
375 }
376 }
377 c = fgetc(in);
378 }
379 *ptr = '\0';
380 }
382 eat_line_trailing(in);
384 DEBUG(6) fprintf(stderr, "rval = %s\n", buf);
386 return TRUE;
387 }
389 static gboolean
390 read_statement(FILE * in, gchar * lval, gint lsize, gchar * rval, gint rsize)
391 {
392 gint c;
394 DEBUG(6) fprintf(stderr, "read_statement()\n");
396 /* eat comments and empty lines: */
397 if (!eat_comments(in))
398 return FALSE;
400 DEBUG(6) fprintf(stderr, "read_statement() 1\n");
402 if (read_lval(in, lval, lsize)) {
403 DEBUG(6) fprintf(stderr, "lval = %s\n", lval);
404 if ((c = fgetc(in) == '=')) {
405 if (read_rval(in, rval, rsize)) {
406 DEBUG(6) fprintf(stderr, "rval = %s\n", rval);
407 return TRUE;
408 }
409 } else {
410 fprintf(stderr, "'=' expected after %s, char was '%c'\n", lval, c);
411 }
412 }
413 return FALSE;
414 }
416 gboolean
417 read_conf(gchar * filename)
418 {
419 FILE *in;
421 conf.log_max_pri = 7;
422 conf.remote_port = 25;
423 conf.do_relay = TRUE;
424 conf.alias_local_cmp = strcmp;
425 conf.max_defer_time = 86400 * 4; /* 4 days */
427 if ((in = fopen(filename, "r"))) {
428 gchar lval[256], rval[2048];
429 while (read_statement(in, lval, 256, rval, 2048)) {
430 if (strcmp(lval, "debug_level") == 0)
431 conf.debug_level = atoi(rval);
432 else if (strcmp(lval, "run_as_user") == 0) {
433 if (!conf.run_as_user) /* you should not be able to reset that flag */
434 conf.run_as_user = parse_boolean(rval);
435 } else if (strcmp(lval, "use_syslog") == 0)
436 conf.use_syslog = parse_boolean(rval);
437 else if (strcmp(lval, "mail_dir") == 0)
438 conf.mail_dir = g_strdup(rval);
439 else if (strcmp(lval, "lock_dir") == 0)
440 conf.lock_dir = g_strdup(rval);
441 else if (strcmp(lval, "spool_dir") == 0)
442 conf.spool_dir = g_strdup(rval);
443 else if (strcmp(lval, "log_dir") == 0)
444 conf.log_dir = g_strdup(rval);
445 else if (strcmp(lval, "host_name") == 0) {
446 if (rval[0] != '/')
447 conf.host_name = g_strdup(rval);
448 else {
449 char buf[256];
450 FILE *fptr = fopen(rval, "rt");
451 if (fptr) {
452 fgets(buf, 255, fptr);
453 g_strchomp(buf);
454 conf.host_name = g_strdup(buf);
455 fclose(fptr);
456 } else {
457 fprintf(stderr, "could not open %s: %s\n", rval,
458 strerror(errno));
459 return FALSE;
460 }
461 }
462 } else if (strcmp(lval, "remote_port") == 0) {
463 fprintf(stderr, "the remote_port option is now deprecated. Use 'mail_host' in the\n"
464 "route configuration instead. See man masqmail.route\n");
465 conf.remote_port = atoi(rval);
466 } else if (strcmp(lval, "local_hosts") == 0)
467 conf.local_hosts = parse_list(rval, FALSE);
468 else if (strcmp(lval, "local_addresses") == 0)
469 conf.local_addresses = parse_list(rval, TRUE);
470 else if (strcmp(lval, "not_local_addresses") == 0)
471 conf.not_local_addresses = parse_list(rval, TRUE);
472 else if (strcmp(lval, "local_nets") == 0)
473 conf.local_nets = parse_list(rval, FALSE);
474 else if (strcmp(lval, "do_save_envelope_to") == 0)
475 conf.do_save_envelope_to = parse_boolean(rval);
476 else if (strcmp(lval, "defer_all") == 0)
477 conf.defer_all = parse_boolean(rval);
478 else if (strcmp(lval, "do_relay") == 0)
479 conf.do_relay = parse_boolean(rval);
480 else if (strcmp(lval, "alias_file") == 0) {
481 conf.alias_file = g_strdup(rval);
482 } else if (strcmp(lval, "alias_local_caseless") == 0) {
483 conf.alias_local_cmp = parse_boolean(rval) ? strcasecmp : strcmp;
484 } else if (strcmp(lval, "mbox_default") == 0) {
485 conf.mbox_default = g_strdup(rval);
486 } else if (strcmp(lval, "mbox_users") == 0) {
487 conf.mbox_users = parse_list(rval, TRUE);
488 } else if (strcmp(lval, "mda_users") == 0) {
489 conf.mda_users = parse_list(rval, TRUE);
490 } else if (strcmp(lval, "maildir_users") == 0) {
491 conf.maildir_users = parse_list(rval, TRUE);
492 } else if (strcmp(lval, "mda") == 0) {
493 conf.mda = g_strdup(rval);
494 } else if (strcmp(lval, "mda_fromline") == 0) {
495 conf.mda_fromline = parse_boolean(rval);
496 } else if (strcmp(lval, "mda_fromhack") == 0) {
497 conf.mda_fromhack = parse_boolean(rval);
498 } else if (strcmp(lval, "pipe_fromline") == 0) {
499 conf.pipe_fromline = parse_boolean(rval);
500 } else if (strcmp(lval, "pipe_fromhack") == 0) {
501 conf.pipe_fromhack = parse_boolean(rval);
502 } else if (strcmp(lval, "listen_addresses") == 0) {
503 GList *node;
504 GList *tmp_list = parse_list(rval, FALSE);
506 conf.listen_addresses = NULL;
507 foreach(tmp_list, node) {
508 conf.listen_addresses = g_list_append(conf.listen_addresses, parse_interface((gchar *) (node-> data), 25));
509 g_free(node->data);
510 }
511 g_list_free(tmp_list);
512 } else if (strcmp(lval, "ident_trusted_nets") == 0) {
513 #ifdef ENABLE_IDENT
514 GList *node;
515 GList *tmp_list = parse_list(rval, FALSE);
517 conf.ident_trusted_nets = NULL;
518 foreach(tmp_list, node) {
519 conf.ident_trusted_nets = g_list_append(conf.ident_trusted_nets, parse_network((gchar *) (node->data), 25));
520 g_free(node->data);
521 }
522 g_list_free(tmp_list);
523 #else
524 fprintf(stderr, "%s ignored: not compiled with ident support\n", lval);
525 #endif
526 } else if ((strncmp(lval, "connect_route.", 14) == 0)
527 || (strncmp(lval, "online_routes.", 14) == 0)) {
528 GList *file_list = parse_list(rval, FALSE);
529 table_pair *pair = create_pair(&(lval[14]), file_list);
530 conf.connect_routes = g_list_append(conf.connect_routes, pair);
531 } else if (strcmp(lval, "local_net_route") == 0) {
532 conf.local_net_routes = parse_list(rval, FALSE);
533 } else if (strcmp(lval, "online_detect") == 0)
534 conf.online_detect = g_strdup(rval);
535 else if (strcmp(lval, "online_file") == 0)
536 conf.online_file = g_strdup(rval);
537 else if (strcmp(lval, "online_pipe") == 0)
538 conf.online_pipe = g_strdup(rval);
539 else if (strcmp(lval, "mserver_iface") == 0)
540 conf.mserver_iface = parse_interface(rval, 224);
541 else if (strcmp(lval, "do_queue") == 0)
542 conf.do_queue = parse_boolean(rval);
543 else if (strncmp(lval, "get.", 4) == 0) {
544 #ifdef ENABLE_POP3
545 table_pair *pair = create_pair_string(&(lval[4]), rval);
546 conf.get_names = g_list_append(conf.get_names, pair);
547 #else
548 fprintf(stderr, "get.<name> ignored: not compiled with pop support\n");
549 #endif
550 } else if (strncmp(lval, "online_gets.", 12) == 0) {
551 #ifdef ENABLE_POP3
552 GList *file_list = parse_list(rval, FALSE);
553 table_pair *pair = create_pair(&(lval[12]), file_list);
554 conf.online_gets = g_list_append(conf.online_gets, pair);
555 #else
556 fprintf(stderr, "online_gets.<name> ignored: not compiled with pop support\n");
557 #endif
558 } else if (strcmp(lval, "errmsg_file") == 0)
559 conf.errmsg_file = g_strdup(rval);
560 else if (strcmp(lval, "warnmsg_file") == 0)
561 conf.warnmsg_file = g_strdup(rval);
562 else if (strcmp(lval, "warn_intervals") == 0)
563 conf.warn_intervals = parse_list(rval, FALSE);
564 else if (strcmp(lval, "max_defer_time") == 0) {
565 gint dummy;
566 gint ival = time_interval(rval, &dummy);
567 if (ival < 0)
568 fprintf(stderr, "invalid time interval for 'max_defer_time': %s\n", rval);
569 else
570 conf.max_defer_time = ival;
571 } else if (strcmp(lval, "log_user") == 0)
572 conf.log_user = g_strdup(rval);
574 else
575 fprintf(stderr, "var '%s' not (yet) known, ignored\n", lval);
576 }
577 fclose(in);
579 if (conf.errmsg_file == NULL)
580 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
581 if (conf.warnmsg_file == NULL)
582 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
584 if (conf.lock_dir == NULL)
585 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
587 if (conf.mbox_default == NULL)
588 conf.mbox_default = g_strdup("mbox");
590 if (conf.warn_intervals == NULL)
591 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", FALSE);
593 return TRUE;
594 } else
595 fprintf(stderr, "could not open config file %s: %s\n", filename, strerror(errno));
596 return FALSE;
597 }
599 connect_route*
600 read_route(gchar * filename, gboolean is_local_net)
601 {
602 gboolean ok = FALSE;
603 FILE *in;
605 connect_route *route = g_malloc(sizeof(connect_route));
606 memset(route, 0, sizeof(connect_route));
608 DEBUG(5) debugf("read_route, filename = %s\n", filename);
610 route->filename = g_strdup(filename);
611 route->name = g_strdup(filename); /* quick hack */
613 route->protocol = g_strdup("smtp");
614 route->expand_h_sender_address = TRUE;
616 route->is_local_net = is_local_net;
618 route->do_pipelining = TRUE;
620 if ((in = fopen(route->filename, "r"))) {
621 gchar lval[256], rval[2048];
622 while (read_statement(in, lval, 256, rval, 2048)) {
623 if (strcmp(lval, "protocol") == 0)
624 route->protocol = g_strdup(rval);
625 else if (strcmp(lval, "mail_host") == 0)
626 route->mail_host = parse_interface(rval, conf.remote_port);
627 else if (strcmp(lval, "helo_name") == 0)
628 route->helo_name = g_strdup(rval);
629 else if (strcmp(lval, "wrapper") == 0)
630 route->wrapper = g_strdup(rval);
631 else if (strcmp(lval, "connect_error_fail") == 0)
632 route->connect_error_fail = parse_boolean(rval);
633 else if (strcmp(lval, "do_correct_helo") == 0)
634 route->do_correct_helo = parse_boolean(rval);
635 else if (strcmp(lval, "do_pipelining") == 0)
636 route->do_pipelining = parse_boolean(rval);
637 else if (strcmp(lval, "allowed_return_paths") == 0)
638 route->allowed_return_paths = parse_address_list(rval, TRUE);
639 else if (strcmp(lval, "allowed_mail_locals") == 0)
640 route->allowed_mail_locals = parse_list(rval, TRUE);
641 else if (strcmp(lval, "not_allowed_return_paths") == 0)
642 route->not_allowed_return_paths = parse_address_list(rval, TRUE);
643 else if (strcmp(lval, "not_allowed_mail_locals") == 0)
644 route->not_allowed_mail_locals = parse_list(rval, TRUE);
645 else if (strcmp(lval, "allowed_rcpt_domains") == 0)
646 route->allowed_rcpt_domains = parse_list(rval, TRUE);
647 else if (strcmp(lval, "not_allowed_rcpt_domains") == 0)
648 route->not_allowed_rcpt_domains = parse_list(rval, TRUE);
649 else if (strcmp(lval, "set_h_from_domain") == 0)
650 route->set_h_from_domain = g_strdup(rval);
651 else if (strcmp(lval, "set_h_reply_to_domain") == 0)
652 route->set_h_reply_to_domain = g_strdup(rval);
653 else if (strcmp(lval, "set_return_path_domain") == 0)
654 route->set_return_path_domain = g_strdup(rval);
655 else if (strcmp(lval, "map_return_path_addresses") == 0) {
656 GList *node, *list;
658 list = parse_list(rval, TRUE);
659 foreach(list, node) {
660 gchar *item = (gchar *) (node->data);
661 table_pair *pair = parse_table_pair(item, ':');
662 address *addr = create_address((gchar *) (pair->value), TRUE);
663 g_free(pair->value);
664 pair->value = (gpointer *) addr;
665 route->map_return_path_addresses = g_list_append(route->map_return_path_addresses, pair);
666 g_free(item);
667 }
668 g_list_free(list);
669 } else if (strcmp(lval, "map_h_from_addresses") == 0) {
670 GList *list, *node;
672 list = parse_list(rval, TRUE);
673 foreach(list, node) {
674 gchar *item = (gchar *) (node->data);
675 table_pair *pair = parse_table_pair(item, ':');
676 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
677 g_free(item);
678 }
679 g_list_free(list);
680 } else if (strcmp(lval, "map_h_reply_to_addresses") == 0) {
681 GList *list, *node;
683 list = parse_list(rval, TRUE);
684 foreach(list, node) {
685 gchar *item = (gchar *) (node->data);
686 table_pair *pair = parse_table_pair(item, ':');
687 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
688 g_free(item);
689 }
690 g_list_free(list);
691 } else if (strcmp(lval, "map_h_mail_followup_to_addresses") == 0) {
692 GList *list, *node;
694 list = parse_list(rval, TRUE);
695 foreach(list, node) {
696 gchar *item = (gchar *) (node->data);
697 table_pair *pair = parse_table_pair(item, ':');
698 route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
699 g_free(item);
700 }
701 g_list_free(list);
702 } else if (strcmp(lval, "expand_h_sender_domain") == 0) {
703 route->expand_h_sender_domain = parse_boolean(rval);
704 } else if (strcmp(lval, "expand_h_sender_address") == 0) {
705 route->expand_h_sender_address = parse_boolean(rval);
706 } else if (strcmp(lval, "resolve_list") == 0)
707 route->resolve_list = parse_resolve_list(rval);
708 else if (strcmp(lval, "do_ssl") == 0) {
709 /* we ignore this. This option is used by sqilconf */
710 ;
711 }
712 #ifdef ENABLE_AUTH
713 else if (strcmp(lval, "auth_name") == 0) {
714 route->auth_name = g_strdup(rval);
715 } else if (strcmp(lval, "auth_login") == 0) {
716 route->auth_login = g_strdup(rval);
717 } else if (strcmp(lval, "auth_secret") == 0) {
718 route->auth_secret = g_strdup(rval);
719 }
720 #else
721 else if ((strcmp(lval, "auth_name") == 0)
722 || (strcmp(lval, "auth_login") == 0)
723 || (strcmp(lval, "auth_secret") == 0)) {
724 logwrite(LOG_WARNING, "%s ignored: not compiled with auth support.\n", lval);
725 }
726 #endif
727 else if (strcmp(lval, "pop3_login") == 0) {
728 #ifdef ENABLE_POP3
729 route->pop3_login = g_strdup(rval);
730 #else
731 logwrite(LOG_WARNING, "pop3_login ignored: not compiled with pop support.\n");
732 #endif
733 } else if (strcmp(lval, "pipe") == 0) {
734 route->pipe = g_strdup(rval);
735 } else if (strcmp(lval, "pipe_fromline") == 0) {
736 route->pipe_fromline = parse_boolean(rval);
737 } else if (strcmp(lval, "pipe_fromhack") == 0) {
738 route->pipe_fromhack = parse_boolean(rval);
739 } else if (strcmp(lval, "last_route") == 0) {
740 route->last_route = parse_boolean(rval);
741 } else
742 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
743 }
745 if (route->resolve_list == NULL) {
746 if (is_local_net) {
747 route->resolve_list = g_list_append(NULL, resolve_byname);
748 } else {
749 #ifdef ENABLE_RESOLVER
750 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_mx);
751 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_a);
752 #endif
753 route->resolve_list = g_list_append(route->resolve_list, resolve_byname);
754 }
755 }
756 fclose(in);
757 ok = TRUE;
759 /* warn user about misconfigurations: */
760 if ((route->map_h_from_addresses != NULL)
761 && (route->set_h_from_domain != NULL)) {
762 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides 'set_h_from_domain'\n");
763 g_free(route->set_h_from_domain);
764 route->set_h_from_domain = NULL;
765 }
766 if ((route->map_h_reply_to_addresses != NULL)
767 && (route->set_h_reply_to_domain != NULL)) {
768 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides 'set_h_reply_to_domain'\n");
769 g_free(route->set_h_reply_to_domain);
770 route->set_h_reply_to_domain = NULL;
771 }
772 } else {
773 logwrite(LOG_ALERT, "could not open route file %s: %s\n", route->filename, strerror(errno));
774 }
776 if (!ok) {
777 g_free(route);
778 route = NULL;
779 }
781 return route;
782 }
784 static void
785 _g_list_free_all(GList * list)
786 {
787 GList *node;
788 if (list) {
789 foreach(list, node)
790 g_free(node->data);
791 g_list_free(list);
792 }
793 }
795 void
796 destroy_route(connect_route * r)
797 {
798 if (r->filename)
799 g_free(r->filename);
800 if (r->protocol)
801 g_free(r->protocol);
802 if (r->mail_host) {
803 g_free(r->mail_host->address);
804 g_free(r->mail_host);
805 }
806 if (r->wrapper)
807 g_free(r->wrapper);
808 if (r->helo_name)
809 g_free(r->helo_name);
810 _g_list_free_all(r->allowed_mail_locals);
811 _g_list_free_all(r->not_allowed_mail_locals);
812 _g_list_free_all(r->allowed_rcpt_domains);
813 _g_list_free_all(r->not_allowed_rcpt_domains);
814 if (r->set_h_from_domain)
815 g_free(r->set_h_from_domain);
816 if (r->set_h_reply_to_domain)
817 g_free(r->set_h_reply_to_domain);
818 if (r->set_return_path_domain)
819 g_free(r->set_return_path_domain);
820 if (r->map_h_reply_to_addresses)
821 destroy_table(r->map_h_reply_to_addresses);
822 if (r->resolve_list)
823 g_list_free(r->resolve_list);
824 #ifdef ENABLE_AUTH
825 if (r->auth_name)
826 g_free(r->auth_name);
827 if (r->auth_login)
828 g_free(r->auth_login);
829 if (r->auth_secret)
830 g_free(r->auth_secret);
831 #endif
832 #ifdef ENABLE_POP3
833 if (r->pop3_login)
834 g_free(r->pop3_login);
835 #endif
836 if (r->pipe)
837 g_free(r->pipe);
838 g_free(r);
839 }
841 GList*
842 read_route_list(GList * rf_list, gboolean is_local_net)
843 {
844 GList *list = NULL;
845 GList *node;
846 uid_t saved_uid, saved_gid;
848 if (!conf.run_as_user) {
849 set_euidgid(0, 0, &saved_uid, &saved_gid);
850 }
852 foreach(rf_list, node) {
853 gchar *fname = (gchar *) (node->data);
854 connect_route *route = read_route(fname, is_local_net);
855 if (route)
856 list = g_list_append(list, route);
857 else
858 logwrite(LOG_ALERT, "could not read route configuration %s\n", fname);
859 }
861 /* set uid and gid back */
862 if (!conf.run_as_user) {
863 set_euidgid(saved_uid, saved_gid, NULL, NULL);
864 }
866 return list;
867 }
869 void
870 destroy_route_list(GList * list)
871 {
872 GList *node;
874 foreach(list, node) {
875 connect_route *route = (connect_route *) (node->data);
876 destroy_route(route);
877 }
878 g_list_free(list);
879 }
881 #ifdef ENABLE_POP3
883 get_conf*
884 read_get_conf(gchar * filename)
885 {
886 FILE *in;
888 get_conf *gc = g_malloc(sizeof(get_conf));
889 memset(gc, 0, sizeof(get_conf));
891 gc->server_port = 110;
893 if ((in = fopen(filename, "r"))) {
894 gchar lval[256], rval[2048];
895 while (read_statement(in, lval, 256, rval, 2048)) {
896 if (strcmp(lval, "protocol") == 0)
897 gc->protocol = g_strdup(rval);
898 else if (strcmp(lval, "server") == 0)
899 gc->server_name = g_strdup(rval);
900 else if (strcmp(lval, "port") == 0)
901 gc->server_port = atoi(rval);
902 else if (strcmp(lval, "wrapper") == 0)
903 gc->wrapper = g_strdup(rval);
904 else if (strcmp(lval, "user") == 0)
905 gc->login_user = g_strdup(rval);
906 else if (strcmp(lval, "pass") == 0)
907 gc->login_pass = g_strdup(rval);
908 else if (strcmp(lval, "address") == 0)
909 gc->address = create_address_qualified(rval, TRUE, conf.host_name);
910 else if (strcmp(lval, "return_path") == 0)
911 gc->return_path = create_address_qualified(rval, TRUE, conf.host_name);
912 else if (strcmp(lval, "do_ssl") == 0)
913 /* we ignore this. This option is used by sqilconf */
914 ;
915 else if (strcmp(lval, "do_keep") == 0)
916 gc->do_keep = parse_boolean(rval);
917 else if (strcmp(lval, "do_uidl") == 0)
918 gc->do_uidl = parse_boolean(rval);
919 else if (strcmp(lval, "do_uidl_dele") == 0)
920 gc->do_uidl_dele = parse_boolean(rval);
921 else if (strcmp(lval, "max_size") == 0)
922 gc->max_size = atoi(rval);
923 else if (strcmp(lval, "max_size_delete") == 0)
924 gc->max_size = parse_boolean(rval);
925 else if (strcmp(lval, "max_count") == 0)
926 gc->max_count = atoi(rval);
927 else if (strcmp(lval, "resolve_list") == 0)
928 gc->resolve_list = parse_resolve_list(rval);
929 else
930 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
931 }
932 fclose(in);
934 if (gc->resolve_list == NULL) {
935 #ifdef ENABLE_RESOLVER
936 gc->resolve_list = g_list_append(NULL, resolve_dns_a);
937 #endif
938 gc->resolve_list = g_list_append(NULL, resolve_byname);
939 }
941 if (gc->protocol == NULL)
942 gc->protocol = g_strdup("pop3");
943 return gc;
944 }
945 logwrite(LOG_ALERT, "could not open get file %s: %s\n", filename, strerror(errno));
947 g_free(gc);
948 return NULL;
949 }
951 void
952 destroy_get_conf(get_conf * gc)
953 {
954 if (gc->protocol)
955 g_free(gc->protocol);
956 if (gc->server_name)
957 g_free(gc->server_name);
958 if (gc->login_user)
959 g_free(gc->login_user);
960 if (gc->login_pass)
961 g_free(gc->login_pass);
962 if (gc->wrapper)
963 g_free(gc->wrapper);
964 if (gc->address)
965 destroy_address(gc->address);
966 if (gc->return_path)
967 destroy_address(gc->return_path);
968 if (gc->resolve_list)
969 g_list_free(gc->resolve_list);
970 g_free(gc);
971 }
973 #endif
975 connect_route*
976 create_local_route()
977 {
978 connect_route *route;
980 route = g_malloc(sizeof(connect_route));
981 if (route) {
982 memset(route, 0, sizeof(connect_route));
983 route->protocol = g_strdup("smtp");
984 route->is_local_net = TRUE;
985 route->name = g_strdup("local_net (default)");
986 route->expand_h_sender_address = TRUE;
987 route->resolve_list = g_list_append(NULL, resolve_byname);
988 route->connect_error_fail = TRUE;
989 }
990 return route;
991 }