0982b628c2144e536afd5b8a6040e0e2166a1bcd
[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 "missing.h"
14 #include "conffile.h"
15
16 #include <stdarg.h>
17
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #if 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 #define NO_SUCH_CHILD_PID (child_pid_t) (0)
32
33 #ifndef NDEBUG
34 #define REQUEST_MAGIC (0xdeadbeef)
35 #endif
36
37 typedef struct auth_req {
38 #ifndef NDEBUG
39         uint32_t                magic; /* for debugging only */
40 #endif
41         RADIUS_PACKET           *packet;
42         RADIUS_PACKET           *proxy;
43         RADIUS_PACKET           *reply;
44         RADIUS_PACKET           *proxy_reply;
45         VALUE_PAIR              *config_items;
46         VALUE_PAIR              *username;
47         VALUE_PAIR              *password;
48         char                    secret[32];
49         child_pid_t             child_pid;
50         time_t                  timestamp;
51         int                     number; /* internal server number */
52
53         /* Could almost keep a const char * here instead of a _copy_ of the
54          * secret... but what if the RADCLIENT structure is freed because it was
55          * taken out of the config file and SIGHUPed? */
56         char                    proxysecret[32];
57         int                     proxy_is_replicate;
58         int                     proxy_try_count;
59         time_t                  proxy_next_try;
60
61         int                     simul_max;
62         int                     simul_count;
63         int                     simul_mpp; /* WEIRD: 1 is false, 2 is true */
64
65         int                     finished;
66         int                     options; /* miscellanous options */
67         void                    **container;
68 } REQUEST;
69
70 #define RAD_REQUEST_OPTION_NONE            (0)
71 #define RAD_REQUEST_OPTION_LOGGED_CHILD    (1 << 0)
72 #define RAD_REQUEST_OPTION_DELAYED_REJECT  (1 << 1)
73 #define RAD_REQUEST_OPTION_DONT_CACHE      (1 << 2)
74
75 /*
76  *  Function handler for requests.
77  */
78 typedef         int (*RAD_REQUEST_FUNP)(REQUEST *);
79
80 typedef struct radclient {
81         uint32_t                ipaddr;
82         uint32_t                netmask;
83         char                    longname[256];
84         u_char                  secret[32];
85         char                    shortname[32];
86         char                    nastype[32];
87         char                    login[32];
88         char                    password[32];
89         struct radclient        *next;
90 } RADCLIENT;
91
92 typedef struct nas {
93         uint32_t                ipaddr;
94         char                    longname[256];
95         char                    shortname[32];
96         char                    nastype[32];
97         struct nas              *next;
98 } NAS;
99
100 typedef struct _realm {
101         char                    realm[64];
102         char                    server[64];
103         char                    acct_server[64];
104         uint32_t                ipaddr; /* authentication */
105         uint32_t                acct_ipaddr;
106         u_char                  secret[32];
107         time_t                  last_reply; /* last time we saw a packet */
108         int                     auth_port;
109         int                     acct_port;
110         int                     striprealm;
111         int                     trusted; /* old */
112         int                     notrealm;
113         int                     active; /* is it dead? */
114         time_t                  wakeup; /* when we should try it again */
115         int                     acct_active;
116         time_t                  acct_wakeup;
117         int                     ldflag;
118         struct _realm           *next;
119 } REALM;
120
121 typedef struct pair_list {
122         char                    *name;
123         VALUE_PAIR              *check;
124         VALUE_PAIR              *reply;
125         int                     lineno;
126         struct pair_list        *next;
127         struct pair_list        *lastdefault;
128 } PAIR_LIST;
129
130 typedef struct main_config_t {
131         struct main_config *next;
132         time_t          config_dead_time;
133         uint32_t        myip;
134         int             log_auth;
135         int             log_auth_badpass;
136         int             log_auth_goodpass;
137         int             do_usercollide;
138 #if WITH_SNMP
139         int             do_snmp;
140 #endif
141         int             allow_core_dumps;
142         int             debug_level;
143         int             proxy_requests;
144         int             post_proxy_authorize;
145         int             wake_all_if_all_dead;
146         int             proxy_synchronous;
147         int             proxy_dead_time;
148         int             proxy_retry_count;
149         int             proxy_retry_delay;
150         int             proxy_fallback;
151         int             reject_delay;
152         int             status_server;
153         int             max_request_time;
154         int             cleanup_delay;
155         int             max_requests;
156         int             kill_unresponsive_children;
157         char            *do_lower_user;
158         char            *do_lower_pass;
159         char            *do_nospace_user;
160         char            *do_nospace_pass;
161         char            *nospace_time;
162         char            *log_file;
163         char            *checkrad;
164         const char      *pid_file;
165         const char      *uid_name;
166         const char      *gid_name;
167         CONF_SECTION    *config;
168         RADCLIENT       *clients;
169         REALM           *realms;
170 } MAIN_CONFIG_T;
171
172 #define DEBUG   if(debug_flag)log_debug
173 #define DEBUG2  if (debug_flag > 1)log_debug
174
175 #define SECONDS_PER_DAY         86400
176 #define MAX_REQUEST_TIME        30
177 #define CLEANUP_DELAY           5
178 #define MAX_REQUESTS            256
179 #define RETRY_DELAY             5
180 #define RETRY_COUNT             3
181 #define DEAD_TIME               120
182
183 #define L_DBG                   1
184 #define L_AUTH                  2
185 #define L_INFO                  3
186 #define L_ERR                   4
187 #define L_PROXY                 5
188 #define L_CONS                  128
189
190 #ifndef FALSE
191 #define FALSE 0
192 #endif
193 #ifndef TRUE
194 /*
195  *      This definition of true as NOT false is definitive. :) Making
196  *      it '1' can cause problems on stupid platforms.  See articles
197  *      on C portability for more information.
198  */
199 #define TRUE (!FALSE)
200 #endif
201
202 /* for paircompare_register */
203 typedef int (*RAD_COMPARE_FUNC)(void *instance, REQUEST *,VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
204
205 typedef enum radlog_dest_t {
206   RADLOG_FILES = 0,
207   RADLOG_SYSLOG,
208   RADLOG_STDOUT,
209   RADLOG_STDERR,
210   RADLOG_NULL
211 } radlog_dest_t;
212
213 /*
214  *      Global variables.
215  */
216 extern const char       *progname;
217 extern int              debug_flag;
218 extern int              syslog_facility;
219 extern const char       *radacct_dir;
220 extern const char       *radlog_dir;
221 extern const char       *radlib_dir;
222 extern const char       *radius_dir;
223 extern const char       *radius_libdir;
224 extern radlog_dest_t    radlog_dest;
225 extern uint32_t         expiration_seconds;
226 extern int              log_stripped_names;
227 extern int              log_auth_detail;
228 extern int              auth_port;
229 extern int              acct_port;
230 extern int              proxy_port;
231 extern int              proxyfd;
232 extern const char      *radiusd_version;
233
234 /*
235  *      Function prototypes.
236  */
237
238 /* acct.c */
239 int             rad_accounting(REQUEST *);
240
241 /* session.c */
242 int             rad_check_ts(uint32_t nasaddr, unsigned int port, const char *user,
243                              const char *sessionid);
244 int             session_zap(int fd, uint32_t nasaddr, unsigned int port, const char *user,
245                             const char *sessionid, uint32_t cliaddr,
246                             char proto, time_t t);
247
248 /* radiusd.c */
249 void            debug_pair(FILE *, VALUE_PAIR *);
250 int             log_err (char *);
251 int             rad_process(REQUEST *, int);
252 int             rad_respond(REQUEST *, RAD_REQUEST_FUNP fun);
253
254 /* util.c */
255 void (*reset_signal(int signo, void (*func)(int)))(int);
256 void            request_free(REQUEST **request);
257 int             rad_mkdir(char *directory, int mode);
258 int             rad_checkfilename(const char *filename);
259 void            *rad_malloc(size_t size); /* calls exit(1) on error! */
260 void            xfree(const char *ptr);
261 void            rad_assert_fail (const char *file, unsigned int line);
262
263 /* client.c */
264 int             read_clients_file(const char *file);
265 RADCLIENT       *client_find(uint32_t ipno);
266 const char      *client_name(uint32_t ipno);
267 void            client_walk(void);
268 void            clients_free(RADCLIENT *cl);
269
270 /* files.c */
271 REALM           *realm_find(const char *, int);
272 REALM           *realm_findbyaddr(uint32_t ipno, int port);
273 void            realm_free(REALM *cl);
274 void            realm_disable(uint32_t ipno, int port);
275 int             pairlist_read(const char *file, PAIR_LIST **list, int complain);
276 void            pairlist_free(PAIR_LIST **);
277 int             read_config_files(void);
278 int             read_realms_file(const char *file);
279
280 /* nas.c */
281 int             read_naslist_file(char *);
282 NAS             *nas_find(uint32_t ipno);
283 const char      *nas_name(uint32_t ipno);
284 const char      *nas_name2(RADIUS_PACKET *r);
285 char  *         nas_name3(char *buf, size_t buflen, uint32_t ipno);
286 NAS             *nas_findbyname(char *nasname);
287
288 /* version.c */
289 void            version(void);
290
291 /* log.c */
292 int             vradlog(int, const char *, va_list ap);
293 int             radlog(int, const char *, ...)
294 #ifdef __GNUC__
295                 __attribute__ ((format (printf, 2, 3)))
296 #endif
297 ;
298 int             log_debug(const char *, ...)
299 #ifdef __GNUC__
300                 __attribute__ ((format (printf, 1, 2)))
301 #endif
302 ;
303 void            vp_listdebug(VALUE_PAIR *vp);
304
305 /* proxy.c */
306 int proxy_receive(REQUEST *request);
307 int proxy_send(REQUEST *request);
308
309 /* auth.c */
310 char    *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli);
311 int             rad_authenticate (REQUEST *);
312 int             rad_check_password(REQUEST *request);
313
314 /* exec.c */
315 int             radius_exec_program(const char *,  REQUEST *, int,
316                                     char *user_msg, int msg_len,
317                                     VALUE_PAIR *input_pairs,
318                                     VALUE_PAIR **output_pairs);
319
320 /* timestr.c */
321 int             timestr_match(char *, time_t);
322
323 /* valuepair.c */
324 int             paircompare_register(int attr, int otherattr,
325                                      RAD_COMPARE_FUNC func,
326                                      void *instance);
327 void            paircompare_unregister(int attr, RAD_COMPARE_FUNC func);
328 int             paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
329                         VALUE_PAIR **reply);
330 int             simplepaircmp(REQUEST *, VALUE_PAIR *, VALUE_PAIR *);
331 void            pair_builtincompare_init(void);
332 void            pairxlatmove(REQUEST *, VALUE_PAIR **to, VALUE_PAIR **from);
333
334 /* xlat.c */
335 typedef int (*RADIUS_ESCAPE_STRING)(char *out, int outlen, const char *in);
336
337 int            radius_xlat(char * out, int outlen, const char *fmt,
338                            REQUEST * request, RADIUS_ESCAPE_STRING func);
339 typedef int (*RAD_XLAT_FUNC)(void *instance, REQUEST *, char *, char *, int, RADIUS_ESCAPE_STRING func);
340 int             xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance);
341 void            xlat_unregister(const char *module, RAD_XLAT_FUNC func);
342
343
344 /* threads.c */
345 extern          int thread_pool_init(void);
346 extern          int thread_pool_clean(time_t now);
347 extern          void rad_exec_init(void);
348 extern          pid_t rad_fork(int exec_wait);
349 extern          pid_t rad_waitpid(pid_t pid, int *status, int options);
350 extern          int rad_savepid(pid_t pid, int status);
351 extern          int total_active_threads(void);
352
353 #ifndef HAVE_PTHREAD_H
354 #define rad_fork(n) fork()
355 #define rad_waitpid waitpid
356 #endif
357
358 /* mainconfig.h */
359 /* Define a global config structure */
360 extern struct main_config_t mainconfig;
361
362 int read_mainconfig(int reload);
363 int free_mainconfig(void);
364 CONF_SECTION *read_radius_conf_file(void); /* for radwho and friends. */
365 #endif /*RADIUSD_H*/