]> jfr.im git - solanum.git/blob - authd/authd.c
Merge branch 'master' of github.com:charybdis-ircd/charybdis
[solanum.git] / authd / authd.c
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
28 static void handle_reload(int parc, char *parv[]);
29 static void handle_stat(int parc, char *parv[]);
30 static void handle_options(int parc, char *parv[]);
31
32 rb_helper *authd_helper = NULL;
33 authd_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
42 authd_stat_handler authd_stat_handlers[256] = {
43 ['D'] = enumerate_nameservers,
44 };
45
46 authd_reload_handler authd_reload_handlers[256] = {
47 ['D'] = reload_nameservers,
48 };
49
50 rb_dictionary *authd_option_handlers;
51
52 static void
53 handle_stat(int parc, char *parv[])
54 {
55 authd_stat_handler handler;
56 long lrid;
57
58 if(parc < 3)
59 {
60 warn_opers(L_CRIT, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc);
61 return;
62 }
63
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
70 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
71 return;
72
73 handler((uint32_t)lrid, parv[2][0]);
74 }
75
76 static void
77 handle_options(int parc, char *parv[])
78 {
79 struct auth_opts_handler *handler;
80
81 if(parc < 3)
82 {
83 warn_opers(L_CRIT, "BUG: handle_options received too few parameters (at least 3 expected, got %d)", parc);
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
99 handler->handler(parv[1], parc - 2, (const char **)&parv[2]);
100 }
101
102 static void
103 handle_reload(int parc, char *parv[])
104 {
105 authd_reload_handler handler;
106
107 if(parc <= 2)
108 {
109 /* Reload all handlers */
110 for(size_t i = 0; i < 256; i++)
111 {
112 if ((handler = authd_reload_handlers[(unsigned char) i]) != NULL)
113 handler('\0');
114 }
115
116 return;
117 }
118
119 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
120 return;
121
122 handler(parv[1][0]);
123 }
124
125 static void
126 parse_request(rb_helper *helper)
127 {
128 static char *parv[MAXPARA + 1];
129 static char readbuf[READBUF_SIZE];
130 int parc;
131 int len;
132 authd_cmd_handler handler;
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
141 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
142 if (handler != NULL)
143 handler(parc, parv);
144 }
145 }
146
147 static void
148 error_cb(rb_helper *helper)
149 {
150 exit(EX_ERROR);
151 }
152
153 #ifndef _WIN32
154 static void
155 dummy_handler(int sig)
156 {
157 return;
158 }
159 #endif
160
161 static void
162 setup_signals(void)
163 {
164 #ifndef _WIN32
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
190 static void
191 do_exit(void)
192 {
193 destroy_providers();
194 }
195
196 int
197 main(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");
205 exit(EX_ERROR);
206 }
207
208 rb_set_time();
209 setup_signals();
210
211 authd_option_handlers = rb_dictionary_create("authd options handlers", strcasecmp);
212
213 init_resolver();
214 init_providers();
215 rb_init_prng(NULL, RB_PRNG_DEFAULT);
216
217 atexit(do_exit);
218
219 rb_helper_loop(authd_helper, 0);
220
221 return 0;
222 }