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
13 static const char rcsid
[] =
14 "@(#) $Header: /tcpdump/master/tcpdump/smbutil.c,v 1.6 2000-01-17 06:24:27 itojun Exp $";
17 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/if_ether.h>
32 #include "interface.h"
35 extern uchar
*startbuf
;
37 /*******************************************************************
38 interpret a 32 bit dos packed date/time to some parameters
39 ********************************************************************/
40 static void interpret_dos_date(uint32 date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
44 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
45 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
47 *second
= 2*(p0
& 0x1F);
48 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
51 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
52 *year
= ((p3
>>1)&0xFF) + 80;
55 /*******************************************************************
56 create a unix date from a dos date
57 ********************************************************************/
58 static time_t make_unix_date(const void *date_ptr
)
63 dos_date
= IVAL(date_ptr
,0);
65 if (dos_date
== 0) return(0);
67 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
68 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
76 /*******************************************************************
77 create a unix date from a dos date
78 ********************************************************************/
79 static time_t make_unix_date2(const void *date_ptr
)
84 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
87 return(make_unix_date((void *)&x
));
90 /****************************************************************************
91 interpret an 8 byte "filetime" structure to a time_t
92 It's originally in "100ns units since jan 1st 1601"
93 ****************************************************************************/
94 static time_t interpret_long_date(const char *p
)
99 /* this gives us seconds since jan 1st 1601 (approx) */
100 d
= (IVAL(p
,4)*256.0 + CVAL(p
,3)) * (1.0e-7 * (1<<24));
102 /* now adjust by 369 years to make the secs since 1970 */
103 d
-= 369.0*365.25*24*60*60;
105 /* and a fudge factor as we got it wrong by a few days */
106 d
+= (3*24*60*60 + 6*60*60 + 2);
117 /****************************************************************************
118 interpret the weird netbios "name". Return the name type
119 ****************************************************************************/
120 static int name_interpret(char *in
,char *out
)
123 int len
= (*in
++) / 2;
127 if (len
> 30 || len
<1) return(0);
131 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
135 *out
= ((in
[0]-'A')<<4) + (in
[1]-'A');
145 /****************************************************************************
146 find a pointer to a netbios name
147 ****************************************************************************/
148 static char *name_ptr(char *buf
,int ofs
)
150 unsigned char c
= *(unsigned char *)(buf
+ofs
);
152 if ((c
& 0xC0) == 0xC0)
154 uint16 l
= RSVAL(buf
, ofs
) & 0x3FFF;
161 /****************************************************************************
162 extract a netbios name from a buf
163 ****************************************************************************/
164 static int name_extract(char *buf
,int ofs
,char *name
)
166 char *p
= name_ptr(buf
,ofs
);
168 return(name_interpret(p
,name
));
172 /****************************************************************************
173 return the total storage length of a mangled name
174 ****************************************************************************/
175 static int name_len(const unsigned char *s
)
178 unsigned char c
= *(unsigned char *)s
;
179 if ((c
& 0xC0) == 0xC0)
181 while (*s
) s
+= (*s
)+1;
182 return(PTR_DIFF(s
,s0
)+1);
185 static void print_asc(const unsigned char *buf
,int len
)
189 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
192 static char *name_type_str(int name_type
)
194 static char *f
= NULL
;
196 case 0: f
= "Workstation"; break;
197 case 0x03: f
= "Client?"; break;
198 case 0x20: f
= "Server"; break;
199 case 0x1d: f
= "Master Browser"; break;
200 case 0x1b: f
= "Domain Controller"; break;
201 case 0x1e: f
= "Browser Server"; break;
202 default: f
= "Unknown"; break;
207 void print_data(const unsigned char *buf
, int len
)
213 printf("%02X ",(int)buf
[i
]);
215 if (i
%8 == 0) printf(" ");
217 print_asc(&buf
[i
-16],8); printf(" ");
218 print_asc(&buf
[i
-8],8); printf("\n");
219 if (i
<len
) printf("[%03X] ",i
);
227 if (n
>8) printf(" ");
228 while (n
--) printf(" ");
231 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
233 if (n
>0) print_asc(&buf
[i
-n
],n
);
239 static void write_bits(unsigned int val
,char *fmt
)
244 while ((p
=strchr(fmt
,'|'))) {
245 int l
= PTR_DIFF(p
,fmt
);
246 if (l
&& (val
& (1<<i
)))
247 printf("%.*s ",l
,fmt
);
253 /* convert a unicode string */
254 static const char *unistr(const char *s
, int *len
)
256 static char buf
[1000];
258 static int use_unicode
= -1;
260 if (use_unicode
== -1) {
261 char *p
= getenv("USE_UNICODE");
262 if (p
&& (atoi(p
) == 1))
268 /* maybe it isn't unicode - a cheap trick */
269 if (!use_unicode
|| (s
[0] && s
[1])) {
276 if (s
[0] == 0 && s
[1] != 0) {
281 while (l
< (sizeof(buf
)-1) && s
[0] && s
[1] == 0) {
291 static const uchar
*fdata1(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
294 char *attrib_fmt
= "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
297 while (*fmt
&& buf
<maxbuf
) {
300 write_bits(CVAL(buf
,0),attrib_fmt
);
305 write_bits(SVAL(buf
,0),attrib_fmt
);
312 char *p
= strchr(++fmt
,'}');
313 int l
= PTR_DIFF(p
,fmt
);
314 strncpy(bitfmt
,fmt
,l
);
317 write_bits(CVAL(buf
,0),bitfmt
);
327 while (isdigit(*fmt
)) fmt
++;
336 unsigned int x
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
337 printf("%d (0x%x)",x
, x
);
344 unsigned int x1
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
345 unsigned int x2
= reverse
?RIVAL(buf
,4):IVAL(buf
,4);
347 printf("0x%08x:%08x",x2
, x1
);
349 printf("%d (0x%08x%08x)",x1
, x2
, x1
);
357 unsigned int x
= reverse
?RSVAL(buf
,0):SVAL(buf
,0);
358 printf("%d (0x%x)",x
, x
);
365 unsigned int x
= reverse
?RIVAL(buf
,0):IVAL(buf
,0);
373 unsigned int x
= reverse
?RSVAL(buf
,0):SVAL(buf
,0);
381 unsigned int x
= CVAL(buf
,0);
389 unsigned int x
= CVAL(buf
,0);
390 printf("%d (0x%x)",x
, x
);
397 printf("%.*s",(int)PTR_DIFF(maxbuf
,buf
),unistr(buf
, &len
));
404 if (*buf
!= 4 && *buf
!= 2)
405 printf("Error! ASCIIZ buffer of type %d (safety=%d)\n",
406 *buf
,(int)PTR_DIFF(maxbuf
,buf
));
407 printf("%.*s",(int)PTR_DIFF(maxbuf
,buf
+1),unistr(buf
+1, &len
));
415 printf("%-*.*s",l
,l
,buf
);
417 fmt
++; while (isdigit(*fmt
)) fmt
++;
423 while (l
--) printf("%02x",*buf
++);
424 fmt
++; while (isdigit(*fmt
)) fmt
++;
434 name_type
= name_extract(startbuf
,PTR_DIFF(buf
,startbuf
),nbuf
);
435 buf
+= name_len(buf
);
436 printf("%-15.15s NameType=0x%02X (%s)",
437 nbuf
,name_type
,name_type_str(name_type
));
441 printf("%-15.15s NameType=0x%02X (%s)",
442 buf
,name_type
,name_type_str(name_type
));
446 fmt
++; while (isdigit(*fmt
)) fmt
++;
453 switch (atoi(fmt
+1)) {
455 if (x
==0 || x
==-1 || x
==0xFFFFFFFF)
458 t
= make_unix_date(buf
);
462 if (x
==0 || x
==-1 || x
==0xFFFFFFFF)
465 t
= make_unix_date2(buf
);
469 t
= interpret_long_date(buf
);
473 printf("%s",t
?asctime(localtime(&t
)):"NULL\n");
474 fmt
++; while (isdigit(*fmt
)) fmt
++;
484 if (buf
>=maxbuf
&& *fmt
)
485 printf("END OF BUFFER\n");
490 const uchar
*fdata(const uchar
*buf
, const char *fmt
, const uchar
*maxbuf
)
500 while (buf
< maxbuf
) {
503 buf2
= fdata(buf
,fmt
,maxbuf
);
505 if (buf2
== buf
) return(buf
);
512 if (buf
>=maxbuf
) return(buf
);
527 if (buf
>=maxbuf
) return(buf
);
530 strncpy(s
,fmt
,p
-fmt
);
532 buf
= fdata1(buf
,s
,maxbuf
);
536 putchar(*fmt
); fmt
++;
541 if (!depth
&& buf
<maxbuf
) {
542 int len
= PTR_DIFF(maxbuf
,buf
);
543 printf("Data: (%d bytes)\n",len
);
557 /* Dos Error Messages */
558 static err_code_struct dos_msgs
[] = {
559 {"ERRbadfunc",1,"Invalid function."},
560 {"ERRbadfile",2,"File not found."},
561 {"ERRbadpath",3,"Directory invalid."},
562 {"ERRnofids",4,"No file descriptors available"},
563 {"ERRnoaccess",5,"Access denied."},
564 {"ERRbadfid",6,"Invalid file handle."},
565 {"ERRbadmcb",7,"Memory control blocks destroyed."},
566 {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
567 {"ERRbadmem",9,"Invalid memory block address."},
568 {"ERRbadenv",10,"Invalid environment."},
569 {"ERRbadformat",11,"Invalid format."},
570 {"ERRbadaccess",12,"Invalid open mode."},
571 {"ERRbaddata",13,"Invalid data."},
572 {"ERR",14,"reserved."},
573 {"ERRbaddrive",15,"Invalid drive specified."},
574 {"ERRremcd",16,"A Delete Directory request attempted to remove the server's current directory."},
575 {"ERRdiffdevice",17,"Not same device."},
576 {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
577 {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing FIDs on the file."},
578 {"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."},
579 {"ERRfilexists",80,"The file named in a Create Directory, Make New File or Link request already exists."},
580 {"ERRbadpipe",230,"Pipe invalid."},
581 {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
582 {"ERRpipeclosing",232,"Pipe close in progress."},
583 {"ERRnotconnected",233,"No process on other end of pipe."},
584 {"ERRmoredata",234,"There is more data to be returned."},
587 /* Server Error Messages */
588 err_code_struct server_msgs
[] = {
589 {"ERRerror",1,"Non-specific error code."},
590 {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
591 {"ERRbadtype",3,"reserved."},
592 {"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."},
593 {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
594 {"ERRinvnetname",6,"Invalid network name in tree connect."},
595 {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or non-printer request made to printer connection."},
596 {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
597 {"ERRqtoobig",50,"Print queue full -- no space."},
598 {"ERRqeof",51,"EOF on print queue dump."},
599 {"ERRinvpfid",52,"Invalid print file FID."},
600 {"ERRsmbcmd",64,"The server did not recognize the command received."},
601 {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
602 {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid combination of values."},
603 {"ERRreserved",68,"reserved."},
604 {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute."},
605 {"ERRreserved",70,"reserved."},
606 {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
607 {"ERRpaused",81,"Server is paused."},
608 {"ERRmsgoff",82,"Not receiving messages."},
609 {"ERRnoroom",83,"No room to buffer message."},
610 {"ERRrmuns",87,"Too many remote user names."},
611 {"ERRtimeout",88,"Operation timed out."},
612 {"ERRnoresource",89,"No resources currently available for request."},
613 {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
614 {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
615 {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
616 {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
617 {"ERRcontmpx",252,"Continue in MPX mode."},
618 {"ERRreserved",253,"reserved."},
619 {"ERRreserved",254,"reserved."},
620 {"ERRnosupport",0xFFFF,"Function not supported."},
623 /* Hard Error Messages */
624 err_code_struct hard_msgs
[] = {
625 {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
626 {"ERRbadunit",20,"Unknown unit."},
627 {"ERRnotready",21,"Drive not ready."},
628 {"ERRbadcmd",22,"Unknown command."},
629 {"ERRdata",23,"Data error (CRC)."},
630 {"ERRbadreq",24,"Bad request structure length."},
631 {"ERRseek",25 ,"Seek error."},
632 {"ERRbadmedia",26,"Unknown media type."},
633 {"ERRbadsector",27,"Sector not found."},
634 {"ERRnopaper",28,"Printer out of paper."},
635 {"ERRwrite",29,"Write fault."},
636 {"ERRread",30,"Read fault."},
637 {"ERRgeneral",31,"General failure."},
638 {"ERRbadshare",32,"A open conflicts with an existing open."},
639 {"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."},
640 {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
641 {"ERRFCBUnavail",35,"No FCBs are available to process request."},
642 {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
650 err_code_struct
*err_msgs
;
653 {0x01,"ERRDOS",dos_msgs
},
654 {0x02,"ERRSRV",server_msgs
},
655 {0x03,"ERRHRD",hard_msgs
},
656 {0x04,"ERRXOS",NULL
},
657 {0xE1,"ERRRMX1",NULL
},
658 {0xE2,"ERRRMX2",NULL
},
659 {0xE3,"ERRRMX3",NULL
},
660 {0xFF,"ERRCMD",NULL
},
664 /****************************************************************************
665 return a SMB error string from a SMB buffer
666 ****************************************************************************/
667 char *smb_errstr(int class,int num
)
669 static char ret
[128];
674 for (i
=0;err_classes
[i
].class;i
++)
675 if (err_classes
[i
].code
== class)
677 if (err_classes
[i
].err_msgs
)
679 err_code_struct
*err
= err_classes
[i
].err_msgs
;
680 for (j
=0;err
[j
].name
;j
++)
681 if (num
== err
[j
].code
)
683 snprintf(ret
,sizeof(ret
),"%s - %s (%s)",err_classes
[i
].class,
684 err
[j
].name
,err
[j
].message
);
689 snprintf(ret
,sizeof(ret
),"%s - %d",err_classes
[i
].class,num
);
693 snprintf(ret
,sizeof(ret
),"ERROR: Unknown error (%d,%d)",class,num
);