Update copyright notices.
[libradsec.git] / radsecproxy.h
1 /*
2  * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
3  * Copyright (C) 2010 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 "tlv11.h"
11 #include "radmsg.h"
12 #include "gconfig.h"
13
14 #define DEBUG_LEVEL 2
15
16 #define CONFIG_MAIN "/etc/radsecproxy.conf"
17
18 /* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */
19 #define MAX_REQUESTS 256
20 #define REQUEST_RETRY_INTERVAL 5
21 #define REQUEST_RETRY_COUNT 2
22 #define DUPLICATE_INTERVAL REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT
23 #define MAX_CERT_DEPTH 5
24 #define STATUS_SERVER_PERIOD 25
25 #define IDLE_TIMEOUT 300
26
27 /* 27262 is vendor DANTE Ltd. */
28 #define DEFAULT_TTL_ATTR "27262:1"
29
30 #define RAD_UDP 0
31 #define RAD_TLS 1
32 #define RAD_TCP 2
33 #define RAD_DTLS 3
34 #define RAD_PROTOCOUNT 4
35
36 struct options {
37     char *logdestination;
38     char *ttlattr;
39     uint32_t ttlattrtype[2];
40     uint8_t addttl;
41     uint8_t loglevel;
42     uint8_t loopprevention;
43 };
44
45 struct commonprotoopts {
46     char **listenargs;
47     char *sourcearg;
48 };
49
50 struct request {
51     struct timeval created;
52     uint32_t refcount;
53     uint8_t *buf, *replybuf;
54     struct radmsg *msg;
55     struct client *from;
56     struct server *to;
57     char *origusername;
58     uint8_t rqid;
59     uint8_t rqauth[16];
60     uint8_t newid;
61     int udpsock; /* only for UDP */
62     uint16_t udpport; /* only for UDP */
63 };
64
65 /* requests that our client will send */
66 struct rqout {
67     pthread_mutex_t *lock;
68     struct request *rq;
69     uint8_t tries;
70     struct timeval expiry;
71 };
72
73 struct gqueue {
74     struct list *entries;
75     pthread_mutex_t mutex;
76     pthread_cond_t cond;
77 };
78
79 struct clsrvconf {
80     char *name;
81     uint8_t type; /* RAD_UDP/RAD_TLS/RAD_TCP */
82     const struct protodefs *pdef;
83     char **hostsrc;
84     char *portsrc;
85     struct list *hostports;
86     char *secret;
87     char *tls;
88     char *matchcertattr;
89     regex_t *certcnregex;
90     regex_t *certuriregex;
91     char *confrewritein;
92     char *confrewriteout;
93     char *confrewriteusername;
94     struct modattr *rewriteusername;
95     char *dynamiclookupcommand;
96     uint8_t statusserver;
97     uint8_t retryinterval;
98     uint8_t retrycount;
99     uint8_t dupinterval;
100     uint8_t certnamecheck;
101     uint8_t addttl;
102     uint8_t loopprevention;
103     struct rewrite *rewritein;
104     struct rewrite *rewriteout;
105     pthread_mutex_t *lock; /* only used for updating clients so far */
106     struct tls *tlsconf;
107     struct list *clients;
108     struct server *servers;
109 };
110
111 #include "tlscommon.h"
112
113 struct client {
114     struct clsrvconf *conf;
115     int sock;
116     SSL *ssl;
117     struct request *rqs[MAX_REQUESTS];
118     struct gqueue *replyq;
119     struct gqueue *rbios; /* for dtls */
120     struct sockaddr *addr;
121     time_t expiry; /* for udp */
122 };
123
124 struct server {
125     struct clsrvconf *conf;
126     int sock;
127     SSL *ssl;
128     pthread_mutex_t lock;
129     pthread_t clientth;
130     uint8_t clientrdgone;
131     struct timeval lastconnecttry;
132     struct timeval lastreply;
133     uint8_t connectionok;
134     uint8_t lostrqs;
135     uint8_t dynstartup;
136     char *dynamiclookuparg;
137     int nextid;
138     struct timeval lastrcv;
139     struct rqout *requests;
140     uint8_t newrq;
141     pthread_mutex_t newrq_mutex;
142     pthread_cond_t newrq_cond;
143     struct gqueue *rbios; /* for dtls */
144 };
145
146 struct realm {
147     char *name;
148     char *message;
149     uint8_t accresp;
150     regex_t regex;
151     uint32_t refcount;
152     pthread_mutex_t mutex;
153     struct realm *parent;
154     struct list *subrealms;
155     struct list *srvconfs;
156     struct list *accsrvconfs;
157 };
158
159 struct modattr {
160     uint8_t t;
161     char *replacement;
162     regex_t *regex;
163 };
164
165 struct rewrite {
166     uint8_t *removeattrs;
167     uint32_t *removevendorattrs;
168     struct list *addattrs;
169     struct list *modattrs;
170 };
171
172 struct protodefs {
173     char *name;
174     char *secretdefault;
175     int socktype;
176     char *portdefault;
177     uint8_t retrycountdefault;
178     uint8_t retrycountmax;
179     uint8_t retryintervaldefault;
180     uint8_t retryintervalmax;
181     uint8_t duplicateintervaldefault;
182     void (*setprotoopts)(struct commonprotoopts *);
183     char **(*getlistenerargs)();
184     void *(*listener)(void*);
185     int (*connecter)(struct server *, struct timeval *, int, char *);
186     void *(*clientconnreader)(void*);
187     int (*clientradput)(struct server *, unsigned char *);
188     void (*addclient)(struct client *);
189     void (*addserverextra)(struct clsrvconf *);
190     void (*setsrcres)();
191     void (*initextra)();
192 };
193
194 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
195
196 #define ATTRTYPE(x) ((x)[0])
197 #define ATTRLEN(x) ((x)[1])
198 #define ATTRVAL(x) ((x) + 2)
199 #define ATTRVALLEN(x) ((x)[1] - 2)
200
201 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
202 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
203 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
204 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
205 void removelockedclient(struct client *client);
206 void removeclient(struct client *client);
207 struct gqueue *newqueue();
208 void freebios(struct gqueue *q);
209 struct request *newrequest();
210 void freerq(struct request *rq);
211 int radsrv(struct request *rq);
212 void replyh(struct server *server, unsigned char *buf);
213 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
214
215 /* Local Variables: */
216 /* c-file-style: "stroustrup" */
217 /* End: */