summaryrefslogtreecommitdiff
path: root/usr.sbin/npppd/npppd/radius_req.c
blob: 98f9b84f6a20aa0f4ed4828e95d01bd22a390f79 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*	$OpenBSD: radius_req.c,v 1.5 2012/05/08 13:15:12 yasuoka Exp $ */

/*-
 * Copyright (c) 2009 Internet Initiative Japan Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/**@file
 * This file provides functions for RADIUS request using radius+.c and event(3).
 * @author	Yasuoka Masahiko
 * $Id: radius_req.c,v 1.5 2012/05/08 13:15:12 yasuoka Exp $
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <syslog.h>
#include <stdlib.h>
#include <radius+.h>
#include <radiusconst.h>
#include <debugutil.h>
#include <time.h>
#include <event.h>
#include <string.h>
#include <errno.h>

#include "radius_req.h"

#ifndef nitems
#define	nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
#endif

struct overlapped {
	struct event ev_sock;
	int socket;
	int ntry;
	int max_tries;
	int failovers;
	struct sockaddr_storage ss;
	struct timespec req_time;
	void *context;
	radius_response *response_fn;
	char secret[MAX_RADIUS_SECRET];
	RADIUS_PACKET *pkt;
	radius_req_setting *setting;
	int acct_delay_time;
};

static int   radius_request0 (struct overlapped *, int);
static int   radius_prepare_socket(struct overlapped *);
static void  radius_request_io_event (int, short, void *);
static void  radius_on_response(RADIUS_REQUEST_CTX, RADIUS_PACKET *, int, int);
static int   select_srcaddr(struct sockaddr const *, struct sockaddr *, socklen_t *);
static void  radius_req_setting_ref(radius_req_setting *);
static void  radius_req_setting_unref(radius_req_setting *);

#ifdef	RADIUS_REQ_DEBUG
#define RADIUS_REQ_DBG(x)	log_printf x
#define	RADIUS_REQ_ASSERT(cond)					\
	if (!(cond)) {						\
	    fprintf(stderr,					\
		"\nASSERT(" #cond ") failed on %s() at %s:%d.\n"\
		, __func__, __FILE__, __LINE__);		\
	    abort(); 						\
	}
#else
#define	RADIUS_REQ_ASSERT(cond)
#define RADIUS_REQ_DBG(x)
#endif

/**
 * Send RADIUS request message.  The pkt(RADIUS packet) will be released
 * by this implementation.
 */
void
radius_request(RADIUS_REQUEST_CTX ctx, RADIUS_PACKET *pkt)
{
	uint32_t ival;
	struct overlapped *lap;

	RADIUS_REQ_ASSERT(pkt != NULL);
	RADIUS_REQ_ASSERT(ctx != NULL);
	lap = ctx;
	lap->pkt = pkt;
	if (radius_get_uint32_attr(pkt, RADIUS_TYPE_ACCT_DELAY_TIME, &ival)
	    == 0)
		lap->acct_delay_time = 1;
	radius_request0(lap, 0);
}

/**
 * Prepare NAS-IP-Address or NAS-IPv6-Address.  If
 * setting->server[setting->curr_server].sock is not initialized, address
 * will be selected automatically.
 */
int
radius_prepare_nas_address(radius_req_setting *setting,
    RADIUS_PACKET *pkt)
{
	int af;
	struct sockaddr_in *sin4;
	struct sockaddr_in6 *sin6;
	socklen_t socklen;

	/* See RFC 2765, 3162 */
	RADIUS_REQ_ASSERT(setting != NULL);

	af = setting->server[setting->curr_server].peer.sin6.sin6_family;
	RADIUS_REQ_ASSERT(af == AF_INET6 || af == AF_INET);

	sin4 = &setting->server[setting->curr_server].sock.sin4;
	sin6 = &setting->server[setting->curr_server].sock.sin6;

	switch (af) {
	case AF_INET:
		socklen = sizeof(*sin4);
		if (sin4->sin_addr.s_addr == INADDR_ANY) {
			if (select_srcaddr((struct sockaddr const *)
			    &setting->server[setting->curr_server].peer,
			    (struct sockaddr *)sin4, &socklen) != 0) {
				RADIUS_REQ_ASSERT("NOTREACHED" == NULL);
				goto fail;
			}
		}
		if (radius_put_ipv4_attr(pkt, RADIUS_TYPE_NAS_IP_ADDRESS,
		    sin4->sin_addr) != 0)
			goto fail;
		break;
	case AF_INET6:
		socklen = sizeof(*sin6);
		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
			if (select_srcaddr((struct sockaddr const *)
			    &setting->server[setting->curr_server].peer,
			    (struct sockaddr *)sin4, &socklen) != 0) {
				RADIUS_REQ_ASSERT("NOTREACHED" == NULL);
				goto fail;
			}
		}
		if (radius_put_raw_attr(pkt, RADIUS_TYPE_NAS_IPV6_ADDRESS,
		    sin6->sin6_addr.s6_addr, sizeof(sin6->sin6_addr.s6_addr))
		    != 0)
			goto fail;
		break;
	}

	return 0;
