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