Now that sha1 and md4 are in 'lib', their header files should be
[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 #if HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif
17
18 #if HAVE_STDINT_H
19 #include <stdint.h>
20 #endif
21
22 #if HAVE_INTTYPES_H
23 #include <inttypes.h>
24 #endif
25
26 #if 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 # if 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 #define PW_AUTH_UDP_PORT                1812
69 #define PW_ACCT_UDP_PORT                1813
70
71 #ifdef _LIBRADIUS
72 #  define AUTH_HDR_LEN          20
73 #  define VENDORPEC_USR         429
74 #  define VENDOR(x)             (x >> 16)
75 #  define DEBUG                 if (librad_debug) printf
76 #  define debug_pair(vp)        do { if (librad_debug) { \
77                                         putchar('\t'); \
78                                         vp_print(stdout, vp); \
79                                         putchar('\n'); \
80                                      } \
81                                 } while(0)
82 #  define TAG_VALID(x)          ((x) > 0 && (x) < 0x20)
83 #  define TAG_VALID_ZERO(x)     ((x) >= 0 && (x) < 0x20)
84 #  define TAG_ANY               -128   /* minimum signed char */
85 #endif
86
87 typedef struct attr_flags {
88         char                    addport;        /* Add port to IP address */
89         char                    has_tag;        /* attribute allows tags */
90         signed char             tag;
91         char                    encrypt;        /* encryption method */
92         signed char             len_disp;       /* length displacement */
93 } ATTR_FLAGS;
94
95 typedef struct dict_attr {
96         char                    name[40];
97         int                     attr;
98         int                     type;
99         int                     vendor;
100         ATTR_FLAGS              flags;
101         struct dict_attr        *next;
102 } DICT_ATTR;
103
104 typedef struct dict_value {
105         char                    name[40];
106         char                    attrname[40];
107         int                     attr;
108         int                     value;
109         struct dict_value       *next;
110 } DICT_VALUE;
111
112 typedef struct dict_vendor {
113         char                    vendorname[40];
114         int                     vendorpec;
115         struct dict_vendor      *next;
116 } DICT_VENDOR;
117
118 typedef struct value_pair {
119         char                    name[40];
120         int                     attribute;
121         int                     type;
122         int                     length; /* of strvalue */
123         uint32_t                lvalue;
124         LRAD_TOKEN              operator;
125         uint8_t                 strvalue[MAX_STRING_LEN];
126         ATTR_FLAGS              flags;
127         struct value_pair       *next;
128 } VALUE_PAIR;
129
130 /*
131  *      vector:         Request authenticator from access-request packet
132  *                      Put in there by rad_decode, and must be put in the
133  *                      response RADIUS_PACKET as well before calling rad_send
134  *
135  *      verified:       Filled in by rad_decode for accounting-request packets
136  *
137  *      data,data_len:  Used between rad_recv and rad_decode.
138  */
139 typedef struct radius_packet {
140         int                     sockfd;
141         uint32_t                src_ipaddr;
142         uint32_t                dst_ipaddr;
143         u_short                 src_port;
144         u_short                 dst_port;
145         int                     id;
146         int                     code;
147         uint8_t                 vector[AUTH_VECTOR_LEN];
148         time_t                  timestamp;
149         int                     verified;
150         uint8_t                 *data;
151         int                     data_len;
152         VALUE_PAIR              *vps;
153 } RADIUS_PACKET;
154
155 /*
156  *      Printing functions.
157  */
158 void            librad_safeprint(char *in, int inlen, char *out, int outlen);
159 int     vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
160 int     vp_prints(char *out, int outlen, VALUE_PAIR *vp);
161 void            vp_print(FILE *, VALUE_PAIR *);
162 void            vp_printlist(FILE *, VALUE_PAIR *);
163 #define         fprint_attr_val vp_print
164
165 /*
166  *      Dictionary functions.
167  */
168 int             dict_addvendor(const char *name, int value);
169 int             dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags);
170 int             dict_addvalue(const char *namestr, char *attrstr, int value);
171 int             dict_init(const char *dir, const char *fn);
172 DICT_ATTR       *dict_attrbyvalue(int attr);
173 DICT_ATTR       *dict_attrbyname(const char *attr);
174 DICT_VALUE      *dict_valbyattr(int attr, int val);
175 DICT_VALUE      *dict_valbyname(int attr, const char *val);
176 int             dict_vendorname(const char *name);
177
178 /*
179  *  Compatibility
180  */
181 #define dict_vendorcode
182 #define dict_vendorpec
183
184
185 #if 1 /* FIXME: compat */
186 #define dict_attrget    dict_attrbyvalue
187 #define dict_attrfind   dict_attrbyname
188 #define dict_valfind    dict_valbyname
189 /*#define dict_valget   dict_valbyattr almost but not quite*/
190 #endif
191
192 /* md5.c */
193
194 void            librad_md5_calc(u_char *, u_char *, u_int);
195
196 /* hmac.c */
197
198 void lrad_hmac_md5(const unsigned char *text, int text_len,
199                    const unsigned char *key, int key_len,
200                    unsigned char *digest);
201
202 /* radius.c */
203 int             rad_send(RADIUS_PACKET *, const RADIUS_PACKET *, const char *secret);
204 RADIUS_PACKET   *rad_recv(int fd);
205 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
206 RADIUS_PACKET   *rad_alloc(int newvector);
207 void            rad_free(RADIUS_PACKET **);
208 int             rad_pwencode(char *encpw, int *len, const char *secret, const char *vector);
209 int             rad_pwdecode(char *encpw, int len, const char *secret, const char *vector);
210 int             rad_tunnel_pwencode(char *encpw, int *len, const char *secret, const char *vector);
211 int             rad_tunnel_pwdecode(char *encpw, int *len, const char *secret, const char *vector);
212 int             rad_chap_encode(RADIUS_PACKET *packet, char *output, int id, VALUE_PAIR *password);
213
214 /* valuepair.c */
215 VALUE_PAIR      *paircreate(int attr, int type);
216 void            pairfree(VALUE_PAIR **);
217 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
218 void            pairdelete(VALUE_PAIR **, int);
219 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
220 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
221 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
222 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
223 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
224 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
225 VALUE_PAIR      *pairread(char **ptr, LRAD_TOKEN *eol);
226 LRAD_TOKEN      userparse(char *buffer, VALUE_PAIR **first_pair);
227
228 /*
229  *      Error functions.
230  */
231 #ifdef _LIBRADIUS
232 void            librad_log(const char *, ...)
233 #ifdef __GNUC__
234                 __attribute__ ((format (printf, 1, 2)))
235 #endif
236 ;
237 #endif
238 void            librad_perror(const char *, ...)
239 #ifdef __GNUC__
240                 __attribute__ ((format (printf, 1, 2)))
241 #endif
242 ;
243 extern char     librad_errstr[];
244 extern int      librad_dodns;   /* 0 = no dns lookups */
245 extern int      librad_debug;   /* 0 = no debugging information */
246 extern int      librad_max_attributes; /* per incoming packet */
247
248 /*
249  *      Several handy miscellaneous functions.
250  */
251 char *          ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
252 uint32_t        ip_getaddr (const char *);
253 char *          ip_ntoa(char *, uint32_t);
254 uint32_t        ip_addr(const char *);
255 char            *strNcpy(char *dest, const char *src, int n);
256 void            rad_lowercase(char *str);
257 void            rad_rmspace(char *str);
258 int             rad_lockfd(int fd, int lock_len);
259 int             rad_unlockfd(int fd, int lock_len);
260
261 #ifdef ASCEND_BINARY
262 /* filters.c */
263 int             filterBinary(VALUE_PAIR *pair, const char *valstr);
264 void            print_abinary(VALUE_PAIR *vp, u_char *buffer, int len);
265 #endif /*ASCEND_BINARY*/
266
267 #ifdef HAVE_LOCAL_SNPRINTF
268 #include <stdarg.h>
269 int snprintf(char *str, size_t count, const char *fmt, ...);
270 int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
271 #endif
272
273 #endif /*LIBRADIUS_H*/