5a6643bdd4a489799f2a6279bde58ebb1adf2cf2
[gd/samba-autobuild/.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 ntvfs_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                 struct odb_lock *lck;
125
126                 if (strcmp(name2->full_name, name->full_name) == 0) {
127                         /* rename to same name is null-op */
128                         return NT_STATUS_OK;
129                 }
130
131                 if (!r->overwrite) {
132                         return NT_STATUS_OBJECT_NAME_COLLISION;
133                 }
134
135                 status = pvfs_can_delete(pvfs, req, name2, &lck);
136                 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
137                         return NT_STATUS_ACCESS_DENIED;
138                 }
139                 if (!NT_STATUS_IS_OK(status)) {
140                         return status;
141                 }
142         }
143
144         status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
145         if (!NT_STATUS_IS_OK(status)) {
146                 return status;
147         }
148
149         if (rename(name->full_name, name2->full_name) == -1) {
150                 return pvfs_map_errno(pvfs, errno);
151         }
152
153         name->full_name = talloc_steal(name, name2->full_name);
154         name->original_name = talloc_steal(name, name2->original_name);
155
156         return NT_STATUS_OK;
157 }
158
159 /*
160   add a single DOS EA
161 */
162 NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs, 
163                                  struct pvfs_filename *name,
164                                  int fd, uint16_t num_eas,
165                                  struct ea_struct *eas)
166 {
167         struct xattr_DosEAs *ealist;
168         int i, j;
169         NTSTATUS status;
170
171         if (num_eas == 0) {
172                 return NT_STATUS_OK;
173         }
174
175         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
176                 return NT_STATUS_NOT_SUPPORTED;
177         }
178
179         ealist = talloc(name, struct xattr_DosEAs);
180
181         /* load the current list */
182         status = pvfs_doseas_load(pvfs, name, fd, ealist);
183         if (!NT_STATUS_IS_OK(status)) {
184                 return status;
185         }
186
187         for (j=0;j<num_eas;j++) {
188                 struct ea_struct *ea = &eas[j];
189                 /* see if its already there */
190                 for (i=0;i<ealist->num_eas;i++) {
191                         if (strcasecmp_m(ealist->eas[i].name, ea->name.s) == 0) {
192                                 ealist->eas[i].value = ea->value;
193                                 break;
194                         }
195                 }
196
197                 if (i==ealist->num_eas) {
198                         /* add it */
199                         ealist->eas = talloc_realloc(ealist, ealist->eas, 
200                                                        struct xattr_EA, 
201                                                        ealist->num_eas+1);
202                         if (ealist->eas == NULL) {
203                                 return NT_STATUS_NO_MEMORY;
204                         }
205                         ealist->eas[i].name = ea->name.s;
206                         ealist->eas[i].value = ea->value;
207                         ealist->num_eas++;
208                 }
209         }
210         
211         /* pull out any null EAs */
212         for (i=0;i<ealist->num_eas;i++) {
213                 if (ealist->eas[i].value.length == 0) {
214                         memmove(&ealist->eas[i],
215                                 &ealist->eas[i+1],
216                                 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
217                         ealist->num_eas--;
218                         i--;
219                 }
220         }
221
222         status = pvfs_doseas_save(pvfs, name, fd, ealist);
223         if (!NT_STATUS_IS_OK(status)) {
224                 return status;
225         }
226
227         name->dos.ea_size = 4;
228         for (i=0;i<ealist->num_eas;i++) {
229                 name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 + 
230                         ealist->eas[i].value.length;
231         }
232
233         /* update the ea_size attrib */
234         return pvfs_dosattrib_save(pvfs, name, fd);
235 }
236
237 /*
238   set info on a open file
239 */
240 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
241                           struct ntvfs_request *req, 
242                           union smb_setfileinfo *info)
243 {
244         struct pvfs_state *pvfs = ntvfs->private_data;
245         struct utimbuf unix_times;
246         struct pvfs_file *f;
247         struct pvfs_file_handle *h;
248         struct pvfs_filename newstats;
249         NTSTATUS status;
250         uint32_t access_needed;
251
252         f = pvfs_find_fd(pvfs, req, info->generic.file.fnum);
253         if (!f) {
254                 return NT_STATUS_INVALID_HANDLE;
255         }
256
257         h = f->handle;
258
259         access_needed = pvfs_setfileinfo_access(info);
260         if ((f->access_mask & access_needed) != access_needed) {
261                 return NT_STATUS_ACCESS_DENIED;
262         }
263
264         /* update the file information */
265         status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
266         if (!NT_STATUS_IS_OK(status)) {
267                 return status;
268         }
269
270         /* we take a copy of the current file stats, then update
271            newstats in each of the elements below. At the end we
272            compare, and make any changes needed */
273         newstats = *h->name;
274
275         switch (info->generic.level) {
276         case RAW_SFILEINFO_SETATTR:
277                 if (!null_time(info->setattr.in.write_time)) {
278                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
279                 }
280                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
281                         newstats.dos.attrib = info->setattr.in.attrib;
282                 }
283                 break;
284
285         case RAW_SFILEINFO_SETATTRE:
286         case RAW_SFILEINFO_STANDARD:
287                 if (!null_time(info->setattre.in.create_time)) {
288                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
289                 }
290                 if (!null_time(info->setattre.in.access_time)) {
291                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
292                 }
293                 if (!null_time(info->setattre.in.write_time)) {
294                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
295                 }
296                 break;
297
298         case RAW_SFILEINFO_EA_SET:
299                 return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd, 
300                                                info->ea_set.in.num_eas,
301                                                info->ea_set.in.eas);
302
303         case RAW_SFILEINFO_BASIC_INFO:
304         case RAW_SFILEINFO_BASIC_INFORMATION:
305                 if (!null_nttime(info->basic_info.in.create_time)) {
306                         newstats.dos.create_time = info->basic_info.in.create_time;
307                 }
308                 if (!null_nttime(info->basic_info.in.access_time)) {
309                         newstats.dos.access_time = info->basic_info.in.access_time;
310                 }
311                 if (!null_nttime(info->basic_info.in.write_time)) {
312                         newstats.dos.write_time = info->basic_info.in.write_time;
313                         newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
314                         h->sticky_write_time = True;
315                 }
316                 if (!null_nttime(info->basic_info.in.change_time)) {
317                         newstats.dos.change_time = info->basic_info.in.change_time;
318                 }
319                 if (info->basic_info.in.attrib != 0) {
320                         newstats.dos.attrib = info->basic_info.in.attrib;
321                 }
322                 break;
323
324         case RAW_SFILEINFO_DISPOSITION_INFO:
325         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
326                 return pvfs_set_delete_on_close(pvfs, req, f, 
327                                                 info->disposition_info.in.delete_on_close);
328
329         case RAW_SFILEINFO_ALLOCATION_INFO:
330         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
331                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
332                 if (newstats.dos.alloc_size < newstats.st.st_size) {
333                         newstats.st.st_size = newstats.dos.alloc_size;
334                 }
335                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
336                                                                 newstats.dos.alloc_size);
337                 break;
338
339         case RAW_SFILEINFO_END_OF_FILE_INFO:
340         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
341                 newstats.st.st_size = info->end_of_file_info.in.size;
342                 break;
343
344         case RAW_SFILEINFO_POSITION_INFORMATION:
345                 h->position = info->position_information.in.position;
346                 break;
347
348         case RAW_SFILEINFO_MODE_INFORMATION:
349                 /* this one is a puzzle */
350                 if (info->mode_information.in.mode != 0 &&
351                     info->mode_information.in.mode != 2 &&
352                     info->mode_information.in.mode != 4 &&
353                     info->mode_information.in.mode != 6) {
354                         return NT_STATUS_INVALID_PARAMETER;
355                 }
356                 h->mode = info->mode_information.in.mode;
357                 break;
358
359         case RAW_SFILEINFO_RENAME_INFORMATION:
360                 return pvfs_setfileinfo_rename(pvfs, req, h->name, 
361                                                &info->rename_information.in);
362
363         case RAW_SFILEINFO_SEC_DESC:
364                 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
365
366         default:
367                 return NT_STATUS_INVALID_LEVEL;
368         }
369
370         /* possibly change the file size */
371         if (newstats.st.st_size != h->name->st.st_size) {
372                 if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
373                         return NT_STATUS_FILE_IS_A_DIRECTORY;
374                 }
375                 if (h->name->stream_name) {
376                         status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
377                         if (!NT_STATUS_IS_OK(status)) {
378                                 return status;
379                         }
380                 } else {
381                         int ret;
382                         if (f->access_mask & 
383                             (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
384                                 ret = ftruncate(h->fd, newstats.st.st_size);
385                         } else {
386                                 ret = truncate(h->name->full_name, newstats.st.st_size);
387                         }
388                         if (ret == -1) {
389                                 return pvfs_map_errno(pvfs, errno);
390                         }
391                 }
392         }
393
394         /* possibly change the file timestamps */
395         ZERO_STRUCT(unix_times);
396         if (newstats.dos.access_time != h->name->dos.access_time) {
397                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
398         }
399         if (newstats.dos.write_time != h->name->dos.write_time) {
400                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
401         }
402         if (unix_times.actime != 0 || unix_times.modtime != 0) {
403                 if (utime(h->name->full_name, &unix_times) == -1) {
404                         return pvfs_map_errno(pvfs, errno);
405                 }
406         }
407
408         /* possibly change the attribute */
409         if (newstats.dos.attrib != h->name->dos.attrib) {
410                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
411                 if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
412                         if (fchmod(h->fd, mode) == -1) {
413                                 return pvfs_map_errno(pvfs, errno);
414                         }
415                 }
416         }
417
418         *h->name = newstats;
419
420         return pvfs_dosattrib_save(pvfs, h->name, h->fd);
421 }
422
423
424 /*
425   set info on a pathname
426 */
427 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
428                           struct ntvfs_request *req, union smb_setfileinfo *info)
429 {
430         struct pvfs_state *pvfs = ntvfs->private_data;
431         struct pvfs_filename *name;
432         struct pvfs_filename newstats;
433         NTSTATUS status;
434         struct utimbuf unix_times;
435         uint32_t access_needed;
436
437         /* resolve the cifs name to a posix name */
438         status = pvfs_resolve_name(pvfs, req, info->generic.file.path, 
439                                    PVFS_RESOLVE_STREAMS, &name);
440         if (!NT_STATUS_IS_OK(status)) {
441                 return status;
442         }
443
444         if (!name->exists) {
445                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
446         }
447
448         access_needed = pvfs_setfileinfo_access(info);
449         status = pvfs_access_check_simple(pvfs, req, name, access_needed);
450         if (!NT_STATUS_IS_OK(status)) {
451                 return status;
452         }
453
454         /* we take a copy of the current file stats, then update
455            newstats in each of the elements below. At the end we
456            compare, and make any changes needed */
457         newstats = *name;
458
459         switch (info->generic.level) {
460         case RAW_SFILEINFO_SETATTR:
461                 if (!null_time(info->setattr.in.write_time)) {
462                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
463                 }
464                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
465                         newstats.dos.attrib = info->setattr.in.attrib;
466                 }
467                 break;
468
469         case RAW_SFILEINFO_SETATTRE:
470         case RAW_SFILEINFO_STANDARD:
471                 if (!null_time(info->setattre.in.create_time)) {
472                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
473                 }
474                 if (!null_time(info->setattre.in.access_time)) {
475                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
476                 }
477                 if (!null_time(info->setattre.in.write_time)) {
478                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
479                 }
480                 break;
481
482         case RAW_SFILEINFO_EA_SET:
483                 return pvfs_setfileinfo_ea_set(pvfs, name, -1, 
484                                                info->ea_set.in.num_eas,
485                                                info->ea_set.in.eas);
486
487         case RAW_SFILEINFO_BASIC_INFO:
488         case RAW_SFILEINFO_BASIC_INFORMATION:
489                 if (!null_nttime(info->basic_info.in.create_time)) {
490                         newstats.dos.create_time = info->basic_info.in.create_time;
491                 }
492                 if (!null_nttime(info->basic_info.in.access_time)) {
493                         newstats.dos.access_time = info->basic_info.in.access_time;
494                 }
495                 if (!null_nttime(info->basic_info.in.write_time)) {
496                         newstats.dos.write_time = info->basic_info.in.write_time;
497                 }
498                 if (!null_nttime(info->basic_info.in.change_time)) {
499                         newstats.dos.change_time = info->basic_info.in.change_time;
500                 }
501                 if (info->basic_info.in.attrib != 0) {
502                         newstats.dos.attrib = info->basic_info.in.attrib;
503                 }
504                 break;
505
506         case RAW_SFILEINFO_ALLOCATION_INFO:
507         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
508                 if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
509                         /* strange. Increasing the allocation size via setpathinfo 
510                            should be silently ignored */
511                         break;
512                 }
513                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
514                 if (newstats.dos.alloc_size < newstats.st.st_size) {
515                         newstats.st.st_size = newstats.dos.alloc_size;
516                 }
517                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
518                                                                 newstats.dos.alloc_size);
519                 break;
520
521         case RAW_SFILEINFO_END_OF_FILE_INFO:
522         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
523                 newstats.st.st_size = info->end_of_file_info.in.size;
524                 break;
525
526         case RAW_SFILEINFO_MODE_INFORMATION:
527                 if (info->mode_information.in.mode != 0 &&
528                     info->mode_information.in.mode != 2 &&
529                     info->mode_information.in.mode != 4 &&
530                     info->mode_information.in.mode != 6) {
531                         return NT_STATUS_INVALID_PARAMETER;
532                 }
533                 return NT_STATUS_OK;
534
535         case RAW_SFILEINFO_RENAME_INFORMATION:
536                 return pvfs_setfileinfo_rename(pvfs, req, name, 
537                                                &info->rename_information.in);
538
539         case RAW_SFILEINFO_DISPOSITION_INFO:
540         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
541         case RAW_SFILEINFO_POSITION_INFORMATION:
542                 return NT_STATUS_OK;
543
544         default:
545                 return NT_STATUS_INVALID_LEVEL;
546         }
547
548         /* possibly change the file size */
549         if (newstats.st.st_size != name->st.st_size) {
550                 if (name->stream_name) {
551                         status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
552                         if (!NT_STATUS_IS_OK(status)) {
553                                 return status;
554                         }
555                 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
556                         return pvfs_map_errno(pvfs, errno);
557                 }
558         }
559
560         /* possibly change the file timestamps */
561         ZERO_STRUCT(unix_times);
562         if (newstats.dos.access_time != name->dos.access_time) {
563                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
564         }
565         if (newstats.dos.write_time != name->dos.write_time) {
566                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
567         }
568         if (unix_times.actime != 0 || unix_times.modtime != 0) {
569                 if (utime(name->full_name, &unix_times) == -1) {
570                         return pvfs_map_errno(pvfs, errno);
571                 }
572         }
573
574         /* possibly change the attribute */
575         newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
576         if (newstats.dos.attrib != name->dos.attrib) {
577                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
578                 if (chmod(name->full_name, mode) == -1) {
579                         return pvfs_map_errno(pvfs, errno);
580                 }
581         }
582
583         *name = newstats;
584
585         return pvfs_dosattrib_save(pvfs, name, -1);
586 }
587