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