radsecproxy.h 5.39 KB
Newer Older
venaas's avatar
 
venaas committed
1
/*
2
 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
venaas's avatar
 
venaas committed
3
4
5
6
7
8
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

venaas's avatar
venaas committed
9
10
11
#include "tlv11.h"
#include "radmsg.h"

venaas's avatar
venaas committed
12
#define DEBUG_LEVEL 3
venaas's avatar
venaas committed
13

14
#define CONFIG_MAIN "/etc/radsecproxy.conf"
venaas's avatar
venaas committed
15

venaas's avatar
venaas committed
16
/* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */
venaas's avatar
 
venaas committed
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
venaas's avatar
 
venaas committed
24

venaas's avatar
venaas committed
25
26
#define RAD_UDP 0
#define RAD_TLS 1
venaas's avatar
venaas committed
27
#define RAD_TCP 2
venaas's avatar
venaas committed
28
#define RAD_DTLS 3
venaas's avatar
venaas committed
29

30
struct options {
31
32
    char **listenudp;
    char **listentcp;
venaas's avatar
venaas committed
33
    char **listentls;
34
    char **listendtls;
35
    char **listenaccudp;
36
37
    char *sourceudp;
    char *sourcetcp;
venaas's avatar
venaas committed
38
    char *sourcetls;
39
    char *sourcedtls;
40
41
    char *logdestination;
    uint8_t loglevel;
venaas's avatar
venaas committed
42
    uint8_t loopprevention;
43
};
venaas's avatar
venaas committed
44

venaas's avatar
 
venaas committed
45
struct request {
46
    struct timeval created;
47
    uint32_t refcount;
venaas's avatar
venaas committed
48
    uint8_t *buf, *replybuf;
venaas's avatar
venaas committed
49
    struct radmsg *msg;
50
    struct client *from;
51
    struct server *to;
venaas's avatar
venaas committed
52
    char *origusername;
53
54
    uint8_t rqid;
    uint8_t rqauth[16];
55
    uint8_t newid;
venaas's avatar
venaas committed
56
57
    int udpsock; /* only for UDP */
    uint16_t udpport; /* only for UDP */
58
59
60
61
};

/* requests that our client will send */
struct rqout {
venaas's avatar
venaas committed
62
    pthread_mutex_t *lock;
63
    struct request *rq;
venaas's avatar
 
venaas committed
64
    uint8_t tries;
65
    struct timeval expiry;
venaas's avatar
 
venaas committed
66
67
};

68
69
struct queue {
    struct list *entries;
70
71
    pthread_mutex_t mutex;
    pthread_cond_t cond;
venaas's avatar
 
venaas committed
72
73
};

venaas's avatar
venaas committed
74
struct clsrvconf {
75
    char *name;
venaas's avatar
venaas committed
76
77
    uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
    const struct protodefs *pdef;
venaas's avatar
 
venaas committed
78
79
    char *host;
    char *port;
80
    char *secret;
81
82
    char *tls;
    char *matchcertattr;
venaas's avatar
venaas committed
83
    regex_t *certcnregex;
84
    regex_t *certuriregex;
venaas's avatar
venaas committed
85
86
    char *confrewritein;
    char *confrewriteout;
87
88
    char *confrewriteusername;
    struct modattr *rewriteusername;
venaas's avatar
venaas committed
89
    char *dynamiclookupcommand;
venaas's avatar
venaas committed
90
    uint8_t statusserver;
91
    uint8_t retryinterval;
92
    uint8_t retrycount;
93
    uint8_t dupinterval;
94
    uint8_t certnamecheck;
venaas's avatar
venaas committed
95
96
    struct rewrite *rewritein;
    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;
venaas's avatar
venaas committed
102
    struct server *servers;
venaas's avatar
venaas committed
103
104
105
};

struct client {
venaas's avatar
venaas committed
106
    struct clsrvconf *conf;
107
    int sock;
venaas's avatar
venaas committed
108
    SSL *ssl;
109
    struct request *rqs[MAX_REQUESTS];
110
111
    struct queue *replyq;
    struct queue *rbios; /* for dtls */
112
    struct sockaddr *addr;
venaas's avatar
venaas committed
113
    time_t expiry; /* for udp */
114
115
116
};

