Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / vsftpd / readwrite.c
blob6ef34e198d774673e823722d3511b372b8338059
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * readwrite.c
7 * Routines to encapsulate the underlying read / write mechanism (OpenSSL vs.
8 * plain read()/write()).
9 */
11 #include "readwrite.h"
12 #include "session.h"
13 #include "netstr.h"
14 #include "ssl.h"
15 #include "privsock.h"
16 #include "defs.h"
17 #include "sysutil.h"
19 static int plain_peek_adapter(struct vsf_session* p_sess,
20 char* p_buf,
21 unsigned int len);
22 static int plain_read_adapter(struct vsf_session* p_sess,
23 char* p_buf,
24 unsigned int len);
25 static int ssl_peek_adapter(struct vsf_session* p_sess,
26 char* p_buf,
27 unsigned int len);
28 static int ssl_read_adapter(struct vsf_session* p_sess,
29 char* p_buf,
30 unsigned int len);
32 int
33 ftp_write_str(const struct vsf_session* p_sess, const struct mystr* p_str,
34 enum EVSFRWTarget target)
36 if (target == kVSFRWData)
38 if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
40 int ret = -1;
41 int written;
42 priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
43 priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
44 written = priv_sock_get_int(p_sess->ssl_consumer_fd);
45 if (written > 0 && written == (int) str_getlen(p_str))
47 ret = 0;
49 return ret;
51 else if (p_sess->data_use_ssl)
53 return ssl_write_str(p_sess->p_data_ssl, p_str);
55 else
57 return str_netfd_write(p_str, p_sess->data_fd);
60 else
62 if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
64 priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_WRITE_USER_RESP);
65 priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
66 return priv_sock_get_int(p_sess->ssl_consumer_fd);
68 else if (p_sess->control_use_ssl)
70 return ssl_write_str(p_sess->p_control_ssl, p_str);
72 else
74 return str_netfd_write(p_str, VSFTP_COMMAND_FD);
79 int
80 ftp_read_data(struct vsf_session* p_sess, char* p_buf, unsigned int len)
82 if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
84 int ret;
85 priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_READ);
86 ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
87 priv_sock_recv_buf(p_sess->ssl_consumer_fd, p_buf, len);
88 /* Need to do this here too because it is useless in the slave process. */
89 vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
90 return ret;
92 else if (p_sess->data_use_ssl)
94 return ssl_read(p_sess, p_sess->p_data_ssl, p_buf, len);
96 else
98 return vsf_sysutil_read(p_sess->data_fd, p_buf, len);
103 ftp_write_data(const struct vsf_session* p_sess, const char* p_buf,
104 unsigned int len)
106 if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
108 int ret;
109 priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
110 priv_sock_send_buf(p_sess->ssl_consumer_fd, p_buf, len);
111 ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
112 /* Need to do this here too because it is useless in the slave process. */
113 vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
114 return ret;
116 else if (p_sess->data_use_ssl)
118 return ssl_write(p_sess->p_data_ssl, p_buf, len);
120 else
122 return vsf_sysutil_write_loop(p_sess->data_fd, p_buf, len);
127 ftp_getline(struct vsf_session* p_sess, struct mystr* p_str, char* p_buf)
129 if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
131 int ret;
132 priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_GET_USER_CMD);
133 ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
134 if (ret >= 0)
136 priv_sock_get_str(p_sess->ssl_consumer_fd, p_str);
138 return ret;
140 else
142 str_netfd_read_t p_peek = plain_peek_adapter;
143 str_netfd_read_t p_read = plain_read_adapter;
144 if (p_sess->control_use_ssl)
146 p_peek = ssl_peek_adapter;
147 p_read = ssl_read_adapter;
149 return str_netfd_alloc(p_sess,
150 p_str,
151 '\n',
152 p_buf,
153 VSFTP_MAX_COMMAND_LINE,
154 p_peek,
155 p_read);
159 static int
160 plain_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
162 (void) p_sess;
163 return vsf_sysutil_recv_peek(VSFTP_COMMAND_FD, p_buf, len);
166 static int
167 plain_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
169 (void) p_sess;
170 return vsf_sysutil_read_loop(VSFTP_COMMAND_FD, p_buf, len);
173 static int
174 ssl_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
176 return ssl_peek(p_sess, p_sess->p_control_ssl, p_buf, len);
179 static int
180 ssl_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
182 return ssl_read(p_sess, p_sess->p_control_ssl, p_buf, len);