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"
53 #define STR(x) __tostr(x)
55 typedef struct srs_config_t {
60 static const char* decoder_ptr = "decoder";
61 static const char* encoder_ptr = "encoder";
63 static void *srsd_new_decoder(void)
65 return (void*)decoder_ptr;
68 static void *srsd_new_encoder(void)
70 return (void*)encoder_ptr;
73 static void *srsd_stater(server_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(server_t *srsd, void* vconfig)
100 srs_config_t* config = vconfig;
101 int res = buffer_read(&srsd->ibuf, srsd->fd, -1);
103 if ((res < 0 && errno != EINTR && errno != EAGAIN) || res == 0)
106 while (srsd->ibuf.len > 4) {
107 char buf[BUFSIZ], *p, *q, *nl;
110 nl = strchr(srsd->ibuf.data + 4, '\n');
112 if (srsd->ibuf.len > BUFSIZ) {
113 syslog(LOG_ERR, "unreasonnable amount of data without a \\n");
116 if (srsd->obuf.len) {
117 epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
122 if (strncmp("get ", srsd->ibuf.data, 4)) {
123 syslog(LOG_ERR, "bad request, not starting with \"get \"");
127 for (p = srsd->ibuf.data + 4; p < nl && isspace(*p); p++);
128 for (q = nl++; q >= p && isspace(*q); *q-- = '\0');
131 buffer_addstr(&srsd->obuf, "400 empty request ???\n");
132 syslog(LOG_WARNING, "empty request");
138 if (srsd->data == (void*)decoder_ptr) {
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(&srsd->obuf, "200 ");
146 buffer_addstr(&srsd->obuf, buf);
148 switch (SRS_ERROR_TYPE(err)) {
149 case SRS_ERRTYPE_SRS:
150 case SRS_ERRTYPE_SYNTAX:
151 buffer_addstr(&srsd->obuf, "500 ");
154 buffer_addstr(&srsd->obuf, "400 ");
157 buffer_addstr(&srsd->obuf, srs_strerror(err));
159 buffer_addch(&srsd->obuf, '\n');
162 buffer_consume(&srsd->ibuf, nl - srsd->ibuf.data);
164 if (srsd->obuf.len) {
165 epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
170 int start_listener(int port, bool decoder)
172 return start_server(port, decoder ? srsd_new_decoder : srsd_new_encoder, NULL);
176 /* administrivia {{{ */
178 static int main_initialize(void)
180 openlog(DAEMON_NAME, LOG_PID, LOG_MAIL);
181 signal(SIGPIPE, SIG_IGN);
182 signal(SIGINT, &common_sighandler);
183 signal(SIGTERM, &common_sighandler);
184 signal(SIGHUP, &common_sighandler);
185 signal(SIGSEGV, &common_sighandler);
186 syslog(LOG_INFO, "Starting...");
190 static void main_shutdown(void)
195 module_init(main_initialize);
196 module_exit(main_shutdown);
200 fputs("usage: "DAEMON_NAME" [options] domain secrets\n"
203 " -e <port> port to listen to for encoding requests\n"
204 " (default: "STR(DEFAULT_ENCODER_PORT)")\n"
205 " -d <port> port to listen to for decoding requests\n"
206 " (default: "STR(DEFAULT_DECODER_PORT)")\n"
207 " -p <pidfile> file to write our pid to\n"
208 " -u unsafe mode: don't drop privilegies\n"
209 " -f stay in foreground\n"
215 static srs_t *srs_read_secrets(const char *sfile)
222 f = fopen(sfile, "r");
230 while (fgets(buf, sizeof(buf), f)) {
234 if (n == sizeof(buf) - 1 && buf[n - 1] != '\n') {
235 syslog(LOG_CRIT, "%s:%d: line too long", sfile, lineno);
239 srs_add_secret(srs, skipspaces(buf));
243 syslog(LOG_CRIT, "%s: empty file, no secrets", sfile);
256 int main(int argc, char *argv[])
259 bool daemonize = true;
260 int port_enc = DEFAULT_ENCODER_PORT;
261 int port_dec = DEFAULT_DECODER_PORT;
262 const char *pidfile = NULL;
267 for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) {
270 port_enc = atoi(optarg);
276 port_dec = atoi(optarg);
290 if (argc - optind != 2) {
295 srs = srs_read_secrets(argv[optind + 1]);
300 if (pidfile_open(pidfile) < 0) {
301 syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
305 if (!unsafe && drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
306 syslog(LOG_CRIT, "unable to drop privileges");
310 if (daemonize && daemon_detach() < 0) {
311 syslog(LOG_CRIT, "unable to fork");
317 srs_config_t config = {
319 .domain = argv[optind]
322 if (start_listener(port_enc, false) < 0)
324 if (start_listener(port_dec, true) < 0)
327 res = server_loop(srsd_stater, NULL, process_srs, &config);
329 syslog(LOG_INFO, "Stopping...");