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

#include "ppp.h"
#include "pwdb.h"
#include "radius.h"

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

static struct ppp_notified_t notified;

static int check_pap(struct radius_pd_t *rpd, const char *username, va_list args)
{
	struct rad_req_t *req;
	int i, r = PWDB_DENIED;
	//int id = va_arg(args, int);
	const char *passwd = va_arg(args, const char *);

	req = rad_req_alloc(rpd, CODE_ACCESS_REQUEST, username);
	if (!req)
		return PWDB_DENIED;

	if (rad_req_add_str(req, "User-Password", passwd, strlen(passwd)))
		goto out;

	for(i = 0; i < conf_max_try; i++) {
		if (rad_req_send(req))
			goto out;

		if (rad_req_wait(req, conf_timeout))
			goto out;

		if (req->reply)
			break;
	}

out:
	rad_req_free(req);

	return r;
}

static int check_chap_md5(struct radius_pd_t *rpd, const char *username, va_list args)
{
	/*int id = va_arg(args, int);
	const uint8_t *challenge = va_arg(args, const uint8_t *);*/
	return PWDB_DENIED;
}

static int check_mschap_v1(struct radius_pd_t *rpd, const char *username, va_list args)
{
	/*int id = va_arg(args, int);
	const uint8_t *challenge = va_arg(args, const uint8_t *);
	const uint8_t *lm_response = va_arg(args, const uint8_t *);
	const uint8_t *nt_response = va_arg(args, const uint8_t *);
	int flags = va_arg(args, int);*/
	return PWDB_DENIED;
}

static int check_mschap_v2(struct radius_pd_t *rpd, const char *username, va_list args)
{
	/*int id = va_arg(args, int);
	const uint8_t *challenge = va_arg(args, const uint8_t *);
	const uint8_t *peer_challenge = va_arg(args, const uint8_t *);
	const uint8_t *response = va_arg(args, const uint8_t *);
	int flags = va_arg(args, int);
	uint8_t *authenticator = va_arg(args, uint8_t *);*/
	return PWDB_DENIED;
}

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 ppp_pd_t *pd;
	struct radius_pd_t *rpd = NULL;

	list_for_each_entry(pd, &ppp->pd_list, entry) {
		if (pd->key == &notified) {
			rpd = container_of(pd, typeof(*rpd), pd);
			break;
		}
	}

	va_copy(args, _args);

	switch(type) {
		case PPP_PAP:
			r = check_pap(rpd, username, args);
			break;
		case PPP_CHAP:
			chap_type = va_arg(args, int);
			switch(chap_type) {
				case 0x05:
					r = check_chap_md5(rpd, username, args);
					break;
				case 0x80:
					r = check_mschap_v1(rpd, username, args);
					break;
				case 0x81:
					r = check_mschap_v2(rpd, username, args);
					break;
			}
			break;
	}

	va_end(args);

	return r;
}

static void ppp_started(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_finished(struct ppp_notified_t *n, 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);
			list_del(&pd->entry);
			free(rpd);
			return;
		}
	}
}

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

static struct ppp_notified_t notified = {
	.started = ppp_started,
	.finished = ppp_finished,
};

static void __init radius_init(void)
{
	char *dict = conf_get_opt("radius", "dictionary");
	if (!dict) {
		fprintf(stderr, "radius: dictionary not specified\n");
		_exit(EXIT_FAILURE);
	}
	if (!rad_dict_load(dict))
		_exit(EXIT_FAILURE);

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