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