req.c 7.6 KB
Newer Older
K
Kozlov Dmitry 已提交
1
#include <stdlib.h>
2
#include <stdio.h>
D
Dmitry Kozlov 已提交
3 4 5 6
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
K
Kozlov Dmitry 已提交
7
#include <sched.h>
D
Dmitry Kozlov 已提交
8 9 10
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
K
Kozlov Dmitry 已提交
11

D
Dmitry Kozlov 已提交
12
#include "log.h"
13
#include "radius_p.h"
K
Kozlov Dmitry 已提交
14

D
Dmitry Kozlov 已提交
15 16
#include "memdebug.h"

17
static int urandom_fd;
K
Kozlov Dmitry 已提交
18 19

static int rad_req_read(struct triton_md_handler_t *h);
D
Dmitry Kozlov 已提交
20
static void rad_req_timeout(struct triton_timer_t *t);
K
Kozlov Dmitry 已提交
21

22
struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username)
K
Kozlov Dmitry 已提交
23
{
K
Kozlov Dmitry 已提交
24
	struct rad_plugin_t *plugin;
D
Dmitry Kozlov 已提交
25
	struct rad_req_t *req = _malloc(sizeof(*req));
K
Kozlov Dmitry 已提交
26

27 28
	if (!req) {
		log_emerg("radius: out of memory\n");
K
Kozlov Dmitry 已提交
29
		return NULL;
30
	}
K
Kozlov Dmitry 已提交
31 32 33 34

	memset(req, 0, sizeof(*req));
	req->rpd = rpd;
	req->hnd.fd = -1;
35
	req->ctx.before_switch = log_switch;
36

37 38 39
	req->type = code == CODE_ACCESS_REQUEST ? RAD_SERV_AUTH : RAD_SERV_ACCT;

	req->serv = rad_server_get(req->type);
K
temp  
Kozlov Dmitry 已提交
40 41 42 43 44
	if (!req->serv)
		goto out_err;
	
	req->server_addr = req->serv->auth_addr;
	req->server_port = req->serv->auth_port;
K
Kozlov Dmitry 已提交
45

46 47 48 49
	while (1) {
		if (read(urandom_fd, req->RA, 16) != 16) {
			if (errno == EINTR)
				continue;
50
			log_ppp_error("radius:req:read urandom: %s\n", strerror(errno));
51 52 53 54 55 56 57 58 59
			goto out_err;
		}
		break;
	}

	req->pack = rad_packet_alloc(code);
	if (!req->pack)
		goto out_err;

K
Kozlov Dmitry 已提交
60
	if (rad_packet_add_str(req->pack, NULL, "User-Name", username))
K
Kozlov Dmitry 已提交
61 62
		goto out_err;
	if (conf_nas_identifier)
K
Kozlov Dmitry 已提交
63
		if (rad_packet_add_str(req->pack, NULL, "NAS-Identifier", conf_nas_identifier))
K
Kozlov Dmitry 已提交
64
			goto out_err;
65
	if (conf_nas_ip_address)
K
Kozlov Dmitry 已提交
66
		if (rad_packet_add_ipaddr(req->pack, NULL, "NAS-IP-Address", conf_nas_ip_address))
67
			goto out_err;
K
Kozlov Dmitry 已提交
68
	if (rad_packet_add_int(req->pack, NULL, "NAS-Port", rpd->ppp->unit_idx))
K
Kozlov Dmitry 已提交
69
		goto out_err;
K
Kozlov Dmitry 已提交
70
	if (rad_packet_add_val(req->pack, NULL, "NAS-Port-Type", "Virtual"))
K
Kozlov Dmitry 已提交
71
		goto out_err;
K
Kozlov Dmitry 已提交
72
	if (rad_packet_add_val(req->pack, NULL, "Service-Type", "Framed-User"))
K
Kozlov Dmitry 已提交
73
		goto out_err;
K
Kozlov Dmitry 已提交
74
	if (rad_packet_add_val(req->pack, NULL, "Framed-Protocol", "PPP"))
K
Kozlov Dmitry 已提交
75
		goto out_err;
76
	if (rpd->ppp->ctrl->calling_station_id)
K
Kozlov Dmitry 已提交
77
		if (rad_packet_add_str(req->pack, NULL, "Calling-Station-Id", rpd->ppp->ctrl->calling_station_id))
78 79
			goto out_err;
	if (rpd->ppp->ctrl->called_station_id)
K
Kozlov Dmitry 已提交
80
		if (rad_packet_add_str(req->pack, NULL, "Called-Station-Id", rpd->ppp->ctrl->called_station_id))
81
			goto out_err;
82
	if (rpd->attr_class)
K
Kozlov Dmitry 已提交
83
		if (rad_packet_add_octets(req->pack, NULL, "Class", rpd->attr_class, rpd->attr_class_len))
84
			goto out_err;
K
Kozlov Dmitry 已提交
85

K
Kozlov Dmitry 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
	list_for_each_entry(plugin, &req->rpd->plugin_list, entry) {
		switch (code) {
			case CODE_ACCESS_REQUEST:
				if (plugin->send_access_request && plugin->send_access_request(plugin, req->pack))
					goto out_err;
				break;
			case CODE_ACCOUNTING_REQUEST:
				if (plugin->send_accounting_request && plugin->send_accounting_request(plugin, req->pack))
					goto out_err;
				break;
		}
	}

K
Kozlov Dmitry 已提交
99
	return req;
D
Dmitry Kozlov 已提交
100 101

out_err:
102 103 104 105
	if (!req->serv)
		log_ppp_error("radius: no servers available\n");
	else
		log_emerg("radius: out of memory\n");
D
Dmitry Kozlov 已提交
106 107
	rad_req_free(req);
	return NULL;
K
Kozlov Dmitry 已提交
108 109
}

