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