aefb40de2ad7072da06807a76bfc4c095da6a673
[radsecproxy.git] / lib / radius / client.h
1 /*
2 Copyright (c) 2011, Network RADIUS SARL
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the <organization> nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /** \file client.h
29  *  \brief Main header file.
30  */
31
32 #ifndef _RADIUS_CLIENT_H_
33 #define _RADIUS_CLIENT_H_ 1
34
35 /*
36  *  System-specific header files.
37  */
38 #include <config.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #ifdef HAVE_STDINT_H
42 #include <stdint.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h>
46 #endif
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #endif
50 #include <stdarg.h>
51 #ifdef HAVE_NETDB_H
52 #include <netdb.h>
53 #endif
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
56 #endif
57 #ifdef HAVE_SYS_TIME_H
58 #include <sys/time.h>
59 #endif
60
61 #include <radsec/radsec.h>
62 #include <radsec/radsec-impl.h>
63 #include <radsec/radius.h>
64
65 /** \defgroup build Build Helpers
66  *
67  * These definitions give the GNU C compiler more information about
68  * the functions being compiled.  They are used to either remove
69  * warnings, or to enable better warnings.
70  **/
71
72 /** \defgroup custom Portability Functions
73  *
74  * These functions and definitions should be modified for your local
75  * system.  See the individual definitions for details.
76  */
77
78 /** \defgroup error Error handling
79  *
80  * These definitions and routines manage errors.
81  */
82
83 /** \defgroup value_pair Attribute manipulation
84  *
85  * These routines manage structures which map to attributes.
86  */
87
88 /**\defgroup dict Dictionary Lookup Functions
89  *
90  * \sa doc/dictionaries.txt
91  *
92  * The RADIUS dictionaries perform name to number mappings.  The names
93  * are used only for administrator convenience, for parsing
94  * configuration files, and printing humanly-readable output.  The
95  * numbers are used when encoding data in a packet.
96  *
97  * When attributes are decoded from a packet, the numbers are used to
98  * look up the associated name, which is then placed into a data
99  * structure.
100  *
101  * When the data structures are encoded into a packet, the numbers are
102  * used to create RFC and VSA format attributes.
103  *
104  * \attention The definitions, structures, and functions given below
105  * are useful only for implementing "low level" RADIUS
106  * functionality. There is usually no need to refer to them in a
107  * client application.  The library should be used at a higher level,
108  * which exposes a much simpler API.
109  */
110
111 /** \defgroup packet Packet manipulation
112  *
113  * These routines perform encoding and decoding of RADIUS packets.
114  */
115
116 /** \defgroup print Print / parse functions
117  *
118  * These routines convert the internal data structures to a printable
119  * form, or parse them.
120  */
121
122 /** \defgroup id ID allocation and freeing
123  *
124  *  These routines manage RADIUS ID allocation.
125  */
126
127 /** \defgroup attr Low-level attribute encode/decoding
128  *
129  * These routines perform "low level" encoding, decoding, sending, and
130  * reception of RADIUS attributes.  They are called by the \ref packet
131  * functions.
132  *
133  * \attention The structures and functions given below are useful only
134  * for implementing "low level" RADIUS functionality. There is usually
135  * no need to refer to them in a client application.  The library
136  * should be used at a higher level, which exposes a much simpler API.
137  */
138
139 /** \defgroup internal Internal support functions.
140  *
141  * These functions are required to perform internal or "low-level"
142  * data manipulation.  While they are exposed for completeness, they
143  * should not be called by any application.
144  */
145
146 #ifdef PW_EAP_MESSAGE
147 #ifndef PW_MESSAGE_AUTHENTICATOR
148 #error EAP-Message requires Message-Authenticator
149 #endif
150 #endif
151
152 #ifdef WITHOUT_OPENSSL
153 #ifndef RS_MD5_CTX
154 #error RS_MD5_CTX must be defined
155 #endif
156 #ifndef RS_MD5Init
157 #error n_rMD5Init must be defined
158 #endif
159 #ifndef RS_MD5Update
160 #error RS_MD5Updyae must be defined
161 #endif
162 #ifndef RS_MD5Final
163 #error RS_MD5Final must be defined
164 #endif
165 #ifndef RS_MD5Transform
166 #error RS_MD5Transform must be defined
167 #endif
168
169 #else  /* WITHOUT_OPENSSL */
170
171 #include <openssl/md5.h>
172 /** Define for compile-time selection of the MD5 functions.  Defaults to using the OpenSSL functions.  \ingroup custom */
173 #define RS_MD5_CTX      MD5_CTX
174 /** Define for compile-time selection of the MD5 functions.  Defaults to using the OpenSSL functions. \ingroup custom */
175 #define RS_MD5Init      MD5_Init
176 /** Define for compile-time selection of the MD5 functions.  Defaults to using the OpenSSL functions. \ingroup custom */
177 #define RS_MD5Update    MD5_Update
178 /** Define for compile-time selection of the MD5 functions.  Defaults to using the OpenSSL functions. \ingroup custom */
179 #define RS_MD5Final     MD5_Final
180 /** Define for compile-time selection of the MD5 functions.  Defaults to using the OpenSSL functions. \ingroup custom */
181 #define RS_MD5Transform MD5_Transform
182 #endif
183
184 #ifndef RS_MAX_PACKET_LEN
185 /** The maximum size of a packet that the library will send or receive.  \ingroup custom
186  *
187  *  The RFC requirement is to handle at least 4K packets.  However, if
188  *  you expect to only do username/password authentication, this value
189  *  can be set to a smaller value, such as 256.
190  *
191  *  Be warned that any packets larger than this value will be ignored
192  *  and silently discarded.
193  */
194 #define RS_MAX_PACKET_LEN (4096)
195 #endif
196
197 #ifndef RS_MAX_ATTRIBUTES
198 /** The maximum number of attributes that the library will allow in a packet.  \ingroup custom
199  *
200  *  Packets which contain more than ::RS_MAX_ATTRIBUTES will generate
201  *  an error.  This value is configurable because there may be a need
202  *  to accept a large mumber of attributes.
203  *
204  *  This value is ignored when packets are sent.  The library will
205  *  send as many attributes as it is told to send.
206  */
207 #define RS_MAX_ATTRIBUTES (200)
208 #endif
209
210 #undef RS_MAX_PACKET_CODE
211 /** The maximum RADIUS_PACKET::code which we can accept. \ingroup dict
212  *
213  *  \attention This should not be changed, as it is used by other
214  *  structures such as ::nr_packet_codes.
215  */
216 #define RS_MAX_PACKET_CODE PW_COA_NAK
217
218 /**  The maximum vendor number which is permitted. \ingroup dict
219  *
220  *  The RFCs require that the Vendor Id or Private Enterprise Number
221  *  be encoded as 32 bits, with the upper 8 bits being zero.
222  */
223 #define RS_MAX_VENDOR           (1 << 24)
224
225 /** Data Type Definitions. \ingroup dict
226  */
227 #define TAG_VALID(x)          ((x) < 0x20)
228
229 /** The attribute is not encrypted. */
230 #define FLAG_ENCRYPT_NONE            (0)
231
232 /** The attribute is encrypted using the RFC 2865 User-Password method */
233 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
234
235 /** The attribute is encrypted using the RFC 2868 Tunnel-Password method */
236 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
237
238 /** A set of flags which determine how the attribute should be handled.
239  *
240  * Most attributes are "normal", and do not require special handling.
241  * However, some require "encryption", tagging, or have other special
242  * formats.  This structure contains the various options for the
243  * attribute formats.
244  */
245 typedef struct attr_flags {
246         unsigned int            has_tag : 1; /**< Attribute has an RFC 2868 tag */
247         unsigned int            unknown : 1; /**< Attribute is unknown */
248 #ifdef RS_TYPE_TLV
249         unsigned int            has_tlv : 1; /* has sub attributes */
250         unsigned int            is_tlv : 1; /* is a sub attribute */
251 #endif
252         unsigned int            extended : 1; /* extended attribute */
253         unsigned int            extended_flags : 1; /* with flag */
254         unsigned int            evs : 1;            /* extended VSA */
255         uint8_t                 encrypt;      /**< Attribute encryption method */
256         uint8_t                 length;       /**< The expected length of the attribute */
257 } ATTR_FLAGS;
258
259
260 /** Defines an dictionary mapping for an attribute.  \ingroup dict
261  *
262  *  The RADIUS dictionaries map humanly readable names to protocol
263  *  numbers.  The protocol numbers are used to encode/decode the
264  *  attributes in a packet.
265  */
266 typedef struct nr_dict_attr {
267         unsigned int            attr;           /**< Attribute number  */
268         rs_attr_type_t          type;           /**< Data type */
269         unsigned int            vendor;         /**< Vendor-Id number  */
270         ATTR_FLAGS              flags;
271         const char              *name;          /**< Printable name  */
272 } DICT_ATTR;
273
274 /** Defines a dictionary mapping for a named enumeration.  \ingroup dict
275  *
276  *  This structure is currently not used.
277  */
278 typedef struct nr_dict_value {
279         const DICT_ATTR         *da;            /**< pointer to a ::DICT_ATTR  */
280         int                     value;          /**< enumerated value  */
281         char                    name[1];        /**< printable name  */
282 } DICT_VALUE;
283
284 /** Defines an dictionary mapping for a vendor.  \ingroup dict
285  *
286  *  The RADIUS dictionaries map humanly readable vendor names to a
287  *  Vendor-Id (or Private Enterprise Code) assigned by IANA.  The
288  *  Vendor-Id is used to encode/decode Vendor-Specific attributes in a
289  *  packet.
290  */
291 typedef struct nr_dict_vendor {
292         unsigned int            vendor; /**< Vendor Private Enterprise Code  */
293         size_t                  type;      /**< size of Vendor-Type field */
294         size_t                  length;    /**< size of Vendor-Length field */
295         const char              *name;          /**< Printable name  */
296 } DICT_VENDOR;
297
298 /** Union holding all possible types of data for a ::VALUE_PAIR. \ingroup value_pair
299  *
300  */
301 typedef union value_pair_data {
302         char                    strvalue[RS_MAX_STRING_LEN]; /* +1 for NUL */
303         uint8_t                 octets[253];
304         struct in_addr          ipaddr;
305         struct in6_addr         ipv6addr;
306         uint32_t                date;
307         uint32_t                integer;
308 #ifdef RS_TYPE_SIGNED
309         int32_t                 sinteger;
310 #endif
311 #ifdef RS_TYPE_ABINARY
312         uint8_t                 filter[32];
313 #endif
314         uint8_t                 ifid[8]; /* struct? */
315         uint8_t                 ipv6prefix[18]; /* struct? */
316 #ifdef RS_TYPE_TLV
317         uint8_t                 *tlv;
318 #endif
319 } VALUE_PAIR_DATA;
320
321
322 /** C structure version of a RADIUS attribute. \ingroup value_pair
323  *
324  * The library APIs use this structure to avoid depending on the
325  * details of the protocol.
326  */
327 typedef struct value_pair {
328         const DICT_ATTR         *da; /**< dictionary definition */
329         size_t                  length; /**< number of octets in the data */
330         int                     tag; /**< tag value if da->flags.has_tag */
331         struct value_pair       *next; /**< enables a linked list of values  */
332         VALUE_PAIR_DATA         data;  /**< the data of the attribute */
333 } VALUE_PAIR;
334 #define vp_strvalue   data.strvalue
335 #define vp_octets     data.octets
336 #define vp_ipv6addr   data.ipv6addr
337 #define vp_ifid       data.ifid
338 #define vp_ipv6prefix data.ipv6prefix
339 #define vp_ipaddr     data.ipaddr.s_addr
340 #define vp_date       data.integer
341 #define vp_integer    data.integer
342 #ifdef RS_TYPE_ABINARY
343 #define vp_filter     data.filter
344 #endif
345 #ifdef RS_TYPE_ETHER
346 #define vp_ether      data.ether
347 #endif
348 #ifdef RS_TYPE_SIGNED
349 #define vp_signed     data.sinteger
350 #endif
351 #ifdef RS_TYPE_TLV
352 #define vp_tlv        data.tlv
353 #endif
354
355 #ifdef RS_TYPE_TLV
356 #define RS_ATTR_MAX_TLV (4)
357 extern const int nr_attr_shift[RS_ATTR_MAX_TLV];
358 extern const int nr_attr_mask[RS_ATTR_MAX_TLV];
359 extern const unsigned int nr_attr_max_tlv;
360 #endif
361
362 /** A structure which describes a RADIUS packet. \ingroup packet
363  *
364  *  In general, it should not be necessary to refererence the elements
365  *  of this structure.
366  */
367 typedef struct radius_packet {
368         int                     sockfd; /** The socket descriptor */
369         struct sockaddr_storage src;    /**< The packet source address  */
370         struct sockaddr_storage dst;    /**< the packet destination address */
371         const char              *secret; /**< The shared secret */
372         size_t                  sizeof_secret; /**< Length of the shared secret */
373         unsigned int            code;   /**< The RADIUS Packet Code */
374         int                     id;     /**< The RADIUS Packet Id */
375         size_t                  length; /**< The RADIUS Packet Length.  This will be no larger than RADIUS_PACKET::sizeof_data */
376         uint8_t                 vector[16]; /**< A copy of the authentication vector */
377         int                     flags; /**< Internal flags.  Do not modify this field. */
378         int                     attempts; /**< The number of transmission attempt  */
379         uint8_t                 *data;    /**< The raw packet data  */
380         size_t                  sizeof_data; /**< size of the data buffer  */
381         VALUE_PAIR              *vps;   /**< linked list of ::VALUE_PAIR */
382 } RADIUS_PACKET;
383
384 #define RS_PACKET_ENCODED  (1 << 0)
385 #define RS_PACKET_HEADER   (1 << 1)
386 #define RS_PACKET_SIGNED   (1 << 2)
387 #define RS_PACKET_OK       (1 << 3)
388 #define RS_PACKET_VERIFIED (1 << 4)
389 #define RS_PACKET_DECODED  (1 << 5)
390
391
392 /** Track packets sent to a server. \ingroup id
393  *
394  * This data structure tracks Identifiers which are used to
395  * communicate with a particular destination server.  The application
396  * should call nr_server_init() to initialize it.  If necessary, the
397  * application should then call nr_server_set_ipv4() to open an IPv4
398  * socket to the server.
399  *
400  * If the RADIUS packets are being transported over an encapsulation
401  * layer (e.g. RADIUS over TLS), then nr_server_set_ipv4() does not
402  * need to be called.  The ::nr_server_t structure should instead be
403  * associated wih the TLS session / socket.
404  */
405 typedef struct nr_server_t {
406         int sockfd;             /**< socket for sending packets  */
407         int code;               /**< default value for the Code */
408
409         struct sockaddr_storage src; /**< Source address of the packet */
410         struct sockaddr_storage dst; /**< Destination address of the packet  */
411
412         /** The shared secret.
413          *
414          *  See also nr_packet_send() and nr_packet_recv().
415          */
416         const char      *secret;
417
418         /** The length of the shared secret.
419          *
420          *  See also nr_packet_send() and nr_packet_recv().
421          */
422         size_t          sizeof_secret;
423
424         int             used;   /**< Number of used IDs */
425
426         void            *free_list; /**< For managing packets */
427
428         RADIUS_PACKET   *ids[256]; /**< Pointers to "in flight" packets  */
429 } nr_server_t;
430
431
432 /** Return a printable error message. \ingroup error
433  *
434  *  This function returns a string describing the last error that
435  *  occurred.  These messages are intended for developers, and are not
436  *  suitable for display to an end user.  The application using this
437  *  library should instead produce a "summary" message when an error
438  *  occurs.  e.g. "Failed to receive a response", is better than
439  *  messages produced by this function, which contain text like
440  *  "invalid response authentication vector".  The first is
441  *  understandable, the second is not.
442  *
443  * @param[in] error   The error code (can be less than zero)
444  * @return            A printable string describing the error.
445  */
446 extern const char *nr_strerror(int error);
447
448 /** Allocate a ::VALUE_PAIR which refers to a ::DICT_ATTR.  \ingroup value_pair
449  *
450  *  This returned ::VALUE_PAIR has no data associated with it.  The
451  *  nr_vp_set_data() function must be called before placing the
452  *  ::VALUE_PAIR in a ::RADIUS_PACKET.
453  *
454  * @param[in] da       The ::DICT_ATTR associated with the ::VALUE_PAIR
455  * @return             The created ::VALUE_PAIR, or NULL on error.
456  */
457 extern VALUE_PAIR *nr_vp_alloc(const DICT_ATTR *da);
458
459 /** Free a ::VALUE_PAIR.  \ingroup value_pair
460  *
461  *  This function frees the ::VALUE_PAIR, and sets the head pointer to NULL.
462  *  If head refers to a ::VALUE_PAIR list, then all of the structures in the
463  *  list are freed.
464  *
465  * @param[in,out] head   The pointer to a ::VALUE_PAIR, or a ::VALUE_PAIR list.
466  */
467 extern void nr_vp_free(VALUE_PAIR **head);
468
469 /** Initializes a ::VALUE_PAIR from a ::DICT_ATTR \ingroup value_pair
470  *
471  *  This function assumes that the ::VALUE_PAIR points to existing
472  *  and writable memory.
473  *
474  * @param[in,out] vp   The ::VALUE_PAIR to be initialized
475  * @param[in] da       The ::DICT_ATTR used to initialize the ::VALUE_PAIR
476  * @return             The initialized  ::VALUE_PAIR, or NULL on error.
477  */
478 extern VALUE_PAIR *nr_vp_init(VALUE_PAIR *vp, const DICT_ATTR *da);
479
480 /** Allocate a ::VALUE_PAIR which refers to an unknown attribute.  \ingroup value_pair
481  *
482  *  It is used when an attribute is received, and that attribute does
483  *  not exist in the dictionaries.
484  *
485  *  The returned ::VALUE_PAIR has no data (i.e. VALUE_PAIR::length is
486  *  zero).  The nr_vp_set_data() function must be called before
487  *  placing the ::VALUE_PAIR in a ::RADIUS_PACKET.
488  *
489  * @param[in] attr     The attribute number, 0..2^16
490  * @param[in] vendor   The vendor number, 0..2^16
491  * @return             The created ::VALUE_PAIR, or NULL on error.
492  */
493 extern VALUE_PAIR *nr_vp_alloc_raw(unsigned int attr, unsigned int vendor);
494
495 /** Set the data associated with a previously allocated ::VALUE_PAIR.  \ingroup value_pair
496  *
497  *  If this function succeeds, VALUE_PAIR::length is no longer zero,
498  *  and the structure contains the data.
499  *
500  * @param[in,out] vp   The ::VALUE_PAIR to update
501  * @param[in] data     Data to set inside of the ::VALUE_PAIR
502  * @param[in] data_len Length of the data field
503  * @return             <0 on error, 0 for "data was truncated"
504  *                      >0 for "data successfully added"
505  */
506 extern int nr_vp_set_data(VALUE_PAIR *vp, const void *data, size_t data_len);
507
508 /** Create a ::VALUE_PAIR and set its data.  \ingroup value_pair
509  *
510  * @param[in] attr     The attribute number of the ::VALUE_PAIR to create
511  * @param[in] vendor   The vendor number of the ::VALUE_PAIR to create
512  * @param[in] data     Data to set inside of the ::VALUE_PAIR
513  * @param[in] data_len Length of the data field
514  * @return             The created ::VALUE_PAIR, or NULL on error.
515  */
516 extern VALUE_PAIR *nr_vp_create(int attr, int vendor, const void *data,
517                               size_t data_len);
518
519 /** Append a ::VALUE_PAIR to the end of a ::VALUE_PAIR list.  \ingroup value_pair
520  *
521  * @param[in,out] head The head of the ::VALUE_PAIR list.  May not be NULL.
522  * @param[in] vp       The ::VALUE_PAIR to append to the list.
523  */
524 extern void nr_vps_append(VALUE_PAIR **head, VALUE_PAIR *vp);
525
526 /** Search a ::VALUE_PAIR list for one of a given number.  \ingroup value_pair
527  *
528  * @param[in] head     The head of the ::VALUE_PAIR list to search.
529  * @param[in] attr     The attribute number of the ::VALUE_PAIR to find
530  * @param[in] vendor   The vendor number of the ::VALUE_PAIR to find
531  * @return             The found ::VALUE_PAIR, or NULL if it was not found.
532  */
533 extern VALUE_PAIR *nr_vps_find(VALUE_PAIR *head,
534                             unsigned int attr, unsigned int vendor);
535
536 /** Look up an attribute in the dictionaries.  \ingroup dict
537  *
538  *  The dictionary mapping contains information about the attribute,
539  *  such as printable name, data type (ipaddr, integer, etc), and
540  *  various other things used to encode/decode the attribute in a
541  *  packet.
542  *
543  *  \attention There is usually no need to call this function.  Use
544  *  the RS_DA_* definitions instead.
545  *
546  * @param[in] attr    Value of the attribute
547  * @param[in] vendor  Value of the vendor
548  * @return    NULL for "not found", or a pointer to the attribute mapping.
549  */
550 extern const DICT_ATTR *nr_dict_attr_byvalue(unsigned int attr,
551                                          unsigned int vendor);
552
553 /** Look up an attribute in the dictionaries.  \ingroup dict
554  *
555  *  The dictionary mapping contains information about the attribute,
556  *  such as printable name, data type (ipaddr, integer, etc), and
557  *  various other things used to encode/decode the attribute in a
558  *  packet.
559  *
560  *  \attention There is usually no need to call this function.
561  *
562  * @param[in] name    Name of the attribute
563  * @return    NULL for "not found", or a pointer to the attribute mapping.
564  */
565 extern const DICT_ATTR *nr_dict_attr_byname(const char *name);
566
567 /** Converts raw data to a ::DICT_ATTR structure.  \ingroup dict
568  *
569  *  It is called when the library is asked to decode an attribute
570  *  which is not in the pre-defined dictionaries.
571  *
572  *  \attention There is usually no need to call this function.
573  *
574  * @param[in,out] da      The ::DICT_ATTR structure to initialize
575  * @param[in]     attr    The attribute number
576  * @param[in]     vendor  The vendor number
577  * @param[in]     buffer  The buffer where the name of the attribute is stored
578  * @param[in]     bufsize Size of the buffer
579  * @return    <0 for error, 0 for success
580  */
581 extern int nr_dict_attr_2struct(DICT_ATTR *da,
582                                 unsigned int attr, unsigned int vendor,
583                                 char *buffer, size_t bufsize);
584
585 /**  Unused. \ngroup dict
586  *
587  */
588 extern const DICT_VALUE *nr_dict_value_byattr(unsigned int attr,
589                                         unsigned int vendor,
590                                         int value);
591
592 /**  Unused. \ngroup dict
593  *
594  */
595 const DICT_VALUE *nr_dict_value_byname(unsigned int attr,
596                                  unsigned int vendor,
597                                  const char *name);
598
599 /** Look up a vendor in the dictionaries.  \ingroup dict
600  *
601  *  The dictionary mapping contains information about the vendor, such
602  *  as printable name, VSA encoding method, etc.
603  *
604  *  \attention There is usually no need to call this function.
605  *  Applications do not need access to low-level RADIUS protocol
606  *  information.
607  *
608  * @param[in] name    Name of the vendor.
609  * @return    NULL for "not found", or a pointer to the vendor mapping.
610  */
611 extern int nr_dict_vendor_byname(const char *name);
612
613 /** Look up an vendor in the dictionaries.  \ingroup dict
614  *
615  *  The dictionary mapping contains information about the vendor, such
616  *  as printable name, VSA encoding method, etc.
617  *
618  *  \attention There is usually no need to call this function.
619  *
620  * @param[in] vendor Vendor-Id (or Private Enterprise code) for the vendor.
621  * @return    NULL for "not found", or a pointer to the vendor mapping.
622  */
623 extern const DICT_VENDOR *nr_dict_vendor_byvalue(unsigned int vendor);
624
625 /**  Static array of known vendors.  \ingroup dict
626  *
627  *  \attention This structure should only be accessed by internal RADIUS library
628  *  functions.
629  */
630 extern const DICT_VENDOR nr_dict_vendors[];
631
632 /** The number of attribute definitions in the dictionary.  \ingroup dict
633  *
634  *  This number is guaranteed to be at least 256, for speed.
635  *
636  *  \attention This variable should only be accessed by internal RADIUS library
637  *  functions.
638  */
639 extern const int nr_dict_num_attrs;
640
641 /** The list of attribute definitions.  \ingroup dict
642  *
643  *  The "standard" RFC attributes are located in the first 256
644  *  entries.  Standard attributes without a dictionary definition are
645  *  given an empty entry.
646  *
647  *  The attributes are orderd by (vendor, attribute), in increasing
648  *  order.  This allows the dictionary lookups to find attributes by a
649  *  binary search.
650  *
651  *  \attention This variable should only be accessed by internal RADIUS library
652  *  functions.
653  */
654 extern const DICT_ATTR nr_dict_attrs[];
655
656 /** The number of attributes with names.  \ingroup dict
657  *
658  *  \attention This variable should only be accessed by internal RADIUS library
659  *  functions.
660  */
661 extern const int nr_dict_num_names;
662
663 /** The list of attribute definitions, organized by name.  \ingroup dict
664  *
665  *  The attributes are orderd by name (case insensitive), in
666  *  increasing order.  This allows the dictionary lookups to find
667  *  attributes by a binary search.
668  *
669  *  \attention This variable should only be accessed by internal RADIUS library
670  *  functions.
671  */
672 extern const DICT_ATTR const *nr_dict_attr_names[];
673
674 /** Static array containing names the RADIUS_PACKET::code field.  \ingroup dict
675  *
676  *  The names are hard-coded and not in any dictionary because they do
677  *  not change.
678  *
679  *  The names are exported because they may be useful in your
680  *  application.  Packet codes which are not handled by the library
681  *  have NULL for their names.
682  */
683 extern const char *nr_packet_codes[RS_MAX_PACKET_CODE + 1];
684
685 /** Verifies that a packet is "well formed".  \ingroup packet
686  *
687  *  This function performs basic validation to see if the packet is
688  *  well formed.  It is automatically called by nr_packet_decode().
689  *
690  * @param[in] packet      A pointer to the ::RADIUS_PACKET data.
691  * @return                <0 means malformed, >= 0 means well-formed.
692  */
693 extern int nr_packet_ok(RADIUS_PACKET *packet);
694
695 /** Verifies that a packet is "well formed".  \ingroup packet
696  *
697  *  This function performs basic validation to see if the packet is
698  *  well formed.  You should normally use nr_packet_ok() instead of
699  *  this function.
700  *
701  * @param[in] data        A pointer to the raw packet data.
702  * @param[in] sizeof_data The length of the raw packet data
703  * @return                <0 means malformed, >= 0 means well-formed.
704  */
705 extern int nr_packet_ok_raw(const uint8_t *data, size_t sizeof_data);
706
707 /** Encodes a packet.  \ingroup packet
708  *
709  *  This function encodes a packet using the fields of the
710  *  ::RADIUS_PACKET structure.  The RADIUS_PACKET::code and
711  *  RADIUS_PACKET::id fields are used to fill in the relevant fields
712  *  of the raw (encoded) packet.  The RADIUS_PACKET::vps list is
713  *  walked to encode the attributes.  The packet is signed, if
714  *  required.
715  *
716  *  The raw packet is placed into the RADIUS_PACKET::data field, up to
717  *  RADIUS_PACKET::sizeof_data bytes.  the RADIUS_PACKET::length field
718  *  is updated with the length of the raw packet.  This field is
719  *  always less than, or equal to, the RADIUS_PACKET::size_data field.
720  *  If there is insufficient room to store all of the attributes, then
721  *  some attributes are silently discarded.
722  *
723  *  The RADIUS_PACKET::vector field is either calculated as part of
724  *  the signing process, or is initialized by this function to be a
725  *  random sequence of bytes.  That field should therefore be left
726  *  alone by the caller.
727  *
728  *  When the encoding has been successful, it sets the
729  *  RADIUS_PACKET::encoded field to non-zero.
730  *
731  *  In addition, all required attribute "encryption" is performed.
732  *
733  *  User-Password.  The vp_strvalue field is assumed to contain the
734  *  "clear-text" version of the password.  The encrypted version is
735  *  calculated, and placed in the packet.
736  *
737  *  CHAP-Password.  The vp_strvalue field is assumed to contain the
738  *  "clear-text" version of the password.  The encrypted version is
739  *  calculated, and placed in the packet.  If the RADIUS_PACKET::vps
740  *  list contains a CHAP-Challenge attribute, it is used.  Otherwise
741  *  the RADIUS_PACKET::vector field is used a the challenge.
742  *
743  *  Message-Authenticator.  The contents of the Message-Authenticator
744  *  in the RADIUS_PACKET::vps list are ignored.  Instead, a
745  *  "place-holder" is put into the packt.  Tthe correct value is
746  *  calculated and placed into the packet by nr_packet_sign().
747  *
748  *  The RADIUS_PACKET::vps list is left untouched by this function,
749  *  even when attribute encryption or signing is performed.  Any
750  *  VALUE_PAIR structures can therefore be taken from static "const"
751  *  variables.
752  *
753  * @param[in] packet   The RADIUS packet to encode.
754  * @param[in] original The original request, when encoding a response.
755  * @return             <0 on error, >= 0 on success.
756  */
757 extern int nr_packet_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
758
759 /** Decodes a packet.  \ingroup packet
760  *
761  *  This function decodes a packet from the RADIUS_PACKET::data field
762  *  into a sequence of ::VALUE_PAIR structures in the
763  *  RADIUS_PACKET::vps list.
764  *
765  * @param[in] packet   The RADIUS packet to decode.
766  * @param[in] original The original request, when decoding a response.
767  * @return             <0 on error, >= 0 on success.
768  */
769 extern int nr_packet_decode(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
770
771 /** Signs a packet so that it can be sent.  \ingroup packet
772  *
773  * This function calculates the Message-Authenticator (if required),
774  * and signs the packet.
775  *
776  * @param[in] packet   The RADIUS packet to sign.
777  * @param[in] original The original request, when signing a response.
778  * @return             <0 on error, >= 0 on success.
779  */
780 extern int nr_packet_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
781
782 /** Verifies that a packet is well-formed and contains the correct signature.  \ingroup packet
783  *
784  *  If "original" is specified, it also verifies that the packet is a
785  *  response to the original request, and that it has the correct
786  *  signature.
787  *
788  * @param[in] packet   The RADIUS packet to verify.
789  * @param[in] original The original request, when verifying a response.
790  * @return             <0 on error, >= 0 on success.
791  */
792 extern int nr_packet_verify(RADIUS_PACKET *packet,
793                             const RADIUS_PACKET *original);
794
795 /** Pretty-prints a hex dump of a RADIUS packet.  \ingroup packet print
796  *
797  *  This function is available only in debugging builds of the
798  *  library.  It is useful during development, but should not be used
799  *  in a production system.
800  *
801  *  The packet headers are printed individually, and each attribute is
802  *  printed as "type length data..."
803  *
804  * @param[in] packet   The RADIUS packet to print
805  */
806 extern void nr_packet_print_hex(RADIUS_PACKET *packet);
807
808
809 /** Return the given number of random bytes.  \ingroup custom
810  *
811  * This function should be replaced by one that is specific to your
812  * system.
813  *
814  *  This is a wrapper function which enables the library to be more
815  *  portable.
816  *
817  * @param[in] data      Location where the random bytes will be stored
818  * @param[in] data_len  Number of bytes to store
819  * @return              <0 on error, or the total number of bytes stored.
820  */
821 extern ssize_t nr_rand_bytes(uint8_t *data, size_t data_len);
822
823 /** Return a random 32-bit integer.  \ingroup custom
824  *
825  * This function should be replaced by one that is specific to your
826  * system.  The version supplied here just calls nr_rand_bytes() each
827  * time, which is slow.
828  *
829  *  This is a wrapper function which enables the library to be more
830  *  portable.
831  *
832  * @return An unsigned 32-bit random integer.
833  */
834 extern uint32_t nr_rand(void);
835
836 /** Add a time to the given ::struct timeval.  \ingroup custom
837  *
838  *  This is a wrapper function which enables the library to be more
839  *  portable.
840  *
841  *  @param[in,out] t       The timeval to which the time is added.
842  *  @param[in]     seconds Time in seconds to add
843  *  @param[in]     usec    Time in microseconds to add
844  */
845 extern void nr_timeval_add(struct timeval *t, unsigned int seconds,
846                            unsigned int usec);
847
848 /** Compare two times.  \ingroup custom
849  *
850  *  This is a wrapper function which enables the library to be more
851  *  portable.
852  *
853  * @param[in] a One timeval
854  * @param[in] b Another one
855  * @return a <=> b
856  */
857 extern int nr_timeval_cmp(const struct timeval *a, const struct timeval *b);
858
859 /** Initializes an ::nr_server_t.  \ingroup id
860  *
861  * @param[in,ut] s      The ::nr_server_t to initialize
862  * @param[in]    code   The packet code used for packets sent to this server
863  * @param[in]    secret The shared secret used for packet sent to this server
864  * @return <0 for error, >= 0 for success
865  */
866 extern int nr_server_init(nr_server_t *s, int code, const char *secret);
867
868 /** Closes an ::nr_server_t data structure.  \ingroup id
869  *
870  *  Ensures that all IDs are free, and closes the socket.
871  *
872  * @param[in] s      The server structure to close.
873  * @return <0 for error, 0 for success
874  */
875 extern int nr_server_close(const nr_server_t *s);
876
877 /** Allocate a RADIUS_PACKET::id value for sending a packet to a server. \ingroup id
878  *
879  * This function allocates a RADIUS_PACKET::id from the ::nr_server_t
880  * structure.  It also fills in the RADIUS_PACKET::sockfd,
881  * RADIUS_PACKET::code, and RADIUS_PACKET::dst fields.
882  *
883  * @param[in] s      The server structure which tracks the ID
884  * @param[in] packet The packet which needs an ID
885  * @return <0 for error, 0 for success
886  */
887 extern int nr_server_id_alloc(nr_server_t *id, RADIUS_PACKET *packet);
888
889 /** Re-allocate a RADIUS_PACKET::id value for sending a packet to a server. \ingroup id
890  *
891  *  It is used when retransmitting an Accounting-Request packet to a
892  *  server, after updating the Acct-Delay-Time field.  The "realloc"
893  *  name means that the new ID is allocated, and is guaranteed to be
894  *  different from the old one.
895  *
896  * @param[in] s      The server structure which tracks the ID
897  * @param[in] packet The packet which needs a new ID
898  * @return <0 for error, 0 for success
899  */
900 extern int nr_server_id_realloc(nr_server_t *id, RADIUS_PACKET *packet);
901
902 /** Free a RADIUS_PACKET::id value after sending a packet to a server. \ingroup id
903  *
904  * @param[in] s      The server structure which tracks the ID
905  * @param[in] packet The packet which has an ID, and wants to free it
906  * @return <0 for error, 0 for success
907  */
908 extern int nr_server_id_free(nr_server_t *id, RADIUS_PACKET *packet);
909
910
911 /** Allocates a packet using malloc(), and initializes it. \ingroup id
912  *
913  * @param[in] s             The server structure
914  * @param[in,out] packet_p  Pointer to the ::RADIUS_PACKET to be allocated
915  * @return <0 for error, 0 for success
916  */
917 extern int nr_server_packet_alloc(const nr_server_t *s, RADIUS_PACKET **packet_p);
918
919 /**  Record a humanly readable error message. \ingroup error
920  *
921  *  \attention This structure should only be accessed by internal
922  *  RADIUS library functions.
923  *
924  * @param[in] fmt   The format to use.
925  */
926 extern void nr_strerror_printf(const char *fmt, ...);
927
928 #ifndef NDEBUG
929 #define nr_debug_error nr_strerror_printf /** \ingroup error */
930 #else
931 #define nr_debug_error if (0) nr_strerror_printf
932 #endif
933
934 /**  Encrypts or decrypts a User-Password attribute. \ingroup internal
935  *
936  *  \attention This structure should only be accessed by internal
937  *  RADIUS library functions.
938  *
939  * @param[out] output   Buffer where the password is stored
940  * @param[out] outlen   Size of the output buffer
941  * @param[in]  input    Input buffer with password
942  * @param[in]  inlen    Length of the input buffer
943  * @param[in]  secret   The shared secret
944  * @param[in]  vector   Authentication vector
945  * @return <0 on error, or the length of data in "output"
946  */
947 extern ssize_t nr_password_encrypt(uint8_t *output, size_t outlen,
948                                    const uint8_t *input, size_t inlen,
949                                    const char *secret, const uint8_t *vector);
950
951 /**  Encrypts a Tunnel-Password attribute. \ingroup internal
952  *
953  *  \attention This structure should only be accessed by internal
954  *  RADIUS library functions.
955  *
956  * @param[out] output   Buffer where the password is stored
957  * @param[out] outlen   Size of the output buffer
958  * @param[in]  input    Input buffer with password
959  * @param[in]  inlen    Length of the input buffer
960  * @param[in]  secret   The shared secret
961  * @param[in]  vector   Authentication vector
962  * @return <0 on error, or the length of data in "output"
963  */
964 extern ssize_t nr_tunnelpw_encrypt(uint8_t *output, size_t outlen,
965                                    const uint8_t *input, size_t inlen,
966                                    const char *secret, const uint8_t *vector);
967
968 /**  Decrypts a Tunnel-Password attribute. \ingroup internal
969  *
970  *
971  *  \attention This structure should only be accessed by internal
972  *  RADIUS library functions.
973  *
974  * @param[out] output   Buffer where the password is stored
975  * @param[out] outlen   Size of the output buffer
976  * @param[in]  input    Input buffer with password
977  * @param[in]  inlen    Length of the input buffer
978  * @param[in]  secret   The shared secret
979  * @param[in]  vector   Authentication vector
980  * @return <0 on error, or the length of data in "output"
981  */
982 extern ssize_t nr_tunnelpw_decrypt(uint8_t *output, size_t outlen,
983                                    const uint8_t *input, size_t inlen,
984                                    const char *secret, const uint8_t *vector);
985
986 /**  Calculates an HMAC-MD5. \ingroup internal
987  *
988  * @param[in] data      Data to be hashed
989  * @param[in] data_len  Length of data to be hashed
990  * @param[in] key       Key for the HMAC
991  * @param[in] key_len   Length of the key
992  * @param[out] digest
993  */
994 extern void nr_hmac_md5(const uint8_t *data, size_t data_len,
995                         const uint8_t *key, size_t key_len,
996                         uint8_t digest[16]);
997
998 /** Checks if a TLV is properly formatted. \ingroup internal
999  *
1000  *  \attention This structure should only be accessed by internal
1001  *  RADIUS library functions.
1002  *
1003  * @param[in] data      Data to check
1004  * @param[in] length    Length of the data field
1005  * @param[in] dv_type   Length of the TLV "type" field
1006  * @param[in] dv_length Length of the TLV "length" field
1007  * @return             <0 on error, 0 for "TLV is OK"
1008  */
1009 extern int nr_tlv_ok(const uint8_t *data, size_t length,
1010                       size_t dv_type, size_t dv_length);
1011
1012 /** A callback function used by nr_packet_walk().  \ingroup packet
1013  *
1014  *  The function should return 0 on success (i.e. keep walking), and
1015  *  otherwise a negative number indicating an error code
1016  *  (::nr_error_t).  That negative number will be used as the return
1017  *  code for nr_packet_walk().
1018  */
1019 typedef int (*nr_packet_walk_func_t)(void *, const DICT_ATTR *, const uint8_t *, size_t);
1020
1021 /** Walks over all attributes in a packet. \ingroup packet
1022  *
1023  *  This function is an iterator which calls a user-supplied callback
1024  *  function for each attribute in the packet.  It should be used
1025  *  instead of manually walking over the attributes.  There are a
1026  *  number of odd corner cases when handling Vendor-Specific
1027  *  attributes, and it is easy to get those corner cases wrong.
1028  *
1029  *  This function iterates over *all* attributes, including nested
1030  *  VSAs.  That is its main value.
1031  *
1032  *  Encrypted attributes such as User-Password are not decrypted.
1033  *
1034  * @param[in] packet    The packet containing the data
1035  * @param[in] ctx       A user-supplied context.  May be NULL
1036  * @param[in] callback  The callback function where the information is passed.
1037  *
1038  * @return <0 for error,
1039  *          0 for success.
1040  */
1041 extern int nr_packet_walk(RADIUS_PACKET *packet, void *ctx,
1042                           nr_packet_walk_func_t callback);
1043
1044 /** Initialize a packet
1045  *
1046  *  If original is specified, the packet is initialized as a response
1047  *  to the original request.
1048  *
1049  * @param[in,out] packet  The packet to initialize
1050  * @param[in] original    The original request (if any) to use as a template
1051  * @param[in] secret      Shared secret
1052  * @param[in] code        RADIUS Code field.
1053  * @param[in] data        Buffer where packets will be stored (RADIUS_PACKET::data)
1054  * @param[in] sizeof_data Size of buffer (RADIUS_PACKET::sizeof_data)
1055  * @return  <0 on error, 0 for success.
1056  */
1057 extern int nr_packet_init(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1058                           const char *secret, int code,
1059                           void *data, size_t sizeof_data);
1060
1061 /** Add one attribute to the packet.
1062  *
1063  *  This function can be used to add "raw" data to a packet.  It
1064  *  allows the caller to extend the RADIUS packet without using a
1065  *  ::VALUE_PAIR data structure.
1066  *
1067  *  Some attributes are handled specially by this function.
1068  *
1069  *  EAP-Message.  This attribute is automatically split into 253-octet
1070  *  chunks.
1071  *
1072  *  User-Password, CHAP-Password, and Message-Authenticator.  These
1073  *  attributes are automatically encrypted, as is done by
1074  *  nr_packet_encode().
1075  *
1076  * @param[in] packet   The packet to edit
1077  * @param[in] original The original request (if any)
1078  * @param[in] da       Pointer to the attribute definition
1079  * @param[in] data     Data to append to the packet
1080  * @param[in] data_len Length of data to append to the packet
1081  *
1082  * @return <0 for error, >= 0 for "successfully appended data"
1083  *  The function returns the number of octets appended to the packet.
1084  */
1085 extern ssize_t nr_packet_attr_append(RADIUS_PACKET *packet,
1086                                      const RADIUS_PACKET *original,
1087                                      const DICT_ATTR *da,
1088                                      const void *data, size_t data_len);
1089
1090
1091 /** Encodes any ::VALUE_PAIR into an attribute.  \ingroup attr
1092  *
1093  *  This function can be called for any ::VALUE_PAIR.  It will examine
1094  *  that structure, and call one of nr_vp2rfc() or nr_vp2vsa() as
1095  *  necessary.
1096  *
1097  * \attention This function should not be called.
1098  *
1099  * @param[in] packet   Where to place the encoded attribute.
1100  * @param[in] original The original request (optional), if "packet" is a response
1101  * @param[in,out] pvp  The ::VALUE_PAIR to encode.  On any return >=0, it is updated to point to the "next" ::VALUE_PAIR which should be encoded.
1102  * @param[in] data     Where the attribute is to be encoded.
1103  * @param[in] room     How many octets are available for attribute encoding.
1104  *
1105  * @return <0 for error, or the number of octets used to encode the attribute.
1106  */
1107 extern ssize_t nr_vp2attr(const RADIUS_PACKET *packet,
1108                       const RADIUS_PACKET *original,
1109                       const VALUE_PAIR **pvp, uint8_t *data, size_t room);
1110
1111 /** Encodes an RFC "standard" ::VALUE_PAIR into an attribute.  \ingroup attr
1112  *
1113  *  \attention This function should not be called.
1114  *
1115  * @param[in] packet   Where to place the encoded attribute.
1116  * @param[in] original The original request (optional), if "packet" is a response
1117  * @param[in,out] pvp  The ::VALUE_PAIR to encode.  On any return >=0, it is updated to point to the "next" ::VALUE_PAIR which should be encoded.
1118  * @param[in] data      Where the attribute is to be encoded.
1119  * @param[in] room     How many octets are available for attribute encoding.
1120  *
1121  * @return <0 for error, or the number of octets used to encode the attribute.
1122  */
1123 extern ssize_t nr_vp2rfc(const RADIUS_PACKET *packet,
1124                      const RADIUS_PACKET *original,
1125                      const VALUE_PAIR **pvp,
1126                      uint8_t *data, size_t room);
1127
1128 /** Decodes any attribute into a ::VALUE_PAIR.  \ingroup attr
1129  *
1130  *  \attention This function should not be called.
1131  *
1132  * @param[in] packet   The packet containing the attribute to be decoded.
1133  * @param[in] original The original request (optional), if "packet" is a response
1134  * @param[out] pvp     Where to place the decoded ::VALUE_PAIR.  On any return >=0, it is updated to point to the ::VALUE_PAIR which was decoded from the packet.
1135  * @param[in] data     Where the attribute is to be encoded.
1136  * @param[in] length   How many octets are available for attribute decoding.
1137  *
1138  * @return <0 for error, or the number of octets used to decode the attribute.
1139  */
1140 extern ssize_t nr_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1141                             const uint8_t *data, size_t length,
1142                             VALUE_PAIR **pvp);
1143
1144 /** Decodes an RFC "standard" attribute into a ::VALUE_PAIR.  \ingroup attr
1145  *
1146  *  \attention This function should not be called.
1147  *
1148  * @param[in] packet   The packet containing the attribute to be decoded.
1149  * @param[in] original The original request (optional), if "packet" is a response
1150  * @param[out] pvp     Where to place the decoded ::VALUE_PAIR.  On any return >=0, it is updated to point to the ::VALUE_PAIR which was decoded from the packet.
1151  * @param[in] data     Where the attribute is to be encoded.
1152  * @param[in] length   How many octets are available for attribute decoding.
1153  *
1154  * @return <0 for error, or the number of octets used to decode the attribute.
1155  */
1156 extern ssize_t nr_attr2vp_rfc(const RADIUS_PACKET *packet,
1157                         const RADIUS_PACKET *original,
1158                         const uint8_t *data, size_t length,
1159                         VALUE_PAIR **pvp);
1160
1161 /** Decodes a Vendor-Specific attribute into a ::VALUE_PAIR.  \ingroup attr
1162  *
1163  *  \attention This function should not be called.
1164  *
1165  * @param[in] packet   The packet containing the attribute to be decoded.
1166  * @param[in] original The original request (optional), if "packet" is a response
1167  * @param[out] pvp     Where to place the decoded ::VALUE_PAIR.  On any return >=0, it is updated to point to the ::VALUE_PAIR which was decoded from the packet.
1168  * @param[in] data     Where the attribute is to be encoded.
1169  * @param[in] length   How many octets are available for attribute decoding.
1170  *
1171  * @return <0 for error, or the number of octets used to decode the attribute.
1172  */
1173 extern ssize_t nr_attr2vp_vsa(const RADIUS_PACKET *packet,
1174                         const RADIUS_PACKET *original,
1175                         const uint8_t *data, size_t length,
1176                         VALUE_PAIR **pvp);
1177
1178 /** Decodes an attribute with an unexpected length into a ::VALUE_PAIR.  \ingroup attr
1179  *
1180  *  \attention This function should not be called.
1181  *
1182  * @param[in] packet   The packet containing the attribute to be decoded.
1183  * @param[in] original The original request (optional), if "packet" is a response
1184  * @param[out] pvp     Where to place the decoded ::VALUE_PAIR.  On any return >=0, it is updated to point to the ::VALUE_PAIR which was decoded from the packet.
1185  * @param[in] data     Where the attribute is to be encoded.
1186  * @param[in] length   How many octets are available for attribute decoding.
1187  *
1188  * @return <0 for error, or the number of octets used to decode the attribute.
1189  */
1190 extern ssize_t nr_attr2vp_raw(const RADIUS_PACKET *packet,
1191                         const RADIUS_PACKET *original,
1192                         const uint8_t *data, size_t length,
1193                         VALUE_PAIR **pvp);
1194
1195 /** Encodes a Vendor-Specific ::VALUE_PAIR into an attribute.
1196  *
1197  *  \attention This function should not be called.
1198  *
1199  * @param[in] packet   Where to place the encoded attribute.
1200  * @param[in] original The original request (optional), if "packet" is a response
1201  * @param[in,out] pvp  The ::VALUE_PAIR to encode.  On any return >=0, it is updated to point to the "next" ::VALUE_PAIR which should be encoded.
1202  * @param[in] data     Where the attribute is to be encoded.
1203  * @param[in] room     How many octets are available for attribute encoding.
1204  *
1205  * @return <0 for error, or the number of octets used to encode the attribute.
1206  */
1207 extern ssize_t nr_vp2vsa(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1208                      const VALUE_PAIR **pvp, uint8_t *data,
1209                      size_t room);
1210
1211 /** Returns raw data from the RADIUS packet, for a given attribute. \ingroup attr
1212  *
1213  *  This function can be called repeatedly to find all instances of a
1214  *  given attribute.  The first time it is called, the "start"
1215  *  parameter should be zero.  If the function returns a non-zero
1216  *  positive number, it means that there *may* be more attributes
1217  *  available.  The returned value should be then passed via the
1218  *  "start" option in any subsequent calls to the function.
1219  *
1220  *  This function should be called by an application when it wants
1221  *  access to data which is not in the pre-defined dictionaries.
1222  *
1223  * @param[in] packet   The packet containing the attribute.
1224  * @param[in] start    Where in the packet we start searching for the attribute.
1225  * @param[in] attr     Value of the attribute to search for
1226  * @param[in] vendor   Value of the vendor (use 0 for IETF attributes)
1227  * @param[out] pdata   Pointer to the data.  If no data was found, the pointer is unchanged.
1228  * @param[out] plength  Length of the data.  If no data was found, the value pointed to is unchanged.
1229  *
1230  * @return <0 for error,
1231  *          0 for "no attribute found, stop searching"
1232  *         >0 offset where the attribute was found.
1233  */
1234 extern ssize_t nr_attr2data(const RADIUS_PACKET *packet, ssize_t start,
1235                              unsigned int attr, unsigned int vendor,
1236                              const uint8_t **pdata, size_t *plength);
1237
1238 /**  Pretty-print the entire ::VALUE_PAIR \ingroup print
1239  *
1240  *  All data is printed in ASCII format.  The data type of "octets" is
1241  *  printed as a hex string (e.g. 0xabcdef01...).  The data type of
1242  *  "ipaddr" is printed as a dotted-quad (e.g. 192.0.2.15).
1243  *
1244  *  The format is "Attribute-Name = value"
1245  *
1246  * @param[out] buffer  Where the printable version of the ::VALUE_PAIR is stored
1247  * @param[in]  bufsize size of the output buffer
1248  * @param[in]  vp      ::VALUE_PAIR to print
1249  * @return   length of data in buffer
1250  */
1251 extern size_t nr_vp_snprintf(char *buffer, size_t bufsize, const VALUE_PAIR *vp);
1252
1253 /**  Pretty-print the VALUE_PAIR::data field \ingroup print
1254  *
1255  *  Prints the value of a ::VALUE_PAIR, without the name or "=" sign.
1256  *
1257  * @param[out] buffer  Where the printable version of the ::VALUE_PAIR is stored
1258  * @param[in]  bufsize size of the output buffer
1259  * @param[in]  vp      ::VALUE_PAIR to print
1260  * @return   length of data in buffer
1261  */
1262 extern size_t nr_vp_snprintf_value(char *buffer, size_t bufsize, const VALUE_PAIR *vp);
1263
1264 /** Prints a list of :VALUE_PAIR structures to the given output. \ingroup print
1265  *
1266  * @param[in] fp   Where to print the results
1267  * @param[in] vps  Linked list of ::VALUE_PAIR to print
1268  */
1269 extern void nr_vp_fprintf_list(FILE *fp, const VALUE_PAIR *vps);
1270
1271 /** Scan a string into a ::VALUE_PAIR.  The counterpart to
1272  * nr_vp_snprintf_value() \ingroup print
1273  *
1274  * @param[in] string  Printable version of the ::VALUE_PAIR
1275  * @param[out] pvp    Newly allocated ::VALUE_PAIR
1276  * @return <0 on error, 0 for success.
1277  */
1278 extern int nr_vp_sscanf(const char *string, VALUE_PAIR **pvp);
1279
1280 /** Scan the data portion of a ::VALUE_PAIR.  The counterpart to
1281  * nr_vp_snprintf_value() \ingroup print
1282  *
1283  * @param[in,out] vp    The ::VALUE_PAIR where the data will be stored
1284  * @param[in]     value The string version of the data to be parsed
1285  * @return             <0 on error, >=0 for the number of characters parsed in value.
1286  */
1287 extern ssize_t nr_vp_sscanf_value(VALUE_PAIR *vp, const char *value);
1288
1289 #if defined(__GNUC__)
1290 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
1291 # define NEVER_RETURNS __attribute__ ((noreturn))
1292 # define UNUSED __attribute__ ((unused))
1293 # define BLANK_FORMAT " "       /* GCC_LINT whines about empty formats */
1294 #else
1295
1296 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1297  *
1298  */
1299 # define PRINTF_LIKE(n)
1300
1301 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1302  *
1303  */
1304 # define NEVER_RETURNS
1305
1306 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1307  *
1308  */
1309 # define UNUSED
1310
1311 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1312  *
1313  */
1314 # define BLANK_FORMAT ""
1315 #endif
1316
1317 #endif /* _RADIUS_CLIENT_H_ */