110 111
int rad_req_acct_fill(struct rad_req_t *req)
{
D
Dmitry Kozlov 已提交
112 113
	struct ipv6db_addr_t *a;

K
temp  
Kozlov Dmitry 已提交
114 115
	req->server_addr = req->serv->acct_addr;
	req->server_port = req->serv->acct_port;
116 117 118

	memset(req->RA, 0, sizeof(req->RA));

K
Kozlov Dmitry 已提交
119
	if (rad_packet_add_val(req->pack, NULL, "Acct-Status-Type", "Start"))
120
		return -1;
K
Kozlov Dmitry 已提交
121
	if (rad_packet_add_val(req->pack, NULL, "Acct-Authentic", "RADIUS"))
122
		return -1;
K
Kozlov Dmitry 已提交
123
	if (rad_packet_add_str(req->pack, NULL, "Acct-Session-Id", req->rpd->ppp->sessionid))
124
		return -1;
K
Kozlov Dmitry 已提交
125
	if (rad_packet_add_int(req->pack, NULL, "Acct-Session-Time", 0))
126
		return -1;
K
Kozlov Dmitry 已提交
127
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Octets", 0))
128
		return -1;
K
Kozlov Dmitry 已提交
129
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Octets", 0))
130
		return -1;
K
Kozlov Dmitry 已提交
131
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Packets", 0))
132
		return -1;
K
Kozlov Dmitry 已提交
133
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Packets", 0))
134
		return -1;
K
Kozlov Dmitry 已提交
135
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Gigawords", 0))
136
		return -1;
K
Kozlov Dmitry 已提交
137
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Gigawords", 0))
138
		return -1;
139 140 141 142
	if (conf_acct_delay_time) {
		if (rad_packet_add_int(req->pack, NULL, "Acct-Delay-Time", 0))
			return -1;
	}
143 144 145 146
	if (req->rpd->ppp->ipv4) {
		if (rad_packet_add_ipaddr(req->pack, NULL, "Framed-IP-Address", req->rpd->ppp->ipv4->peer_addr))
			return -1;
	}
D
Dmitry Kozlov 已提交
147
	if (req->rpd->ppp->ipv6) {
K
Kozlov Dmitry 已提交
148
		if (rad_packet_add_ifid(req->pack, NULL, "Framed-Interface-Id", req->rpd->ppp->ipv6->peer_intf_id))
D
Dmitry Kozlov 已提交
149 150 151 152 153 154
			return -1;
		list_for_each_entry(a, &req->rpd->ppp->ipv6->addr_list, entry) {
			if (rad_packet_add_ipv6prefix(req->pack, NULL, "Framed-IPv6-Prefix", &a->addr, a->prefix_len))
				return -1;
		}
	}
155 156 157 158

	return 0;
}

159
void rad_req_free(struct rad_req_t *req)
K
Kozlov Dmitry 已提交
160
{
K
temp  
Kozlov Dmitry 已提交
161
	if (req->serv)
162
		rad_server_put(req->serv, req->type);
163 164 165 166 167 168
	if (req->hnd.fd >= 0 )
		close(req->hnd.fd);
	if (req->pack)
		rad_packet_free(req->pack);
	if (req->reply)
		rad_packet_free(req->reply);
D
Dmitry Kozlov 已提交
169
	_free(req);
K
Kozlov Dmitry 已提交
170 171
}

