1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2006-2007 Pierre Habouzit
44 #define DAEMON_NAME "postlicyd"
45 #define DEFAULT_PORT 10000
46 #define RUNAS_USER "nobody"
47 #define RUNAS_GROUP "nogroup"
53 SMTP_HELO = SMTP_EHLO,
62 /* \see http://www.postfix.org/SMTPD_POLICY_README.html */
63 typedef struct query_t {
67 const char *helo_name;
70 const char *recipient;
71 const char *recipient_count;
72 const char *client_address;
73 const char *client_name;
74 const char *reverse_client_name;
78 const char *sasl_method;
79 const char *sasl_username;
80 const char *sasl_sender;
82 const char *ccert_subject;
83 const char *ccert_issuer;
84 const char *ccsert_fingerprint;
87 const char *encryption_protocol;
88 const char *encryption_cipher;
89 const char *encryption_keysize;
90 const char *etrn_domain;
93 typedef struct plicyd_t {
94 unsigned listener : 1;
102 static plicyd_t *plicyd_new(void)
104 plicyd_t *plicyd = p_new(plicyd_t, 1);
110 static void plicyd_delete(plicyd_t **plicyd)
113 if ((*plicyd)->fd >= 0)
114 close((*plicyd)->fd);
115 buffer_wipe(&(*plicyd)->ibuf);
116 buffer_wipe(&(*plicyd)->obuf);
122 static int postfix_parsejob(query_t *query, char *p)
124 #define PARSE_CHECK(expr, error, ...) \
127 syslog(LOG_ERR, error, ##__VA_ARGS__); \
139 p = strchr(k = p, '=');
140 PARSE_CHECK(p, "could not find '=' in line");
141 for (klen = p - k; klen && isblank(k[klen]); klen--);
146 p = strchr(v = p, '\n');
147 PARSE_CHECK(p, "could not find final \\n in line");
148 for (vlen = p - v; vlen && isblank(v[vlen]); vlen--);
149 p += 1; /* skip \n */
151 vtk = tokenize(v, vlen);
152 switch (tokenize(k, klen)) {
153 #define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; break;
154 CASE(HELO_NAME, helo_name);
155 CASE(QUEUE_ID, queue_id);
156 CASE(SENDER, sender);
157 CASE(RECIPIENT, recipient);
158 CASE(RECIPIENT_COUNT, recipient_count);
159 CASE(CLIENT_ADDRESS, client_address);
160 CASE(CLIENT_NAME, client_name);
161 CASE(REVERSE_CLIENT_NAME, reverse_client_name);
162 CASE(INSTANCE, instance);
163 CASE(SASL_METHOD, sasl_method);
164 CASE(SASL_USERNAME, sasl_username);
165 CASE(SASL_SENDER, sasl_sender);
167 CASE(CCERT_SUBJECT, ccert_subject);
168 CASE(CCERT_ISSUER, ccert_issuer);
169 CASE(CCSERT_FINGERPRINT, ccsert_fingerprint);
170 CASE(ENCRYPTION_PROTOCOL, encryption_protocol);
171 CASE(ENCRYPTION_CIPHER, encryption_cipher);
172 CASE(ENCRYPTION_KEYSIZE, encryption_keysize);
173 CASE(ETRN_DOMAIN, etrn_domain);
177 PARSE_CHECK(vtk == PTK_SMTPD_ACCESS_POLICY,
178 "unexpected `request' value: %.*s", vlen, v);
181 case PTK_PROTOCOL_NAME:
182 PARSE_CHECK(vtk == PTK_SMTP || vtk == PTK_ESMTP,
183 "unexpected `protocol_name' value: %.*s", vlen, v);
184 query->esmtp = vtk == PTK_ESMTP;
187 case PTK_PROTOCOL_STATE:
189 #define CASE(name) case PTK_##name: query->state = SMTP_##name; break;
196 CASE(END_OF_MESSAGE);
200 PARSE_CHECK(false, "unexpected `protocol_state` value: %.*s",
207 syslog(LOG_WARNING, "unexpected key, skipped: %.*s", klen, k);
212 return query->state == SMTP_UNKNOWN ? -1 : 0;
216 static void *policy_run(int fd, void *data)
222 ssize_t search_offs = MAX(0, buf.len - 1);
223 int nb = buffer_read(&buf, fd, -1);
228 if (errno == EAGAIN || errno == EINTR)
235 syslog(LOG_ERR, "unexpected end of data");
239 eoq = strstr(buf.data + search_offs, "\n\n");
243 if (postfix_parsejob(&q, buf.data) < 0)
246 buffer_consume(&buf, eoq + strlen("\n\n") - buf.data);
247 if (xwrite(fd, "DUNNO\n\n", strlen("DUNNO\n\n"))) {
258 int start_listener(int port)
260 struct sockaddr_in addr = {
261 .sin_family = AF_INET,
262 .sin_addr = { htonl(INADDR_LOOPBACK) },
264 struct epoll_event evt = { .events = EPOLLIN };
268 addr.sin_port = htons(port);
269 sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
274 evt.data.ptr = tmp = plicyd_new();
276 tmp->listener = true;
277 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &evt) < 0) {
278 UNIXERR("epoll_ctl");
283 /* administrivia {{{ */
285 static int main_initialize(void)
287 openlog("postlicyd", LOG_PID, LOG_MAIL);
288 signal(SIGPIPE, SIG_IGN);
289 signal(SIGINT, &common_sighandler);
290 signal(SIGTERM, &common_sighandler);
291 signal(SIGHUP, &common_sighandler);
292 signal(SIGSEGV, &common_sighandler);
293 syslog(LOG_INFO, "Starting...");
297 static void main_shutdown(void)
302 module_init(main_initialize);
303 module_exit(main_shutdown);
307 fputs("usage: "DAEMON_NAME" [options] config\n"
310 " -l <port> port to listen to\n"
311 " -p <pidfile> file to write our pid to\n"
312 " -f stay in foreground\n"
318 int main(int argc, char *argv[])
320 const char *pidfile = NULL;
321 bool daemonize = true;
322 int port = DEFAULT_PORT;
324 for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) {
341 if (argc - optind != 1) {
346 if (pidfile_open(pidfile) < 0) {
347 syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
351 if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
352 syslog(LOG_CRIT, "unable to drop privileges");
356 if (daemonize && daemon_detach() < 0) {
357 syslog(LOG_CRIT, "unable to fork");
363 if (start_listener(port) < 0)
367 struct epoll_event evts[1024];
370 n = epoll_wait(epollfd, evts, countof(evts), -1);
372 if (errno != EAGAIN && errno != EINTR) {
373 UNIXERR("epoll_wait");
380 plicyd_t *d = evts[n].data.ptr;
383 int fd = accept(d->fd, NULL, 0);
385 if (errno != EINTR && errno != EAGAIN) {
391 thread_launch(policy_run, fd, NULL);
397 syslog(LOG_INFO, "Stopping...");