process-packet.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002-2004 Matt Johnston
  5. * All rights reserved.
  6. *
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "packet.h"
  26. #include "session.h"
  27. #include "dbutil.h"
  28. #include "ssh.h"
  29. #include "algo.h"
  30. #include "buffer.h"
  31. #include "kex.h"
  32. #include "dbrandom.h"
  33. #include "service.h"
  34. #include "auth.h"
  35. #include "channel.h"
  36. #define MAX_UNAUTH_PACKET_TYPE SSH_MSG_USERAUTH_PK_OK
  37. static void recv_unimplemented(void);
  38. /* process a decrypted packet, call the appropriate handler */
  39. void process_packet() {
  40. unsigned char type;
  41. unsigned int i;
  42. time_t now;
  43. TRACE2(("enter process_packet"))
  44. type = buf_getbyte(ses.payload);
  45. TRACE(("process_packet: packet type = %d, len %d", type, ses.payload->len))
  46. now = monotonic_now();
  47. ses.last_packet_time_keepalive_recv = now;
  48. /* These packets we can receive at any time */
  49. switch(type) {
  50. case SSH_MSG_IGNORE:
  51. goto out;
  52. case SSH_MSG_DEBUG:
  53. goto out;
  54. case SSH_MSG_UNIMPLEMENTED:
  55. /* debugging XXX */
  56. TRACE(("SSH_MSG_UNIMPLEMENTED"))
  57. goto out;
  58. case SSH_MSG_DISCONNECT:
  59. /* TODO cleanup? */
  60. dropbear_close("Disconnect received");
  61. }
  62. /* Ignore these packet types so that keepalives don't interfere with
  63. idle detection. This is slightly incorrect since a tcp forwarded
  64. global request with failure won't trigger the idle timeout,
  65. but that's probably acceptable */
  66. if (!(type == SSH_MSG_GLOBAL_REQUEST
  67. || type == SSH_MSG_REQUEST_FAILURE
  68. || type == SSH_MSG_CHANNEL_FAILURE)) {
  69. ses.last_packet_time_idle = now;
  70. }
  71. /* This applies for KEX, where the spec says the next packet MUST be
  72. * NEWKEYS */
  73. if (ses.requirenext != 0) {
  74. if (ses.requirenext == type)
  75. {
  76. /* Got what we expected */
  77. TRACE(("got expected packet %d during kexinit", type))
  78. }
  79. else
  80. {
  81. /* RFC4253 7.1 - various messages are allowed at this point.
  82. The only ones we know about have already been handled though,
  83. so just return "unimplemented" */
  84. if (type >= 1 && type <= 49
  85. && type != SSH_MSG_SERVICE_REQUEST
  86. && type != SSH_MSG_SERVICE_ACCEPT
  87. && type != SSH_MSG_KEXINIT)
  88. {
  89. TRACE(("unknown allowed packet during kexinit"))
  90. recv_unimplemented();
  91. goto out;
  92. }
  93. else
  94. {
  95. TRACE(("disallowed packet during kexinit"))
  96. dropbear_exit("Unexpected packet type %d, expected %d", type,
  97. ses.requirenext);
  98. }
  99. }
  100. }
  101. /* Check if we should ignore this packet. Used currently only for
  102. * KEX code, with first_kex_packet_follows */
  103. if (ses.ignorenext) {
  104. TRACE(("Ignoring packet, type = %d", type))
  105. ses.ignorenext = 0;
  106. goto out;
  107. }
  108. /* Only clear the flag after we have checked ignorenext */
  109. if (ses.requirenext != 0 && ses.requirenext == type)
  110. {
  111. ses.requirenext = 0;
  112. }
  113. /* Kindly the protocol authors gave all the preauth packets type values
  114. * less-than-or-equal-to 60 ( == MAX_UNAUTH_PACKET_TYPE ).
  115. * NOTE: if the protocol changes and new types are added, revisit this
  116. * assumption */
  117. if ( !ses.authstate.authdone && type > MAX_UNAUTH_PACKET_TYPE ) {
  118. dropbear_exit("Received message %d before userauth", type);
  119. }
  120. for (i = 0; ; i++) {
  121. if (ses.packettypes[i].type == 0) {
  122. /* end of list */
  123. break;
  124. }
  125. if (ses.packettypes[i].type == type) {
  126. ses.packettypes[i].handler();
  127. goto out;
  128. }
  129. }
  130. /* TODO do something more here? */
  131. TRACE(("preauth unknown packet"))
  132. recv_unimplemented();
  133. out:
  134. ses.lastpacket = type;
  135. buf_free(ses.payload);
  136. ses.payload = NULL;
  137. TRACE2(("leave process_packet"))
  138. }
  139. /* This must be called directly after receiving the unimplemented packet.
  140. * Isn't the most clean implementation, it relies on packet processing
  141. * occurring directly after decryption (direct use of ses.recvseq).
  142. * This is reasonably valid, since there is only a single decryption buffer */
  143. static void recv_unimplemented() {
  144. CHECKCLEARTOWRITE();
  145. buf_putbyte(ses.writepayload, SSH_MSG_UNIMPLEMENTED);
  146. /* the decryption routine increments the sequence number, we must
  147. * decrement */
  148. buf_putint(ses.writepayload, ses.recvseq - 1);
  149. encrypt_packet();
  150. }