Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / include / radiusd.h
1 #ifndef RADIUSD_H
2 #define RADIUSD_H
3 /*
4  * radiusd.h    Structures, prototypes and global variables
5  *              for the FreeRADIUS server.
6  *
7  * Version:     $Id$
8  *
9  */
10
11 #include <freeradius-devel/ident.h>
12 RCSIDH(radiusd_h, "$Id$")
13
14 #include <freeradius-devel/libradius.h>
15 #include <freeradius-devel/radpaths.h>
16 #include <freeradius-devel/conf.h>
17 #include <freeradius-devel/conffile.h>
18 #include <freeradius-devel/realms.h>
19
20 #ifdef HAVE_PTHREAD_H
21 #include        <pthread.h>
22 typedef pthread_t child_pid_t;
23 #define child_kill pthread_kill
24 #else
25 typedef pid_t child_pid_t;
26 #define child_kill kill
27 #endif
28
29 #define NO_SUCH_CHILD_PID (child_pid_t) (0)
30
31 #ifndef NDEBUG
32 #define REQUEST_MAGIC (0xdeadbeef)
33 #endif
34
35 /*
36  *      See util.c
37  */
38 typedef struct request_data_t request_data_t;
39
40 /*
41  *      For listening on multiple IP's and ports.
42  */
43 typedef struct rad_listen_t rad_listen_t;
44
45 #define REQUEST_DATA_REGEX (0xadbeef00)
46 #define REQUEST_MAX_REGEX (8)
47
48 typedef struct auth_req {
49 #ifndef NDEBUG
50         uint32_t                magic; /* for debugging only */
51 #endif
52         RADIUS_PACKET           *packet;
53         RADIUS_PACKET           *proxy;
54         RADIUS_PACKET           *reply;
55         RADIUS_PACKET           *proxy_reply;
56         VALUE_PAIR              *config_items;
57         VALUE_PAIR              *username;
58         VALUE_PAIR              *password;
59
60         request_data_t          *data;
61         char                    secret[32];
62         child_pid_t             child_pid;
63         time_t                  timestamp;
64         int                     number; /* internal server number */
65
66         rad_listen_t            *listener;
67         rad_listen_t            *proxy_listener;
68
69         int                     simul_max;
70         int                     simul_count;
71         int                     simul_mpp; /* WEIRD: 1 is false, 2 is true */
72
73         int                     options; /* miscellanous options */
74         const char              *module; /* for debugging unresponsive children */
75         const char              *component; /* ditto */
76
77         struct timeval          received;
78         struct timeval          when;           /* to wake up */
79         int                     delay;
80
81         int                     master_state;
82         int                     child_state;
83
84         struct timeval          next_when;
85         void                    *next_callback;
86
87         int                     in_request_hash;
88         int                     in_proxy_hash;
89
90         home_server             *home_server;
91
92         struct timeval          proxy_when;
93
94         int                     num_proxied_requests;
95         int                     num_proxied_responses;
96
97 } REQUEST;
98
99 #define RAD_REQUEST_OPTION_NONE            (0)
100
101 #define REQUEST_ACTIVE          (1)
102 #define REQUEST_STOP_PROCESSING (2)
103
104 #define REQUEST_QUEUED          (1)
105 #define REQUEST_RUNNING         (2)
106 #define REQUEST_PROXIED         (3)
107 #define REQUEST_REJECT_DELAY    (4)
108 #define REQUEST_CLEANUP_DELAY   (5)
109 #define REQUEST_DONE            (6)
110
111 /*
112  *  Function handler for requests.
113  */
114 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
115
116 typedef struct radclient {
117         lrad_ipaddr_t           ipaddr;
118         int                     prefix;
119         char                    *longname;
120         char                    *secret;
121         char                    *shortname;
122         char                    *nastype;
123         char                    *login;
124         char                    *password;
125         int                     number; /* internal use only */
126 } RADCLIENT;
127
128 typedef struct radclient_list RADCLIENT_LIST;
129
130 typedef struct pair_list {
131         char                    *name;
132         VALUE_PAIR              *check;
133         VALUE_PAIR              *reply;
134         int                     lineno;
135         int                     order;
136         struct pair_list        *next;
137         struct pair_list        *lastdefault;
138 } PAIR_LIST;
139
140
141 /*
142  *      Types of listeners.
143  *
144  *      FIXME: Separate ports for proxy auth/acct?
145  */
146 typedef enum RAD_LISTEN_TYPE {
147         RAD_LISTEN_NONE = 0,
148         RAD_LISTEN_AUTH,
149         RAD_LISTEN_ACCT,
150         RAD_LISTEN_PROXY,
151         RAD_LISTEN_DETAIL,
152         RAD_LISTEN_MAX
153 } RAD_LISTEN_TYPE;
154
155 typedef int (*rad_listen_recv_t)(rad_listen_t *, RAD_REQUEST_FUNP *, REQUEST **);
156 typedef int (*rad_listen_send_t)(rad_listen_t *, REQUEST *);
157 typedef int (*rad_listen_print_t)(rad_listen_t *, char *, size_t);
158 typedef int (*rad_listen_encode_t)(rad_listen_t *, REQUEST *);
159 typedef int (*rad_listen_decode_t)(rad_listen_t *, REQUEST *);
160
161 struct rad_listen_t {
162         struct rad_listen_t *next; /* should be rbtree stuff */
163
164         /*
165          *      For normal sockets.
166          */
167         RAD_LISTEN_TYPE type;
168         int             fd;
169         const char      *identity;
170
171         rad_listen_recv_t recv;
172         rad_listen_send_t send;
173         rad_listen_encode_t encode;
174         rad_listen_decode_t decode;
175         rad_listen_print_t print;
176
177         void            *data;
178 };
179
180
181 typedef enum radlog_dest_t {
182   RADLOG_STDOUT = 0,
183   RADLOG_FILES,
184   RADLOG_SYSLOG,
185   RADLOG_STDERR,
186   RADLOG_NULL,
187   RADLOG_NUM_DEST
188 } radlog_dest_t;
189
190 typedef struct main_config_t {
191         struct main_config *next;
192         time_t          config_dead_time;
193         lrad_ipaddr_t   myip;   /* from the command-line only */
194         int             port;   /* from the command-line only */
195         int             log_auth;
196         int             log_auth_badpass;
197         int             log_auth_goodpass;
198 #ifdef WITH_SNMP
199         int             do_snmp;
200 #endif
201         int             allow_core_dumps;
202         int             debug_level;
203         int             proxy_requests;
204         int             wake_all_if_all_dead;
205         int             proxy_synchronous;
206         int             proxy_dead_time;
207         int             proxy_retry_count;
208         int             proxy_retry_delay;
209         int             proxy_fallback;
210         const char      *proxy_fail_type;
211         int             reject_delay;
212         int             status_server;
213         int             max_request_time;
214         int             cleanup_delay;
215         int             max_requests;
216 #ifdef DELETE_BLOCKED_REQUESTS
217         int             kill_unresponsive_children;
218 #endif
219         char            *log_file;
220         char            *checkrad;
221         const char      *pid_file;
222         const char      *uid_name;
223         const char      *gid_name;
224         rad_listen_t    *listen;
225         int             syslog_facility;
226         int             radlog_fd;
227         radlog_dest_t   radlog_dest;
228         CONF_SECTION    *config;
229         RADCLIENT_LIST  *clients;
230         const char      *radiusd_conf;
231 } MAIN_CONFIG_T;
232
233 #define DEBUG   if(debug_flag)log_debug
234 #define DEBUG2  if (debug_flag > 1)log_debug
235 #define DEBUG3  if (debug_flag > 2)log_debug
236
237 #define SECONDS_PER_DAY         86400
238 #define MAX_REQUEST_TIME        30
239 #define CLEANUP_DELAY           5
240 #define MAX_REQUESTS            256
241 #define RETRY_DELAY             5
242 #define RETRY_COUNT             3
243 #define DEAD_TIME               120
244
245 #define L_DBG                   1
246 #define L_AUTH                  2
247 #define L_INFO                  3
248 #define L_ERR                   4
249 #define L_PROXY                 5
250 #define L_ACCT                  6
251 #define L_CONS                  128
252
253 #ifndef FALSE
254 #define FALSE 0
255 #endif
256 #ifndef TRUE
257 /*
258  *      This definition of true as NOT false is definitive. :) Making
259  *      it '1' can cause problems on stupid platforms.  See articles
260  *      on C portability for more information.
261  */
262 #define TRUE (!FALSE)
263 #endif
264
265 /* for paircompare_register */
266 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
267
268 typedef enum request_fail_t {
269   REQUEST_FAIL_UNKNOWN = 0,
270   REQUEST_FAIL_NO_THREADS,      /* no threads to handle it */
271   REQUEST_FAIL_DECODE,          /* rad_decode didn't like it */
272   REQUEST_FAIL_PROXY,           /* call to proxy modules failed */
273   REQUEST_FAIL_PROXY_SEND,      /* proxy_send didn't like it */
274   REQUEST_FAIL_NO_RESPONSE,     /* we weren't told to respond, so we reject */
275   REQUEST_FAIL_HOME_SERVER,     /* the home server didn't respond */
276   REQUEST_FAIL_HOME_SERVER2,    /* another case of the above */
277   REQUEST_FAIL_HOME_SERVER3,    /* another case of the above */
278   REQUEST_FAIL_NORMAL_REJECT,   /* authentication failure */
279   REQUEST_FAIL_SERVER_TIMEOUT   /* the server took too long to process the request */
280 } request_fail_t;
281
282 /*
283  *      Global variables.
284  *
285  *      We really shouldn't have this many.
286  */
287 extern const char       *progname;
288 extern int              debug_flag;
289 extern const char       *radacct_dir;
290 extern const char       *radlog_dir;
291 extern const char       *radlib_dir;
292 extern const char       *radius_dir;
293 extern const char       *radius_libdir;
294 extern uint32_t         expiration_seconds;
295 extern int              log_stripped_names;
296 extern int              log_auth_detail;
297 extern const char      *radiusd_version;
298
299 /*
300  *      Function prototypes.
301  */
302
303 /* acct.c */
304 int             rad_accounting(REQUEST *);
305
306 /* session.c */
307 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
308                              const char *sessionid);
309 int             session_zap(REQUEST *request, uint32_t nasaddr,
310                             unsigned int port, const char *user,
311                             const char *sessionid, uint32_t cliaddr,
312                             char proto,int session_time);
313
314 /* radiusd.c */
315 #ifndef _LIBRADIUS
316 void            debug_pair(FILE *, VALUE_PAIR *);
317 #endif
318 int             log_err (char *);
319
320 /* util.c */
321 void (*reset_signal(int signo, void (*func)(int)))(int);
322 void            request_free(REQUEST **request);
323 int             rad_mkdir(char *directory, int mode);
324 int             rad_checkfilename(const char *filename);
325 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
326 REQUEST         *request_alloc(void);
327 REQUEST         *request_alloc_fake(REQUEST *oldreq);
328 int             request_data_add(REQUEST *request,
329                                  void *unique_ptr, int unique_int,
330                                  void *opaque, void (*free_opaque)(void *));
331 void            *request_data_get(REQUEST *request,
332                                   void *unique_ptr, int unique_int);
333 void            *request_data_reference(REQUEST *request,
334                                   void *unique_ptr, int unique_int);
335 int             rad_copy_string(char *dst, const char *src);
336 int             rad_copy_variable(char *dst, const char *from);
337
338 /* request_process.c */
339 int rad_respond(REQUEST *request, RAD_REQUEST_FUNP fun);
340 void            request_reject(REQUEST *request, request_fail_t reason);
341
342 /* client.c */
343 RADCLIENT_LIST  *clients_init(void);
344 void            clients_free(RADCLIENT_LIST *clients);
345 RADCLIENT_LIST  *clients_parse_section(const char *filename,
346                                        CONF_SECTION *section);
347 void            client_free(RADCLIENT *client);
348 int             client_add(RADCLIENT_LIST *clients, RADCLIENT *client);
349 RADCLIENT       *client_find(const RADCLIENT_LIST *clients,
350                              const lrad_ipaddr_t *ipaddr);
351 const char      *client_name(const RADCLIENT_LIST *clients,
352                              const lrad_ipaddr_t *ipaddr);
353 RADCLIENT       *client_findbynumber(const RADCLIENT_LIST *clients,
354                                      int number);
355 RADCLIENT       *client_find_old(const lrad_ipaddr_t *ipaddr);
356 const char      *client_name_old(const lrad_ipaddr_t *ipaddr);
357
358 /* files.c */
359 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
360 void            pairlist_free(PAIR_LIST **);
361 int             read_config_files(void);
362
363 /* version.c */
364 void            version(void);
365
366 /* log.c */
367 int             vradlog(int, const char *, va_list ap);
368 int             radlog(int, const char *, ...)
369 #ifdef __GNUC__
370                 __attribute__ ((format (printf, 2, 3)))
371 #endif
372 ;
373 int             log_debug(const char *, ...)
374 #ifdef __GNUC__
375                 __attribute__ ((format (printf, 1, 2)))
376 #endif
377 ;
378 void            vp_listdebug(VALUE_PAIR *vp);
379
380 /* proxy.c */
381 int proxy_receive(REQUEST *request);
382 int proxy_send(REQUEST *request);
383
384 /* auth.c */
385 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
386 int             rad_authenticate (REQUEST *);
387 int             rad_postauth(REQUEST *);
388
389 /* exec.c */
390 int             radius_exec_program(const char *,  REQUEST *, int,
391                                     char *user_msg, int msg_len,
392                                     VALUE_PAIR *input_pairs,
393                                     VALUE_PAIR **output_pairs,
394                                         int shell_escape);
395
396 /* timestr.c */
397 int             timestr_match(char *, time_t);
398
399 /* valuepair.c */
400 int             paircompare_register(int attr, int otherattr,
401                                      RAD_COMPARE_FUNC func,
402                                      void *instance);
403 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
404 int             paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
405                             VALUE_PAIR **reply);
406 int             simplepaircmp(REQUEST *, VALUE_PAIR *, VALUE_PAIR *);
407 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
408
409 /* xlat.c */
410 typedef int (*RADIUS_ESCAPE_STRING)(char *out, int outlen, const char *in);
411
412 int            radius_xlat(char * out, int outlen, const char *fmt,
413                            REQUEST * request, RADIUS_ESCAPE_STRING func);
414 typedef int (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING func);
415 int             xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance);
416 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
417
418
419 /* threads.c */
420 extern          int thread_pool_init(int spawn_flag);
421 extern          int thread_pool_clean(time_t now);
422 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
423 extern          pid_t rad_fork(void);
424 extern          pid_t rad_waitpid(pid_t pid, int *status);
425 extern          int total_active_threads(void);
426
427 #ifndef HAVE_PTHREAD_H
428 #define rad_fork(n) fork()
429 #define rad_waitpid(a,b) waitpid(a,b, 0)
430 #endif
431
432 /* mainconfig.c */
433 /* Define a global config structure */
434 extern struct main_config_t mainconfig;
435
436 int read_mainconfig(int reload);
437 int free_mainconfig(void);
438
439 /* listen.c */
440 void listen_free(rad_listen_t **head);
441 int listen_init(const char *filename, rad_listen_t **head);
442 rad_listen_t *proxy_new_listener(void);
443
444 /* event.c */
445 int radius_event_init(int spawn_flag);
446 void radius_event_free(void);
447 int radius_event_process(struct timeval **pptv);
448 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun);
449 int received_request(rad_listen_t *listener,
450                      RADIUS_PACKET *packet, REQUEST **prequest,
451                      const RADCLIENT *client);
452 REQUEST *received_proxy_response(RADIUS_PACKET *packet);
453
454 #endif /*RADIUSD_H*/