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.
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
12 # - http://developer.apple.com/techpubs/macos8/NetworkCommSvcs/AppleShare/
13 # - http://www2.opendoor.com/asip/ (excellent Mac sharing / security site)
15 # todo: log in as guest & get a list of shares
19 # This edition is a part of netatalk @NETATALK_VERSION@.
23 use IO::Socket; # sucks because Timeout doesn't
30 while ($arg = shift @ARGV)
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 !~ /^-/);
41 if ($main::showver ==1)
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";
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";
61 ($host, $port) = split(/\:/, $hostport);
62 $port = "548" if ($port eq "");
64 my ($packet) = build_packet();
65 my ($code) = sendpacket($host, $port, $packet);
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
82 my ($packet) = pack("C*", @packet);
90 my ($host, $port, $packet) = @_;
93 print "opening $host:$port\n" if ($main::debug);
95 my ($asip_sock) = IO::Socket::INET->new(
101 ) || die "connect to $host failure: $!";
102 $asip_sock->autoflush(1);
104 print "sending packet\n" if ($main::debug);
106 my ($count) = syswrite($asip_sock, $packet, length($packet));
108 if ($count != length($packet))
110 print "only wrote $count of " . length($packet) . " bytes?\n";
114 # reply can span multiple packets
116 print "sysread: " if ($main::debug);
117 while (sysread($asip_sock, $b, 256))
120 print "." if ($main::debug);
125 print " read " . length($buf) . " bytes\n" if ($main::debug);
127 if (length($buf) == 0)
129 print "empty reply packet?\n";
134 print "AFP reply from $host:$port\n";
135 return (parse_packet($buf));
142 my ($buf) = shift @_;
146 hexdump($buf) if ($main::hexdump);
148 for ($i=0;$i<length($buf);$i++)
150 push(@packet, substr($buf, $i, 1));
153 my ($flags) = unpack("C", @packet[0]);
154 my ($cmd) = unpack("C", @packet[1]);
156 my ($request_id) = unpack("n", @packet[2] . @packet[3]);
157 print "Flags: $flags Cmd: $cmd ID: $request_id\n";
159 print getasipsrv("flags", $flags) . ": " . getasipsrv("command", $cmd) . "\n";
160 print "Request ID: $request_id\n";
162 print "** Request ID didn't match what we sent!\n" if ($request_id != 0xdead);
164 # "Error Code / Enclosed Data Offset"
165 # I have never seen this be non-zero ..
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);
170 # "Total Data Length"
172 my ($datalen) = unpack("N2", @packet[8] . @packet[9] . @packet[10] . @packet[11]);
174 print "Total data length: $datalen\n" if ($main::debug);
178 my ($reserved) = unpack("N2", @packet[12] . @packet[13] . @packet[14] . @packet[15]);
180 print "Reserved field: $reserved\n" if ($reserved);
184 print "I can only parse packets of reply-type DSIGetStatus (3)\n";
185 print "This is reply-type " . getasipsrv("command", $cmd) . "\n";
189 print "No data in packet?\n";
191 if (($datalen > 0) && ($cmd == 3))
193 my (@AFPpacket) = @packet[($edo+16)..($edo+16+$datalen)];
194 return (parse_FPGetSrvrInfo(@AFPpacket));
198 print "I don't know how to parse this type of packet.\n";
205 sub parse_FPGetSrvrInfo()
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";
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";
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";
225 my ($allow_guest) = 0;
226 $allow_guest = 1 if (grep(/No User Authen/, @uams));
228 # it would be cute to see the icon.
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: exist\n" if ($icon_offset);
234 my ($flags) = unpack("n", @packet[8] . @packet[9]);
235 my (@flags) = parse_afp_flags($flags);
238 print "$flags - " if ($main::debug);
239 print join(",", @flags) . "\n";
241 # server name starts at offset+10, length byte first.
243 my ($servername_len) = unpack("C1", @packet[10]);
244 my ($servername) = join("", @packet[11..(11+$servername_len-1)]);
245 print "Server name length: $servername_len\n" if ($main::debug);
246 print "Server name: $servername\n";
248 my ($offset) = 11 + $servername_len;
250 # quietly ++ the $offset to account for the padding that happens
251 # in the reply packet if the field names don't align on an even boundary
253 $offset++ if ($servername_len % 2 == 0);
255 print "New offset: $offset\n" if ($main::debug);
257 my ($signature_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
258 print "Signature offset: $signature_offset\n" if ($main::debug);
259 if ($signature_offset)
261 my ($signature) = join("", @packet[$signature_offset..$signature_offset+15]);
263 print "Signature:\n";
267 my ($network_address_count_offset) = unpack("n2", @packet[$offset+2] . @packet[$offset+3]);
268 print "Network address count offset: $network_address_count_offset\n" if ($main::debug);
270 extract_network_address($network_address_count_offset, @packet);
273 if ($flags & (1<<8)) { # Supports directory services
274 my ($directory_service_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
275 print "Directory service offset: $directory_service_offset\n" if ($main::debug);
276 if ($directory_service_offset)
278 my (@dirsvcs) = extract(0, $directory_service_offset, @packet);
281 printf "Directory Service: %s\n", shift @dirsvcs;
287 if ($flags & (1<<9)) { # Supports UTF8 servername
288 my ($utf8_name_offset) = unpack("n2", @packet[$offset] . @packet[$offset+1]);
289 print "UTF8 name offset: $utf8_name_offset\n" if ($main::debug);
290 if ($utf8_name_offset)
292 my ($utf8name) = extract(1, $utf8_name_offset+1, @packet);
293 print "UTF8 Servername: $utf8name\n";
297 draw_icon($icon_offset, @packet) if ($main::show_icon && $icon_offset);
302 # getsrvbyname .. sorta ..
306 my ($what, $code) = @_;
308 if ($what eq "flags")
310 return "Request" if ($code == 0);
311 return "Reply" if ($code == 1);
314 if ($what eq "command")
316 return "DSICloseSession" if ($code == 1);
317 return "DSICommand" if ($code == 2);
318 return "DSIGetStatus" if ($code == 3);
319 return "DSIOpenSession" if ($code == 4);
320 return "DSITickle" if ($code == 5);
321 return "DSIWrite" if ($code == 6);
322 return "DSIAttention" if ($code == 7);
324 return "[$what/$code] unknown";
328 # return "counted" data at @packet[$offset]
329 # when called with a zero as the first argument, this will
330 # look in the packet for the count. Otherwise, it will
331 # assume I know what I'm doing. (hah, what a foolish function..)
335 my ($count, $offset, @packet) = @_;
342 ($count) = unpack("C", @packet[$offset]);
343 return if ($count == 0);
350 #print ">> extracting $count items from offset $offset\n";
351 for ($i=0;$i<$count;$i++)
353 #print "Working on count $i\n";
354 my ($len) = unpack("C1", @packet[$offset]);
355 $data = join("", @packet[$offset+1..$offset+$len]);
356 #print "$i. [$data] ($len)\n";
357 push (@items, $data);
358 $offset = $offset + $len + 1;
359 #print "new offset is $offset\n";
361 return $data if ($hack);
367 my ($offset, @packet) = @_;
371 # icons are 32x32 bitmaps; 128 byte icon + 128 byte mask
372 # to show the mask, change 128 to 256.
374 for ($i=0;$i<128;$i++)
376 my ($c) = @packet[$i+$offset];
377 my ($bin) = unpack ("B*", $c);
381 if (substr($bin, $j, 1))
404 my ($flags) = shift @_;
407 # $flags is a 16 bit little-endian number
409 push (@flags, "SupportsCopyFile") if ($flags & (1<<0));
410 push (@flags, "SupportsChgPwd") if ($flags & (1<<1));
411 push (@flags, "DontAllowSavePwd") if ($flags & (1<<2));
412 push (@flags, "SupportsServerMessages") if ($flags & (1<<3));
413 push (@flags, "SupportsServerSignature") if ($flags & (1<<4));
414 push (@flags, "SupportsTCP/IP") if ($flags & (1<<5));
415 push (@flags, "SupportsSrvrNotifications") if ($flags & (1<<6));
416 push (@flags, "SupportsReconnect") if ($flags & (1<<7));
417 push (@flags, "SupportsOpenDirectory") if ($flags & (1<<8));
418 push (@flags, "SupportsUTF8Servername") if ($flags & (1<<9));
419 push (@flags, "SupportsUUIDs") if ($flags & (1<<10));
420 push (@flags, "SupportsSuperClient") if ($flags & (1<<15));
429 my ($p, $c, $pc, $str);
432 for ($i=0;$i<length($buf);$i++)
434 $p = substr($buf, $i, 1);
438 if (($c > 31) && ($c < 127))
453 print " " x (16 - $pc);
458 sub extract_network_address
460 my ($offset, @packet) = @_;
465 # get the number of addresses
466 ($count) = unpack("C", @packet[$offset]);
467 return if ($count == 0);
470 #print "\n>> extracting $count items from offset $offset\n";
471 for ($i=0;$i<$count;$i++)
473 #print "Working on count $i\n";
474 my ($len) = unpack("C1", @packet[$offset]);
475 #printf "len: %u\n",$len;
476 my ($type) = unpack("C1", @packet[$offset+1]);
477 #printf "type: %u\n",$type;
478 $data = join("", @packet[$offset+2..$offset+$len-1]);
479 #print "$i. [$data] ($len)\n";
480 $offset = $offset + $len ;
481 #print "new offset is $offset\n";
485 # 1 - IP address; 4 bytes
486 # 2 - IP address (4) + port (2)
487 # 3 - DDP (2 bytes net, 1 byte node, 1 byte socket)
489 # 5 - IP address (4) + port (2), for SSH tunnel
490 # 6 - IPV6 address (16)
491 # 7 - IPV6 address (16) + port (2)
493 my (@nap) = unpack("C*", $data);
498 my ($ip) = sprintf "%d.%d.%d.%d (TCP/IP address)",
499 $nap[0], $nap[1], @nap[2], @nap[3];
501 print "Network address: $ip\n";
506 my ($ipport) = sprintf "%d.%d.%d.%d:%d",
507 @nap[0], @nap[1], @nap[2], @nap[3], (@nap[4]*256 + @nap[5]);
508 print "Network address: $ipport (TCP/IP address and port)\n";
512 printf "Network address: %d.%d (ddp address)\n",
513 (@nap[0] * 256) + @nap[1], @nap[2];
517 print "Network address: $data (DNS address)\n";
521 # according to the specs this should be an IP address
522 # however, OSX Server uses the FQDN instead
523 print "Network address: $data (SSH tunnel address)\n";
525 elsif ($type == 6 || $type == 7)
527 print "Network address: ";
529 for ( $j = 0; $j<=13; $j = $j+2) {
530 printf("%.2x%.2x:", @nap[$j], @nap[$j+1]);
532 printf("%.2x%.2x", @nap[14], @nap[15]);
534 printf(":%d", (@nap[16]*256) + @nap[17]);
535 print " (IPv6 address + port)\n";
538 print " (IPv6 address)\n";
543 printf "unsupported address type: %u\n", $type;