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