GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/smtpd/smtpd/../pony.c Lines: 0 61 0.0 %
Date: 2017-11-07 Branches: 0 61 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: pony.c,v 1.18 2017/08/13 11:10:30 eric Exp $	*/
2
3
/*
4
 * Copyright (c) 2014 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
24
#include <ctype.h>
25
#include <err.h>
26
#include <errno.h>
27
#include <event.h>
28
#include <imsg.h>
29
#include <inttypes.h>
30
#include <pwd.h>
31
#include <signal.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <time.h>
36
#include <unistd.h>
37
#include <limits.h>
38
#include <grp.h>
39
#include <vis.h>
40
41
#include "smtpd.h"
42
#include "log.h"
43
44
void mda_imsg(struct mproc *, struct imsg *);
45
void mta_imsg(struct mproc *, struct imsg *);
46
void smtp_imsg(struct mproc *, struct imsg *);
47
48
static void pony_shutdown(void);
49
50
void
51
pony_imsg(struct mproc *p, struct imsg *imsg)
52
{
53
	struct msg	m;
54
	int		v;
55
56
	if (imsg == NULL)
57
		pony_shutdown();
58
59
	switch (imsg->hdr.type) {
60
	case IMSG_CONF_START:
61
		return;
62
	case IMSG_CONF_END:
63
		smtp_configure();
64
		return;
65
	case IMSG_CTL_VERBOSE:
66
		m_msg(&m, imsg);
67
		m_get_int(&m, &v);
68
		m_end(&m);
69
		log_trace_verbose(v);
70
		return;
71
	case IMSG_CTL_PROFILE:
72
		m_msg(&m, imsg);
73
		m_get_int(&m, &v);
74
		m_end(&m);
75
		profiling = v;
76
		return;
77
78
	/* smtp imsg */
79
	case IMSG_SMTP_DNS_PTR:
80
	case IMSG_SMTP_CHECK_SENDER:
81
	case IMSG_SMTP_EXPAND_RCPT:
82
	case IMSG_SMTP_LOOKUP_HELO:
83
	case IMSG_SMTP_AUTHENTICATE:
84
	case IMSG_SMTP_TLS_INIT:
85
	case IMSG_SMTP_TLS_VERIFY:
86
	case IMSG_SMTP_MESSAGE_COMMIT:
87
	case IMSG_SMTP_MESSAGE_CREATE:
88
	case IMSG_SMTP_MESSAGE_OPEN:
89
	case IMSG_QUEUE_ENVELOPE_SUBMIT:
90
	case IMSG_QUEUE_ENVELOPE_COMMIT:
91
	case IMSG_QUEUE_SMTP_SESSION:
92
	case IMSG_CTL_SMTP_SESSION:
93
	case IMSG_CTL_PAUSE_SMTP:
94
	case IMSG_CTL_RESUME_SMTP:
95
		smtp_imsg(p, imsg);
96
		return;
97
98
        /* mta imsg */
99
	case IMSG_QUEUE_TRANSFER:
100
	case IMSG_MTA_OPEN_MESSAGE:
101
	case IMSG_MTA_LOOKUP_CREDENTIALS:
102
	case IMSG_MTA_LOOKUP_SOURCE:
103
	case IMSG_MTA_LOOKUP_HELO:
104
	case IMSG_MTA_DNS_HOST:
105
	case IMSG_MTA_DNS_HOST_END:
106
	case IMSG_MTA_DNS_MX_PREFERENCE:
107
	case IMSG_MTA_DNS_PTR:
108
	case IMSG_MTA_TLS_INIT:
109
	case IMSG_MTA_TLS_VERIFY:
110
	case IMSG_CTL_RESUME_ROUTE:
111
	case IMSG_CTL_MTA_SHOW_HOSTS:
112
	case IMSG_CTL_MTA_SHOW_RELAYS:
113
	case IMSG_CTL_MTA_SHOW_ROUTES:
114
	case IMSG_CTL_MTA_SHOW_HOSTSTATS:
115
	case IMSG_CTL_MTA_BLOCK:
116
	case IMSG_CTL_MTA_UNBLOCK:
117
	case IMSG_CTL_MTA_SHOW_BLOCK:
118
		mta_imsg(p, imsg);
119
		return;
120
121
        /* mda imsg */
122
	case IMSG_MDA_LOOKUP_USERINFO:
123
	case IMSG_QUEUE_DELIVER:
124
	case IMSG_MDA_OPEN_MESSAGE:
125
	case IMSG_MDA_FORK:
126
	case IMSG_MDA_DONE:
127
		mda_imsg(p, imsg);
128
		return;
129
	default:
130
		break;
131
	}
132
133
	errx(1, "session_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type));
134
}
135
136
static void
137
pony_shutdown(void)
138
{
139
	log_debug("debug: pony agent exiting");
140
	_exit(0);
141
}
142
143
int
144
pony(void)
145
{
146
	struct passwd	*pw;
147
148
	mda_postfork();
149
	mta_postfork();
150
	smtp_postfork();
151
152
	/* do not purge listeners and pki, they are purged
153
	 * in smtp_configure()
154
	 */
155
	purge_config(PURGE_TABLES|PURGE_RULES);
156
157
	if ((pw = getpwnam(SMTPD_USER)) == NULL)
158
		fatalx("unknown user " SMTPD_USER);
159
160
	if (chroot(PATH_CHROOT) == -1)
161
		fatal("pony: chroot");
162
	if (chdir("/") == -1)
163
		fatal("pony: chdir(\"/\")");
164
165
	config_process(PROC_PONY);
166
167
	if (setgroups(1, &pw->pw_gid) ||
168
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
169
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
170
		fatal("pony: cannot drop privileges");
171
172
	imsg_callback = pony_imsg;
173
	event_init();
174
175
	mda_postprivdrop();
176
	mta_postprivdrop();
177
	smtp_postprivdrop();
178
179
	signal(SIGINT, SIG_IGN);
180
	signal(SIGTERM, SIG_IGN);
181
	signal(SIGPIPE, SIG_IGN);
182
	signal(SIGHUP, SIG_IGN);
183
184
	config_peer(PROC_PARENT);
185
	config_peer(PROC_QUEUE);
186
	config_peer(PROC_LKA);
187
	config_peer(PROC_CONTROL);
188
	config_peer(PROC_CA);
189
190
	ca_engine_init();
191
192
	if (pledge("stdio inet unix recvfd sendfd flock rpath cpath wpath", NULL) == -1)
193
		err(1, "pledge");
194
195
	event_dispatch();
196
	fatalx("exited event loop");
197
198
	return (0);
199
}