lots of changes to radsrv/reply and use of new radmsg stuff
[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 #define DEBUG_LEVEL 3
10
11 #define CONFIG_MAIN "/etc/radsecproxy.conf"
12
13 /* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */
14 #define MAX_REQUESTS 256
15 #define REQUEST_RETRY_INTERVAL 5
16 #define REQUEST_RETRY_COUNT 2
17 #define MAX_CERT_DEPTH 5
18 #define STATUS_SERVER_PERIOD 25
19 #define IDLE_TIMEOUT 300
20
21 #define RAD_UDP 0
22 #define RAD_TLS 1
23 #define RAD_TCP 2
24 #define RAD_DTLS 3
25
26 struct options {
27     char **listenudp;
28     char **listentcp;
29     char **listentls;
30     char **listendtls;
31     char **listenaccudp;
32     char *sourceudp;
33     char *sourcetcp;
34     char *sourcetls;
35     char *sourcedtls;
36     char *logdestination;
37     uint8_t loglevel;
38     uint8_t loopprevention;
39 };
40
41 /* requests that our client will send */
42 struct request {
43     unsigned char *buf;
44     uint8_t tries;
45     uint8_t received;
46     struct timeval expiry;
47     struct client *from;
48     char *origusername;
49     uint8_t origid; /* used by servwr */
50     char origauth[16]; /* used by servwr */
51     struct sockaddr_storage fromsa; /* used by udpservwr */
52     int fromudpsock; /* used by udpservwr */
53 };
54
55 /* replies that a server will send */
56 struct reply {
57     unsigned char *buf;
58     struct sockaddr_storage tosa; /* used by udpservwr */
59     int toudpsock; /* used by udpservwr */
60 };
61
62 struct queue {
63     struct list *entries;
64     pthread_mutex_t mutex;
65     pthread_cond_t cond;
66 };
67
68 struct clsrvconf {
69     char *name;
70     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
71     const struct protodefs *pdef;
72     char *host;
73     char *port;
74     char *secret;
75     char *tls;
76     char *matchcertattr;
77     regex_t *certcnregex;
78     regex_t *certuriregex;
79     char *confrewritein;
80     char *confrewriteout;
81     char *confrewriteusername;
82     struct modattr *rewriteusername;
83     char *dynamiclookupcommand;
84     uint8_t statusserver;
85     uint8_t retryinterval;
86     uint8_t retrycount;
87     uint8_t certnamecheck;
88     SSL_CTX *ssl_ctx;
89     struct rewrite *rewritein;
90     struct rewrite *rewriteout;
91     struct addrinfo *addrinfo;
92     uint8_t prefixlen;
93     struct list *clients;
94     struct server *servers;
95 };
96
97 struct client {
98     struct clsrvconf *conf;
99     int sock; /* for tcp/dtls */
100     SSL *ssl;
101     struct queue *replyq;
102     struct queue *rbios; /* for dtls */
103     struct sockaddr_storage addr; /* for dtls */
104 };
105
106 struct server {
107     struct clsrvconf *conf;
108     int sock;
109     SSL *ssl;
110     pthread_mutex_t lock;
111     pthread_t clientth;
112     uint8_t clientrdgone;
113     struct timeval lastconnecttry;
114     struct timeval lastreply;
115     uint8_t connectionok;
116     uint8_t lostrqs;
117     char *dynamiclookuparg;
118     int nextid;
119     struct timeval lastrcv;
120     struct request *requests;
121     uint8_t newrq;
122     pthread_mutex_t newrq_mutex;
123     pthread_cond_t newrq_cond;
124     struct queue *rbios; /* for dtls */
125 };
126
127 struct realm {
128     char *name;
129     char *message;
130     uint8_t accresp;
131     regex_t regex;
132     pthread_mutex_t subrealms_mutex;
133     struct list *subrealms;
134     struct list *srvconfs;
135     struct list *accsrvconfs;
136 };
137
138 struct tls {
139     char *name;
140     char *cacertfile;
141     char *cacertpath;
142     char *certfile;
143     char *certkeyfile;
144     char *certkeypwd;
145     uint8_t crlcheck;
146     SSL_CTX *tlsctx;
147     SSL_CTX *dtlsctx;
148 };
149
150 struct modattr {
151     uint8_t t;
152     char *replacement;
153     regex_t *regex;
154 };
155
156 struct rewrite {
157     uint8_t *removeattrs;
158     uint32_t *removevendorattrs;
159     struct list *addattrs;
160     struct list *modattrs;
161 };
162
163 struct protodefs {
164     char *name;
165     char *secretdefault;
166     uint8_t socktype;
167     char *portdefault;
168     uint8_t retrycountdefault;
169     uint8_t retrycountmax;
170     uint8_t retryintervaldefault;
171     uint8_t retryintervalmax;
172     void *(*listener)(void*);
173     char **srcaddrport;
174     int (*connecter)(struct server *, struct timeval *, int, char *);
175     void *(*clientconnreader)(void*);
176     int (*clientradput)(struct server *, unsigned char *);
177     void (*addclient)(struct client *);
178     void (*addserverextra)(struct clsrvconf *);
179     void (*initextra)();
180 };
181
182 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
183
184 #define ATTRTYPE(x) ((x)[0])
185 #define ATTRLEN(x) ((x)[1])
186 #define ATTRVAL(x) ((x) + 2)
187 #define ATTRVALLEN(x) ((x)[1] - 2)
188
189 #define SOCKADDR_SIZE(addr) ((addr).ss_family == AF_INET ? \
190                             sizeof(struct sockaddr_in) : \
191                             sizeof(struct sockaddr_in6))
192
193 struct addrinfo *getsrcprotores(uint8_t type);
194 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
195 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
196 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
197 struct client *addclient(struct clsrvconf *conf);
198 void removeclient(struct client *client);
199 void removeclientrqs(struct client *client);
200 struct queue *newqueue();
201 void removequeue(struct queue *q);
202 void freebios(struct queue *q);
203 int radsrv(struct request *rq);
204 X509 *verifytlscert(SSL *ssl);
205 int verifyconfcert(X509 *cert, struct clsrvconf *conf);
206 int replyh(struct server *server, unsigned char *buf);
207 int connecttcp(struct addrinfo *addrinfo, struct addrinfo *src);
208 int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only);