cleaning up code
[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 #define RAD_PROTOCOUNT 4
30
31 struct options {
32     char **listenargs[RAD_PROTOCOUNT];
33     char *sourceudp;
34     char *sourcetcp;
35     char *sourcetls;
36     char *sourcedtls;
37     char *logdestination;
38     uint8_t loglevel;
39     uint8_t loopprevention;
40 };
41
42 struct request {
43     struct timeval created;
44     uint32_t refcount;
45     uint8_t *buf, *replybuf;
46     struct radmsg *msg;
47     struct client *from;
48     struct server *to;
49     char *origusername;
50     uint8_t rqid;
51     uint8_t rqauth[16];
52     uint8_t newid;
53     int udpsock; /* only for UDP */
54     uint16_t udpport; /* only for UDP */
55 };
56
57 /* requests that our client will send */
58 struct rqout {
59     pthread_mutex_t *lock;
60     struct request *rq;
61     uint8_t tries;
62     struct timeval expiry;
63 };
64
65 struct queue {
66     struct list *entries;
67     pthread_mutex_t mutex;
68     pthread_cond_t cond;
69 };
70
71 struct clsrvconf {
72     char *name;
73     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
74     const struct protodefs *pdef;
75     char *host;
76     char *port;
77     char *secret;
78     char *tls;
79     char *matchcertattr;
80     regex_t *certcnregex;
81     regex_t *certuriregex;
82     char *confrewritein;
83     char *confrewriteout;
84     char *confrewriteusername;
85     struct modattr *rewriteusername;
86     char *dynamiclookupcommand;
87     uint8_t statusserver;
88     uint8_t retryinterval;
89     uint8_t retrycount;
90     uint8_t dupinterval;
91     uint8_t certnamecheck;
92     struct rewrite *rewritein;
93     struct rewrite *rewriteout;
94     struct addrinfo *addrinfo;
95     uint8_t prefixlen;
96     pthread_mutex_t *lock; /* only used for updating clients so far */
97     struct tls *tlsconf;
98     struct list *clients;
99     struct server *servers;
100 };
101
102 struct client {
103     struct clsrvconf *conf;
104     int sock;
105     SSL *ssl;
106     struct request *rqs[MAX_REQUESTS];
107     struct queue *replyq;
108     struct queue *rbios; /* for dtls */
109     struct sockaddr *addr;
110     time_t expiry; /* for udp */
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     uint32_t refcount;
140     pthread_mutex_t mutex;
141     struct realm *parent;
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     char **policyoids;
156     uint32_t cacheexpiry;
157     uint32_t tlsexpiry;
158     uint32_t dtlsexpiry;
159     X509_VERIFY_PARAM *vpm;
160     SSL_CTX *tlsctx;
161     SSL_CTX *dtlsctx;
162 };
163
164 struct modattr {
165     uint8_t t;
166     char *replacement;
167     regex_t *regex;
168 };
169
170 struct rewrite {
171     uint8_t *removeattrs;
172     uint32_t *removevendorattrs;
173     struct list *addattrs;
174     struct list *modattrs;
175 };
176
177 struct protodefs {
178     char *name;
179     char *secretdefault;
180     uint8_t socktype;
181     char *portdefault;
182     uint8_t retrycountdefault;
183     uint8_t retrycountmax;
184     uint8_t retryintervaldefault;
185     uint8_t retryintervalmax;
186     uint8_t duplicateintervaldefault;
187     void *(*listener)(void*);
188     char **srcaddrport;
189     int (*connecter)(struct server *, struct timeval *, int, char *);
190     void *(*clientconnreader)(void*);
191     int (*clientradput)(struct server *, unsigned char *);
192     void (*addclient)(struct client *);
193     void (*addserverextra)(struct clsrvconf *);
194     uint8_t freesrcprotores;
195     void (*initextra)();
196 };
197
198 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
199
200 #define ATTRTYPE(x) ((x)[0])
201 #define ATTRLEN(x) ((x)[1])
202 #define ATTRVAL(x) ((x) + 2)
203 #define ATTRVALLEN(x) ((x)[1] - 2)
204
205 struct addrinfo *getsrcprotores(uint8_t type);
206 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
207 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
208 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
209 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
210 void removelockedclient(struct client *client);
211 void removeclient(struct client *client);
212 struct queue *newqueue();
213 void freebios(struct queue *q);
214 struct request *newrequest();
215 void freerq(struct request *rq);
216 int radsrv(struct request *rq);
217 X509 *verifytlscert(SSL *ssl);
218 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
219 void replyh(struct server *server, unsigned char *buf);
220 SSL_CTX *tlsgetctx(uint8_t type, struct tls *t);