F-Ticks logging amendments
[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 };
135
136 #include "tlscommon.h"
137
138 struct client {
139     struct clsrvconf *conf;
140     int sock;
141     SSL *ssl;
142     struct request *rqs[MAX_REQUESTS];
143     struct gqueue *replyq;
144     struct gqueue *rbios; /* for dtls */
145     struct sockaddr *addr;
146     time_t expiry; /* for udp */
147 };
148
149 struct server {
150     struct clsrvconf *conf;
151     int sock;
152     SSL *ssl;
153     pthread_mutex_t lock;
154     pthread_t clientth;
155     uint8_t clientrdgone;
156     struct timeval lastconnecttry;
157     struct timeval lastreply;
158     uint8_t connectionok;
159     uint8_t lostrqs;
160     uint8_t dynstartup;
161     char *dynamiclookuparg;
162     int nextid;
163     struct timeval lastrcv;
164     struct rqout *requests;
165     uint8_t newrq;
166     pthread_mutex_t newrq_mutex;
167     pthread_cond_t newrq_cond;
168     struct gqueue *rbios; /* for dtls */
169 };
170
171 struct realm {
172     char *name;
173     char *message;
174     uint8_t accresp;
175     regex_t regex;
176     uint32_t refcount;
177     pthread_mutex_t mutex;
178     struct realm *parent;
179     struct list *subrealms;
180     struct list *srvconfs;
181     struct list *accsrvconfs;
182 };
183
184 struct modattr {
185     uint8_t t;
186     char *replacement;
187     regex_t *regex;
188 };
189
190 struct rewrite {
191     uint8_t *removeattrs;
192     uint32_t *removevendorattrs;
193     struct list *addattrs;
194     struct list *modattrs;
195 };
196
197 struct protodefs {
198     char *name;
199     char *secretdefault;
200     int socktype;
201     char *portdefault;
202     uint8_t retrycountdefault;
203     uint8_t retrycountmax;
204     uint8_t retryintervaldefault;
205     uint8_t retryintervalmax;
206     uint8_t duplicateintervaldefault;
207     void (*setprotoopts)(struct commonprotoopts *);
208     char **(*getlistenerargs)();
209     void *(*listener)(void*);
210     int (*connecter)(struct server *, struct timeval *, int, char *);
211     void *(*clientconnreader)(void*);
212     int (*clientradput)(struct server *, unsigned char *);
213     void (*addclient)(struct client *);
214     void (*addserverextra)(struct clsrvconf *);
215     void (*setsrcres)();
216     void (*initextra)();
217 };
218
219 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
220
221 #define ATTRTYPE(x) ((x)[0])
222 #define ATTRLEN(x) ((x)[1])
223 #define ATTRVAL(x) ((x) + 2)
224 #define ATTRVALLEN(x) ((x)[1] - 2)
225
226 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
227 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
228 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
229 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
230 void removelockedclient(struct client *client);
231 void removeclient(struct client *client);
232 struct gqueue *newqueue();
233 void freebios(struct gqueue *q);
234 struct request *newrequest();
235 void freerq(struct request *rq);
236 int radsrv(struct request *rq);
237 void replyh(struct server *server, unsigned char *buf);
238 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
239 uint8_t *radattr2ascii(struct tlv *attr);
240
241 /* Local Variables: */
242 /* c-file-style: "stroustrup" */
243 /* End: */