9afc841661317061136c2bb50cc3cadf8a696010
[freeradius.git] / src / include / libradius.h
1 #ifndef LIBRADIUS_H
2 #define LIBRADIUS_H
3
4 /*
5  * libradius.h  Structures and prototypes
6  *              for the radius library.
7  *
8  * Version:     $Id$
9  *
10  *   This library is free software; you can redistribute it and/or
11  *   modify it under the terms of the GNU Lesser General Public
12  *   License as published by the Free Software Foundation; either
13  *   version 2.1 of the License, or (at your option) any later version.
14  *
15  *   This library 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 GNU
18  *   Lesser General Public License for more details.
19  *
20  *   You should have received a copy of the GNU Lesser General Public
21  *   License along with this library; if not, write to the Free Software
22  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  * Copyright 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008  The FreeRADIUS server project
25  */
26
27 #include <freeradius-devel/ident.h>
28 RCSIDH(libradius_h, "$Id$")
29
30 #include <freeradius-devel/missing.h>
31
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39
40 #include <freeradius-devel/threads.h>
41 #include <freeradius-devel/radius.h>
42 #include <freeradius-devel/token.h>
43 #include <freeradius-devel/hash.h>
44
45 #ifdef SIZEOF_UNSIGNED_INT
46 #if SIZEOF_UNSIGNED_INT != 4
47 #error FATAL: sizeof(unsigned int) != 4
48 #endif
49 #endif
50
51 #ifndef FALSE
52 #define FALSE 0
53 #endif
54 #ifndef TRUE
55 /*
56  *      This definition of true as NOT false is definitive. :) Making
57  *      it '1' can cause problems on stupid platforms.  See articles
58  *      on C portability for more information.
59  */
60 #define TRUE (!FALSE)
61 #endif
62
63 /*
64  *  Include for modules.
65  */
66 #include <freeradius-devel/sha1.h>
67 #include <freeradius-devel/md4.h>
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 #define EAP_START               2
74
75 #define AUTH_VECTOR_LEN         16
76 #define CHAP_VALUE_LENGTH       16
77 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
78
79 #  define VENDOR(x)             ((x >> 16) & 0xffff)
80
81 #ifdef _LIBRADIUS
82 #  define AUTH_HDR_LEN          20
83 #  define VENDORPEC_USR         429
84 #define VENDORPEC_LUCENT        4846
85 #define VENDORPEC_STARENT       8164
86 #  define DEBUG                 if (fr_debug_flag && fr_log_fp) fr_printf_log
87 #  define debug_pair(vp)        do { if (fr_debug_flag && fr_log_fp) { \
88                                         vp_print(fr_log_fp, vp); \
89                                      } \
90                                 } while(0)
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 */
94 #endif
95
96 #if defined(__GNUC__)
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 */
101 #else
102 # define PRINTF_LIKE(n) /* ignore */
103 # define NEVER_RETURNS /* ignore */
104 # define UNUSED /* ignore */
105 # define BLANK_FORMAT ""
106 #endif
107
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            unknown_attr : 1; /* not in dictionary */
113         unsigned int            array : 1; /* pack multiples into 1 attr */
114         unsigned int            has_value : 1; /* has a value */
115         unsigned int            has_value_alias : 1; /* has a value alias */
116         unsigned int            has_tlv : 1; /* has sub attributes */
117         unsigned int            is_tlv : 1; /* is a sub attribute */
118         unsigned int            encoded : 1; /* has been put into packet */
119
120         int8_t                  tag;          /* tag for tunneled attributes */
121         uint8_t                 encrypt;      /* encryption method */
122 } ATTR_FLAGS;
123
124 /*
125  *  Values of the encryption flags.
126  */
127 #define FLAG_ENCRYPT_NONE            (0)
128 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
129 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
130 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
131
132 extern const FR_NAME_NUMBER dict_attr_types[];
133
134 typedef struct dict_attr {
135         unsigned int            attr;
136         int                     type;
137         int                     vendor;
138         ATTR_FLAGS              flags;
139         char                    name[1];
140 } DICT_ATTR;
141
142 typedef struct dict_value {
143         unsigned int            attr;
144         int                     value;
145         char                    name[1];
146 } DICT_VALUE;
147
148 typedef struct dict_vendor {
149         int                     vendorpec;
150         int                     type; /* length of type data */
151         int                     length; /* length of length data */
152         int                     flags;
153         char                    name[1];
154 } DICT_VENDOR;
155
156 typedef union value_pair_data {
157         char                    strvalue[MAX_STRING_LEN];
158         uint8_t                 octets[MAX_STRING_LEN];
159         struct in_addr          ipaddr;
160         struct in6_addr         ipv6addr;
161         uint32_t                date;
162         uint32_t                integer;
163         int32_t                 sinteger;
164         uint8_t                 filter[32];
165         uint8_t                 ifid[8]; /* struct? */
166         uint8_t                 ipv6prefix[18]; /* struct? */
167         uint8_t                 ether[6];
168         uint8_t                 *tlv;
169 } VALUE_PAIR_DATA;
170
171 typedef struct value_pair {
172         const char              *name;
173         int                     attribute;
174         int                     vendor;
175         int                     type;
176         size_t                  length; /* of data */
177 #ifdef __cplusplus
178         /*
179          *      C++ hackery.  The server and modules are all C, so
180          *      the defs here don't affect them.  But any C++ code
181          *      gets excited over "operator", so we change the name.
182          */
183         FR_TOKEN                op_token;
184 #else
185         FR_TOKEN                operator;
186 #endif
187         ATTR_FLAGS              flags;
188         struct value_pair       *next;
189         uint32_t                lvalue;
190         VALUE_PAIR_DATA         data;
191 } VALUE_PAIR;
192 #define vp_strvalue   data.strvalue
193 #define vp_octets     data.octets
194 #define vp_ipv6addr   data.ipv6addr
195 #define vp_ifid       data.ifid
196 #define vp_ipv6prefix data.ipv6prefix
197 #define vp_filter     data.filter
198 #define vp_ether      data.ether
199 #define vp_signed     data.sinteger
200 #define vp_tlv        data.tlv
201
202 #if 0
203 #define vp_ipaddr     data.ipaddr.s_addr
204 #define vp_date       data.date
205 #define vp_integer    data.integer
206 #else
207 /*
208  *      These are left as lvalue until we audit the source for code
209  *      that prints to vp_strvalue for integer/ipaddr/date types.
210  */
211 #define vp_ipaddr     lvalue
212 #define vp_date       lvalue
213 #define vp_integer    lvalue
214 #endif
215
216
217 typedef struct fr_ipaddr_t {
218         int             af;     /* address family */
219         union {
220                 struct in_addr  ip4addr;
221                 struct in6_addr ip6addr; /* maybe defined in missing.h */
222         } ipaddr;
223         uint32_t        scope;  /* for IPv6 */
224 } fr_ipaddr_t;
225
226 /*
227  *      vector:         Request authenticator from access-request packet
228  *                      Put in there by rad_decode, and must be put in the
229  *                      response RADIUS_PACKET as well before calling rad_send
230  *
231  *      verified:       Filled in by rad_decode for accounting-request packets
232  *
233  *      data,data_len:  Used between rad_recv and rad_decode.
234  */
235 typedef struct radius_packet {
236         int                     sockfd;
237         fr_ipaddr_t             src_ipaddr;
238         fr_ipaddr_t             dst_ipaddr;
239         uint16_t                src_port;
240         uint16_t                dst_port;
241         int                     id;
242         unsigned int            code;
243         uint32_t                hash;
244         uint8_t                 vector[AUTH_VECTOR_LEN];
245         time_t                  timestamp;
246         uint8_t                 *data;
247         int                     data_len;
248         VALUE_PAIR              *vps;
249         ssize_t                 offset;
250 } RADIUS_PACKET;
251
252 /*
253  *      Printing functions.
254  */
255 int             fr_utf8_char(const uint8_t *str);
256 size_t          fr_print_string(const char *in, size_t inlen,
257                                  char *out, size_t outlen);
258 int             vp_prints_value(char *out, size_t outlen,
259                                 VALUE_PAIR *vp, int delimitst);
260 const char      *vp_print_name(char *buffer, size_t bufsize, int attr);
261 int             vp_prints(char *out, size_t outlen, VALUE_PAIR *vp);
262 void            vp_print(FILE *, VALUE_PAIR *);
263 void            vp_printlist(FILE *, VALUE_PAIR *);
264 #define         fprint_attr_val vp_print
265
266 /*
267  *      Dictionary functions.
268  */
269 int             dict_addvendor(const char *name, int value);
270 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
271 int             dict_addvalue(const char *namestr, const char *attrstr, int value);
272 int             dict_init(const char *dir, const char *fn);
273 void            dict_free(void);
274 DICT_ATTR       *dict_attrbyvalue(unsigned int attr);
275 DICT_ATTR       *dict_attrbyname(const char *attr);
276 DICT_VALUE      *dict_valbyattr(unsigned int attr, int val);
277 DICT_VALUE      *dict_valbyname(unsigned int attr, const char *val);
278 const char      *dict_valnamebyattr(unsigned int attr, int value);
279 int             dict_vendorbyname(const char *name);
280 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
281
282 #if 1 /* FIXME: compat */
283 #define dict_attrget    dict_attrbyvalue
284 #define dict_attrfind   dict_attrbyname
285 #define dict_valfind    dict_valbyname
286 /*#define dict_valget   dict_valbyattr almost but not quite*/
287 #endif
288
289 /* get around diffrent ctime_r styles */
290 #if defined(CTIMERSTYLE) && ( CTIMERSTYLE == SOLARISSTYLE)
291 #define CTIME_R(a,b,c) ctime_r(a,b,c)
292 #define ASCTIME_R(a,b,c) asctime_r(a,b,c)
293 #else
294 #define CTIME_R(a,b,c) ctime_r(a,b)
295 #define ASCTIME_R(a,b,c) asctime_r(a,b)
296 #endif
297
298 /* md5.c */
299
300 void            fr_md5_calc(uint8_t *, const uint8_t *, unsigned int);
301
302 /* hmac.c */
303
304 void fr_hmac_md5(const uint8_t *text, int text_len,
305                    const uint8_t *key, int key_len,
306                    unsigned char *digest);
307
308 /* hmacsha1.c */
309
310 void fr_hmac_sha1(const uint8_t *text, int text_len,
311                     const uint8_t *key, int key_len,
312                     uint8_t *digest);
313
314 /* radius.c */
315 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
316 int             rad_packet_ok(RADIUS_PACKET *packet, int flags);
317 RADIUS_PACKET   *rad_recv(int fd, int flags);
318 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
319                         int *code);
320 void            rad_recv_discard(int sockfd);
321 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
322                            const char *secret);
323 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
324 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
325                            const char *secret);
326 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
327                          const char *secret);
328
329 int rad_digest_cmp(const uint8_t *a, const uint8_t *b, size_t length);
330 RADIUS_PACKET   *rad_alloc(int newvector);
331 RADIUS_PACKET   *rad_alloc_reply(RADIUS_PACKET *);
332 void            rad_free(RADIUS_PACKET **);
333 int             rad_pwencode(char *encpw, size_t *len, const char *secret,
334                              const uint8_t *vector);
335 int             rad_pwdecode(char *encpw, size_t len, const char *secret,
336                              const uint8_t *vector);
337 int             rad_tunnel_pwencode(char *encpw, size_t *len, const char *secret,
338                                     const uint8_t *vector);
339 int             rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
340                                     const char *secret, const uint8_t *vector);
341 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
342                                 int id, VALUE_PAIR *password);
343 VALUE_PAIR      *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
344                              const char *secret, int attribute, int length,
345                              const uint8_t *data);
346 ssize_t         rad_vp2data(const VALUE_PAIR *vp, uint8_t *out, size_t outlen);
347 int             rad_vp2attr(const RADIUS_PACKET *packet,
348                             const RADIUS_PACKET *original, const char *secret,
349                             const VALUE_PAIR *vp, uint8_t *ptr);
350
351 /* valuepair.c */
352 VALUE_PAIR      *pairalloc(DICT_ATTR *da);
353 VALUE_PAIR      *paircreate_raw(int attr, int type, VALUE_PAIR *vp);
354 VALUE_PAIR      *paircreate(int attr, int type);
355 void            pairfree(VALUE_PAIR **);
356 void            pairbasicfree(VALUE_PAIR *pair);
357 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
358 void            pairdelete(VALUE_PAIR **, int);
359 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
360 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
361 int             paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
362 VALUE_PAIR      *paircopyvp(const VALUE_PAIR *vp);
363 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
364 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
365 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
366 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
367 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
368 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
369 VALUE_PAIR      *pairread(const char **ptr, FR_TOKEN *eol);
370 FR_TOKEN        userparse(const char *buffer, VALUE_PAIR **first_pair);
371 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
372
373 /*
374  *      Error functions.
375  */
376 #ifdef _LIBRADIUS
377 void            fr_strerror_printf(const char *, ...)
378 #ifdef __GNUC__
379                 __attribute__ ((format (printf, 1, 2)))
380 #endif
381 ;
382 #endif
383 void            fr_perror(const char *, ...)
384 #ifdef __GNUC__
385                 __attribute__ ((format (printf, 1, 2)))
386 #endif
387 ;
388 extern const char *fr_strerror(void);
389 extern int      fr_dns_lookups; /* 0 = no dns lookups */
390 extern int      fr_debug_flag;  /* 0 = no debugging information */
391 extern int      fr_max_attributes; /* per incoming packet */
392 #define FR_MAX_PACKET_CODE (52)
393 extern const char *fr_packet_codes[FR_MAX_PACKET_CODE];
394 extern FILE     *fr_log_fp;
395 extern void rad_print_hex(RADIUS_PACKET *packet);
396 void            fr_printf_log(const char *, ...)
397 #ifdef __GNUC__
398                 __attribute__ ((format (printf, 1, 2)))
399 #endif
400 ;
401
402 /*
403  *      Several handy miscellaneous functions.
404  */
405 const char *    ip_ntoa(char *, uint32_t);
406 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
407 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
408 int             rad_lockfd(int fd, int lock_len);
409 int             rad_lockfd_nonblock(int fd, int lock_len);
410 int             rad_unlockfd(int fd, int lock_len);
411 void            fr_bin2hex(const uint8_t *bin, char *hex, size_t len);
412 size_t          fr_hex2bin(const char *hex, uint8_t *bin, size_t len);
413 #ifndef HAVE_INET_PTON
414 int             inet_pton(int af, const char *src, void *dst);
415 #endif
416 #ifndef HAVE_INET_NTOP
417 const char      *inet_ntop(int af, const void *src, char *dst, size_t cnt);
418 #endif
419 #ifndef HAVE_CLOSEFROM
420 int             closefrom(int fd);
421 #endif
422 int fr_ipaddr_cmp(const fr_ipaddr_t *a, const fr_ipaddr_t *b);
423
424 int             ip_hton(const char *src, int af, fr_ipaddr_t *dst);
425 const char      *ip_ntoh(const fr_ipaddr_t *src, char *dst, size_t cnt);
426 int fr_ipaddr2sockaddr(const fr_ipaddr_t *ipaddr, int port,
427                        struct sockaddr_storage *sa, socklen_t *salen);
428 int fr_sockaddr2ipaddr(const struct sockaddr_storage *sa, socklen_t salen,
429                        fr_ipaddr_t *ipaddr, int * port);
430
431
432 #ifdef ASCEND_BINARY
433 /* filters.c */
434 int             ascend_parse_filter(VALUE_PAIR *pair);
435 void            print_abinary(const VALUE_PAIR *vp, char *buffer, size_t len, int delimitst);
436 #endif /*ASCEND_BINARY*/
437
438 /* random numbers in isaac.c */
439 /* context of random number generator */
440 typedef struct fr_randctx {
441   uint32_t randcnt;
442   uint32_t randrsl[256];
443   uint32_t randmem[256];
444   uint32_t randa;
445   uint32_t randb;
446   uint32_t randc;
447 } fr_randctx;
448
449 void fr_isaac(fr_randctx *ctx);
450 void fr_randinit(fr_randctx *ctx, int flag);
451 uint32_t fr_rand(void); /* like rand(), but better. */
452 void fr_rand_seed(const void *, size_t ); /* seed the random pool */
453
454
455 /* crypt wrapper from crypt.c */
456 int fr_crypt_check(const char *key, const char *salt);
457
458 /* rbtree.c */
459 typedef struct rbtree_t rbtree_t;
460 typedef struct rbnode_t rbnode_t;
461
462 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
463                                void (*freeNode)(void *),
464                                int replace_flag);
465 void            rbtree_free(rbtree_t *tree);
466 int             rbtree_insert(rbtree_t *tree, void *Data);
467 rbnode_t        *rbtree_insertnode(rbtree_t *tree, void *Data);
468 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
469 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
470 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
471 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
472 int             rbtree_num_elements(rbtree_t *tree);
473 void           *rbtree_min(rbtree_t *tree);
474 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
475
476 /* callback order for walking  */
477 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
478
479 /*
480  *      The callback should be declared as:
481  *      int callback(void *context, void *data)
482  *
483  *      The "context" is some user-defined context.
484  *      The "data" is the pointer to the user data in the node,
485  *        NOT the node itself.
486  *
487  *      It should return 0 if all is OK, and !0 for any error.
488  *      The walking will stop on any error.
489  */
490 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
491
492 /*
493  *      FIFOs
494  */
495 typedef struct fr_fifo_t fr_fifo_t;
496 typedef void (*fr_fifo_free_t)(void *);
497 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
498 void fr_fifo_free(fr_fifo_t *fi);
499 int fr_fifo_push(fr_fifo_t *fi, void *data);
500 void *fr_fifo_pop(fr_fifo_t *fi);
501 void *fr_fifo_peek(fr_fifo_t *fi);
502 int fr_fifo_num_elements(fr_fifo_t *fi);
503
504 #ifdef __cplusplus
505 }
506 #endif
507
508 #include <freeradius-devel/packet.h>
509
510 #endif /*LIBRADIUS_H*/