better logging, fixed bug with crash when removing client
[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     char origauth[16];
53     uint8_t origid;
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     SSL_CTX *ssl_ctx;
94     struct rewrite *rewritein;
95     struct rewrite *rewriteout;
96     struct addrinfo *addrinfo;
97     uint8_t prefixlen;
98     pthread_mutex_t *lock; /* only used for updating clients so far */
99     struct list *clients;
100     struct server *servers;
101 };
102
103 struct client {
104     struct clsrvconf *conf;
105     int sock; /* for tcp/dtls */
106     SSL *ssl;
107     pthread_mutex_t lock; /* used for updating rqs */
108     struct request *rqs[MAX_REQUESTS];
109     struct queue *replyq;
110     struct queue *rbios; /* for dtls */
111     struct sockaddr *addr;
112 };
113
114 struct server {
115     struct clsrvconf *conf;
116     int sock;
117     SSL *ssl;
118     pthread_mutex_t lock;
119     pthread_t clientth;
120     uint8_t clientrdgone;
121     struct timeval lastconnecttry;
122     struct timeval lastreply;
123     uint8_t connectionok;
124     uint8_t lostrqs;
125     char *dynamiclookuparg;
126     int nextid;
127     struct timeval lastrcv;
128     struct rqout *requests;
129     uint8_t newrq;
130     pthread_mutex_t newrq_mutex;
131     pthread_cond_t newrq_cond;
132     struct queue *rbios; /* for dtls */
133 };
134
135 struct realm {
136     char *name;
137     char *message;
138     uint8_t accresp;
139     regex_t regex;
140     pthread_mutex_t subrealms_mutex;
141     struct list *subrealms;
142     struct list *srvconfs;
143     struct list *accsrvconfs;
144 };
145
146 struct tls {
147     char *name;
148     char *cacertfile;
149     char *cacertpath;
150     char *certfile;
151     char *certkeyfile;
152     char *certkeypwd;
153     uint8_t crlcheck;
154     SSL_CTX *tlsctx;
155     SSL_CTX *dtlsctx;
156 };
157
158 struct modattr {
159     uint8_t t;
160     char *replacement;
161     regex_t *regex;
162 };
163
164 struct rewrite {
165     uint8_t *removeattrs;
166     uint32_t *removevendorattrs;
167     struct list *addattrs;
168     struct list *modattrs;
169 };
170
171 struct protodefs {
172     char *name;
173     char *secretdefault;
174     uint8_t socktype;
175     char *portdefault;
176     uint8_t retrycountdefault;
177     uint8_t retrycountmax;
178     uint8_t retryintervaldefault;
179     uint8_t retryintervalmax;
180     uint8_t duplicateintervaldefault;
181     void *(*listener)(void*);
182     char **srcaddrport;
183     int (*connecter)(struct server *, struct timeval *, int, char *);
184     void *(*clientconnreader)(void*);
185     int (*clientradput)(struct server *, unsigned char *);
186     void (*addclient)(struct client *);
187     void (*addserverextra)(struct clsrvconf *);
188     void (*initextra)();
189 };
190
191 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
192
193 #define ATTRTYPE(x) ((x)[0])
194 #define ATTRLEN(x) ((x)[1])
195 #define ATTRVAL(x) ((x) + 2)
196 #define ATTRVALLEN(x) ((x)[1] - 2)
197
198 struct addrinfo *getsrcprotores(uint8_t type);
199 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
200 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
201 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
202 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
203 void removeclient(struct client *client);
204 void removeclientrqs(struct client *client);
205 struct queue *newqueue();
206 void removequeue(struct queue *q);
207 void freebios(struct queue *q);
208 struct request *newrequest();
209 void freerq(struct request *rq);
210 int radsrv(struct request *rq);
211 X509 *verifytlscert(SSL *ssl);
212 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
213 void replyh(struct server *server, unsigned char *buf);
214 int connecttcp(struct addrinfo *addrinfo, struct addrinfo *src);
215 int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only);