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 /* Initalise all providers */
69 auth_clients
= rb_dictionary_create("pending auth clients", rb_uint32cmp
);
70 timeout_ev
= rb_event_addish("provider_timeout_event", provider_timeout_event
, NULL
, 1);
72 load_provider(&rdns_provider
);
73 load_provider(&ident_provider
);
74 load_provider(&blacklist_provider
);
75 load_provider(&opm_provider
);
78 /* Terminate all providers */
80 destroy_providers(void)
82 rb_dlink_node
*ptr
, *nptr
;
83 rb_dictionary_iter iter
;
84 struct auth_client
*auth
;
86 /* Cancel outstanding connections */
87 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
89 auth_client_ref(auth
);
91 /* TBD - is this the right thing? */
92 reject_client(auth
, UINT32_MAX
, "destroy",
93 "Authentication system is down... try reconnecting in a few seconds");
95 auth_client_unref(auth
);
98 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, auth_providers
.head
)
100 struct auth_provider
*provider
= ptr
->data
;
102 if(provider
->destroy
)
105 rb_dlinkDelete(ptr
, &auth_providers
);
108 rb_dictionary_destroy(auth_clients
, NULL
, NULL
);
109 rb_event_delete(timeout_ev
);
112 /* Load a provider */
114 load_provider(struct auth_provider
*provider
)
117 if(rb_dlink_list_length(&free_pids
) > 0)
119 /* use the free list */
120 provider
->id
= RB_POINTER_TO_UINT(free_pids
.head
->data
);
121 rb_dlinkDestroy(free_pids
.head
, &free_pids
);
125 if(allocated_pids
== MAX_PROVIDERS
|| allocated_pids
== UINT32_MAX
)
127 warn_opers(L_WARN
, "Cannot load additional provider, max reached!");
131 provider
->id
= allocated_pids
++;
134 if(provider
->opt_handlers
!= NULL
)
136 struct auth_opts_handler
*handler
;
138 for(handler
= provider
->opt_handlers
; handler
->option
!= NULL
; handler
++)
139 rb_dictionary_add(authd_option_handlers
, handler
->option
, handler
);
142 if(provider
->stats_handler
.letter
!= '\0')
143 authd_stat_handlers
[(unsigned char)provider
->stats_handler
.letter
] = provider
->stats_handler
.handler
;
145 if(provider
->init
!= NULL
)
148 rb_dlinkAdd(provider
, &provider
->node
, &auth_providers
);
152 unload_provider(struct auth_provider
*provider
)
154 if(provider
->opt_handlers
!= NULL
)
156 struct auth_opts_handler
*handler
;
158 for(handler
= provider
->opt_handlers
; handler
->option
!= NULL
; handler
++)
159 rb_dictionary_delete(authd_option_handlers
, handler
->option
);
162 if(provider
->stats_handler
.letter
!= '\0')
163 authd_stat_handlers
[(unsigned char)provider
->stats_handler
.letter
] = NULL
;
165 if(provider
->destroy
!= NULL
)
168 rb_dlinkDelete(&provider
->node
, &auth_providers
);
171 rb_dlinkAddAlloc(RB_UINT_TO_POINTER(provider
->id
), &free_pids
);
175 auth_client_free(struct auth_client
*auth
)
177 rb_dictionary_delete(auth_clients
, RB_UINT_TO_POINTER(auth
->cid
));
182 /* Cancel outstanding providers for a client (if any). */
184 cancel_providers(struct auth_client
*auth
)
186 if(auth
->providers_cancelled
)
189 auth
->providers_cancelled
= true;
191 if(auth
->providers_active
> 0)
195 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
197 struct auth_provider
*provider
= ptr
->data
;
199 if(provider
->cancel
!= NULL
&& is_provider_running(auth
, provider
->id
))
200 /* Cancel if required */
201 provider
->cancel(auth
);
206 /* Provider is done */
208 provider_done(struct auth_client
*auth
, uint32_t id
)
212 lrb_assert(is_provider_running(auth
, id
));
213 lrb_assert(id
!= UINT32_MAX
);
214 lrb_assert(id
< allocated_pids
);
216 set_provider_done(auth
, id
);
218 if(auth
->providers_active
== 0 && !auth
->providers_starting
)
225 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
227 struct auth_provider
*provider
= ptr
->data
;
229 if(provider
->completed
!= NULL
&& is_provider_running(auth
, provider
->id
))
230 /* Notify pending clients who asked for it */
231 provider
->completed(auth
, id
);
235 /* Reject a client and cancel any outstanding providers */
237 reject_client(struct auth_client
*auth
, uint32_t id
, const char *data
, const char *fmt
, ...)
243 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
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.
250 rb_helper_write(authd_helper
, "R %x %c %s %s %s :%s",
251 auth
->cid
, id
!= UINT32_MAX
? auth
->data
[id
].provider
->letter
: '*',
252 auth
->username
, auth
->hostname
,
253 data
== NULL
? "*" : data
, buf
);
256 set_provider_done(auth
, id
);
258 cancel_providers(auth
);
261 /* Accept a client and cancel outstanding providers if any */
263 accept_client(struct auth_client
*auth
)
265 rb_helper_write(authd_helper
, "A %x %s %s", auth
->cid
, auth
->username
, auth
->hostname
);
266 cancel_providers(auth
);
269 /* Begin authenticating user */
271 start_auth(const char *cid
, const char *l_ip
, const char *l_port
, const char *c_ip
, const char *c_port
)
273 struct auth_client
*auth
;
274 unsigned long long lcid
= strtoull(cid
, NULL
, 16);
277 if(lcid
== 0 || lcid
> UINT32_MAX
)
280 auth
= rb_malloc(sizeof(struct auth_client
));
281 auth_client_ref(auth
);
282 auth
->cid
= (uint32_t)lcid
;
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
);
288 warn_opers(L_CRIT
, "provider: duplicate client added via start_auth: %s", cid
);
289 exit(EX_PROVIDER_ERROR
);
292 rb_strlcpy(auth
->l_ip
, l_ip
, sizeof(auth
->l_ip
));
293 auth
->l_port
= (uint16_t)atoi(l_port
); /* should be safe */
294 (void) rb_inet_pton_sock(l_ip
, (struct sockaddr
*)&auth
->l_addr
);
295 SET_SS_PORT(&auth
->l_addr
, htons(auth
->l_port
));
297 rb_strlcpy(auth
->c_ip
, c_ip
, sizeof(auth
->c_ip
));
298 auth
->c_port
= (uint16_t)atoi(c_port
);
299 (void) rb_inet_pton_sock(c_ip
, (struct sockaddr
*)&auth
->c_addr
);
300 SET_SS_PORT(&auth
->c_addr
, htons(auth
->c_port
));
302 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
303 rb_strlcpy(auth
->username
, "*", sizeof(auth
->username
));
305 auth
->data
= rb_malloc(allocated_pids
* sizeof(struct auth_client_data
));
307 auth
->providers_starting
= true;
308 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
310 struct auth_provider
*provider
= ptr
->data
;
312 auth
->data
[provider
->id
].provider
= provider
;
314 lrb_assert(provider
->start
!= NULL
);
316 /* Execute providers */
317 if(!provider
->start(auth
))
318 /* Rejected immediately */
321 if(auth
->providers_cancelled
)
324 auth
->providers_starting
= false;
326 /* If no providers are running, accept the client */
327 if(auth
->providers_active
== 0)
331 auth_client_unref(auth
);
334 /* Callback for the initiation */
336 handle_new_connection(int parc
, char *parv
[])
340 warn_opers(L_CRIT
, "provider: received too few params for new connection (6 expected, got %d)", parc
);
341 exit(EX_PROVIDER_ERROR
);
344 start_auth(parv
[1], parv
[2], parv
[3], parv
[4], parv
[5]);
348 handle_cancel_connection(int parc
, char *parv
[])
350 struct auth_client
*auth
;
351 unsigned long long lcid
;
355 warn_opers(L_CRIT
, "provider: received too few params for new connection (2 expected, got %d)", parc
);
356 exit(EX_PROVIDER_ERROR
);
359 lcid
= strtoull(parv
[1], NULL
, 16);
360 if(lcid
== 0 || lcid
> UINT32_MAX
)
362 warn_opers(L_CRIT
, "provider: got a request to cancel a connection that can't exist: %s", parv
[1]);
363 exit(EX_PROVIDER_ERROR
);
366 if((auth
= rb_dictionary_retrieve(auth_clients
, RB_UINT_TO_POINTER((uint32_t)lcid
))) == NULL
)
368 /* This could happen as a race if we've accepted/rejected but they cancel, so don't die here.
373 auth_client_ref(auth
);
374 cancel_providers(auth
);
375 auth_client_unref(auth
);
379 provider_timeout_event(void *notused __unused
)
381 struct auth_client
*auth
;
382 rb_dictionary_iter iter
;
383 const time_t curtime
= rb_current_time();
385 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
389 auth_client_ref(auth
);
391 RB_DLINK_FOREACH(ptr
, auth_providers
.head
)
393 struct auth_provider
*provider
= ptr
->data
;
394 const time_t timeout
= get_provider_timeout(auth
, provider
->id
);
396 if(is_provider_running(auth
, provider
->id
) && provider
->timeout
!= NULL
&&
397 timeout
> 0 && timeout
< curtime
)
399 provider
->timeout(auth
);
403 auth_client_unref(auth
);