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