finished attribute rewrite code, also used for username, testing needed
[libradsec.git] / radsecproxy.h
1 /*
2  * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #define DEBUG_LEVEL 3
10
11 #define CONFIG_MAIN "/etc/radsecproxy.conf"
12
13 /* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */
14 #define MAX_REQUESTS 256
15 #define REQUEST_RETRY_INTERVAL 5
16 #define REQUEST_RETRY_COUNT 2
17 #define MAX_CERT_DEPTH 5
18 #define STATUS_SERVER_PERIOD 25
19 #define IDLE_TIMEOUT 300
20 #define RAD_Access_Request 1
21 #define RAD_Access_Accept 2
22 #define RAD_Access_Reject 3
23 #define RAD_Accounting_Request 4
24 #define RAD_Accounting_Response 5
25 #define RAD_Access_Challenge 11
26 #define RAD_Status_Server 12
27 #define RAD_Status_Client 13
28
29 #define RAD_UDP 0
30 #define RAD_TLS 1
31 #define RAD_TCP 2
32 #define RAD_DTLS 3
33
34 #define RAD_Attr_User_Name 1
35 #define RAD_Attr_User_Password 2
36 #define RAD_Attr_Reply_Message 18
37 #define RAD_Attr_Vendor_Specific 26
38 #define RAD_Attr_Calling_Station_Id 31
39 #define RAD_Attr_Tunnel_Password 69
40 #define RAD_Attr_Message_Authenticator 80
41
42 #define RAD_VS_ATTR_MS_MPPE_Send_Key 16
43 #define RAD_VS_ATTR_MS_MPPE_Recv_Key 17
44
45 struct options {
46     char **listenudp;
47     char **listentcp;
48     char **listentls;
49     char **listendtls;
50     char **listenaccudp;
51     char *sourceudp;
52     char *sourcetcp;
53     char *sourcetls;
54     char *sourcedtls;
55     char *logdestination;
56     uint8_t loglevel;
57     uint8_t loopprevention;
58 };
59
60 /* requests that our client will send */
61 struct request {
62     unsigned char *buf;
63     uint8_t tries;
64     uint8_t received;
65     struct timeval expiry;
66     struct client *from;
67     char *origusername;
68     uint8_t origid; /* used by servwr */
69     char origauth[16]; /* used by servwr */
70     struct sockaddr_storage fromsa; /* used by udpservwr */
71     int fromudpsock; /* used by udpservwr */
72 };
73
74 /* replies that a server will send */
75 struct reply {
76     unsigned char *buf;
77     struct sockaddr_storage tosa; /* used by udpservwr */
78     int toudpsock; /* used by udpservwr */
79 };
80
81 struct queue {
82     struct list *entries;
83     pthread_mutex_t mutex;
84     pthread_cond_t cond;
85 };
86
87 struct clsrvconf {
88     char *name;
89     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
90     const struct protodefs *pdef;
91     char *host;
92     char *port;
93     char *secret;
94     char *tls;
95     char *matchcertattr;
96     regex_t *certcnregex;
97     regex_t *certuriregex;
98     char *confrewritein;
99     char *confrewriteout;
100     char *confrewriteusername;
101     struct modattr *rewriteusername;
102     char *dynamiclookupcommand;
103     uint8_t statusserver;
104     uint8_t retryinterval;
105     uint8_t retrycount;
106     uint8_t certnamecheck;
107     SSL_CTX *ssl_ctx;
108     struct rewrite *rewritein;
109     struct rewrite *rewriteout;
110     struct addrinfo *addrinfo;
111     uint8_t prefixlen;
112     struct list *clients;
113     struct server *servers;
114 };
115
116 struct client {
117     struct clsrvconf *conf;
118     int sock; /* for tcp/dtls */
119     SSL *ssl;
120     struct queue *replyq;
121     struct queue *rbios; /* for dtls */
122     struct sockaddr_storage addr; /* for dtls */
123 };
124
125 struct server {
126     struct clsrvconf *conf;
127     int sock;
128     SSL *ssl;
129     pthread_mutex_t lock;
130     pthread_t clientth;
131     uint8_t clientrdgone;
132     struct timeval lastconnecttry;
133     struct timeval lastreply;
134     uint8_t connectionok;
135     uint8_t lostrqs;
136     char *dynamiclookuparg;
137     int nextid;
138     struct timeval lastrcv;
139     struct request *requests;
140     uint8_t newrq;
141     pthread_mutex_t newrq_mutex;
142     pthread_cond_t newrq_cond;
143     struct queue *rbios; /* for dtls */
144 };
145
146 struct realm {
147     char *name;
148     char *message;
149     uint8_t accresp;
150     regex_t regex;
151     pthread_mutex_t subrealms_mutex;
152     struct list *subrealms;
153     struct list *srvconfs;
154     struct list *accsrvconfs;
155 };
156
157 struct tls {
158     char *name;
159     char *cacertfile;
160     char *cacertpath;
161     char *certfile;
162     char *certkeyfile;
163     char *certkeypwd;
164     uint8_t crlcheck;
165     SSL_CTX *tlsctx;
166     SSL_CTX *dtlsctx;
167 };
168
169 struct attribute {
170     uint8_t t;
171     uint8_t l;
172     uint8_t *v;
173 };
174
175 struct modattr {
176     uint8_t t;
177     char *replacement;
178     regex_t *regex;
179 };
180
181 struct rewrite {
182     uint8_t *removeattrs;
183     uint32_t *removevendorattrs;
184     struct list *addattrs;
185     struct list *modattrs;
186 };
187
188 struct protodefs {
189     char *name;
190     char *secretdefault;
191     uint8_t socktype;
192     char *portdefault;
193     uint8_t retrycountdefault;
194     uint8_t retrycountmax;
195     uint8_t retryintervaldefault;
196     uint8_t retryintervalmax;
197     void *(*listener)(void*);
198     char **srcaddrport;
199     int (*connecter)(struct server *, struct timeval *, int, char *);
200     void *(*clientconnreader)(void*);
201     int (*clientradput)(struct server *, unsigned char *);
202     void (*addclient)(struct client *);
203     void (*addserverextra)(struct clsrvconf *);
204     void (*initextra)();
205 };
206
207 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
208
209 #define ATTRTYPE(x) ((x)[0])
210 #define ATTRLEN(x) ((x)[1])
211 #define ATTRVAL(x) ((x) + 2)
212 #define ATTRVALLEN(x) ((x)[1] - 2)
213
214 #define SOCKADDR_SIZE(addr) ((addr).ss_family == AF_INET ? \
215                             sizeof(struct sockaddr_in) : \
216                             sizeof(struct sockaddr_in6))
217
218 struct addrinfo *getsrcprotores(uint8_t type);
219 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
220 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
221 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
222 struct client *addclient(struct clsrvconf *conf);
223 void removeclient(struct client *client);
224 void removeclientrqs(struct client *client);
225 struct queue *newqueue();
226 void removequeue(struct queue *q);
227 void freebios(struct queue *q);
228 int radsrv(struct request *rq);
229 X509 *verifytlscert(SSL *ssl);
230 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
231 int replyh(struct server *server, unsigned char *buf);
232 int connecttcp(struct addrinfo *addrinfo, struct addrinfo *src);
233 int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only);