]> jfr.im git - solanum.git/blob - authd/provider.c
rename auth.[ch] to provider.[ch]
[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 /* So the basic design here is to have "authentication providers" that do
22 * things like query ident and blacklists and even open proxies.
23 *
24 * Providers are registered statically in the struct auth_providers array. You will
25 * probably want to add an item to the provider_t enum also.
26 *
27 * Providers can either return failure immediately, immediate acceptance, or
28 * do work in the background (calling set_provider to signal this).
29 *
30 * It is up to providers to keep their own state on clients if they need to.
31 *
32 * All providers must implement at a minimum a perform_provider function. You
33 * don't have to implement the others if you don't need them.
34 *
35 * Providers may kick clients off by rejecting them. Upon rejection, all
36 * providers are cancelled. They can also unconditionally accept them.
37 *
38 * When a provider is done and is neutral on accepting/rejecting a client, it
39 * should call provider_done. Do NOT call this if you have accepted or rejected
40 * the client.
41 *
42 * --Elizafox, 9 March 2016
43 */
44
45 #include "authd.h"
46 #include "provider.h"
47
48 rb_dlink_list auth_providers;
49
50 /* Clients waiting */
51 struct auth_client auth_clients[MAX_CLIENTS];
52
53 /* Load a provider */
54 void load_provider(struct auth_provider *provider)
55 {
56 provider->init();
57 rb_dlinkAdd(provider, &provider->node, &auth_providers);
58 }
59
60 void unload_provider(struct auth_provider *provider)
61 {
62 provider->destroy();
63 rb_dlinkDelete(&provider->node, &auth_providers);
64 }
65
66 /* Initalise all providers */
67 void init_providers(void)
68 {
69 rb_dlink_node *ptr;
70 struct auth_provider *provider;
71
72 RB_DLINK_FOREACH(ptr, auth_providers.head)
73 {
74 provider = ptr->data;
75
76 if(provider->init && !provider->init())
77 /* Provider failed to init, time to go */
78 exit(1);
79 }
80 }
81
82 /* Terminate all providers */
83 void destroy_providers(void)
84 {
85 rb_dlink_node *ptr;
86 struct auth_provider *provider;
87
88 /* Cancel outstanding connections */
89 for (size_t i = 0; i < MAX_CLIENTS; i++)
90 {
91 if(auth_clients[i].cid)
92 {
93 /* TBD - is this the right thing?
94 * (NOTE - this error message is designed for morons) */
95 reject_client(&auth_clients[i], 0,
96 "IRC server reloading... try reconnecting in a few seconds");
97 }
98 }
99
100 RB_DLINK_FOREACH(ptr, auth_providers.head)
101 {
102 provider = ptr->data;
103
104 if(provider->destroy)
105 provider->destroy();
106 }
107 }
108
109 /* Cancel outstanding providers for a client */
110 void cancel_providers(struct auth_client *auth)
111 {
112 rb_dlink_node *ptr;
113 struct auth_provider *provider;
114
115 RB_DLINK_FOREACH(ptr, auth_providers.head)
116 {
117 provider = ptr->data;
118
119 if(provider->cancel && is_provider(auth, provider->id))
120 /* Cancel if required */
121 provider->cancel(auth);
122 }
123 }
124
125 /* Provider is done */
126 void provider_done(struct auth_client *auth, provider_t id)
127 {
128 rb_dlink_node *ptr;
129 struct auth_provider *provider;
130
131 unset_provider(auth, id);
132
133 if(!auth->providers)
134 {
135 /* No more providers, done */
136 accept_client(auth, 0);
137 return;
138 }
139
140 RB_DLINK_FOREACH(ptr, auth_providers.head)
141 {
142 provider = ptr->data;
143
144 if(provider->completed && is_provider(auth, provider->id))
145 /* Notify pending clients who asked for it */
146 provider->completed(auth, id);
147 }
148 }
149
150 /* Reject a client, cancel outstanding providers if any */
151 void reject_client(struct auth_client *auth, provider_t id, const char *reason)
152 {
153 uint16_t cid = auth->cid;
154
155 rb_helper_write(authd_helper, "R %x :%s", auth->cid, reason);
156
157 unset_provider(auth, id);
158
159 if(auth->providers)
160 cancel_providers(auth);
161
162 memset(&auth_clients[cid], 0, sizeof(struct auth_client));
163 }
164
165 /* Accept a client, cancel outstanding providers if any */
166 void accept_client(struct auth_client *auth, provider_t id)
167 {
168 uint16_t cid = auth->cid;
169
170 rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname);
171
172 unset_provider(auth, id);
173
174 if(auth->providers)
175 cancel_providers(auth);
176
177 memset(&auth_clients[cid], 0, sizeof(struct auth_client));
178 }
179
180 /* Send a notice to a client */
181 void notice_client(struct auth_client *auth, const char *notice)
182 {
183 rb_helper_write(authd_helper, "N %x :%s", auth->cid, notice);
184 }
185
186 /* Begin authenticating user */
187 void start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
188 {
189 rb_dlink_node *ptr;
190 struct auth_provider *provider;
191 struct auth_client *auth;
192 long lcid = strtol(cid, NULL, 16);
193
194 if(lcid >= MAX_CLIENTS)
195 return;
196
197 auth = &auth_clients[lcid];
198 if(auth->cid != 0)
199 /* Shouldn't get here */
200 return;
201
202 auth->cid = (uint16_t)lcid;
203
204 rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
205 auth->l_port = (uint16_t)atoi(l_port); /* Safe cast, port shouldn't exceed 16 bits */
206
207 rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip));
208 auth->c_port = (uint16_t)atoi(c_port);
209
210 RB_DLINK_FOREACH(ptr, auth_providers.head)
211 {
212 provider = ptr->data;
213
214 /* Execute providers */
215 if(!provider->start(auth))
216 {
217 /* Rejected immediately */
218 cancel_providers(auth);
219 return;
220 }
221 }
222
223 /* If no providers are running, accept the client */
224 if(!auth->providers)
225 accept_client(auth, 0);
226 }
227
228 /* Callback for the initiation */
229 void handle_new_connection(int parc, char *parv[])
230 {
231 if(parc < 5)
232 return;
233
234 start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
235 }