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