masqmail

view src/conf.c @ 394:c8e3d1a79313

Refactoring in conf.c: read_rval().
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 19:32:40 +0100
parents 5e728dd64c1b
children 5f0829f8e6c7
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 static gboolean
260 eat_spaces(FILE *in)
261 {
262 gint c;
264 while ((c = fgetc(in)) != EOF) {
265 if (!isspace(c)) {
266 ungetc(c, in);
267 return TRUE;
268 }
269 }
270 return FALSE;
271 }
273 static gboolean
274 read_lval(FILE *in, gchar *buf, gint size)
275 {
276 gint c;
277 gchar *ptr = buf;
279 DEBUG(9) fprintf(stderr, "read_lval()\n");
280 if (!eat_spaces(in)) {
281 return FALSE;
282 }
284 c = fgetc(in);
285 DEBUG(9) fprintf(stderr, "read_lval() 2\n");
286 while (1) {
287 if ((c = fgetc(in)) == EOF) {
288 fprintf(stderr, "unexpected EOF after %s\n", buf);
289 return FALSE;
290 }
291 if (ptr >= buf+size-1) {
292 fprintf(stderr, "lval too long\n");
293 break;
294 }
295 if (!isalnum(c) && c != '_' && c != '-' && c != '.') {
296 break;
297 }
298 *ptr++ = c;
299 }
300 *ptr = '\0';
301 ungetc(c, in);
302 eat_spaces(in);
303 DEBUG(9) fprintf(stderr, "lval = %s\n", buf);
304 return *buf != '\0';
305 }
307 static gboolean
308 read_rval(FILE *in, gchar *buf, gint size)
309 {
310 gint c;
311 gchar *ptr = buf;
313 DEBUG(9) fprintf(stderr, "read_rval()\n");
314 if (!eat_spaces(in)) {
315 return FALSE;
316 }
318 c = fgetc(in);
319 if (c != '"') {
320 /* unquoted rval */
321 ungetc(c, in);
322 while ((c = fgetc(in)) != EOF) {
323 if (ptr >= buf+size-1) {
324 /* rval too long */
325 break;
326 }
327 if (!isalnum(c) && c != '_' && c != '-' &&
328 c != '.' && c != '/' && c != '@' &&
329 c != ';' && c != ':') {
330 break;
331 }
332 *ptr++ = c;
333 }
334 *ptr = '\0';
335 ungetc(c, in);
336 } else {
337 /* quoted rval */
338 gboolean escape = FALSE;
339 while ((c = fgetc(in)) != EOF) {
340 if (ptr >= buf+size-1) {
341 /* rval too long */
342 break;
343 }
344 if (!escape && c == '"') {
345 break;
346 }
347 if (!escape && c == '\\') {
348 escape = TRUE;
349 continue;
350 }
351 *ptr++ = c;
352 escape = FALSE;
353 }
354 *ptr = '\0';
355 }
356 DEBUG(9) fprintf(stderr, "rval = %s\n", buf);
357 /* eat trailing of line */
358 while ((c = fgetc(in)) != EOF && c != '\n') {
359 continue;
360 }
362 return TRUE;
363 }
365 static gboolean
366 read_statement(FILE *in, gchar *lval, gint lsize, gchar *rval, gint rsize)
367 {
368 gint c;
370 DEBUG(9) fprintf(stderr, "read_statement()\n");
372 /* eat comments and empty lines: */
373 if (!eat_comments(in)) {
374 return FALSE;
375 }
376 if (!read_lval(in, lval, lsize)) {
377 return FALSE;
378 }
380 DEBUG(9) fprintf(stderr, " lval = %s\n", lval);
381 if ((c = fgetc(in) != '=')) {
382 fprintf(stderr, "'=' expected after %s, char was '%c'\n",
383 lval, c);
384 }
385 if (!read_rval(in, rval, rsize)) {
386 return FALSE;
387 }
388 DEBUG(9) fprintf(stderr, " rval = %s\n", rval);
389 return TRUE;
390 }
392 gboolean
393 read_conf(gchar *filename)
394 {
395 FILE *in;
396 gchar lval[256], rval[2048];
397 GList *listen_addrs_tmp = NULL;
399 conf.log_max_pri = 7;
400 conf.do_relay = TRUE;
401 conf.localpartcmp = strcmp;
402 conf.max_defer_time = 86400 * 4; /* 4 days */
403 conf.max_msg_size = 0; /* no limit on msg size */
404 conf.spool_dir = SPOOL_DIR;
405 conf.mail_dir = "/var/mail";
407 if (!(in = fopen(filename, "r"))) {
408 logwrite(LOG_ALERT, "could not open config file %s: %s\n",
409 filename, strerror(errno));
410 return FALSE;
411 }
413 while (read_statement(in, lval, sizeof lval, rval, sizeof rval)) {
414 DEBUG(9) fprintf(stderr,"read_conf(): lval=%s\n", lval);
415 if (strcmp(lval, "debug_level")==0) {
416 conf.debug_level = atoi(rval);
417 } else if (strcmp(lval, "run_as_user")==0) {
418 if (!conf.run_as_user) {
419 /* you should not be able to reset that flag */
420 conf.run_as_user = parse_boolean(rval);
421 }
422 } else if (strcmp(lval, "use_syslog")==0) {
423 conf.use_syslog = parse_boolean(rval);
424 } else if (strcmp(lval, "mail_dir")==0) {
425 conf.mail_dir = g_strdup(rval);
426 } else if (strcmp(lval, "lock_dir")==0) {
427 conf.lock_dir = g_strdup(rval);
428 } else if (strcmp(lval, "spool_dir")==0) {
429 conf.spool_dir = g_strdup(rval);
430 } else if (strcmp(lval, "log_dir")==0) {
431 conf.log_dir = g_strdup(rval);
432 } else if (strcmp(lval, "host_name")==0) {
433 if (rval[0] != '/') {
434 conf.host_name = g_strdup(rval);
435 } else {
436 char buf[256];
437 FILE *fptr = fopen(rval, "rt");
438 if (!fptr) {
439 logwrite(LOG_ALERT, "could not open "
440 "%s: %s\n", rval,
441 strerror(errno));
442 return FALSE;
443 }
444 fgets(buf, sizeof buf, fptr);
445 g_strstrip(buf);
446 conf.host_name = g_strdup(buf);
447 fclose(fptr);
448 }
449 } else if (strcmp(lval, "local_hosts")==0) {
450 conf.local_hosts = parse_list(rval, TRUE);
451 } else if (strcmp(lval, "local_addresses")==0) {
452 conf.local_addresses = parse_list(rval, TRUE);
453 } else if (strcmp(lval, "not_local_addresses")==0) {
454 conf.not_local_addresses = parse_list(rval, TRUE);
455 } else if (strcmp(lval, "do_save_envelope_to")==0) {
456 conf.do_save_envelope_to = parse_boolean(rval);
457 } else if (strcmp(lval, "defer_all")==0) {
458 conf.defer_all = parse_boolean(rval);
459 } else if (strcmp(lval, "do_relay")==0) {
460 conf.do_relay = parse_boolean(rval);
461 } else if (strcmp(lval, "alias_file")==0) {
462 conf.alias_file = g_strdup(rval);
463 } else if (strcmp(lval, "globalias_file")==0) {
464 conf.globalias_file = g_strdup(rval);
465 } else if (strcmp(lval, "caseless_matching")==0) {
466 conf.localpartcmp = parse_boolean(rval) ?
467 strcasecmp : strcmp;
468 } else if (strcmp(lval, "mbox_default")==0) {
469 conf.mbox_default = g_strdup(rval);
470 } else if (strcmp(lval, "mbox_users")==0) {
471 conf.mbox_users = parse_list(rval, TRUE);
472 } else if (strcmp(lval, "mda_users")==0) {
473 conf.mda_users = parse_list(rval, TRUE);
474 } else if (strcmp(lval, "mda")==0) {
475 conf.mda = g_strdup(rval);
476 } else if (strcmp(lval, "mda_fromline")==0) {
477 conf.mda_fromline = parse_boolean(rval);
478 } else if (strcmp(lval, "mda_fromhack")==0) {
479 conf.mda_fromhack = parse_boolean(rval);
480 } else if (strcmp(lval, "pipe_fromline")==0) {
481 conf.pipe_fromline = parse_boolean(rval);
482 } else if (strcmp(lval, "pipe_fromhack")==0) {
483 conf.pipe_fromhack = parse_boolean(rval);
484 } else if (strcmp(lval, "listen_addresses")==0) {
485 listen_addrs_tmp = parse_list(rval, TRUE);
486 } else if (strncmp(lval, "query_routes.", 13)==0) {
487 GList *file_list = parse_list(rval, FALSE);
488 table_pair *pair = create_pair(lval+13, file_list);
489 conf.query_routes = g_list_append(conf.query_routes,
490 pair);
491 } else if (strcmp(lval, "permanent_routes")==0) {
492 conf.perma_routes = parse_list(rval, FALSE);
493 } else if (strcmp(lval, "online_query")==0) {
494 conf.online_query = g_strdup(rval);
495 } else if (strcmp(lval, "do_queue")==0) {
496 conf.do_queue = parse_boolean(rval);
497 } else if (strcmp(lval, "errmsg_file")==0) {
498 conf.errmsg_file = g_strdup(rval);
499 } else if (strcmp(lval, "warnmsg_file")==0) {
500 conf.warnmsg_file = g_strdup(rval);
501 } else if (strcmp(lval, "warn_intervals")==0) {
502 conf.warn_intervals = parse_list(rval, TRUE);
503 } else if (strcmp(lval, "max_defer_time")==0) {
504 gint ival = time_interval(rval);
505 if (ival < 0) {
506 logwrite(LOG_WARNING, "invalid time interval "
507 "for 'max_defer_time': %s\n",
508 rval);
509 } else {
510 conf.max_defer_time = ival;
511 }
512 } else if (strcmp(lval, "log_user")==0) {
513 conf.log_user = g_strdup(rval);
514 } else if(strcmp(lval, "max_msg_size")==0) {
515 conf.max_msg_size = atol(rval);
516 DEBUG(9) fprintf(stderr,
517 "rval=%s, conf.max_msg_size=%ld\n",
518 rval, conf.max_msg_size);
519 } else {
520 logwrite(LOG_WARNING, "var '%s' unknown: ignored\n",
521 lval);
522 }
523 }
524 fclose(in);
526 if (!conf.host_name) {
527 logwrite(LOG_ALERT, "`host_name' MUST be set in "
528 "masqmail.conf. See man page\n");
529 return FALSE;
530 }
531 if (!conf.errmsg_file) {
532 conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl");
533 }
534 if (!conf.warnmsg_file) {
535 conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
536 }
537 if (!conf.lock_dir) {
538 conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
539 }
540 if (!conf.mbox_default) {
541 conf.mbox_default = g_strdup("mbox");
542 }
543 if (!conf.warn_intervals) {
544 conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", TRUE);
545 }
546 if (!conf.local_hosts) {
547 char *shortname = strdup(conf.host_name);
548 char *p = strchr(shortname, '.');
549 if (p) {
550 *p = '\0';
551 }
552 /* don't care if shortname and conf.host_name are the same */
553 char *local_hosts_str = g_strdup_printf("localhost;%s;%s",
554 shortname, conf.host_name);
555 conf.local_hosts = parse_list(local_hosts_str, TRUE);
556 free(shortname);
557 free(local_hosts_str);
558 }
559 if (!listen_addrs_tmp) {
560 conf.listen_addresses = g_list_append(NULL,
561 parse_interface("localhost", 25));
562 } else {
563 GList *node;
565 foreach(listen_addrs_tmp, node) {
566 conf.listen_addresses =
567 g_list_append(conf.listen_addresses,
568 parse_interface((gchar *) node->data,
569 25));
570 g_free(node->data);
571 }
572 g_list_free(listen_addrs_tmp);
573 }
575 return TRUE;
576 }
578 connect_route*
579 read_route(gchar *filename, gboolean is_perma)
580 {
581 FILE *in;
582 connect_route *route;
583 gchar lval[256], rval[2048];
585 DEBUG(5) debugf("read_route, filename = %s\n", filename);
587 if (!(in = fopen(filename, "r"))) {
588 logwrite(LOG_ALERT, "could not open route file %s: %s\n",
589 filename, strerror(errno));
590 return NULL;
591 }
593 route = g_malloc(sizeof(connect_route));
594 memset(route, 0, sizeof(connect_route));
595 route->filename = g_strdup(filename);
596 route->name = route->filename; /* quick hack */
597 route->expand_h_sender_address = TRUE;
598 route->is_perma = is_perma;
599 route->do_pipelining = TRUE;
601 while (read_statement(in, lval, sizeof lval, rval, sizeof rval)) {
602 if (strcmp(lval, "mail_host")==0) {
603 route->mail_host = parse_interface(rval, 25);
604 } else if (strcmp(lval, "helo_name")==0) {
605 route->helo_name = g_strdup(rval);
606 } else if (strcmp(lval, "wrapper")==0) {
607 route->wrapper = g_strdup(rval);
608 } else if (strcmp(lval, "connect_error_fail")==0) {
609 route->connect_error_fail = parse_boolean(rval);
610 } else if (strcmp(lval, "do_correct_helo")==0) {
611 route->do_correct_helo = parse_boolean(rval);
612 } else if (strcmp(lval, "instant_helo")==0) {
613 route->instant_helo = parse_boolean(rval);
614 } else if (strcmp(lval, "do_pipelining")==0) {
615 route->do_pipelining = parse_boolean(rval);
617 } else if (strcmp(lval, "allowed_senders")==0) {
618 route->allowed_senders = parse_address_glob_list(rval);
619 } else if (strcmp(lval, "denied_senders")==0) {
620 route->denied_senders = parse_address_glob_list(rval);
621 } else if (strcmp(lval, "allowed_recipients")==0) {
622 route->allowed_recipients = parse_address_glob_list(rval);
623 } else if (strcmp(lval, "denied_recipients")==0) {
624 route->denied_recipients = parse_address_glob_list(rval);
626 } else if (strcmp(lval, "set_h_from_domain")==0) {
627 route->set_h_from_domain = g_strdup(rval);
628 } else if (strcmp(lval, "set_h_reply_to_domain")==0) {
629 route->set_h_reply_to_domain = g_strdup(rval);
630 } else if (strcmp(lval, "set_return_path_domain")==0) {
631 route->set_return_path_domain = g_strdup(rval);
632 } else if (strcmp(lval, "map_return_path_addresses")==0) {
633 GList *node, *list;
635 list = parse_list(rval, TRUE);
636 foreach(list, node) {
637 gchar *item = (gchar *) (node->data);
638 table_pair *pair = parse_table_pair(item, ':');
639 address *addr = create_address(
640 (gchar *) (pair->value), TRUE);
641 g_free(pair->value);
642 pair->value = (gpointer *) addr;
643 route->map_return_path_addresses = g_list_append( route->map_return_path_addresses, pair);
644 g_free(item);
645 }
646 g_list_free(list);
647 } else if (strcmp(lval, "map_h_from_addresses")==0) {
648 GList *list, *node;
650 list = parse_list(rval, TRUE);
651 foreach(list, node) {
652 gchar *item = (gchar *) (node->data);
653 table_pair *pair = parse_table_pair(item, ':');
654 route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair);
655 g_free(item);
656 }
657 g_list_free(list);
658 } else if (strcmp(lval, "map_h_reply_to_addresses")==0) {
659 GList *list, *node;
661 list = parse_list(rval, TRUE);
662 foreach(list, node) {
663 gchar *item = (gchar *) (node->data);
664 table_pair *pair = parse_table_pair(item, ':');
665 route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair);
666 g_free(item);
667 }
668 g_list_free(list);
669 } else if (strcmp(lval, "map_h_mail_followup_to_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_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair);
677 g_free(item);
678 }
679 g_list_free(list);
680 } else if (strcmp(lval, "expand_h_sender_domain")==0) {
681 route->expand_h_sender_domain = parse_boolean(rval);
682 } else if (strcmp(lval, "expand_h_sender_address")==0) {
683 route->expand_h_sender_address = parse_boolean(rval);
684 } else if (strcmp(lval, "resolve_list")==0) {
685 route->resolve_list = parse_resolve_list(rval);
686 } else if (strcmp(lval, "do_ssl")==0) {
687 /* we ignore this. This option is used by sqilconf */
688 ;
689 #ifdef ENABLE_AUTH
690 } else if (strcmp(lval, "auth_name")==0) {
691 route->auth_name = g_strdup(rval);
692 } else if (strcmp(lval, "auth_login")==0) {
693 route->auth_login = g_strdup(rval);
694 } else if (strcmp(lval, "auth_secret")==0) {
695 route->auth_secret = g_strdup(rval);
696 #else
697 } else if ((strcmp(lval, "auth_name")==0) ||
698 (strcmp(lval, "auth_login")==0) ||
699 (strcmp(lval, "auth_secret")==0)) {
700 logwrite(LOG_WARNING, "%s ignored: not compiled with "
701 "auth support.\n", lval);
702 }
703 #endif
704 } else if (strcmp(lval, "pipe")==0) {
705 route->pipe = g_strdup(rval);
706 } else if (strcmp(lval, "pipe_fromline")==0) {
707 route->pipe_fromline = parse_boolean(rval);
708 } else if (strcmp(lval, "pipe_fromhack")==0) {
709 route->pipe_fromhack = parse_boolean(rval);
710 } else if (strcmp(lval, "last_route")==0) {
711 route->last_route = parse_boolean(rval);
712 } else {
713 logwrite(LOG_WARNING, "var '%s' unknown: ignored\n",
714 lval);
715 }
716 }
718 if (!route->resolve_list) {
719 #ifdef ENABLE_RESOLVER
720 route->resolve_list = g_list_append(route->resolve_list,
721 resolve_dns_mx);
722 route->resolve_list = g_list_append(route->resolve_list,
723 resolve_dns_a);
724 #endif
725 route->resolve_list = g_list_append(route->resolve_list,
726 resolve_byname);
727 }
728 fclose(in);
730 /* warn user about mis-configurations: */
731 if (route->map_h_from_addresses && route->set_h_from_domain) {
732 logwrite(LOG_WARNING, "'map_h_from_addresses' overrides "
733 "'set_h_from_domain'\n");
734 g_free(route->set_h_from_domain);
735 route->set_h_from_domain = NULL;
736 }
737 if (route->map_h_reply_to_addresses && route->set_h_reply_to_domain) {
738 logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides "
739 "'set_h_reply_to_domain'\n");
740 g_free(route->set_h_reply_to_domain);
741 route->set_h_reply_to_domain = NULL;
742 }
744 return route;
745 }
747 static void
748 _g_list_free_all(GList *list)
749 {
750 GList *node;
751 if (!list) {
752 return;
753 }
754 foreach(list, node) {
755 g_free(node->data);
756 }
757 g_list_free(list);
758 }
760 void
761 destroy_route(connect_route *r)
762 {
763 if (r->filename) {
764 g_free(r->filename);
765 }
766 if (r->mail_host) {
767 g_free(r->mail_host->address);
768 g_free(r->mail_host);
769 }
770 if (r->wrapper) {
771 g_free(r->wrapper);
772 }
773 if (r->helo_name) {
774 g_free(r->helo_name);
775 }
776 _g_list_free_all(r->allowed_senders);
777 _g_list_free_all(r->denied_senders);
778 _g_list_free_all(r->allowed_recipients);
779 _g_list_free_all(r->denied_recipients);
780 if (r->set_h_from_domain) {
781 g_free(r->set_h_from_domain);
782 }
783 if (r->set_h_reply_to_domain) {
784 g_free(r->set_h_reply_to_domain);
785 }
786 if (r->set_return_path_domain) {
787 g_free(r->set_return_path_domain);
788 }
789 if (r->map_h_reply_to_addresses) {
790 destroy_table(r->map_h_reply_to_addresses);
791 }
792 if (r->resolve_list) {
793 g_list_free(r->resolve_list);
794 }
795 #ifdef ENABLE_AUTH
796 if (r->auth_name) {
797 g_free(r->auth_name);
798 }
799 if (r->auth_login) {
800 g_free(r->auth_login);
801 }
802 if (r->auth_secret) {
803 g_free(r->auth_secret);
804 }
805 #endif
806 if (r->pipe) {
807 g_free(r->pipe);
808 }
809 g_free(r);
810 }
812 GList*
813 read_route_list(GList *rf_list, gboolean is_perma)
814 {
815 GList *list = NULL;
816 GList *node;
817 uid_t saved_uid, saved_gid;
819 if (!conf.run_as_user) {
820 set_euidgid(0, 0, &saved_uid, &saved_gid);
821 }
822 foreach(rf_list, node) {
823 gchar *fname = (gchar *) (node->data);
824 connect_route *route = read_route(fname, is_perma);
825 if (route) {
826 list = g_list_append(list, route);
827 } else {
828 logwrite(LOG_ALERT, "could not read route "
829 "configuration %s\n", fname);
830 }
831 }
832 /* set uid and gid back */
833 if (!conf.run_as_user) {
834 set_euidgid(saved_uid, saved_gid, NULL, NULL);
835 }
836 return list;
837 }
839 void
840 destroy_route_list(GList *list)
841 {
842 GList *node;
844 foreach(list, node) {
845 connect_route *route = (connect_route *) (node->data);
846 destroy_route(route);
847 }
848 g_list_free(list);
849 }