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