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