aboutsummaryrefslogtreecommitdiffstats
path: root/src/libradius/radius_client.c
blob: acdac78c9f9f5712b61aa3693b4d0a9f55b46c3c (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
/*
 * Copyright (C) 2009 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "radius_client.h"
#include "radius_config.h"

#include <unistd.h>
#include <errno.h>

#include <debug.h>
#include <utils/host.h>
#include <utils/linked_list.h>
#include <threading/condvar.h>
#include <threading/mutex.h>

typedef struct private_radius_client_t private_radius_client_t;

/**
 * Private data of an radius_client_t object.
 */
struct private_radius_client_t {

	/**
	 * Public radius_client_t interface.
	 */
	radius_client_t public;

	/**
	 * Selected RADIUS server configuration
	 */
	radius_config_t *config;

	/**
	 * RADIUS servers State attribute
	 */
	chunk_t state;

	/**
	 * EAP MSK, from MPPE keys
	 */
	chunk_t msk;
};

/**
 * Save the state attribute to include in further request
 */
static void save_state(private_radius_client_t *this, radius_message_t *msg)
{
	enumerator_t *enumerator;
	int type;
	chunk_t data;

	enumerator = msg->create_enumerator(msg);
	while (enumerator->enumerate(enumerator, &type, &data))
	{
		if (type == RAT_STATE)
		{
			free(this->state.ptr);
			this->state = chunk_clone(data);
			enumerator->destroy(enumerator);
			return;
		}
	}
	enumerator->destroy(enumerator);
	/* no state attribute found, remove state */
	chunk_free(&this->state);
}

METHOD(radius_client_t, request, radius_message_t*,
	private_radius_client_t *this, radius_message_t *req)
{
	char virtual[] = {0x00,0x00,0x00,0x05};
	radius_socket_t *socket;
	radius_message_t *res;
	chunk_t data;

	/* we add the "Virtual" NAS-Port-Type, as we SHOULD include one */
	req->add(req, RAT_NAS_PORT_TYPE, chunk_create(virtual, sizeof(virtual)));
	/* add our NAS-Identifier */
	req->add(req, RAT_NAS_IDENTIFIER,
			 this->config->get_nas_identifier(this->config));
	/* add State attribute, if server sent one */
	if (this->state.ptr)
	{
		req->add(req, RAT_STATE, this->state);
	}
	socket = this->config->get_socket(this->config);
	DBG1(DBG_CFG, "sending RADIUS %N to server '%s'", radius_message_code_names,
		 req->get_code(req), this->config->get_name(this->config));

	res = socket->request(socket, req);
	if (res)
	{
		DBG1(DBG_CFG, "received RADIUS %N from server '%s'",
			 radius_message_code_names, res->get_code(res),
			 this->config->get_name(this->config));
		data = res->get_encoding(res);
		DBG3(DBG_CFG, "%B", &data);

		save_state(this, res);
		if (res->get_code(res) == RMC_ACCESS_ACCEPT)
		{
			chunk_clear(&this->msk);
			this->msk = socket->decrypt_msk(socket, req, res);
		}
		this->config->put_socket(this->config, socket, TRUE);
		return res;
	}
	this->config->put_socket(this->config, socket, FALSE);
	return NULL;
}

METHOD(radius_client_t, get_msk, chunk_t,
	private_radius_client_t *this)
{
	return this->msk;
}

METHOD(radius_client_t, destroy, void,
	private_radius_client_t *this)
{
	this->config->destroy(this->config);
	chunk_clear(&this->msk);
	free(this->state.ptr);
	free(this);
}

/**
 * See header
 */
radius_client_t *radius_client_create(radius_config_t *config)
{
	private_radius_client_t *this;

	INIT(this,
		.public = {
			.request = _request,
			.get_msk = _get_msk,
			.destroy = _destroy,
		},
		.config = config,
	);

	return &this->public;
}