]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-smb.c
Make it clearer that the hex or hex-and-ASCII dump for "-x", "-xx",
[tcpdump] / print-smb.c
index 573e7c80d7bc7febe792170c68b5d85b793cb35f..8f262019610b8503d11bf3d9069b0fde0aacd7dd 100644 (file)
@@ -12,7 +12,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-     "@(#) $Header: /tcpdump/master/tcpdump/print-smb.c,v 1.40 2004-12-29 03:10:24 guy Exp $";
+     "@(#) $Header: /tcpdump/master/tcpdump/print-smb.c,v 1.41.2.2 2005-05-08 20:01:09 guy Exp $";
 #endif
 
 #include <tcpdump-stdinc.h>
@@ -794,7 +794,10 @@ static struct smbfns smb_fns[] = {
 static void
 print_smb(const u_char *buf, const u_char *maxbuf)
 {
+    u_int16_t flags2;
+    int nterrcodes;
     int command;
+    u_int32_t nterror;
     const u_char *words, *maxwords, *data;
     struct smbfns *fn;
     const char *fmt_smbheader =
@@ -803,7 +806,9 @@ print_smb(const u_char *buf, const u_char *maxbuf)
 
     TCHECK(buf[9]);
     request = (buf[9] & 0x80) ? 0 : 1;
-    unicodestr = EXTRACT_LE_16BITS(&buf[10]) & 0x8000;
+    flags2 = EXTRACT_LE_16BITS(&buf[10]);
+    unicodestr = flags2 & 0x8000;
+    nterrcodes = flags2 & 0x4000;
     startbuf = buf;
 
     command = buf[4];
@@ -821,8 +826,14 @@ print_smb(const u_char *buf, const u_char *maxbuf)
     /* print out the header */
     smb_fdata(buf, fmt_smbheader, buf + 33, unicodestr);
 
-    if (buf[5])
-       printf("SMBError = %s\n", smb_errstr(buf[5], EXTRACT_LE_16BITS(&buf[7])));
+    if (nterrcodes) {
+       nterror = EXTRACT_LE_32BITS(&buf[5]);
+       if (nterror)
+           printf("NTError = %s\n", nt_errstr(nterror));
+    } else {
+       if (buf[5])
+           printf("SMBError = %s\n", smb_errstr(buf[5], EXTRACT_LE_16BITS(&buf[7])));
+    }
 
     smboffset = 32;
 
@@ -914,17 +925,25 @@ trunc:
 void
 nbt_tcp_print(const u_char *data, int length)
 {
-    const u_char *maxbuf = data + length;
+    int caplen;
     int type;
     u_int nbt_len;
+    const u_char *maxbuf;
 
-    TCHECK2(data[2], 2);
+    if (length < 4)
+       goto trunc;
+    if (snapend < data)
+       goto trunc;
+    caplen = snapend - data;
+    if (caplen < 4)
+       goto trunc;
+    maxbuf = data + caplen;
     type = data[0];
     nbt_len = EXTRACT_16BITS(data + 2);
+    length -= 4;
+    caplen -= 4;
 
     startbuf = data;
-    if (maxbuf <= data)
-       return;
 
     if (vflag < 2) {
        printf(" NBT Session Packet: ");
@@ -945,7 +964,12 @@ nbt_tcp_print(const u_char *data, int length)
          {
            int ecode;
 
-           TCHECK(data[4]);
+           if (nbt_len < 4)
+               goto trunc;
+           if (length < 4)
+               goto trunc;
+           if (caplen < 4)
+               goto trunc;
            ecode = data[4];
 
            printf("Session Reject, ");
@@ -985,13 +1009,17 @@ nbt_tcp_print(const u_char *data, int length)
                data + 4, 0);
            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 (%lu)\n",
-                       (unsigned long)PTR_DIFF(maxbuf, data));
+           if (nbt_len >= 4 && caplen >= 4 && memcmp(data,"\377SMB",4) == 0) {
+               if ((int)nbt_len > caplen) {
+                   if ((int)nbt_len > length)
+                       printf("WARNING: Packet is continued in later TCP segments\n");
+                   else
+                       printf("WARNING: Short packet. Try increasing the snap length by %d\n",
+                           nbt_len - caplen);
+               }
                print_smb(data, maxbuf > data + nbt_len ? data + nbt_len : maxbuf);
            } else
-               printf("Session packet:(raw data?)\n");
+               printf("Session packet:(raw data or continuation?)\n");
            break;
 
        case 0x81:
@@ -1006,29 +1034,33 @@ nbt_tcp_print(const u_char *data, int length)
 
        case 0x83:
          {
+           const u_char *origdata;
            int ecode;
 
-           TCHECK(data[4]);
-           ecode = data[4];
-
+           origdata = data;
            data = smb_fdata(data, "[P1]NBT SessionReject\nFlags=[B]\nLength=[rd]\nReason=[B]\n",
                maxbuf, 0);
-           switch (ecode) {
-           case 0x80:
-               printf("Not listening on called name\n");
-               break;
-           case 0x81:
-               printf("Not listening for calling name\n");
-               break;
-           case 0x82:
-               printf("Called name not present\n");
-               break;
-           case 0x83:
-               printf("Called name present, but insufficient resources\n");
-               break;
-           default:
-               printf("Unspecified error 0x%X\n", ecode);
+           if (data == NULL)
                break;
+           if (nbt_len >= 1 && caplen >= 1) {
+               ecode = origdata[4];
+               switch (ecode) {
+               case 0x80:
+                   printf("Not listening on called name\n");
+                   break;
+               case 0x81:
+                   printf("Not listening for calling name\n");
+                   break;
+               case 0x82:
+                   printf("Called name not present\n");
+                   break;
+               case 0x83:
+                   printf("Called name present, but insufficient resources\n");
+                   break;
+               default:
+                   printf("Unspecified error 0x%X\n", ecode);
+                   break;
+               }
            }
          }
            break;
@@ -1130,12 +1162,13 @@ nbt_udp137_print(const u_char *data, int length)
 
     if (qdcount) {
        printf("QuestionRecords:\n");
-       for (i = 0; i < qdcount; i++)
+       for (i = 0; i < qdcount; i++) {
            p = smb_fdata(p,
                "|Name=[n1]\nQuestionType=[rw]\nQuestionClass=[rw]\n#",
                maxbuf, 0);
-       if (p == NULL)
-           goto out;
+           if (p == NULL)
+               goto out;
+       }
     }
 
     if (total) {
@@ -1169,6 +1202,8 @@ nbt_udp137_print(const u_char *data, int length)
                        goto out;
                    while (numnames--) {
                        p = smb_fdata(p, "Name=[n2]\t#", maxbuf, 0);
+                       if (p == NULL)
+                           goto out;
                        TCHECK(*p);
                        if (p[0] & 0x80)
                            printf("<GROUP> ");