Patches to print/parse IPv6 interface ID's.
[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  */
11
12 #include "autoconf.h"
13
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif
17
18 #ifdef HAVE_STDINT_H
19 #include <stdint.h>
20 #endif
21
22 #ifdef HAVE_INTTYPES_H
23 #include <inttypes.h>
24 #endif
25
26 #ifdef HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29
30 #include <stdio.h>
31
32 /*
33  *  Check for inclusion of <time.h>, versus <sys/time.h>
34  *  Taken verbatim from the autoconf manual.
35  */
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # ifdef HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include "radius.h"
48 #include "token.h"
49
50 #ifdef SIZEOF_UNSIGNED_INT
51 #if SIZEOF_UNSIGNED_INT != 4
52 #error FATAL: sizeof(unsigned int) != 4
53 #endif
54 #endif
55
56 /*
57  *  Include for modules.
58  */
59 #include <sha1.h>
60 #include <md4.h>
61
62 #define EAP_START               2
63
64 #define AUTH_VECTOR_LEN         16
65 #define CHAP_VALUE_LENGTH       16
66 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
67
68 #ifdef _LIBRADIUS
69 #  define AUTH_HDR_LEN          20
70 #  define VENDORPEC_USR         429
71 #  define VENDOR(x)             ((x >> 16) & 0xffff)
72 #  define DEBUG                 if (librad_debug) printf
73 #  define debug_pair(vp)        do { if (librad_debug) { \
74                                         putchar('\t'); \
75                                         vp_print(stdout, vp); \
76                                         putchar('\n'); \
77                                      } \
78                                 } while(0)
79 #  define TAG_VALID(x)          ((x) > 0 && (x) < 0x20)
80 #  define TAG_VALID_ZERO(x)     ((x) >= 0 && (x) < 0x20)
81 #  define TAG_ANY               -128   /* minimum signed char */
82 #endif
83
84 typedef struct attr_flags {
85         char                    addport;        /* Add port to IP address */
86         char                    has_tag;        /* attribute allows tags */
87         signed char             tag;
88         uint8_t                 encrypt;        /* encryption method */
89         signed char             len_disp;       /* length displacement */
90         char                    do_xlat;
91 } ATTR_FLAGS;
92
93 /*
94  *  Values of the encryption flags.
95  */
96 #define FLAG_ENCRYPT_NONE            (0)
97 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
98 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
99 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
100
101 typedef struct dict_attr {
102         char                    name[40];
103         int                     attr;
104         int                     type;
105         int                     vendor;
106         ATTR_FLAGS              flags;
107         struct dict_attr        *next;
108 } DICT_ATTR;
109
110 typedef struct dict_value {
111         char                    name[40];
112         char                    attrname[40];
113         int                     attr;
114         int                     value;
115         struct dict_value       *next;
116 } DICT_VALUE;
117
118 typedef struct dict_vendor {
119         char                    vendorname[40];
120         int                     vendorpec;
121         struct dict_vendor      *next;
122 } DICT_VENDOR;
123
124 typedef struct value_pair {
125         char                    name[40];
126         int                     attribute;
127         int                     type;
128         int                     length; /* of strvalue */
129         uint32_t                lvalue;
130         LRAD_TOKEN              operator;
131         uint8_t                 strvalue[MAX_STRING_LEN];
132         ATTR_FLAGS              flags;
133         struct value_pair       *next;
134 } VALUE_PAIR;
135
136 /*
137  *      vector:         Request authenticator from access-request packet
138  *                      Put in there by rad_decode, and must be put in the
139  *                      response RADIUS_PACKET as well before calling rad_send
140  *
141  *      verified:       Filled in by rad_decode for accounting-request packets
142  *
143  *      data,data_len:  Used between rad_recv and rad_decode.
144  */
145 typedef struct radius_packet {
146         int                     sockfd;
147         uint32_t                src_ipaddr;
148         uint32_t                dst_ipaddr;
149         u_short                 src_port;
150         u_short                 dst_port;
151         int                     id;
152         int                     code;
153         uint8_t                 vector[AUTH_VECTOR_LEN];
154         time_t                  timestamp;
155         int                     verified;
156         uint8_t                 *data;
157         int                     data_len;
158         VALUE_PAIR              *vps;
159 } RADIUS_PACKET;
160
161 /*
162  *      Printing functions.
163  */
164 void            librad_safeprint(char *in, int inlen, char *out, int outlen);
165 int     vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
166 int     vp_prints(char *out, int outlen, VALUE_PAIR *vp);
167 void            vp_print(FILE *, VALUE_PAIR *);
168 void            vp_printlist(FILE *, VALUE_PAIR *);
169 #define         fprint_attr_val vp_print
170
171 /*
172  *      Dictionary functions.
173  */
174 int             dict_addvendor(const char *name, int value);
175 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
176 int             dict_addvalue(const char *namestr, char *attrstr, int value);
177 int             dict_init(const char *dir, const char *fn);
178 DICT_ATTR       *dict_attrbyvalue(int attr);
179 DICT_ATTR       *dict_attrbyname(const char *attr);
180 DICT_VALUE      *dict_valbyattr(int attr, int val);
181 DICT_VALUE      *dict_valbyname(int attr, const char *val);
182 int             dict_vendorname(const char *name);
183
184 /*
185  *  Compatibility
186  */
187 #define dict_vendorcode
188 #define dict_vendorpec
189
190
191 #if 1 /* FIXME: compat */
192 #define dict_attrget    dict_attrbyvalue
193 #define dict_attrfind   dict_attrbyname
194 #define dict_valfind    dict_valbyname
195 /*#define dict_valget   dict_valbyattr almost but not quite*/
196 #endif
197
198 /* md5.c */
199
200 void            librad_md5_calc(u_char *, u_char *, u_int);
201
202 /* hmac.c */
203
204 void lrad_hmac_md5(const unsigned char *text, int text_len,
205                    const unsigned char *key, int key_len,
206                    unsigned char *digest);
207
208 /* radius.c */
209 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
210 RADIUS_PACKET   *rad_recv(int fd);
211 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
212 RADIUS_PACKET   *rad_alloc(int newvector);
213 void            rad_free(RADIUS_PACKET **);
214 int             rad_pwencode(char *encpw, int *len, const char *secret, const char *vector);
215 int             rad_pwdecode(char *encpw, int len, const char *secret, const char *vector);
216 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret, const char *vector);
217 int             rad_tunnel_pwdecode(char *encpw, int *len, const char *secret, const char *vector);
218 int             rad_chap_encode(RADIUS_PACKET *packet, char *output, int id, VALUE_PAIR *password);
219
220 /* valuepair.c */
221 VALUE_PAIR      *paircreate(int attr, int type);
222 void            pairfree(VALUE_PAIR **);
223 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
224 void            pairdelete(VALUE_PAIR **, int);
225 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
226 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
227 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
228 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
229 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
230 VALUE_PAIR      *pairparsevalue(VALUE_PAIR *vp, const char *value);
231 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
232 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
233 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
234
235 /*
236  *      Error functions.
237  */
238 #ifdef _LIBRADIUS
239 void            librad_log(const char *, ...)
240 #ifdef __GNUC__
241                 __attribute__ ((format (printf, 1, 2)))
242 #endif
243 ;
244 #endif
245 void            librad_perror(const char *, ...)
246 #ifdef __GNUC__
247                 __attribute__ ((format (printf, 1, 2)))
248 #endif
249 ;
250 extern char     librad_errstr[];
251 extern int      librad_dodns;   /* 0 = no dns lookups */
252 extern int      librad_debug;   /* 0 = no debugging information */
253 extern int      librad_max_attributes; /* per incoming packet */
254
255 /*
256  *      Several handy miscellaneous functions.
257  */
258 char *          ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
259 uint32_t        ip_getaddr (const char *);
260 char *          ip_ntoa(char *, uint32_t);
261 uint32_t        ip_addr(const char *);
262 char            *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid);
263 uint8_t         *ifid_aton(const char *ifid_str, uint8_t *ifid);
264 char            *strNcpy(char *dest, const char *src, int n);
265 void            rad_lowercase(char *str);
266 void            rad_rmspace(char *str);
267 int             rad_lockfd(int fd, int lock_len);
268 int             rad_lockfd_nonblock(int fd, int lock_len);
269 int             rad_unlockfd(int fd, int lock_len);
270
271 #ifdef ASCEND_BINARY
272 /* filters.c */
273 int             filterBinary(VALUE_PAIR *pair, const char *valstr);
274 void            print_abinary(VALUE_PAIR *vp, u_char *buffer, int len);
275 #endif /*ASCEND_BINARY*/
276
277 #ifdef HAVE_LOCAL_SNPRINTF
278 #include <stdarg.h>
279 int snprintf(char *str, size_t count, const char *fmt, ...);
280 int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
281 #endif
282
283 /* random numbers in isaac.c */
284 /* context of random number generator */
285 typedef struct lrad_randctx {
286   uint32_t randcnt;
287   uint32_t randrsl[256];
288   uint32_t randmem[256];
289   uint32_t randa;
290   uint32_t randb;
291   uint32_t randc;
292 } lrad_randctx;
293
294 void lrad_isaac(lrad_randctx *ctx);
295 void lrad_randinit(lrad_randctx *ctx, int flag);
296 uint32_t lrad_rand(void);       /* like rand(), but better. */
297
298 void lrad_lmpwdhash(const unsigned char *password,unsigned char *lmhash);
299 void lrad_mschap(const unsigned char *win_password,
300                  const unsigned char *challenge, unsigned char *response);
301
302
303 /* crypt wrapper from crypt.c */
304 int lrad_crypt_check(const char *key, const char *salt);
305
306 #endif /*LIBRADIUS_H*/