struct server {
venaas's avatar
venaas committed
117
    struct clsrvconf *conf;
118
    int sock;
venaas's avatar
venaas committed
119
    SSL *ssl;
venaas's avatar
 
venaas committed
120
121
    pthread_mutex_t lock;
    pthread_t clientth;
122
    uint8_t clientrdgone;
venaas's avatar
venaas committed
123
    struct timeval lastconnecttry;
124
    struct timeval lastreply;
venaas's avatar
venaas committed
125
    uint8_t connectionok;
126
    uint8_t lostrqs;
127
    char *dynamiclookuparg;
venaas's avatar
venaas committed
128
    int nextid;
129
    struct timeval lastrcv;
130
    struct rqout *requests;
venaas's avatar
 
venaas committed
131
132
133
    uint8_t newrq;
    pthread_mutex_t newrq_mutex;
    pthread_cond_t newrq_cond;
134
    struct queue *rbios; /* for dtls */
venaas's avatar
 
venaas committed
135
136
};

137
138
struct realm {
    char *name;
venaas's avatar
venaas committed
139
    char *message;
140
    uint8_t accresp;
venaas's avatar
venaas committed
141
    regex_t regex;
142
143
144
    uint32_t refcount;
    pthread_mutex_t mutex;
    struct realm *parent;
venaas's avatar
venaas committed
145
    struct list *subrealms;
146
    struct list *srvconfs;
147
    struct list *accsrvconfs;
148
149
};

venaas's avatar
venaas committed
150
151
struct tls {
    char *name;
venaas's avatar
venaas committed
152
153
154
155
156
157
    char *cacertfile;
    char *cacertpath;
    char *certfile;
    char *certkeyfile;
    char *certkeypwd;
    uint8_t crlcheck;
158
159
160
    uint32_t cacheexpiry;
    uint32_t tlsexpiry;
    uint32_t dtlsexpiry;
venaas's avatar
venaas committed
161
162
    SSL_CTX *tlsctx;
    SSL_CTX *dtlsctx;
venaas's avatar
venaas committed
163
164
};

165
166
167
168
169
170
struct modattr {
    uint8_t t;
    char *replacement;
    regex_t *regex;
};

171
172
struct rewrite {
    uint8_t *removeattrs;
venaas's avatar
venaas committed
173
    uint32_t *removevendorattrs;
venaas's avatar
venaas committed
174
    struct list *addattrs;
175
    struct list *modattrs;
venaas's avatar
venaas committed
176
177
};

178
struct protodefs {
venaas's avatar
venaas committed
179
180
    char *name;
    char *secretdefault;
venaas's avatar
venaas committed
181
182
    uint8_t socktype;
    char *portdefault;
183
184
185
186
    uint8_t retrycountdefault;
    uint8_t retrycountmax;
    uint8_t retryintervaldefault;
    uint8_t retryintervalmax;
187
    uint8_t duplicateintervaldefault;
188
    void *(*listener)(void*);
venaas's avatar
venaas committed
189
190
    char **srcaddrport;
    int (*connecter)(struct server *, struct timeval *, int, char *);
venaas's avatar
venaas committed
191
    void *(*clientconnreader)(void*);
venaas's avatar
venaas committed
192
    int (*clientradput)(struct server *, unsigned char *);
venaas's avatar
venaas committed
193
194
195
    void (*addclient)(struct client *);
    void (*addserverextra)(struct clsrvconf *);
    void (*initextra)();
196
197
};

venaas's avatar
venaas committed
198
199
#define RADLEN(x) ntohs(((uint16_t *)(x))[1])

venaas's avatar
venaas committed
200
201
202
203
204
#define ATTRTYPE(x) ((x)[0])
#define ATTRLEN(x) ((x)[1])
#define ATTRVAL(x) ((x) + 2)
#define ATTRVALLEN(x) ((x)[1] - 2)

venaas's avatar
venaas committed
205
struct addrinfo *getsrcprotores(uint8_t type);
venaas's avatar
venaas committed
206
207
struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
venaas's avatar
venaas committed
208
struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
209
struct client *addclient(struct clsrvconf *conf, uint8_t lock);
venaas's avatar
venaas committed
210
void removelockedclient(struct client *client);
venaas's avatar
venaas committed
211
void removeclient(struct client *client);
venaas's avatar
venaas committed
212
struct queue *newqueue();
213
void freebios(struct queue *q);
214
215
struct request *newrequest();
void freerq(struct request *rq);
venaas's avatar
venaas committed
216
217
218
int radsrv(struct request *rq);
X509 *verifytlscert(SSL *ssl);
int verifyconfcert(X509 *cert, struct clsrvconf *conf);
venaas's avatar
venaas committed
219
void replyh(struct server *server, unsigned char *buf);
220
SSL_CTX *tlsgetctx(uint8_t type, struct tls *t);