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