Add top-level config options IPv4Only and IPv6Only.
[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
32 /* 27262 is vendor DANTE Ltd. */
33 #define DEFAULT_TTL_ATTR "27262:1"
34
35 #define RAD_UDP 0
36 #define RAD_TLS 1
37 #define RAD_TCP 2
38 #define RAD_DTLS 3
39 #define RAD_PROTOCOUNT 4
40
41 enum rsp_fticks_reporting_type {
42     RSP_FTICKS_REPORTING_NONE = 0, /* Default.  */
43     RSP_FTICKS_REPORTING_BASIC,
44     RSP_FTICKS_REPORTING_FULL
45 };
46
47 enum rsp_fticks_mac_type {
48     RSP_FTICKS_MAC_STATIC = 0,
49     RSP_FTICKS_MAC_ORIGINAL,
50     RSP_FTICKS_MAC_VENDOR_HASHED,
51     RSP_FTICKS_MAC_VENDOR_KEY_HASHED, /* Default.  */
52     RSP_FTICKS_MAC_FULLY_HASHED,
53     RSP_FTICKS_MAC_FULLY_KEY_HASHED
54 };
55
56 struct options {
57     char *pidfile;
58     char *logdestination;
59     char *ftickssyslogfacility;
60     char *ttlattr;
61     uint32_t ttlattrtype[2];
62     uint8_t addttl;
63     uint8_t loglevel;
64     uint8_t loopprevention;
65     enum rsp_fticks_reporting_type fticks_reporting;
66     enum rsp_fticks_mac_type fticks_mac;
67     uint8_t *fticks_key;
68     uint8_t ipv4only;
69     uint8_t ipv6only;
70 };
71
72 struct commonprotoopts {
73     char **listenargs;
74     char *sourcearg;
75 };
76
77 struct request {
78     struct timeval created;
79     uint32_t refcount;
80     uint8_t *buf, *replybuf;
81     struct radmsg *msg;
82     struct client *from;
83     struct server *to;
84     char *origusername;
85     uint8_t rqid;
86     uint8_t rqauth[16];
87     uint8_t newid;
88     int udpsock; /* only for UDP */
89     uint16_t udpport; /* only for UDP */
90 };
91
92 /* requests that our client will send */
93 struct rqout {
94     pthread_mutex_t *lock;
95     struct request *rq;
96     uint8_t tries;
97     struct timeval expiry;
98 };
99
100 struct gqueue {
101     struct list *entries;
102     pthread_mutex_t mutex;
103     pthread_cond_t cond;
104 };
105
106 struct clsrvconf {
107     char *name;
108     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
109     const struct protodefs *pdef;
110     char **hostsrc;
111     int hostaf;
112     char *portsrc;
113     struct list *hostports;
114     char *secret;
115     char *tls;
116     char *matchcertattr;
117     regex_t *certcnregex;
118     regex_t *certuriregex;
119     char *confrewritein;
120     char *confrewriteout;
121     char *confrewriteusername;
122     struct modattr *rewriteusername;
123     char *dynamiclookupcommand;
124     uint8_t statusserver;
125     uint8_t retryinterval;
126     uint8_t retrycount;
127     uint8_t dupinterval;
128     uint8_t certnamecheck;
129     uint8_t addttl;
130     uint8_t loopprevention;
131     struct rewrite *rewritein;
132     struct rewrite *rewriteout;
133     pthread_mutex_t *lock; /* only used for updating clients so far */
134     struct tls *tlsconf;
135     struct list *clients;
136     struct server *servers;
137     char *fticks_viscountry;
138     char *fticks_visinst;
139 };
140
141 #include "tlscommon.h"
142
143 struct client {
144     struct clsrvconf *conf;
145     int sock;
146     SSL *ssl;
147     struct request *rqs[MAX_REQUESTS];
148     struct gqueue *replyq;
149     struct gqueue *rbios; /* for dtls */
150     struct sockaddr *addr;
151     time_t expiry; /* for udp */
152 };
153
154 struct server {
155     struct clsrvconf *conf;
156     int sock;
157     SSL *ssl;
158     pthread_mutex_t lock;
159     pthread_t clientth;
160     uint8_t clientrdgone;
161     struct timeval lastconnecttry;
162     struct timeval lastreply;
163     uint8_t connectionok;
164     uint8_t lostrqs;
165     uint8_t dynstartup;
166     uint8_t dynfailing;
167     char *dynamiclookuparg;
168     int nextid;
169     struct timeval lastrcv;
170     struct rqout *requests;
171     uint8_t newrq;
172     pthread_mutex_t newrq_mutex;
173     pthread_cond_t newrq_cond;
174     struct gqueue *rbios; /* for dtls */
175 };
176
177 struct realm {
178     char *name;
179     char *message;
180     uint8_t accresp;
181     regex_t regex;
182     uint32_t refcount;
183     pthread_mutex_t mutex;
184     struct realm *parent;
185     struct list *subrealms;
186     struct list *srvconfs;
187     struct list *accsrvconfs;
188 };
189
190 struct modattr {
191     uint8_t t;
192     char *replacement;
193     regex_t *regex;
194 };
195
196 struct rewrite {
197     uint8_t *removeattrs;
198     uint32_t *removevendorattrs;
199     struct list *addattrs;
200     struct list *modattrs;
201 };
202
203 struct protodefs {
204     char *name;
205     char *secretdefault;
206     int socktype;
207     char *portdefault;
208     uint8_t retrycountdefault;
209     uint8_t retrycountmax;
210     uint8_t retryintervaldefault;
211     uint8_t retryintervalmax;
212     uint8_t duplicateintervaldefault;
213     void (*setprotoopts)(struct commonprotoopts *);
214     char **(*getlistenerargs)();
215     void *(*listener)(void*);
216     int (*connecter)(struct server *, struct timeval *, int, char *);
217     void *(*clientconnreader)(void*);
218     int (*clientradput)(struct server *, unsigned char *);
219     void (*addclient)(struct client *);
220     void (*addserverextra)(struct clsrvconf *);
221     void (*setsrcres)();
222     void (*initextra)();
223 };
224
225 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
226
227 #define ATTRTYPE(x) ((x)[0])
228 #define ATTRLEN(x) ((x)[1])
229 #define ATTRVAL(x) ((x) + 2)
230 #define ATTRVALLEN(x) ((x)[1] - 2)
231
232 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
233 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
234 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
235 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
236 void removelockedclient(struct client *client);
237 void removeclient(struct client *client);
238 struct gqueue *newqueue();
239 void freebios(struct gqueue *q);
240 struct request *newrequest();
241 void freerq(struct request *rq);
242 int radsrv(struct request *rq);
243 void replyh(struct server *server, unsigned char *buf);
244 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
245 uint8_t *radattr2ascii(struct tlv *attr);
246
247 /* Local Variables: */
248 /* c-file-style: "stroustrup" */
249 /* End: */