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
46 #define DAEMON_NAME "pfix-srsd"
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 {
59 static const char* decoder_ptr = "decoder";
60 static const char* encoder_ptr = "encoder";
62 static void *srsd_new_decoder(void)
64 return (void*)decoder_ptr;
67 static void *srsd_new_encoder(void)
69 return (void*)encoder_ptr;
72 static void *srsd_stater(server_t *server)
77 void urldecode(char *s, char *end)
82 if (*p == '%' && end - p >= 3) {
83 int h = (hexval(p[1]) << 4) | hexval(p[2]);
97 int process_srs(server_t *srsd, void* vconfig)
99 srs_config_t* config = vconfig;
100 int res = buffer_read(&srsd->ibuf, srsd->fd, -1);
102 if ((res < 0 && errno != EINTR && errno != EAGAIN) || res == 0)
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");
115 if (srsd->obuf.len) {
116 epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
121 if (strncmp("get ", srsd->ibuf.data, 4)) {
122 syslog(LOG_ERR, "bad request, not starting with \"get \"");
126 for (p = srsd->ibuf.data + 4; p < nl && isspace(*p); p++);
127 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
130 buffer_addstr(&srsd->obuf, "400 empty request ???\n");
131 syslog(LOG_WARNING, "empty request");
137 if (srsd->data == (void*)decoder_ptr) {
138 err = srs_reverse(config->srs, buf, ssizeof(buf), p);
140 err = srs_forward(config->srs, buf, ssizeof(buf), p, config->domain);
144 buffer_addstr(&srsd->obuf, "200 ");
145 buffer_addstr(&srsd->obuf, buf);
147 switch (SRS_ERROR_TYPE(err)) {
148 case SRS_ERRTYPE_SRS:
149 case SRS_ERRTYPE_SYNTAX:
150 buffer_addstr(&srsd->obuf, "500 ");
153 buffer_addstr(&srsd->obuf, "400 ");
156 buffer_addstr(&srsd->obuf, srs_strerror(err));
158 buffer_addch(&srsd->obuf, '\n');
161 buffer_consume(&srsd->ibuf, nl - srsd->ibuf.data);
163 if (srsd->obuf.len) {
164 epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
169 int start_listener(int port, bool decoder)
171 return start_server(port, decoder ? srsd_new_decoder : srsd_new_encoder, NULL);
175 /* administrivia {{{ */
180 fputs("usage: "DAEMON_NAME" [options] domain secrets\n"
183 " -e <port> port to listen to for encoding requests\n"
184 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
185 " -d <port> port to listen to for decoding requests\n"
186 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
187 " -p <pidfile> file to write our pid to\n"
188 " -u unsafe mode: don't drop privilegies\n"
189 " -f stay in foreground\n"
195 static srs_t *srs_read_secrets(const char *sfile)
202 f = fopen(sfile, "r");
210 while (fgets(buf, sizeof(buf), f)) {
214 if (n == sizeof(buf) - 1 && buf[n - 1] != '\n') {
215 syslog(LOG_CRIT, "%s:%d: line too long", sfile, lineno);
219 srs_add_secret(srs, skipspaces(buf));
223 syslog(LOG_CRIT, "%s: empty file, no secrets", sfile);
236 int main(int argc, char *argv[])
239 bool daemonize = true;
240 int port_enc = DEFAULT_ENCODER_PORT;
241 int port_dec = DEFAULT_DECODER_PORT;
242 const char *pidfile = NULL;
246 for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) {
249 port_enc = atoi(optarg);
255 port_dec = atoi(optarg);
269 if (argc - optind != 2) {
274 srs = srs_read_secrets(argv[optind + 1]);
279 if (common_setup(pidfile, unsafe, RUNAS_USER, RUNAS_GROUP, daemonize)
284 srs_config_t config = {
286 .domain = argv[optind]
289 if (start_listener(port_enc, false) < 0)
291 if (start_listener(port_dec, true) < 0)
294 return server_loop(srsd_stater, NULL, process_srs, &config);