]>
Commit | Line | Data |
---|---|---|
0f95a274 | 1 | /* authd/provider.h - authentication provider framework |
05e17ac2 EM |
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 | ||
0f95a274 EM |
21 | #ifndef __CHARYBDIS_AUTHD_PROVIDER_H__ |
22 | #define __CHARYBDIS_AUTHD_PROVIDER_H__ | |
05e17ac2 EM |
23 | |
24 | #include "stdinc.h" | |
a51487e0 | 25 | #include "authd.h" |
3e875f62 | 26 | #include "rb_dictionary.h" |
05e17ac2 | 27 | |
3e875f62 | 28 | #define MAX_PROVIDERS 32 /* This should be enough */ |
05e17ac2 | 29 | |
376ae2e2 EM |
30 | typedef enum |
31 | { | |
32 | PROVIDER_STATUS_NOTRUN = 0, | |
33 | PROVIDER_STATUS_RUNNING, | |
34 | PROVIDER_STATUS_DONE, | |
35 | } provider_status_t; | |
36 | ||
a68d9a2b EM |
37 | struct auth_client_data |
38 | { | |
731d1289 | 39 | struct auth_provider *provider; /* Pointer back */ |
376ae2e2 EM |
40 | time_t timeout; /* Provider timeout */ |
41 | void *data; /* Provider data */ | |
42 | provider_status_t status; /* Provider status */ | |
a68d9a2b EM |
43 | }; |
44 | ||
05e17ac2 EM |
45 | struct auth_client |
46 | { | |
2392770f | 47 | uint32_t cid; /* Client ID */ |
05e17ac2 | 48 | |
c6ad9b0c SA |
49 | int protocol; /* IP protocol (TCP/SCTP) */ |
50 | ||
2b0cc3d3 EM |
51 | char l_ip[HOSTIPLEN + 1]; /* Listener IP address */ |
52 | uint16_t l_port; /* Listener port */ | |
9c7498d5 | 53 | struct rb_sockaddr_storage l_addr; /* Listener address/port */ |
2b0cc3d3 EM |
54 | |
55 | char c_ip[HOSTIPLEN + 1]; /* Client IP address */ | |
56 | uint16_t c_port; /* Client port */ | |
9c7498d5 | 57 | struct rb_sockaddr_storage c_addr; /* Client address/port */ |
05e17ac2 | 58 | |
5bfc606f | 59 | char hostname[HOSTLEN + 1]; /* Used for DNS lookup */ |
be67cfca | 60 | char username[USERLEN + 1]; /* Used for ident lookup */ |
05e17ac2 | 61 | |
247b304f | 62 | bool providers_starting; /* Providers are still warming up */ |
9f928dc5 | 63 | bool providers_cancelled; /* Providers are being cancelled */ |
a5f52774 | 64 | unsigned int providers_active; /* Number of active providers */ |
376ae2e2 | 65 | unsigned int refcount; /* Held references */ |
f7b37c1d | 66 | |
a68d9a2b | 67 | struct auth_client_data *data; /* Provider-specific data */ |
2b0cc3d3 EM |
68 | }; |
69 | ||
70 | typedef bool (*provider_init_t)(void); | |
2b0cc3d3 EM |
71 | typedef void (*provider_destroy_t)(void); |
72 | ||
89d22b9a EM |
73 | typedef bool (*provider_start_t)(struct auth_client *); |
74 | typedef void (*provider_cancel_t)(struct auth_client *); | |
731d1289 EM |
75 | typedef void (*uint32_timeout_t)(struct auth_client *); |
76 | typedef void (*provider_complete_t)(struct auth_client *, uint32_t); | |
89d22b9a | 77 | |
ee7f9271 EM |
78 | struct auth_stats_handler |
79 | { | |
80 | const char letter; | |
81 | authd_stat_handler handler; | |
82 | }; | |
83 | ||
05e17ac2 EM |
84 | struct auth_provider |
85 | { | |
99e53867 EM |
86 | rb_dlink_node node; |
87 | ||
731d1289 EM |
88 | uint32_t id; /* Provider ID */ |
89 | ||
90 | const char *name; /* Name of the provider */ | |
91 | char letter; /* Letter used on reject, etc. */ | |
05e17ac2 EM |
92 | |
93 | provider_init_t init; /* Initalise the provider */ | |
94 | provider_destroy_t destroy; /* Terminate the provider */ | |
95 | ||
89d22b9a | 96 | provider_start_t start; /* Perform authentication */ |
05e17ac2 | 97 | provider_cancel_t cancel; /* Authentication cancelled */ |
731d1289 | 98 | uint32_timeout_t timeout; /* Timeout callback */ |
05e17ac2 | 99 | provider_complete_t completed; /* Callback for when other performers complete (think dependency chains) */ |
a51487e0 | 100 | |
ee7f9271 EM |
101 | struct auth_stats_handler stats_handler; |
102 | ||
a51487e0 | 103 | struct auth_opts_handler *opt_handlers; |
05e17ac2 EM |
104 | }; |
105 | ||
18764319 | 106 | extern struct auth_provider rdns_provider; |
f1861e48 | 107 | extern struct auth_provider ident_provider; |
add80afd | 108 | extern struct auth_provider blacklist_provider; |
4e85459a | 109 | extern struct auth_provider opm_provider; |
9b5b2ded | 110 | |
d955cd9f | 111 | extern rb_dlink_list auth_providers; |
a71b65b1 | 112 | extern rb_dictionary *auth_clients; |
74909c9a | 113 | |
9b5b2ded EM |
114 | void load_provider(struct auth_provider *provider); |
115 | void unload_provider(struct auth_provider *provider); | |
116 | ||
05e17ac2 EM |
117 | void init_providers(void); |
118 | void destroy_providers(void); | |
119 | void cancel_providers(struct auth_client *auth); | |
120 | ||
731d1289 | 121 | void provider_done(struct auth_client *auth, uint32_t id); |
2f598dac | 122 | void accept_client(struct auth_client *auth); |
731d1289 | 123 | void reject_client(struct auth_client *auth, uint32_t id, const char *data, const char *fmt, ...); |
05e17ac2 | 124 | |
05e17ac2 | 125 | void handle_new_connection(int parc, char *parv[]); |
60374ac9 | 126 | void handle_cancel_connection(int parc, char *parv[]); |
b585278b | 127 | void auth_client_free(struct auth_client *auth); |
05e17ac2 | 128 | |
b585278b AC |
129 | static inline void |
130 | auth_client_ref(struct auth_client *auth) | |
131 | { | |
132 | auth->refcount++; | |
133 | } | |
134 | ||
135 | static inline void | |
136 | auth_client_unref(struct auth_client *auth) | |
137 | { | |
138 | auth->refcount--; | |
139 | if (auth->refcount == 0) | |
140 | auth_client_free(auth); | |
141 | } | |
74909c9a | 142 | |
a71b65b1 AC |
143 | /* Get a provider by name */ |
144 | static inline struct auth_provider * | |
d955cd9f | 145 | find_provider(const char *name) |
a71b65b1 | 146 | { |
d955cd9f SA |
147 | rb_dlink_node *ptr; |
148 | ||
149 | RB_DLINK_FOREACH(ptr, auth_providers.head) | |
150 | { | |
151 | struct auth_provider *provider = ptr->data; | |
152 | ||
153 | if(strcasecmp(provider->name, name) == 0) | |
154 | return provider; | |
155 | } | |
156 | ||
157 | return NULL; | |
a71b65b1 AC |
158 | } |
159 | ||
731d1289 EM |
160 | /* Get a provider's id by name */ |
161 | static inline bool | |
6b3e61f1 | 162 | get_provider_id(const char *name, uint32_t *id) |
731d1289 | 163 | { |
d955cd9f | 164 | struct auth_provider *provider = find_provider(name); |
731d1289 EM |
165 | |
166 | if(provider != NULL) | |
167 | { | |
168 | *id = provider->id; | |
169 | return true; | |
170 | } | |
171 | else | |
172 | return false; | |
173 | } | |
174 | ||
376ae2e2 EM |
175 | /* Get a provider's raw status */ |
176 | static inline provider_status_t | |
731d1289 | 177 | get_provider_status(struct auth_client *auth, uint32_t provider) |
376ae2e2 EM |
178 | { |
179 | return auth->data[provider].status; | |
180 | } | |
181 | ||
05e17ac2 | 182 | /* Check if provider is operating on this auth client */ |
60374ac9 | 183 | static inline bool |
731d1289 | 184 | is_provider_running(struct auth_client *auth, uint32_t provider) |
2b0cc3d3 | 185 | { |
376ae2e2 | 186 | return get_provider_status(auth, provider) == PROVIDER_STATUS_RUNNING; |
2b0cc3d3 EM |
187 | } |
188 | ||
376ae2e2 | 189 | /* Check if provider has finished on this client */ |
60374ac9 | 190 | static inline bool |
731d1289 | 191 | is_provider_done(struct auth_client *auth, uint32_t provider) |
05e17ac2 | 192 | { |
376ae2e2 EM |
193 | return get_provider_status(auth, provider) == PROVIDER_STATUS_DONE; |
194 | } | |
195 | ||
196 | /* Get provider auth client data */ | |
197 | static inline void * | |
198 | get_provider_data(struct auth_client *auth, uint32_t id) | |
199 | { | |
376ae2e2 EM |
200 | return auth->data[id].data; |
201 | } | |
202 | ||
203 | /* Set provider auth client data */ | |
204 | static inline void | |
205 | set_provider_data(struct auth_client *auth, uint32_t id, void *data) | |
206 | { | |
376ae2e2 EM |
207 | auth->data[id].data = data; |
208 | } | |
209 | ||
210 | /* Set timeout relative to current time on provider | |
211 | * When the timeout lapses, the provider's timeout call will execute */ | |
212 | static inline void | |
213 | set_provider_timeout_relative(struct auth_client *auth, uint32_t id, time_t timeout) | |
214 | { | |
376ae2e2 EM |
215 | auth->data[id].timeout = timeout + rb_current_time(); |
216 | } | |
217 | ||
218 | /* Set timeout value in absolute time (Unix timestamp) | |
219 | * When the timeout lapses, the provider's timeout call will execute */ | |
220 | static inline void | |
221 | set_provider_timeout_absolute(struct auth_client *auth, uint32_t id, time_t timeout) | |
222 | { | |
376ae2e2 EM |
223 | auth->data[id].timeout = timeout; |
224 | } | |
225 | ||
226 | /* Get the timeout value for the provider */ | |
227 | static inline time_t | |
228 | get_provider_timeout(struct auth_client *auth, uint32_t id) | |
229 | { | |
376ae2e2 | 230 | return auth->data[id].timeout; |
05e17ac2 EM |
231 | } |
232 | ||
0f95a274 | 233 | #endif /* __CHARYBDIS_AUTHD_PROVIDER_H__ */ |