]> jfr.im git - solanum.git/blame - authd/provider.c
Name the fallback strncasecmp properly [ci skip]
[solanum.git] / authd / provider.c
CommitLineData
2b0cc3d3
EM
1/* authd/provider.c - authentication provider framework
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
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
89d22b9a
EM
21/* The basic design here is to have "authentication providers" that do things
22 * like query ident and blacklists and even open proxies.
2b0cc3d3 23 *
f7b37c1d
EM
24 * Providers are registered in the auth_providers linked list. It is planned to
25 * use a bitmap to store provider ID's later.
2b0cc3d3 26 *
89d22b9a
EM
27 * Providers can either return failure immediately, immediate acceptance, or do
28 * work in the background (calling set_provider to signal this).
2b0cc3d3 29 *
3e875f62
EM
30 * Provider-specific data for each client can be kept in an index of the data
31 * struct member (using the provider's ID).
2b0cc3d3
EM
32 *
33 * All providers must implement at a minimum a perform_provider function. You
34 * don't have to implement the others if you don't need them.
35 *
36 * Providers may kick clients off by rejecting them. Upon rejection, all
37 * providers are cancelled. They can also unconditionally accept them.
38 *
39 * When a provider is done and is neutral on accepting/rejecting a client, it
40 * should call provider_done. Do NOT call this if you have accepted or rejected
41 * the client.
42 *
89d22b9a
EM
43 * Eventually, stuff like *:line handling will be moved here, but that means we
44 * have to talk to bandb directly first.
45 *
2b0cc3d3
EM
46 * --Elizafox, 9 March 2016
47 */
48
52d49164 49#include "stdinc.h"
46d17a88 50#include "rb_dictionary.h"
2b0cc3d3
EM
51#include "authd.h"
52#include "provider.h"
db821ee9 53#include "notice.h"
2b0cc3d3 54
15c49abb
EM
55static EVH provider_timeout_event;
56
aba29d5a 57rb_dictionary *auth_clients;
731d1289 58rb_dictionary *auth_providers; /* Referenced by name */
2b0cc3d3 59
731d1289
EM
60static rb_dlink_list free_pids;
61static uint32_t pid;
15c49abb
EM
62static struct ev_entry *timeout_ev;
63
731d1289
EM
64/* Initalise all providers */
65void
66init_providers(void)
67{
68 auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp);
69 auth_providers = rb_dictionary_create("auth providers", strcmp);
70 timeout_ev = rb_event_addish("provider_timeout_event", provider_timeout_event, NULL, 1);
71
72 load_provider(&rdns_provider);
73 load_provider(&ident_provider);
74 load_provider(&blacklist_provider);
75 load_provider(&opm_provider);
76}
77
78/* Terminate all providers */
79void
80destroy_providers(void)
81{
82 rb_dlink_node *ptr;
83 rb_dictionary_iter iter;
84 struct auth_client *auth;
85 struct auth_provider *provider;
86
87 /* Cancel outstanding connections */
88 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
89 {
90 /* TBD - is this the right thing? */
4434f375
EM
91 reject_client(auth, UINT32_MAX, "destroy",
92 "Authentication system is down... try reconnecting in a few seconds");
731d1289
EM
93 }
94
95 RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
96 {
97 if(provider->destroy)
98 provider->destroy();
99 }
100
101 rb_dictionary_destroy(auth_clients, NULL, NULL);
102 rb_dictionary_destroy(auth_providers, NULL, NULL);
103 rb_event_delete(timeout_ev);
104}
105
2b0cc3d3 106/* Load a provider */
60374ac9
EM
107void
108load_provider(struct auth_provider *provider)
2b0cc3d3 109{
731d1289
EM
110 /* Assign a PID */
111 if(rb_dlink_list_length(&free_pids) > 0)
b2ede1aa 112 {
731d1289
EM
113 /* use the free list */
114 provider->id = RB_POINTER_TO_UINT(free_pids.head->data);
115 rb_dlinkDestroy(free_pids.head, &free_pids);
b2ede1aa 116 }
731d1289 117 else
4434f375
EM
118 {
119 if(pid == UINT32_MAX)
120 {
121 /* If this happens, well, I don't know what to say. Probably a bug.
122 * In any case, UINT32_MAX is a special sentinel. */
123 warn_opers(L_WARN, "Cannot load additional provider, max reached!");
124 return;
125 }
126
731d1289 127 provider->id = pid++;
4434f375 128 }
3e875f62 129
a51487e0
EM
130 if(provider->opt_handlers != NULL)
131 {
132 struct auth_opts_handler *handler;
133
134 for(handler = provider->opt_handlers; handler->option != NULL; handler++)
135 rb_dictionary_add(authd_option_handlers, handler->option, handler);
136 }
137
ee7f9271
EM
138 if(provider->stats_handler.letter != '\0')
139 authd_stat_handlers[provider->stats_handler.letter] = provider->stats_handler.handler;
140
9f9ab5c2
EM
141 if(provider->init != NULL)
142 provider->init();
143
731d1289 144 rb_dictionary_add(auth_providers, provider->name, provider);
2b0cc3d3
EM
145}
146
60374ac9
EM
147void
148unload_provider(struct auth_provider *provider)
2b0cc3d3 149{
a51487e0
EM
150 if(provider->opt_handlers != NULL)
151 {
152 struct auth_opts_handler *handler;
153
154 for(handler = provider->opt_handlers; handler->option != NULL; handler++)
155 rb_dictionary_delete(authd_option_handlers, handler->option);
156 }
ee7f9271
EM
157
158 if(provider->stats_handler.letter != '\0')
159 authd_stat_handlers[provider->stats_handler.letter] = NULL;
160
9f9ab5c2
EM
161 if(provider->destroy != NULL)
162 provider->destroy();
163
731d1289 164 rb_dictionary_delete(auth_providers, provider->name);
2b0cc3d3 165
731d1289
EM
166 /* Reclaim ID */
167 rb_dlinkAddAlloc(RB_UINT_TO_POINTER(provider->id), &free_pids);
2b0cc3d3
EM
168}
169
2b0cc3d3 170
4434f375
EM
171/* Cancel outstanding providers for a client (if any) and free the auth instance
172 * WARNING: do not use auth instance after calling! */
60374ac9
EM
173void
174cancel_providers(struct auth_client *auth)
2b0cc3d3 175{
4434f375 176 if(auth->refcount > 0)
2b0cc3d3 177 {
4434f375
EM
178 rb_dictionary_iter iter;
179 struct auth_provider *provider;
180
181 RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
182 {
183 if(provider->cancel != NULL && is_provider_running(auth, provider->id))
184 /* Cancel if required */
185 provider->cancel(auth);
186 }
2b0cc3d3 187 }
46d17a88 188
3e875f62 189 rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid));
a68d9a2b 190 rb_free(auth->data);
3e875f62 191 rb_free(auth);
2b0cc3d3
EM
192}
193
45e6c746
EM
194/* Provider is done
195 * WARNING: do not use auth instance after calling! */
60374ac9 196void
731d1289 197provider_done(struct auth_client *auth, uint32_t id)
2b0cc3d3 198{
731d1289 199 rb_dictionary_iter iter;
2b0cc3d3
EM
200 struct auth_provider *provider;
201
4434f375
EM
202 lrb_assert(is_provider_running(auth, id));
203 lrb_assert(id != UINT32_MAX);
204 lrb_assert(id <= pid);
205
206 set_provider_done(auth, id);
207
208 if(auth->refcount == 0 && !auth->providers_starting)
2b0cc3d3 209 {
4434f375 210 /* All done */
45e6c746 211 accept_client(auth, id);
2b0cc3d3
EM
212 return;
213 }
214
731d1289 215 RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
2b0cc3d3 216 {
376ae2e2 217 if(provider->completed != NULL && is_provider_running(auth, provider->id))
2b0cc3d3
EM
218 /* Notify pending clients who asked for it */
219 provider->completed(auth, id);
220 }
221}
222
4434f375 223/* Reject a client and cancel any outstanding providers
45e6c746 224 * WARNING: do not use auth instance after calling! */
60374ac9 225void
731d1289 226reject_client(struct auth_client *auth, uint32_t id, const char *data, const char *fmt, ...)
2b0cc3d3 227{
45e6c746 228 unsigned int refcount = auth->refcount;
64afc358
EM
229 char buf[BUFSIZE];
230 va_list args;
2b0cc3d3 231
a5ab1062 232 va_start(args, fmt);
64afc358
EM
233 vsnprintf(buf, sizeof(buf), fmt, args);
234 va_end(args);
235
3ad21f61
EM
236 /* We send back username and hostname in case ircd wants to overrule our decision.
237 * In the future this may not be the case.
238 * --Elizafox
239 */
731d1289
EM
240 rb_helper_write(authd_helper, "R %x %c %s %s %s :%s",
241 auth->cid, auth->data[id].provider->letter,
242 auth->username, auth->hostname,
4434f375 243 data == NULL ? "*" : data, buf);
2b0cc3d3 244
4434f375
EM
245 if(id != UINT32_MAX)
246 set_provider_done(auth, id);
247
248 cancel_providers(auth);
2b0cc3d3
EM
249}
250
4434f375 251/* Accept a client and cancel outstanding providers if any
45e6c746 252 * WARNING: do not use auth instance after calling! */
60374ac9 253void
731d1289 254accept_client(struct auth_client *auth, uint32_t id)
2b0cc3d3 255{
45e6c746
EM
256 unsigned int refcount = auth->refcount;
257
2b0cc3d3
EM
258 rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname);
259
4434f375
EM
260 if(id != UINT32_MAX)
261 set_provider_done(auth, id);
262
263 cancel_providers(auth);
2b0cc3d3
EM
264}
265
2b0cc3d3 266/* Begin authenticating user */
60374ac9
EM
267static void
268start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
2b0cc3d3
EM
269{
270 struct auth_provider *provider;
3e875f62 271 struct auth_client *auth = rb_malloc(sizeof(struct auth_client));
2b0cc3d3 272 long lcid = strtol(cid, NULL, 16);
731d1289 273 rb_dictionary_iter iter;
2b0cc3d3 274
3e875f62 275 if(lcid >= UINT32_MAX)
2b0cc3d3
EM
276 return;
277
3e875f62 278 auth->cid = (uint32_t)lcid;
2b0cc3d3 279
05fdc030
EM
280 if(rb_dictionary_find(auth_clients, RB_UINT_TO_POINTER(auth->cid)) == NULL)
281 rb_dictionary_add(auth_clients, RB_UINT_TO_POINTER(auth->cid), auth);
282 else
283 {
c23f9755
EM
284 warn_opers(L_CRIT, "provider: duplicate client added via start_auth: %x", auth->cid);
285 exit(EX_PROVIDER_ERROR);
05fdc030
EM
286 }
287
2b0cc3d3
EM
288 rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
289 auth->l_port = (uint16_t)atoi(l_port); /* should be safe */
32f8c78b 290 (void) rb_inet_pton_sock(l_ip, (struct sockaddr *)&auth->l_addr);
d86692fa 291 SET_SS_PORT(&auth->l_addr, htons(auth->l_port));
2b0cc3d3
EM
292
293 rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip));
294 auth->c_port = (uint16_t)atoi(c_port);
32f8c78b 295 (void) rb_inet_pton_sock(c_ip, (struct sockaddr *)&auth->c_addr);
d86692fa 296 SET_SS_PORT(&auth->c_addr, htons(auth->c_port));
2b0cc3d3 297
1345a41d
EM
298 rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
299 rb_strlcpy(auth->username, "*", sizeof(auth->username));
300
731d1289 301 auth->data = rb_malloc(rb_dictionary_size(auth_providers) *
a68d9a2b 302 sizeof(struct auth_client_data));
0cff7adb 303
05fdc030 304 auth->providers_starting = true;
731d1289 305 RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
2b0cc3d3 306 {
731d1289 307 auth->data[provider->id].provider = provider;
2b0cc3d3 308
05fdc030
EM
309 lrb_assert(provider->start != NULL);
310
2b0cc3d3
EM
311 /* Execute providers */
312 if(!provider->start(auth))
2b0cc3d3 313 /* Rejected immediately */
2b0cc3d3 314 return;
2b0cc3d3 315 }
05fdc030 316 auth->providers_starting = false;
2b0cc3d3
EM
317
318 /* If no providers are running, accept the client */
4434f375
EM
319 if(auth->refcount == 0)
320 accept_client(auth, UINT32_MAX);
2b0cc3d3
EM
321}
322
323/* Callback for the initiation */
60374ac9
EM
324void
325handle_new_connection(int parc, char *parv[])
2b0cc3d3 326{
0cff7adb 327 if(parc < 6)
b2ede1aa 328 {
c23f9755
EM
329 warn_opers(L_CRIT, "provider: received too few params for new connection (6 expected, got %d)", parc);
330 exit(EX_PROVIDER_ERROR);
b2ede1aa 331 }
2b0cc3d3
EM
332
333 start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
334}
60374ac9
EM
335
336void
337handle_cancel_connection(int parc, char *parv[])
338{
339 struct auth_client *auth;
340 long lcid;
341
342 if(parc < 2)
343 {
c23f9755
EM
344 warn_opers(L_CRIT, "provider: received too few params for new connection (2 expected, got %d)", parc);
345 exit(EX_PROVIDER_ERROR);
60374ac9
EM
346 }
347
348 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX)
349 {
c23f9755
EM
350 warn_opers(L_CRIT, "provider: got a request to cancel a connection that can't exist: %lx", lcid);
351 exit(EX_PROVIDER_ERROR);
60374ac9
EM
352 }
353
354 if((auth = rb_dictionary_retrieve(auth_clients, RB_UINT_TO_POINTER((uint32_t)lcid))) == NULL)
355 {
5cbfed54
EM
356 /* This could happen as a race if we've accepted/rejected but they cancel, so don't die here.
357 * --Elizafox */
a3b112f4 358 return;
60374ac9
EM
359 }
360
361 cancel_providers(auth);
362}
15c49abb
EM
363
364static void
365provider_timeout_event(void *notused __unused)
366{
367 struct auth_client *auth;
368 rb_dictionary_iter iter;
369 const time_t curtime = rb_current_time();
370
371 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
372 {
731d1289
EM
373 rb_dictionary_iter iter2;
374 struct auth_provider *provider;
15c49abb 375
731d1289 376 RB_DICTIONARY_FOREACH(provider, &iter2, auth_providers)
15c49abb 377 {
a68d9a2b 378 const time_t timeout = get_provider_timeout(auth, provider->id);
15c49abb 379
376ae2e2 380 if(is_provider_running(auth, provider->id) && provider->timeout != NULL &&
c23f9755 381 timeout > 0 && timeout < curtime)
15c49abb
EM
382 {
383 provider->timeout(auth);
384 }
385 }
386 }
387}