Change where we do suid up/down.
[freeradius.git] / src / include / radiusd.h
1 #ifndef RADIUSD_H
2 #define RADIUSD_H
3 /*
4  * radiusd.h    Structures, prototypes and global variables
5  *              for the FreeRADIUS server.
6  *
7  * Version:     $Id$
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  *
23  * Copyright 1999,2000,2002,2003,2004,2005,2006,2007,2008  The FreeRADIUS server project
24  *
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
36 typedef struct auth_req REQUEST;
37
38 #ifdef HAVE_PTHREAD_H
39 #include        <pthread.h>
40 #endif
41
42 #ifndef NDEBUG
43 #define REQUEST_MAGIC (0xdeadbeef)
44 #endif
45
46 /*
47  *      New defines for minimizing the size of the server, to strip
48  *      out functionality.  In order to ensure that people don't have
49  *      to re-run "configure", after "cvs update", we play some
50  *      special games with the defines.  i.e. any top-level "configure"
51  *      option should set both WITH_FOO and WITHOUT_FOO.  After a few
52  *      weeks, the WITHOUT_FOO can be deleted from the configure script.
53  */
54 #ifndef WITHOUT_PROXY
55 #define WITH_PROXY (1)
56 #endif
57
58 #ifndef WITHOUT_DETAIL
59 #define WITH_DETAIL (1)
60 #endif
61
62 #ifndef WITHOUT_SESSION_MGMT
63 #define WITH_SESSION_MGMT (1)
64 #endif
65
66 #ifndef WITHOUT_UNLANG
67 #define WITH_UNLANG (1)
68 #endif
69
70 #ifndef WITHOUT_ACCOUNTING
71 #define WITH_ACCOUNTING (1)
72 #else
73 #ifdef WITH_SESSION_MGMT
74 #error WITH_SESSION_MGMT is defined, but WITH_ACCOUNTING is not.  Session management requires accounting.
75 #endif
76 #ifdef WITH_DETAIL
77 #error WITH_DETAIL is defined, but WITH_ACCOUNTING is not.  Detail file reading requires accounting.
78 #endif
79 #endif
80
81 #ifndef WITHOUT_DYNAMIC_CLIENTS
82 #define WITH_DYNAMIC_CLIENTS (1)
83 #endif
84
85 #ifndef WITHOUT_STATS
86 #define WITH_STATS
87 #endif
88
89 #ifndef WITHOUT_COMMAND_SOCKET
90 #ifdef HAVE_SYS_UN_H
91 #define WITH_COMMAND_SOCKET (1)
92 #endif
93 #endif
94
95 #include <freeradius-devel/stats.h>
96 #include <freeradius-devel/realms.h>
97
98
99 /*
100  *      See util.c
101  */
102 typedef struct request_data_t request_data_t;
103
104 typedef struct radclient {
105         fr_ipaddr_t             ipaddr;
106         int                     prefix;
107         char                    *longname;
108         char                    *secret;
109         char                    *shortname;
110         int                     message_authenticator;
111         char                    *nastype;
112         char                    *login;
113         char                    *password;
114         char                    *server;
115         int                     number; /* internal use only */
116         const CONF_SECTION      *cs;
117 #ifdef WITH_STATS
118         fr_stats_t              *auth;
119 #ifdef WITH_ACCOUNTING
120         fr_stats_t              *acct;
121 #endif
122 #endif
123
124 #ifdef WITH_DYNAMIC_CLIENTS
125         int                     lifetime;
126         int                     dynamic; /* was dynamically defined */
127         time_t                  created;
128         time_t                  last_new_client;
129         char                    *client_server;
130 #endif
131 } RADCLIENT;
132
133 /*
134  *      Types of listeners.
135  *
136  *      Ordered by priority!
137  */
138 typedef enum RAD_LISTEN_TYPE {
139         RAD_LISTEN_NONE = 0,
140 #ifdef WITH_PROXY
141         RAD_LISTEN_PROXY,
142 #endif
143         RAD_LISTEN_AUTH,
144 #ifdef WITH_ACCOUNTING
145         RAD_LISTEN_ACCT,
146 #endif
147 #ifdef WITH_DETAIL
148         RAD_LISTEN_DETAIL,
149 #endif
150 #ifdef WITH_VMPS
151         RAD_LISTEN_VQP,
152 #endif
153 #ifdef WITH_DHCP
154         RAD_LISTEN_DHCP,
155 #endif
156 #ifdef WITH_COMMAND_SOCKET
157         RAD_LISTEN_COMMAND,
158 #endif
159         RAD_LISTEN_MAX
160 } RAD_LISTEN_TYPE;
161
162
163 /*
164  *      For listening on multiple IP's and ports.
165  */
166 typedef struct rad_listen_t rad_listen_t;
167 typedef         void (*radlog_func_t)(int, int, REQUEST *, const char *, ...);
168
169 #define REQUEST_DATA_REGEX (0xadbeef00)
170 #define REQUEST_MAX_REGEX (8)
171
172 struct auth_req {
173 #ifndef NDEBUG
174         uint32_t                magic; /* for debugging only */
175 #endif
176         RADIUS_PACKET           *packet;
177 #ifdef WITH_PROXY
178         RADIUS_PACKET           *proxy;
179 #endif
180         RADIUS_PACKET           *reply;
181 #ifdef WITH_PROXY
182         RADIUS_PACKET           *proxy_reply;
183 #endif
184         VALUE_PAIR              *config_items;
185         VALUE_PAIR              *username;
186         VALUE_PAIR              *password;
187
188         struct main_config_t    *root;
189
190         request_data_t          *data;
191         RADCLIENT               *client;
192 #ifdef HAVE_PTHREAD_H
193         pthread_t               child_pid;
194 #endif
195         time_t                  timestamp;
196         int                     number; /* internal server number */
197
198         rad_listen_t            *listener;
199 #ifdef WITH_PROXY
200         rad_listen_t            *proxy_listener;
201 #endif
202
203
204         int                     simul_max; /* see modcall.c && xlat.c */
205 #ifdef WITH_SESSION_MGMT
206         int                     simul_count;
207         int                     simul_mpp; /* WEIRD: 1 is false, 2 is true */
208 #endif
209
210         int                     options; /* miscellanous options */
211         const char              *module; /* for debugging unresponsive children */
212         const char              *component; /* ditto */
213
214         struct timeval          received;
215         struct timeval          when;           /* to wake up */
216         int                     delay;
217
218         int                     master_state;
219         int                     child_state;
220         RAD_LISTEN_TYPE         priority;
221
222         fr_event_t              *ev;
223         struct timeval          next_when;
224         fr_event_callback_t     next_callback;
225
226         int                     in_request_hash;
227 #ifdef WITH_PROXY
228         int                     in_proxy_hash;
229
230         home_server             *home_server;
231         home_pool_t             *home_pool; /* for dynamic failover */
232
233         struct timeval          proxy_when;
234
235         int                     num_proxied_requests;
236         int                     num_proxied_responses;
237 #endif
238
239         const char              *server;
240         REQUEST                 *parent;
241         radlog_func_t           radlog; /* logging function, if set */
242 };                              /* REQUEST typedef */
243
244 #define RAD_REQUEST_OPTION_NONE            (0)
245 #define RAD_REQUEST_OPTION_DEBUG           (1)
246 #define RAD_REQUEST_OPTION_DEBUG2          (2)
247 #define RAD_REQUEST_OPTION_DEBUG3          (3)
248 #define RAD_REQUEST_OPTION_DEBUG4          (4)
249
250 #define REQUEST_ACTIVE          (1)
251 #define REQUEST_STOP_PROCESSING (2)
252 #define REQUEST_COUNTED         (3)
253
254 #define REQUEST_QUEUED          (1)
255 #define REQUEST_RUNNING         (2)
256 #define REQUEST_PROXIED         (3)
257 #define REQUEST_REJECT_DELAY    (4)
258 #define REQUEST_CLEANUP_DELAY   (5)
259 #define REQUEST_DONE            (6)
260
261 /*
262  *  Function handler for requests.
263  */
264 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
265
266 typedef struct radclient_list RADCLIENT_LIST;
267
268 typedef struct pair_list {
269         const char              *name;
270         VALUE_PAIR              *check;
271         VALUE_PAIR              *reply;
272         int                     lineno;
273         int                     order;
274         struct pair_list        *next;
275         struct pair_list        *lastdefault;
276 } PAIR_LIST;
277
278
279 typedef int (*rad_listen_recv_t)(rad_listen_t *, RAD_REQUEST_FUNP *, REQUEST **);
280 typedef int (*rad_listen_send_t)(rad_listen_t *, REQUEST *);
281 typedef int (*rad_listen_print_t)(rad_listen_t *, char *, size_t);
282 typedef int (*rad_listen_encode_t)(rad_listen_t *, REQUEST *);
283 typedef int (*rad_listen_decode_t)(rad_listen_t *, REQUEST *);
284
285 struct rad_listen_t {
286         struct rad_listen_t *next; /* should be rbtree stuff */
287
288         /*
289          *      For normal sockets.
290          */
291         RAD_LISTEN_TYPE type;
292         int             fd;
293         const char      *server;
294         int             status;
295
296         rad_listen_recv_t recv;
297         rad_listen_send_t send;
298         rad_listen_encode_t encode;
299         rad_listen_decode_t decode;
300         rad_listen_print_t print;
301
302         void            *data;
303
304 #ifdef WITH_STATS
305         fr_stats_t      stats;
306 #endif
307 };
308
309 #define RAD_LISTEN_STATUS_INIT   (0)
310 #define RAD_LISTEN_STATUS_KNOWN  (1)
311 #define RAD_LISTEN_STATUS_CLOSED (2)
312 #define RAD_LISTEN_STATUS_FINISH (3)
313
314 typedef enum radlog_dest_t {
315   RADLOG_STDOUT = 0,
316   RADLOG_FILES,
317   RADLOG_SYSLOG,
318   RADLOG_STDERR,
319   RADLOG_NULL,
320   RADLOG_NUM_DEST
321 } radlog_dest_t;
322
323 typedef struct main_config_t {
324         struct main_config *next;
325         int             refcount;
326         fr_ipaddr_t     myip;   /* from the command-line only */
327         int             port;   /* from the command-line only */
328         int             log_auth;
329         int             log_auth_badpass;
330         int             log_auth_goodpass;
331         int             allow_core_dumps;
332         int             debug_level;
333         int             proxy_requests;
334         int             reject_delay;
335         int             status_server;
336         int             max_request_time;
337         int             cleanup_delay;
338         int             max_requests;
339 #ifdef DELETE_BLOCKED_REQUESTS
340         int             kill_unresponsive_children;
341 #endif
342         char            *log_file;
343         char            *checkrad;
344         const char      *pid_file;
345         rad_listen_t    *listen;
346         int             syslog_facility;
347         int             radlog_fd;
348         radlog_dest_t   radlog_dest;
349         CONF_SECTION    *config;
350         const char      *name;
351 } MAIN_CONFIG_T;
352
353 #define DEBUG   if(debug_flag)log_debug
354 #define DEBUG2  if (debug_flag > 1)log_debug
355 #define DEBUG3  if (debug_flag > 2)log_debug
356 #define DEBUG4  if (debug_flag > 3)log_debug
357
358 #if __GNUC__ >= 3
359 #define RDEBUG(fmt, ...)   if(request && request->radlog) request->radlog(L_DBG, 1, request, fmt, ## __VA_ARGS__)
360 #define RDEBUG2(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 2, request, fmt, ## __VA_ARGS__)
361 #define RDEBUG3(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 3, request, fmt, ## __VA_ARGS__)
362 #define RDEBUG4(fmt, ...)  if(request && request->radlog) request->radlog(L_DBG, 4, request, fmt, ## __VA_ARGS__)
363 #else
364 #define RDEBUG  DEBUG
365 #define RDEBUG2 DEBUG2
366 #define RDEBUG3 DEBUG3
367 #define RDEBUG4 DEBUG4
368 #endif
369
370 #define SECONDS_PER_DAY         86400
371 #define MAX_REQUEST_TIME        30
372 #define CLEANUP_DELAY           5
373 #define MAX_REQUESTS            256
374 #define RETRY_DELAY             5
375 #define RETRY_COUNT             3
376 #define DEAD_TIME               120
377
378 #define L_DBG                   1
379 #define L_AUTH                  2
380 #define L_INFO                  3
381 #define L_ERR                   4
382 #define L_PROXY                 5
383 #define L_ACCT                  6
384 #define L_CONS                  128
385
386 #ifndef FALSE
387 #define FALSE 0
388 #endif
389 #ifndef TRUE
390 /*
391  *      This definition of true as NOT false is definitive. :) Making
392  *      it '1' can cause problems on stupid platforms.  See articles
393  *      on C portability for more information.
394  */
395 #define TRUE (!FALSE)
396 #endif
397
398 /* for paircompare_register */
399 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
400
401 typedef enum request_fail_t {
402   REQUEST_FAIL_UNKNOWN = 0,
403   REQUEST_FAIL_NO_THREADS,      /* no threads to handle it */
404   REQUEST_FAIL_DECODE,          /* rad_decode didn't like it */
405   REQUEST_FAIL_PROXY,           /* call to proxy modules failed */
406   REQUEST_FAIL_PROXY_SEND,      /* proxy_send didn't like it */
407   REQUEST_FAIL_NO_RESPONSE,     /* we weren't told to respond, so we reject */
408   REQUEST_FAIL_HOME_SERVER,     /* the home server didn't respond */
409   REQUEST_FAIL_HOME_SERVER2,    /* another case of the above */
410   REQUEST_FAIL_HOME_SERVER3,    /* another case of the above */
411   REQUEST_FAIL_NORMAL_REJECT,   /* authentication failure */
412   REQUEST_FAIL_SERVER_TIMEOUT   /* the server took too long to process the request */
413 } request_fail_t;
414
415 /*
416  *      Global variables.
417  *
418  *      We really shouldn't have this many.
419  */
420 extern const char       *progname;
421 extern int              debug_flag;
422 extern const char       *radacct_dir;
423 extern const char       *radlog_dir;
424 extern const char       *radlib_dir;
425 extern const char       *radius_dir;
426 extern const char       *radius_libdir;
427 extern uint32_t         expiration_seconds;
428 extern int              log_stripped_names;
429 extern int              log_auth_detail;
430 extern const char      *radiusd_version;
431 void                    radius_signal_self(int flag);
432
433 #define RADIUS_SIGNAL_SELF_NONE         (0)
434 #define RADIUS_SIGNAL_SELF_HUP          (1 << 0)
435 #define RADIUS_SIGNAL_SELF_TERM         (1 << 1)
436 #define RADIUS_SIGNAL_SELF_EXIT         (1 << 2)
437 #define RADIUS_SIGNAL_SELF_DETAIL       (1 << 3)
438 #define RADIUS_SIGNAL_SELF_NEW_FD       (1 << 4)
439 #define RADIUS_SIGNAL_SELF_MAX          (1 << 5)
440
441
442 /*
443  *      Function prototypes.
444  */
445
446 /* acct.c */
447 int             rad_accounting(REQUEST *);
448
449 /* session.c */
450 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
451                              const char *sessionid);
452 int             session_zap(REQUEST *request, uint32_t nasaddr,
453                             unsigned int port, const char *user,
454                             const char *sessionid, uint32_t cliaddr,
455                             char proto,int session_time);
456
457 /* radiusd.c */
458 #undef debug_pair
459 void            debug_pair(VALUE_PAIR *);
460 void            debug_pair_list(VALUE_PAIR *);
461 int             log_err (char *);
462
463 /* util.c */
464 void (*reset_signal(int signo, void (*func)(int)))(int);
465 void            request_free(REQUEST **request);
466 int             rad_mkdir(char *directory, int mode);
467 int             rad_checkfilename(const char *filename);
468 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
469 REQUEST         *request_alloc(void);
470 REQUEST         *request_alloc_fake(REQUEST *oldreq);
471 int             request_data_add(REQUEST *request,
472                                  void *unique_ptr, int unique_int,
473                                  void *opaque, void (*free_opaque)(void *));
474 void            *request_data_get(REQUEST *request,
475                                   void *unique_ptr, int unique_int);
476 void            *request_data_reference(REQUEST *request,
477                                   void *unique_ptr, int unique_int);
478 int             rad_copy_string(char *dst, const char *src);
479 int             rad_copy_variable(char *dst, const char *from);
480
481 /* client.c */
482 RADCLIENT_LIST  *clients_init(void);
483 void            clients_free(RADCLIENT_LIST *clients);
484 RADCLIENT_LIST  *clients_parse_section(CONF_SECTION *section);
485 void            client_free(RADCLIENT *client);
486 int             client_add(RADCLIENT_LIST *clients, RADCLIENT *client);
487 #ifdef WITH_DYNAMIC_CLIENTS
488 void            client_delete(RADCLIENT_LIST *clients, RADCLIENT *client);
489 RADCLIENT       *client_create(RADCLIENT_LIST *clients, REQUEST *request);
490 #endif
491 RADCLIENT       *client_find(const RADCLIENT_LIST *clients,
492                              const fr_ipaddr_t *ipaddr);
493 RADCLIENT       *client_findbynumber(const RADCLIENT_LIST *clients,
494                                      int number);
495 RADCLIENT       *client_find_old(const fr_ipaddr_t *ipaddr);
496 int             client_validate(RADCLIENT_LIST *clients, RADCLIENT *master,
497                                 RADCLIENT *c);
498 RADCLIENT       *client_read(const char *filename, int in_server, int flag);
499
500
501 /* files.c */
502 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
503 void            pairlist_free(PAIR_LIST **);
504
505 /* version.c */
506 void            version(void);
507
508 /* log.c */
509 int             vradlog(int, const char *, va_list ap);
510 int             radlog(int, const char *, ...)
511 #ifdef __GNUC__
512                 __attribute__ ((format (printf, 2, 3)))
513 #endif
514 ;
515 int             log_debug(const char *, ...)
516 #ifdef __GNUC__
517                 __attribute__ ((format (printf, 1, 2)))
518 #endif
519 ;
520 void            vp_listdebug(VALUE_PAIR *vp);
521 void radlog_request(int lvl, int priority, REQUEST *request, const char *msg, ...)
522 #ifdef __GNUC__
523                 __attribute__ ((format (printf, 4, 5)))
524 #endif
525 ;
526
527 /* auth.c */
528 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
529 int             rad_authenticate (REQUEST *);
530 int             rad_postauth(REQUEST *);
531
532 /* exec.c */
533 int             radius_exec_program(const char *,  REQUEST *, int,
534                                     char *user_msg, int msg_len,
535                                     VALUE_PAIR *input_pairs,
536                                     VALUE_PAIR **output_pairs,
537                                         int shell_escape);
538
539 /* timestr.c */
540 int             timestr_match(char *, time_t);
541
542 /* valuepair.c */
543 int             paircompare_register(int attr, int otherattr,
544                                      RAD_COMPARE_FUNC func,
545                                      void *instance);
546 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
547 int             paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
548                             VALUE_PAIR **reply);
549 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
550 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp);
551 int radius_callback_compare(REQUEST *req, VALUE_PAIR *request,
552                             VALUE_PAIR *check, VALUE_PAIR *check_pairs,
553                             VALUE_PAIR **reply_pairs);
554 int radius_find_compare(int attribute);
555 VALUE_PAIR      *radius_paircreate(REQUEST *request, VALUE_PAIR **vps,
556                                   int attribute, int type);
557 VALUE_PAIR *radius_pairmake(REQUEST *request, VALUE_PAIR **vps,
558                             const char *attribute, const char *value,
559                             int operator);
560
561 /* xlat.c */
562 typedef size_t (*RADIUS_ESCAPE_STRING)(char *out, size_t outlen, const char *in);
563
564 int            radius_xlat(char * out, int outlen, const char *fmt,
565                            REQUEST * request, RADIUS_ESCAPE_STRING func);
566 typedef size_t (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING func);
567 int             xlat_register(const char *module, RAD_XLAT_FUNC func,
568                               void *instance);
569 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
570 void            xlat_free(void);
571
572 /* threads.c */
573 extern          int thread_pool_init(CONF_SECTION *cs, int spawn_flag);
574 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
575 extern          pid_t rad_fork(void);
576 extern          pid_t rad_waitpid(pid_t pid, int *status);
577 extern          int total_active_threads(void);
578 extern          void thread_pool_lock(void);
579 extern          void thread_pool_unlock(void);
580 extern          void thread_pool_queue_stats(int *array);
581
582 #ifndef HAVE_PTHREAD_H
583 #define rad_fork(n) fork()
584 #define rad_waitpid(a,b) waitpid(a,b, 0)
585 #endif
586
587 /* mainconfig.c */
588 /* Define a global config structure */
589 extern struct main_config_t mainconfig;
590
591 int read_mainconfig(int reload);
592 int free_mainconfig(void);
593 void fr_suid_down(void);
594 void fr_suid_up(void);
595 void fr_suid_down_permanent(void);
596
597 /* listen.c */
598 void listen_free(rad_listen_t **head);
599 int listen_init(CONF_SECTION *cs, rad_listen_t **head);
600 rad_listen_t *proxy_new_listener(void);
601 RADCLIENT *client_listener_find(const rad_listen_t *listener,
602                                 const fr_ipaddr_t *ipaddr, int src_port);
603 #ifdef WITH_STATS
604 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
605                                           int port);
606 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port);
607 #endif
608
609 /* event.c */
610 int radius_event_init(CONF_SECTION *cs, int spawn_flag);
611 void radius_event_free(void);
612 int radius_event_process(void);
613 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun);
614 int received_request(rad_listen_t *listener,
615                      RADIUS_PACKET *packet, REQUEST **prequest,
616                      RADCLIENT *client);
617 REQUEST *received_proxy_response(RADIUS_PACKET *packet);
618 void event_new_fd(rad_listen_t *listener);
619
620 /* evaluate.c */
621 int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
622                               const char **ptr, int evaluate_it, int *presult);
623 int radius_update_attrlist(REQUEST *request, CONF_SECTION *cs,
624                            VALUE_PAIR *input_vps, const char *name);
625 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from);
626 #endif /*RADIUSD_H*/