summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/forward.c
blob: 151e8a3d97744f7de6e92a3873db0cb866f54495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*	$OpenBSD: forward.c,v 1.35 2013/05/24 17:03:14 eric Exp $	*/

/*
 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <ctype.h>
#include <event.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include <unistd.h>

#include "smtpd.h"
#include "log.h"

#define	MAX_FORWARD_SIZE	(4 * 1024)
#define	MAX_EXPAND_NODES	(100)

int
forwards_get(int fd, struct expand *expand)
{
	FILE	       *fp = NULL;
	char	       *line = NULL;
	size_t		len;
	size_t		lineno;
	size_t		save;
	int		ret;
	struct stat	sb;

	ret = -1;
	if (fstat(fd, &sb) == -1)
		goto end;

	/* if it's empty just pretend that no expansion took place */
	if (sb.st_size == 0) {
		log_info("info: forward file is empty");
		ret = 0;
		goto end;
	}

	/* over MAX_FORWARD_SIZE, temporarily fail */
	if (sb.st_size >= MAX_FORWARD_SIZE) {
		log_info("info: forward file exceeds max size");
		goto end;
	}

	if ((fp = fdopen(fd, "r")) == NULL) {
		log_warn("warn: fdopen failure in forwards_get()");
		goto end;
	}

	lineno = 0;
	save = expand->nb_nodes;
	while ((line = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
		if (! expand_line(expand, line, 0)) {
			log_info("info: parse error in forward file");
			goto end;
		}
		if (expand->nb_nodes > MAX_EXPAND_NODES) {
			log_info("info: forward file expanded too many nodes");
			goto end;
		}
		free(line);
	}
	       
	ret = expand->nb_nodes > save ? 1 : 0;

end:
	if (line)
		free(line);
	if (fp)
		fclose(fp);
	else
		close(fd);
	return ret;
}