meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@224: Copyright (C) 2010 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@13: #include meillo@13: #include meillo@13: meillo@0: #include "masqmail.h" meillo@0: meillo@0: masqmail_conf conf; meillo@0: meillo@10: void meillo@10: init_conf() meillo@0: { meillo@10: struct passwd *passwd; meillo@10: struct group *group; meillo@0: meillo@10: memset(&conf, 0, sizeof(masqmail_conf)); meillo@0: meillo@10: conf.orig_uid = getuid(); meillo@10: conf.orig_gid = getgid(); meillo@0: meillo@10: if ((passwd = getpwnam(DEF_MAIL_USER))) meillo@10: conf.mail_uid = passwd->pw_uid; meillo@10: else { meillo@10: fprintf(stderr, "user %s not found! (terminating)\n", DEF_MAIL_USER); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: if ((group = getgrnam(DEF_MAIL_GROUP))) meillo@10: conf.mail_gid = group->gr_gid; meillo@10: else { meillo@10: fprintf(stderr, "group %s not found! (terminating)\n", DEF_MAIL_GROUP); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@0: } meillo@0: meillo@10: static gchar* true_strings[] = { meillo@10: "yes", "on", "true", NULL meillo@0: }; meillo@0: meillo@10: static gchar *false_strings[] = { meillo@10: "no", "off", "false", NULL meillo@0: }; meillo@0: meillo@10: static gboolean meillo@10: parse_boolean(gchar * rval) meillo@0: { meillo@10: gchar **str; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "parse_boolean: %s\n", rval); meillo@0: meillo@10: str = true_strings; meillo@10: while (*str) { meillo@10: if (strncasecmp(*str, rval, strlen(*str)) == 0) meillo@10: return TRUE; meillo@10: str++; meillo@10: } meillo@0: meillo@10: str = false_strings; meillo@10: while (*str) { meillo@10: if (strncasecmp(*str, rval, strlen(*str)) == 0) meillo@10: return FALSE; meillo@10: str++; meillo@10: } meillo@0: meillo@10: fprintf(stderr, "cannot parse value '%s'\n", rval); meillo@10: exit(EXIT_FAILURE); meillo@0: } meillo@0: meillo@0: /* make a list from each line in a file */ meillo@10: static GList* meillo@10: parse_list_file(gchar * fname) meillo@0: { meillo@10: GList *list = NULL; meillo@10: FILE *fptr; meillo@0: meillo@28: if ((fptr = fopen(fname, "rt")) == NULL) { meillo@10: logwrite(LOG_ALERT, "could not open %s for reading: %s\n", fname, strerror(errno)); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@0: meillo@28: gchar buf[256]; meillo@28: meillo@28: while (!feof(fptr)) { meillo@28: fgets(buf, 255, fptr); meillo@28: if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) { meillo@28: g_strchomp(buf); meillo@114: DEBUG(6) fprintf(stderr,"parse_list_file: item = %s\n", buf); meillo@28: list = g_list_append(list, g_strdup(buf)); meillo@28: } meillo@28: } meillo@28: fclose(fptr); meillo@28: meillo@10: return list; meillo@0: } meillo@0: meillo@13: /* given a semicolon separated string, this function makes a GList out of it. */ meillo@10: GList* meillo@10: parse_list(gchar * line, gboolean read_file) meillo@0: { meillo@10: GList *list = NULL; meillo@10: gchar buf[256]; meillo@10: gchar *p, *q; meillo@0: meillo@114: DEBUG(6) fprintf(stderr, "parsing list %s, file?:%d\n", line, read_file); meillo@0: meillo@10: p = line; meillo@14: while (*p != '\0') { meillo@10: q = buf; meillo@0: meillo@10: while (*p && (*p != ';') && (q < buf + 255)) meillo@10: *(q++) = *(p++); meillo@14: *q = '\0'; meillo@0: meillo@10: if ((buf[0] == '/') && (read_file)) meillo@10: /* item is a filename, include its contents */ meillo@10: list = g_list_concat(list, parse_list_file(buf)); meillo@10: else meillo@10: /* just a normal item */ meillo@10: list = g_list_append(list, g_strdup(buf)); meillo@0: meillo@114: DEBUG(6) fprintf(stderr, "item = %s\n", buf); meillo@0: meillo@10: if (*p) meillo@10: p++; meillo@10: } meillo@10: return list; meillo@0: } meillo@0: meillo@10: static GList* meillo@10: parse_address_list(gchar * line, gboolean read_file) meillo@0: { meillo@10: GList *plain_list = parse_list(line, read_file); meillo@10: GList *node; meillo@10: GList *list = NULL; meillo@0: meillo@10: foreach(plain_list, node) { meillo@10: gchar *item = (gchar *) (node->data); meillo@10: address *addr = create_address(item, TRUE); meillo@10: if (addr) meillo@10: list = g_list_append(list, addr); meillo@10: g_free(item); meillo@10: } meillo@10: g_list_free(plain_list); meillo@0: meillo@10: return list; meillo@0: } meillo@0: meillo@10: static GList* meillo@10: parse_resolve_list(gchar * line) meillo@0: { meillo@10: GList *list; meillo@10: GList *list_node; meillo@10: GList *res_list = NULL; meillo@0: meillo@10: list = parse_list(line, FALSE); meillo@28: if (!list) { meillo@28: return NULL; meillo@28: } meillo@28: meillo@28: foreach(list, list_node) { meillo@28: gchar *item = (gchar *) (list_node->data); meillo@28: if (strcmp(item, "byname") == 0) { meillo@28: res_list = g_list_append(res_list, resolve_byname); meillo@0: #ifdef ENABLE_RESOLVER meillo@28: } else if (strcmp(item, "dns_a") == 0) { meillo@28: res_list = g_list_append(res_list, resolve_dns_a); meillo@28: } else if (strcmp(item, "dns_mx") == 0) { meillo@28: res_list = g_list_append(res_list, resolve_dns_mx); meillo@0: #endif meillo@28: } else { meillo@28: logwrite(LOG_ALERT, "unknown resolver %s\n", item); meillo@28: exit(EXIT_FAILURE); meillo@10: } meillo@28: g_free(item); meillo@10: } meillo@28: g_list_free(list); meillo@10: return res_list; meillo@0: } meillo@0: meillo@10: static interface* meillo@10: parse_interface(gchar * line, gint def_port) meillo@0: { meillo@10: gchar buf[256]; meillo@10: gchar *p, *q; meillo@10: interface *iface; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "parse_interface: %s\n", line); meillo@0: meillo@10: p = line; meillo@10: q = buf; meillo@14: while ((*p != '\0') && (*p != ':') && (q < buf + 255)) meillo@10: *(q++) = *(p++); meillo@14: *q = '\0'; meillo@0: meillo@10: iface = g_malloc(sizeof(interface)); meillo@10: iface->address = g_strdup(buf); meillo@0: meillo@10: if (*p) { meillo@10: p++; meillo@10: iface->port = atoi(p); meillo@10: } else meillo@10: iface->port = def_port; meillo@114: DEBUG(6) fprintf(stderr,"rval=%s, address:port=%s:%i\n",line, iface->address, iface->port); meillo@0: meillo@10: return iface; meillo@0: } meillo@0: meillo@10: #ifdef ENABLE_IDENT /* so far used for that only */ meillo@10: static struct in_addr* meillo@10: parse_network(gchar * line, gint def_port) meillo@0: { meillo@10: gchar buf[256]; meillo@10: gchar *p, *q; meillo@10: struct in_addr addr, mask_addr, net_addr, *p_net_addr; meillo@10: guint n; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "parse_network: %s\n", line); meillo@0: meillo@10: p = line; meillo@10: q = buf; meillo@14: while ((*p != '\0') && (*p != '/') && (q < buf + 255)) meillo@10: *(q++) = *(p++); meillo@14: *q = '\0'; meillo@0: meillo@28: if ((addr.s_addr = inet_addr(buf)) == INADDR_NONE) { meillo@10: fprintf(stderr, "'%s' is not a valid address (must be ip)\n", buf); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: meillo@28: if (*p) { meillo@28: guint i; meillo@28: p++; meillo@28: i = atoi(p); meillo@28: if ((i >= 0) && (i <= 32)) meillo@28: n = i ? ~((1 << (32 - i)) - 1) : 0; meillo@28: else { meillo@28: fprintf(stderr, "'%d' is not a valid net mask (must be >= 0 and <= 32)\n", i); meillo@28: exit(EXIT_FAILURE); meillo@28: } meillo@28: } else meillo@28: n = 0; meillo@28: meillo@28: mask_addr.s_addr = htonl(n); meillo@28: net_addr.s_addr = mask_addr.s_addr & addr.s_addr; meillo@28: meillo@10: p_net_addr = g_malloc(sizeof(struct in_addr)); meillo@10: p_net_addr->s_addr = net_addr.s_addr; meillo@10: return p_net_addr; meillo@0: } meillo@0: #endif meillo@0: meillo@10: static gboolean meillo@10: eat_comments(FILE * in) meillo@0: { meillo@10: gint c; meillo@0: meillo@10: for (c = fgetc(in); (c == '#' || isspace(c)) && c != EOF; meillo@10: c = fgetc(in)) { meillo@10: if (c == '#') { meillo@10: gint c; meillo@10: for (c = fgetc(in); (c != '\n') && (c != EOF); c = fgetc(in)); meillo@10: } meillo@10: } meillo@10: if (c == EOF) meillo@10: return FALSE; meillo@10: ungetc(c, in); meillo@10: return TRUE; meillo@0: } meillo@0: meillo@0: /* after parsing, eat trailing character until LF */ meillo@10: static gboolean meillo@10: eat_line_trailing(FILE * in) meillo@0: { meillo@10: gint c; meillo@0: meillo@10: for (c = fgetc(in); c != EOF && c != '\n'; c = fgetc(in)); meillo@10: if (c == EOF) meillo@10: return FALSE; meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static gboolean meillo@10: eat_spaces(FILE * in) meillo@0: { meillo@10: gint c; meillo@10: meillo@28: for (c = fgetc(in); c != EOF && isspace(c); c = fgetc(in)) { meillo@28: /* empty */ meillo@28: } meillo@10: if (c == EOF) meillo@10: return FALSE; meillo@10: ungetc(c, in); meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static gboolean meillo@10: read_lval(FILE * in, gchar * buf, gint size) meillo@0: { meillo@10: gint c; meillo@10: gchar *ptr = buf; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "read_lval()\n"); meillo@0: meillo@10: if (!eat_spaces(in)) meillo@10: return FALSE; meillo@0: meillo@10: c = fgetc(in); meillo@10: DEBUG(6) fprintf(stderr, "read_lval() 2\n"); meillo@10: while ((isalnum(c) || c == '_' || c == '-' || c == '.') meillo@10: && (ptr < buf + size - 1) meillo@10: && (c != EOF)) { meillo@10: *ptr = c; meillo@10: ptr++; meillo@10: c = fgetc(in); meillo@10: } meillo@14: *ptr = '\0'; meillo@10: ungetc(c, in); meillo@0: meillo@10: if (c == EOF) { meillo@10: fprintf(stderr, "unexpected EOF after %s\n", buf); meillo@10: return FALSE; meillo@10: } else if (ptr >= buf + size - 1) { meillo@10: fprintf(stderr, "lval too long\n"); meillo@10: } meillo@0: meillo@10: eat_spaces(in); meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "lval = %s\n", buf); meillo@10: meillo@14: return buf[0] != '\0'; meillo@0: } meillo@0: meillo@10: static gboolean meillo@10: read_rval(FILE * in, gchar * buf, gint size) meillo@0: { meillo@10: gint c; meillo@10: gchar *ptr = buf; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "read_rval()\n"); meillo@0: meillo@10: if (!eat_spaces(in)) meillo@10: return FALSE; meillo@10: meillo@10: c = fgetc(in); meillo@10: if (c != '\"') { meillo@115: while ((isalnum(c) || c == '_' || c == '-' || c == '.' meillo@115: || c == '/' || c == '@' || c == ';' || c == ':') meillo@10: && (ptr < buf + size - 1) meillo@10: && (c != EOF)) { meillo@10: *ptr = c; meillo@10: ptr++; meillo@10: c = fgetc(in); meillo@10: } meillo@14: *ptr = '\0'; meillo@10: ungetc(c, in); meillo@10: } else { meillo@10: gboolean escape = FALSE; meillo@10: c = fgetc(in); meillo@10: while (((c != '\"') || escape) && (ptr < buf + size - 1)) { meillo@13: if (c != '\n') { /* ignore line breaks */ meillo@10: if ((c == '\\') && (!escape)) { meillo@10: escape = TRUE; meillo@10: } else { meillo@10: *ptr = c; meillo@10: ptr++; meillo@10: escape = FALSE; meillo@10: } meillo@10: } meillo@10: c = fgetc(in); meillo@10: } meillo@14: *ptr = '\0'; meillo@0: } meillo@0: meillo@10: eat_line_trailing(in); meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "rval = %s\n", buf); meillo@10: meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static gboolean meillo@10: read_statement(FILE * in, gchar * lval, gint lsize, gchar * rval, gint rsize) meillo@0: { meillo@10: gint c; meillo@0: meillo@10: DEBUG(6) fprintf(stderr, "read_statement()\n"); meillo@0: meillo@10: /* eat comments and empty lines: */ meillo@10: if (!eat_comments(in)) meillo@10: return FALSE; meillo@0: meillo@28: if (!read_lval(in, lval, lsize)) { meillo@28: return FALSE; meillo@28: } meillo@28: meillo@114: DEBUG(6) fprintf(stderr, " lval = %s\n", lval); meillo@28: if ((c = fgetc(in) == '=')) { meillo@28: if (read_rval(in, rval, rsize)) { meillo@114: DEBUG(6) fprintf(stderr, " rval = %s\n", rval); meillo@28: return TRUE; meillo@10: } meillo@28: } else { meillo@114: DEBUG(6) fprintf(stderr," '=' expected after %s, char was '%c'\n", lval, c); meillo@28: fprintf(stderr, "'=' expected after %s, char was '%c'\n", lval, c); meillo@10: } meillo@10: return FALSE; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: read_conf(gchar * filename) meillo@0: { meillo@10: FILE *in; meillo@0: meillo@10: conf.log_max_pri = 7; meillo@10: conf.do_relay = TRUE; meillo@10: conf.alias_local_cmp = strcmp; meillo@10: conf.max_defer_time = 86400 * 4; /* 4 days */ meillo@120: conf.max_msg_size = 0; /* no limit on msg size */ meillo@151: conf.spool_dir = SPOOL_DIR; meillo@152: conf.mail_dir = "/var/mail"; meillo@206: /* we use 127.0.0.1 because `localhost' could be bound to some meillo@206: other IP address. This is unlikely but could be. Using meillo@206: 127.0.0.1 is more safe. See mailing list for details */ meillo@206: conf.listen_addresses = g_list_append(NULL, parse_interface("127.0.0.1", 25)); meillo@0: meillo@28: if ((in = fopen(filename, "r")) == NULL) { meillo@155: logwrite(LOG_ALERT, "could not open config file %s: %s\n", filename, strerror(errno)); meillo@28: return FALSE; meillo@28: } meillo@28: meillo@28: gchar lval[256], rval[2048]; meillo@28: while (read_statement(in, lval, 256, rval, 2048)) { meillo@114: DEBUG(6) fprintf(stderr,"read_conf(): lval=%s\n", lval); meillo@28: if (strcmp(lval, "debug_level") == 0) meillo@28: conf.debug_level = atoi(rval); meillo@28: else if (strcmp(lval, "run_as_user") == 0) { meillo@28: if (!conf.run_as_user) /* you should not be able to reset that flag */ meillo@28: conf.run_as_user = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "use_syslog") == 0) meillo@28: conf.use_syslog = parse_boolean(rval); meillo@28: else if (strcmp(lval, "mail_dir") == 0) meillo@28: conf.mail_dir = g_strdup(rval); meillo@28: else if (strcmp(lval, "lock_dir") == 0) meillo@28: conf.lock_dir = g_strdup(rval); meillo@28: else if (strcmp(lval, "spool_dir") == 0) meillo@28: conf.spool_dir = g_strdup(rval); meillo@28: else if (strcmp(lval, "log_dir") == 0) meillo@28: conf.log_dir = g_strdup(rval); meillo@28: else if (strcmp(lval, "host_name") == 0) { meillo@28: if (rval[0] != '/') meillo@28: conf.host_name = g_strdup(rval); meillo@28: else { meillo@28: char buf[256]; meillo@28: FILE *fptr = fopen(rval, "rt"); meillo@28: if (fptr) { meillo@155: logwrite(LOG_ALERT, "could not open %s: %s\n", rval, strerror(errno)); meillo@28: return FALSE; meillo@10: } meillo@28: fgets(buf, 255, fptr); meillo@28: g_strchomp(buf); meillo@28: conf.host_name = g_strdup(buf); meillo@28: fclose(fptr); meillo@28: } meillo@28: } else if (strcmp(lval, "local_hosts") == 0) meillo@28: conf.local_hosts = parse_list(rval, FALSE); meillo@28: else if (strcmp(lval, "local_addresses") == 0) meillo@28: conf.local_addresses = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "not_local_addresses") == 0) meillo@28: conf.not_local_addresses = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "local_nets") == 0) meillo@28: conf.local_nets = parse_list(rval, FALSE); meillo@28: else if (strcmp(lval, "do_save_envelope_to") == 0) meillo@28: conf.do_save_envelope_to = parse_boolean(rval); meillo@28: else if (strcmp(lval, "defer_all") == 0) meillo@28: conf.defer_all = parse_boolean(rval); meillo@28: else if (strcmp(lval, "do_relay") == 0) meillo@28: conf.do_relay = parse_boolean(rval); meillo@28: else if (strcmp(lval, "alias_file") == 0) { meillo@28: conf.alias_file = g_strdup(rval); meillo@28: } else if (strcmp(lval, "alias_local_caseless") == 0) { meillo@28: conf.alias_local_cmp = parse_boolean(rval) ? strcasecmp : strcmp; meillo@28: } else if (strcmp(lval, "mbox_default") == 0) { meillo@28: conf.mbox_default = g_strdup(rval); meillo@28: } else if (strcmp(lval, "mbox_users") == 0) { meillo@28: conf.mbox_users = parse_list(rval, TRUE); meillo@28: } else if (strcmp(lval, "mda_users") == 0) { meillo@28: conf.mda_users = parse_list(rval, TRUE); meillo@28: } else if (strcmp(lval, "mda") == 0) { meillo@28: conf.mda = g_strdup(rval); meillo@28: } else if (strcmp(lval, "mda_fromline") == 0) { meillo@28: conf.mda_fromline = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "mda_fromhack") == 0) { meillo@28: conf.mda_fromhack = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "pipe_fromline") == 0) { meillo@28: conf.pipe_fromline = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "pipe_fromhack") == 0) { meillo@28: conf.pipe_fromhack = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "listen_addresses") == 0) { meillo@28: GList *node; meillo@28: GList *tmp_list = parse_list(rval, FALSE); meillo@0: meillo@28: conf.listen_addresses = NULL; meillo@28: foreach(tmp_list, node) { meillo@28: conf.listen_addresses = g_list_append(conf.listen_addresses, parse_interface((gchar *) (node-> data), 25)); meillo@28: g_free(node->data); meillo@28: } meillo@28: g_list_free(tmp_list); meillo@28: } else if (strcmp(lval, "ident_trusted_nets") == 0) { meillo@10: #ifdef ENABLE_IDENT meillo@28: GList *node; meillo@28: GList *tmp_list = parse_list(rval, FALSE); meillo@0: meillo@28: conf.ident_trusted_nets = NULL; meillo@28: foreach(tmp_list, node) { meillo@28: conf.ident_trusted_nets = g_list_append(conf.ident_trusted_nets, parse_network((gchar *) (node->data), 25)); meillo@28: g_free(node->data); meillo@28: } meillo@28: g_list_free(tmp_list); meillo@10: #else meillo@155: logwrite(LOG_WARNING, "%s ignored: not compiled with ident support\n", lval); meillo@10: #endif meillo@28: } else if ((strncmp(lval, "connect_route.", 14) == 0) meillo@28: || (strncmp(lval, "online_routes.", 14) == 0)) { meillo@28: GList *file_list = parse_list(rval, FALSE); meillo@28: table_pair *pair = create_pair(&(lval[14]), file_list); meillo@28: conf.connect_routes = g_list_append(conf.connect_routes, pair); meillo@28: } else if (strcmp(lval, "local_net_route") == 0) { meillo@28: conf.local_net_routes = parse_list(rval, FALSE); meillo@28: } else if (strcmp(lval, "online_detect") == 0) meillo@28: conf.online_detect = g_strdup(rval); meillo@28: else if (strcmp(lval, "online_file") == 0) meillo@28: conf.online_file = g_strdup(rval); meillo@28: else if (strcmp(lval, "online_pipe") == 0) meillo@28: conf.online_pipe = g_strdup(rval); meillo@28: else if (strcmp(lval, "do_queue") == 0) meillo@28: conf.do_queue = parse_boolean(rval); meillo@192: else if (strcmp(lval, "errmsg_file") == 0) meillo@28: conf.errmsg_file = g_strdup(rval); meillo@28: else if (strcmp(lval, "warnmsg_file") == 0) meillo@28: conf.warnmsg_file = g_strdup(rval); meillo@28: else if (strcmp(lval, "warn_intervals") == 0) meillo@28: conf.warn_intervals = parse_list(rval, FALSE); meillo@28: else if (strcmp(lval, "max_defer_time") == 0) { meillo@28: gint dummy; meillo@28: gint ival = time_interval(rval, &dummy); meillo@28: if (ival < 0) meillo@155: logwrite(LOG_WARNING, "invalid time interval for 'max_defer_time': %s\n", rval); meillo@28: else meillo@28: conf.max_defer_time = ival; meillo@28: } else if (strcmp(lval, "log_user") == 0) meillo@28: conf.log_user = g_strdup(rval); meillo@117: else if(strcmp(lval, "max_msg_size") == 0) { meillo@117: conf.max_msg_size = atol(rval); meillo@117: DEBUG(6) fprintf(stderr,"rval=%s, conf.max_msg_size=%ld\n", meillo@117: rval, conf.max_msg_size); meillo@117: } meillo@28: else meillo@155: logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval); meillo@28: } meillo@28: fclose(in); meillo@0: meillo@156: if (!conf.host_name) { meillo@156: logwrite(LOG_ALERT, "`host_name' MUST be set in masqmail.conf. See man page\n"); meillo@156: return FALSE; meillo@156: } meillo@156: meillo@28: if (conf.errmsg_file == NULL) meillo@28: conf.errmsg_file = g_strdup(DATA_DIR "/tpl/failmsg.tpl"); meillo@28: if (conf.warnmsg_file == NULL) meillo@28: conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl"); meillo@0: meillo@28: if (conf.lock_dir == NULL) meillo@28: conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir); meillo@0: meillo@28: if (conf.mbox_default == NULL) meillo@28: conf.mbox_default = g_strdup("mbox"); meillo@0: meillo@28: if (conf.warn_intervals == NULL) meillo@28: conf.warn_intervals = parse_list("1h;4h;8h;1d;2d;3d", FALSE); meillo@0: meillo@157: if (!conf.local_hosts) { meillo@157: char* shortname = strdup(conf.host_name); meillo@157: char* p = strchr(shortname, '.'); meillo@157: if (p) { meillo@157: *p = '\0'; meillo@157: } meillo@157: /* we don't care if shortname and conf.host_name are the same */ meillo@157: char* local_hosts_str = g_strdup_printf("localhost;%s;%s", shortname, conf.host_name); meillo@157: conf.local_hosts = parse_list(local_hosts_str, FALSE); meillo@157: free(shortname); meillo@157: free(local_hosts_str); meillo@157: } meillo@157: meillo@157: meillo@28: return TRUE; meillo@0: } meillo@0: meillo@10: connect_route* meillo@10: read_route(gchar * filename, gboolean is_local_net) meillo@0: { meillo@10: gboolean ok = FALSE; meillo@10: FILE *in; meillo@0: meillo@10: connect_route *route = g_malloc(sizeof(connect_route)); meillo@10: memset(route, 0, sizeof(connect_route)); meillo@0: meillo@10: DEBUG(5) debugf("read_route, filename = %s\n", filename); meillo@0: meillo@10: route->filename = g_strdup(filename); meillo@10: route->name = g_strdup(filename); /* quick hack */ meillo@0: meillo@10: route->protocol = g_strdup("smtp"); meillo@10: route->expand_h_sender_address = TRUE; meillo@0: meillo@10: route->is_local_net = is_local_net; meillo@0: meillo@10: route->do_pipelining = TRUE; meillo@0: meillo@28: if ((in = fopen(route->filename, "r")) == NULL) { meillo@28: logwrite(LOG_ALERT, "could not open route file %s: %s\n", route->filename, strerror(errno)); meillo@28: g_free(route); meillo@28: return NULL; meillo@28: } meillo@0: meillo@28: gchar lval[256], rval[2048]; meillo@28: while (read_statement(in, lval, 256, rval, 2048)) { meillo@28: if (strcmp(lval, "protocol") == 0) meillo@28: route->protocol = g_strdup(rval); meillo@28: else if (strcmp(lval, "mail_host") == 0) meillo@178: route->mail_host = parse_interface(rval, 25); meillo@28: else if (strcmp(lval, "helo_name") == 0) meillo@28: route->helo_name = g_strdup(rval); meillo@28: else if (strcmp(lval, "wrapper") == 0) meillo@28: route->wrapper = g_strdup(rval); meillo@28: else if (strcmp(lval, "connect_error_fail") == 0) meillo@28: route->connect_error_fail = parse_boolean(rval); meillo@28: else if (strcmp(lval, "do_correct_helo") == 0) meillo@28: route->do_correct_helo = parse_boolean(rval); meillo@222: else if (strcmp(lval, "instant_helo") == 0) meillo@222: route->instant_helo = parse_boolean(rval); meillo@28: else if (strcmp(lval, "do_pipelining") == 0) meillo@28: route->do_pipelining = parse_boolean(rval); meillo@28: else if (strcmp(lval, "allowed_return_paths") == 0) meillo@28: route->allowed_return_paths = parse_address_list(rval, TRUE); meillo@28: else if (strcmp(lval, "allowed_mail_locals") == 0) meillo@28: route->allowed_mail_locals = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "not_allowed_return_paths") == 0) meillo@28: route->not_allowed_return_paths = parse_address_list(rval, TRUE); meillo@28: else if (strcmp(lval, "not_allowed_mail_locals") == 0) meillo@28: route->not_allowed_mail_locals = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "allowed_rcpt_domains") == 0) meillo@28: route->allowed_rcpt_domains = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "not_allowed_rcpt_domains") == 0) meillo@28: route->not_allowed_rcpt_domains = parse_list(rval, TRUE); meillo@28: else if (strcmp(lval, "set_h_from_domain") == 0) meillo@28: route->set_h_from_domain = g_strdup(rval); meillo@28: else if (strcmp(lval, "set_h_reply_to_domain") == 0) meillo@28: route->set_h_reply_to_domain = g_strdup(rval); meillo@28: else if (strcmp(lval, "set_return_path_domain") == 0) meillo@28: route->set_return_path_domain = g_strdup(rval); meillo@28: else if (strcmp(lval, "map_return_path_addresses") == 0) { meillo@28: GList *node, *list; meillo@10: meillo@28: list = parse_list(rval, TRUE); meillo@28: foreach(list, node) { meillo@28: gchar *item = (gchar *) (node->data); meillo@28: table_pair *pair = parse_table_pair(item, ':'); meillo@28: address *addr = create_address((gchar *) (pair->value), TRUE); meillo@28: g_free(pair->value); meillo@28: pair->value = (gpointer *) addr; meillo@28: route->map_return_path_addresses = g_list_append(route->map_return_path_addresses, pair); meillo@28: g_free(item); meillo@28: } meillo@28: g_list_free(list); meillo@28: } else if (strcmp(lval, "map_h_from_addresses") == 0) { meillo@28: GList *list, *node; meillo@10: meillo@28: list = parse_list(rval, TRUE); meillo@28: foreach(list, node) { meillo@28: gchar *item = (gchar *) (node->data); meillo@28: table_pair *pair = parse_table_pair(item, ':'); meillo@28: route->map_h_from_addresses = g_list_append(route->map_h_from_addresses, pair); meillo@28: g_free(item); meillo@28: } meillo@28: g_list_free(list); meillo@28: } else if (strcmp(lval, "map_h_reply_to_addresses") == 0) { meillo@28: GList *list, *node; meillo@10: meillo@28: list = parse_list(rval, TRUE); meillo@28: foreach(list, node) { meillo@28: gchar *item = (gchar *) (node->data); meillo@28: table_pair *pair = parse_table_pair(item, ':'); meillo@28: route->map_h_reply_to_addresses = g_list_append(route->map_h_reply_to_addresses, pair); meillo@28: g_free(item); meillo@10: } meillo@28: g_list_free(list); meillo@28: } else if (strcmp(lval, "map_h_mail_followup_to_addresses") == 0) { meillo@28: GList *list, *node; meillo@28: meillo@28: list = parse_list(rval, TRUE); meillo@28: foreach(list, node) { meillo@28: gchar *item = (gchar *) (node->data); meillo@28: table_pair *pair = parse_table_pair(item, ':'); meillo@28: route->map_h_mail_followup_to_addresses = g_list_append(route->map_h_mail_followup_to_addresses, pair); meillo@28: g_free(item); meillo@28: } meillo@28: g_list_free(list); meillo@28: } else if (strcmp(lval, "expand_h_sender_domain") == 0) { meillo@28: route->expand_h_sender_domain = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "expand_h_sender_address") == 0) { meillo@28: route->expand_h_sender_address = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "resolve_list") == 0) meillo@28: route->resolve_list = parse_resolve_list(rval); meillo@28: else if (strcmp(lval, "do_ssl") == 0) { meillo@28: /* we ignore this. This option is used by sqilconf */ meillo@28: ; meillo@28: } meillo@10: #ifdef ENABLE_AUTH meillo@28: else if (strcmp(lval, "auth_name") == 0) { meillo@28: route->auth_name = g_strdup(rval); meillo@28: } else if (strcmp(lval, "auth_login") == 0) { meillo@28: route->auth_login = g_strdup(rval); meillo@28: } else if (strcmp(lval, "auth_secret") == 0) { meillo@28: route->auth_secret = g_strdup(rval); meillo@28: } meillo@10: #else meillo@28: else if ((strcmp(lval, "auth_name") == 0) meillo@28: || (strcmp(lval, "auth_login") == 0) meillo@28: || (strcmp(lval, "auth_secret") == 0)) { meillo@28: logwrite(LOG_WARNING, "%s ignored: not compiled with auth support.\n", lval); meillo@28: } meillo@10: #endif meillo@190: else if (strcmp(lval, "pipe") == 0) { meillo@28: route->pipe = g_strdup(rval); meillo@28: } else if (strcmp(lval, "pipe_fromline") == 0) { meillo@28: route->pipe_fromline = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "pipe_fromhack") == 0) { meillo@28: route->pipe_fromhack = parse_boolean(rval); meillo@28: } else if (strcmp(lval, "last_route") == 0) { meillo@28: route->last_route = parse_boolean(rval); meillo@28: } else meillo@28: logwrite(LOG_WARNING, "var '%s' not (yet) known, ignored\n", lval); meillo@28: } meillo@28: meillo@28: if (route->resolve_list == NULL) { meillo@28: if (is_local_net) { meillo@28: route->resolve_list = g_list_append(NULL, resolve_byname); meillo@28: } else { meillo@28: #ifdef ENABLE_RESOLVER meillo@28: route->resolve_list = g_list_append(route->resolve_list, resolve_dns_mx); meillo@28: route->resolve_list = g_list_append(route->resolve_list, resolve_dns_a); meillo@28: #endif meillo@28: route->resolve_list = g_list_append(route->resolve_list, resolve_byname); meillo@10: } meillo@28: } meillo@28: fclose(in); meillo@28: ok = TRUE; meillo@10: meillo@28: /* warn user about misconfigurations: */ meillo@28: if ((route->map_h_from_addresses != NULL) && (route->set_h_from_domain != NULL)) { meillo@28: logwrite(LOG_WARNING, "'map_h_from_addresses' overrides 'set_h_from_domain'\n"); meillo@28: g_free(route->set_h_from_domain); meillo@28: route->set_h_from_domain = NULL; meillo@28: } meillo@28: if ((route->map_h_reply_to_addresses != NULL) && (route->set_h_reply_to_domain != NULL)) { meillo@28: logwrite(LOG_WARNING, "'map_h_reply_to_addresses' overrides 'set_h_reply_to_domain'\n"); meillo@28: g_free(route->set_h_reply_to_domain); meillo@28: route->set_h_reply_to_domain = NULL; meillo@10: } meillo@10: meillo@10: if (!ok) { meillo@10: g_free(route); meillo@10: route = NULL; meillo@10: } meillo@10: meillo@10: return route; meillo@10: } meillo@10: meillo@10: static void meillo@10: _g_list_free_all(GList * list) meillo@10: { meillo@10: GList *node; meillo@10: if (list) { meillo@10: foreach(list, node) meillo@10: g_free(node->data); meillo@10: g_list_free(list); meillo@10: } meillo@10: } meillo@10: meillo@10: void meillo@10: destroy_route(connect_route * r) meillo@10: { meillo@10: if (r->filename) meillo@10: g_free(r->filename); meillo@10: if (r->protocol) meillo@10: g_free(r->protocol); meillo@10: if (r->mail_host) { meillo@10: g_free(r->mail_host->address); meillo@10: g_free(r->mail_host); meillo@10: } meillo@10: if (r->wrapper) meillo@10: g_free(r->wrapper); meillo@10: if (r->helo_name) meillo@10: g_free(r->helo_name); meillo@10: _g_list_free_all(r->allowed_mail_locals); meillo@10: _g_list_free_all(r->not_allowed_mail_locals); meillo@10: _g_list_free_all(r->allowed_rcpt_domains); meillo@10: _g_list_free_all(r->not_allowed_rcpt_domains); meillo@10: if (r->set_h_from_domain) meillo@10: g_free(r->set_h_from_domain); meillo@10: if (r->set_h_reply_to_domain) meillo@10: g_free(r->set_h_reply_to_domain); meillo@10: if (r->set_return_path_domain) meillo@10: g_free(r->set_return_path_domain); meillo@10: if (r->map_h_reply_to_addresses) meillo@10: destroy_table(r->map_h_reply_to_addresses); meillo@10: if (r->resolve_list) meillo@10: g_list_free(r->resolve_list); meillo@10: #ifdef ENABLE_AUTH meillo@10: if (r->auth_name) meillo@10: g_free(r->auth_name); meillo@10: if (r->auth_login) meillo@10: g_free(r->auth_login); meillo@10: if (r->auth_secret) meillo@10: g_free(r->auth_secret); meillo@10: #endif meillo@10: if (r->pipe) meillo@10: g_free(r->pipe); meillo@10: g_free(r); meillo@10: } meillo@10: meillo@10: GList* meillo@10: read_route_list(GList * rf_list, gboolean is_local_net) meillo@10: { meillo@10: GList *list = NULL; meillo@10: GList *node; meillo@10: uid_t saved_uid, saved_gid; meillo@10: meillo@10: if (!conf.run_as_user) { meillo@10: set_euidgid(0, 0, &saved_uid, &saved_gid); meillo@10: } meillo@10: meillo@10: foreach(rf_list, node) { meillo@10: gchar *fname = (gchar *) (node->data); meillo@10: connect_route *route = read_route(fname, is_local_net); meillo@10: if (route) meillo@10: list = g_list_append(list, route); meillo@10: else meillo@10: logwrite(LOG_ALERT, "could not read route configuration %s\n", fname); meillo@10: } meillo@10: meillo@10: /* set uid and gid back */ meillo@10: if (!conf.run_as_user) { meillo@10: set_euidgid(saved_uid, saved_gid, NULL, NULL); meillo@10: } meillo@10: meillo@10: return list; meillo@10: } meillo@10: meillo@10: void meillo@10: destroy_route_list(GList * list) meillo@10: { meillo@10: GList *node; meillo@10: meillo@10: foreach(list, node) { meillo@10: connect_route *route = (connect_route *) (node->data); meillo@10: destroy_route(route); meillo@0: } meillo@0: g_list_free(list); meillo@0: } meillo@0: meillo@10: connect_route* meillo@10: create_local_route() meillo@0: { meillo@10: connect_route *route; meillo@0: meillo@10: route = g_malloc(sizeof(connect_route)); meillo@28: if (!route) { meillo@28: return NULL; meillo@10: } meillo@28: memset(route, 0, sizeof(connect_route)); meillo@28: route->protocol = g_strdup("smtp"); meillo@28: route->is_local_net = TRUE; meillo@234: route->name = g_strdup("default local_net_route"); meillo@28: route->expand_h_sender_address = TRUE; meillo@28: route->resolve_list = g_list_append(NULL, resolve_byname); meillo@28: route->connect_error_fail = TRUE; meillo@10: return route; meillo@0: }