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