Expose dict_parent
[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 #include <freeradius-devel/regex.h>
85
86 #ifdef SIZEOF_UNSIGNED_INT
87 #  if SIZEOF_UNSIGNED_INT != 4
88 #    error FATAL: sizeof(unsigned int) != 4
89 #  endif
90 #endif
91
92 /*
93  *  Include for modules.
94  */
95 #include <freeradius-devel/sha1.h>
96 #include <freeradius-devel/md4.h>
97
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101
102 #ifndef HAVE_SIG_T
103 typedef void (*sig_t)(int);
104 #endif
105
106 #if defined(WITH_VERIFY_PTR)
107 #  define FREE_MAGIC (0xF4EEF4EE)
108
109 /*
110  * @FIXME
111  *  Add if (_x->da) (void) talloc_get_type_abort(_x->da, DICT_ATTR);
112  *  to the macro below when dictionaries are talloced.
113  */
114 #  define VERIFY_VP(_x)         fr_pair_verify(__FILE__,  __LINE__, _x)
115 #  define VERIFY_LIST(_x)       fr_pair_list_verify(__FILE__,  __LINE__, NULL, _x)
116 #  define VERIFY_PACKET(_x)     (void) talloc_get_type_abort(_x, RADIUS_PACKET)
117 #else
118 /*
119  *  Even if were building without WITH_VERIFY_PTR
120  *  the pointer must not be NULL when these various macros are used
121  *  so we can add some sneaky soft asserts.
122  */
123 #  define VERIFY_VP(_x)         fr_assert(_x)
124 #  define VERIFY_LIST(_x)       fr_assert(_x)
125 #  define VERIFY_PACKET(_x)     fr_assert(_x)
126 #endif
127
128 #define AUTH_VECTOR_LEN         16
129 #define CHAP_VALUE_LENGTH       16
130 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
131 #define FR_MAX_VENDOR           (1 << 24) /* RFC limitations */
132
133 #ifdef _LIBRADIUS
134 #  define RADIUS_HDR_LEN        20
135 #  define VENDORPEC_USR         429
136 #  define VENDORPEC_LUCENT      4846
137 #  define VENDORPEC_STARENT     8164
138 #  define DEBUG                 if (fr_debug_lvl && fr_log_fp) fr_printf_log
139 #endif
140
141 #  define debug_pair(vp)        do { if (fr_debug_lvl && fr_log_fp) { \
142                                         vp_print(fr_log_fp, vp); \
143                                      } \
144                                 } while(0)
145
146 #define TAG_VALID(x)            ((x) > 0 && (x) < 0x20)
147 #define TAG_VALID_ZERO(x)       ((x) < 0x20)
148 #define TAG_ANY                 INT8_MIN
149 #define TAG_NONE                0
150 /** Check if tags are equal
151  *
152  * @param _x tag were matching on.
153  * @param _y tag belonging to the attribute were checking.
154  */
155 #define TAG_EQ(_x, _y) ((_x == _y) || (_x == TAG_ANY) || ((_x == TAG_NONE) && (_y == TAG_ANY)))
156 #define ATTRIBUTE_EQ(_x, _y) ((_x && _y) && (_x->da == _y->da) && (!_x->da->flags.has_tag || TAG_EQ(_x->tag, _y->tag)))
157
158 #define NUM_ANY                 INT_MIN
159 #define NUM_ALL                 (INT_MIN + 1)
160 #define NUM_COUNT               (INT_MIN + 2)
161 #define NUM_LAST                (INT_MIN + 3)
162
163 #define PAD(_x, _y)             (_y - ((_x) % _y))
164
165 #define PRINTF_LIKE(n)          CC_HINT(format(printf, n, n+1))
166 #define NEVER_RETURNS           CC_HINT(noreturn)
167 #define UNUSED                  CC_HINT(unused)
168 #define BLANK_FORMAT            " "     /* GCC_LINT whines about empty formats */
169
170 typedef struct attr_flags {
171         unsigned int    is_unknown : 1;                         //!< Attribute number or vendor is unknown.
172         unsigned int    is_tlv : 1;                             //!< Is a sub attribute.
173
174         unsigned int    has_tag : 1;                            //!< Tagged attribute.
175         unsigned int    array : 1;                              //!< Pack multiples into 1 attr.
176         unsigned int    has_value : 1;                          //!< Has a value.
177         unsigned int    has_value_alias : 1;                    //!< Has a value alias.
178         unsigned int    has_tlv : 1;                            //!< Has sub attributes.
179
180         unsigned int    extended : 1;                           //!< Extended attribute.
181         unsigned int    long_extended : 1;                      //!< Long format.
182         unsigned int    evs : 1;                                //!< Extended VSA.
183         unsigned int    wimax: 1;                               //!< WiMAX format=1,1,c.
184
185         unsigned int    concat : 1;                             //!< concatenate multiple instances
186         unsigned int    is_pointer : 1;                         //!< data is a pointer
187
188         unsigned int    virtual : 1;                            //!< for dynamic expansion
189
190         unsigned int    compare : 1;                            //!< has a paircompare registered
191
192         uint8_t         encrypt;                                //!< Ecryption method.
193         uint8_t         length;
194 } ATTR_FLAGS;
195
196 /*
197  *  Values of the encryption flags.
198  */
199 #define FLAG_ENCRYPT_NONE           (0)
200 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
201 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
202 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
203
204 extern const FR_NAME_NUMBER dict_attr_types[];
205 extern const size_t dict_attr_sizes[PW_TYPE_MAX][2];
206 extern const int fr_attr_max_tlv;
207 extern const int fr_attr_shift[];
208 extern const unsigned int fr_attr_mask[];
209
210 /** dictionary attribute
211  *
212  */
213 typedef struct dict_attr {
214         unsigned int            attr;
215         PW_TYPE                 type;
216         unsigned int            vendor;
217         ATTR_FLAGS              flags;
218         char                    name[1];
219 } DICT_ATTR;
220
221 /** value of an enumerated attribute
222  *
223  */
224 typedef struct dict_value {
225         unsigned int            attr;
226         unsigned int            vendor;
227         int                     value;
228         char                    name[1];
229 } DICT_VALUE;
230
231 /** dictionary vendor
232  *
233  */
234 typedef struct dict_vendor {
235         unsigned int            vendorpec;
236         size_t                  type;                           //!< Length of type data
237         size_t                  length;                         //!< Length of length data
238         size_t                  flags;
239         char                    name[1];
240 } DICT_VENDOR;
241
242 /** Union containing all data types supported by the server
243  *
244  * This union contains all data types that can be represented by VALUE_PAIRs. It may also be used in other parts
245  * of the server where values of different types need to be stored.
246  *
247  * PW_TYPE should be an enumeration of the values in this union.
248  */
249 typedef union value_data {
250         char const              *strvalue;                      //!< Pointer to UTF-8 string.
251         uint8_t const           *octets;                        //!< Pointer to binary string.
252         uint32_t                integer;                        //!< 32bit unsigned integer.
253         struct in_addr          ipaddr;                         //!< IPv4 Address.
254         uint32_t                date;                           //!< Date (32bit Unix timestamp).
255         size_t                  filter[32/sizeof(size_t)];      //!< Ascend binary format a packed data
256                                                                 //!< structure.
257
258         uint8_t                 ifid[8];                        //!< IPv6 interface ID (should be struct?).
259         struct in6_addr         ipv6addr;                       //!< IPv6 Address.
260         uint8_t                 ipv6prefix[18];                 //!< IPv6 prefix (should be struct?).
261
262         uint8_t                 byte;                           //!< 8bit unsigned integer.
263         uint16_t                ushort;                         //!< 16bit unsigned integer.
264
265         uint8_t                 ether[6];                       //!< Ethernet (MAC) address.
266
267         int32_t                 sinteger;                       //!< 32bit signed integer.
268         uint64_t                integer64;                      //!< 64bit unsigned integer.
269
270         uint8_t                 ipv4prefix[6];                  //!< IPv4 prefix (should be struct?).
271
272         void                    *ptr;                           //!< generic pointer.
273 } value_data_t;
274
275 /** The type of value a VALUE_PAIR contains
276  *
277  * This is used to add structure to nested VALUE_PAIRs and specifies what type of node it is (set, list, data).
278  *
279  * xlat is another type of data node which must first be expanded before use.
280  */
281 typedef enum value_type {
282         VT_NONE = 0,                                            //!< VALUE_PAIR has no value.
283         VT_SET,                                                 //!< VALUE_PAIR has children.
284         VT_LIST,                                                //!< VALUE_PAIR has multiple values.
285         VT_DATA,                                                //!< VALUE_PAIR has a single value.
286         VT_XLAT                                                 //!< valuepair value must be xlat expanded when it's
287                                                                 //!< added to VALUE_PAIR tree.
288 } value_type_t;
289
290 /** Stores an attribute, a value and various bits of other data
291  *
292  * VALUE_PAIRs are the main data structure used in the server
293  *
294  * They also specify what behaviour should be used when the attribute is merged into a new list/tree.
295  */
296 typedef struct value_pair {
297         DICT_ATTR const         *da;                            //!< Dictionary attribute defines the attribute
298                                                                 //!< number, vendor and type of the attribute.
299
300         struct value_pair       *next;
301
302         FR_TOKEN                op;                             //!< Operator to use when moving or inserting
303                                                                 //!< valuepair into a list.
304
305         int8_t                  tag;                            //!< Tag value used to group valuepairs.
306
307         union {
308         //      VALUE_SET       *set;                           //!< Set of child attributes.
309         //      VALUE_LIST      *list;                          //!< List of values for
310                                                                 //!< multivalued attribute.
311         //      value_data_t    *data;                          //!< Value data for this attribute.
312
313                 char const      *xlat;                          //!< Source string for xlat expansion.
314         } value;
315
316         value_type_t            type;                           //!< Type of pointer in value union.
317
318         size_t                  length;                         //!< of Data field.
319         value_data_t            data;
320 } VALUE_PAIR;
321
322 /** Abstraction to allow iterating over different configurations of VALUE_PAIRs
323  *
324  * This allows functions which do not care about the structure of collections of VALUE_PAIRs
325  * to iterate over all members in a collection.
326  *
327  * Field within a vp_cursor should not be accessed directly, and vp_cursors should only be
328  * manipulated with the pair* functions.
329  */
330 typedef struct vp_cursor {
331         VALUE_PAIR      **first;
332         VALUE_PAIR      *found;                                 //!< pairfind marker.
333         VALUE_PAIR      *last;                                  //!< Temporary only used for fr_cursor_insert
334         VALUE_PAIR      *current;                               //!< The current attribute.
335         VALUE_PAIR      *next;                                  //!< Next attribute to process.
336 } vp_cursor_t;
337
338 /** A VALUE_PAIR in string format.
339  *
340  * Used to represent pairs in the legacy 'users' file format.
341  */
342 typedef struct value_pair_raw {
343         char l_opand[256];                                      //!< Left hand side of the pair.
344         char r_opand[1024];                                     //!< Right hand side of the pair.
345
346         FR_TOKEN quote;                                         //!< Type of quoting around the r_opand.
347
348         FR_TOKEN op;                                            //!< Operator.
349 } VALUE_PAIR_RAW;
350
351 #define vp_strvalue     data.strvalue
352 #define vp_integer      data.integer
353 #define vp_ipaddr       data.ipaddr.s_addr
354 #define vp_date         data.date
355 #define vp_filter       data.filter
356 #define vp_octets       data.octets
357 #define vp_ifid         data.ifid
358 #define vp_ipv6addr     data.ipv6addr
359 #define vp_ipv6prefix   data.ipv6prefix
360 #define vp_byte         data.byte
361 #define vp_short        data.ushort
362 #define vp_ether        data.ether
363 #define vp_signed       data.sinteger
364 #define vp_integer64    data.integer64
365 #define vp_ipv4prefix   data.ipv4prefix
366 #define vp_length       length
367
368 typedef struct fr_ipaddr_t {
369         int             af;     /* address family */
370         union {
371                 struct in_addr  ip4addr;
372                 struct in6_addr ip6addr; /* maybe defined in missing.h */
373         } ipaddr;
374         uint8_t         prefix;
375         uint32_t        scope;  /* for IPv6 */
376 } fr_ipaddr_t;
377
378 /*
379  *      vector:         Request authenticator from access-request packet
380  *                      Put in there by rad_decode, and must be put in the
381  *                      response RADIUS_PACKET as well before calling rad_send
382  *
383  *      verified:       Filled in by rad_decode for accounting-request packets
384  *
385  *      data,data_len:  Used between rad_recv and rad_decode.
386  */
387 typedef struct radius_packet {
388         int                     sockfd;
389         fr_ipaddr_t             src_ipaddr;
390         fr_ipaddr_t             dst_ipaddr;
391         uint16_t                src_port;
392         uint16_t                dst_port;
393         int                     id;
394         unsigned int            code;
395         uint8_t                 vector[AUTH_VECTOR_LEN];
396         struct timeval          timestamp;
397         uint8_t                 *data;
398         size_t                  data_len;
399         VALUE_PAIR              *vps;
400         ssize_t                 offset;
401 #ifdef WITH_TCP
402         size_t                  partial;
403         int                     proto;
404 #endif
405 } RADIUS_PACKET;
406
407 typedef enum {
408         DECODE_FAIL_NONE = 0,
409         DECODE_FAIL_MIN_LENGTH_PACKET,
410         DECODE_FAIL_MIN_LENGTH_FIELD,
411         DECODE_FAIL_MIN_LENGTH_MISMATCH,
412         DECODE_FAIL_HEADER_OVERFLOW,
413         DECODE_FAIL_UNKNOWN_PACKET_CODE,
414         DECODE_FAIL_INVALID_ATTRIBUTE,
415         DECODE_FAIL_ATTRIBUTE_TOO_SHORT,
416         DECODE_FAIL_ATTRIBUTE_OVERFLOW,
417         DECODE_FAIL_MA_INVALID_LENGTH,
418         DECODE_FAIL_ATTRIBUTE_UNDERFLOW,
419         DECODE_FAIL_TOO_MANY_ATTRIBUTES,
420         DECODE_FAIL_MA_MISSING,
421         DECODE_FAIL_MAX
422 } decode_fail_t;
423
424 /*
425  *      Version check.
426  */
427 int             fr_check_lib_magic(uint64_t magic);
428
429 /*
430  *      Printing functions.
431  */
432 int             fr_utf8_char(uint8_t const *str, ssize_t inlen);
433 char const      *fr_utf8_strchr(int *chr_len, char const *str, char const *chr);
434 size_t          fr_prints(char *out, size_t outlen, char const *in, ssize_t inlen, char quote);
435 size_t          fr_prints_len(char const *in, ssize_t inlen, char quote);
436 char            *fr_aprints(TALLOC_CTX *ctx, char const *in, ssize_t inlen, char quote);
437
438 #define         is_truncated(_ret, _max) ((_ret) >= (_max))
439 #define         truncate_len(_ret, _max) (((_ret) >= (_max)) ? ((_max) - 1) : _ret)
440 size_t          vp_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, char quote);
441
442
443 char            *vp_aprints_value(TALLOC_CTX *ctx, VALUE_PAIR const *vp, char quote);
444
445 size_t          vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
446 size_t          vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
447 void            vp_print(FILE *, VALUE_PAIR const *);
448 void            vp_printlist(FILE *, VALUE_PAIR const *);
449 char            *vp_aprints_type(TALLOC_CTX *ctx, PW_TYPE type);
450
451 char            *vp_aprints(TALLOC_CTX *ctx, VALUE_PAIR const *vp, char quote);
452 #define         fprint_attr_val vp_print
453
454 /*
455  *      Dictionary functions.
456  */
457 #define DICT_VALUE_MAX_NAME_LEN (128)
458 #define DICT_VENDOR_MAX_NAME_LEN (128)
459 #define DICT_ATTR_MAX_NAME_LEN (128)
460
461 #define DICT_ATTR_SIZE sizeof(DICT_ATTR) + DICT_ATTR_MAX_NAME_LEN
462
463 extern const int dict_attr_allowed_chars[256];
464 int             dict_valid_name(char const *name);
465 int             str2argv(char *str, char **argv, int max_argc);
466 int             dict_str2oid(char const *ptr, unsigned int *pattr,
467                              unsigned int *pvendor, int tlv_depth);
468 int             dict_addvendor(char const *name, unsigned int value);
469 int             dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type, ATTR_FLAGS flags);
470 int             dict_addvalue(char const *namestr, char const *attrstr, int value);
471 int             dict_init(char const *dir, char const *fn);
472 void            dict_free(void);
473 int             dict_read(char const *dir, char const *filename);
474
475 void            dict_attr_free(DICT_ATTR const **da);
476 int             dict_unknown_from_fields(DICT_ATTR *da, unsigned int attr, unsigned int vendor);
477 DICT_ATTR const *dict_unknown_afrom_fields(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
478 int             dict_unknown_from_str(DICT_ATTR *da, char const *name);
479 int             dict_unknown_from_substr(DICT_ATTR *da, char const **name);
480 DICT_ATTR const *dict_unknown_afrom_str(TALLOC_CTX *ctx, char const *name);
481 DICT_ATTR const *dict_unknown_add(DICT_ATTR const *old);
482
483 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
484 DICT_ATTR const *dict_attrbyname(char const *attr);
485 DICT_ATTR const *dict_attrbyname_substr(char const **name);
486 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
487                                  PW_TYPE type);
488 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
489                                            unsigned int vendor);
490 DICT_ATTR const *dict_parent(unsigned int attr, unsigned int vendor);
491 int             dict_attr_child(DICT_ATTR const *parent,
492                                 unsigned int *pattr, unsigned int *pvendor);
493 DICT_VALUE      *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
494 DICT_VALUE      *dict_valbyname(unsigned int attr, unsigned int vendor, char const *val);
495 char const      *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value);
496 int             dict_vendorbyname(char const *name);
497 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
498
499 #if 1 /* FIXME: compat */
500 #define dict_attrget    dict_attrbyvalue
501 #define dict_attrfind   dict_attrbyname
502 #define dict_valfind    dict_valbyname
503 /*#define dict_valget   dict_valbyattr almost but not quite*/
504 #endif
505
506 /* radius.c */
507 int             rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
508 bool            rad_packet_ok(RADIUS_PACKET *packet, int flags, decode_fail_t *reason);
509 RADIUS_PACKET   *rad_recv(TALLOC_CTX *ctx, int fd, int flags);
510 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, int *code);
511 void            rad_recv_discard(int sockfd);
512 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
513                            char const *secret);
514 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, char const *secret);
515 int             rad_encode(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
516                            char const *secret);
517 int             rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
518                          char const *secret);
519
520 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
521 RADIUS_PACKET   *rad_alloc(TALLOC_CTX *ctx, bool new_vector);
522 RADIUS_PACKET   *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
523 RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
524
525 void            rad_free(RADIUS_PACKET **);
526 int             rad_pwencode(char *encpw, size_t *len, char const *secret,
527                              uint8_t const *vector);
528 int             rad_pwdecode(char *encpw, size_t len, char const *secret,
529                              uint8_t const *vector);
530
531 #define FR_TUNNEL_PW_ENC_LENGTH(_x) (2 + 1 + _x + PAD(_x + 1, 16))
532 ssize_t         rad_tunnel_pwencode(char *encpw, size_t *len, char const *secret,
533                                     uint8_t const *vector);
534 ssize_t         rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
535                                     char const *secret, uint8_t const *vector);
536 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
537                                 int id, VALUE_PAIR *password);
538
539 int             rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
540                             DICT_ATTR *da, uint8_t const *data, size_t length);
541 int             rad_tlv_ok(uint8_t const *data, size_t length,
542                            size_t dv_type, size_t dv_length);
543
544 ssize_t         data2vp(TALLOC_CTX *ctx,
545                         RADIUS_PACKET *packet, RADIUS_PACKET const *original,
546                         char const *secret,
547                         DICT_ATTR const *da, uint8_t const *start,
548                         size_t const attrlen, size_t const packetlen,
549                         VALUE_PAIR **pvp);
550
551 ssize_t         rad_attr2vp(TALLOC_CTX *ctx,
552                             RADIUS_PACKET *packet, RADIUS_PACKET const *original,
553                             char const *secret,
554                             uint8_t const *data, size_t length,
555                             VALUE_PAIR **pvp);
556
557 ssize_t rad_data2vp_tlvs(TALLOC_CTX *ctx,
558                          RADIUS_PACKET *packet, RADIUS_PACKET const *original,
559                          char const *secret, DICT_ATTR const *da,
560                          uint8_t const *start, size_t length,
561                          VALUE_PAIR **pvp);
562
563 ssize_t         rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
564
565 int             rad_vp2extended(RADIUS_PACKET const *packet,
566                                 RADIUS_PACKET const *original,
567                                 char const *secret, VALUE_PAIR const **pvp,
568                                 uint8_t *ptr, size_t room);
569 int             rad_vp2wimax(RADIUS_PACKET const *packet,
570                              RADIUS_PACKET const *original,
571                              char const *secret, VALUE_PAIR const **pvp,
572                              uint8_t *ptr, size_t room);
573
574 int             rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
575                            char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
576                            size_t room);
577
578 int             rad_vp2rfc(RADIUS_PACKET const *packet,
579                            RADIUS_PACKET const *original,
580                            char const *secret, VALUE_PAIR const **pvp,
581                            uint8_t *ptr, size_t room);
582
583 int             rad_vp2attr(RADIUS_PACKET const *packet,
584                             RADIUS_PACKET const *original, char const *secret,
585                             VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
586
587 /* pair.c */
588 VALUE_PAIR      *fr_pair_afrom_da(TALLOC_CTX *ctx, DICT_ATTR const *da);
589 VALUE_PAIR      *fr_pair_afrom_num(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
590 int             fr_pair_to_unknown(VALUE_PAIR *vp);
591 void            fr_pair_list_free(VALUE_PAIR **);
592 VALUE_PAIR      *fr_pair_find_by_num(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
593 VALUE_PAIR      *fr_pair_find_by_da(VALUE_PAIR *, DICT_ATTR const *da, int8_t tag);
594
595 VALUE_PAIR      *fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR * const *node);
596 void            fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in);
597 VALUE_PAIR      *fr_cursor_first(vp_cursor_t *cursor);
598 VALUE_PAIR      *fr_cursor_last(vp_cursor_t *cursor);
599 VALUE_PAIR      *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
600
601 VALUE_PAIR      *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag)
602                 CC_HINT(nonnull);
603
604 VALUE_PAIR      *fr_cursor_next(vp_cursor_t *cursor);
605 VALUE_PAIR      *fr_cursor_next_peek(vp_cursor_t *cursor);
606 VALUE_PAIR      *fr_cursor_current(vp_cursor_t *cursor);
607 void            fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp);
608 void            fr_cursor_merge(vp_cursor_t *cursor, VALUE_PAIR *vp);
609 VALUE_PAIR      *fr_cursor_remove(vp_cursor_t *cursor);
610 VALUE_PAIR      *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
611 void            fr_pair_delete_by_num(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
612 void            fr_pair_add(VALUE_PAIR **, VALUE_PAIR *);
613 void            fr_pair_replace(VALUE_PAIR **first, VALUE_PAIR *add);
614 int             fr_pair_cmp(VALUE_PAIR *a, VALUE_PAIR *b);
615 int             fr_pair_list_cmp(VALUE_PAIR *a, VALUE_PAIR *b);
616
617 typedef         int8_t (*fr_cmp_t)(void const *a, void const *b);
618 int8_t          attrcmp(void const *a, void const *b);
619 int8_t          fr_pair_cmp_by_da_tag(void const *a, void const *b);
620 void            fr_pair_list_sort(VALUE_PAIR **vps, fr_cmp_t cmp);
621 void            fr_pair_validate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2]);
622 bool            fr_pair_validate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
623 bool            fr_pair_validate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
624 VALUE_PAIR      *fr_pair_copy(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
625 VALUE_PAIR      *fr_pair_list_copy(TALLOC_CTX *ctx, VALUE_PAIR *from);
626 VALUE_PAIR      *fr_pair_list_copy_by_num(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
627 void            fr_pair_steal(TALLOC_CTX *ctx, VALUE_PAIR *vp);
628 void            fr_pair_value_memcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
629 void            fr_pair_value_memsteal(VALUE_PAIR *vp, uint8_t const *src);
630 void            fr_pair_value_strsteal(VALUE_PAIR *vp, char const *src);
631 void            fr_pair_value_strcpy(VALUE_PAIR *vp, char const * src);
632 void            fr_pair_value_bstrncpy(VALUE_PAIR *vp, void const * src, size_t len);
633 void            fr_pair_value_sprintf(VALUE_PAIR *vp, char const * fmt, ...) CC_HINT(format (printf, 2, 3));
634 void            fr_pair_list_move(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
635 void            fr_pair_list_move_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
636                                          unsigned int attr, unsigned int vendor, int8_t tag);
637 void            fr_pair_list_mcopy_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
638                                           unsigned int attr, unsigned int vendor, int8_t tag);
639 VALUE_PAIR      *fr_pair_afrom_ip_str(TALLOC_CTX *ctx, char const *value,
640                              DICT_ATTR *ipv4, DICT_ATTR *ipv6, DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix);
641 int             fr_pair_value_from_str(VALUE_PAIR *vp, char const *value, size_t len);
642 VALUE_PAIR      *fr_pair_make(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
643 int             fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value);
644 FR_TOKEN        fr_pair_raw_from_str(char const **ptr, VALUE_PAIR_RAW *raw);
645 FR_TOKEN        fr_pair_list_afrom_str(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
646 int             fr_pair_list_afrom_file(TALLOC_CTX *ctx, VALUE_PAIR **out, FILE *fp, bool *pfiledone);
647
648
649 /** Compare two attributes using and operator.
650  *
651  * @return 1 if equal, 0 if not eaqual, -1 on error.
652  */
653 #define         fr_pair_cmp_op(_op, _a, _b)     value_data_cmp_op(_op, _a->da->type, &_a->data, _a->vp_length, _b->da->type, &_b->data, _b->vp_length)
654
655 /* value.c */
656 int             value_data_cmp(PW_TYPE a_type, value_data_t const *a, size_t a_len,
657                                PW_TYPE b_type, value_data_t const *b, size_t b_len);
658
659 int             value_data_cmp_op(FR_TOKEN op,
660                                   PW_TYPE a_type, value_data_t const *a, size_t a_len,
661                                   PW_TYPE b_type, value_data_t const *b, size_t b_len);
662
663 ssize_t         value_data_from_str(TALLOC_CTX *ctx, value_data_t *dst,
664                                     PW_TYPE *src_type, DICT_ATTR const *src_enumv,
665                                     char const *src, ssize_t src_len, char quote);
666
667 ssize_t         value_data_cast(TALLOC_CTX *ctx, value_data_t *dst,
668                                 PW_TYPE dst_type, DICT_ATTR const *dst_enumv,
669                                 PW_TYPE src_type, DICT_ATTR const *src_enumv,
670                                 value_data_t const *src, size_t src_len);
671
672 ssize_t         value_data_copy(TALLOC_CTX *ctx, value_data_t *dst, PW_TYPE type,
673                                 const value_data_t *src, size_t src_len);
674
675 size_t          value_data_prints(char *out, size_t outlen,
676                                   PW_TYPE type, DICT_ATTR const *enumv,
677                                   value_data_t const *data, ssize_t inlen, char quote);
678
679 char            *value_data_aprints(TALLOC_CTX *ctx,
680                                     PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
681                                     size_t inlen, char quote);
682
683 /*
684  *      Error functions.
685  */
686 void            fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2));
687 void            fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));
688
689
690 char const      *fr_strerror(void);
691 char const      *fr_syserror(int num);
692 extern bool     fr_dns_lookups; /* do IP -> hostname lookups? */
693 extern bool     fr_hostname_lookups; /* do hostname -> IP lookups? */
694 extern int      fr_debug_lvl;   /* 0 = no debugging information */
695 extern uint32_t fr_max_attributes; /* per incoming packet */
696 #define FR_MAX_PACKET_CODE (52)
697 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
698 #define is_radius_code(_x) ((_x > 0) && (_x < FR_MAX_PACKET_CODE))
699 extern FILE     *fr_log_fp;
700 void            rad_print_hex(RADIUS_PACKET const *packet);
701 void            fr_printf_log(char const *, ...) CC_HINT(format (printf, 1, 2));
702
703 /*
704  *      Several handy miscellaneous functions.
705  */
706 int             fr_set_signal(int sig, sig_t func);
707 int             fr_unset_signal(int sig);
708 int             fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child);
709 char const      *fr_inet_ntop(int af, void const *src);
710 char const      *ip_ntoa(char *, uint32_t);
711 int             fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback);
712 int             fr_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback);
713 int             fr_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve);
714 int             fr_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af,
715                              bool resolve);
716 int             fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr);
717 char            *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
718 uint8_t         *ifid_aton(char const *ifid_str, uint8_t *ifid);
719 int             rad_lockfd(int fd, int lock_len);
720 int             rad_lockfd_nonblock(int fd, int lock_len);
721 int             rad_unlockfd(int fd, int lock_len);
722 char            *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen);
723 size_t          fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
724 size_t          fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen);
725 uint32_t        fr_strtoul(char const *value, char **end);
726 bool            is_whitespace(char const *value);
727 bool            is_printable(void const *value, size_t len);
728 bool            is_integer(char const *value);
729 bool            is_zero(char const *value);
730
731 int             fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
732
733 int             ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
734 char const      *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
735 struct in_addr  fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix);
736 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix);
737 void            fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
738 int             fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
739                                    struct sockaddr_storage *sa, socklen_t *salen);
740 int             fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
741                                    fr_ipaddr_t *ipaddr, uint16_t *port);
742 int             fr_nonblock(int fd);
743 int             fr_blocking(int fd);
744 ssize_t         fr_writev(int fd, struct iovec[], int iovcnt, struct timeval *timeout);
745
746 ssize_t         fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
747 size_t          fr_prints_uint128(char *out, size_t outlen, uint128_t const num);
748 int             fr_get_time(char const *date_str, time_t *date);
749 int8_t          fr_pointer_cmp(void const *a, void const *b);
750 void            fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
751 /*
752  *      Define TALLOC_DEBUG to check overflows with talloc.
753  *      we can't use valgrind, because the memory used by
754  *      talloc is valid memory... just not for us.
755  */
756 #ifdef TALLOC_DEBUG
757 void            fr_talloc_verify_cb(const void *ptr, int depth,
758                                     int max_depth, int is_ref,
759                                     void *private_data);
760 #define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
761 #else
762 #define VERIFY_ALL_TALLOC
763 #endif
764
765 #ifdef WITH_ASCEND_BINARY
766 /* filters.c */
767 int             ascend_parse_filter(value_data_t *out, char const *value, size_t len);
768 void            print_abinary(char *out, size_t outlen, uint8_t const *data, size_t len, int8_t quote);
769 #endif /*WITH_ASCEND_BINARY*/
770
771 /* random numbers in isaac.c */
772 /* context of random number generator */
773 typedef struct fr_randctx {
774         uint32_t randcnt;
775         uint32_t randrsl[256];
776         uint32_t randmem[256];
777         uint32_t randa;
778         uint32_t randb;
779         uint32_t randc;
780 } fr_randctx;
781
782 void            fr_isaac(fr_randctx *ctx);
783 void            fr_randinit(fr_randctx *ctx, int flag);
784 uint32_t        fr_rand(void);  /* like rand(), but better. */
785 void            fr_rand_seed(void const *, size_t ); /* seed the random pool */
786
787
788 /* crypt wrapper from crypt.c */
789 int             fr_crypt_check(char const *key, char const *salt);
790
791 /* cbuff.c */
792
793 typedef struct fr_cbuff fr_cbuff_t;
794
795 fr_cbuff_t      *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
796 void            fr_cbuff_rp_insert(fr_cbuff_t *cbuff, void *obj);
797 void            *fr_cbuff_rp_next(fr_cbuff_t *cbuff, TALLOC_CTX *ctx);
798
799 /* debug.c */
800 typedef enum {
801         DEBUG_STATE_UNKNOWN_NO_PTRACE           = -3,   //!< We don't have ptrace so can't check.
802         DEBUG_STATE_UNKNOWN_NO_PTRACE_CAP       = -2,   //!< CAP_SYS_PTRACE not set for the process.
803         DEBUG_STATE_UNKNOWN                     = -1,   //!< Unknown, likely fr_get_debug_state() not called yet.
804         DEBUG_STATE_NOT_ATTACHED                = 0,    //!< We can attach, so a debugger must not be.
805         DEBUG_STATE_ATTACHED                    = 1     //!< We can't attach, it's likely a debugger is already tracing.
806 } fr_debug_state_t;
807
808 #define FR_FAULT_LOG(fmt, ...) fr_fault_log(fmt "\n", ## __VA_ARGS__)
809 typedef void (*fr_fault_log_t)(char const *msg, ...) CC_HINT(format (printf, 1, 2));
810 extern fr_debug_state_t fr_debug_state;
811
812 /** Optional callback passed to fr_fault_setup
813  *
814  * Allows optional logic to be run before calling the main fault handler.
815  *
816  * If the callback returns < 0, the main fault handler will not be called.
817  *
818  * @param signum signal raised.
819  * @return 0 on success < 0 on failure.
820  */
821 typedef int (*fr_fault_cb_t)(int signum);
822 typedef struct fr_bt_marker fr_bt_marker_t;
823
824 void            fr_store_debug_state(void);
825 char const      *fr_debug_state_to_msg(fr_debug_state_t state);
826 void            fr_debug_break(bool always);
827 void            backtrace_print(fr_cbuff_t *cbuff, void *obj);
828 int             fr_backtrace_do(fr_bt_marker_t *marker);
829 fr_bt_marker_t  *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj);
830
831 void            fr_panic_on_free(TALLOC_CTX *ctx);
832 int             fr_set_dumpable_init(void);
833 int             fr_set_dumpable(bool allow_core_dumps);
834 int             fr_reset_dumpable(void);
835 int             fr_log_talloc_report(TALLOC_CTX *ctx);
836 void            fr_fault(int sig);
837 void            fr_talloc_fault_setup(void);
838 int             fr_fault_setup(char const *cmd, char const *program);
839 void            fr_fault_set_cb(fr_fault_cb_t func);
840 void            fr_fault_set_log_fd(int fd);
841 void            fr_fault_log(char const *msg, ...) CC_HINT(format (printf, 1, 2));
842
843 #  ifdef WITH_VERIFY_PTR
844 void            fr_pair_verify(char const *file, int line, VALUE_PAIR const *vp);
845 void            fr_pair_list_verify(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps);
846 #  endif
847
848 bool            fr_assert_cond(char const *file, int line, char const *expr, bool cond);
849 #  define       fr_assert(_x) fr_assert_cond(__FILE__,  __LINE__, #_x, (_x))
850
851 void            NEVER_RETURNS _fr_exit(char const *file, int line, int status);
852 #  define       fr_exit(_x) _fr_exit(__FILE__,  __LINE__, (_x))
853
854 void            NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
855 #  define       fr_exit_now(_x) _fr_exit_now(__FILE__,  __LINE__, (_x))
856
857 /* rbtree.c */
858 typedef struct rbtree_t rbtree_t;
859 typedef struct rbnode_t rbnode_t;
860
861 /* callback order for walking  */
862 typedef enum {
863         RBTREE_PRE_ORDER,
864         RBTREE_IN_ORDER,
865         RBTREE_POST_ORDER,
866         RBTREE_DELETE_ORDER
867 } rb_order_t;
868
869 #define RBTREE_FLAG_NONE    (0)
870 #define RBTREE_FLAG_REPLACE (1 << 0)
871 #define RBTREE_FLAG_LOCK    (1 << 1)
872
873 typedef int (*rb_comparator_t)(void const *ctx, void const *data);
874 typedef int (*rb_walker_t)(void *ctx, void *data);
875 typedef void (*rb_free_t)(void *data);
876
877 rbtree_t        *rbtree_create(TALLOC_CTX *ctx, rb_comparator_t compare, rb_free_t node_free, int flags);
878 void            rbtree_free(rbtree_t *tree);
879 bool            rbtree_insert(rbtree_t *tree, void *data);
880 rbnode_t        *rbtree_insert_node(rbtree_t *tree, void *data);
881 void            rbtree_delete(rbtree_t *tree, rbnode_t *z);
882 bool            rbtree_deletebydata(rbtree_t *tree, void const *data);
883 rbnode_t        *rbtree_find(rbtree_t *tree, void const *data);
884 void            *rbtree_finddata(rbtree_t *tree, void const *data);
885 uint32_t        rbtree_num_elements(rbtree_t *tree);
886 void            *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
887
888 /*
889  *      The callback should be declared as:
890  *      int callback(void *context, void *data)
891  *
892  *      The "context" is some user-defined context.
893  *      The "data" is the pointer to the user data in the node,
894  *      NOT the node itself.
895  *
896  *      It should return 0 if all is OK, and !0 for any error.
897  *      The walking will stop on any error.
898  *
899  *      Except with RBTREE_DELETE_ORDER, where the callback should return <0 for
900  *      errors, and may return 1 to delete the current node and halt,
901  *      or 2 to delete the current node and continue.  This may be
902  *      used to batch-delete select nodes from a locked rbtree.
903  */
904 int             rbtree_walk(rbtree_t *tree, rb_order_t order, rb_walker_t compare, void *context);
905
906 /*
907  *      FIFOs
908  */
909 typedef struct  fr_fifo_t fr_fifo_t;
910 typedef void (*fr_fifo_free_t)(void *);
911 fr_fifo_t       *fr_fifo_create(TALLOC_CTX *ctx, int max_entries, fr_fifo_free_t freeNode);
912 void            fr_fifo_free(fr_fifo_t *fi);
913 int             fr_fifo_push(fr_fifo_t *fi, void *data);
914 void            *fr_fifo_pop(fr_fifo_t *fi);
915 void            *fr_fifo_peek(fr_fifo_t *fi);
916 unsigned int    fr_fifo_num_elements(fr_fifo_t *fi);
917
918 /*
919  *      socket.c
920  */
921 int             fr_socket_client_unix(char const *path, bool async);
922 int             fr_socket_client_udp(fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr, uint16_t dst_port, bool async);
923 int             fr_socket_client_tcp(fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr, uint16_t dst_port, bool async);
924 int             fr_socket_wait_for_connect(int sockfd, struct timeval *timeout);
925
926 #ifdef __cplusplus
927 }
928 #endif
929
930 #include <freeradius-devel/packet.h>
931
932 #ifdef WITH_TCP
933 #  include <freeradius-devel/tcp.h>
934 #endif
935
936 #endif /*LIBRADIUS_H*/