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.
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.
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
22 * @brief Structures, prototypes and global variables for the FreeRADIUS server.
24 * @copyright 1999-2000,2002-2008 The FreeRADIUS server project
27 #include <freeradius-devel/ident.h>
28 RCSIDH(radiusd_h, "$Id$")
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>
37 typedef struct auth_req REQUEST;
44 #define REQUEST_MAGIC (0xdeadbeef)
48 * WITH_VMPS is handled by src/include/features.h
55 #include <freeradius-devel/tls.h>
58 #include <freeradius-devel/stats.h>
59 #include <freeradius-devel/realms.h>
61 #ifdef WITH_COMMAND_SOCKET
62 #define PW_RADMIN_PORT 18120
72 typedef struct request_data_t request_data_t;
74 typedef struct radclient {
76 fr_ipaddr_t src_ipaddr;
81 int message_authenticator;
86 int number; /* internal use only */
87 const CONF_SECTION *cs;
90 #ifdef WITH_ACCOUNTING
101 fr_socket_limit_t limit;
104 #ifdef WITH_DYNAMIC_CLIENTS
106 int dynamic; /* was dynamically defined */
108 time_t last_new_client;
115 home_server *coa_server;
116 home_pool_t *coa_pool;
121 * Types of listeners.
123 * Ordered by priority!
125 typedef enum RAD_LISTEN_TYPE {
131 #ifdef WITH_ACCOUNTING
143 #ifdef WITH_COMMAND_SOCKET
154 * For listening on multiple IP's and ports.
156 typedef struct rad_listen_t rad_listen_t;
157 typedef void (*radlog_func_t)(int, int, REQUEST *, const char *, ...);
159 typedef void (*fr_request_process_t)(REQUEST *, int);
161 * Function handler for requests.
163 typedef int (*RAD_REQUEST_FUNP)(REQUEST *);
165 #define REQUEST_DATA_REGEX (0xadbeef00)
166 #define REQUEST_MAX_REGEX (8)
170 uint32_t magic; /* for debugging only */
172 RADIUS_PACKET *packet;
174 RADIUS_PACKET *proxy;
176 RADIUS_PACKET *reply;
178 RADIUS_PACKET *proxy_reply;
180 VALUE_PAIR *config_items;
181 VALUE_PAIR *username;
182 VALUE_PAIR *password;
184 fr_request_process_t process;
185 RAD_REQUEST_FUNP handle;
186 struct main_config_t *root;
188 request_data_t *data;
190 #ifdef HAVE_PTHREAD_H
194 unsigned int number; /* internal server number */
196 rad_listen_t *listener;
198 rad_listen_t *proxy_listener;
202 int simul_max; /* see modcall.c && xlat.c */
203 #ifdef WITH_SESSION_MGMT
205 int simul_mpp; /* WEIRD: 1 is false, 2 is true */
208 int options; /* miscellanous options */
209 const char *module; /* for debugging unresponsive children */
210 const char *component; /* ditto */
216 RAD_LISTEN_TYPE priority;
225 home_server *home_server;
226 home_pool_t *home_pool; /* for dynamic failover */
228 struct timeval proxy_retransmit;
230 int num_proxied_requests;
231 int num_proxied_responses;
236 radlog_func_t radlog; /* logging function, if set */
239 int num_coa_requests;
241 }; /* REQUEST typedef */
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)
249 #define REQUEST_ACTIVE (1)
250 #define REQUEST_STOP_PROCESSING (2)
251 #define REQUEST_COUNTED (3)
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)
260 typedef struct radclient_list RADCLIENT_LIST;
262 typedef enum pair_lists {
263 PAIR_LIST_UNKNOWN = 0,
268 PAIR_LIST_PROXY_REQUEST,
269 PAIR_LIST_PROXY_REPLY,
279 extern const FR_NAME_NUMBER pair_lists[];
281 typedef enum requests {
285 REQUEST_PARENT /* For future use */
288 extern const FR_NAME_NUMBER request_refs[];
290 typedef struct pair_list {
296 struct pair_list *next;
297 struct pair_list *lastdefault;
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 *);
306 struct rad_listen_t {
307 struct rad_listen_t *next; /* should be rbtree stuff */
310 * For normal sockets.
312 RAD_LISTEN_TYPE type;
321 fr_tls_server_conf_t *tls;
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;
330 const CONF_SECTION *cs;
339 * This shouldn't really be exposed...
341 typedef struct listen_socket_t {
343 * For normal sockets.
345 fr_ipaddr_t my_ipaddr;
348 const char *interface;
357 /* for outgoing sockets */
359 fr_ipaddr_t other_ipaddr;
365 /* for a proxy connecting to home servers */
370 fr_socket_limit_t limit;
372 struct listen_socket_t *parent;
375 RADIUS_PACKET *packet; /* for reading partial packets */
380 REQUEST *request; /* horrible hacks */
382 pthread_mutex_t mutex;
386 RADCLIENT_LIST *clients;
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)
395 typedef enum radlog_dest_t {
404 typedef struct main_config_t {
405 struct main_config *next;
407 fr_ipaddr_t myip; /* from the command-line only */
408 int port; /* from the command-line only */
410 int log_auth_badpass;
411 int log_auth_goodpass;
412 int allow_core_dumps;
419 int max_request_time;
422 #ifdef DELETE_BLOCKED_REQUESTS
423 int kill_unresponsive_children;
427 const char *pid_file;
428 rad_listen_t *listen;
431 radlog_dest_t radlog_dest;
432 CONF_SECTION *config;
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.
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
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__)
453 #define RDEBUG2 DEBUG2
454 #define RDEBUG3 DEBUG3
455 #define RDEBUG4 DEBUG4
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
474 /* for paircompare_register */
475 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
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,
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.
496 * We really shouldn't have this many.
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);
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)
521 * Function prototypes.
525 int rad_accounting(REQUEST *);
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);
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 *);
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);
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);
575 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
576 const fr_ipaddr_t *ipaddr, int proto);
578 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
580 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr);
581 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master,
583 RADCLIENT *client_read(const char *filename, int in_server, int flag);
587 int pairlist_read(const char *file, PAIR_LIST **list, int complain);
588 void pairlist_free(PAIR_LIST **);
591 int ssl_check_version(void);
592 const char *ssl_version(void);
596 int vradlog(int, const char *, va_list ap);
597 int radlog(int, const char *, ...)
599 __attribute__ ((format (printf, 2, 3)))
602 int log_debug(const char *, ...)
604 __attribute__ ((format (printf, 1, 2)))
607 void vp_listdebug(VALUE_PAIR *vp);
608 void radlog_request(int lvl, int priority, REQUEST *request, const char *msg, ...)
610 __attribute__ ((format (printf, 4, 5)))
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 *);
621 pid_t radius_start_program(const char *cmd, REQUEST *request,
625 VALUE_PAIR *input_pairs,
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,
633 void exec_trigger(REQUEST *request, CONF_SECTION *cs, const char *name, int quench);
636 int timestr_match(char *, time_t);
639 int paircompare_register(unsigned int attr, int otherattr,
640 RAD_COMPARE_FUNC func,
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,
659 typedef size_t (*RADIUS_ESCAPE_STRING)(REQUEST *, char *out, size_t outlen, const char *in, void *arg);
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,
666 void xlat_unregister(const char *module, RAD_XLAT_FUNC func,
668 void xlat_free(void);
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]);
681 #ifndef HAVE_PTHREAD_H
682 #define rad_fork(n) fork()
683 #define rad_waitpid(a,b) waitpid(a,b, 0)
687 /* Define a global config structure */
688 extern struct main_config_t mainconfig;
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);
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);
706 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
709 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port,
711 int rad_status_server(REQUEST *request);
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);
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);
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);
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.
743 extern const FR_NAME_NUMBER vpt_types[];
745 /** A pre-parsed template attribute
747 * Value pair template, used when processing various mappings sections
748 * to create a real valuepair later.
750 * @see value_pair_map_t
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
758 request_refs_t request; //!< Request to search or insert in.
759 pair_lists_t list; //!< List to search or insert in.
761 const DICT_ATTR *da; //!< Resolved dictionary attribute.
762 vpt_type_t type; //!< What type of value tmpl refers to.
767 * Value pair maps contain a pair of templates, that describe a src attribute
768 * or value, and a destination attribute.
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).
773 * @see value_pair_tmpl_t
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.
781 FR_TOKEN op; //!< The operator that controls
782 //!< insertion of the dst attribute.
784 CONF_ITEM *ci; //!< Config item that the map was
785 //!< created from. Mainly used for
786 //!< logging validation errors.
788 struct value_pair_map *next; //!< The next valuepair map.
791 typedef VALUE_PAIR *(*radius_tmpl_getvalue_t)(REQUEST *request,
792 const value_pair_map_t *map,
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);
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,
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);
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);
816 int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p);
820 * For run-time patching of which function handles which socket.
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);