2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002-2004 Matt Johnston
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #define MAX_UNAUTH_PACKET_TYPE SSH_MSG_USERAUTH_PK_OK
40 static void recv_unimplemented();
42 /* process a decrypted packet, call the appropriate handler */
43 void process_packet() {
48 TRACE(("enter process_packet"))
50 type
= buf_getbyte(ses
.payload
);
51 TRACE(("process_packet: packet type = %d", type
))
53 ses
.lastpacket
= type
;
55 ses
.last_packet_time
= time(NULL
);
57 /* These packets we can receive at any time */
65 case SSH_MSG_UNIMPLEMENTED
:
67 TRACE(("SSH_MSG_UNIMPLEMENTED"))
68 dropbear_exit("Received SSH_MSG_UNIMPLEMENTED");
70 case SSH_MSG_DISCONNECT
:
72 dropbear_close("Disconnect received");
75 /* This applies for KEX, where the spec says the next packet MUST be
77 if (ses
.requirenext
!= 0) {
78 if (ses
.requirenext
!= type
) {
79 /* TODO send disconnect? */
80 dropbear_exit("Unexpected packet type %d, expected %d", type
,
83 /* Got what we expected */
88 /* Check if we should ignore this packet. Used currently only for
89 * KEX code, with first_kex_packet_follows */
91 TRACE(("Ignoring packet, type = %d", type
))
97 /* Kindly the protocol authors gave all the preauth packets type values
98 * less-than-or-equal-to 60 ( == MAX_UNAUTH_PACKET_TYPE ).
99 * NOTE: if the protocol changes and new types are added, revisit this
101 if ( !ses
.authstate
.authdone
&& type
> MAX_UNAUTH_PACKET_TYPE
) {
102 dropbear_exit("Received message %d before userauth", type
);
106 if (ses
.packettypes
[i
].type
== 0) {
111 if (ses
.packettypes
[i
].type
== type
) {
112 ses
.packettypes
[i
].handler();
118 /* TODO do something more here? */
119 TRACE(("preauth unknown packet"))
120 recv_unimplemented();
123 buf_free(ses
.payload
);
126 TRACE(("leave process_packet"))
131 /* This must be called directly after receiving the unimplemented packet.
132 * Isn't the most clean implementation, it relies on packet processing
133 * occurring directly after decryption (direct use of ses.recvseq).
134 * This is reasonably valid, since there is only a single decryption buffer */
135 static void recv_unimplemented() {
139 buf_putbyte(ses
.writepayload
, SSH_MSG_UNIMPLEMENTED
);
140 /* the decryption routine increments the sequence number, we must
142 buf_putint(ses
.writepayload
, ses
.recvseq
- 1);