ftpcmdio.c
上传用户:ig0539
上传日期:2022-05-21
资源大小:181k
文件大小:6k
- /*
- * Part of Very Secure FTPd
- * Licence: GPL v2
- * Author: Chris Evans
- * ftpcmdio.c
- *
- * Routines applicable to reading and writing the FTP command stream.
- */
- #include "ftpcmdio.h"
- #include "ftpcodes.h"
- #include "str.h"
- #include "netstr.h"
- #include "sysutil.h"
- #include "tunables.h"
- #include "defs.h"
- #include "secbuf.h"
- #include "utility.h"
- #include "logging.h"
- #include "session.h"
- #include "readwrite.h"
- /* Internal functions */
- static void control_getline(struct mystr* p_str, struct vsf_session* p_sess);
- static void ftp_write_text_common(struct vsf_session* p_sess, int status,
- const char* p_text, int noblock, char sep);
- static void ftp_write_str_common(struct vsf_session* p_sess, int status,
- char sep, const struct mystr* p_str,
- int noblock);
- static void handle_alarm_timeout(void* p_private);
- void
- vsf_cmdio_sock_setup(void)
- {
- vsf_sysutil_activate_keepalive(VSFTP_COMMAND_FD);
- vsf_sysutil_set_nodelay(VSFTP_COMMAND_FD);
- vsf_sysutil_activate_oobinline(VSFTP_COMMAND_FD);
- }
- static void
- handle_alarm_timeout(void* p_private)
- {
- struct vsf_session* p_sess = (struct vsf_session*) p_private;
- vsf_cmdio_write_exit(p_sess, FTP_IDLE_TIMEOUT, "Timeout.");
- }
- void
- vsf_cmdio_write(struct vsf_session* p_sess, int status, const char* p_text)
- {
- ftp_write_text_common(p_sess, status, p_text, 0, ' ');
- }
- void
- vsf_cmdio_write_hyphen(struct vsf_session* p_sess, int status,
- const char* p_text)
- {
- ftp_write_text_common(p_sess, status, p_text, 0, '-');
- }
- void
- vsf_cmdio_write_raw(struct vsf_session* p_sess, const char* p_text)
- {
- static struct mystr s_the_str;
- int retval;
- str_alloc_text(&s_the_str, p_text);
- if (tunable_log_ftp_protocol)
- {
- vsf_log_line(p_sess, kVSFLogEntryFTPOutput, &s_the_str);
- }
- retval = ftp_write_str(p_sess, &s_the_str, kVSFRWControl);
- if (retval != 0)
- {
- die("ftp_write_str");
- }
- }
- void
- vsf_cmdio_write_exit(struct vsf_session* p_sess, int status, const char* p_text)
- {
- /* Unblock any readers on the dying control channel. This is needed for SSL
- * connections, where the SSL control channel slave is in a separate
- * process.
- */
- vsf_sysutil_shutdown_read_failok(VSFTP_COMMAND_FD);
- ftp_write_text_common(p_sess, status, p_text, 1, ' ');
- vsf_sysutil_shutdown_failok(VSFTP_COMMAND_FD);
- vsf_sysutil_exit(0);
- }
- static void
- ftp_write_text_common(struct vsf_session* p_sess, int status,
- const char* p_text, int noblock, char sep)
- {
- /* XXX - could optimize */
- static struct mystr s_the_str;
- str_alloc_text(&s_the_str, p_text);
- ftp_write_str_common(p_sess, status, sep, &s_the_str, noblock);
- }
- void
- vsf_cmdio_write_str_hyphen(struct vsf_session* p_sess, int status,
- const struct mystr* p_str)
- {
- ftp_write_str_common(p_sess, status, '-', p_str, 0);
- }
- void
- vsf_cmdio_write_str(struct vsf_session* p_sess, int status,
- const struct mystr* p_str)
- {
- ftp_write_str_common(p_sess, status, ' ', p_str, 0);
- }
- static void
- ftp_write_str_common(struct vsf_session* p_sess, int status, char sep,
- const struct mystr* p_str, int noblock)
- {
- static struct mystr s_write_buf_str;
- static struct mystr s_text_mangle_str;
- int retval;
- if (tunable_log_ftp_protocol)
- {
- str_alloc_ulong(&s_write_buf_str, (unsigned long) status);
- str_append_char(&s_write_buf_str, sep);
- str_append_str(&s_write_buf_str, p_str);
- vsf_log_line(p_sess, kVSFLogEntryFTPOutput, &s_write_buf_str);
- }
- str_copy(&s_text_mangle_str, p_str);
- /* Process the output response according to the specifications.. */
- /* Escape telnet characters properly */
- str_replace_text(&s_text_mangle_str, "377", "377377");
- /* Change n for