Massively cleaned up #include's, so they're in a consistent
[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 2001,2002,2003,2004,2005,2006  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) & 0x7fff)
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 (librad_debug) printf
70 #  define debug_pair(vp)        do { if (librad_debug) { \
71                                         putchar('\t'); \
72                                         vp_print(stdout, vp); \
73                                         putchar('\n'); \
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            caseless : 1; /* case insensitive compares */
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
102         int8_t                  tag;          /* tag for tunneled attributes */
103         uint8_t                 encrypt;      /* encryption method */
104 } ATTR_FLAGS;
105
106 /*
107  *  Values of the encryption flags.
108  */
109 #define FLAG_ENCRYPT_NONE            (0)
110 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
111 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
112 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
113
114 typedef struct dict_attr {
115         char                    name[40];
116         int                     attr;
117         int                     type;
118         int                     vendor;
119         ATTR_FLAGS              flags;
120 } DICT_ATTR;
121
122 typedef struct dict_value {
123         int                     attr;
124         int                     value;
125         char                    name[1];
126 } DICT_VALUE;
127
128 typedef struct dict_vendor {
129         int                     vendorpec;
130         int                     type; /* length of type data */
131         int                     length; /* length of length data */
132         char                    name[1];
133 } DICT_VENDOR;
134
135 typedef struct value_pair {
136         char                    name[40];
137         int                     attribute;
138         int                     vendor;
139         int                     type;
140         int                     length; /* of data */
141         LRAD_TOKEN              operator;
142         uint32_t                lvalue; /* DELETE ME ASAP */
143         ATTR_FLAGS              flags;
144         struct value_pair       *next;
145         union {
146                 char                    strvalue[MAX_STRING_LEN];
147                 uint8_t                 octets[MAX_STRING_LEN];
148                 struct in_addr          ipaddr;
149                 struct in6_addr         ipv6addr;
150                 uint32_t                date;
151                 uint32_t                integer;
152                 uint8_t                 filter[32];
153                 uint8_t                 ifid[8]; /* struct? */
154                 uint8_t                 ipv6prefix[18]; /* struct? */
155         } data;
156 } VALUE_PAIR;
157 #define vp_strvalue   data.strvalue
158 #define vp_octets     data.octets
159 #define vp_ipaddr     lvalue
160 #define vp_ipv6addr   data.ipv6addr
161 #define vp_date       lvalue
162 #define vp_integer    lvalue
163 #define vp_ifid       data.ifid
164 #define vp_ipv6prefix data.ipv6prefix
165 #define vp_filter     data.filter
166
167
168 typedef struct lrad_ipaddr_t {
169         int             af;     /* address family */
170         union {
171                 struct in_addr  ip4addr;
172                 struct in6_addr ip6addr; /* maybe defined in missing.h */
173         } ipaddr;
174 } lrad_ipaddr_t;
175
176
177 /*
178  *      vector:         Request authenticator from access-request packet
179  *                      Put in there by rad_decode, and must be put in the
180  *                      response RADIUS_PACKET as well before calling rad_send
181  *
182  *      verified:       Filled in by rad_decode for accounting-request packets
183  *
184  *      data,data_len:  Used between rad_recv and rad_decode.
185  */
186 typedef struct radius_packet {
187         int                     sockfd;
188         lrad_ipaddr_t           src_ipaddr;
189         lrad_ipaddr_t           dst_ipaddr;
190         uint16_t                src_port;
191         uint16_t                dst_port;
192         int                     id;
193         unsigned int            code;
194         uint8_t                 vector[AUTH_VECTOR_LEN];
195         time_t                  timestamp;
196         int                     verified;
197         uint8_t                 *data;
198         int                     data_len;
199         VALUE_PAIR              *vps;
200         uint32_t                hash;
201 } RADIUS_PACKET;
202
203 /*
204  *      Printing functions.
205  */
206 void            librad_safeprint(char *in, int inlen, char *out, int outlen);
207 int     vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
208 int     vp_prints(char *out, int outlen, VALUE_PAIR *vp);
209 void            vp_print(FILE *, VALUE_PAIR *);
210 void            vp_printlist(FILE *, VALUE_PAIR *);
211 #define         fprint_attr_val vp_print
212
213 /*
214  *      Dictionary functions.
215  */
216 int             dict_addvendor(const char *name, int value);
217 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
218 int             dict_addvalue(const char *namestr, const char *attrstr, int value);
219 int             dict_init(const char *dir, const char *fn);
220 void            dict_free(void);
221 DICT_ATTR       *dict_attrbyvalue(int attr);
222 DICT_ATTR       *dict_attrbyname(const char *attr);
223 DICT_VALUE      *dict_valbyattr(int attr, int val);
224 DICT_VALUE      *dict_valbyname(int attr, const char *val);
225 int             dict_vendorbyname(const char *name);
226 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
227
228 #if 1 /* FIXME: compat */
229 #define dict_attrget    dict_attrbyvalue
230 #define dict_attrfind   dict_attrbyname
231 #define dict_valfind    dict_valbyname
232 /*#define dict_valget   dict_valbyattr almost but not quite*/
233 #endif
234
235 /* get around diffrent ctime_r styles */
236 #ifdef CTIMERSTYLE
237 #if CTIMERSTYLE == SOLARISSTYLE
238 #define CTIME_R(a,b,c) ctime_r(a,b,c)
239 #else
240 #define CTIME_R(a,b,c) ctime_r(a,b)
241 #endif
242 #else
243 #define CTIME_R(a,b,c) ctime_r(a,b)
244 #endif
245
246 /* md5.c */
247
248 void            librad_md5_calc(uint8_t *, const uint8_t *, unsigned int);
249
250 /* hmac.c */
251
252 void lrad_hmac_md5(const uint8_t *text, int text_len,
253                    const uint8_t *key, int key_len,
254                    unsigned char *digest);
255
256 /* hmacsha1.c */
257
258 void lrad_hmac_sha1(const uint8_t *text, int text_len,
259                     const uint8_t *key, int key_len,
260                     uint8_t *digest);
261
262 /* radius.c */
263 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
264 int             rad_packet_ok(RADIUS_PACKET *packet);
265 RADIUS_PACKET   *rad_recv(int fd);
266 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
267                            const char *secret);
268 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
269 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
270                            const char *secret);
271 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
272                          const char *secret);
273
274 RADIUS_PACKET   *rad_alloc(int newvector);
275 void            rad_free(RADIUS_PACKET **);
276 int             rad_pwencode(char *encpw, int *len, const char *secret,
277                              const uint8_t *vector);
278 int             rad_pwdecode(char *encpw, int len, const char *secret,
279                              const uint8_t *vector);
280 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret,
281                                     const uint8_t *vector);
282 int             rad_tunnel_pwdecode(uint8_t *encpw, int *len,
283                                     const char *secret, const uint8_t *vector);
284 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
285                                 int id, VALUE_PAIR *password);
286 VALUE_PAIR      *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
287                              const char *secret, int attribute, int length,
288                              const uint8_t *data);
289 int             rad_vp2attr(const RADIUS_PACKET *packet,
290                             const RADIUS_PACKET *original, const char *secret,
291                             const VALUE_PAIR *vp, uint8_t *ptr);
292
293 /* valuepair.c */
294 VALUE_PAIR      *paircreate(int attr, int type);
295 void            pairfree(VALUE_PAIR **);
296 void            pairbasicfree(VALUE_PAIR *pair);
297 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
298 void            pairdelete(VALUE_PAIR **, int);
299 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
300 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
301 int             paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
302 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
303 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
304 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
305 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
306 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
307 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
308 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
309 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
310 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
311
312 /*
313  *      Error functions.
314  */
315 #ifdef _LIBRADIUS
316 void            librad_log(const char *, ...)
317 #ifdef __GNUC__
318                 __attribute__ ((format (printf, 1, 2)))
319 #endif
320 ;
321 #endif
322 void            librad_perror(const char *, ...)
323 #ifdef __GNUC__
324                 __attribute__ ((format (printf, 1, 2)))
325 #endif
326 ;
327 extern char     librad_errstr[];
328 extern int      librad_dodns;   /* 0 = no dns lookups */
329 extern int      librad_debug;   /* 0 = no debugging information */
330 extern int      librad_max_attributes; /* per incoming packet */
331
332 /*
333  *      Several handy miscellaneous functions.
334  */
335 const char *    ip_ntoa(char *, uint32_t);
336 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
337 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
338 int             rad_lockfd(int fd, int lock_len);
339 int             rad_lockfd_nonblock(int fd, int lock_len);
340 int             rad_unlockfd(int fd, int lock_len);
341 void            lrad_bin2hex(const uint8_t *bin, char *hex, int len);
342 int             lrad_hex2bin(const char *hex, uint8_t *bin, int len);
343 #ifndef HAVE_INET_PTON
344 int             inet_pton(int af, const char *src, void *dst);
345 #endif
346 #ifndef HAVE_INET_NTOP
347 const char      *inet_ntop(int af, const void *src, char *dst, size_t cnt);
348 #endif
349 #ifndef HAVE_CLOSEFROM
350 int             closefrom(int fd);
351 #endif
352
353 int             ip_hton(const char *src, int af, lrad_ipaddr_t *dst);
354 const char      *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt);
355
356
357 #ifdef ASCEND_BINARY
358 /* filters.c */
359 int             ascend_parse_filter(VALUE_PAIR *pair);
360 void            print_abinary(VALUE_PAIR *vp, char *buffer, int len);
361 #endif /*ASCEND_BINARY*/
362
363 /* random numbers in isaac.c */
364 /* context of random number generator */
365 typedef struct lrad_randctx {
366   uint32_t randcnt;
367   uint32_t randrsl[256];
368   uint32_t randmem[256];
369   uint32_t randa;
370   uint32_t randb;
371   uint32_t randc;
372 } lrad_randctx;
373
374 void lrad_isaac(lrad_randctx *ctx);
375 void lrad_randinit(lrad_randctx *ctx, int flag);
376 uint32_t lrad_rand(void);       /* like rand(), but better. */
377 void lrad_rand_seed(const void *, size_t ); /* seed the random pool */
378
379
380 /* crypt wrapper from crypt.c */
381 int lrad_crypt_check(const char *key, const char *salt);
382
383 /* rbtree.c */
384 typedef struct rbtree_t rbtree_t;
385 typedef struct rbnode_t rbnode_t;
386
387 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
388                                void (*freeNode)(void *),
389                                int replace_flag);
390 void            rbtree_free(rbtree_t *tree);
391 int             rbtree_insert(rbtree_t *tree, void *Data);
392 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
393 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
394 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
395 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
396 int             rbtree_num_elements(rbtree_t *tree);
397 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
398
399 /* callback order for walking  */
400 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
401
402 /*
403  *      The callback should be declared as:
404  *      int callback(void *context, void *data)
405  *
406  *      The "context" is some user-defined context.
407  *      The "data" is the pointer to the user data in the node,
408  *        NOT the node itself.
409  *
410  *      It should return 0 if all is OK, and !0 for any error.
411  *      The walking will stop on any error.
412  */
413 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
414
415 /*
416  *      FIFOs
417  */
418 typedef struct lrad_fifo_t lrad_fifo_t;
419 typedef void (*lrad_fifo_free_t)(void *);
420 lrad_fifo_t *lrad_fifo_create(int max_entries, lrad_fifo_free_t freeNode);
421 void lrad_fifo_free(lrad_fifo_t *fi);
422 int lrad_fifo_push(lrad_fifo_t *fi, void *data);
423 void *lrad_fifo_pop(lrad_fifo_t *fi);
424
425 #include <freeradius-devel/packet.h>
426
427 #endif /*LIBRADIUS_H*/