5 * libradius.h Structures and prototypes
6 * for the radius library.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Copyright 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008 The FreeRADIUS server project
27 #include <freeradius-devel/ident.h>
28 RCSIDH(libradius_h, "$Id$")
30 #include <freeradius-devel/missing.h>
40 #include <freeradius-devel/radius.h>
41 #include <freeradius-devel/token.h>
42 #include <freeradius-devel/hash.h>
44 #ifdef SIZEOF_UNSIGNED_INT
45 #if SIZEOF_UNSIGNED_INT != 4
46 #error FATAL: sizeof(unsigned int) != 4
51 * Include for modules.
53 #include <freeradius-devel/sha1.h>
54 #include <freeradius-devel/md4.h>
66 #define AUTH_VECTOR_LEN 16
67 #define CHAP_VALUE_LENGTH 16
68 #define MAX_STRING_LEN 254 /* RFC2138: string 0-253 octets */
69 #define FR_MAX_VENDOR (1 << 24) /* RFC limitations */
72 # define AUTH_HDR_LEN 20
73 # define VENDORPEC_USR 429
74 #define VENDORPEC_LUCENT 4846
75 #define VENDORPEC_STARENT 8164
76 # define DEBUG if (fr_debug_flag && fr_log_fp) fr_printf_log
77 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
78 fputc('\t', fr_log_fp); \
79 vp_print(fr_log_fp, vp); \
80 fputc('\n', fr_log_fp); \
83 # define TAG_VALID(x) ((x) > 0 && (x) < 0x20)
84 # define TAG_VALID_ZERO(x) ((x) < 0x20)
85 # define TAG_ANY -128 /* minimum signed char */
89 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
90 # define NEVER_RETURNS __attribute__ ((noreturn))
91 # define UNUSED __attribute__ ((unused))
92 # define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
94 # define PRINTF_LIKE(n) /* ignore */
95 # define NEVER_RETURNS /* ignore */
96 # define UNUSED /* ignore */
97 # define BLANK_FORMAT ""
100 typedef struct attr_flags {
101 unsigned int addport : 1; /* add NAS-Port to IP address */
102 unsigned int has_tag : 1; /* tagged attribute */
103 unsigned int do_xlat : 1; /* strvalue is dynamic */
104 unsigned int unknown_attr : 1; /* not in dictionary */
105 unsigned int array : 1; /* pack multiples into 1 attr */
106 unsigned int has_value : 1; /* has a value */
107 unsigned int has_value_alias : 1; /* has a value alias */
108 unsigned int has_tlv : 1; /* has sub attributes */
109 unsigned int is_tlv : 1; /* is a sub attribute */
110 unsigned int encoded : 1; /* has been put into packet */
111 unsigned int extended : 1; /* extended attribute */
112 unsigned int extended_flags : 1; /* with flag */
114 int8_t tag; /* tag for tunneled attributes */
115 uint8_t encrypt; /* encryption method */
120 * Values of the encryption flags.
122 #define FLAG_ENCRYPT_NONE (0)
123 #define FLAG_ENCRYPT_USER_PASSWORD (1)
124 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
125 #define FLAG_ENCRYPT_ASCEND_SECRET (3)
127 typedef struct dict_attr {
135 typedef struct dict_value {
142 typedef struct dict_vendor {
144 size_t type; /* length of type data */
145 size_t length; /* length of length data */
150 typedef union value_pair_data {
151 char strvalue[MAX_STRING_LEN];
152 uint8_t octets[MAX_STRING_LEN];
153 struct in_addr ipaddr;
154 struct in6_addr ipv6addr;
159 uint8_t ifid[8]; /* struct? */
160 uint8_t ipv6prefix[18]; /* struct? */
165 typedef struct value_pair {
167 unsigned int attribute;
170 size_t length; /* of data */
173 * C++ hackery. The server and modules are all C, so
174 * the defs here don't affect them. But any C++ code
175 * gets excited over "operator", so we change the name.
182 struct value_pair *next;
184 VALUE_PAIR_DATA data;
186 #define vp_strvalue data.strvalue
187 #define vp_octets data.octets
188 #define vp_ipv6addr data.ipv6addr
189 #define vp_ifid data.ifid
190 #define vp_ipv6prefix data.ipv6prefix
191 #define vp_filter data.filter
192 #define vp_ether data.ether
193 #define vp_signed data.sinteger
194 #define vp_tlv data.tlv
197 #define vp_ipaddr data.ipaddr.s_addr
198 #define vp_date data.date
199 #define vp_integer data.integer
202 * These are left as lvalue until we audit the source for code
203 * that prints to vp_strvalue for integer/ipaddr/date types.
205 #define vp_ipaddr lvalue
206 #define vp_date lvalue
207 #define vp_integer lvalue
211 typedef struct fr_ipaddr_t {
212 int af; /* address family */
214 struct in_addr ip4addr;
215 struct in6_addr ip6addr; /* maybe defined in missing.h */
217 uint32_t scope; /* for IPv6 */
221 * vector: Request authenticator from access-request packet
222 * Put in there by rad_decode, and must be put in the
223 * response RADIUS_PACKET as well before calling rad_send
225 * verified: Filled in by rad_decode for accounting-request packets
227 * data,data_len: Used between rad_recv and rad_decode.
229 typedef struct radius_packet {
231 fr_ipaddr_t src_ipaddr;
232 fr_ipaddr_t dst_ipaddr;
238 uint8_t vector[AUTH_VECTOR_LEN];
250 * Printing functions.
252 int fr_utf8_char(const uint8_t *str);
253 void fr_print_string(const char *in, size_t inlen,
254 char *out, size_t outlen);
255 int vp_prints_value(char *out, size_t outlen,
256 VALUE_PAIR *vp, int delimitst);
257 const char *vp_print_name(char *buffer, size_t bufsize, int attr, int vendor);
258 int vp_prints(char *out, size_t outlen, VALUE_PAIR *vp);
259 void vp_print(FILE *, VALUE_PAIR *);
260 void vp_printlist(FILE *, VALUE_PAIR *);
261 #define fprint_attr_val vp_print
264 * Dictionary functions.
266 int dict_addvendor(const char *name, int value);
267 int dict_addattr(const char *name, int attr, int vendor, int type, ATTR_FLAGS flags);
268 int dict_addvalue(const char *namestr, const char *attrstr, int value);
269 int dict_init(const char *dir, const char *fn);
270 void dict_free(void);
271 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
272 DICT_ATTR *dict_attrbyname(const char *attr);
273 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
274 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *val);
275 int dict_vendorbyname(const char *name);
276 DICT_VENDOR *dict_vendorbyvalue(int vendor);
278 #if 1 /* FIXME: compat */
279 #define dict_attrget dict_attrbyvalue
280 #define dict_attrfind dict_attrbyname
281 #define dict_valfind dict_valbyname
282 /*#define dict_valget dict_valbyattr almost but not quite*/
285 /* get around diffrent ctime_r styles */
287 #if CTIMERSTYLE == SOLARISSTYLE
288 #define CTIME_R(a,b,c) ctime_r(a,b,c)
290 #define CTIME_R(a,b,c) ctime_r(a,b)
293 #define CTIME_R(a,b,c) ctime_r(a,b)
298 void fr_md5_calc(uint8_t *, const uint8_t *, unsigned int);
302 void fr_hmac_md5(const uint8_t *text, int text_len,
303 const uint8_t *key, int key_len,
304 unsigned char *digest);
308 void fr_hmac_sha1(const uint8_t *text, int text_len,
309 const uint8_t *key, int key_len,
313 int rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
314 int rad_packet_ok(RADIUS_PACKET *packet, int flags);
315 RADIUS_PACKET *rad_recv(int fd, int flags);
316 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
318 void rad_recv_discard(int sockfd);
319 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
321 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
322 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
324 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
327 RADIUS_PACKET *rad_alloc(int newvector);
328 RADIUS_PACKET *rad_alloc_reply(RADIUS_PACKET *);
329 void rad_free(RADIUS_PACKET **);
330 int rad_pwencode(char *encpw, size_t *len, const char *secret,
331 const uint8_t *vector);
332 int rad_pwdecode(char *encpw, size_t len, const char *secret,
333 const uint8_t *vector);
334 int rad_tunnel_pwencode(char *encpw, size_t *len, const char *secret,
335 const uint8_t *vector);
336 int rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
337 const char *secret, const uint8_t *vector);
338 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
339 int id, VALUE_PAIR *password);
340 VALUE_PAIR *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
341 const char *secret, int attribute, int vendor,
342 int length, const uint8_t *data);
343 int rad_vp2attr(const RADIUS_PACKET *packet,
344 const RADIUS_PACKET *original, const char *secret,
345 const VALUE_PAIR *vp, uint8_t *ptr, size_t room);
348 VALUE_PAIR *pairalloc(DICT_ATTR *da);
349 VALUE_PAIR *paircreate_raw(int attr, int vendor, int type, VALUE_PAIR *);
350 VALUE_PAIR *paircreate(int attr, int vendor, int type);
351 void pairfree(VALUE_PAIR **);
352 void pairbasicfree(VALUE_PAIR *pair);
353 VALUE_PAIR *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor);
354 void pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor);
355 void pairadd(VALUE_PAIR **, VALUE_PAIR *);
356 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
357 int paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
358 VALUE_PAIR *paircopyvp(const VALUE_PAIR *vp);
359 VALUE_PAIR *paircopy(VALUE_PAIR *vp);
360 VALUE_PAIR *paircopy2(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor);
361 void pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
362 void pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor);
363 VALUE_PAIR *pairparsevalue(VALUE_PAIR *vp, const char *value);
364 VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator);
365 VALUE_PAIR *pairread(const char **ptr, FR_TOKEN *eol);
366 FR_TOKEN userparse(const char *buffer, VALUE_PAIR **first_pair);
367 VALUE_PAIR *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
373 void fr_strerror_printf(const char *, ...)
375 __attribute__ ((format (printf, 1, 2)))
379 void fr_perror(const char *, ...)
381 __attribute__ ((format (printf, 1, 2)))
384 extern const char *fr_strerror(void);
385 extern int fr_dns_lookups; /* 0 = no dns lookups */
386 extern int fr_debug_flag; /* 0 = no debugging information */
387 extern int fr_max_attributes; /* per incoming packet */
388 #define FR_MAX_PACKET_CODE (52)
389 extern const char *fr_packet_codes[FR_MAX_PACKET_CODE];
390 extern FILE *fr_log_fp;
391 void fr_printf_log(const char *, ...)
393 __attribute__ ((format (printf, 1, 2)))
398 * Several handy miscellaneous functions.
400 const char * ip_ntoa(char *, uint32_t);
401 char *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
402 uint8_t *ifid_aton(const char *ifid_str, uint8_t *ifid);
403 int rad_lockfd(int fd, int lock_len);
404 int rad_lockfd_nonblock(int fd, int lock_len);
405 int rad_unlockfd(int fd, int lock_len);
406 void fr_bin2hex(const uint8_t *bin, char *hex, size_t len);
407 size_t fr_hex2bin(const char *hex, uint8_t *bin, size_t len);
408 #ifndef HAVE_INET_PTON
409 int inet_pton(int af, const char *src, void *dst);
411 #ifndef HAVE_INET_NTOP
412 const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
414 #ifndef HAVE_CLOSEFROM
415 int closefrom(int fd);
417 int fr_ipaddr_cmp(const fr_ipaddr_t *a, const fr_ipaddr_t *b);
419 int ip_hton(const char *src, int af, fr_ipaddr_t *dst);
420 const char *ip_ntoh(const fr_ipaddr_t *src, char *dst, size_t cnt);
421 int fr_ipaddr2sockaddr(const fr_ipaddr_t *ipaddr, int port,
422 struct sockaddr_storage *sa, socklen_t *salen);
423 int fr_sockaddr2ipaddr(const struct sockaddr_storage *sa, socklen_t salen,
424 fr_ipaddr_t *ipaddr, int * port);
429 int ascend_parse_filter(VALUE_PAIR *pair);
430 void print_abinary(VALUE_PAIR *vp, char *buffer, size_t len);
431 #endif /*ASCEND_BINARY*/
433 /* random numbers in isaac.c */
434 /* context of random number generator */
435 typedef struct fr_randctx {
437 uint32_t randrsl[256];
438 uint32_t randmem[256];
444 void fr_isaac(fr_randctx *ctx);
445 void fr_randinit(fr_randctx *ctx, int flag);
446 uint32_t fr_rand(void); /* like rand(), but better. */
447 void fr_rand_seed(const void *, size_t ); /* seed the random pool */
450 /* crypt wrapper from crypt.c */
451 int fr_crypt_check(const char *key, const char *salt);
454 typedef struct rbtree_t rbtree_t;
455 typedef struct rbnode_t rbnode_t;
457 rbtree_t *rbtree_create(int (*Compare)(const void *, const void *),
458 void (*freeNode)(void *),
460 void rbtree_free(rbtree_t *tree);
461 int rbtree_insert(rbtree_t *tree, void *Data);
462 rbnode_t *rbtree_insertnode(rbtree_t *tree, void *Data);
463 void rbtree_delete(rbtree_t *tree, rbnode_t *Z);
464 int rbtree_deletebydata(rbtree_t *tree, const void *data);
465 rbnode_t *rbtree_find(rbtree_t *tree, const void *Data);
466 void *rbtree_finddata(rbtree_t *tree, const void *Data);
467 int rbtree_num_elements(rbtree_t *tree);
468 void *rbtree_min(rbtree_t *tree);
469 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
471 /* callback order for walking */
472 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
475 * The callback should be declared as:
476 * int callback(void *context, void *data)
478 * The "context" is some user-defined context.
479 * The "data" is the pointer to the user data in the node,
480 * NOT the node itself.
482 * It should return 0 if all is OK, and !0 for any error.
483 * The walking will stop on any error.
485 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
490 typedef struct fr_fifo_t fr_fifo_t;
491 typedef void (*fr_fifo_free_t)(void *);
492 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
493 void fr_fifo_free(fr_fifo_t *fi);
494 int fr_fifo_push(fr_fifo_t *fi, void *data);
495 void *fr_fifo_pop(fr_fifo_t *fi);
496 void *fr_fifo_peek(fr_fifo_t *fi);
497 int fr_fifo_num_elements(fr_fifo_t *fi);
503 #include <freeradius-devel/packet.h>
506 #include <freeradius-devel/tcp.h>
509 #endif /*LIBRADIUS_H*/