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