]>
Commit | Line | Data |
---|---|---|
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 | 21 | /* The basic design here is to have "authentication providers" that do things |
a389de2a | 22 | * like query ident and DNSBLs 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" |
a71b65b1 | 50 | #include "rb_dictionary.h" |
d955cd9f | 51 | #include "rb_lib.h" |
2b0cc3d3 EM |
52 | #include "authd.h" |
53 | #include "provider.h" | |
db821ee9 | 54 | #include "notice.h" |
2b0cc3d3 | 55 | |
15c49abb EM |
56 | static EVH provider_timeout_event; |
57 | ||
a71b65b1 | 58 | rb_dictionary *auth_clients; |
d955cd9f | 59 | rb_dlink_list auth_providers; |
2b0cc3d3 | 60 | |
731d1289 | 61 | static rb_dlink_list free_pids; |
075d4d56 | 62 | static uint32_t allocated_pids; |
15c49abb EM |
63 | static struct ev_entry *timeout_ev; |
64 | ||
f21ef0ce SA |
65 | /* Set a provider's raw status */ |
66 | static inline void | |
67 | set_provider_status(struct auth_client *auth, uint32_t provider, provider_status_t status) | |
68 | { | |
69 | auth->data[provider].status = status; | |
70 | } | |
71 | ||
72 | /* Set the provider as running */ | |
73 | static inline void | |
74 | set_provider_running(struct auth_client *auth, uint32_t provider) | |
75 | { | |
76 | auth->providers_active++; | |
77 | set_provider_status(auth, provider, PROVIDER_STATUS_RUNNING); | |
78 | } | |
79 | ||
80 | /* Provider is no longer operating on this auth client */ | |
81 | static inline void | |
82 | set_provider_done(struct auth_client *auth, uint32_t provider) | |
83 | { | |
84 | set_provider_status(auth, provider, PROVIDER_STATUS_DONE); | |
85 | auth->providers_active--; | |
86 | } | |
87 | ||
731d1289 EM |
88 | /* Initalise all providers */ |
89 | void | |
90 | init_providers(void) | |
91 | { | |
a71b65b1 | 92 | auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp); |
731d1289 EM |
93 | timeout_ev = rb_event_addish("provider_timeout_event", provider_timeout_event, NULL, 1); |
94 | ||
f21ef0ce | 95 | /* FIXME must be started before rdns/ident to receive completion notification from them */ |
a389de2a | 96 | load_provider(&dnsbl_provider); |
731d1289 | 97 | load_provider(&opm_provider); |
f21ef0ce | 98 | |
a389de2a | 99 | /* FIXME must be started after dnsbl/opm in case of early completion notifications */ |
f21ef0ce SA |
100 | load_provider(&rdns_provider); |
101 | load_provider(&ident_provider); | |
731d1289 EM |
102 | } |
103 | ||
104 | /* Terminate all providers */ | |
105 | void | |
106 | destroy_providers(void) | |
107 | { | |
d955cd9f | 108 | rb_dlink_node *ptr, *nptr; |
a71b65b1 AC |
109 | rb_dictionary_iter iter; |
110 | struct auth_client *auth; | |
731d1289 EM |
111 | |
112 | /* Cancel outstanding connections */ | |
a71b65b1 | 113 | RB_DICTIONARY_FOREACH(auth, &iter, auth_clients) |
731d1289 | 114 | { |
a5f52774 SA |
115 | auth_client_ref(auth); |
116 | ||
a71b65b1 | 117 | /* TBD - is this the right thing? */ |
4434f375 EM |
118 | reject_client(auth, UINT32_MAX, "destroy", |
119 | "Authentication system is down... try reconnecting in a few seconds"); | |
a5f52774 SA |
120 | |
121 | auth_client_unref(auth); | |
731d1289 EM |
122 | } |
123 | ||
d955cd9f | 124 | RB_DLINK_FOREACH_SAFE(ptr, nptr, auth_providers.head) |
731d1289 | 125 | { |
d955cd9f SA |
126 | struct auth_provider *provider = ptr->data; |
127 | ||
731d1289 EM |
128 | if(provider->destroy) |
129 | provider->destroy(); | |
d955cd9f SA |
130 | |
131 | rb_dlinkDelete(ptr, &auth_providers); | |
731d1289 EM |
132 | } |
133 | ||
a71b65b1 | 134 | rb_dictionary_destroy(auth_clients, NULL, NULL); |
731d1289 EM |
135 | rb_event_delete(timeout_ev); |
136 | } | |
137 | ||
2b0cc3d3 | 138 | /* Load a provider */ |
60374ac9 EM |
139 | void |
140 | load_provider(struct auth_provider *provider) | |
2b0cc3d3 | 141 | { |
731d1289 EM |
142 | /* Assign a PID */ |
143 | if(rb_dlink_list_length(&free_pids) > 0) | |
b2ede1aa | 144 | { |
731d1289 EM |
145 | /* use the free list */ |
146 | provider->id = RB_POINTER_TO_UINT(free_pids.head->data); | |
147 | rb_dlinkDestroy(free_pids.head, &free_pids); | |
b2ede1aa | 148 | } |
731d1289 | 149 | else |
4434f375 | 150 | { |
075d4d56 | 151 | if(allocated_pids == MAX_PROVIDERS || allocated_pids == UINT32_MAX) |
4434f375 | 152 | { |
4434f375 EM |
153 | warn_opers(L_WARN, "Cannot load additional provider, max reached!"); |
154 | return; | |
155 | } | |
156 | ||
075d4d56 | 157 | provider->id = allocated_pids++; |
4434f375 | 158 | } |
3e875f62 | 159 | |
a51487e0 EM |
160 | if(provider->opt_handlers != NULL) |
161 | { | |
162 | struct auth_opts_handler *handler; | |
163 | ||
164 | for(handler = provider->opt_handlers; handler->option != NULL; handler++) | |
165 | rb_dictionary_add(authd_option_handlers, handler->option, handler); | |
166 | } | |
167 | ||
ee7f9271 | 168 | if(provider->stats_handler.letter != '\0') |
1729f46e | 169 | authd_stat_handlers[(unsigned char)provider->stats_handler.letter] = provider->stats_handler.handler; |
ee7f9271 | 170 | |
9f9ab5c2 EM |
171 | if(provider->init != NULL) |
172 | provider->init(); | |
173 | ||
d955cd9f | 174 | rb_dlinkAdd(provider, &provider->node, &auth_providers); |
2b0cc3d3 EM |
175 | } |
176 | ||
60374ac9 EM |
177 | void |
178 | unload_provider(struct auth_provider *provider) | |
2b0cc3d3 | 179 | { |
a51487e0 EM |
180 | if(provider->opt_handlers != NULL) |
181 | { | |
182 | struct auth_opts_handler *handler; | |
183 | ||
184 | for(handler = provider->opt_handlers; handler->option != NULL; handler++) | |
185 | rb_dictionary_delete(authd_option_handlers, handler->option); | |
186 | } | |
ee7f9271 EM |
187 | |
188 | if(provider->stats_handler.letter != '\0') | |
1729f46e | 189 | authd_stat_handlers[(unsigned char)provider->stats_handler.letter] = NULL; |
ee7f9271 | 190 | |
9f9ab5c2 EM |
191 | if(provider->destroy != NULL) |
192 | provider->destroy(); | |
193 | ||
d955cd9f | 194 | rb_dlinkDelete(&provider->node, &auth_providers); |
2b0cc3d3 | 195 | |
731d1289 EM |
196 | /* Reclaim ID */ |
197 | rb_dlinkAddAlloc(RB_UINT_TO_POINTER(provider->id), &free_pids); | |
2b0cc3d3 EM |
198 | } |
199 | ||
b585278b AC |
200 | void |
201 | auth_client_free(struct auth_client *auth) | |
202 | { | |
203 | rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid)); | |
204 | rb_free(auth->data); | |
205 | rb_free(auth); | |
206 | } | |
2b0cc3d3 | 207 | |
b585278b | 208 | /* Cancel outstanding providers for a client (if any). */ |
60374ac9 EM |
209 | void |
210 | cancel_providers(struct auth_client *auth) | |
2b0cc3d3 | 211 | { |
9f928dc5 SA |
212 | if(auth->providers_cancelled) |
213 | return; | |
214 | ||
215 | auth->providers_cancelled = true; | |
216 | ||
a5f52774 | 217 | if(auth->providers_active > 0) |
2b0cc3d3 | 218 | { |
d955cd9f | 219 | rb_dlink_node *ptr; |
4434f375 | 220 | |
d955cd9f | 221 | RB_DLINK_FOREACH(ptr, auth_providers.head) |
4434f375 | 222 | { |
d955cd9f SA |
223 | struct auth_provider *provider = ptr->data; |
224 | ||
4434f375 EM |
225 | if(provider->cancel != NULL && is_provider_running(auth, provider->id)) |
226 | /* Cancel if required */ | |
227 | provider->cancel(auth); | |
228 | } | |
2b0cc3d3 EM |
229 | } |
230 | } | |
231 | ||
84d0b55e | 232 | /* Provider is done */ |
60374ac9 | 233 | void |
731d1289 | 234 | provider_done(struct auth_client *auth, uint32_t id) |
2b0cc3d3 | 235 | { |
d955cd9f | 236 | rb_dlink_node *ptr; |
2b0cc3d3 | 237 | |
4434f375 EM |
238 | lrb_assert(is_provider_running(auth, id)); |
239 | lrb_assert(id != UINT32_MAX); | |
075d4d56 | 240 | lrb_assert(id < allocated_pids); |
4434f375 EM |
241 | |
242 | set_provider_done(auth, id); | |
243 | ||
a5f52774 | 244 | if(auth->providers_active == 0 && !auth->providers_starting) |
2b0cc3d3 | 245 | { |
4434f375 | 246 | /* All done */ |
2f598dac | 247 | accept_client(auth); |
2b0cc3d3 EM |
248 | return; |
249 | } | |
250 | ||
d955cd9f | 251 | RB_DLINK_FOREACH(ptr, auth_providers.head) |
2b0cc3d3 | 252 | { |
d955cd9f SA |
253 | struct auth_provider *provider = ptr->data; |
254 | ||
376ae2e2 | 255 | if(provider->completed != NULL && is_provider_running(auth, provider->id)) |
2b0cc3d3 EM |
256 | /* Notify pending clients who asked for it */ |
257 | provider->completed(auth, id); | |
258 | } | |
259 | } | |
260 | ||
84d0b55e | 261 | /* Reject a client and cancel any outstanding providers */ |
60374ac9 | 262 | void |
731d1289 | 263 | reject_client(struct auth_client *auth, uint32_t id, const char *data, const char *fmt, ...) |
2b0cc3d3 | 264 | { |
64afc358 EM |
265 | char buf[BUFSIZE]; |
266 | va_list args; | |
2b0cc3d3 | 267 | |
a5ab1062 | 268 | va_start(args, fmt); |
64afc358 EM |
269 | vsnprintf(buf, sizeof(buf), fmt, args); |
270 | va_end(args); | |
271 | ||
3ad21f61 EM |
272 | /* We send back username and hostname in case ircd wants to overrule our decision. |
273 | * In the future this may not be the case. | |
274 | * --Elizafox | |
275 | */ | |
731d1289 | 276 | rb_helper_write(authd_helper, "R %x %c %s %s %s :%s", |
84d0b55e | 277 | auth->cid, id != UINT32_MAX ? auth->data[id].provider->letter : '*', |
731d1289 | 278 | auth->username, auth->hostname, |
4434f375 | 279 | data == NULL ? "*" : data, buf); |
2b0cc3d3 | 280 | |
4434f375 EM |
281 | if(id != UINT32_MAX) |
282 | set_provider_done(auth, id); | |
283 | ||
284 | cancel_providers(auth); | |
2b0cc3d3 EM |
285 | } |
286 | ||
2f598dac | 287 | /* Accept a client and cancel outstanding providers if any */ |
60374ac9 | 288 | void |
2f598dac | 289 | accept_client(struct auth_client *auth) |
2b0cc3d3 | 290 | { |
2b0cc3d3 | 291 | rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname); |
4434f375 | 292 | cancel_providers(auth); |
2b0cc3d3 EM |
293 | } |
294 | ||
2b0cc3d3 | 295 | /* Begin authenticating user */ |
60374ac9 | 296 | static void |
c6ad9b0c | 297 | start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port, const char *protocol) |
2b0cc3d3 | 298 | { |
a4da4fe5 | 299 | struct auth_client *auth; |
2392770f | 300 | unsigned long long lcid = strtoull(cid, NULL, 16); |
d955cd9f | 301 | rb_dlink_node *ptr; |
2b0cc3d3 | 302 | |
2392770f | 303 | if(lcid == 0 || lcid > UINT32_MAX) |
2b0cc3d3 EM |
304 | return; |
305 | ||
a4da4fe5 | 306 | auth = rb_malloc(sizeof(struct auth_client)); |
a5f52774 | 307 | auth_client_ref(auth); |
3e875f62 | 308 | auth->cid = (uint32_t)lcid; |
2b0cc3d3 | 309 | |
a71b65b1 AC |
310 | if(rb_dictionary_find(auth_clients, RB_UINT_TO_POINTER(auth->cid)) == NULL) |
311 | rb_dictionary_add(auth_clients, RB_UINT_TO_POINTER(auth->cid), auth); | |
05fdc030 EM |
312 | else |
313 | { | |
2392770f | 314 | warn_opers(L_CRIT, "provider: duplicate client added via start_auth: %s", cid); |
c23f9755 | 315 | exit(EX_PROVIDER_ERROR); |
05fdc030 EM |
316 | } |
317 | ||
c6ad9b0c SA |
318 | auth->protocol = strtoull(protocol, NULL, 16); |
319 | ||
2b0cc3d3 EM |
320 | rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip)); |
321 | auth->l_port = (uint16_t)atoi(l_port); /* should be safe */ | |
17809d2d | 322 | (void) rb_inet_pton_sock(l_ip, &auth->l_addr); |
d86692fa | 323 | SET_SS_PORT(&auth->l_addr, htons(auth->l_port)); |
2b0cc3d3 EM |
324 | |
325 | rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip)); | |
326 | auth->c_port = (uint16_t)atoi(c_port); | |
17809d2d | 327 | (void) rb_inet_pton_sock(c_ip, &auth->c_addr); |
d86692fa | 328 | SET_SS_PORT(&auth->c_addr, htons(auth->c_port)); |
2b0cc3d3 | 329 | |
1345a41d EM |
330 | rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname)); |
331 | rb_strlcpy(auth->username, "*", sizeof(auth->username)); | |
332 | ||
075d4d56 | 333 | auth->data = rb_malloc(allocated_pids * sizeof(struct auth_client_data)); |
0cff7adb | 334 | |
05fdc030 | 335 | auth->providers_starting = true; |
d955cd9f | 336 | RB_DLINK_FOREACH(ptr, auth_providers.head) |
2b0cc3d3 | 337 | { |
d955cd9f SA |
338 | struct auth_provider *provider = ptr->data; |
339 | ||
731d1289 | 340 | auth->data[provider->id].provider = provider; |
2b0cc3d3 | 341 | |
05fdc030 EM |
342 | lrb_assert(provider->start != NULL); |
343 | ||
2b0cc3d3 | 344 | /* Execute providers */ |
f21ef0ce | 345 | set_provider_running(auth, provider->id); |
2b0cc3d3 | 346 | if(!provider->start(auth)) |
2b0cc3d3 | 347 | /* Rejected immediately */ |
a5f52774 | 348 | goto done; |
9f928dc5 SA |
349 | |
350 | if(auth->providers_cancelled) | |
351 | break; | |
2b0cc3d3 | 352 | } |
05fdc030 | 353 | auth->providers_starting = false; |
2b0cc3d3 EM |
354 | |
355 | /* If no providers are running, accept the client */ | |
a5f52774 | 356 | if(auth->providers_active == 0) |
2f598dac | 357 | accept_client(auth); |
a5f52774 SA |
358 | |
359 | done: | |
360 | auth_client_unref(auth); | |
2b0cc3d3 EM |
361 | } |
362 | ||
363 | /* Callback for the initiation */ | |
60374ac9 EM |
364 | void |
365 | handle_new_connection(int parc, char *parv[]) | |
2b0cc3d3 | 366 | { |
c6ad9b0c | 367 | if (parc < 6) { |
c23f9755 EM |
368 | warn_opers(L_CRIT, "provider: received too few params for new connection (6 expected, got %d)", parc); |
369 | exit(EX_PROVIDER_ERROR); | |
b2ede1aa | 370 | } |
2b0cc3d3 | 371 | |
c6ad9b0c | 372 | start_auth(parv[1], parv[2], parv[3], parv[4], parv[5], parc > 6 ? parv[6] : "0"); |
2b0cc3d3 | 373 | } |
60374ac9 EM |
374 | |
375 | void | |
376 | handle_cancel_connection(int parc, char *parv[]) | |
377 | { | |
378 | struct auth_client *auth; | |
2392770f | 379 | unsigned long long lcid; |
60374ac9 EM |
380 | |
381 | if(parc < 2) | |
382 | { | |
c23f9755 EM |
383 | warn_opers(L_CRIT, "provider: received too few params for new connection (2 expected, got %d)", parc); |
384 | exit(EX_PROVIDER_ERROR); | |
60374ac9 EM |
385 | } |
386 | ||
2392770f SA |
387 | lcid = strtoull(parv[1], NULL, 16); |
388 | if(lcid == 0 || lcid > UINT32_MAX) | |
60374ac9 | 389 | { |
2392770f | 390 | warn_opers(L_CRIT, "provider: got a request to cancel a connection that can't exist: %s", parv[1]); |
c23f9755 | 391 | exit(EX_PROVIDER_ERROR); |
60374ac9 EM |
392 | } |
393 | ||
a71b65b1 | 394 | if((auth = rb_dictionary_retrieve(auth_clients, RB_UINT_TO_POINTER((uint32_t)lcid))) == NULL) |
60374ac9 | 395 | { |
5cbfed54 EM |
396 | /* This could happen as a race if we've accepted/rejected but they cancel, so don't die here. |
397 | * --Elizafox */ | |
a3b112f4 | 398 | return; |
60374ac9 EM |
399 | } |
400 | ||
a5f52774 | 401 | auth_client_ref(auth); |
60374ac9 | 402 | cancel_providers(auth); |
a5f52774 | 403 | auth_client_unref(auth); |
60374ac9 | 404 | } |
15c49abb EM |
405 | |
406 | static void | |
407 | provider_timeout_event(void *notused __unused) | |
408 | { | |
409 | struct auth_client *auth; | |
a71b65b1 | 410 | rb_dictionary_iter iter; |
15c49abb EM |
411 | const time_t curtime = rb_current_time(); |
412 | ||
a71b65b1 | 413 | RB_DICTIONARY_FOREACH(auth, &iter, auth_clients) |
15c49abb | 414 | { |
d955cd9f | 415 | rb_dlink_node *ptr; |
15c49abb | 416 | |
a5f52774 SA |
417 | auth_client_ref(auth); |
418 | ||
d955cd9f | 419 | RB_DLINK_FOREACH(ptr, auth_providers.head) |
15c49abb | 420 | { |
d955cd9f | 421 | struct auth_provider *provider = ptr->data; |
a68d9a2b | 422 | const time_t timeout = get_provider_timeout(auth, provider->id); |
15c49abb | 423 | |
376ae2e2 | 424 | if(is_provider_running(auth, provider->id) && provider->timeout != NULL && |
c23f9755 | 425 | timeout > 0 && timeout < curtime) |
15c49abb EM |
426 | { |
427 | provider->timeout(auth); | |
428 | } | |
429 | } | |
a5f52774 SA |
430 | |
431 | auth_client_unref(auth); | |
15c49abb EM |
432 | } |
433 | } |