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