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 */
30 /* Registered providers */
41 PROVIDER_STATUS_NOTRUN
= 0,
42 PROVIDER_STATUS_RUNNING
,
46 struct auth_client_data
48 time_t timeout
; /* Provider timeout */
49 void *data
; /* Provider data */
50 provider_status_t status
; /* Provider status */
55 uint16_t cid
; /* Client ID */
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 */
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 */
65 char hostname
[HOSTLEN
+ 1]; /* Used for DNS lookup */
66 char username
[USERLEN
+ 1]; /* Used for ident lookup */
68 bool providers_starting
; /* Providers are still warming up */
69 unsigned int refcount
; /* Held references */
71 struct auth_client_data
*data
; /* Provider-specific data */
74 typedef bool (*provider_init_t
)(void);
75 typedef void (*provider_destroy_t
)(void);
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
);
82 struct auth_stats_handler
85 authd_stat_handler handler
;
94 provider_init_t init
; /* Initalise the provider */
95 provider_destroy_t destroy
; /* Terminate the provider */
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) */
102 struct auth_stats_handler stats_handler
;
104 struct auth_opts_handler
*opt_handlers
;
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
;
112 extern rb_dlink_list auth_providers
;
113 extern rb_dictionary
*auth_clients
;
115 void load_provider(struct auth_provider
*provider
);
116 void unload_provider(struct auth_provider
*provider
);
118 void init_providers(void);
119 void destroy_providers(void);
120 void cancel_providers(struct auth_client
*auth
);
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
, ...);
126 void handle_new_connection(int parc
, char *parv
[]);
127 void handle_cancel_connection(int parc
, char *parv
[]);
130 /* Get a provider's raw status */
131 static inline provider_status_t
132 get_provider_status(struct auth_client
*auth
, provider_t provider
)
134 return auth
->data
[provider
].status
;
137 /* Set a provider's raw status */
139 set_provider_status(struct auth_client
*auth
, provider_t provider
, provider_status_t status
)
141 auth
->data
[provider
].status
= status
;
144 /* Set the provider as running
145 * If you're doing asynchronous work call this */
147 set_provider_running(struct auth_client
*auth
, provider_t provider
)
150 set_provider_status(auth
, provider
, PROVIDER_STATUS_RUNNING
);
153 /* Provider is no longer operating on this auth client
154 * You should use provider_done and not this */
156 set_provider_done(struct auth_client
*auth
, provider_t provider
)
159 set_provider_status(auth
, provider
, PROVIDER_STATUS_DONE
);
162 /* Check if provider is operating on this auth client */
164 is_provider_running(struct auth_client
*auth
, provider_t provider
)
166 return get_provider_status(auth
, provider
) == PROVIDER_STATUS_RUNNING
;
169 /* Check if provider has finished on this client */
171 is_provider_done(struct auth_client
*auth
, provider_t provider
)
173 return get_provider_status(auth
, provider
) == PROVIDER_STATUS_DONE
;
176 /* Get provider auth client data */
178 get_provider_data(struct auth_client
*auth
, uint32_t id
)
180 lrb_assert(id
< rb_dlink_list_length(&auth_providers
));
181 return auth
->data
[id
].data
;
184 /* Set provider auth client data */
186 set_provider_data(struct auth_client
*auth
, uint32_t id
, void *data
)
188 lrb_assert(id
< rb_dlink_list_length(&auth_providers
));
189 auth
->data
[id
].data
= data
;
192 /* Set timeout relative to current time on provider
193 * When the timeout lapses, the provider's timeout call will execute */
195 set_provider_timeout_relative(struct auth_client
*auth
, uint32_t id
, time_t timeout
)
197 lrb_assert(id
< rb_dlink_list_length(&auth_providers
));
198 auth
->data
[id
].timeout
= timeout
+ rb_current_time();
201 /* Set timeout value in absolute time (Unix timestamp)
202 * When the timeout lapses, the provider's timeout call will execute */
204 set_provider_timeout_absolute(struct auth_client
*auth
, uint32_t id
, time_t timeout
)
206 lrb_assert(id
< rb_dlink_list_length(&auth_providers
));
207 auth
->data
[id
].timeout
= timeout
;
210 /* Get the timeout value for the provider */
212 get_provider_timeout(struct auth_client
*auth
, uint32_t id
)
214 lrb_assert(id
< rb_dlink_list_length(&auth_providers
));
215 return auth
->data
[id
].timeout
;
218 #endif /* __CHARYBDIS_AUTHD_PROVIDER_H__ */