Added copy packet functionality
[freeradius.git] / src / include / libradius.h
1 /*
2  *   This library is free software; you can redistribute it and/or
3  *   modify it under the terms of the GNU Lesser General Public
4  *   License as published by the Free Software Foundation; either
5  *   version 2.1 of the License, or (at your option) any later version.
6  *
7  *   This library is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  *   Lesser General Public License for more details.
11  *
12  *   You should have received a copy of the GNU Lesser General Public
13  *   License along with this library; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef LIBRADIUS_H
17 #define LIBRADIUS_H
18 /*
19  * $Id$
20  *
21  * @file libradius.h
22  * @brief Structures and prototypes for the radius library.
23  *
24  * @copyright 1999-2014 The FreeRADIUS server project
25  */
26 RCSIDH(libradius_h, "$Id$")
27
28 /*
29  *  Preprocessor hacks.
30  */
31 #ifndef STRINGIFY
32 #  define XSTRINGIFY(x) #x
33 #  define STRINGIFY(x) XSTRINGIFY(x)
34 #endif
35
36 #ifndef HEXIFY
37 #  define XHEXIFY4(b1,b2,b3,b4) (0x ## b1 ## b2 ## b3 ## b4)
38 #  define HEXIFY4(b1,b2,b3,b4)  XHEXIFY4(b1, b2, b3, b4)
39
40 #  define XHEXIFY3(b1,b2,b3)    (0x ## b1 ## b2 ## b3)
41 #  define HEXIFY3(b1,b2,b3)     XHEXIFY3(b1, b2, b3)
42
43 #  define XHEXIFY2(b1,b2)       (0x ## b1 ## b2)
44 #  define HEXIFY2(b1,b2)        XHEXIFY2(b1, b2)
45
46 #  define XHEXIFY(b1)           (0x ## b1)
47 #  define HEXIFY(b1)            XHEXIFY(b1)
48 #endif
49
50 /*
51  *  Let any external program building against the library know what
52  *  features the library was built with.
53  */
54 #include <freeradius-devel/features.h>
55
56 #ifdef WITHOUT_VERSION_CHECK
57 #  define RADIUSD_MAGIC_NUMBER  ((uint64_t) (0xf4ee4ad3f4ee4ad3))
58 #  define MAGIC_PREFIX(_x)      ((uint8_t) 0x00)
59 #  define MAGIC_VERSION(_x)     ((uint32_t) 0x00000000)
60 #  define MAGIC_COMMIT(_x)      ((uint32_t) 0x00000000)
61 #else
62 #  ifdef RADIUSD_VERSION_COMMIT
63 #    define RADIUSD_MAGIC_NUMBER ((uint64_t) HEXIFY4(f4, RADIUSD_VERSION, RADIUSD_VERSION_COMMIT, 0))
64 #  else
65 #    define RADIUSD_MAGIC_NUMBER ((uint64_t) HEXIFY3(f4, RADIUSD_VERSION, 00000000))
66 #  endif
67 #  define MAGIC_PREFIX(_x)      ((uint8_t) (_x >> 56))
68 #  define MAGIC_VERSION(_x)     ((uint32_t) ((_x >> 32) & 0x00ffffff))
69 #  define MAGIC_COMMIT(_x)      ((uint32_t) (_x & 0xffffffff))
70 #endif
71
72 /*
73  *  Talloc memory allocation is used in preference to malloc throughout
74  *  the libraries and server.
75  */
76 #include <talloc.h>
77
78 /*
79  *  Defines signatures for any missing functions.
80  */
81 #include <freeradius-devel/missing.h>
82
83 /*
84  *  Include system headers.
85  */
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <stdarg.h>
89 #include <stdbool.h>
90 #include <signal.h>
91
92 #include <freeradius-devel/threads.h>
93 #include <freeradius-devel/radius.h>
94 #include <freeradius-devel/token.h>
95 #include <freeradius-devel/hash.h>
96
97 #ifdef SIZEOF_UNSIGNED_INT
98 #  if SIZEOF_UNSIGNED_INT != 4
99 #    error FATAL: sizeof(unsigned int) != 4
100 #  endif
101 #endif
102
103 /*
104  *  Include for modules.
105  */
106 #include <freeradius-devel/sha1.h>
107 #include <freeradius-devel/md4.h>
108
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
112
113 #if defined(WITH_VERIFY_PTR)
114 #  define FREE_MAGIC (0xF4EEF4EE)
115
116 /*
117  * @FIXME
118  *  Add if (_x->da) (void) talloc_get_type_abort(_x->da, DICT_ATTR);
119  *  to the macro below when dictionaries are talloced.
120  */
121 #  define VERIFY_VP(_x) \
122 do {\
123         (void) talloc_get_type_abort(_x, VALUE_PAIR);\
124         if (_x->data.ptr) switch (_x->da->type) {\
125         case PW_TYPE_OCTETS:\
126         case PW_TYPE_TLV:\
127                 if (!talloc_get_type(_x->data.ptr, uint8_t)) {\
128                         fr_perror("Type check failed for attribute \"%s\"", _x->da->name);\
129                         (void) talloc_get_type_abort(_x->data.ptr, uint8_t);\
130                 }\
131                 break;\
132         case PW_TYPE_STRING:\
133                 if (!talloc_get_type(_x->data.ptr, char)) {\
134                         fr_perror("Type check failed for attribute \"%s\"", _x->da->name);\
135                         (void) talloc_get_type_abort(_x->data.ptr, char);\
136                 }\
137                 break;\
138         default:\
139                 break;\
140         }\
141 } while (0)\
142
143 #  define VERIFY_PACKET(_x) (void) talloc_get_type_abort(_x, RADIUS_PACKET)
144 #else
145 #  define VERIFY_VP(_x)
146 #  define VERIFY_PACKET(_x)
147 #endif
148
149 #define AUTH_VECTOR_LEN         16
150 #define CHAP_VALUE_LENGTH       16
151 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
152 #define FR_MAX_VENDOR           (1 << 24) /* RFC limitations */
153
154 #ifdef _LIBRADIUS
155 #  define AUTH_HDR_LEN          20
156 #  define VENDORPEC_USR         429
157 #define VENDORPEC_LUCENT        4846
158 #define VENDORPEC_STARENT       8164
159 #  define DEBUG                 if (fr_debug_flag && fr_log_fp) fr_printf_log
160 #  define debug_pair(vp)        do { if (fr_debug_flag && fr_log_fp) { \
161                                         vp_print(fr_log_fp, vp); \
162                                      } \
163                                 } while(0)
164 #endif
165
166 #define TAG_VALID(x)            ((x) > 0 && (x) < 0x20)
167 #define TAG_VALID_ZERO(x)       ((x) < 0x20)
168 #define TAG_ANY                 -128    /* minimum signed char */
169 #define TAG_UNUSED              0
170
171 #define PAD(_x, _y)             (_y - ((_x) % _y))
172
173 #if defined(__GNUC__)
174 #  define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
175 #  define NEVER_RETURNS __attribute__ ((noreturn))
176 #  define UNUSED __attribute__ ((unused))
177 #  define BLANK_FORMAT " "      /* GCC_LINT whines about empty formats */
178 #else
179 #  define PRINTF_LIKE(n)        /* ignore */
180 #  define NEVER_RETURNS /* ignore */
181 #  define UNUSED /* ignore */
182 #  define BLANK_FORMAT ""
183 #endif
184
185 typedef struct attr_flags {
186         unsigned int    is_unknown : 1;                         //!< Attribute number or vendor is unknown.
187         unsigned int    is_tlv : 1;                             //!< Is a sub attribute.
188         unsigned int    vp_free : 1;                            //!< Should be freed when VALUE_PAIR is freed.
189
190         unsigned int    has_tag : 1;                            //!< Tagged attribute.
191         unsigned int    array : 1;                              //!< Pack multiples into 1 attr.
192         unsigned int    has_value : 1;                          //!< Has a value.
193         unsigned int    has_value_alias : 1;                    //!< Has a value alias.
194         unsigned int    has_tlv : 1;                            //!< Has sub attributes.
195
196         unsigned int    extended : 1;                           //!< Extended attribute.
197         unsigned int    long_extended : 1;                      //!< Long format.
198         unsigned int    evs : 1;                                //!< Extended VSA.
199         unsigned int    wimax: 1;                               //!< WiMAX format=1,1,c.
200
201         unsigned int    concat : 1;                             //!< concatenate multiple instances
202         unsigned int    is_pointer : 1;                         //!< data is a pointer
203
204         uint8_t         encrypt;                                //!< Ecryption method.
205         uint8_t         length;
206 } ATTR_FLAGS;
207
208 /*
209  *  Values of the encryption flags.
210  */
211 #define FLAG_ENCRYPT_NONE           (0)
212 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
213 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
214 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
215
216 extern const FR_NAME_NUMBER dict_attr_types[];
217 extern const size_t dict_attr_sizes[PW_TYPE_MAX][2];
218
219 /** dictionary attribute
220  *
221  */
222 typedef struct dict_attr {
223         unsigned int            attr;
224         PW_TYPE                 type;
225         unsigned int            vendor;
226         ATTR_FLAGS              flags;
227         char                    name[1];
228 } DICT_ATTR;
229
230 /** value of an enumerated attribute
231  *
232  */
233 typedef struct dict_value {
234         unsigned int            attr;
235         unsigned int            vendor;
236         int                     value;
237         char                    name[1];
238 } DICT_VALUE;
239
240 /** dictionary vendor
241  *
242  */
243 typedef struct dict_vendor {
244         unsigned int            vendorpec;
245         size_t                  type;                           //!< Length of type data
246         size_t                  length;                         //!< Length of length data
247         size_t                  flags;
248         char                    name[1];
249 } DICT_VENDOR;
250
251 /** Union containing all data types supported by the server
252  *
253  * This union contains all data types that can be represented with VALUE_PAIRs. It may also be used in other parts
254  * of the server where values of different types need to be stored.
255  *
256  * PW_TYPE should be an enumeration of the values in this union.
257  */
258 typedef union value_data {
259         char const              *strvalue;                      //!< Pointer to UTF-8 string.
260         uint8_t const           *octets;                        //!< Pointer to binary string.
261         uint32_t                integer;                        //!< 32bit unsigned integer.
262         struct in_addr          ipaddr;                         //!< IPv4 Address.
263         uint32_t                date;                           //!< Date (32bit Unix timestamp).
264         size_t                  filter[32/sizeof(size_t)];      //!< Ascend binary format a packed data
265                                                                 //!< structure.
266
267         uint8_t                 ifid[8]; /* struct? */          //!< IPv6 interface ID.
268         struct in6_addr         ipv6addr;                       //!< IPv6 Address.
269         uint8_t                 ipv6prefix[18]; /* struct? */   //!< IPv6 prefix.
270
271         uint8_t                 byte;                           //!< 8bit unsigned integer.
272         uint16_t                ushort;                         //!< 16bit unsigned integer.
273
274         uint8_t                 ether[6];                       //!< Ethernet (MAC) address.
275
276         int32_t                 sinteger;                       //!< 32bit signed integer.
277         uint64_t                integer64;                      //!< 64bit unsigned integer.
278
279         uint8_t                 ipv4prefix[6]; /* struct? */    //!< IPv4 prefix.
280
281         uint8_t                 *tlv;                           //!< Nested TLV (should go away).
282         void const              *ptr;                           //!< generic pointer.
283 } value_data_t;
284
285 /** The type of value a VALUE_PAIR contains
286  *
287  * This is used to add structure to nested VALUE_PAIRs and specifies what type of node it is (set, list, data).
288  *
289  * xlat is another type of data node which must first be expanded before use.
290  */
291 typedef enum value_type {
292         VT_NONE = 0,                                            //!< VALUE_PAIR has no value.
293         VT_SET,                                                 //!< VALUE_PAIR has children.
294         VT_LIST,                                                //!< VALUE_PAIR has multiple values.
295         VT_DATA,                                                //!< VALUE_PAIR has a single value.
296         VT_XLAT                                                 //!< valuepair value must be xlat expanded when it's
297                                                                 //!< added to VALUE_PAIR tree.
298 } value_type_t;
299
300 /** Stores an attribute, a value and various bits of other data
301  *
302  * VALUE_PAIRs are the main data structure used in the server, they specify an attribute, it's children and
303  * it's siblings.
304  *
305  * They also specify what behaviour should be used when the attribute is merged into a new list/tree.
306  */
307 typedef struct value_pair {
308         DICT_ATTR const         *da;                            //!< Dictionary attribute defines the attribute
309                                                                 //!< number, vendor and type of the attribute.
310
311         struct value_pair       *next;
312
313         FR_TOKEN                op;                             //!< Operator to use when moving or inserting
314                                                                 //!< valuepair into a list.
315
316         int8_t                  tag;                            //!< Tag value used to group valuepairs.
317
318         union {
319         //      VALUE_SET       *set;                           //!< Set of child attributes.
320         //      VALUE_LIST      *list;                          //!< List of values for
321                                                                 //!< multivalued attribute.
322         //      value_data_t    *data;                          //!< Value data for this attribute.
323
324                 char const      *xlat;                          //!< Source string for xlat expansion.
325         } value;
326
327         value_type_t            type;                           //!< Type of pointer in value union.
328
329         size_t                  length;                         //!< of Data field.
330         value_data_t            data;
331 } VALUE_PAIR;
332
333 /** Abstraction to allow iterating over different configurations of VALUE_PAIRs
334  *
335  * This allows functions which do not care about the structure of collections of VALUE_PAIRs
336  * to iterate over all members in a collection.
337  *
338  * Field within a vp_cursor should not be accessed directly, and vp_cursors should only be
339  * manipulated with the pair* functions.
340  */
341 typedef struct vp_cursor {
342         VALUE_PAIR      **first;
343         VALUE_PAIR      *found;                                 //!< pairfind marker.
344         VALUE_PAIR      *last;                                  //!< Temporary only used for fr_cursor_insert
345         VALUE_PAIR      *current;                               //!< The current attribute.
346         VALUE_PAIR      *next;                                  //!< Next attribute to process.
347 } vp_cursor_t;
348
349 /** A VALUE_PAIR in string format.
350  *
351  * Used to represent pairs in the legacy 'users' file format.
352  */
353 typedef struct value_pair_raw {
354         char l_opand[256];                                      //!< Left hand side of the pair.
355         char r_opand[1024];                                     //!< Right hand side of the pair.
356
357         FR_TOKEN quote;                                         //!< Type of quoting around the r_opand.
358
359         FR_TOKEN op;                                            //!< Operator.
360 } VALUE_PAIR_RAW;
361
362 #define vp_strvalue     data.strvalue
363 #define vp_integer      data.integer
364 #define vp_ipaddr       data.ipaddr.s_addr
365 #define vp_date         data.date
366 #define vp_filter       data.filter
367 #define vp_octets       data.octets
368 #define vp_ifid         data.ifid
369 #define vp_ipv6addr     data.ipv6addr
370 #define vp_ipv6prefix   data.ipv6prefix
371 #define vp_byte         data.byte
372 #define vp_short        data.ushort
373 #define vp_ether        data.ether
374 #define vp_signed       data.sinteger
375 #define vp_integer64    data.integer64
376 #define vp_ipv4prefix   data.ipv4prefix
377 #define vp_tlv          data.tlv
378
379 typedef struct fr_ipaddr_t {
380         int             af;     /* address family */
381         union {
382                 struct in_addr  ip4addr;
383                 struct in6_addr ip6addr; /* maybe defined in missing.h */
384         } ipaddr;
385         uint32_t        scope;  /* for IPv6 */
386 } fr_ipaddr_t;
387
388 /*
389  *      vector:         Request authenticator from access-request packet
390  *                      Put in there by rad_decode, and must be put in the
391  *                      response RADIUS_PACKET as well before calling rad_send
392  *
393  *      verified:       Filled in by rad_decode for accounting-request packets
394  *
395  *      data,data_len:  Used between rad_recv and rad_decode.
396  */
397 typedef struct radius_packet {
398         int                     sockfd;
399         fr_ipaddr_t             src_ipaddr;
400         fr_ipaddr_t             dst_ipaddr;
401         uint16_t                src_port;
402         uint16_t                dst_port;
403         int                     id;
404         unsigned int            code;
405         uint8_t                 vector[AUTH_VECTOR_LEN];
406         struct timeval          timestamp;
407         uint8_t                 *data;
408         size_t                  data_len;
409         VALUE_PAIR              *vps;
410         ssize_t                 offset;
411 #ifdef WITH_TCP
412         size_t                  partial;
413         int                     proto;
414 #endif
415 } RADIUS_PACKET;
416
417 typedef enum {
418         DECODE_FAIL_NONE = 0,
419         DECODE_FAIL_MIN_LENGTH_PACKET,
420         DECODE_FAIL_MIN_LENGTH_FIELD,
421         DECODE_FAIL_MIN_LENGTH_MISMATCH,
422         DECODE_FAIL_HEADER_OVERFLOW,
423         DECODE_FAIL_UNKNOWN_PACKET_CODE,
424         DECODE_FAIL_INVALID_ATTRIBUTE,
425         DECODE_FAIL_ATTRIBUTE_TOO_SHORT,
426         DECODE_FAIL_ATTRIBUTE_OVERFLOW,
427         DECODE_FAIL_MA_INVALID_LENGTH,
428         DECODE_FAIL_ATTRIBUTE_UNDERFLOW,
429         DECODE_FAIL_TOO_MANY_ATTRIBUTES,
430         DECODE_FAIL_MA_MISSING,
431         DECODE_FAIL_MAX
432 } decode_fail_t;
433
434 /*
435  *      Version check.
436  */
437 int             fr_check_lib_magic(uint64_t magic);
438
439 /*
440  *      Printing functions.
441  */
442 int             fr_utf8_char(uint8_t const *str);
443 size_t          fr_print_string(char const *in, size_t inlen,
444                                  char *out, size_t outlen);
445 size_t          fr_print_string_len(char const *in, size_t inlen);
446
447 #define         is_truncated(_ret, _max) ((_ret) >= (_max))
448 #define         truncate_len(_ret, _max) (((_ret) >= (_max)) ? ((_max) - 1) : _ret)
449 size_t          vp_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
450 char            *vp_aprinttype(TALLOC_CTX *ctx, PW_TYPE type);
451 char            *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
452 size_t          vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
453 size_t          vp_print_name(char *out, size_t outlen, unsigned int attr, unsigned int vendor);
454 size_t          vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
455 void            vp_print(FILE *, VALUE_PAIR const *);
456 void            vp_printlist(FILE *, VALUE_PAIR const *);
457 #define         fprint_attr_val vp_print
458
459 /*
460  *      Dictionary functions.
461  */
462 extern const int dict_attr_allowed_chars[256];
463 int             str2argv(char *str, char **argv, int max_argc);
464 int             dict_str2oid(char const *ptr, unsigned int *pattr,
465                              unsigned int *pvendor, int tlv_depth);
466 int             dict_addvendor(char const *name, unsigned int value);
467 int             dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type, ATTR_FLAGS flags);
468 int             dict_addvalue(char const *namestr, char const *attrstr, int value);
469 int             dict_init(char const *dir, char const *fn);
470 void            dict_free(void);
471 int             dict_read(char const *dir, char const *filename);
472 void            dict_attr_free(DICT_ATTR const **da);
473 DICT_ATTR const *dict_attr_copy(DICT_ATTR const *da, int vp_free);
474 DICT_ATTR const *dict_attrunknown(unsigned int attr, unsigned int vendor, int vp_free);
475 DICT_ATTR const *dict_attrunknownbyname(char const *attribute, int vp_free);
476 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
477 DICT_ATTR const *dict_attrbyname(char const *attr);
478 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
479                                  PW_TYPE type);
480 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
481                                            unsigned int vendor);
482 int             dict_attr_child(DICT_ATTR const *parent,
483                                 unsigned int *pattr, unsigned int *pvendor);
484 DICT_VALUE      *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
485 DICT_VALUE      *dict_valbyname(unsigned int attr, unsigned int vendor, char const *val);
486 char const      *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value);
487 int             dict_vendorbyname(char const *name);
488 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
489
490 #if 1 /* FIXME: compat */
491 #define dict_attrget    dict_attrbyvalue
492 #define dict_attrfind   dict_attrbyname
493 #define dict_valfind    dict_valbyname
494 /*#define dict_valget   dict_valbyattr almost but not quite*/
495 #endif
496
497 /* md5.c */
498
499 void            fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);
500
501 /* hmac.c */
502
503 void fr_hmac_md5(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, unsigned char *digest);
504
505 /* hmacsha1.c */
506
507 void fr_hmac_sha1(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, uint8_t *digest);
508
509 /* radius.c */
510 int             rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
511 int             rad_packet_ok(RADIUS_PACKET *packet, int flags);
512 RADIUS_PACKET   *rad_recv(int fd, int flags);
513 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
514                         int *code);
515 void            rad_recv_discard(int sockfd);
516 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
517                            char const *secret);
518 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, char const *secret);
519 int             rad_encode(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
520                            char const *secret);
521 int             rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
522                          char const *secret);
523
524 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
525 RADIUS_PACKET   *rad_alloc(TALLOC_CTX *ctx, int newvector);
526 RADIUS_PACKET   *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
527 RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
528
529 void            rad_free(RADIUS_PACKET **);
530 int             rad_pwencode(char *encpw, size_t *len, char const *secret,
531                              uint8_t const *vector);
532 int             rad_pwdecode(char *encpw, size_t len, char const *secret,
533                              uint8_t const *vector);
534
535 #define FR_TUNNEL_PW_ENC_LENGTH(_x) (2 + 1 + _x + PAD(_x + 1, 16))
536 int             rad_tunnel_pwencode(char *encpw, size_t *len, char const *secret,
537                                     uint8_t const *vector);
538 int             rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
539                                     char const *secret, uint8_t const *vector);
540 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
541                                 int id, VALUE_PAIR *password);
542
543 int             rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
544                             DICT_ATTR *da, uint8_t const *data, size_t length);
545 int             rad_tlv_ok(uint8_t const *data, size_t length,
546                            size_t dv_type, size_t dv_length);
547
548 ssize_t         rad_attr2vp(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
549                             char const *secret,
550                             uint8_t const *data, size_t length,
551                             VALUE_PAIR **pvp);
552
553 ssize_t         rad_data2vp(unsigned int attribute, unsigned int vendor,
554                             uint8_t const *data, size_t length,
555                             VALUE_PAIR **pvp);
556
557 ssize_t         rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
558
559 int             rad_vp2extended(RADIUS_PACKET const *packet,
560                                 RADIUS_PACKET const *original,
561                                 char const *secret, VALUE_PAIR const **pvp,
562                                 uint8_t *ptr, size_t room);
563 int             rad_vp2wimax(RADIUS_PACKET const *packet,
564                              RADIUS_PACKET const *original,
565                              char const *secret, VALUE_PAIR const **pvp,
566                              uint8_t *ptr, size_t room);
567
568 int             rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
569                            char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
570                            size_t room);
571
572 int             rad_vp2rfc(RADIUS_PACKET const *packet,
573                            RADIUS_PACKET const *original,
574                            char const *secret, VALUE_PAIR const **pvp,
575                            uint8_t *ptr, size_t room);
576
577 int             rad_vp2attr(RADIUS_PACKET const *packet,
578                             RADIUS_PACKET const *original, char const *secret,
579                             VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
580
581 /* valuepair.c */
582 VALUE_PAIR      *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
583 VALUE_PAIR      *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
584 int             pair2unknown(VALUE_PAIR *vp);
585 void            pairfree(VALUE_PAIR **);
586 VALUE_PAIR      *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
587 VALUE_PAIR      *pairfind_da(VALUE_PAIR *, DICT_ATTR const *da, int8_t tag);
588
589 #define         fr_cursor_init(_x, _y)  _fr_cursor_init(_x,(VALUE_PAIR const * const *) _y)
590 VALUE_PAIR      *_fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
591 void            fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in);
592 VALUE_PAIR      *fr_cursor_first(vp_cursor_t *cursor);
593 VALUE_PAIR      *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
594 VALUE_PAIR      *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag);
595 VALUE_PAIR      *fr_cursor_next(vp_cursor_t *cursor);
596 VALUE_PAIR      *fr_cursor_current(vp_cursor_t *cursor);
597 void            fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp);
598 VALUE_PAIR      *fr_cursor_remove(vp_cursor_t *cursor);
599 VALUE_PAIR      *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
600 void            pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
601 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
602 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
603 int8_t          paircmp_value(VALUE_PAIR const *a, VALUE_PAIR const *b);
604 int8_t          paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b);
605 int8_t          paircmp(VALUE_PAIR *a, VALUE_PAIR *b);
606 int8_t          pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b);
607
608 typedef int8_t (*fr_pair_cmp_t)(VALUE_PAIR const *a, VALUE_PAIR const *b);
609 int8_t          attrcmp(VALUE_PAIR const *a, VALUE_PAIR const *b);
610 int8_t          attrtagcmp(VALUE_PAIR const *a, VALUE_PAIR const *b);
611 void            pairsort(VALUE_PAIR **vps, fr_pair_cmp_t cmp);
612 bool            pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list);
613 bool            pairvalidate_relaxed(VALUE_PAIR *filter, VALUE_PAIR *list);
614 VALUE_PAIR      *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
615 VALUE_PAIR      *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp);
616 VALUE_PAIR      *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from);
617 VALUE_PAIR      *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
618 VALUE_PAIR      *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from);
619 void            pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
620 void            pairmemsteal(VALUE_PAIR *vp, uint8_t const *src);
621 void            pairstrsteal(VALUE_PAIR *vp, char const *src);
622 void            pairstrcpy(VALUE_PAIR *vp, char const * src);
623 void            pairsprintf(VALUE_PAIR *vp, char const * fmt, ...)
624 #ifdef __GNUC__
625                 __attribute__ ((format (printf, 2, 3)))
626 #endif
627 ;
628 void            pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
629 void            pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
630                            unsigned int attr, unsigned int vendor, int8_t tag);
631 VALUE_PAIR      *pairmake_ip(TALLOC_CTX *ctx, char const *value,
632                              DICT_ATTR *ipv4, DICT_ATTR *ipv6, DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix);
633 bool            pairparsevalue(VALUE_PAIR *vp, char const *value);
634 VALUE_PAIR      *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
635 int             pairmark_xlat(VALUE_PAIR *vp, char const *value);
636 FR_TOKEN        pairread(char const **ptr, VALUE_PAIR_RAW *raw);
637 FR_TOKEN        userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
638 VALUE_PAIR      *readvp2(TALLOC_CTX *ctx, FILE *fp, bool *pfiledone, char const *errprefix);
639
640 /*
641  *      Error functions.
642  */
643 void            fr_strerror_printf(char const *, ...)
644 #ifdef __GNUC__
645                 __attribute__ ((format (printf, 1, 2)))
646 #endif
647 ;
648 void            fr_perror(char const *, ...)
649 #ifdef __GNUC__
650                 __attribute__ ((format (printf, 1, 2)))
651 #endif
652 ;
653
654 extern bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
655 #define fr_assert(_x) fr_assert_cond(__FILE__,  __LINE__, #_x, (_x))
656
657 extern void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
658 #define fr_exit(_x) _fr_exit(__FILE__,  __LINE__, (_x))
659
660 extern void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
661 #define fr_exit_now(_x) _fr_exit_now(__FILE__,  __LINE__, (_x))
662
663 extern char const *fr_strerror(void);
664 extern char const *fr_syserror(int num);
665 extern bool     fr_dns_lookups; /* do IP -> hostname lookups? */
666 extern bool     fr_hostname_lookups; /* do hostname -> IP lookups? */
667 extern int      fr_debug_flag;  /* 0 = no debugging information */
668 extern int      fr_max_attributes; /* per incoming packet */
669 #define FR_MAX_PACKET_CODE (52)
670 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
671 #define is_radius_code(_x) ((_x > 0) && (_x < FR_MAX_PACKET_CODE))
672 extern FILE     *fr_log_fp;
673 extern void rad_print_hex(RADIUS_PACKET *packet);
674 void            fr_printf_log(char const *, ...)
675 #ifdef __GNUC__
676                 __attribute__ ((format (printf, 1, 2)))
677 #endif
678 ;
679
680 /*
681  *      Several handy miscellaneous functions.
682  */
683 int             fr_set_signal(int sig, sig_t func);
684 TALLOC_CTX      *fr_autofree_ctx(void);
685 char const      *fr_inet_ntop(int af, void const *src);
686 char const      *ip_ntoa(char *, uint32_t);
687 char            *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
688 uint8_t         *ifid_aton(char const *ifid_str, uint8_t *ifid);
689 int             rad_lockfd(int fd, int lock_len);
690 int             rad_lockfd_nonblock(int fd, int lock_len);
691 int             rad_unlockfd(int fd, int lock_len);
692 size_t          fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
693 size_t          fr_hex2bin(uint8_t *bin, char const *hex, size_t outlen);
694 uint32_t        fr_strtoul(char const *value, char **end);
695 bool            fr_whitespace_check(char const *value);
696 bool            fr_integer_check(char const *value);
697
698 int             fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
699
700 int             ip_ptonx(char const *src, fr_ipaddr_t *dst);
701 int             ip_hton(char const *src, int af, fr_ipaddr_t *dst);
702 char const      *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
703 struct in_addr  fr_ipaddr_mask(struct in_addr const *ipaddr, uint8_t prefix);
704 struct in6_addr fr_ipaddr_mask6(struct in6_addr const *ipaddr, uint8_t prefix);
705 int             fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, int port,
706                                    struct sockaddr_storage *sa, socklen_t *salen);
707 int             fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
708                                    fr_ipaddr_t *ipaddr, int * port);
709 ssize_t         fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
710 size_t          fr_prints_uint128(char *out, size_t outlen, uint128_t const num);
711 int64_t         fr_pow(int32_t base, uint8_t exp);
712 int             fr_get_time(char const *date_str, time_t *date);
713
714 /*
715  *      Define TALLOC_DEBUG to check overflows with talloc.
716  *      we can't use valgrind, because the memory used by
717  *      talloc is valid memory... just not for us.
718  */
719 #ifdef TALLOC_DEBUG
720 void            fr_talloc_verify_cb(const void *ptr, int depth,
721                                     int max_depth, int is_ref,
722                                     void *private_data);
723 #define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
724 #else
725 #define VERIFY_ALL_TALLOC
726 #endif
727
728 #ifdef WITH_ASCEND_BINARY
729 /* filters.c */
730 int             ascend_parse_filter(VALUE_PAIR *vp, char const *value);
731 void            print_abinary(VALUE_PAIR const *vp, char *buffer, size_t len, int8_t quote);
732 #endif /*WITH_ASCEND_BINARY*/
733
734 /* random numbers in isaac.c */
735 /* context of random number generator */
736 typedef struct fr_randctx {
737         uint32_t randcnt;
738         uint32_t randrsl[256];
739         uint32_t randmem[256];
740         uint32_t randa;
741         uint32_t randb;
742         uint32_t randc;
743 } fr_randctx;
744
745 void            fr_isaac(fr_randctx *ctx);
746 void            fr_randinit(fr_randctx *ctx, int flag);
747 uint32_t        fr_rand(void);  /* like rand(), but better. */
748 void            fr_rand_seed(void const *, size_t ); /* seed the random pool */
749
750
751 /* crypt wrapper from crypt.c */
752 int             fr_crypt_check(char const *key, char const *salt);
753
754 /* cbuff.c */
755 typedef struct fr_cbuff fr_cbuff_t;
756
757 fr_cbuff_t      *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
758 void            fr_cbuff_rp_insert(fr_cbuff_t *cbuff, void *obj);
759 void            *fr_cbuff_rp_next(fr_cbuff_t *cbuff, TALLOC_CTX *ctx);
760
761 /* debug.c */
762
763 /** Optional callback passed to fr_fault_setup
764  *
765  * Allows optional logic to be run before calling the main fault handler.
766  *
767  * If the callback returns < 0, the main fault handler will not be called.
768  *
769  * @param signum signal raised.
770  * @return 0 on success < 0 on failure.
771  */
772 typedef int (*fr_fault_cb)(int signum);
773 typedef struct fr_bt_marker fr_bt_marker_t;
774
775 void            fr_debug_break(void);
776 void            backtrace_print(fr_cbuff_t *cbuff, void *obj);
777 fr_bt_marker_t  *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj);
778 void            fr_fault(int sig);
779 int             fr_fault_setup(char const *cmd, char const *program);
780 void            fr_fault_set_cb(fr_fault_cb cb);
781
782 /* rbtree.c */
783 typedef struct rbtree_t rbtree_t;
784 typedef struct rbnode_t rbnode_t;
785
786 /* callback order for walking  */
787 typedef enum {
788         RBTREE_PRE_ORDER,
789         RBTREE_IN_ORDER,
790         RBTREE_POST_ORDER,
791         RBTREE_DELETE_ORDER
792 } rb_order_t;
793
794 #define RBTREE_FLAG_NONE    (0)
795 #define RBTREE_FLAG_REPLACE (1 << 0)
796 #define RBTREE_FLAG_LOCK    (1 << 1)
797
798 typedef int (*rb_comparator_t)(void const *ctx, void const *data);
799 typedef int (*rb_walker_t)(void *ctx, void *data);
800 typedef void (*rb_free_t)(void *data);
801
802 rbtree_t        *rbtree_create(rb_comparator_t compare, rb_free_t node_free, int flags);
803 void            rbtree_free(rbtree_t *tree);
804 bool            rbtree_insert(rbtree_t *tree, void *data);
805 rbnode_t        *rbtree_insert_node(rbtree_t *tree, void *data);
806 void            rbtree_delete(rbtree_t *tree, rbnode_t *z);
807 bool            rbtree_deletebydata(rbtree_t *tree, void const *data);
808 rbnode_t        *rbtree_find(rbtree_t *tree, void const *data);
809 void            *rbtree_finddata(rbtree_t *tree, void const *data);
810 int             rbtree_num_elements(rbtree_t *tree);
811 void            *rbtree_min(rbtree_t *tree);
812 void            *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
813
814 /*
815  *      The callback should be declared as:
816  *      int callback(void *context, void *data)
817  *
818  *      The "context" is some user-defined context.
819  *      The "data" is the pointer to the user data in the node,
820  *        NOT the node itself.
821  *
822  *      It should return 0 if all is OK, and !0 for any error.
823  *      The walking will stop on any error.
824  *
825  *      Except with RBTREE_DELETE_ORDER, where the callback should return <0 for
826  *      errors, and may return 1 to delete the current node and halt,
827  *      or 2 to delete the current node and continue.  This may be
828  *      used to batch-delete select nodes from a locked rbtree.
829  */
830 int             rbtree_walk(rbtree_t *tree, rb_order_t order, rb_walker_t compare, void *context);
831
832 /*
833  *      Find a matching data item in an rbtree and, if one is found,
834  *      perform a callback on it.
835  *
836  *      The callback is similar to rbtree_walk above, except that a
837  *      positive return code from the callback will cause the found node
838  *      to be deleted from the tree.  If the tree was created with
839  *      RBTREE_FLAG_LOCK, then the entire find/callback/delete/rebalance
840  *      sequence happens while the lock is held.
841  *
842  *      Note that the callback MUST NOT alter any of the data which
843  *      is used as the rbtree key, nor attempt to alter the rest of
844  *      the rbtree in any way.
845  *
846  *      Returns a pointer to the user data in the found node, or NULL if the
847  *      item was not found, or NULL if the item was deleted and the tree was
848  *      created with a freeNode garbage collection routine.
849  */
850 void            *rbtree_callbydata(rbtree_t *tree, void const *data, rb_comparator_t compare, void *context);
851
852 /*
853  *      FIFOs
854  */
855 typedef struct  fr_fifo_t fr_fifo_t;
856 typedef void (*fr_fifo_free_t)(void *);
857 fr_fifo_t       *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
858 void            fr_fifo_free(fr_fifo_t *fi);
859 int             fr_fifo_push(fr_fifo_t *fi, void *data);
860 void            *fr_fifo_pop(fr_fifo_t *fi);
861 void            *fr_fifo_peek(fr_fifo_t *fi);
862 int             fr_fifo_num_elements(fr_fifo_t *fi);
863
864 #ifdef __cplusplus
865 }
866 #endif
867
868 #include <freeradius-devel/packet.h>
869
870 #ifdef WITH_TCP
871 #  include <freeradius-devel/tcp.h>
872 #endif
873
874 #endif /*LIBRADIUS_H*/