]> asedeno.scripts.mit.edu Git - PuTTY_svn.git/blob - contrib/logparse.pl
Make logparse handle completely bogus channel numbers.
[PuTTY_svn.git] / contrib / logparse.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use FileHandle;
6
7 my $dumpchannels = 0;
8 my $dumpdata = 0;
9 while ($ARGV[0] =~ /^-/) {
10     my $opt = shift @ARGV;
11     if ($opt eq "--") {
12         last; # stop processing options
13     } elsif ($opt eq "-c") {
14         $dumpchannels = 1;
15     } elsif ($opt eq "-d") {
16         $dumpdata = 1;
17     } else {
18         die "unrecognised option '$opt'\n";
19     }
20 }
21
22 my @channels = (); # ultimate channel ids are indices in this array
23 my %chan_by_id = (); # indexed by 'c%d' or 's%d' for client and server ids
24 my %globalreq = (); # indexed by 'i' or 'o'
25
26 my %packets = (
27 #define SSH2_MSG_DISCONNECT                       1     /* 0x1 */
28     'SSH2_MSG_DISCONNECT' => sub {
29         my ($direction, $seq, $data) = @_;
30         my ($reason, $description, $lang) = &parse("uss", $data);
31         printf "%s\n", &str($description);
32     },
33 #define SSH2_MSG_IGNORE                           2     /* 0x2 */
34     'SSH2_MSG_IGNORE' => sub {
35         my ($direction, $seq, $data) = @_;
36         my ($str) = &parse("s", $data);
37         printf "(%d bytes)\n", length $str;
38     },
39 #define SSH2_MSG_UNIMPLEMENTED                    3     /* 0x3 */
40     'SSH2_MSG_UNIMPLEMENTED' => sub {
41         my ($direction, $seq, $data) = @_;
42         my ($rseq) = &parse("u", $data);
43         printf "i%d\n", $rseq;
44     },
45 #define SSH2_MSG_DEBUG                            4     /* 0x4 */
46     'SSH2_MSG_DEBUG' => sub {
47         my ($direction, $seq, $data) = @_;
48         my ($disp, $message, $lang) = &parse("bss", $data);
49         printf "%s\n", &str($message);
50     },
51 #define SSH2_MSG_SERVICE_REQUEST                  5     /* 0x5 */
52     'SSH2_MSG_SERVICE_REQUEST' => sub {
53         my ($direction, $seq, $data) = @_;
54         my ($service) = &parse("s", $data);
55         printf "%s\n", &str($service);
56     },
57 #define SSH2_MSG_SERVICE_ACCEPT                   6     /* 0x6 */
58     'SSH2_MSG_SERVICE_ACCEPT' => sub {
59         my ($direction, $seq, $data) = @_;
60         my ($service) = &parse("s", $data);
61         printf "%s\n", &str($service);
62     },
63 #define SSH2_MSG_KEXINIT                          20    /* 0x14 */
64     'SSH2_MSG_KEXINIT' => sub {
65         my ($direction, $seq, $data) = @_;
66         print "\n";
67     },
68 #define SSH2_MSG_NEWKEYS                          21    /* 0x15 */
69     'SSH2_MSG_NEWKEYS' => sub {
70         my ($direction, $seq, $data) = @_;
71         print "\n";
72     },
73 #define SSH2_MSG_KEXDH_INIT                       30    /* 0x1e */
74     'SSH2_MSG_KEXDH_INIT' => sub {
75         my ($direction, $seq, $data) = @_;
76         print "\n";
77     },
78 #define SSH2_MSG_KEXDH_REPLY                      31    /* 0x1f */
79     'SSH2_MSG_KEXDH_REPLY' => sub {
80         my ($direction, $seq, $data) = @_;
81         print "\n";
82     },
83 #define SSH2_MSG_KEX_DH_GEX_REQUEST               30    /* 0x1e */
84     'SSH2_MSG_KEX_DH_GEX_REQUEST' => sub {
85         my ($direction, $seq, $data) = @_;
86         print "\n";
87     },
88 #define SSH2_MSG_KEX_DH_GEX_GROUP                 31    /* 0x1f */
89     'SSH2_MSG_KEX_DH_GEX_GROUP' => sub {
90         my ($direction, $seq, $data) = @_;
91         print "\n";
92     },
93 #define SSH2_MSG_KEX_DH_GEX_INIT                  32    /* 0x20 */
94     'SSH2_MSG_KEX_DH_GEX_INIT' => sub {
95         my ($direction, $seq, $data) = @_;
96         print "\n";
97     },
98 #define SSH2_MSG_KEX_DH_GEX_REPLY                 33    /* 0x21 */
99     'SSH2_MSG_KEX_DH_GEX_REPLY' => sub {
100         my ($direction, $seq, $data) = @_;
101         print "\n";
102     },
103 #define SSH2_MSG_KEXRSA_PUBKEY                    30    /* 0x1e */
104     'SSH2_MSG_KEXRSA_PUBKEY' => sub {
105         my ($direction, $seq, $data) = @_;
106         print "\n";
107     },
108 #define SSH2_MSG_KEXRSA_SECRET                    31    /* 0x1f */
109     'SSH2_MSG_KEXRSA_SECRET' => sub {
110         my ($direction, $seq, $data) = @_;
111         print "\n";
112     },
113 #define SSH2_MSG_KEXRSA_DONE                      32    /* 0x20 */
114     'SSH2_MSG_KEXRSA_DONE' => sub {
115         my ($direction, $seq, $data) = @_;
116         print "\n";
117     },
118 #define SSH2_MSG_USERAUTH_REQUEST                 50    /* 0x32 */
119     'SSH2_MSG_USERAUTH_REQUEST' => sub {
120         my ($direction, $seq, $data) = @_;
121         my ($user, $service, $method) = &parse("sss", $data);
122         my $out = sprintf "%s %s %s",
123             &str($user), &str($service), &str($method);
124         if ($method eq "publickey") {
125             my ($real) = &parse("b", $data);
126             $out .= " real=$real";
127         } elsif ($method eq "password") {
128             my ($change) = &parse("b", $data);
129             $out .= " change=$change";
130         }
131         print "$out\n";
132     },
133 #define SSH2_MSG_USERAUTH_FAILURE                 51    /* 0x33 */
134     'SSH2_MSG_USERAUTH_FAILURE' => sub {
135         my ($direction, $seq, $data) = @_;
136         my ($options) = &parse("s", $data);
137         printf "%s\n", &str($options);
138     },
139 #define SSH2_MSG_USERAUTH_SUCCESS                 52    /* 0x34 */
140     'SSH2_MSG_USERAUTH_SUCCESS' => sub {
141         my ($direction, $seq, $data) = @_;
142         print "\n";
143     },
144 #define SSH2_MSG_USERAUTH_BANNER                  53    /* 0x35 */
145     'SSH2_MSG_USERAUTH_BANNER' => sub {
146         my ($direction, $seq, $data) = @_;
147         print "\n";
148     },
149 #define SSH2_MSG_USERAUTH_PK_OK                   60    /* 0x3c */
150     'SSH2_MSG_USERAUTH_PK_OK' => sub {
151         my ($direction, $seq, $data) = @_;
152         print "\n";
153     },
154 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ        60    /* 0x3c */
155     'SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ' => sub {
156         my ($direction, $seq, $data) = @_;
157         print "\n";
158     },
159 #define SSH2_MSG_USERAUTH_INFO_REQUEST            60    /* 0x3c */
160     'SSH2_MSG_USERAUTH_INFO_REQUEST' => sub {
161         my ($direction, $seq, $data) = @_;
162         print "\n";
163     },
164 #define SSH2_MSG_USERAUTH_INFO_RESPONSE           61    /* 0x3d */
165     'SSH2_MSG_USERAUTH_INFO_RESPONSE' => sub {
166         my ($direction, $seq, $data) = @_;
167         print "\n";
168     },
169 #define SSH2_MSG_GLOBAL_REQUEST                   80    /* 0x50 */
170     'SSH2_MSG_GLOBAL_REQUEST' => sub {
171         my ($direction, $seq, $data) = @_;
172         my ($type, $wantreply) = &parse("sb", $data);
173         printf "%s (%s)", $type, $wantreply eq "yes" ? "reply" : "noreply";
174         my $request = [$seq, $type];
175         push @{$globalreq{$direction}}, $request if $wantreply eq "yes";
176         if ($type eq "tcpip-forward" or $type eq "cancel-tcpip-forward") {
177             my ($addr, $port) = &parse("su", $data);
178             printf " %s:%s", $addr, $port;
179             push @$request, $port;
180         }
181         print "\n";
182     },
183 #define SSH2_MSG_REQUEST_SUCCESS                  81    /* 0x51 */
184     'SSH2_MSG_REQUEST_SUCCESS' => sub {
185         my ($direction, $seq, $data) = @_;
186         my $otherdir = ($direction eq "i" ? "o" : "i");
187         my $request = shift @{$globalreq{$otherdir}};
188         if (defined $request) {
189             printf "to %s", $request->[0];
190             if ($request->[1] eq "tcpip-forward" and $request->[2] == 0) {
191                 my ($port) = &parse("u", $data);
192                 printf " port=%s", $port;
193             }
194         } else {
195             print "(spurious?)";
196         }
197         print "\n";
198     },
199 #define SSH2_MSG_REQUEST_FAILURE                  82    /* 0x52 */
200     'SSH2_MSG_REQUEST_FAILURE' => sub {
201         my ($direction, $seq, $data) = @_;
202         my $otherdir = ($direction eq "i" ? "o" : "i");
203         my $request = shift @{$globalreq{$otherdir}};
204         if (defined $request) {
205             printf "to %s", $request->[0];
206         } else {
207             print "(spurious?)";
208         }
209         print "\n";
210     },
211 #define SSH2_MSG_CHANNEL_OPEN                     90    /* 0x5a */
212     'SSH2_MSG_CHANNEL_OPEN' => sub {
213         my ($direction, $seq, $data) = @_;
214         my ($type, $sid, $winsize, $packet) = &parse("suuu", $data);
215         # CHANNEL_OPEN tells the other side the _sender's_ id for the
216         # channel, so this choice between "s" and "c" prefixes is
217         # opposite to every other message in the protocol, which all
218         # quote the _recipient's_ id of the channel.
219         $sid = ($direction eq "i" ? "s" : "c") . $sid;
220         my $chan = {'id'=>$sid, 'state'=>'halfopen',
221                     'i'=>{'win'=>0, 'seq'=>0},
222                     'o'=>{'win'=>0, 'seq'=>0}};
223         $chan->{$direction}{'win'} = $winsize;
224         push @channels, $chan;
225         my $index = $#channels;
226         $chan_by_id{$sid} = $index;
227         printf "ch%d (%s) %s (--%d)", $index, $chan->{'id'}, $type,
228             $chan->{$direction}{'win'};
229         if ($type eq "x11") {
230             my ($addr, $port) = &parse("su", $data);
231             printf " from %s:%s", $addr, $port;
232         } elsif ($type eq "forwarded-tcpip") {
233             my ($saddr, $sport, $paddr, $pport) = &parse("susu", $data);
234             printf " to %s:%s from %s:%s", $saddr, $sport, $paddr, $pport;
235         } elsif ($type eq "direct-tcpip") {
236             my ($daddr, $dport, $saddr, $sport) = &parse("susu", $data);
237             printf " to %s:%s from %s:%s", $daddr, $dport, $saddr, $sport;
238         }
239         print "\n";
240     },
241 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION        91    /* 0x5b */
242     'SSH2_MSG_CHANNEL_OPEN_CONFIRMATION' => sub {
243         my ($direction, $seq, $data) = @_;
244         my ($rid, $sid, $winsize, $packet) = &parse("uuuu", $data);
245         $rid = ($direction eq "i" ? "c" : "s") . $rid;
246         my $index = $chan_by_id{$rid};
247         if (!defined $index) {
248             printf "UNKNOWN_CHANNEL (%s) (--%d)\n", $rid, $winsize;
249             return;
250         }
251         $sid = ($direction eq "i" ? "s" : "c") . $sid;
252         $chan_by_id{$sid} = $index;
253         my $chan = $channels[$index];
254         $chan->{'id'} = ($direction eq "i" ? "$rid/$sid" : "$sid/$rid");
255         $chan->{'state'} = 'open';
256         $chan->{$direction}{'win'} = $winsize;
257         printf "ch%d (%s) (--%d)\n", $index, $chan->{'id'},
258             $chan->{$direction}{'win'};
259     },
260 #define SSH2_MSG_CHANNEL_OPEN_FAILURE             92    /* 0x5c */
261     'SSH2_MSG_CHANNEL_OPEN_FAILURE' => sub {
262         my ($direction, $seq, $data) = @_;
263         my ($rid, $reason, $desc, $lang) = &parse("uuss", $data);
264         $rid = ($direction eq "i" ? "c" : "s") . $rid;
265         my $index = $chan_by_id{$rid};
266         if (!defined $index) {
267             printf "UNKNOWN_CHANNEL (%s) %s\n", $rid, &str($reason);
268             return;
269         }
270         my $chan = $channels[$index];
271         $chan->{'state'} = 'rejected';
272         printf "ch%d (%s) %s\n", $index, $chan->{'id'}, &str($reason);
273     },
274 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST            93    /* 0x5d */
275     'SSH2_MSG_CHANNEL_WINDOW_ADJUST' => sub {
276         my ($direction, $seq, $data) = @_;
277         my ($rid, $bytes) = &parse("uu", $data);
278         $rid = ($direction eq "i" ? "c" : "s") . $rid;
279         my $index = $chan_by_id{$rid};
280         if (!defined $index) {
281             printf "UNKNOWN_CHANNEL (%s) +%d\n", $rid, $bytes;
282             return;
283         }
284         my $chan = $channels[$index];
285         $chan->{$direction}{'win'} += $bytes;
286         printf "ch%d (%s) +%d (--%d)\n", $index, $chan->{'id'}, $bytes,
287             $chan->{$direction}{'win'};
288     },
289 #define SSH2_MSG_CHANNEL_DATA                     94    /* 0x5e */
290     'SSH2_MSG_CHANNEL_DATA' => sub {
291         my ($direction, $seq, $data) = @_;
292         my ($rid, $bytes) = &parse("uu", $data);
293         $rid = ($direction eq "i" ? "c" : "s") . $rid;
294         my $index = $chan_by_id{$rid};
295         if (!defined $index) {
296             printf "UNKNOWN_CHANNEL (%s), %s bytes\n", $rid, $bytes;
297             return;
298         }
299         my $chan = $channels[$index];
300         $chan->{$direction}{'seq'} += $bytes;
301         printf "ch%d (%s), %s bytes (%d--%d)\n", $index, $chan->{'id'}, $bytes,
302             $chan->{$direction}{'seq'}-$bytes, $chan->{$direction}{'seq'};
303         my @realdata = splice @$data, 0, $bytes;
304         if ($dumpdata) {
305             my $filekey = $direction . "file";
306             if (!defined $chan->{$filekey}) {
307                 my $filename = sprintf "ch%d.%s", $index, $direction;
308                 $chan->{$filekey} = FileHandle->new(">$filename");
309                 if (!defined $chan->{$filekey}) {
310                     die "$filename: $!\n";
311                 }
312             }
313             die "channel data not present in $seq\n" if @realdata < $bytes;
314             my $rawdata = pack "C*", @realdata;
315             my $fh = $chan->{$filekey};
316             print $fh $rawdata;
317         }
318         if (@realdata == $bytes and defined $chan->{$direction."data"}) {
319             my $rawdata = pack "C*", @realdata;
320             $chan->{$direction."data"}->($chan, $index, $direction, $rawdata);
321         }
322     },
323 #define SSH2_MSG_CHANNEL_EXTENDED_DATA            95    /* 0x5f */
324     'SSH2_MSG_CHANNEL_EXTENDED_DATA' => sub {
325         my ($direction, $seq, $data) = @_;
326         my ($rid, $bytes) = &parse("uu", $data);
327         $rid = ($direction eq "i" ? "c" : "s") . $rid;
328         my $index = $chan_by_id{$rid};
329         if (!defined $index) {
330             printf "UNKNOWN_CHANNEL (%s), type %s, %s bytes\n", $rid,
331                 $type, $bytes;
332             return;
333         }
334         my $chan = $channels[$index];
335         my $dir = $direction eq "i" ? 'sc' : 'cs';
336         $chan->{$dir}{'seq'} += $bytes;
337         printf "ch%d (%s), %s bytes (%d--%d)\n", $index, $chan->{'id'}, $bytes,
338             $chan->{$dir}{$seq}-$bytes, $chan->{$dir}{$seq};
339         printf "ch%d (%s), %s bytes\n", $index, $chan->{'id'}, $bytes;
340         my @realdata = splice @$data, 0, $bytes;
341         if ($dumpdata) {
342             # We treat EXTENDED_DATA as equivalent to DATA, for the
343             # moment. It's not clear what else would be a better thing
344             # to do with it, and this at least is the Right Answer if
345             # the data is going to a terminal and the aim is to debug
346             # the terminal emulator.
347             my $filekey = $direction . "file";
348             if (!defined $chan->{$filekey}) {
349                 my $filename = sprintf "ch%d.%s", $index, $direction;
350                 $chan->{$filekey} = FileHandle->new;
351                 if (!$chan->{$filekey}->open(">", $filename)) {
352                     die "$filename: $!\n";
353                 }
354             }
355             die "channel data not present in $seq\n" if @realdata < $bytes;
356             my $rawdata = pack "C*", @realdata;
357             my $fh = $chan->{$filekey};
358             print $fh $rawdata;
359         }
360         if (@realdata == $bytes and defined $chan->{$direction."data"}) {
361             my $rawdata = pack "C*", @realdata;
362             $chan->{$direction."data"}->($chan, $index, $direction, $rawdata);
363         }
364     },
365 #define SSH2_MSG_CHANNEL_EOF                      96    /* 0x60 */
366     'SSH2_MSG_CHANNEL_EOF' => sub {
367         my ($direction, $seq, $data) = @_;
368         my ($rid) = &parse("uu", $data);
369         $rid = ($direction eq "i" ? "c" : "s") . $rid;
370         my $index = $chan_by_id{$rid};
371         if (!defined $index) {
372             printf "UNKNOWN_CHANNEL (%s)\n", $rid;
373             return;
374         }
375         my $chan = $channels[$index];
376         printf "ch%d (%s)\n", $index, $chan->{'id'};
377     },
378 #define SSH2_MSG_CHANNEL_CLOSE                    97    /* 0x61 */
379     'SSH2_MSG_CHANNEL_CLOSE' => sub {
380         my ($direction, $seq, $data) = @_;
381         my ($rid) = &parse("uu", $data);
382         $rid = ($direction eq "i" ? "c" : "s") . $rid;
383         my $index = $chan_by_id{$rid};
384         if (!defined $index) {
385             printf "UNKNOWN_CHANNEL (%s)\n", $rid;
386             return;
387         }
388         my $chan = $channels[$index];
389         $chan->{'state'} = ($chan->{'state'} eq "open" ? "halfclosed" :
390                             $chan->{'state'} eq "halfclosed" ? "closed" :
391                             "confused");
392         if ($chan->{'state'} eq "closed") {
393             $chan->{'ifile'}->close if defined $chan->{'ifile'};
394             $chan->{'ofile'}->close if defined $chan->{'ofile'};
395         }
396         printf "ch%d (%s)\n", $index, $chan->{'id'};
397     },
398 #define SSH2_MSG_CHANNEL_REQUEST                  98    /* 0x62 */
399     'SSH2_MSG_CHANNEL_REQUEST' => sub {
400         my ($direction, $seq, $data) = @_;
401         my ($rid, $type, $wantreply) = &parse("usb", $data);
402         $rid = ($direction eq "i" ? "c" : "s") . $rid;
403         my $index = $chan_by_id{$rid};
404         my $chan;
405         if (!defined $index) {
406             printf "UNKNOWN_CHANNEL (%s) %s (%s)", $rid,
407                 $type, $wantreply eq "yes" ? "reply" : "noreply";
408         } else {
409             $chan = $channels[$index];
410             printf "ch%d (%s) %s (%s)", $index, $chan->{'id'},
411                 $type, $wantreply eq "yes" ? "reply" : "noreply";
412             push @{$chan->{'requests_'.$direction}}, [$seq, $type]
413                 if $wantreply eq "yes";
414         }
415         if ($type eq "pty-req") {
416             my ($term, $w, $h, $pw, $ph, $modes) = &parse("suuuus", $data);
417             printf " %s %sx%s", &str($term), $w, $h;
418         } elsif ($type eq "x11-req") {
419             my ($single, $xprot, $xcookie, $xscreen) = &parse("bssu", $data);
420             print " one-off" if $single eq "yes";
421             printf " %s :%s", $xprot, $xscreen;
422         } elsif ($type eq "exec") {
423             my ($command) = &parse("s", $data);
424             printf " %s", &str($command);
425         } elsif ($type eq "subsystem") {
426             my ($subsys) = &parse("s", $data);
427             printf " %s", &str($subsys);
428             if ($subsys eq "sftp") {
429                 &sftp_setup($index);
430             }
431         } elsif ($type eq "window-change") {
432             my ($w, $h, $pw, $ph) = &parse("uuuu", $data);
433             printf " %sx%s", $w, $h;
434         } elsif ($type eq "xon-xoff") {
435             my ($can) = &parse("b", $data);
436             printf " %s", $can;
437         } elsif ($type eq "signal") {
438             my ($sig) = &parse("s", $data);
439             printf " %s", &str($sig);
440         } elsif ($type eq "exit-status") {
441             my ($status) = &parse("u", $data);
442             printf " %s", $status;
443         } elsif ($type eq "exit-signal") {
444             my ($sig, $core, $error, $lang) = &parse("sbss", $data);
445             printf " %s", &str($sig);
446             print " (core dumped)" if $core eq "yes";
447         }
448         print "\n";
449     },
450 #define SSH2_MSG_CHANNEL_SUCCESS                  99    /* 0x63 */
451     'SSH2_MSG_CHANNEL_SUCCESS' => sub {
452         my ($direction, $seq, $data) = @_;
453         my ($rid) = &parse("uu", $data);
454         $rid = ($direction eq "i" ? "c" : "s") . $rid;
455         my $index = $chan_by_id{$rid};
456         if (!defined $index) {
457             printf "UNKNOWN_CHANNEL (%s)\n", $rid;
458             return;
459         }
460         my $chan = $channels[$index];
461         printf "ch%d (%s)", $index, $chan->{'id'};
462         my $otherdir = ($direction eq "i" ? "o" : "i");
463         my $request = shift @{$chan->{'requests_' . $otherdir}};
464         if (defined $request) {
465             printf " to %s", $request->[0];
466         } else {
467             print " (spurious?)";
468         }
469         print "\n";
470     },
471 #define SSH2_MSG_CHANNEL_FAILURE                  100   /* 0x64 */
472     'SSH2_MSG_CHANNEL_FAILURE' => sub {
473         my ($direction, $seq, $data) = @_;
474         my ($rid) = &parse("uu", $data);
475         $rid = ($direction eq "i" ? "c" : "s") . $rid;
476         my $index = $chan_by_id{$rid};
477         if (!defined $index) {
478             printf "UNKNOWN_CHANNEL (%s)\n", $rid;
479             return;
480         }
481         my $chan = $channels[$index];
482         printf "ch%d (%s)", $index, $chan->{'id'};
483         my $otherdir = ($direction eq "i" ? "o" : "i");
484         my $request = shift @{$chan->{'requests_' . $otherdir}};
485         if (defined $request) {
486             printf " to %s", $request->[0];
487         } else {
488             print " (spurious?)";
489         }
490         print "\n";
491     },
492 #define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE               60
493     'SSH2_MSG_USERAUTH_GSSAPI_RESPONSE' => sub {
494         my ($direction, $seq, $data) = @_;
495         print "\n";
496     },
497 #define SSH2_MSG_USERAUTH_GSSAPI_TOKEN                  61
498     'SSH2_MSG_USERAUTH_GSSAPI_TOKEN' => sub {
499         my ($direction, $seq, $data) = @_;
500         print "\n";
501     },
502 #define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE      63
503     'SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE' => sub {
504         my ($direction, $seq, $data) = @_;
505         print "\n";
506     },
507 #define SSH2_MSG_USERAUTH_GSSAPI_ERROR                  64
508     'SSH2_MSG_USERAUTH_GSSAPI_ERROR' => sub {
509         my ($direction, $seq, $data) = @_;
510         print "\n";
511     },
512 #define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK                 65
513     'SSH2_MSG_USERAUTH_GSSAPI_ERRTOK' => sub {
514         my ($direction, $seq, $data) = @_;
515         print "\n";
516     },
517 #define SSH2_MSG_USERAUTH_GSSAPI_MIC                    66
518     'SSH2_MSG_USERAUTH_GSSAPI_MIC' => sub {
519         my ($direction, $seq, $data) = @_;
520         print "\n";
521     },
522 );
523
524 my %sftp_packets = (
525 #define SSH_FXP_INIT                              1     /* 0x1 */
526     0x1 => sub {
527         my ($chan, $index, $direction, $id, $data) = @_;
528         my ($ver) = &parse("u", $data);
529         printf "SSH_FXP_INIT %d\n", $ver;
530     },
531 #define SSH_FXP_VERSION                           2     /* 0x2 */
532     0x2 => sub {
533         my ($chan, $index, $direction, $id, $data) = @_;
534         my ($ver) = &parse("u", $data);
535         printf "SSH_FXP_VERSION %d\n", $ver;
536     },
537 #define SSH_FXP_OPEN                              3     /* 0x3 */
538     0x3 => sub {
539         my ($chan, $index, $direction, $id, $data) = @_;
540         my ($reqid, $path, $pflags) = &parse("usu", $data);
541         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_OPEN");
542         printf " \"%s\" ", $path;
543         if ($pflags eq 0) {
544             print "0";
545         } else {
546             my $sep = "";
547             if ($pflags & 1) { $pflags ^= 1; print "${sep}READ"; $sep = "|"; }
548             if ($pflags & 2) { $pflags ^= 2; print "${sep}WRITE"; $sep = "|"; }
549             if ($pflags & 4) { $pflags ^= 4; print "${sep}APPEND"; $sep = "|"; }
550             if ($pflags & 8) { $pflags ^= 8; print "${sep}CREAT"; $sep = "|"; }
551             if ($pflags & 16) { $pflags ^= 16; print "${sep}TRUNC"; $sep = "|"; }
552             if ($pflags & 32) { $pflags ^= 32; print "${sep}EXCL"; $sep = "|"; }
553             if ($pflags) { print "${sep}${pflags}"; }
554         }
555         print "\n";
556     },
557 #define SSH_FXP_CLOSE                             4     /* 0x4 */
558     0x4 => sub {
559         my ($chan, $index, $direction, $id, $data) = @_;
560         my ($reqid, $handle) = &parse("us", $data);
561         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_CLOSE");
562         printf " \"%s\"", &stringescape($handle);
563         print "\n";
564     },
565 #define SSH_FXP_READ                              5     /* 0x5 */
566     0x5 => sub {
567         my ($chan, $index, $direction, $id, $data) = @_;
568         my ($reqid, $handle, $offset, $len) = &parse("usUu", $data);
569         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_READ");
570         printf " \"%s\" %d %d", &stringescape($handle), $offset, $len;
571         print "\n";
572     },
573 #define SSH_FXP_WRITE                             6     /* 0x6 */
574     0x6 => sub {
575         my ($chan, $index, $direction, $id, $data) = @_;
576         my ($reqid, $handle, $offset, $wdata) = &parse("usUs", $data);
577         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_WRITE");
578         printf " \"%s\" %d [%d bytes]", &stringescape($handle), $offset, length $wdata;
579         print "\n";
580     },
581 #define SSH_FXP_LSTAT                             7     /* 0x7 */
582     0x7 => sub {
583         my ($chan, $index, $direction, $id, $data) = @_;
584         my ($reqid, $path) = &parse("us", $data);
585         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_LSTAT");
586         printf " \"%s\"", $path;
587         print "\n";
588     },
589 #define SSH_FXP_FSTAT                             8     /* 0x8 */
590     0x8 => sub {
591         my ($chan, $index, $direction, $id, $data) = @_;
592         my ($reqid, $handle) = &parse("us", $data);
593         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_FSTAT");
594         printf " \"%s\"", &stringescape($handle);
595         print "\n";
596     },
597 #define SSH_FXP_SETSTAT                           9     /* 0x9 */
598     0x9 => sub {
599         my ($chan, $index, $direction, $id, $data) = @_;
600         my ($reqid, $path) = &parse("us", $data);
601         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_SETSTAT");
602         my $attrs = &sftp_parse_attrs($data);
603         printf " \"%s\" %s", $path, $attrs;
604         print "\n";
605     },
606 #define SSH_FXP_FSETSTAT                          10    /* 0xa */
607     0xa => sub {
608         my ($chan, $index, $direction, $id, $data) = @_;
609         my ($reqid, $handle) = &parse("us", $data);
610         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_FSETSTAT");
611         my $attrs = &sftp_parse_attrs($data);
612         printf " \"%s\" %s", &stringescape($handle), $attrs;
613         print "\n";
614     },
615 #define SSH_FXP_OPENDIR                           11    /* 0xb */
616     0xb => sub {
617         my ($chan, $index, $direction, $id, $data) = @_;
618         my ($reqid, $path) = &parse("us", $data);
619         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_OPENDIR");
620         printf " \"%s\"", $path;
621         print "\n";
622     },
623 #define SSH_FXP_READDIR                           12    /* 0xc */
624     0xc => sub {
625         my ($chan, $index, $direction, $id, $data) = @_;
626         my ($reqid, $handle) = &parse("us", $data);
627         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_READDIR");
628         printf " \"%s\"", &stringescape($handle);
629         print "\n";
630     },
631 #define SSH_FXP_REMOVE                            13    /* 0xd */
632     0xd => sub {
633         my ($chan, $index, $direction, $id, $data) = @_;
634         my ($reqid, $path) = &parse("us", $data);
635         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_REMOVE");
636         printf " \"%s\"", $path;
637         print "\n";
638     },
639 #define SSH_FXP_MKDIR                             14    /* 0xe */
640     0xe => sub {
641         my ($chan, $index, $direction, $id, $data) = @_;
642         my ($reqid, $path) = &parse("us", $data);
643         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_MKDIR");
644         printf " \"%s\"", $path;
645         print "\n";
646     },
647 #define SSH_FXP_RMDIR                             15    /* 0xf */
648     0xf => sub {
649         my ($chan, $index, $direction, $id, $data) = @_;
650         my ($reqid, $path) = &parse("us", $data);
651         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_RMDIR");
652         printf " \"%s\"", $path;
653         print "\n";
654     },
655 #define SSH_FXP_REALPATH                          16    /* 0x10 */
656     0x10 => sub {
657         my ($chan, $index, $direction, $id, $data) = @_;
658         my ($reqid, $path) = &parse("us", $data);
659         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_REALPATH");
660         printf " \"%s\"", $path;
661         print "\n";
662     },
663 #define SSH_FXP_STAT                              17    /* 0x11 */
664     0x11 => sub {
665         my ($chan, $index, $direction, $id, $data) = @_;
666         my ($reqid, $path) = &parse("us", $data);
667         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_STAT");
668         printf " \"%s\"", $path;
669         print "\n";
670     },
671 #define SSH_FXP_RENAME                            18    /* 0x12 */
672     0x12 => sub {
673         my ($chan, $index, $direction, $id, $data) = @_;
674         my ($reqid, $srcpath, $dstpath) = &parse("uss", $data);
675         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_RENAME");
676         printf " \"%s\" \"%s\"", $srcpath, $dstpath;
677         print "\n";
678     },
679 #define SSH_FXP_STATUS                            101   /* 0x65 */
680     0x65 => sub {
681         my ($chan, $index, $direction, $id, $data) = @_;
682         my ($reqid, $status) = &parse("uu", $data);
683         &sftp_logreply($chan, $direction, $reqid, $id, "SSH_FXP_STATUS");
684         print " ";
685         if ($status eq "0") { print "SSH_FX_OK"; }
686         elsif ($status eq "1") { print "SSH_FX_EOF"; }
687         elsif ($status eq "2") { print "SSH_FX_NO_SUCH_FILE"; }
688         elsif ($status eq "3") { print "SSH_FX_PERMISSION_DENIED"; }
689         elsif ($status eq "4") { print "SSH_FX_FAILURE"; }
690         elsif ($status eq "5") { print "SSH_FX_BAD_MESSAGE"; }
691         elsif ($status eq "6") { print "SSH_FX_NO_CONNECTION"; }
692         elsif ($status eq "7") { print "SSH_FX_CONNECTION_LOST"; }
693         elsif ($status eq "8") { print "SSH_FX_OP_UNSUPPORTED"; }
694         else { printf "[unknown status %d]", $status; }
695         print "\n";
696     },
697 #define SSH_FXP_HANDLE                            102   /* 0x66 */
698     0x66 => sub {
699         my ($chan, $index, $direction, $id, $data) = @_;
700         my ($reqid, $handle) = &parse("us", $data);
701         &sftp_logreply($chan, $direction, $reqid, $id, "SSH_FXP_HANDLE");
702         printf " \"%s\"", &stringescape($handle);
703         print "\n";
704     },
705 #define SSH_FXP_DATA                              103   /* 0x67 */
706     0x67 => sub {
707         my ($chan, $index, $direction, $id, $data) = @_;
708         my ($reqid, $retdata) = &parse("us", $data);
709         &sftp_logreply($chan, $direction, $reqid, $id, "SSH_FXP_DATA");
710         printf " [%d bytes]", length $retdata;
711         print "\n";
712     },
713 #define SSH_FXP_NAME                              104   /* 0x68 */
714     0x68 => sub {
715         my ($chan, $index, $direction, $id, $data) = @_;
716         my ($reqid, $count) = &parse("uu", $data);
717         &sftp_logreply($chan, $direction, $reqid, $id, "SSH_FXP_NAME");
718         for my $i (1..$count) {
719             my ($name, $longname) = &parse("ss", $data);
720             my $attrs = &sftp_parse_attrs($data);
721             print " [name=\"$name\", longname=\"$longname\", attrs=$attrs]";
722         }
723         print "\n";
724     },
725 #define SSH_FXP_ATTRS                             105   /* 0x69 */
726     0x69 => sub {
727         my ($chan, $index, $direction, $id, $data) = @_;
728         my ($reqid) = &parse("u", $data);
729         &sftp_logreply($chan, $direction, $reqid, $id, "SSH_FXP_ATTRS");
730         my $attrs = &sftp_parse_attrs($data);
731         printf " %s", $attrs;
732         print "\n";
733     },
734 #define SSH_FXP_EXTENDED                          200   /* 0xc8 */
735     0xc8 => sub {
736         my ($chan, $index, $direction, $id, $data) = @_;
737         my ($reqid, $type) = &parse("us", $data);
738         &sftp_logreq($chan, $direction, $reqid, $id, "SSH_FXP_EXTENDED");
739         printf " \"%s\"", $type;
740         print "\n";
741     },
742 #define SSH_FXP_EXTENDED_REPLY                    201   /* 0xc9 */
743     0xc9 => sub {
744         my ($chan, $index, $direction, $id, $data) = @_;
745         my ($reqid) = &parse("u", $data);
746         print "\n";
747         &sftp_logreply($chan, $direction, $reqid,$id,"SSH_FXP_EXTENDED_REPLY");
748     },
749 );
750
751 my ($direction, $seq, $ourseq, $type, $data, $recording);
752 my %ourseqs = ('i'=>0, 'o'=>0);
753
754 $recording = 0;
755 while (<>) {
756     if ($recording) {
757         if (/^  [0-9a-fA-F]{8}  ((?:[0-9a-fA-F]{2} )*[0-9a-fA-F]{2})/) {
758             push @$data, map { $_ eq "XX" ? -1 : hex $_ } split / /, $1;
759         } else {
760             $recording = 0;
761             my $fullseq = "$direction$ourseq";
762             print "$fullseq: $type ";
763             if (defined $packets{$type}) {
764                 $packets{$type}->($direction, $fullseq, $data);
765             } else {
766                 printf "raw %s\n", join "", map { sprintf "%02x", $_ } @$data;
767             }
768         }
769     }
770     if (/^(Incoming|Outgoing) packet #0x([0-9a-fA-F]+), type \d+ \/ 0x[0-9a-fA-F]+ \((.*)\)/) {
771         $direction = ($1 eq "Incoming" ? 'i' : 'o');
772         # $seq is the sequence number quoted in the log file. $ourseq
773         # is our own count of the sequence number, which differs in
774         # that it shouldn't wrap at 2^32, should anyone manage to run
775         # this script over such a huge log file.
776         $seq = hex $2;
777         $ourseq = $ourseqs{$direction}++;
778         $type = $3;
779         $data = [];
780         $recording = 1;
781     }
782 }
783
784 if ($dumpchannels) {
785     my %stateorder = ('closed'=>0, 'rejected'=>1,
786                       'halfclosed'=>2, 'open'=>3, 'halfopen'=>4);
787     for my $index (0..$#channels) {
788         my $chan = $channels[$index];
789         my $so = $stateorder{$chan->{'state'}};
790         $so = 1000 unless defined $so; # any state I've missed above comes last
791         $chan->{'index'} = sprintf "ch%d", $index;
792         $chan->{'order'} = sprintf "%08d %08d", $so, $index;
793     }
794     my @sortedchannels = sort { $a->{'order'} cmp $b->{'order'} } @channels;
795     for my $chan (@sortedchannels) {
796         printf "%s (%s): %s\n", $chan->{'index'}, $chan->{'id'}, $chan->{'state'};
797     }
798 }
799
800 sub parseone {
801     my ($type, $data) = @_;
802     if ($type eq "u") { # uint32
803         my @bytes = splice @$data, 0, 4;
804         return "<missing>" if @bytes < 4 or grep { $_<0 } @bytes;
805         return unpack "N", pack "C*", @bytes;
806     } elsif ($type eq "U") { # uint64
807         my @bytes = splice @$data, 0, 8;
808         return "<missing>" if @bytes < 8 or grep { $_<0 } @bytes;
809         my @words = unpack "NN", pack "C*", @bytes;
810         return ($words[0] << 32) + $words[1];
811     } elsif ($type eq "b") { # boolean
812         my $byte = shift @$data;
813         return "<missing>" if !defined $byte or $byte < 0;
814         return $byte ? "yes" : "no";
815     } elsif ($type eq "B") { # byte
816         my $byte = shift @$data;
817         return "<missing>" if !defined $byte or $byte < 0;
818         return $byte;
819     } elsif ($type eq "s" or $type eq "m") { # string, mpint
820         my @bytes = splice @$data, 0, 4;
821         return "<missing>" if @bytes < 4 or grep { $_<0 } @bytes;
822         my $len = unpack "N", pack "C*", @bytes;
823         @bytes = splice @$data, 0, $len;
824         return "<missing>" if @bytes < $len or grep { $_<0 } @bytes;
825         if ($type eq "mpint") {
826             my $str = "";
827             if ($bytes[0] >= 128) {
828                 # Take two's complement.
829                 @bytes = map { 0xFF ^ $_ } @bytes;
830                 for my $i (reverse 0..$#bytes) {
831                     if ($bytes[$i] < 0xFF) {
832                         $bytes[$i]++;
833                         last;
834                     } else {
835                         $bytes[$i] = 0;
836                     }
837                 }
838                 $str = "-";
839             }
840             $str .= "0x" . join "", map { sprintf "%02x", $_ } @bytes;
841             return $str;
842         } else {
843             return pack "C*", @bytes;
844         }
845     }
846 }
847
848 sub parse {
849     my ($template, $data) = @_;
850     return map { &parseone($_, $data) } split //, $template;
851 }
852
853 sub str {
854     # Quote as a string. If I get enthusiastic I might arrange for
855     # strange characters inside the string to be quoted.
856     my $str = shift @_;
857     return "'$str'";
858 }
859
860 sub sftp_setup {
861     my $index = shift @_;
862     my $chan = $channels[$index];
863     $chan->{'obuf'} = $chan->{'ibuf'} = '';
864     $chan->{'ocnt'} = $chan->{'icnt'} = 0;
865     $chan->{'odata'} = $chan->{'idata'} = \&sftp_data;
866     $chan->{'sftpreqs'} = {};
867 }
868
869 sub sftp_data {
870     my ($chan, $index, $direction, $data) = @_;
871     my $buf = \$chan->{$direction."buf"};
872     my $cnt = \$chan->{$direction."cnt"};
873     $$buf .= $data;
874     while (length $$buf >= 4) {
875         my $msglen = unpack "N", $$buf;
876         last if length $$buf < 4 + $msglen;
877         my $msg = substr $$buf, 4, $msglen;
878         $$buf = substr $$buf, 4 + $msglen;
879         $msg = [unpack "C*", $msg];
880         my $type = shift @$msg;
881         my $id = sprintf "ch%d_sftp_%s%d", $index, $direction, ${$cnt}++;
882         print "$id: ";
883         if (defined $sftp_packets{$type}) {
884             $sftp_packets{$type}->($chan, $index, $direction, $id, $msg);
885         } else {
886             printf "unknown SFTP packet type %d\n", $type;
887         }
888     }
889 }
890
891 sub sftp_logreq {
892     my ($chan, $direction, $reqid, $id, $name) = @_;
893     print "$name";
894     if ($direction eq "o") { # requests coming _in_ are too weird to track
895         $chan->{'sftpreqs'}->{$reqid} = $id;
896     }
897 }
898
899 sub sftp_logreply {
900     my ($chan, $direction, $reqid, $id, $name) = @_;
901     print "$name";
902     if ($direction eq "i") { # replies going _out_ are too weird to track
903         if (defined $chan->{'sftpreqs'}->{$reqid}) {
904             print " to ", $chan->{'sftpreqs'}->{$reqid};
905             $chan->{'sftpreqs'}->{$reqid} = undef;
906         }
907     }
908 }
909
910 sub sftp_parse_attrs {
911     my ($data) = @_;
912     my ($flags) = &parse("u", $data);
913     return $flags if $flags eq "<missing>";
914     my $out = "{";
915     my $sep = "";
916     if ($flags & 0x00000001) { # SSH_FILEXFER_ATTR_SIZE
917         $out .= $sep . sprintf "size=%d", &parse("U", $data);
918         $sep = ", ";
919     }
920     if ($flags & 0x00000002) { # SSH_FILEXFER_ATTR_UIDGID
921         $out .= $sep . sprintf "uid=%d", &parse("u", $data);
922         $out .= $sep . sprintf "gid=%d", &parse("u", $data);
923         $sep = ", ";
924     }
925     if ($flags & 0x00000004) { # SSH_FILEXFER_ATTR_PERMISSIONS
926         $out .= $sep . sprintf "perms=%#o", &parse("u", $data);
927         $sep = ", ";
928     }
929     if ($flags & 0x00000008) { # SSH_FILEXFER_ATTR_ACMODTIME
930         $out .= $sep . sprintf "atime=%d", &parse("u", $data);
931         $out .= $sep . sprintf "mtime=%d", &parse("u", $data);
932         $sep = ", ";
933     }
934     if ($flags & 0x80000000) { # SSH_FILEXFER_ATTR_EXTENDED
935         my $extcount = &parse("u", $data);
936         while ($extcount-- > 0) {
937             $out .= $sep . sprintf "\"%s\"=\"%s\"", &parse("ss", $data);
938             $sep = ", ";
939         }
940     }
941     $out .= "}";
942     return $out;
943 }
944
945 sub stringescape {
946     my ($str) = @_;
947     $str =~ s!\\!\\\\!g;
948     $str =~ s![^ -~]!sprintf "\\x%02X", ord $&!eg;
949     return $str;
950 }