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
45 SMTP_HELO = SMTP_EHLO,
54 /* \see http://www.postfix.org/SMTPD_POLICY_README.html */
55 typedef struct query_t {
59 const char *helo_name;
62 const char *recipient;
63 const char *recipient_count;
64 const char *client_address;
65 const char *client_name;
66 const char *rclient_name;
70 const char *sasl_method;
71 const char *sasl_username;
72 const char *sasl_sender;
74 const char *ccert_subject;
75 const char *ccert_issuer;
76 const char *ccsert_fingerprint;
79 const char *encryption_protocol;
80 const char *encryption_cipher;
81 const char *encryption_keysize;
82 const char *etrn_domain;
87 static query_t *query_init(query_t *rq) {
88 memset(rq, 0, offsetof(query_t, data));
89 buffer_init(&rq->data);
92 static void query_wipe(query_t *rq) {
93 buffer_wipe(&rq->data);
96 static int xwrite(int fd, const char *s, size_t l)
99 int nb = write(fd, s, l);
101 if (errno == EINTR || errno == EAGAIN)
110 static int postfix_parsejob(query_t *query)
112 #define PARSE_CHECK(expr, error, ...) \
115 syslog(LOG_ERR, error, ##__VA_ARGS__); \
120 char *p = vskipspaces(query->data.data);
122 memset(query, 0, offsetof(query_t, data));
123 while (p[0] != '\r' || p[1] != '\n') {
129 p = strchr(k = p, '=');
130 PARSE_CHECK(p, "could not find '=' in line");
131 for (klen = p - k; klen && isblank(k[klen]); klen--);
136 p = strstr(v = p, "\r\n");
137 PARSE_CHECK(p, "could not find final \\r\\n in line");
138 for (vlen = p - v; vlen && isblank(v[vlen]); vlen--);
139 p += 2; /* skip \r\n */
141 vtk = tokenize(v, vlen);
142 switch (tokenize(k, klen)) {
143 #define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; break;
144 CASE(HELO_NAME, helo_name);
145 CASE(QUEUE_ID, queue_id);
146 CASE(SENDER, sender);
147 CASE(RECIPIENT, recipient);
148 CASE(RECIPIENT_COUNT, recipient_count);
149 CASE(CLIENT_ADDRESS, client_address);
150 CASE(CLIENT_NAME, client_name);
151 CASE(RCLIENT_NAME, rclient_name);
152 CASE(INSTANCE, instance);
153 CASE(SASL_METHOD, sasl_method);
154 CASE(SASL_USERNAME, sasl_username);
155 CASE(SASL_SENDER, sasl_sender);
157 CASE(CCERT_SUBJECT, ccert_subject);
158 CASE(CCERT_ISSUER, ccert_issuer);
159 CASE(CCSERT_FINGERPRINT, ccsert_fingerprint);
160 CASE(ENCRYPTION_PROTOCOL, encryption_protocol);
161 CASE(ENCRYPTION_CIPHER, encryption_cipher);
162 CASE(ENCRYPTION_KEYSIZE, encryption_keysize);
163 CASE(ETRN_DOMAIN, etrn_domain);
167 PARSE_CHECK(vtk == PTK_SMTPD_ACCESS_POLICY,
168 "unexpected `request' value: %.*s", vlen, v);
171 case PTK_PROTOCOL_NAME:
172 PARSE_CHECK(vtk == PTK_SMTP || vtk == PTK_ESMTP,
173 "unexpected `protocol_name' value: %.*s", vlen, v);
174 query->esmtp = vtk == PTK_ESMTP;
177 case PTK_PROTOCOL_STATE:
179 #define CASE(name) case PTK_##name: query->state = SMTP_##name; break;
186 CASE(END_OF_MESSAGE);
190 PARSE_CHECK(false, "unexpected `protocol_state` value: %.*s",
197 syslog(LOG_WARNING, "unexpected key, skipped: %.*s", klen, k);
202 return query->state == SMTP_UNKNOWN ? -1 : 0;
206 void *policy_run(int fd, void *data)
212 int nb = buffer_read(&q.data, fd, -1);
216 if (errno == EAGAIN || errno == EINTR)
223 syslog(LOG_ERR, "unexpected end of data");
227 eoq = strstr(q.data.data + MAX(0, q.data.len - 3), "\r\n\r\n");
231 if (postfix_parsejob(&q) < 0)
234 buffer_consume(&q.data, eoq + strlen("\r\n\r\n") - q.data.data);
235 if (xwrite(fd, "DUNNO\r\n", strlen("DUNNO\r\n"))) {