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