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