Quick hack for colourising log output to stderr/stdout
[freeradius.git] / src / include / radiusd.h
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef RADIUSD_H
17 #define RADIUSD_H
18 /*
19  * $Id$
20  *
21  * @file radiusd.h
22  * @brief Structures, prototypes and global variables for the FreeRADIUS server.
23  *
24  * @copyright 1999-2000,2002-2008  The FreeRADIUS server project
25  */
26
27 #include <freeradius-devel/ident.h>
28 RCSIDH(radiusd_h, "$Id$")
29
30 #include <freeradius-devel/libradius.h>
31 #include <freeradius-devel/radpaths.h>
32 #include <freeradius-devel/conf.h>
33 #include <freeradius-devel/conffile.h>
34 #include <freeradius-devel/event.h>
35 #include <freeradius-devel/connection.h>
36
37 typedef struct auth_req REQUEST;
38
39 #ifdef HAVE_PTHREAD_H
40 #include        <pthread.h>
41 #endif
42
43 #ifndef NDEBUG
44 #define REQUEST_MAGIC (0xdeadbeef)
45 #endif
46
47 /*
48  *      WITH_VMPS is handled by src/include/features.h
49  */
50 #ifdef WITHOUT_VMPS
51 #undef WITH_VMPS
52 #endif
53
54 #ifdef WITH_TLS
55 #include <freeradius-devel/tls.h>
56 #endif
57
58 #include <freeradius-devel/stats.h>
59 #include <freeradius-devel/realms.h>
60
61 #ifdef WITH_COMMAND_SOCKET
62 #define PW_RADMIN_PORT 18120
63 #endif
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 /*
70  *      See util.c
71  */
72 typedef struct request_data_t request_data_t;
73
74 typedef struct radclient {
75         fr_ipaddr_t             ipaddr;
76         fr_ipaddr_t             src_ipaddr;
77         int                     prefix;
78         char                    *longname;
79         char                    *secret;
80         char                    *shortname;
81         int                     message_authenticator;
82         char                    *nastype;
83         char                    *login;
84         char                    *password;
85         char                    *server;
86         int                     number; /* internal use only */
87         const CONF_SECTION      *cs;
88 #ifdef WITH_STATS
89         fr_stats_t              auth;
90 #ifdef WITH_ACCOUNTING
91         fr_stats_t              acct;
92 #endif
93 #ifdef WITH_COA
94         fr_stats_t              coa;
95         fr_stats_t              dsc;
96 #endif
97 #endif
98
99         int                     proto;
100 #ifdef WITH_TCP
101         fr_socket_limit_t       limit;
102 #endif
103
104 #ifdef WITH_DYNAMIC_CLIENTS
105         int                     lifetime;
106         int                     dynamic; /* was dynamically defined */
107         time_t                  created;
108         time_t                  last_new_client;
109         char                    *client_server;
110         int                     rate_limit;
111 #endif
112
113 #ifdef WITH_COA
114         char                    *coa_name;
115         home_server             *coa_server;
116         home_pool_t             *coa_pool;
117 #endif
118 } RADCLIENT;
119
120 /*
121  *      Types of listeners.
122  *
123  *      Ordered by priority!
124  */
125 typedef enum RAD_LISTEN_TYPE {
126         RAD_LISTEN_NONE = 0,
127 #ifdef WITH_PROXY
128         RAD_LISTEN_PROXY,
129 #endif
130         RAD_LISTEN_AUTH,
131 #ifdef WITH_ACCOUNTING
132         RAD_LISTEN_ACCT,
133 #endif
134 #ifdef WITH_DETAIL
135         RAD_LISTEN_DETAIL,
136 #endif
137 #ifdef WITH_VMPS
138         RAD_LISTEN_VQP,
139 #endif
140 #ifdef WITH_DHCP
141         RAD_LISTEN_DHCP,
142 #endif
143 #ifdef WITH_COMMAND_SOCKET
144         RAD_LISTEN_COMMAND,
145 #endif
146 #ifdef WITH_COA
147         RAD_LISTEN_COA,
148 #endif
149         RAD_LISTEN_MAX
150 } RAD_LISTEN_TYPE;
151
152
153 /*
154  *      For listening on multiple IP's and ports.
155  */
156 typedef struct rad_listen_t rad_listen_t;
157 typedef         void (*radlog_func_t)(int, int, REQUEST *, const char *, ...);
158
159 typedef         void (*fr_request_process_t)(REQUEST *, int);
160 /*
161  *  Function handler for requests.
162  */
163 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
164
165 #define REQUEST_DATA_REGEX (0xadbeef00)
166 #define REQUEST_MAX_REGEX (8)
167
168 struct auth_req {
169 #ifndef NDEBUG
170         uint32_t                magic; /* for debugging only */
171 #endif
172         RADIUS_PACKET           *packet;
173 #ifdef WITH_PROXY
174         RADIUS_PACKET           *proxy;
175 #endif
176         RADIUS_PACKET           *reply;
177 #ifdef WITH_PROXY
178         RADIUS_PACKET           *proxy_reply;
179 #endif
180         VALUE_PAIR              *config_items;
181         VALUE_PAIR              *username;
182         VALUE_PAIR              *password;
183
184         fr_request_process_t    process;
185         RAD_REQUEST_FUNP        handle;
186         struct main_config_t    *root;
187
188         request_data_t          *data;
189         RADCLIENT               *client;
190 #ifdef HAVE_PTHREAD_H
191         pthread_t               child_pid;
192 #endif
193         time_t                  timestamp;
194         unsigned int            number; /* internal server number */
195
196         rad_listen_t            *listener;
197 #ifdef WITH_PROXY
198         rad_listen_t            *proxy_listener;
199 #endif
200
201
202         int                     simul_max; /* see modcall.c && xlat.c */
203 #ifdef WITH_SESSION_MGMT
204         int                     simul_count;
205         int                     simul_mpp; /* WEIRD: 1 is false, 2 is true */
206 #endif
207
208         int                     options; /* miscellanous options */
209         const char              *module; /* for debugging unresponsive children */
210         const char              *component; /* ditto */
211
212         int                     delay;
213
214         int                     master_state;
215         int                     child_state;
216         RAD_LISTEN_TYPE         priority;
217
218         int                     timer_action;
219         fr_event_t              *ev;
220
221         int                     in_request_hash;
222 #ifdef WITH_PROXY
223         int                     in_proxy_hash;
224
225         home_server             *home_server;
226         home_pool_t             *home_pool; /* for dynamic failover */
227
228         struct timeval          proxy_retransmit;
229
230         int                     num_proxied_requests;
231         int                     num_proxied_responses;
232 #endif
233
234         const char              *server;
235         REQUEST                 *parent;
236         radlog_func_t           radlog; /* logging function, if set */
237 #ifdef WITH_COA
238         REQUEST                 *coa;
239         int                     num_coa_requests;
240 #endif
241 };                              /* REQUEST typedef */
242
243 #define RAD_REQUEST_OPTION_NONE            (0)
244 #define RAD_REQUEST_OPTION_DEBUG           (1)
245 #define RAD_REQUEST_OPTION_DEBUG2          (2)
246 #define RAD_REQUEST_OPTION_DEBUG3          (3)
247 #define RAD_REQUEST_OPTION_DEBUG4          (4)
248
249 #define REQUEST_ACTIVE          (1)
250 #define REQUEST_STOP_PROCESSING (2)
251 #define REQUEST_COUNTED         (3)
252
253 #define REQUEST_QUEUED          (1)
254 #define REQUEST_RUNNING         (2)
255 #define REQUEST_PROXIED         (3)
256 #define REQUEST_REJECT_DELAY    (4)
257 #define REQUEST_CLEANUP_DELAY   (5)
258 #define REQUEST_DONE            (6)
259
260 typedef struct radclient_list RADCLIENT_LIST;
261
262 typedef enum pair_lists {
263         PAIR_LIST_UNKNOWN = 0,
264         PAIR_LIST_REQUEST,
265         PAIR_LIST_REPLY,
266         PAIR_LIST_CONTROL,
267 #ifdef WITH_PROXY
268         PAIR_LIST_PROXY_REQUEST,
269         PAIR_LIST_PROXY_REPLY,
270 #endif
271 #ifdef WITH_COA
272         PAIR_LIST_COA,
273         PAIR_LIST_COA_REPLY,
274         PAIR_LIST_DM,
275         PAIR_LIST_DM_REPLY
276 #endif
277 } pair_lists_t;
278
279 extern const FR_NAME_NUMBER pair_lists[];
280
281 typedef enum requests {
282         REQUEST_UNKNOWN = 0,
283         REQUEST_OUTER,
284         REQUEST_CURRENT,
285         REQUEST_PARENT  /* For future use */
286 } request_refs_t;
287
288 extern const FR_NAME_NUMBER request_refs[];
289
290 typedef struct pair_list {
291         const char              *name;
292         VALUE_PAIR              *check;
293         VALUE_PAIR              *reply;
294         int                     lineno;
295         int                     order;
296         struct pair_list        *next;
297         struct pair_list        *lastdefault;
298 } PAIR_LIST;
299
300 typedef int (*rad_listen_recv_t)(rad_listen_t *);
301 typedef int (*rad_listen_send_t)(rad_listen_t *, REQUEST *);
302 typedef int (*rad_listen_print_t)(const rad_listen_t *, char *, size_t);
303 typedef int (*rad_listen_encode_t)(rad_listen_t *, REQUEST *);
304 typedef int (*rad_listen_decode_t)(rad_listen_t *, REQUEST *);
305
306 struct rad_listen_t {
307         struct rad_listen_t *next; /* should be rbtree stuff */
308
309         /*
310          *      For normal sockets.
311          */
312         RAD_LISTEN_TYPE type;
313         int             fd;
314         const char      *server;
315         int             status;
316 #ifdef WITH_TCP
317         int             count;
318 #endif
319
320 #ifdef WITH_TLS
321         fr_tls_server_conf_t *tls;
322 #endif
323
324         rad_listen_recv_t recv;
325         rad_listen_send_t send;
326         rad_listen_encode_t encode;
327         rad_listen_decode_t decode;
328         rad_listen_print_t print;
329
330         const CONF_SECTION *cs;
331         void            *data;
332
333 #ifdef WITH_STATS
334         fr_stats_t      stats;
335 #endif
336 };
337
338 /*
339  *      This shouldn't really be exposed...
340  */
341 typedef struct listen_socket_t {
342         /*
343          *      For normal sockets.
344          */
345         fr_ipaddr_t     my_ipaddr;
346         int             my_port;
347
348         const char      *interface;
349 #ifdef SO_BROADCAST
350         int             broadcast;
351 #endif
352         time_t          rate_time;
353         int             rate_pps_old;
354         int             rate_pps_now;
355         int             max_rate;
356         
357         /* for outgoing sockets */
358         home_server     *home;
359         fr_ipaddr_t     other_ipaddr;
360         int             other_port;
361
362         int             proto;
363
364 #ifdef WITH_TCP
365         /* for a proxy connecting to home servers */
366         time_t          last_packet;
367         time_t          opened;
368         fr_event_t      *ev;
369
370         fr_socket_limit_t limit;
371
372         struct listen_socket_t *parent;
373         RADCLIENT       *client;
374
375         RADIUS_PACKET   *packet; /* for reading partial packets */
376 #endif
377
378 #ifdef WITH_TLS
379         tls_session_t   *ssn;
380         REQUEST         *request; /* horrible hacks */
381         VALUE_PAIR      *certs;
382         pthread_mutex_t mutex;
383         uint8_t         *data;
384 #endif
385
386         RADCLIENT_LIST  *clients;
387 } listen_socket_t;
388
389 #define RAD_LISTEN_STATUS_INIT   (0)
390 #define RAD_LISTEN_STATUS_KNOWN  (1)
391 #define RAD_LISTEN_STATUS_REMOVE_FD (2)
392 #define RAD_LISTEN_STATUS_CLOSED (3)
393 #define RAD_LISTEN_STATUS_FINISH (4)
394
395 typedef enum radlog_dest_t {
396   RADLOG_STDOUT = 0,
397   RADLOG_FILES,
398   RADLOG_SYSLOG,
399   RADLOG_STDERR,
400   RADLOG_NULL,
401   RADLOG_NUM_DEST
402 } radlog_dest_t;
403
404 typedef struct main_config_t {
405         struct main_config *next;
406         int             refcount;
407         fr_ipaddr_t     myip;   /* from the command-line only */
408         int             port;   /* from the command-line only */
409         int             log_auth;
410         int             log_auth_badpass;
411         int             log_auth_goodpass;
412         int             allow_core_dumps;
413         int             debug_level;
414 #ifdef WITH_PROXY
415         int             proxy_requests;
416 #endif
417         int             reject_delay;
418         int             status_server;
419         int             max_request_time;
420         int             cleanup_delay;
421         int             max_requests;
422 #ifdef DELETE_BLOCKED_REQUESTS
423         int             kill_unresponsive_children;
424 #endif
425         char            *log_file;
426         char            *checkrad;
427         const char      *pid_file;
428         rad_listen_t    *listen;
429         int             syslog_facility;
430         int             radlog_fd;
431         radlog_dest_t   radlog_dest;
432         CONF_SECTION    *config;
433         const char      *name;
434         const char      *auth_badpass_msg;
435         const char      *auth_goodpass_msg;
436         int             colourise;      //!< Messages output to stderr and
437                                         //!< stdout may be formatted using 
438                                         //!< VT100 escape sequences.
439 } MAIN_CONFIG_T;
440
441 #define DEBUG   if(debug_flag)log_debug
442 #define DEBUG2  if (debug_flag > 1)log_debug
443 #define DEBUG3  if (debug_flag > 2)log_debug
444 #define DEBUG4  if (debug_flag > 3)log_debug
445
446 #if __GNUC__ >= 3
447 #define RDEBUG(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG, 1, request, fmt, ## __VA_ARGS__)
448 #define RDEBUG2(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 2, request, fmt, ## __VA_ARGS__)
449 #define RDEBUG3(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 3, request, fmt, ## __VA_ARGS__)
450 #define RDEBUG4(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 4, request, fmt, ## __VA_ARGS__)
451 #else
452 #define RDEBUG  DEBUG
453 #define RDEBUG2 DEBUG2
454 #define RDEBUG3 DEBUG3
455 #define RDEBUG4 DEBUG4
456 #endif
457
458 #define SECONDS_PER_DAY         86400
459 #define MAX_REQUEST_TIME        30
460 #define CLEANUP_DELAY           5
461 #define MAX_REQUESTS            256
462 #define RETRY_DELAY             5
463 #define RETRY_COUNT             3
464 #define DEAD_TIME               120
465
466 #define L_DBG                   1
467 #define L_AUTH                  2
468 #define L_INFO                  3
469 #define L_ERR                   4
470 #define L_PROXY                 5
471 #define L_ACCT                  6
472 #define L_CONS                  128
473
474 /* for paircompare_register */
475 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
476
477 typedef enum request_fail {
478         REQUEST_FAIL_UNKNOWN = 0,
479         REQUEST_FAIL_NO_THREADS,        //!< No threads to handle it.
480         REQUEST_FAIL_DECODE,            //!< Rad_decode didn't like it.
481         REQUEST_FAIL_PROXY,             //!< Call to proxy modules failed.
482         REQUEST_FAIL_PROXY_SEND,        //!< Proxy_send didn't like it.
483         REQUEST_FAIL_NO_RESPONSE,       //!< We weren't told to respond, 
484                                         //!< so we reject.
485         REQUEST_FAIL_HOME_SERVER,       //!< The home server didn't respond.
486         REQUEST_FAIL_HOME_SERVER2,      //!< Another case of the above.
487         REQUEST_FAIL_HOME_SERVER3,      //!< Another case of the above.
488         REQUEST_FAIL_NORMAL_REJECT,     //!< Authentication failure.
489         REQUEST_FAIL_SERVER_TIMEOUT     //!< The server took too long to 
490                                         //!< process the request.
491 } request_fail_t;
492
493 /*
494  *      Global variables.
495  *
496  *      We really shouldn't have this many.
497  */
498 extern const char       *progname;
499 extern int              debug_flag;
500 extern const char       *radacct_dir;
501 extern const char       *radlog_dir;
502 extern const char       *radlib_dir;
503 extern char             *radius_dir;
504 extern const char       *radius_libdir;
505 extern uint32_t         expiration_seconds;
506 extern int              log_stripped_names;
507 extern int              log_auth_detail;
508 extern const char      *radiusd_version;
509 void                    radius_signal_self(int flag);
510
511 #define RADIUS_SIGNAL_SELF_NONE         (0)
512 #define RADIUS_SIGNAL_SELF_HUP          (1 << 0)
513 #define RADIUS_SIGNAL_SELF_TERM         (1 << 1)
514 #define RADIUS_SIGNAL_SELF_EXIT         (1 << 2)
515 #define RADIUS_SIGNAL_SELF_DETAIL       (1 << 3)
516 #define RADIUS_SIGNAL_SELF_NEW_FD       (1 << 4)
517 #define RADIUS_SIGNAL_SELF_MAX          (1 << 5)
518
519
520 /*
521  *      Function prototypes.
522  */
523
524 /* acct.c */
525 int             rad_accounting(REQUEST *);
526
527 /* session.c */
528 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
529                              const char *sessionid);
530 int             session_zap(REQUEST *request, uint32_t nasaddr,
531                             unsigned int port, const char *user,
532                             const char *sessionid, uint32_t cliaddr,
533                             char proto,int session_time);
534
535 /* radiusd.c */
536 #undef debug_pair
537 void            debug_pair(VALUE_PAIR *);
538 void            debug_pair_list(VALUE_PAIR *);
539 void            rdebug_pair_list(int, REQUEST *, VALUE_PAIR *);
540 int             log_err (char *);
541
542 /* util.c */
543 void (*reset_signal(int signo, void (*func)(int)))(int);
544 void            request_free(REQUEST **request);
545 int             rad_mkdir(char *directory, mode_t mode);
546 int             rad_checkfilename(const char *filename);
547 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
548 void            *rad_calloc(size_t size); /* calls exit(1) on error! */
549 void            rad_cfree(const void *ptr);
550 REQUEST         *request_alloc(void);
551 REQUEST         *request_alloc_fake(REQUEST *oldreq);
552 REQUEST         *request_alloc_coa(REQUEST *request);
553 int             request_data_add(REQUEST *request,
554                                  void *unique_ptr, int unique_int,
555                                  void *opaque, void (*free_opaque)(void *));
556 void            *request_data_get(REQUEST *request,
557                                   void *unique_ptr, int unique_int);
558 void            *request_data_reference(REQUEST *request,
559                                   void *unique_ptr, int unique_int);
560 int             rad_copy_string(char *dst, const char *src);
561 int             rad_copy_variable(char *dst, const char *from);
562 int             rad_pps(int *past, int *present, time_t *then,
563                         struct timeval *now);
564
565 /* client.c */
566 RADCLIENT_LIST  *clients_init(void);
567 void            clients_free(RADCLIENT_LIST *clients);
568 RADCLIENT_LIST  *clients_parse_section(CONF_SECTION *section);
569 void            client_free(RADCLIENT *client);
570 int             client_add(RADCLIENT_LIST *clients, RADCLIENT *client);
571 #ifdef WITH_DYNAMIC_CLIENTS
572 void            client_delete(RADCLIENT_LIST *clients, RADCLIENT *client);
573 RADCLIENT       *client_create(RADCLIENT_LIST *clients, REQUEST *request);
574 #endif
575 RADCLIENT       *client_find(const RADCLIENT_LIST *clients,
576                              const fr_ipaddr_t *ipaddr, int proto);
577
578 RADCLIENT       *client_findbynumber(const RADCLIENT_LIST *clients,
579                                      int number);
580 RADCLIENT       *client_find_old(const fr_ipaddr_t *ipaddr);
581 int             client_validate(RADCLIENT_LIST *clients, RADCLIENT *master,
582                                 RADCLIENT *c);
583 RADCLIENT       *client_read(const char *filename, int in_server, int flag);
584
585
586 /* files.c */
587 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
588 void            pairlist_free(PAIR_LIST **);
589
590 /* version.c */
591 int             ssl_check_version(void);
592 const char      *ssl_version(void);
593 void            version(void);
594
595 /* log.c */
596 int             vradlog(int, const char *, va_list ap);
597 int             radlog(int, const char *, ...)
598 #ifdef __GNUC__
599                 __attribute__ ((format (printf, 2, 3)))
600 #endif
601 ;
602 int             log_debug(const char *, ...)
603 #ifdef __GNUC__
604                 __attribute__ ((format (printf, 1, 2)))
605 #endif
606 ;
607 void            vp_listdebug(VALUE_PAIR *vp);
608 void radlog_request(int lvl, int priority, REQUEST *request, const char *msg, ...)
609 #ifdef __GNUC__
610                 __attribute__ ((format (printf, 4, 5)))
611 #endif
612 ;
613
614 /* auth.c */
615 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
616 int             rad_authenticate (REQUEST *);
617 int             rad_postauth(REQUEST *);
618 int             rad_virtual_server(REQUEST *);
619
620 /* exec.c */
621 pid_t radius_start_program(const char *cmd, REQUEST *request,
622                         int exec_wait,
623                         int *input_fd,
624                         int *output_fd,
625                         VALUE_PAIR *input_pairs,
626                         int shell_escape);
627 int radius_readfrom_program(int fd, pid_t pid, int timeout, char *answer, int left);
628 int             radius_exec_program(const char *,  REQUEST *, int,
629                                     char *user_msg, int msg_len,
630                                     VALUE_PAIR *input_pairs,
631                                     VALUE_PAIR **output_pairs,
632                                         int shell_escape);
633 void exec_trigger(REQUEST *request, CONF_SECTION *cs, const char *name, int quench);
634
635 /* timestr.c */
636 int             timestr_match(char *, time_t);
637
638 /* valuepair.c */
639 int             paircompare_register(unsigned int attr, int otherattr,
640                                      RAD_COMPARE_FUNC func,
641                                      void *instance);
642 void            paircompare_unregister(unsigned int attr, RAD_COMPARE_FUNC func);
643 int             paircompare(REQUEST *request, VALUE_PAIR *req_list,
644                             VALUE_PAIR *check, VALUE_PAIR **rep_list);
645 int             radius_xlat_do(REQUEST *request, VALUE_PAIR *vp);
646 void            radius_xlat_move(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
647 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp);
648 int radius_callback_compare(REQUEST *req, VALUE_PAIR *request,
649                             VALUE_PAIR *check, VALUE_PAIR *check_pairs,
650                             VALUE_PAIR **reply_pairs);
651 int radius_find_compare(unsigned int attribute);
652 VALUE_PAIR      *radius_paircreate(REQUEST *request, VALUE_PAIR **vps,
653                                    unsigned int attribute, unsigned int vendor);
654 VALUE_PAIR *radius_pairmake(REQUEST *request, VALUE_PAIR **vps,
655                             const char *attribute, const char *value,
656                             FR_TOKEN op);
657
658 /* xlat.c */
659 typedef size_t (*RADIUS_ESCAPE_STRING)(REQUEST *, char *out, size_t outlen, const char *in, void *arg);
660
661 size_t          radius_xlat(char * out, int outlen, const char *fmt,
662                             REQUEST * request, RADIUS_ESCAPE_STRING func, void *funcarg);
663 typedef size_t (*RAD_XLAT_FUNC)(void *instance, REQUEST *, const char *, char *, size_t);
664 int             xlat_register(const char *module, RAD_XLAT_FUNC func,
665                               void *instance);
666 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func,
667                                 void *instance);
668 void            xlat_free(void);
669
670 /* threads.c */
671 extern          int thread_pool_init(CONF_SECTION *cs, int *spawn_flag);
672 extern          void thread_pool_stop(void);
673 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
674 extern          pid_t rad_fork(void);
675 extern          pid_t rad_waitpid(pid_t pid, int *status);
676 extern          int total_active_threads(void);
677 extern          void thread_pool_lock(void);
678 extern          void thread_pool_unlock(void);
679 extern          void thread_pool_queue_stats(int array[RAD_LISTEN_MAX], int pps[2]);
680
681 #ifndef HAVE_PTHREAD_H
682 #define rad_fork(n) fork()
683 #define rad_waitpid(a,b) waitpid(a,b, 0)
684 #endif
685
686 /* mainconfig.c */
687 /* Define a global config structure */
688 extern struct main_config_t mainconfig;
689
690 int read_mainconfig(int reload);
691 int free_mainconfig(void);
692 void hup_mainconfig(void);
693 void hup_logfile(void);
694 void fr_suid_down(void);
695 void fr_suid_up(void);
696 void fr_suid_down_permanent(void);
697
698 /* listen.c */
699 void listen_free(rad_listen_t **head);
700 int listen_init(CONF_SECTION *cs, rad_listen_t **head, int spawn_flag);
701 int proxy_new_listener(home_server *home, int src_port);
702 RADCLIENT *client_listener_find(rad_listen_t *listener,
703                                 const fr_ipaddr_t *ipaddr, int src_port);
704
705 #ifdef WITH_STATS
706 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
707                                           int port);
708 #endif
709 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port,
710                                      int proto);
711 int rad_status_server(REQUEST *request);
712
713 /* event.c */
714 int radius_event_init(CONF_SECTION *cs, int spawn_flag);
715 void radius_event_free(void);
716 int radius_event_process(void);
717 int event_new_fd(rad_listen_t *listener);
718 void revive_home_server(void *ctx);
719 void mark_home_server_dead(home_server *home, struct timeval *when);
720
721 /* evaluate.c */
722 int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
723                               const char **ptr, int evaluate_it, int *presult);
724 int radius_update_attrlist(REQUEST *request, CONF_SECTION *cs,
725                            VALUE_PAIR *input_vps, const char *name);
726 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from);
727
728 VALUE_PAIR **radius_list(REQUEST *request, pair_lists_t list);
729 pair_lists_t radius_list_name(const char **name, pair_lists_t unknown);
730 int radius_request(REQUEST **request, request_refs_t name);
731 request_refs_t radius_request_name(const char **name, request_refs_t unknown);
732
733
734 typedef enum vpt_type {
735         VPT_TYPE_UNKNOWN = 0,
736         VPT_TYPE_LITERAL,               //!< Is a literal string.
737         VPT_TYPE_XLAT,                  //!< Needs to be expanded.
738         VPT_TYPE_ATTR,                  //!< Is an attribute.
739         VPT_TYPE_LIST,                  //!< Is a list.
740         VPT_TYPE_EXEC                   //!< Needs to be executed.
741 } vpt_type_t;
742
743 extern const FR_NAME_NUMBER vpt_types[];
744
745 /** A pre-parsed template attribute
746  *  
747  *  Value pair template, used when processing various mappings sections
748  *  to create a real valuepair later.
749  *
750  * @see value_pair_map_t
751  */
752 typedef struct value_pair_tmpl_t {
753         const char              *name;   //!< Original attribute ref string, or
754                                          //!< where this refers to a none FR
755                                          //!< attribute, just the string id for
756                                          //!< the attribute.
757
758         request_refs_t          request; //!< Request to search or insert in.
759         pair_lists_t            list;    //!< List to search or insert in.
760                                        
761         const DICT_ATTR         *da;     //!< Resolved dictionary attribute.
762         vpt_type_t              type;    //!< What type of value tmpl refers to.
763 } value_pair_tmpl_t;
764
765 /** Value pair map
766  *
767  * Value pair maps contain a pair of templates, that describe a src attribute
768  * or value, and a destination attribute.
769  *
770  * Neither src or dst need to be an FR attribute, and their type can be inferred
771  * from whether map->da is NULL (not FR).
772  *
773  * @see value_pair_tmpl_t
774  */
775 typedef struct value_pair_map {
776         value_pair_tmpl_t       *dst;   //!< Typically describes the attribute
777                                         //!< to add or modify.
778         value_pair_tmpl_t       *src;   //!< Typically describes a value or a
779                                         //!< src attribute to copy.
780         
781         FR_TOKEN                op;     //!< The operator that controls
782                                         //!< insertion of the dst attribute.
783         
784         CONF_ITEM               *ci;    //!< Config item that the map was 
785                                         //!< created from. Mainly used for 
786                                         //!< logging validation errors.
787         
788         struct value_pair_map   *next;  //!< The next valuepair map.
789 } value_pair_map_t;
790
791 typedef VALUE_PAIR *(*radius_tmpl_getvalue_t)(REQUEST *request,
792                                               const value_pair_map_t *map,
793                                               void *ctx);
794 void radius_tmplfree(value_pair_tmpl_t **tmpl);
795 int radius_parse_attr(const char *name, value_pair_tmpl_t *vpt,
796                       request_refs_t request_def,
797                       pair_lists_t list_def);
798 value_pair_tmpl_t *radius_attr2tmpl(const char *name,
799                                     request_refs_t request_def,
800                                     pair_lists_t list_def);
801                                     
802 value_pair_tmpl_t *radius_str2tmpl(const char *name, FR_TOKEN type);
803 int radius_attrmap(CONF_SECTION *cs, value_pair_map_t **head,
804                    pair_lists_t dst_list_def, pair_lists_t src_list_def,
805                    unsigned int max);
806 value_pair_map_t *radius_cp2map(CONF_PAIR *cp,
807                                 request_refs_t dst_request_def,
808                                 pair_lists_t dst_list_def,
809                                 request_refs_t src_request_def,
810                                 pair_lists_t src_list_def);
811                                 
812 int radius_map2request(REQUEST *request, const value_pair_map_t *map,
813                        const char *src, radius_tmpl_getvalue_t func, void *ctx);
814 void radius_mapfree(value_pair_map_t **map);
815
816 int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p);
817
818 #ifdef WITH_TLS
819 /*
820  *      For run-time patching of which function handles which socket.
821  */
822 int dual_tls_recv(rad_listen_t *listener);
823 int dual_tls_send(rad_listen_t *listener, REQUEST *request);
824 int proxy_tls_recv(rad_listen_t *listener);
825 int proxy_tls_send(rad_listen_t *listener, REQUEST *request);
826 #endif
827
828 #ifdef __cplusplus
829 }
830 #endif
831
832 #endif /*RADIUSD_H*/