]> The Tcpdump Group git mirrors - tcpdump/blob - smbutil.c
Get rid of unneeded includes of <net/if.h>.
[tcpdump] / smbutil.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 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #ifndef lint
13 static const char rcsid[] =
14 "@(#) $Header: /tcpdump/master/tcpdump/smbutil.c,v 1.11 2000-09-28 06:43:09 guy Exp $";
15 #endif
16
17 #include <sys/param.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21
22
23 #include <netinet/in.h>
24
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "interface.h"
32 #include "smb.h"
33
34 extern uchar *startbuf;
35
36 /*******************************************************************
37 interpret a 32 bit dos packed date/time to some parameters
38 ********************************************************************/
39 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
40 {
41 uint32 p0,p1,p2,p3;
42
43 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
44 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
45
46 *second = 2*(p0 & 0x1F);
47 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
48 *hour = (p1>>3)&0xFF;
49 *day = (p2&0x1F);
50 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
51 *year = ((p3>>1)&0xFF) + 80;
52 }
53
54 /*******************************************************************
55 create a unix date from a dos date
56 ********************************************************************/
57 static time_t make_unix_date(const void *date_ptr)
58 {
59 uint32 dos_date=0;
60 struct tm t;
61
62 dos_date = IVAL(date_ptr,0);
63
64 if (dos_date == 0) return(0);
65
66 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
67 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
68 t.tm_wday = 1;
69 t.tm_yday = 1;
70 t.tm_isdst = 0;
71
72 return (mktime(&t));
73 }
74
75 /*******************************************************************
76 create a unix date from a dos date
77 ********************************************************************/
78 static time_t make_unix_date2(const void *date_ptr)
79 {
80 uint32 x,x2;
81
82 x = IVAL(date_ptr,0);
83 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
84 SIVAL(&x,0,x2);
85
86 return(make_unix_date((void *)&x));
87 }
88
89 /****************************************************************************
90 interpret an 8 byte "filetime" structure to a time_t
91 It's originally in "100ns units since jan 1st 1601"
92 ****************************************************************************/
93 static time_t interpret_long_date(const char *p)
94 {
95 double d;
96 time_t ret;
97
98 /* this gives us seconds since jan 1st 1601 (approx) */
99 d = (IVAL(p,4)*256.0 + CVAL(p,3)) * (1.0e-7 * (1<<24));
100
101 /* now adjust by 369 years to make the secs since 1970 */
102 d -= 369.0*365.25*24*60*60;
103
104 /* and a fudge factor as we got it wrong by a few days */
105 d += (3*24*60*60 + 6*60*60 + 2);
106
107 if (d<0)
108 return(0);
109
110 ret = (time_t)d;
111
112 return(ret);
113 }
114
115
116 /****************************************************************************
117 interpret the weird netbios "name". Return the name type
118 ****************************************************************************/
119 static int name_interpret(char *in,char *out)
120 {
121 int ret;
122 int len = (*in++) / 2;
123
124 *out=0;
125
126 if (len > 30 || len<1) return(0);
127
128 while (len--)
129 {
130 if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
131 *out = 0;
132 return(0);
133 }
134 *out = ((in[0]-'A')<<4) + (in[1]-'A');
135 in += 2;
136 out++;
137 }
138 *out = 0;
139 ret = out[-1];
140
141 return(ret);
142 }
143
144 /****************************************************************************
145 find a pointer to a netbios name
146 ****************************************************************************/
147 static char *name_ptr(char *buf,int ofs)
148 {
149 unsigned char c = *(unsigned char *)(buf+ofs);
150
151 if ((c & 0xC0) == 0xC0)
152 {
153 uint16 l = RSVAL(buf, ofs) & 0x3FFF;
154 return(buf + l);
155 }
156 else
157 return(buf+ofs);
158 }
159
160 /****************************************************************************
161 extract a netbios name from a buf
162 ****************************************************************************/
163 static int name_extract(char *buf,int ofs,char *name)
164 {
165 char *p = name_ptr(buf,ofs);
166 strcpy(name,"");
167 return(name_interpret(p,name));
168 }
169
170
171 /****************************************************************************
172 return the total storage length of a mangled name
173 ****************************************************************************/
174 static int name_len(const unsigned char *s)
175 {
176 const char *s0 = s;
177 unsigned char c = *(unsigned char *)s;
178 if ((c & 0xC0) == 0xC0)
179 return(2);
180 while (*s) s += (*s)+1;
181 return(PTR_DIFF(s,s0)+1);
182 }
183
184 static void print_asc(const unsigned char *buf,int len)
185 {
186 int i;
187 for (i=0;i<len;i++)
188 printf("%c",isprint(buf[i])?buf[i]:'.');
189 }
190
191 static char *name_type_str(int name_type)
192 {
193 static char *f = NULL;
194 switch (name_type) {
195 case 0: f = "Workstation"; break;
196 case 0x03: f = "Client?"; break;
197 case 0x20: f = "Server"; break;
198 case 0x1d: f = "Master Browser"; break;
199 case 0x1b: f = "Domain Controller"; break;
200 case 0x1e: f = "Browser Server"; break;
201 default: f = "Unknown"; break;
202 }
203 return(f);
204 }
205
206 void print_data(const unsigned char *buf, int len)
207 {
208 int i=0;
209 if (len<=0) return;
210 printf("[%03X] ",i);
211 for (i=0;i<len;) {
212 printf("%02X ",(int)buf[i]);
213 i++;
214 if (i%8 == 0) printf(" ");
215 if (i%16 == 0) {
216 print_asc(&buf[i-16],8); printf(" ");
217 print_asc(&buf[i-8],8); printf("\n");
218 if (i<len) printf("[%03X] ",i);
219 }
220 }
221 if (i%16) {
222 int n;
223
224 n = 16 - (i%16);
225 printf(" ");
226 if (n>8) printf(" ");
227 while (n--) printf(" ");
228
229 n = MIN(8,i%16);
230 print_asc(&buf[i-(i%16)],n); printf(" ");
231 n = (i%16) - n;
232 if (n>0) print_asc(&buf[i-n],n);
233 printf("\n");
234 }
235 }
236
237
238 static void write_bits(unsigned int val,char *fmt)
239 {
240 char *p = fmt;
241 int i=0;
242
243 while ((p=strchr(fmt,'|'))) {
244 int l = PTR_DIFF(p,fmt);
245 if (l && (val & (1<<i)))
246 printf("%.*s ",l,fmt);
247 fmt = p+1;
248 i++;
249 }
250 }
251
252 /* convert a unicode string */
253 static const char *unistr(const char *s, int *len)
254 {
255 static char buf[1000];
256 int l=0;
257 static int use_unicode = -1;
258
259 if (use_unicode == -1) {
260 char *p = getenv("USE_UNICODE");
261 if (p && (atoi(p) == 1))
262 use_unicode = 1;
263 else
264 use_unicode = 0;
265 }
266
267 /* maybe it isn't unicode - a cheap trick */
268 if (!use_unicode || (s[0] && s[1])) {
269 *len = strlen(s)+1;
270 return s;
271 }
272
273 *len = 0;
274
275 if (s[0] == 0 && s[1] != 0) {
276 s++;
277 *len = 1;
278 }
279
280 while (l < (sizeof(buf)-1) && s[0] && s[1] == 0) {
281 buf[l] = s[0];
282 s += 2; l++;
283 *len += 2;
284 }
285 buf[l] = 0;
286 *len += 2;
287 return buf;
288 }
289
290 static const uchar *fdata1(const uchar *buf, const char *fmt, const uchar *maxbuf)
291 {
292 int reverse=0;
293 char *attrib_fmt = "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
294 int len;
295
296 while (*fmt && buf<maxbuf) {
297 switch (*fmt) {
298 case 'a':
299 write_bits(CVAL(buf,0),attrib_fmt);
300 buf++; fmt++;
301 break;
302
303 case 'A':
304 write_bits(SVAL(buf,0),attrib_fmt);
305 buf+=2; fmt++;
306 break;
307
308 case '{':
309 {
310 char bitfmt[128];
311 char *p = strchr(++fmt,'}');
312 int l = PTR_DIFF(p,fmt);
313 strncpy(bitfmt,fmt,l);
314 bitfmt[l]=0;
315 fmt = p+1;
316 write_bits(CVAL(buf,0),bitfmt);
317 buf++;
318 break;
319 }
320
321 case 'P':
322 {
323 int l = atoi(fmt+1);
324 buf += l;
325 fmt++;
326 while (isdigit(*fmt)) fmt++;
327 break;
328 }
329 case 'r':
330 reverse = !reverse;
331 fmt++;
332 break;
333 case 'D':
334 {
335 unsigned int x = reverse?RIVAL(buf,0):IVAL(buf,0);
336 printf("%d (0x%x)",x, x);
337 buf += 4;
338 fmt++;
339 break;
340 }
341 case 'L':
342 {
343 unsigned int x1 = reverse?RIVAL(buf,0):IVAL(buf,0);
344 unsigned int x2 = reverse?RIVAL(buf,4):IVAL(buf,4);
345 if (x2) {
346 printf("0x%08x:%08x",x2, x1);
347 } else {
348 printf("%d (0x%08x%08x)",x1, x2, x1);
349 }
350 buf += 8;
351 fmt++;
352 break;
353 }
354 case 'd':
355 {
356 unsigned int x = reverse?RSVAL(buf,0):SVAL(buf,0);
357 printf("%d (0x%x)",x, x);
358 buf += 2;
359 fmt++;
360 break;
361 }
362 case 'W':
363 {
364 unsigned int x = reverse?RIVAL(buf,0):IVAL(buf,0);
365 printf("0x%X",x);
366 buf += 4;
367 fmt++;
368 break;
369 }
370 case 'w':
371 {
372 unsigned int x = reverse?RSVAL(buf,0):SVAL(buf,0);
373 printf("0x%X",x);
374 buf += 2;
375 fmt++;
376 break;
377 }
378 case 'B':
379 {
380 unsigned int x = CVAL(buf,0);
381 printf("0x%X",x);
382 buf += 1;
383 fmt++;
384 break;
385 }
386 case 'b':
387 {
388 unsigned int x = CVAL(buf,0);
389 printf("%d (0x%x)",x, x);
390 buf += 1;
391 fmt++;
392 break;
393 }
394 case 'S':
395 {
396 printf("%.*s",(int)PTR_DIFF(maxbuf,buf),unistr(buf, &len));
397 buf += len;
398 fmt++;
399 break;
400 }
401 case 'Z':
402 {
403 if (*buf != 4 && *buf != 2)
404 printf("Error! ASCIIZ buffer of type %d (safety=%d)\n",
405 *buf,(int)PTR_DIFF(maxbuf,buf));
406 printf("%.*s",(int)PTR_DIFF(maxbuf,buf+1),unistr(buf+1, &len));
407 buf += len+1;
408 fmt++;
409 break;
410 }
411 case 's':
412 {
413 int l = atoi(fmt+1);
414 printf("%-*.*s",l,l,buf);
415 buf += l;
416 fmt++; while (isdigit(*fmt)) fmt++;
417 break;
418 }
419 case 'h':
420 {
421 int l = atoi(fmt+1);
422 while (l--) printf("%02x",*buf++);
423 fmt++; while (isdigit(*fmt)) fmt++;
424 break;
425 }
426 case 'n':
427 {
428 int t = atoi(fmt+1);
429 char nbuf[255];
430 int name_type;
431 switch (t) {
432 case 1:
433 name_type = name_extract(startbuf,PTR_DIFF(buf,startbuf),nbuf);
434 buf += name_len(buf);
435 printf("%-15.15s NameType=0x%02X (%s)",
436 nbuf,name_type,name_type_str(name_type));
437 break;
438 case 2:
439 name_type = buf[15];
440 printf("%-15.15s NameType=0x%02X (%s)",
441 buf,name_type,name_type_str(name_type));
442 buf += 16;
443 break;
444 }
445 fmt++; while (isdigit(*fmt)) fmt++;
446 break;
447 }
448 case 'T':
449 {
450 time_t t;
451 int x = IVAL(buf,0);
452 switch (atoi(fmt+1)) {
453 case 1:
454 if (x==0 || x==-1 || x==0xFFFFFFFF)
455 t = 0;
456 else
457 t = make_unix_date(buf);
458 buf+=4;
459 break;
460 case 2:
461 if (x==0 || x==-1 || x==0xFFFFFFFF)
462 t = 0;
463 else
464 t = make_unix_date2(buf);
465 buf+=4;
466 break;
467 case 3:
468 t = interpret_long_date(buf);
469 buf+=8;
470 break;
471 }
472 printf("%s",t?asctime(localtime(&t)):"NULL\n");
473 fmt++; while (isdigit(*fmt)) fmt++;
474 break;
475 }
476 default:
477 putchar(*fmt);
478 fmt++;
479 break;
480 }
481 }
482
483 if (buf>=maxbuf && *fmt)
484 printf("END OF BUFFER\n");
485
486 return(buf);
487 }
488
489 const uchar *fdata(const uchar *buf, const char *fmt, const uchar *maxbuf)
490 {
491 static int depth=0;
492 char s[128];
493 char *p;
494
495 while (*fmt) {
496 switch (*fmt) {
497 case '*':
498 fmt++;
499 while (buf < maxbuf) {
500 const uchar *buf2;
501 depth++;
502 buf2 = fdata(buf,fmt,maxbuf);
503 depth--;
504 if (buf2 == buf) return(buf);
505 buf = buf2;
506 }
507 break;
508
509 case '|':
510 fmt++;
511 if (buf>=maxbuf) return(buf);
512 break;
513
514 case '%':
515 fmt++;
516 buf=maxbuf;
517 break;
518
519 case '#':
520 fmt++;
521 return(buf);
522 break;
523
524 case '[':
525 fmt++;
526 if (buf>=maxbuf) return(buf);
527 memset(s, 0, sizeof(s));
528 p = strchr(fmt,']');
529 strncpy(s,fmt,p-fmt);
530 fmt = p+1;
531 buf = fdata1(buf,s,maxbuf);
532 break;
533
534 default:
535 putchar(*fmt); fmt++;
536 fflush(stdout);
537 break;
538 }
539 }
540 if (!depth && buf<maxbuf) {
541 int len = PTR_DIFF(maxbuf,buf);
542 printf("Data: (%d bytes)\n",len);
543 print_data(buf,len);
544 return(buf+len);
545 }
546 return(buf);
547 }
548
549 typedef struct
550 {
551 char *name;
552 int code;
553 char *message;
554 } err_code_struct;
555
556 /* Dos Error Messages */
557 static err_code_struct dos_msgs[] = {
558 {"ERRbadfunc",1,"Invalid function."},
559 {"ERRbadfile",2,"File not found."},
560 {"ERRbadpath",3,"Directory invalid."},
561 {"ERRnofids",4,"No file descriptors available"},
562 {"ERRnoaccess",5,"Access denied."},
563 {"ERRbadfid",6,"Invalid file handle."},
564 {"ERRbadmcb",7,"Memory control blocks destroyed."},
565 {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
566 {"ERRbadmem",9,"Invalid memory block address."},
567 {"ERRbadenv",10,"Invalid environment."},
568 {"ERRbadformat",11,"Invalid format."},
569 {"ERRbadaccess",12,"Invalid open mode."},
570 {"ERRbaddata",13,"Invalid data."},
571 {"ERR",14,"reserved."},
572 {"ERRbaddrive",15,"Invalid drive specified."},
573 {"ERRremcd",16,"A Delete Directory request attempted to remove the server's current directory."},
574 {"ERRdiffdevice",17,"Not same device."},
575 {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
576 {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing FIDs on the file."},
577 {"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."},
578 {"ERRfilexists",80,"The file named in a Create Directory, Make New File or Link request already exists."},
579 {"ERRbadpipe",230,"Pipe invalid."},
580 {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
581 {"ERRpipeclosing",232,"Pipe close in progress."},
582 {"ERRnotconnected",233,"No process on other end of pipe."},
583 {"ERRmoredata",234,"There is more data to be returned."},
584 {NULL,-1,NULL}};
585
586 /* Server Error Messages */
587 err_code_struct server_msgs[] = {
588 {"ERRerror",1,"Non-specific error code."},
589 {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
590 {"ERRbadtype",3,"reserved."},
591 {"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."},
592 {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
593 {"ERRinvnetname",6,"Invalid network name in tree connect."},
594 {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or non-printer request made to printer connection."},
595 {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
596 {"ERRqtoobig",50,"Print queue full -- no space."},
597 {"ERRqeof",51,"EOF on print queue dump."},
598 {"ERRinvpfid",52,"Invalid print file FID."},
599 {"ERRsmbcmd",64,"The server did not recognize the command received."},
600 {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
601 {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid combination of values."},
602 {"ERRreserved",68,"reserved."},
603 {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute."},
604 {"ERRreserved",70,"reserved."},
605 {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
606 {"ERRpaused",81,"Server is paused."},
607 {"ERRmsgoff",82,"Not receiving messages."},
608 {"ERRnoroom",83,"No room to buffer message."},
609 {"ERRrmuns",87,"Too many remote user names."},
610 {"ERRtimeout",88,"Operation timed out."},
611 {"ERRnoresource",89,"No resources currently available for request."},
612 {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
613 {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
614 {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
615 {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
616 {"ERRcontmpx",252,"Continue in MPX mode."},
617 {"ERRreserved",253,"reserved."},
618 {"ERRreserved",254,"reserved."},
619 {"ERRnosupport",0xFFFF,"Function not supported."},
620 {NULL,-1,NULL}};
621
622 /* Hard Error Messages */
623 err_code_struct hard_msgs[] = {
624 {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
625 {"ERRbadunit",20,"Unknown unit."},
626 {"ERRnotready",21,"Drive not ready."},
627 {"ERRbadcmd",22,"Unknown command."},
628 {"ERRdata",23,"Data error (CRC)."},
629 {"ERRbadreq",24,"Bad request structure length."},
630 {"ERRseek",25 ,"Seek error."},
631 {"ERRbadmedia",26,"Unknown media type."},
632 {"ERRbadsector",27,"Sector not found."},
633 {"ERRnopaper",28,"Printer out of paper."},
634 {"ERRwrite",29,"Write fault."},
635 {"ERRread",30,"Read fault."},
636 {"ERRgeneral",31,"General failure."},
637 {"ERRbadshare",32,"A open conflicts with an existing open."},
638 {"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."},
639 {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
640 {"ERRFCBUnavail",35,"No FCBs are available to process request."},
641 {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
642 {NULL,-1,NULL}};
643
644
645 static struct
646 {
647 int code;
648 char *class;
649 err_code_struct *err_msgs;
650 } err_classes[] = {
651 {0,"SUCCESS",NULL},
652 {0x01,"ERRDOS",dos_msgs},
653 {0x02,"ERRSRV",server_msgs},
654 {0x03,"ERRHRD",hard_msgs},
655 {0x04,"ERRXOS",NULL},
656 {0xE1,"ERRRMX1",NULL},
657 {0xE2,"ERRRMX2",NULL},
658 {0xE3,"ERRRMX3",NULL},
659 {0xFF,"ERRCMD",NULL},
660 {-1,NULL,NULL}};
661
662
663 /****************************************************************************
664 return a SMB error string from a SMB buffer
665 ****************************************************************************/
666 char *smb_errstr(int class,int num)
667 {
668 static char ret[128];
669 int i,j;
670
671 ret[0]=0;
672
673 for (i=0;err_classes[i].class;i++)
674 if (err_classes[i].code == class)
675 {
676 if (err_classes[i].err_msgs)
677 {
678 err_code_struct *err = err_classes[i].err_msgs;
679 for (j=0;err[j].name;j++)
680 if (num == err[j].code)
681 {
682 snprintf(ret,sizeof(ret),"%s - %s (%s)",err_classes[i].class,
683 err[j].name,err[j].message);
684 return ret;
685 }
686 }
687
688 snprintf(ret,sizeof(ret),"%s - %d",err_classes[i].class,num);
689 return ret;
690 }
691
692 snprintf(ret,sizeof(ret),"ERROR: Unknown error (%d,%d)",class,num);
693 return(ret);
694 }
695
696
697