2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - setfileinfo
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "vfs_posix.h"
25 #include "system/time.h"
26 #include "librpc/gen_ndr/xattr.h"
30 determine what access bits are needed for a call
32 static uint32_t pvfs_setfileinfo_access(union smb_setfileinfo *info)
36 switch (info->generic.level) {
37 case RAW_SFILEINFO_EA_SET:
38 needed = SEC_FILE_WRITE_EA;
41 case RAW_SFILEINFO_DISPOSITION_INFO:
42 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
43 needed = SEC_STD_DELETE;
46 case RAW_SFILEINFO_END_OF_FILE_INFO:
47 needed = SEC_FILE_WRITE_DATA;
50 case RAW_SFILEINFO_POSITION_INFORMATION:
54 case RAW_SFILEINFO_SEC_DESC:
56 if (info->set_secdesc.in.secinfo_flags & (SECINFO_DACL|SECINFO_SACL)) {
57 needed |= SEC_STD_WRITE_DAC;
62 needed = SEC_FILE_WRITE_ATTRIBUTE;
69 rename_information level
71 static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
72 struct ntvfs_request *req,
73 struct pvfs_filename *name,
74 union smb_setfileinfo *info)
77 struct pvfs_filename *name2;
80 /* renames are only allowed within a directory */
81 if (strchr_m(info->rename_information.in.new_name, '\\')) {
82 return NT_STATUS_NOT_SUPPORTED;
85 if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
86 /* don't allow this for now */
87 return NT_STATUS_FILE_IS_A_DIRECTORY;
90 /* don't allow stream renames for now */
91 if (name->stream_name) {
92 return NT_STATUS_INVALID_PARAMETER;
95 /* w2k3 does not appear to allow relative rename */
96 if (info->rename_information.in.root_fid != 0) {
97 return NT_STATUS_INVALID_PARAMETER;
100 /* construct the fully qualified windows name for the new file name */
101 new_name = talloc_strdup(req, name->original_name);
102 if (new_name == NULL) {
103 return NT_STATUS_NO_MEMORY;
105 p = strrchr_m(new_name, '\\');
107 return NT_STATUS_OBJECT_NAME_INVALID;
111 new_name = talloc_asprintf(req, "%s\\%s", new_name,
112 info->rename_information.in.new_name);
113 if (new_name == NULL) {
114 return NT_STATUS_NO_MEMORY;
117 /* resolve the new name */
118 status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
119 if (!NT_STATUS_IS_OK(status)) {
123 /* if the destination exists, then check the rename is allowed */
125 struct odb_lock *lck;
127 if (strcmp(name2->full_name, name->full_name) == 0) {
128 /* rename to same name is null-op */
132 if (!info->rename_information.in.overwrite) {
133 return NT_STATUS_OBJECT_NAME_COLLISION;
136 status = pvfs_can_delete(pvfs, req, name2, &lck);
137 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
138 return NT_STATUS_ACCESS_DENIED;
140 if (!NT_STATUS_IS_OK(status)) {
145 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
146 if (!NT_STATUS_IS_OK(status)) {
150 if (rename(name->full_name, name2->full_name) == -1) {
151 return pvfs_map_errno(pvfs, errno);
154 name->full_name = talloc_steal(name, name2->full_name);
155 name->original_name = talloc_steal(name, name2->original_name);
163 NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs,
164 struct pvfs_filename *name,
165 int fd, uint16_t num_eas,
166 struct ea_struct *eas)
168 struct xattr_DosEAs *ealist;
176 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
177 return NT_STATUS_NOT_SUPPORTED;
180 ealist = talloc(name, struct xattr_DosEAs);
182 /* load the current list */
183 status = pvfs_doseas_load(pvfs, name, fd, ealist);
184 if (!NT_STATUS_IS_OK(status)) {
188 for (j=0;j<num_eas;j++) {
189 struct ea_struct *ea = &eas[j];
190 /* see if its already there */
191 for (i=0;i<ealist->num_eas;i++) {
192 if (strcasecmp_m(ealist->eas[i].name, ea->name.s) == 0) {
193 ealist->eas[i].value = ea->value;
198 if (i==ealist->num_eas) {
200 ealist->eas = talloc_realloc(ealist, ealist->eas,
203 if (ealist->eas == NULL) {
204 return NT_STATUS_NO_MEMORY;
206 ealist->eas[i].name = ea->name.s;
207 ealist->eas[i].value = ea->value;
212 /* pull out any null EAs */
213 for (i=0;i<ealist->num_eas;i++) {
214 if (ealist->eas[i].value.length == 0) {
215 memmove(&ealist->eas[i],
217 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
223 status = pvfs_doseas_save(pvfs, name, fd, ealist);
224 if (!NT_STATUS_IS_OK(status)) {
228 name->dos.ea_size = 4;
229 for (i=0;i<ealist->num_eas;i++) {
230 name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 +
231 ealist->eas[i].value.length;
234 /* update the ea_size attrib */
235 return pvfs_dosattrib_save(pvfs, name, fd);
239 set info on a open file
241 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
242 struct ntvfs_request *req,
243 union smb_setfileinfo *info)
245 struct pvfs_state *pvfs = ntvfs->private_data;
246 struct utimbuf unix_times;
248 struct pvfs_file_handle *h;
249 struct pvfs_filename newstats;
251 uint32_t access_needed;
253 f = pvfs_find_fd(pvfs, req, info->generic.in.file.fnum);
255 return NT_STATUS_INVALID_HANDLE;
260 access_needed = pvfs_setfileinfo_access(info);
261 if ((f->access_mask & access_needed) != access_needed) {
262 return NT_STATUS_ACCESS_DENIED;
265 /* update the file information */
266 status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
267 if (!NT_STATUS_IS_OK(status)) {
271 /* we take a copy of the current file stats, then update
272 newstats in each of the elements below. At the end we
273 compare, and make any changes needed */
276 switch (info->generic.level) {
277 case RAW_SFILEINFO_SETATTR:
278 if (!null_time(info->setattr.in.write_time)) {
279 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
281 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
282 newstats.dos.attrib = info->setattr.in.attrib;
286 case RAW_SFILEINFO_SETATTRE:
287 case RAW_SFILEINFO_STANDARD:
288 if (!null_time(info->setattre.in.create_time)) {
289 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
291 if (!null_time(info->setattre.in.access_time)) {
292 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
294 if (!null_time(info->setattre.in.write_time)) {
295 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
299 case RAW_SFILEINFO_EA_SET:
300 return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd,
301 info->ea_set.in.num_eas,
302 info->ea_set.in.eas);
304 case RAW_SFILEINFO_BASIC_INFO:
305 case RAW_SFILEINFO_BASIC_INFORMATION:
306 if (!null_nttime(info->basic_info.in.create_time)) {
307 newstats.dos.create_time = info->basic_info.in.create_time;
309 if (!null_nttime(info->basic_info.in.access_time)) {
310 newstats.dos.access_time = info->basic_info.in.access_time;
312 if (!null_nttime(info->basic_info.in.write_time)) {
313 newstats.dos.write_time = info->basic_info.in.write_time;
314 newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
315 h->sticky_write_time = True;
317 if (!null_nttime(info->basic_info.in.change_time)) {
318 newstats.dos.change_time = info->basic_info.in.change_time;
320 if (info->basic_info.in.attrib != 0) {
321 newstats.dos.attrib = info->basic_info.in.attrib;
325 case RAW_SFILEINFO_DISPOSITION_INFO:
326 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
327 return pvfs_set_delete_on_close(pvfs, req, f,
328 info->disposition_info.in.delete_on_close);
330 case RAW_SFILEINFO_ALLOCATION_INFO:
331 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
332 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
333 if (newstats.dos.alloc_size < newstats.st.st_size) {
334 newstats.st.st_size = newstats.dos.alloc_size;
336 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
337 newstats.dos.alloc_size);
340 case RAW_SFILEINFO_END_OF_FILE_INFO:
341 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
342 newstats.st.st_size = info->end_of_file_info.in.size;
345 case RAW_SFILEINFO_POSITION_INFORMATION:
346 h->position = info->position_information.in.position;
349 case RAW_SFILEINFO_MODE_INFORMATION:
350 /* this one is a puzzle */
351 if (info->mode_information.in.mode != 0 &&
352 info->mode_information.in.mode != 2 &&
353 info->mode_information.in.mode != 4 &&
354 info->mode_information.in.mode != 6) {
355 return NT_STATUS_INVALID_PARAMETER;
357 h->mode = info->mode_information.in.mode;
360 case RAW_SFILEINFO_RENAME_INFORMATION:
361 return pvfs_setfileinfo_rename(pvfs, req, h->name,
364 case RAW_SFILEINFO_SEC_DESC:
365 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
368 return NT_STATUS_INVALID_LEVEL;
371 /* possibly change the file size */
372 if (newstats.st.st_size != h->name->st.st_size) {
373 if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
374 return NT_STATUS_FILE_IS_A_DIRECTORY;
376 if (h->name->stream_name) {
377 status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
378 if (!NT_STATUS_IS_OK(status)) {
384 (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
385 ret = ftruncate(h->fd, newstats.st.st_size);
387 ret = truncate(h->name->full_name, newstats.st.st_size);
390 return pvfs_map_errno(pvfs, errno);
395 /* possibly change the file timestamps */
396 ZERO_STRUCT(unix_times);
397 if (newstats.dos.access_time != h->name->dos.access_time) {
398 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
400 if (newstats.dos.write_time != h->name->dos.write_time) {
401 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
403 if (unix_times.actime != 0 || unix_times.modtime != 0) {
404 if (utime(h->name->full_name, &unix_times) == -1) {
405 return pvfs_map_errno(pvfs, errno);
409 /* possibly change the attribute */
410 if (newstats.dos.attrib != h->name->dos.attrib) {
411 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
412 if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
413 if (fchmod(h->fd, mode) == -1) {
414 return pvfs_map_errno(pvfs, errno);
421 return pvfs_dosattrib_save(pvfs, h->name, h->fd);
426 set info on a pathname
428 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
429 struct ntvfs_request *req, union smb_setfileinfo *info)
431 struct pvfs_state *pvfs = ntvfs->private_data;
432 struct pvfs_filename *name;
433 struct pvfs_filename newstats;
435 struct utimbuf unix_times;
436 uint32_t access_needed;
438 /* resolve the cifs name to a posix name */
439 status = pvfs_resolve_name(pvfs, req, info->generic.in.file.path,
440 PVFS_RESOLVE_STREAMS, &name);
441 if (!NT_STATUS_IS_OK(status)) {
446 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
449 access_needed = pvfs_setfileinfo_access(info);
450 status = pvfs_access_check_simple(pvfs, req, name, access_needed);
451 if (!NT_STATUS_IS_OK(status)) {
455 /* we take a copy of the current file stats, then update
456 newstats in each of the elements below. At the end we
457 compare, and make any changes needed */
460 switch (info->generic.level) {
461 case RAW_SFILEINFO_SETATTR:
462 if (!null_time(info->setattr.in.write_time)) {
463 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
465 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
466 newstats.dos.attrib = info->setattr.in.attrib;
470 case RAW_SFILEINFO_SETATTRE:
471 case RAW_SFILEINFO_STANDARD:
472 if (!null_time(info->setattre.in.create_time)) {
473 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
475 if (!null_time(info->setattre.in.access_time)) {
476 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
478 if (!null_time(info->setattre.in.write_time)) {
479 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
483 case RAW_SFILEINFO_EA_SET:
484 return pvfs_setfileinfo_ea_set(pvfs, name, -1,
485 info->ea_set.in.num_eas,
486 info->ea_set.in.eas);
488 case RAW_SFILEINFO_BASIC_INFO:
489 case RAW_SFILEINFO_BASIC_INFORMATION:
490 if (!null_nttime(info->basic_info.in.create_time)) {
491 newstats.dos.create_time = info->basic_info.in.create_time;
493 if (!null_nttime(info->basic_info.in.access_time)) {
494 newstats.dos.access_time = info->basic_info.in.access_time;
496 if (!null_nttime(info->basic_info.in.write_time)) {
497 newstats.dos.write_time = info->basic_info.in.write_time;
499 if (!null_nttime(info->basic_info.in.change_time)) {
500 newstats.dos.change_time = info->basic_info.in.change_time;
502 if (info->basic_info.in.attrib != 0) {
503 newstats.dos.attrib = info->basic_info.in.attrib;
507 case RAW_SFILEINFO_ALLOCATION_INFO:
508 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
509 if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
510 /* strange. Increasing the allocation size via setpathinfo
511 should be silently ignored */
514 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
515 if (newstats.dos.alloc_size < newstats.st.st_size) {
516 newstats.st.st_size = newstats.dos.alloc_size;
518 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
519 newstats.dos.alloc_size);
522 case RAW_SFILEINFO_END_OF_FILE_INFO:
523 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
524 newstats.st.st_size = info->end_of_file_info.in.size;
527 case RAW_SFILEINFO_MODE_INFORMATION:
528 if (info->mode_information.in.mode != 0 &&
529 info->mode_information.in.mode != 2 &&
530 info->mode_information.in.mode != 4 &&
531 info->mode_information.in.mode != 6) {
532 return NT_STATUS_INVALID_PARAMETER;
536 case RAW_SFILEINFO_RENAME_INFORMATION:
537 return pvfs_setfileinfo_rename(pvfs, req, name,
540 case RAW_SFILEINFO_DISPOSITION_INFO:
541 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
542 case RAW_SFILEINFO_POSITION_INFORMATION:
546 return NT_STATUS_INVALID_LEVEL;
549 /* possibly change the file size */
550 if (newstats.st.st_size != name->st.st_size) {
551 if (name->stream_name) {
552 status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
553 if (!NT_STATUS_IS_OK(status)) {
556 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
557 return pvfs_map_errno(pvfs, errno);
561 /* possibly change the file timestamps */
562 ZERO_STRUCT(unix_times);
563 if (newstats.dos.access_time != name->dos.access_time) {
564 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
566 if (newstats.dos.write_time != name->dos.write_time) {
567 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
569 if (unix_times.actime != 0 || unix_times.modtime != 0) {
570 if (utime(name->full_name, &unix_times) == -1) {
571 return pvfs_map_errno(pvfs, errno);
575 /* possibly change the attribute */
576 newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
577 if (newstats.dos.attrib != name->dos.attrib) {
578 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
579 if (chmod(name->full_name, mode) == -1) {
580 return pvfs_map_errno(pvfs, errno);
586 return pvfs_dosattrib_save(pvfs, name, -1);