Abstracted listen parse/free/send/recv into callbacks, which
[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  */
10 #include "missing.h"
11 #include "libradius.h"
12 #include "radpaths.h"
13 #include "conf.h"
14 #include "conffile.h"
15
16 #include <stdarg.h>
17
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #ifdef HAVE_PTHREAD_H
23 #include        <pthread.h>
24 typedef pthread_t child_pid_t;
25 #define child_kill pthread_kill
26 #else
27 typedef pid_t child_pid_t;
28 #define child_kill kill
29 #endif
30
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
38
39
40 #define NO_SUCH_CHILD_PID (child_pid_t) (0)
41
42 #ifndef NDEBUG
43 #define REQUEST_MAGIC (0xdeadbeef)
44 #endif
45
46 /*
47  *      See util.c
48  */
49 typedef struct request_data_t request_data_t;
50
51 /*
52  *      For listening on multiple IP's and ports.
53  */
54 typedef struct rad_listen_t rad_listen_t;
55
56 #define REQUEST_DATA_REGEX (0xadbeef00)
57 #define REQUEST_MAX_REGEX (8)
58
59 typedef struct auth_req {
60 #ifndef NDEBUG
61         uint32_t                magic; /* for debugging only */
62 #endif
63         RADIUS_PACKET           *packet;
64         RADIUS_PACKET           *proxy;
65         RADIUS_PACKET           *reply;
66         RADIUS_PACKET           *proxy_reply;
67         VALUE_PAIR              *config_items;
68         VALUE_PAIR              *username;
69         VALUE_PAIR              *password;
70
71         request_data_t          *data;
72         char                    secret[32];
73         child_pid_t             child_pid;
74         time_t                  timestamp;
75         int                     number; /* internal server number */
76
77         rad_listen_t            *listener;
78         rad_listen_t            *proxy_listener;
79
80         /*
81          *      We could almost keep a const char here instead of a
82          *      _copy_ of the secret... but what if the RADCLIENT
83          *      structure is freed because it was taken out of the
84          *      config file and SIGHUPed?
85          */
86         char                    proxysecret[32];
87         int                     proxy_try_count;
88         int                     proxy_outstanding;
89         time_t                  proxy_next_try;
90
91         int                     simul_max;
92         int                     simul_count;
93         int                     simul_mpp; /* WEIRD: 1 is false, 2 is true */
94
95         int                     finished;
96         int                     options; /* miscellanous options */
97         const char              *module; /* for debugging unresponsive children */
98         const char              *component; /* ditto */
99         void                    *container;
100 } REQUEST;
101
102 #define RAD_REQUEST_OPTION_NONE            (0)
103 #define RAD_REQUEST_OPTION_LOGGED_CHILD    (1 << 0)
104 #define RAD_REQUEST_OPTION_DELAYED_REJECT  (1 << 1)
105 #define RAD_REQUEST_OPTION_DONT_CACHE      (1 << 2)
106 #define RAD_REQUEST_OPTION_FAKE_REQUEST    (1 << 3)
107 #define RAD_REQUEST_OPTION_REJECTED        (1 << 4)
108 #define RAD_REQUEST_OPTION_PROXIED         (1 << 5)
109 #define RAD_REQUEST_OPTION_STOP_NOW        (1 << 6)
110 #define RAD_REQUEST_OPTION_REPROCESS       (1 << 7)
111
112 /*
113  *  Function handler for requests.
114  */
115 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
116
117 typedef struct radclient {
118         lrad_ipaddr_t           ipaddr;
119         int                     prefix;
120         char                    *longname;
121         u_char                  *secret;
122         char                    *shortname;
123         char                    *nastype;
124         char                    *login;
125         char                    *password;
126         struct radclient        *next;
127 } RADCLIENT;
128
129 typedef struct nas {
130         uint32_t                ipaddr;
131         char                    longname[256];
132         char                    shortname[32];
133         char                    nastype[32];
134         struct nas              *next;
135 } NAS;
136
137 typedef struct _realm {
138         char                    realm[64];
139         char                    server[64];
140         char                    acct_server[64];
141         lrad_ipaddr_t           ipaddr; /* authentication */
142         lrad_ipaddr_t           acct_ipaddr;
143         u_char                  secret[32];
144         time_t                  last_reply; /* last time we saw a packet */
145         int                     auth_port;
146         int                     acct_port;
147         int                     striprealm;
148         int                     trusted; /* old */
149         int                     notrealm;
150         int                     active; /* is it dead? */
151         time_t                  wakeup; /* when we should try it again */
152         int                     acct_active;
153         time_t                  acct_wakeup;
154         int                     ldflag;
155         struct _realm           *next;
156 } REALM;
157
158 typedef struct pair_list {
159         char                    *name;
160         VALUE_PAIR              *check;
161         VALUE_PAIR              *reply;
162         int                     lineno;
163         struct pair_list        *next;
164         struct pair_list        *lastdefault;
165 } PAIR_LIST;
166
167
168 /*
169  *      Types of listeners.
170  *
171  *      FIXME: Separate ports for proxy auth/acct?
172  */
173 typedef enum RAD_LISTEN_TYPE {
174         RAD_LISTEN_NONE = 0,
175         RAD_LISTEN_AUTH,
176         RAD_LISTEN_ACCT,
177         RAD_LISTEN_PROXY,
178         RAD_LISTEN_DETAIL,
179         RAD_LISTEN_MAX
180 } RAD_LISTEN_TYPE;
181
182 typedef int (*rad_listen_recv_t)(rad_listen_t *, RAD_REQUEST_FUNP *, REQUEST **);
183 typedef int (*rad_listen_send_t)(rad_listen_t *, REQUEST *);
184
185
186 struct rad_listen_t {
187         struct rad_listen_t *next; /* should be rbtree stuff */
188         /*
189          *      For normal sockets.
190          */
191         RAD_LISTEN_TYPE type;
192         int             fd;
193         const char      *identity;
194
195         rad_listen_recv_t recv;
196         rad_listen_send_t send;
197
198         /*
199          *      For normal sockets.
200          */
201         lrad_ipaddr_t   ipaddr;
202         int             port;
203
204         /*
205          *      For detail file
206          *
207          *      FIXME: this stuff should either be in a union,
208          *      or in a structure allocated per-type...
209          */
210         const char      *detail;
211         VALUE_PAIR      *vps;
212         FILE            *fp;
213         int             state;
214         int             max_outstanding;
215         int             *outstanding;
216 };
217
218
219 typedef enum radlog_dest_t {
220   RADLOG_STDOUT = 0,
221   RADLOG_FILES,
222   RADLOG_SYSLOG,
223   RADLOG_STDERR,
224   RADLOG_NULL
225 } radlog_dest_t;
226
227 typedef struct main_config_t {
228         struct main_config *next;
229         time_t          config_dead_time;
230         lrad_ipaddr_t   myip;   /* from the command-line only */
231         int             port;   /* from the command-line only */
232         int             log_auth;
233         int             log_auth_badpass;
234         int             log_auth_goodpass;
235         int             do_usercollide;
236 #ifdef WITH_SNMP
237         int             do_snmp;
238 #endif
239         int             allow_core_dumps;
240         int             debug_level;
241         int             proxy_requests;
242         int             post_proxy_authorize;
243         int             wake_all_if_all_dead;
244         int             proxy_synchronous;
245         int             proxy_dead_time;
246         int             proxy_retry_count;
247         int             proxy_retry_delay;
248         int             proxy_fallback;
249         const char      *proxy_fail_type;
250         int             reject_delay;
251         int             status_server;
252         int             max_request_time;
253         int             cleanup_delay;
254         int             max_requests;
255         int             kill_unresponsive_children;
256         char            *do_lower_user;
257         char            *do_lower_pass;
258         char            *do_nospace_user;
259         char            *do_nospace_pass;
260         char            *nospace_time;
261         char            *log_file;
262         char            *checkrad;
263         const char      *pid_file;
264         const char      *uid_name;
265         const char      *gid_name;
266         rad_listen_t    *listen;
267         int             syslog_facility;
268         int             radlog_fd;
269         radlog_dest_t   radlog_dest;
270         CONF_SECTION    *config;
271         RADCLIENT       *clients;
272         REALM           *realms;
273         const char      *radiusd_conf;
274 } MAIN_CONFIG_T;
275
276 #define DEBUG   if(debug_flag)log_debug
277 #define DEBUG2  if (debug_flag > 1)log_debug
278 #define DEBUG3  if (debug_flag > 2)log_debug
279
280 #define SECONDS_PER_DAY         86400
281 #define MAX_REQUEST_TIME        30
282 #define CLEANUP_DELAY           5
283 #define MAX_REQUESTS            256
284 #define RETRY_DELAY             5
285 #define RETRY_COUNT             3
286 #define DEAD_TIME               120
287
288 #define L_DBG                   1
289 #define L_AUTH                  2
290 #define L_INFO                  3
291 #define L_ERR                   4
292 #define L_PROXY                 5
293 #define L_CONS                  128
294
295 #ifndef FALSE
296 #define FALSE 0
297 #endif
298 #ifndef TRUE
299 /*
300  *      This definition of true as NOT false is definitive. :) Making
301  *      it '1' can cause problems on stupid platforms.  See articles
302  *      on C portability for more information.
303  */
304 #define TRUE (!FALSE)
305 #endif
306
307 /* for paircompare_register */
308 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
309
310 typedef enum request_fail_t {
311   REQUEST_FAIL_UNKNOWN = 0,
312   REQUEST_FAIL_NO_THREADS,      /* no threads to handle it */
313   REQUEST_FAIL_DECODE,          /* rad_decode didn't like it */
314   REQUEST_FAIL_PROXY,           /* call to proxy modules failed */
315   REQUEST_FAIL_PROXY_SEND,      /* proxy_send didn't like it */
316   REQUEST_FAIL_NO_RESPONSE,     /* we weren't told to respond, so we reject */
317   REQUEST_FAIL_HOME_SERVER,     /* the home server didn't respond */
318   REQUEST_FAIL_HOME_SERVER2,    /* another case of the above */
319   REQUEST_FAIL_HOME_SERVER3,    /* another case of the above */
320   REQUEST_FAIL_NORMAL_REJECT,   /* authentication failure */
321   REQUEST_FAIL_SERVER_TIMEOUT   /* the server took too long to process the request */
322 } request_fail_t;
323
324 /*
325  *      Global variables.
326  *
327  *      We really shouldn't have this many.
328  */
329 extern const char       *progname;
330 extern int              debug_flag;
331 extern const char       *radacct_dir;
332 extern const char       *radlog_dir;
333 extern const char       *radlib_dir;
334 extern const char       *radius_dir;
335 extern const char       *radius_libdir;
336 extern uint32_t         expiration_seconds;
337 extern int              log_stripped_names;
338 extern int              log_auth_detail;
339 extern const char      *radiusd_version;
340
341 /*
342  *      Function prototypes.
343  */
344
345 /* acct.c */
346 int             rad_accounting(REQUEST *);
347
348 /* session.c */
349 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
350                              const char *sessionid);
351 int             session_zap(REQUEST *request, uint32_t nasaddr,
352                             unsigned int port, const char *user,
353                             const char *sessionid, uint32_t cliaddr,
354                             char proto,int session_time);
355
356 /* radiusd.c */
357 #ifndef _LIBRADIUS
358 void            debug_pair(FILE *, VALUE_PAIR *);
359 #endif
360 int             log_err (char *);
361
362 /* util.c */
363 void (*reset_signal(int signo, void (*func)(int)))(int);
364 void            request_free(REQUEST **request);
365 int             rad_mkdir(char *directory, int mode);
366 int             rad_checkfilename(const char *filename);
367 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
368 REQUEST         *request_alloc(void);
369 REQUEST         *request_alloc_fake(REQUEST *oldreq);
370 int             request_data_add(REQUEST *request,
371                                  void *unique_ptr, int unique_int,
372                                  void *opaque, void (*free_opaque)(void *));
373 void            *request_data_get(REQUEST *request,
374                                   void *unique_ptr, int unique_int);
375 int             rad_copy_string(char *dst, const char *src);
376 int             rad_copy_variable(char *dst, const char *from);
377
378 /* request_process.c */
379 int rad_respond(REQUEST *request, RAD_REQUEST_FUNP fun);
380 void            request_reject(REQUEST *request, request_fail_t reason);
381 void            rfc_clean(RADIUS_PACKET *packet);
382
383 /* client.c */
384 int             read_clients_file(const char *file);
385 RADCLIENT       *client_find(const lrad_ipaddr_t *ipaddr);
386 const char      *client_name(const lrad_ipaddr_t *ipaddr);
387 void            clients_free(RADCLIENT *cl);
388
389 /* files.c */
390 REALM           *realm_find(const char *, int);
391 REALM           *realm_findbyaddr(uint32_t ipno, int port);
392 void            realm_free(REALM *cl);
393 void            realm_disable(uint32_t ipno, int port);
394 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
395 void            pairlist_free(PAIR_LIST **);
396 int             read_config_files(void);
397 int             read_realms_file(const char *file);
398
399 /* nas.c */
400 int             read_naslist_file(char *);
401 NAS             *nas_find(uint32_t ipno);
402
403 /* version.c */
404 void            version(void);
405
406 /* log.c */
407 int             vradlog(int, const char *, va_list ap);
408 int             radlog(int, const char *, ...)
409 #ifdef __GNUC__
410                 __attribute__ ((format (printf, 2, 3)))
411 #endif
412 ;
413 int             log_debug(const char *, ...)
414 #ifdef __GNUC__
415                 __attribute__ ((format (printf, 1, 2)))
416 #endif
417 ;
418 void            vp_listdebug(VALUE_PAIR *vp);
419
420 /* proxy.c */
421 int proxy_receive(REQUEST *request);
422 int proxy_send(REQUEST *request);
423
424 /* auth.c */
425 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
426 int             rad_authenticate (REQUEST *);
427 int             rad_check_password(REQUEST *request);
428 int             rad_postauth(REQUEST *);
429
430 /* exec.c */
431 int             radius_exec_program(const char *,  REQUEST *, int,
432                                     char *user_msg, int msg_len,
433                                     VALUE_PAIR *input_pairs,
434                                     VALUE_PAIR **output_pairs,
435                                         int shell_escape);
436
437 /* timestr.c */
438 int             timestr_match(char *, time_t);
439
440 /* valuepair.c */
441 int             paircompare_register(int attr, int otherattr,
442                                      RAD_COMPARE_FUNC func,
443                                      void *instance);
444 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
445 int             paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
446                         VALUE_PAIR **reply);
447 int             simplepaircmp(REQUEST *, VALUE_PAIR *, VALUE_PAIR *);
448 void            pair_builtincompare_init(void);
449 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
450
451 /* xlat.c */
452 typedef int (*RADIUS_ESCAPE_STRING)(char *out, int outlen, const char *in);
453
454 int            radius_xlat(char * out, int outlen, const char *fmt,
455                            REQUEST * request, RADIUS_ESCAPE_STRING func);
456 typedef int (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING func);
457 int             xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance);
458 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
459
460
461 /* threads.c */
462 extern          int thread_pool_init(int spawn_flag);
463 extern          int thread_pool_clean(time_t now);
464 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
465 extern          pid_t rad_fork(int exec_wait);
466 extern          pid_t rad_waitpid(pid_t pid, int *status, int options);
467 extern          int total_active_threads(void);
468
469 #ifndef HAVE_PTHREAD_H
470 #define rad_fork(n) fork()
471 #define rad_waitpid waitpid
472 #endif
473
474 /* mainconfig.c */
475 /* Define a global config structure */
476 extern struct main_config_t mainconfig;
477
478 int read_mainconfig(int reload);
479 int free_mainconfig(void);
480 CONF_SECTION *read_radius_conf_file(void); /* for radwho and friends. */
481
482 /* listen.c */
483 void listen_free(rad_listen_t **head);
484 int listen_init(const char *filename, rad_listen_t **head);
485 rad_listen_t *proxy_new_listener(void);
486
487 #endif /*RADIUSD_H*/