]> The Tcpdump Group git mirrors - tcpdump/blob - smbutil.c
add AC_REVISION
[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 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16
17 #include <net/if.h>
18
19 #include <netinet/in.h>
20 #include <netinet/if_ether.h>
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "interface.h"
28 #include "smb.h"
29
30 extern uchar *startbuf;
31
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)
36 {
37 uint32 p0,p1,p2,p3;
38
39 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
40 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
41
42 *second = 2*(p0 & 0x1F);
43 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
44 *hour = (p1>>3)&0xFF;
45 *day = (p2&0x1F);
46 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
47 *year = ((p3>>1)&0xFF) + 80;
48 }
49
50 /*******************************************************************
51 create a unix date from a dos date
52 ********************************************************************/
53 static time_t make_unix_date(const void *date_ptr)
54 {
55 uint32 dos_date=0;
56 struct tm t;
57
58 dos_date = IVAL(date_ptr,0);
59
60 if (dos_date == 0) return(0);
61
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);
64 t.tm_wday = 1;
65 t.tm_yday = 1;
66 t.tm_isdst = 0;
67
68 return (mktime(&t));
69 }
70
71 /*******************************************************************
72 create a unix date from a dos date
73 ********************************************************************/
74 static time_t make_unix_date2(const void *date_ptr)
75 {
76 uint32 x,x2;
77
78 x = IVAL(date_ptr,0);
79 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
80 SIVAL(&x,0,x2);
81
82 return(make_unix_date((void *)&x));
83 }
84
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)
90 {
91 double d;
92 time_t ret;
93
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));
96
97 /* now adjust by 369 years to make the secs since 1970 */
98 d -= 369.0*365.25*24*60*60;
99
100 /* and a fudge factor as we got it wrong by a few days */
101 d += (3*24*60*60 + 6*60*60 + 2);
102
103 if (d<0)
104 return(0);
105
106 ret = (time_t)d;
107
108 return(ret);
109 }
110
111
112 /****************************************************************************
113 interpret the weird netbios "name". Return the name type
114 ****************************************************************************/
115 static int name_interpret(char *in,char *out)
116 {
117 int ret;
118 int len = (*in++) / 2;
119
120 *out=0;
121
122 if (len > 30 || len<1) return(0);
123
124 while (len--)
125 {
126 if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
127 *out = 0;
128 return(0);
129 }
130 *out = ((in[0]-'A')<<4) + (in[1]-'A');
131 in += 2;
132 out++;
133 }
134 *out = 0;
135 ret = out[-1];
136
137 return(ret);
138 }
139
140 /****************************************************************************
141 find a pointer to a netbios name
142 ****************************************************************************/
143 static char *name_ptr(char *buf,int ofs)
144 {
145 unsigned char c = *(unsigned char *)(buf+ofs);
146
147 if ((c & 0xC0) == 0xC0)
148 {
149 uint16 l = RSVAL(buf, ofs) & 0x3FFF;
150 return(buf + l);
151 }
152 else
153 return(buf+ofs);
154 }
155
156 /****************************************************************************
157 extract a netbios name from a buf
158 ****************************************************************************/
159 static int name_extract(char *buf,int ofs,char *name)
160 {
161 char *p = name_ptr(buf,ofs);
162 strcpy(name,"");
163 return(name_interpret(p,name));
164 }
165
166
167 /****************************************************************************
168 return the total storage length of a mangled name
169 ****************************************************************************/
170 static int name_len(const unsigned char *s)
171 {
172 const char *s0 = s;
173 unsigned char c = *(unsigned char *)s;
174 if ((c & 0xC0) == 0xC0)
175 return(2);
176 while (*s) s += (*s)+1;
177 return(PTR_DIFF(s,s0)+1);
178 }
179
180 static void print_asc(const unsigned char *buf,int len)
181 {
182 int i;
183 for (i=0;i<len;i++)
184 printf("%c",isprint(buf[i])?buf[i]:'.');
185 }
186
187 static char *name_type_str(int name_type)
188 {
189 static char *f = NULL;
190 switch (name_type) {
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;
198 }
199 return(f);
200 }
201
202 void print_data(const unsigned char *buf, int len)
203 {
204 int i=0;
205 if (len<=0) return;
206 printf("[%03X] ",i);
207 for (i=0;i<len;) {
208 printf("%02X ",(int)buf[i]);
209 i++;
210 if (i%8 == 0) printf(" ");
211 if (i%16 == 0) {
212 print_asc(&buf[i-16],8); printf(" ");
213 print_asc(&buf[i-8],8); printf("\n");
214 if (i<len) printf("[%03X] ",i);
215 }
216 }
217 if (i%16) {
218 int n;
219
220 n = 16 - (i%16);
221 printf(" ");
222 if (n>8) printf(" ");
223 while (n--) printf(" ");
224
225 n = MIN(8,i%16);
226 print_asc(&buf[i-(i%16)],n); printf(" ");
227 n = (i%16) - n;
228 if (n>0) print_asc(&buf[i-n],n);
229 printf("\n");
230 }
231 }
232
233
234 static void write_bits(unsigned int val,char *fmt)
235 {
236 char *p = fmt;
237 int i=0;
238
239 while ((p=strchr(fmt,'|'))) {
240 int l = PTR_DIFF(p,fmt);
241 if (l && (val & (1<<i)))
242 printf("%.*s ",l,fmt);
243 fmt = p+1;
244 i++;
245 }
246 }
247
248 /* convert a unicode string */
249 static const char *unistr(const char *s, int *len)
250 {
251 static char buf[1000];
252 int l=0;
253 static int use_unicode = -1;
254
255 if (use_unicode == -1) {
256 char *p = getenv("USE_UNICODE");
257 if (p && (atoi(p) == 1))
258 use_unicode = 1;
259 else
260 use_unicode = 0;
261 }
262
263 /* maybe it isn't unicode - a cheap trick */
264 if (!use_unicode || (s[0] && s[1])) {
265 *len = strlen(s)+1;
266 return s;
267 }
268
269 *len = 0;
270
271 if (s[0] == 0 && s[1] != 0) {
272 s++;
273 *len = 1;
274 }
275
276 while (l < (sizeof(buf)-1) && s[0] && s[1] == 0) {
277 buf[l] = s[0];
278 s += 2; l++;
279 *len += 2;
280 }
281 buf[l] = 0;
282 *len += 2;
283 return buf;
284 }
285
286 static const uchar *fdata1(const uchar *buf, const char *fmt, const uchar *maxbuf)
287 {
288 int reverse=0;
289 char *attrib_fmt = "READONLY|HIDDEN|SYSTEM|VOLUME|DIR|ARCHIVE|";
290 int len;
291
292 while (*fmt && buf<maxbuf) {
293 switch (*fmt) {
294 case 'a':
295 write_bits(CVAL(buf,0),attrib_fmt);
296 buf++; fmt++;
297 break;
298
299 case 'A':
300 write_bits(SVAL(buf,0),attrib_fmt);
301 buf+=2; fmt++;
302 break;
303
304 case '{':
305 {
306 char bitfmt[128];
307 char *p = strchr(++fmt,'}');
308 int l = PTR_DIFF(p,fmt);
309 strncpy(bitfmt,fmt,l);
310 bitfmt[l]=0;
311 fmt = p+1;
312 write_bits(CVAL(buf,0),bitfmt);
313 buf++;
314 break;
315 }
316
317 case 'P':
318 {
319 int l = atoi(fmt+1);
320 buf += l;
321 fmt++;
322 while (isdigit(*fmt)) fmt++;
323 break;
324 }
325 case 'r':
326 reverse = !reverse;
327 fmt++;
328 break;
329 case 'D':
330 {
331 unsigned int x = reverse?RIVAL(buf,0):IVAL(buf,0);
332 printf("%d (0x%x)",x, x);
333 buf += 4;
334 fmt++;
335 break;
336 }
337 case 'L':
338 {
339 unsigned int x1 = reverse?RIVAL(buf,0):IVAL(buf,0);
340 unsigned int x2 = reverse?RIVAL(buf,4):IVAL(buf,4);
341 if (x2) {
342 printf("0x%08x:%08x",x2, x1);
343 } else {
344 printf("%d (0x%08x%08x)",x1, x2, x1);
345 }
346 buf += 8;
347 fmt++;
348 break;
349 }
350 case 'd':
351 {
352 unsigned int x = reverse?RSVAL(buf,0):SVAL(buf,0);
353 printf("%d (0x%x)",x, x);
354 buf += 2;
355 fmt++;
356 break;
357 }
358 case 'W':
359 {
360 unsigned int x = reverse?RIVAL(buf,0):IVAL(buf,0);
361 printf("0x%X",x);
362 buf += 4;
363 fmt++;
364 break;
365 }
366 case 'w':
367 {
368 unsigned int x = reverse?RSVAL(buf,0):SVAL(buf,0);
369 printf("0x%X",x);
370 buf += 2;
371 fmt++;
372 break;
373 }
374 case 'B':
375 {
376 unsigned int x = CVAL(buf,0);
377 printf("0x%X",x);
378 buf += 1;
379 fmt++;
380 break;
381 }
382 case 'b':
383 {
384 unsigned int x = CVAL(buf,0);
385 printf("%d (0x%x)",x, x);
386 buf += 1;
387 fmt++;
388 break;
389 }
390 case 'S':
391 {
392 printf("%.*s",(int)PTR_DIFF(maxbuf,buf),unistr(buf, &len));
393 buf += len;
394 fmt++;
395 break;
396 }
397 case 'Z':
398 {
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));
403 buf += len+1;
404 fmt++;
405 break;
406 }
407 case 's':
408 {
409 int l = atoi(fmt+1);
410 printf("%-*.*s",l,l,buf);
411 buf += l;
412 fmt++; while (isdigit(*fmt)) fmt++;
413 break;
414 }
415 case 'h':
416 {
417 int l = atoi(fmt+1);
418 while (l--) printf("%02x",*buf++);
419 fmt++; while (isdigit(*fmt)) fmt++;
420 break;
421 }
422 case 'n':
423 {
424 int t = atoi(fmt+1);
425 char nbuf[255];
426 int name_type;
427 switch (t) {
428 case 1:
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));
433 break;
434 case 2:
435 name_type = buf[15];
436 printf("%-15.15s NameType=0x%02X (%s)",
437 buf,name_type,name_type_str(name_type));
438 buf += 16;
439 break;
440 }
441 fmt++; while (isdigit(*fmt)) fmt++;
442 break;
443 }
444 case 'T':
445 {
446 time_t t;
447 int x = IVAL(buf,0);
448 switch (atoi(fmt+1)) {
449 case 1:
450 if (x==0 || x==-1 || x==0xFFFFFFFF)
451 t = 0;
452 else
453 t = make_unix_date(buf);
454 buf+=4;
455 break;
456 case 2:
457 if (x==0 || x==-1 || x==0xFFFFFFFF)
458 t = 0;
459 else
460 t = make_unix_date2(buf);
461 buf+=4;
462 break;
463 case 3:
464 t = interpret_long_date(buf);
465 buf+=8;
466 break;
467 }
468 printf("%s",t?asctime(localtime(&t)):"NULL\n");
469 fmt++; while (isdigit(*fmt)) fmt++;
470 break;
471 }
472 default:
473 putchar(*fmt);
474 fmt++;
475 break;
476 }
477 }
478
479 if (buf>=maxbuf && *fmt)
480 printf("END OF BUFFER\n");
481
482 return(buf);
483 }
484
485 const uchar *fdata(const uchar *buf, const char *fmt, const uchar *maxbuf)
486 {
487 static int depth=0;
488 char s[128];
489 char *p;
490
491 while (*fmt) {
492 switch (*fmt) {
493 case '*':
494 fmt++;
495 while (buf < maxbuf) {
496 const uchar *buf2;
497 depth++;
498 buf2 = fdata(buf,fmt,maxbuf);
499 depth--;
500 if (buf2 == buf) return(buf);
501 buf = buf2;
502 }
503 break;
504
505 case '|':
506 fmt++;
507 if (buf>=maxbuf) return(buf);
508 break;
509
510 case '%':
511 fmt++;
512 buf=maxbuf;
513 break;
514
515 case '#':
516 fmt++;
517 return(buf);
518 break;
519
520 case '[':
521 fmt++;
522 if (buf>=maxbuf) return(buf);
523 bzero(s,sizeof(s));
524 p = strchr(fmt,']');
525 strncpy(s,fmt,p-fmt);
526 fmt = p+1;
527 buf = fdata1(buf,s,maxbuf);
528 break;
529
530 default:
531 putchar(*fmt); fmt++;
532 fflush(stdout);
533 break;
534 }
535 }
536 if (!depth && buf<maxbuf) {
537 int len = PTR_DIFF(maxbuf,buf);
538 printf("Data: (%d bytes)\n",len);
539 print_data(buf,len);
540 return(buf+len);
541 }
542 return(buf);
543 }
544
545 typedef struct
546 {
547 char *name;
548 int code;
549 char *message;
550 } err_code_struct;
551
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."},
580 {NULL,-1,NULL}};
581
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."},
616 {NULL,-1,NULL}};
617
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."},
638 {NULL,-1,NULL}};
639
640
641 static struct
642 {
643 int code;
644 char *class;
645 err_code_struct *err_msgs;
646 } err_classes[] = {
647 {0,"SUCCESS",NULL},
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},
656 {-1,NULL,NULL}};
657
658
659 /****************************************************************************
660 return a SMB error string from a SMB buffer
661 ****************************************************************************/
662 char *smb_errstr(int class,int num)
663 {
664 static char ret[128];
665 int i,j;
666
667 ret[0]=0;
668
669 for (i=0;err_classes[i].class;i++)
670 if (err_classes[i].code == class)
671 {
672 if (err_classes[i].err_msgs)
673 {
674 err_code_struct *err = err_classes[i].err_msgs;
675 for (j=0;err[j].name;j++)
676 if (num == err[j].code)
677 {
678 sprintf(ret,"%s - %s (%s)",err_classes[i].class,
679 err[j].name,err[j].message);
680 return ret;
681 }
682 }
683
684 sprintf(ret,"%s - %d",err_classes[i].class,num);
685 return ret;
686 }
687
688 sprintf(ret,"ERROR: Unknown error (%d,%d)",class,num);
689 return(ret);
690 }
691
692
693