some code improvemetns, more efficiently removing outstanding requests when removing...
[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, *replybuf;
49     struct radmsg *msg;
50     struct client *from;
51     struct server *to;
52     char *origusername;
53     uint8_t rqid;
54     uint8_t rqauth[16];
55     uint8_t newid;
56     int udpsock; /* only for UDP */
57     uint16_t udpport; /* only for UDP */
58 };
59
60 /* requests that our client will send */
61 struct rqout {
62     pthread_mutex_t *lock;
63     struct request *rq;
64     uint8_t tries;
65     struct timeval expiry;
66 };
67
68 struct queue {
69     struct list *entries;
70     pthread_mutex_t mutex;
71     pthread_cond_t cond;
72 };
73
74 struct clsrvconf {
75     char *name;
76     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
77     const struct protodefs *pdef;
78     char *host;
79     char *port;
80     char *secret;
81     char *tls;
82     char *matchcertattr;
83     regex_t *certcnregex;
84     regex_t *certuriregex;
85     char *confrewritein;
86     char *confrewriteout;
87     char *confrewriteusername;
88     struct modattr *rewriteusername;
89     char *dynamiclookupcommand;
90     uint8_t statusserver;
91     uint8_t retryinterval;
92     uint8_t retrycount;
93     uint8_t dupinterval;
94     uint8_t certnamecheck;
95     struct rewrite *rewritein;
96     struct rewrite *rewriteout;
97     struct addrinfo *addrinfo;
98     uint8_t prefixlen;
99     pthread_mutex_t *lock; /* only used for updating clients so far */
100     struct tls *tlsconf;
101     struct list *clients;
102     struct server *servers;
103 };
104
105 struct client {
106     struct clsrvconf *conf;
107     int sock;
108     SSL *ssl;
109     struct request *rqs[MAX_REQUESTS];
110     struct queue *replyq;
111     struct queue *rbios; /* for dtls */
112     struct sockaddr *addr;
113 };
114
115 struct server {
116     struct clsrvconf *conf;
117     int sock;
118     SSL *ssl;
119     pthread_mutex_t lock;
120     pthread_t clientth;
121     uint8_t clientrdgone;
122     struct timeval lastconnecttry;
123     struct timeval lastreply;
124     uint8_t connectionok;
125     uint8_t lostrqs;
126     char *dynamiclookuparg;
127     int nextid;
128     struct timeval lastrcv;
129     struct rqout *requests;
130     uint8_t newrq;
131     pthread_mutex_t newrq_mutex;
132     pthread_cond_t newrq_cond;
133     struct queue *rbios; /* for dtls */
134 };
135
136 struct realm {
137     char *name;
138     char *message;
139     uint8_t accresp;
140     regex_t regex;
141     pthread_mutex_t subrealms_mutex;
142     struct list *subrealms;
143     struct list *srvconfs;
144     struct list *accsrvconfs;
145 };
146
147 struct tls {
148     char *name;
149     char *cacertfile;
150     char *cacertpath;
151     char *certfile;
152     char *certkeyfile;
153     char *certkeypwd;
154     uint8_t crlcheck;
155     uint32_t cacheexpiry;
156     uint32_t tlsexpiry;
157     uint32_t dtlsexpiry;
158     SSL_CTX *tlsctx;
159     SSL_CTX *dtlsctx;
160 };
161
162 struct modattr {
163     uint8_t t;
164     char *replacement;
165     regex_t *regex;
166 };
167
168 struct rewrite {
169     uint8_t *removeattrs;
170     uint32_t *removevendorattrs;
171     struct list *addattrs;
172     struct list *modattrs;
173 };
174
175 struct protodefs {
176     char *name;
177     char *secretdefault;
178     uint8_t socktype;
179     char *portdefault;
180     uint8_t retrycountdefault;
181     uint8_t retrycountmax;
182     uint8_t retryintervaldefault;
183     uint8_t retryintervalmax;
184     uint8_t duplicateintervaldefault;
185     void *(*listener)(void*);
186     char **srcaddrport;
187     int (*connecter)(struct server *, struct timeval *, int, char *);
188     void *(*clientconnreader)(void*);
189     int (*clientradput)(struct server *, unsigned char *);
190     void (*addclient)(struct client *);
191     void (*addserverextra)(struct clsrvconf *);
192     void (*initextra)();
193 };
194
195 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
196
197 #define ATTRTYPE(x) ((x)[0])
198 #define ATTRLEN(x) ((x)[1])
199 #define ATTRVAL(x) ((x) + 2)
200 #define ATTRVALLEN(x) ((x)[1] - 2)
201
202 struct addrinfo *getsrcprotores(uint8_t type);
203 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
204 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
205 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
206 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
207 void removeclient(struct client *client);
208 struct queue *newqueue();
209 void freebios(struct queue *q);
210 struct request *newrequest();
211 void freerq(struct request *rq);
212 int radsrv(struct request *rq);
213 X509 *verifytlscert(SSL *ssl);
214 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
215 void replyh(struct server *server, unsigned char *buf);
216 SSL_CTX *tlsgetctx(uint8_t type, struct tls *t);