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