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_OWNER|SECINFO_GROUP)) {
57 needed |= SEC_STD_WRITE_OWNER;
59 if (info->set_secdesc.in.secinfo_flags & SECINFO_DACL) {
60 needed |= SEC_STD_WRITE_DAC;
62 if (info->set_secdesc.in.secinfo_flags & SECINFO_SACL) {
63 needed |= SEC_FLAG_SYSTEM_SECURITY;
68 needed = SEC_FILE_WRITE_ATTRIBUTE;
76 rename_information level
78 static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
79 struct ntvfs_request *req,
80 struct pvfs_filename *name,
81 union smb_setfileinfo *info)
84 struct pvfs_filename *name2;
87 /* renames are only allowed within a directory */
88 if (strchr_m(info->rename_information.in.new_name, '\\')) {
89 return NT_STATUS_NOT_SUPPORTED;
92 if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
93 /* don't allow this for now */
94 return NT_STATUS_FILE_IS_A_DIRECTORY;
97 /* don't allow stream renames for now */
98 if (name->stream_name) {
99 return NT_STATUS_INVALID_PARAMETER;
102 /* w2k3 does not appear to allow relative rename */
103 if (info->rename_information.in.root_fid != 0) {
104 return NT_STATUS_INVALID_PARAMETER;
107 /* construct the fully qualified windows name for the new file name */
108 new_name = talloc_strdup(req, name->original_name);
109 if (new_name == NULL) {
110 return NT_STATUS_NO_MEMORY;
112 p = strrchr_m(new_name, '\\');
114 return NT_STATUS_OBJECT_NAME_INVALID;
118 new_name = talloc_asprintf(req, "%s\\%s", new_name,
119 info->rename_information.in.new_name);
120 if (new_name == NULL) {
121 return NT_STATUS_NO_MEMORY;
124 /* resolve the new name */
125 status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
126 if (!NT_STATUS_IS_OK(status)) {
130 /* if the destination exists, then check the rename is allowed */
132 struct odb_lock *lck;
134 if (strcmp(name2->full_name, name->full_name) == 0) {
135 /* rename to same name is null-op */
139 if (!info->rename_information.in.overwrite) {
140 return NT_STATUS_OBJECT_NAME_COLLISION;
143 status = pvfs_can_delete(pvfs, req, name2, &lck);
144 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
145 return NT_STATUS_ACCESS_DENIED;
147 if (!NT_STATUS_IS_OK(status)) {
152 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
153 if (!NT_STATUS_IS_OK(status)) {
157 status = pvfs_do_rename(pvfs, name, name2->full_name);
158 if (NT_STATUS_IS_OK(status)) {
159 name->full_name = talloc_steal(name, name2->full_name);
160 name->original_name = talloc_steal(name, name2->original_name);
169 NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs,
170 struct pvfs_filename *name,
171 int fd, uint16_t num_eas,
172 struct ea_struct *eas)
174 struct xattr_DosEAs *ealist;
182 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
183 return NT_STATUS_NOT_SUPPORTED;
186 ealist = talloc(name, struct xattr_DosEAs);
188 /* load the current list */
189 status = pvfs_doseas_load(pvfs, name, fd, ealist);
190 if (!NT_STATUS_IS_OK(status)) {
194 for (j=0;j<num_eas;j++) {
195 struct ea_struct *ea = &eas[j];
196 /* see if its already there */
197 for (i=0;i<ealist->num_eas;i++) {
198 if (strcasecmp_m(ealist->eas[i].name, ea->name.s) == 0) {
199 ealist->eas[i].value = ea->value;
204 if (i==ealist->num_eas) {
206 ealist->eas = talloc_realloc(ealist, ealist->eas,
209 if (ealist->eas == NULL) {
210 return NT_STATUS_NO_MEMORY;
212 ealist->eas[i].name = ea->name.s;
213 ealist->eas[i].value = ea->value;
218 /* pull out any null EAs */
219 for (i=0;i<ealist->num_eas;i++) {
220 if (ealist->eas[i].value.length == 0) {
221 memmove(&ealist->eas[i],
223 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
229 status = pvfs_doseas_save(pvfs, name, fd, ealist);
230 if (!NT_STATUS_IS_OK(status)) {
234 notify_trigger(pvfs->notify_context,
235 NOTIFY_ACTION_MODIFIED,
236 FILE_NOTIFY_CHANGE_EA,
239 name->dos.ea_size = 4;
240 for (i=0;i<ealist->num_eas;i++) {
241 name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 +
242 ealist->eas[i].value.length;
245 /* update the ea_size attrib */
246 return pvfs_dosattrib_save(pvfs, name, fd);
250 set info on a open file
252 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
253 struct ntvfs_request *req,
254 union smb_setfileinfo *info)
256 struct pvfs_state *pvfs = ntvfs->private_data;
257 struct utimbuf unix_times;
259 struct pvfs_file_handle *h;
260 struct pvfs_filename newstats;
262 uint32_t access_needed;
263 uint32_t change_mask = 0;
265 f = pvfs_find_fd(pvfs, req, info->generic.in.file.ntvfs);
267 return NT_STATUS_INVALID_HANDLE;
272 access_needed = pvfs_setfileinfo_access(info);
273 if ((f->access_mask & access_needed) != access_needed) {
274 return NT_STATUS_ACCESS_DENIED;
277 /* update the file information */
278 status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
279 if (!NT_STATUS_IS_OK(status)) {
283 /* we take a copy of the current file stats, then update
284 newstats in each of the elements below. At the end we
285 compare, and make any changes needed */
288 switch (info->generic.level) {
289 case RAW_SFILEINFO_SETATTR:
290 if (!null_time(info->setattr.in.write_time)) {
291 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
293 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
294 newstats.dos.attrib = info->setattr.in.attrib;
298 case RAW_SFILEINFO_SETATTRE:
299 case RAW_SFILEINFO_STANDARD:
300 if (!null_time(info->setattre.in.create_time)) {
301 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
303 if (!null_time(info->setattre.in.access_time)) {
304 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
306 if (!null_time(info->setattre.in.write_time)) {
307 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
311 case RAW_SFILEINFO_EA_SET:
312 return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd,
313 info->ea_set.in.num_eas,
314 info->ea_set.in.eas);
316 case RAW_SFILEINFO_BASIC_INFO:
317 case RAW_SFILEINFO_BASIC_INFORMATION:
318 if (!null_nttime(info->basic_info.in.create_time)) {
319 newstats.dos.create_time = info->basic_info.in.create_time;
321 if (!null_nttime(info->basic_info.in.access_time)) {
322 newstats.dos.access_time = info->basic_info.in.access_time;
324 if (!null_nttime(info->basic_info.in.write_time)) {
325 newstats.dos.write_time = info->basic_info.in.write_time;
326 newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
327 h->sticky_write_time = True;
329 if (!null_nttime(info->basic_info.in.change_time)) {
330 newstats.dos.change_time = info->basic_info.in.change_time;
332 if (info->basic_info.in.attrib != 0) {
333 newstats.dos.attrib = info->basic_info.in.attrib;
337 case RAW_SFILEINFO_DISPOSITION_INFO:
338 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
339 return pvfs_set_delete_on_close(pvfs, req, f,
340 info->disposition_info.in.delete_on_close);
342 case RAW_SFILEINFO_ALLOCATION_INFO:
343 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
344 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
345 if (newstats.dos.alloc_size < newstats.st.st_size) {
346 newstats.st.st_size = newstats.dos.alloc_size;
348 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
349 newstats.dos.alloc_size);
352 case RAW_SFILEINFO_END_OF_FILE_INFO:
353 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
354 newstats.st.st_size = info->end_of_file_info.in.size;
357 case RAW_SFILEINFO_POSITION_INFORMATION:
358 h->position = info->position_information.in.position;
361 case RAW_SFILEINFO_MODE_INFORMATION:
362 /* this one is a puzzle */
363 if (info->mode_information.in.mode != 0 &&
364 info->mode_information.in.mode != 2 &&
365 info->mode_information.in.mode != 4 &&
366 info->mode_information.in.mode != 6) {
367 return NT_STATUS_INVALID_PARAMETER;
369 h->mode = info->mode_information.in.mode;
372 case RAW_SFILEINFO_RENAME_INFORMATION:
373 return pvfs_setfileinfo_rename(pvfs, req, h->name,
376 case RAW_SFILEINFO_SEC_DESC:
377 notify_trigger(pvfs->notify_context,
378 NOTIFY_ACTION_MODIFIED,
379 FILE_NOTIFY_CHANGE_SECURITY,
381 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
384 return NT_STATUS_INVALID_LEVEL;
387 /* possibly change the file size */
388 if (newstats.st.st_size != h->name->st.st_size) {
389 if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
390 return NT_STATUS_FILE_IS_A_DIRECTORY;
392 if (h->name->stream_name) {
393 status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
394 if (!NT_STATUS_IS_OK(status)) {
398 change_mask |= FILE_NOTIFY_CHANGE_STREAM_SIZE;
402 (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
403 ret = ftruncate(h->fd, newstats.st.st_size);
405 ret = truncate(h->name->full_name, newstats.st.st_size);
408 return pvfs_map_errno(pvfs, errno);
410 change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
414 /* possibly change the file timestamps */
415 ZERO_STRUCT(unix_times);
416 if (newstats.dos.create_time != h->name->dos.create_time) {
417 change_mask |= FILE_NOTIFY_CHANGE_CREATION;
419 if (newstats.dos.access_time != h->name->dos.access_time) {
420 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
421 change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
423 if (newstats.dos.write_time != h->name->dos.write_time) {
424 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
425 change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
427 if (unix_times.actime != 0 || unix_times.modtime != 0) {
428 if (utime(h->name->full_name, &unix_times) == -1) {
429 return pvfs_map_errno(pvfs, errno);
433 /* possibly change the attribute */
434 if (newstats.dos.attrib != h->name->dos.attrib) {
435 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
436 if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
437 if (fchmod(h->fd, mode) == -1) {
438 return pvfs_map_errno(pvfs, errno);
441 change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
446 notify_trigger(pvfs->notify_context,
447 NOTIFY_ACTION_MODIFIED,
451 return pvfs_dosattrib_save(pvfs, h->name, h->fd);
456 set info on a pathname
458 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
459 struct ntvfs_request *req, union smb_setfileinfo *info)
461 struct pvfs_state *pvfs = ntvfs->private_data;
462 struct pvfs_filename *name;
463 struct pvfs_filename newstats;
465 struct utimbuf unix_times;
466 uint32_t access_needed;
467 uint32_t change_mask = 0;
469 /* resolve the cifs name to a posix name */
470 status = pvfs_resolve_name(pvfs, req, info->generic.in.file.path,
471 PVFS_RESOLVE_STREAMS, &name);
472 if (!NT_STATUS_IS_OK(status)) {
477 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
480 access_needed = pvfs_setfileinfo_access(info);
481 status = pvfs_access_check_simple(pvfs, req, name, access_needed);
482 if (!NT_STATUS_IS_OK(status)) {
486 /* we take a copy of the current file stats, then update
487 newstats in each of the elements below. At the end we
488 compare, and make any changes needed */
491 switch (info->generic.level) {
492 case RAW_SFILEINFO_SETATTR:
493 if (!null_time(info->setattr.in.write_time)) {
494 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
496 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
497 newstats.dos.attrib = info->setattr.in.attrib;
501 case RAW_SFILEINFO_SETATTRE:
502 case RAW_SFILEINFO_STANDARD:
503 if (!null_time(info->setattre.in.create_time)) {
504 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
506 if (!null_time(info->setattre.in.access_time)) {
507 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
509 if (!null_time(info->setattre.in.write_time)) {
510 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
514 case RAW_SFILEINFO_EA_SET:
515 return pvfs_setfileinfo_ea_set(pvfs, name, -1,
516 info->ea_set.in.num_eas,
517 info->ea_set.in.eas);
519 case RAW_SFILEINFO_BASIC_INFO:
520 case RAW_SFILEINFO_BASIC_INFORMATION:
521 if (!null_nttime(info->basic_info.in.create_time)) {
522 newstats.dos.create_time = info->basic_info.in.create_time;
524 if (!null_nttime(info->basic_info.in.access_time)) {
525 newstats.dos.access_time = info->basic_info.in.access_time;
527 if (!null_nttime(info->basic_info.in.write_time)) {
528 newstats.dos.write_time = info->basic_info.in.write_time;
530 if (!null_nttime(info->basic_info.in.change_time)) {
531 newstats.dos.change_time = info->basic_info.in.change_time;
533 if (info->basic_info.in.attrib != 0) {
534 newstats.dos.attrib = info->basic_info.in.attrib;
538 case RAW_SFILEINFO_ALLOCATION_INFO:
539 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
540 if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
541 /* strange. Increasing the allocation size via setpathinfo
542 should be silently ignored */
545 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
546 if (newstats.dos.alloc_size < newstats.st.st_size) {
547 newstats.st.st_size = newstats.dos.alloc_size;
549 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
550 newstats.dos.alloc_size);
553 case RAW_SFILEINFO_END_OF_FILE_INFO:
554 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
555 newstats.st.st_size = info->end_of_file_info.in.size;
558 case RAW_SFILEINFO_MODE_INFORMATION:
559 if (info->mode_information.in.mode != 0 &&
560 info->mode_information.in.mode != 2 &&
561 info->mode_information.in.mode != 4 &&
562 info->mode_information.in.mode != 6) {
563 return NT_STATUS_INVALID_PARAMETER;
567 case RAW_SFILEINFO_RENAME_INFORMATION:
568 return pvfs_setfileinfo_rename(pvfs, req, name,
571 case RAW_SFILEINFO_DISPOSITION_INFO:
572 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
573 case RAW_SFILEINFO_POSITION_INFORMATION:
577 return NT_STATUS_INVALID_LEVEL;
580 /* possibly change the file size */
581 if (newstats.st.st_size != name->st.st_size) {
582 if (name->stream_name) {
583 status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
584 if (!NT_STATUS_IS_OK(status)) {
587 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
588 return pvfs_map_errno(pvfs, errno);
590 change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
593 /* possibly change the file timestamps */
594 ZERO_STRUCT(unix_times);
595 if (newstats.dos.create_time != name->dos.create_time) {
596 change_mask |= FILE_NOTIFY_CHANGE_CREATION;
598 if (newstats.dos.access_time != name->dos.access_time) {
599 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
600 change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
602 if (newstats.dos.write_time != name->dos.write_time) {
603 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
604 change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
606 if (unix_times.actime != 0 || unix_times.modtime != 0) {
607 if (utime(name->full_name, &unix_times) == -1) {
608 return pvfs_map_errno(pvfs, errno);
612 /* possibly change the attribute */
613 newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
614 if (newstats.dos.attrib != name->dos.attrib) {
615 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
616 if (chmod(name->full_name, mode) == -1) {
617 return pvfs_map_errno(pvfs, errno);
619 change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
624 if (change_mask != 0) {
625 notify_trigger(pvfs->notify_context,
626 NOTIFY_ACTION_MODIFIED,
631 return pvfs_dosattrib_save(pvfs, name, -1);