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
34 * Copyright © 2008 Florent Bruneau
45 #define DAEMON_NAME "pfix-srsd"
46 #define DAEMON_VERSION "0.4"
47 #define DEFAULT_ENCODER_PORT 10001
48 #define DEFAULT_DECODER_PORT 10002
49 #define RUNAS_USER "nobody"
50 #define RUNAS_GROUP "nogroup"
54 typedef struct srs_config_t {
63 static listener_t *decoder_ptr = NULL;
64 static listener_t *encoder_ptr = NULL;
66 static void *srsd_starter(listener_t *server)
75 void urldecode(char *s, char *end)
80 if (*p == '%' && end - p >= 3) {
81 int h = (hexval(p[1]) << 4) | hexval(p[2]);
95 int process_srs(client_t *srsd, void* vconfig)
97 srs_config_t* config = vconfig;
98 buffer_t *ibuf = client_input_buffer(srsd);
99 buffer_t *obuf = client_output_buffer(srsd);
100 bool decoder = (client_data(srsd) == decoder_ptr);
101 int res = client_read(srsd);
103 if ((res < 0 && errno != EINTR && errno != EAGAIN) || res == 0)
106 while (ibuf->len > 4) {
107 char buf[BUFSIZ], *p, *q, *nl;
110 nl = strchr(ibuf->data + 4, '\n');
112 if (ibuf->len > BUFSIZ) {
113 err("unreasonnable amount of data without a \\n");
122 if (strncmp("get ", ibuf->data, 4)) {
123 err("bad request, not starting with \"get \"");
127 for (p = ibuf->data + 4; p < nl && isspace(*p); p++);
128 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
131 buffer_addstr(&*obuf, "400 empty request ???\n");
132 warn("empty request");
139 err = srs_reverse(config->srs, buf, ssizeof(buf), p);
141 err = srs_forward(config->srs, buf, ssizeof(buf), p, config->domain);
145 buffer_addstr(&*obuf, "200 ");
146 buffer_addstr(&*obuf, buf);
148 switch (SRS_ERROR_TYPE(err)) {
149 case SRS_ERRTYPE_SRS:
150 case SRS_ERRTYPE_SYNTAX:
151 buffer_addstr(&*obuf, "500 ");
154 buffer_addstr(&*obuf, "400 ");
157 buffer_addstr(obuf, srs_strerror(err));
159 buffer_addch(obuf, '\n');
162 buffer_consume(ibuf, nl - ibuf->data);
174 static srs_config_t config = {
179 /** overload srs_free since the lib is not properly maintained.
181 inline void srs_free(srs_t* srs)
184 for (i = 0; i < srs->numsecrets; i++) {
185 memset(srs->secrets[i], 0, strlen(srs->secrets[i]));
186 free(srs->secrets[i]);
187 srs->secrets[i] = '\0';
195 static void config_shutdown(void)
198 srs_free(config.srs);
203 module_exit(config_shutdown);
205 static srs_t *srs_read_secrets(const char *sfile)
212 f = fopen(sfile, "r");
220 while (fgets(buf, sizeof(buf), f)) {
224 if (n == sizeof(buf) - 1 && buf[n - 1] != '\n') {
225 crit("%s:%d: line too long", sfile, lineno);
229 srs_add_secret(srs, skipspaces(buf));
233 crit("%s: empty file, no secrets", sfile);
246 /* administrivia {{{1
251 fputs("usage: "DAEMON_NAME" [options] domain secrets\n"
254 " -e <port> port to listen to for encoding requests\n"
255 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
256 " -d <port> port to listen to for decoding requests\n"
257 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
258 " -p <pidfile> file to write our pid to\n"
259 " -u unsafe mode: don't drop privilegies\n"
260 " -f stay in foreground\n"
267 int main(int argc, char *argv[])
270 bool daemonize = true;
271 int port_enc = DEFAULT_ENCODER_PORT;
272 int port_dec = DEFAULT_DECODER_PORT;
273 const char *pidfile = NULL;
275 for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) {
278 port_enc = atoi(optarg);
284 port_dec = atoi(optarg);
302 if (argc - optind != 2) {
307 info("%s v%s...", DAEMON_NAME, DAEMON_VERSION);
309 config.domain = argv[optind];
310 config.srs = srs_read_secrets(argv[optind + 1]);
312 || common_setup(pidfile, unsafe, RUNAS_USER, RUNAS_GROUP,
313 daemonize) != EXIT_SUCCESS
314 || (encoder_ptr = start_listener(port_enc)) == NULL
315 || (decoder_ptr = start_listener(port_dec)) == NULL) {
318 return server_loop(srsd_starter, NULL, process_srs, NULL, &config);