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
|
#include "log.h"
#include "ppp.h"
#include "ppp_auth.h"
#define MSG_FAILED "Authentication failed"
#define MSG_SUCCESSED "Authentication successed"
#define HDR_LEN (sizeof(struct pap_hdr_t)-2)
static int lcp_get_conf_req(struct auth_driver_t*, struct ppp_t*, struct lcp_opt32_t*);
static int lcp_recv_conf_req(struct auth_driver_t*, struct ppp_t*, struct lcp_opt32_t*);
static int begin(struct auth_driver_t*, struct ppp_t*);
static int terminate(struct auth_driver_t*, struct ppp_t*);
static void pap_recv(struct ppp_handler_t*h);
struct pap_proto_t
{
struct ppp_handler_t h;
struct ppp_t *ppp;
};
struct pap_hdr_t
{
uint16_t proto;
uint8_t code;
uint8_t id;
uint16_t len;
} __attribute__((packed));
struct pap_ack_t
{
struct pap_hdr_t hdr;
uint8_t msg_len;
char msg[0];
} __attribute__((packed));
static struct auth_driver_t pap=
{
.type=PPP_PAP,
.get_conf_req=lcp_get_conf_req,
.recv_conf_req=lcp_recv_conf_req,
.start=pap_start,
.finish=pap_finish,
};
int plugin_init(void)
{
if (auth_register(&pap))
{
log_error("pap: failed to register driver\n");
return -1;
}
return 0;
}
static int pap_start(struct auth_driver_t *d, struct ppp_t *ppp)
{
struct pap_proto_t *p=malloc(sizeof(*p));
memset(&p,0,sizeof(*p));
p->h.proto=PPP_PAP;
p->h.recv=pap_recv;
p->ppp=ppp;
ppp->auth_pd=p;
ppp_register_handler(p->ppp,p->h);
return 0;
}
static int pap_finish(struct auth_driver_t *d, struct ppp_t *ppp)
{
struct pap_proto_t *p=(struct pap_proto_t*)ppp->auth_pd;
ppp_unregister_handler(p->ppp,p->h);
free(p);
return 0;
}
static int lcp_get_conf_req(struct auth_driver_t *d, struct ppp_t *ppp, struct lcp_opt32_t *opt)
{
return 0;
}
static int lcp_recv_conf_req(struct auth_driver_t *d, struct ppp_t *ppp, struct lcp_opt32_t *opt)
{
return 0;
}
static void pap_send_ack(struct pap_proto_t *p, int id)
{
uint8_t buf[128];
struct pap_ack_t *msg=(struct pap_ack_t*)buf;
msg->hdr.proto=PPP_PAP;
msg->hdr.code=PAP_ACK;
msg->hdr.id=id;
msg->hdr.len=HDR_LEN+1+sizeof(MSG_SUCCESSED);
msg->len=sizeof(MSG_SUCCESSED);
memcpy(msg->msg,MSG_SUCCESSED,sizeof(MSG_SUCCESSED));
ppp_send(p->ppp,msg,msg->hdr.len+2);
}
static void pap_send_nack(struct pap_proto_t *p,int id)
{
uint8_t buf[128];
struct pap_ack_t *msg=(struct pap_ack_t*)buf;
msg->hdr.proto=PPP_PAP;
msg->hdr.code=PAP_NACK;
msg->hdr.id=id;
msg->hdr.len=HDR_LEN+1+sizeof(MSG_FAILED);
msg->len=sizeof(MSG_FAILED);
memcpy(msg->msg,MSG_FAILED,sizeof(MSG_FAILED));
ppp_send(p->ppp,msg,msg->hdr.len+2);
}
static int pap_recv_req(struct pap_proto_t *p,struct pap_hdr_t *hdr)
{
int ret;
char *peer_id;
char *passwd;
int peer_id_len;
int passwd_len;
uint8_t *ptr=(uint8_t*)(hdr+1);
peer_id_len=*(uint8_t*)ptr; ptr++;
if (peer_id_len>htons(hdr->len)-sizeof(*hdr)-1)
{
log_warn("PAP: short packet received\n");
return -1;
}
peer_id=ptr; ptr+=peer_id_len;
passwd_len=*(uint8_t*)ptr; ptr++;
if (passwd_len>htons(hdr->len)-sizeof(*hdr)-2-peer_id_len)
{
log_warn("PAP: short packet received\n");
return -1;
}
peer_id=stdndup(peer_id,peer_id_len);
passwd=stdndup(ptr,passwd_len);
if (pwdb_check(peer_id,passwd))
{
log_warn("PAP: authentication error\n");
pap_send_nack(p,hdr->id);
auth_failed(p->ppp);
ret=-1;
}else
{
pap_send_ack(p,hdr->id);
auth_successed(p->ppp);
ret=0;
}
free(peer_id);
free(passwd);
return ret;
}
static void pap_recv(struct ppp_handler_t *h)
{
struct pap_proto_t *p=container_of(h,typeof(*p),h);
struct pap_hdr_t *hdr=(struct pap_hdr_t *)p->ppp->in_buf;
if (p->ppp->in_buf_size<sizeof(*hdr) || htons(hdr->len)<HDR_LEN || htons(hdr->len)<p->ppp->in_buf_size-2)
{
log_warn("PAP: short packet received\n");
return;
}
if (hdr->code==PAP_REQ) pap_recv_req(p,hdr);
else
{
log_warn("PAP: unknown code received %x\n",hdr->code);
}
}
|