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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "vfs_posix.h"
24 #include "system/time.h"
25 #include "librpc/gen_ndr/xattr.h"
29 determine what access bits are needed for a call
31 static uint32_t pvfs_setfileinfo_access(union smb_setfileinfo *info)
35 switch (info->generic.level) {
36 case RAW_SFILEINFO_EA_SET:
37 needed = SEC_FILE_WRITE_EA;
40 case RAW_SFILEINFO_DISPOSITION_INFO:
41 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
42 needed = SEC_STD_DELETE;
45 case RAW_SFILEINFO_END_OF_FILE_INFO:
46 needed = SEC_FILE_WRITE_DATA;
49 case RAW_SFILEINFO_POSITION_INFORMATION:
53 case RAW_SFILEINFO_SEC_DESC:
55 if (info->set_secdesc.in.secinfo_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
56 needed |= SEC_STD_WRITE_OWNER;
58 if (info->set_secdesc.in.secinfo_flags & SECINFO_DACL) {
59 needed |= SEC_STD_WRITE_DAC;
61 if (info->set_secdesc.in.secinfo_flags & SECINFO_SACL) {
62 needed |= SEC_FLAG_SYSTEM_SECURITY;
67 needed = SEC_FILE_WRITE_ATTRIBUTE;
75 rename_information level
77 static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
78 struct ntvfs_request *req,
79 struct pvfs_filename *name,
80 union smb_setfileinfo *info)
83 struct pvfs_filename *name2;
86 /* renames are only allowed within a directory */
87 if (strchr_m(info->rename_information.in.new_name, '\\')) {
88 return NT_STATUS_NOT_SUPPORTED;
91 if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
92 /* don't allow this for now */
93 return NT_STATUS_FILE_IS_A_DIRECTORY;
96 /* don't allow stream renames for now */
97 if (name->stream_name) {
98 return NT_STATUS_INVALID_PARAMETER;
101 /* w2k3 does not appear to allow relative rename */
102 if (info->rename_information.in.root_fid != 0) {
103 return NT_STATUS_INVALID_PARAMETER;
106 /* construct the fully qualified windows name for the new file name */
107 new_name = talloc_strdup(req, name->original_name);
108 if (new_name == NULL) {
109 return NT_STATUS_NO_MEMORY;
111 p = strrchr_m(new_name, '\\');
113 return NT_STATUS_OBJECT_NAME_INVALID;
117 new_name = talloc_asprintf(req, "%s\\%s", new_name,
118 info->rename_information.in.new_name);
119 if (new_name == NULL) {
120 return NT_STATUS_NO_MEMORY;
123 /* resolve the new name */
124 status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
125 if (!NT_STATUS_IS_OK(status)) {
129 /* if the destination exists, then check the rename is allowed */
131 struct odb_lock *lck;
133 if (strcmp(name2->full_name, name->full_name) == 0) {
134 /* rename to same name is null-op */
138 if (!info->rename_information.in.overwrite) {
139 return NT_STATUS_OBJECT_NAME_COLLISION;
142 status = pvfs_can_delete(pvfs, req, name2, &lck);
143 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
144 return NT_STATUS_ACCESS_DENIED;
146 if (!NT_STATUS_IS_OK(status)) {
151 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
152 if (!NT_STATUS_IS_OK(status)) {
156 status = pvfs_do_rename(pvfs, name, name2->full_name);
157 if (NT_STATUS_IS_OK(status)) {
158 name->full_name = talloc_steal(name, name2->full_name);
159 name->original_name = talloc_steal(name, name2->original_name);
168 NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs,
169 struct pvfs_filename *name,
170 int fd, uint16_t num_eas,
171 struct ea_struct *eas)
173 struct xattr_DosEAs *ealist;
181 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
182 return NT_STATUS_NOT_SUPPORTED;
185 ealist = talloc(name, struct xattr_DosEAs);
187 /* load the current list */
188 status = pvfs_doseas_load(pvfs, name, fd, ealist);
189 if (!NT_STATUS_IS_OK(status)) {
193 for (j=0;j<num_eas;j++) {
194 struct ea_struct *ea = &eas[j];
195 /* see if its already there */
196 for (i=0;i<ealist->num_eas;i++) {
197 if (strcasecmp_m(ealist->eas[i].name, ea->name.s) == 0) {
198 ealist->eas[i].value = ea->value;
203 if (i==ealist->num_eas) {
205 ealist->eas = talloc_realloc(ealist, ealist->eas,
208 if (ealist->eas == NULL) {
209 return NT_STATUS_NO_MEMORY;
211 ealist->eas[i].name = ea->name.s;
212 ealist->eas[i].value = ea->value;
217 /* pull out any null EAs */
218 for (i=0;i<ealist->num_eas;i++) {
219 if (ealist->eas[i].value.length == 0) {
220 memmove(&ealist->eas[i],
222 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
228 status = pvfs_doseas_save(pvfs, name, fd, ealist);
229 if (!NT_STATUS_IS_OK(status)) {
233 notify_trigger(pvfs->notify_context,
234 NOTIFY_ACTION_MODIFIED,
235 FILE_NOTIFY_CHANGE_EA,
238 name->dos.ea_size = 4;
239 for (i=0;i<ealist->num_eas;i++) {
240 name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 +
241 ealist->eas[i].value.length;
244 /* update the ea_size attrib */
245 return pvfs_dosattrib_save(pvfs, name, fd);
249 set info on a open file
251 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
252 struct ntvfs_request *req,
253 union smb_setfileinfo *info)
255 struct pvfs_state *pvfs = ntvfs->private_data;
256 struct utimbuf unix_times;
258 struct pvfs_file_handle *h;
259 struct pvfs_filename newstats;
261 uint32_t access_needed;
262 uint32_t change_mask = 0;
264 f = pvfs_find_fd(pvfs, req, info->generic.in.file.ntvfs);
266 return NT_STATUS_INVALID_HANDLE;
271 access_needed = pvfs_setfileinfo_access(info);
272 if ((f->access_mask & access_needed) != access_needed) {
273 return NT_STATUS_ACCESS_DENIED;
276 /* update the file information */
277 status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
278 if (!NT_STATUS_IS_OK(status)) {
282 /* we take a copy of the current file stats, then update
283 newstats in each of the elements below. At the end we
284 compare, and make any changes needed */
287 switch (info->generic.level) {
288 case RAW_SFILEINFO_SETATTR:
289 if (!null_time(info->setattr.in.write_time)) {
290 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
292 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
293 newstats.dos.attrib = info->setattr.in.attrib;
297 case RAW_SFILEINFO_SETATTRE:
298 case RAW_SFILEINFO_STANDARD:
299 if (!null_time(info->setattre.in.create_time)) {
300 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
302 if (!null_time(info->setattre.in.access_time)) {
303 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
305 if (!null_time(info->setattre.in.write_time)) {
306 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
310 case RAW_SFILEINFO_EA_SET:
311 return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd,
312 info->ea_set.in.num_eas,
313 info->ea_set.in.eas);
315 case RAW_SFILEINFO_BASIC_INFO:
316 case RAW_SFILEINFO_BASIC_INFORMATION:
317 if (!null_nttime(info->basic_info.in.create_time)) {
318 newstats.dos.create_time = info->basic_info.in.create_time;
320 if (!null_nttime(info->basic_info.in.access_time)) {
321 newstats.dos.access_time = info->basic_info.in.access_time;
323 if (!null_nttime(info->basic_info.in.write_time)) {
324 newstats.dos.write_time = info->basic_info.in.write_time;
325 newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
326 h->sticky_write_time = True;
328 if (!null_nttime(info->basic_info.in.change_time)) {
329 newstats.dos.change_time = info->basic_info.in.change_time;
331 if (info->basic_info.in.attrib != 0) {
332 newstats.dos.attrib = info->basic_info.in.attrib;
336 case RAW_SFILEINFO_DISPOSITION_INFO:
337 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
338 return pvfs_set_delete_on_close(pvfs, req, f,
339 info->disposition_info.in.delete_on_close);
341 case RAW_SFILEINFO_ALLOCATION_INFO:
342 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
343 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
344 if (newstats.dos.alloc_size < newstats.st.st_size) {
345 newstats.st.st_size = newstats.dos.alloc_size;
347 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
348 newstats.dos.alloc_size);
351 case RAW_SFILEINFO_END_OF_FILE_INFO:
352 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
353 newstats.st.st_size = info->end_of_file_info.in.size;
356 case RAW_SFILEINFO_POSITION_INFORMATION:
357 h->position = info->position_information.in.position;
360 case RAW_SFILEINFO_MODE_INFORMATION:
361 /* this one is a puzzle */
362 if (info->mode_information.in.mode != 0 &&
363 info->mode_information.in.mode != 2 &&
364 info->mode_information.in.mode != 4 &&
365 info->mode_information.in.mode != 6) {
366 return NT_STATUS_INVALID_PARAMETER;
368 h->mode = info->mode_information.in.mode;
371 case RAW_SFILEINFO_RENAME_INFORMATION:
372 return pvfs_setfileinfo_rename(pvfs, req, h->name,
375 case RAW_SFILEINFO_SEC_DESC:
376 notify_trigger(pvfs->notify_context,
377 NOTIFY_ACTION_MODIFIED,
378 FILE_NOTIFY_CHANGE_SECURITY,
380 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
383 return NT_STATUS_INVALID_LEVEL;
386 /* possibly change the file size */
387 if (newstats.st.st_size != h->name->st.st_size) {
388 if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
389 return NT_STATUS_FILE_IS_A_DIRECTORY;
391 if (h->name->stream_name) {
392 status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
393 if (!NT_STATUS_IS_OK(status)) {
397 change_mask |= FILE_NOTIFY_CHANGE_STREAM_SIZE;
401 (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
402 ret = ftruncate(h->fd, newstats.st.st_size);
404 ret = truncate(h->name->full_name, newstats.st.st_size);
407 return pvfs_map_errno(pvfs, errno);
409 change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
413 /* possibly change the file timestamps */
414 ZERO_STRUCT(unix_times);
415 if (newstats.dos.create_time != h->name->dos.create_time) {
416 change_mask |= FILE_NOTIFY_CHANGE_CREATION;
418 if (newstats.dos.access_time != h->name->dos.access_time) {
419 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
420 change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
422 if (newstats.dos.write_time != h->name->dos.write_time) {
423 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
424 change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
426 if (unix_times.actime != 0 || unix_times.modtime != 0) {
427 if (utime(h->name->full_name, &unix_times) == -1) {
428 return pvfs_map_errno(pvfs, errno);
432 /* possibly change the attribute */
433 if (newstats.dos.attrib != h->name->dos.attrib) {
434 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
435 if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
436 if (fchmod(h->fd, mode) == -1) {
437 return pvfs_map_errno(pvfs, errno);
440 change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
445 notify_trigger(pvfs->notify_context,
446 NOTIFY_ACTION_MODIFIED,
450 return pvfs_dosattrib_save(pvfs, h->name, h->fd);
455 set info on a pathname
457 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
458 struct ntvfs_request *req, union smb_setfileinfo *info)
460 struct pvfs_state *pvfs = ntvfs->private_data;
461 struct pvfs_filename *name;
462 struct pvfs_filename newstats;
464 struct utimbuf unix_times;
465 uint32_t access_needed;
466 uint32_t change_mask = 0;
468 /* resolve the cifs name to a posix name */
469 status = pvfs_resolve_name(pvfs, req, info->generic.in.file.path,
470 PVFS_RESOLVE_STREAMS, &name);
471 if (!NT_STATUS_IS_OK(status)) {
476 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
479 access_needed = pvfs_setfileinfo_access(info);
480 status = pvfs_access_check_simple(pvfs, req, name, access_needed);
481 if (!NT_STATUS_IS_OK(status)) {
485 /* we take a copy of the current file stats, then update
486 newstats in each of the elements below. At the end we
487 compare, and make any changes needed */
490 switch (info->generic.level) {
491 case RAW_SFILEINFO_SETATTR:
492 if (!null_time(info->setattr.in.write_time)) {
493 unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
495 if (info->setattr.in.attrib == 0) {
496 newstats.dos.attrib = FILE_ATTRIBUTE_NORMAL;
497 } else if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
498 newstats.dos.attrib = info->setattr.in.attrib;
502 case RAW_SFILEINFO_SETATTRE:
503 case RAW_SFILEINFO_STANDARD:
504 if (!null_time(info->setattre.in.create_time)) {
505 unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
507 if (!null_time(info->setattre.in.access_time)) {
508 unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
510 if (!null_time(info->setattre.in.write_time)) {
511 unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
515 case RAW_SFILEINFO_EA_SET:
516 return pvfs_setfileinfo_ea_set(pvfs, name, -1,
517 info->ea_set.in.num_eas,
518 info->ea_set.in.eas);
520 case RAW_SFILEINFO_BASIC_INFO:
521 case RAW_SFILEINFO_BASIC_INFORMATION:
522 if (!null_nttime(info->basic_info.in.create_time)) {
523 newstats.dos.create_time = info->basic_info.in.create_time;
525 if (!null_nttime(info->basic_info.in.access_time)) {
526 newstats.dos.access_time = info->basic_info.in.access_time;
528 if (!null_nttime(info->basic_info.in.write_time)) {
529 newstats.dos.write_time = info->basic_info.in.write_time;
531 if (!null_nttime(info->basic_info.in.change_time)) {
532 newstats.dos.change_time = info->basic_info.in.change_time;
534 if (info->basic_info.in.attrib != 0) {
535 newstats.dos.attrib = info->basic_info.in.attrib;
539 case RAW_SFILEINFO_ALLOCATION_INFO:
540 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
541 if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
542 /* strange. Increasing the allocation size via setpathinfo
543 should be silently ignored */
546 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
547 if (newstats.dos.alloc_size < newstats.st.st_size) {
548 newstats.st.st_size = newstats.dos.alloc_size;
550 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs,
551 newstats.dos.alloc_size);
554 case RAW_SFILEINFO_END_OF_FILE_INFO:
555 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
556 newstats.st.st_size = info->end_of_file_info.in.size;
559 case RAW_SFILEINFO_MODE_INFORMATION:
560 if (info->mode_information.in.mode != 0 &&
561 info->mode_information.in.mode != 2 &&
562 info->mode_information.in.mode != 4 &&
563 info->mode_information.in.mode != 6) {
564 return NT_STATUS_INVALID_PARAMETER;
568 case RAW_SFILEINFO_RENAME_INFORMATION:
569 return pvfs_setfileinfo_rename(pvfs, req, name,
572 case RAW_SFILEINFO_DISPOSITION_INFO:
573 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
574 case RAW_SFILEINFO_POSITION_INFORMATION:
578 return NT_STATUS_INVALID_LEVEL;
581 /* possibly change the file size */
582 if (newstats.st.st_size != name->st.st_size) {
583 if (name->stream_name) {
584 status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
585 if (!NT_STATUS_IS_OK(status)) {
588 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
589 return pvfs_map_errno(pvfs, errno);
591 change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
594 /* possibly change the file timestamps */
595 ZERO_STRUCT(unix_times);
596 if (newstats.dos.create_time != name->dos.create_time) {
597 change_mask |= FILE_NOTIFY_CHANGE_CREATION;
599 if (newstats.dos.access_time != name->dos.access_time) {
600 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
601 change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
603 if (newstats.dos.write_time != name->dos.write_time) {
604 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
605 change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
607 if (unix_times.actime != 0 || unix_times.modtime != 0) {
608 if (utime(name->full_name, &unix_times) == -1) {
609 return pvfs_map_errno(pvfs, errno);
613 /* possibly change the attribute */
614 newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
615 if (newstats.dos.attrib != name->dos.attrib) {
616 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
617 if (chmod(name->full_name, mode) == -1) {
618 return pvfs_map_errno(pvfs, errno);
620 change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
625 if (change_mask != 0) {
626 notify_trigger(pvfs->notify_context,
627 NOTIFY_ACTION_MODIFIED,
632 return pvfs_dosattrib_save(pvfs, name, -1);