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