API to copy just one VP.
[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 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008  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 #ifndef NDEBUG
70 #  define DEBUG                 fr_printf_log
71 #else
72         /*
73          *      Rely on the compiler to optimize it out.
74          */
75 #  define DEBUG                 if (0) fr_printf_log
76 #endif
77 #  define debug_pair(vp)        do { if (librad_debug && fr_log_fp) { \
78                                         fputc('\t', fr_log_fp); \
79                                         vp_print(fr_log_fp, vp); \
80                                         fputc('\n', fr_log_fp); \
81                                      } \
82                                 } while(0)
83 #  define TAG_VALID(x)          ((x) > 0 && (x) < 0x20)
84 #  define TAG_VALID_ZERO(x)     ((x) < 0x20)
85 #  define TAG_ANY               -128   /* minimum signed char */
86 #endif
87
88 #if defined(__GNUC__)
89 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
90 # define NEVER_RETURNS __attribute__ ((noreturn))
91 # define UNUSED __attribute__ ((unused))
92 # define BLANK_FORMAT " "       /* GCC_LINT whines about empty formats */
93 #else
94 # define PRINTF_LIKE(n) /* ignore */
95 # define NEVER_RETURNS /* ignore */
96 # define UNUSED /* ignore */
97 # define BLANK_FORMAT ""
98 #endif
99
100 typedef struct attr_flags {
101         unsigned int            addport : 1;  /* add NAS-Port to IP address */
102         unsigned int            has_tag : 1;  /* tagged attribute */
103         unsigned int            do_xlat : 1;  /* strvalue is dynamic */
104         unsigned int            unknown_attr : 1; /* not in dictionary */
105         unsigned int            array : 1; /* pack multiples into 1 attr */
106         unsigned int            has_value : 1; /* has a value */
107         unsigned int            has_value_alias : 1; /* has a value alias */
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         char                    name[40];
123         int                     attr;
124         int                     type;
125         int                     vendor;
126         ATTR_FLAGS              flags;
127 } DICT_ATTR;
128
129 typedef struct dict_value {
130         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         char                    name[1];
140 } DICT_VENDOR;
141
142 typedef union value_pair_data {
143         char                    strvalue[MAX_STRING_LEN];
144         uint8_t                 octets[MAX_STRING_LEN];
145         struct in_addr          ipaddr;
146         struct in6_addr         ipv6addr;
147         uint32_t                date;
148         uint32_t                integer;
149         uint8_t                 filter[32];
150         uint8_t                 ifid[8]; /* struct? */
151         uint8_t                 ipv6prefix[18]; /* struct? */
152         uint8_t                 ether[6];
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
175 #if 0
176 #define vp_ipaddr     data.ipaddr.s_addr
177 #define vp_date       data.date
178 #define vp_integer    data.integer
179 #else
180 /*
181  *      These are left as lvalue until we audit the source for code
182  *      that prints to vp_strvalue for integer/ipaddr/date types.
183  */
184 #define vp_ipaddr     lvalue
185 #define vp_date       lvalue
186 #define vp_integer    lvalue
187 #endif
188
189
190 typedef struct fr_ipaddr_t {
191         int             af;     /* address family */
192         union {
193                 struct in_addr  ip4addr;
194                 struct in6_addr ip6addr; /* maybe defined in missing.h */
195         } ipaddr;
196 } fr_ipaddr_t;
197
198 /*
199  *      vector:         Request authenticator from access-request packet
200  *                      Put in there by rad_decode, and must be put in the
201  *                      response RADIUS_PACKET as well before calling rad_send
202  *
203  *      verified:       Filled in by rad_decode for accounting-request packets
204  *
205  *      data,data_len:  Used between rad_recv and rad_decode.
206  */
207 typedef struct radius_packet {
208         int                     sockfd;
209         fr_ipaddr_t             src_ipaddr;
210         fr_ipaddr_t             dst_ipaddr;
211         uint16_t                src_port;
212         uint16_t                dst_port;
213         int                     id;
214         unsigned int            code;
215         uint32_t                hash;
216         uint8_t                 vector[AUTH_VECTOR_LEN];
217         time_t                  timestamp;
218         uint8_t                 *data;
219         int                     data_len;
220         VALUE_PAIR              *vps;
221         ssize_t                 offset;
222 } RADIUS_PACKET;
223
224 /*
225  *      Printing functions.
226  */
227 void            librad_safeprint(char *in, size_t inlen,
228                                  char *out, size_t outlen);
229 int             vp_prints_value(char *out, size_t outlen,
230                                 VALUE_PAIR *vp, int delimitst);
231 const char      *vp_print_name(char *buffer, size_t bufsize, int attr);
232 int             vp_prints(char *out, size_t outlen, VALUE_PAIR *vp);
233 void            vp_print(FILE *, VALUE_PAIR *);
234 void            vp_printlist(FILE *, VALUE_PAIR *);
235 #define         fprint_attr_val vp_print
236
237 /*
238  *      Dictionary functions.
239  */
240 int             dict_addvendor(const char *name, int value);
241 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
242 int             dict_addvalue(const char *namestr, const char *attrstr, int value);
243 int             dict_init(const char *dir, const char *fn);
244 void            dict_free(void);
245 DICT_ATTR       *dict_attrbyvalue(int attr);
246 DICT_ATTR       *dict_attrbyname(const char *attr);
247 DICT_VALUE      *dict_valbyattr(int attr, int val);
248 DICT_VALUE      *dict_valbyname(int attr, const char *val);
249 int             dict_vendorbyname(const char *name);
250 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
251
252 #if 1 /* FIXME: compat */
253 #define dict_attrget    dict_attrbyvalue
254 #define dict_attrfind   dict_attrbyname
255 #define dict_valfind    dict_valbyname
256 /*#define dict_valget   dict_valbyattr almost but not quite*/
257 #endif
258
259 /* get around diffrent ctime_r styles */
260 #ifdef CTIMERSTYLE
261 #if CTIMERSTYLE == SOLARISSTYLE
262 #define CTIME_R(a,b,c) ctime_r(a,b,c)
263 #else
264 #define CTIME_R(a,b,c) ctime_r(a,b)
265 #endif
266 #else
267 #define CTIME_R(a,b,c) ctime_r(a,b)
268 #endif
269
270 /* md5.c */
271
272 void            fr_md5_calc(uint8_t *, const uint8_t *, unsigned int);
273
274 /* hmac.c */
275
276 void fr_hmac_md5(const uint8_t *text, int text_len,
277                    const uint8_t *key, int key_len,
278                    unsigned char *digest);
279
280 /* hmacsha1.c */
281
282 void fr_hmac_sha1(const uint8_t *text, int text_len,
283                     const uint8_t *key, int key_len,
284                     uint8_t *digest);
285
286 /* radius.c */
287 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
288 int             rad_packet_ok(RADIUS_PACKET *packet, int flags);
289 RADIUS_PACKET   *rad_recv(int fd, int flags);
290 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
291                         int *code);
292 void            rad_recv_discard(int sockfd);
293 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
294                            const char *secret);
295 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
296 int             rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
297                            const char *secret);
298 int             rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
299                          const char *secret);
300
301 RADIUS_PACKET   *rad_alloc(int newvector);
302 void            rad_free(RADIUS_PACKET **);
303 int             rad_pwencode(char *encpw, size_t *len, const char *secret,
304                              const uint8_t *vector);
305 int             rad_pwdecode(char *encpw, size_t len, const char *secret,
306                              const uint8_t *vector);
307 int             rad_tunnel_pwencode(char *encpw, size_t *len, const char *secret,
308                                     const uint8_t *vector);
309 int             rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
310                                     const char *secret, const uint8_t *vector);
311 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
312                                 int id, VALUE_PAIR *password);
313 VALUE_PAIR      *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
314                              const char *secret, int attribute, int length,
315                              const uint8_t *data);
316 int             rad_vp2attr(const RADIUS_PACKET *packet,
317                             const RADIUS_PACKET *original, const char *secret,
318                             const VALUE_PAIR *vp, uint8_t *ptr);
319
320 /* valuepair.c */
321 VALUE_PAIR      *pairalloc(DICT_ATTR *da);
322 VALUE_PAIR      *paircreate(int attr, int type);
323 void            pairfree(VALUE_PAIR **);
324 void            pairbasicfree(VALUE_PAIR *pair);
325 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
326 void            pairdelete(VALUE_PAIR **, int);
327 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
328 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
329 int             paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
330 VALUE_PAIR      *paircopyvp(const VALUE_PAIR *vp);
331 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
332 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
333 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
334 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
335 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
336 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
337 VALUE_PAIR      *pairread(const char **ptr, FR_TOKEN *eol);
338 FR_TOKEN        userparse(const char *buffer, VALUE_PAIR **first_pair);
339 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
340
341 /*
342  *      Error functions.
343  */
344 #ifdef _LIBRADIUS
345 void            librad_log(const char *, ...)
346 #ifdef __GNUC__
347                 __attribute__ ((format (printf, 1, 2)))
348 #endif
349 ;
350 #endif
351 void            librad_perror(const char *, ...)
352 #ifdef __GNUC__
353                 __attribute__ ((format (printf, 1, 2)))
354 #endif
355 ;
356 extern char     librad_errstr[];
357 extern int      librad_dodns;   /* 0 = no dns lookups */
358 extern int      librad_debug;   /* 0 = no debugging information */
359 extern int      librad_max_attributes; /* per incoming packet */
360 extern FILE     *fr_log_fp;
361 void            fr_printf_log(const char *, ...)
362 #ifdef __GNUC__
363                 __attribute__ ((format (printf, 1, 2)))
364 #endif
365 ;
366
367 /*
368  *      Several handy miscellaneous functions.
369  */
370 const char *    ip_ntoa(char *, uint32_t);
371 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
372 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
373 int             rad_lockfd(int fd, int lock_len);
374 int             rad_lockfd_nonblock(int fd, int lock_len);
375 int             rad_unlockfd(int fd, int lock_len);
376 void            fr_bin2hex(const uint8_t *bin, char *hex, size_t len);
377 size_t          fr_hex2bin(const char *hex, uint8_t *bin, size_t len);
378 #ifndef HAVE_INET_PTON
379 int             inet_pton(int af, const char *src, void *dst);
380 #endif
381 #ifndef HAVE_INET_NTOP
382 const char      *inet_ntop(int af, const void *src, char *dst, size_t cnt);
383 #endif
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
392
393
394 #ifdef ASCEND_BINARY
395 /* filters.c */
396 int             ascend_parse_filter(VALUE_PAIR *pair);
397 void            print_abinary(VALUE_PAIR *vp, char *buffer, size_t len);
398 #endif /*ASCEND_BINARY*/
399
400 /* random numbers in isaac.c */
401 /* context of random number generator */
402 typedef struct fr_randctx {
403   uint32_t randcnt;
404   uint32_t randrsl[256];
405   uint32_t randmem[256];
406   uint32_t randa;
407   uint32_t randb;
408   uint32_t randc;
409 } fr_randctx;
410
411 void fr_isaac(fr_randctx *ctx);
412 void fr_randinit(fr_randctx *ctx, int flag);
413 uint32_t fr_rand(void); /* like rand(), but better. */
414 void fr_rand_seed(const void *, size_t ); /* seed the random pool */
415
416
417 /* crypt wrapper from crypt.c */
418 int fr_crypt_check(const char *key, const char *salt);
419
420 /* rbtree.c */
421 typedef struct rbtree_t rbtree_t;
422 typedef struct rbnode_t rbnode_t;
423
424 rbtree_t       *rbtree_create(int (*Compare)(const void *, const void *),
425                                void (*freeNode)(void *),
426                                int replace_flag);
427 void            rbtree_free(rbtree_t *tree);
428 int             rbtree_insert(rbtree_t *tree, void *Data);
429 rbnode_t        *rbtree_insertnode(rbtree_t *tree, void *Data);
430 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
431 int             rbtree_deletebydata(rbtree_t *tree, const void *data);
432 rbnode_t       *rbtree_find(rbtree_t *tree, const void *Data);
433 void           *rbtree_finddata(rbtree_t *tree, const void *Data);
434 int             rbtree_num_elements(rbtree_t *tree);
435 void           *rbtree_min(rbtree_t *tree);
436 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
437
438 /* callback order for walking  */
439 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
440
441 /*
442  *      The callback should be declared as:
443  *      int callback(void *context, void *data)
444  *
445  *      The "context" is some user-defined context.
446  *      The "data" is the pointer to the user data in the node,
447  *        NOT the node itself.
448  *
449  *      It should return 0 if all is OK, and !0 for any error.
450  *      The walking will stop on any error.
451  */
452 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
453
454 /*
455  *      FIFOs
456  */
457 typedef struct fr_fifo_t fr_fifo_t;
458 typedef void (*fr_fifo_free_t)(void *);
459 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
460 void fr_fifo_free(fr_fifo_t *fi);
461 int fr_fifo_push(fr_fifo_t *fi, void *data);
462 void *fr_fifo_pop(fr_fifo_t *fi);
463 void *fr_fifo_peek(fr_fifo_t *fi);
464 int fr_fifo_num_elements(fr_fifo_t *fi);
465
466 #include <freeradius-devel/packet.h>
467
468 #endif /*LIBRADIUS_H*/