made pairfree() take a VALUE_PAIR**, not a VALUE_PAIR*. This
[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_ERRNO_H
23 #include <errno.h>
24 #endif
25
26 #include <stdio.h>
27
28 /*
29  *  Check for inclusion of <time.h>, versus <sys/time.h>
30  *  Taken verbatim from the autoconf manual.
31  */
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #include "radius.h"
44 #include "token.h"
45
46 #ifdef SIZEOF_UNSIGNED_INT
47 #if SIZEOF_UNSIGNED_INT != 4
48 #error FATAL: sizeof(unsigned int) != 4
49 #endif
50 #endif
51
52 #define AUTH_VECTOR_LEN         16
53 #define CHAP_VALUE_LENGTH       16
54 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
55
56 #define PW_AUTH_UDP_PORT                1645
57 #define PW_ACCT_UDP_PORT                1646
58
59 #ifdef _LIBRADIUS
60 #  define AUTH_HDR_LEN          20
61 #  define VENDORPEC_USR         429
62 #  define VENDOR(x)             (x >> 16)
63 #  define DEBUG                 if (librad_debug) printf
64 #  define debug_pair(vp)        do { if (librad_debug) { \
65                                         putchar('\t'); \
66                                         vp_print(stdout, vp); \
67                                         putchar('\n'); \
68                                      } \
69                                 } while(0)
70 #endif
71
72 typedef struct dict_attr {
73         char                    name[32];
74         int                     attr;
75         int                     type;
76         int                     vendor;
77         struct dict_attr        *next;
78 } DICT_ATTR;
79
80 typedef struct dict_value {
81         char                    name[32];
82         char                    attrname[32];
83         int                     attr;
84         int                     value;
85         struct dict_value       *next;
86 } DICT_VALUE;
87
88 typedef struct dict_vendor {
89         char                    vendorname[32];
90         int                     vendorpec;
91         int                     vendorcode;
92         struct dict_vendor      *next;
93 } DICT_VENDOR;
94
95 typedef struct value_pair {
96         char                    name[32];
97         int                     attribute;
98         int                     type;
99         int                     length; /* of strvalue */
100         uint32_t                lvalue;
101         int                     operator;
102         int                     addport;
103         uint8_t                 strvalue[MAX_STRING_LEN];
104         struct value_pair       *next;
105 } VALUE_PAIR;
106
107 /*
108  *      vector:         Request authenticator from access-request packet
109  *                      Put in there by rad_decode, and must be put in the
110  *                      response RADIUS_PACKET as well before calling rad_send
111  *
112  *      verified:       Filled in by rad_decode for accounting-request packets
113  *
114  *      data,data_len:  Used between rad_recv and rad_decode.
115  */
116 typedef struct radius_packet {
117         int                     sockfd;
118         uint32_t                src_ipaddr;
119         uint32_t                dst_ipaddr;
120         u_short                 src_port;
121         u_short                 dst_port;
122         int                     id;
123         int                     code;
124         uint8_t                 vector[AUTH_VECTOR_LEN];
125         time_t                  timestamp;
126         int                     verified;
127         uint8_t                 *data;
128         int                     data_len;
129         VALUE_PAIR              *vps;
130 } RADIUS_PACKET;
131
132 /*
133  *      Printing functions.
134  */
135 void            librad_safeprint(char *in, int inlen, char *out, int outlen);
136 int     vp_prints_value(char *out, int outlen, VALUE_PAIR *vp,int delimitst);
137 int     vp_prints(char *out, int outlen, VALUE_PAIR *vp);
138 void            vp_print(FILE *, VALUE_PAIR *);
139 void            vp_printlist(FILE *, VALUE_PAIR *);
140 #define         fprint_attr_val vp_print
141
142 /*
143  *      Dictionary functions.
144  */
145 int             dict_addvendor(const char *name, int value);
146 int             dict_addattr(const char *name, int vendor, int type, int value);
147 int             dict_addvalue(const char *namestr, char *attrstr, int value);
148 int             dict_init(const char *dir, const char *fn);
149 DICT_ATTR       *dict_attrbyvalue(int attr);
150 DICT_ATTR       *dict_attrbyname(const char *attr);
151 DICT_VALUE      *dict_valbyattr(int attr, int val);
152 DICT_VALUE      *dict_valbyname(const char *val);
153 int             dict_vendorcode(int);
154 int             dict_vendorpec(int);
155 int             dict_vendorname(const char *name);
156
157 #if 1 /* FIXME: compat */
158 #define dict_attrget    dict_attrbyvalue
159 #define dict_attrfind   dict_attrbyname
160 #define dict_valfind    dict_valbyname
161 /*#define dict_valget   dict_valbyattr almost but not quite*/
162 #endif
163
164 /* md5.c */
165
166 void            librad_md5_calc(u_char *, u_char *, u_int);
167
168 /* radius.c */
169 int             rad_send(RADIUS_PACKET *, const char *secret);
170 RADIUS_PACKET   *rad_recv(int fd);
171 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, const char *secret);
172 RADIUS_PACKET   *rad_alloc(int newvector);
173 void            rad_free(RADIUS_PACKET **);
174 int             rad_pwencode(char *encpw, int *len, const char *secret, const char *vector);
175 int             rad_pwdecode(char *encpw, int len, const char *secret, const char *vector);
176 int             rad_chap_encode(RADIUS_PACKET *packet, char *output, int id, VALUE_PAIR *password);
177 int             calc_acctdigest(RADIUS_PACKET *packet, const char *secret,
178                         char *data, int len);
179
180 /* valuepair.c */
181 VALUE_PAIR      *paircreate(int attr, int type);
182 void            pairfree(VALUE_PAIR **);
183 VALUE_PAIR      *pairfind(VALUE_PAIR *, int);
184 void            pairdelete(VALUE_PAIR **, int);
185 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
186 VALUE_PAIR      *paircopy(VALUE_PAIR *vp);
187 VALUE_PAIR      *paircopy2(VALUE_PAIR *vp, int attr);
188 void            pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
189 void            pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr);
190 VALUE_PAIR      *pairmake(const char *attribute, const char *value, int operator);
191 VALUE_PAIR      *pairread(char **ptr, int *eol);
192 int             userparse(char *buffer, VALUE_PAIR **first_pair);
193
194 /*
195  *      Error functions.
196  */
197 #ifdef _LIBRADIUS
198 void            librad_log(const char *, ...)
199 #ifdef __GNUC__
200                 __attribute__ ((format (printf, 1, 2)))
201 #endif
202 ;
203 #endif
204 void            librad_perror(const char *, ...)
205 #ifdef __GNUC__
206                 __attribute__ ((format (printf, 1, 2)))
207 #endif
208 ;
209 extern char     librad_errstr[];
210 extern int      librad_dodns;
211 extern int      librad_debug;
212
213 /*
214  *      Several handy miscellaneous functions.
215  */
216 char *          ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
217 uint32_t        ip_getaddr (const char *);
218 char *          ip_ntoa(char *, uint32_t);
219 uint32_t        ip_addr(const char *);
220 char            *strNcpy(char *dest, const char *src, int n);
221 void            rad_lowercase(char *str);
222 void            rad_rmspace(char *str);
223
224 #ifdef ASCEND_BINARY
225 /* filters.c */
226 int             filterBinary(VALUE_PAIR *pair, const char *valstr);
227 void            print_abinary(VALUE_PAIR *vp, u_char *buffer, int len);
228 #endif /*ASCEND_BINARY*/
229
230 #endif /*LIBRADIUS_H*/