masqmail

annotate src/conf.c @ 373:4cab237ce923

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