]> jfr.im git - solanum.git/blob - authd/provider.h
Clean up the provider status logic.
[solanum.git] / authd / provider.h
1 /* authd/provider.h - 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 #ifndef __CHARYBDIS_AUTHD_PROVIDER_H__
22 #define __CHARYBDIS_AUTHD_PROVIDER_H__
23
24 #include "stdinc.h"
25 #include "authd.h"
26 #include "rb_dictionary.h"
27
28 #define MAX_PROVIDERS 32 /* This should be enough */
29
30 /* Registered providers */
31 typedef enum
32 {
33 PROVIDER_RDNS,
34 PROVIDER_IDENT,
35 PROVIDER_BLACKLIST,
36 PROVIDER_OPM,
37 } provider_t;
38
39 typedef enum
40 {
41 PROVIDER_STATUS_NOTRUN = 0,
42 PROVIDER_STATUS_RUNNING,
43 PROVIDER_STATUS_DONE,
44 } provider_status_t;
45
46 struct auth_client_data
47 {
48 time_t timeout; /* Provider timeout */
49 void *data; /* Provider data */
50 provider_status_t status; /* Provider status */
51 };
52
53 struct auth_client
54 {
55 uint16_t cid; /* Client ID */
56
57 char l_ip[HOSTIPLEN + 1]; /* Listener IP address */
58 uint16_t l_port; /* Listener port */
59 struct rb_sockaddr_storage l_addr; /* Listener address/port */
60
61 char c_ip[HOSTIPLEN + 1]; /* Client IP address */
62 uint16_t c_port; /* Client port */
63 struct rb_sockaddr_storage c_addr; /* Client address/port */
64
65 char hostname[HOSTLEN + 1]; /* Used for DNS lookup */
66 char username[USERLEN + 1]; /* Used for ident lookup */
67
68 bool providers_starting; /* Providers are still warming up */
69 unsigned int refcount; /* Held references */
70
71 struct auth_client_data *data; /* Provider-specific data */
72 };
73
74 typedef bool (*provider_init_t)(void);
75 typedef void (*provider_destroy_t)(void);
76
77 typedef bool (*provider_start_t)(struct auth_client *);
78 typedef void (*provider_cancel_t)(struct auth_client *);
79 typedef void (*provider_timeout_t)(struct auth_client *);
80 typedef void (*provider_complete_t)(struct auth_client *, provider_t);
81
82 struct auth_stats_handler
83 {
84 const char letter;
85 authd_stat_handler handler;
86 };
87
88 struct auth_provider
89 {
90 rb_dlink_node node;
91
92 provider_t id;
93
94 provider_init_t init; /* Initalise the provider */
95 provider_destroy_t destroy; /* Terminate the provider */
96
97 provider_start_t start; /* Perform authentication */
98 provider_cancel_t cancel; /* Authentication cancelled */
99 provider_timeout_t timeout; /* Timeout callback */
100 provider_complete_t completed; /* Callback for when other performers complete (think dependency chains) */
101
102 struct auth_stats_handler stats_handler;
103
104 struct auth_opts_handler *opt_handlers;
105 };
106
107 extern struct auth_provider rdns_provider;
108 extern struct auth_provider ident_provider;
109 extern struct auth_provider blacklist_provider;
110 extern struct auth_provider opm_provider;
111
112 extern rb_dlink_list auth_providers;
113 extern rb_dictionary *auth_clients;
114
115 void load_provider(struct auth_provider *provider);
116 void unload_provider(struct auth_provider *provider);
117
118 void init_providers(void);
119 void destroy_providers(void);
120 void cancel_providers(struct auth_client *auth);
121
122 void provider_done(struct auth_client *auth, provider_t id);
123 void accept_client(struct auth_client *auth, provider_t id);
124 void reject_client(struct auth_client *auth, provider_t id, const char *data, const char *fmt, ...);
125
126 void handle_new_connection(int parc, char *parv[]);
127 void handle_cancel_connection(int parc, char *parv[]);
128
129
130 /* Get a provider's raw status */
131 static inline provider_status_t
132 get_provider_status(struct auth_client *auth, provider_t provider)
133 {
134 return auth->data[provider].status;
135 }
136
137 /* Set a provider's raw status */
138 static inline void
139 set_provider_status(struct auth_client *auth, provider_t provider, provider_status_t status)
140 {
141 auth->data[provider].status = status;
142 }
143
144 /* Set the provider as running
145 * If you're doing asynchronous work call this */
146 static inline void
147 set_provider_running(struct auth_client *auth, provider_t provider)
148 {
149 auth->refcount++;
150 set_provider_status(auth, provider, PROVIDER_STATUS_RUNNING);
151 }
152
153 /* Provider is no longer operating on this auth client
154 * You should use provider_done and not this */
155 static inline void
156 set_provider_done(struct auth_client *auth, provider_t provider)
157 {
158 auth->refcount--;
159 set_provider_status(auth, provider, PROVIDER_STATUS_DONE);
160 }
161
162 /* Check if provider is operating on this auth client */
163 static inline bool
164 is_provider_running(struct auth_client *auth, provider_t provider)
165 {
166 return get_provider_status(auth, provider) == PROVIDER_STATUS_RUNNING;
167 }
168
169 /* Check if provider has finished on this client */
170 static inline bool
171 is_provider_done(struct auth_client *auth, provider_t provider)
172 {
173 return get_provider_status(auth, provider) == PROVIDER_STATUS_DONE;
174 }
175
176 /* Get provider auth client data */
177 static inline void *
178 get_provider_data(struct auth_client *auth, uint32_t id)
179 {
180 lrb_assert(id < rb_dlink_list_length(&auth_providers));
181 return auth->data[id].data;
182 }
183
184 /* Set provider auth client data */
185 static inline void
186 set_provider_data(struct auth_client *auth, uint32_t id, void *data)
187 {
188 lrb_assert(id < rb_dlink_list_length(&auth_providers));
189 auth->data[id].data = data;
190 }
191
192 /* Set timeout relative to current time on provider
193 * When the timeout lapses, the provider's timeout call will execute */
194 static inline void
195 set_provider_timeout_relative(struct auth_client *auth, uint32_t id, time_t timeout)
196 {
197 lrb_assert(id < rb_dlink_list_length(&auth_providers));
198 auth->data[id].timeout = timeout + rb_current_time();
199 }
200
201 /* Set timeout value in absolute time (Unix timestamp)
202 * When the timeout lapses, the provider's timeout call will execute */
203 static inline void
204 set_provider_timeout_absolute(struct auth_client *auth, uint32_t id, time_t timeout)
205 {
206 lrb_assert(id < rb_dlink_list_length(&auth_providers));
207 auth->data[id].timeout = timeout;
208 }
209
210 /* Get the timeout value for the provider */
211 static inline time_t
212 get_provider_timeout(struct auth_client *auth, uint32_t id)
213 {
214 lrb_assert(id < rb_dlink_list_length(&auth_providers));
215 return auth->data[id].timeout;
216 }
217
218 #endif /* __CHARYBDIS_AUTHD_PROVIDER_H__ */