import from 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 #  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 DICT_ATTR       *dict_attrbyvalue(int attr);
192 DICT_ATTR       *dict_attrbyname(const char *attr);
193 DICT_VALUE      *dict_valbyattr(int attr, int val);
194 DICT_VALUE      *dict_valbyname(int attr, const char *val);
195 int             dict_vendorbyname(const char *name);
196 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
197
198 /*
199  *  Compatibility
200  */
201 #define dict_vendorcode
202 #define dict_vendorpec
203
204
205 #if 1 /* FIXME: compat */
206 #define dict_attrget    dict_attrbyvalue
207 #define dict_attrfind   dict_attrbyname
208 #define dict_valfind    dict_valbyname
209 /*#define dict_valget   dict_valbyattr almost but not quite*/
210 #endif
211
212 /* get around diffrent ctime_r styles */
213 #ifdef CTIMERSTYLE
214 #if CTIMERSTYLE == SOLARISSTYLE
215 #define CTIME_R(a,b,c) ctime_r(a,b,c)
216 #else
217 #define CTIME_R(a,b,c) ctime_r(a,b)
218 #endif
219 #else
220 #define CTIME_R(a,b,c) ctime_r(a,b)
221 #endif
222
223 /* md5.c */
224
225 void            librad_md5_calc(u_char *, u_char *, u_int);
226
227 /* hmac.c */
228
229 void lrad_hmac_md5(const unsigned char *text, int text_len,
230                    const unsigned char *key, int key_len,
231                    unsigned char *digest);
232
233 /* hmacsha1.c */
234
235 void lrad_hmac_sha1(const unsigned char *text, int text_len,
236                     const unsigned char *key, int key_len,
237                     unsigned char *digest);
238
239 /* radius.c */
240 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
241 RADIUS_PACKET   *rad_recv(int fd);
242 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
243 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
244 RADIUS_PACKET   *rad_alloc(int newvector);
245 void            rad_free(RADIUS_PACKET **);
246 int             rad_pwencode(char *encpw, int *len, const char *secret, const char *vector);
247 int             rad_pwdecode(char *encpw, int len, const char *secret, const char *vector);
248 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret, const char *vector);
249 int             rad_tunnel_pwdecode(uint8_t *encpw, int *len, const char *secret, const char *vector);
250 int             rad_chap_encode(RADIUS_PACKET *packet, char *output, int id, VALUE_PAIR *password);
251 int             rad_vp2attr(const RADIUS_PACKET *packet,
252                             const RADIUS_PACKET *original, const char *secret,
253                             const VALUE_PAIR *vp, uint8_t *ptr);
254 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
255                            const char *secret);
256 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
257                          const char *secret);
258
259 /* valuepair.c */
260 VALUE_PAIR      *paircreate(int attr, int type);
261 void            pairfree(VALUE_PAIR **);
262 void            pairbasicfree(VALUE_PAIR *pair);
263 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
264 void            pairdelete(VALUE_PAIR **, int);
265 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
266 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
267 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
268 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
269 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
270 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
271 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
272 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
273 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
274 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
275 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
276
277 /*
278  *      Error functions.
279  */
280 #ifdef _LIBRADIUS
281 void            librad_log(const char *, ...)
282 #ifdef __GNUC__
283                 __attribute__ ((format (printf, 1, 2)))
284 #endif
285 ;
286 #endif
287 void            librad_perror(const char *, ...)
288 #ifdef __GNUC__
289                 __attribute__ ((format (printf, 1, 2)))
290 #endif
291 ;
292 extern char     librad_errstr[];
293 extern int      librad_dodns;   /* 0 = no dns lookups */
294 extern int      librad_debug;   /* 0 = no debugging information */
295 extern int      librad_max_attributes; /* per incoming packet */
296
297 /*
298  *      Several handy miscellaneous functions.
299  */
300 char *          ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
301 uint32_t        ip_getaddr (const char *);
302 char *          ip_ntoa(char *, uint32_t);
303 uint32_t        ip_addr(const char *);
304 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
305 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
306 const char      *ipv6_ntoa(char *buffer, size_t size, void *ip6addr);
307 int             ipv6_addr(const char *ip6_str, void *ip6addr);
308 char            *strNcpy(char *dest, const char *src, int n);
309 void            rad_lowercase(char *str);
310 void            rad_rmspace(char *str);
311 int             rad_lockfd(int fd, int lock_len);
312 int             rad_lockfd_nonblock(int fd, int lock_len);
313 int             rad_unlockfd(int fd, int lock_len);
314 void            lrad_bin2hex(const uint8_t *bin, char *hex, int len);
315 int             lrad_hex2bin(const char *hex, uint8_t *bin, int len);
316 #ifndef HAVE_CLOSEFROM
317 int             closefrom(int fd);
318 #endif
319
320 #ifdef ASCEND_BINARY
321 /* filters.c */
322 int             ascend_parse_filter(VALUE_PAIR *pair);
323 void            print_abinary(VALUE_PAIR *vp, u_char *buffer, int len);
324 #endif /*ASCEND_BINARY*/
325
326 /* snprintf.c */
327 #ifndef HAVE_VSNPRINTF
328 #include <stdarg.h>
329 int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
330 #endif
331 #ifndef HAVE_SNPRINTF
332 int snprintf(char *str, size_t count, const char *fmt, ...);
333 #endif
334
335 /* random numbers in isaac.c */
336 /* context of random number generator */
337 typedef struct lrad_randctx {
338   uint32_t randcnt;
339   uint32_t randrsl[256];
340   uint32_t randmem[256];
341   uint32_t randa;
342   uint32_t randb;
343   uint32_t randc;
344 } lrad_randctx;
345
346 void lrad_isaac(lrad_randctx *ctx);
347 void lrad_randinit(lrad_randctx *ctx, int flag);
348 uint32_t lrad_rand(void);       /* like rand(), but better. */
349 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
350
351 /* crypt wrapper from crypt.c */
352 int lrad_crypt_check(const char *key, const char *salt);
353
354 /* rbtree.c */
355 typedef struct rbtree_t rbtree_t;
356 typedef struct rbnode_t rbnode_t;
357
358 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
359                                void (*freeNode)(void *),
360                                int replace_flag);
361 void            rbtree_free(rbtree_t *tree);
362 int             rbtree_insert(rbtree_t *tree, void *Data);
363 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
364 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
365 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
366 int             rbtree_num_elements(rbtree_t *tree);
367 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
368 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
369
370 /* callback order for walking  */
371 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
372 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
373
374 /*
375  *      Fast hash, which isn't too bad.  Don't use for cryptography,
376  *      just for hashing internal data.
377  */
378 uint32_t lrad_hash(const void *, size_t);
379 uint32_t lrad_hash_update(const void *data, size_t size, uint32_t hash);
380 uint32_t lrad_hash_string(const char *p);
381
382 /*
383  *      If you need fewer than 32-bits of hash, use this macro to get
384  *      the number of bits in the hash you need.  The upper bits of the
385  *      hash will be set to zero.
386  */
387 uint32_t lrad_hash_fold(uint32_t hash, int bits);
388
389 typedef struct lrad_hash_table_t lrad_hash_table_t;
390 typedef void (*lrad_hash_table_free_t)(void *);
391 typedef uint32_t (*lrad_hash_table_hash_t)(const void *);
392 typedef int (*lrad_hash_table_cmp_t)(const void *, const void *);
393 typedef int (*lrad_hash_table_walk_t)(void * /* ctx */, void * /* data */);
394
395 lrad_hash_table_t *lrad_hash_table_create(lrad_hash_table_hash_t hashNode,
396                                           lrad_hash_table_cmp_t cmpNode,
397                                           lrad_hash_table_free_t freeNode);
398 void            lrad_hash_table_free(lrad_hash_table_t *ht);
399 int             lrad_hash_table_insert(lrad_hash_table_t *ht, void *data);
400 int             lrad_hash_table_delete(lrad_hash_table_t *ht, const void *data);
401 void            *lrad_hash_table_yank(lrad_hash_table_t *ht, const void *data);
402 int             lrad_hash_table_replace(lrad_hash_table_t *ht, void *data);
403 void            *lrad_hash_table_finddata(lrad_hash_table_t *ht, const void *data);
404 int             lrad_hash_table_num_elements(lrad_hash_table_t *ht);
405 int             lrad_hash_table_walk(lrad_hash_table_t *ht,
406                                      lrad_hash_table_walk_t callback,
407                                      void *ctx);
408 #endif /*LIBRADIUS_H*/