import from HEAD:
[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         void                    *container;
89 } REQUEST;
90
91 #define RAD_REQUEST_OPTION_NONE            (0)
92 #define RAD_REQUEST_OPTION_LOGGED_CHILD    (1 << 0)
93 #define RAD_REQUEST_OPTION_DELAYED_REJECT  (1 << 1)
94 #define RAD_REQUEST_OPTION_DONT_CACHE      (1 << 2)
95 #define RAD_REQUEST_OPTION_FAKE_REQUEST    (1 << 3)
96 #define RAD_REQUEST_OPTION_REJECTED        (1 << 4)
97 #define RAD_REQUEST_OPTION_PROXIED         (1 << 5)
98
99 /*
100  *  Function handler for requests.
101  */
102 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
103
104 typedef struct radclient {
105         uint32_t                ipaddr;
106         uint32_t                netmask;
107         char                    longname[256];
108         u_char                  secret[32];
109         char                    shortname[32];
110         char                    nastype[32];
111         char                    login[32];
112         char                    password[32];
113         struct radclient        *next;
114 } RADCLIENT;
115
116 typedef struct nas {
117         uint32_t                ipaddr;
118         char                    longname[256];
119         char                    shortname[32];
120         char                    nastype[32];
121         struct nas              *next;
122 } NAS;
123
124 typedef struct _realm {
125         char                    realm[64];
126         char                    server[64];
127         char                    acct_server[64];
128         uint32_t                ipaddr; /* authentication */
129         uint32_t                acct_ipaddr;
130         u_char                  secret[32];
131         time_t                  last_reply; /* last time we saw a packet */
132         int                     auth_port;
133         int                     acct_port;
134         int                     striprealm;
135         int                     trusted; /* old */
136         int                     notrealm;
137         int                     active; /* is it dead? */
138         time_t                  wakeup; /* when we should try it again */
139         int                     acct_active;
140         time_t                  acct_wakeup;
141         int                     ldflag;
142         struct _realm           *next;
143 } REALM;
144
145 typedef struct pair_list {
146         char                    *name;
147         VALUE_PAIR              *check;
148         VALUE_PAIR              *reply;
149         int                     lineno;
150         struct pair_list        *next;
151         struct pair_list        *lastdefault;
152 } PAIR_LIST;
153
154
155 /*
156  *      Types of listeners.
157  */
158 typedef enum RAD_LISTEN_TYPE {
159         RAD_LISTEN_NONE = 0,
160         RAD_LISTEN_AUTH,
161         RAD_LISTEN_ACCT,
162         RAD_LISTEN_PROXY
163 } RAD_LISTEN_TYPE;
164
165
166 /*
167  *      For listening on multiple IP's and ports.
168  */
169 typedef struct rad_listen_t {
170         struct rad_listen_t *next; /* could be rbtree stuff */
171         uint32_t        ipaddr;
172         RAD_LISTEN_TYPE type;
173         int             port;
174         int             fd;
175 } rad_listen_t;
176
177
178 typedef struct main_config_t {
179         struct main_config *next;
180         time_t          config_dead_time;
181         uint32_t        myip;
182         int             log_auth;
183         int             log_auth_badpass;
184         int             log_auth_goodpass;
185         int             do_usercollide;
186 #ifdef WITH_SNMP
187         int             do_snmp;
188 #endif
189         int             allow_core_dumps;
190         int             debug_level;
191         int             proxy_requests;
192         int             post_proxy_authorize;
193         int             wake_all_if_all_dead;
194         int             proxy_synchronous;
195         int             proxy_dead_time;
196         int             proxy_retry_count;
197         int             proxy_retry_delay;
198         int             proxy_fallback;
199         int             reject_delay;
200         int             status_server;
201         int             max_request_time;
202         int             cleanup_delay;
203         int             max_requests;
204         int             kill_unresponsive_children;
205         char            *do_lower_user;
206         char            *do_lower_pass;
207         char            *do_nospace_user;
208         char            *do_nospace_pass;
209         char            *nospace_time;
210         char            *log_file;
211         char            *checkrad;
212         const char      *pid_file;
213         const char      *uid_name;
214         const char      *gid_name;
215         rad_listen_t    *listen;
216         CONF_SECTION    *config;
217         RADCLIENT       *clients;
218         REALM           *realms;
219 } MAIN_CONFIG_T;
220
221 #define DEBUG   if(debug_flag)log_debug
222 #define DEBUG2  if (debug_flag > 1)log_debug
223 #define DEBUG3  if (debug_flag > 2)log_debug
224
225 #define SECONDS_PER_DAY         86400
226 #define MAX_REQUEST_TIME        30
227 #define CLEANUP_DELAY           5
228 #define MAX_REQUESTS            256
229 #define RETRY_DELAY             5
230 #define RETRY_COUNT             3
231 #define DEAD_TIME               120
232
233 #define L_DBG                   1
234 #define L_AUTH                  2
235 #define L_INFO                  3
236 #define L_ERR                   4
237 #define L_PROXY                 5
238 #define L_CONS                  128
239
240 #ifndef FALSE
241 #define FALSE 0
242 #endif
243 #ifndef TRUE
244 /*
245  *      This definition of true as NOT false is definitive. :) Making
246  *      it '1' can cause problems on stupid platforms.  See articles
247  *      on C portability for more information.
248  */
249 #define TRUE (!FALSE)
250 #endif
251
252 /* for paircompare_register */
253 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
254
255 typedef enum radlog_dest_t {
256   RADLOG_FILES = 0,
257   RADLOG_SYSLOG,
258   RADLOG_STDOUT,
259   RADLOG_STDERR,
260   RADLOG_NULL
261 } radlog_dest_t;
262
263 /*
264  *      Global variables.
265  *
266  *      We really shouldn't have this many.
267  */
268 extern const char       *progname;
269 extern int              debug_flag;
270 extern int              syslog_facility;
271 extern const char       *radacct_dir;
272 extern const char       *radlog_dir;
273 extern const char       *radlib_dir;
274 extern const char       *radius_dir;
275 extern const char       *radius_libdir;
276 extern radlog_dest_t    radlog_dest;
277 extern uint32_t         expiration_seconds;
278 extern int              log_stripped_names;
279 extern int              log_auth_detail;
280 extern int              acct_port;
281 extern const char      *radiusd_version;
282
283 /*
284  *      Function prototypes.
285  */
286
287 /* acct.c */
288 int             rad_accounting(REQUEST *);
289
290 /* session.c */
291 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
292                              const char *sessionid);
293 int             session_zap(REQUEST *request, uint32_t nasaddr,
294                             unsigned int port, const char *user,
295                             const char *sessionid, uint32_t cliaddr,
296                             char proto);
297
298 /* radiusd.c */
299 #ifndef _LIBRADIUS
300 void            debug_pair(FILE *, VALUE_PAIR *);
301 #endif
302 int             log_err (char *);
303 int             rad_respond(REQUEST *, RAD_REQUEST_FUNP fun);
304
305 /* util.c */
306 void (*reset_signal(int signo, void (*func)(int)))(int);
307 void            request_free(REQUEST **request);
308 int             rad_mkdir(char *directory, int mode);
309 int             rad_checkfilename(const char *filename);
310 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
311 void            xfree(const char *ptr);
312 REQUEST         *request_alloc(void);
313 REQUEST         *request_alloc_fake(REQUEST *oldreq);
314 int             request_data_add(REQUEST *request,
315                                  void *unique_ptr, int unique_int,
316                                  void *opaque, void (*free_opaque)(void *));
317 void            *request_data_get(REQUEST *request,
318                                   void *unique_ptr, int unique_int);
319 void            request_reject(REQUEST *request);
320 void            rfc_clean(RADIUS_PACKET *packet);
321
322 /* client.c */
323 int             read_clients_file(const char *file);
324 RADCLIENT       *client_find(uint32_t ipno);
325 const char      *client_name(uint32_t ipno);
326 void            client_walk(void);
327 void            clients_free(RADCLIENT *cl);
328
329 /* files.c */
330 REALM           *realm_find(const char *, int);
331 REALM           *realm_findbyaddr(uint32_t ipno, int port);
332 void            realm_free(REALM *cl);
333 void            realm_disable(uint32_t ipno, int port);
334 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
335 void            pairlist_free(PAIR_LIST **);
336 int             read_config_files(void);
337 int             read_realms_file(const char *file);
338
339 /* nas.c */
340 int             read_naslist_file(char *);
341 NAS             *nas_find(uint32_t ipno);
342 const char      *nas_name(uint32_t ipno);
343 const char      *nas_name2(RADIUS_PACKET *r);
344 char  *         nas_name3(char *buf, size_t buflen, uint32_t ipno);
345 NAS             *nas_findbyname(char *nasname);
346
347 /* version.c */
348 void            version(void);
349
350 /* log.c */
351 int             vradlog(int, const char *, va_list ap);
352 int             radlog(int, const char *, ...)
353 #ifdef __GNUC__
354                 __attribute__ ((format (printf, 2, 3)))
355 #endif
356 ;
357 int             log_debug(const char *, ...)
358 #ifdef __GNUC__
359                 __attribute__ ((format (printf, 1, 2)))
360 #endif
361 ;
362 void            vp_listdebug(VALUE_PAIR *vp);
363
364 /* proxy.c */
365 int proxy_receive(REQUEST *request);
366 int proxy_send(REQUEST *request);
367
368 /* auth.c */
369 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
370 int             rad_authenticate (REQUEST *);
371 int             rad_check_password(REQUEST *request);
372 int             rad_postauth(REQUEST *);
373
374 /* exec.c */
375 int             radius_exec_program(const char *,  REQUEST *, int,
376                                     char *user_msg, int msg_len,
377                                     VALUE_PAIR *input_pairs,
378                                     VALUE_PAIR **output_pairs);
379
380 /* timestr.c */
381 int             timestr_match(char *, time_t);
382
383 /* valuepair.c */
384 int             paircompare_register(int attr, int otherattr,
385                                      RAD_COMPARE_FUNC func,
386                                      void *instance);
387 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
388 int             paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
389                         VALUE_PAIR **reply);
390 int             simplepaircmp(REQUEST *, VALUE_PAIR *, VALUE_PAIR *);
391 void            pair_builtincompare_init(void);
392 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
393
394 /* xlat.c */
395 typedef int (*RADIUS_ESCAPE_STRING)(char *out, int outlen, const char *in);
396
397 int            radius_xlat(char * out, int outlen, const char *fmt,
398                            REQUEST * request, RADIUS_ESCAPE_STRING func);
399 typedef int (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING func);
400 int             xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance);
401 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
402
403
404 /* threads.c */
405 extern          int thread_pool_init(void);
406 extern          int thread_pool_clean(time_t now);
407 extern          int thread_pool_addrequest(REQUEST *, RAD_REQUEST_FUNP);
408 extern          pid_t rad_fork(void);
409 extern          pid_t rad_waitpid(pid_t pid, int *status);
410 extern          int total_active_threads(void);
411
412 #ifndef HAVE_PTHREAD_H
413 #define rad_fork(n) fork()
414 #define rad_waitpid(a,b) waitpid(a,b,0)
415 #endif
416
417 /* mainconfig.h */
418 /* Define a global config structure */
419 extern struct main_config_t mainconfig;
420
421 int read_mainconfig(int reload);
422 int free_mainconfig(void);
423 CONF_SECTION *read_radius_conf_file(void); /* for radwho and friends. */
424 #endif /*RADIUSD_H*/