1 /* $OpenBSD: dispatch.c,v 1.22 2008/10/31 15:05:34 stevesk Exp $ */
2 /*
3  * Copyright © 2013
4  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
5  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 
30 #include <signal.h>
31 #include <stdarg.h>
32 
33 #include "ssh1.h"
34 #include "ssh2.h"
35 #include "log.h"
36 #include "dispatch.h"
37 #include "packet.h"
38 #include "compat.h"
39 
40 __RCSID("$MirOS: src/usr.bin/ssh/dispatch.c,v 1.7 2013/10/31 20:07:11 tg Exp $");
41 
42 #define DISPATCH_MAX	255
43 
44 dispatch_fn *dispatch[DISPATCH_MAX];
45 
46 void
dispatch_protocol_error(int type,u_int32_t seq,void * ctxt)47 dispatch_protocol_error(int type, u_int32_t seq,
48     void *ctxt __attribute__((__unused__)))
49 {
50 	logit("dispatch_protocol_error: type %d seq %u", type, seq);
51 	if (!compat20)
52 		fatal("protocol error");
53 	packet_start(SSH2_MSG_UNIMPLEMENTED);
54 	packet_put_int(seq);
55 	packet_send();
56 	packet_write_wait();
57 }
58 void
dispatch_protocol_ignore(int type,u_int32_t seq,void * ctxt)59 dispatch_protocol_ignore(int type, u_int32_t seq,
60     void *ctxt __attribute__((__unused__)))
61 {
62 	logit("dispatch_protocol_ignore: type %d seq %u", type, seq);
63 }
64 void
dispatch_init(dispatch_fn * dflt)65 dispatch_init(dispatch_fn *dflt)
66 {
67 	u_int i;
68 	for (i = 0; i < DISPATCH_MAX; i++)
69 		dispatch[i] = dflt;
70 }
71 void
dispatch_range(u_int from,u_int to,dispatch_fn * fn)72 dispatch_range(u_int from, u_int to, dispatch_fn *fn)
73 {
74 	u_int i;
75 
76 	for (i = from; i <= to; i++) {
77 		if (i >= DISPATCH_MAX)
78 			break;
79 		dispatch[i] = fn;
80 	}
81 }
82 void
dispatch_set(int type,dispatch_fn * fn)83 dispatch_set(int type, dispatch_fn *fn)
84 {
85 	dispatch[type] = fn;
86 }
87 void
dispatch_run(int mode,volatile sig_atomic_t * done,void * ctxt)88 dispatch_run(int mode, volatile sig_atomic_t *done, void *ctxt)
89 {
90 	for (;;) {
91 		int type;
92 		u_int32_t seqnr;
93 
94 		if (mode == DISPATCH_BLOCK) {
95 			type = packet_read_seqnr(&seqnr);
96 		} else {
97 			type = packet_read_poll_seqnr(&seqnr);
98 			if (type == SSH_MSG_NONE)
99 				return;
100 		}
101 		if (type > 0 && type < DISPATCH_MAX && dispatch[type] != NULL)
102 			(*dispatch[type])(type, seqnr, ctxt);
103 		else
104 			packet_disconnect("protocol error: rcvd type %d", type);
105 		if (done != NULL && *done)
106 			return;
107 	}
108 }
109