]> jfr.im git - solanum.git/blob - authd/provider.c
Merge branch 'master' into authd-framework-2
[solanum.git] / authd / provider.c
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
21 /* The basic design here is to have "authentication providers" that do things
22 * like query ident and blacklists and even open proxies.
23 *
24 * Providers are registered in the auth_providers linked list. It is planned to
25 * use a bitmap to store provider ID's later.
26 *
27 * Providers can either return failure immediately, immediate acceptance, or do
28 * work in the background (calling set_provider to signal this).
29 *
30 * Provider-specific data for each client can be kept in an index of the data
31 * struct member (using the provider's ID).
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 *
43 * Eventually, stuff like *:line handling will be moved here, but that means we
44 * have to talk to bandb directly first.
45 *
46 * --Elizafox, 9 March 2016
47 */
48
49 #include "rb_dictionary.h"
50 #include "authd.h"
51 #include "provider.h"
52 #include "notice.h"
53
54 rb_dlink_list auth_providers;
55
56 /* Clients waiting */
57 rb_dictionary *auth_clients;
58
59 /* Load a provider */
60 void load_provider(struct auth_provider *provider)
61 {
62 if(rb_dlink_list_length(&auth_providers) >= MAX_PROVIDERS)
63 {
64 warn_opers(L_CRIT, "Exceeded maximum level of authd providers (%d max)", MAX_PROVIDERS);
65 return;
66 }
67
68 if(provider->opt_handlers != NULL)
69 {
70 struct auth_opts_handler *handler;
71
72 for(handler = provider->opt_handlers; handler->option != NULL; handler++)
73 rb_dictionary_add(authd_option_handlers, handler->option, handler);
74 }
75
76 provider->init();
77 rb_dlinkAdd(provider, &provider->node, &auth_providers);
78 }
79
80 void unload_provider(struct auth_provider *provider)
81 {
82 if(provider->opt_handlers != NULL)
83 {
84 struct auth_opts_handler *handler;
85
86 for(handler = provider->opt_handlers; handler->option != NULL; handler++)
87 rb_dictionary_delete(authd_option_handlers, handler->option);
88 }
89 provider->destroy();
90 rb_dlinkDelete(&provider->node, &auth_providers);
91 }
92
93 /* Initalise all providers */
94 void init_providers(void)
95 {
96 auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp);
97 load_provider(&rdns_provider);
98 load_provider(&ident_provider);
99 }
100
101 /* Terminate all providers */
102 void destroy_providers(void)
103 {
104 rb_dlink_node *ptr;
105 rb_dictionary_iter iter;
106 struct auth_client *auth;
107 struct auth_provider *provider;
108
109 /* Cancel outstanding connections */
110 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
111 {
112 /* TBD - is this the right thing? */
113 reject_client(auth, 0, "Authentication system is down... try reconnecting in a few seconds");
114 }
115
116 RB_DLINK_FOREACH(ptr, auth_providers.head)
117 {
118 provider = ptr->data;
119
120 if(provider->destroy)
121 provider->destroy();
122 }
123 }
124
125 /* Cancel outstanding providers for a client */
126 void cancel_providers(struct auth_client *auth)
127 {
128 rb_dlink_node *ptr;
129 struct auth_provider *provider;
130
131 RB_DLINK_FOREACH(ptr, auth_providers.head)
132 {
133 provider = ptr->data;
134
135 if(provider->cancel && is_provider_on(auth, provider->id))
136 /* Cancel if required */
137 provider->cancel(auth);
138 }
139
140 rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid));
141 rb_free(auth);
142 }
143
144 /* Provider is done */
145 void provider_done(struct auth_client *auth, provider_t id)
146 {
147 rb_dlink_node *ptr;
148 struct auth_provider *provider;
149
150 set_provider_off(auth, id);
151 set_provider_done(auth, id);
152
153 if(!auth->providers)
154 {
155 /* No more providers, done */
156 accept_client(auth, 0);
157 return;
158 }
159
160 RB_DLINK_FOREACH(ptr, auth_providers.head)
161 {
162 provider = ptr->data;
163
164 if(provider->completed && is_provider_on(auth, provider->id))
165 /* Notify pending clients who asked for it */
166 provider->completed(auth, id);
167 }
168 }
169
170 /* Reject a client */
171 void reject_client(struct auth_client *auth, provider_t id, const char *reason)
172 {
173 char reject;
174
175 switch(id)
176 {
177 case PROVIDER_RDNS:
178 reject = 'D';
179 break;
180 case PROVIDER_IDENT:
181 reject = 'I';
182 break;
183 case PROVIDER_BLACKLIST:
184 reject = 'B';
185 break;
186 default:
187 reject = 'N';
188 break;
189 }
190
191 /* We send back username and hostname in case ircd wants to overrule our decision.
192 * In the future this may not be the case.
193 * --Elizafox
194 */
195 rb_helper_write(authd_helper, "R %x %c %s %s :%s", auth->cid, reject, auth->username, auth->hostname, reason);
196
197 set_provider_off(auth, id);
198 cancel_providers(auth);
199 }
200
201 /* Accept a client, cancel outstanding providers if any */
202 void accept_client(struct auth_client *auth, provider_t id)
203 {
204 uint32_t cid = auth->cid;
205
206 rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname);
207
208 set_provider_off(auth, id);
209 cancel_providers(auth);
210 }
211
212 /* Begin authenticating user */
213 static void start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
214 {
215 struct auth_provider *provider;
216 struct auth_client *auth = rb_malloc(sizeof(struct auth_client));
217 long lcid = strtol(cid, NULL, 16);
218 rb_dlink_node *ptr;
219
220 if(lcid >= UINT32_MAX)
221 return;
222
223 auth->cid = (uint32_t)lcid;
224
225 rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
226 auth->l_port = (uint16_t)atoi(l_port); /* should be safe */
227 (void) rb_inet_pton_sock(l_ip, (struct sockaddr *)&auth->l_addr);
228
229 rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip));
230 auth->c_port = (uint16_t)atoi(c_port);
231 (void) rb_inet_pton_sock(c_ip, (struct sockaddr *)&auth->c_addr);
232
233 #ifdef RB_IPV6
234 if(GET_SS_FAMILY(&auth->l_addr) == AF_INET6)
235 ((struct sockaddr_in6 *)&auth->l_addr)->sin6_port = htons(auth->l_port);
236 else
237 #endif
238 ((struct sockaddr_in *)&auth->l_addr)->sin_port = htons(auth->l_port);
239
240 #ifdef RB_IPV6
241 if(GET_SS_FAMILY(&auth->c_addr) == AF_INET6)
242 ((struct sockaddr_in6 *)&auth->c_addr)->sin6_port = htons(auth->c_port);
243 else
244 #endif
245 ((struct sockaddr_in *)&auth->c_addr)->sin_port = htons(auth->c_port);
246
247 rb_dictionary_add(auth_clients, RB_UINT_TO_POINTER(auth->cid), auth);
248
249 RB_DLINK_FOREACH(ptr, auth_providers.head)
250 {
251 provider = ptr->data;
252
253 /* Execute providers */
254 if(!provider->start(auth))
255 {
256 /* Rejected immediately */
257 cancel_providers(auth);
258 return;
259 }
260 }
261
262 /* If no providers are running, accept the client */
263 if(!auth->providers)
264 accept_client(auth, 0);
265 }
266
267 /* Callback for the initiation */
268 void handle_new_connection(int parc, char *parv[])
269 {
270 if(parc < 7)
271 {
272 warn_opers(L_CRIT, "BUG: received too few params for new connection (7 expected, got %d)", parc);
273 return;
274 }
275
276 start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
277 }