Factor out argv splitting into rad_expand_xlat
[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 request 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 request {
169 #ifndef NDEBUG
170         uint32_t                magic;          //!< Magic number used to 
171                                                 //!< detect memory corruption,
172                                                 //!< or request structs that
173                                                 //!< have not been properly
174                                                 //!< initialised.
175 #endif
176         RADIUS_PACKET           *packet;        //!< Incoming request.
177 #ifdef WITH_PROXY
178         RADIUS_PACKET           *proxy;         //!< Outgoing request.
179 #endif
180         RADIUS_PACKET           *reply;         //!< Outgoing response.
181 #ifdef WITH_PROXY
182         RADIUS_PACKET           *proxy_reply;   //!< Incoming response.
183 #endif
184         VALUE_PAIR              *config_items;  //!< VALUE_PAIR s used to set
185                                                 //!< per request parameters for
186                                                 //!< modules and the server 
187                                                 //!< core at runtime.
188         VALUE_PAIR              *username;      //!< Cached username VALUE_PAIR.
189         VALUE_PAIR              *password;      //!< Cached password VALUE_PAIR.
190
191         fr_request_process_t    process;        //!< The function to call to
192                                                 //!< move the request through
193                                                 //!< the state machine.
194                                                 
195         RAD_REQUEST_FUNP        handle;         //!< The function to call to
196                                                 //!< move the request through
197                                                 //!< the various server
198                                                 //!< configuration sections.
199                                                 
200         struct main_config_t    *root;          //!< Pointer to the main config
201                                                 //!< hack to try and deal with
202                                                 //!< hup. 
203
204         request_data_t          *data;          //!< Request metadata.
205         
206         RADCLIENT               *client;        //!< The client that originally
207                                                 //!< sent us the request.
208                                                 
209 #ifdef HAVE_PTHREAD_H
210         pthread_t               child_pid;      //!< Current thread handling
211                                                 //!< the request.
212 #endif
213         time_t                  timestamp;      //!< When the request was 
214                                                 //!< received.
215         unsigned int            number;         //!< Monotonically increasing
216                                                 //!< request number. Reset on 
217                                                 //!< server restart.
218
219         rad_listen_t            *listener;      //!< The listener that received
220                                                 //!< the request.
221 #ifdef WITH_PROXY
222         rad_listen_t            *proxy_listener;//!< Listener for outgoing 
223                                                 //!< requests.
224 #endif
225
226
227         int                     simul_max;      //!< Maximum number of 
228                                                 //!< concurrent sessions for
229                                                 //!< this user.
230 #ifdef WITH_SESSION_MGMT
231         int                     simul_count;    //!< The current number of
232                                                 //!< sessions for this user.
233         int                     simul_mpp;      //!< WEIRD: 1 is false,
234                                                 //!< 2 is true.
235 #endif
236
237         int                     options;        //!< Request options, currently
238                                                 //!< just holds the debug level 
239                                                 //!< for the request.
240                                                 
241         const char              *module;        //!< Module the request is
242                                                 //!< currently being processed
243                                                 //!< by.        
244         const char              *component;     //!< Section the request is
245                                                 //!< in.
246
247         int                     delay;
248
249         int                     master_state;
250         int                     child_state;
251         RAD_LISTEN_TYPE         priority;
252
253         int                     timer_action;
254         fr_event_t              *ev;
255
256         int                     in_request_hash;
257 #ifdef WITH_PROXY
258         int                     in_proxy_hash;
259
260         home_server             *home_server;
261         home_pool_t             *home_pool; /* for dynamic failover */
262
263         struct timeval          proxy_retransmit;
264
265         int                     num_proxied_requests;
266         int                     num_proxied_responses;
267 #endif
268
269         const char              *server;
270         REQUEST                 *parent;
271         radlog_func_t           radlog;         //!< Function to call to output 
272                                                 //!< log messages about this
273                                                 //!< request.
274 #ifdef WITH_COA
275         REQUEST                 *coa;           //!< CoA request originated
276                                                 //!< by this request.
277         int                     num_coa_requests;//!< Counter for number of
278                                                 //!< requests sent including
279                                                 //!< retransmits.
280 #endif
281 };                              /* REQUEST typedef */
282
283 #define RAD_REQUEST_OPTION_NONE            (0)
284 #define RAD_REQUEST_OPTION_DEBUG           (1)
285 #define RAD_REQUEST_OPTION_DEBUG2          (2)
286 #define RAD_REQUEST_OPTION_DEBUG3          (3)
287 #define RAD_REQUEST_OPTION_DEBUG4          (4)
288
289 #define REQUEST_ACTIVE          (1)
290 #define REQUEST_STOP_PROCESSING (2)
291 #define REQUEST_COUNTED         (3)
292
293 #define REQUEST_QUEUED          (1)
294 #define REQUEST_RUNNING         (2)
295 #define REQUEST_PROXIED         (3)
296 #define REQUEST_REJECT_DELAY    (4)
297 #define REQUEST_CLEANUP_DELAY   (5)
298 #define REQUEST_DONE            (6)
299
300 typedef struct radclient_list RADCLIENT_LIST;
301
302 typedef enum pair_lists {
303         PAIR_LIST_UNKNOWN = 0,
304         PAIR_LIST_REQUEST,
305         PAIR_LIST_REPLY,
306         PAIR_LIST_CONTROL,
307 #ifdef WITH_PROXY
308         PAIR_LIST_PROXY_REQUEST,
309         PAIR_LIST_PROXY_REPLY,
310 #endif
311 #ifdef WITH_COA
312         PAIR_LIST_COA,
313         PAIR_LIST_COA_REPLY,
314         PAIR_LIST_DM,
315         PAIR_LIST_DM_REPLY
316 #endif
317 } pair_lists_t;
318
319 extern const FR_NAME_NUMBER pair_lists[];
320
321 typedef enum requests {
322         REQUEST_UNKNOWN = 0,
323         REQUEST_OUTER,
324         REQUEST_CURRENT,
325         REQUEST_PARENT  /* For future use */
326 } request_refs_t;
327
328 extern const FR_NAME_NUMBER request_refs[];
329
330 typedef struct pair_list {
331         const char              *name;
332         VALUE_PAIR              *check;
333         VALUE_PAIR              *reply;
334         int                     lineno;
335         int                     order;
336         struct pair_list        *next;
337         struct pair_list        *lastdefault;
338 } PAIR_LIST;
339
340 typedef int (*rad_listen_recv_t)(rad_listen_t *);
341 typedef int (*rad_listen_send_t)(rad_listen_t *, REQUEST *);
342 typedef int (*rad_listen_print_t)(const rad_listen_t *, char *, size_t);
343 typedef int (*rad_listen_encode_t)(rad_listen_t *, REQUEST *);
344 typedef int (*rad_listen_decode_t)(rad_listen_t *, REQUEST *);
345
346 struct rad_listen_t {
347         struct rad_listen_t *next; /* should be rbtree stuff */
348
349         /*
350          *      For normal sockets.
351          */
352         RAD_LISTEN_TYPE type;
353         int             fd;
354         const char      *server;
355         int             status;
356 #ifdef WITH_TCP
357         int             count;
358 #endif
359
360 #ifdef WITH_TLS
361         fr_tls_server_conf_t *tls;
362 #endif
363
364         rad_listen_recv_t recv;
365         rad_listen_send_t send;
366         rad_listen_encode_t encode;
367         rad_listen_decode_t decode;
368         rad_listen_print_t print;
369
370         const CONF_SECTION *cs;
371         void            *data;
372
373 #ifdef WITH_STATS
374         fr_stats_t      stats;
375 #endif
376 };
377
378 /*
379  *      This shouldn't really be exposed...
380  */
381 typedef struct listen_socket_t {
382         /*
383          *      For normal sockets.
384          */
385         fr_ipaddr_t     my_ipaddr;
386         int             my_port;
387
388         const char      *interface;
389 #ifdef SO_BROADCAST
390         int             broadcast;
391 #endif
392         time_t          rate_time;
393         int             rate_pps_old;
394         int             rate_pps_now;
395         int             max_rate;
396         
397         /* for outgoing sockets */
398         home_server     *home;
399         fr_ipaddr_t     other_ipaddr;
400         int             other_port;
401
402         int             proto;
403
404 #ifdef WITH_TCP
405         /* for a proxy connecting to home servers */
406         time_t          last_packet;
407         time_t          opened;
408         fr_event_t      *ev;
409
410         fr_socket_limit_t limit;
411
412         struct listen_socket_t *parent;
413         RADCLIENT       *client;
414
415         RADIUS_PACKET   *packet; /* for reading partial packets */
416 #endif
417
418 #ifdef WITH_TLS
419         tls_session_t   *ssn;
420         REQUEST         *request; /* horrible hacks */
421         VALUE_PAIR      *certs;
422         pthread_mutex_t mutex;
423         uint8_t         *data;
424 #endif
425
426         RADCLIENT_LIST  *clients;
427 } listen_socket_t;
428
429 #define RAD_LISTEN_STATUS_INIT   (0)
430 #define RAD_LISTEN_STATUS_KNOWN  (1)
431 #define RAD_LISTEN_STATUS_REMOVE_FD (2)
432 #define RAD_LISTEN_STATUS_CLOSED (3)
433 #define RAD_LISTEN_STATUS_FINISH (4)
434
435 typedef enum radlog_dest_t {
436   RADLOG_STDOUT = 0,
437   RADLOG_FILES,
438   RADLOG_SYSLOG,
439   RADLOG_STDERR,
440   RADLOG_NULL,
441   RADLOG_NUM_DEST
442 } radlog_dest_t;
443
444 typedef struct main_config_t {
445         struct main_config *next;
446         int             refcount;
447         fr_ipaddr_t     myip;   /* from the command-line only */
448         int             port;   /* from the command-line only */
449         int             log_auth;
450         int             log_auth_badpass;
451         int             log_auth_goodpass;
452         int             allow_core_dumps;
453         int             debug_level;
454 #ifdef WITH_PROXY
455         int             proxy_requests;
456 #endif
457         int             reject_delay;
458         int             status_server;
459         int             max_request_time;
460         int             cleanup_delay;
461         int             max_requests;
462 #ifdef DELETE_BLOCKED_REQUESTS
463         int             kill_unresponsive_children;
464 #endif
465         char            *log_file;
466         char            *checkrad;
467         const char      *pid_file;
468         rad_listen_t    *listen;
469         int             syslog_facility;
470         int             radlog_fd;
471         radlog_dest_t   radlog_dest;
472         CONF_SECTION    *config;
473         const char      *name;
474         const char      *auth_badpass_msg;
475         const char      *auth_goodpass_msg;
476         int             colourise;      //!< Messages output to stderr and
477                                         //!< stdout may be formatted using 
478                                         //!< VT100 escape sequences.
479 } MAIN_CONFIG_T;
480
481 /* DEBUG is defined below */
482 #define DEBUG3  if (debug_flag > 2) log_debug
483 #define DEBUG4  if (debug_flag > 3) log_debug
484
485 #if __GNUC__ >= 3
486 #define RDEBUG(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG, 1, request, fmt, ## __VA_ARGS__)
487 #define RDEBUGW(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG_WARN, 1, request, fmt, ## __VA_ARGS__)
488 #define RDEBUGE(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG_ERR, 1, request, fmt, ## __VA_ARGS__)
489
490 #define RDEBUG2(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 2, request, fmt, ## __VA_ARGS__)
491 #define RDEBUG2W(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG_WARN, 2, request, fmt, ## __VA_ARGS__)
492 #define RDEBUG2E(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG_ERR, 2, request, fmt, ## __VA_ARGS__)
493
494 #define RDEBUG3(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 3, request, fmt, ## __VA_ARGS__)
495 #define RDEBUG4(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 4, request, fmt, ## __VA_ARGS__)
496
497 #define DEBUG(...)      if (debug_flag) radlog(L_DBG, ## __VA_ARGS__)
498 #define DEBUGW(...)     if (debug_flag) radlog(L_DBG_WARN, ## __VA_ARGS__)
499 #define DEBUGE(...)     if (debug_flag) radlog(L_DBG_ERR, ## __VA_ARGS__)
500
501 #define DEBUG2  if (debug_flag > 1) log_debug
502 #define DEBUG2W(...)  if (debug_flag > 1) radlog(L_DBG_WARN, ## __VA_ARGS__)
503
504 #else
505 #define DEBUG   if (debug_flag) log_debug
506 #define DEBUG2  if (debug_flag > 1) log_debug
507
508 #define RDEBUG  DEBUG
509 #define RDEBUG2 DEBUG2
510 #define RDEBUG3 DEBUG3
511 #define RDEBUG4 DEBUG4
512 #endif
513
514 void log_talloc(const char *message);
515 void log_talloc_report(TALLOC_CTX *ctx);
516
517 #define SECONDS_PER_DAY         86400
518 #define MAX_REQUEST_TIME        30
519 #define CLEANUP_DELAY           5
520 #define MAX_REQUESTS            256
521 #define RETRY_DELAY             5
522 #define RETRY_COUNT             3
523 #define DEAD_TIME               120
524
525 #define L_AUTH                  2
526 #define L_INFO                  3
527 #define L_ERR                   4
528 #define L_PROXY                 5
529 #define L_ACCT                  6
530
531 #define L_DBG                   16
532 #define L_DBG_WARN              17
533 #define L_DBG_ERR               18
534 #define L_DBG_WARN2             19
535 #define L_DBG_ERR2              20
536
537
538 /* for paircompare_register */
539 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
540
541 typedef enum request_fail {
542         REQUEST_FAIL_UNKNOWN = 0,
543         REQUEST_FAIL_NO_THREADS,        //!< No threads to handle it.
544         REQUEST_FAIL_DECODE,            //!< Rad_decode didn't like it.
545         REQUEST_FAIL_PROXY,             //!< Call to proxy modules failed.
546         REQUEST_FAIL_PROXY_SEND,        //!< Proxy_send didn't like it.
547         REQUEST_FAIL_NO_RESPONSE,       //!< We weren't told to respond, 
548                                         //!< so we reject.
549         REQUEST_FAIL_HOME_SERVER,       //!< The home server didn't respond.
550         REQUEST_FAIL_HOME_SERVER2,      //!< Another case of the above.
551         REQUEST_FAIL_HOME_SERVER3,      //!< Another case of the above.
552         REQUEST_FAIL_NORMAL_REJECT,     //!< Authentication failure.
553         REQUEST_FAIL_SERVER_TIMEOUT     //!< The server took too long to 
554                                         //!< process the request.
555 } request_fail_t;
556
557 /*
558  *      Global variables.
559  *
560  *      We really shouldn't have this many.
561  */
562 extern const char       *progname;
563 extern int              debug_flag;
564 extern const char       *radacct_dir;
565 extern const char       *radlog_dir;
566 extern const char       *radlib_dir;
567 extern char             *radius_dir;
568 extern const char       *radius_libdir;
569 extern uint32_t         expiration_seconds;
570 extern int              log_stripped_names;
571 extern int              log_auth_detail;
572 extern const char      *radiusd_version;
573 void                    radius_signal_self(int flag);
574
575 #define RADIUS_SIGNAL_SELF_NONE         (0)
576 #define RADIUS_SIGNAL_SELF_HUP          (1 << 0)
577 #define RADIUS_SIGNAL_SELF_TERM         (1 << 1)
578 #define RADIUS_SIGNAL_SELF_EXIT         (1 << 2)
579 #define RADIUS_SIGNAL_SELF_DETAIL       (1 << 3)
580 #define RADIUS_SIGNAL_SELF_NEW_FD       (1 << 4)
581 #define RADIUS_SIGNAL_SELF_MAX          (1 << 5)
582
583
584 /*
585  *      Function prototypes.
586  */
587
588 /* acct.c */
589 int             rad_accounting(REQUEST *);
590
591 /* session.c */
592 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
593                              const char *sessionid);
594 int             session_zap(REQUEST *request, uint32_t nasaddr,
595                             unsigned int port, const char *user,
596                             const char *sessionid, uint32_t cliaddr,
597                             char proto,int session_time);
598
599 /* radiusd.c */
600 #undef debug_pair
601 void            debug_pair(VALUE_PAIR *);
602 void            debug_pair_list(VALUE_PAIR *);
603 void            rdebug_pair_list(int, REQUEST *, VALUE_PAIR *);
604 int             log_err (char *);
605
606 /* util.c */
607 void (*reset_signal(int signo, void (*func)(int)))(int);
608 void            request_free(REQUEST **request);
609 int             rad_mkdir(char *directory, mode_t mode);
610 int             rad_checkfilename(const char *filename);
611 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
612 void            *rad_calloc(size_t size); /* calls exit(1) on error! */
613 void            rad_const_free(const void *ptr);
614 void            rad_cfree(const void *ptr);
615 REQUEST         *request_alloc(void);
616 REQUEST         *request_alloc_fake(REQUEST *oldreq);
617 REQUEST         *request_alloc_coa(REQUEST *request);
618 int             request_data_add(REQUEST *request,
619                                  void *unique_ptr, int unique_int,
620                                  void *opaque, void (*free_opaque)(void *));
621 void            *request_data_get(REQUEST *request,
622                                   void *unique_ptr, int unique_int);
623 void            *request_data_reference(REQUEST *request,
624                                   void *unique_ptr, int unique_int);
625 int             rad_copy_string(char *dst, const char *src);
626 int             rad_copy_variable(char *dst, const char *from);
627 int             rad_pps(int *past, int *present, time_t *then,
628                         struct timeval *now);
629 int             rad_expand_xlat(REQUEST *request, const char *cmd,
630                                 int max_argc, const char *argv[], int can_fail,
631                                 size_t argv_buflen, char *argv_buf);
632
633 /* client.c */
634 RADCLIENT_LIST  *clients_init(CONF_SECTION *cs);
635 void            clients_free(RADCLIENT_LIST *clients);
636 RADCLIENT_LIST  *clients_parse_section(CONF_SECTION *section);
637 void            client_free(RADCLIENT *client);
638 int             client_add(RADCLIENT_LIST *clients, RADCLIENT *client);
639 #ifdef WITH_DYNAMIC_CLIENTS
640 void            client_delete(RADCLIENT_LIST *clients, RADCLIENT *client);
641 RADCLIENT       *client_create(RADCLIENT_LIST *clients, REQUEST *request);
642 #endif
643 RADCLIENT       *client_find(const RADCLIENT_LIST *clients,
644                              const fr_ipaddr_t *ipaddr, int proto);
645
646 RADCLIENT       *client_findbynumber(const RADCLIENT_LIST *clients,
647                                      int number);
648 RADCLIENT       *client_find_old(const fr_ipaddr_t *ipaddr);
649 int             client_validate(RADCLIENT_LIST *clients, RADCLIENT *master,
650                                 RADCLIENT *c);
651 RADCLIENT       *client_read(const char *filename, int in_server, int flag);
652
653
654 /* files.c */
655 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
656 void            pairlist_free(PAIR_LIST **);
657
658 /* version.c */
659 int             ssl_check_version(void);
660 const char      *ssl_version(void);
661 void            version(void);
662
663 /* log.c */
664 int             vradlog(int, const char *, va_list ap);
665 int             radlog(int, const char *, ...)
666 #ifdef __GNUC__
667                 __attribute__ ((format (printf, 2, 3)))
668 #endif
669 ;
670 int             log_debug(const char *, ...)
671 #ifdef __GNUC__
672                 __attribute__ ((format (printf, 1, 2)))
673 #endif
674 ;
675 void            vp_listdebug(VALUE_PAIR *vp);
676 void radlog_request(int lvl, int priority, REQUEST *request, const char *msg, ...)
677 #ifdef __GNUC__
678                 __attribute__ ((format (printf, 4, 5)))
679 #endif
680 ;
681
682 /* auth.c */
683 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
684 int             rad_authenticate (REQUEST *);
685 int             rad_postauth(REQUEST *);
686 int             rad_virtual_server(REQUEST *);
687
688 /* exec.c */
689 pid_t radius_start_program(const char *cmd, REQUEST *request,
690                         int exec_wait,
691                         int *input_fd,
692                         int *output_fd,
693                         VALUE_PAIR *input_pairs,
694                         int shell_escape);
695 int radius_readfrom_program(int fd, pid_t pid, int timeout, char *answer, int left);
696 int             radius_exec_program(const char *,  REQUEST *, int,
697                                     char *user_msg, int msg_len,
698                                     VALUE_PAIR *input_pairs,
699                                     VALUE_PAIR **output_pairs,
700                                         int shell_escape);
701 void exec_trigger(REQUEST *request, CONF_SECTION *cs, const char *name, int quench);
702
703 /* timestr.c */
704 int             timestr_match(char *, time_t);
705
706 /* valuepair.c */
707 int             paircompare_register(unsigned int attr, int otherattr,
708                                      RAD_COMPARE_FUNC func,
709                                      void *instance);
710 void            paircompare_unregister(unsigned int attr, RAD_COMPARE_FUNC func);
711 int             paircompare(REQUEST *request, VALUE_PAIR *req_list,
712                             VALUE_PAIR *check, VALUE_PAIR **rep_list);
713 int             radius_xlat_do(REQUEST *request, VALUE_PAIR *vp);
714 void            radius_xlat_move(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
715 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp);
716 int radius_callback_compare(REQUEST *req, VALUE_PAIR *request,
717                             VALUE_PAIR *check, VALUE_PAIR *check_pairs,
718                             VALUE_PAIR **reply_pairs);
719 int radius_find_compare(unsigned int attribute);
720 VALUE_PAIR      *radius_paircreate(REQUEST *request, VALUE_PAIR **vps,
721                                    unsigned int attribute, unsigned int vendor);
722 VALUE_PAIR *radius_pairmake(REQUEST *request, VALUE_PAIR **vps,
723                             const char *attribute, const char *value,
724                             FR_TOKEN op);
725
726 /* xlat.c */
727 typedef size_t (*RADIUS_ESCAPE_STRING)(REQUEST *, char *out, size_t outlen, const char *in, void *arg);
728
729 size_t          radius_xlat(char * out, int outlen, const char *fmt,
730                             REQUEST * request, RADIUS_ESCAPE_STRING func, void *funcarg);
731 typedef size_t (*RAD_XLAT_FUNC)(void *instance, REQUEST *, const char *, char *, size_t);
732 int             xlat_register(const char *module, RAD_XLAT_FUNC func,
733                               void *instance);
734 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func,
735                                 void *instance);
736 void            xlat_free(void);
737
738 /* threads.c */
739 extern          int thread_pool_init(CONF_SECTION *cs, int *spawn_flag);
740 extern          void thread_pool_stop(void);
741 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
742 extern          pid_t rad_fork(void);
743 extern          pid_t rad_waitpid(pid_t pid, int *status);
744 extern          int total_active_threads(void);
745 extern          void thread_pool_lock(void);
746 extern          void thread_pool_unlock(void);
747 extern          void thread_pool_queue_stats(int array[RAD_LISTEN_MAX], int pps[2]);
748
749 #ifndef HAVE_PTHREAD_H
750 #define rad_fork(n) fork()
751 #define rad_waitpid(a,b) waitpid(a,b, 0)
752 #endif
753
754 /* mainconfig.c */
755 /* Define a global config structure */
756 extern struct main_config_t mainconfig;
757
758 int read_mainconfig(int reload);
759 int free_mainconfig(void);
760 void hup_mainconfig(void);
761 void hup_logfile(void);
762 void fr_suid_down(void);
763 void fr_suid_up(void);
764 void fr_suid_down_permanent(void);
765
766 /* listen.c */
767 void listen_free(rad_listen_t **head);
768 int listen_init(CONF_SECTION *cs, rad_listen_t **head, int spawn_flag);
769 int proxy_new_listener(home_server *home, int src_port);
770 RADCLIENT *client_listener_find(rad_listen_t *listener,
771                                 const fr_ipaddr_t *ipaddr, int src_port);
772
773 #ifdef WITH_STATS
774 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
775                                           int port);
776 #endif
777 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port,
778                                      int proto);
779 int rad_status_server(REQUEST *request);
780
781 /* event.c */
782 int radius_event_init(CONF_SECTION *cs, int spawn_flag);
783 void radius_event_free(void);
784 int radius_event_process(void);
785 int event_new_fd(rad_listen_t *listener);
786 void revive_home_server(void *ctx);
787 void mark_home_server_dead(home_server *home, struct timeval *when);
788
789 /* evaluate.c */
790 int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
791                               const char **ptr, int evaluate_it, int *presult);
792 int radius_update_attrlist(REQUEST *request, CONF_SECTION *cs,
793                            VALUE_PAIR *input_vps, const char *name);
794 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from);
795
796 VALUE_PAIR **radius_list(REQUEST *request, pair_lists_t list);
797 pair_lists_t radius_list_name(const char **name, pair_lists_t unknown);
798 int radius_request(REQUEST **request, request_refs_t name);
799 request_refs_t radius_request_name(const char **name, request_refs_t unknown);
800
801
802 typedef enum vpt_type {
803         VPT_TYPE_UNKNOWN = 0,
804         VPT_TYPE_LITERAL,               //!< Is a literal string.
805         VPT_TYPE_XLAT,                  //!< Needs to be expanded.
806         VPT_TYPE_ATTR,                  //!< Is an attribute.
807         VPT_TYPE_LIST,                  //!< Is a list.
808         VPT_TYPE_EXEC                   //!< Needs to be executed.
809 } vpt_type_t;
810
811 extern const FR_NAME_NUMBER vpt_types[];
812
813 /** A pre-parsed template attribute
814  *  
815  *  Value pair template, used when processing various mappings sections
816  *  to create a real valuepair later.
817  *
818  * @see value_pair_map_t
819  */
820 typedef struct value_pair_tmpl_t {
821         const char              *name;   //!< Original attribute ref string, or
822                                          //!< where this refers to a none FR
823                                          //!< attribute, just the string id for
824                                          //!< the attribute.
825
826         request_refs_t          request; //!< Request to search or insert in.
827         pair_lists_t            list;    //!< List to search or insert in.
828                                        
829         const DICT_ATTR         *da;     //!< Resolved dictionary attribute.
830         vpt_type_t              type;    //!< What type of value tmpl refers to.
831 } value_pair_tmpl_t;
832
833 /** Value pair map
834  *
835  * Value pair maps contain a pair of templates, that describe a src attribute
836  * or value, and a destination attribute.
837  *
838  * Neither src or dst need to be an FR attribute, and their type can be inferred
839  * from whether map->da is NULL (not FR).
840  *
841  * @see value_pair_tmpl_t
842  */
843 typedef struct value_pair_map {
844         value_pair_tmpl_t       *dst;   //!< Typically describes the attribute
845                                         //!< to add or modify.
846         value_pair_tmpl_t       *src;   //!< Typically describes a value or a
847                                         //!< src attribute to copy.
848         
849         FR_TOKEN                op;     //!< The operator that controls
850                                         //!< insertion of the dst attribute.
851         
852         CONF_ITEM               *ci;    //!< Config item that the map was 
853                                         //!< created from. Mainly used for 
854                                         //!< logging validation errors.
855         
856         struct value_pair_map   *next;  //!< The next valuepair map.
857 } value_pair_map_t;
858
859 typedef VALUE_PAIR *(*radius_tmpl_getvalue_t)(REQUEST *request,
860                                               const value_pair_map_t *map,
861                                               void *ctx);
862 void radius_tmplfree(value_pair_tmpl_t **tmpl);
863 int radius_parse_attr(const char *name, value_pair_tmpl_t *vpt,
864                       request_refs_t request_def,
865                       pair_lists_t list_def);
866 value_pair_tmpl_t *radius_attr2tmpl(const char *name,
867                                     request_refs_t request_def,
868                                     pair_lists_t list_def);
869                                     
870 value_pair_tmpl_t *radius_str2tmpl(const char *name, FR_TOKEN type);
871 int radius_attrmap(CONF_SECTION *cs, value_pair_map_t **head,
872                    pair_lists_t dst_list_def, pair_lists_t src_list_def,
873                    unsigned int max);
874 value_pair_map_t *radius_cp2map(CONF_PAIR *cp,
875                                 request_refs_t dst_request_def,
876                                 pair_lists_t dst_list_def,
877                                 request_refs_t src_request_def,
878                                 pair_lists_t src_list_def);
879                                 
880 int radius_map2request(REQUEST *request, const value_pair_map_t *map,
881                        const char *src, radius_tmpl_getvalue_t func, void *ctx);
882 void radius_mapfree(value_pair_map_t **map);
883
884 int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p);
885
886 #ifdef WITH_TLS
887 /*
888  *      For run-time patching of which function handles which socket.
889  */
890 int dual_tls_recv(rad_listen_t *listener);
891 int dual_tls_send(rad_listen_t *listener, REQUEST *request);
892 int proxy_tls_recv(rad_listen_t *listener);
893 int proxy_tls_send(rad_listen_t *listener, REQUEST *request);
894 #endif
895
896 #ifdef __cplusplus
897 }
898 #endif
899
900 #endif /*RADIUSD_H*/