added initial ttl support, need testing
[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 /* 27262 is vendor DANTE Ltd. */
26 #define DEFAULT_TTL_ATTR "27262:1"
27
28 #define RAD_UDP 0
29 #define RAD_TLS 1
30 #define RAD_TCP 2
31 #define RAD_DTLS 3
32 #define RAD_PROTOCOUNT 4
33
34 struct options {
35     char **listenargs[RAD_PROTOCOUNT];
36     char *sourcearg[RAD_PROTOCOUNT];
37     char *logdestination;
38     char *ttlattr;
39     uint32_t ttlattrtype[2];
40     uint8_t addttl;
41     uint8_t loglevel;
42     uint8_t loopprevention;
43 };
44
45 struct request {
46     struct timeval created;
47     uint32_t refcount;
48     uint8_t *buf, *replybuf;
49     struct radmsg *msg;
50     struct client *from;
51     struct server *to;
52     char *origusername;
53     uint8_t rqid;
54     uint8_t rqauth[16];
55     uint8_t newid;
56     int udpsock; /* only for UDP */
57     uint16_t udpport; /* only for UDP */
58 };
59
60 /* requests that our client will send */
61 struct rqout {
62     pthread_mutex_t *lock;
63     struct request *rq;
64     uint8_t tries;
65     struct timeval expiry;
66 };
67
68 struct queue {
69     struct list *entries;
70     pthread_mutex_t mutex;
71     pthread_cond_t cond;
72 };
73
74 struct clsrvconf {
75     char *name;
76     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
77     const struct protodefs *pdef;
78     char *host;
79     char *port;
80     char *secret;
81     char *tls;
82     char *matchcertattr;
83     regex_t *certcnregex;
84     regex_t *certuriregex;
85     char *confrewritein;
86     char *confrewriteout;
87     char *confrewriteusername;
88     struct modattr *rewriteusername;
89     char *dynamiclookupcommand;
90     uint8_t statusserver;
91     uint8_t retryinterval;
92     uint8_t retrycount;
93     uint8_t dupinterval;
94     uint8_t certnamecheck;
95     struct rewrite *rewritein;
96     struct rewrite *rewriteout;
97     struct addrinfo *addrinfo;
98     uint8_t prefixlen;
99     pthread_mutex_t *lock; /* only used for updating clients so far */
100     struct tls *tlsconf;
101     struct list *clients;
102     struct server *servers;
103 };
104
105 struct client {
106     struct clsrvconf *conf;
107     int sock;
108     SSL *ssl;
109     struct request *rqs[MAX_REQUESTS];
110     struct queue *replyq;
111     struct queue *rbios; /* for dtls */
112     struct sockaddr *addr;
113     time_t expiry; /* for udp */
114 };
115
116 struct server {
117     struct clsrvconf *conf;
118     int sock;
119     SSL *ssl;
120     pthread_mutex_t lock;
121     pthread_t clientth;
122     uint8_t clientrdgone;
123     struct timeval lastconnecttry;
124     struct timeval lastreply;
125     uint8_t connectionok;
126     uint8_t lostrqs;
127     char *dynamiclookuparg;
128     int nextid;
129     struct timeval lastrcv;
130     struct rqout *requests;
131     uint8_t newrq;
132     pthread_mutex_t newrq_mutex;
133     pthread_cond_t newrq_cond;
134     struct queue *rbios; /* for dtls */
135 };
136
137 struct realm {
138     char *name;
139     char *message;
140     uint8_t accresp;
141     regex_t regex;
142     uint32_t refcount;
143     pthread_mutex_t mutex;
144     struct realm *parent;
145     struct list *subrealms;
146     struct list *srvconfs;
147     struct list *accsrvconfs;
148 };
149
150 struct tls {
151     char *name;
152     char *cacertfile;
153     char *cacertpath;
154     char *certfile;
155     char *certkeyfile;
156     char *certkeypwd;
157     uint8_t crlcheck;
158     char **policyoids;
159     uint32_t cacheexpiry;
160     uint32_t tlsexpiry;
161     uint32_t dtlsexpiry;
162     X509_VERIFY_PARAM *vpm;
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     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     uint8_t freesrcprotores;
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 struct addrinfo *getsrcprotores(uint8_t type);
208 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
209 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
210 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
211 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
212 void removelockedclient(struct client *client);
213 void removeclient(struct client *client);
214 struct queue *newqueue();
215 void freebios(struct queue *q);
216 struct request *newrequest();
217 void freerq(struct request *rq);
218 int radsrv(struct request *rq);
219 X509 *verifytlscert(SSL *ssl);
220 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
221 void replyh(struct server *server, unsigned char *buf);
222 SSL_CTX *tlsgetctx(uint8_t type, struct tls *t);