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
34 * Copyright © 2008 Florent Bruneau
45 #define DAEMON_NAME "postlicyd"
46 #define DEFAULT_PORT 10000
47 #define RUNAS_USER "nobody"
48 #define RUNAS_GROUP "nogroup"
54 SMTP_HELO = SMTP_EHLO,
63 /* \see http://www.postfix.org/SMTPD_POLICY_README.html */
64 typedef struct query_t {
68 const char *helo_name;
71 const char *recipient;
72 const char *recipient_count;
73 const char *client_address;
74 const char *client_name;
75 const char *reverse_client_name;
79 const char *sasl_method;
80 const char *sasl_username;
81 const char *sasl_sender;
83 const char *ccert_subject;
84 const char *ccert_issuer;
85 const char *ccsert_fingerprint;
88 const char *encryption_protocol;
89 const char *encryption_cipher;
90 const char *encryption_keysize;
91 const char *etrn_domain;
96 static query_t *query_new(void)
98 return p_new(query_t, 1);
101 static void query_delete(query_t **query)
108 static void *query_starter(server_t* server)
113 static int postfix_parsejob(query_t *query, char *p)
115 #define PARSE_CHECK(expr, error, ...) \
118 syslog(LOG_ERR, error, ##__VA_ARGS__); \
130 p = strchr(k = p, '=');
131 PARSE_CHECK(p, "could not find '=' in line");
132 for (klen = p - k; klen && isblank(k[klen]); klen--);
137 p = strchr(v = p, '\n');
138 PARSE_CHECK(p, "could not find final \\n in line");
139 for (vlen = p - v; vlen && isblank(v[vlen]); vlen--);
140 p += 1; /* skip \n */
142 vtk = tokenize(v, vlen);
143 switch (tokenize(k, klen)) {
144 #define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; break;
145 CASE(HELO_NAME, helo_name);
146 CASE(QUEUE_ID, queue_id);
147 CASE(SENDER, sender);
148 CASE(RECIPIENT, recipient);
149 CASE(RECIPIENT_COUNT, recipient_count);
150 CASE(CLIENT_ADDRESS, client_address);
151 CASE(CLIENT_NAME, client_name);
152 CASE(REVERSE_CLIENT_NAME, reverse_client_name);
153 CASE(INSTANCE, instance);
154 CASE(SASL_METHOD, sasl_method);
155 CASE(SASL_USERNAME, sasl_username);
156 CASE(SASL_SENDER, sasl_sender);
158 CASE(CCERT_SUBJECT, ccert_subject);
159 CASE(CCERT_ISSUER, ccert_issuer);
160 CASE(CCSERT_FINGERPRINT, ccsert_fingerprint);
161 CASE(ENCRYPTION_PROTOCOL, encryption_protocol);
162 CASE(ENCRYPTION_CIPHER, encryption_cipher);
163 CASE(ENCRYPTION_KEYSIZE, encryption_keysize);
164 CASE(ETRN_DOMAIN, etrn_domain);
168 PARSE_CHECK(vtk == PTK_SMTPD_ACCESS_POLICY,
169 "unexpected `request' value: %.*s", vlen, v);
172 case PTK_PROTOCOL_NAME:
173 PARSE_CHECK(vtk == PTK_SMTP || vtk == PTK_ESMTP,
174 "unexpected `protocol_name' value: %.*s", vlen, v);
175 query->esmtp = vtk == PTK_ESMTP;
178 case PTK_PROTOCOL_STATE:
180 #define CASE(name) case PTK_##name: query->state = SMTP_##name; break;
187 CASE(END_OF_MESSAGE);
191 PARSE_CHECK(false, "unexpected `protocol_state` value: %.*s",
198 syslog(LOG_WARNING, "unexpected key, skipped: %.*s", klen, k);
203 return query->state == SMTP_UNKNOWN ? -1 : 0;
207 __attribute__((format(printf,2,0)))
208 static void policy_answer(server_t *pcy, const char *fmt, ...)
212 buffer_addvf(&pcy->obuf, fmt, args);
214 buffer_addstr(&pcy->obuf, "\n\n");
215 buffer_consume(&pcy->ibuf, ((query_t*)(pcy->data))->eoq - pcy->ibuf.data);
216 epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy);
219 static void policy_process(server_t *pcy)
221 policy_answer(pcy, "DUNNO");
224 static int policy_run(server_t *pcy, void* config)
226 ssize_t search_offs = MAX(0, pcy->ibuf.len - 1);
227 int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
231 if (errno == EAGAIN || errno == EINTR)
238 syslog(LOG_ERR, "unexpected end of data");
242 if (!(eoq = strstr(pcy->ibuf.data + search_offs, "\n\n")))
245 if (postfix_parsejob(pcy->data, pcy->ibuf.data) < 0)
247 ((query_t*)pcy->data)->eoq = eoq + strlen("\n\n");
248 epoll_modify(pcy->fd, 0, pcy);
253 int start_listener(int port)
255 return start_server(port, NULL, NULL);
258 /* administrivia {{{ */
260 static int main_initialize(void)
262 openlog("postlicyd", LOG_PID, LOG_MAIL);
263 signal(SIGPIPE, SIG_IGN);
264 signal(SIGINT, &common_sighandler);
265 signal(SIGTERM, &common_sighandler);
266 signal(SIGHUP, &common_sighandler);
267 signal(SIGSEGV, &common_sighandler);
268 syslog(LOG_INFO, "Starting...");
272 static void main_shutdown(void)
277 module_init(main_initialize);
278 module_exit(main_shutdown);
282 fputs("usage: "DAEMON_NAME" [options] config\n"
285 " -l <port> port to listen to\n"
286 " -p <pidfile> file to write our pid to\n"
287 " -f stay in foreground\n"
293 int main(int argc, char *argv[])
295 const char *pidfile = NULL;
296 bool daemonize = true;
297 int port = DEFAULT_PORT;
299 for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) {
316 if (argc - optind != 1) {
321 if (pidfile_open(pidfile) < 0) {
322 syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
326 if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
327 syslog(LOG_CRIT, "unable to drop privileges");
331 if (daemonize && daemon_detach() < 0) {
332 syslog(LOG_CRIT, "unable to fork");
338 if (start_listener(port) < 0)
341 (void)server_loop(query_starter, (delete_client_t)query_delete,
344 syslog(LOG_INFO, "Stopping...");