]> arthur.barton.de Git - netatalk.git/blob - contrib/shell_utils/asip-status.pl.in
Change loglevel and add a few debug log statements
[netatalk.git] / contrib / shell_utils / asip-status.pl.in
1 #!@PERL@
2 #
3 # asip-status - send DSIGetStatus to an AppleShare IP file server (aka
4 #               ASIP, aka AFP over TCP port 548).  A returned UAM of 
5 #               "No User Authen" means that the server supports guest access.
6 #
7 # author: James W. Abendschan  <jwa@jammed.com>
8 # license: GPL - http://www.gnu.org/copyleft/gpl.html
9 # url: http://www.jammed.com/~jwa/hacks/security/asip/
10 # Date: 7 May 1997 (v1.0) - original version
11 # see also: 
12 #   - http://developer.apple.com/techpubs/macos8/NetworkCommSvcs/AppleShare/
13 #   - http://www2.opendoor.com/asip/   (excellent Mac sharing / security site)
14 #
15 # todo: log in as guest & get a list of shares
16 #
17
18 #
19 # This edition is a part of netatalk @NETATALK_VERSION@.
20 #
21
22 use strict;
23 use IO::Socket;                 # sucks because Timeout doesn't
24
25 my ($arg);
26 my ($hostport);
27 my ($host);
28 my ($port);
29
30 while ($arg = shift @ARGV)
31 {
32         $main::show_icon = 1 if ($arg eq "-i");
33         $main::debug = 1 if ($arg eq "-d");
34         $main::hexdump = 1 if ($arg eq "-x");
35         $main::showver = 1 if ($arg eq "-v");
36         $main::showver = 1 if ($arg eq "-version");
37         $main::showver = 1 if ($arg eq "--version");
38         $hostport = $arg if ($arg !~ /^-/);
39 }
40
41 if ($main::showver ==1)
42 {
43         print "$0\n";
44         print "Original edition: 7 May 1997 \(v1.0\) James W. Abendschan\n";
45         print "This edition is a part of Netatalk @NETATALK_VERSION@\n";
46         exit(-1);
47 }
48
49 if ($hostport eq "")
50 {
51         print "usage: $0 [-d] [-i] [-x] hostname[:port]\n";
52         print "       $0 -v|-version|--version\n";
53         print "Queries AFP servers for their capabilities.\n";
54         print "  -d: Enable debug output.\n";
55         print "  -i: Show icon if it exists.\n";
56         print "  -x: Enable hex dump output.\n";
57         print "  -v,-version,--version: Show version.\n";
58         exit(-1);
59 }
60
61 ($host, $port) = split(/\:/, $hostport);
62 $port = "548" if ($port eq "");
63
64 my ($packet) = build_packet();
65 my ($code) = sendpacket($host, $port, $packet);
66 exit $code;
67
68
69 sub build_packet 
70 {
71         my (@packet) = 
72                 (
73                 0x00,                   # 0- request, 1-reply
74                 0x03,                   # 3- DSIGetStatus
75                 0xde, 0xad, 0x00,       # request ID
76                 0x00, 0x00, 0x00, 0x00, # data field
77                 0x00, 0x00, 0x00, 0x00, # length of data stream header
78                 0x00, 0x00, 0x00, 0x00  # reserved
79                 );
80
81
82         my ($packet) = pack("C*", @packet);
83
84         return $packet;
85
86 }
87
88 sub sendpacket 
89 {
90         my ($host, $port, $packet) = @_;
91         my ($b, $buf);
92
93         print "opening $host:$port\n" if ($main::debug);
94
95         my ($asip_sock) = IO::Socket::INET->new( 
96                 PeerAddr => $host, 
97                 PeerPort => $port, 
98                 Proto => 'tcp', 
99                 Type => SOCK_STREAM, 
100                 Timeout => 10
101                 ) || die "connect to $host failure: $!"; 
102         $asip_sock->autoflush(1);
103
104         print "sending packet\n" if ($main::debug);
105
106         my ($count) = syswrite($asip_sock, $packet, length($packet));
107
108         if ($count != length($packet))
109         {
110                 print "only wrote $count of " . length($packet) . " bytes?\n";
111                 exit(-1);
112         }
113
114         # reply can span multiple packets
115
116         print "sysread: " if ($main::debug);
117         while (sysread($asip_sock, $b, 256))
118         {
119                 $buf .= $b;
120                 print "." if ($main::debug);
121         }       
122
123         close ($asip_sock);
124
125         print " read " . length($buf) . " bytes\n" if ($main::debug);
126
127         if (length($buf) == 0)
128         {
129                 print "empty reply packet?\n";
130                 return -2;
131         }
132         else
133         {
134                 print "AFP reply from $host:$port\n";
135                 return (parse_packet($buf));
136         }       
137 }
138
139
140 sub parse_packet 
141 {
142         my ($buf) = shift @_;
143         my (@packet);
144         my ($i);
145
146         hexdump($buf) if ($main::hexdump);
147
148         for ($i=0;$i<length($buf);$i++)
149         {
150                 push(@packet, substr($buf, $i, 1));
151         }       
152
153         my ($flags) = unpack("C", @packet[0]);
154         my ($cmd) = unpack("C", @packet[1]);
155
156         my ($request_id) = unpack("n", @packet[2] . @packet[3]);
157         print "Flags: $flags  Cmd: $cmd  ID: $request_id\n";
158
159         print getasipsrv("flags", $flags) . ": " . getasipsrv("command", $cmd) . "\n";
160         print "Request ID: $request_id\n";
161
162         print "** Request ID didn't match what we sent!\n" if ($request_id != 0xdead);
163
164         # "Error Code / Enclosed Data Offset"
165         # I have never seen this be non-zero ..
166
167         my ($edo) = unpack("N2", @packet[4] . @packet[5] . @packet[6] . @packet[7]);
168         print "** Wow, a non-zero Error/Enclosed Data Offset: $edo\n" if ($edo);
169
170         # "Total Data Length"
171
172         my ($datalen) = unpack("N2", @packet[8] . @packet[9] . @packet[10] . @packet[11]);
173
174         print "Total data length: $datalen\n" if ($main::debug);
175
176         # "Reserved Field"
177
178         my ($reserved) = unpack("N2", @packet[12] . @packet[13] . @packet[14] . @packet[15]);
179
180         print "Reserved field: $reserved\n" if ($reserved);
181
182         if ($cmd != 3)
183         {
184                 print "I can only parse packets of reply-type DSIGetStatus (3)\n";
185                 print "This is reply-type " . getasipsrv("command", $cmd) . "\n";
186         }
187         if ($datalen == 0)
188         {
189                 print "No data in packet?\n";
190         }       
191         if (($datalen > 0) && ($cmd == 3)) 
192         {
193                 my (@AFPpacket) = @packet[($edo+16)..($edo+16+$datalen)];
194                 return (parse_FPGetSrvrInfo(@AFPpacket));
195         }
196         else
197         {
198                 print "I don't know how to parse this type of packet.\n";
199                 return(2);
200         }       
201 }
202
203
204
205 sub parse_FPGetSrvrInfo() 
206 {
207         my (@packet) = @_;
208         my ($i);
209
210         my ($machinetype_offset) = unpack("n", @packet[0] . @packet[1]);
211         print "Machine type offset in packet: $machinetype_offset\n" if ($main::debug);
212         my ($machinetype) = extract(1, $machinetype_offset, @packet);
213         print "Machine type: $machinetype\n";
214
215         my ($afpversioncount_offset) = unpack("n", @packet[2] . @packet[3]);
216         print "AFPversion count offset: $afpversioncount_offset\n" if ($main::debug);
217         my (@afpversions) = extract(0, $afpversioncount_offset, @packet);
218         print "AFP versions: " . join(",", @afpversions) . "\n";
219
220         my ($uamcount_offset) = unpack("n", @packet[4] . @packet[5]);
221         print "UAMcount offset: $uamcount_offset\n" if ($main::debug);
222         my (@uams) = extract(0, $uamcount_offset, @packet);
223         print "UAMs: " . join(",", @uams) . "\n";
224
225         my ($allow_guest) = 0;
226         $allow_guest = 1 if (grep(/No User Authen/, @uams));
227
228         # it would be cute to see the icon.
229
230         my ($icon_offset) = unpack("n", @packet[6] . @packet[7]);
231         print "Volume Icon & Mask offset: $icon_offset\n" if ($main::debug);
232         print "Volume Icon & Mask: ";
233         if ($icon_offset) {
234             print "Yes\n";
235         } else {
236             print "No\n";
237         }
238
239         my ($flags) = unpack("n", @packet[8] . @packet[9]);
240         my (@flags) = parse_afp_flags($flags);
241
242         print "Flags: ";
243         print "$flags - " if ($main::debug);
244         print join(",", @flags) . "\n";
245
246         # server name starts at offset+10, length byte first.
247
248         my ($servername_len) = unpack("C1", @packet[10]);
249         my ($servername) = join("", @packet[11..(11+$servername_len-1)]);
250         print "Server name length: $servername_len\n" if ($main::debug);
251         print "Server name: $servername\n";
252
253         my ($offset) = 11 + $servername_len;
254
255         # quietly ++ the $offset to account for the padding that happens
256         # in the reply packet if the field names don't align on an even boundary
257
258         $offset++ if ($servername_len % 2 == 0);
259
260         print "New offset: $offset\n" if ($main::debug);
261
262         my ($signature_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
263         print "Signature offset: $signature_offset\n" if ($main::debug);
264         if ($signature_offset)
265         {
266                 my ($signature) = join("",  @packet[$signature_offset..$signature_offset+15]);
267
268                 print "Signature:\n";   
269                 hexdump($signature);
270         }
271
272         my ($network_address_count_offset) = unpack("n2", @packet[$offset+2] . @packet[$offset+3]);
273         print "Network address count offset: $network_address_count_offset\n" if ($main::debug);
274
275         extract_network_address($network_address_count_offset, @packet);
276
277         $offset += 4;
278         if ($flags & (1<<8)) { # Supports directory services
279                 my ($directory_service_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
280                 print "Directory service offset: $directory_service_offset\n" if ($main::debug);
281                 if ($directory_service_offset)
282                 {
283                         my (@dirsvcs) = extract(0, $directory_service_offset, @packet);
284                         while (@dirsvcs)
285                         {
286                                 printf "Directory Service: %s\n", shift @dirsvcs;
287                         }
288                 }
289                 $offset +=2;
290         }
291
292         if ($flags & (1<<9)) { # Supports UTF8 servername
293                 my ($utf8_name_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
294                 print "UTF8 name offset: $utf8_name_offset\n" if ($main::debug);
295                 if ($utf8_name_offset)
296                 {
297                         my ($utf8name) = extract(1, $utf8_name_offset+1, @packet);
298                         print "UTF8 Servername: $utf8name\n";
299                 }
300         }
301
302         draw_icon($icon_offset, @packet) if ($main::show_icon && $icon_offset);
303
304         return $allow_guest;
305 }
306
307 # getsrvbyname .. sorta ..
308
309 sub getasipsrv 
310 {
311         my ($what, $code) = @_;
312
313         if ($what eq "flags") 
314         {
315                 return "Request" if ($code == 0);
316                 return "Reply" if ($code == 1); 
317         }
318
319         if ($what eq "command") 
320         {
321                 return "DSICloseSession" if ($code == 1);
322                 return "DSICommand" if ($code == 2);
323                 return "DSIGetStatus" if ($code == 3);
324                 return "DSIOpenSession" if ($code == 4);
325                 return "DSITickle" if ($code == 5);
326                 return "DSIWrite" if ($code == 6);
327                 return "DSIAttention" if ($code == 7);
328         }
329         return "[$what/$code] unknown";
330 }
331
332
333 # return "counted" data at @packet[$offset]
334 # when called with a zero as the first argument, this will
335 # look in the packet for the count.  Otherwise, it will
336 # assume I know what I'm doing.  (hah, what a foolish function..)
337
338 sub extract 
339 {
340         my ($count, $offset, @packet) = @_;
341         my ($i, $j);
342         my (@items, $data);
343         my ($hack);
344
345         if ($count == 0)
346         {
347                 ($count) = unpack("C", @packet[$offset]);
348                 return if ($count == 0);
349                 $offset++;
350         }
351         else
352         {
353                 $hack = 1;
354         }       
355         #print ">> extracting $count items from offset $offset\n";
356         for ($i=0;$i<$count;$i++)
357         {
358                 #print "Working on count $i\n";
359                 my ($len) = unpack("C1", @packet[$offset]);
360                 $data = join("",  @packet[$offset+1..$offset+$len]);
361                 #print "$i. [$data] ($len)\n";
362                 push (@items, $data);
363                 $offset = $offset + $len + 1;
364                 #print "new offset is $offset\n";
365         }
366         return $data if ($hack);
367         return @items;
368 }
369
370 sub draw_icon
371 {
372         my ($offset, @packet) = @_;
373         my ($cols);
374         my ($i, $j);
375
376         # icons are 32x32 bitmaps; 128 byte icon + 128 byte mask
377         # to show the mask, change 128 to 256.  
378
379         for ($i=0;$i<128;$i++)
380         {
381                 my ($c) = @packet[$i+$offset];
382                 my ($bin) = unpack ("B*", $c);
383
384                 for ($j=0;$j<8;$j++)
385                 {
386                         if (substr($bin, $j, 1))
387                         {
388                                 print "#";
389                         }
390                         else
391                         {
392                                 print " ";
393                         }
394                 }       
395                 $cols++;
396                 if ($cols == 4)
397                 {
398                         $cols = 0;
399                         print "\n";
400                 }
401
402         }
403         print "\n";
404 }
405
406
407 sub parse_afp_flags
408 {
409         my ($flags) = shift @_;
410         my (@flags);
411
412         # $flags is a 16 bit little-endian number
413
414         push (@flags, "SupportsCopyFile") if ($flags & (1<<0));
415         push (@flags, "SupportsChgPwd") if ($flags & (1<<1));
416         push (@flags, "DontAllowSavePwd") if ($flags & (1<<2));
417         push (@flags, "SupportsServerMessages") if ($flags & (1<<3));
418         push (@flags, "SupportsServerSignature") if ($flags & (1<<4));
419         push (@flags, "SupportsTCP/IP") if ($flags & (1<<5));
420         push (@flags, "SupportsSrvrNotifications") if ($flags & (1<<6));
421         push (@flags, "SupportsReconnect") if ($flags & (1<<7));
422         push (@flags, "SupportsOpenDirectory") if ($flags & (1<<8));
423         push (@flags, "SupportsUTF8Servername") if ($flags & (1<<9));
424         push (@flags, "SupportsUUIDs") if ($flags & (1<<10));
425         push (@flags, "SupportsSuperClient") if ($flags & (1<<15));
426
427         return @flags;
428 }
429
430
431 sub hexdump
432 {
433         my ($buf) = @_;
434         my ($p, $c, $pc, $str);
435         my ($i);
436
437         for ($i=0;$i<length($buf);$i++)
438         {
439                 $p = substr($buf, $i, 1);
440                 $c = ord ($p);
441                 printf "%.2x ", $c;
442                 $pc++;
443                 if (($c > 31) && ($c < 127))
444                 {
445                         $str .= $p;
446                 }
447                 else
448                 {
449                         $str .= ".";
450                 }       
451                 if ($pc == 16)
452                 {
453                         print " $str\n";
454                         undef $str;
455                         $pc = 0;
456                 }       
457         }
458         print "   " x (16 - $pc);
459         print " $str \n";
460 }
461
462
463 sub extract_network_address
464 {
465         my ($offset, @packet) = @_;
466         my ($count);
467         my ($i) = 0;
468         my ($data);
469
470         # get the number of addresses
471         ($count) = unpack("C", @packet[$offset]);
472         return if ($count == 0);
473         $offset++;
474
475         #print "\n>> extracting $count items from offset $offset\n";
476         for ($i=0;$i<$count;$i++)
477         {
478                 #print "Working on count $i\n";
479                 my ($len) = unpack("C1", @packet[$offset]);
480                 #printf "len:  %u\n",$len;
481                 my ($type) = unpack("C1", @packet[$offset+1]);
482                 #printf "type: %u\n",$type;
483                 $data = join("",  @packet[$offset+2..$offset+$len-1]);
484                 #print "$i. [$data] ($len)\n";
485                 $offset = $offset + $len ;
486                 #print "new offset is $offset\n";
487
488
489                 # 1st byte is 'tag'
490                 # 1 - IP address; 4 bytes
491                 # 2 - IP address (4) + port (2)
492                 # 3 - DDP (2 bytes net, 1 byte node, 1 byte socket)
493                 # 4 - DNS address
494                 # 5 - IP address (4) + port (2), for SSH tunnel
495                 # 6 - IPV6 address (16)
496                 # 7 - IPV6 address (16) + port (2)
497
498                 my (@nap) = unpack("C*", $data);
499
500                 if ($type == 1)
501                 {
502                         # quad
503                         my ($ip) = sprintf "%d.%d.%d.%d (TCP/IP address)",
504                                 $nap[0], $nap[1], @nap[2], @nap[3];
505         
506                         print "Network address: $ip\n";
507                 }
508                 elsif ($type == 2)
509                 {
510                         # quad+port
511                         my ($ipport) = sprintf "%d.%d.%d.%d:%d",
512                                 @nap[0], @nap[1], @nap[2], @nap[3], (@nap[4]*256 + @nap[5]);
513                         print "Network address: $ipport (TCP/IP address and port)\n";
514                 }
515                 elsif ($type == 3)
516                 {
517                         printf "Network address: %d.%d (ddp address)\n",
518                                 (@nap[0] * 256) + @nap[1], @nap[2];
519                 }
520                 elsif ($type == 4)
521                 {
522                         print "Network address: $data (DNS address)\n";
523                 }
524                 elsif ($type == 5)
525                 {
526                         # according to the specs this should be an IP address
527                         # however, OSX Server uses the FQDN instead
528                         print "Network address: $data (SSH tunnel address)\n";
529                 }
530                 elsif ($type == 6 || $type == 7)
531                 {
532                         print "Network address: ";
533                         my ($j) = 0;
534                         for ( $j = 0; $j<=13; $j = $j+2) {
535                                 printf("%.2x%.2x:", @nap[$j], @nap[$j+1]);
536                         }
537                         printf("%.2x%.2x", @nap[14], @nap[15]);
538                         if ($type == 7 ) {
539                                 printf(":%d", (@nap[16]*256) + @nap[17]);
540                                 print " (IPv6 address + port)\n";
541                         }
542                         else {
543                                 print " (IPv6 address)\n";
544                         }
545                 }
546                 else 
547                 {
548                         printf "unsupported address type: %u\n", $type;
549                 }
550
551         }
552 }