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
|
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pwdb.h"
#include "ipdb.h"
#include "ppp.h"
#include "events.h"
#include "triton.h"
#include "log.h"
#include "memdebug.h"
static const char *conf_chap_secrets = "/etc/ppp/chap-secrets";
static in_addr_t conf_gw_ip_address = 0;
static void *pd_key;
static struct ipdb_t ipdb;
struct cs_pd_t
{
struct ppp_pd_t pd;
struct ipdb_item_t ip;
char *passwd;
};
static char *skip_word(char *ptr)
{
for(; *ptr; ptr++)
if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
break;
return ptr;
}
static char *skip_space(char *ptr)
{
for(; *ptr; ptr++)
if (*ptr != ' ' && *ptr != '\t')
break;
return ptr;
}
static int split(char *buf, char **ptr)
{
int i;
for (i = 0; i < 3; i++) {
buf = skip_word(buf);
if (!*buf)
return i;
*buf = 0;
buf = skip_space(buf + 1);
if (!*buf)
return i;
ptr[i] = buf;
}
buf = skip_word(buf);
//if (*buf == '\n')
*buf = 0;
//else if (*buf)
// return -1;
return i;
}
static struct cs_pd_t *create_pd(struct ppp_t *ppp, const char *username)
{
FILE *f;
char *buf;
char *ptr[4];
int n;
struct cs_pd_t *pd;
if (!conf_chap_secrets)
return NULL;
f = fopen(conf_chap_secrets, "r");
if (!f) {
log_error("chap-secrets: open '%s': %s\n", conf_chap_secrets, strerror(errno));
return NULL;
}
buf = _malloc(4096);
if (!buf) {
log_emerg("chap-secrets: out of memory\n");
fclose(f);
return NULL;
}
while (fgets(buf, 4096, f)) {
n = split(buf, ptr);
if (n < 3)
continue;
if (!strcmp(buf, username))
goto found;
}
out:
fclose(f);
_free(buf);
return NULL;
found:
pd = _malloc(sizeof(*pd));
if (!pd) {
log_emerg("chap-secrets: out of memory\n");
goto out;
}
memset(pd, 0, sizeof(*pd));
pd->pd.key = &pd_key;
pd->passwd = _strdup(ptr[1]);
if (!pd->passwd) {
log_emerg("chap-secrets: out of memory\n");
_free(pd);
goto out;
}
pd->ip.addr = conf_gw_ip_address;
if (n == 3)
pd->ip.peer_addr = inet_addr(ptr[2]);
pd->ip.owner = &ipdb;
list_add_tail(&pd->pd.entry, &ppp->pd_list);
fclose(f);
_free(buf);
return pd;
}
static struct cs_pd_t *find_pd(struct ppp_t *ppp)
{
struct ppp_pd_t *pd;
list_for_each_entry(pd, &ppp->pd_list, entry) {
if (pd->key == &pd_key) {
return container_of(pd, typeof(struct cs_pd_t), pd);
}
}
return NULL;
}
static void ev_ppp_finished(struct ppp_t *ppp)
{
struct cs_pd_t *pd = find_pd(ppp);
if (!pd)
return;
list_del(&pd->pd.entry);
_free(pd->passwd);
_free(pd);
}
static struct ipdb_item_t *get_ip(struct ppp_t *ppp)
{
struct cs_pd_t *pd;
if (!conf_gw_ip_address)
return NULL;
pd = find_pd(ppp);
if (!pd)
return NULL;
if (!pd->ip.addr)
return NULL;
return &pd->ip;
}
static char* get_passwd(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username)
{
struct cs_pd_t *pd = find_pd(ppp);
if (!pd)
pd = create_pd(ppp, username);
if (!pd)
return NULL;
return _strdup(pd->passwd);
}
static struct ipdb_t ipdb = {
.get = get_ip,
};
static struct pwdb_t pwdb = {
.get_passwd = get_passwd,
};
static void __init init(void)
{
const char *opt;
opt = conf_get_opt("chap-secrets", "chap-secrets");
if (opt)
conf_chap_secrets = opt;
opt = conf_get_opt("chap-secrets", "gw-ip-address");
if (opt)
conf_gw_ip_address = inet_addr(opt);
pwdb_register(&pwdb);
ipdb_register(&ipdb);
triton_event_register_handler(EV_PPP_FINISHED, (triton_event_func)ev_ppp_finished);
}
|