5 * libradius.h Structures and prototypes
6 * for the radius library.
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
20 #ifdef HAVE_INTTYPES_H
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
32 #ifdef HAVE_ARPA_INET_H
33 #include <arpa/inet.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
43 * Check for inclusion of <time.h>, versus <sys/time.h>
44 * Taken verbatim from the autoconf manual.
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
50 # ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
57 #include <freeradius-devel/radius.h>
58 #include <freeradius-devel/token.h>
60 #ifdef SIZEOF_UNSIGNED_INT
61 #if SIZEOF_UNSIGNED_INT != 4
62 #error FATAL: sizeof(unsigned int) != 4
67 * Include for modules.
69 #include <freeradius-devel/sha1.h>
70 #include <freeradius-devel/md4.h>
74 #define AUTH_VECTOR_LEN 16
75 #define CHAP_VALUE_LENGTH 16
76 #define MAX_STRING_LEN 254 /* RFC2138: string 0-253 octets */
79 # define AUTH_HDR_LEN 20
80 # define VENDORPEC_USR 429
81 #define VENDORPEC_LUCENT 4846
82 #define VENDORPEC_STARENT 8164
83 # define VENDOR(x) ((x >> 16) & 0xffff)
84 # define DEBUG if (librad_debug) printf
85 # define debug_pair(vp) do { if (librad_debug) { \
87 vp_print(stdout, vp); \
91 # define TAG_VALID(x) ((x) > 0 && (x) < 0x20)
92 # define TAG_VALID_ZERO(x) ((x) < 0x20)
93 # define TAG_ANY -128 /* minimum signed char */
97 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
98 # define NEVER_RETURNS __attribute__ ((noreturn))
99 # define UNUSED __attribute__ ((unused))
100 # define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
102 # define PRINTF_LIKE(n) /* ignore */
103 # define NEVER_RETURNS /* ignore */
104 # define UNUSED /* ignore */
105 # define BLANK_FORMAT ""
108 typedef struct attr_flags {
109 unsigned int addport : 1; /* add NAS-Port to IP address */
110 unsigned int has_tag : 1; /* tagged attribute */
111 unsigned int do_xlat : 1; /* strvalue is dynamic */
112 unsigned int caseless : 1; /* case insensitive compares */
113 int8_t tag; /* tag for tunneled attributes */
114 uint8_t encrypt; /* encryption method */
118 * Values of the encryption flags.
120 #define FLAG_ENCRYPT_NONE (0)
121 #define FLAG_ENCRYPT_USER_PASSWORD (1)
122 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
123 #define FLAG_ENCRYPT_ASCEND_SECRET (3)
125 typedef struct dict_attr {
133 typedef struct dict_value {
139 typedef struct dict_vendor {
141 int type; /* length of type data */
142 int length; /* length of length data */
146 typedef struct value_pair {
150 int length; /* of data */
152 uint32_t lvalue; /* DELETE ME ASAP */
154 char strvalue[MAX_STRING_LEN];
155 uint8_t octets[MAX_STRING_LEN];
156 struct in_addr ipaddr;
157 struct in6_addr ipv6addr;
161 uint8_t ifid[8]; /* struct? */
162 uint8_t ipv6prefix[18]; /* struct? */
165 struct value_pair *next;
167 #define vp_strvalue data.strvalue
168 #define vp_octets data.octets
169 #define vp_ipaddr lvalue
170 #define vp_ipv6addr data.ipv6addr
171 #define vp_data lvalue
172 #define vp_integer lvalue
173 #define vp_ifid data.ifid
174 #define vp_ipv6prefix data.ipv6prefix
175 #define vp_filter data.filter
178 typedef struct lrad_ipaddr_t {
179 int af; /* address family */
181 struct in_addr ip4addr;
182 struct in6_addr ip6addr; /* maybe defined in missing.h */
188 * vector: Request authenticator from access-request packet
189 * Put in there by rad_decode, and must be put in the
190 * response RADIUS_PACKET as well before calling rad_send
192 * verified: Filled in by rad_decode for accounting-request packets
194 * data,data_len: Used between rad_recv and rad_decode.
196 typedef struct radius_packet {
198 lrad_ipaddr_t src_ipaddr;
199 lrad_ipaddr_t dst_ipaddr;
204 uint8_t vector[AUTH_VECTOR_LEN];
214 * Printing functions.
216 void librad_safeprint(char *in, int inlen, char *out, int outlen);
217 int vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
218 int vp_prints(char *out, int outlen, VALUE_PAIR *vp);
219 void vp_print(FILE *, VALUE_PAIR *);
220 void vp_printlist(FILE *, VALUE_PAIR *);
221 #define fprint_attr_val vp_print
224 * Dictionary functions.
226 int dict_addvendor(const char *name, int value);
227 int dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
228 int dict_addvalue(const char *namestr, const char *attrstr, int value);
229 int dict_init(const char *dir, const char *fn);
230 void dict_free(void);
231 DICT_ATTR *dict_attrbyvalue(int attr);
232 DICT_ATTR *dict_attrbyname(const char *attr);
233 DICT_VALUE *dict_valbyattr(int attr, int val);
234 DICT_VALUE *dict_valbyname(int attr, const char *val);
235 int dict_vendorbyname(const char *name);
236 DICT_VENDOR *dict_vendorbyvalue(int vendor);
238 #if 1 /* FIXME: compat */
239 #define dict_attrget dict_attrbyvalue
240 #define dict_attrfind dict_attrbyname
241 #define dict_valfind dict_valbyname
242 /*#define dict_valget dict_valbyattr almost but not quite*/
245 /* get around diffrent ctime_r styles */
247 #if CTIMERSTYLE == SOLARISSTYLE
248 #define CTIME_R(a,b,c) ctime_r(a,b,c)
250 #define CTIME_R(a,b,c) ctime_r(a,b)
253 #define CTIME_R(a,b,c) ctime_r(a,b)
258 void librad_md5_calc(uint8_t *, const uint8_t *, unsigned int);
262 void lrad_hmac_md5(const uint8_t *text, int text_len,
263 const uint8_t *key, int key_len,
264 unsigned char *digest);
268 void lrad_hmac_sha1(const uint8_t *text, int text_len,
269 const uint8_t *key, int key_len,
273 int rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
274 int rad_packet_ok(RADIUS_PACKET *packet);
275 RADIUS_PACKET *rad_recv(int fd);
276 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
278 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
279 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
281 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
284 RADIUS_PACKET *rad_alloc(int newvector);
285 void rad_free(RADIUS_PACKET **);
286 int rad_pwencode(char *encpw, int *len, const char *secret,
287 const uint8_t *vector);
288 int rad_pwdecode(char *encpw, int len, const char *secret,
289 const uint8_t *vector);
290 int rad_tunnel_pwencode(char *encpw, int *len, const char *secret,
291 const uint8_t *vector);
292 int rad_tunnel_pwdecode(uint8_t *encpw, int *len,
293 const char *secret, const uint8_t *vector);
294 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
295 int id, VALUE_PAIR *password);
296 VALUE_PAIR *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
297 const char *secret, int attribute, int length,
298 const uint8_t *data);
299 int rad_vp2attr(const RADIUS_PACKET *packet,
300 const RADIUS_PACKET *original, const char *secret,
301 const VALUE_PAIR *vp, uint8_t *ptr);
304 VALUE_PAIR *paircreate(int attr, int type);
305 void pairfree(VALUE_PAIR **);
306 void pairbasicfree(VALUE_PAIR *pair);
307 VALUE_PAIR *pairfind(VALUE_PAIR *, int);
308 void pairdelete(VALUE_PAIR **, int);
309 void pairadd(VALUE_PAIR **, VALUE_PAIR *);
310 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
311 int paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
312 VALUE_PAIR *paircopy(VALUE_PAIR *vp);
313 VALUE_PAIR *paircopy2(VALUE_PAIR *vp, int attr);
314 void pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
315 void pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
316 VALUE_PAIR *pairparsevalue(VALUE_PAIR *vp, const char *value);
317 VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator);
318 VALUE_PAIR *pairread(char **ptr, LRAD_TOKEN *eol);
319 LRAD_TOKEN userparse(char *buffer, VALUE_PAIR **first_pair);
320 VALUE_PAIR *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
326 void librad_log(const char *, ...)
328 __attribute__ ((format (printf, 1, 2)))
332 void librad_perror(const char *, ...)
334 __attribute__ ((format (printf, 1, 2)))
337 extern char librad_errstr[];
338 extern int librad_dodns; /* 0 = no dns lookups */
339 extern int librad_debug; /* 0 = no debugging information */
340 extern int librad_max_attributes; /* per incoming packet */
343 * Several handy miscellaneous functions.
345 const char * ip_ntoa(char *, uint32_t);
346 char *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
347 uint8_t *ifid_aton(const char *ifid_str, uint8_t *ifid);
348 char *strNcpy(char *dest, const char *src, int n);
349 int rad_lockfd(int fd, int lock_len);
350 int rad_lockfd_nonblock(int fd, int lock_len);
351 int rad_unlockfd(int fd, int lock_len);
352 void lrad_bin2hex(const uint8_t *bin, char *hex, int len);
353 int lrad_hex2bin(const char *hex, uint8_t *bin, int len);
354 #ifndef HAVE_INET_PTON
355 int inet_pton(int af, const char *src, void *dst);
357 #ifndef HAVE_INET_NTOP
358 const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
360 #ifndef HAVE_CLOSEFROM
361 int closefrom(int fd);
364 int ip_hton(const char *src, int af, lrad_ipaddr_t *dst);
365 const char *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt);
370 int ascend_parse_filter(VALUE_PAIR *pair);
371 void print_abinary(VALUE_PAIR *vp, char *buffer, int len);
372 #endif /*ASCEND_BINARY*/
374 /* random numbers in isaac.c */
375 /* context of random number generator */
376 typedef struct lrad_randctx {
378 uint32_t randrsl[256];
379 uint32_t randmem[256];
385 void lrad_isaac(lrad_randctx *ctx);
386 void lrad_randinit(lrad_randctx *ctx, int flag);
387 uint32_t lrad_rand(void); /* like rand(), but better. */
388 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
392 * Fast hash, which isn't too bad. Don't use for cryptography,
393 * just for hashing internal data.
395 uint32_t lrad_hash(const void *, size_t);
396 uint32_t lrad_hash_update(const void *data, size_t size, uint32_t hash);
399 * If you need fewer than 32-bits of hash, use this macro to get
400 * the number of bits in the hash you need. The upper bits of the
401 * hash will be set to zero.
403 uint32_t lrad_hash_fold(uint32_t hash, int bits);
405 /* crypt wrapper from crypt.c */
406 int lrad_crypt_check(const char *key, const char *salt);
409 typedef struct rbtree_t rbtree_t;
410 typedef struct rbnode_t rbnode_t;
412 rbtree_t *rbtree_create(int (*Compare)(const void *, const void *),
413 void (*freeNode)(void *),
415 void rbtree_free(rbtree_t *tree);
416 int rbtree_insert(rbtree_t *tree, void *Data);
417 void rbtree_delete(rbtree_t *tree, rbnode_t *Z);
418 int rbtree_deletebydata(rbtree_t *tree, const void *data);
419 rbnode_t *rbtree_find(rbtree_t *tree, const void *Data);
420 void *rbtree_finddata(rbtree_t *tree, const void *Data);
421 int rbtree_num_elements(rbtree_t *tree);
422 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
424 /* callback order for walking */
425 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
428 * The callback should be declared as:
429 * int callback(void *context, void *data)
431 * The "context" is some user-defined context.
432 * The "data" is the pointer to the user data in the node,
433 * NOT the node itself.
435 * It should return 0 if all is OK, and !0 for any error.
436 * The walking will stop on any error.
438 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
441 typedef struct lrad_hash_table_t lrad_hash_table_t;
442 typedef void (*lrad_hash_table_free_t)(void *);
443 typedef int (*lrad_hash_table_walk_t)(void * /* ctx */, void * /* data */);
445 lrad_hash_table_t *lrad_hash_table_create(lrad_hash_table_free_t freeNode);
446 void lrad_hash_table_free(lrad_hash_table_t *ht);
447 int lrad_hash_table_insert(lrad_hash_table_t *ht, uint32_t key, void *data);
448 int lrad_hash_table_delete(lrad_hash_table_t *ht, uint32_t key);
449 void *lrad_hash_table_yank(lrad_hash_table_t *ht, uint32_t key);
450 int lrad_hash_table_replace(lrad_hash_table_t *ht, uint32_t key, void *data);
451 void *lrad_hash_table_finddata(lrad_hash_table_t *ht, uint32_t key);
452 int lrad_hash_table_num_elements(lrad_hash_table_t *ht);
453 int lrad_hash_table_walk(lrad_hash_table_t *ht,
454 lrad_hash_table_walk_t callback,
460 typedef struct lrad_fifo_t lrad_fifo_t;
461 typedef void (*lrad_fifo_free_t)(void *);
462 lrad_fifo_t *lrad_fifo_create(int max_entries, lrad_fifo_free_t freeNode);
463 void lrad_fifo_free(lrad_fifo_t *fi);
464 int lrad_fifo_push(lrad_fifo_t *fi, void *data);
465 void *lrad_fifo_pop(lrad_fifo_t *fi);
467 #endif /*LIBRADIUS_H*/