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