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