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