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
170 bool exiting; //!< are we exiting?
173 #ifdef ENABLE_OPENSSL_VERSION_CHECK
174 char const *allow_vulnerable_openssl; //!< The CVE number of the last security issue acknowledged.
178 #if defined(WITH_VERIFY_PTR)
179 # define VERIFY_REQUEST(_x) verify_request(__FILE__, __LINE__, _x)
182 * Even if were building without WITH_VERIFY_PTR
183 * the pointer must not be NULL when these various macros are used
184 * so we can add some sneaky asserts.
186 # define VERIFY_REQUEST(_x) rad_assert(_x)
191 REQUEST_STOP_PROCESSING,
193 } rad_master_state_t;
194 #define REQUEST_MASTER_NUM_STATES (REQUEST_COUNTED + 1)
200 REQUEST_RESPONSE_DELAY,
201 REQUEST_CLEANUP_DELAY,
204 #define REQUEST_CHILD_NUM_STATES (REQUEST_DONE + 1)
208 uint32_t magic; //!< Magic number used to detect memory corruption,
209 //!< or request structs that have not been properly initialised.
211 unsigned int number; //!< Monotonically increasing request number. Reset on server restart.
212 time_t timestamp; //!< When the request was received.
214 request_data_t *data; //!< Request metadata.
216 rad_listen_t *listener; //!< The listener that received the request.
217 RADCLIENT *client; //!< The client that originally sent us the request.
219 RADIUS_PACKET *packet; //!< Incoming request.
220 VALUE_PAIR *username; //!< Cached username #VALUE_PAIR from request #RADIUS_PACKET.
221 VALUE_PAIR *password; //!< Cached password #VALUE_PAIR from request #RADIUS_PACKET.
223 RADIUS_PACKET *reply; //!< Outgoing response.
225 VALUE_PAIR *config; //!< #VALUE_PAIR (s) used to set per request parameters
226 //!< for modules and the server core at runtime.
228 TALLOC_CTX *state_ctx; //!< for request->state
229 VALUE_PAIR *state; //!< #VALUE_PAIR (s) available over the lifetime of the authentication
230 //!< attempt. Useful where the attempt involves a sequence of
231 //!< many request/challenge packets, like OTP, and EAP.
234 rad_listen_t *proxy_listener;//!< Listener for outgoing requests.
235 RADIUS_PACKET *proxy; //!< Outgoing request to proxy server.
236 RADIUS_PACKET *proxy_reply; //!< Incoming response from proxy server.
238 home_server_t *home_server;
239 home_pool_t *home_pool; //!< For dynamic failover
242 fr_request_process_t process; //!< The function to call to move the request through the state machine.
244 struct timeval response_delay; //!< How long to wait before sending Access-Rejects.
245 fr_state_action_t timer_action; //!< What action to perform when the timer event fires.
246 fr_event_t *ev; //!< Event in event loop tied to this request.
248 RAD_REQUEST_FUNP handle; //!< The function to call to move the request through the
249 //!< various server configuration sections.
250 rlm_rcode_t rcode; //!< Last rcode returned by a module
251 char const *module; //!< Module the request is currently being processed by.
252 char const *component; //!< Section the request is in.
256 rad_master_state_t master_state; //!< Set by the master thread to signal the child that's currently
257 //!< working with the request, to do something.
258 rad_child_state_t child_state;
260 #ifdef HAVE_PTHREAD_H
261 pthread_t child_pid; //!< Current thread handling the request.
264 main_config_t *root; //!< Pointer to the main config hack to try and deal with hup.
267 int simul_max; //!< Maximum number of concurrent sessions for this user.
268 #ifdef WITH_SESSION_MGMT
269 int simul_count; //!< The current number of sessions for this user.
270 int simul_mpp; //!< WEIRD: 1 is false, 2 is true.
273 RAD_LISTEN_TYPE priority;
275 bool in_request_hash;
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 log_lvl_t rad_debug_lvl;
348 extern char const *radacct_dir;
349 extern char const *radlog_dir;
350 extern char const *radlib_dir;
351 extern bool log_stripped_names;
352 extern char const *radiusd_version;
353 extern char const *radiusd_version_short;
354 void radius_signal_self(int flag);
357 RADIUS_SIGNAL_SELF_NONE = (0),
358 RADIUS_SIGNAL_SELF_HUP = (1 << 0),
359 RADIUS_SIGNAL_SELF_TERM = (1 << 1),
360 RADIUS_SIGNAL_SELF_EXIT = (1 << 2),
361 RADIUS_SIGNAL_SELF_DETAIL = (1 << 3),
362 RADIUS_SIGNAL_SELF_NEW_FD = (1 << 4),
363 RADIUS_SIGNAL_SELF_MAX = (1 << 5)
366 * Function prototypes.
370 int rad_accounting(REQUEST *);
372 int rad_coa_recv(REQUEST *request);
375 int rad_check_ts(uint32_t nasaddr, uint32_t nas_port, char const *user, char const *sessionid);
376 int session_zap(REQUEST *request, uint32_t nasaddr,
377 uint32_t nas_port, char const *user,
378 char const *sessionid, uint32_t cliaddr,
379 char proto, int session_time);
383 void debug_pair(VALUE_PAIR *);
384 void rdebug_pair(log_lvl_t level, REQUEST *, VALUE_PAIR *, char const *);
385 void rdebug_pair_list(log_lvl_t level, REQUEST *, VALUE_PAIR *, char const *);
386 void rdebug_proto_pair_list(log_lvl_t level, REQUEST *, VALUE_PAIR *);
387 int log_err (char *);
390 #define MEM(x) if (!(x)) { ERROR("%s[%u] OUT OF MEMORY", __FILE__, __LINE__); _fr_exit_now(__FILE__, __LINE__, 1); }
391 void (*reset_signal(int signo, void (*func)(int)))(int);
392 int rad_mkdir(char *directory, mode_t mode, uid_t uid, gid_t gid);
393 size_t rad_filename_make_safe(UNUSED REQUEST *request, char *out, size_t outlen,
394 char const *in, UNUSED void *arg);
395 size_t rad_filename_escape(UNUSED REQUEST *request, char *out, size_t outlen,
396 char const *in, UNUSED void *arg);
397 ssize_t rad_filename_unescape(char *out, size_t outlen, char const *in, size_t inlen);
398 void *rad_malloc(size_t size); /* calls exit(1) on error! */
399 void rad_const_free(void const *ptr);
400 REQUEST *request_alloc(TALLOC_CTX *ctx);
401 REQUEST *request_alloc_fake(REQUEST *oldreq);
402 REQUEST *request_alloc_coa(REQUEST *request);
403 int request_data_add(REQUEST *request,
404 void *unique_ptr, int unique_int,
405 void *opaque, bool free_opaque);
406 void *request_data_get(REQUEST *request,
407 void *unique_ptr, int unique_int);
408 void *request_data_reference(REQUEST *request,
409 void *unique_ptr, int unique_int);
410 int rad_copy_string(char *dst, char const *src);
411 int rad_copy_string_bare(char *dst, char const *src);
412 int rad_copy_variable(char *dst, char const *from);
413 uint32_t rad_pps(uint32_t *past, uint32_t *present, time_t *then, struct timeval *now);
414 int rad_expand_xlat(REQUEST *request, char const *cmd,
415 int max_argc, char const *argv[], bool can_fail,
416 size_t argv_buflen, char *argv_buf);
418 void verify_request(char const *file, int line, REQUEST *request); /* only for special debug builds */
419 void rad_mode_to_str(char out[10], mode_t mode);
420 void rad_mode_to_oct(char out[5], mode_t mode);
421 int rad_getpwuid(TALLOC_CTX *ctx, struct passwd **out, uid_t uid);
422 int rad_getpwnam(TALLOC_CTX *ctx, struct passwd **out, char const *name);
423 int rad_getgrgid(TALLOC_CTX *ctx, struct group **out, gid_t gid);
424 int rad_getgrnam(TALLOC_CTX *ctx, struct group **out, char const *name);
425 int rad_getgid(TALLOC_CTX *ctx, gid_t *out, char const *name);
426 int rad_prints_uid(TALLOC_CTX *ctx, char *out, size_t outlen, uid_t uid);
427 int rad_prints_gid(TALLOC_CTX *ctx, char *out, size_t outlen, gid_t gid);
428 int rad_seuid(uid_t uid);
429 int rad_segid(gid_t gid);
431 void rad_suid_set_down_uid(uid_t uid);
432 void rad_suid_down(void);
433 void rad_suid_up(void);
434 void rad_suid_down_permanent(void);
439 * Increasing this is essentially free
440 * It just increases memory usage. 12-16 bytes for each additional subcapture.
442 # define REQUEST_MAX_REGEX 32
444 void regex_sub_to_request(REQUEST *request, regex_t **preg, char const *value,
445 size_t len, regmatch_t rxmatch[], size_t nmatch);
447 int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t num);
450 * Named capture groups only supported by PCRE.
453 int regex_request_to_sub_named(TALLOC_CTX *ctx, char **out, REQUEST *request, char const *name);
458 int pairlist_read(TALLOC_CTX *ctx, char const *file, PAIR_LIST **list, int complain);
459 void pairlist_free(PAIR_LIST **);
462 int rad_check_lib_magic(uint64_t magic);
463 int ssl_check_consistency(void);
464 char const *ssl_version_by_num(uint32_t version);
465 char const *ssl_version_num(void);
466 char const *ssl_version_range(uint32_t low, uint32_t high);
467 char const *ssl_version(void);
468 int version_add_feature(CONF_SECTION *cs, char const *name, bool enabled);
469 int version_add_number(CONF_SECTION *cs, char const *name, char const *version);
470 void version_init_features(CONF_SECTION *cs);
471 void version_init_numbers(CONF_SECTION *cs);
472 void version_print(void);
475 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli);
476 int rad_authenticate (REQUEST *);
477 int rad_postauth(REQUEST *);
478 int rad_virtual_server(REQUEST *);
481 pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
482 int *input_fd, int *output_fd,
483 VALUE_PAIR *input_pairs, bool shell_escape);
484 int radius_readfrom_program(int fd, pid_t pid, int timeout,
485 char *answer, int left);
486 int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, VALUE_PAIR **output_pairs,
487 REQUEST *request, char const *cmd, VALUE_PAIR *input_pairs,
488 bool exec_wait, bool shell_escape, int timeout) CC_HINT(nonnull (5, 6));
489 void exec_trigger(REQUEST *request, CONF_SECTION *cs, char const *name, int quench)
490 CC_HINT(nonnull (3));
493 int paircompare_register_byname(char const *name, DICT_ATTR const *from,
494 bool first_only, RAD_COMPARE_FUNC func, void *instance);
495 int paircompare_register(DICT_ATTR const *attribute, DICT_ATTR const *from,
496 bool first_only, RAD_COMPARE_FUNC func, void *instance);
497 void paircompare_unregister(DICT_ATTR const *attr, RAD_COMPARE_FUNC func);
498 void paircompare_unregister_instance(void *instance);
499 int paircompare(REQUEST *request, VALUE_PAIR *req_list,
500 VALUE_PAIR *check, VALUE_PAIR **rep_list);
501 vp_tmpl_t *xlat_to_tmpl_attr(TALLOC_CTX *ctx, xlat_exp_t *xlat);
502 xlat_exp_t *xlat_from_tmpl_attr(TALLOC_CTX *ctx, vp_tmpl_t *vpt);
503 int radius_xlat_do(REQUEST *request, VALUE_PAIR *vp);
504 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp);
505 int radius_callback_compare(REQUEST *request, VALUE_PAIR *req,
506 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
507 VALUE_PAIR **reply_pairs);
508 int radius_find_compare(DICT_ATTR const *attribute);
509 VALUE_PAIR *radius_pair_create(TALLOC_CTX *ctx, VALUE_PAIR **vps, unsigned int attribute, unsigned int vendor);
511 void module_failure_msg(REQUEST *request, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
512 void vmodule_failure_msg(REQUEST *request, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0));
514 int radius_get_vp(VALUE_PAIR **out, REQUEST *request, char const *name);
515 int radius_copy_vp(TALLOC_CTX *ctx, VALUE_PAIR **out, REQUEST *request, char const *name);
519 * Less code == fewer bugs
521 * @param _a attribute
525 #define pair_make_request(_a, _b, _c) fr_pair_make(request->packet, &request->packet->vps, _a, _b, _c)
526 #define pair_make_reply(_a, _b, _c) fr_pair_make(request->reply, &request->reply->vps, _a, _b, _c)
527 #define pair_make_config(_a, _b, _c) fr_pair_make(request, &request->config, _a, _b, _c)
530 int thread_pool_init(CONF_SECTION *cs, bool *spawn_flag);
531 void thread_pool_stop(void);
532 int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
533 pid_t rad_fork(void);
534 pid_t rad_waitpid(pid_t pid, int *status);
535 int total_active_threads(void);
536 void thread_pool_lock(void);
537 void thread_pool_unlock(void);
538 void thread_pool_queue_stats(int array[RAD_LISTEN_MAX], int pps[2]);
540 #ifndef HAVE_PTHREAD_H
541 # define rad_fork(n) fork()
542 # define rad_waitpid(a,b) waitpid(a,b, 0)
546 /* Define a global config structure */
547 extern bool log_dates_utc;
548 extern main_config_t main_config;
549 extern bool event_loop_started;
551 void set_radius_dir(TALLOC_CTX *ctx, char const *path);
552 char const *get_radius_dir(void);
553 int main_config_init(void);
554 int main_config_free(void);
555 void main_config_hup(void);
556 void hup_logfile(void);
559 void listen_free(rad_listen_t **head);
560 int listen_init(CONF_SECTION *cs, rad_listen_t **head, bool spawn_flag);
561 rad_listen_t *proxy_new_listener(TALLOC_CTX *ctx, home_server_t *home, uint16_t src_port);
562 RADCLIENT *client_listener_find(rad_listen_t *listener, fr_ipaddr_t const *ipaddr, uint16_t src_port);
565 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port, int proto);
567 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto);
568 int rad_status_server(REQUEST *request);
571 typedef enum event_corral_t {
572 EVENT_CORRAL_MAIN = 0, //!< Always main thread event list
573 EVENT_CORRAL_AUX //!< Maybe main thread or one shared by modules
576 fr_event_list_t *radius_event_list_corral(event_corral_t hint);
577 int radius_event_init(TALLOC_CTX *ctx);
578 int radius_event_start(CONF_SECTION *cs, bool spawn_flag);
579 void radius_event_free(void);
580 int radius_event_process(void);
581 void radius_update_listener(rad_listen_t *listener);
582 void revive_home_server(void *ctx);
583 void mark_home_server_dead(home_server_t *home, struct timeval *when);
586 typedef struct fr_cond_t fr_cond_t;
587 int radius_evaluate_tmpl(REQUEST *request, int modreturn, int depth,
588 vp_tmpl_t const *vpt);
589 int radius_evaluate_map(REQUEST *request, int modreturn, int depth,
591 int radius_evaluate_cond(REQUEST *request, int modreturn, int depth,
593 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from, bool do_xlat) CC_HINT(nonnull);
597 * For run-time patching of which function handles which socket.
599 int dual_tls_recv(rad_listen_t *listener);
600 int dual_tls_send(rad_listen_t *listener, REQUEST *request);
601 int proxy_tls_recv(rad_listen_t *listener);
602 int proxy_tls_send(rad_listen_t *listener, REQUEST *request);
606 * For radmin over TCP.
608 #define PW_RADMIN_PORT 18120