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