fail:
	return 1;
}


/** Checks whether the request can fail over to another server */
int
radius_request_can_failover(RADIUS_REQUEST_CTX ctx)
{
	struct overlapped *lap;
	radius_req_setting *setting;

	lap = ctx;
	setting = lap->setting;

	if (lap->failovers >= setting->max_failovers)
		return 0;
	if (memcmp(&lap->ss, &setting->server[setting->curr_server].peer,
	    setting->server[setting->curr_server].peer.sin6.sin6_len) == 0)
		/* flagged server doesn't differ from the last server. */
		return 0;

	return 1;
}

/** Send RADIUS request failing over to another server. */
int
radius_request_failover(RADIUS_REQUEST_CTX ctx)
{
	struct overlapped *lap;

	lap = ctx;
	RADIUS_REQ_ASSERT(lap != NULL);
	RADIUS_REQ_ASSERT(lap->socket >= 0)

	if (!radius_request_can_failover(lap))
		return -1;

	if (radius_prepare_socket(lap) != 0)
		return -1;

	if (radius_request0(lap, 1) != 0)
		return -1;

	lap->failovers++;

	return 0;
}

static int
radius_prepare_socket(struct overlapped *lap)
{
	int sock;
	radius_req_setting *setting;
	struct sockaddr *sa;

	setting = lap->setting;
	if (lap->socket >= 0)
		close(lap->socket);
	lap->socket = -1;

	sa = (struct sockaddr *)&setting->server[setting->curr_server].peer;

	if ((sock = socket(sa->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		log_printf(LOG_ERR, "socket() failed in %s: %m", __func__);
		return -1;
	}
	if (connect(sock, sa, sa->sa_len) != 0) {
		log_printf(LOG_ERR, "connect() failed in %s: %m", __func__);
		close(sock);
		return -1;
	}
	memcpy(&lap->ss, sa, sa->sa_len);
	lap->socket = sock;
	memcpy(lap->secret, setting->server[setting->curr_server].secret,
	    sizeof(lap->secret));
	lap->ntry = lap->max_tries;

	return 0;
}

/**
 * Prepare sending RADIUS request.  This implementation will call back to
 * notice that it receives the response or it fails for timeouts to the
 * The context that is set as 'pctx' and response packet that is given
 * by the callback function will be released by this implementation internally.
 * @param setting	Setting for RADIUS server or request.
 * @param context	Context for the caller.
 * @param pctx		Pointer to the space for context of RADIUS request
 *			(RADIUS_REQUEST_CTX).  This will be used for canceling.
 *			NULL can be specified when you don't need.
 * @param response_fn	Specify callback function as a pointer. The function
 *			will be called when it receives a response or when
 *			request fails for timeouts.
 * @param timeout	response timeout in second.
 */
int
radius_prepare(radius_req_setting *setting, void *context,
    RADIUS_REQUEST_CTX *pctx, radius_response response_fn, int timeout)
{
	struct overlapped *lap;

	RADIUS_REQ_ASSERT(setting != NULL);
	lap = NULL;

	if (setting->server[setting->curr_server].enabled == 0)
		return 1;
	if ((lap = malloc(sizeof(struct overlapped))) == NULL) {
		log_printf(LOG_ERR, "malloc() failed in %s: %m", __func__);
		goto fail;
	}
	memset(lap, 0, sizeof(struct overlapped));
	lap->context = context;
	lap->response_fn = response_fn;
	lap->socket = -1;
	lap->setting = setting;

	if (timeout != 0 &&
	    (setting->max_tries == 0 ||
		    timeout < setting->max_tries * setting->timeout))
		lap->max_tries = timeout / setting->timeout;
	else
		lap->max_tries = setting->max_tries;

	if (lap->max_tries <= 0)
		lap->max_tries = 3;	/* default max tries */

	if (radius_prepare_socket(lap) != 0)
		goto fail;

	if (pctx != NULL)
		*pctx = lap;

	radius_req_setting_ref(setting);	

	return 0;
fail:
	if (lap != NULL)
		free(lap);

	return 1;
}

/**
 * Cancel the RADIUS request.
 * @param	The context received by {@link radius_request()}
 */
void
radius_cancel_request(RADIUS_REQUEST_CTX ctx)
{
	struct overlapped *lap;

	lap = ctx;
	if (lap->socket >= 0) {
		event_del(&lap->ev_sock);
		close(lap->socket);
		lap->socket = -1;
	}
	if (lap->pkt != NULL) {
		radius_delete_packet(lap->pkt);
		lap->pkt = NULL;
	}
	radius_req_setting_unref(lap->setting);

	memset(lap->secret, 0x41, sizeof(lap->secret));

	free(lap);
}

/** Return the shared secret for RADIUS server that is used by this context.  */
const char *
radius_get_server_secret(RADIUS_REQUEST_CTX ctx)
{
	struct overlapped *lap;

	lap = ctx;
	RADIUS_REQ_ASSERT(lap != NULL);

	return lap->secret;
}

/** Return the address of RADIUS server that is used by this context.  */
struct sockaddr *
radius_get_server_address(RADIUS_REQUEST_CTX ctx)
{
	struct overlapped *lap;

	lap = ctx;
	RADIUS_REQ_ASSERT(lap != NULL);

	return (struct sockaddr *)&lap->ss;
}

static int
radius_request0(struct overlapped *lap, int new_message)
{
	struct timeval tv0;

	RADIUS_REQ_ASSERT(lap->ntry > 0);

	if (lap->acct_delay_time != 0) {
		struct timespec curr, delta;

		if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) {
			log_printf(LOG_CRIT,
			    "clock_gettime(CLOCK_MONOTONIC,) failed: %m");
			RADIUS_REQ_ASSERT(0);
		}
		if (!timespecisset(&lap->req_time))
			lap->req_time = curr;
		else {
			timespecsub(&curr, &lap->req_time, &delta);
			if (radius_set_uint32_attr(lap->pkt,
			    RADIUS_TYPE_ACCT_DELAY_TIME, delta.tv_sec) == 0) {
				radius_update_id(lap->pkt);
				new_message = 1;
			}
		}
	}
	if (new_message) {
		radius_set_request_authenticator(lap->pkt,
		    radius_get_server_secret(lap));
	}

	lap->ntry--;
	if (radius_send(lap->socket, lap->pkt, 0) != 0) {
		log_printf(LOG_ERR, "sendto() failed in %s: %m",
		    __func__);
		radius_on_response(lap, NULL, RADIUS_REQUEST_ERROR, 1);
		return 1;
	}
	tv0.tv_usec = 0;
	tv0.tv_sec = lap->setting->timeout;

	event_set(&lap->ev_sock, lap->socket, EV_READ | EV_PERSIST,
	    radius_request_io_event, lap);
	event_add(&lap->ev_sock, &tv0);

	return 0;
}

static void
radius_request_io_event(int fd, short evmask, void *context)
{
	struct overlapped *lap;
	struct sockaddr_storage ss;
	int flags;
	socklen_t len;
	RADIUS_PACKET *respkt;

	RADIUS_REQ_ASSERT(context != NULL);

	lap = context;
	respkt = NULL;
	flags = 0;
	if ((evmask & EV_READ) != 0) {
		RADIUS_REQ_ASSERT(lap->socket >= 0);
		if (lap->socket < 0)
			return;
		RADIUS_REQ_ASSERT(lap->pkt != NULL);
		memset(&ss, 0, sizeof(ss));
		len = sizeof(ss);
		if ((respkt = radius_recv(lap->socket, 0)) == NULL) {
			RADIUS_REQ_DBG((LOG_DEBUG,
			    "radius_recv() on %s(): %m", __func__));
			/*
			 * Ignore error by icmp.  Wait a response from the
			 * server anyway, it may eventually become ready.
			 */
			switch (errno) {
			case EHOSTDOWN: case EHOSTUNREACH: case ECONNREFUSED:
				return;	/* sleep the rest of timeout time */
			}
			flags |= RADIUS_REQUEST_ERROR;
		} else if (lap->secret[0] == '\0') {
			flags |= RADIUS_REQUEST_CHECK_AUTHENTICATOR_NO_CHECK;
		} else {
			radius_set_request_packet(respkt, lap->pkt);
			if (!radius_check_response_authenticator(respkt,
			    lap->secret))
				flags |= RADIUS_REQUEST_CHECK_AUTHENTICATOR_OK;
		}
		radius_on_response(lap, respkt, flags, 0);
		radius_delete_packet(respkt);
	} else if ((evmask & EV_TIMEOUT) != 0) {
		if (lap->ntry > 0) {
			RADIUS_REQ_DBG((LOG_DEBUG,
			    "%s() timed out retry", __func__));
			radius_request0(lap, 0);
			return;
		}
		RADIUS_REQ_DBG((LOG_DEBUG, "%s() timed out", __func__));
		flags |= RADIUS_REQUEST_TIMEOUT;
		radius_on_response(lap, NULL, flags, 1);
	}
}

static void
radius_on_response(RADIUS_REQUEST_CTX ctx, RADIUS_PACKET *pkt, int flags,
    int server_failure)
{
	struct overlapped *lap;
	int failovers;

	lap = ctx;
	if (server_failure) {
		int i, n;
		struct sockaddr *sa_curr;

		sa_curr = (struct sockaddr *)&lap->setting->server[
		    lap->setting->curr_server].peer;
		if (sa_curr->sa_len == lap->ss.ss_len &&
		    memcmp(sa_curr, &lap->ss, sa_curr->sa_len) == 0) {
			/*
			 * The server on failure is flagged as the current.
			 * change the current
			 */
			for (i = 1; i < nitems(lap->setting->server); i++) {
				n = (lap->setting->curr_server + i) %
				    nitems(lap->setting->server);
				if (lap->setting->server[n].enabled) {
					lap->setting->curr_server = n;
					break;
				}
			}
		}
	}

	failovers = lap->failovers;
	if (lap->response_fn != NULL)
		lap->response_fn(lap->context, pkt, flags, ctx);
	if (failovers == lap->failovers)
		radius_cancel_request(lap);
}

static int
select_srcaddr(struct sockaddr const *dst, struct sockaddr *src,
    socklen_t *srclen)
{
	int sock;

	sock = -1;
	if ((sock = socket(dst->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
		goto fail;
	if (connect(sock, dst, dst->sa_len) != 0)
		goto fail;
	if (getsockname(sock, src, srclen) != 0)
		goto fail;

	close(sock);

	return 0;
fail:
	if (sock >= 0)
		close(sock);

	return 1;
}

radius_req_setting *
radius_req_setting_create(void)
{
	radius_req_setting *setting;

	if ((setting = malloc(sizeof(radius_req_setting))) == NULL)
		return NULL;
	memset(setting, 0, sizeof(radius_req_setting));

	return setting;
}

int
radius_req_setting_has_server(radius_req_setting *setting)
{
	return setting->server[setting->curr_server].enabled;
}

void
radius_req_setting_destroy(radius_req_setting *setting)
{
	setting->destroyed = 1;

	if (setting->refcnt == 0)
		free(setting);
}

static void
radius_req_setting_ref(radius_req_setting *setting)
{
	setting->refcnt++;
}

static void
radius_req_setting_unref(radius_req_setting *setting)
{
	setting->refcnt--;
	if (setting->destroyed)
		radius_req_setting_destroy(setting);
}