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