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 © 2005-2007 Pierre Habouzit
37 #include <netinet/in.h>
38 #include <sys/epoll.h>
48 #define DAEMON_NAME "pfix-srsd"
49 #define DEFAULT_ENCODER_PORT 10000
50 #define DEFAULT_DECODER_PORT 10001
52 #define STR(x) __tostr(x)
54 /* srs encoder/decoder/listener worker {{{ */
56 typedef struct srsd_t {
57 unsigned listener : 1;
65 static srsd_t *srsd_new(void)
67 srsd_t *srsd = p_new(srsd_t, 1);
72 static void srsd_delete(srsd_t **srsd)
77 buffer_wipe(&(*srsd)->ibuf);
78 buffer_wipe(&(*srsd)->obuf);
83 void urldecode(char *s, char *end)
88 if (*p == '%' && end - p >= 3) {
89 int h = (hexval(p[1]) << 4) | hexval(p[2]);
103 int process_srs(srs_t *srs, const char *domain, srsd_t *srsd)
105 while (srsd->ibuf.len > 4) {
106 char buf[BUFSIZ], *p, *q, *nl;
109 nl = strchr(srsd->ibuf.data + 4, '\n');
111 if (srsd->ibuf.len > BUFSIZ) {
112 syslog(LOG_ERR, "unreasonnable amount of data without a \\n");
118 if (strncmp("get ", srsd->ibuf.data, 4)) {
119 syslog(LOG_ERR, "bad request, not starting with \"get \"");
123 for (p = srsd->ibuf.data + 4; p < nl && isspace(*p); p++);
124 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
127 buffer_addstr(&srsd->obuf, "400 empty request ???\n");
128 syslog(LOG_WARNING, "empty request");
135 err = srs_reverse(srs, buf, ssizeof(buf), p);
137 err = srs_forward(srs, buf, ssizeof(buf), p, domain);
141 buffer_addstr(&srsd->obuf, "200 ");
142 buffer_addstr(&srsd->obuf, buf);
144 switch (SRS_ERROR_TYPE(err)) {
145 case SRS_ERRTYPE_SRS:
146 case SRS_ERRTYPE_SYNTAX:
147 buffer_addstr(&srsd->obuf, "500 ");
150 buffer_addstr(&srsd->obuf, "400 ");
153 buffer_addstr(&srsd->obuf, srs_strerror(err));
155 buffer_addch(&srsd->obuf, '\n');
158 buffer_consume(&srsd->ibuf, nl - srsd->ibuf.data);
164 int start_listener(int epollfd, int port, bool decoder)
166 struct sockaddr_in addr = {
167 .sin_family = AF_INET,
168 .sin_addr = { htonl(INADDR_LOOPBACK) },
170 struct epoll_event evt = { .events = EPOLLIN };
174 addr.sin_port = htons(port);
175 sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
180 evt.data.ptr = tmp = srsd_new();
182 tmp->decoder = decoder;
183 tmp->listener = true;
184 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &evt) < 0) {
185 UNIXERR("epoll_ctl");
192 /* administrivia {{{ */
194 static int main_initialize(void)
196 openlog(DAEMON_NAME, LOG_PID, LOG_MAIL);
197 signal(SIGPIPE, SIG_IGN);
198 signal(SIGINT, &common_sighandler);
199 signal(SIGTERM, &common_sighandler);
200 signal(SIGHUP, &common_sighandler);
201 syslog(LOG_INFO, "Starting...");
205 static void main_shutdown(void)
207 syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit...");
211 module_init(main_initialize);
212 module_exit(main_shutdown);
216 fputs("usage: "DAEMON_NAME" [ -e <port> ] [ -d <port> ] domain secrets\n"
218 " -e <port> port to listen to for encoding requests\n"
219 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
220 " -d <port> port to listen to for decoding requests\n"
221 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
227 int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec)
229 int exitcode = EXIT_SUCCESS;
230 int epollfd = epoll_create(128);
233 UNIXERR("epoll_create");
234 exitcode = EXIT_FAILURE;
238 if (start_listener(epollfd, port_enc, false) < 0)
240 if (start_listener(epollfd, port_dec, true) < 0)
244 struct epoll_event evts[1024];
247 n = epoll_wait(epollfd, evts, countof(evts), -1);
249 if (errno != EAGAIN && errno != EINTR) {
250 UNIXERR("epoll_wait");
251 exitcode = EXIT_FAILURE;
258 srsd_t *srsd = evts[n].data.ptr;
260 if (srsd->listener) {
261 struct epoll_event evt = { .events = EPOLLIN };
265 sock = accept_nonblock(srsd->fd);
271 evt.data.ptr = tmp = srsd_new();
272 tmp->decoder = srsd->decoder;
274 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &evt) < 0) {
275 UNIXERR("epoll_ctl");
282 if (evts[n].events & EPOLLIN) {
283 int res = buffer_read(&srsd->ibuf, srsd->fd, -1);
285 if ((res < 0 && errno != EINTR && errno != EAGAIN)
292 if (process_srs(srs, domain, srsd) < 0) {
298 if ((evts[n].events & EPOLLOUT) && srsd->obuf.len) {
299 int res = write(srsd->fd, srsd->obuf.data, srsd->obuf.len);
301 if (res < 0 && errno != EINTR && errno != EAGAIN) {
307 buffer_consume(&srsd->obuf, res);
311 if (srsd->watchwr == !srsd->obuf.len) {
312 struct epoll_event evt = {
313 .events = EPOLLIN | (srsd->obuf.len ? EPOLLOUT : 0),
316 if (epoll_ctl(epollfd, EPOLL_CTL_MOD, srsd->fd, &evt) < 0) {
317 UNIXERR("epoll_ctl");
321 srsd->watchwr = srsd->obuf.len != 0;
333 static srs_t *srs_read_secrets(const char *sfile)
340 f = fopen(sfile, "r");
348 while (fgets(buf, sizeof(buf), f)) {
352 if (buf[n - 1] != '\n') {
353 syslog(LOG_CRIT, "%s:%d: line too long", sfile, lineno);
357 srs_add_secret(srs, buf);
361 syslog(LOG_CRIT, "%s: empty file, no secrets", sfile);
374 int main(int argc, char *argv[])
376 int port_enc = DEFAULT_ENCODER_PORT;
377 int port_dec = DEFAULT_DECODER_PORT;
381 if (atexit(common_shutdown)) {
382 fputs("Cannot hook my atexit function, quitting !\n", stderr);
387 for (int c = 0; (c = getopt(argc, argv, "he:d:")) >= 0; ) {
390 port_enc = atoi(optarg);
393 port_dec = atoi(optarg);
401 if (argc - optind != 2) {
406 srs = srs_read_secrets(argv[optind + 1]);
411 if (daemon_detach() < 0) {
412 syslog(LOG_CRIT, "unable to fork");
415 return main_loop(srs, argv[optind], port_enc, port_dec);