summaryrefslogtreecommitdiff
path: root/accel-pptpd/radius/radius.c
blob: 89491b34eb73f32075b81998cda41e71432a6def (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
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#include "log.h"
#include "ppp.h"
#include "pwdb.h"
#include "ipdb.h"

#include "radius.h"

#define CHAP_MD5 5
#define MSCHAP_V1 0x80
#define MSCHAP_V2 0x81

int conf_max_try = 3;
int conf_timeout = 3;
char *conf_nas_identifier = "accel-pptpd";
char *conf_nas_ip_address;
char *conf_gw_ip_address;
int conf_verbose = 0;

char *conf_auth_server;
int conf_auth_server_port = 1812;
char *conf_auth_server_secret;

char *conf_acct_server;
int conf_acct_server_port = 1813;
char *conf_acct_server_secret;

static struct ppp_notified_t notified;

void rad_proc_attrs(struct rad_req_t *req)
{
	struct rad_req_attr_t *attr;

	list_for_each_entry(attr, &req->reply->attrs, entry) {
		if (!strcmp(attr->attr->name, "Framed-IP-Address"))
			req->rpd->ipaddr = attr->val.ipaddr;
		else if (!strcmp(attr->attr->name, "Acct-Interim-Interval"))
			req->rpd->acct_interim_interval = attr->val.integer;
	}
}

static int check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, int type, va_list _args)
{
	int r = PWDB_NO_IMPL;
	va_list args;
	int chap_type;
	struct radius_pd_t *rpd = find_pd(ppp);

	va_copy(args, _args);

	switch(type) {
		case PPP_PAP:
			r = rad_auth_pap(rpd, username, args);
			break;
		case PPP_CHAP:
			chap_type = va_arg(args, int);
			switch(chap_type) {
				case CHAP_MD5:
					r = rad_auth_chap_md5(rpd, username, args);
					break;
				case MSCHAP_V1:
					r = rad_auth_mschap_v1(rpd, username, args);
					break;
				case MSCHAP_V2:
					r = rad_auth_mschap_v2(rpd, username, args);
					break;
			}
			break;
	}

	va_end(args);

	return r;
}

static int get_ip(struct ppp_t *ppp, in_addr_t *addr, in_addr_t *peer_addr)
{
	struct radius_pd_t *rpd = find_pd(ppp);
	
	if (rpd->ipaddr) {
		if (!conf_gw_ip_address) {
			log_warn("radius: gw-ip-address not specified, cann't assign IP address...\n");
			return -1;
		}
		*peer_addr = rpd->ipaddr;
		*addr = inet_addr(conf_gw_ip_address);
		return 0;
	}
	return -1;
}

static void ppp_starting(struct ppp_notified_t *n, struct ppp_t *ppp)
{
	struct radius_pd_t *pd = malloc(sizeof(*pd));

	memset(pd, 0, sizeof(*pd));
	pd->pd.key = n;
	pd->ppp = ppp;
	list_add_tail(&pd->pd.entry, &ppp->pd_list);
}

static void ppp_started(struct ppp_notified_t *n, struct ppp_t *ppp)
{
	struct radius_pd_t *rpd = find_pd(ppp);

	if (rad_acct_start(rpd))
		ppp_terminate(rpd->ppp, 0);
}
static void ppp_finishing(struct ppp_notified_t *n, struct ppp_t *ppp)
{
	struct radius_pd_t *rpd = find_pd(ppp);

	rad_acct_stop(rpd);
}
static void ppp_finished(struct ppp_notified_t *n, struct ppp_t *ppp)
{
	struct radius_pd_t *rpd = find_pd(ppp);

	list_del(&rpd->pd.entry);
	free(rpd);
}

struct radius_pd_t *find_pd(struct ppp_t *ppp)
{
	struct ppp_pd_t *pd;
	struct radius_pd_t *rpd;

	list_for_each_entry(pd, &ppp->pd_list, entry) {
		if (pd->key == &notified) {
			rpd = container_of(pd, typeof(*rpd), pd);
			return rpd;
		}
	}
	log_error("radius:BUG: rpd not found\n");
	abort();
}


static struct ipdb_t ipdb = {
	.get = get_ip,
};

static struct pwdb_t pwdb = {
	.check = check,
};

static struct ppp_notified_t notified = {
	.starting = ppp_starting,
	.started = ppp_started,
	.finishing = ppp_finishing,
	.finished = ppp_finished,
};

static int parse_server(const char *opt, char **name, 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;
	
	*name = str;
	if (p1) {
		*port = atoi(p1 + 1);
		if (*port <=0 )
			return -1;
	}
	*secret = p2 + 1;

	return 0;
}

static void __init radius_init(void)
{
	char *opt;

	opt = conf_get_opt("radius", "max-try");
	if (opt && atoi(opt) > 0)
		conf_max_try = atoi(opt);
	
	opt = conf_get_opt("radius", "timeout");
	if (opt && atoi(opt) > 0)
		conf_timeout = atoi(opt);

	opt = conf_get_opt("radius", "verbose");
	if (opt && atoi(opt) > 0)
		conf_verbose = 1;
	
	opt = conf_get_opt("radius", "nas-ip-address");
	if (opt)
		conf_nas_ip_address = opt;
	
	opt = conf_get_opt("radius", "gw-ip-address");
	if (opt)
		conf_gw_ip_address = opt;

	opt = conf_get_opt("radius", "auth_server");
	if (!opt) {
		log_error("radius: auth_server not specified\n");
		_exit(EXIT_FAILURE);
	} else if (parse_server(opt, &conf_auth_server, &conf_auth_server_port, &conf_auth_server_secret)) {
		log_error("radius: failed to parse auth_server\n");
		_exit(EXIT_FAILURE);
	}

	opt = conf_get_opt("radius", "acct_server");
	if (opt && parse_server(opt, &conf_acct_server, &conf_acct_server_port, &conf_acct_server_secret)) {
		log_error("radius: failed to parse acct_server\n");
		_exit(EXIT_FAILURE);
	}

	opt = conf_get_opt("radius", "dictionary");
	if (!opt) {
		fprintf(stderr, "radius: dictionary not specified\n");
		_exit(EXIT_FAILURE);
	}
	if (rad_dict_load(opt))
		_exit(EXIT_FAILURE);

	pwdb_register(&pwdb);
	ipdb_register(&ipdb);
	ppp_register_notified(&notified);
}