172
static int make_socket(struct rad_req_t *req)
K
Kozlov Dmitry 已提交
173 174
{
  struct sockaddr_in addr;
175

176 177
	req->hnd.fd = socket(PF_INET, SOCK_DGRAM, 0);
	if (req->hnd.fd < 0) {
178
		log_ppp_error("radius:socket: %s\n", strerror(errno));
179 180
		return -1;
	}
K
Kozlov Dmitry 已提交
181

182 183
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
K
Kozlov Dmitry 已提交
184

K
Kozlov Dmitry 已提交
185 186
	if (conf_bind) {
		addr.sin_addr.s_addr = conf_bind;
187
		if (bind(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) {
188
			log_ppp_error("radius:bind: %s\n", strerror(errno));
K
Kozlov Dmitry 已提交
189 190
			goto out_err;
		}
191
	}
K
Kozlov Dmitry 已提交
192

193
	addr.sin_addr.s_addr = req->server_addr;
194
	addr.sin_port = htons(req->server_port);
K
Kozlov Dmitry 已提交
195

196
	if (connect(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) {
197
		log_ppp_error("radius:connect: %s\n", strerror(errno));
198 199 200 201
		goto out_err;
	}

	if (fcntl(req->hnd.fd, F_SETFL, O_NONBLOCK)) {
202
		log_ppp_error("radius: failed to set nonblocking mode: %s\n", strerror(errno));
203
		goto out_err;
K
Kozlov Dmitry 已提交
204 205
	}
	
206 207 208 209 210 211 212 213
	return 0;

out_err:
	close(req->hnd.fd);
	req->hnd.fd = -1;
	return -1;
}

214
int rad_req_send(struct rad_req_t *req, int verbose)
215 216 217 218 219 220 221
{
	if (req->hnd.fd == -1 && make_socket(req))
		return -1;

	if (!req->pack->buf && rad_packet_build(req->pack, req->RA))
		goto out_err;
	
222
	if (verbose) {
223
		log_ppp_info1("send ");
224
		rad_packet_print(req->pack, req->serv, log_ppp_info1);
225 226
	}

227
	rad_packet_send(req->pack, req->hnd.fd, NULL);
K
Kozlov Dmitry 已提交
228 229 230 231 232 233 234 235 236

	return 0;

out_err:
	close(req->hnd.fd);
	req->hnd.fd = -1;
	return -1;
}

237 238
static void req_wakeup(struct rad_req_t *req)
{
239
	struct triton_context_t *ctx = req->rpd->ppp->ctrl->ctx;
K
Kozlov Dmitry 已提交
240 241
	if (req->timeout.tpd)
		triton_timer_del(&req->timeout);
242 243
	triton_md_unregister_handler(&req->hnd);
	triton_context_unregister(&req->ctx);
244
	triton_context_wakeup(ctx);
245
}
K
Kozlov Dmitry 已提交
246 247 248
static int rad_req_read(struct triton_md_handler_t *h)
{
	struct rad_req_t *req = container_of(h, typeof(*req), hnd);
K
Kozlov Dmitry 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	struct rad_packet_t *pack;
	int r;

	while (1) {
		r = rad_packet_recv(h->fd, &pack, NULL);
		
		if (pack) {
			if (req->reply)
				rad_packet_free(req->reply);
			req->reply = pack;
		}

		if (r)
			break;
	}
K
Kozlov Dmitry 已提交
264

265 266
	req_wakeup(req);
	
D
Dmitry Kozlov 已提交
267
	return 1;
K
Kozlov Dmitry 已提交
268
}
D
Dmitry Kozlov 已提交
269
static void rad_req_timeout(struct triton_timer_t *t)
K
Kozlov Dmitry 已提交
270
{
271 272 273
	struct rad_req_t *req = container_of(t, typeof(*req), timeout);
	
	req_wakeup(req);
K
Kozlov Dmitry 已提交
274 275
}

D
Dmitry Kozlov 已提交
276
int rad_req_wait(struct rad_req_t *req, int timeout)
K
Kozlov Dmitry 已提交
277
{
278 279 280
	req->hnd.read = rad_req_read;
	req->timeout.expire = rad_req_timeout;

281
	triton_context_register(&req->ctx, req->rpd->ppp);
282
	triton_md_register_handler(&req->ctx, &req->hnd);
K
Kozlov Dmitry 已提交
283
	triton_md_enable_handler(&req->hnd, MD_MODE_READ);
K
Kozlov Dmitry 已提交
284

D
Dmitry Kozlov 已提交
285
	req->timeout.period = timeout * 1000;
K
Kozlov Dmitry 已提交
286 287 288
	triton_timer_add(&req->ctx, &req->timeout, 0);
	
	triton_context_wakeup(&req->ctx);
K
Kozlov Dmitry 已提交
289

290
	triton_context_schedule();
K
Kozlov Dmitry 已提交
291

292
	if (conf_verbose && req->reply) {
293
		log_ppp_info1("recv ");
294
		rad_packet_print(req->reply, req->serv, log_ppp_info1);
295
	}
K
Kozlov Dmitry 已提交
296 297 298
	return 0;
}

299
static void req_init(void)
300 301 302
{
	urandom_fd = open("/dev/urandom", O_RDONLY);
	if (!urandom_fd) {
303
		log_emerg("radius:req: open /dev/urandom: %s\n", strerror(errno));
304 305 306
		_exit(EXIT_FAILURE);
	}
}
307

K
Kozlov Dmitry 已提交
308
DEFINE_INIT(50, req_init);