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