process-packet.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. ses.lastpacket = type;
  47. now = monotonic_now();
  48. ses.last_packet_time_keepalive_recv = now;
  49. /* These packets we can receive at any time */
  50. switch(type) {
  51. case SSH_MSG_IGNORE:
  52. goto out;
  53. case SSH_MSG_DEBUG:
  54. goto out;
  55. case SSH_MSG_UNIMPLEMENTED:
  56. /* debugging XXX */
  57. TRACE(("SSH_MSG_UNIMPLEMENTED"))
  58. goto out;
  59. case SSH_MSG_DISCONNECT:
  60. /* TODO cleanup? */
  61. dropbear_close("Disconnect received");
  62. }
  63. /* Ignore these packet types so that keepalives don't interfere with
  64. idle detection. This is slightly incorrect since a tcp forwarded
  65. global request with failure won't trigger the idle timeout,
  66. but that's probably acceptable */
  67. if (!(type == SSH_MSG_GLOBAL_REQUEST || type == SSH_MSG_REQUEST_FAILURE)) {
  68. ses.last_packet_time_idle = now;
  69. }
  70. /* This applies for KEX, where the spec says the next packet MUST be
  71. * NEWKEYS */
  72. if (ses.requirenext != 0) {
  73. if (ses.requirenext == type)
  74. {
  75. /* Got what we expected */
  76. TRACE(("got expected packet %d during kexinit", type))
  77. }
  78. else
  79. {
  80. /* RFC4253 7.1 - various messages are allowed at this point.
  81. The only ones we know about have already been handled though,
  82. so just return "unimplemented" */
  83. if (type >= 1 && type <= 49
  84. && type != SSH_MSG_SERVICE_REQUEST
  85. && type != SSH_MSG_SERVICE_ACCEPT
  86. && type != SSH_MSG_KEXINIT)
  87. {
  88. TRACE(("unknown allowed packet during kexinit"))
  89. recv_unimplemented();
  90. goto out;
  91. }
  92. else
  93. {
  94. TRACE(("disallowed packet during kexinit"))
  95. dropbear_exit("Unexpected packet type %d, expected %d", type,
  96. ses.requirenext);
  97. }
  98. }
  99. }
  100. /* Check if we should ignore this packet. Used currently only for
  101. * KEX code, with first_kex_packet_follows */
  102. if (ses.ignorenext) {
  103. TRACE(("Ignoring packet, type = %d", type))
  104. ses.ignorenext = 0;
  105. goto out;
  106. }
  107. /* Only clear the flag after we have checked ignorenext */
  108. if (ses.requirenext != 0 && ses.requirenext == type)
  109. {
  110. ses.requirenext = 0;
  111. }
  112. /* Kindly the protocol authors gave all the preauth packets type values
  113. * less-than-or-equal-to 60 ( == MAX_UNAUTH_PACKET_TYPE ).
  114. * NOTE: if the protocol changes and new types are added, revisit this
  115. * assumption */
  116. if ( !ses.authstate.authdone && type > MAX_UNAUTH_PACKET_TYPE ) {
  117. dropbear_exit("Received message %d before userauth", type);
  118. }
  119. for (i = 0; ; i++) {
  120. if (ses.packettypes[i].type == 0) {
  121. /* end of list */
  122. break;
  123. }
  124. if (ses.packettypes[i].type == type) {
  125. ses.packettypes[i].handler();
  126. goto out;
  127. }
  128. }
  129. /* TODO do something more here? */
  130. TRACE(("preauth unknown packet"))
  131. recv_unimplemented();
  132. out:
  133. buf_free(ses.payload);
  134. ses.payload = NULL;
  135. TRACE2(("leave process_packet"))
  136. }
  137. /* This must be called directly after receiving the unimplemented packet.
  138. * Isn't the most clean implementation, it relies on packet processing
  139. * occurring directly after decryption (direct use of ses.recvseq).
  140. * This is reasonably valid, since there is only a single decryption buffer */
  141. static void recv_unimplemented() {
  142. CHECKCLEARTOWRITE();
  143. buf_putbyte(ses.writepayload, SSH_MSG_UNIMPLEMENTED);
  144. /* the decryption routine increments the sequence number, we must
  145. * decrement */
  146. buf_putint(ses.writepayload, ses.recvseq - 1);
  147. encrypt_packet();
  148. }