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