]> jfr.im git - solanum.git/blob - authd/authd.c
authd: implement DNS module
[solanum.git] / authd / authd.c
1 /* authd/authd.c - main code for authd
2 * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "authd.h"
22 #include "dns.h"
23
24 #define MAXPARA 10
25
26 rb_helper *authd_helper = NULL;
27 authd_cmd_handler authd_cmd_handlers[255] = {
28 ['D'] = resolve_dns,
29 };
30
31 static void
32 parse_request(rb_helper *helper)
33 {
34 static char *parv[MAXPARA + 1];
35 static char readbuf[READBUF_SIZE];
36 int parc;
37 int len;
38 authd_cmd_handler handler;
39
40 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
41 {
42 parc = rb_string_to_array(readbuf, parv, MAXPARA);
43
44 if(parc < 1)
45 continue;
46
47 handler = authd_cmd_handlers[parv[0][0]];
48 if (handler != NULL)
49 handler(parc, parv);
50 }
51 }
52
53 static void
54 error_cb(rb_helper *helper)
55 {
56 exit(1);
57 }
58
59 #ifndef WINDOWS
60 static void
61 dummy_handler(int sig)
62 {
63 return;
64 }
65 #endif
66
67 static void
68 setup_signals(void)
69 {
70 #ifndef WINDOWS
71 struct sigaction act;
72
73 act.sa_flags = 0;
74 act.sa_handler = SIG_IGN;
75 sigemptyset(&act.sa_mask);
76 sigaddset(&act.sa_mask, SIGPIPE);
77 sigaddset(&act.sa_mask, SIGALRM);
78 #ifdef SIGTRAP
79 sigaddset(&act.sa_mask, SIGTRAP);
80 #endif
81
82 #ifdef SIGWINCH
83 sigaddset(&act.sa_mask, SIGWINCH);
84 sigaction(SIGWINCH, &act, 0);
85 #endif
86 sigaction(SIGPIPE, &act, 0);
87 #ifdef SIGTRAP
88 sigaction(SIGTRAP, &act, 0);
89 #endif
90
91 act.sa_handler = dummy_handler;
92 sigaction(SIGALRM, &act, 0);
93 #endif
94 }
95
96 int
97 main(int argc, char *argv[])
98 {
99 setup_signals();
100
101 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
102 if(authd_helper == NULL)
103 {
104 fprintf(stderr, "authd is not meant to be invoked by end users\n");
105 exit(1);
106 }
107
108 rb_set_time();
109 setup_signals();
110 init_resolver();
111 rb_init_prng(NULL, RB_PRNG_DEFAULT);
112
113 rb_helper_loop(authd_helper, 0);
114
115 return 0;
116 }