xref: /netbsd-src/crypto/external/bsd/openssh/dist/dispatch.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*	$NetBSD: dispatch.c,v 1.6 2015/07/03 01:00:00 christos Exp $	*/
2 /* $OpenBSD: dispatch.c,v 1.27 2015/05/01 07:10:01 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.6 2015/07/03 01:00:00 christos Exp $");
29 #include <sys/types.h>
30 
31 #include <signal.h>
32 #include <stdarg.h>
33 
34 #include "ssh1.h"
35 #include "ssh2.h"
36 #include "log.h"
37 #include "dispatch.h"
38 #include "packet.h"
39 #include "compat.h"
40 #include "ssherr.h"
41 
42 int
43 dispatch_protocol_error(int type, u_int32_t seq, void *ctx)
44 {
45 	struct ssh *ssh = active_state; /* XXX */
46 	int r;
47 
48 	logit("dispatch_protocol_error: type %d seq %u", type, seq);
49 	if (!compat20)
50 		fatal("protocol error");
51 	if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
52 	    (r = sshpkt_put_u32(ssh, seq)) != 0 ||
53 	    (r = sshpkt_send(ssh)) != 0 ||
54 	    (r = ssh_packet_write_wait(ssh)) < 0)
55 		sshpkt_fatal(ssh, __func__, r);
56 	return 0;
57 }
58 
59 int
60 dispatch_protocol_ignore(int type, u_int32_t seq, void *ssh)
61 {
62 	logit("dispatch_protocol_ignore: type %d seq %u", type, seq);
63 	return 0;
64 }
65 
66 void
67 ssh_dispatch_init(struct ssh *ssh, dispatch_fn *dflt)
68 {
69 	u_int i;
70 	for (i = 0; i < DISPATCH_MAX; i++)
71 		ssh->dispatch[i] = dflt;
72 }
73 
74 void
75 ssh_dispatch_range(struct ssh *ssh, u_int from, u_int to, dispatch_fn *fn)
76 {
77 	u_int i;
78 
79 	for (i = from; i <= to; i++) {
80 		if (i >= DISPATCH_MAX)
81 			break;
82 		ssh->dispatch[i] = fn;
83 	}
84 }
85 
86 void
87 ssh_dispatch_set(struct ssh *ssh, int type, dispatch_fn *fn)
88 {
89 	ssh->dispatch[type] = fn;
90 }
91 
92 int
93 ssh_dispatch_run(struct ssh *ssh, int mode, volatile sig_atomic_t *done,
94     void *ctxt)
95 {
96 	int r;
97 	u_char type;
98 	u_int32_t seqnr;
99 
100 	for (;;) {
101 		if (mode == DISPATCH_BLOCK) {
102 			r = ssh_packet_read_seqnr(ssh, &type, &seqnr);
103 			if (r != 0)
104 				return r;
105 		} else {
106 			r = ssh_packet_read_poll_seqnr(ssh, &type, &seqnr);
107 			if (r != 0)
108 				return r;
109 			if (type == SSH_MSG_NONE)
110 				return 0;
111 		}
112 		if (type > 0 && type < DISPATCH_MAX &&
113 		    ssh->dispatch[type] != NULL) {
114 			if (ssh->dispatch_skip_packets) {
115 				debug2("skipped packet (type %u)", type);
116 				ssh->dispatch_skip_packets--;
117 				continue;
118 			}
119 			/* XXX 'ssh' will replace 'ctxt' later */
120 			r = (*ssh->dispatch[type])(type, seqnr, ctxt);
121 			if (r != 0)
122 				return r;
123 		} else {
124 			r = sshpkt_disconnect(ssh,
125 			    "protocol error: rcvd type %d", type);
126 			if (r != 0)
127 				return r;
128 			return SSH_ERR_DISCONNECTED;
129 		}
130 		if (done != NULL && *done)
131 			return 0;
132 	}
133 }
134 
135 void
136 ssh_dispatch_run_fatal(struct ssh *ssh, int mode, volatile sig_atomic_t *done,
137     void *ctxt)
138 {
139 	int r;
140 
141 	if ((r = ssh_dispatch_run(ssh, mode, done, ctxt)) != 0)
142 		sshpkt_fatal(ssh, __func__, r);
143 }
144