]> jfr.im git - solanum.git/blame - authd/authd.c
authd: also check size correctly
[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
6445c1cf 26static void handle_reload(int parc, char *parv[]);
394b8dde
EM
27static void handle_stat(int parc, char *parv[]);
28
f3e11b1d 29rb_helper *authd_helper = NULL;
6f39a80e 30authd_cmd_handler authd_cmd_handlers[256] = {
122ae255 31 ['R'] = handle_reload,
8cf45447 32 ['D'] = resolve_dns,
394b8dde
EM
33 ['S'] = handle_stat,
34};
35
6f39a80e 36authd_stat_handler authd_stat_handlers[256] = {
394b8dde 37 ['D'] = enumerate_nameservers,
8cf45447 38};
0d73e7db 39
6f39a80e 40authd_reload_handler authd_reload_handlers[256] = {
6445c1cf
EM
41 ['D'] = reload_nameservers,
42};
43
394b8dde
EM
44static void
45handle_stat(int parc, char *parv[])
46{
47 authd_stat_handler handler;
48
49 if(parc < 3)
50 /* XXX Should log this somehow */
51 return;
52
e23126c8 53 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
394b8dde
EM
54 return;
55
56 handler(parv[1], parv[2][0]);
57}
58
6445c1cf
EM
59static void
60handle_reload(int parc, char *parv[])
61{
62 authd_reload_handler handler;
63
64 if(parc < 2)
122ae255
EM
65 {
66 /* Reload all handlers */
58c343f4 67 for(size_t i = 0; i < 256; i++)
c63cd21e
AC
68 {
69 if ((handler = authd_reload_handlers[(unsigned char) i]) != NULL)
70 handler(parv[1][0]);
71 }
122ae255 72
6445c1cf 73 return;
122ae255 74 }
6445c1cf 75
e23126c8 76 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
6445c1cf
EM
77 return;
78
79 handler(parv[1][0]);
80}
81
0d73e7db
AC
82static void
83parse_request(rb_helper *helper)
84{
85 static char *parv[MAXPARA + 1];
86 static char readbuf[READBUF_SIZE];
87 int parc;
88 int len;
f3e11b1d 89 authd_cmd_handler handler;
0d73e7db
AC
90
91 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
92 {
93 parc = rb_string_to_array(readbuf, parv, MAXPARA);
94
95 if(parc < 1)
96 continue;
97
e23126c8 98 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
f3e11b1d
AC
99 if (handler != NULL)
100 handler(parc, parv);
0d73e7db
AC
101 }
102}
103
104static void
105error_cb(rb_helper *helper)
106{
107 exit(1);
108}
109
8da0b2f2 110#ifndef _WIN32
0d73e7db
AC
111static void
112dummy_handler(int sig)
113{
114 return;
115}
116#endif
117
118static void
119setup_signals(void)
120{
8da0b2f2 121#ifndef _WIN32
0d73e7db
AC
122 struct sigaction act;
123
124 act.sa_flags = 0;
125 act.sa_handler = SIG_IGN;
126 sigemptyset(&act.sa_mask);
127 sigaddset(&act.sa_mask, SIGPIPE);
128 sigaddset(&act.sa_mask, SIGALRM);
129#ifdef SIGTRAP
130 sigaddset(&act.sa_mask, SIGTRAP);
131#endif
132
133#ifdef SIGWINCH
134 sigaddset(&act.sa_mask, SIGWINCH);
135 sigaction(SIGWINCH, &act, 0);
136#endif
137 sigaction(SIGPIPE, &act, 0);
138#ifdef SIGTRAP
139 sigaction(SIGTRAP, &act, 0);
140#endif
141
142 act.sa_handler = dummy_handler;
143 sigaction(SIGALRM, &act, 0);
144#endif
145}
146
147int
148main(int argc, char *argv[])
149{
150 setup_signals();
151
152 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
153 if(authd_helper == NULL)
154 {
155 fprintf(stderr, "authd is not meant to be invoked by end users\n");
156 exit(1);
157 }
158
8cf45447
AC
159 rb_set_time();
160 setup_signals();
161 init_resolver();
162 rb_init_prng(NULL, RB_PRNG_DEFAULT);
163
0d73e7db
AC
164 rb_helper_loop(authd_helper, 0);
165
166 return 0;
167}