1 /* authd/provider.c - authentication provider framework
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
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.
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.
21 /* The basic design here is to have "authentication providers" that do things
22 * like query ident and blacklists and even open proxies.
24 * Providers are registered in the auth_providers linked list. It is planned to
25 * use a bitmap to store provider ID's later.
27 * Providers can either return failure immediately, immediate acceptance, or do
28 * work in the background (calling set_provider to signal this).
30 * Provider-specific data for each client can be kept in an index of the data
31 * struct member (using the provider's ID).
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.
36 * Providers may kick clients off by rejecting them. Upon rejection, all
37 * providers are cancelled. They can also unconditionally accept them.
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
43 * Eventually, stuff like *:line handling will be moved here, but that means we
44 * have to talk to bandb directly first.
46 * --Elizafox, 9 March 2016
50 #include "rb_dictionary.h"
56 static EVH provider_timeout_event
;
58 rb_dictionary
*auth_clients
;
59 rb_dlink_list auth_providers
;
61 static rb_dlink_list free_pids
;
62 static uint32_t allocated_pids
;
63 static struct ev_entry
*timeout_ev
;
65 /* Set a provider's raw status */
67 set_provider_status(struct auth_client
*auth
, uint32_t provider
, provider_status_t status
)
69 auth
->data
[provider
].status
= status
;
72 /* Set the provider as running */
74 set_provider_running(struct auth_client
*auth
, uint32_t provider
)
76 auth
->providers_active
++;
77 set_provider_status(auth
, provider
, PROVIDER_STATUS_RUNNING
);
80 /* Provider is no longer operating on this auth client */
82 set_provider_done(struct auth_client
*auth
, uint32_t provider
)
84 set_provider_status(auth
, provider
, PROVIDER_STATUS_DONE
);
85 auth
->providers_active
--;
88 /* Initalise all providers */
92 auth_clients
= rb_dictionary_create("pending auth clients", rb_uint32cmp
);
93 timeout_ev
= rb_event_addish("provider_timeout_event", provider_timeout_event
, NULL
, 1);
95 /* FIXME must be started before rdns/ident to receive completion notification from them */
96 load_provider(&blacklist_provider
);
97 load_provider(&opm_provider
);
99 /* FIXME must be started after blacklist/opm in case of early completion notifications */
100 load_provider(&rdns_provider
);
101 load_provider(&ident_provider
);
104 /* Terminate all providers */
106 destroy_providers(void)
108 rb_dlink_node
*ptr
, *nptr
;
109 rb_dictionary_iter iter
;
110 struct auth_client
*auth
;
112 /* Cancel outstanding connections */
113 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
115 auth_client_ref(auth
);
117 /* TBD - is this the right thing? */
118 reject_client(auth
, UINT32_MAX
, "destroy",
119 "Authentication system is down... try reconnecting in a few seconds");
121 auth_client_unref(auth
);
124 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, auth_providers
.head
)
126 struct auth_provider
*provider
= ptr
->data
;
128 if(provider
->destroy
)
131 rb_dlinkDelete(ptr
, &auth_providers
);
134 rb_dictionary_destroy(auth_clients
, NULL
, NULL
);
135 rb_event_delete(timeout_ev
);
138 /* Load a provider */
140 load_provider(struct auth_provider
*provider
)
143 if(rb_dlink_list_length(&free_pids
) > 0)
145 /* use the free list */
146 provider
->id
= RB_POINTER_TO_UINT(free_pids
.head
->data
);
147 rb_dlinkDestroy(free_pids
.head
, &free_pids
);
151 if(allocated_pids
== MAX_PROVIDERS
|| allocated_pids
== UINT32_MAX
)
153 warn_opers(L_WARN
, "Cannot load additional provider, max reached!");
157 provider
->id
= allocated_pids
++;
160 if(provider
->opt_handlers
!= NULL
)
162 struct auth_opts_handler
*handler
;
164 for(handler
= provider
->opt_handlers
; handler
->option
!= NULL
; handler
++)
165 rb_dictionary_add(authd_option_handlers
, handler
->option
, handler
);
168 if(provider
->stats_handler
.letter
!= '\0')
169 authd_stat_handlers
[(unsigned char)provider
->stats_handler
.letter
] = provider
->stats_handler
.handler
;
171 if(provider
->init
!= NULL
)
174 rb_dlinkAdd(provider
, &provider
->node
, &auth_providers
);
178 unload_provider(struct auth_provider
*provider
)
180 if(provider
->opt_handlers
!= NULL
)
182 struct auth_opts_handler
*handler
;
184 for(handler
= provider
->opt_handlers
; handler
->option
!= NULL
; handler
++)
185 rb_dictionary_delete(authd_option_handlers
, handler
->option
);
188 if(provider
->stats_handler
.letter
!= '\0')
189 authd_stat_handlers
[(unsigned char)provider
->stats_handler
.letter
] = NULL
;
191 if(provider
->destroy
!= NULL
)
194 rb_dlinkDelete(&provider
->node
, &auth_providers
);
197 rb_dlinkAddAlloc(RB_UINT_TO_POINTER(provider
->id
), &free_pids
);
201 auth_client_free(struct auth_client
*auth
)
203 rb_dictionary_delete(auth_clients
, RB_UINT_TO_POINTER(auth
->cid
));
208 /* Cancel outstanding providers for a client (if any). */
210 cancel_providers(struct auth_client
*auth
)
212 if(auth
->providers_cancelled
)
215 auth
->providers_cancelled
= true;
217 if(auth
->providers_active
> 0)
221 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
223 struct auth_provider
*provider
= ptr
->data
;
225 if(provider
->cancel
!= NULL
&& is_provider_running(auth
, provider
->id
))
226 /* Cancel if required */
227 provider
->cancel(auth
);
232 /* Provider is done */
234 provider_done(struct auth_client
*auth
, uint32_t id
)
238 lrb_assert(is_provider_running(auth
, id
));
239 lrb_assert(id
!= UINT32_MAX
);
240 lrb_assert(id
< allocated_pids
);
242 set_provider_done(auth
, id
);
244 if(auth
->providers_active
== 0 && !auth
->providers_starting
)
251 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
253 struct auth_provider
*provider
= ptr
->data
;
255 if(provider
->completed
!= NULL
&& is_provider_running(auth
, provider
->id
))
256 /* Notify pending clients who asked for it */
257 provider
->completed(auth
, id
);
261 /* Reject a client and cancel any outstanding providers */
263 reject_client(struct auth_client
*auth
, uint32_t id
, const char *data
, const char *fmt
, ...)
269 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
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.
276 rb_helper_write(authd_helper
, "R %x %c %s %s %s :%s",
277 auth
->cid
, id
!= UINT32_MAX
? auth
->data
[id
].provider
->letter
: '*',
278 auth
->username
, auth
->hostname
,
279 data
== NULL
? "*" : data
, buf
);
282 set_provider_done(auth
, id
);
284 cancel_providers(auth
);
287 /* Accept a client and cancel outstanding providers if any */
289 accept_client(struct auth_client
*auth
)
291 rb_helper_write(authd_helper
, "A %x %s %s", auth
->cid
, auth
->username
, auth
->hostname
);
292 cancel_providers(auth
);
295 /* Begin authenticating user */
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
)
299 struct auth_client
*auth
;
300 unsigned long long lcid
= strtoull(cid
, NULL
, 16);
303 if(lcid
== 0 || lcid
> UINT32_MAX
)
306 auth
= rb_malloc(sizeof(struct auth_client
));
307 auth_client_ref(auth
);
308 auth
->cid
= (uint32_t)lcid
;
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
);
314 warn_opers(L_CRIT
, "provider: duplicate client added via start_auth: %s", cid
);
315 exit(EX_PROVIDER_ERROR
);
318 auth
->protocol
= strtoull(protocol
, NULL
, 16);
320 rb_strlcpy(auth
->l_ip
, l_ip
, sizeof(auth
->l_ip
));
321 auth
->l_port
= (uint16_t)atoi(l_port
); /* should be safe */
322 (void) rb_inet_pton_sock(l_ip
, &auth
->l_addr
);
323 SET_SS_PORT(&auth
->l_addr
, htons(auth
->l_port
));
325 rb_strlcpy(auth
->c_ip
, c_ip
, sizeof(auth
->c_ip
));
326 auth
->c_port
= (uint16_t)atoi(c_port
);
327 (void) rb_inet_pton_sock(c_ip
, &auth
->c_addr
);
328 SET_SS_PORT(&auth
->c_addr
, htons(auth
->c_port
));
330 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
331 rb_strlcpy(auth
->username
, "*", sizeof(auth
->username
));
333 auth
->data
= rb_malloc(allocated_pids
* sizeof(struct auth_client_data
));
335 auth
->providers_starting
= true;
336 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
338 struct auth_provider
*provider
= ptr
->data
;
340 auth
->data
[provider
->id
].provider
= provider
;
342 lrb_assert(provider
->start
!= NULL
);
344 /* Execute providers */
345 set_provider_running(auth
, provider
->id
);
346 if(!provider
->start(auth
))
347 /* Rejected immediately */
350 if(auth
->providers_cancelled
)
353 auth
->providers_starting
= false;
355 /* If no providers are running, accept the client */
356 if(auth
->providers_active
== 0)
360 auth_client_unref(auth
);
363 /* Callback for the initiation */
365 handle_new_connection(int parc
, char *parv
[])
368 warn_opers(L_CRIT
, "provider: received too few params for new connection (6 expected, got %d)", parc
);
369 exit(EX_PROVIDER_ERROR
);
372 start_auth(parv
[1], parv
[2], parv
[3], parv
[4], parv
[5], parc
> 6 ? parv
[6] : "0");
376 handle_cancel_connection(int parc
, char *parv
[])
378 struct auth_client
*auth
;
379 unsigned long long lcid
;
383 warn_opers(L_CRIT
, "provider: received too few params for new connection (2 expected, got %d)", parc
);
384 exit(EX_PROVIDER_ERROR
);
387 lcid
= strtoull(parv
[1], NULL
, 16);
388 if(lcid
== 0 || lcid
> UINT32_MAX
)
390 warn_opers(L_CRIT
, "provider: got a request to cancel a connection that can't exist: %s", parv
[1]);
391 exit(EX_PROVIDER_ERROR
);
394 if((auth
= rb_dictionary_retrieve(auth_clients
, RB_UINT_TO_POINTER((uint32_t)lcid
))) == NULL
)
396 /* This could happen as a race if we've accepted/rejected but they cancel, so don't die here.
401 auth_client_ref(auth
);
402 cancel_providers(auth
);
403 auth_client_unref(auth
);
407 provider_timeout_event(void *notused __unused
)
409 struct auth_client
*auth
;
410 rb_dictionary_iter iter
;
411 const time_t curtime
= rb_current_time();
413 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
417 auth_client_ref(auth
);
419 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
421 struct auth_provider
*provider
= ptr
->data
;
422 const time_t timeout
= get_provider_timeout(auth
, provider
->id
);
424 if(is_provider_running(auth
, provider
->id
) && provider
->timeout
!= NULL
&&
425 timeout
> 0 && timeout
< curtime
)
427 provider
->timeout(auth
);
431 auth_client_unref(auth
);