2 * Part of Very Secure FTPd
7 * Routines to encapsulate the underlying read / write mechanism (OpenSSL vs.
8 * plain read()/write()).
11 #include "readwrite.h"
19 static int plain_peek_adapter(struct vsf_session
* p_sess
,
22 static int plain_read_adapter(struct vsf_session
* p_sess
,
25 static int ssl_peek_adapter(struct vsf_session
* p_sess
,
28 static int ssl_read_adapter(struct vsf_session
* p_sess
,
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
)
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
))
51 else if (p_sess
->data_use_ssl
)
53 return ssl_write_str(p_sess
->p_data_ssl
, p_str
);
57 return str_netfd_write(p_str
, p_sess
->data_fd
);
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
);
74 return str_netfd_write(p_str
, VSFTP_COMMAND_FD
);
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
)
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
);
92 else if (p_sess
->data_use_ssl
)
94 return ssl_read(p_sess
, p_sess
->p_data_ssl
, p_buf
, len
);
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
,
106 if (p_sess
->data_use_ssl
&& p_sess
->ssl_slave_active
)
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
);
116 else if (p_sess
->data_use_ssl
)
118 return ssl_write(p_sess
->p_data_ssl
, p_buf
, len
);
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
)
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
);
136 priv_sock_get_str(p_sess
->ssl_consumer_fd
, p_str
);
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
,
153 VSFTP_MAX_COMMAND_LINE
,
160 plain_peek_adapter(struct vsf_session
* p_sess
, char* p_buf
, unsigned int len
)
163 return vsf_sysutil_recv_peek(VSFTP_COMMAND_FD
, p_buf
, len
);
167 plain_read_adapter(struct vsf_session
* p_sess
, char* p_buf
, unsigned int len
)
170 return vsf_sysutil_read_loop(VSFTP_COMMAND_FD
, p_buf
, len
);
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
);
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
);