1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include "log.h"
#include "packet.h"
#include "ssh.h"
#include "bug.h"
#include "packetparser.h"
#include "global.h"
static void cleanup(void) { global_purge(); }
__attribute__((noreturn)) static void die_fatal(const char *trouble,
const char *d, const char *fn) {
cleanup();
if (d) {
if (fn)
log_f5(trouble, " ", d, "/", fn);
else
log_f3(trouble, " ", d);
}
else { log_f1(trouble); }
_exit(111);
}
static int packet_disconnect_(struct buf *b) {
buf_purge(b);
buf_putnum8(b, SSH_MSG_DISCONNECT);
buf_putnum32(b, 0);
buf_putstring(b, "bye");
buf_putstring(b, "");
packet_put(b);
return packet_sendall();
}
#if 0
static int _packet_debug(struct buf *b) {
buf_purge(b);
buf_putnum8(b, SSH_MSG_DEBUG);
buf_putnum8(b, 1);
buf_putstring(b, "test message");
buf_putstring(b, "");
packet_put(b);
return packet_sendall();
}
#endif
static int packet_kex_receive_(void) {
struct buf *b = &packet.kexrecv;
long long pos = 0;
crypto_uint8 ch;
crypto_uint32 len;
if (!packet_getall(b, SSH_MSG_KEXINIT)) return 0;
pos = packetparser_uint8(b->buf, b->len, pos, &ch);
if (ch != SSH_MSG_KEXINIT) bug_proto();
pos = packetparser_skip(b->buf, b->len, pos, 16);
pos = packetparser_uint32(b->buf, b->len, pos, &len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("kex algorithms: ", (char *) b->buf + pos - len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("server host key algorithms: ", (char *) b->buf + pos - len);
pos = packetparser_uint32(
b->buf, b->len, pos, &len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("encryption algorithms client to server: ",
(char *) b->buf + pos - len);
pos = packetparser_uint32(
b->buf, b->len, pos, &len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("encryption algorithms server to client: ",
(char *) b->buf + pos - len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("mac algorithms client to server: ", (char *) b->buf + pos - len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
log_i2("mac algorithms server to client: ", (char *) b->buf + pos - len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
pos = packetparser_uint32(b->buf, b->len, pos,
&len);
pos = packetparser_skip(b->buf, b->len, pos, len);
pos = packetparser_uint8(b->buf, b->len, pos,
&ch);
pos = packetparser_uint32(b->buf, b->len, pos, &len);
return 1;
}
static unsigned char bspace[1024];
static struct buf b;
int main(int argc, char **argv) {
pid_t pid;
int tochild[2] = {-1, -1};
int fromchild[2] = {-1, -1};
if (argc < 2) _exit(111);
if (!argv[0]) _exit(111);
if (!argv[1]) _exit(111);
++argv;
buf_init(&b, bspace, sizeof bspace);
if (pipe(tochild) == -1) _exit(111);
if (pipe(fromchild) == -1) _exit(111);
pid = fork();
if (pid == -1) _exit(111);
if (pid == 0) {
close(tochild[1]);
close(fromchild[0]);
if (dup2(tochild[0], 0) == -1) _exit(111);
if (dup2(fromchild[1], 1) == -1) _exit(111);
execvp(*argv, argv);
_exit(111);
}
close(tochild[0]);
close(fromchild[1]);
close(0);
if (dup2(fromchild[0], 0) == -1) _exit(111);
close(1);
if (dup2(tochild[1], 1) == -1) _exit(111);
signal(SIGPIPE, SIG_IGN);
global_init();
log_init(0, "_tinysshd-test-kex1", 0, 0);
if (!packet_hello_receive())
die_fatal("unable to receive hello-string", 0, 0);
if (!packet_hello_send()) die_fatal("unable to send hello-string", 0, 0);
if (!packet_kex_receive_())
die_fatal("unable to receive kex-message", 0, 0);
if (!packet_disconnect_(&b))
die_fatal("unable to send disconnect-message", 0, 0);
_exit(111);
}
|