/
files.c
2645 lines (2218 loc) · 61.3 KB
/
files.c
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Unix SMB/CIFS implementation.
Files[] structure handling
Copyright (C) Andrew Tridgell 1998
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "smbd/smbd.h"
#include "smbd/globals.h"
#include "smbd/smbXsrv_open.h"
#include "libcli/security/security.h"
#include "util_tdb.h"
#include "lib/util/bitmap.h"
#include "lib/util/strv.h"
#include "lib/util/memcache.h"
#include "libcli/smb/reparse.h"
#define FILE_HANDLE_OFFSET 0x1000
static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
struct smb_filename **_smb_fname);
/**
* create new fsp to be used for file_new or a durable handle reconnect
*/
NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
files_struct **result)
{
NTSTATUS status = NT_STATUS_NO_MEMORY;
files_struct *fsp = NULL;
struct smbd_server_connection *sconn = conn->sconn;
fsp = talloc_zero(mem_ctx, struct files_struct);
if (fsp == NULL) {
goto fail;
}
/*
* This can't be a child of fsp because the file_handle can be ref'd
* when doing a dos/fcb open, which will then share the file_handle
* across multiple fsps.
*/
fsp->fh = fd_handle_create(mem_ctx);
if (fsp->fh == NULL) {
goto fail;
}
fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
#ifndef HAVE_OFD_LOCKS
fsp->fsp_flags.use_ofd_locks = false;
#endif
fh_set_refcount(fsp->fh, 1);
fsp_set_fd(fsp, -1);
fsp->fnum = FNUM_FIELD_INVALID;
fsp->conn = conn;
fsp->close_write_time = make_omit_timespec();
DLIST_ADD(sconn->files, fsp);
sconn->num_files += 1;
conn->num_files_open++;
DBG_INFO("allocated files structure (%u used)\n",
(unsigned int)sconn->num_files);
*result = fsp;
return NT_STATUS_OK;
fail:
if (fsp != NULL) {
TALLOC_FREE(fsp->fh);
}
TALLOC_FREE(fsp);
return status;
}
void fsp_set_gen_id(files_struct *fsp)
{
static uint64_t gen_id = 1;
/*
* A billion of 64-bit increments per second gives us
* more than 500 years of runtime without wrap.
*/
gen_id++;
fh_set_gen_id(fsp->fh, gen_id);
}
/****************************************************************************
Find first available file slot.
****************************************************************************/
NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
{
struct smbXsrv_open *op = NULL;
NTTIME now;
NTSTATUS status;
if (req == NULL) {
DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
return NT_STATUS_OK;
}
now = timeval_to_nttime(&fsp->open_time);
status = smbXsrv_open_create(req->xconn,
fsp->conn->session_info,
now,
&op);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
fsp->op = op;
op->compat = fsp;
fsp->fnum = op->local_id;
fsp->mid = req->mid;
req->chain_fsp = fsp;
DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
fsp_str_dbg(fsp), fsp->mid);
return NT_STATUS_OK;
}
NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
files_struct **result)
{
struct smbd_server_connection *sconn = conn->sconn;
files_struct *fsp;
NTSTATUS status;
status = fsp_new(conn, conn, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
GetTimeOfDay(&fsp->open_time);
status = fsp_bind_smb(fsp, req);
if (!NT_STATUS_IS_OK(status)) {
file_free(NULL, fsp);
return status;
}
fsp_set_gen_id(fsp);
/*
* Create an smb_filename with "" for the base_name. There are very
* few NULL checks, so make sure it's initialized with something. to
* be safe until an audit can be done.
*/
fsp->fsp_name = synthetic_smb_fname(fsp,
"",
NULL,
NULL,
0,
0);
if (fsp->fsp_name == NULL) {
file_free(NULL, fsp);
return NT_STATUS_NO_MEMORY;
}
DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
/* A new fsp invalidates the positive and
negative fsp_fi_cache as the new fsp is pushed
at the start of the list and we search from
a cache hit to the *end* of the list. */
ZERO_STRUCT(sconn->fsp_fi_cache);
*result = fsp;
return NT_STATUS_OK;
}
NTSTATUS create_internal_fsp(connection_struct *conn,
const struct smb_filename *smb_fname,
struct files_struct **_fsp)
{
struct files_struct *fsp = NULL;
NTSTATUS status;
status = file_new(NULL, conn, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
status = fsp_set_smb_fname(fsp, smb_fname);
if (!NT_STATUS_IS_OK(status)) {
file_free(NULL, fsp);
return status;
}
*_fsp = fsp;
return NT_STATUS_OK;
}
/*
* Create an internal fsp for an *existing* directory.
*
* This should only be used by callers in the VFS that need to control the
* opening of the directory. Otherwise use open_internal_dirfsp().
*/
NTSTATUS create_internal_dirfsp(connection_struct *conn,
const struct smb_filename *smb_dname,
struct files_struct **_fsp)
{
struct files_struct *fsp = NULL;
NTSTATUS status;
status = create_internal_fsp(conn, smb_dname, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
fsp->access_mask = FILE_LIST_DIRECTORY;
fsp->fsp_flags.is_directory = true;
fsp->fsp_flags.is_dirfsp = true;
*_fsp = fsp;
return NT_STATUS_OK;
}
/*
* Open an internal fsp for an *existing* directory.
*/
NTSTATUS open_internal_dirfsp(connection_struct *conn,
const struct smb_filename *smb_dname,
int _open_flags,
struct files_struct **_fsp)
{
struct vfs_open_how how = { .flags = _open_flags, };
struct files_struct *fsp = NULL;
NTSTATUS status;
status = create_internal_dirfsp(conn, smb_dname, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
#ifdef O_DIRECTORY
how.flags |= O_DIRECTORY;
#endif
status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, &how);
if (!NT_STATUS_IS_OK(status)) {
DBG_INFO("Could not open fd for %s (%s)\n",
smb_fname_str_dbg(smb_dname),
nt_errstr(status));
file_free(NULL, fsp);
return status;
}
status = vfs_stat_fsp(fsp);
if (!NT_STATUS_IS_OK(status)) {
file_free(NULL, fsp);
return status;
}
if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
DBG_ERR("%s is not a directory!\n",
smb_fname_str_dbg(smb_dname));
file_free(NULL, fsp);
return NT_STATUS_NOT_A_DIRECTORY;
}
fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
*_fsp = fsp;
return NT_STATUS_OK;
}
/*
* Convert a pathref dirfsp into a real fsp. No need to do any cwd
* tricks, we just open ".".
*/
NTSTATUS openat_internal_dir_from_pathref(
struct files_struct *dirfsp,
int _open_flags,
struct files_struct **_fsp)
{
struct connection_struct *conn = dirfsp->conn;
struct smb_filename *smb_dname = dirfsp->fsp_name;
struct files_struct *fsp = NULL;
char dot[] = ".";
struct smb_filename smb_dot = {
.base_name = dot,
.flags = smb_dname->flags,
.twrp = smb_dname->twrp,
};
struct vfs_open_how how = { .flags = _open_flags, };
NTSTATUS status;
status = create_internal_dirfsp(conn, smb_dname, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/*
* Pointless for opening ".", but you never know...
*/
how.flags |= O_NOFOLLOW;
status = fd_openat(dirfsp, &smb_dot, fsp, &how);
if (!NT_STATUS_IS_OK(status)) {
DBG_INFO("fd_openat(\"%s\", \".\") failed: %s\n",
fsp_str_dbg(dirfsp),
nt_errstr(status));
file_free(NULL, fsp);
return status;
}
fsp->fsp_name->st = smb_dname->st;
fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
*_fsp = fsp;
return NT_STATUS_OK;
}
/*
* The "link" in the name doesn't imply link in the filesystem
* sense. It's a object that "links" together an fsp and an smb_fname
* and the link allocated as talloc child of an fsp.
*
* The link is created for fsps that openat_pathref_fsp() returns in
* smb_fname->fsp. When this fsp is freed by file_free() by some caller
* somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
* will use the link to reset the reference in smb_fname->fsp that is about to
* go away.
*
* This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
* pointers.
*/
struct fsp_smb_fname_link {
struct fsp_smb_fname_link **smb_fname_link;
struct files_struct **smb_fname_fsp;
};
static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
{
if (link->smb_fname_link == NULL) {
return 0;
}
*link->smb_fname_link = NULL;
*link->smb_fname_fsp = NULL;
return 0;
}
static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
struct fsp_smb_fname_link **smb_fname_link,
struct files_struct **smb_fname_fsp)
{
struct fsp_smb_fname_link *link = NULL;
SMB_ASSERT(*smb_fname_link == NULL);
SMB_ASSERT(*smb_fname_fsp == NULL);
link = talloc_zero(fsp, struct fsp_smb_fname_link);
if (link == NULL) {
return NT_STATUS_NO_MEMORY;
}
link->smb_fname_link = smb_fname_link;
link->smb_fname_fsp = smb_fname_fsp;
*smb_fname_link = link;
*smb_fname_fsp = fsp;
talloc_set_destructor(link, fsp_smb_fname_link_destructor);
return NT_STATUS_OK;
}
/*
* Free a link, carefully avoiding to trigger the link destructor
*/
static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
{
struct fsp_smb_fname_link *link = *_link;
if (link == NULL) {
return;
}
talloc_set_destructor(link, NULL);
TALLOC_FREE(link);
*_link = NULL;
}
/*
* Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
* close the embedded smb_fname->fsp.
*/
static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
{
struct files_struct *fsp = smb_fname->fsp;
struct files_struct *base_fsp = NULL;
NTSTATUS status;
int saved_errno = errno;
destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
if (fsp == NULL) {
errno = saved_errno;
return 0;
}
if (fsp_is_alternate_stream(fsp)) {
base_fsp = fsp->base_fsp;
}
status = fd_close(fsp);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("Closing fd for fsp [%s] failed: %s. "
"Please check your filesystem!!!\n",
fsp_str_dbg(fsp), nt_errstr(status));
}
file_free(NULL, fsp);
smb_fname->fsp = NULL;
if (base_fsp != NULL) {
base_fsp->stream_fsp = NULL;
status = fd_close(base_fsp);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("Closing fd for base_fsp [%s] failed: %s. "
"Please check your filesystem!!!\n",
fsp_str_dbg(base_fsp), nt_errstr(status));
}
file_free(NULL, base_fsp);
}
errno = saved_errno;
return 0;
}
static NTSTATUS openat_pathref_fullname(
struct connection_struct *conn,
const struct files_struct *dirfsp,
struct files_struct *basefsp,
struct smb_filename **full_fname,
struct smb_filename *smb_fname,
const struct vfs_open_how *how)
{
struct files_struct *fsp = NULL;
bool have_dirfsp = (dirfsp != NULL);
bool have_basefsp = (basefsp != NULL);
NTSTATUS status;
DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
SMB_ASSERT(smb_fname->fsp == NULL);
SMB_ASSERT(have_dirfsp != have_basefsp);
status = fsp_new(conn, conn, &fsp);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
GetTimeOfDay(&fsp->open_time);
fsp_set_gen_id(fsp);
ZERO_STRUCT(conn->sconn->fsp_fi_cache);
fsp->fsp_flags.is_pathref = true;
status = fsp_attach_smb_fname(fsp, full_fname);
if (!NT_STATUS_IS_OK(status)) {
goto fail;
}
fsp_set_base_fsp(fsp, basefsp);
status = fd_openat(dirfsp, smb_fname, fsp, how);
if (!NT_STATUS_IS_OK(status)) {
smb_fname->st = fsp->fsp_name->st;
if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK))
{
/*
* streams_xattr return NT_STATUS_NOT_FOUND for
* opens of not yet existing streams.
*
* ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
* and this will result from a open request from
* a POSIX client on a symlink.
*
* NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
* ENOENT case.
*
* NT_STATUS_STOPPED_ON_SYMLINK is returned when trying
* to open a symlink, our callers are not interested in
* this.
*/
status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
goto fail;
}
/*
* fd_openat() has done an FSTAT on the handle
* so update the smb_fname stat info with "truth".
* from the handle.
*/
smb_fname->st = fsp->fsp_name->st;
fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
status = fsp_smb_fname_link(fsp,
&smb_fname->fsp_link,
&smb_fname->fsp);
if (!NT_STATUS_IS_OK(status)) {
goto fail;
}
DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
return NT_STATUS_OK;
fail:
DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
smb_fname_str_dbg(smb_fname),
nt_errstr(status));
fsp_set_base_fsp(fsp, NULL);
fd_close(fsp);
file_free(NULL, fsp);
return status;
}
/*
* Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
* available, open O_RDONLY as root. Both is done in fd_open() ->
* non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
* true.
*/
NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
struct smb_filename *smb_fname)
{
connection_struct *conn = dirfsp->conn;
struct smb_filename *full_fname = NULL;
struct smb_filename *base_fname = NULL;
struct vfs_open_how how = { .flags = O_RDONLY|O_NONBLOCK, };
NTSTATUS status;
DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
if (smb_fname->fsp != NULL) {
/* We already have one for this name. */
DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
smb_fname_str_dbg(smb_fname));
return NT_STATUS_OK;
}
if (is_named_stream(smb_fname) &&
((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
DBG_DEBUG("stream open [%s] on non-stream share\n",
smb_fname_str_dbg(smb_fname));
return NT_STATUS_OBJECT_NAME_INVALID;
}
if (!is_named_stream(smb_fname)) {
/*
* openat_pathref_fullname() will make "full_fname" a
* talloc child of the smb_fname->fsp. Don't use
* talloc_tos() to allocate it to avoid making the
* talloc stackframe pool long-lived.
*/
full_fname = full_path_from_dirfsp_atname(
conn,
dirfsp,
smb_fname);
if (full_fname == NULL) {
status = NT_STATUS_NO_MEMORY;
goto fail;
}
status = openat_pathref_fullname(
conn, dirfsp, NULL, &full_fname, smb_fname, &how);
TALLOC_FREE(full_fname);
return status;
}
/*
* stream open
*/
base_fname = cp_smb_filename_nostream(conn, smb_fname);
if (base_fname == NULL) {
return NT_STATUS_NO_MEMORY;
}
full_fname = full_path_from_dirfsp_atname(
conn, /* no talloc_tos(), see comment above */
dirfsp,
base_fname);
if (full_fname == NULL) {
status = NT_STATUS_NO_MEMORY;
goto fail;
}
status = openat_pathref_fullname(
conn, dirfsp, NULL, &full_fname, base_fname, &how);
TALLOC_FREE(full_fname);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("openat_pathref_fullname() failed: %s\n",
nt_errstr(status));
goto fail;
}
status = open_stream_pathref_fsp(&base_fname->fsp, smb_fname);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("open_stream_pathref_fsp failed: %s\n",
nt_errstr(status));
goto fail;
}
smb_fname_fsp_unlink(base_fname);
fail:
TALLOC_FREE(base_fname);
return status;
}
/*
* Open a stream given an already opened base_fsp. Avoid
* non_widelink_open: This is only valid for the case where we have a
* valid non-cwd_fsp dirfsp that we can pass to SMB_VFS_OPENAT()
*/
NTSTATUS open_stream_pathref_fsp(
struct files_struct **_base_fsp,
struct smb_filename *smb_fname)
{
struct files_struct *base_fsp = *_base_fsp;
connection_struct *conn = base_fsp->conn;
struct smb_filename *base_fname = base_fsp->fsp_name;
struct smb_filename *full_fname = NULL;
struct vfs_open_how how = { .flags = O_RDONLY|O_NONBLOCK, };
NTSTATUS status;
SMB_ASSERT(smb_fname->fsp == NULL);
SMB_ASSERT(is_named_stream(smb_fname));
full_fname = synthetic_smb_fname(
conn, /* no talloc_tos(), this will be long-lived */
base_fname->base_name,
smb_fname->stream_name,
&smb_fname->st,
smb_fname->twrp,
smb_fname->flags);
if (full_fname == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = openat_pathref_fullname(
conn, NULL, base_fsp, &full_fname, smb_fname, &how);
TALLOC_FREE(full_fname);
return status;
}
static char *path_to_strv(TALLOC_CTX *mem_ctx, const char *path)
{
char *result = talloc_strdup(mem_ctx, path);
if (result == NULL) {
return NULL;
}
string_replace(result, '/', '\0');
return result;
}
NTSTATUS readlink_talloc(
TALLOC_CTX *mem_ctx,
struct files_struct *dirfsp,
struct smb_filename *smb_relname,
char **_substitute)
{
struct smb_filename null_fname = {
.base_name = discard_const_p(char, ""),
};
char buf[PATH_MAX];
ssize_t ret;
char *substitute;
NTSTATUS status;
if (smb_relname == NULL) {
/*
* We have a Linux O_PATH handle in dirfsp and want to
* read its value, essentially a freadlink
*/
smb_relname = &null_fname;
}
ret = SMB_VFS_READLINKAT(
dirfsp->conn, dirfsp, smb_relname, buf, sizeof(buf));
if (ret < 0) {
status = map_nt_error_from_unix(errno);
DBG_DEBUG("SMB_VFS_READLINKAT() failed: %s\n",
strerror(errno));
return status;
}
if ((size_t)ret == sizeof(buf)) {
/*
* Do we need symlink targets longer than PATH_MAX?
*/
DBG_DEBUG("Got full %zu bytes from readlink, too long\n",
sizeof(buf));
return NT_STATUS_BUFFER_OVERFLOW;
}
substitute = talloc_strndup(mem_ctx, buf, ret);
if (substitute == NULL) {
DBG_DEBUG("talloc_strndup() failed\n");
return NT_STATUS_NO_MEMORY;
}
*_substitute = substitute;
return NT_STATUS_OK;
}
NTSTATUS read_symlink_reparse(TALLOC_CTX *mem_ctx,
struct files_struct *dirfsp,
struct smb_filename *smb_relname,
struct reparse_data_buffer **_reparse)
{
struct reparse_data_buffer *reparse = NULL;
struct symlink_reparse_struct *lnk = NULL;
NTSTATUS status;
reparse = talloc_zero(mem_ctx, struct reparse_data_buffer);
if (reparse == NULL) {
goto nomem;
}
*reparse = (struct reparse_data_buffer){
.tag = IO_REPARSE_TAG_SYMLINK,
};
lnk = &reparse->parsed.lnk;
status = readlink_talloc(reparse,
dirfsp,
smb_relname,
&lnk->substitute_name);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("readlink_talloc failed: %s\n", nt_errstr(status));
goto fail;
}
if (lnk->substitute_name[0] == '/') {
char *subdir_path = NULL;
char *abs_target_canon = NULL;
const char *relative = NULL;
bool in_share;
subdir_path = talloc_asprintf(talloc_tos(),
"%s/%s",
dirfsp->conn->connectpath,
dirfsp->fsp_name->base_name);
if (subdir_path == NULL) {
goto nomem;
}
abs_target_canon = canonicalize_absolute_path(
talloc_tos(), lnk->substitute_name);
if (abs_target_canon == NULL) {
goto nomem;
}
in_share = subdir_of(subdir_path,
strlen(subdir_path),
abs_target_canon,
&relative);
if (in_share) {
TALLOC_FREE(lnk->substitute_name);
lnk->substitute_name = talloc_strdup(reparse,
relative);
if (lnk->substitute_name == NULL) {
goto nomem;
}
}
}
if (!IS_DIRECTORY_SEP(lnk->substitute_name[0])) {
lnk->flags |= SYMLINK_FLAG_RELATIVE;
}
*_reparse = reparse;
return NT_STATUS_OK;
nomem:
status = NT_STATUS_NO_MEMORY;
fail:
TALLOC_FREE(reparse);
return status;
}
static bool full_path_extend(char **dir, const char *atname)
{
talloc_asprintf_addbuf(dir,
"%s%s",
(*dir)[0] == '\0' ? "" : "/",
atname);
return (*dir) != NULL;
}
/*
* Create the memcache-key for GETREALFILENAME_CACHE: This supplements
* the stat cache for the last component to be looked up. Cache
* contents is the correctly capitalized translation of the parameter
* "name" as it exists on disk. This is indexed by inode of the dirfsp
* and name, and contrary to stat_cahce_lookup() it does not
* vfs_stat() the last component. This will be taken care of by an
* attempt to do a openat_pathref_fsp().
*/
static bool get_real_filename_cache_key(TALLOC_CTX *mem_ctx,
struct files_struct *dirfsp,
const char *name,
DATA_BLOB *_key)
{
struct file_id fid = vfs_file_id_from_sbuf(dirfsp->conn,
&dirfsp->fsp_name->st);
char *upper = NULL;
uint8_t *key = NULL;
size_t namelen, keylen;
upper = talloc_strdup_upper(mem_ctx, name);
if (upper == NULL) {
return false;
}
namelen = talloc_get_size(upper);
keylen = namelen + sizeof(fid);
if (keylen < sizeof(fid)) {
TALLOC_FREE(upper);
return false;
}
key = talloc_size(mem_ctx, keylen);
if (key == NULL) {
TALLOC_FREE(upper);
return false;
}
memcpy(key, &fid, sizeof(fid));
memcpy(key + sizeof(fid), upper, namelen);
TALLOC_FREE(upper);
*_key = (DATA_BLOB){
.data = key,
.length = keylen,
};
return true;
}
static int smb_vfs_openat_ci(TALLOC_CTX *mem_ctx,
bool case_sensitive,
struct connection_struct *conn,
struct files_struct *dirfsp,
struct smb_filename *smb_fname_rel,
files_struct *fsp,
const struct vfs_open_how *how)
{
char *orig_base_name = smb_fname_rel->base_name;
DATA_BLOB cache_key = {
.data = NULL,
};
DATA_BLOB cache_value = {
.data = NULL,
};
NTSTATUS status;
int fd;
bool ok;
fd = SMB_VFS_OPENAT(conn, dirfsp, smb_fname_rel, fsp, how);
if ((fd >= 0) || case_sensitive) {
return fd;
}
if (errno != ENOENT) {
return -1;
}
if (!lp_stat_cache()) {
goto lookup;
}
ok = get_real_filename_cache_key(mem_ctx,
dirfsp,
orig_base_name,
&cache_key);
if (!ok) {
/*
* probably ENOMEM, just bail
*/
errno = ENOMEM;
return -1;
}
DO_PROFILE_INC(statcache_lookups);
ok = memcache_lookup(NULL,
GETREALFILENAME_CACHE,
cache_key,
&cache_value);
if (!ok) {
DO_PROFILE_INC(statcache_misses);
goto lookup;
}
DO_PROFILE_INC(statcache_hits);
smb_fname_rel->base_name = talloc_strndup(mem_ctx,
(char *)cache_value.data,
cache_value.length);
if (smb_fname_rel->base_name == NULL) {
TALLOC_FREE(cache_key.data);
smb_fname_rel->base_name = orig_base_name;
errno = ENOMEM;
return -1;
}
if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
DBG_DEBUG("veto files rejecting last component %s\n",
smb_fname_str_dbg(smb_fname_rel));
TALLOC_FREE(cache_key.data);
smb_fname_rel->base_name = orig_base_name;
errno = EPERM;
return -1;
}
fd = SMB_VFS_OPENAT(conn, dirfsp, smb_fname_rel, fsp, how);
if (fd >= 0) {
TALLOC_FREE(cache_key.data);
return fd;
}
memcache_delete(NULL, GETREALFILENAME_CACHE, cache_key);
/*
* For the "new filename" case we need to preserve the
* capitalization the client sent us, see
* https://bugzilla.samba.org/show_bug.cgi?id=15481
*/
TALLOC_FREE(smb_fname_rel->base_name);
smb_fname_rel->base_name = orig_base_name;
lookup:
status = get_real_filename_at(dirfsp,
orig_base_name,
mem_ctx,
&smb_fname_rel->base_name);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("get_real_filename_at() failed: %s\n",
nt_errstr(status));
errno = ENOENT;
return -1;
}
if (IS_VETO_PATH(conn, smb_fname_rel->base_name)) {
DBG_DEBUG("found veto files path component "
"%s => %s\n",
orig_base_name,
smb_fname_rel->base_name);
TALLOC_FREE(smb_fname_rel->base_name);
smb_fname_rel->base_name = orig_base_name;
errno = ENOENT;
return -1;
}
fd = SMB_VFS_OPENAT(conn, dirfsp, smb_fname_rel, fsp, how);
if ((fd >= 0) && (cache_key.data != NULL)) {
DATA_BLOB value = {
.data = (uint8_t *)smb_fname_rel->base_name,
.length = strlen(smb_fname_rel->base_name) + 1,
};
memcache_add(NULL, GETREALFILENAME_CACHE, cache_key, value);
TALLOC_FREE(cache_key.data);
}
return fd;
}
NTSTATUS openat_pathref_fsp_nosymlink(
TALLOC_CTX *mem_ctx,
struct connection_struct *conn,
struct files_struct *in_dirfsp,