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