2 * Copyright (C) Andrew Tridgell 1995-1999
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
14 static const char rcsid
[] =
15 "@(#) $Header: /tcpdump/master/tcpdump/smbutil.c,v 1.16 2001-06-25 21:04:01 itojun Exp $";
18 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
31 #include "interface.h"
34 extern const uchar
*startbuf
;
37 * interpret a 32 bit dos packed date/time to some parameters
40 interpret_dos_date(uint32 date
, struct tm
*tp
)
42 uint32 p0
, p1
, p2
, p3
;
45 p1
= ((date
& 0xFF00) >> 8) & 0xFF;
46 p2
= ((date
& 0xFF0000) >> 16) & 0xFF;
47 p3
= ((date
& 0xFF000000) >> 24) & 0xFF;
49 tp
->tm_sec
= 2 * (p0
& 0x1F);
50 tp
->tm_min
= ((p0
>> 5) & 0xFF) + ((p1
& 0x7) << 3);
51 tp
->tm_hour
= (p1
>> 3) & 0xFF;
52 tp
->tm_mday
= (p2
& 0x1F);
53 tp
->tm_mon
= ((p2
>> 5) & 0xFF) + ((p3
& 0x1) << 3) - 1;
54 tp
->tm_year
= ((p3
>> 1) & 0xFF) + 80;
58 * create a unix date from a dos date
61 make_unix_date(const void *date_ptr
)
66 dos_date
= IVAL(date_ptr
, 0);
71 interpret_dos_date(dos_date
, &t
);
80 * create a unix date from a dos date
83 make_unix_date2(const void *date_ptr
)
87 x
= IVAL(date_ptr
, 0);
88 x2
= ((x
& 0xFFFF) << 16) | ((x
& 0xFFFF0000) >> 16);
91 return(make_unix_date((void *)&x
));
95 * interpret an 8 byte "filetime" structure to a time_t
96 * It's originally in "100ns units since jan 1st 1601"
99 interpret_long_date(const char *p
)
104 /* this gives us seconds since jan 1st 1601 (approx) */
105 d
= (IVAL(p
, 4) * 256.0 + CVAL(p
, 3)) * (1.0e-7 * (1 << 24));
107 /* now adjust by 369 years to make the secs since 1970 */
108 d
-= 369.0 * 365.25 * 24 * 60 * 60;
110 /* and a fudge factor as we got it wrong by a few days */
111 d
+= (3 * 24 * 60 * 60 + 6 * 60 * 60 + 2);
122 * interpret the weird netbios "name". Return the name type, or -1 if
123 * we run past the end of the buffer
126 name_interpret(const uchar
*in
, const uchar
*maxbuf
, char *out
)
132 return(-1); /* name goes past the end of the buffer */
138 if (len
> 30 || len
< 1)
142 if (in
+ 1 >= maxbuf
)
143 return(-1); /* name goes past the end of the buffer */
145 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
149 *out
= ((in
[0] - 'A') << 4) + (in
[1] - 'A');
163 * find a pointer to a netbios name
166 name_ptr(const uchar
*buf
, int ofs
, const uchar
*maxbuf
)
173 return(NULL
); /* name goes past the end of the buffer */
178 /* XXX - this should use the same code that the DNS dissector does */
179 if ((c
& 0xC0) == 0xC0) {
180 uint16 l
= RSVAL(buf
, ofs
) & 0x3FFF;
182 /* We have a pointer that points to itself. */
187 return(NULL
); /* name goes past the end of the buffer */
194 return(NULL
); /* name goes past the end of the buffer */
198 * extract a netbios name from a buf
201 name_extract(const uchar
*buf
, int ofs
, const uchar
*maxbuf
, char *name
)
203 const uchar
*p
= name_ptr(buf
, ofs
, maxbuf
);
205 return(-1); /* error (probably name going past end of buffer) */
207 return(name_interpret(p
, maxbuf
, name
));
212 * return the total storage length of a mangled name
215 name_len(const unsigned char *s
, const unsigned char *maxbuf
)
217 const unsigned char *s0
= s
;
221 return(-1); /* name goes past the end of the buffer */
224 if ((c
& 0xC0) == 0xC0)
228 return(-1); /* name goes past the end of the buffer */
232 return(PTR_DIFF(s
, s0
) + 1);
235 return(-1); /* name goes past the end of the buffer */
239 print_asc(const unsigned char *buf
, int len
)
242 for (i
= 0; i
< len
; i
++)
247 name_type_str(int name_type
)
252 case 0: f
= "Workstation"; break;
253 case 0x03: f
= "Client?"; break;
254 case 0x20: f
= "Server"; break;
255 case 0x1d: f
= "Master Browser"; break;
256 case 0x1b: f
= "Domain Controller"; break;
257 case 0x1e: f
= "Browser Server"; break;
258 default: f
= "Unknown"; break;
264 print_data(const unsigned char *buf
, int len
)
270 printf("[%03X] ", i
);
271 for (i
= 0; i
< len
; /*nothing*/) {
272 printf("%02X ", buf
[i
] & 0xff);
277 print_asc(&buf
[i
- 16], 8);
279 print_asc(&buf
[i
- 8], 8);
282 printf("[%03X] ", i
);
296 print_asc(&buf
[i
- (i
% 16)], n
);
300 print_asc(&buf
[i
- n
], n
);
307 write_bits(unsigned int val
, char *fmt
)
312 while ((p
= strchr(fmt
, '|'))) {
313 size_t l
= PTR_DIFF(p
, fmt
);
314 if (l
&& (val
& (1 << i
)))
315 printf("%.*s ", (int)l
, fmt
);
321 /* convert a UCS2 string into iso-8859-1 string */
323 unistr(const char *s
, int *len
)
325 static char buf
[1000];
327 static int use_unicode
= -1;
329 if (use_unicode
== -1) {
330 char *p
= getenv("USE_UNICODE");
331 if (p
&& (atoi(p
) == 1))
337 /* maybe it isn't unicode - a cheap trick */
338 if (!use_unicode
|| (s
[0] && s
[1])) {
339 *len
= strlen(s
) + 1;
345 if (s
[0] == 0 && s
[1] != 0) {
350 while (l
< (sizeof(buf
) - 1) && s
[0] && s
[1] == 0) {
362 fdata1(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
365 char *attrib_fmt
= "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
368 while (*fmt
&& buf
<maxbuf
) {
371 write_bits(CVAL(buf
,0), attrib_fmt
);
377 write_bits(SVAL(buf
, 0), attrib_fmt
);
385 char *p
= strchr(++fmt
, '}');
386 int l
= PTR_DIFF(p
, fmt
);
387 strncpy(bitfmt
, fmt
, l
);
390 write_bits(CVAL(buf
, 0), bitfmt
);
397 int l
= atoi(fmt
+ 1);
400 while (isdigit(*fmt
))
410 unsigned int x
= reverse
? RIVAL(buf
, 0) : IVAL(buf
, 0);
411 printf("%d (0x%x)", x
, x
);
418 unsigned int x1
= reverse
? RIVAL(buf
, 0) : IVAL(buf
, 0);
419 unsigned int x2
= reverse
? RIVAL(buf
, 4) : IVAL(buf
, 4);
421 printf("0x%08x:%08x", x2
, x1
);
423 printf("%d (0x%08x%08x)", x1
, x2
, x1
);
430 unsigned int x
= reverse
? RSVAL(buf
, 0) : SVAL(buf
, 0);
431 printf("%d (0x%x)", x
, x
);
438 unsigned int x
= reverse
? RIVAL(buf
, 0) : IVAL(buf
, 0);
446 unsigned int x
= reverse
? RSVAL(buf
, 0) : SVAL(buf
, 0);
454 unsigned int x
= CVAL(buf
,0);
462 unsigned int x
= CVAL(buf
, 0);
463 printf("%u (0x%x)", x
, x
);
470 printf("%.*s", (int)PTR_DIFF(maxbuf
, buf
), unistr(buf
, &len
));
477 if (*buf
!= 4 && *buf
!= 2)
478 printf("Error! ASCIIZ buffer of type %u (safety=%lu)\n", *buf
,
479 (unsigned long)PTR_DIFF(maxbuf
, buf
));
480 printf("%.*s", (int)PTR_DIFF(maxbuf
, buf
+ 1),
481 unistr(buf
+ 1, &len
));
488 int l
= atoi(fmt
+ 1);
489 printf("%-*.*s", l
, l
, buf
);
492 while (isdigit(*fmt
))
498 int l
= atoi(fmt
+ 1);
500 printf("%02x", *buf
++);
502 while (isdigit(*fmt
))
515 name_type
= name_extract(startbuf
, PTR_DIFF(buf
, startbuf
),
519 len
= name_len(buf
, maxbuf
);
523 printf("%-15.15s NameType=0x%02X (%s)", nbuf
, name_type
,
524 name_type_str(name_type
));
528 printf("%-15.15s NameType=0x%02X (%s)", buf
, name_type
,
529 name_type_str(name_type
));
534 while (isdigit(*fmt
))
543 switch (atoi(fmt
+ 1)) {
545 if (x
== 0 || x
== -1 || x
== 0xFFFFFFFF)
548 t
= make_unix_date(buf
);
552 if (x
== 0 || x
== -1 || x
== 0xFFFFFFFF)
555 t
= make_unix_date2(buf
);
559 t
= interpret_long_date(buf
);
563 printf("%s", t
? asctime(localtime(&t
)) : "NULL\n");
565 while (isdigit(*fmt
))
576 if (buf
>= maxbuf
&& *fmt
)
577 printf("END OF BUFFER\n");
583 printf("WARNING: Short packet. Try increasing the snap length\n");
588 fdata(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
590 static int depth
= 0;
598 while (buf
< maxbuf
) {
601 buf2
= fdata(buf
, fmt
, maxbuf
);
629 memset(s
, 0, sizeof(s
));
630 p
= strchr(fmt
, ']');
631 if (p
- fmt
+ 1 > sizeof(s
)) {
635 strncpy(s
, fmt
, p
- fmt
);
638 buf
= fdata1(buf
, s
, maxbuf
);
650 if (!depth
&& buf
< maxbuf
) {
651 size_t len
= PTR_DIFF(maxbuf
, buf
);
652 printf("Data: (%lu bytes)\n", (unsigned long)len
);
653 print_data(buf
, len
);
665 /* Dos Error Messages */
666 static err_code_struct dos_msgs
[] = {
667 { "ERRbadfunc", 1, "Invalid function." },
668 { "ERRbadfile", 2, "File not found." },
669 { "ERRbadpath", 3, "Directory invalid." },
670 { "ERRnofids", 4, "No file descriptors available" },
671 { "ERRnoaccess", 5, "Access denied." },
672 { "ERRbadfid", 6, "Invalid file handle." },
673 { "ERRbadmcb", 7, "Memory control blocks destroyed." },
674 { "ERRnomem", 8, "Insufficient server memory to perform the requested function." },
675 { "ERRbadmem", 9, "Invalid memory block address." },
676 { "ERRbadenv", 10, "Invalid environment." },
677 { "ERRbadformat", 11, "Invalid format." },
678 { "ERRbadaccess", 12, "Invalid open mode." },
679 { "ERRbaddata", 13, "Invalid data." },
680 { "ERR", 14, "reserved." },
681 { "ERRbaddrive", 15, "Invalid drive specified." },
682 { "ERRremcd", 16, "A Delete Directory request attempted to remove the server's current directory." },
683 { "ERRdiffdevice", 17, "Not same device." },
684 { "ERRnofiles", 18, "A File Search command can find no more files matching the specified criteria." },
685 { "ERRbadshare", 32, "The sharing mode specified for an Open conflicts with existing FIDs on the file." },
686 { "ERRlock", 33, "A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process." },
687 { "ERRfilexists", 80, "The file named in a Create Directory, Make New File or Link request already exists." },
688 { "ERRbadpipe", 230, "Pipe invalid." },
689 { "ERRpipebusy", 231, "All instances of the requested pipe are busy." },
690 { "ERRpipeclosing", 232, "Pipe close in progress." },
691 { "ERRnotconnected", 233, "No process on other end of pipe." },
692 { "ERRmoredata", 234, "There is more data to be returned." },
696 /* Server Error Messages */
697 err_code_struct server_msgs
[] = {
698 { "ERRerror", 1, "Non-specific error code." },
699 { "ERRbadpw", 2, "Bad password - name/password pair in a Tree Connect or Session Setup are invalid." },
700 { "ERRbadtype", 3, "reserved." },
701 { "ERRaccess", 4, "The requester does not have the necessary access rights within the specified context for the requested function. The context is defined by the TID or the UID." },
702 { "ERRinvnid", 5, "The tree ID (TID) specified in a command was invalid." },
703 { "ERRinvnetname", 6, "Invalid network name in tree connect." },
704 { "ERRinvdevice", 7, "Invalid device - printer request made to non-printer connection or non-printer request made to printer connection." },
705 { "ERRqfull", 49, "Print queue full (files) -- returned by open print file." },
706 { "ERRqtoobig", 50, "Print queue full -- no space." },
707 { "ERRqeof", 51, "EOF on print queue dump." },
708 { "ERRinvpfid", 52, "Invalid print file FID." },
709 { "ERRsmbcmd", 64, "The server did not recognize the command received." },
710 { "ERRsrverror", 65, "The server encountered an internal error, e.g., system file unavailable." },
711 { "ERRfilespecs", 67, "The file handle (FID) and pathname parameters contained an invalid combination of values." },
712 { "ERRreserved", 68, "reserved." },
713 { "ERRbadpermits", 69, "The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute." },
714 { "ERRreserved", 70, "reserved." },
715 { "ERRsetattrmode", 71, "The attribute mode in the Set File Attribute request is invalid." },
716 { "ERRpaused", 81, "Server is paused." },
717 { "ERRmsgoff", 82, "Not receiving messages." },
718 { "ERRnoroom", 83, "No room to buffer message." },
719 { "ERRrmuns", 87, "Too many remote user names." },
720 { "ERRtimeout", 88, "Operation timed out." },
721 { "ERRnoresource", 89, "No resources currently available for request." },
722 { "ERRtoomanyuids", 90, "Too many UIDs active on this session." },
723 { "ERRbaduid", 91, "The UID is not known as a valid ID on this session." },
724 { "ERRusempx", 250, "Temp unable to support Raw, use MPX mode." },
725 { "ERRusestd", 251, "Temp unable to support Raw, use standard read/write." },
726 { "ERRcontmpx", 252, "Continue in MPX mode." },
727 { "ERRreserved", 253, "reserved." },
728 { "ERRreserved", 254, "reserved." },
729 { "ERRnosupport", 0xFFFF, "Function not supported." },
733 /* Hard Error Messages */
734 err_code_struct hard_msgs
[] = {
735 { "ERRnowrite", 19, "Attempt to write on write-protected diskette." },
736 { "ERRbadunit", 20, "Unknown unit." },
737 { "ERRnotready", 21, "Drive not ready." },
738 { "ERRbadcmd", 22, "Unknown command." },
739 { "ERRdata", 23, "Data error (CRC)." },
740 { "ERRbadreq", 24, "Bad request structure length." },
741 { "ERRseek", 25 , "Seek error." },
742 { "ERRbadmedia", 26, "Unknown media type." },
743 { "ERRbadsector", 27, "Sector not found." },
744 { "ERRnopaper", 28, "Printer out of paper." },
745 { "ERRwrite", 29, "Write fault." },
746 { "ERRread", 30, "Read fault." },
747 { "ERRgeneral", 31, "General failure." },
748 { "ERRbadshare", 32, "A open conflicts with an existing open." },
749 { "ERRlock", 33, "A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process." },
750 { "ERRwrongdisk", 34, "The wrong disk was found in a drive." },
751 { "ERRFCBUnavail", 35, "No FCBs are available to process request." },
752 { "ERRsharebufexc", 36, "A sharing buffer has been exceeded." },
759 err_code_struct
*err_msgs
;
761 { 0, "SUCCESS", NULL
},
762 { 0x01, "ERRDOS", dos_msgs
},
763 { 0x02, "ERRSRV", server_msgs
},
764 { 0x03, "ERRHRD", hard_msgs
},
765 { 0x04, "ERRXOS", NULL
},
766 { 0xE1, "ERRRMX1", NULL
},
767 { 0xE2, "ERRRMX2", NULL
},
768 { 0xE3, "ERRRMX3", NULL
},
769 { 0xFF, "ERRCMD", NULL
},
774 * return a SMB error string from a SMB buffer
777 smb_errstr(int class, int num
)
779 static char ret
[128];
784 for (i
= 0; err_classes
[i
].class; i
++)
785 if (err_classes
[i
].code
== class) {
786 if (err_classes
[i
].err_msgs
) {
787 err_code_struct
*err
= err_classes
[i
].err_msgs
;
788 for (j
= 0; err
[j
].name
; j
++)
789 if (num
== err
[j
].code
) {
790 snprintf(ret
, sizeof(ret
), "%s - %s (%s)",
791 err_classes
[i
].class, err
[j
].name
, err
[j
].message
);
796 snprintf(ret
, sizeof(ret
), "%s - %d", err_classes
[i
].class, num
);
800 snprintf(ret
, sizeof(ret
), "ERROR: Unknown error (%d,%d)", class, num
);