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
12 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/if_ether.h>
27 #include "interface.h"
30 extern uchar
*startbuf
;
32 /*******************************************************************
33 interpret a 32 bit dos packed date/time to some parameters
34 ********************************************************************/
35 static void interpret_dos_date(uint32 date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
39 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
40 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
42 *second
= 2*(p0
& 0x1F);
43 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
46 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
47 *year
= ((p3
>>1)&0xFF) + 80;
50 /*******************************************************************
51 create a unix date from a dos date
52 ********************************************************************/
53 static time_t make_unix_date(const void *date_ptr
)
58 dos_date
= IVAL(date_ptr
,0);
60 if (dos_date
== 0) return(0);
62 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
63 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
71 /*******************************************************************
72 create a unix date from a dos date
73 ********************************************************************/
74 static time_t make_unix_date2(const void *date_ptr
)
79 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
82 return(make_unix_date((void *)&x
));
85 /****************************************************************************
86 interpret an 8 byte "filetime" structure to a time_t
87 It's originally in "100ns units since jan 1st 1601"
88 ****************************************************************************/
89 static time_t interpret_long_date(const char *p
)
94 /* this gives us seconds since jan 1st 1601 (approx) */
95 d
= (IVAL(p
,4)*256.0 + CVAL(p
,3)) * (1.0e-7 * (1<<24));
97 /* now adjust by 369 years to make the secs since 1970 */
98 d
-= 369.0*365.25*24*60*60;
100 /* and a fudge factor as we got it wrong by a few days */
101 d
+= (3*24*60*60 + 6*60*60 + 2);
112 /****************************************************************************
113 interpret the weird netbios "name". Return the name type
114 ****************************************************************************/
115 static int name_interpret(char *in
,char *out
)
118 int len
= (*in
++) / 2;
122 if (len
> 30 || len
<1) return(0);
126 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
130 *out
= ((in
[0]-'A')<<4) + (in
[1]-'A');
140 /****************************************************************************
141 find a pointer to a netbios name
142 ****************************************************************************/
143 static char *name_ptr(char *buf
,int ofs
)
145 unsigned char c
= *(unsigned char *)(buf
+ofs
);
147 if ((c
& 0xC0) == 0xC0)
149 uint16 l
= RSVAL(buf
, ofs
) & 0x3FFF;
156 /****************************************************************************
157 extract a netbios name from a buf
158 ****************************************************************************/
159 static int name_extract(char *buf
,int ofs
,char *name
)
161 char *p
= name_ptr(buf
,ofs
);
163 return(name_interpret(p
,name
));
167 /****************************************************************************
168 return the total storage length of a mangled name
169 ****************************************************************************/
170 static int name_len(const unsigned char *s
)
173 unsigned char c
= *(unsigned char *)s
;
174 if ((c
& 0xC0) == 0xC0)
176 while (*s
) s
+= (*s
)+1;
177 return(PTR_DIFF(s
,s0
)+1);
180 static void print_asc(const unsigned char *buf
,int len
)
184 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
187 static char *name_type_str(int name_type
)
189 static char *f
= NULL
;
191 case 0: f
= "Workstation"; break;
192 case 0x03: f
= "Client?"; break;
193 case 0x20: f
= "Server"; break;
194 case 0x1d: f
= "Master Browser"; break;
195 case 0x1b: f
= "Domain Controller"; break;
196 case 0x1e: f
= "Browser Server"; break;
197 default: f
= "Unknown"; break;
202 void print_data(const unsigned char *buf
, int len
)
208 printf("%02X ",(int)buf
[i
]);
210 if (i
%8 == 0) printf(" ");
212 print_asc(&buf
[i
-16],8); printf(" ");
213 print_asc(&buf
[i
-8],8); printf("\n");
214 if (i
<len
) printf("[%03X] ",i
);
222 if (n
>8) printf(" ");
223 while (n
--) printf(" ");
226 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
228 if (n
>0) print_asc(&buf
[i
-n
],n
);
234 static void write_bits(unsigned int val
,char *fmt
)
239 while ((p
=strchr(fmt
,'|'))) {
240 int l
= PTR_DIFF(p
,fmt
);
241 if (l
&& (val
& (1<<i
)))
242 printf("%.*s ",l
,fmt
);
248 /* convert a unicode string */
249 static const char *unistr(const char *s
, int *len
)
251 static char buf
[1000];
253 static int use_unicode
= -1;
255 if (use_unicode
== -1) {
256 char *p
= getenv("USE_UNICODE");
257 if (p
&& (atoi(p
) == 1))
263 /* maybe it isn't unicode - a cheap trick */
264 if (!use_unicode
|| (s
[0] && s
[1])) {
271 if (s
[0] == 0 && s
[1] != 0) {
276 while (l
< (sizeof(buf
)-1) && s
[0] && s
[1] == 0) {
286 static const uchar
*fdata1(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
289 char *attrib_fmt
= "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
292 while (*fmt
&& buf
<maxbuf
) {
295 write_bits(CVAL(buf
,0),attrib_fmt
);
300 write_bits(SVAL(buf
,0),attrib_fmt
);
307 char *p
= strchr(++fmt
,'}');
308 int l
= PTR_DIFF(p
,fmt
);
309 strncpy(bitfmt
,fmt
,l
);
312 write_bits(CVAL(buf
,0),bitfmt
);
322 while (isdigit(*fmt
)) fmt
++;
331 unsigned int x
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
332 printf("%d (0x%x)",x
, x
);
339 unsigned int x1
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
340 unsigned int x2
= reverse
?RIVAL(buf
,4):IVAL(buf
,4);
342 printf("0x%08x:%08x",x2
, x1
);
344 printf("%d (0x%08x%08x)",x1
, x2
, x1
);
352 unsigned int x
= reverse
?RSVAL(buf
,0):SVAL(buf
,0);
353 printf("%d (0x%x)",x
, x
);
360 unsigned int x
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
368 unsigned int x
= reverse
?RSVAL(buf
,0):SVAL(buf
,0);
376 unsigned int x
= CVAL(buf
,0);
384 unsigned int x
= CVAL(buf
,0);
385 printf("%d (0x%x)",x
, x
);
392 printf("%.*s",(int)PTR_DIFF(maxbuf
,buf
),unistr(buf
, &len
));
399 if (*buf
!= 4 && *buf
!= 2)
400 printf("Error! ASCIIZ buffer of type %d (safety=%d)\n",
401 *buf
,(int)PTR_DIFF(maxbuf
,buf
));
402 printf("%.*s",(int)PTR_DIFF(maxbuf
,buf
+1),unistr(buf
+1, &len
));
410 printf("%-*.*s",l
,l
,buf
);
412 fmt
++; while (isdigit(*fmt
)) fmt
++;
418 while (l
--) printf("%02x",*buf
++);
419 fmt
++; while (isdigit(*fmt
)) fmt
++;
429 name_type
= name_extract(startbuf
,PTR_DIFF(buf
,startbuf
),nbuf
);
430 buf
+= name_len(buf
);
431 printf("%-15.15s NameType=0x%02X (%s)",
432 nbuf
,name_type
,name_type_str(name_type
));
436 printf("%-15.15s NameType=0x%02X (%s)",
437 buf
,name_type
,name_type_str(name_type
));
441 fmt
++; while (isdigit(*fmt
)) fmt
++;
448 switch (atoi(fmt
+1)) {
450 if (x
==0 || x
==-1 || x
==0xFFFFFFFF)
453 t
= make_unix_date(buf
);
457 if (x
==0 || x
==-1 || x
==0xFFFFFFFF)
460 t
= make_unix_date2(buf
);
464 t
= interpret_long_date(buf
);
468 printf("%s",t
?asctime(localtime(&t
)):"NULL\n");
469 fmt
++; while (isdigit(*fmt
)) fmt
++;
479 if (buf
>=maxbuf
&& *fmt
)
480 printf("END OF BUFFER\n");
485 const uchar
*fdata(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
495 while (buf
< maxbuf
) {
498 buf2
= fdata(buf
,fmt
,maxbuf
);
500 if (buf2
== buf
) return(buf
);
507 if (buf
>=maxbuf
) return(buf
);
522 if (buf
>=maxbuf
) return(buf
);
525 strncpy(s
,fmt
,p
-fmt
);
527 buf
= fdata1(buf
,s
,maxbuf
);
531 putchar(*fmt
); fmt
++;
536 if (!depth
&& buf
<maxbuf
) {
537 int len
= PTR_DIFF(maxbuf
,buf
);
538 printf("Data: (%d bytes)\n",len
);
552 /* Dos Error Messages */
553 static err_code_struct dos_msgs
[] = {
554 {"ERRbadfunc",1,"Invalid function."},
555 {"ERRbadfile",2,"File not found."},
556 {"ERRbadpath",3,"Directory invalid."},
557 {"ERRnofids",4,"No file descriptors available"},
558 {"ERRnoaccess",5,"Access denied."},
559 {"ERRbadfid",6,"Invalid file handle."},
560 {"ERRbadmcb",7,"Memory control blocks destroyed."},
561 {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
562 {"ERRbadmem",9,"Invalid memory block address."},
563 {"ERRbadenv",10,"Invalid environment."},
564 {"ERRbadformat",11,"Invalid format."},
565 {"ERRbadaccess",12,"Invalid open mode."},
566 {"ERRbaddata",13,"Invalid data."},
567 {"ERR",14,"reserved."},
568 {"ERRbaddrive",15,"Invalid drive specified."},
569 {"ERRremcd",16,"A Delete Directory request attempted to remove the server's current directory."},
570 {"ERRdiffdevice",17,"Not same device."},
571 {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
572 {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing FIDs on the file."},
573 {"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."},
574 {"ERRfilexists",80,"The file named in a Create Directory, Make New File or Link request already exists."},
575 {"ERRbadpipe",230,"Pipe invalid."},
576 {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
577 {"ERRpipeclosing",232,"Pipe close in progress."},
578 {"ERRnotconnected",233,"No process on other end of pipe."},
579 {"ERRmoredata",234,"There is more data to be returned."},
582 /* Server Error Messages */
583 err_code_struct server_msgs
[] = {
584 {"ERRerror",1,"Non-specific error code."},
585 {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
586 {"ERRbadtype",3,"reserved."},
587 {"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."},
588 {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
589 {"ERRinvnetname",6,"Invalid network name in tree connect."},
590 {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or non-printer request made to printer connection."},
591 {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
592 {"ERRqtoobig",50,"Print queue full -- no space."},
593 {"ERRqeof",51,"EOF on print queue dump."},
594 {"ERRinvpfid",52,"Invalid print file FID."},
595 {"ERRsmbcmd",64,"The server did not recognize the command received."},
596 {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
597 {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid combination of values."},
598 {"ERRreserved",68,"reserved."},
599 {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute."},
600 {"ERRreserved",70,"reserved."},
601 {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
602 {"ERRpaused",81,"Server is paused."},
603 {"ERRmsgoff",82,"Not receiving messages."},
604 {"ERRnoroom",83,"No room to buffer message."},
605 {"ERRrmuns",87,"Too many remote user names."},
606 {"ERRtimeout",88,"Operation timed out."},
607 {"ERRnoresource",89,"No resources currently available for request."},
608 {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
609 {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
610 {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
611 {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
612 {"ERRcontmpx",252,"Continue in MPX mode."},
613 {"ERRreserved",253,"reserved."},
614 {"ERRreserved",254,"reserved."},
615 {"ERRnosupport",0xFFFF,"Function not supported."},
618 /* Hard Error Messages */
619 err_code_struct hard_msgs
[] = {
620 {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
621 {"ERRbadunit",20,"Unknown unit."},
622 {"ERRnotready",21,"Drive not ready."},
623 {"ERRbadcmd",22,"Unknown command."},
624 {"ERRdata",23,"Data error (CRC)."},
625 {"ERRbadreq",24,"Bad request structure length."},
626 {"ERRseek",25 ,"Seek error."},
627 {"ERRbadmedia",26,"Unknown media type."},
628 {"ERRbadsector",27,"Sector not found."},
629 {"ERRnopaper",28,"Printer out of paper."},
630 {"ERRwrite",29,"Write fault."},
631 {"ERRread",30,"Read fault."},
632 {"ERRgeneral",31,"General failure."},
633 {"ERRbadshare",32,"A open conflicts with an existing open."},
634 {"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."},
635 {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
636 {"ERRFCBUnavail",35,"No FCBs are available to process request."},
637 {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
645 err_code_struct
*err_msgs
;
648 {0x01,"ERRDOS",dos_msgs
},
649 {0x02,"ERRSRV",server_msgs
},
650 {0x03,"ERRHRD",hard_msgs
},
651 {0x04,"ERRXOS",NULL
},
652 {0xE1,"ERRRMX1",NULL
},
653 {0xE2,"ERRRMX2",NULL
},
654 {0xE3,"ERRRMX3",NULL
},
655 {0xFF,"ERRCMD",NULL
},
659 /****************************************************************************
660 return a SMB error string from a SMB buffer
661 ****************************************************************************/
662 char *smb_errstr(int class,int num
)
664 static char ret
[128];
669 for (i
=0;err_classes
[i
].class;i
++)
670 if (err_classes
[i
].code
== class)
672 if (err_classes
[i
].err_msgs
)
674 err_code_struct
*err
= err_classes
[i
].err_msgs
;
675 for (j
=0;err
[j
].name
;j
++)
676 if (num
== err
[j
].code
)
678 sprintf(ret
,"%s - %s (%s)",err_classes
[i
].class,
679 err
[j
].name
,err
[j
].message
);
684 sprintf(ret
,"%s - %d",err_classes
[i
].class,num
);
688 sprintf(ret
,"ERROR: Unknown error (%d,%d)",class,num
);