]> jfr.im git - solanum.git/blob - authd/authd.c
authd: allow querying the list of DNS servers.
[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 static void handle_stat(int parc, char *parv[]);
27
28 rb_helper *authd_helper = NULL;
29 authd_cmd_handler authd_cmd_handlers[255] = {
30 ['D'] = resolve_dns,
31 ['S'] = handle_stat,
32 };
33
34 authd_stat_handler authd_stat_handlers[255] = {
35 ['D'] = enumerate_nameservers,
36 };
37
38 static void
39 handle_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
53 static void
54 parse_request(rb_helper *helper)
55 {
56 static char *parv[MAXPARA + 1];
57 static char readbuf[READBUF_SIZE];
58 int parc;
59 int len;
60 authd_cmd_handler handler;
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
69 handler = authd_cmd_handlers[parv[0][0]];
70 if (handler != NULL)
71 handler(parc, parv);
72 }
73 }
74
75 static void
76 error_cb(rb_helper *helper)
77 {
78 exit(1);
79 }
80
81 #ifndef WINDOWS
82 static void
83 dummy_handler(int sig)
84 {
85 return;
86 }
87 #endif
88
89 static void
90 setup_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
118 int
119 main(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
130 rb_set_time();
131 setup_signals();
132 init_resolver();
133 rb_init_prng(NULL, RB_PRNG_DEFAULT);
134
135 rb_helper_loop(authd_helper, 0);
136
137 return 0;
138 }