masqmail-0.2
view src/conf.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | 49dab67fe461 |
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 "masqmail.h"
21 #include "pwd.h"
22 #include "grp.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
111 makes a GList out of it.
112 */
113 GList*
114 parse_list(gchar * line, gboolean read_file)
115 {
116 GList *list = NULL;
117 gchar buf[256];
118 gchar *p, *q;
120 DEBUG(6) fprintf(stderr, "parsing list %s\n", line);
122 p = line;
123 while (*p != 0) {
124 q = buf;
126 while (*p && (*p != ';') && (q < buf + 255))
127 *(q++) = *(p++);
128 *q = 0;
130 if ((buf[0] == '/') && (read_file))
131 /* item is a filename, include its contents */
132 list = g_list_concat(list, parse_list_file(buf));
133 else
134 /* just a normal item */
135 list = g_list_append(list, g_strdup(buf));
137 DEBUG(6) printf("item = %s\n", buf);
139 if (*p)
140 p++;
141 }
142 return list;
143 }
145 static GList*
146 parse_address_list(gchar * line, gboolean read_file)
147 {
148 GList *plain_list = parse_list(line, read_file);
149 GList *node;
150 GList *list = NULL;
152 foreach(plain_list, node) {
153 gchar *item = (gchar *) (node->data);
154 address *addr = create_address(item, TRUE);
155 if (addr)
156 list = g_list_append(list, addr);
157 g_free(item);
158 }
159 g_list_free(plain_list);
161 return list;
162 }
164 static GList*
165 parse_resolve_list(gchar * line)
166 {
167 GList *list;
168 GList *list_node;
169 GList *res_list = NULL;
171 list = parse_list(line, FALSE);
172 if (list) {
173 foreach(list, list_node) {
174 gchar *item = (gchar *) (list_node->data);
175 if (strcmp(item, "byname") == 0) {
176 res_list = g_list_append(res_list, resolve_byname);
177 #ifdef ENABLE_RESOLVER
178 } else if (strcmp(item, "dns_a") == 0) {
179 res_list = g_list_append(res_list, resolve_dns_a);
180 } else if (strcmp(item, "dns_mx") == 0) {
181 res_list = g_list_append(res_list, resolve_dns_mx);
182 #endif
183 } else {
184 logwrite(LOG_ALERT, "unknown resolver %s\n", item);
185 exit(EXIT_FAILURE);
186 }
187 g_free(item);
188 }
189 g_list_free(list);
190 }
191 return res_list;
192 }
194 static interface*
195 parse_interface(gchar * line, gint def_port)
196 {
197 gchar buf[256];
198 gchar *p, *q;
199 interface *iface;
201 DEBUG(6) fprintf(stderr, "parse_interface: %s\n", line);
203 p = line;
204 q = buf;
205 while ((*p != 0) && (*p != ':') && (q < buf + 255))
206 *(q++) = *(p++);
207 *q = 0;
209 iface = g_malloc(sizeof(interface));
210 iface->address = g_strdup(buf);
212 if (*p) {
213 p++;
214 iface->port = atoi(p);
215 } else
216 iface->port = def_port;
218 return iface;
219 }
221 #ifdef ENABLE_IDENT /* so far used for that only */
222 static struct in_addr*
223 parse_network(gchar * line, gint def_port)
224 {
225 gchar buf[256];
226 gchar *p, *q;
227 struct in_addr addr, mask_addr, net_addr, *p_net_addr;
228 guint n;
230 DEBUG(6) fprintf(stderr, "parse_network: %s\n", line);
232 p = line;
233 q = buf;
234 while ((*p != 0) && (*p != '/') && (q < buf + 255))
235 *(q++) = *(p++);
236 *q = 0;
238 if ((addr.s_addr = inet_addr(buf)) != INADDR_NONE) {
239 if (*p) {
240 guint i;
241 p++;
242 i = atoi(p);
243 if ((i >= 0) && (i <= 32))
244 n = i ? ~((1 << (32 - i)) - 1) : 0;
245 else {
246 fprintf(stderr, "'%d' is not a valid net mask (must be >= 0 and <= 32)\n", i);
247 exit(EXIT_FAILURE);
248 }
249 } else
250 n = 0;
252 mask_addr.s_addr = htonl(n);
253 net_addr.s_addr = mask_addr.s_addr & addr.s_addr;
254 } else {
255 fprintf(stderr, "'%s' is not a valid address (must be ip)\n", buf);
256 exit(EXIT_FAILURE);
257 }
259 p_net_addr = g_malloc(sizeof(struct in_addr));
260 p_net_addr->s_addr = net_addr.s_addr;
261 return p_net_addr;
262 }
263 #endif
265 static gboolean
266 eat_comments(FILE * in)
267 {
268 gint c;
270 for (c = fgetc(in); (c == '#' || isspace(c)) && c != EOF;
271 c = fgetc(in)) {
272 if (c == '#') {
273 gint c;
274 for (c = fgetc(in); (c != '\n') && (c != EOF); c = fgetc(in));
275 }
276 }
277 if (c == EOF)
278 return FALSE;
279 ungetc(c, in);
280 return TRUE;
281 }
283 /* after parsing, eat trailing character until LF */
284 static gboolean
285 eat_line_trailing(FILE * in)
286 {
287 gint c;
289 for (c = fgetc(in); c != EOF && c != '\n'; c = fgetc(in));
290 if (c == EOF)
291 return FALSE;
292 return TRUE;
293 }
295 static gboolean
296 eat_spaces(FILE * in)
297 {
298 gint c;
300 for (c = fgetc(in); c != EOF && isspace(c); c = fgetc(in));
301 if (c == EOF)
302 return FALSE;
303 ungetc(c, in);
304 return TRUE;
305 }
307 static gboolean
308 read_lval(FILE * in, gchar * buf, gint size)
309 {
310 gint c;
311 gchar *ptr = buf;
313 DEBUG(6) fprintf(stderr, "read_lval()\n");
315 if (!eat_spaces(in))
316 return FALSE;
318 c = fgetc(in);
319 DEBUG(6) fprintf(stderr, "read_lval() 2\n");
320 while ((isalnum(c) || c == '_' || c == '-' || c == '.')
321 && (ptr < buf + size - 1)
322 && (c != EOF)) {
323 *ptr = c;
324 ptr++;
325 c = fgetc(in);
326 }
327 *ptr = 0;
328 ungetc(c, in);
330 if (c == EOF) {
331 fprintf(stderr, "unexpected EOF after %s\n", buf);
332 return FALSE;
333 } else if (ptr >= buf + size - 1) {
334 fprintf(stderr, "lval too long\n");
335 }
337 eat_spaces(in);
339 DEBUG(6) fprintf(stderr, "lval = %s\n", buf);
341 return buf[0] != 0;
342 }
344 static gboolean
345 read_rval(FILE * in, gchar * buf, gint size)
346 {
347 gint c;
348 gchar *ptr = buf;
350 DEBUG(6) fprintf(stderr, "read_rval()\n");
352 if (!eat_spaces(in))
353 return FALSE;
355 c = fgetc(in);
356 if (c != '\"') {
357 while ((isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/'
358 || c == '@' || c == ';')
359 && (ptr < buf + size - 1)
360 && (c != EOF)) {
361 *ptr = c;
362 ptr++;
363 c = fgetc(in);
364 }
365 *ptr = 0;
366 ungetc(c, in);
367 } else {
368 gboolean escape = FALSE;
369 c = fgetc(in);
370 while (((c != '\"') || escape) && (ptr < buf + size - 1)) {
371 if (c != '\n') { /* ignore line breaks */
372 if ((c == '\\') && (!escape)) {
373 escape = TRUE;
374 } else {
375 *ptr = c;
376 ptr++;
377 escape = FALSE;
378 }
379 }
380 c = fgetc(in);
381 }
382 *ptr = 0;
383 }
385 eat_line_trailing(in);
387 DEBUG(6) fprintf(stderr, "rval = %s\n", buf);
389 return TRUE;
390 }
392 static gboolean
393 read_statement(FILE * in, gchar * lval, gint lsize, gchar * rval, gint rsize)
394 {
395 gint c;
397 DEBUG(6) fprintf(stderr, "read_statement()\n");
399 /* eat comments and empty lines: */
400 if (!eat_comments(in))
401 return FALSE;
403 DEBUG(6) fprintf(stderr, "read_statement() 1\n");
405 if (read_lval(in, lval, lsize)) {
406 DEBUG(6) fprintf(stderr, "lval = %s\n", lval);
407 if ((c = fgetc(in) == '=')) {
408 if (read_rval(in, rval, rsize)) {
409 DEBUG(6) fprintf(stderr, "rval = %s\n", rval);
410 return TRUE;
411 }
412 } else {
413 fprintf(stderr, "'=' expected after %s, char was '%c'\n", lval,
414 c);
415 }
416 }
417 return FALSE;
418 }
420 gboolean
421 read_conf(gchar * filename)
422 {
423 FILE *in;
425 conf.log_max_pri = 7;
426 conf.remote_port = 25;
427 conf.do_relay = TRUE;
428 conf.alias_local_cmp = strcmp;
429 conf.max_defer_time = 86400 * 4; /* 4 days */
431 if ((in = fopen(filename, "r"))) {
432 gchar lval[256], rval[2048];
433 while (read_statement(in, lval, 256, rval, 2048)) {
434 if (strcmp(lval, "debug_level") == 0)
435 conf.debug_level = atoi(rval);
436 else if (strcmp(lval, "run_as_user") == 0) {
437 if (!conf.run_as_user) /* you should not be able to reset that flag */
438 conf.run_as_user = parse_boolean(rval);
439 } else if (strcmp(lval, "use_syslog") == 0)
440 conf.use_syslog = parse_boolean(rval);
441 else if (strcmp(lval, "mail_dir") == 0)
442 conf.mail_dir = g_strdup(rval);
443 else if (strcmp(lval, "lock_dir") == 0)
444 conf.lock_dir = g_strdup(rval);
445 else if (strcmp(lval, "spool_dir") == 0)
446 conf.spool_dir = g_strdup(rval);
447 else if (strcmp(lval, "log_dir") == 0)
448 conf.log_dir = g_strdup(rval);
449 else if (strcmp(lval, "host_name") == 0) {
450 if (rval[0] != '/')
451 conf.host_name = g_strdup(rval);
452 else {
453 char buf[256];
454 FILE *fptr = fopen(rval, "rt");
455 if (fptr) {
456 fgets(buf, 255, fptr);
457 g_strchomp(buf);
458 conf.host_name = g_strdup(buf);
459 fclose(fptr);
460 } else {
461 fprintf(stderr, "could not open %s: %s\n", rval,
462 strerror(errno));
463 return FALSE;
464 }
465 }
466 } else if (strcmp(lval, "remote_port") == 0) {
467 fprintf(stderr, "the remote_port option is now deprecated. Use 'mail_host' in the\n"
468 "route configuration instead. See man masqmail.route\n");
469 conf.remote_port = atoi(rval);
470 } else if (strcmp(lval, "local_hosts") == 0)
471 conf.local_hosts = parse_list(rval, FALSE);
472 else if (strcmp(lval, "local_addresses") == 0)
473 conf.local_addresses = parse_list(rval, TRUE);
474 else if (strcmp(lval, "not_local_addresses") == 0)
475 conf.not_local_addresses = parse_list(rval, TRUE);
476 else if (strcmp(lval, "local_nets") == 0)
477 conf.local_nets = parse_list(rval, FALSE);
478 else if (strcmp(lval, "do_save_envelope_to") == 0)
479 conf.do_save_envelope_to = parse_boolean(rval);
480 else if (strcmp(lval, "defer_all") == 0)
481 conf.defer_all = parse_boolean(rval);
482 else if (strcmp(lval, "do_relay") == 0)
483 conf.do_relay = parse_boolean(rval);
484 else if (strcmp(lval, "alias_file") == 0) {
485 conf.alias_file = g_strdup(rval);
486 } else if (strcmp(lval, "alias_local_caseless") == 0) {
487 conf.alias_local_cmp = parse_boolean(rval) ? strcasecmp : strcmp;
488 } else if (strcmp(lval, "mbox_default") == 0) {
489 conf.mbox_default = g_strdup(rval);
490 } else if (strcmp(lval, "mbox_users") == 0) {
491 conf.mbox_users = parse_list(rval, TRUE);
492 } else if (strcmp(lval, "mda_users") == 0) {
493 conf.mda_users = parse_list(rval, TRUE);
494 } else if (strcmp(lval, "maildir_users") == 0) {
495 conf.maildir_users = parse_list(rval, TRUE);
496 } else if (strcmp(lval, "mda") == 0) {
497 conf.mda = g_strdup(rval);
498 } else if (strcmp(lval, "mda_fromline") == 0) {
499 conf.mda_fromline = parse_boolean(rval);
500 } else if (strcmp(lval, "mda_fromhack") == 0) {
501 conf.mda_fromhack = parse_boolean(rval);
502 } else if (strcmp(lval, "pipe_fromline") == 0) {
503 conf.pipe_fromline = parse_boolean(rval);
504 } else if (strcmp(lval, "pipe_fromhack") == 0) {
505 conf.pipe_fromhack = parse_boolean(rval);
506 } else if (strcmp(lval, "listen_addresses") == 0) {
507 GList *node;
508 GList *tmp_list = parse_list(rval, FALSE);
510 conf.listen_addresses = NULL;
511 foreach(tmp_list, node) {
512 conf.listen_addresses = g_list_append(conf.listen_addresses, parse_interface((gchar *) (node-> data), 25));
513 g_free(node->data);
514 }
515 g_list_free(tmp_list);
516 } else if (strcmp(lval, "ident_trusted_nets") == 0) {
517 #ifdef ENABLE_IDENT
518 GList *node;
519 GList *tmp_list = parse_list(rval, FALSE);
521 conf.ident_trusted_nets = NULL;
522 foreach(tmp_list, node) {
523 conf.ident_trusted_nets = g_list_append(conf.ident_trusted_nets, parse_network((gchar *) (node->data), 25));
524 g_free(node->data);
525 }
526 g_list_free(tmp_list);
527 #else
528 fprintf(stderr, "%s ignored: not compiled with ident support\n", lval);
529 #endif
530 } else if ((strncmp(lval, "connect_route.", 14) == 0)
531 || (strncmp(lval, "online_routes.", 14) == 0)) {
532 GList *file_list = parse_list(rval, FALSE);
533 table_pair *pair = create_pair(&(lval[14]), file_list);
534 conf.connect_routes = g_list_append(conf.connect_routes, pair);
535 } else if (strcmp(lval, "local_net_route") == 0) {
536 conf.local_net_routes = parse_list(rval, FALSE);
537 } else if (strcmp(lval, "online_detect") == 0)
538 conf.online_detect = g_strdup(rval);
539 else if (strcmp(lval, "online_file") == 0)
540 conf.online_file = g_strdup(rval);
541 else if (strcmp(lval, "online_pipe") == 0)
542 conf.online_pipe = g_strdup(rval);
543 else if (strcmp(lval, "mserver_iface") == 0)
544 conf.mserver_iface = parse_interface(rval, 224);
545 else if (strcmp(lval, "do_queue") == 0)
546 conf.do_queue = parse_boolean(rval);
547 else if (strncmp(lval, "get.", 4) == 0) {
548 #ifdef ENABLE_POP3
549 table_pair *pair = create_pair_string(&(lval[4]), rval);
550 conf.get_names = g_list_append(conf.get_names, pair);
551 #else
552 fprintf(stderr, "get.<name> ignored: not compiled with pop support\n");
553 #endif
554 } else if (strncmp(lval, "online_gets.", 12) == 0) {
555 #ifdef ENABLE_POP3
556 GList *file_list = parse_list(rval, FALSE);
557 table_pair *pair = create_pair(&(lval[12]), file_list);
558 conf.online_gets = g_list_append(conf.online_gets, pair);
559 #else
560 fprintf(stderr, "online_gets.<name> ignored: not compiled with pop support\n");
561 #endif
562 } else if (strcmp(lval, "errmsg_file") == 0)
563 conf.errmsg_file = g_strdup(rval);
564 else if (strcmp(lval, "warnmsg_file") == 0)
565 conf.warnmsg_file = g_strdup(rval);
566 else if (strcmp(lval, "warn_intervals") == 0)
567 conf.warn_intervals = parse_list(rval, FALSE);
568 else if (strcmp(lval, "max_defer_time") == 0) {
569 gint dummy;
570 gint ival = time_interval(rval, &dummy);
571 if (ival < 0)
572 fprintf(stderr, "invalid time interval for 'max_defer_time': %s\n", rval);
573 else
574 conf.max_defer_time = ival;
575 } else if (strcmp(lval, "log_user") == 0)
576 conf.log_user = g_strdup(rval);
578 else
579 fprintf(stderr, "var '%s' not (yet) known, ignored\n", lval);
580 }
581 fclose(in);
583 if (conf.errmsg_file == NULL)
584 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
585 if (conf.warnmsg_file == NULL)
586 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
588 if (conf.lock_dir == NULL)
589 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
591 if (conf.mbox_default == NULL)
592 conf.mbox_default = g_strdup("mbox");
594 if (conf.warn_intervals == NULL)
595 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", FALSE);
597 return TRUE;
598 } else
599 fprintf(stderr, "could not open config file %s: %s\n", filename, strerror(errno));
600 return FALSE;
601 }
603 connect_route*
604 read_route(gchar * filename, gboolean is_local_net)
605 {
606 gboolean ok = FALSE;
607 FILE *in;
609 connect_route *route = g_malloc(sizeof(connect_route));
610 memset(route, 0, sizeof(connect_route));
612 DEBUG(5) debugf("read_route, filename = %s\n", filename);
614 route->filename = g_strdup(filename);
615 route->name = g_strdup(filename); /* quick hack */
617 route->protocol = g_strdup("smtp");
618 route->expand_h_sender_address = TRUE;
620 route->is_local_net = is_local_net;
622 route->do_pipelining = TRUE;
624 if ((in = fopen(route->filename, "r"))) {
625 gchar lval[256], rval[2048];
626 while (read_statement(in, lval, 256, rval, 2048)) {
627 if (strcmp(lval, "protocol") == 0)
628 route->protocol = g_strdup(rval);
629 else if (strcmp(lval, "mail_host") == 0)
630 route->mail_host = parse_interface(rval, conf.remote_port);
631 else if (strcmp(lval, "helo_name") == 0)
632 route->helo_name = g_strdup(rval);
633 else if (strcmp(lval, "wrapper") == 0)
634 route->wrapper = g_strdup(rval);
635 else if (strcmp(lval, "connect_error_fail") == 0)
636 route->connect_error_fail = parse_boolean(rval);
637 else if (strcmp(lval, "do_correct_helo") == 0)
638 route->do_correct_helo = parse_boolean(rval);
639 else if (strcmp(lval, "do_pipelining") == 0)
640 route->do_pipelining = parse_boolean(rval);
641 else if (strcmp(lval, "allowed_return_paths") == 0)
642 route->allowed_return_paths = parse_address_list(rval, TRUE);
643 else if (strcmp(lval, "allowed_mail_locals") == 0)
644 route->allowed_mail_locals = parse_list(rval, TRUE);
645 else if (strcmp(lval, "not_allowed_return_paths") == 0)
646 route->not_allowed_return_paths = parse_address_list(rval, TRUE);
647 else if (strcmp(lval, "not_allowed_mail_locals") == 0)
648 route->not_allowed_mail_locals = parse_list(rval, TRUE);
649 else if (strcmp(lval, "allowed_rcpt_domains") == 0)
650 route->allowed_rcpt_domains = parse_list(rval, TRUE);
651 else if (strcmp(lval, "not_allowed_rcpt_domains") == 0)
652 route->not_allowed_rcpt_domains = parse_list(rval, TRUE);
653 else if (strcmp(lval, "set_h_from_domain") == 0)
654 route->set_h_from_domain = g_strdup(rval);
655 else if (strcmp(lval, "set_h_reply_to_domain") == 0)
656 route->set_h_reply_to_domain = g_strdup(rval);
657 else if (strcmp(lval, "set_return_path_domain") == 0)
658 route->set_return_path_domain = g_strdup(rval);
659 else if (strcmp(lval, "map_return_path_addresses") == 0) {
660 GList *node, *list;
662 list = parse_list(rval, TRUE);
663 foreach(list, node) {
664 gchar *item = (gchar *) (node->data);
665 table_pair *pair = parse_table_pair(item, ':');
666 address *addr = create_address((gchar *) (pair->value), TRUE);
667 g_free(pair->value);
668 pair->value = (gpointer *) addr;
669 route->map_return_path_addresses = g_list_append(route->map_return_path_addresses, pair);
670 g_free(item);
671 }
672 g_list_free(list);
673 } else if (strcmp(lval, "map_h_from_addresses") == 0) {
674 GList *list, *node;
676 list = parse_list(rval, TRUE);
677 foreach(list, node) {
678 gchar *item = (gchar *) (node->data);
679 table_pair *pair = parse_table_pair(item, ':');
680 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
681 g_free(item);
682 }
683 g_list_free(list);
684 } else if (strcmp(lval, "map_h_reply_to_addresses") == 0) {
685 GList *list, *node;
687 list = parse_list(rval, TRUE);
688 foreach(list, node) {
689 gchar *item = (gchar *) (node->data);
690 table_pair *pair = parse_table_pair(item, ':');
691 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
692 g_free(item);
693 }
694 g_list_free(list);
695 } else if (strcmp(lval, "map_h_mail_followup_to_addresses") == 0) {
696 GList *list, *node;
698 list = parse_list(rval, TRUE);
699 foreach(list, node) {
700 gchar *item = (gchar *) (node->data);
701 table_pair *pair = parse_table_pair(item, ':');
702 route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
703 g_free(item);
704 }
705 g_list_free(list);
706 } else if (strcmp(lval, "expand_h_sender_domain") == 0) {
707 route->expand_h_sender_domain = parse_boolean(rval);
708 } else if (strcmp(lval, "expand_h_sender_address") == 0) {
709 route->expand_h_sender_address = parse_boolean(rval);
710 } else if (strcmp(lval, "resolve_list") == 0)
711 route->resolve_list = parse_resolve_list(rval);
712 else if (strcmp(lval, "do_ssl") == 0) {
713 /* we ignore this. This option is used by sqilconf */
714 ;
715 }
716 #ifdef ENABLE_AUTH
717 else if (strcmp(lval, "auth_name") == 0) {
718 route->auth_name = g_strdup(rval);
719 } else if (strcmp(lval, "auth_login") == 0) {
720 route->auth_login = g_strdup(rval);
721 } else if (strcmp(lval, "auth_secret") == 0) {
722 route->auth_secret = g_strdup(rval);
723 }
724 #else
725 else if ((strcmp(lval, "auth_name") == 0)
726 || (strcmp(lval, "auth_login") == 0)
727 || (strcmp(lval, "auth_secret") == 0)) {
728 logwrite(LOG_WARNING, "%s ignored: not compiled with auth support.\n", lval);
729 }
730 #endif
731 else if (strcmp(lval, "pop3_login") == 0) {
732 #ifdef ENABLE_POP3
733 route->pop3_login = g_strdup(rval);
734 #else
735 logwrite(LOG_WARNING, "pop3_login ignored: not compiled with pop support.\n");
736 #endif
737 } else if (strcmp(lval, "pipe") == 0) {
738 route->pipe = g_strdup(rval);
739 } else if (strcmp(lval, "pipe_fromline") == 0) {
740 route->pipe_fromline = parse_boolean(rval);
741 } else if (strcmp(lval, "pipe_fromhack") == 0) {
742 route->pipe_fromhack = parse_boolean(rval);
743 } else if (strcmp(lval, "last_route") == 0) {
744 route->last_route = parse_boolean(rval);
745 } else
746 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
747 }
749 if (route->resolve_list == NULL) {
750 if (is_local_net) {
751 route->resolve_list = g_list_append(NULL, resolve_byname);
752 } else {
753 #ifdef ENABLE_RESOLVER
754 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_mx);
755 route->resolve_list = g_list_append(route->resolve_list, resolve_dns_a);
756 #endif
757 route->resolve_list = g_list_append(route->resolve_list, resolve_byname);
758 }
759 }
760 fclose(in);
761 ok = TRUE;
763 /* warn user about misconfigurations: */
764 if ((route->map_h_from_addresses != NULL)
765 && (route->set_h_from_domain != NULL)) {
766 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides 'set_h_from_domain'\n");
767 g_free(route->set_h_from_domain);
768 route->set_h_from_domain = NULL;
769 }
770 if ((route->map_h_reply_to_addresses != NULL)
771 && (route->set_h_reply_to_domain != NULL)) {
772 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides 'set_h_reply_to_domain'\n");
773 g_free(route->set_h_reply_to_domain);
774 route->set_h_reply_to_domain = NULL;
775 }
776 } else {
777 logwrite(LOG_ALERT, "could not open route file %s: %s\n", route->filename, strerror(errno));
778 }
780 if (!ok) {
781 g_free(route);
782 route = NULL;
783 }
785 return route;
786 }
788 static void
789 _g_list_free_all(GList * list)
790 {
791 GList *node;
792 if (list) {
793 foreach(list, node)
794 g_free(node->data);
795 g_list_free(list);
796 }
797 }
799 void
800 destroy_route(connect_route * r)
801 {
802 if (r->filename)
803 g_free(r->filename);
804 if (r->protocol)
805 g_free(r->protocol);
806 if (r->mail_host) {
807 g_free(r->mail_host->address);
808 g_free(r->mail_host);
809 }
810 if (r->wrapper)
811 g_free(r->wrapper);
812 if (r->helo_name)
813 g_free(r->helo_name);
814 _g_list_free_all(r->allowed_mail_locals);
815 _g_list_free_all(r->not_allowed_mail_locals);
816 _g_list_free_all(r->allowed_rcpt_domains);
817 _g_list_free_all(r->not_allowed_rcpt_domains);
818 if (r->set_h_from_domain)
819 g_free(r->set_h_from_domain);
820 if (r->set_h_reply_to_domain)
821 g_free(r->set_h_reply_to_domain);
822 if (r->set_return_path_domain)
823 g_free(r->set_return_path_domain);
824 if (r->map_h_reply_to_addresses)
825 destroy_table(r->map_h_reply_to_addresses);
826 if (r->resolve_list)
827 g_list_free(r->resolve_list);
828 #ifdef ENABLE_AUTH
829 if (r->auth_name)
830 g_free(r->auth_name);
831 if (r->auth_login)
832 g_free(r->auth_login);
833 if (r->auth_secret)
834 g_free(r->auth_secret);
835 #endif
836 #ifdef ENABLE_POP3
837 if (r->pop3_login)
838 g_free(r->pop3_login);
839 #endif
840 if (r->pipe)
841 g_free(r->pipe);
842 g_free(r);
843 }
845 GList*
846 read_route_list(GList * rf_list, gboolean is_local_net)
847 {
848 GList *list = NULL;
849 GList *node;
850 uid_t saved_uid, saved_gid;
852 if (!conf.run_as_user) {
853 set_euidgid(0, 0, &saved_uid, &saved_gid);
854 }
856 foreach(rf_list, node) {
857 gchar *fname = (gchar *) (node->data);
858 connect_route *route = read_route(fname, is_local_net);
859 if (route)
860 list = g_list_append(list, route);
861 else
862 logwrite(LOG_ALERT, "could not read route configuration %s\n", fname);
863 }
865 /* set uid and gid back */
866 if (!conf.run_as_user) {
867 set_euidgid(saved_uid, saved_gid, NULL, NULL);
868 }
870 return list;
871 }
873 void
874 destroy_route_list(GList * list)
875 {
876 GList *node;
878 foreach(list, node) {
879 connect_route *route = (connect_route *) (node->data);
880 destroy_route(route);
881 }
882 g_list_free(list);
883 }
885 #ifdef ENABLE_POP3
887 get_conf*
888 read_get_conf(gchar * filename)
889 {
890 FILE *in;
892 get_conf *gc = g_malloc(sizeof(get_conf));
893 memset(gc, 0, sizeof(get_conf));
895 gc->server_port = 110;
897 if ((in = fopen(filename, "r"))) {
898 gchar lval[256], rval[2048];
899 while (read_statement(in, lval, 256, rval, 2048)) {
900 if (strcmp(lval, "protocol") == 0)
901 gc->protocol = g_strdup(rval);
902 else if (strcmp(lval, "server") == 0)
903 gc->server_name = g_strdup(rval);
904 else if (strcmp(lval, "port") == 0)
905 gc->server_port = atoi(rval);
906 else if (strcmp(lval, "wrapper") == 0)
907 gc->wrapper = g_strdup(rval);
908 else if (strcmp(lval, "user") == 0)
909 gc->login_user = g_strdup(rval);
910 else if (strcmp(lval, "pass") == 0)
911 gc->login_pass = g_strdup(rval);
912 else if (strcmp(lval, "address") == 0)
913 gc->address = create_address_qualified(rval, TRUE, conf.host_name);
914 else if (strcmp(lval, "return_path") == 0)
915 gc->return_path = create_address_qualified(rval, TRUE, conf.host_name);
916 else if (strcmp(lval, "do_ssl") == 0)
917 /* we ignore this. This option is used by sqilconf */
918 ;
919 else if (strcmp(lval, "do_keep") == 0)
920 gc->do_keep = parse_boolean(rval);
921 else if (strcmp(lval, "do_uidl") == 0)
922 gc->do_uidl = parse_boolean(rval);
923 else if (strcmp(lval, "do_uidl_dele") == 0)
924 gc->do_uidl_dele = parse_boolean(rval);
925 else if (strcmp(lval, "max_size") == 0)
926 gc->max_size = atoi(rval);
927 else if (strcmp(lval, "max_size_delete") == 0)
928 gc->max_size = parse_boolean(rval);
929 else if (strcmp(lval, "max_count") == 0)
930 gc->max_count = atoi(rval);
931 else if (strcmp(lval, "resolve_list") == 0)
932 gc->resolve_list = parse_resolve_list(rval);
933 else
934 logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval);
935 }
936 fclose(in);
938 if (gc->resolve_list == NULL) {
939 #ifdef ENABLE_RESOLVER
940 gc->resolve_list = g_list_append(NULL, resolve_dns_a);
941 #endif
942 gc->resolve_list = g_list_append(NULL, resolve_byname);
943 }
945 if (gc->protocol == NULL)
946 gc->protocol = g_strdup("pop3");
947 return gc;
948 }
949 logwrite(LOG_ALERT, "could not open get file %s: %s\n", filename, strerror(errno));
951 g_free(gc);
952 return NULL;
953 }
955 void
956 destroy_get_conf(get_conf * gc)
957 {
958 if (gc->protocol)
959 g_free(gc->protocol);
960 if (gc->server_name)
961 g_free(gc->server_name);
962 if (gc->login_user)
963 g_free(gc->login_user);
964 if (gc->login_pass)
965 g_free(gc->login_pass);
966 if (gc->wrapper)
967 g_free(gc->wrapper);
968 if (gc->address)
969 destroy_address(gc->address);
970 if (gc->return_path)
971 destroy_address(gc->return_path);
972 if (gc->resolve_list)
973 g_list_free(gc->resolve_list);
974 g_free(gc);
975 }
977 #endif
979 connect_route*
980 create_local_route()
981 {
982 connect_route *route;
984 route = g_malloc(sizeof(connect_route));
985 if (route) {
986 memset(route, 0, sizeof(connect_route));
987 route->protocol = g_strdup("smtp");
988 route->is_local_net = TRUE;
989 route->name = g_strdup("local_net (default)");
990 route->expand_h_sender_address = TRUE;
991 route->resolve_list = g_list_append(NULL, resolve_byname);
992 route->connect_error_fail = TRUE;
993 }
994 return route;
995 }