r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[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/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                                         union smb_setfileinfo *info)
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(info->rename_information.in.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 (info->rename_information.in.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,
112                                    info->rename_information.in.new_name);
113         if (new_name == NULL) {
114                 return NT_STATUS_NO_MEMORY;
115         }
116
117         /* resolve the new name */
118         status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return status;
121         }
122
123         /* if the destination exists, then check the rename is allowed */
124         if (name2->exists) {
125                 struct odb_lock *lck;
126
127                 if (strcmp(name2->full_name, name->full_name) == 0) {
128                         /* rename to same name is null-op */
129                         return NT_STATUS_OK;
130                 }
131
132                 if (!info->rename_information.in.overwrite) {
133                         return NT_STATUS_OBJECT_NAME_COLLISION;
134                 }
135
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;
139                 }
140                 if (!NT_STATUS_IS_OK(status)) {
141                         return status;
142                 }
143         }
144
145         status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
146         if (!NT_STATUS_IS_OK(status)) {
147                 return status;
148         }
149
150         if (rename(name->full_name, name2->full_name) == -1) {
151                 return pvfs_map_errno(pvfs, errno);
152         }
153
154         name->full_name = talloc_steal(name, name2->full_name);
155         name->original_name = talloc_steal(name, name2->original_name);
156
157         return NT_STATUS_OK;
158 }
159
160 /*
161   add a single DOS EA
162 */
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)
167 {
168         struct xattr_DosEAs *ealist;
169         int i, j;
170         NTSTATUS status;
171
172         if (num_eas == 0) {
173                 return NT_STATUS_OK;
174         }
175
176         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
177                 return NT_STATUS_NOT_SUPPORTED;
178         }
179
180         ealist = talloc(name, struct xattr_DosEAs);
181
182         /* load the current list */
183         status = pvfs_doseas_load(pvfs, name, fd, ealist);
184         if (!NT_STATUS_IS_OK(status)) {
185                 return status;
186         }
187
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;
194                                 break;
195                         }
196                 }
197
198                 if (i==ealist->num_eas) {
199                         /* add it */
200                         ealist->eas = talloc_realloc(ealist, ealist->eas, 
201                                                        struct xattr_EA, 
202                                                        ealist->num_eas+1);
203                         if (ealist->eas == NULL) {
204                                 return NT_STATUS_NO_MEMORY;
205                         }
206                         ealist->eas[i].name = ea->name.s;
207                         ealist->eas[i].value = ea->value;
208                         ealist->num_eas++;
209                 }
210         }
211         
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],
216                                 &ealist->eas[i+1],
217                                 (ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
218                         ealist->num_eas--;
219                         i--;
220                 }
221         }
222
223         status = pvfs_doseas_save(pvfs, name, fd, ealist);
224         if (!NT_STATUS_IS_OK(status)) {
225                 return status;
226         }
227
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;
232         }
233
234         /* update the ea_size attrib */
235         return pvfs_dosattrib_save(pvfs, name, fd);
236 }
237
238 /*
239   set info on a open file
240 */
241 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
242                           struct ntvfs_request *req, 
243                           union smb_setfileinfo *info)
244 {
245         struct pvfs_state *pvfs = ntvfs->private_data;
246         struct utimbuf unix_times;
247         struct pvfs_file *f;
248         struct pvfs_file_handle *h;
249         struct pvfs_filename newstats;
250         NTSTATUS status;
251         uint32_t access_needed;
252
253         f = pvfs_find_fd(pvfs, req, info->generic.in.file.fnum);
254         if (!f) {
255                 return NT_STATUS_INVALID_HANDLE;
256         }
257
258         h = f->handle;
259
260         access_needed = pvfs_setfileinfo_access(info);
261         if ((f->access_mask & access_needed) != access_needed) {
262                 return NT_STATUS_ACCESS_DENIED;
263         }
264
265         /* update the file information */
266         status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
267         if (!NT_STATUS_IS_OK(status)) {
268                 return status;
269         }
270
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 */
274         newstats = *h->name;
275
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);
280                 }
281                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
282                         newstats.dos.attrib = info->setattr.in.attrib;
283                 }
284                 break;
285
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);
290                 }
291                 if (!null_time(info->setattre.in.access_time)) {
292                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
293                 }
294                 if (!null_time(info->setattre.in.write_time)) {
295                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
296                 }
297                 break;
298
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);
303
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;
308                 }
309                 if (!null_nttime(info->basic_info.in.access_time)) {
310                         newstats.dos.access_time = info->basic_info.in.access_time;
311                 }
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;
316                 }
317                 if (!null_nttime(info->basic_info.in.change_time)) {
318                         newstats.dos.change_time = info->basic_info.in.change_time;
319                 }
320                 if (info->basic_info.in.attrib != 0) {
321                         newstats.dos.attrib = info->basic_info.in.attrib;
322                 }
323                 break;
324
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);
329
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;
335                 }
336                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
337                                                                 newstats.dos.alloc_size);
338                 break;
339
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;
343                 break;
344
345         case RAW_SFILEINFO_POSITION_INFORMATION:
346                 h->position = info->position_information.in.position;
347                 break;
348
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;
356                 }
357                 h->mode = info->mode_information.in.mode;
358                 break;
359
360         case RAW_SFILEINFO_RENAME_INFORMATION:
361                 return pvfs_setfileinfo_rename(pvfs, req, h->name, 
362                                                info);
363
364         case RAW_SFILEINFO_SEC_DESC:
365                 return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);
366
367         default:
368                 return NT_STATUS_INVALID_LEVEL;
369         }
370
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;
375                 }
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)) {
379                                 return status;
380                         }
381                 } else {
382                         int ret;
383                         if (f->access_mask & 
384                             (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
385                                 ret = ftruncate(h->fd, newstats.st.st_size);
386                         } else {
387                                 ret = truncate(h->name->full_name, newstats.st.st_size);
388                         }
389                         if (ret == -1) {
390                                 return pvfs_map_errno(pvfs, errno);
391                         }
392                 }
393         }
394
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);
399         }
400         if (newstats.dos.write_time != h->name->dos.write_time) {
401                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
402         }
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);
406                 }
407         }
408
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);
415                         }
416                 }
417         }
418
419         *h->name = newstats;
420
421         return pvfs_dosattrib_save(pvfs, h->name, h->fd);
422 }
423
424
425 /*
426   set info on a pathname
427 */
428 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
429                           struct ntvfs_request *req, union smb_setfileinfo *info)
430 {
431         struct pvfs_state *pvfs = ntvfs->private_data;
432         struct pvfs_filename *name;
433         struct pvfs_filename newstats;
434         NTSTATUS status;
435         struct utimbuf unix_times;
436         uint32_t access_needed;
437
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)) {
442                 return status;
443         }
444
445         if (!name->exists) {
446                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
447         }
448
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)) {
452                 return status;
453         }
454
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 */
458         newstats = *name;
459
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);
464                 }
465                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
466                         newstats.dos.attrib = info->setattr.in.attrib;
467                 }
468                 break;
469
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);
474                 }
475                 if (!null_time(info->setattre.in.access_time)) {
476                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
477                 }
478                 if (!null_time(info->setattre.in.write_time)) {
479                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
480                 }
481                 break;
482
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);
487
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;
492                 }
493                 if (!null_nttime(info->basic_info.in.access_time)) {
494                         newstats.dos.access_time = info->basic_info.in.access_time;
495                 }
496                 if (!null_nttime(info->basic_info.in.write_time)) {
497                         newstats.dos.write_time = info->basic_info.in.write_time;
498                 }
499                 if (!null_nttime(info->basic_info.in.change_time)) {
500                         newstats.dos.change_time = info->basic_info.in.change_time;
501                 }
502                 if (info->basic_info.in.attrib != 0) {
503                         newstats.dos.attrib = info->basic_info.in.attrib;
504                 }
505                 break;
506
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 */
512                         break;
513                 }
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;
517                 }
518                 newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
519                                                                 newstats.dos.alloc_size);
520                 break;
521
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;
525                 break;
526
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;
533                 }
534                 return NT_STATUS_OK;
535
536         case RAW_SFILEINFO_RENAME_INFORMATION:
537                 return pvfs_setfileinfo_rename(pvfs, req, name, 
538                                                info);
539
540         case RAW_SFILEINFO_DISPOSITION_INFO:
541         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
542         case RAW_SFILEINFO_POSITION_INFORMATION:
543                 return NT_STATUS_OK;
544
545         default:
546                 return NT_STATUS_INVALID_LEVEL;
547         }
548
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)) {
554                                 return status;
555                         }
556                 } else if (truncate(name->full_name, newstats.st.st_size) == -1) {
557                         return pvfs_map_errno(pvfs, errno);
558                 }
559         }
560
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);
565         }
566         if (newstats.dos.write_time != name->dos.write_time) {
567                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
568         }
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);
572                 }
573         }
574
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);
581                 }
582         }
583
584         *name = newstats;
585
586         return pvfs_dosattrib_save(pvfs, name, -1);
587 }
588