dispatch.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* $OpenBSD: dispatch.c,v 1.32 2019/01/19 21:33:13 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <signal.h>
  28. #include <stdarg.h>
  29. #include "ssh2.h"
  30. #include "log.h"
  31. #include "dispatch.h"
  32. #include "packet.h"
  33. #include "compat.h"
  34. #include "ssherr.h"
  35. int
  36. dispatch_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
  37. {
  38. int r;
  39. logit("dispatch_protocol_error: type %d seq %u", type, seq);
  40. if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
  41. (r = sshpkt_put_u32(ssh, seq)) != 0 ||
  42. (r = sshpkt_send(ssh)) != 0 ||
  43. (r = ssh_packet_write_wait(ssh)) != 0)
  44. sshpkt_fatal(ssh, r, "%s", __func__);
  45. return 0;
  46. }
  47. int
  48. dispatch_protocol_ignore(int type, u_int32_t seq, struct ssh *ssh)
  49. {
  50. logit("dispatch_protocol_ignore: type %d seq %u", type, seq);
  51. return 0;
  52. }
  53. void
  54. ssh_dispatch_init(struct ssh *ssh, dispatch_fn *dflt)
  55. {
  56. u_int i;
  57. for (i = 0; i < DISPATCH_MAX; i++)
  58. ssh->dispatch[i] = dflt;
  59. }
  60. void
  61. ssh_dispatch_range(struct ssh *ssh, u_int from, u_int to, dispatch_fn *fn)
  62. {
  63. u_int i;
  64. for (i = from; i <= to; i++) {
  65. if (i >= DISPATCH_MAX)
  66. break;
  67. ssh->dispatch[i] = fn;
  68. }
  69. }
  70. void
  71. ssh_dispatch_set(struct ssh *ssh, int type, dispatch_fn *fn)
  72. {
  73. ssh->dispatch[type] = fn;
  74. }
  75. int
  76. ssh_dispatch_run(struct ssh *ssh, int mode, volatile sig_atomic_t *done)
  77. {
  78. int r;
  79. u_char type;
  80. u_int32_t seqnr;
  81. for (;;) {
  82. if (mode == DISPATCH_BLOCK) {
  83. r = ssh_packet_read_seqnr(ssh, &type, &seqnr);
  84. if (r != 0)
  85. return r;
  86. } else {
  87. r = ssh_packet_read_poll_seqnr(ssh, &type, &seqnr);
  88. if (r != 0)
  89. return r;
  90. if (type == SSH_MSG_NONE)
  91. return 0;
  92. }
  93. if (type > 0 && type < DISPATCH_MAX &&
  94. ssh->dispatch[type] != NULL) {
  95. if (ssh->dispatch_skip_packets) {
  96. debug2("skipped packet (type %u)", type);
  97. ssh->dispatch_skip_packets--;
  98. continue;
  99. }
  100. r = (*ssh->dispatch[type])(type, seqnr, ssh);
  101. if (r != 0)
  102. return r;
  103. } else {
  104. r = sshpkt_disconnect(ssh,
  105. "protocol error: rcvd type %d", type);
  106. if (r != 0)
  107. return r;
  108. return SSH_ERR_DISCONNECTED;
  109. }
  110. if (done != NULL && *done)
  111. return 0;
  112. }
  113. }
  114. void
  115. ssh_dispatch_run_fatal(struct ssh *ssh, int mode, volatile sig_atomic_t *done)
  116. {
  117. int r;
  118. if ((r = ssh_dispatch_run(ssh, mode, done)) != 0)
  119. sshpkt_fatal(ssh, r, "%s", __func__);
  120. }