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