Make hash table have comparison callback, it's needed.
[freeradius.git] / src / include / libradius.h
1 #ifndef LIBRADIUS_H
2 #define LIBRADIUS_H
3
4 /*
5  * libradius.h  Structures and prototypes
6  *              for the radius library.
7  *
8  * Version:     $Id$
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  * Copyright 2001,2002,2003,2004,2005,2006  The FreeRADIUS server project
25  */
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #endif
34
35 #ifdef HAVE_INTTYPES_H
36 #include <inttypes.h>
37 #endif
38
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 #include        <netinet/in.h>
45 #endif
46
47 #ifdef HAVE_ARPA_INET_H
48 #include        <arpa/inet.h>
49 #endif
50
51 #ifdef HAVE_SYS_SOCKET_H
52 #include        <sys/socket.h>
53 #endif
54
55 #include <stdio.h>
56
57 /*
58  *  Check for inclusion of <time.h>, versus <sys/time.h>
59  *  Taken verbatim from the autoconf manual.
60  */
61 #if TIME_WITH_SYS_TIME
62 # include <sys/time.h>
63 # include <time.h>
64 #else
65 # ifdef HAVE_SYS_TIME_H
66 #  include <sys/time.h>
67 # else
68 #  include <time.h>
69 # endif
70 #endif
71
72 #include <freeradius-devel/radius.h>
73 #include <freeradius-devel/token.h>
74 #include <freeradius-devel/hash.h>
75
76 #ifdef SIZEOF_UNSIGNED_INT
77 #if SIZEOF_UNSIGNED_INT != 4
78 #error FATAL: sizeof(unsigned int) != 4
79 #endif
80 #endif
81
82 /*
83  *  Include for modules.
84  */
85 #include <freeradius-devel/sha1.h>
86 #include <freeradius-devel/md4.h>
87
88 #define EAP_START               2
89
90 #define AUTH_VECTOR_LEN         16
91 #define CHAP_VALUE_LENGTH       16
92 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
93
94 #ifdef _LIBRADIUS
95 #  define AUTH_HDR_LEN          20
96 #  define VENDORPEC_USR         429
97 #define VENDORPEC_LUCENT        4846
98 #define VENDORPEC_STARENT       8164
99 #  define VENDOR(x)             ((x >> 16) & 0xffff)
100 #  define DEBUG                 if (librad_debug) printf
101 #  define debug_pair(vp)        do { if (librad_debug) { \
102                                         putchar('\t'); \
103                                         vp_print(stdout, vp); \
104                                         putchar('\n'); \
105                                      } \
106                                 } while(0)
107 #  define TAG_VALID(x)          ((x) > 0 && (x) < 0x20)
108 #  define TAG_VALID_ZERO(x)     ((x) < 0x20)
109 #  define TAG_ANY               -128   /* minimum signed char */
110 #endif
111
112 #if defined(__GNUC__)
113 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
114 # define NEVER_RETURNS __attribute__ ((noreturn))
115 # define UNUSED __attribute__ ((unused))
116 # define BLANK_FORMAT " "       /* GCC_LINT whines about empty formats */
117 #else
118 # define PRINTF_LIKE(n) /* ignore */
119 # define NEVER_RETURNS /* ignore */
120 # define UNUSED /* ignore */
121 # define BLANK_FORMAT ""
122 #endif
123
124 typedef struct attr_flags {
125         unsigned int            addport : 1;  /* add NAS-Port to IP address */
126         unsigned int            has_tag : 1;  /* tagged attribute */
127         unsigned int            do_xlat : 1;  /* strvalue is dynamic */
128         unsigned int            caseless : 1; /* case insensitive compares */
129
130         int8_t                  tag;          /* tag for tunneled attributes */
131         uint8_t                 encrypt;      /* encryption method */
132 } ATTR_FLAGS;
133
134 /*
135  *  Values of the encryption flags.
136  */
137 #define FLAG_ENCRYPT_NONE            (0)
138 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
139 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
140 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
141
142 typedef struct dict_attr {
143         char                    name[40];
144         int                     attr;
145         int                     type;
146         int                     vendor;
147         ATTR_FLAGS              flags;
148 } DICT_ATTR;
149
150 typedef struct dict_value {
151         int                     attr;
152         int                     value;
153         char                    name[1];
154 } DICT_VALUE;
155
156 typedef struct dict_vendor {
157         int                     vendorpec;
158         int                     type; /* length of type data */
159         int                     length; /* length of length data */
160         char                    name[1];
161 } DICT_VENDOR;
162
163 typedef struct value_pair {
164         char                    name[40];
165         int                     attribute;
166         int                     type;
167         int                     length; /* of data */
168         LRAD_TOKEN              operator;
169         uint32_t                lvalue; /* DELETE ME ASAP */
170         ATTR_FLAGS              flags;
171         struct value_pair       *next;
172         union {
173                 char                    strvalue[MAX_STRING_LEN];
174                 uint8_t                 octets[MAX_STRING_LEN];
175                 struct in_addr          ipaddr;
176                 struct in6_addr         ipv6addr;
177                 uint32_t                date;
178                 uint32_t                integer;
179                 uint8_t                 filter[32];
180                 uint8_t                 ifid[8]; /* struct? */
181                 uint8_t                 ipv6prefix[18]; /* struct? */
182         } data;
183 } VALUE_PAIR;
184 #define vp_strvalue   data.strvalue
185 #define vp_octets     data.octets
186 #define vp_ipaddr     lvalue
187 #define vp_ipv6addr   data.ipv6addr
188 #define vp_data       lvalue
189 #define vp_integer    lvalue
190 #define vp_ifid       data.ifid
191 #define vp_ipv6prefix data.ipv6prefix
192 #define vp_filter     data.filter
193
194
195 typedef struct lrad_ipaddr_t {
196         int             af;     /* address family */
197         union {
198                 struct in_addr  ip4addr;
199                 struct in6_addr ip6addr; /* maybe defined in missing.h */
200         } ipaddr;
201 } lrad_ipaddr_t;
202
203
204 /*
205  *      vector:         Request authenticator from access-request packet
206  *                      Put in there by rad_decode, and must be put in the
207  *                      response RADIUS_PACKET as well before calling rad_send
208  *
209  *      verified:       Filled in by rad_decode for accounting-request packets
210  *
211  *      data,data_len:  Used between rad_recv and rad_decode.
212  */
213 typedef struct radius_packet {
214         int                     sockfd;
215         lrad_ipaddr_t           src_ipaddr;
216         lrad_ipaddr_t           dst_ipaddr;
217         uint16_t                src_port;
218         uint16_t                dst_port;
219         int                     id;
220         unsigned int            code;
221         uint8_t                 vector[AUTH_VECTOR_LEN];
222         time_t                  timestamp;
223         int                     verified;
224         uint8_t                 *data;
225         int                     data_len;
226         VALUE_PAIR              *vps;
227 } RADIUS_PACKET;
228
229 /*
230  *      Printing functions.
231  */
232 void            librad_safeprint(char *in, int inlen, char *out, int outlen);
233 int     vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
234 int     vp_prints(char *out, int outlen, VALUE_PAIR *vp);
235 void            vp_print(FILE *, VALUE_PAIR *);
236 void            vp_printlist(FILE *, VALUE_PAIR *);
237 #define         fprint_attr_val vp_print
238
239 /*
240  *      Dictionary functions.
241  */
242 int             dict_addvendor(const char *name, int value);
243 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
244 int             dict_addvalue(const char *namestr, const char *attrstr, int value);
245 int             dict_init(const char *dir, const char *fn);
246 void            dict_free(void);
247 DICT_ATTR       *dict_attrbyvalue(int attr);
248 DICT_ATTR       *dict_attrbyname(const char *attr);
249 DICT_VALUE      *dict_valbyattr(int attr, int val);
250 DICT_VALUE      *dict_valbyname(int attr, const char *val);
251 int             dict_vendorbyname(const char *name);
252 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
253
254 #if 1 /* FIXME: compat */
255 #define dict_attrget    dict_attrbyvalue
256 #define dict_attrfind   dict_attrbyname
257 #define dict_valfind    dict_valbyname
258 /*#define dict_valget   dict_valbyattr almost but not quite*/
259 #endif
260
261 /* get around diffrent ctime_r styles */
262 #ifdef CTIMERSTYLE
263 #if CTIMERSTYLE == SOLARISSTYLE
264 #define CTIME_R(a,b,c) ctime_r(a,b,c)
265 #else
266 #define CTIME_R(a,b,c) ctime_r(a,b)
267 #endif
268 #else
269 #define CTIME_R(a,b,c) ctime_r(a,b)
270 #endif
271
272 /* md5.c */
273
274 void            librad_md5_calc(uint8_t *, const uint8_t *, unsigned int);
275
276 /* hmac.c */
277
278 void lrad_hmac_md5(const uint8_t *text, int text_len,
279                    const uint8_t *key, int key_len,
280                    unsigned char *digest);
281
282 /* hmacsha1.c */
283
284 void lrad_hmac_sha1(const uint8_t *text, int text_len,
285                     const uint8_t *key, int key_len,
286                     uint8_t *digest);
287
288 /* radius.c */
289 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
290 int             rad_packet_ok(RADIUS_PACKET *packet);
291 RADIUS_PACKET   *rad_recv(int fd);
292 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
293                            const char *secret);
294 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
295 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
296                            const char *secret);
297 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
298                          const char *secret);
299
300 RADIUS_PACKET   *rad_alloc(int newvector);
301 void            rad_free(RADIUS_PACKET **);
302 int             rad_pwencode(char *encpw, int *len, const char *secret,
303                              const uint8_t *vector);
304 int             rad_pwdecode(char *encpw, int len, const char *secret,
305                              const uint8_t *vector);
306 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret,
307                                     const uint8_t *vector);
308 int             rad_tunnel_pwdecode(uint8_t *encpw, int *len,
309                                     const char *secret, const uint8_t *vector);
310 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
311                                 int id, VALUE_PAIR *password);
312 VALUE_PAIR      *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
313                              const char *secret, int attribute, int length,
314                              const uint8_t *data);
315 int             rad_vp2attr(const RADIUS_PACKET *packet,
316                             const RADIUS_PACKET *original, const char *secret,
317                             const VALUE_PAIR *vp, uint8_t *ptr);
318
319 /* valuepair.c */
320 VALUE_PAIR      *paircreate(int attr, int type);
321 void            pairfree(VALUE_PAIR **);
322 void            pairbasicfree(VALUE_PAIR *pair);
323 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
324 void            pairdelete(VALUE_PAIR **, int);
325 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
326 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
327 int             paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
328 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
329 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
330 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
331 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
332 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
333 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
334 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
335 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
336 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
337
338 /*
339  *      Error functions.
340  */
341 #ifdef _LIBRADIUS
342 void            librad_log(const char *, ...)
343 #ifdef __GNUC__
344                 __attribute__ ((format (printf, 1, 2)))
345 #endif
346 ;
347 #endif
348 void            librad_perror(const char *, ...)
349 #ifdef __GNUC__
350                 __attribute__ ((format (printf, 1, 2)))
351 #endif
352 ;
353 extern char     librad_errstr[];
354 extern int      librad_dodns;   /* 0 = no dns lookups */
355 extern int      librad_debug;   /* 0 = no debugging information */
356 extern int      librad_max_attributes; /* per incoming packet */
357
358 /*
359  *      Several handy miscellaneous functions.
360  */
361 const char *    ip_ntoa(char *, uint32_t);
362 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
363 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
364 char            *strNcpy(char *dest, const char *src, int n);
365 int             rad_lockfd(int fd, int lock_len);
366 int             rad_lockfd_nonblock(int fd, int lock_len);
367 int             rad_unlockfd(int fd, int lock_len);
368 void            lrad_bin2hex(const uint8_t *bin, char *hex, int len);
369 int             lrad_hex2bin(const char *hex, uint8_t *bin, int len);
370 #ifndef HAVE_INET_PTON
371 int             inet_pton(int af, const char *src, void *dst);
372 #endif
373 #ifndef HAVE_INET_NTOP
374 const char      *inet_ntop(int af, const void *src, char *dst, size_t cnt);
375 #endif
376 #ifndef HAVE_CLOSEFROM
377 int             closefrom(int fd);
378 #endif
379
380 int             ip_hton(const char *src, int af, lrad_ipaddr_t *dst);
381 const char      *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt);
382
383
384 #ifdef ASCEND_BINARY
385 /* filters.c */
386 int             ascend_parse_filter(VALUE_PAIR *pair);
387 void            print_abinary(VALUE_PAIR *vp, char *buffer, int len);
388 #endif /*ASCEND_BINARY*/
389
390 /* random numbers in isaac.c */
391 /* context of random number generator */
392 typedef struct lrad_randctx {
393   uint32_t randcnt;
394   uint32_t randrsl[256];
395   uint32_t randmem[256];
396   uint32_t randa;
397   uint32_t randb;
398   uint32_t randc;
399 } lrad_randctx;
400
401 void lrad_isaac(lrad_randctx *ctx);
402 void lrad_randinit(lrad_randctx *ctx, int flag);
403 uint32_t lrad_rand(void);       /* like rand(), but better. */
404 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
405
406
407 /* crypt wrapper from crypt.c */
408 int lrad_crypt_check(const char *key, const char *salt);
409
410 /* rbtree.c */
411 typedef struct rbtree_t rbtree_t;
412 typedef struct rbnode_t rbnode_t;
413
414 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
415                                void (*freeNode)(void *),
416                                int replace_flag);
417 void            rbtree_free(rbtree_t *tree);
418 int             rbtree_insert(rbtree_t *tree, void *Data);
419 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
420 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
421 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
422 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
423 int             rbtree_num_elements(rbtree_t *tree);
424 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
425
426 /* callback order for walking  */
427 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
428
429 /*
430  *      The callback should be declared as:
431  *      int callback(void *context, void *data)
432  *
433  *      The "context" is some user-defined context.
434  *      The "data" is the pointer to the user data in the node,
435  *        NOT the node itself.
436  *
437  *      It should return 0 if all is OK, and !0 for any error.
438  *      The walking will stop on any error.
439  */
440 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
441
442 /*
443  *      FIFOs
444  */
445 typedef struct lrad_fifo_t lrad_fifo_t;
446 typedef void (*lrad_fifo_free_t)(void *);
447 lrad_fifo_t *lrad_fifo_create(int max_entries, lrad_fifo_free_t freeNode);
448 void lrad_fifo_free(lrad_fifo_t *fi);
449 int lrad_fifo_push(lrad_fifo_t *fi, void *data);
450 void *lrad_fifo_pop(lrad_fifo_t *fi);
451
452 #include <freeradius-devel/packet.h>
453
454 #endif /*LIBRADIUS_H*/