1 |
|
|
/* $OpenBSD: forward.c,v 1.39 2015/12/28 22:08:30 jung 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/types.h> |
20 |
|
|
#include <sys/queue.h> |
21 |
|
|
#include <sys/tree.h> |
22 |
|
|
#include <sys/socket.h> |
23 |
|
|
#include <sys/stat.h> |
24 |
|
|
|
25 |
|
|
#include <ctype.h> |
26 |
|
|
#include <event.h> |
27 |
|
|
#include <imsg.h> |
28 |
|
|
#include <stdio.h> |
29 |
|
|
#include <stdlib.h> |
30 |
|
|
#include <string.h> |
31 |
|
|
#include <util.h> |
32 |
|
|
#include <unistd.h> |
33 |
|
|
#include <limits.h> |
34 |
|
|
|
35 |
|
|
#include "smtpd.h" |
36 |
|
|
#include "log.h" |
37 |
|
|
|
38 |
|
|
#define MAX_FORWARD_SIZE (4 * 1024) |
39 |
|
|
#define MAX_EXPAND_NODES (100) |
40 |
|
|
|
41 |
|
|
int |
42 |
|
|
forwards_get(int fd, struct expand *expand) |
43 |
|
|
{ |
44 |
|
|
FILE *fp = NULL; |
45 |
|
|
char *line = NULL; |
46 |
|
|
size_t len; |
47 |
|
|
size_t lineno; |
48 |
|
|
size_t save; |
49 |
|
|
int ret; |
50 |
|
|
struct stat sb; |
51 |
|
|
|
52 |
|
|
ret = -1; |
53 |
|
|
if (fstat(fd, &sb) == -1) |
54 |
|
|
goto end; |
55 |
|
|
|
56 |
|
|
/* if it's empty just pretend that no expansion took place */ |
57 |
|
|
if (sb.st_size == 0) { |
58 |
|
|
log_info("info: forward file is empty"); |
59 |
|
|
ret = 0; |
60 |
|
|
goto end; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/* over MAX_FORWARD_SIZE, temporarily fail */ |
64 |
|
|
if (sb.st_size >= MAX_FORWARD_SIZE) { |
65 |
|
|
log_info("info: forward file exceeds max size"); |
66 |
|
|
goto end; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
if ((fp = fdopen(fd, "r")) == NULL) { |
70 |
|
|
log_warn("warn: fdopen failure in forwards_get()"); |
71 |
|
|
goto end; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
lineno = 0; |
75 |
|
|
save = expand->nb_nodes; |
76 |
|
|
while ((line = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { |
77 |
|
|
if (!expand_line(expand, line, 0)) { |
78 |
|
|
log_info("info: parse error in forward file"); |
79 |
|
|
goto end; |
80 |
|
|
} |
81 |
|
|
if (expand->nb_nodes > MAX_EXPAND_NODES) { |
82 |
|
|
log_info("info: forward file expanded too many nodes"); |
83 |
|
|
goto end; |
84 |
|
|
} |
85 |
|
|
free(line); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
ret = expand->nb_nodes > save ? 1 : 0; |
89 |
|
|
|
90 |
|
|
end: |
91 |
|
|
free(line); |
92 |
|
|
if (fp) |
93 |
|
|
fclose(fp); |
94 |
|
|
else |
95 |
|
|
close(fd); |
96 |
|
|
return ret; |
97 |
|
|
} |