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>
60 #ifdef SIZEOF_UNSIGNED_INT
61 #if SIZEOF_UNSIGNED_INT != 4
62 #error FATAL: sizeof(unsigned int) != 4
67 * Include for modules.
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 {
144 typedef struct value_pair {
148 int length; /* of data */
150 uint32_t lvalue; /* DELETE ME ASAP */
152 char strvalue[MAX_STRING_LEN];
153 uint8_t octets[MAX_STRING_LEN];
154 struct in_addr ipaddr;
155 struct in6_addr ipv6addr;
159 uint8_t ifid[8]; /* struct? */
160 uint8_t ipv6prefix[18]; /* struct? */
163 struct value_pair *next;
165 #define vp_strvalue data.strvalue
166 #define vp_octets data.octets
167 #define vp_ipaddr lvalue
168 #define vp_ipv6addr data.ipv6addr
169 #define vp_data lvalue
170 #define vp_integer lvalue
171 #define vp_ifid data.ifid
172 #define vp_ipv6prefix data.ipv6prefix
173 #define vp_filter data.filter
176 typedef struct lrad_ipaddr_t {
177 int af; /* address family */
179 struct in_addr ip4addr;
180 struct in6_addr ip6addr; /* maybe defined in missing.h */
186 * vector: Request authenticator from access-request packet
187 * Put in there by rad_decode, and must be put in the
188 * response RADIUS_PACKET as well before calling rad_send
190 * verified: Filled in by rad_decode for accounting-request packets
192 * data,data_len: Used between rad_recv and rad_decode.
194 typedef struct radius_packet {
196 lrad_ipaddr_t src_ipaddr;
197 lrad_ipaddr_t dst_ipaddr;
202 uint8_t vector[AUTH_VECTOR_LEN];
212 * Printing functions.
214 void librad_safeprint(char *in, int inlen, char *out, int outlen);
215 int vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
216 int vp_prints(char *out, int outlen, VALUE_PAIR *vp);
217 void vp_print(FILE *, VALUE_PAIR *);
218 void vp_printlist(FILE *, VALUE_PAIR *);
219 #define fprint_attr_val vp_print
222 * Dictionary functions.
224 int dict_addvendor(const char *name, int value);
225 int dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
226 int dict_addvalue(const char *namestr, const char *attrstr, int value);
227 int dict_init(const char *dir, const char *fn);
228 void dict_free(void);
229 DICT_ATTR *dict_attrbyvalue(int attr);
230 DICT_ATTR *dict_attrbyname(const char *attr);
231 DICT_VALUE *dict_valbyattr(int attr, int val);
232 DICT_VALUE *dict_valbyname(int attr, const char *val);
233 int dict_vendorbyname(const char *name);
234 DICT_VENDOR *dict_vendorbyvalue(int vendor);
236 #if 1 /* FIXME: compat */
237 #define dict_attrget dict_attrbyvalue
238 #define dict_attrfind dict_attrbyname
239 #define dict_valfind dict_valbyname
240 /*#define dict_valget dict_valbyattr almost but not quite*/
243 /* get around diffrent ctime_r styles */
245 #if CTIMERSTYLE == SOLARISSTYLE
246 #define CTIME_R(a,b,c) ctime_r(a,b,c)
248 #define CTIME_R(a,b,c) ctime_r(a,b)
251 #define CTIME_R(a,b,c) ctime_r(a,b)
256 void librad_md5_calc(uint8_t *, const uint8_t *, unsigned int);
260 void lrad_hmac_md5(const uint8_t *text, int text_len,
261 const uint8_t *key, int key_len,
262 unsigned char *digest);
266 void lrad_hmac_sha1(const uint8_t *text, int text_len,
267 const uint8_t *key, int key_len,
271 int rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
272 RADIUS_PACKET *rad_recv(int fd);
273 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
275 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
276 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
278 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
281 RADIUS_PACKET *rad_alloc(int newvector);
282 void rad_free(RADIUS_PACKET **);
283 int rad_pwencode(char *encpw, int *len, const char *secret,
284 const uint8_t *vector);
285 int rad_pwdecode(char *encpw, int len, const char *secret,
286 const uint8_t *vector);
287 int rad_tunnel_pwencode(char *encpw, int *len, const char *secret,
288 const uint8_t *vector);
289 int rad_tunnel_pwdecode(uint8_t *encpw, int *len,
290 const char *secret, const uint8_t *vector);
291 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
292 int id, VALUE_PAIR *password);
293 VALUE_PAIR *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
294 const char *secret, int attribute, int length,
295 const uint8_t *data);
296 int rad_vp2attr(const RADIUS_PACKET *packet,
297 const RADIUS_PACKET *original, const char *secret,
298 const VALUE_PAIR *vp, uint8_t *ptr);
301 VALUE_PAIR *paircreate(int attr, int type);
302 void pairfree(VALUE_PAIR **);
303 void pairbasicfree(VALUE_PAIR *pair);
304 VALUE_PAIR *pairfind(VALUE_PAIR *, int);
305 void pairdelete(VALUE_PAIR **, int);
306 void pairadd(VALUE_PAIR **, VALUE_PAIR *);
307 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
308 int paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
309 VALUE_PAIR *paircopy(VALUE_PAIR *vp);
310 VALUE_PAIR *paircopy2(VALUE_PAIR *vp, int attr);
311 void pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
312 void pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
313 VALUE_PAIR *pairparsevalue(VALUE_PAIR *vp, const char *value);
314 VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator);
315 VALUE_PAIR *pairread(char **ptr, LRAD_TOKEN *eol);
316 LRAD_TOKEN userparse(char *buffer, VALUE_PAIR **first_pair);
317 VALUE_PAIR *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
323 void librad_log(const char *, ...)
325 __attribute__ ((format (printf, 1, 2)))
329 void librad_perror(const char *, ...)
331 __attribute__ ((format (printf, 1, 2)))
334 extern char librad_errstr[];
335 extern int librad_dodns; /* 0 = no dns lookups */
336 extern int librad_debug; /* 0 = no debugging information */
337 extern int librad_max_attributes; /* per incoming packet */
340 * Several handy miscellaneous functions.
342 const char * ip_ntoa(char *, uint32_t);
343 char *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
344 uint8_t *ifid_aton(const char *ifid_str, uint8_t *ifid);
345 char *strNcpy(char *dest, const char *src, int n);
346 int rad_lockfd(int fd, int lock_len);
347 int rad_lockfd_nonblock(int fd, int lock_len);
348 int rad_unlockfd(int fd, int lock_len);
349 void lrad_bin2hex(const uint8_t *bin, char *hex, int len);
350 int lrad_hex2bin(const char *hex, uint8_t *bin, int len);
351 #ifndef HAVE_INET_PTON
352 int inet_pton(int af, const char *src, void *dst);
354 #ifndef HAVE_INET_NTOP
355 const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
357 #ifndef HAVE_CLOSEFROM
358 int closefrom(int fd);
361 int ip_hton(const char *src, int af, lrad_ipaddr_t *dst);
362 const char *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt);
367 int ascend_parse_filter(VALUE_PAIR *pair);
368 void print_abinary(VALUE_PAIR *vp, char *buffer, int len);
369 #endif /*ASCEND_BINARY*/
371 /* random numbers in isaac.c */
372 /* context of random number generator */
373 typedef struct lrad_randctx {
375 uint32_t randrsl[256];
376 uint32_t randmem[256];
382 void lrad_isaac(lrad_randctx *ctx);
383 void lrad_randinit(lrad_randctx *ctx, int flag);
384 uint32_t lrad_rand(void); /* like rand(), but better. */
385 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
389 * Fast hash, which isn't too bad. Don't use for cryptography,
390 * just for hashing internal data.
392 uint32_t lrad_hash(const void *, size_t);
393 uint32_t lrad_hash_update(const void *data, size_t size, uint32_t hash);
396 * If you need fewer than 32-bits of hash, use this macro to get
397 * the number of bits in the hash you need. The upper bits of the
398 * hash will be set to zero.
400 uint32_t lrad_hash_fold(uint32_t hash, int bits);
402 /* crypt wrapper from crypt.c */
403 int lrad_crypt_check(const char *key, const char *salt);
406 typedef struct rbtree_t rbtree_t;
407 typedef struct rbnode_t rbnode_t;
409 rbtree_t *rbtree_create(int (*Compare)(const void *, const void *),
410 void (*freeNode)(void *),
412 void rbtree_free(rbtree_t *tree);
413 int rbtree_insert(rbtree_t *tree, void *Data);
414 void rbtree_delete(rbtree_t *tree, rbnode_t *Z);
415 int rbtree_deletebydata(rbtree_t *tree, const void *data);
416 rbnode_t *rbtree_find(rbtree_t *tree, const void *Data);
417 void *rbtree_finddata(rbtree_t *tree, const void *Data);
418 int rbtree_num_elements(rbtree_t *tree);
419 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
421 /* callback order for walking */
422 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
425 * The callback should be declared as:
426 * int callback(void *context, void *data)
428 * The "context" is some user-defined context.
429 * The "data" is the pointer to the user data in the node,
430 * NOT the node itself.
432 * It should return 0 if all is OK, and !0 for any error.
433 * The walking will stop on any error.
435 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
438 typedef struct lrad_hash_table_t lrad_hash_table_t;
440 lrad_hash_table_t *lrad_hash_table_create(int size, void (*freeNode)(void *),
442 void lrad_hash_table_free(lrad_hash_table_t *ht);
443 int lrad_hash_table_insert(lrad_hash_table_t *ht, uint32_t key,
445 int lrad_hash_table_delete(lrad_hash_table_t *ht, uint32_t key);
446 void *lrad_hash_table_finddata(lrad_hash_table_t *ht, uint32_t key);
447 int lrad_hash_table_num_elements(lrad_hash_table_t *ht);
448 int lrad_hash_table_walk(lrad_hash_table_t *ht,
449 int (*callback)(void * /* ctx */,
452 int lrad_hash_table_set_data_size(lrad_hash_table_t *ht,
455 #endif /*LIBRADIUS_H*/