]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-smb.c
This commit was generated by cvs2svn to compensate for changes in r123109,
[FreeBSD/FreeBSD.git] / contrib / tcpdump / print-smb.c
1 /*
2  * Copyright (C) Andrew Tridgell 1995-1999
3  *
4  * This software may be distributed either under the terms of the
5  * BSD-style license that accompanies tcpdump or the GNU GPL version 2
6  * or later
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifndef lint
14 static const char rcsid[] =
15      "@(#) $Header: /tcpdump/master/tcpdump/print-smb.c,v 1.20.2.4 2002/07/11 07:47:01 guy Exp $";
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21
22 #include <netinet/in.h>
23
24 #include "interface.h"
25 #include "extract.h"
26 #include "smb.h"
27
28 static int request = 0;
29
30 const u_char *startbuf = NULL;
31
32 struct smbdescript {
33     const char *req_f1;
34     const char *req_f2;
35     const char *rep_f1;
36     const char *rep_f2;
37     void (*fn)(const u_char *, const u_char *, const u_char *, const u_char *);
38 };
39
40 struct smbdescriptint {
41     const char *req_f1;
42     const char *req_f2;
43     const char *rep_f1;
44     const char *rep_f2;
45     void (*fn)(const u_char *, const u_char *, int, int);
46 };
47
48 struct smbfns
49 {
50     int id;
51     const char *name;
52     int flags;
53     struct smbdescript descript;
54 };
55
56 struct smbfnsint
57 {
58     int id;
59     const char *name;
60     int flags;
61     struct smbdescriptint descript;
62 };
63
64 #define DEFDESCRIPT     { NULL, NULL, NULL, NULL, NULL }
65
66 #define FLG_CHAIN       (1 << 0)
67
68 static struct smbfns *
69 smbfind(int id, struct smbfns *list)
70 {
71     int sindex;
72
73     for (sindex = 0; list[sindex].name; sindex++)
74         if (list[sindex].id == id)
75             return(&list[sindex]);
76
77     return(&list[0]);
78 }
79
80 static struct smbfnsint *
81 smbfindint(int id, struct smbfnsint *list)
82 {
83     int sindex;
84
85     for (sindex = 0; list[sindex].name; sindex++)
86         if (list[sindex].id == id)
87             return(&list[sindex]);
88
89     return(&list[0]);
90 }
91
92 static void
93 trans2_findfirst(const u_char *param, const u_char *data, int pcnt, int dcnt)
94 {
95     const char *fmt;
96
97     if (request)
98         fmt = "Attribute=[A]\nSearchCount=[d]\nFlags=[w]\nLevel=[dP5]\nFile=[S]\n";
99     else
100         fmt = "Handle=[w]\nCount=[d]\nEOS=[w]\nEoffset=[d]\nLastNameOfs=[w]\n";
101
102     smb_fdata(param, fmt, param + pcnt);
103     if (dcnt) {
104         printf("data:\n");
105         print_data(data, dcnt);
106     }
107 }
108
109 static void
110 trans2_qfsinfo(const u_char *param, const u_char *data, int pcnt, int dcnt)
111 {
112     static int level = 0;
113     const char *fmt="";
114
115     if (request) {
116         TCHECK2(*param, 2);
117         level = EXTRACT_LE_16BITS(param);
118         fmt = "InfoLevel=[d]\n";
119         smb_fdata(param, fmt, param + pcnt);
120     } else {
121         switch (level) {
122         case 1:
123             fmt = "idFileSystem=[W]\nSectorUnit=[D]\nUnit=[D]\nAvail=[D]\nSectorSize=[d]\n";
124             break;
125         case 2:
126             fmt = "CreationTime=[T2]VolNameLength=[B]\nVolumeLabel=[s12]\n";
127             break;
128         case 0x105:
129             fmt = "Capabilities=[W]\nMaxFileLen=[D]\nVolNameLen=[D]\nVolume=[S]\n";
130             break;
131         default:
132             fmt = "UnknownLevel\n";
133             break;
134         }
135         smb_fdata(data, fmt, data + dcnt);
136     }
137     if (dcnt) {
138         printf("data:\n");
139         print_data(data, dcnt);
140     }
141     return;
142 trunc:
143     printf("[|SMB]");
144     return;
145 }
146
147 struct smbfnsint trans2_fns[] = {
148     { 0, "TRANSACT2_OPEN", 0,
149         { "Flags2=[w]\nMode=[w]\nSearchAttrib=[A]\nAttrib=[A]\nTime=[T2]\nOFun=[w]\nSize=[D]\nRes=([w, w, w, w, w])\nPath=[S]",
150           NULL,
151           "Handle=[d]\nAttrib=[A]\nTime=[T2]\nSize=[D]\nAccess=[w]\nType=[w]\nState=[w]\nAction=[w]\nInode=[W]\nOffErr=[d]\n|EALength=[d]\n",
152           NULL, NULL }},
153     { 1, "TRANSACT2_FINDFIRST", 0,
154         { NULL, NULL, NULL, NULL, trans2_findfirst }},
155     { 2, "TRANSACT2_FINDNEXT", 0, DEFDESCRIPT },
156     { 3, "TRANSACT2_QFSINFO", 0,
157         { NULL, NULL, NULL, NULL, trans2_qfsinfo }},
158     { 4, "TRANSACT2_SETFSINFO", 0, DEFDESCRIPT },
159     { 5, "TRANSACT2_QPATHINFO", 0, DEFDESCRIPT },
160     { 6, "TRANSACT2_SETPATHINFO", 0, DEFDESCRIPT },
161     { 7, "TRANSACT2_QFILEINFO", 0, DEFDESCRIPT },
162     { 8, "TRANSACT2_SETFILEINFO", 0, DEFDESCRIPT },
163     { 9, "TRANSACT2_FSCTL", 0, DEFDESCRIPT },
164     { 10, "TRANSACT2_IOCTL", 0, DEFDESCRIPT },
165     { 11, "TRANSACT2_FINDNOTIFYFIRST", 0, DEFDESCRIPT },
166     { 12, "TRANSACT2_FINDNOTIFYNEXT", 0, DEFDESCRIPT },
167     { 13, "TRANSACT2_MKDIR", 0, DEFDESCRIPT },
168     { -1, NULL, 0, DEFDESCRIPT }
169 };
170
171
172 static void
173 print_trans2(const u_char *words, const u_char *dat, const u_char *buf, const u_char *maxbuf)
174 {
175     static struct smbfnsint *fn = &trans2_fns[0];
176     const u_char *data, *param;
177     const u_char *w = words + 1;
178     const char *f1 = NULL, *f2 = NULL;
179     int pcnt, dcnt;
180
181     TCHECK(words[0]);
182     if (request) {
183         TCHECK2(w[14 * 2], 2);
184         pcnt = EXTRACT_LE_16BITS(w + 9 * 2);
185         param = buf + EXTRACT_LE_16BITS(w + 10 * 2);
186         dcnt = EXTRACT_LE_16BITS(w + 11 * 2);
187         data = buf + EXTRACT_LE_16BITS(w + 12 * 2);
188         fn = smbfindint(EXTRACT_LE_16BITS(w + 14 * 2), trans2_fns);
189     } else {
190         if (words[0] == 0) {
191             printf("%s\n", fn->name);
192             printf("Trans2Interim\n");
193             return;
194         }
195         TCHECK2(w[7 * 2], 2);
196         pcnt = EXTRACT_LE_16BITS(w + 3 * 2);
197         param = buf + EXTRACT_LE_16BITS(w + 4 * 2);
198         dcnt = EXTRACT_LE_16BITS(w + 6 * 2);
199         data = buf + EXTRACT_LE_16BITS(w + 7 * 2);
200     }
201
202     printf("%s param_length=%d data_length=%d\n", fn->name, pcnt, dcnt);
203
204     if (request) {
205         if (words[0] == 8) {
206             smb_fdata(words + 1,
207                 "Trans2Secondary\nTotParam=[d]\nTotData=[d]\nParamCnt=[d]\nParamOff=[d]\nParamDisp=[d]\nDataCnt=[d]\nDataOff=[d]\nDataDisp=[d]\nHandle=[d]\n",
208                 maxbuf);
209             return;
210         } else {
211             smb_fdata(words + 1,
212                 "TotParam=[d]\nTotData=[d]\nMaxParam=[d]\nMaxData=[d]\nMaxSetup=[d]\nFlags=[w]\nTimeOut=[D]\nRes1=[w]\nParamCnt=[d]\nParamOff=[d]\nDataCnt=[d]\nDataOff=[d]\nSetupCnt=[d]\n",
213                 words + 1 + 14 * 2);
214             smb_fdata(data + 1, "TransactionName=[S]\n%", maxbuf);
215         }
216         f1 = fn->descript.req_f1;
217         f2 = fn->descript.req_f2;
218     } else {
219         smb_fdata(words + 1,
220             "TotParam=[d]\nTotData=[d]\nRes1=[w]\nParamCnt=[d]\nParamOff=[d]\nParamDisp[d]\nDataCnt=[d]\nDataOff=[d]\nDataDisp=[d]\nSetupCnt=[d]\n",
221             words + 1 + 10 * 2);
222         f1 = fn->descript.rep_f1;
223         f2 = fn->descript.rep_f2;
224     }
225
226     if (fn->descript.fn)
227         (*fn->descript.fn)(param, data, pcnt, dcnt);
228     else {
229         smb_fdata(param, f1 ? f1 : "Parameters=\n", param + pcnt);
230         smb_fdata(data, f2 ? f2 : "Data=\n", data + dcnt);
231     }
232     return;
233 trunc:
234     printf("[|SMB]");
235     return;
236 }
237
238
239 static void
240 print_browse(const u_char *param, int paramlen, const u_char *data, int datalen)
241 {
242     const u_char *maxbuf = data + datalen;
243     int command;
244
245     TCHECK(data[0]);
246     command = data[0];
247
248     smb_fdata(param, "BROWSE PACKET\n|Param ", param+paramlen);
249
250     switch (command) {
251     case 0xF:
252         data = smb_fdata(data,
253             "BROWSE PACKET:\nType=[B] (LocalMasterAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nElectionVersion=[w]\nBrowserConstant=[w]\n",
254             maxbuf);
255         break;
256
257     case 0x1:
258         data = smb_fdata(data,
259             "BROWSE PACKET:\nType=[B] (HostAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nElectionVersion=[w]\nBrowserConstant=[w]\n",
260             maxbuf);
261         break;
262
263     case 0x2:
264         data = smb_fdata(data,
265             "BROWSE PACKET:\nType=[B] (AnnouncementRequest)\nFlags=[B]\nReplySystemName=[S]\n",
266             maxbuf);
267         break;
268
269     case 0xc:
270         data = smb_fdata(data,
271             "BROWSE PACKET:\nType=[B] (WorkgroupAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nCommentPointer=[W]\nServerName=[S]\n",
272             maxbuf);
273         break;
274
275     case 0x8:
276         data = smb_fdata(data,
277             "BROWSE PACKET:\nType=[B] (ElectionFrame)\nElectionVersion=[B]\nOSSummary=[W]\nUptime=[(W, W)]\nServerName=[S]\n",
278             maxbuf);
279         break;
280
281     case 0xb:
282         data = smb_fdata(data,
283             "BROWSE PACKET:\nType=[B] (BecomeBackupBrowser)\nName=[S]\n",
284             maxbuf);
285         break;
286
287     case 0x9:
288         data = smb_fdata(data,
289             "BROWSE PACKET:\nType=[B] (GetBackupList)\nListCount?=[B]\nToken?=[B]\n",
290             maxbuf);
291         break;
292
293     case 0xa:
294         data = smb_fdata(data,
295             "BROWSE PACKET:\nType=[B] (BackupListResponse)\nServerCount?=[B]\nToken?=[B]*Name=[S]\n",
296             maxbuf);
297         break;
298
299     case 0xd:
300         data = smb_fdata(data,
301             "BROWSE PACKET:\nType=[B] (MasterAnnouncement)\nMasterName=[S]\n",
302             maxbuf);
303         break;
304
305     case 0xe:
306         data = smb_fdata(data,
307             "BROWSE PACKET:\nType=[B] (ResetBrowser)\nOptions=[B]\n", maxbuf);
308         break;
309
310     default:
311         data = smb_fdata(data, "Unknown Browser Frame ", maxbuf);
312         break;
313     }
314     return;
315 trunc:
316     printf("[|SMB]");
317     return;
318 }
319
320
321 static void
322 print_ipc(const u_char *param, int paramlen, const u_char *data, int datalen)
323 {
324     if (paramlen)
325         smb_fdata(param, "Command=[w]\nStr1=[S]\nStr2=[S]\n", param + paramlen);
326     if (datalen)
327         smb_fdata(data, "IPC ", data + datalen);
328 }
329
330
331 static void
332 print_trans(const u_char *words, const u_char *data1, const u_char *buf, const u_char *maxbuf)
333 {
334     const char *f1, *f2, *f3, *f4;
335     const u_char *data, *param;
336     const u_char *w = words + 1;
337     int datalen, paramlen;
338
339     if (request) {
340         TCHECK2(w[12 * 2], 2);
341         paramlen = EXTRACT_LE_16BITS(w + 9 * 2);
342         param = buf + EXTRACT_LE_16BITS(w + 10 * 2);
343         datalen = EXTRACT_LE_16BITS(w + 11 * 2);
344         data = buf + EXTRACT_LE_16BITS(w + 12 * 2);
345         f1 = "TotParamCnt=[d] \nTotDataCnt=[d] \nMaxParmCnt=[d] \nMaxDataCnt=[d]\nMaxSCnt=[d] \nTransFlags=[w] \nRes1=[w] \nRes2=[w] \nRes3=[w]\nParamCnt=[d] \nParamOff=[d] \nDataCnt=[d] \nDataOff=[d] \nSUCnt=[d]\n";
346         f2 = "|Name=[S]\n";
347         f3 = "|Param ";
348         f4 = "|Data ";
349     } else {
350         TCHECK2(w[7 * 2], 2);
351         paramlen = EXTRACT_LE_16BITS(w + 3 * 2);
352         param = buf + EXTRACT_LE_16BITS(w + 4 * 2);
353         datalen = EXTRACT_LE_16BITS(w + 6 * 2);
354         data = buf + EXTRACT_LE_16BITS(w + 7 * 2);
355         f1 = "TotParamCnt=[d] \nTotDataCnt=[d] \nRes1=[d]\nParamCnt=[d] \nParamOff=[d] \nRes2=[d] \nDataCnt=[d] \nDataOff=[d] \nRes3=[d]\nLsetup=[d]\n";
356         f2 = "|Unknown ";
357         f3 = "|Param ";
358         f4 = "|Data ";
359     }
360
361     smb_fdata(words + 1, f1, SMBMIN(words + 1 + 2 * words[0], maxbuf));
362     smb_fdata(data1 + 2, f2, maxbuf - (paramlen + datalen));
363
364     if (strcmp((const char *)(data1 + 2), "\\MAILSLOT\\BROWSE") == 0) {
365         print_browse(param, paramlen, data, datalen);
366         return;
367     }
368
369     if (strcmp((const char *)(data1 + 2), "\\PIPE\\LANMAN") == 0) {
370         print_ipc(param, paramlen, data, datalen);
371         return;
372     }
373
374     if (paramlen)
375         smb_fdata(param, f3, SMBMIN(param + paramlen, maxbuf));
376     if (datalen)
377         smb_fdata(data, f4, SMBMIN(data + datalen, maxbuf));
378     return;
379 trunc:
380     printf("[|SMB]");
381     return;
382 }
383
384
385 static void
386 print_negprot(const u_char *words, const u_char *data, const u_char *buf, const u_char *maxbuf)
387 {
388     const char *f1 = NULL, *f2 = NULL;
389
390     TCHECK(words[0]);
391     if (request)
392         f2 = "*|Dialect=[Z]\n";
393     else {
394         if (words[0] == 1)
395             f1 = "Core Protocol\nDialectIndex=[d]";
396         else if (words[0] == 17)
397             f1 = "NT1 Protocol\nDialectIndex=[d]\nSecMode=[B]\nMaxMux=[d]\nNumVcs=[d]\nMaxBuffer=[D]\nRawSize=[D]\nSessionKey=[W]\nCapabilities=[W]\nServerTime=[T3]TimeZone=[d]\nCryptKey=";
398         else if (words[0] == 13)
399             f1 = "Coreplus/Lanman1/Lanman2 Protocol\nDialectIndex=[d]\nSecMode=[w]\nMaxXMit=[d]\nMaxMux=[d]\nMaxVcs=[d]\nBlkMode=[w]\nSessionKey=[W]\nServerTime=[T1]TimeZone=[d]\nRes=[W]\nCryptKey=";
400     }
401
402     if (f1)
403         smb_fdata(words + 1, f1, SMBMIN(words + 1 + words[0] * 2, maxbuf));
404     else
405         print_data(words + 1, SMBMIN(words[0] * 2,
406             PTR_DIFF(maxbuf, words + 1)));
407
408     TCHECK2(*data, 2);
409     if (f2)
410         smb_fdata(data + 2, f2, SMBMIN(data + 2 + EXTRACT_LE_16BITS(data), maxbuf));
411     else
412         print_data(data + 2, SMBMIN(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));
413     return;
414 trunc:
415     printf("[|SMB]");
416     return;
417 }
418
419 static void
420 print_sesssetup(const u_char *words, const u_char *data, const u_char *buf, const u_char *maxbuf)
421 {
422     int wcnt;
423     const char *f1 = NULL, *f2 = NULL;
424
425     TCHECK(words[0]);
426     wcnt = words[0];
427     if (request) {
428         if (wcnt == 10)
429             f1 = "Com2=[w]\nOff2=[d]\nBufSize=[d]\nMpxMax=[d]\nVcNum=[d]\nSessionKey=[W]\nPassLen=[d]\nCryptLen=[d]\nCryptOff=[d]\nPass&Name=\n";
430         else
431             f1 = "Com2=[B]\nRes1=[B]\nOff2=[d]\nMaxBuffer=[d]\nMaxMpx=[d]\nVcNumber=[d]\nSessionKey=[W]\nCaseInsensitivePasswordLength=[d]\nCaseSensitivePasswordLength=[d]\nRes=[W]\nCapabilities=[W]\nPass1&Pass2&Account&Domain&OS&LanMan=\n";
432     } else {
433         if (words[0] == 3) {
434             f1 = "Com2=[w]\nOff2=[d]\nAction=[w]\n";
435         } else if (words[0] == 13) {
436             f1 = "Com2=[B]\nRes=[B]\nOff2=[d]\nAction=[w]\n";
437             f2 = "NativeOS=[S]\nNativeLanMan=[S]\nPrimaryDomain=[S]\n";
438         }
439     }
440
441     if (f1)
442         smb_fdata(words + 1, f1, SMBMIN(words + 1 + words[0] * 2, maxbuf));
443     else
444         print_data(words + 1, SMBMIN(words[0] * 2,
445             PTR_DIFF(maxbuf, words + 1)));
446
447     TCHECK2(*data, 2);
448     if (f2)
449         smb_fdata(data + 2, f2, SMBMIN(data + 2 + EXTRACT_LE_16BITS(data), maxbuf));
450     else
451         print_data(data + 2, SMBMIN(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));
452     return;
453 trunc:
454     printf("[|SMB]");
455     return;
456 }
457
458
459 static struct smbfns smb_fns[] = {
460     { -1, "SMBunknown", 0, DEFDESCRIPT },
461
462     { SMBtcon, "SMBtcon", 0,
463         { NULL, "Path=[Z]\nPassword=[Z]\nDevice=[Z]\n",
464           "MaxXmit=[d]\nTreeId=[d]\n", NULL,
465           NULL } },
466
467     { SMBtdis, "SMBtdis", 0, DEFDESCRIPT },
468     { SMBexit,  "SMBexit", 0, DEFDESCRIPT },
469     { SMBioctl, "SMBioctl", 0, DEFDESCRIPT },
470
471     { SMBecho, "SMBecho", 0,
472         { "ReverbCount=[d]\n", NULL,
473           "SequenceNum=[d]\n", NULL,
474           NULL } },
475
476     { SMBulogoffX, "SMBulogoffX", FLG_CHAIN, DEFDESCRIPT },
477
478     { SMBgetatr, "SMBgetatr", 0,
479         { NULL, "Path=[Z]\n",
480           "Attribute=[A]\nTime=[T2]Size=[D]\nRes=([w,w,w,w,w])\n", NULL,
481           NULL } },
482
483     { SMBsetatr, "SMBsetatr", 0,
484         { "Attribute=[A]\nTime=[T2]Res=([w,w,w,w,w])\n", "Path=[Z]\n",
485           NULL, NULL, NULL } },
486
487     { SMBchkpth, "SMBchkpth", 0,
488        { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
489
490     { SMBsearch, "SMBsearch", 0,
491         { "Count=[d]\nAttrib=[A]\n",
492           "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\n",
493           "Count=[d]\n",
494           "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
495           NULL } },
496
497     { SMBopen, "SMBopen", 0,
498         { "Mode=[w]\nAttribute=[A]\n", "Path=[Z]\n",
499           "Handle=[d]\nOAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\n",
500           NULL, NULL } },
501
502     { SMBcreate, "SMBcreate", 0,
503         { "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },
504
505     { SMBmknew, "SMBmknew", 0,
506         { "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },
507
508     { SMBunlink, "SMBunlink", 0,
509         { "Attrib=[A]\n", "Path=[Z]\n", NULL, NULL, NULL } },
510
511     { SMBread, "SMBread", 0,
512         { "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
513           "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },
514
515     { SMBwrite, "SMBwrite", 0,
516         { "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
517           "Count=[d]\n", NULL, NULL } },
518
519     { SMBclose, "SMBclose", 0,
520         { "Handle=[d]\nTime=[T2]", NULL, NULL, NULL, NULL } },
521
522     { SMBmkdir, "SMBmkdir", 0,
523         { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
524
525     { SMBrmdir, "SMBrmdir", 0,
526         { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
527
528     { SMBdskattr, "SMBdskattr", 0,
529         { NULL, NULL,
530           "TotalUnits=[d]\nBlocksPerUnit=[d]\nBlockSize=[d]\nFreeUnits=[d]\nMedia=[w]\n",
531           NULL, NULL } },
532
533     { SMBmv, "SMBmv", 0,
534         { "Attrib=[A]\n", "OldPath=[Z]\nNewPath=[Z]\n", NULL, NULL, NULL } },
535
536     /*
537      * this is a Pathworks specific call, allowing the
538      * changing of the root path
539      */
540     { pSETDIR, "SMBsetdir", 0, { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
541
542     { SMBlseek, "SMBlseek", 0,
543         { "Handle=[d]\nMode=[w]\nOffset=[D]\n", "Offset=[D]\n", NULL, NULL } },
544
545     { SMBflush, "SMBflush", 0, { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
546
547     { SMBsplopen, "SMBsplopen", 0,
548         { "SetupLen=[d]\nMode=[w]\n", "Ident=[Z]\n", "Handle=[d]\n",
549           NULL, NULL } },
550
551     { SMBsplclose, "SMBsplclose", 0,
552         { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
553
554     { SMBsplretq, "SMBsplretq", 0,
555         { "MaxCount=[d]\nStartIndex=[d]\n", NULL,
556           "Count=[d]\nIndex=[d]\n",
557           "*Time=[T2]Status=[B]\nJobID=[d]\nSize=[D]\nRes=[B]Name=[s16]\n",
558           NULL } },
559
560     { SMBsplwr, "SMBsplwr", 0,
561         { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
562
563     { SMBlock, "SMBlock", 0,
564         { "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },
565
566     { SMBunlock, "SMBunlock", 0,
567         { "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },
568
569     /* CORE+ PROTOCOL FOLLOWS */
570
571     { SMBreadbraw, "SMBreadbraw", 0,
572         { "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[d]\n",
573           NULL, NULL, NULL, NULL } },
574
575     { SMBwritebraw, "SMBwritebraw", 0,
576         { "Handle=[d]\nTotalCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\n|DataSize=[d]\nDataOff=[d]\n",
577           NULL, "WriteRawAck", NULL, NULL } },
578
579     { SMBwritec, "SMBwritec", 0,
580         { NULL, NULL, "Count=[d]\n", NULL, NULL } },
581
582     { SMBwriteclose, "SMBwriteclose", 0,
583         { "Handle=[d]\nCount=[d]\nOffset=[D]\nTime=[T2]Res=([w,w,w,w,w,w])",
584           NULL, "Count=[d]\n", NULL, NULL } },
585
586     { SMBlockread, "SMBlockread", 0,
587         { "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
588           "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },
589
590     { SMBwriteunlock, "SMBwriteunlock", 0,
591         { "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
592           "Count=[d]\n", NULL, NULL } },
593
594     { SMBreadBmpx, "SMBreadBmpx", 0,
595         { "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[w]\n",
596           NULL,
597           "Offset=[D]\nTotCount=[d]\nRemaining=[d]\nRes=([w,w])\nDataSize=[d]\nDataOff=[d]\n",
598           NULL, NULL } },
599
600     { SMBwriteBmpx, "SMBwriteBmpx", 0,
601         { "Handle=[d]\nTotCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\nDataSize=[d]\nDataOff=[d]\n", NULL,
602           "Remaining=[d]\n", NULL, NULL } },
603
604     { SMBwriteBs, "SMBwriteBs", 0,
605         { "Handle=[d]\nTotCount=[d]\nOffset=[D]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\n",
606           NULL, "Count=[d]\n", NULL, NULL } },
607
608     { SMBsetattrE, "SMBsetattrE", 0,
609         { "Handle=[d]\nCreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]", NULL,
610           NULL, NULL, NULL } },
611
612     { SMBgetattrE, "SMBgetattrE", 0,
613         { "Handle=[d]\n", NULL,
614           "CreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]Size=[D]\nAllocSize=[D]\nAttribute=[A]\n",
615           NULL, NULL } },
616
617     { SMBtranss, "SMBtranss", 0, DEFDESCRIPT },
618     { SMBioctls, "SMBioctls", 0, DEFDESCRIPT },
619
620     { SMBcopy, "SMBcopy", 0,
621         { "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",
622           "CopyCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },
623
624     { SMBmove, "SMBmove", 0,
625         { "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",
626           "MoveCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },
627
628     { SMBopenX, "SMBopenX", FLG_CHAIN,
629         { "Com2=[w]\nOff2=[d]\nFlags=[w]\nMode=[w]\nSearchAttrib=[A]\nAttrib=[A]\nTime=[T2]OFun=[w]\nSize=[D]\nTimeOut=[D]\nRes=[W]\n",
630           "Path=[S]\n",
631           "Com2=[w]\nOff2=[d]\nHandle=[d]\nAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\nType=[w]\nState=[w]\nAction=[w]\nFileID=[W]\nRes=[w]\n",
632           NULL, NULL } },
633
634     { SMBreadX, "SMBreadX", FLG_CHAIN,
635         { "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nCountLeft=[d]\n",
636           NULL,
637           "Com2=[w]\nOff2=[d]\nRemaining=[d]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\nRes=([w,w,w,w])\n",
638           NULL, NULL } },
639
640     { SMBwriteX, "SMBwriteX", FLG_CHAIN,
641         { "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nCountLeft=[d]\nRes=[w]\nDataSize=[d]\nDataOff=[d]\n",
642           NULL,
643           "Com2=[w]\nOff2=[d]\nCount=[d]\nRemaining=[d]\nRes=[W]\n",
644           NULL, NULL } },
645
646     { SMBlockingX, "SMBlockingX", FLG_CHAIN,
647         { "Com2=[w]\nOff2=[d]\nHandle=[d]\nLockType=[w]\nTimeOut=[D]\nUnlockCount=[d]\nLockCount=[d]\n",
648           "*Process=[d]\nOffset=[D]\nLength=[D]\n",
649           "Com2=[w]\nOff2=[d]\n", NULL, NULL } },
650
651     { SMBffirst, "SMBffirst", 0,
652         { "Count=[d]\nAttrib=[A]\n",
653           "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
654           "Count=[d]\n",
655           "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
656           NULL } },
657
658     { SMBfunique, "SMBfunique", 0,
659         { "Count=[d]\nAttrib=[A]\n",
660           "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
661           "Count=[d]\n",
662           "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
663           NULL } },
664
665     { SMBfclose, "SMBfclose", 0,
666         { "Count=[d]\nAttrib=[A]\n",
667           "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
668           "Count=[d]\n",
669           "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
670           NULL } },
671
672     { SMBfindnclose, "SMBfindnclose", 0,
673         { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
674
675     { SMBfindclose, "SMBfindclose", 0,
676         { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
677
678     { SMBsends, "SMBsends", 0,
679         { NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },
680
681     { SMBsendstrt, "SMBsendstrt", 0,
682         { NULL, "Source=[Z]\nDest=[Z]\n", "GroupID=[d]\n", NULL, NULL } },
683
684     { SMBsendend, "SMBsendend", 0,
685         { "GroupID=[d]\n", NULL, NULL, NULL, NULL } },
686
687     { SMBsendtxt, "SMBsendtxt", 0,
688         { "GroupID=[d]\n", NULL, NULL, NULL, NULL } },
689
690     { SMBsendb, "SMBsendb", 0,
691         { NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },
692
693     { SMBfwdname, "SMBfwdname", 0, DEFDESCRIPT },
694     { SMBcancelf, "SMBcancelf", 0, DEFDESCRIPT },
695     { SMBgetmac, "SMBgetmac", 0, DEFDESCRIPT },
696
697     { SMBnegprot, "SMBnegprot", 0,
698         { NULL, NULL, NULL, NULL, print_negprot } },
699
700     { SMBsesssetupX, "SMBsesssetupX", FLG_CHAIN,
701         { NULL, NULL, NULL, NULL, print_sesssetup } },
702
703     { SMBtconX, "SMBtconX", FLG_CHAIN,
704         { "Com2=[w]\nOff2=[d]\nFlags=[w]\nPassLen=[d]\nPasswd&Path&Device=\n",
705           NULL, "Com2=[w]\nOff2=[d]\n", "ServiceType=[S]\n", NULL } },
706
707     { SMBtrans2, "SMBtrans2", 0, { NULL, NULL, NULL, NULL, print_trans2 } },
708
709     { SMBtranss2, "SMBtranss2", 0, DEFDESCRIPT },
710     { SMBctemp, "SMBctemp", 0, DEFDESCRIPT },
711     { SMBreadBs, "SMBreadBs", 0, DEFDESCRIPT },
712     { SMBtrans, "SMBtrans", 0, { NULL, NULL, NULL, NULL, print_trans } },
713
714     { SMBnttrans, "SMBnttrans", 0, DEFDESCRIPT },
715     { SMBnttranss, "SMBnttranss", 0, DEFDESCRIPT },
716
717     { SMBntcreateX, "SMBntcreateX", FLG_CHAIN,
718         { "Com2=[w]\nOff2=[d]\nRes=[b]\nNameLen=[d]\nFlags=[W]\nRootDirectoryFid=[D]\nAccessMask=[W]\nAllocationSize=[L]\nExtFileAttributes=[W]\nShareAccess=[W]\nCreateDisposition=[W]\nCreateOptions=[W]\nImpersonationLevel=[W]\nSecurityFlags=[b]\n",
719           "Path=[S]\n",
720           "Com2=[w]\nOff2=[d]\nOplockLevel=[b]\nFid=[d]\nCreateAction=[W]\nCreateTime=[T3]LastAccessTime=[T3]LastWriteTime=[T3]ChangeTime=[T3]ExtFileAttributes=[W]\nAllocationSize=[L]\nEndOfFile=[L]\nFileType=[w]\nDeviceState=[w]\nDirectory=[b]\n",
721           NULL } },
722
723     { SMBntcancel, "SMBntcancel", 0, DEFDESCRIPT },
724
725     { -1, NULL, 0, DEFDESCRIPT }
726 };
727
728
729 /*
730  * print a SMB message
731  */
732 static void
733 print_smb(const u_char *buf, const u_char *maxbuf)
734 {
735     int command;
736     const u_char *words, *data;
737     struct smbfns *fn;
738     char *fmt_smbheader =
739         "[P4]SMB Command   =  [B]\nError class   =  [BP1]\nError code    =  [d]\nFlags1        =  [B]\nFlags2        =  [B][P13]\nTree ID       =  [d]\nProc ID       =  [d]\nUID           =  [d]\nMID           =  [d]\nWord Count    =  [b]\n";
740
741
742     TCHECK(buf[9]);
743     request = (buf[9] & 0x80) ? 0 : 1;
744
745     command = buf[4];
746
747     fn = smbfind(command, smb_fns);
748
749     if (vflag > 1)
750         printf("\n");
751
752     printf("SMB PACKET: %s (%s)\n", fn->name, request ? "REQUEST" : "REPLY");
753
754     if (vflag < 2)
755         return;
756
757     /* print out the header */
758     smb_fdata(buf, fmt_smbheader, buf + 33);
759
760     if (buf[5])
761         printf("SMBError = %s\n", smb_errstr(buf[5], EXTRACT_LE_16BITS(&buf[7])));
762
763     words = buf + 32;
764     TCHECK(words[0]);
765
766     for (;;) {
767         const char *f1, *f2;
768         int wct;
769         int bcc;
770
771         TCHECK(words[0]);
772         wct = words[0];
773         data = words + 1 + wct * 2;
774
775         if (request) {
776             f1 = fn->descript.req_f1;
777             f2 = fn->descript.req_f2;
778         } else {
779             f1 = fn->descript.rep_f1;
780             f2 = fn->descript.rep_f2;
781         }
782
783         if (fn->descript.fn)
784             (*fn->descript.fn)(words, data, buf, maxbuf);
785         else {
786             if (wct) {
787                 printf("smbvwv[]=\n");
788                 if (f1)
789                     smb_fdata(words + 1, f1, words + 1 + wct * 2);
790                 else {
791                     int i;
792                     int v;
793
794                     for (i = 0; i < wct; i++) {
795                         TCHECK2(words[1 + 2 * i], 2);
796                         v = EXTRACT_LE_16BITS(words + 1 + 2 * i);
797                         printf("smb_vwv[%d]=%d (0x%X)\n", i, v, v);
798                     }
799                 }
800             }
801
802             TCHECK2(*data, 2);
803             bcc = EXTRACT_LE_16BITS(data);
804             if (f2) {
805                 if (bcc > 0) {
806                     printf("smbbuf[]=\n");
807                     smb_fdata(data + 2, f2, data + 2 + bcc);
808                 }
809             } else {
810                 printf("smb_bcc=%d\n", bcc);
811                 if (bcc > 0) {
812                     printf("smb_buf[]=\n");
813                     print_data(data + 2, SMBMIN(bcc, PTR_DIFF(maxbuf, data + 2)));
814                 }
815             }
816         }
817
818         if ((fn->flags & FLG_CHAIN) == 0)
819             break;
820         if (wct == 0)
821             break;
822         TCHECK(words[1]);
823         command = EXTRACT_LE_16BITS(words + 1);
824         if (command == 0xFF)
825             break;
826         TCHECK2(words[3], 2);
827         words = buf + EXTRACT_LE_16BITS(words + 3);
828
829         fn = smbfind(command, smb_fns);
830
831         printf("\nSMB PACKET: %s (%s) (CHAINED)\n",
832             fn->name, request ? "REQUEST" : "REPLY");
833     }
834
835     printf("\n");
836     return;
837 trunc:
838     printf("[|SMB]");
839     return;
840 }
841
842
843 /*
844  * print a NBT packet received across tcp on port 139
845  */
846 void
847 nbt_tcp_print(const u_char *data, int length)
848 {
849     const u_char *maxbuf = data + length;
850     int flags;
851     int nbt_len;
852
853     TCHECK2(data[2], 2);
854     flags = data[0];
855     nbt_len = EXTRACT_16BITS(data + 2);
856
857     startbuf = data;
858     if (maxbuf <= data)
859         return;
860
861     if (vflag > 1)
862         printf ("\n>>>");
863
864     printf(" NBT Packet");
865
866     if (vflag < 2)
867         return;
868
869     printf("\n");
870
871     switch (flags) {
872     case 1:
873         printf("flags=0x%x\n", flags);
874     case 0:
875         data = smb_fdata(data, "NBT Session Packet\nFlags=[rw]\nLength=[rd]\n",
876             data + 4);
877         if (data == NULL)
878             break;
879         if (memcmp(data,"\377SMB",4) == 0) {
880             if (nbt_len > PTR_DIFF(maxbuf, data))
881             printf("WARNING: Short packet. Try increasing the snap length (%lu)\n",
882                (unsigned long)PTR_DIFF(maxbuf, data));
883             print_smb(data, maxbuf > data + nbt_len ? data + nbt_len : maxbuf);
884         } else
885             printf("Session packet:(raw data?)\n");
886         break;
887
888         case 0x81:
889             data = smb_fdata(data,
890                 "NBT Session Request\nFlags=[rW]\nDestination=[n1]\nSource=[n1]\n",
891                 maxbuf);
892             break;
893
894         case 0x82:
895             data = smb_fdata(data, "NBT Session Granted\nFlags=[rW]\n", maxbuf);
896             break;
897
898         case 0x83:
899           {
900             int ecode;
901
902             TCHECK(data[4]);
903             ecode = data[4];
904
905             data = smb_fdata(data, "NBT SessionReject\nFlags=[rW]\nReason=[B]\n",
906                 maxbuf);
907             switch (ecode) {
908             case 0x80:
909                 printf("Not listening on called name\n");
910                 break;
911             case 0x81:
912                 printf("Not listening for calling name\n");
913                 break;
914             case 0x82:
915                 printf("Called name not present\n");
916                 break;
917             case 0x83:
918                 printf("Called name present, but insufficient resources\n");
919                 break;
920             default:
921                 printf("Unspecified error 0x%X\n", ecode);
922                 break;
923             }
924           }
925             break;
926
927         case 0x85:
928             data = smb_fdata(data, "NBT Session Keepalive\nFlags=[rW]\n", maxbuf);
929             break;
930
931         default:
932             printf("flags=0x%x\n", flags);
933             data = smb_fdata(data, "NBT - Unknown packet type\nType=[rW]\n", maxbuf);
934     }
935     printf("\n");
936     fflush(stdout);
937     return;
938 trunc:
939     printf("[|SMB]");
940     return;
941 }
942
943
944 /*
945  * print a NBT packet received across udp on port 137
946  */
947 void
948 nbt_udp137_print(const u_char *data, int length)
949 {
950     const u_char *maxbuf = data + length;
951     int name_trn_id, response, opcode, nm_flags, rcode;
952     int qdcount, ancount, nscount, arcount;
953     char *opcodestr;
954     const u_char *p;
955     int total, i;
956
957     TCHECK2(data[10], 2);
958     name_trn_id = EXTRACT_16BITS(data);
959     response = (data[2] >> 7);
960     opcode = (data[2] >> 3) & 0xF;
961     nm_flags = ((data[2] & 0x7) << 4) + (data[3] >> 4);
962     rcode = data[3] & 0xF;
963     qdcount = EXTRACT_16BITS(data + 4);
964     ancount = EXTRACT_16BITS(data + 6);
965     nscount = EXTRACT_16BITS(data + 8);
966     arcount = EXTRACT_16BITS(data + 10);
967     startbuf = data;
968
969     if (maxbuf <= data)
970         return;
971
972     if (vflag > 1)
973         printf("\n>>> ");
974
975     printf("NBT UDP PACKET(137): ");
976
977     switch (opcode) {
978     case 0: opcodestr = "QUERY"; break;
979     case 5: opcodestr = "REGISTRATION"; break;
980     case 6: opcodestr = "RELEASE"; break;
981     case 7: opcodestr = "WACK"; break;
982     case 8: opcodestr = "REFRESH(8)"; break;
983     case 9: opcodestr = "REFRESH"; break;
984     case 15: opcodestr = "MULTIHOMED REGISTRATION"; break;
985     default: opcodestr = "OPUNKNOWN"; break;
986     }
987     printf("%s", opcodestr);
988     if (response) {
989         if (rcode)
990             printf("; NEGATIVE");
991         else
992             printf("; POSITIVE");
993     }
994
995     if (response)
996         printf("; RESPONSE");
997     else
998         printf("; REQUEST");
999
1000     if (nm_flags & 1)
1001         printf("; BROADCAST");
1002     else
1003         printf("; UNICAST");
1004
1005     if (vflag < 2)
1006         return;
1007
1008     printf("\nTrnID=0x%X\nOpCode=%d\nNmFlags=0x%X\nRcode=%d\nQueryCount=%d\nAnswerCount=%d\nAuthorityCount=%d\nAddressRecCount=%d\n",
1009         name_trn_id, opcode, nm_flags, rcode, qdcount, ancount, nscount,
1010         arcount);
1011
1012     p = data + 12;
1013
1014     total = ancount + nscount + arcount;
1015
1016     if (qdcount > 100 || total > 100) {
1017         printf("Corrupt packet??\n");
1018         return;
1019     }
1020
1021     if (qdcount) {
1022         printf("QuestionRecords:\n");
1023         for (i = 0; i < qdcount; i++)
1024             p = smb_fdata(p,
1025                 "|Name=[n1]\nQuestionType=[rw]\nQuestionClass=[rw]\n#",
1026                 maxbuf);
1027         if (p == NULL)
1028             goto out;
1029     }
1030
1031     if (total) {
1032         printf("\nResourceRecords:\n");
1033         for (i = 0; i < total; i++) {
1034             int rdlen;
1035             int restype;
1036
1037             p = smb_fdata(p, "Name=[n1]\n#", maxbuf);
1038             if (p == NULL)
1039                 goto out;
1040             restype = EXTRACT_16BITS(p);
1041             p = smb_fdata(p, "ResType=[rw]\nResClass=[rw]\nTTL=[rD]\n", p + 8);
1042             if (p == NULL)
1043                 goto out;
1044             rdlen = EXTRACT_16BITS(p);
1045             printf("ResourceLength=%d\nResourceData=\n", rdlen);
1046             p += 2;
1047             if (rdlen == 6) {
1048                 p = smb_fdata(p, "AddrType=[rw]\nAddress=[b.b.b.b]\n", p + rdlen);
1049                 if (p == NULL)
1050                     goto out;
1051             } else {
1052                 if (restype == 0x21) {
1053                     int numnames;
1054
1055                     TCHECK(*p);
1056                     numnames = p[0];
1057                     p = smb_fdata(p, "NumNames=[B]\n", p + 1);
1058                     if (p == NULL)
1059                         goto out;
1060                     while (numnames--) {
1061                         p = smb_fdata(p, "Name=[n2]\t#", maxbuf);
1062                         TCHECK(*p);
1063                         if (p[0] & 0x80)
1064                             printf("<GROUP> ");
1065                         switch (p[0] & 0x60) {
1066                         case 0x00: printf("B "); break;
1067                         case 0x20: printf("P "); break;
1068                         case 0x40: printf("M "); break;
1069                         case 0x60: printf("_ "); break;
1070                         }
1071                         if (p[0] & 0x10)
1072                             printf("<DEREGISTERING> ");
1073                         if (p[0] & 0x08)
1074                             printf("<CONFLICT> ");
1075                         if (p[0] & 0x04)
1076                             printf("<ACTIVE> ");
1077                         if (p[0] & 0x02)
1078                             printf("<PERMANENT> ");
1079                         printf("\n");
1080                         p += 2;
1081                     }
1082                 } else {
1083                     print_data(p, min(rdlen, length - (p - data)));
1084                     p += rdlen;
1085                 }
1086             }
1087         }
1088     }
1089
1090     if (p < maxbuf)
1091         smb_fdata(p, "AdditionalData:\n", maxbuf);
1092
1093 out:
1094     printf("\n");
1095     fflush(stdout);
1096     return;
1097 trunc:
1098     printf("[|SMB]");
1099     return;
1100 }
1101
1102
1103
1104 /*
1105  * print a NBT packet received across udp on port 138
1106  */
1107 void
1108 nbt_udp138_print(const u_char *data, int length)
1109 {
1110     const u_char *maxbuf = data + length;
1111
1112     if (maxbuf > snapend)
1113         maxbuf = snapend;
1114     if (maxbuf <= data)
1115         return;
1116     startbuf = data;
1117
1118     if (vflag < 2) {
1119         printf("NBT UDP PACKET(138)");
1120         return;
1121     }
1122
1123     data = smb_fdata(data,
1124         "\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#",
1125         maxbuf);
1126
1127     if (data != NULL) {
1128         /* If there isn't enough data for "\377SMB", don't check for it. */
1129         if (&data[3] >= maxbuf)
1130             goto out;
1131
1132         if (memcmp(data, "\377SMB",4) == 0)
1133             print_smb(data, maxbuf);
1134     }
1135 out:
1136     printf("\n");
1137     fflush(stdout);
1138 }
1139
1140
1141 /*
1142    print netbeui frames
1143 */
1144 void
1145 netbeui_print(u_short control, const u_char *data, int length)
1146 {
1147     const u_char *maxbuf = data + length;
1148     int len;
1149     int command;
1150     const u_char *data2;
1151     int is_truncated = 0;
1152
1153     if (maxbuf > snapend)
1154         maxbuf = snapend;
1155     TCHECK(data[4]);
1156     len = EXTRACT_LE_16BITS(data);
1157     command = data[4];
1158     data2 = data + len;
1159     if (data2 >= maxbuf) {
1160         data2 = maxbuf;
1161         is_truncated = 1;
1162     }
1163
1164     startbuf = data;
1165
1166     if (vflag < 2) {
1167         printf("NetBeui Packet");
1168         return;
1169     }
1170
1171     printf("\n>>> NetBeui Packet\nType=0x%X ", control);
1172     data = smb_fdata(data, "Length=[d] Signature=[w] Command=[B]\n#", maxbuf);
1173     if (data == NULL)
1174         goto out;
1175
1176     switch (command) {
1177     case 0xA:
1178         data = smb_fdata(data, "NameQuery:[P1]\nSessionNumber=[B]\nNameType=[B][P2]\nResponseCorrelator=[w]\nDestination=[n2]\nSource=[n2]\n", data2);
1179         break;
1180
1181     case 0x8:
1182         data = smb_fdata(data,
1183             "NetbiosDataGram:[P7]\nDestination=[n2]\nSource=[n2]\n", data2);
1184         break;
1185
1186     case 0xE:
1187         data = smb_fdata(data,
1188             "NameRecognise:\n[P1]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nDestination=[n2]\nSource=[n2]\n",
1189             data2);
1190         break;
1191
1192     case 0x19:
1193         data = smb_fdata(data,
1194             "SessionInitialise:\nData1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",
1195             data2);
1196         break;
1197
1198     case 0x17:
1199         data = smb_fdata(data,
1200             "SessionConfirm:\nData1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",
1201             data2);
1202         break;
1203
1204     case 0x16:
1205         data = smb_fdata(data,
1206             "NetbiosDataOnlyLast:\nFlags=[{|NO_ACK|PIGGYBACK_ACK_ALLOWED|PIGGYBACK_ACK_INCLUDED|}]\nResyncIndicator=[w][P2]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",
1207             data2);
1208         break;
1209
1210     case 0x14:
1211         data = smb_fdata(data,
1212             "NetbiosDataAck:\n[P3]TransmitCorrelator=[w][P2]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",
1213             data2);
1214         break;
1215
1216     case 0x18:
1217         data = smb_fdata(data,
1218             "SessionEnd:\n[P1]Data2=[w][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n",
1219             data2);
1220         break;
1221
1222     case 0x1f:
1223         data = smb_fdata(data, "SessionAlive\n", data2);
1224         break;
1225
1226     default:
1227         data = smb_fdata(data, "Unknown Netbios Command ", data2);
1228         break;
1229     }
1230     if (data == NULL)
1231         goto out;
1232
1233     if (is_truncated) {
1234         /* data2 was past the end of the buffer */
1235         goto out;
1236     }
1237
1238     /* If there isn't enough data for "\377SMB", don't look for it. */
1239     if (&data2[3] >= maxbuf)
1240         goto out;
1241
1242     if (memcmp(data2, "\377SMB",4) == 0)
1243         print_smb(data2, maxbuf);
1244     else {
1245         int i;
1246         for (i = 0; i < 128; i++) {
1247             if (&data2[i + 3] >= maxbuf)
1248                 break;
1249             if (memcmp(&data2[i], "\377SMB", 4) == 0) {
1250                 printf("found SMB packet at %d\n", i);
1251                 print_smb(&data2[i], maxbuf);
1252                 break;
1253             }
1254         }
1255     }
1256
1257 out:
1258     printf("\n");
1259     return;
1260 trunc:
1261     printf("[|SMB]");
1262     return;
1263 }
1264
1265
1266 /*
1267  * print IPX-Netbios frames
1268  */
1269 void
1270 ipx_netbios_print(const u_char *data, u_int length)
1271 {
1272     /*
1273      * this is a hack till I work out how to parse the rest of the
1274      * NetBIOS-over-IPX stuff
1275      */
1276     int i;
1277     const u_char *maxbuf;
1278
1279     maxbuf = data + length;
1280     /* Don't go past the end of the captured data in the packet. */
1281     if (maxbuf > snapend)
1282         maxbuf = snapend;
1283     startbuf = data;
1284     for (i = 0; i < 128; i++) {
1285         if (&data[i + 4] > maxbuf)
1286             break;
1287         if (memcmp(&data[i], "\377SMB", 4) == 0) {
1288             smb_fdata(data, "\n>>> IPX transport ", &data[i]);
1289             if (data != NULL)
1290                 print_smb(&data[i], maxbuf);
1291             printf("\n");
1292             fflush(stdout);
1293             break;
1294         }
1295     }
1296     if (i == 128)
1297         smb_fdata(data, "\n>>> Unknown IPX ", maxbuf);
1298 }