forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans2.c
9742 lines (8438 loc) · 251 KB
/
trans2.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.
SMB transaction2 handling
Copyright (C) Jeremy Allison 1994-2007
Copyright (C) Stefan (metze) Metzmacher 2003
Copyright (C) Volker Lendecke 2005-2007
Copyright (C) Steve French 2005
Copyright (C) James Peach 2006-2007
Extensively modified by Andrew Tridgell, 1995
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 "ntioctl.h"
#include "system/filesys.h"
#include "version.h"
#include "smbd/smbd.h"
#include "smbd/globals.h"
#include "../libcli/auth/libcli_auth.h"
#include "../librpc/gen_ndr/xattr.h"
#include "../librpc/gen_ndr/ndr_security.h"
#include "../librpc/gen_ndr/open_files.h"
#include "libcli/security/security.h"
#include "trans2.h"
#include "auth.h"
#include "smbprofile.h"
#include "rpc_server/srv_pipe_hnd.h"
#include "printing.h"
#include "lib/util_ea.h"
#include "lib/readdir_attr.h"
#define DIR_ENTRY_SAFETY_MARGIN 4096
static char *store_file_unix_basic(connection_struct *conn,
char *pdata,
files_struct *fsp,
const SMB_STRUCT_STAT *psbuf);
static char *store_file_unix_basic_info2(connection_struct *conn,
char *pdata,
files_struct *fsp,
const SMB_STRUCT_STAT *psbuf);
/****************************************************************************
Check if an open file handle or smb_fname is a symlink.
****************************************************************************/
static NTSTATUS refuse_symlink(connection_struct *conn,
const files_struct *fsp,
const struct smb_filename *smb_fname)
{
SMB_STRUCT_STAT sbuf;
const SMB_STRUCT_STAT *pst = NULL;
if (fsp) {
pst = &fsp->fsp_name->st;
} else {
pst = &smb_fname->st;
}
if (!VALID_STAT(*pst)) {
int ret = vfs_stat_smb_basename(conn,
smb_fname,
&sbuf);
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
pst = &sbuf;
}
if (S_ISLNK(pst->st_ex_mode)) {
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
}
NTSTATUS check_access_fsp(const struct files_struct *fsp,
uint32_t access_mask)
{
if (!(fsp->access_mask & access_mask)) {
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
}
/********************************************************************
The canonical "check access" based on object handle or path function.
********************************************************************/
NTSTATUS check_access(connection_struct *conn,
files_struct *fsp,
const struct smb_filename *smb_fname,
uint32_t access_mask)
{
NTSTATUS status;
if (fsp) {
status = check_access_fsp(fsp, access_mask);
} else {
status = smbd_check_access_rights(conn, smb_fname,
false, access_mask);
}
return status;
}
/********************************************************************
Roundup a value to the nearest allocation roundup size boundary.
Only do this for Windows clients.
********************************************************************/
uint64_t smb_roundup(connection_struct *conn, uint64_t val)
{
uint64_t rval = lp_allocation_roundup_size(SNUM(conn));
/* Only roundup for Windows clients. */
enum remote_arch_types ra_type = get_remote_arch();
if (rval && (ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
val = SMB_ROUNDUP(val,rval);
}
return val;
}
/********************************************************************
Create a 64 bit FileIndex. If the file is on the same device as
the root of the share, just return the 64-bit inode. If it isn't,
mangle as we used to do.
********************************************************************/
uint64_t get_FileIndex(connection_struct *conn, const SMB_STRUCT_STAT *psbuf)
{
uint64_t file_index;
if (conn->base_share_dev == psbuf->st_ex_dev) {
return (uint64_t)psbuf->st_ex_ino;
}
file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
return file_index;
}
/****************************************************************************
Utility functions for dealing with extended attributes.
****************************************************************************/
/****************************************************************************
Refuse to allow clients to overwrite our private xattrs.
****************************************************************************/
bool samba_private_attr_name(const char *unix_ea_name)
{
static const char * const prohibited_ea_names[] = {
SAMBA_POSIX_INHERITANCE_EA_NAME,
SAMBA_XATTR_DOS_ATTRIB,
SAMBA_XATTR_MARKER,
XATTR_NTACL_NAME,
NULL
};
int i;
for (i = 0; prohibited_ea_names[i]; i++) {
if (strequal( prohibited_ea_names[i], unix_ea_name))
return true;
}
if (strncasecmp_m(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
return true;
}
return false;
}
/****************************************************************************
Get one EA value. Fill in a struct ea_struct.
****************************************************************************/
NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
files_struct *fsp, const char *fname,
const char *ea_name, struct ea_struct *pea)
{
/* Get the value of this xattr. Max size is 64k. */
size_t attr_size = 256;
char *val = NULL;
ssize_t sizeret;
again:
val = talloc_realloc(mem_ctx, val, char, attr_size);
if (!val) {
return NT_STATUS_NO_MEMORY;
}
if (fsp && fsp->fh->fd != -1) {
sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
} else {
sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
}
if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
attr_size = 65536;
goto again;
}
if (sizeret == -1) {
return map_nt_error_from_unix(errno);
}
DEBUG(10,("get_ea_value: EA %s is of length %u\n", ea_name, (unsigned int)sizeret));
dump_data(10, (uint8_t *)val, sizeret);
pea->flags = 0;
if (strnequal(ea_name, "user.", 5)) {
pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
} else {
pea->name = talloc_strdup(mem_ctx, ea_name);
}
if (pea->name == NULL) {
TALLOC_FREE(val);
return NT_STATUS_NO_MEMORY;
}
pea->value.data = (unsigned char *)val;
pea->value.length = (size_t)sizeret;
return NT_STATUS_OK;
}
NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx,
connection_struct *conn,
files_struct *fsp,
const struct smb_filename *smb_fname,
char ***pnames,
size_t *pnum_names)
{
/* Get a list of all xattrs. Max namesize is 64k. */
size_t ea_namelist_size = 1024;
char *ea_namelist = NULL;
char *p;
char **names, **tmp;
size_t num_names;
ssize_t sizeret = -1;
NTSTATUS status;
if (pnames) {
*pnames = NULL;
}
*pnum_names = 0;
if (!lp_ea_support(SNUM(conn))) {
return NT_STATUS_OK;
}
status = refuse_symlink(conn, fsp, smb_fname);
if (!NT_STATUS_IS_OK(status)) {
/*
* Just return no EA's on a symlink.
*/
return NT_STATUS_OK;
}
/*
* TALLOC the result early to get the talloc hierarchy right.
*/
names = talloc_array(mem_ctx, char *, 1);
if (names == NULL) {
DEBUG(0, ("talloc failed\n"));
return NT_STATUS_NO_MEMORY;
}
while (ea_namelist_size <= 65536) {
ea_namelist = talloc_realloc(
names, ea_namelist, char, ea_namelist_size);
if (ea_namelist == NULL) {
DEBUG(0, ("talloc failed\n"));
TALLOC_FREE(names);
return NT_STATUS_NO_MEMORY;
}
if (fsp && fsp->fh->fd != -1) {
sizeret = SMB_VFS_FLISTXATTR(fsp, ea_namelist,
ea_namelist_size);
} else {
sizeret = SMB_VFS_LISTXATTR(conn,
smb_fname->base_name,
ea_namelist,
ea_namelist_size);
}
if ((sizeret == -1) && (errno == ERANGE)) {
ea_namelist_size *= 2;
}
else {
break;
}
}
if (sizeret == -1) {
TALLOC_FREE(names);
return map_nt_error_from_unix(errno);
}
DEBUG(10, ("%s: ea_namelist size = %u\n",
__func__, (unsigned int)sizeret));
if (sizeret == 0) {
TALLOC_FREE(names);
return NT_STATUS_OK;
}
/*
* Ensure the result is 0-terminated
*/
if (ea_namelist[sizeret-1] != '\0') {
TALLOC_FREE(names);
return NT_STATUS_INTERNAL_ERROR;
}
/*
* count the names
*/
num_names = 0;
for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
num_names += 1;
}
tmp = talloc_realloc(mem_ctx, names, char *, num_names);
if (tmp == NULL) {
DEBUG(0, ("talloc failed\n"));
TALLOC_FREE(names);
return NT_STATUS_NO_MEMORY;
}
names = tmp;
num_names = 0;
for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
names[num_names++] = p;
}
if (pnames) {
*pnames = names;
} else {
TALLOC_FREE(names);
}
*pnum_names = num_names;
return NT_STATUS_OK;
}
/****************************************************************************
Return a linked list of the total EA's. Plus the total size
****************************************************************************/
static NTSTATUS get_ea_list_from_file_path(TALLOC_CTX *mem_ctx,
connection_struct *conn,
files_struct *fsp,
const struct smb_filename *smb_fname,
size_t *pea_total_len,
struct ea_list **ea_list)
{
/* Get a list of all xattrs. Max namesize is 64k. */
size_t i, num_names;
char **names;
struct ea_list *ea_list_head = NULL;
bool posix_pathnames = false;
NTSTATUS status;
*pea_total_len = 0;
*ea_list = NULL;
if (fsp) {
posix_pathnames =
(fsp->fsp_name->flags & SMB_FILENAME_POSIX_PATH);
} else {
posix_pathnames = (smb_fname->flags & SMB_FILENAME_POSIX_PATH);
}
status = get_ea_names_from_file(talloc_tos(),
conn,
fsp,
smb_fname,
&names,
&num_names);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
if (num_names == 0) {
*ea_list = NULL;
return NT_STATUS_OK;
}
for (i=0; i<num_names; i++) {
struct ea_list *listp;
fstring dos_ea_name;
if (strnequal(names[i], "system.", 7)
|| samba_private_attr_name(names[i]))
continue;
/*
* Filter out any underlying POSIX EA names
* that a Windows client can't handle.
*/
if (!posix_pathnames &&
is_invalid_windows_ea_name(names[i])) {
continue;
}
listp = talloc(mem_ctx, struct ea_list);
if (listp == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = get_ea_value(listp,
conn,
fsp,
smb_fname->base_name,
names[i],
&listp->ea);
if (!NT_STATUS_IS_OK(status)) {
TALLOC_FREE(listp);
return status;
}
if (listp->ea.value.length == 0) {
/*
* We can never return a zero length EA.
* Windows reports the EA's as corrupted.
*/
TALLOC_FREE(listp);
continue;
}
push_ascii_fstring(dos_ea_name, listp->ea.name);
*pea_total_len +=
4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len "
"= %u\n", (unsigned int)*pea_total_len, dos_ea_name,
(unsigned int)listp->ea.value.length));
DLIST_ADD_END(ea_list_head, listp);
}
/* Add on 4 for total length. */
if (*pea_total_len) {
*pea_total_len += 4;
}
DEBUG(10, ("get_ea_list_from_file: total_len = %u\n",
(unsigned int)*pea_total_len));
*ea_list = ea_list_head;
return NT_STATUS_OK;
}
static NTSTATUS get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
const struct smb_filename *smb_fname, size_t *pea_total_len, struct ea_list **ea_list)
{
*pea_total_len = 0;
*ea_list = NULL;
if (!lp_ea_support(SNUM(conn))) {
return NT_STATUS_OK;
}
if (is_ntfs_stream_smb_fname(smb_fname)) {
return NT_STATUS_INVALID_PARAMETER;
}
return get_ea_list_from_file_path(mem_ctx,
conn,
fsp,
smb_fname,
pea_total_len,
ea_list);
}
/****************************************************************************
Fill a qfilepathinfo buffer with EA's. Returns the length of the buffer
that was filled.
****************************************************************************/
static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned int total_data_size,
connection_struct *conn, struct ea_list *ea_list)
{
unsigned int ret_data_size = 4;
char *p = pdata;
SMB_ASSERT(total_data_size >= 4);
if (!lp_ea_support(SNUM(conn))) {
SIVAL(pdata,4,0);
return 4;
}
for (p = pdata + 4; ea_list; ea_list = ea_list->next) {
size_t dos_namelen;
fstring dos_ea_name;
push_ascii_fstring(dos_ea_name, ea_list->ea.name);
dos_namelen = strlen(dos_ea_name);
if (dos_namelen > 255 || dos_namelen == 0) {
break;
}
if (ea_list->ea.value.length > 65535) {
break;
}
if (4 + dos_namelen + 1 + ea_list->ea.value.length > total_data_size) {
break;
}
/* We know we have room. */
SCVAL(p,0,ea_list->ea.flags);
SCVAL(p,1,dos_namelen);
SSVAL(p,2,ea_list->ea.value.length);
strlcpy(p+4, dos_ea_name, dos_namelen+1);
memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
p += 4 + dos_namelen + 1 + ea_list->ea.value.length;
}
ret_data_size = PTR_DIFF(p, pdata);
DEBUG(10,("fill_ea_buffer: data_size = %u\n", ret_data_size ));
SIVAL(pdata,0,ret_data_size);
return ret_data_size;
}
static NTSTATUS fill_ea_chained_buffer(TALLOC_CTX *mem_ctx,
char *pdata,
unsigned int total_data_size,
unsigned int *ret_data_size,
connection_struct *conn,
struct ea_list *ea_list)
{
uint8_t *p = (uint8_t *)pdata;
uint8_t *last_start = NULL;
bool do_store_data = (pdata != NULL);
*ret_data_size = 0;
if (!lp_ea_support(SNUM(conn))) {
return NT_STATUS_NO_EAS_ON_FILE;
}
for (; ea_list; ea_list = ea_list->next) {
size_t dos_namelen;
fstring dos_ea_name;
size_t this_size;
size_t pad = 0;
if (last_start != NULL && do_store_data) {
SIVAL(last_start, 0, PTR_DIFF(p, last_start));
}
last_start = p;
push_ascii_fstring(dos_ea_name, ea_list->ea.name);
dos_namelen = strlen(dos_ea_name);
if (dos_namelen > 255 || dos_namelen == 0) {
return NT_STATUS_INTERNAL_ERROR;
}
if (ea_list->ea.value.length > 65535) {
return NT_STATUS_INTERNAL_ERROR;
}
this_size = 0x08 + dos_namelen + 1 + ea_list->ea.value.length;
if (ea_list->next) {
pad = (4 - (this_size % 4)) % 4;
this_size += pad;
}
if (do_store_data) {
if (this_size > total_data_size) {
return NT_STATUS_INFO_LENGTH_MISMATCH;
}
/* We know we have room. */
SIVAL(p, 0x00, 0); /* next offset */
SCVAL(p, 0x04, ea_list->ea.flags);
SCVAL(p, 0x05, dos_namelen);
SSVAL(p, 0x06, ea_list->ea.value.length);
strlcpy((char *)(p+0x08), dos_ea_name, dos_namelen+1);
memcpy(p + 0x08 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
if (pad) {
memset(p + 0x08 + dos_namelen + 1 + ea_list->ea.value.length,
'\0',
pad);
}
total_data_size -= this_size;
}
p += this_size;
}
*ret_data_size = PTR_DIFF(p, pdata);
DEBUG(10,("fill_ea_chained_buffer: data_size = %u\n", *ret_data_size));
return NT_STATUS_OK;
}
static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp, const struct smb_filename *smb_fname)
{
size_t total_ea_len = 0;
TALLOC_CTX *mem_ctx;
struct ea_list *ea_list = NULL;
if (!lp_ea_support(SNUM(conn))) {
return 0;
}
mem_ctx = talloc_stackframe();
/* If this is a stream fsp, then we need to instead find the
* estimated ea len from the main file, not the stream
* (streams cannot have EAs), but the estimate isn't just 0 in
* this case! */
if (is_ntfs_stream_smb_fname(smb_fname)) {
fsp = NULL;
}
(void)get_ea_list_from_file_path(mem_ctx,
conn,
fsp,
smb_fname,
&total_ea_len,
&ea_list);
if(conn->sconn->using_smb2) {
NTSTATUS status;
unsigned int ret_data_size;
/*
* We're going to be using fill_ea_chained_buffer() to
* marshall EA's - this size is significantly larger
* than the SMB1 buffer. Re-calculate the size without
* marshalling.
*/
status = fill_ea_chained_buffer(mem_ctx,
NULL,
0,
&ret_data_size,
conn,
ea_list);
if (!NT_STATUS_IS_OK(status)) {
ret_data_size = 0;
}
total_ea_len = ret_data_size;
}
TALLOC_FREE(mem_ctx);
return total_ea_len;
}
/****************************************************************************
Ensure the EA name is case insensitive by matching any existing EA name.
****************************************************************************/
static void canonicalize_ea_name(connection_struct *conn,
files_struct *fsp,
const struct smb_filename *smb_fname,
fstring unix_ea_name)
{
size_t total_ea_len;
TALLOC_CTX *mem_ctx = talloc_tos();
struct ea_list *ea_list;
NTSTATUS status = get_ea_list_from_file_path(mem_ctx,
conn,
fsp,
smb_fname,
&total_ea_len,
&ea_list);
if (!NT_STATUS_IS_OK(status)) {
return;
}
for (; ea_list; ea_list = ea_list->next) {
if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
&unix_ea_name[5], ea_list->ea.name));
strlcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-5);
break;
}
}
}
/****************************************************************************
Set or delete an extended attribute.
****************************************************************************/
NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
const struct smb_filename *smb_fname, struct ea_list *ea_list)
{
NTSTATUS status;
bool posix_pathnames = false;
if (!lp_ea_support(SNUM(conn))) {
return NT_STATUS_EAS_NOT_SUPPORTED;
}
if (fsp) {
posix_pathnames =
(fsp->fsp_name->flags & SMB_FILENAME_POSIX_PATH);
} else {
posix_pathnames = (smb_fname->flags & SMB_FILENAME_POSIX_PATH);
}
status = refuse_symlink(conn, fsp, smb_fname);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
status = check_access(conn, fsp, smb_fname, FILE_WRITE_EA);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/* Setting EAs on streams isn't supported. */
if (is_ntfs_stream_smb_fname(smb_fname)) {
return NT_STATUS_INVALID_PARAMETER;
}
/*
* Filter out invalid Windows EA names - before
* we set *any* of them.
*/
if (!posix_pathnames && ea_list_has_invalid_name(ea_list)) {
return STATUS_INVALID_EA_NAME;
}
for (;ea_list; ea_list = ea_list->next) {
int ret;
fstring unix_ea_name;
fstrcpy(unix_ea_name, "user."); /* All EA's must start with user. */
fstrcat(unix_ea_name, ea_list->ea.name);
canonicalize_ea_name(conn,
fsp,
smb_fname,
unix_ea_name);
DEBUG(10,("set_ea: ea_name %s ealen = %u\n", unix_ea_name, (unsigned int)ea_list->ea.value.length));
if (samba_private_attr_name(unix_ea_name)) {
DEBUG(10,("set_ea: ea name %s is a private Samba name.\n", unix_ea_name));
return NT_STATUS_ACCESS_DENIED;
}
if (ea_list->ea.value.length == 0) {
/* Remove the attribute. */
if (fsp && (fsp->fh->fd != -1)) {
DEBUG(10,("set_ea: deleting ea name %s on "
"file %s by file descriptor.\n",
unix_ea_name, fsp_str_dbg(fsp)));
ret = SMB_VFS_FREMOVEXATTR(fsp, unix_ea_name);
} else {
DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
unix_ea_name, smb_fname->base_name));
ret = SMB_VFS_REMOVEXATTR(conn,
smb_fname->base_name,
unix_ea_name);
}
#ifdef ENOATTR
/* Removing a non existent attribute always succeeds. */
if (ret == -1 && errno == ENOATTR) {
DEBUG(10,("set_ea: deleting ea name %s didn't exist - succeeding by default.\n",
unix_ea_name));
ret = 0;
}
#endif
} else {
if (fsp && (fsp->fh->fd != -1)) {
DEBUG(10,("set_ea: setting ea name %s on file "
"%s by file descriptor.\n",
unix_ea_name, fsp_str_dbg(fsp)));
ret = SMB_VFS_FSETXATTR(fsp, unix_ea_name,
ea_list->ea.value.data, ea_list->ea.value.length, 0);
} else {
DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
unix_ea_name, smb_fname->base_name));
ret = SMB_VFS_SETXATTR(conn,
smb_fname->base_name,
unix_ea_name,
ea_list->ea.value.data,
ea_list->ea.value.length,
0);
}
}
if (ret == -1) {
#ifdef ENOTSUP
if (errno == ENOTSUP) {
return NT_STATUS_EAS_NOT_SUPPORTED;
}
#endif
return map_nt_error_from_unix(errno);
}
}
return NT_STATUS_OK;
}
/****************************************************************************
Read a list of EA names from an incoming data buffer. Create an ea_list with them.
****************************************************************************/
static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
{
struct ea_list *ea_list_head = NULL;
size_t converted_size, offset = 0;
while (offset + 2 < data_size) {
struct ea_list *eal = talloc_zero(ctx, struct ea_list);
unsigned int namelen = CVAL(pdata,offset);
offset++; /* Go past the namelen byte. */
/* integer wrap paranioa. */
if ((offset + namelen < offset) || (offset + namelen < namelen) ||
(offset > data_size) || (namelen > data_size) ||
(offset + namelen >= data_size)) {
break;
}
/* Ensure the name is null terminated. */
if (pdata[offset + namelen] != '\0') {
return NULL;
}
if (!pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset],
&converted_size)) {
DEBUG(0,("read_ea_name_list: pull_ascii_talloc "
"failed: %s", strerror(errno)));
}
if (!eal->ea.name) {
return NULL;
}
offset += (namelen + 1); /* Go past the name + terminating zero. */
DLIST_ADD_END(ea_list_head, eal);
DEBUG(10,("read_ea_name_list: read ea name %s\n", eal->ea.name));
}
return ea_list_head;
}
/****************************************************************************
Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
****************************************************************************/
static struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
{
struct ea_list *ea_list_head = NULL;
size_t offset = 0;
size_t bytes_used = 0;
while (offset < data_size) {
struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset, data_size - offset, &bytes_used);
if (!eal) {
return NULL;
}
DLIST_ADD_END(ea_list_head, eal);
offset += bytes_used;
}
return ea_list_head;
}
/****************************************************************************
Count the total EA size needed.
****************************************************************************/
static size_t ea_list_size(struct ea_list *ealist)
{
fstring dos_ea_name;
struct ea_list *listp;
size_t ret = 0;
for (listp = ealist; listp; listp = listp->next) {
push_ascii_fstring(dos_ea_name, listp->ea.name);
ret += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
}
/* Add on 4 for total length. */
if (ret) {
ret += 4;
}
return ret;
}
/****************************************************************************
Return a union of EA's from a file list and a list of names.
The TALLOC context for the two lists *MUST* be identical as we steal
memory from one list to add to another. JRA.
****************************************************************************/
static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *file_list, size_t *total_ea_len)
{
struct ea_list *nlistp, *flistp;
for (nlistp = name_list; nlistp; nlistp = nlistp->next) {
for (flistp = file_list; flistp; flistp = flistp->next) {
if (strequal(nlistp->ea.name, flistp->ea.name)) {
break;
}
}
if (flistp) {
/* Copy the data from this entry. */
nlistp->ea.flags = flistp->ea.flags;
nlistp->ea.value = flistp->ea.value;
} else {
/* Null entry. */
nlistp->ea.flags = 0;
ZERO_STRUCT(nlistp->ea.value);
}
}
*total_ea_len = ea_list_size(name_list);
return name_list;
}
/****************************************************************************
Send the required number of replies back.
We assume all fields other than the data fields are
set correctly for the type of call.
HACK ! Always assumes smb_setup field is zero.
****************************************************************************/
void send_trans2_replies(connection_struct *conn,
struct smb_request *req,
NTSTATUS status,
const char *params,
int paramsize,
const char *pdata,
int datasize,
int max_data_bytes)
{
/* As we are using a protocol > LANMAN1 then the max_send
variable must have been set in the sessetupX call.
This takes precedence over the max_xmit field in the
global struct. These different max_xmit variables should
be merged as this is now too confusing */
int data_to_send = datasize;
int params_to_send = paramsize;
int useable_space;
const char *pp = params;
const char *pd = pdata;
int params_sent_thistime, data_sent_thistime, total_sent_thistime;
int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
int data_alignment_offset = 0;
bool overflow = False;
struct smbXsrv_connection *xconn = req->xconn;
int max_send = xconn->smb1.sessions.max_send;
/* Modify the data_to_send and datasize and set the error if
we're trying to send more than max_data_bytes. We still send
the part of the packet(s) that fit. Strange, but needed
for OS/2. */
if (max_data_bytes > 0 && datasize > max_data_bytes) {
DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
max_data_bytes, datasize ));
datasize = data_to_send = max_data_bytes;
overflow = True;
}
/* If there genuinely are no parameters or data to send just send the empty packet */
if(params_to_send == 0 && data_to_send == 0) {
reply_outbuf(req, 10, 0);
if (NT_STATUS_V(status)) {
uint8_t eclass;
uint32_t ecode;
ntstatus_to_dos(status, &eclass, &ecode);
error_packet_set((char *)req->outbuf,
eclass, ecode, status,
__LINE__,__FILE__);
}
show_msg((char *)req->outbuf);
if (!srv_send_smb(xconn,
(char *)req->outbuf,
true, req->seqnum+1,
IS_CONN_ENCRYPTED(conn),
&req->pcd)) {