r4615: added acl checking on directory search in pvfs
[ira/wip.git] / source4 / ntvfs / posix / pvfs_setfileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - setfileinfo
5
6    Copyright (C) Andrew Tridgell 2004
7
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "system/time.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27
28
29 /*
30   determine what access bits are needed for a call
31 */
32 static uint32_t pvfs_setfileinfo_access(union smb_setfileinfo *info)
33 {
34         uint32_t needed;
35
36         switch (info->generic.level) {
37         case RAW_SFILEINFO_EA_SET:
38                 needed = SEC_FILE_WRITE_EA;
39                 break;
40
41         case RAW_SFILEINFO_DISPOSITION_INFO:
42         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
43                 needed = SEC_STD_DELETE;
44                 break;
45
46         case RAW_SFILEINFO_END_OF_FILE_INFO:
47                 needed = SEC_FILE_WRITE_DATA;
48                 break;
49
50         case RAW_SFILEINFO_POSITION_INFORMATION:
51                 needed = 0;
52                 break;
53
54         case RAW_SFILEINFO_SEC_DESC:
55                 needed = 0;
56                 if (info->set_secdesc.in.secinfo_flags & (SECINFO_DACL|SECINFO_SACL)) {
57                         needed |= SEC_STD_WRITE_DAC;
58                 }
59                 break;
60
61         default:
62                 needed = SEC_FILE_WRITE_ATTRIBUTE;
63                 break;
64         }
65         return needed;  
66 }
67
68 /*
69   rename_information level
70 */
71 static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs, 
72                                         struct smbsrv_request *req, 
73                                         struct pvfs_filename *name,
74                                         struct smb_rename_information *r)
75 {
76         NTSTATUS status;
77         struct pvfs_filename *name2;
78         char *new_name, *p;
79
80         /* renames are only allowed within a directory */
81         if (strchr_m(r->new_name, '\\')) {
82                 return NT_STATUS_NOT_SUPPORTED;
83         }
84
85         if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
86                 /* don't allow this for now */
87                 return NT_STATUS_FILE_IS_A_DIRECTORY;
88         }
89
90         /* don't allow stream renames for now */
91         if (name->stream_name) {
92                 return NT_STATUS_INVALID_PARAMETER;
93         }
94
95         /* w2k3 does not appear to allow relative rename */
96         if (r->root_fid != 0) {
97                 return NT_STATUS_INVALID_PARAMETER;
98         }
99
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;
104         }
105         p = strrchr_m(new_name, '\\');
106         if (p == NULL) {
107                 return NT_STATUS_OBJECT_NAME_INVALID;
108         }
109         *p = 0;
110
111         new_name = talloc_asprintf(req, "%s\\%s", new_name, r->new_name);
112         if (new_name == NULL) {
113                 return NT_STATUS_NO_MEMORY;
114         }
115
116         /* resolve the new name */
117         status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
118         if (!NT_STATUS_IS_OK(status)) {
119                 return status;
120         }
121
122         /* if the destination exists, then check the rename is allowed */
123         if (name2->exists) {
124                 if (strcmp(name2->full_name, name->full_name) == 0) {
125                         /* rename to same name is null-op */
126                         return NT_STATUS_OK;
127                 }
128
129                 if (!r->overwrite) {
130                         return NT_STATUS_OBJECT_NAME_COLLISION;
131                 }
132
133                 status = pvfs_can_delete(pvfs, req, name2);
134                 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
135                         return NT_STATUS_ACCESS_DENIED;
136                 }
137                 if (!NT_STATUS_IS_OK(status)) {
138                         return status;
139                 }
140         }
141
142         status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
143         if (!NT_STATUS_IS_OK(status)) {
144                 return status;
145         }
146
147         if (rename(name->full_name, name2->full_name) == -1) {
148                 return pvfs_map_errno(pvfs, errno);
149         }
150
151         name->full_name = talloc_steal(name, name2->full_name);
152         name->original_name = talloc_steal(name, name2->original_name);
153
154         return NT_STATUS_OK;
155 }
156
157 /*
158   add a single DOS EA
159 */
160 NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs, 
161                                  struct pvfs_filename *name,
162                                  int fd, uint16_t num_eas,
163                                  struct ea_struct *eas)
164 {
165         struct xattr_DosEAs *ealist;
166         int i, j;
167         NTSTATUS status;
168
169         if (num_eas == 0) {
170                 return NT_STATUS_OK;
171         }
172
173         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
174                 return NT_STATUS_NOT_SUPPORTED;
175         }
176
177         ealist = talloc_p(name, struct xattr_DosEAs);
178
179         /* load the current list */
180         status = pvfs_doseas_load(pvfs, name, fd, ealist);
181         if (!NT_STATUS_IS_OK(status)) {
182                 return status;
183         }
184
185         for (j=0;j<num_eas;j++) {
186                 struct ea_struct *ea = &eas[j];
187                 /* see if its already there */
188                 for (i=0;i<ealist->num_eas;i++) {
189                         if (StrCaseCmp(ealist->eas[i].name, ea->name.s) == 0) {
190                                 ealist->eas[i].value = ea->value;
191                                 break;
192                         }
193                 }
194
195                 if (i==ealist->num_eas) {
196                         /* add it */
197                         ealist->eas = talloc_realloc_p(ealist, ealist->eas, 
198                                                        struct xattr_EA, 
199                                                        ealist->num_eas+1);
200                         if (ealist->eas == NULL) {
201                                 return NT_STATUS_NO_MEMORY;
202                         }
203                         ealist->eas[i].name = ea->name.s;
204                         ealist->eas[i].value = ea->value;
205                         ealist->num_eas++;
206                 }
207         }
208         
209         /* pull out any null EAs */
210         for (i=0;i<ealist->num_eas;i++) {
211                 if (ealist->eas[i].value.length == 0) {
212                         memmove(&ealist->eas[i],
213                                 &ealist->eas[i+1],
214                                 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
215                         ealist->num_eas--;
216                         i--;
217                 }
218         }
219
220         status = pvfs_doseas_save(pvfs, name, fd, ealist);
221         if (!NT_STATUS_IS_OK(status)) {
222                 return status;
223         }
224
225         name->dos.ea_size = 4;
226         for (i=0;i<ealist->num_eas;i++) {
227                 name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 + 
228                         ealist->eas[i].value.length;
229         }
230
231         /* update the ea_size attrib */
232         return pvfs_dosattrib_save(pvfs, name, fd);
233 }
234
235 /*
236   set info on a open file
237 */
238 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
239                           struct smbsrv_request *req, 
240                           union smb_setfileinfo *info)
241 {
242         struct pvfs_state *pvfs = ntvfs->private_data;
243         struct utimbuf unix_times;
244         struct pvfs_file *f;
245         struct pvfs_file_handle *h;
246         uint32_t create_options;
247         struct pvfs_filename newstats;
248         NTSTATUS status;
249         uint32_t access_needed;
250
251         f = pvfs_find_fd(pvfs, req, info->generic.file.fnum);
252         if (!f) {
253                 return NT_STATUS_INVALID_HANDLE;
254         }
255
256         h = f->handle;
257
258         access_needed = pvfs_setfileinfo_access(info);
259         if ((f->access_mask & access_needed) != access_needed) {
260                 return NT_STATUS_ACCESS_DENIED;
261         }
262
263         /* update the file information */
264         status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
265         if (!NT_STATUS_IS_OK(status)) {
266                 return status;
267         }
268
269         /* we take a copy of the current file stats, then update
270            newstats in each of the elements below. At the end we
271            compare, and make any changes needed */
272         newstats = *h->name;
273
274         switch (info->generic.level) {
275         case RAW_SFILEINFO_SETATTR:
276                 if (!null_time(info->setattr.in.write_time)) {
277                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
278                 }
279                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
280                         newstats.dos.attrib = info->setattr.in.attrib;
281                 }
282                 break;
283
284         case RAW_SFILEINFO_SETATTRE:
285         case RAW_SFILEINFO_STANDARD:
286                 if (!null_time(info->setattre.in.create_time)) {
287                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
288                 }
289                 if (!null_time(info->setattre.in.access_time)) {
290                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
291                 }
292                 if (!null_time(info->setattre.in.write_time)) {
293                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
294                 }
295                 break;
296
297         case RAW_SFILEINFO_EA_SET:
298                 return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd, 
299                                                info->ea_set.in.num_eas,
300                                                info->ea_set.in.eas);
301
302         case RAW_SFILEINFO_BASIC_INFO:
303         case RAW_SFILEINFO_BASIC_INFORMATION:
304                 if (info->basic_info.in.create_time) {
305                         newstats.dos.create_time = info->basic_info.in.create_time;
306                 }
307                 if (info->basic_info.in.access_time) {
308                         newstats.dos.access_time = info->basic_info.in.access_time;
309                 }
310                 if (info->basic_info.in.write_time) {
311                         newstats.dos.write_time = info->basic_info.in.write_time;
312                         newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
313                         h->sticky_write_time = True;
314                 }
315                 if (info->basic_info.in.change_time) {
316                         newstats.dos.change_time = info->basic_info.in.change_time;
317                 }
318                 if (info->basic_info.in.attrib != 0) {
319                         newstats.dos.attrib = info->basic_info.in.attrib;
320                 }
321                 break;
322
323         case RAW_SFILEINFO_DISPOSITION_INFO:
324         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
325                 create_options = h->create_options;
326                 if (info->disposition_info.in.delete_on_close) {
327                         create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
328                 } else {
329                         create_options &= ~NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
330                 }
331                 return pvfs_change_create_options(pvfs, req, f, create_options);
332
333         case RAW_SFILEINFO_ALLOCATION_INFO:
334         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
335                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
336                 if (newstats.dos.alloc_size < newstats.st.st_size) {
337                         newstats.st.st_size = newstats.dos.alloc_size;
338                 }
339                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
340                                                                 newstats.dos.alloc_size);
341                 break;
342
343         case RAW_SFILEINFO_END_OF_FILE_INFO:
344         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
345                 newstats.st.st_size = info->end_of_file_info.in.size;
346                 break;
347
348         case RAW_SFILEINFO_POSITION_INFORMATION:
349                 h->position = info->position_information.in.position;
350                 break;
351
352         case RAW_SFILEINFO_MODE_INFORMATION:
353                 /* this one is a puzzle */
354                 if (info->mode_information.in.mode != 0 &&
355                     info->mode_information.in.mode != 2 &&
356                     info->mode_information.in.mode != 4 &&
357                     info->mode_information.in.mode != 6) {
358                         return NT_STATUS_INVALID_PARAMETER;
359                 }
360                 h->mode = info->mode_information.in.mode;
361                 break;
362
363         case RAW_SFILEINFO_RENAME_INFORMATION:
364                 return pvfs_setfileinfo_rename(pvfs, req, h->name, 
365                                                &info->rename_information.in);
366
367         case RAW_SFILEINFO_SEC_DESC:
368                 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
369
370         default:
371                 return NT_STATUS_INVALID_LEVEL;
372         }
373
374         /* possibly change the file size */
375         if (newstats.st.st_size != h->name->st.st_size) {
376                 if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
377                         return NT_STATUS_FILE_IS_A_DIRECTORY;
378                 }
379                 if (h->name->stream_name) {
380                         status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
381                         if (!NT_STATUS_IS_OK(status)) {
382                                 return status;
383                         }
384                 } else {
385                         int ret;
386                         if (f->access_mask & 
387                             (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
388                                 ret = ftruncate(h->fd, newstats.st.st_size);
389                         } else {
390                                 ret = truncate(h->name->full_name, newstats.st.st_size);
391                         }
392                         if (ret == -1) {
393                                 return pvfs_map_errno(pvfs, errno);
394                         }
395                 }
396         }
397
398         /* possibly change the file timestamps */
399         ZERO_STRUCT(unix_times);
400         if (newstats.dos.access_time != h->name->dos.access_time) {
401                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
402         }
403         if (newstats.dos.write_time != h->name->dos.write_time) {
404                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
405         }
406         if (unix_times.actime != 0 || unix_times.modtime != 0) {
407                 if (utime(h->name->full_name, &unix_times) == -1) {
408                         return pvfs_map_errno(pvfs, errno);
409                 }
410         }
411
412         /* possibly change the attribute */
413         if (newstats.dos.attrib != h->name->dos.attrib) {
414                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
415                 if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
416                         if (fchmod(h->fd, mode) == -1) {
417                                 return pvfs_map_errno(pvfs, errno);
418                         }
419                 }
420         }
421
422         *h->name = newstats;
423
424         return pvfs_dosattrib_save(pvfs, h->name, h->fd);
425 }
426
427
428 /*
429   set info on a pathname
430 */
431 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
432                           struct smbsrv_request *req, union smb_setfileinfo *info)
433 {
434         struct pvfs_state *pvfs = ntvfs->private_data;
435         struct pvfs_filename *name;
436         struct pvfs_filename newstats;
437         NTSTATUS status;
438         struct utimbuf unix_times;
439         uint32_t access_needed;
440
441         /* resolve the cifs name to a posix name */
442         status = pvfs_resolve_name(pvfs, req, info->generic.file.fname, 
443                                    PVFS_RESOLVE_STREAMS, &name);
444         if (!NT_STATUS_IS_OK(status)) {
445                 return status;
446         }
447
448         if (!name->exists) {
449                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
450         }
451
452         access_needed = pvfs_setfileinfo_access(info);
453         status = pvfs_access_check_simple(pvfs, req, name, access_needed);
454         if (!NT_STATUS_IS_OK(status)) {
455                 return status;
456         }
457
458         /* we take a copy of the current file stats, then update
459            newstats in each of the elements below. At the end we
460            compare, and make any changes needed */
461         newstats = *name;
462
463         switch (info->generic.level) {
464         case RAW_SFILEINFO_SETATTR:
465                 if (!null_time(info->setattr.in.write_time)) {
466                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
467                 }
468                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
469                         newstats.dos.attrib = info->setattr.in.attrib;
470                 }
471                 break;
472
473         case RAW_SFILEINFO_SETATTRE:
474         case RAW_SFILEINFO_STANDARD:
475                 if (!null_time(info->setattre.in.create_time)) {
476                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
477                 }
478                 if (!null_time(info->setattre.in.access_time)) {
479                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
480                 }
481                 if (!null_time(info->setattre.in.write_time)) {
482                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
483                 }
484                 break;
485
486         case RAW_SFILEINFO_EA_SET:
487                 return pvfs_setfileinfo_ea_set(pvfs, name, -1, 
488                                                info->ea_set.in.num_eas,
489                                                info->ea_set.in.eas);
490
491         case RAW_SFILEINFO_BASIC_INFO:
492         case RAW_SFILEINFO_BASIC_INFORMATION:
493                 if (info->basic_info.in.create_time) {
494                         newstats.dos.create_time = info->basic_info.in.create_time;
495                 }
496                 if (info->basic_info.in.access_time) {
497                         newstats.dos.access_time = info->basic_info.in.access_time;
498                 }
499                 if (info->basic_info.in.write_time) {
500                         newstats.dos.write_time = info->basic_info.in.write_time;
501                 }
502                 if (info->basic_info.in.change_time) {
503                         newstats.dos.change_time = info->basic_info.in.change_time;
504                 }
505                 if (info->basic_info.in.attrib != 0) {
506                         newstats.dos.attrib = info->basic_info.in.attrib;
507                 }
508                 break;
509
510         case RAW_SFILEINFO_ALLOCATION_INFO:
511         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
512                 if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
513                         /* strange. Increasing the allocation size via setpathinfo 
514                            should be silently ignored */
515                         break;
516                 }
517                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
518                 if (newstats.dos.alloc_size < newstats.st.st_size) {
519                         newstats.st.st_size = newstats.dos.alloc_size;
520                 }
521                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
522                                                                 newstats.dos.alloc_size);
523                 break;
524
525         case RAW_SFILEINFO_END_OF_FILE_INFO:
526         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
527                 newstats.st.st_size = info->end_of_file_info.in.size;
528                 break;
529
530         case RAW_SFILEINFO_MODE_INFORMATION:
531                 if (info->mode_information.in.mode != 0 &&
532                     info->mode_information.in.mode != 2 &&
533                     info->mode_information.in.mode != 4 &&
534                     info->mode_information.in.mode != 6) {
535                         return NT_STATUS_INVALID_PARAMETER;
536                 }
537                 return NT_STATUS_OK;
538
539         case RAW_SFILEINFO_RENAME_INFORMATION:
540                 return pvfs_setfileinfo_rename(pvfs, req, name, 
541                                                &info->rename_information.in);
542
543         case RAW_SFILEINFO_DISPOSITION_INFO:
544         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
545         case RAW_SFILEINFO_POSITION_INFORMATION:
546                 return NT_STATUS_OK;
547
548         default:
549                 return NT_STATUS_INVALID_LEVEL;
550         }
551
552         /* possibly change the file size */
553         if (newstats.st.st_size != name->st.st_size) {
554                 if (name->stream_name) {
555                         status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
556                         if (!NT_STATUS_IS_OK(status)) {
557                                 return status;
558                         }
559                 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
560                         return pvfs_map_errno(pvfs, errno);
561                 }
562         }
563
564         /* possibly change the file timestamps */
565         ZERO_STRUCT(unix_times);
566         if (newstats.dos.access_time != name->dos.access_time) {
567                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
568         }
569         if (newstats.dos.write_time != name->dos.write_time) {
570                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
571         }
572         if (unix_times.actime != 0 || unix_times.modtime != 0) {
573                 if (utime(name->full_name, &unix_times) == -1) {
574                         return pvfs_map_errno(pvfs, errno);
575                 }
576         }
577
578         /* possibly change the attribute */
579         newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
580         if (newstats.dos.attrib != name->dos.attrib) {
581                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
582                 if (chmod(name->full_name, mode) == -1) {
583                         return pvfs_map_errno(pvfs, errno);
584                 }
585         }
586
587         *name = newstats;
588
589         return pvfs_dosattrib_save(pvfs, name, -1);
590 }
591