-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocks5_username_password_authenticator.c
182 lines (157 loc) · 6.13 KB
/
socks5_username_password_authenticator.c
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include "logging.h"
#include "socks5_protocol.h"
#include "socks5_username_password_authenticator.h"
struct username_password_authenticator_inner {
struct identity *identities;
size_t size;
};
static int authenticate_username_password(struct username_password_authenticator_inner *inner,
const char *username, const char *password);
static int username_password_authenticate(struct authenticator *authenticator);
static int username_password_poll_input(struct authenticator *authenticator);
static int username_password_flush_output(struct authenticator *authenticator);
static void username_password_inner_free(void *inner);
struct authenticator *username_password_authenticator_new(struct bufferevent *underlying_bev,
struct identity *identities, size_t n)
{
struct username_password_authenticator_inner *inner = NULL;
inner = malloc(sizeof(struct username_password_authenticator_inner));
if (inner == NULL) {
error("malloc() for struct username_password_authenticator_inner failed: %s (errno=%d)",
strerror(errno), errno);
return NULL;
}
inner->identities = calloc(n, sizeof(struct identity));
if (inner == NULL) {
error("calloc() for struct identity failed: %s (errno=%d)", strerror(errno), errno);
free(inner);
return NULL;
}
size_t i;
for (i = 0; i < n; i++) {
strcpy(inner->identities[i].username, identities[i].username);
strcpy(inner->identities[i].password, identities[i].password);
}
inner->size = n;
struct authenticator_imp imp = {
username_password_authenticate,
username_password_poll_input,
username_password_flush_output,
username_password_inner_free,
};
return authenticator_new(SOCKS5_AUTH_METHOD_USERNAME_PASSWORD, imp, inner, underlying_bev);
}
static int authenticate_username_password(struct username_password_authenticator_inner *inner,
const char *username, const char *password)
{
size_t i;
for (i = 0; i < inner->size; i++) {
if (strcmp(inner->identities[i].username, username) == 0
&& strcmp(inner->identities[i].password, password) == 0) {
return YM_AUTH_SUCCESS;
}
}
return YM_AUTH_ERROR;
}
#define SOCKS5_USERNAME_PASSWORD_AUTH_V1 1
struct s5_auth_username_password_request {
uint8_t version;
uint8_t username_len;
char username[256];
uint8_t password_len;
char password[256];
};
#define SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_SUCCESS 0
#define SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_FAILURE 1
struct s5_auth_username_password_reply {
uint8_t version;
uint8_t status;
};
static int evbuffer_read_s5_auth_username_password_request(struct evbuffer *buffer,
struct s5_auth_username_password_request *req)
{
size_t buflen = evbuffer_get_length(buffer);
if (buflen < 2)
return YM_NEED_MORE_DATA;
uint8_t *ptr = evbuffer_pullup(buffer, 2);
uint8_t ver = ptr[0];
uint8_t username_len = ptr[1];
if (buflen < 3 + username_len)
return YM_NEED_MORE_DATA;
ptr = evbuffer_pullup(buffer, 3 + username_len);
uint8_t password_len = ptr[2 + username_len];
uint8_t req_len = 3 + username_len + password_len;
if (buflen < req_len)
return YM_NEED_MORE_DATA;
ptr = evbuffer_pullup(buffer, req_len);
req->version = ver;
req->username_len = username_len;
strncpy(req->username, (char *)ptr + 2, username_len);
req->username[username_len] = '\0';
req->password_len = password_len;
strncpy(req->password, (char *)ptr + 3 + username_len, password_len);
req->password[password_len] = '\0';
evbuffer_drain(buffer, req_len);
return YM_SUCCESS;
}
static int evbuffer_write_s5_auth_username_password_reply(struct evbuffer *buffer,
struct s5_auth_username_password_reply *res)
{
if (evbuffer_add(buffer, &res->version, sizeof(res->version)) != 0)
return YM_ERROR;
if (evbuffer_add(buffer, &res->status, sizeof(res->status)) != 0)
return YM_ERROR;
debug("evbuffer_write_s5_auth_username_password_reply: YM_SUCCESS");
return YM_SUCCESS;
}
static int username_password_authenticate(struct authenticator *authenticator)
{
debug("username_password_authenticate");
struct s5_auth_username_password_request req;
struct s5_auth_username_password_reply res;
res.version = SOCKS5_USERNAME_PASSWORD_AUTH_V1;
/* FIXME: ugly code... */
struct evbuffer *input = authenticator_get_input_buffer(authenticator);
int n = evbuffer_read_s5_auth_username_password_request(input, &req);
if (n == YM_NEED_MORE_DATA)
return YM_AUTH_PENDING;
else if (n == YM_ERROR) {
error("evbuffer_read_s5_auth_negotiation_request() failed:%s (errno=%d)",
strerror(errno), errno);
res.status = SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_FAILURE;
} else { /* YM_SUCCESS */
n = authenticate_username_password(authenticator->inner, req.username, req.password);
if (n == YM_ERROR)
res.status = SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_FAILURE;
else
res.status = SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_SUCCESS;
}
struct evbuffer *output = authenticator_get_output_buffer(authenticator);
if (evbuffer_write_s5_auth_username_password_reply(output, &res) == YM_ERROR)
return YM_ERROR;
if (authenticator_flush(authenticator) == YM_ERROR)
return YM_ERROR;
if (res.status == SOCKS5_USERNAME_PASSWORD_AUTH_STATUS_SUCCESS)
return YM_SUCCESS;
else
return YM_ERROR;
}
static int username_password_poll_input(struct authenticator *authenticator)
{
return bufferevent_read_buffer(authenticator->underlying_bev, authenticator->input);
}
static int username_password_flush_output(struct authenticator *authenticator)
{
return bufferevent_write_buffer(authenticator->underlying_bev, authenticator->output);
}
static void username_password_inner_free(void *inner)
{
struct username_password_authenticator_inner *realinner = inner;
free(realinner->identities);
free(realinner);
}