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