Handle failing rs_context_create().
[libradsec.git] / radsecproxy.h
1 /*
2  * Copyright (C) 2006-2009 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 2
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 gqueue {
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 **hostsrc;
83     char *portsrc;
84     struct list *hostports;
85     char *secret;
86     char *tls;
87     char *matchcertattr;
88     regex_t *certcnregex;
89     regex_t *certuriregex;
90     char *confrewritein;
91     char *confrewriteout;
92     char *confrewriteusername;
93     struct modattr *rewriteusername;
94     char *dynamiclookupcommand;
95     uint8_t statusserver;
96     uint8_t retryinterval;
97     uint8_t retrycount;
98     uint8_t dupinterval;
99     uint8_t certnamecheck;
100     uint8_t addttl;
101     uint8_t loopprevention;
102     struct rewrite *rewritein;
103     struct rewrite *rewriteout;
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 "rsp_tlscommon.h"
111
112 struct client {
113     struct clsrvconf *conf;
114     int sock;
115     SSL *ssl;
116     struct request *rqs[MAX_REQUESTS];
117     struct gqueue *replyq;
118     struct gqueue *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     uint8_t dynstartup;
135     char *dynamiclookuparg;
136     int nextid;
137     struct timeval lastrcv;
138     struct rqout *requests;
139     uint8_t newrq;
140     pthread_mutex_t newrq_mutex;
141     pthread_cond_t newrq_cond;
142     struct gqueue *rbios; /* for dtls */
143 };
144
145 struct realm {
146     char *name;
147     char *message;
148     uint8_t accresp;
149     regex_t regex;
150     uint32_t refcount;
151     pthread_mutex_t mutex;
152     struct realm *parent;
153     struct list *subrealms;
154     struct list *srvconfs;
155     struct list *accsrvconfs;
156 };
157
158 struct modattr {
159     uint8_t t;
160     char *replacement;
161     regex_t *regex;
162 };
163
164 struct rewrite {
165     uint8_t *removeattrs;
166     uint32_t *removevendorattrs;
167     struct list *addattrs;
168     struct list *modattrs;
169 };
170
171 struct protodefs {
172     char *name;
173     char *secretdefault;
174     int socktype;
175     char *portdefault;
176     uint8_t retrycountdefault;
177     uint8_t retrycountmax;
178     uint8_t retryintervaldefault;
179     uint8_t retryintervalmax;
180     uint8_t duplicateintervaldefault;
181     void (*setprotoopts)(struct commonprotoopts *);
182     char **(*getlistenerargs)();
183     void *(*listener)(void*);
184     int (*connecter)(struct server *, struct timeval *, int, char *);
185     void *(*clientconnreader)(void*);
186     int (*clientradput)(struct server *, unsigned char *);
187     void (*addclient)(struct client *);
188     void (*addserverextra)(struct clsrvconf *);
189     void (*setsrcres)();
190     void (*initextra)();
191 };
192
193 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
194
195 #define ATTRTYPE(x) ((x)[0])
196 #define ATTRLEN(x) ((x)[1])
197 #define ATTRVAL(x) ((x) + 2)
198 #define ATTRVALLEN(x) ((x)[1] - 2)
199
200 struct clsrvconf *find_clconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
201 struct clsrvconf *find_srvconf(uint8_t type, struct sockaddr *addr, struct list_node **cur);
202 struct clsrvconf *find_clconf_type(uint8_t type, struct list_node **cur);
203 struct client *addclient(struct clsrvconf *conf, uint8_t lock);
204 void removelockedclient(struct client *client);
205 void removeclient(struct client *client);
206 struct gqueue *newqueue();
207 void freebios(struct gqueue *q);
208 struct request *newrequest();
209 void freerq(struct request *rq);
210 int radsrv(struct request *rq);
211 void replyh(struct server *server, unsigned char *buf);
212 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
213
214 /* Local Variables: */
215 /* c-file-style: "stroustrup" */
216 /* End: */