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