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