Add re-transmits of accounting packets back in, that was deleted
[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_start_time;
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 typedef int (*rad_listen_print_t)(rad_listen_t *, char *, size_t);
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         rad_listen_print_t print;
206
207         void            *data;
208 };
209
210
211 typedef enum radlog_dest_t {
212   RADLOG_STDOUT = 0,
213   RADLOG_FILES,
214   RADLOG_SYSLOG,
215   RADLOG_STDERR,
216   RADLOG_NULL
217 } radlog_dest_t;
218
219 typedef struct main_config_t {
220         struct main_config *next;
221         time_t          config_dead_time;
222         lrad_ipaddr_t   myip;   /* from the command-line only */
223         int             port;   /* from the command-line only */
224         int             log_auth;
225         int             log_auth_badpass;
226         int             log_auth_goodpass;
227         int             do_usercollide;
228 #ifdef WITH_SNMP
229         int             do_snmp;
230 #endif
231         int             allow_core_dumps;
232         int             debug_level;
233         int             proxy_requests;
234         int             post_proxy_authorize;
235         int             wake_all_if_all_dead;
236         int             proxy_synchronous;
237         int             proxy_dead_time;
238         int             proxy_retry_count;
239         int             proxy_retry_delay;
240         int             proxy_fallback;
241         const char      *proxy_fail_type;
242         int             reject_delay;
243         int             status_server;
244         int             max_request_time;
245         int             cleanup_delay;
246         int             max_requests;
247         int             kill_unresponsive_children;
248         char            *do_lower_user;
249         char            *do_lower_pass;
250         char            *do_nospace_user;
251         char            *do_nospace_pass;
252         char            *nospace_time;
253         char            *log_file;
254         char            *checkrad;
255         const char      *pid_file;
256         const char      *uid_name;
257         const char      *gid_name;
258         rad_listen_t    *listen;
259         int             syslog_facility;
260         int             radlog_fd;
261         radlog_dest_t   radlog_dest;
262         CONF_SECTION    *config;
263         RADCLIENT       *clients;
264         REALM           *realms;
265         const char      *radiusd_conf;
266 } MAIN_CONFIG_T;
267
268 #define DEBUG   if(debug_flag)log_debug
269 #define DEBUG2  if (debug_flag > 1)log_debug
270 #define DEBUG3  if (debug_flag > 2)log_debug
271
272 #define SECONDS_PER_DAY         86400
273 #define MAX_REQUEST_TIME        30
274 #define CLEANUP_DELAY           5
275 #define MAX_REQUESTS            256
276 #define RETRY_DELAY             5
277 #define RETRY_COUNT             3
278 #define DEAD_TIME               120
279
280 #define L_DBG                   1
281 #define L_AUTH                  2
282 #define L_INFO                  3
283 #define L_ERR                   4
284 #define L_PROXY                 5
285 #define L_CONS                  128
286
287 #ifndef FALSE
288 #define FALSE 0
289 #endif
290 #ifndef TRUE
291 /*
292  *      This definition of true as NOT false is definitive. :) Making
293  *      it '1' can cause problems on stupid platforms.  See articles
294  *      on C portability for more information.
295  */
296 #define TRUE (!FALSE)
297 #endif
298
299 /* for paircompare_register */
300 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
301
302 typedef enum request_fail_t {
303   REQUEST_FAIL_UNKNOWN = 0,
304   REQUEST_FAIL_NO_THREADS,      /* no threads to handle it */
305   REQUEST_FAIL_DECODE,          /* rad_decode didn't like it */
306   REQUEST_FAIL_PROXY,           /* call to proxy modules failed */
307   REQUEST_FAIL_PROXY_SEND,      /* proxy_send didn't like it */
308   REQUEST_FAIL_NO_RESPONSE,     /* we weren't told to respond, so we reject */
309   REQUEST_FAIL_HOME_SERVER,     /* the home server didn't respond */
310   REQUEST_FAIL_HOME_SERVER2,    /* another case of the above */
311   REQUEST_FAIL_HOME_SERVER3,    /* another case of the above */
312   REQUEST_FAIL_NORMAL_REJECT,   /* authentication failure */
313   REQUEST_FAIL_SERVER_TIMEOUT   /* the server took too long to process the request */
314 } request_fail_t;
315
316 /*
317  *      Global variables.
318  *
319  *      We really shouldn't have this many.
320  */
321 extern const char       *progname;
322 extern int              debug_flag;
323 extern const char       *radacct_dir;
324 extern const char       *radlog_dir;
325 extern const char       *radlib_dir;
326 extern const char       *radius_dir;
327 extern const char       *radius_libdir;
328 extern uint32_t         expiration_seconds;
329 extern int              log_stripped_names;
330 extern int              log_auth_detail;
331 extern const char      *radiusd_version;
332
333 /*
334  *      Function prototypes.
335  */
336
337 /* acct.c */
338 int             rad_accounting(REQUEST *);
339
340 /* session.c */
341 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
342                              const char *sessionid);
343 int             session_zap(REQUEST *request, uint32_t nasaddr,
344                             unsigned int port, const char *user,
345                             const char *sessionid, uint32_t cliaddr,
346                             char proto,int session_time);
347
348 /* radiusd.c */
349 #ifndef _LIBRADIUS
350 void            debug_pair(FILE *, VALUE_PAIR *);
351 #endif
352 int             log_err (char *);
353
354 /* util.c */
355 void (*reset_signal(int signo, void (*func)(int)))(int);
356 void            request_free(REQUEST **request);
357 int             rad_mkdir(char *directory, int mode);
358 int             rad_checkfilename(const char *filename);
359 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
360 REQUEST         *request_alloc(void);
361 REQUEST         *request_alloc_fake(REQUEST *oldreq);
362 int             request_data_add(REQUEST *request,
363                                  void *unique_ptr, int unique_int,
364                                  void *opaque, void (*free_opaque)(void *));
365 void            *request_data_get(REQUEST *request,
366                                   void *unique_ptr, int unique_int);
367 int             rad_copy_string(char *dst, const char *src);
368 int             rad_copy_variable(char *dst, const char *from);
369
370 /* request_process.c */
371 int rad_respond(REQUEST *request, RAD_REQUEST_FUNP fun);
372 void            request_reject(REQUEST *request, request_fail_t reason);
373 void            rfc_clean(RADIUS_PACKET *packet);
374
375 /* client.c */
376 int             read_clients_file(const char *file);
377 RADCLIENT       *client_find(const lrad_ipaddr_t *ipaddr);
378 const char      *client_name(const lrad_ipaddr_t *ipaddr);
379 void            clients_free(RADCLIENT *cl);
380
381 /* files.c */
382 REALM           *realm_find(const char *, int);
383 REALM           *realm_findbyaddr(uint32_t ipno, int port);
384 void            realm_free(REALM *cl);
385 void            realm_disable(uint32_t ipno, int port);
386 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
387 void            pairlist_free(PAIR_LIST **);
388 int             read_config_files(void);
389 int             read_realms_file(const char *file);
390
391 /* nas.c */
392 int             read_naslist_file(char *);
393 NAS             *nas_find(uint32_t ipno);
394
395 /* version.c */
396 void            version(void);
397
398 /* log.c */
399 int             vradlog(int, const char *, va_list ap);
400 int             radlog(int, const char *, ...)
401 #ifdef __GNUC__
402                 __attribute__ ((format (printf, 2, 3)))
403 #endif
404 ;
405 int             log_debug(const char *, ...)
406 #ifdef __GNUC__
407                 __attribute__ ((format (printf, 1, 2)))
408 #endif
409 ;
410 void            vp_listdebug(VALUE_PAIR *vp);
411
412 /* proxy.c */
413 int proxy_receive(REQUEST *request);
414 int proxy_send(REQUEST *request);
415
416 /* auth.c */
417 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
418 int             rad_authenticate (REQUEST *);
419 int             rad_check_password(REQUEST *request);
420 int             rad_postauth(REQUEST *);
421
422 /* exec.c */
423 int             radius_exec_program(const char *,  REQUEST *, int,
424                                     char *user_msg, int msg_len,
425                                     VALUE_PAIR *input_pairs,
426                                     VALUE_PAIR **output_pairs,
427                                         int shell_escape);
428
429 /* timestr.c */
430 int             timestr_match(char *, time_t);
431
432 /* valuepair.c */
433 int             paircompare_register(int attr, int otherattr,
434                                      RAD_COMPARE_FUNC func,
435                                      void *instance);
436 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
437 int             paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
438                         VALUE_PAIR **reply);
439 int             simplepaircmp(REQUEST *, VALUE_PAIR *, VALUE_PAIR *);
440 void            pair_builtincompare_init(void);
441 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
442
443 /* xlat.c */
444 typedef int (*RADIUS_ESCAPE_STRING)(char *out, int outlen, const char *in);
445
446 int            radius_xlat(char * out, int outlen, const char *fmt,
447                            REQUEST * request, RADIUS_ESCAPE_STRING func);
448 typedef int (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING func);
449 int             xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance);
450 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
451
452
453 /* threads.c */
454 extern          int thread_pool_init(int spawn_flag);
455 extern          int thread_pool_clean(time_t now);
456 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
457 extern          pid_t rad_fork(int exec_wait);
458 extern          pid_t rad_waitpid(pid_t pid, int *status, int options);
459 extern          int total_active_threads(void);
460
461 #ifndef HAVE_PTHREAD_H
462 #define rad_fork(n) fork()
463 #define rad_waitpid waitpid
464 #endif
465
466 /* mainconfig.c */
467 /* Define a global config structure */
468 extern struct main_config_t mainconfig;
469
470 int read_mainconfig(int reload);
471 int free_mainconfig(void);
472 CONF_SECTION *read_radius_conf_file(void); /* for radwho and friends. */
473
474 /* listen.c */
475 void listen_free(rad_listen_t **head);
476 int listen_init(const char *filename, rad_listen_t **head);
477 rad_listen_t *proxy_new_listener(void);
478
479 #endif /*RADIUSD_H*/