2 Copyright (c) 2011, Network RADIUS SARL
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.
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.
29 * \brief Main header file.
32 #ifndef _RADIUS_CLIENT_H_
33 #define _RADIUS_CLIENT_H_ 1
36 * System-specific header files.
45 #include <netinet/in.h>
48 #include <radsec/radsec.h>
51 * Definitions of attributes.
53 #include <radsec/radius.h>
55 /** \defgroup build Build Helpers
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.
62 /** \defgroup custom Portability Functions
64 * These functions and definitions should be modified for your local
65 * system. See the individual definitions for details.
68 /** \defgroup error Error handling
70 * These definitions and routines manage errors.
73 /** \defgroup value_pair Attribute manipulation
75 * These routines manage structures which map to attributes.
78 /**\defgroup dict Dictionary Lookup Functions
80 * \sa doc/dictionaries.txt
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.
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
91 * When the data structures are encoded into a packet, the numbers are
92 * used to create RFC and VSA format attributes.
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.
101 /** \defgroup packet Packet manipulation
103 * These routines perform encoding and decoding of RADIUS packets.
106 /** \defgroup print Print / parse functions
108 * These routines convert the internal data structures to a printable
109 * form, or parse them.
112 /** \defgroup id ID allocation and freeing
114 * These routines manage RADIUS ID allocation.
117 /** \defgroup attr Low-level attribute encode/decoding
119 * These routines perform "low level" encoding, decoding, sending, and
120 * reception of RADIUS attributes. They are called by the \ref packet
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.
129 /** \defgroup internal Internal support functions.
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.
136 #ifdef PW_EAP_MESSAGE
137 #ifndef PW_MESSAGE_AUTHENTICATOR
138 #error EAP-Message requires Message-Authenticator
142 #ifdef WITHOUT_OPENSSL
144 #error RS_MD5_CTX must be defined
147 #error n_rMD5Init must be defined
150 #error RS_MD5Updyae must be defined
153 #error RS_MD5Final must be defined
155 #ifndef RS_MD5Transform
156 #error RS_MD5Transform must be defined
159 #else /* WITHOUT_OPENSSL */
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
174 #ifndef RS_MAX_PACKET_LEN
175 /** The maximum size of a packet that the library will send or receive. \ingroup custom
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.
181 * Be warned that any packets larger than this value will be ignored
182 * and silently discarded.
184 #define RS_MAX_PACKET_LEN (4096)
187 #ifndef RS_MAX_ATTRIBUTES
188 /** The maximum number of attributes that the library will allow in a packet. \ingroup custom
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.
194 * This value is ignored when packets are sent. The library will
195 * send as many attributes as it is told to send.
197 #define RS_MAX_ATTRIBUTES (200)
200 #undef RS_MAX_PACKET_CODE
201 /** The maximum RADIUS_PACKET::code which we can accept. \ingroup dict
203 * \attention This should not be changed, as it is used by other
204 * structures such as ::nr_packet_codes.
206 #define RS_MAX_PACKET_CODE PW_COA_NAK
208 /** The maximum vendor number which is permitted. \ingroup dict
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.
213 #define RS_MAX_VENDOR (1 << 24)
215 /** Data Type Definitions. \ingroup dict
217 #define TAG_VALID(x) ((x) < 0x20)
219 /** The attribute is not encrypted. */
220 #define FLAG_ENCRYPT_NONE (0)
222 /** The attribute is encrypted using the RFC 2865 User-Password method */
223 #define FLAG_ENCRYPT_USER_PASSWORD (1)
225 /** The attribute is encrypted using the RFC 2868 Tunnel-Password method */
226 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
228 /** A set of flags which determine how the attribute should be handled.
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
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 */
239 unsigned int has_tlv : 1; /* has sub attributes */
240 unsigned int is_tlv : 1; /* is a sub attribute */
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 */
250 /** Defines an dictionary mapping for an attribute. \ingroup dict
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.
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 */
261 const char *name; /**< Printable name */
264 /** Defines a dictionary mapping for a named enumeration. \ingroup dict
266 * This structure is currently not used.
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 */
274 /** Defines an dictionary mapping for a vendor. \ingroup dict
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
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 */
288 /** Union holding all possible types of data for a ::VALUE_PAIR. \ingroup value_pair
291 typedef union value_pair_data {
292 char strvalue[RS_MAX_STRING_LEN]; /* +1 for NUL */
294 struct in_addr ipaddr;
295 struct in6_addr ipv6addr;
298 #ifdef RS_TYPE_SIGNED
301 #ifdef RS_TYPE_ABINARY
304 uint8_t ifid[8]; /* struct? */
305 uint8_t ipv6prefix[18]; /* struct? */
312 /** C structure version of a RADIUS attribute. \ingroup value_pair
314 * The library APIs use this structure to avoid depending on the
315 * details of the protocol.
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 */
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
336 #define vp_ether data.ether
338 #ifdef RS_TYPE_SIGNED
339 #define vp_signed data.sinteger
342 #define vp_tlv data.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;
352 /** A structure which describes a RADIUS packet. \ingroup packet
354 * In general, it should not be necessary to refererence the elements
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 */
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)
382 /** Track packets sent to a server. \ingroup id
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.
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.
395 typedef struct nr_server_t {
396 int sockfd; /**< socket for sending packets */
397 int code; /**< default value for the Code */
399 struct sockaddr_storage src; /**< Source address of the packet */
400 struct sockaddr_storage dst; /**< Destination address of the packet */
402 /** The shared secret.
404 * See also nr_packet_send() and nr_packet_recv().
408 /** The length of the shared secret.
410 * See also nr_packet_send() and nr_packet_recv().
412 size_t sizeof_secret;
414 int used; /**< Number of used IDs */
416 void *free_list; /**< For managing packets */
418 RADIUS_PACKET *ids[256]; /**< Pointers to "in flight" packets */
422 /** Return a printable error message. \ingroup error
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.
433 * @param[in] error The error code (can be less than zero)
434 * @return A printable string describing the error.
436 extern const char *nr_strerror(int error);
438 /** Allocate a ::VALUE_PAIR which refers to a ::DICT_ATTR. \ingroup value_pair
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.
444 * @param[in] da The ::DICT_ATTR associated with the ::VALUE_PAIR
445 * @return The created ::VALUE_PAIR, or NULL on error.
447 extern VALUE_PAIR *nr_vp_alloc(const DICT_ATTR *da);
449 /** Free a ::VALUE_PAIR. \ingroup value_pair
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
455 * @param[in,out] head The pointer to a ::VALUE_PAIR, or a ::VALUE_PAIR list.
457 extern void nr_vp_free(VALUE_PAIR **head);
459 /** Initializes a ::VALUE_PAIR from a ::DICT_ATTR \ingroup value_pair
461 * This function assumes that the ::VALUE_PAIR points to existing
462 * and writable memory.
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.
468 extern VALUE_PAIR *nr_vp_init(VALUE_PAIR *vp, const DICT_ATTR *da);
470 /** Allocate a ::VALUE_PAIR which refers to an unknown attribute. \ingroup value_pair
472 * It is used when an attribute is received, and that attribute does
473 * not exist in the dictionaries.
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.
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.
483 extern VALUE_PAIR *nr_vp_alloc_raw(unsigned int attr, unsigned int vendor);
485 /** Set the data associated with a previously allocated ::VALUE_PAIR. \ingroup value_pair
487 * If this function succeeds, VALUE_PAIR::length is no longer zero,
488 * and the structure contains the data.
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"
496 extern int nr_vp_set_data(VALUE_PAIR *vp, const void *data, size_t data_len);
498 /** Create a ::VALUE_PAIR and set its data. \ingroup value_pair
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.
506 extern VALUE_PAIR *nr_vp_create(int attr, int vendor, const void *data,
509 /** Append a ::VALUE_PAIR to the end of a ::VALUE_PAIR list. \ingroup value_pair
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.
514 extern void nr_vps_append(VALUE_PAIR **head, VALUE_PAIR *vp);
516 /** Search a ::VALUE_PAIR list for one of a given number. \ingroup value_pair
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.
523 extern VALUE_PAIR *nr_vps_find(VALUE_PAIR *head,
524 unsigned int attr, unsigned int vendor);
526 /** Look up an attribute in the dictionaries. \ingroup dict
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
533 * \attention There is usually no need to call this function. Use
534 * the RS_DA_* definitions instead.
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.
540 extern const DICT_ATTR *nr_dict_attr_byvalue(unsigned int attr,
541 unsigned int vendor);
543 /** Look up an attribute in the dictionaries. \ingroup dict
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
550 * \attention There is usually no need to call this function.
552 * @param[in] name Name of the attribute
553 * @return NULL for "not found", or a pointer to the attribute mapping.
555 extern const DICT_ATTR *nr_dict_attr_byname(const char *name);
557 /** Converts raw data to a ::DICT_ATTR structure. \ingroup dict
559 * It is called when the library is asked to decode an attribute
560 * which is not in the pre-defined dictionaries.
562 * \attention There is usually no need to call this function.
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
571 extern int nr_dict_attr_2struct(DICT_ATTR *da,
572 unsigned int attr, unsigned int vendor,
573 char *buffer, size_t bufsize);
575 /** Unused. \ngroup dict
578 extern const DICT_VALUE *nr_dict_value_byattr(unsigned int attr,
582 /** Unused. \ngroup dict
585 const DICT_VALUE *nr_dict_value_byname(unsigned int attr,
589 /** Look up a vendor in the dictionaries. \ingroup dict
591 * The dictionary mapping contains information about the vendor, such
592 * as printable name, VSA encoding method, etc.
594 * \attention There is usually no need to call this function.
595 * Applications do not need access to low-level RADIUS protocol
598 * @param[in] name Name of the vendor.
599 * @return NULL for "not found", or a pointer to the vendor mapping.
601 extern int nr_dict_vendor_byname(const char *name);
603 /** Look up an vendor in the dictionaries. \ingroup dict
605 * The dictionary mapping contains information about the vendor, such
606 * as printable name, VSA encoding method, etc.
608 * \attention There is usually no need to call this function.
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.
613 extern const DICT_VENDOR *nr_dict_vendor_byvalue(unsigned int vendor);
615 /** Static array of known vendors. \ingroup dict
617 * \attention This structure should only be accessed by internal RADIUS library
620 extern const DICT_VENDOR nr_dict_vendors[];
622 /** The number of attribute definitions in the dictionary. \ingroup dict
624 * This number is guaranteed to be at least 256, for speed.
626 * \attention This variable should only be accessed by internal RADIUS library
629 extern const int nr_dict_num_attrs;
631 /** The list of attribute definitions. \ingroup dict
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.
637 * The attributes are orderd by (vendor, attribute), in increasing
638 * order. This allows the dictionary lookups to find attributes by a
641 * \attention This variable should only be accessed by internal RADIUS library
644 extern const DICT_ATTR nr_dict_attrs[];
646 /** The number of attributes with names. \ingroup dict
648 * \attention This variable should only be accessed by internal RADIUS library
651 extern const int nr_dict_num_names;
653 /** The list of attribute definitions, organized by name. \ingroup dict
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.
659 * \attention This variable should only be accessed by internal RADIUS library
662 extern const DICT_ATTR const *nr_dict_attr_names[];
664 /** Static array containing names the RADIUS_PACKET::code field. \ingroup dict
666 * The names are hard-coded and not in any dictionary because they do
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.
673 extern const char *nr_packet_codes[RS_MAX_PACKET_CODE + 1];
675 /** Verifies that a packet is "well formed". \ingroup packet
677 * This function performs basic validation to see if the packet is
678 * well formed. It is automatically called by nr_packet_decode().
680 * @param[in] packet A pointer to the ::RADIUS_PACKET data.
681 * @return <0 means malformed, >= 0 means well-formed.
683 extern int nr_packet_ok(RADIUS_PACKET *packet);
685 /** Verifies that a packet is "well formed". \ingroup packet
687 * This function performs basic validation to see if the packet is
688 * well formed. You should normally use nr_packet_ok() instead of
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.
695 extern int nr_packet_ok_raw(const uint8_t *data, size_t sizeof_data);
697 /** Encodes a packet. \ingroup packet
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
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.
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.
718 * When the encoding has been successful, it sets the
719 * RADIUS_PACKET::encoded field to non-zero.
721 * In addition, all required attribute "encryption" is performed.
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.
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.
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().
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"
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.
747 extern int nr_packet_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
749 /** Decodes a packet. \ingroup packet
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.
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.
759 extern int nr_packet_decode(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
761 /** Signs a packet so that it can be sent. \ingroup packet
763 * This function calculates the Message-Authenticator (if required),
764 * and signs the packet.
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.
770 extern int nr_packet_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original);
772 /** Verifies that a packet is well-formed and contains the correct signature. \ingroup packet
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
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.
782 extern int nr_packet_verify(RADIUS_PACKET *packet,
783 const RADIUS_PACKET *original);
785 /** Pretty-prints a hex dump of a RADIUS packet. \ingroup packet print
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.
791 * The packet headers are printed individually, and each attribute is
792 * printed as "type length data..."
794 * @param[in] packet The RADIUS packet to print
796 extern void nr_packet_print_hex(RADIUS_PACKET *packet);
799 /** Return the given number of random bytes. \ingroup custom
801 * This function should be replaced by one that is specific to your
804 * This is a wrapper function which enables the library to be more
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.
811 extern ssize_t nr_rand_bytes(uint8_t *data, size_t data_len);
813 /** Return a random 32-bit integer. \ingroup custom
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.
819 * This is a wrapper function which enables the library to be more
822 * @return An unsigned 32-bit random integer.
824 extern uint32_t nr_rand(void);
826 /** Add a time to the given ::struct timeval. \ingroup custom
828 * This is a wrapper function which enables the library to be more
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
835 extern void nr_timeval_add(struct timeval *t, unsigned int seconds,
838 /** Compare two times. \ingroup custom
840 * This is a wrapper function which enables the library to be more
843 * @param[in] a One timeval
844 * @param[in] b Another one
847 extern int nr_timeval_cmp(const struct timeval *a, const struct timeval *b);
849 /** Initializes an ::nr_server_t. \ingroup id
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
856 extern int nr_server_init(nr_server_t *s, int code, const char *secret);
858 /** Closes an ::nr_server_t data structure. \ingroup id
860 * Ensures that all IDs are free, and closes the socket.
862 * @param[in] s The server structure to close.
863 * @return <0 for error, 0 for success
865 extern int nr_server_close(const nr_server_t *s);
867 /** Allocate a RADIUS_PACKET::id value for sending a packet to a server. \ingroup id
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.
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
877 extern int nr_server_id_alloc(nr_server_t *id, RADIUS_PACKET *packet);
879 /** Re-allocate a RADIUS_PACKET::id value for sending a packet to a server. \ingroup id
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.
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
890 extern int nr_server_id_realloc(nr_server_t *id, RADIUS_PACKET *packet);
892 /** Free a RADIUS_PACKET::id value after sending a packet to a server. \ingroup id
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
898 extern int nr_server_id_free(nr_server_t *id, RADIUS_PACKET *packet);
901 /** Allocates a packet using malloc(), and initializes it. \ingroup id
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
907 extern int nr_server_packet_alloc(const nr_server_t *s, RADIUS_PACKET **packet_p);
909 /** Record a humanly readable error message. \ingroup error
911 * \attention This structure should only be accessed by internal
912 * RADIUS library functions.
914 * @param[in] fmt The format to use.
916 extern void nr_strerror_printf(const char *fmt, ...);
919 #define nr_debug_error nr_strerror_printf /** \ingroup error */
921 #define nr_debug_error if (0) nr_strerror_printf
924 /** Encrypts or decrypts a User-Password attribute. \ingroup internal
926 * \attention This structure should only be accessed by internal
927 * RADIUS library functions.
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"
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);
941 /** Encrypts a Tunnel-Password attribute. \ingroup internal
943 * \attention This structure should only be accessed by internal
944 * RADIUS library functions.
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"
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);
958 /** Decrypts a Tunnel-Password attribute. \ingroup internal
961 * \attention This structure should only be accessed by internal
962 * RADIUS library functions.
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"
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);
976 /** Calculates an HMAC-MD5. \ingroup internal
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
984 extern void nr_hmac_md5(const uint8_t *data, size_t data_len,
985 const uint8_t *key, size_t key_len,
988 /** Checks if a TLV is properly formatted. \ingroup internal
990 * \attention This structure should only be accessed by internal
991 * RADIUS library functions.
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"
999 extern int nr_tlv_ok(const uint8_t *data, size_t length,
1000 size_t dv_type, size_t dv_length);
1002 /** A callback function used by nr_packet_walk(). \ingroup packet
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().
1009 typedef int (*nr_packet_walk_func_t)(void *, const DICT_ATTR *, const uint8_t *, size_t);
1011 /** Walks over all attributes in a packet. \ingroup packet
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.
1019 * This function iterates over *all* attributes, including nested
1020 * VSAs. That is its main value.
1022 * Encrypted attributes such as User-Password are not decrypted.
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.
1028 * @return <0 for error,
1031 extern int nr_packet_walk(RADIUS_PACKET *packet, void *ctx,
1032 nr_packet_walk_func_t callback);
1034 /** Initialize a packet
1036 * If original is specified, the packet is initialized as a response
1037 * to the original request.
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.
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);
1051 /** Add one attribute to the packet.
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.
1057 * Some attributes are handled specially by this function.
1059 * EAP-Message. This attribute is automatically split into 253-octet
1062 * User-Password, CHAP-Password, and Message-Authenticator. These
1063 * attributes are automatically encrypted, as is done by
1064 * nr_packet_encode().
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
1072 * @return <0 for error, >= 0 for "successfully appended data"
1073 * The function returns the number of octets appended to the packet.
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);
1081 /** Encodes any ::VALUE_PAIR into an attribute. \ingroup attr
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
1087 * \attention This function should not be called.
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.
1095 * @return <0 for error, or the number of octets used to encode the attribute.
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);
1101 /** Encodes an RFC "standard" ::VALUE_PAIR into an attribute. \ingroup attr
1103 * \attention This function should not be called.
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.
1111 * @return <0 for error, or the number of octets used to encode the attribute.
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);
1118 /** Decodes any attribute into a ::VALUE_PAIR. \ingroup attr
1120 * \attention This function should not be called.
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.
1128 * @return <0 for error, or the number of octets used to decode the attribute.
1130 extern ssize_t nr_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1131 const uint8_t *data, size_t length,
1134 /** Decodes an RFC "standard" attribute into a ::VALUE_PAIR. \ingroup attr
1136 * \attention This function should not be called.
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.
1144 * @return <0 for error, or the number of octets used to decode the attribute.
1146 extern ssize_t nr_attr2vp_rfc(const RADIUS_PACKET *packet,
1147 const RADIUS_PACKET *original,
1148 const uint8_t *data, size_t length,
1151 /** Decodes a Vendor-Specific attribute into a ::VALUE_PAIR. \ingroup attr
1153 * \attention This function should not be called.
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.
1161 * @return <0 for error, or the number of octets used to decode the attribute.
1163 extern ssize_t nr_attr2vp_vsa(const RADIUS_PACKET *packet,
1164 const RADIUS_PACKET *original,
1165 const uint8_t *data, size_t length,
1168 /** Decodes an attribute with an unexpected length into a ::VALUE_PAIR. \ingroup attr
1170 * \attention This function should not be called.
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.
1178 * @return <0 for error, or the number of octets used to decode the attribute.
1180 extern ssize_t nr_attr2vp_raw(const RADIUS_PACKET *packet,
1181 const RADIUS_PACKET *original,
1182 const uint8_t *data, size_t length,
1185 /** Encodes a Vendor-Specific ::VALUE_PAIR into an attribute.
1187 * \attention This function should not be called.
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.
1195 * @return <0 for error, or the number of octets used to encode the attribute.
1197 extern ssize_t nr_vp2vsa(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1198 const VALUE_PAIR **pvp, uint8_t *data,
1201 /** Returns raw data from the RADIUS packet, for a given attribute. \ingroup attr
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.
1210 * This function should be called by an application when it wants
1211 * access to data which is not in the pre-defined dictionaries.
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.
1220 * @return <0 for error,
1221 * 0 for "no attribute found, stop searching"
1222 * >0 offset where the attribute was found.
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);
1228 /** Pretty-print the entire ::VALUE_PAIR \ingroup print
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).
1234 * The format is "Attribute-Name = value"
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
1241 extern size_t nr_vp_snprintf(char *buffer, size_t bufsize, const VALUE_PAIR *vp);
1243 /** Pretty-print the VALUE_PAIR::data field \ingroup print
1245 * Prints the value of a ::VALUE_PAIR, without the name or "=" sign.
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
1252 extern size_t nr_vp_snprintf_value(char *buffer, size_t bufsize, const VALUE_PAIR *vp);
1254 /** Prints a list of :VALUE_PAIR structures to the given output. \ingroup print
1256 * @param[in] fp Where to print the results
1257 * @param[in] vps Linked list of ::VALUE_PAIR to print
1259 extern void nr_vp_fprintf_list(FILE *fp, const VALUE_PAIR *vps);
1261 /** Scan a string into a ::VALUE_PAIR. The counterpart to
1262 * nr_vp_snprintf_value() \ingroup print
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.
1268 extern int nr_vp_sscanf(const char *string, VALUE_PAIR **pvp);
1270 /** Scan the data portion of a ::VALUE_PAIR. The counterpart to
1271 * nr_vp_snprintf_value() \ingroup print
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.
1277 extern ssize_t nr_vp_sscanf_value(VALUE_PAIR *vp, const char *value);
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 */
1286 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1289 # define PRINTF_LIKE(n)
1291 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1294 # define NEVER_RETURNS
1296 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1301 /** Macro used to quiet compiler warnings inside of the library. \ingroup build
1304 # define BLANK_FORMAT ""
1307 #endif /* _RADIUS_CLIENT_H_ */