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