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