]> jfr.im git - solanum.git/blame - authd/authd.c
tools: fix stub rb_strcasecmp() build
[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"
a16b484f 23#include "provider.h"
db821ee9 24#include "notice.h"
0d73e7db
AC
25
26#define MAXPARA 10
27
6445c1cf 28static void handle_reload(int parc, char *parv[]);
394b8dde 29static void handle_stat(int parc, char *parv[]);
a51487e0 30static void handle_options(int parc, char *parv[]);
394b8dde 31
f3e11b1d 32rb_helper *authd_helper = NULL;
6f39a80e 33authd_cmd_handler authd_cmd_handlers[256] = {
a16b484f 34 ['C'] = handle_new_connection,
60374ac9
EM
35 ['D'] = handle_resolve_dns,
36 ['E'] = handle_cancel_connection,
a51487e0 37 ['O'] = handle_options,
f49198a6 38 ['R'] = handle_reload,
394b8dde
EM
39 ['S'] = handle_stat,
40};
41
6f39a80e 42authd_stat_handler authd_stat_handlers[256] = {
394b8dde 43 ['D'] = enumerate_nameservers,
8cf45447 44};
0d73e7db 45
6f39a80e 46authd_reload_handler authd_reload_handlers[256] = {
6445c1cf
EM
47 ['D'] = reload_nameservers,
48};
49
a51487e0
EM
50rb_dictionary *authd_option_handlers;
51
394b8dde
EM
52static void
53handle_stat(int parc, char *parv[])
54{
55 authd_stat_handler handler;
26d491b9 56 long lrid;
394b8dde
EM
57
58 if(parc < 3)
b2ede1aa
EM
59 {
60 warn_opers(L_CRIT, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc);
394b8dde 61 return;
b2ede1aa 62 }
394b8dde 63
26d491b9
EM
64 if((lrid = strtol(parv[1], NULL, 16)) > UINT32_MAX)
65 {
66 warn_opers(L_CRIT, "BUG: handle_stat got a rid that was too large: %lx", lrid);
67 return;
68 }
69
e23126c8 70 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
394b8dde
EM
71 return;
72
26d491b9 73 handler((uint32_t)lrid, parv[2][0]);
394b8dde
EM
74}
75
a51487e0
EM
76static void
77handle_options(int parc, char *parv[])
78{
79 struct auth_opts_handler *handler;
80
850ced64 81 if(parc < 2)
a51487e0 82 {
850ced64 83 warn_opers(L_CRIT, "BUG: handle_options received too few parameters (at least 2 expected, got %d)", parc);
a51487e0
EM
84 return;
85 }
86
87 if((handler = rb_dictionary_retrieve(authd_option_handlers, parv[1])) == NULL)
88 {
89 warn_opers(L_CRIT, "BUG: handle_options got a bad option type %s", parv[1]);
90 return;
91 }
92
93 if((parc - 2) < handler->min_parc)
94 {
95 warn_opers(L_CRIT, "BUG: handle_options received too few parameters (at least %d expected, got %d)", handler->min_parc, parc);
96 return;
97 }
98
f5586c3a 99 handler->handler(parv[1], parc - 2, (const char **)&parv[2]);
a51487e0
EM
100}
101
6445c1cf
EM
102static void
103handle_reload(int parc, char *parv[])
104{
105 authd_reload_handler handler;
106
7c4b574e 107 if(parc <= 2)
b2ede1aa 108 {
122ae255 109 /* Reload all handlers */
a51487e0 110 for(size_t i = 0; i < 256; i++)
c63cd21e
AC
111 {
112 if ((handler = authd_reload_handlers[(unsigned char) i]) != NULL)
7c4b574e 113 handler('\0');
c63cd21e 114 }
122ae255 115
6445c1cf 116 return;
b2ede1aa 117 }
6445c1cf 118
e23126c8 119 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
6445c1cf
EM
120 return;
121
122 handler(parv[1][0]);
123}
124
0d73e7db
AC
125static void
126parse_request(rb_helper *helper)
127{
128 static char *parv[MAXPARA + 1];
129 static char readbuf[READBUF_SIZE];
130 int parc;
131 int len;
f3e11b1d 132 authd_cmd_handler handler;
0d73e7db
AC
133
134 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
135 {
136 parc = rb_string_to_array(readbuf, parv, MAXPARA);
137
138 if(parc < 1)
139 continue;
140
e23126c8 141 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
f3e11b1d
AC
142 if (handler != NULL)
143 handler(parc, parv);
0d73e7db
AC
144 }
145}
146
147static void
148error_cb(rb_helper *helper)
149{
34b96d7f 150 exit(EX_ERROR);
0d73e7db
AC
151}
152
8da0b2f2 153#ifndef _WIN32
0d73e7db
AC
154static void
155dummy_handler(int sig)
156{
157 return;
158}
159#endif
160
161static void
162setup_signals(void)
163{
8da0b2f2 164#ifndef _WIN32
0d73e7db
AC
165 struct sigaction act;
166
167 act.sa_flags = 0;
168 act.sa_handler = SIG_IGN;
169 sigemptyset(&act.sa_mask);
170 sigaddset(&act.sa_mask, SIGPIPE);
171 sigaddset(&act.sa_mask, SIGALRM);
172#ifdef SIGTRAP
173 sigaddset(&act.sa_mask, SIGTRAP);
174#endif
175
176#ifdef SIGWINCH
177 sigaddset(&act.sa_mask, SIGWINCH);
178 sigaction(SIGWINCH, &act, 0);
179#endif
180 sigaction(SIGPIPE, &act, 0);
181#ifdef SIGTRAP
182 sigaction(SIGTRAP, &act, 0);
183#endif
184
185 act.sa_handler = dummy_handler;
186 sigaction(SIGALRM, &act, 0);
187#endif
188}
189
dfd7d4b1
EM
190static void
191do_exit(void)
192{
193 destroy_providers();
194}
195
0d73e7db
AC
196int
197main(int argc, char *argv[])
198{
199 setup_signals();
200
201 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
202 if(authd_helper == NULL)
203 {
204 fprintf(stderr, "authd is not meant to be invoked by end users\n");
34b96d7f 205 exit(EX_ERROR);
0d73e7db
AC
206 }
207
8cf45447
AC
208 rb_set_time();
209 setup_signals();
a51487e0 210
f956cb0f 211 authd_option_handlers = rb_dictionary_create("authd options handlers", rb_strcasecmp);
a51487e0 212
8cf45447 213 init_resolver();
a16b484f 214 init_providers();
8cf45447
AC
215 rb_init_prng(NULL, RB_PRNG_DEFAULT);
216
dfd7d4b1 217 atexit(do_exit);
0d73e7db 218
dfd7d4b1 219 rb_helper_loop(authd_helper, 0);
0da2a404 220
0d73e7db
AC
221 return 0;
222}