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