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