summaryrefslogtreecommitdiff
path: root/accel-pppd/radius/serv.c
blob: bcdce144cbbd7906d0343fd16038614ca12e38b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "log.h"
#include "triton.h"
#include "radius_p.h"

#include "memdebug.h"

static LIST_HEAD(serv_list);

struct rad_server_t *rad_server_get(int type)
{
	struct rad_server_t *s, *s0 = NULL;
	struct timespec ts;
	
	clock_gettime(CLOCK_MONOTONIC, &ts);

	list_for_each_entry(s, &serv_list, entry) {
		if (s->fail_time && ts.tv_sec < s->fail_time)
			continue;

		if (type == 0 && !s->auth_addr)
			continue;
		else if (type == 1 && !s->acct_addr)
			continue;

		if (!s0) {
			s0 = s;
			continue;
		}

		if (s->client_cnt < s0->client_cnt)
			s0 = s;
	}

	if (!s0)
		return NULL;

	__sync_add_and_fetch(&s0->client_cnt, 1);

	return s0;
}

void rad_server_put(struct rad_server_t *s)
{
	__sync_sub_and_fetch(&s->client_cnt, 1);
}

int rad_server_req_enter(struct rad_req_t *req)
{
	struct timespec ts;
	
	clock_gettime(CLOCK_MONOTONIC, &ts);
	
	if (!req->serv->max_req_cnt)
		return 0;

	pthread_mutex_lock(&req->serv->lock);
	
	if (ts.tv_sec < req->serv->fail_time) {
		pthread_mutex_unlock(&req->serv->lock);
		return -1;
	}

	if (req->serv->req_cnt >= req->serv->max_req_cnt) {
		list_add_tail(&req->entry, &req->serv->req_queue);
		pthread_mutex_unlock(&req->serv->lock);
		triton_context_schedule();
		pthread_mutex_lock(&req->serv->lock);

		if (ts.tv_sec < req->serv->fail_time) {
			pthread_mutex_unlock(&req->serv->lock);
			return -1;
		}
	}

	req->serv->req_cnt++;
	pthread_mutex_unlock(&req->serv->lock);

	return 0;
}

void rad_server_req_exit(struct rad_req_t *req)
{
	struct rad_req_t *r = NULL;
	
	if (!req->serv->max_req_cnt)
		return;

	pthread_mutex_lock(&req->serv->lock);
	req->serv->req_cnt--;
	if (req->serv->req_cnt < req->serv->max_req_cnt && !list_empty(&req->serv->req_queue)) {
		r = list_entry(req->serv->req_queue.next, typeof(*r), entry);
		list_del(&r->entry);
	}
	pthread_mutex_unlock(&req->serv->lock);

	if (r)
		triton_context_wakeup(r->rpd->ppp->ctrl->ctx);
}

int rad_server_realloc(struct rad_req_t *req, int type)
{
	if (req->hnd.fd != -1) {
		close(req->hnd.fd);
		req->hnd.fd = -1;
	}

	if (req->serv)
		rad_server_put(req->serv);

	req->serv = rad_server_get(type);

	if (!req->serv)
		return -1;

	if (type) {
		req->server_addr = req->serv->acct_addr;
		req->server_port = req->serv->acct_port;
	} else {
		req->server_addr = req->serv->auth_addr;
		req->server_port = req->serv->auth_port;
	}

	return 0;
}

void rad_server_fail(struct rad_server_t *s)
{
	struct rad_req_t *r;
	struct timespec ts;
	
	clock_gettime(CLOCK_MONOTONIC, &ts);

	pthread_mutex_lock(&s->lock);

	if (ts.tv_sec > s->fail_time) {
		s->fail_time = ts.tv_sec + s->conf_fail_time;
		log_ppp_warn("radius: server not responding\n");
		log_warn("radius: server noy responding\n");
	}

	while (!list_empty(&s->req_queue)) {
		r = list_entry(s->req_queue.next, typeof(*r), entry);
		list_del(&r->entry);
		triton_context_wakeup(r->rpd->ppp->ctrl->ctx);
	}
	pthread_mutex_unlock(&s->lock);
}

