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