forked from lyndon160/vsftpd-backdoor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwrite.c
183 lines (173 loc) · 4.97 KB
/
readwrite.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
183
/*
* Part of Very Secure FTPd
* Licence: GPL v2
* Author: Chris Evans
* readwrite.c
*
* Routines to encapsulate the underlying read / write mechanism (OpenSSL vs.
* plain read()/write()).
*/
#include "readwrite.h"
#include "session.h"
#include "netstr.h"
#include "ssl.h"
#include "privsock.h"
#include "defs.h"
#include "sysutil.h"
static int plain_peek_adapter(struct vsf_session* p_sess,
char* p_buf,
unsigned int len);
static int plain_read_adapter(struct vsf_session* p_sess,
char* p_buf,
unsigned int len);
static int ssl_peek_adapter(struct vsf_session* p_sess,
char* p_buf,
unsigned int len);
static int ssl_read_adapter(struct vsf_session* p_sess,
char* p_buf,
unsigned int len);
int
ftp_write_str(const struct vsf_session* p_sess, const struct mystr* p_str,
enum EVSFRWTarget target)
{
if (target == kVSFRWData)
{
if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
{
int ret = -1;
int written;
priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
written = priv_sock_get_int(p_sess->ssl_consumer_fd);
if (written > 0 && written == (int) str_getlen(p_str))
{
ret = 0;
}
return ret;
}
else if (p_sess->data_use_ssl)
{
return ssl_write_str(p_sess->p_data_ssl, p_str);
}
else
{
return str_netfd_write(p_str, p_sess->data_fd);
}
}
else
{
if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
{
priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_WRITE_USER_RESP);
priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
return priv_sock_get_int(p_sess->ssl_consumer_fd);
}
else if (p_sess->control_use_ssl)
{
return ssl_write_str(p_sess->p_control_ssl, p_str);
}
else
{
return str_netfd_write(p_str, VSFTP_COMMAND_FD);
}
}
}
int
ftp_read_data(struct vsf_session* p_sess, char* p_buf, unsigned int len)
{
if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
{
int ret;
priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_READ);
ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
priv_sock_recv_buf(p_sess->ssl_consumer_fd, p_buf, len);
/* Need to do this here too because it is useless in the slave process. */
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
return ret;
}
else if (p_sess->data_use_ssl)
{
return ssl_read(p_sess, p_sess->p_data_ssl, p_buf, len);
}
else
{
return vsf_sysutil_read(p_sess->data_fd, p_buf, len);
}
}
int
ftp_write_data(const struct vsf_session* p_sess, const char* p_buf,
unsigned int len)
{
if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
{
int ret;
priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
priv_sock_send_buf(p_sess->ssl_consumer_fd, p_buf, len);
ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
/* Need to do this here too because it is useless in the slave process. */
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
return ret;
}
else if (p_sess->data_use_ssl)
{
return ssl_write(p_sess->p_data_ssl, p_buf, len);
}
else
{
return vsf_sysutil_write_loop(p_sess->data_fd, p_buf, len);
}
}
int
ftp_getline(struct vsf_session* p_sess, struct mystr* p_str, char* p_buf)
{
if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
{
int ret;
priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_GET_USER_CMD);
ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
if (ret >= 0)
{
priv_sock_get_str(p_sess->ssl_consumer_fd, p_str);
}
return ret;
}
else
{
str_netfd_read_t p_peek = plain_peek_adapter;
str_netfd_read_t p_read = plain_read_adapter;
if (p_sess->control_use_ssl)
{
p_peek = ssl_peek_adapter;
p_read = ssl_read_adapter;
}
return str_netfd_alloc(p_sess,
p_str,
'\n',
p_buf,
VSFTP_MAX_COMMAND_LINE,
p_peek,
p_read);
}
}
static int
plain_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
{
(void) p_sess;
return vsf_sysutil_recv_peek(VSFTP_COMMAND_FD, p_buf, len);
}
static int
plain_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
{
(void) p_sess;
return vsf_sysutil_read_loop(VSFTP_COMMAND_FD, p_buf, len);
}
static int
ssl_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
{
return ssl_peek(p_sess, p_sess->p_control_ssl, p_buf, len);
}
static int
ssl_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
{
return ssl_read(p_sess, p_sess->p_control_ssl, p_buf, len);
}