static void __add_server(struct rad_server_t *s)
{
	INIT_LIST_HEAD(&s->req_queue);
	pthread_mutex_init(&s->lock, NULL);
	s->conf_fail_time = conf_fail_time;
	list_add_tail(&s->entry, &serv_list);
}

static int parse_server_old(const char *opt, in_addr_t *addr, int *port, char **secret)
{
	char *str = _strdup(opt);
	char *p1, *p2;

	p1 = strstr(str, ":");
	p2 = strstr(str, ",");

	if (p1)
		*p1 = 0;
	if (p2)
		*p2 = 0;
	else
		return -1;
	
	*addr = inet_addr(str);
	
	if (p1) {
		*port = atoi(p1 + 1);
		if (*port <=0 )
			return -1;
	}

	p1 = _strdup(p2 + 1);
	p2 = *secret;
	*secret = p1;
	if (p2)
		_free(p2);
	
	_free(str);

	return 0;
}

static void add_server_old(void)
{
	const char *opt;
	struct rad_server_t *s = _malloc(sizeof(*s));

	memset(s, 0, sizeof(*s));

	opt = conf_get_opt("radius", "auth-server");
	if (opt) {
		if (parse_server_old(opt, &s->auth_addr, &s->auth_port, &s->auth_secret)) {
			log_emerg("radius: failed to parse 'auth-server'\n");
			goto out;
		}
	}

	opt = conf_get_opt("radius", "acct-server");
	if (opt) {
		if (parse_server_old(opt, &s->acct_addr, &s->acct_port, &s->acct_secret)) {
			log_emerg("radius: failed to parse 'acct-server'\n");
			goto out;
		}
		conf_accounting = 1;
	}

	if (s->auth_addr || s->acct_addr) {
		__add_server(s);
		return;
	}

out:
	_free(s);
}

static int parse_server(const char *_opt, struct rad_server_t *s)
{
	char *opt = _strdup(_opt);
	char *ptr1, *ptr2, *ptr3, *endptr;

	ptr1 = strchr(opt, ',');
	if (!ptr1)
		goto out;
	
	ptr2 = strchr(ptr1 + 1, ',');

	if (ptr2)
		ptr3 = strchr(ptr2 + 1, ',');
	else
		ptr3 = NULL;
	
	*ptr1 = 0;
	if (ptr2)
		*ptr2 = 0;
	if (ptr3)
		*ptr3 = 0;

	s->auth_addr = s->acct_addr = inet_addr(opt);

	if (ptr2) {
		if (ptr2[1]) {
			s->auth_port = strtol(ptr2 + 1, &endptr, 10);
			if (*endptr)
				goto out;
		}
		if (!s->auth_port)
			s->auth_addr = 0;
	} else
		s->auth_port = 1812;
	
	if (ptr3) {
		if (ptr3[1]) {
			s->acct_port = strtol(ptr3 + 1, &endptr, 10);
			if (*endptr)
				goto out;
		}
		if (!s->acct_port)
			s->acct_addr = 0;
	} else
		s->acct_port = 1813;

	if (!s->auth_addr && !s->acct_addr)
		goto out;

	if (s->auth_addr)
		s->auth_secret = _strdup(ptr1 + 1);
	
	if (s->acct_addr) {
		s->acct_secret = _strdup(ptr1 + 1);
		conf_accounting = 1;
	}

	return 0;

out:
	_free(opt);

	return -1;
}

static void add_server(const char *opt)
{
	struct rad_server_t *s = _malloc(sizeof(*s));
	
	memset(s, 0, sizeof(*s));

	if (parse_server(opt, s)) {
		log_emerg("radius: failed to parse '%s'\n", opt);
		_free(s);
		return;
	}

	__add_server(s);
}

static void init(void)
{
	struct conf_sect_t *s = conf_get_section("radius");
	struct conf_option_t *opt;

	add_server_old();


	list_for_each_entry(opt, &s->items, entry) {
		if (strcmp(opt->name, "server"))
			continue;
		add_server(opt->val);
	}
}

DEFINE_INIT(21, init);