1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Msgbuf buffer management implementation. The smb_msgbuf interface is
* typically used to encode or decode SMB data using sprintf/scanf
* style operations. It contains special handling for the SMB header.
* It can also be used for general purpose encoding and decoding.
*/
#include <sys/types.h>
#include <sys/varargs.h>
#include <sys/byteorder.h>
#ifndef _KERNEL
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <strings.h>
#else
#include <sys/sunddi.h>
#include <sys/kmem.h>
#endif
#include <smbsrv/string.h>
#include <smbsrv/msgbuf.h>
#include <smbsrv/smb.h>
static int buf_decode(smb_msgbuf_t *, char *, va_list ap);
static int buf_encode(smb_msgbuf_t *, char *, va_list ap);
static void *smb_msgbuf_malloc(smb_msgbuf_t *, size_t);
static int smb_msgbuf_chkerc(char *text, int erc);
static void buf_decode_wcs(smb_wchar_t *, smb_wchar_t *, int wcstrlen);
/*
* Returns the offset or number of bytes used within the buffer.
*/
size_t
smb_msgbuf_used(smb_msgbuf_t *mb)
{
/*LINTED E_PTRDIFF_OVERFLOW*/
return (mb->scan - mb->base);
}
/*
* Returns the actual buffer size.
*/
size_t
smb_msgbuf_size(smb_msgbuf_t *mb)
{
return (mb->max);
}
uint8_t *
smb_msgbuf_base(smb_msgbuf_t *mb)
{
return (mb->base);
}
/*
* Ensure that the scan is aligned on a word (16-bit) boundary.
*/
void
smb_msgbuf_word_align(smb_msgbuf_t *mb)
{
mb->scan = (uint8_t *)((uintptr_t)(mb->scan + 1) & ~1);
}
/*
* Ensure that the scan is aligned on a dword (32-bit) boundary.
*/
void
smb_msgbuf_dword_align(smb_msgbuf_t *mb)
{
mb->scan = (uint8_t *)((uintptr_t)(mb->scan + 3) & ~3);
}
/*
* Checks whether or not the buffer has space for the amount of data
* specified. Returns 1 if there is space, otherwise returns 0.
*/
int
smb_msgbuf_has_space(smb_msgbuf_t *mb, size_t size)
{
if (size > mb->max || (mb->scan + size) > mb->end)
return (0);
return (1);
}
/*
* Set flags the smb_msgbuf.
*/
void
smb_msgbuf_fset(smb_msgbuf_t *mb, uint32_t flags)
{
mb->flags |= flags;
}
/*
* Clear flags the smb_msgbuf.
*/
void
smb_msgbuf_fclear(smb_msgbuf_t *mb, uint32_t flags)
{
mb->flags &= ~flags;
}
/*
* smb_msgbuf_init
*
* Initialize a smb_msgbuf_t structure based on the buffer and size
* specified. Both scan and base initially point to the beginning
* of the buffer and end points to the limit of the buffer. As
* data is added scan should be incremented to point to the next
* offset at which data will be written. Max and count are set
* to the actual buffer size.
*/
void
smb_msgbuf_init(smb_msgbuf_t *mb, uint8_t *buf, size_t size, uint32_t flags)
{
mb->scan = mb->base = buf;
mb->max = mb->count = size;
mb->end = &buf[size];
mb->flags = flags;
mb->mlist.next = 0;
}
/*
* smb_msgbuf_term
*
* Destruct a smb_msgbuf_t. Free any memory hanging off the mlist.
*/
void
smb_msgbuf_term(smb_msgbuf_t *mb)
{
smb_msgbuf_mlist_t *item = mb->mlist.next;
smb_msgbuf_mlist_t *tmp;
while (item) {
tmp = item;
item = item->next;
#ifndef _KERNEL
free(tmp);
#else
kmem_free(tmp, tmp->size);
#endif
}
}
/*
* smb_msgbuf_decode
*
* Decode a smb_msgbuf buffer as indicated by the format string into
* the variable arg list. This is similar to a scanf operation.
*
* On success, returns the number of bytes encoded. Otherwise
* returns a -ve error code.
*/
int
smb_msgbuf_decode(smb_msgbuf_t *mb, char *fmt, ...)
{
int rc;
uint8_t *orig_scan;
va_list ap;
va_start(ap, fmt);
orig_scan = mb->scan;
rc = buf_decode(mb, fmt, ap);
va_end(ap);
if (rc != SMB_MSGBUF_SUCCESS) {
(void) smb_msgbuf_chkerc("smb_msgbuf_decode", rc);
mb->scan = orig_scan;
return (rc);
}
/*LINTED E_PTRDIFF_OVERFLOW*/
return (mb->scan - orig_scan);
}
/*
* buf_decode
*
* Private decode function, where the real work of decoding the smb_msgbuf
* is done. This function should only be called via smb_msgbuf_decode to
* ensure correct behaviour and error handling.
*/
static int
buf_decode(smb_msgbuf_t *mb, char *fmt, va_list ap)
{
uint32_t ival;
uint8_t c;
uint8_t *cvalp;
uint8_t **cvalpp;
uint16_t *wvalp;
uint32_t *lvalp;
uint64_t *llvalp;
smb_wchar_t *wcs;
int repc;
int rc;
while ((c = *fmt++) != 0) {
repc = 1;
if (c == ' ' || c == '\t')
continue;
if (c == '(') {
while (((c = *fmt++) != 0) && c != ')')
;
if (!c)
return (SMB_MSGBUF_SUCCESS);
continue;
}
if ('0' <= c && c <= '9') {
repc = 0;
do {
repc = repc * 10 + c - '0';
c = *fmt++;
} while ('0' <= c && c <= '9');
} else if (c == '#') {
repc = va_arg(ap, int);
c = *fmt++;
}
switch (c) {
case '.':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_UNDERFLOW);
mb->scan += repc;
break;
case 'c':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_UNDERFLOW);
cvalp = va_arg(ap, uint8_t *);
bcopy(mb->scan, cvalp, repc);
mb->scan += repc;
break;
case 'b':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_UNDERFLOW);
cvalp = va_arg(ap, uint8_t *);
while (repc-- > 0) {
*cvalp++ = *mb->scan++;
}
break;
case 'w':
rc = smb_msgbuf_has_space(mb, repc * sizeof (uint16_t));
if (rc == 0)
return (SMB_MSGBUF_UNDERFLOW);
wvalp = va_arg(ap, uint16_t *);
while (repc-- > 0) {
*wvalp++ = LE_IN16(mb->scan);
mb->scan += sizeof (uint16_t);
}
break;
case 'l':
rc = smb_msgbuf_has_space(mb, repc * sizeof (int32_t));
if (rc == 0)
return (SMB_MSGBUF_UNDERFLOW);
lvalp = va_arg(ap, uint32_t *);
while (repc-- > 0) {
*lvalp++ = LE_IN32(mb->scan);
mb->scan += sizeof (int32_t);
}
break;
case 'q':
rc = smb_msgbuf_has_space(mb, repc * sizeof (int64_t));
if (rc == 0)
return (SMB_MSGBUF_UNDERFLOW);
llvalp = va_arg(ap, uint64_t *);
while (repc-- > 0) {
*llvalp++ = LE_IN64(mb->scan);
mb->scan += sizeof (int64_t);
}
break;
case 'u': /* Convert from unicode if flags are set */
if (mb->flags & SMB_MSGBUF_UNICODE)
goto unicode_translation;
/*FALLTHROUGH*/
case 's':
ival = strlen((const char *)mb->scan) + 1;
if (smb_msgbuf_has_space(mb, ival) == 0)
return (SMB_MSGBUF_UNDERFLOW);
if ((cvalp = smb_msgbuf_malloc(mb, ival * 2)) == 0)
return (SMB_MSGBUF_UNDERFLOW);
if ((ival = smb_stombs((char *)cvalp,
(char *)mb->scan, ival * 2)) ==
(uint32_t)-1) {
return (SMB_MSGBUF_DATA_ERROR);
}
cvalpp = va_arg(ap, uint8_t **);
*cvalpp = cvalp;
mb->scan += (ival+1);
break;
case 'U': /* Convert from unicode */
unicode_translation:
/*
* Unicode strings are always word aligned.
* The malloc'd area is larger than the
* original string because the UTF-8 chars
* may be longer than the wide-chars.
*/
smb_msgbuf_word_align(mb);
/*LINTED E_BAD_PTR_CAST_ALIGN*/
wcs = (smb_wchar_t *)mb->scan;
/* count the null wchar */
repc = sizeof (smb_wchar_t);
while (*wcs++)
repc += sizeof (smb_wchar_t);
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_UNDERFLOW);
/* Decode wchar string into host byte-order */
if ((wcs = smb_msgbuf_malloc(mb, repc)) == 0)
return (SMB_MSGBUF_UNDERFLOW);
/*LINTED E_BAD_PTR_CAST_ALIGN*/
buf_decode_wcs(wcs, (smb_wchar_t *)mb->scan,
repc / sizeof (smb_wchar_t));
/* Get space for translated string */
if ((cvalp = smb_msgbuf_malloc(mb, repc * 2)) == 0)
return (SMB_MSGBUF_UNDERFLOW);
/* Translate string */
(void) smb_wcstombs((char *)cvalp, wcs, repc * 2);
cvalpp = va_arg(ap, uint8_t **);
*cvalpp = cvalp;
mb->scan += repc;
break;
case 'M':
if (smb_msgbuf_has_space(mb, 4) == 0)
return (SMB_MSGBUF_UNDERFLOW);
if (mb->scan[0] != 0xFF ||
mb->scan[1] != 'S' ||
mb->scan[2] != 'M' ||
mb->scan[3] != 'B') {
return (SMB_MSGBUF_INVALID_HEADER);
}
mb->scan += 4;
break;
default:
return (SMB_MSGBUF_INVALID_FORMAT);
}
}
return (SMB_MSGBUF_SUCCESS);
}
/*
* smb_msgbuf_encode
*
* Encode a smb_msgbuf buffer as indicated by the format string using
* the variable arg list. This is similar to a sprintf operation.
*
* On success, returns the number of bytes encoded. Otherwise
* returns a -ve error code.
*/
int
smb_msgbuf_encode(smb_msgbuf_t *mb, char *fmt, ...)
{
int rc;
uint8_t *orig_scan;
va_list ap;
va_start(ap, fmt);
orig_scan = mb->scan;
rc = buf_encode(mb, fmt, ap);
va_end(ap);
if (rc != SMB_MSGBUF_SUCCESS) {
(void) smb_msgbuf_chkerc("smb_msgbuf_encode", rc);
mb->scan = orig_scan;
return (rc);
}
/*LINTED E_PTRDIFF_OVERFLOW*/
return (mb->scan - orig_scan);
}
/*
* buf_encode
*
* Private encode function, where the real work of encoding the smb_msgbuf
* is done. This function should only be called via smb_msgbuf_encode to
* ensure correct behaviour and error handling.
*/
static int
buf_encode(smb_msgbuf_t *mb, char *fmt, va_list ap)
{
uint8_t cval;
uint16_t wval;
uint32_t lval;
uint64_t llval;
uint32_t ival;
uint8_t *cvalp;
uint8_t c;
smb_wchar_t wcval;
int count;
int repc = 1;
int rc;
while ((c = *fmt++) != 0) {
repc = 1;
if (c == ' ' || c == '\t')
continue;
if (c == '(') {
while (((c = *fmt++) != 0) && c != ')')
;
if (!c)
return (SMB_MSGBUF_SUCCESS);
continue;
}
if ('0' <= c && c <= '9') {
repc = 0;
do {
repc = repc * 10 + c - '0';
c = *fmt++;
} while ('0' <= c && c <= '9');
} else if (c == '#') {
repc = va_arg(ap, int);
c = *fmt++;
}
switch (c) {
case '.':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_OVERFLOW);
while (repc-- > 0)
*mb->scan++ = 0;
break;
case 'c':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_OVERFLOW);
cvalp = va_arg(ap, uint8_t *);
bcopy(cvalp, mb->scan, repc);
mb->scan += repc;
break;
case 'b':
if (smb_msgbuf_has_space(mb, repc) == 0)
return (SMB_MSGBUF_OVERFLOW);
while (repc-- > 0) {
cval = va_arg(ap, int);
*mb->scan++ = cval;
}
break;
case 'w':
rc = smb_msgbuf_has_space(mb, repc * sizeof (uint16_t));
if (rc == 0)
return (SMB_MSGBUF_OVERFLOW);
while (repc-- > 0) {
wval = va_arg(ap, int);
LE_OUT16(mb->scan, wval);
mb->scan += sizeof (uint16_t);
}
break;
case 'l':
rc = smb_msgbuf_has_space(mb, repc * sizeof (int32_t));
if (rc == 0)
return (SMB_MSGBUF_OVERFLOW);
while (repc-- > 0) {
lval = va_arg(ap, uint32_t);
LE_OUT32(mb->scan, lval);
mb->scan += sizeof (int32_t);
}
break;
case 'q':
rc = smb_msgbuf_has_space(mb, repc * sizeof (int64_t));
if (rc == 0)
return (SMB_MSGBUF_OVERFLOW);
while (repc-- > 0) {
llval = va_arg(ap, uint64_t);
LE_OUT64(mb->scan, llval);
mb->scan += sizeof (uint64_t);
}
break;
case 'u': /* conditional unicode */
if (mb->flags & SMB_MSGBUF_UNICODE)
goto unicode_translation;
/* FALLTHROUGH */
case 's':
cvalp = va_arg(ap, uint8_t *);
ival = strlen((const char *)cvalp) + 1;
if (smb_msgbuf_has_space(mb, ival) == 0)
return (SMB_MSGBUF_OVERFLOW);
ival =
smb_mbstos((char *)mb->scan, (const char *)cvalp);
mb->scan += ival + 1;
break;
case 'U': /* unicode */
unicode_translation:
/*
* Unicode strings are always word aligned.
*/
smb_msgbuf_word_align(mb);
cvalp = va_arg(ap, uint8_t *);
for (;;) {
rc = smb_msgbuf_has_space(mb,
sizeof (smb_wchar_t));
if (rc == 0)
return (SMB_MSGBUF_OVERFLOW);
count = smb_mbtowc(&wcval, (const char *)cvalp,
MTS_MB_CHAR_MAX);
if (count < 0) {
return (SMB_MSGBUF_DATA_ERROR);
} else if (count == 0) {
/*
* No longer need to do this now that
* mbtowc correctly writes the null
* before returning zero but paranoia
* wins.
*/
wcval = 0;
count = 1;
}
/* Write wchar in wire-format */
LE_OUT16(mb->scan, wcval);
if (*cvalp == 0) {
/*
* End of string. Check to see whether
* or not to include the null
* terminator.
*/
if ((mb->flags & SMB_MSGBUF_NOTERM) ==
0)
mb->scan +=
sizeof (smb_wchar_t);
break;
}
mb->scan += sizeof (smb_wchar_t);
cvalp += count;
}
break;
case 'M':
if (smb_msgbuf_has_space(mb, 4) == 0)
return (SMB_MSGBUF_OVERFLOW);
*mb->scan++ = 0xFF;
*mb->scan++ = 'S';
*mb->scan++ = 'M';
*mb->scan++ = 'B';
break;
default:
return (SMB_MSGBUF_INVALID_FORMAT);
}
}
return (SMB_MSGBUF_SUCCESS);
}
/*
* smb_msgbuf_malloc
*
* Allocate some memory for use with this smb_msgbuf. We increase the
* requested size to hold the list pointer and return a pointer
* to the area for use by the caller.
*/
static void *
smb_msgbuf_malloc(smb_msgbuf_t *mb, size_t size)
{
smb_msgbuf_mlist_t *item;
size += sizeof (smb_msgbuf_mlist_t);
#ifndef _KERNEL
if ((item = malloc(size)) == NULL)
return (NULL);
#else
item = kmem_alloc(size, KM_SLEEP);
#endif
item->next = mb->mlist.next;
item->size = size;
mb->mlist.next = item;
/*
* The caller gets a pointer to the address
* immediately after the smb_msgbuf_mlist_t.
*/
return ((void *)(item + 1));
}
/*
* smb_msgbuf_chkerc
*
* Diagnostic function to write an appropriate message to the system log.
*/
static int
smb_msgbuf_chkerc(char *text, int erc)
{
static struct {
int erc;
char *name;
} etable[] = {
{ SMB_MSGBUF_SUCCESS, "success" },
{ SMB_MSGBUF_UNDERFLOW, "overflow/underflow" },
{ SMB_MSGBUF_INVALID_FORMAT, "invalid format" },
{ SMB_MSGBUF_INVALID_HEADER, "invalid header" },
{ SMB_MSGBUF_DATA_ERROR, "data error" }
};
int i;
for (i = 0; i < sizeof (etable)/sizeof (etable[0]); ++i) {
if (etable[i].erc == erc) {
if (text == 0)
text = "smb_msgbuf_chkerc";
break;
}
}
return (erc);
}
static void
buf_decode_wcs(smb_wchar_t *dst_wcstr, smb_wchar_t *src_wcstr, int wcstrlen)
{
int i;
for (i = 0; i < wcstrlen; i++) {
*dst_wcstr = LE_IN16(src_wcstr);
dst_wcstr++;
src_wcstr++;
}
}
|