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