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 CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
20 /* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
23 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
24 /* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
25 /* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
26 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
27 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
28 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
29 /* POSSIBILITY OF SUCH DAMAGE. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2005-2007 Pierre Habouzit
37 * Copyright © 2008 Florent Bruneau
48 #define DAEMON_NAME "pfix-srsd"
49 #define DAEMON_VERSION "0.5"
50 #define DEFAULT_ENCODER_PORT 10001
51 #define DEFAULT_DECODER_PORT 10002
52 #define RUNAS_USER "nobody"
53 #define RUNAS_GROUP "nogroup"
57 typedef struct srs_config_t {
68 static listener_t *decoder_ptr = NULL;
69 static listener_t *encoder_ptr = NULL;
71 static void *srsd_starter(listener_t *server)
80 char *urldecode(char *s, char *end)
85 if (*p == '%' && end - p >= 3) {
86 int h = (hexval(p[1]) << 4) | hexval(p[2]);
101 int process_srs(client_t *srsd, void* vconfig)
103 srs_config_t* config = vconfig;
104 buffer_t *ibuf = client_input_buffer(srsd);
105 buffer_t *obuf = client_output_buffer(srsd);
106 bool decoder = (client_data(srsd) == decoder_ptr);
107 int res = client_read(srsd);
109 if ((res < 0 && errno != EINTR && errno != EAGAIN) || res == 0)
112 while (ibuf->len > 4) {
113 char buf[BUFSIZ], *p, *q, *nl;
116 nl = strchr(ibuf->data + 4, '\n');
118 if (ibuf->len > BUFSIZ) {
119 err("unreasonnable amount of data without a \\n");
128 if (strncmp("get ", ibuf->data, 4)) {
129 err("bad request, not starting with \"get \"");
133 for (p = ibuf->data + 4; p < nl && isspace(*p); p++);
134 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
137 buffer_addstr(obuf, "400 empty request ???\n");
138 warn("empty request");
145 if (config->ignore_ext) {
146 int dlen = config->domainlen;
148 if (q - p <= dlen || q[-1 - dlen] != '@' ||
149 memcmp(q - dlen, config->domain, dlen))
151 buffer_addstr(obuf, "200 ");
152 buffer_add(obuf, p, q - p);
153 buffer_addch(obuf, '\n');
157 err = srs_reverse(config->srs, buf, ssizeof(buf), p);
159 err = srs_forward(config->srs, buf, ssizeof(buf), p, config->domain);
163 buffer_addstr(obuf, "200 ");
164 buffer_addstr(obuf, buf);
166 switch (SRS_ERROR_TYPE(err)) {
167 case SRS_ERRTYPE_SRS:
168 case SRS_ERRTYPE_SYNTAX:
169 buffer_addstr(obuf, "500 ");
172 buffer_addstr(obuf, "400 ");
175 buffer_addstr(obuf, srs_strerror(err));
177 buffer_addch(obuf, '\n');
180 buffer_consume(ibuf, nl - ibuf->data);
192 static srs_config_t config = {
197 /** overload srs_free since the lib is not properly maintained.
199 inline void srs_free(srs_t* srs)
202 for (i = 0; i < srs->numsecrets; i++) {
203 memset(srs->secrets[i], 0, strlen(srs->secrets[i]));
204 free(srs->secrets[i]);
205 srs->secrets[i] = '\0';
213 static void config_shutdown(void)
216 srs_free(config.srs);
221 module_exit(config_shutdown);
223 static srs_t *srs_read_secrets(const char *sfile)
230 f = fopen(sfile, "r");
238 while (fgets(buf, sizeof(buf), f)) {
242 if (n == sizeof(buf) - 1 && buf[n - 1] != '\n') {
243 crit("%s:%d: line too long", sfile, lineno);
247 srs_add_secret(srs, skipspaces(buf));
251 crit("%s: empty file, no secrets", sfile);
264 /* administrivia {{{1
269 fputs("usage: "DAEMON_NAME" [options] domain secrets\n"
272 " -e <port> port to listen to for encoding requests\n"
273 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
274 " -d <port> port to listen to for decoding requests\n"
275 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
276 " -p <pidfile> file to write our pid to\n"
277 " -u unsafe mode: don't drop privilegies\n"
278 " -I do not touch mails outside of \"domain\" in decoding mode\n"
279 " -f stay in foreground\n"
286 int main(int argc, char *argv[])
289 bool daemonize = true;
290 int port_enc = DEFAULT_ENCODER_PORT;
291 int port_dec = DEFAULT_DECODER_PORT;
292 const char *pidfile = NULL;
294 for (int c = 0; (c = getopt(argc, argv, "hfuI" "e:d:p:")) >= 0; ) {
297 port_enc = atoi(optarg);
303 port_dec = atoi(optarg);
312 config.ignore_ext = true;
324 if (argc - optind != 2) {
329 info("%s v%s...", DAEMON_NAME, DAEMON_VERSION);
331 config.domain = argv[optind];
332 config.domainlen = strlen(config.domain);
333 config.srs = srs_read_secrets(argv[optind + 1]);
335 || common_setup(pidfile, unsafe, RUNAS_USER, RUNAS_GROUP,
336 daemonize) != EXIT_SUCCESS
337 || (encoder_ptr = start_listener(port_enc)) == NULL
338 || (decoder_ptr = start_listener(port_dec)) == NULL) {
341 return server_loop(srsd_starter, NULL, process_srs, NULL, &config);