]> jfr.im git - solanum.git/blame - authd/authd.c
Merge branch 'authd-framework' of github.com:charybdis-ircd/charybdis into authd...
[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;
56
57 if(parc < 3)
b2ede1aa
EM
58 {
59 warn_opers(L_CRIT, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc);
394b8dde 60 return;
b2ede1aa 61 }
394b8dde 62
e23126c8 63 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
394b8dde
EM
64 return;
65
66 handler(parv[1], parv[2][0]);
67}
68
a51487e0
EM
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
f5586c3a 92 handler->handler(parv[1], parc - 2, (const char **)&parv[2]);
a51487e0
EM
93}
94
6445c1cf
EM
95static void
96handle_reload(int parc, char *parv[])
97{
98 authd_reload_handler handler;
99
100 if(parc < 2)
b2ede1aa 101 {
122ae255 102 /* Reload all handlers */
a51487e0 103 for(size_t i = 0; i < 256; i++)
c63cd21e
AC
104 {
105 if ((handler = authd_reload_handlers[(unsigned char) i]) != NULL)
106 handler(parv[1][0]);
107 }
122ae255 108
6445c1cf 109 return;
b2ede1aa 110 }
6445c1cf 111
e23126c8 112 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
6445c1cf
EM
113 return;
114
115 handler(parv[1][0]);
116}
117
0d73e7db
AC
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;
f3e11b1d 125 authd_cmd_handler handler;
0d73e7db
AC
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
e23126c8 134 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
f3e11b1d
AC
135 if (handler != NULL)
136 handler(parc, parv);
0d73e7db
AC
137 }
138}
139
140static void
141error_cb(rb_helper *helper)
142{
143 exit(1);
144}
145
8da0b2f2 146#ifndef _WIN32
0d73e7db
AC
147static void
148dummy_handler(int sig)
149{
150 return;
151}
152#endif
153
154static void
155setup_signals(void)
156{
8da0b2f2 157#ifndef _WIN32
0d73e7db
AC
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
8cf45447
AC
195 rb_set_time();
196 setup_signals();
a51487e0
EM
197
198 authd_option_handlers = rb_dictionary_create("authd options handlers", strcasecmp);
199
8cf45447 200 init_resolver();
a16b484f 201 init_providers();
8cf45447
AC
202 rb_init_prng(NULL, RB_PRNG_DEFAULT);
203
0d73e7db
AC
204 rb_helper_loop(authd_helper, 0);
205
0da2a404
EM
206 destroy_providers();
207
0d73e7db
AC
208 return 0;
209}