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