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.21 2002-04-26 05:12:40 guy Exp $";
18 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #ifdef TIME_WITH_SYS_TIME
33 #include "interface.h"
37 extern const u_char
*startbuf
;
40 * interpret a 32 bit dos packed date/time to some parameters
43 interpret_dos_date(u_int32_t date
, struct tm
*tp
)
45 u_int32_t p0
, p1
, p2
, p3
;
48 p1
= ((date
& 0xFF00) >> 8) & 0xFF;
49 p2
= ((date
& 0xFF0000) >> 16) & 0xFF;
50 p3
= ((date
& 0xFF000000) >> 24) & 0xFF;
52 tp
->tm_sec
= 2 * (p0
& 0x1F);
53 tp
->tm_min
= ((p0
>> 5) & 0xFF) + ((p1
& 0x7) << 3);
54 tp
->tm_hour
= (p1
>> 3) & 0xFF;
55 tp
->tm_mday
= (p2
& 0x1F);
56 tp
->tm_mon
= ((p2
>> 5) & 0xFF) + ((p3
& 0x1) << 3) - 1;
57 tp
->tm_year
= ((p3
>> 1) & 0xFF) + 80;
62 * create a unix date from a dos date
65 int_unix_date(u_int32_t dos_date
)
72 interpret_dos_date(dos_date
, &t
);
81 * create a unix date from a dos date
82 * in network byte order
85 make_unix_date(const u_char
*date_ptr
)
87 u_int32_t dos_date
= 0;
89 dos_date
= EXTRACT_LE_32BITS(date_ptr
);
91 return int_unix_date(dos_date
);
95 * create a unix date from a dos date
96 * in halfword-swapped network byte order!
99 make_unix_date2(const u_char
*date_ptr
)
103 x
= EXTRACT_LE_32BITS(date_ptr
);
104 x2
= ((x
& 0xFFFF) << 16) | ((x
& 0xFFFF0000) >> 16);
105 return int_unix_date(x2
);
109 * interpret an 8 byte "filetime" structure to a time_t
110 * It's originally in "100ns units since jan 1st 1601"
113 interpret_long_date(const u_char
*p
)
120 /* this gives us seconds since jan 1st 1601 (approx) */
121 d
= (EXTRACT_LE_32BITS(p
+ 4) * 256.0 + p
[3]) * (1.0e-7 * (1 << 24));
123 /* now adjust by 369 years to make the secs since 1970 */
124 d
-= 369.0 * 365.25 * 24 * 60 * 60;
126 /* and a fudge factor as we got it wrong by a few days */
127 d
+= (3 * 24 * 60 * 60 + 6 * 60 * 60 + 2);
140 * interpret the weird netbios "name". Return the name type, or -1 if
141 * we run past the end of the buffer
144 name_interpret(const u_char
*in
, const u_char
*maxbuf
, char *out
)
150 return(-1); /* name goes past the end of the buffer */
156 if (len
> 30 || len
< 1)
161 if (in
+ 1 >= maxbuf
)
162 return(-1); /* name goes past the end of the buffer */
163 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
167 *out
= ((in
[0] - 'A') << 4) + (in
[1] - 'A');
181 * find a pointer to a netbios name
183 static const u_char
*
184 name_ptr(const u_char
*buf
, int ofs
, const u_char
*maxbuf
)
191 return(NULL
); /* name goes past the end of the buffer */
196 /* XXX - this should use the same code that the DNS dissector does */
197 if ((c
& 0xC0) == 0xC0) {
198 u_int16_t l
= EXTRACT_16BITS(buf
+ ofs
) & 0x3FFF;
200 /* We have a pointer that points to itself. */
205 return(NULL
); /* name goes past the end of the buffer */
212 return(NULL
); /* name goes past the end of the buffer */
216 * extract a netbios name from a buf
219 name_extract(const u_char
*buf
, int ofs
, const u_char
*maxbuf
, char *name
)
221 const u_char
*p
= name_ptr(buf
, ofs
, maxbuf
);
223 return(-1); /* error (probably name going past end of buffer) */
225 return(name_interpret(p
, maxbuf
, name
));
230 * return the total storage length of a mangled name
233 name_len(const unsigned char *s
, const unsigned char *maxbuf
)
235 const unsigned char *s0
= s
;
239 return(-1); /* name goes past the end of the buffer */
242 if ((c
& 0xC0) == 0xC0)
246 return(-1); /* name goes past the end of the buffer */
250 return(PTR_DIFF(s
, s0
) + 1);
253 return(-1); /* name goes past the end of the buffer */
257 print_asc(const unsigned char *buf
, int len
)
260 for (i
= 0; i
< len
; i
++)
265 name_type_str(int name_type
)
270 case 0: f
= "Workstation"; break;
271 case 0x03: f
= "Client?"; break;
272 case 0x20: f
= "Server"; break;
273 case 0x1d: f
= "Master Browser"; break;
274 case 0x1b: f
= "Domain Controller"; break;
275 case 0x1e: f
= "Browser Server"; break;
276 default: f
= "Unknown"; break;
282 print_data(const unsigned char *buf
, int len
)
288 printf("[%03X] ", i
);
289 for (i
= 0; i
< len
; /*nothing*/) {
290 printf("%02X ", buf
[i
] & 0xff);
295 print_asc(&buf
[i
- 16], 8);
297 print_asc(&buf
[i
- 8], 8);
300 printf("[%03X] ", i
);
313 n
= SMBMIN(8, i
% 16);
314 print_asc(&buf
[i
- (i
% 16)], n
);
318 print_asc(&buf
[i
- n
], n
);
325 write_bits(unsigned int val
, char *fmt
)
330 while ((p
= strchr(fmt
, '|'))) {
331 size_t l
= PTR_DIFF(p
, fmt
);
332 if (l
&& (val
& (1 << i
)))
333 printf("%.*s ", (int)l
, fmt
);
339 /* convert a UCS2 string into iso-8859-1 string */
341 unistr(const u_char
*s
, int *len
)
343 static char buf
[1000];
345 static int use_unicode
= -1;
347 if (use_unicode
== -1) {
348 char *p
= getenv("USE_UNICODE");
349 if (p
&& (atoi(p
) == 1))
355 /* maybe it isn't unicode - a cheap trick */
356 if (!use_unicode
|| (s
[0] && s
[1])) {
357 *len
= strlen((const char *)s
) + 1;
358 return (const char *)s
;
363 if (s
[0] == 0 && s
[1] != 0) {
368 while (l
< (sizeof(buf
) - 1) && s
[0] && s
[1] == 0) {
379 static const u_char
*
380 smb_fdata1(const u_char
*buf
, const char *fmt
, const u_char
*maxbuf
)
383 char *attrib_fmt
= "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
386 while (*fmt
&& buf
<maxbuf
) {
389 write_bits(buf
[0], attrib_fmt
);
395 write_bits(EXTRACT_LE_16BITS(buf
), attrib_fmt
);
406 p
= strchr(++fmt
, '}');
407 l
= PTR_DIFF(p
, fmt
);
408 strncpy(bitfmt
, fmt
, l
);
411 write_bits(buf
[0], bitfmt
);
418 int l
= atoi(fmt
+ 1);
421 while (isdigit((unsigned char)*fmt
))
434 x
= reverse
? EXTRACT_32BITS(buf
) : EXTRACT_LE_32BITS(buf
);
435 printf("%d (0x%x)", x
, x
);
445 x1
= reverse
? EXTRACT_32BITS(buf
) :
446 EXTRACT_LE_32BITS(buf
);
447 x2
= reverse
? EXTRACT_32BITS(buf
+ 4) :
448 EXTRACT_LE_32BITS(buf
+ 4);
450 printf("0x%08x:%08x", x2
, x1
);
452 printf("%d (0x%08x%08x)", x1
, x2
, x1
);
461 x
= reverse
? EXTRACT_16BITS(buf
) :
462 EXTRACT_LE_16BITS(buf
);
463 printf("%d (0x%x)", x
, x
);
472 x
= reverse
? EXTRACT_32BITS(buf
) :
473 EXTRACT_LE_32BITS(buf
);
483 x
= reverse
? EXTRACT_16BITS(buf
) :
484 EXTRACT_LE_16BITS(buf
);
505 printf("%u (0x%x)", x
, x
);
513 printf("%.*s", (int)PTR_DIFF(maxbuf
, buf
), unistr(buf
, &len
));
520 if (*buf
!= 4 && *buf
!= 2)
521 printf("Error! ASCIIZ buffer of type %u (safety=%lu)\n", *buf
,
522 (unsigned long)PTR_DIFF(maxbuf
, buf
));
523 printf("%.*s", (int)PTR_DIFF(maxbuf
, buf
+ 1),
524 unistr(buf
+ 1, &len
));
531 int l
= atoi(fmt
+ 1);
532 printf("%-*.*s", l
, l
, buf
);
535 while (isdigit((unsigned char)*fmt
))
541 int l
= atoi(fmt
+ 1);
543 printf("%02x", *buf
++);
545 while (isdigit((unsigned char)*fmt
))
558 name_type
= name_extract(startbuf
, PTR_DIFF(buf
, startbuf
),
562 len
= name_len(buf
, maxbuf
);
566 printf("%-15.15s NameType=0x%02X (%s)", nbuf
, name_type
,
567 name_type_str(name_type
));
571 printf("%-15.15s NameType=0x%02X (%s)", buf
, name_type
,
572 name_type_str(name_type
));
577 while (isdigit((unsigned char)*fmt
))
585 x
= EXTRACT_LE_32BITS(buf
);
587 switch (atoi(fmt
+ 1)) {
589 if (x
== 0 || x
== -1 || x
== 0xFFFFFFFF)
592 t
= make_unix_date(buf
);
596 if (x
== 0 || x
== -1 || x
== 0xFFFFFFFF)
599 t
= make_unix_date2(buf
);
603 t
= interpret_long_date(buf
);
607 printf("%s", t
? asctime(localtime(&t
)) : "NULL\n");
609 while (isdigit((unsigned char)*fmt
))
620 if (buf
>= maxbuf
&& *fmt
)
621 printf("END OF BUFFER\n");
627 printf("WARNING: Short packet. Try increasing the snap length\n");
632 smb_fdata(const u_char
*buf
, const char *fmt
, const u_char
*maxbuf
)
634 static int depth
= 0;
642 while (buf
< maxbuf
) {
645 buf2
= smb_fdata(buf
, fmt
, maxbuf
);
675 memset(s
, 0, sizeof(s
));
676 p
= strchr(fmt
, ']');
677 if (p
- fmt
+ 1 > sizeof(s
)) {
681 strncpy(s
, fmt
, p
- fmt
);
684 buf
= smb_fdata1(buf
, s
, maxbuf
);
696 if (!depth
&& buf
< maxbuf
) {
697 size_t len
= PTR_DIFF(maxbuf
, buf
);
698 printf("Data: (%lu bytes)\n", (unsigned long)len
);
699 print_data(buf
, len
);
711 /* Dos Error Messages */
712 static err_code_struct dos_msgs
[] = {
713 { "ERRbadfunc", 1, "Invalid function." },
714 { "ERRbadfile", 2, "File not found." },
715 { "ERRbadpath", 3, "Directory invalid." },
716 { "ERRnofids", 4, "No file descriptors available" },
717 { "ERRnoaccess", 5, "Access denied." },
718 { "ERRbadfid", 6, "Invalid file handle." },
719 { "ERRbadmcb", 7, "Memory control blocks destroyed." },
720 { "ERRnomem", 8, "Insufficient server memory to perform the requested function." },
721 { "ERRbadmem", 9, "Invalid memory block address." },
722 { "ERRbadenv", 10, "Invalid environment." },
723 { "ERRbadformat", 11, "Invalid format." },
724 { "ERRbadaccess", 12, "Invalid open mode." },
725 { "ERRbaddata", 13, "Invalid data." },
726 { "ERR", 14, "reserved." },
727 { "ERRbaddrive", 15, "Invalid drive specified." },
728 { "ERRremcd", 16, "A Delete Directory request attempted to remove the server's current directory." },
729 { "ERRdiffdevice", 17, "Not same device." },
730 { "ERRnofiles", 18, "A File Search command can find no more files matching the specified criteria." },
731 { "ERRbadshare", 32, "The sharing mode specified for an Open conflicts with existing FIDs on the file." },
732 { "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." },
733 { "ERRfilexists", 80, "The file named in a Create Directory, Make New File or Link request already exists." },
734 { "ERRbadpipe", 230, "Pipe invalid." },
735 { "ERRpipebusy", 231, "All instances of the requested pipe are busy." },
736 { "ERRpipeclosing", 232, "Pipe close in progress." },
737 { "ERRnotconnected", 233, "No process on other end of pipe." },
738 { "ERRmoredata", 234, "There is more data to be returned." },
742 /* Server Error Messages */
743 err_code_struct server_msgs
[] = {
744 { "ERRerror", 1, "Non-specific error code." },
745 { "ERRbadpw", 2, "Bad password - name/password pair in a Tree Connect or Session Setup are invalid." },
746 { "ERRbadtype", 3, "reserved." },
747 { "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." },
748 { "ERRinvnid", 5, "The tree ID (TID) specified in a command was invalid." },
749 { "ERRinvnetname", 6, "Invalid network name in tree connect." },
750 { "ERRinvdevice", 7, "Invalid device - printer request made to non-printer connection or non-printer request made to printer connection." },
751 { "ERRqfull", 49, "Print queue full (files) -- returned by open print file." },
752 { "ERRqtoobig", 50, "Print queue full -- no space." },
753 { "ERRqeof", 51, "EOF on print queue dump." },
754 { "ERRinvpfid", 52, "Invalid print file FID." },
755 { "ERRsmbcmd", 64, "The server did not recognize the command received." },
756 { "ERRsrverror", 65, "The server encountered an internal error, e.g., system file unavailable." },
757 { "ERRfilespecs", 67, "The file handle (FID) and pathname parameters contained an invalid combination of values." },
758 { "ERRreserved", 68, "reserved." },
759 { "ERRbadpermits", 69, "The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute." },
760 { "ERRreserved", 70, "reserved." },
761 { "ERRsetattrmode", 71, "The attribute mode in the Set File Attribute request is invalid." },
762 { "ERRpaused", 81, "Server is paused." },
763 { "ERRmsgoff", 82, "Not receiving messages." },
764 { "ERRnoroom", 83, "No room to buffer message." },
765 { "ERRrmuns", 87, "Too many remote user names." },
766 { "ERRtimeout", 88, "Operation timed out." },
767 { "ERRnoresource", 89, "No resources currently available for request." },
768 { "ERRtoomanyuids", 90, "Too many UIDs active on this session." },
769 { "ERRbaduid", 91, "The UID is not known as a valid ID on this session." },
770 { "ERRusempx", 250, "Temp unable to support Raw, use MPX mode." },
771 { "ERRusestd", 251, "Temp unable to support Raw, use standard read/write." },
772 { "ERRcontmpx", 252, "Continue in MPX mode." },
773 { "ERRreserved", 253, "reserved." },
774 { "ERRreserved", 254, "reserved." },
775 { "ERRnosupport", 0xFFFF, "Function not supported." },
779 /* Hard Error Messages */
780 err_code_struct hard_msgs
[] = {
781 { "ERRnowrite", 19, "Attempt to write on write-protected diskette." },
782 { "ERRbadunit", 20, "Unknown unit." },
783 { "ERRnotready", 21, "Drive not ready." },
784 { "ERRbadcmd", 22, "Unknown command." },
785 { "ERRdata", 23, "Data error (CRC)." },
786 { "ERRbadreq", 24, "Bad request structure length." },
787 { "ERRseek", 25 , "Seek error." },
788 { "ERRbadmedia", 26, "Unknown media type." },
789 { "ERRbadsector", 27, "Sector not found." },
790 { "ERRnopaper", 28, "Printer out of paper." },
791 { "ERRwrite", 29, "Write fault." },
792 { "ERRread", 30, "Read fault." },
793 { "ERRgeneral", 31, "General failure." },
794 { "ERRbadshare", 32, "A open conflicts with an existing open." },
795 { "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." },
796 { "ERRwrongdisk", 34, "The wrong disk was found in a drive." },
797 { "ERRFCBUnavail", 35, "No FCBs are available to process request." },
798 { "ERRsharebufexc", 36, "A sharing buffer has been exceeded." },
805 err_code_struct
*err_msgs
;
807 { 0, "SUCCESS", NULL
},
808 { 0x01, "ERRDOS", dos_msgs
},
809 { 0x02, "ERRSRV", server_msgs
},
810 { 0x03, "ERRHRD", hard_msgs
},
811 { 0x04, "ERRXOS", NULL
},
812 { 0xE1, "ERRRMX1", NULL
},
813 { 0xE2, "ERRRMX2", NULL
},
814 { 0xE3, "ERRRMX3", NULL
},
815 { 0xFF, "ERRCMD", NULL
},
820 * return a SMB error string from a SMB buffer
823 smb_errstr(int class, int num
)
825 static char ret
[128];
830 for (i
= 0; err_classes
[i
].class; i
++)
831 if (err_classes
[i
].code
== class) {
832 if (err_classes
[i
].err_msgs
) {
833 err_code_struct
*err
= err_classes
[i
].err_msgs
;
834 for (j
= 0; err
[j
].name
; j
++)
835 if (num
== err
[j
].code
) {
836 snprintf(ret
, sizeof(ret
), "%s - %s (%s)",
837 err_classes
[i
].class, err
[j
].name
, err
[j
].message
);
842 snprintf(ret
, sizeof(ret
), "%s - %d", err_classes
[i
].class, num
);
846 snprintf(ret
, sizeof(ret
), "ERROR: Unknown error (%d,%d)", class, num
);