1 /* authd/provider.h - 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 #ifndef __CHARYBDIS_AUTHD_PROVIDER_H__
22 #define __CHARYBDIS_AUTHD_PROVIDER_H__
26 #include "rb_dictionary.h"
28 #define MAX_PROVIDERS 32 /* This should be enough */
32 PROVIDER_STATUS_NOTRUN
= 0,
33 PROVIDER_STATUS_RUNNING
,
37 struct auth_client_data
39 struct auth_provider
*provider
; /* Pointer back */
40 time_t timeout
; /* Provider timeout */
41 void *data
; /* Provider data */
42 provider_status_t status
; /* Provider status */
47 uint16_t cid
; /* Client ID */
49 char l_ip
[HOSTIPLEN
+ 1]; /* Listener IP address */
50 uint16_t l_port
; /* Listener port */
51 struct rb_sockaddr_storage l_addr
; /* Listener address/port */
53 char c_ip
[HOSTIPLEN
+ 1]; /* Client IP address */
54 uint16_t c_port
; /* Client port */
55 struct rb_sockaddr_storage c_addr
; /* Client address/port */
57 char hostname
[HOSTLEN
+ 1]; /* Used for DNS lookup */
58 char username
[USERLEN
+ 1]; /* Used for ident lookup */
60 bool providers_starting
; /* Providers are still warming up */
61 unsigned int refcount
; /* Held references */
63 struct auth_client_data
*data
; /* Provider-specific data */
66 typedef bool (*provider_init_t
)(void);
67 typedef void (*provider_destroy_t
)(void);
69 typedef bool (*provider_start_t
)(struct auth_client
*);
70 typedef void (*provider_cancel_t
)(struct auth_client
*);
71 typedef void (*uint32_timeout_t
)(struct auth_client
*);
72 typedef void (*provider_complete_t
)(struct auth_client
*, uint32_t);
74 struct auth_stats_handler
77 authd_stat_handler handler
;
84 uint32_t id
; /* Provider ID */
86 const char *name
; /* Name of the provider */
87 char letter
; /* Letter used on reject, etc. */
89 provider_init_t init
; /* Initalise the provider */
90 provider_destroy_t destroy
; /* Terminate the provider */
92 provider_start_t start
; /* Perform authentication */
93 provider_cancel_t cancel
; /* Authentication cancelled */
94 uint32_timeout_t timeout
; /* Timeout callback */
95 provider_complete_t completed
; /* Callback for when other performers complete (think dependency chains) */
97 struct auth_stats_handler stats_handler
;
99 struct auth_opts_handler
*opt_handlers
;
102 extern struct auth_provider rdns_provider
;
103 extern struct auth_provider ident_provider
;
104 extern struct auth_provider blacklist_provider
;
105 extern struct auth_provider opm_provider
;
107 extern rb_dictionary
*auth_providers
;
108 extern rb_dictionary
*auth_clients
;
110 void load_provider(struct auth_provider
*provider
);
111 void unload_provider(struct auth_provider
*provider
);
113 void init_providers(void);
114 void destroy_providers(void);
115 void cancel_providers(struct auth_client
*auth
);
117 void provider_done(struct auth_client
*auth
, uint32_t id
);
118 void accept_client(struct auth_client
*auth
, uint32_t id
);
119 void reject_client(struct auth_client
*auth
, uint32_t id
, const char *data
, const char *fmt
, ...);
121 void handle_new_connection(int parc
, char *parv
[]);
122 void handle_cancel_connection(int parc
, char *parv
[]);
125 /* Get a provider by name */
126 static inline struct auth_provider
*
127 get_provider(const char *name
)
129 return rb_dictionary_retrieve(auth_providers
, name
);
132 /* Get a provider's id by name */
134 get_provider_id(const char *name
, uint32_t *id
)
136 struct auth_provider
*provider
= get_provider(name
);
147 /* Get a provider's raw status */
148 static inline provider_status_t
149 get_provider_status(struct auth_client
*auth
, uint32_t provider
)
151 return auth
->data
[provider
].status
;
154 /* Set a provider's raw status */
156 set_provider_status(struct auth_client
*auth
, uint32_t provider
, provider_status_t status
)
158 auth
->data
[provider
].status
= status
;
161 /* Set the provider as running
162 * If you're doing asynchronous work call this */
164 set_provider_running(struct auth_client
*auth
, uint32_t provider
)
167 set_provider_status(auth
, provider
, PROVIDER_STATUS_RUNNING
);
170 /* Provider is no longer operating on this auth client
171 * You should use provider_done and not this */
173 set_provider_done(struct auth_client
*auth
, uint32_t provider
)
176 set_provider_status(auth
, provider
, PROVIDER_STATUS_DONE
);
179 /* Check if provider is operating on this auth client */
181 is_provider_running(struct auth_client
*auth
, uint32_t provider
)
183 return get_provider_status(auth
, provider
) == PROVIDER_STATUS_RUNNING
;
186 /* Check if provider has finished on this client */
188 is_provider_done(struct auth_client
*auth
, uint32_t provider
)
190 return get_provider_status(auth
, provider
) == PROVIDER_STATUS_DONE
;
193 /* Get provider auth client data */
195 get_provider_data(struct auth_client
*auth
, uint32_t id
)
197 return auth
->data
[id
].data
;
200 /* Set provider auth client data */
202 set_provider_data(struct auth_client
*auth
, uint32_t id
, void *data
)
204 auth
->data
[id
].data
= data
;
207 /* Set timeout relative to current time on provider
208 * When the timeout lapses, the provider's timeout call will execute */
210 set_provider_timeout_relative(struct auth_client
*auth
, uint32_t id
, time_t timeout
)
212 auth
->data
[id
].timeout
= timeout
+ rb_current_time();
215 /* Set timeout value in absolute time (Unix timestamp)
216 * When the timeout lapses, the provider's timeout call will execute */
218 set_provider_timeout_absolute(struct auth_client
*auth
, uint32_t id
, time_t timeout
)
220 auth
->data
[id
].timeout
= timeout
;
223 /* Get the timeout value for the provider */
225 get_provider_timeout(struct auth_client
*auth
, uint32_t id
)
227 return auth
->data
[id
].timeout
;
230 #endif /* __CHARYBDIS_AUTHD_PROVIDER_H__ */