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 RCSIDH(radiusd_h, "$Id$")
29 #include <freeradius-devel/libradius.h>
30 #include <freeradius-devel/radpaths.h>
31 #include <freeradius-devel/conf.h>
32 #include <freeradius-devel/conffile.h>
33 #include <freeradius-devel/event.h>
34 #include <freeradius-devel/connection.h>
36 typedef struct rad_request REQUEST;
38 #include <freeradius-devel/log.h>
43 # include <sys/wait.h>
47 # define REQUEST_MAGIC (0xdeadbeef)
51 * WITH_VMPS is handled by src/include/features.h
58 # include <freeradius-devel/tls.h>
61 #include <freeradius-devel/stats.h>
62 #include <freeradius-devel/realms.h>
63 #include <freeradius-devel/xlat.h>
64 #include <freeradius-devel/tmpl.h>
65 #include <freeradius-devel/map.h>
66 #include <freeradius-devel/clients.h>
67 #include <freeradius-devel/process.h>
69 * All POSIX systems should have these headers
81 typedef struct request_data_t request_data_t;
83 /** Return codes indicating the result of the module call
85 * All module functions must return one of the codes listed below (apart from
86 * RLM_MODULE_NUMCODES, which is used to check for validity).
88 typedef enum rlm_rcodes {
89 RLM_MODULE_REJECT = 0, //!< Immediately reject the request.
90 RLM_MODULE_FAIL, //!< Module failed, don't reply.
91 RLM_MODULE_OK, //!< The module is OK, continue.
92 RLM_MODULE_HANDLED, //!< The module handled the request, so stop.
93 RLM_MODULE_INVALID, //!< The module considers the request invalid.
94 RLM_MODULE_USERLOCK, //!< Reject the request (user is locked out).
95 RLM_MODULE_NOTFOUND, //!< User not found.
96 RLM_MODULE_NOOP, //!< Module succeeded without doing anything.
97 RLM_MODULE_UPDATED, //!< OK (pairs modified).
98 RLM_MODULE_NUMCODES, //!< How many valid return codes there are.
99 RLM_MODULE_UNKNOWN //!< Error resolving rcode (should not be
100 //!< returned by modules).
102 extern const FR_NAME_NUMBER modreturn_table[];
104 /** Main server configuration
106 * The parsed version of the main server config.
108 typedef struct main_config {
109 struct main_config *next; //!< Next version of the main_config.
111 char const *name; //!< Name of the daemon, usually 'radiusd'.
112 CONF_SECTION *config; //!< Root of the server config.
114 fr_ipaddr_t myip; //!< IP to bind to. Set on command line.
115 uint16_t port; //!< Port to bind to. Set on command line.
117 bool log_auth; //!< Log authentication attempts.
118 bool log_auth_badpass; //!< Log successful authentications.
119 bool log_auth_goodpass; //!< Log failed authentications.
120 char const *auth_badpass_msg; //!< Additional text to append to successful auth messages.
121 char const *auth_goodpass_msg; //!< Additional text to append to failed auth messages.
123 char const *denied_msg; //!< Additional text to append if the user is already logged
124 //!< in (simultaneous use check failed).
126 bool daemonize; //!< Should the server daemonize on startup.
127 char const *pid_file; //!< Path to write out PID file.
130 bool proxy_requests; //!< Toggle to enable/disable proxying globally.
132 struct timeval reject_delay; //!< How long to wait before sending an Access-Reject.
133 bool status_server; //!< Whether to respond to status-server messages.
136 uint32_t max_request_time; //!< How long a request can be processed for before
138 uint32_t cleanup_delay; //!< How long before cleaning up cached responses.
139 uint32_t max_requests;
141 uint32_t debug_level;
142 char const *log_file;
145 char const *dictionary_dir; //!< Where to load dictionaries from.
147 char const *checkrad; //!< Script to use to determine if a user is already
150 rad_listen_t *listen; //!< Head of a linked list of listeners.
153 char const *panic_action; //!< Command to execute if the server receives a fatal
156 struct timeval init_delay; //!< Initial request processing delay.
158 uint32_t talloc_pool_size; //!< Size of pool to allocate to hold each #REQUEST.
159 bool debug_memory; //!< Cleanup the server properly on exit, freeing
160 //!< up any memory we allocated.
161 bool memory_report; //!< Print a memory report on what's left unfreed.
162 //!< Can only be used when the server is running in single
165 bool allow_core_dumps; //!< Whether the server is allowed to drop a core when
166 //!< receiving a fatal signal.
168 bool write_pid; //!< write the PID file
171 #ifdef ENABLE_OPENSSL_VERSION_CHECK
172 char const *allow_vulnerable_openssl; //!< The CVE number of the last security issue acknowledged.
176 #if defined(WITH_VERIFY_PTR)
177 # define VERIFY_REQUEST(_x) verify_request(__FILE__, __LINE__, _x)
180 * Even if were building without WITH_VERIFY_PTR
181 * the pointer must not be NULL when these various macros are used
182 * so we can add some sneaky asserts.
184 # define VERIFY_REQUEST(_x) rad_assert(_x)
189 REQUEST_STOP_PROCESSING,
191 } rad_master_state_t;
192 #define REQUEST_MASTER_NUM_STATES (REQUEST_COUNTED + 1)
198 REQUEST_RESPONSE_DELAY,
199 REQUEST_CLEANUP_DELAY,
202 #define REQUEST_CHILD_NUM_STATES (REQUEST_DONE + 1)
206 uint32_t magic; //!< Magic number used to detect memory corruption,
207 //!< or request structs that have not been properly initialised.
209 unsigned int number; //!< Monotonically increasing request number. Reset on server restart.
210 time_t timestamp; //!< When the request was received.
212 request_data_t *data; //!< Request metadata.
214 rad_listen_t *listener; //!< The listener that received the request.
215 RADCLIENT *client; //!< The client that originally sent us the request.
217 RADIUS_PACKET *packet; //!< Incoming request.
218 VALUE_PAIR *username; //!< Cached username #VALUE_PAIR from request #RADIUS_PACKET.
219 VALUE_PAIR *password; //!< Cached password #VALUE_PAIR from request #RADIUS_PACKET.
221 RADIUS_PACKET *reply; //!< Outgoing response.
223 VALUE_PAIR *config; //!< #VALUE_PAIR (s) used to set per request parameters
224 //!< for modules and the server core at runtime.
226 TALLOC_CTX *state_ctx; //!< for request->state
227 VALUE_PAIR *state; //!< #VALUE_PAIR (s) available over the lifetime of the authentication
228 //!< attempt. Useful where the attempt involves a sequence of
229 //!< many request/challenge packets, like OTP, and EAP.
232 rad_listen_t *proxy_listener;//!< Listener for outgoing requests.
233 RADIUS_PACKET *proxy; //!< Outgoing request to proxy server.
234 RADIUS_PACKET *proxy_reply; //!< Incoming response from proxy server.
236 home_server_t *home_server;
237 home_pool_t *home_pool; //!< For dynamic failover
240 fr_request_process_t process; //!< The function to call to move the request through the state machine.
242 struct timeval response_delay; //!< How long to wait before sending Access-Rejects.
243 fr_state_action_t timer_action; //!< What action to perform when the timer event fires.
244 fr_event_t *ev; //!< Event in event loop tied to this request.
246 RAD_REQUEST_FUNP handle; //!< The function to call to move the request through the
247 //!< various server configuration sections.
248 rlm_rcode_t rcode; //!< Last rcode returned by a module
249 char const *module; //!< Module the request is currently being processed by.
250 char const *component; //!< Section the request is in.
254 rad_master_state_t master_state; //!< Set by the master thread to signal the child that's currently
255 //!< working with the request, to do something.
256 rad_child_state_t child_state;
258 #ifdef HAVE_PTHREAD_H
259 pthread_t child_pid; //!< Current thread handling the request.
262 main_config_t *root; //!< Pointer to the main config hack to try and deal with hup.
265 int simul_max; //!< Maximum number of concurrent sessions for this user.
266 #ifdef WITH_SESSION_MGMT
267 int simul_count; //!< The current number of sessions for this user.
268 int simul_mpp; //!< WEIRD: 1 is false, 2 is true.
271 RAD_LISTEN_TYPE priority;
273 bool in_request_hash;
277 struct timeval proxy_retransmit;
279 uint32_t num_proxied_requests; //!< How many times this request was proxied.
280 //!< Retransmissions are driven by requests from the NAS.
281 uint32_t num_proxied_responses;
288 radlog_func_t func; //!< Function to call to output log messages about this
291 log_lvl_t lvl; //!< Controls the verbosity of debug statements regarding
294 uint8_t indent; //!< By how much to indent log messages. uin8_t so it's obvious
295 //!< when a request has been exdented too much.
298 uint32_t options; //!< mainly for proxying EAP-MSCHAPv2.
301 REQUEST *coa; //!< CoA request originated by this request.
302 uint32_t num_coa_requests;//!< Counter for number of requests sent including
305 }; /* REQUEST typedef */
307 #define RAD_REQUEST_LVL_NONE (0) //!< No debug messages should be printed.
308 #define RAD_REQUEST_LVL_DEBUG (1)
309 #define RAD_REQUEST_LVL_DEBUG2 (2)
310 #define RAD_REQUEST_LVL_DEBUG3 (3)
311 #define RAD_REQUEST_LVL_DEBUG4 (4)
313 #define RAD_REQUEST_OPTION_COA (1 << 0)
314 #define RAD_REQUEST_OPTION_CTX (1 << 1)
316 #define SECONDS_PER_DAY 86400
317 #define MAX_REQUEST_TIME 30
318 #define CLEANUP_DELAY 5
319 #define MAX_REQUESTS 256
320 #define RETRY_DELAY 5
321 #define RETRY_COUNT 3
322 #define DEAD_TIME 120
323 #define EXEC_TIMEOUT 10
325 /* for paircompare_register */
326 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
328 typedef enum request_fail {
329 REQUEST_FAIL_UNKNOWN = 0,
330 REQUEST_FAIL_NO_THREADS, //!< No threads to handle it.
331 REQUEST_FAIL_DECODE, //!< Rad_decode didn't like it.
332 REQUEST_FAIL_PROXY, //!< Call to proxy modules failed.
333 REQUEST_FAIL_PROXY_SEND, //!< Proxy_send didn't like it.
334 REQUEST_FAIL_NO_RESPONSE, //!< We weren't told to respond, so we reject.
335 REQUEST_FAIL_HOME_SERVER, //!< The home server didn't respond.
336 REQUEST_FAIL_HOME_SERVER2, //!< Another case of the above.
337 REQUEST_FAIL_HOME_SERVER3, //!< Another case of the above.
338 REQUEST_FAIL_NORMAL_REJECT, //!< Authentication failure.
339 REQUEST_FAIL_SERVER_TIMEOUT //!< The server took too long to process the request.
345 * We really shouldn't have this many.
347 extern char const *progname;
348 extern log_lvl_t rad_debug_lvl;
349 extern char const *radacct_dir;
350 extern char const *radlog_dir;
351 extern char const *radlib_dir;
352 extern bool log_stripped_names;
353 extern char const *radiusd_version;
354 extern char const *radiusd_version_short;
355 void radius_signal_self(int flag);
358 RADIUS_SIGNAL_SELF_NONE = (0),
359 RADIUS_SIGNAL_SELF_HUP = (1 << 0),
360 RADIUS_SIGNAL_SELF_TERM = (1 << 1),
361 RADIUS_SIGNAL_SELF_EXIT = (1 << 2),
362 RADIUS_SIGNAL_SELF_DETAIL = (1 << 3),
363 RADIUS_SIGNAL_SELF_NEW_FD = (1 << 4),
364 RADIUS_SIGNAL_SELF_MAX = (1 << 5)
367 * Function prototypes.
371 int rad_accounting(REQUEST *);
373 int rad_coa_recv(REQUEST *request);
376 int rad_check_ts(uint32_t nasaddr, uint32_t nas_port, char const *user, char const *sessionid);
377 int session_zap(REQUEST *request, uint32_t nasaddr,
378 uint32_t nas_port, char const *user,
379 char const *sessionid, uint32_t cliaddr,
380 char proto, int session_time);
384 void debug_pair(VALUE_PAIR *);
385 void rdebug_pair(log_lvl_t level, REQUEST *, VALUE_PAIR *, char const *);
386 void rdebug_pair_list(log_lvl_t level, REQUEST *, VALUE_PAIR *, char const *);
387 void rdebug_proto_pair_list(log_lvl_t level, REQUEST *, VALUE_PAIR *);
388 int log_err (char *);
391 #define MEM(x) if (!(x)) { ERROR("%s[%u] OUT OF MEMORY", __FILE__, __LINE__); _fr_exit_now(__FILE__, __LINE__, 1); }
392 void (*reset_signal(int signo, void (*func)(int)))(int);
393 int rad_mkdir(char *directory, mode_t mode, uid_t uid, gid_t gid);
394 size_t rad_filename_make_safe(UNUSED REQUEST *request, char *out, size_t outlen,
395 char const *in, UNUSED void *arg);
396 size_t rad_filename_escape(UNUSED REQUEST *request, char *out, size_t outlen,
397 char const *in, UNUSED void *arg);
398 ssize_t rad_filename_unescape(char *out, size_t outlen, char const *in, size_t inlen);
399 void *rad_malloc(size_t size); /* calls exit(1) on error! */
400 void rad_const_free(void const *ptr);
401 REQUEST *request_alloc(TALLOC_CTX *ctx);
402 REQUEST *request_alloc_fake(REQUEST *oldreq);
403 REQUEST *request_alloc_coa(REQUEST *request);
404 int request_data_add(REQUEST *request,
405 void *unique_ptr, int unique_int,
406 void *opaque, bool free_opaque);
407 void *request_data_get(REQUEST *request,
408 void *unique_ptr, int unique_int);
409 void *request_data_reference(REQUEST *request,
410 void *unique_ptr, int unique_int);
411 int rad_copy_string(char *dst, char const *src);
412 int rad_copy_string_bare(char *dst, char const *src);
413 int rad_copy_variable(char *dst, char const *from);
414 uint32_t rad_pps(uint32_t *past, uint32_t *present, time_t *then, struct timeval *now);
415 int rad_expand_xlat(REQUEST *request, char const *cmd,
416 int max_argc, char const *argv[], bool can_fail,
417 size_t argv_buflen, char *argv_buf);
419 void verify_request(char const *file, int line, REQUEST *request); /* only for special debug builds */
420 void rad_mode_to_str(char out[10], mode_t mode);
421 void rad_mode_to_oct(char out[5], mode_t mode);
422 int rad_getpwuid(TALLOC_CTX *ctx, struct passwd **out, uid_t uid);
423 int rad_getpwnam(TALLOC_CTX *ctx, struct passwd **out, char const *name);
424 int rad_getgrgid(TALLOC_CTX *ctx, struct group **out, gid_t gid);
425 int rad_getgrnam(TALLOC_CTX *ctx, struct group **out, char const *name);
426 int rad_getgid(TALLOC_CTX *ctx, gid_t *out, char const *name);
427 int rad_prints_uid(TALLOC_CTX *ctx, char *out, size_t outlen, uid_t uid);
428 int rad_prints_gid(TALLOC_CTX *ctx, char *out, size_t outlen, gid_t gid);
429 int rad_seuid(uid_t uid);
430 int rad_segid(gid_t gid);
432 void rad_suid_set_down_uid(uid_t uid);
433 void rad_suid_down(void);
434 void rad_suid_up(void);
435 void rad_suid_down_permanent(void);
440 * Increasing this is essentially free
441 * It just increases memory usage. 12-16 bytes for each additional subcapture.
443 # define REQUEST_MAX_REGEX 32
445 void regex_sub_to_request(REQUEST *request, regex_t **preg, char const *value,
446 size_t len, regmatch_t rxmatch[], size_t nmatch);
448 int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t num);
451 * Named capture groups only supported by PCRE.
454 int regex_request_to_sub_named(TALLOC_CTX *ctx, char **out, REQUEST *request, char const *name);
459 int pairlist_read(TALLOC_CTX *ctx, char const *file, PAIR_LIST **list, int complain);
460 void pairlist_free(PAIR_LIST **);
463 int rad_check_lib_magic(uint64_t magic);
464 int ssl_check_consistency(void);
465 char const *ssl_version_by_num(uint32_t version);
466 char const *ssl_version_num(void);
467 char const *ssl_version_range(uint32_t low, uint32_t high);
468 char const *ssl_version(void);
469 int version_add_feature(CONF_SECTION *cs, char const *name, bool enabled);
470 int version_add_number(CONF_SECTION *cs, char const *name, char const *version);
471 void version_init_features(CONF_SECTION *cs);
472 void version_init_numbers(CONF_SECTION *cs);
473 void version_print(void);
476 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli);
477 int rad_authenticate (REQUEST *);
478 int rad_postauth(REQUEST *);
479 int rad_virtual_server(REQUEST *);
482 pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
483 int *input_fd, int *output_fd,
484 VALUE_PAIR *input_pairs, bool shell_escape);
485 int radius_readfrom_program(int fd, pid_t pid, int timeout,
486 char *answer, int left);
487 int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, VALUE_PAIR **output_pairs,
488 REQUEST *request, char const *cmd, VALUE_PAIR *input_pairs,
489 bool exec_wait, bool shell_escape, int timeout) CC_HINT(nonnull (5, 6));
490 void exec_trigger(REQUEST *request, CONF_SECTION *cs, char const *name, int quench)
491 CC_HINT(nonnull (3));
494 int paircompare_register_byname(char const *name, DICT_ATTR const *from,
495 bool first_only, RAD_COMPARE_FUNC func, void *instance);
496 int paircompare_register(DICT_ATTR const *attribute, DICT_ATTR const *from,
497 bool first_only, RAD_COMPARE_FUNC func, void *instance);
498 void paircompare_unregister(DICT_ATTR const *attr, RAD_COMPARE_FUNC func);
499 void paircompare_unregister_instance(void *instance);
500 int paircompare(REQUEST *request, VALUE_PAIR *req_list,
501 VALUE_PAIR *check, VALUE_PAIR **rep_list);
502 vp_tmpl_t *xlat_to_tmpl_attr(TALLOC_CTX *ctx, xlat_exp_t *xlat);
503 xlat_exp_t *xlat_from_tmpl_attr(TALLOC_CTX *ctx, vp_tmpl_t *vpt);
504 int radius_xlat_do(REQUEST *request, VALUE_PAIR *vp);
505 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp);
506 int radius_callback_compare(REQUEST *request, VALUE_PAIR *req,
507 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
508 VALUE_PAIR **reply_pairs);
509 int radius_find_compare(DICT_ATTR const *attribute);
510 VALUE_PAIR *radius_pair_create(TALLOC_CTX *ctx, VALUE_PAIR **vps, unsigned int attribute, unsigned int vendor);
512 void module_failure_msg(REQUEST *request, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
513 void vmodule_failure_msg(REQUEST *request, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0));
515 int radius_get_vp(VALUE_PAIR **out, REQUEST *request, char const *name);
516 int radius_copy_vp(TALLOC_CTX *ctx, VALUE_PAIR **out, REQUEST *request, char const *name);
520 * Less code == fewer bugs
522 * @param _a attribute
526 #define pair_make_request(_a, _b, _c) fr_pair_make(request->packet, &request->packet->vps, _a, _b, _c)
527 #define pair_make_reply(_a, _b, _c) fr_pair_make(request->reply, &request->reply->vps, _a, _b, _c)
528 #define pair_make_config(_a, _b, _c) fr_pair_make(request, &request->config, _a, _b, _c)
531 int thread_pool_init(CONF_SECTION *cs, bool *spawn_flag);
532 void thread_pool_stop(void);
533 int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
534 pid_t rad_fork(void);
535 pid_t rad_waitpid(pid_t pid, int *status);
536 int total_active_threads(void);
537 void thread_pool_lock(void);
538 void thread_pool_unlock(void);
539 void thread_pool_queue_stats(int array[RAD_LISTEN_MAX], int pps[2]);
541 #ifndef HAVE_PTHREAD_H
542 # define rad_fork(n) fork()
543 # define rad_waitpid(a,b) waitpid(a,b, 0)
547 /* Define a global config structure */
548 extern bool log_dates_utc;
549 extern main_config_t main_config;
550 extern bool event_loop_started;
552 void set_radius_dir(TALLOC_CTX *ctx, char const *path);
553 char const *get_radius_dir(void);
554 int main_config_init(void);
555 int main_config_free(void);
556 void main_config_hup(void);
557 void hup_logfile(void);
560 void listen_free(rad_listen_t **head);
561 int listen_init(CONF_SECTION *cs, rad_listen_t **head, bool spawn_flag);
562 rad_listen_t *proxy_new_listener(TALLOC_CTX *ctx, home_server_t *home, uint16_t src_port);
563 RADCLIENT *client_listener_find(rad_listen_t *listener, fr_ipaddr_t const *ipaddr, uint16_t src_port);
566 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port, int proto);
568 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto);
569 int rad_status_server(REQUEST *request);
572 typedef enum event_corral_t {
573 EVENT_CORRAL_MAIN = 0, //!< Always main thread event list
574 EVENT_CORRAL_AUX //!< Maybe main thread or one shared by modules
577 fr_event_list_t *radius_event_list_corral(event_corral_t hint);
578 int radius_event_init(TALLOC_CTX *ctx);
579 int radius_event_start(CONF_SECTION *cs, bool spawn_flag);
580 void radius_event_free(void);
581 int radius_event_process(void);
582 void radius_update_listener(rad_listen_t *listener);
583 void revive_home_server(void *ctx);
584 void mark_home_server_dead(home_server_t *home, struct timeval *when);
587 typedef struct fr_cond_t fr_cond_t;
588 int radius_evaluate_tmpl(REQUEST *request, int modreturn, int depth,
589 vp_tmpl_t const *vpt);
590 int radius_evaluate_map(REQUEST *request, int modreturn, int depth,
592 int radius_evaluate_cond(REQUEST *request, int modreturn, int depth,
594 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from, bool do_xlat) CC_HINT(nonnull);
598 * For run-time patching of which function handles which socket.
600 int dual_tls_recv(rad_listen_t *listener);
601 int dual_tls_send(rad_listen_t *listener, REQUEST *request);
602 int proxy_tls_recv(rad_listen_t *listener);
603 int proxy_tls_send(rad_listen_t *listener, REQUEST *request);
607 * For radmin over TCP.
609 #define PW_RADMIN_PORT 18120