]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-smb.c
Allow tcpdump to work with earlier libpcaps.
[tcpdump] / print-smb.c
index 9810cda90467946535b0a7ab104cc1c1d606af4f..85e2b43751bbe1c3750e460239a674d84f3ddb52 100644 (file)
@@ -9,6 +9,11 @@
 #include "config.h"
 #endif
 
+#ifndef lint
+static const char rcsid[] =
+     "@(#) $Header: /tcpdump/master/tcpdump/print-smb.c,v 1.7 2000-12-05 06:42:47 guy Exp $";
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -714,6 +719,8 @@ void nbt_tcp_print(const uchar *data,int length)
     printf("flags=0x%x\n", flags);
   case 0:    
     data = fdata(data,"NBT Session Packet\nFlags=[rw]\nLength=[rd]\n",data+4);
+    if (data == NULL)
+      break;
     if (memcmp(data,"\377SMB",4)==0) {
       if (nbt_len>PTR_DIFF(maxbuf,data))
        printf("WARNING: Short packet. Try increasing the snap length (%ld)\n",
@@ -784,15 +791,14 @@ void nbt_udp137_print(const uchar *data, int length)
   int ancount = RSVAL(data,6);
   int nscount = RSVAL(data,8);
   int arcount = RSVAL(data,10);
-  char des[1024];
-  char *opcodestr="OPUNKNOWN";  
+  char *opcodestr;  
   const char *p;
 
   startbuf = data;
 
   if (maxbuf <= data) return;
 
-  strcpy(des,"\n>>> NBT UDP PACKET(137): ");
+  printf("\n>>> NBT UDP PACKET(137): ");
 
   switch (opcode) {
   case 0: opcodestr = "QUERY"; break;
@@ -801,27 +807,26 @@ void nbt_udp137_print(const uchar *data, int length)
   case 7: opcodestr = "WACK"; break;
   case 8: opcodestr = "REFRESH(8)"; break;
   case 9: opcodestr = "REFRESH"; break;
+  default: opcodestr = "OPUNKNOWN"; break;
   }
-  strcat(des,opcodestr);
+  printf("%s", opcodestr);
   if (response) {
     if (rcode)
-      strcat(des,"; NEGATIVE");
+      printf("; NEGATIVE");
     else
-      strcat(des,"; POSITIVE");
+      printf("; POSITIVE");
   }
     
   if (response) 
-    strcat(des,"; RESPONSE");
+    printf("; RESPONSE");
   else
-    strcat(des,"; REQUEST");
+    printf("; REQUEST");
 
   if (nm_flags&1)
-    strcat(des,"; BROADCAST");
+    printf("; BROADCAST");
   else
-    strcat(des,"; UNICAST");
+    printf("; UNICAST");
   
-  printf("%s", des);
-
   if (vflag == 0) return;
 
   printf("\nTrnID=0x%X\nOpCode=%d\nNmFlags=0x%X\nRcode=%d\nQueryCount=%d\nAnswerCount=%d\nAuthorityCount=%d\nAddressRecCount=%d\n",
@@ -842,6 +847,8 @@ void nbt_udp137_print(const uchar *data, int length)
       printf("QuestionRecords:\n");
       for (i=0;i<qdcount;i++)
        p = fdata(p,"|Name=[n1]\nQuestionType=[rw]\nQuestionClass=[rw]\n#",maxbuf);
+       if (p == NULL)
+         goto out;
     }
 
     if (total) {
@@ -850,30 +857,39 @@ void nbt_udp137_print(const uchar *data, int length)
        int rdlen;
        int restype;
        p = fdata(p,"Name=[n1]\n#",maxbuf);
+       if (p == NULL)
+         goto out;
        restype = RSVAL(p,0);
        p = fdata(p,"ResType=[rw]\nResClass=[rw]\nTTL=[rD]\n",p+8);
+       if (p == NULL)
+         goto out;
        rdlen = RSVAL(p,0);
        printf("ResourceLength=%d\nResourceData=\n",rdlen);
        p += 2;
        if (rdlen == 6) {
          p = fdata(p,"AddrType=[rw]\nAddress=[b.b.b.b]\n",p+rdlen);
+         if (p == NULL)
+           goto out;
        } else {
          if (restype == 0x21) {
            int numnames = CVAL(p,0);
            p = fdata(p,"NumNames=[B]\n",p+1);
+           if (p == NULL)
+             goto out;
            while (numnames--) {
-             char flags[128]="";
              p = fdata(p,"Name=[n2]\t#",maxbuf);
-             if (p[0] & 0x80) strcat(flags,"<GROUP> ");
-             if ((p[0] & 0x60) == 0x00) strcat(flags,"B ");
-             if ((p[0] & 0x60) == 0x20) strcat(flags,"P ");
-             if ((p[0] & 0x60) == 0x40) strcat(flags,"M ");
-             if ((p[0] & 0x60) == 0x60) strcat(flags,"_ ");
-             if (p[0] & 0x10) strcat(flags,"<DEREGISTERING> ");
-             if (p[0] & 0x08) strcat(flags,"<CONFLICT> ");
-             if (p[0] & 0x04) strcat(flags,"<ACTIVE> ");
-             if (p[0] & 0x02) strcat(flags,"<PERMANENT> ");
-             printf("%s\n",flags);
+             if (p[0] & 0x80) printf("<GROUP> ");
+             switch (p[0] & 0x60) {
+             case 0x00: printf("B "); break;
+             case 0x20: printf("P "); break;
+             case 0x40: printf("M "); break;
+             case 0x60: printf("_ "); break;
+             }
+             if (p[0] & 0x10) printf("<DEREGISTERING> ");
+             if (p[0] & 0x08) printf("<CONFLICT> ");
+             if (p[0] & 0x04) printf("<ACTIVE> ");
+             if (p[0] & 0x02) printf("<PERMANENT> ");
+             printf("\n");
              p += 2;
            }
          } else {
@@ -889,6 +905,7 @@ void nbt_udp137_print(const uchar *data, int length)
     fdata(p,"AdditionalData:\n",maxbuf);    
   }      
   
+out:
   printf("\n");
   fflush(stdout);
 }
@@ -906,7 +923,8 @@ void nbt_udp138_print(const uchar *data, int length)
 
   data = fdata(data,"\n>>> NBT UDP PACKET(138) Res=[rw] ID=[rw] IP=[b.b.b.b] Port=[rd] Length=[rd] Res2=[rw]\nSourceName=[n1]\nDestName=[n1]\n#",maxbuf);
 
-  print_smb(data,maxbuf);
+  if (data != NULL)
+    print_smb(data,maxbuf);
   
   printf("\n");
   fflush(stdout);
@@ -917,15 +935,24 @@ void nbt_udp138_print(const uchar *data, int length)
 /*
    print netbeui frames 
 */
-void netbeui_print(const uchar *data, const uchar *maxbuf)
+void netbeui_print(u_short control, const uchar *data, const uchar *maxbuf)
 {
-  int len = SVAL(data,1);
-  int command = CVAL(data,5);
-  const uchar *data2 = data + 1 + len;
+  int len = SVAL(data,0);
+  int command = CVAL(data,4);
+  const uchar *data2 = data + len;
+  int is_truncated = 0;
+
+  if (data2 >= maxbuf) {
+    data2 = maxbuf;
+    is_truncated = 1;
+  }
 
   startbuf = data;
 
-  data = fdata(data,"\n>>> NetBeui Packet\nType=[B] Length=[d] Signature=[w] Command=[B]\n#",maxbuf);
+  printf("\n>>> NetBeui Packet\nType=0x%X ", control);
+  data = fdata(data,"Length=[d] Signature=[w] Command=[B]\n#",maxbuf);
+  if (data == NULL)
+    goto out;
 
   switch (command) {
   case 0xA: 
@@ -960,16 +987,29 @@ void netbeui_print(const uchar *data, const uchar *maxbuf)
     data = fdata(data,"SessionEnd:\n[P1]Data2=[w][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",data2);
     break;
 
+  case 0x1f:
+    data = fdata(data,"SessionAlive\n",data2);
+    break;
+
   default:
     data = fdata(data,"Unknown Netbios Command ",data2);
     break;
   }
+  if (data == NULL)
+    goto out;
+
+  if (is_truncated) {
+    /* data2 was past the end of the buffer */
+    goto out;
+  }
 
   if (memcmp(data2,"\377SMB",4)==0) {
     print_smb(data2,maxbuf);
   } else {
     int i;
     for (i=0;i<128;i++) {
+      if (&data2[i] >= maxbuf)
+        break;
       if (memcmp(&data2[i],"\377SMB",4)==0) {
        printf("found SMB packet at %d\n", i);
        print_smb(&data2[i],maxbuf);
@@ -978,6 +1018,7 @@ void netbeui_print(const uchar *data, const uchar *maxbuf)
     }
   }
 
+out:
   printf("\n");
 }
 
@@ -993,7 +1034,8 @@ void ipx_netbios_print(const uchar *data, const uchar *maxbuf)
   for (i=0;i<128;i++)
     if (memcmp(&data[i],"\377SMB",4)==0) {
       fdata(data,"\n>>> IPX transport ",&data[i]);
-      print_smb(&data[i],maxbuf);
+      if (data != NULL)
+       print_smb(&data[i],maxbuf);
       printf("\n");
       fflush(stdout);
       break;