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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
|
/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/inttypes.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <netsmb/smb_osdep.h>
#include <netsmb/smb.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_subr.h>
#include <netsmb/smb_rq.h>
#include <smbfs/smbfs.h>
#include <smbfs/smbfs_node.h>
#include <smbfs/smbfs_subr.h>
/*
* Todo: locking over-the-wire
*/
#if 0 // todo
int
smbfs_smb1_lockandx(struct smbnode *np, int op, uint32_t pid,
offset_t start, uint64_t len, int largelock,
struct smb_cred *scrp, uint32_t timeout)
{
struct smb_share *ssp = np->n_mount->smi_share;
struct smb_rq rq, *rqp = &rq;
struct mbchain *mbp;
uint8_t ltype = 0;
int error;
/* Shared lock for n_fid use below. */
ASSERT(smbfs_rw_lock_held(&np->r_lkserlock, RW_READER));
/* After reconnect, n_fid is invalid */
if (np->n_vcgenid != ssp->ss_vcgenid)
return (ESTALE);
if (op == SMB_LOCK_SHARED)
ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
/* XXX: if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_FILES)? */
if (largelock)
ltype |= SMB_LOCKING_ANDX_LARGE_FILES;
error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scrp);
if (error)
return (error);
smb_rq_getrequest(rqp, &mbp);
smb_rq_wstart(rqp);
mb_put_uint8(mbp, 0xff); /* secondary command */
mb_put_uint8(mbp, 0); /* MBZ */
mb_put_uint16le(mbp, 0);
mb_put_uint16le(mbp, np->n_fid);
mb_put_uint8(mbp, ltype); /* locktype */
mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
mb_put_uint32le(mbp, timeout); /* 0 nowait, -1 infinite wait */
mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
smb_rq_wend(rqp);
smb_rq_bstart(rqp);
mb_put_uint16le(mbp, pid);
if (!largelock) {
mb_put_uint32le(mbp, start);
mb_put_uint32le(mbp, len);
} else {
mb_put_uint16le(mbp, 0); /* pad */
mb_put_uint32le(mbp, start >> 32); /* OffsetHigh */
mb_put_uint32le(mbp, start & 0xffffffff); /* OffsetLow */
mb_put_uint32le(mbp, len >> 32); /* LengthHigh */
mb_put_uint32le(mbp, len & 0xffffffff); /* LengthLow */
}
smb_rq_bend(rqp);
/*
* Don't want to risk missing a successful
* unlock send or lock response, or we could
* lose track of an outstanding lock.
*/
if (op == SMB_LOCK_RELEASE)
rqp->sr_flags |= SMBR_NOINTR_SEND;
else
rqp->sr_flags |= SMBR_NOINTR_RECV;
error = smb_rq_simple(rqp);
smb_rq_done(rqp);
return (error);
}
#endif // todo
/*
* Common function for QueryFileInfo, QueryPathInfo.
*/
int
smbfs_smb1_trans2_query(struct smbnode *np, uint16_t fid,
struct smbfattr *fap, struct smb_cred *scrp)
{
struct smb_share *ssp = np->n_mount->smi_share;
struct smb_vc *vcp = SSTOVC(ssp);
struct smb_t2rq *t2p;
struct mbchain *mbp;
struct mdchain *mdp;
uint16_t cmd;
uint16_t infolevel = SMB_QFILEINFO_ALL_INFO;
int error;
/*
* If we have a valid open FID, use it.
*/
if (fid != SMB_FID_UNUSED)
cmd = SMB_TRANS2_QUERY_FILE_INFORMATION;
else
cmd = SMB_TRANS2_QUERY_PATH_INFORMATION;
error = smb_t2_alloc(SSTOCP(ssp), cmd, scrp, &t2p);
if (error)
return (error);
mbp = &t2p->t2_tparam;
mb_init(mbp);
if (cmd == SMB_TRANS2_QUERY_FILE_INFORMATION)
mb_put_uint16le(mbp, fid);
mb_put_uint16le(mbp, infolevel);
if (cmd == SMB_TRANS2_QUERY_PATH_INFORMATION) {
mb_put_uint32le(mbp, 0);
/* mb_put_uint8(mbp, SMB_DT_ASCII); specs are wrong */
error = smbfs_fullpath(mbp, vcp, np, NULL, 0, '\\');
if (error)
goto out;
}
t2p->t2_maxpcount = 2;
t2p->t2_maxdcount = vcp->vc_txmax;
error = smb_t2_request(t2p);
if (error)
goto out;
/*
* Parse the SMB_QFILEINFO_ALL_INFO
*/
mdp = &t2p->t2_rdata;
error = smbfs_decode_file_all_info(ssp, mdp, fap);
out:
smb_t2_done(t2p);
return (error);
}
/*
* Get some FS information
*/
static int
smbfs_smb1_query_fs_info(struct smb_share *ssp, struct mdchain *info_mdp,
uint16_t level, struct smb_cred *scrp)
{
struct smb_t2rq *t2p;
struct mbchain *mbp;
struct mdchain *mdp;
int error;
error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
scrp, &t2p);
if (error)
return (error);
mbp = &t2p->t2_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, level);
t2p->t2_maxpcount = 4;
t2p->t2_maxdcount = 1024;
error = smb_t2_request(t2p);
if (error)
goto out;
mdp = &t2p->t2_rdata;
*info_mdp = *mdp;
bzero(mdp, sizeof (*mdp));
out:
smb_t2_done(t2p);
return (error);
}
/*
* Get FILE_FS_ATTRIBUTE_INFORMATION
*/
int
smbfs_smb1_qfsattr(struct smb_share *ssp, struct smb_fs_attr_info *fsa,
struct smb_cred *scrp)
{
struct mdchain info_mdc, *mdp = &info_mdc;
int error;
bzero(mdp, sizeof (*mdp));
error = smbfs_smb1_query_fs_info(ssp, mdp,
SMB_QFS_ATTRIBUTE_INFO, scrp);
if (error)
goto out;
error = smbfs_decode_fs_attr_info(ssp, mdp, fsa);
out:
md_done(mdp);
return (error);
}
/*
* Get FileFsFullSizeInformation and
* parse into *info
*/
int
smbfs_smb1_statfs(struct smb_share *ssp,
struct smb_fs_size_info *info,
struct smb_cred *scrp)
{
struct mdchain info_mdc, *mdp = &info_mdc;
struct smb_vc *vcp = SSTOVC(ssp);
uint16_t level;
int error;
bzero(mdp, sizeof (*mdp));
if (vcp->vc_sopt.sv_caps & SMB_CAP_INFOLEVEL_PASSTHRU)
level = SMB_QFS_FULL_SIZE_INFORMATION;
else
level = SMB_QFS_SIZE_INFO;
error = smbfs_smb1_query_fs_info(ssp, mdp, level, scrp);
if (error)
goto out;
md_get_uint64le(mdp, &info->total_units);
md_get_uint64le(mdp, &info->caller_avail);
if (level == SMB_QFS_FULL_SIZE_INFORMATION)
md_get_uint64le(mdp, &info->actual_avail);
else
info->actual_avail = info->caller_avail;
md_get_uint32le(mdp, &info->sect_per_unit);
error = md_get_uint32le(mdp, &info->bytes_per_sect);
out:
md_done(mdp);
return (error);
}
int
smbfs_smb1_flush(struct smb_share *ssp, uint16_t fid, struct smb_cred *scrp)
{
struct smb_rq rq, *rqp = &rq;
struct mbchain *mbp;
int error;
error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scrp);
if (error)
return (error);
smb_rq_getrequest(rqp, &mbp);
smb_rq_wstart(rqp);
mb_put_uint16le(mbp, fid);
smb_rq_wend(rqp);
smb_rq_bstart(rqp);
smb_rq_bend(rqp);
error = smb_rq_simple(rqp);
smb_rq_done(rqp);
return (error);
}
/*
* Set file info via an open handle.
* Caller provides payload, info level.
*/
static int
smbfs_smb1_setinfo_file(struct smb_share *ssp, uint16_t fid,
struct mbchain *info_mbp, uint16_t level, struct smb_cred *scrp)
{
struct smb_t2rq *t2p = NULL;
struct mbchain *mbp;
uint16_t cmd = SMB_TRANS2_SET_FILE_INFORMATION;
int error;
ASSERT(fid != SMB_FID_UNUSED);
error = smb_t2_alloc(SSTOCP(ssp), cmd, scrp, &t2p);
if (error)
return (error);
mbp = &t2p->t2_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, fid);
mb_put_uint16le(mbp, level);
mb_put_uint16le(mbp, 0); /* pad */
/* put the payload */
mbp = &t2p->t2_tdata;
mb_init(mbp);
error = mb_put_mbchain(mbp, info_mbp);
if (error)
goto out;
t2p->t2_maxpcount = 2;
t2p->t2_maxdcount = 0;
error = smb_t2_request(t2p);
out:
smb_t2_done(t2p);
return (error);
}
int
smbfs_smb1_seteof(struct smb_share *ssp, uint16_t fid,
uint64_t newsize, struct smb_cred *scrp)
{
struct mbchain data_mb, *mbp = &data_mb;
uint16_t level;
int error;
if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_INFOLEVEL_PASSTHRU)
level = SMB_SFILEINFO_END_OF_FILE_INFORMATION;
else
level = SMB_SFILEINFO_END_OF_FILE_INFO;
mb_init(mbp);
error = mb_put_uint64le(mbp, newsize);
if (error)
goto out;
error = smbfs_smb1_setinfo_file(ssp, fid, mbp, level, scrp);
out:
mb_done(mbp);
return (error);
}
int
smbfs_smb1_setdisp(struct smb_share *ssp, uint16_t fid,
uint8_t newdisp, struct smb_cred *scrp)
{
struct mbchain data_mb, *mbp = &data_mb;
uint16_t level;
int error;
if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_INFOLEVEL_PASSTHRU)
level = SMB_SFILEINFO_DISPOSITION_INFORMATION;
else
level = SMB_SFILEINFO_DISPOSITION_INFO;
mb_init(mbp);
error = mb_put_uint8(mbp, newdisp);
if (error)
goto out;
error = smbfs_smb1_setinfo_file(ssp, fid, mbp, level, scrp);
out:
mb_done(mbp);
return (error);
}
/*
* Set FileBasicInformation on an open handle
* Caller builds the mbchain.
* Always have a FID here.
*/
int
smbfs_smb1_setfattr(struct smb_share *ssp, uint16_t fid,
struct mbchain *mbp, struct smb_cred *scrp)
{
uint16_t level;
int error;
if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_INFOLEVEL_PASSTHRU)
level = SMB_SFILEINFO_BASIC_INFORMATION;
else
level = SMB_SFILEINFO_BASIC_INFO;
error = smbfs_smb1_setinfo_file(ssp, fid, mbp, level, scrp);
return (error);
}
/*
* On SMB1, the trans2 rename only allows a rename where the
* source and target are in the same directory. If you give
* the server any separators, you get "status not supported".
*
* Why bother using this instead of smbfs_smb1_oldrename?
* Because it works with an open file, and some servers don't
* allow oldrename of a file that's currently open. We call
* this when deleting an open file in smbfsremove(), where
* the rename is always in the same directory.
*/
/*ARGSUSED*/
int
smbfs_smb1_t2rename(struct smbnode *np,
const char *tname, int tnlen,
uint16_t fid, struct smb_cred *scrp)
{
struct smb_share *ssp = np->n_mount->smi_share;
struct mbchain data_mb, *mbp = &data_mb;
struct smb_vc *vcp = SSTOVC(ssp);
uint32_t *name_lenp;
uint16_t level = SMB_SFILEINFO_RENAME_INFORMATION;
int base, len;
int error;
mb_init(mbp);
mb_put_uint32le(mbp, 0); /* don't overwrite */
mb_put_uint32le(mbp, 0); /* obsolete target dir fid */
name_lenp = mb_reserve(mbp, 4); /* name len */
/* New name */
base = mbp->mb_count;
error = smb_put_dmem(mbp, vcp, tname, tnlen, SMB_CS_NONE, NULL);
if (error)
goto out;
len = mbp->mb_count - base;
*name_lenp = htolel(len);
error = smbfs_smb1_setinfo_file(ssp, fid, mbp, level, scrp);
out:
mb_done(mbp);
return (error);
}
/*
* Do an SMB1 (old style) rename using a full dest. path.
* This is used when renaming to a different directory,
* because the (preferred) t2rename can't do that.
*/
int
smbfs_smb1_oldrename(struct smbnode *src, struct smbnode *tdnp,
const char *tname, int tnmlen, struct smb_cred *scrp)
{
struct smb_rq rq, *rqp = &rq;
struct smb_share *ssp = src->n_mount->smi_share;
struct mbchain *mbp;
int error;
uint16_t fa;
char sep;
error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scrp);
if (error)
return (error);
smb_rq_getrequest(rqp, &mbp);
smb_rq_wstart(rqp);
/* freebsd bug: Let directories be renamed - Win98 requires DIR bit */
fa = (SMBTOV(src)->v_type == VDIR) ? SMB_FA_DIR : 0;
fa |= SMB_FA_SYSTEM | SMB_FA_HIDDEN;
mb_put_uint16le(mbp, fa);
smb_rq_wend(rqp);
smb_rq_bstart(rqp);
/*
* When we're not adding any component name, the
* passed sep is ignored, so just pass sep=0.
*/
mb_put_uint8(mbp, SMB_DT_ASCII);
error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0, 0);
if (error)
goto out;
/*
* After XATTR directories, separator is ":"
*/
sep = (src->n_flag & N_XATTR) ? ':' : '\\';
mb_put_uint8(mbp, SMB_DT_ASCII);
error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen, sep);
if (error)
goto out;
smb_rq_bend(rqp);
error = smb_rq_simple(rqp);
out:
smb_rq_done(rqp);
return (error);
}
/*
* TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
*/
static int
smbfs_smb1_trans2find2(struct smbfs_fctx *ctx)
{
struct smb_t2rq *t2p;
struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
struct mbchain *mbp;
struct mdchain *mdp;
uint16_t ecnt, eos, lno, flags;
uint16_t amask, limit;
int error;
/* smbfs_smb_findnextLM2 sets this */
limit = ctx->f_limit;
amask = (uint16_t)ctx->f_attrmask;
if (ctx->f_t2) {
smb_t2_done(ctx->f_t2);
ctx->f_t2 = NULL;
}
flags = FIND2_RETURN_RESUME_KEYS | FIND2_CLOSE_ON_EOS;
if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
flags |= FIND2_CLOSE_AFTER_REQUEST;
ctx->f_flags |= SMBFS_RDD_NOCLOSE;
}
if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
ctx->f_scred, &t2p);
if (error)
return (error);
ctx->f_t2 = t2p;
mbp = &t2p->t2_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, amask);
mb_put_uint16le(mbp, limit);
mb_put_uint16le(mbp, flags);
mb_put_uint16le(mbp, ctx->f_infolevel);
mb_put_uint32le(mbp, 0);
error = smbfs_fullpath(mbp, vcp, ctx->f_dnp,
ctx->f_wildcard, ctx->f_wclen, '\\');
if (error)
return (error);
} else {
error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
ctx->f_scred, &t2p);
if (error)
return (error);
ctx->f_t2 = t2p;
mbp = &t2p->t2_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, ctx->f_Sid);
mb_put_uint16le(mbp, limit);
mb_put_uint16le(mbp, ctx->f_infolevel);
/* Send whatever resume key we received... */
mb_put_uint32le(mbp, ctx->f_rkey);
mb_put_uint16le(mbp, flags);
/* ... and the resume name if we have one. */
if (ctx->f_rname) {
/* resume file name */
mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen,
MB_MSYSTEM);
}
/* Add trailing null - 1 byte if ASCII, 2 if Unicode */
if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp)))
mb_put_uint8(mbp, 0); /* 1st byte NULL Unicode char */
mb_put_uint8(mbp, 0);
}
t2p->t2_maxpcount = 5 * 2;
t2p->t2_maxdcount = 0xF000; /* 64K less some overhead */
error = smb_t2_request(t2p);
if (error)
return (error);
/*
* This is the "resume name" we just sent.
* We want the new one (if any) that may be
* found in the response we just received and
* will now begin parsing. Free the old one
* now so we'll know if we found a new one.
*/
if (ctx->f_rname) {
kmem_free(ctx->f_rname, ctx->f_rnamelen);
ctx->f_rname = NULL;
ctx->f_rnamelen = 0;
}
mdp = &t2p->t2_rparam;
if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
if ((error = md_get_uint16le(mdp, &ctx->f_Sid)) != 0)
goto nodata;
ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
}
md_get_uint16le(mdp, &ecnt); /* entry count */
md_get_uint16le(mdp, &eos); /* end of search */
md_get_uint16le(mdp, NULL); /* EA err. off. */
error = md_get_uint16le(mdp, &lno); /* last name off. */
if (error != 0)
goto nodata;
/*
* The "end of search" flag from an XP server sometimes
* comes back zero when the prior find_next returned exactly
* the number of entries requested. in which case we'd try again
* but the search has in fact been closed so an EBADF results.
* our circumvention is to check here for a zero entry count.
*/
ctx->f_ecnt = ecnt;
if (eos || ctx->f_ecnt == 0)
ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
if (ctx->f_ecnt == 0)
return (ENOENT);
/* Last Name Off (LNO) is the entry with the resume name. */
ctx->f_rnameofs = lno;
ctx->f_eofs = 0;
/*
* Have data. Put the payload in ctx->f_mdchain
* Note struct assignments here.
*/
mdp = &t2p->t2_rdata;
md_done(&ctx->f_mdchain);
ctx->f_mdchain = *mdp;
ctx->f_left = m_fixhdr(mdp->md_top);
bzero(mdp, sizeof (*mdp));
return (0);
nodata:
/*
* Failed parsing the FindFirst or FindNext response.
* Force this directory listing closed, otherwise the
* calling process may hang in an infinite loop.
*/
ctx->f_ecnt = 0; /* Force closed. */
ctx->f_flags |= SMBFS_RDD_EOF;
return (EIO);
}
static int
smbfs_smb1_findclose2(struct smbfs_fctx *ctx)
{
struct smb_rq rq, *rqp = &rq;
struct mbchain *mbp;
int error;
error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2,
ctx->f_scred);
if (error)
return (error);
smb_rq_getrequest(rqp, &mbp);
smb_rq_wstart(rqp);
mb_put_uint16le(mbp, ctx->f_Sid);
smb_rq_wend(rqp);
smb_rq_bstart(rqp);
smb_rq_bend(rqp);
/* Ditto comments at _smb_close */
rqp->sr_flags |= SMBR_NOINTR_SEND;
error = smb_rq_simple(rqp);
smb_rq_done(rqp);
return (error);
}
/*ARGSUSED*/
int
smbfs_smb_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
const char *wildcard, int wclen, uint32_t attr)
{
ctx->f_type = ft_LM2;
ctx->f_namesz = SMB_MAXFNAMELEN + 1;
ctx->f_name = kmem_alloc(ctx->f_namesz, KM_SLEEP);
ctx->f_infolevel = SMB_FIND_FULL_DIRECTORY_INFO;
ctx->f_attrmask = attr;
ctx->f_wildcard = wildcard;
ctx->f_wclen = wclen;
return (0);
}
int
smbfs_smb_findcloseLM2(struct smbfs_fctx *ctx)
{
int error = 0;
if (ctx->f_name)
kmem_free(ctx->f_name, ctx->f_namesz);
if (ctx->f_t2)
smb_t2_done(ctx->f_t2);
md_done(&ctx->f_mdchain);
/*
* If SMBFS_RDD_FINDFIRST is still set, we were opened
* but never saw a findfirst, so we don't have any
* search handle to close.
*/
if ((ctx->f_flags & (SMBFS_RDD_FINDFIRST | SMBFS_RDD_NOCLOSE)) == 0)
error = smbfs_smb1_findclose2(ctx);
return (error);
}
/*
* Get a buffer of directory entries (if we don't already have
* some remaining in the current buffer) then decode one.
*/
int
smbfs_smb_findnextLM2(struct smbfs_fctx *ctx, uint16_t limit)
{
int error;
/*
* If we've scanned to the end of the current buffer
* try to read anohther buffer of dir entries.
* Treat anything less than 8 bytes as an "empty"
* buffer to ensure we can read something.
* (There may be up to 8 bytes of padding.)
*/
if ((ctx->f_eofs + 8) > ctx->f_left) {
/* Scanned the whole buffer. */
if (ctx->f_flags & SMBFS_RDD_EOF)
return (ENOENT);
ctx->f_limit = limit;
error = smbfs_smb1_trans2find2(ctx);
if (error)
return (error);
ctx->f_otws++;
}
/*
* Decode one entry, advance f_eofs
*/
error = smbfs_decode_dirent(ctx);
return (error);
}
/*
* Helper for smbfs_xa_get_streaminfo
* Query stream info
*/
int
smbfs_smb1_get_streaminfo(smbnode_t *np, struct mdchain *mdp,
struct smb_cred *scrp)
{
smb_share_t *ssp = np->n_mount->smi_share;
struct smb_vc *vcp = SSTOVC(ssp);
struct smb_t2rq *t2p = NULL;
struct mbchain *mbp;
mblk_t *m;
uint16_t cmd = SMB_TRANS2_QUERY_PATH_INFORMATION;
int error;
error = smb_t2_alloc(SSTOCP(ssp), cmd, scrp, &t2p);
if (error)
return (error);
mbp = &t2p->t2_tparam;
(void) mb_init(mbp);
(void) mb_put_uint16le(mbp, SMB_QFILEINFO_STREAM_INFO);
(void) mb_put_uint32le(mbp, 0);
error = smbfs_fullpath(mbp, vcp, np, NULL, 0, 0);
if (error)
goto out;
t2p->t2_maxpcount = 2;
t2p->t2_maxdcount = INT16_MAX;
error = smb_t2_request(t2p);
if (error) {
if (t2p->t2_sr_error == NT_STATUS_INVALID_PARAMETER)
error = ENOTSUP;
goto out;
}
/*
* Have data. Move it to *mdp
*/
m = t2p->t2_rdata.md_top;
if (m == NULL) {
error = EBADRPC;
goto out;
}
t2p->t2_rdata.md_top = NULL;
md_initm(mdp, m);
out:
smb_t2_done(t2p);
return (error);
}
/*
* OTW function to Get a security descriptor (SD).
*
* The *reslen param is bufsize(in) / length(out)
* Note: On success, this fills in mdp->md_top,
* which the caller should free.
*/
int
smbfs_smb1_getsec(struct smb_share *ssp, uint16_t fid,
uint32_t selector, mblk_t **res, uint32_t *reslen,
struct smb_cred *scrp)
{
struct smb_ntrq *ntp;
struct mbchain *mbp;
struct mdchain *mdp;
uint32_t dlen;
int error;
*res = NULL;
error = smb_nt_alloc(SSTOCP(ssp), NT_TRANSACT_QUERY_SECURITY_DESC,
scrp, &ntp);
if (error)
return (error);
/* Parameters part */
mbp = &ntp->nt_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, fid);
mb_put_uint16le(mbp, 0); /* reserved */
mb_put_uint32le(mbp, selector);
/* Data part (none) */
/* Max. returned parameters and data. */
ntp->nt_maxpcount = 4;
ntp->nt_maxdcount = *reslen; // out buf size
error = smb_nt_request(ntp);
if (error && !(ntp->nt_flags & SMBT2_MOREDATA))
goto done;
/* Get data len */
mdp = &ntp->nt_rparam;
error = md_get_uint32le(mdp, &dlen);
if (error)
goto done;
/*
* if there's more data than we said we could receive,
* here is where we pick up the length of it
*/
*reslen = dlen;
if (dlen == 0) {
error = EBADRPC;
goto done;
}
/*
* get the SD data part.
*/
mdp = &ntp->nt_rdata;
error = md_get_mbuf(mdp, dlen, res);
done:
if (error == 0 && *res == NULL) {
ASSERT(*res);
error = EBADRPC;
}
smb_nt_done(ntp);
return (error);
}
/*
* OTW function to Set a security descriptor (SD).
* Caller data are carried in an mbchain_t.
*
* Note: This normally consumes mbp->mb_top, and clears
* that pointer when it does.
*/
int
smbfs_smb1_setsec(struct smb_share *ssp, uint16_t fid,
uint32_t selector, mblk_t **mp, struct smb_cred *scrp)
{
struct smb_ntrq *ntp;
struct mbchain *mbp;
int error;
error = smb_nt_alloc(SSTOCP(ssp), NT_TRANSACT_SET_SECURITY_DESC,
scrp, &ntp);
if (error)
return (error);
/* Parameters part */
mbp = &ntp->nt_tparam;
mb_init(mbp);
mb_put_uint16le(mbp, fid);
mb_put_uint16le(mbp, 0); /* reserved */
mb_put_uint32le(mbp, selector);
/* Data part */
mbp = &ntp->nt_tdata;
mb_initm(mbp, *mp);
*mp = NULL; /* consumed */
/* No returned parameters or data. */
ntp->nt_maxpcount = 0;
ntp->nt_maxdcount = 0;
error = smb_nt_request(ntp);
smb_nt_done(ntp);
return (error);
}
|