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