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