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 #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             len_disp;       /* length displacement */
102         char                    do_xlat;
103         signed char             tag;
104         uint8_t                 encrypt;        /* encryption method */
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_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
242 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
243 RADIUS_PACKET   *rad_alloc(int newvector);
244 void            rad_free(RADIUS_PACKET **);
245 int             rad_pwencode(char *encpw, int *len, const char *secret, const char *vector);
246 int             rad_pwdecode(char *encpw, int len, const char *secret, const char *vector);
247 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret, const char *vector);
248 int             rad_tunnel_pwdecode(uint8_t *encpw, int *len, const char *secret, const char *vector);
249 int             rad_chap_encode(RADIUS_PACKET *packet, char *output, int id, VALUE_PAIR *password);
250 int             rad_vp2attr(const RADIUS_PACKET *packet,
251                             const RADIUS_PACKET *original, const char *secret,
252                             const VALUE_PAIR *vp, uint8_t *ptr);
253 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
254                            const char *secret);
255 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
256                          const char *secret);
257
258 /* valuepair.c */
259 VALUE_PAIR      *paircreate(int attr, int type);
260 void            pairfree(VALUE_PAIR **);
261 void            pairbasicfree(VALUE_PAIR *pair);
262 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
263 void            pairdelete(VALUE_PAIR **, int);
264 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
265 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
266 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
267 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
268 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
269 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
270 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
271 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
272 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
273 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
274 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
275
276 /*
277  *      Error functions.
278  */
279 #ifdef _LIBRADIUS
280 void            librad_log(const char *, ...)
281 #ifdef __GNUC__
282                 __attribute__ ((format (printf, 1, 2)))
283 #endif
284 ;
285 #endif
286 void            librad_perror(const char *, ...)
287 #ifdef __GNUC__
288                 __attribute__ ((format (printf, 1, 2)))
289 #endif
290 ;
291 extern char     librad_errstr[];
292 extern int      librad_dodns;   /* 0 = no dns lookups */
293 extern int      librad_debug;   /* 0 = no debugging information */
294 extern int      librad_max_attributes; /* per incoming packet */
295
296 /*
297  *      Several handy miscellaneous functions.
298  */
299 char *          ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
300 uint32_t        ip_getaddr (const char *);
301 char *          ip_ntoa(char *, uint32_t);
302 uint32_t        ip_addr(const char *);
303 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
304 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
305 const char      *ipv6_ntoa(char *buffer, size_t size, void *ip6addr);
306 int             ipv6_addr(const char *ip6_str, void *ip6addr);
307 char            *strNcpy(char *dest, const char *src, int n);
308 void            rad_lowercase(char *str);
309 void            rad_rmspace(char *str);
310 int             rad_lockfd(int fd, int lock_len);
311 int             rad_lockfd_nonblock(int fd, int lock_len);
312 int             rad_unlockfd(int fd, int lock_len);
313 void            lrad_bin2hex(const uint8_t *bin, char *hex, int len);
314 int             lrad_hex2bin(const char *hex, uint8_t *bin, int len);
315 #ifndef HAVE_CLOSEFROM
316 int             closefrom(int fd);
317 #endif
318
319 #ifdef ASCEND_BINARY
320 /* filters.c */
321 int             ascend_parse_filter(VALUE_PAIR *pair);
322 void            print_abinary(VALUE_PAIR *vp, u_char *buffer, int len);
323 #endif /*ASCEND_BINARY*/
324
325 /* snprintf.c */
326 #ifndef HAVE_VSNPRINTF
327 #include <stdarg.h>
328 int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
329 #endif
330 #ifndef HAVE_SNPRINTF
331 int snprintf(char *str, size_t count, const char *fmt, ...);
332 #endif
333
334 /* random numbers in isaac.c */
335 /* context of random number generator */
336 typedef struct lrad_randctx {
337   uint32_t randcnt;
338   uint32_t randrsl[256];
339   uint32_t randmem[256];
340   uint32_t randa;
341   uint32_t randb;
342   uint32_t randc;
343 } lrad_randctx;
344
345 void lrad_isaac(lrad_randctx *ctx);
346 void lrad_randinit(lrad_randctx *ctx, int flag);
347 uint32_t lrad_rand(void);       /* like rand(), but better. */
348 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
349
350 /* crypt wrapper from crypt.c */
351 int lrad_crypt_check(const char *key, const char *salt);
352
353 /* rbtree.c */
354 typedef struct rbtree_t rbtree_t;
355 typedef struct rbnode_t rbnode_t;
356
357 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
358                                void (*freeNode)(void *),
359                                int replace_flag);
360 void            rbtree_free(rbtree_t *tree);
361 int             rbtree_insert(rbtree_t *tree, void *Data);
362 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
363 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
364 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
365 int             rbtree_num_elements(rbtree_t *tree);
366 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
367 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
368
369 /* callback order for walking  */
370 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
371 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
372
373 /*
374  *      Fast hash, which isn't too bad.  Don't use for cryptography,
375  *      just for hashing internal data.
376  */
377 uint32_t lrad_hash(const void *, size_t);
378 uint32_t lrad_hash_update(const void *data, size_t size, uint32_t hash);
379 uint32_t lrad_hash_string(const char *p);
380
381 /*
382  *      If you need fewer than 32-bits of hash, use this macro to get
383  *      the number of bits in the hash you need.  The upper bits of the
384  *      hash will be set to zero.
385  */
386 uint32_t lrad_hash_fold(uint32_t hash, int bits);
387
388 typedef struct lrad_hash_table_t lrad_hash_table_t;
389 typedef void (*lrad_hash_table_free_t)(void *);
390 typedef int (*lrad_hash_table_walk_t)(void * /* ctx */, void * /* data */);
391
392 lrad_hash_table_t *lrad_hash_table_create(lrad_hash_table_free_t freeNode);
393 void            lrad_hash_table_free(lrad_hash_table_t *ht);
394 int             lrad_hash_table_insert(lrad_hash_table_t *ht, uint32_t key, void *data);
395 int             lrad_hash_table_delete(lrad_hash_table_t *ht, uint32_t key);
396 void            *lrad_hash_table_yank(lrad_hash_table_t *ht, uint32_t key);
397 int             lrad_hash_table_replace(lrad_hash_table_t *ht, uint32_t key, void *data);
398 void            *lrad_hash_table_finddata(lrad_hash_table_t *ht, uint32_t key);
399 int             lrad_hash_table_num_elements(lrad_hash_table_t *ht);
400 int             lrad_hash_table_walk(lrad_hash_table_t *ht,
401                                      lrad_hash_table_walk_t callback,
402                                      void *ctx);
403 #endif /*LIBRADIUS_H*/