1 /* $OpenBSD: forward.c,v 1.40 2021/06/14 17:58:15 eric Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/stat.h> 20 21 #include <stdlib.h> 22 #include <util.h> 23 #include <unistd.h> 24 25 #include "smtpd.h" 26 #include "log.h" 27 28 #define MAX_FORWARD_SIZE (4 * 1024) 29 #define MAX_EXPAND_NODES (100) 30 31 int 32 forwards_get(int fd, struct expand *expand) 33 { 34 FILE *fp = NULL; 35 char *line = NULL; 36 size_t len; 37 size_t lineno; 38 size_t save; 39 int ret; 40 struct stat sb; 41 42 ret = -1; 43 if (fstat(fd, &sb) == -1) 44 goto end; 45 46 /* if it's empty just pretend that no expansion took place */ 47 if (sb.st_size == 0) { 48 log_info("info: forward file is empty"); 49 ret = 0; 50 goto end; 51 } 52 53 /* over MAX_FORWARD_SIZE, temporarily fail */ 54 if (sb.st_size >= MAX_FORWARD_SIZE) { 55 log_info("info: forward file exceeds max size"); 56 goto end; 57 } 58 59 if ((fp = fdopen(fd, "r")) == NULL) { 60 log_warn("warn: fdopen failure in forwards_get()"); 61 goto end; 62 } 63 64 lineno = 0; 65 save = expand->nb_nodes; 66 while ((line = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 67 if (!expand_line(expand, line, 0)) { 68 log_info("info: parse error in forward file"); 69 goto end; 70 } 71 if (expand->nb_nodes > MAX_EXPAND_NODES) { 72 log_info("info: forward file expanded too many nodes"); 73 goto end; 74 } 75 free(line); 76 } 77 78 ret = expand->nb_nodes > save ? 1 : 0; 79 80 end: 81 free(line); 82 if (fp) 83 fclose(fp); 84 else 85 close(fd); 86 return ret; 87 } 88