]> jfr.im git - solanum.git/blame - authd/authd.c
authd: allow querying the list of DNS servers.
[solanum.git] / authd / authd.c
CommitLineData
0d73e7db
AC
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
f3e11b1d
AC
21#include "authd.h"
22#include "dns.h"
0d73e7db
AC
23
24#define MAXPARA 10
25
394b8dde
EM
26static void handle_stat(int parc, char *parv[]);
27
f3e11b1d 28rb_helper *authd_helper = NULL;
8cf45447
AC
29authd_cmd_handler authd_cmd_handlers[255] = {
30 ['D'] = resolve_dns,
394b8dde
EM
31 ['S'] = handle_stat,
32};
33
34authd_stat_handler authd_stat_handlers[255] = {
35 ['D'] = enumerate_nameservers,
8cf45447 36};
0d73e7db 37
394b8dde
EM
38static void
39handle_stat(int parc, char *parv[])
40{
41 authd_stat_handler handler;
42
43 if(parc < 3)
44 /* XXX Should log this somehow */
45 return;
46
47 if (!(handler = authd_stat_handlers[parv[2][0]]))
48 return;
49
50 handler(parv[1], parv[2][0]);
51}
52
0d73e7db
AC
53static void
54parse_request(rb_helper *helper)
55{
56 static char *parv[MAXPARA + 1];
57 static char readbuf[READBUF_SIZE];
58 int parc;
59 int len;
f3e11b1d 60 authd_cmd_handler handler;
0d73e7db
AC
61
62 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
63 {
64 parc = rb_string_to_array(readbuf, parv, MAXPARA);
65
66 if(parc < 1)
67 continue;
68
f3e11b1d
AC
69 handler = authd_cmd_handlers[parv[0][0]];
70 if (handler != NULL)
71 handler(parc, parv);
0d73e7db
AC
72 }
73}
74
75static void
76error_cb(rb_helper *helper)
77{
78 exit(1);
79}
80
81#ifndef WINDOWS
82static void
83dummy_handler(int sig)
84{
85 return;
86}
87#endif
88
89static void
90setup_signals(void)
91{
92#ifndef WINDOWS
93 struct sigaction act;
94
95 act.sa_flags = 0;
96 act.sa_handler = SIG_IGN;
97 sigemptyset(&act.sa_mask);
98 sigaddset(&act.sa_mask, SIGPIPE);
99 sigaddset(&act.sa_mask, SIGALRM);
100#ifdef SIGTRAP
101 sigaddset(&act.sa_mask, SIGTRAP);
102#endif
103
104#ifdef SIGWINCH
105 sigaddset(&act.sa_mask, SIGWINCH);
106 sigaction(SIGWINCH, &act, 0);
107#endif
108 sigaction(SIGPIPE, &act, 0);
109#ifdef SIGTRAP
110 sigaction(SIGTRAP, &act, 0);
111#endif
112
113 act.sa_handler = dummy_handler;
114 sigaction(SIGALRM, &act, 0);
115#endif
116}
117
118int
119main(int argc, char *argv[])
120{
121 setup_signals();
122
123 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
124 if(authd_helper == NULL)
125 {
126 fprintf(stderr, "authd is not meant to be invoked by end users\n");
127 exit(1);
128 }
129
8cf45447
AC
130 rb_set_time();
131 setup_signals();
132 init_resolver();
133 rb_init_prng(NULL, RB_PRNG_DEFAULT);
134
0d73e7db
AC
135 rb_helper_loop(authd_helper, 0);
136
137 return 0;
138}