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