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