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 {
66 static listener_t *decoder_ptr = NULL;
67 static listener_t *encoder_ptr = NULL;
69 static void *srsd_starter(listener_t *server)
78 void urldecode(char *s, char *end)
83 if (*p == '%' && end - p >= 3) {
84 int h = (hexval(p[1]) << 4) | hexval(p[2]);
98 int process_srs(client_t *srsd, void* vconfig)
100 srs_config_t* config = vconfig;
101 buffer_t *ibuf = client_input_buffer(srsd);
102 buffer_t *obuf = client_output_buffer(srsd);
103 bool decoder = (client_data(srsd) == decoder_ptr);
104 int res = client_read(srsd);
106 if ((res < 0 && errno != EINTR && errno != EAGAIN) || res == 0)
109 while (ibuf->len > 4) {
110 char buf[BUFSIZ], *p, *q, *nl;
113 nl = strchr(ibuf->data + 4, '\n');
115 if (ibuf->len > BUFSIZ) {
116 err("unreasonnable amount of data without a \\n");
125 if (strncmp("get ", ibuf->data, 4)) {
126 err("bad request, not starting with \"get \"");
130 for (p = ibuf->data + 4; p < nl && isspace(*p); p++);
131 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
134 buffer_addstr(&*obuf, "400 empty request ???\n");
135 warn("empty request");
142 err = srs_reverse(config->srs, buf, ssizeof(buf), p);
144 err = srs_forward(config->srs, buf, ssizeof(buf), p, config->domain);
148 buffer_addstr(&*obuf, "200 ");
149 buffer_addstr(&*obuf, buf);
151 switch (SRS_ERROR_TYPE(err)) {
152 case SRS_ERRTYPE_SRS:
153 case SRS_ERRTYPE_SYNTAX:
154 buffer_addstr(&*obuf, "500 ");
157 buffer_addstr(&*obuf, "400 ");
160 buffer_addstr(obuf, srs_strerror(err));
162 buffer_addch(obuf, '\n');
165 buffer_consume(ibuf, nl - ibuf->data);
177 static srs_config_t config = {
182 /** overload srs_free since the lib is not properly maintained.
184 inline void srs_free(srs_t* srs)
187 for (i = 0; i < srs->numsecrets; i++) {
188 memset(srs->secrets[i], 0, strlen(srs->secrets[i]));
189 free(srs->secrets[i]);
190 srs->secrets[i] = '\0';
198 static void config_shutdown(void)
201 srs_free(config.srs);
206 module_exit(config_shutdown);
208 static srs_t *srs_read_secrets(const char *sfile)
215 f = fopen(sfile, "r");
223 while (fgets(buf, sizeof(buf), f)) {
227 if (n == sizeof(buf) - 1 && buf[n - 1] != '\n') {
228 crit("%s:%d: line too long", sfile, lineno);
232 srs_add_secret(srs, skipspaces(buf));
236 crit("%s: empty file, no secrets", sfile);
249 /* administrivia {{{1
254 fputs("usage: "DAEMON_NAME" [options] domain secrets\n"
257 " -e <port> port to listen to for encoding requests\n"
258 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
259 " -d <port> port to listen to for decoding requests\n"
260 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
261 " -p <pidfile> file to write our pid to\n"
262 " -u unsafe mode: don't drop privilegies\n"
263 " -f stay in foreground\n"
270 int main(int argc, char *argv[])
273 bool daemonize = true;
274 int port_enc = DEFAULT_ENCODER_PORT;
275 int port_dec = DEFAULT_DECODER_PORT;
276 const char *pidfile = NULL;
278 for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) {
281 port_enc = atoi(optarg);
287 port_dec = atoi(optarg);
305 if (argc - optind != 2) {
310 info("%s v%s...", DAEMON_NAME, DAEMON_VERSION);
312 config.domain = argv[optind];
313 config.srs = srs_read_secrets(argv[optind + 1]);
315 || common_setup(pidfile, unsafe, RUNAS_USER, RUNAS_GROUP,
316 daemonize) != EXIT_SUCCESS
317 || (encoder_ptr = start_listener(port_enc)) == NULL
318 || (decoder_ptr = start_listener(port_dec)) == NULL) {
321 return server_loop(srsd_starter, NULL, process_srs, NULL, &config);