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 /* 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 *logdestination;
36     char *ttlattr;
37     uint32_t ttlattrtype[2];
38     uint8_t addttl;
39     uint8_t loglevel;
40     uint8_t loopprevention;
41 };
42
43 struct commonprotoopts {
44     char **listenargs;
45     char *sourcearg;
46 };
47
48 struct request {
49     struct timeval created;
50     uint32_t refcount;
51     uint8_t *buf, *replybuf;
52     struct radmsg *msg;
53     struct client *from;
54     struct server *to;
55     char *origusername;
56     uint8_t rqid;
57     uint8_t rqauth[16];
58     uint8_t newid;
59     int udpsock; /* only for UDP */
60     uint16_t udpport; /* only for UDP */
61 };
62
63 /* requests that our client will send */
64 struct rqout {
65     pthread_mutex_t *lock;
66     struct request *rq;
67     uint8_t tries;
68     struct timeval expiry;
69 };
70
71 struct queue {
72     struct list *entries;
73     pthread_mutex_t mutex;
74     pthread_cond_t cond;
75 };
76
77 struct clsrvconf {
78     char *name;
79     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
80     const struct protodefs *pdef;
81     char *host;
82     char *port;
83     char *secret;
84     char *tls;
85     char *matchcertattr;
86     regex_t *certcnregex;
87     regex_t *certuriregex;
88     char *confrewritein;
89     char *confrewriteout;
90     char *confrewriteusername;
91     struct modattr *rewriteusername;
92     char *dynamiclookupcommand;
93     uint8_t statusserver;
94     uint8_t retryinterval;
95     uint8_t retrycount;
96     uint8_t dupinterval;
97     uint8_t certnamecheck;
98     uint8_t addttl;
99     struct rewrite *rewritein;
100     struct rewrite *rewriteout;
101     struct addrinfo *addrinfo;
102     uint8_t prefixlen;
103     pthread_mutex_t *lock; /* only used for updating clients so far */
104     struct tls *tlsconf;
105     struct list *clients;
106     struct server *servers;
107 };
108
109 struct client {
110     struct clsrvconf *conf;
111     int sock;
112     SSL *ssl;
113     struct request *rqs[MAX_REQUESTS];
114     struct queue *replyq;
115     struct queue *rbios; /* for dtls */
116     struct sockaddr *addr;
117     time_t expiry; /* for udp */
118 };
119
120 struct server {
121     struct clsrvconf *conf;
122     int sock;
123     SSL *ssl;
124     pthread_mutex_t lock;
125     pthread_t clientth;
126     uint8_t clientrdgone;
127     struct timeval lastconnecttry;
128     struct timeval lastreply;
129     uint8_t connectionok;
130     uint8_t lostrqs;
131     char *dynamiclookuparg;
132     int nextid;
133     struct timeval lastrcv;
134     struct rqout *requests;
135     uint8_t newrq;
136     pthread_mutex_t newrq_mutex;
137     pthread_cond_t newrq_cond;
138     struct queue *rbios; /* for dtls */
139 };
140
141 struct realm {
142     char *name;
143     char *message;
144     uint8_t accresp;
145     regex_t regex;
146     uint32_t refcount;
147     pthread_mutex_t mutex;
148     struct realm *parent;
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     char **policyoids;
163     uint32_t cacheexpiry;
164     uint32_t tlsexpiry;
165     uint32_t dtlsexpiry;
166     X509_VERIFY_PARAM *vpm;
167     SSL_CTX *tlsctx;
168     SSL_CTX *dtlsctx;
169 };
170
171 struct modattr {
172     uint8_t t;
173     char *replacement;
174     regex_t *regex;
175 };
176
177 struct rewrite {
178     uint8_t *removeattrs;
179     uint32_t *removevendorattrs;
180     struct list *addattrs;
181     struct list *modattrs;
182 };
183
184 struct protodefs {
185     char *name;
186     char *secretdefault;
187     int socktype;
188     char *portdefault;
189     uint8_t retrycountdefault;
190     uint8_t retrycountmax;
191     uint8_t retryintervaldefault;
192     uint8_t retryintervalmax;
193     uint8_t duplicateintervaldefault;
194     void (*setprotoopts)(struct commonprotoopts *);
195     char **(*getlistenerargs)();
196     void *(*listener)(void*);
197     int (*connecter)(struct server *, struct timeval *, int, char *);
198     void *(*clientconnreader)(void*);
199     int (*clientradput)(struct server *, unsigned char *);
200     void (*addclient)(struct client *);
201     void (*addserverextra)(struct clsrvconf *);
202     void (*setsrcres)();
203     void (*initextra)();
204 };
205
206 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
207
208 #define ATTRTYPE(x) ((x)[0])
209 #define ATTRLEN(x) ((x)[1])
210 #define ATTRVAL(x) ((x) + 2)
211 #define ATTRVALLEN(x) ((x)[1] - 2)
212
213 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
214 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
215 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
216 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
217 void removelockedclient(struct client *client);
218 void removeclient(struct client *client);
219 struct queue *newqueue();
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 SSL_CTX *tlsgetctx(uint8_t type, struct tls *t);
228 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);