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