radsecproxy-1.6.5.
[libradsec.git] / radsecproxy.h
1 /*
2  * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
3  * Copyright (C) 2010,2011,2012 NORDUnet A/S
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  */
9
10 #include <sys/time.h>
11 #include <stdint.h>
12 #include <pthread.h>
13 #include <regex.h>
14 #include "list.h"
15 #include "tlv11.h"
16 #include "radmsg.h"
17 #include "gconfig.h"
18
19 #define DEBUG_LEVEL 2
20
21 #define CONFIG_MAIN SYSCONFDIR"/radsecproxy.conf"
22
23 /* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */
24 #define MAX_REQUESTS 256
25 #define REQUEST_RETRY_INTERVAL 5
26 #define REQUEST_RETRY_COUNT 2
27 #define DUPLICATE_INTERVAL REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT
28 #define MAX_CERT_DEPTH 5
29 #define STATUS_SERVER_PERIOD 25
30 #define IDLE_TIMEOUT 300
31 #define PTHREAD_STACK_SIZE 32768
32
33 /* 27262 is vendor DANTE Ltd. */
34 #define DEFAULT_TTL_ATTR "27262:1"
35
36 #define RAD_UDP 0
37 #define RAD_TLS 1
38 #define RAD_TCP 2
39 #define RAD_DTLS 3
40 #define RAD_PROTOCOUNT 4
41
42 enum rsp_fticks_reporting_type {
43     RSP_FTICKS_REPORTING_NONE = 0, /* Default.  */
44     RSP_FTICKS_REPORTING_BASIC,
45     RSP_FTICKS_REPORTING_FULL
46 };
47
48 enum rsp_fticks_mac_type {
49     RSP_FTICKS_MAC_STATIC = 0,
50     RSP_FTICKS_MAC_ORIGINAL,
51     RSP_FTICKS_MAC_VENDOR_HASHED,
52     RSP_FTICKS_MAC_VENDOR_KEY_HASHED, /* Default.  */
53     RSP_FTICKS_MAC_FULLY_HASHED,
54     RSP_FTICKS_MAC_FULLY_KEY_HASHED
55 };
56
57 struct options {
58     char *pidfile;
59     char *logdestination;
60     char *ftickssyslogfacility;
61     char *ttlattr;
62     uint32_t ttlattrtype[2];
63     uint8_t addttl;
64     uint8_t loglevel;
65     uint8_t loopprevention;
66     enum rsp_fticks_reporting_type fticks_reporting;
67     enum rsp_fticks_mac_type fticks_mac;
68     uint8_t *fticks_key;
69     uint8_t ipv4only;
70     uint8_t ipv6only;
71 };
72
73 struct commonprotoopts {
74     char **listenargs;
75     char *sourcearg;
76 };
77
78 struct request {
79     struct timeval created;
80     uint32_t refcount;
81     uint8_t *buf, *replybuf;
82     struct radmsg *msg;
83     struct client *from;
84     struct server *to;
85     char *origusername;
86     uint8_t rqid;
87     uint8_t rqauth[16];
88     uint8_t newid;
89     int udpsock; /* only for UDP */
90     uint16_t udpport; /* only for UDP */
91 };
92
93 /* requests that our client will send */
94 struct rqout {
95     pthread_mutex_t *lock;
96     struct request *rq;
97     uint8_t tries;
98     struct timeval expiry;
99 };
100
101 struct gqueue {
102     struct list *entries;
103     pthread_mutex_t mutex;
104     pthread_cond_t cond;
105 };
106
107 struct clsrvconf {
108     char *name;
109     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
110     const struct protodefs *pdef;
111     char **hostsrc;
112     int hostaf;
113     char *portsrc;
114     struct list *hostports;
115     char *secret;
116     char *tls;
117     char *matchcertattr;
118     regex_t *certcnregex;
119     regex_t *certuriregex;
120     char *confrewritein;
121     char *confrewriteout;
122     char *confrewriteusername;
123     struct modattr *rewriteusername;
124     char *dynamiclookupcommand;
125     uint8_t statusserver;
126     uint8_t retryinterval;
127     uint8_t retrycount;
128     uint8_t dupinterval;
129     uint8_t certnamecheck;
130     uint8_t addttl;
131     uint8_t loopprevention;
132     struct rewrite *rewritein;
133     struct rewrite *rewriteout;
134     pthread_mutex_t *lock; /* only used for updating clients so far */
135     struct tls *tlsconf;
136     struct list *clients;
137     struct server *servers;
138     char *fticks_viscountry;
139     char *fticks_visinst;
140 };
141
142 #include "tlscommon.h"
143
144 struct client {
145     struct clsrvconf *conf;
146     int sock;
147     SSL *ssl;
148     struct request *rqs[MAX_REQUESTS];
149     struct gqueue *replyq;
150     struct gqueue *rbios; /* for dtls */
151     struct sockaddr *addr;
152     time_t expiry; /* for udp */
153 };
154
155 struct server {
156     struct clsrvconf *conf;
157     int sock;
158     SSL *ssl;
159     pthread_mutex_t lock;
160     pthread_t clientth;
161     uint8_t clientrdgone;
162     struct timeval lastconnecttry;
163     struct timeval lastreply;
164     uint8_t connectionok;
165     uint8_t lostrqs;
166     uint8_t dynstartup;
167     uint8_t dynfailing;
168 #if defined ENABLE_EXPERIMENTAL_DYNDISC
169     uint8_t in_use;
170 #endif
171     char *dynamiclookuparg;
172     int nextid;
173     struct timeval lastrcv;
174     struct rqout *requests;
175     uint8_t newrq;
176     pthread_mutex_t newrq_mutex;
177     pthread_cond_t newrq_cond;
178     struct gqueue *rbios; /* for dtls */
179 };
180
181 struct realm {
182     char *name;
183     char *message;
184     uint8_t accresp;
185     regex_t regex;
186     uint32_t refcount;
187     pthread_mutex_t mutex;
188     struct realm *parent;
189     struct list *subrealms;
190     struct list *srvconfs;
191     struct list *accsrvconfs;
192 };
193
194 struct modattr {
195     uint8_t t;
196     char *replacement;
197     regex_t *regex;
198 };
199
200 struct rewrite {
201     uint8_t *removeattrs;
202     uint32_t *removevendorattrs;
203     struct list *addattrs;
204     struct list *modattrs;
205 };
206
207 struct protodefs {
208     char *name;
209     char *secretdefault;
210     int socktype;
211     char *portdefault;
212     uint8_t retrycountdefault;
213     uint8_t retrycountmax;
214     uint8_t retryintervaldefault;
215     uint8_t retryintervalmax;
216     uint8_t duplicateintervaldefault;
217     void (*setprotoopts)(struct commonprotoopts *);
218     char **(*getlistenerargs)();
219     void *(*listener)(void*);
220     int (*connecter)(struct server *, struct timeval *, int, char *);
221     void *(*clientconnreader)(void*);
222     int (*clientradput)(struct server *, unsigned char *);
223     void (*addclient)(struct client *);
224     void (*addserverextra)(struct clsrvconf *);
225     void (*setsrcres)();
226     void (*initextra)();
227 };
228
229 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
230
231 #define ATTRTYPE(x) ((x)[0])
232 #define ATTRLEN(x) ((x)[1])
233 #define ATTRVAL(x) ((x) + 2)
234 #define ATTRVALLEN(x) ((x)[1] - 2)
235
236 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
237 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
238 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
239 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
240 void removelockedclient(struct client *client);
241 void removeclient(struct client *client);
242 struct gqueue *newqueue();
243 void freebios(struct gqueue *q);
244 struct request *newrequest();
245 void freerq(struct request *rq);
246 int radsrv(struct request *rq);
247 void replyh(struct server *server, unsigned char *buf);
248 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
249 uint8_t *radattr2ascii(struct tlv *attr);
250 pthread_attr_t pthread_attr;
251
252 /* Local Variables: */
253 /* c-file-style: "stroustrup" */
254 /* End: */