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