1 /*
2  * ProFTPD - mod_sftp packet IO
3  * Copyright (c) 2008-2020 TJ Saunders
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, TJ Saunders and other respective copyright holders
20  * give permission to link this program with OpenSSL, and distribute the
21  * resulting executable, without including the source code for OpenSSL in the
22  * source distribution.
23  */
24 
25 #ifndef MOD_SFTP_PACKET_H
26 #define MOD_SFTP_PACKET_H
27 
28 #include "mod_sftp.h"
29 
30 /* From RFC 4253, Section 6 */
31 struct ssh2_packet {
32   pool *pool;
33 
34   /* Length of the packet, not including mac or packet_len field itself. */
35   uint32_t packet_len;
36 
37   /* Length of the padding field. */
38   unsigned char padding_len;
39 
40   unsigned char *payload;
41   uint32_t payload_len;
42 
43   /* Must be at least 4 bytes of padding, with a maximum of 255 bytes. */
44   unsigned char *padding;
45 
46   /* Message Authentication Code. */
47   unsigned char *mac;
48   uint32_t mac_len;
49 
50   /* Packet sequence number. */
51   uint32_t seqno;
52 };
53 
54 #define SFTP_MIN_PADDING_LEN	4
55 #define SFTP_MAX_PADDING_LEN	255
56 
57 /* From the SFTP Draft, Section 4. */
58 struct sftp_packet {
59   uint32_t packet_len;
60   unsigned char packet_type;
61   uint32_t request_id;
62 };
63 
64 struct ssh2_packet *sftp_ssh2_packet_create(pool *);
65 char sftp_ssh2_packet_get_mesg_type(struct ssh2_packet *);
66 const char *sftp_ssh2_packet_get_mesg_type_desc(unsigned char);
67 
68 /* Returns a struct timeval populated with the time we last received an SSH2
69  * packet from the client.
70  */
71 int sftp_ssh2_packet_get_last_recvd(time_t *);
72 
73 /* Returns a struct timeval populated with the time we last sent an SSH2
74  * packet from the client.
75  */
76 int sftp_ssh2_packet_get_last_sent(time_t *);
77 
78 int sftp_ssh2_packet_read(int, struct ssh2_packet *);
79 int sftp_ssh2_packet_sock_read(int, void *, size_t, int);
80 
81 /* This sftp_ssh2_packet_sock_read() flag is used to tell the function to
82  * read in as many of the requested length of data as it can, but to NOT
83  * keep polling until that length has been acquired (i.e. to read the
84  * requested length pessimistically, assuming that it will not all appear).
85  */
86 #define SFTP_PACKET_READ_FL_PESSIMISTIC		0x001
87 
88 int sftp_ssh2_packet_send(int, struct ssh2_packet *);
89 
90 /* Wrapper function around sftp_ssh2_packet_send() which handles the sending
91  * of TAP messages and buffering of messages for network efficiency.
92  */
93 int sftp_ssh2_packet_write(int, struct ssh2_packet *);
94 
95 int sftp_ssh2_packet_handle(void);
96 
97 /* These specialized functions are for handling the additional message types
98  * defined in RFC 4253, Section 11, e.g. during KEX.
99  */
100 void sftp_ssh2_packet_handle_debug(struct ssh2_packet *);
101 void sftp_ssh2_packet_handle_disconnect(struct ssh2_packet *);
102 void sftp_ssh2_packet_handle_ext_info(struct ssh2_packet *);
103 void sftp_ssh2_packet_handle_ignore(struct ssh2_packet *);
104 void sftp_ssh2_packet_handle_unimplemented(struct ssh2_packet *);
105 
106 int sftp_ssh2_packet_rekey_reset(void);
107 int sftp_ssh2_packet_rekey_set_seqno(uint32_t);
108 int sftp_ssh2_packet_rekey_set_size(off_t);
109 
110 int sftp_ssh2_packet_send_version(void);
111 int sftp_ssh2_packet_set_poll_timeout(int);
112 int sftp_ssh2_packet_set_version(const char *);
113 
114 int sftp_ssh2_packet_set_client_alive(unsigned int, unsigned int);
115 
116 #endif /* MOD_SFTP_PACKET_H */
117