Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[kai/samba.git] / source4 / ntvfs / posix / pvfs_qfileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - read
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "vfs_posix.h"
24 #include "librpc/gen_ndr/xattr.h"
25
26
27 /*
28   determine what access bits are needed for a call
29 */
30 static uint32_t pvfs_fileinfo_access(union smb_fileinfo *info)
31 {
32         uint32_t needed;
33
34         switch (info->generic.level) {
35         case RAW_FILEINFO_EA_LIST:
36         case RAW_FILEINFO_ALL_EAS:
37                 needed = SEC_FILE_READ_EA;
38                 break;
39
40         case RAW_FILEINFO_IS_NAME_VALID:
41                 needed = 0;
42                 break;
43
44         case RAW_FILEINFO_SEC_DESC:
45                 needed = 0;
46                 if (info->query_secdesc.in.secinfo_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
47                         needed |= SEC_STD_READ_CONTROL;
48                 }
49                 if (info->query_secdesc.in.secinfo_flags & SECINFO_DACL) {
50                         needed |= SEC_STD_READ_CONTROL;
51                 }
52                 if (info->query_secdesc.in.secinfo_flags & SECINFO_SACL) {
53                         needed |= SEC_FLAG_SYSTEM_SECURITY;
54                 }
55                 break;
56
57         default:
58                 needed = SEC_FILE_READ_ATTRIBUTE;
59                 break;
60         }
61
62         return needed;
63 }
64
65 /*
66   reply to a RAW_FILEINFO_EA_LIST call
67 */
68 NTSTATUS pvfs_query_ea_list(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
69                             struct pvfs_filename *name, int fd, 
70                             uint_t num_names,
71                             struct ea_name *names,
72                             struct smb_ea_list *eas)
73 {
74         NTSTATUS status;
75         int i;
76         struct xattr_DosEAs *ealist = talloc(mem_ctx, struct xattr_DosEAs);
77
78         ZERO_STRUCTP(eas);
79         status = pvfs_doseas_load(pvfs, name, fd, ealist);
80         if (!NT_STATUS_IS_OK(status)) {
81                 return status;
82         }
83         eas->eas = talloc_array(mem_ctx, struct ea_struct, num_names);
84         if (eas->eas == NULL) {
85                 return NT_STATUS_NO_MEMORY;
86         }
87         eas->num_eas = num_names;
88         for (i=0;i<num_names;i++) {
89                 int j;
90                 eas->eas[i].flags = 0;
91                 eas->eas[i].name.s = names[i].name.s;
92                 eas->eas[i].value = data_blob(NULL, 0);
93                 for (j=0;j<ealist->num_eas;j++) {
94                         if (strcasecmp_m(eas->eas[i].name.s, 
95                                        ealist->eas[j].name) == 0) {
96                                 eas->eas[i].value = ealist->eas[j].value;
97                                 break;
98                         }
99                 }
100         }
101         return NT_STATUS_OK;
102 }
103
104 /*
105   reply to a RAW_FILEINFO_ALL_EAS call
106 */
107 static NTSTATUS pvfs_query_all_eas(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
108                                    struct pvfs_filename *name, int fd, 
109                                    struct smb_ea_list *eas)
110 {
111         NTSTATUS status;
112         int i;
113         struct xattr_DosEAs *ealist = talloc(mem_ctx, struct xattr_DosEAs);
114
115         ZERO_STRUCTP(eas);
116         status = pvfs_doseas_load(pvfs, name, fd, ealist);
117         if (!NT_STATUS_IS_OK(status)) {
118                 return status;
119         }
120         eas->eas = talloc_array(mem_ctx, struct ea_struct, ealist->num_eas);
121         if (eas->eas == NULL) {
122                 return NT_STATUS_NO_MEMORY;
123         }
124         eas->num_eas = 0;
125         for (i=0;i<ealist->num_eas;i++) {
126                 eas->eas[eas->num_eas].flags = 0;
127                 eas->eas[eas->num_eas].name.s = ealist->eas[i].name;
128                 eas->eas[eas->num_eas].value = ealist->eas[i].value;
129                 eas->num_eas++;
130         }
131         return NT_STATUS_OK;
132 }
133
134 /*
135   approximately map a struct pvfs_filename to a generic fileinfo struct
136 */
137 static NTSTATUS pvfs_map_fileinfo(struct pvfs_state *pvfs, 
138                                   struct ntvfs_request *req,
139                                   struct pvfs_filename *name, union smb_fileinfo *info, 
140                                   int fd)
141 {
142         switch (info->generic.level) {
143         case RAW_FILEINFO_GENERIC:
144                 return NT_STATUS_INVALID_LEVEL;
145
146         case RAW_FILEINFO_GETATTR:
147                 info->getattr.out.attrib     = name->dos.attrib;
148                 info->getattr.out.size       = name->st.st_size;
149                 info->getattr.out.write_time = nt_time_to_unix(name->dos.write_time);
150                 return NT_STATUS_OK;
151
152         case RAW_FILEINFO_GETATTRE:
153         case RAW_FILEINFO_STANDARD:
154                 info->standard.out.create_time = nt_time_to_unix(name->dos.create_time);
155                 info->standard.out.access_time = nt_time_to_unix(name->dos.access_time);
156                 info->standard.out.write_time  = nt_time_to_unix(name->dos.write_time);
157                 info->standard.out.size        = name->st.st_size;
158                 info->standard.out.alloc_size  = name->dos.alloc_size;
159                 info->standard.out.attrib      = name->dos.attrib;
160                 return NT_STATUS_OK;
161
162         case RAW_FILEINFO_EA_SIZE:
163                 info->ea_size.out.create_time = nt_time_to_unix(name->dos.create_time);
164                 info->ea_size.out.access_time = nt_time_to_unix(name->dos.access_time);
165                 info->ea_size.out.write_time  = nt_time_to_unix(name->dos.write_time);
166                 info->ea_size.out.size        = name->st.st_size;
167                 info->ea_size.out.alloc_size  = name->dos.alloc_size;
168                 info->ea_size.out.attrib      = name->dos.attrib;
169                 info->ea_size.out.ea_size     = name->dos.ea_size;
170                 return NT_STATUS_OK;
171
172         case RAW_FILEINFO_EA_LIST:
173                 return pvfs_query_ea_list(pvfs, req, name, fd, 
174                                           info->ea_list.in.num_names,
175                                           info->ea_list.in.ea_names, 
176                                           &info->ea_list.out);
177
178         case RAW_FILEINFO_ALL_EAS:
179                 return pvfs_query_all_eas(pvfs, req, name, fd, &info->all_eas.out);
180
181         case RAW_FILEINFO_SMB2_ALL_EAS: {
182                 NTSTATUS status = pvfs_query_all_eas(pvfs, req, name, fd, &info->all_eas.out);
183                 if (NT_STATUS_IS_OK(status) &&
184                     info->all_eas.out.num_eas == 0) {
185                         return NT_STATUS_NO_EAS_ON_FILE;
186                 }
187                 return status;
188         }
189
190         case RAW_FILEINFO_IS_NAME_VALID:
191                 return NT_STATUS_OK;
192
193         case RAW_FILEINFO_BASIC_INFO:
194         case RAW_FILEINFO_BASIC_INFORMATION:
195                 info->basic_info.out.create_time = name->dos.create_time;
196                 info->basic_info.out.access_time = name->dos.access_time;
197                 info->basic_info.out.write_time  = name->dos.write_time;
198                 info->basic_info.out.change_time = name->dos.change_time;
199                 info->basic_info.out.attrib      = name->dos.attrib;
200                 return NT_STATUS_OK;
201
202         case RAW_FILEINFO_STANDARD_INFO:
203         case RAW_FILEINFO_STANDARD_INFORMATION:
204                 info->standard_info.out.alloc_size     = name->dos.alloc_size;
205                 info->standard_info.out.size           = name->st.st_size;
206                 info->standard_info.out.nlink          = name->dos.nlink;
207                 info->standard_info.out.delete_pending = 0; /* only for qfileinfo */
208                 info->standard_info.out.directory   = 
209                         (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)? 1 : 0;
210                 return NT_STATUS_OK;
211
212         case RAW_FILEINFO_EA_INFO:
213         case RAW_FILEINFO_EA_INFORMATION:
214                 info->ea_info.out.ea_size = name->dos.ea_size;
215                 return NT_STATUS_OK;
216
217         case RAW_FILEINFO_NAME_INFO:
218         case RAW_FILEINFO_NAME_INFORMATION:
219                 info->name_info.out.fname.s = name->original_name;
220                 return NT_STATUS_OK;
221
222         case RAW_FILEINFO_ALL_INFO:
223         case RAW_FILEINFO_ALL_INFORMATION:
224                 info->all_info.out.create_time    = name->dos.create_time;
225                 info->all_info.out.access_time    = name->dos.access_time;
226                 info->all_info.out.write_time     = name->dos.write_time;
227                 info->all_info.out.change_time    = name->dos.change_time;
228                 info->all_info.out.attrib         = name->dos.attrib;
229                 info->all_info.out.alloc_size     = name->dos.alloc_size;
230                 info->all_info.out.size           = name->st.st_size;
231                 info->all_info.out.nlink          = name->dos.nlink;
232                 info->all_info.out.delete_pending = 0; /* only set by qfileinfo */
233                 info->all_info.out.directory      = 
234                         (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)? 1 : 0;
235                 info->all_info.out.ea_size        = name->dos.ea_size;
236                 info->all_info.out.fname.s        = name->original_name;
237                 return NT_STATUS_OK;
238
239         case RAW_FILEINFO_ALT_NAME_INFO:
240         case RAW_FILEINFO_ALT_NAME_INFORMATION:
241                 info->name_info.out.fname.s = pvfs_short_name(pvfs, name, name);
242                 return NT_STATUS_OK;
243
244         case RAW_FILEINFO_STREAM_INFO:
245         case RAW_FILEINFO_STREAM_INFORMATION:
246                 return pvfs_stream_information(pvfs, req, name, fd, &info->stream_info.out);
247
248         case RAW_FILEINFO_COMPRESSION_INFO:
249         case RAW_FILEINFO_COMPRESSION_INFORMATION:
250                 info->compression_info.out.compressed_size = name->st.st_size;
251                 info->compression_info.out.format          = 0;
252                 info->compression_info.out.unit_shift      = 0;
253                 info->compression_info.out.chunk_shift     = 0;
254                 info->compression_info.out.cluster_shift   = 0;
255                 return NT_STATUS_OK;
256
257         case RAW_FILEINFO_INTERNAL_INFORMATION:
258                 info->internal_information.out.file_id = name->dos.file_id;
259                 return NT_STATUS_OK;
260
261         case RAW_FILEINFO_ACCESS_INFORMATION:
262                 info->access_information.out.access_flags = 0; /* only set by qfileinfo */
263                 return NT_STATUS_OK;
264
265         case RAW_FILEINFO_POSITION_INFORMATION:
266                 info->position_information.out.position = 0; /* only set by qfileinfo */
267                 return NT_STATUS_OK;
268
269         case RAW_FILEINFO_MODE_INFORMATION:
270                 info->mode_information.out.mode = 0; /* only set by qfileinfo */
271                 return NT_STATUS_OK;
272
273         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
274                 info->alignment_information.out.alignment_requirement = 0;
275                 return NT_STATUS_OK;
276
277         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
278                 info->network_open_information.out.create_time = name->dos.create_time;
279                 info->network_open_information.out.access_time = name->dos.access_time;
280                 info->network_open_information.out.write_time  = name->dos.write_time;
281                 info->network_open_information.out.change_time = name->dos.change_time;
282                 info->network_open_information.out.alloc_size  = name->dos.alloc_size;
283                 info->network_open_information.out.size        = name->st.st_size;
284                 info->network_open_information.out.attrib      = name->dos.attrib;
285                 return NT_STATUS_OK;
286
287         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
288                 info->attribute_tag_information.out.attrib      = name->dos.attrib;
289                 info->attribute_tag_information.out.reparse_tag = 0;
290                 return NT_STATUS_OK;
291
292         case RAW_FILEINFO_SEC_DESC:
293                 return pvfs_acl_query(pvfs, req, name, fd, info);
294
295         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
296                 info->all_info2.out.create_time    = name->dos.create_time;
297                 info->all_info2.out.access_time    = name->dos.access_time;
298                 info->all_info2.out.write_time     = name->dos.write_time;
299                 info->all_info2.out.change_time    = name->dos.change_time;
300                 info->all_info2.out.attrib         = name->dos.attrib;
301                 info->all_info2.out.unknown1       = 0;
302                 info->all_info2.out.alloc_size     = name->dos.alloc_size;
303                 info->all_info2.out.size           = name->st.st_size;
304                 info->all_info2.out.nlink          = name->dos.nlink;
305                 info->all_info2.out.delete_pending = 0; /* only set by qfileinfo */
306                 info->all_info2.out.directory      = 
307                         (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)? 1 : 0;
308                 info->all_info2.out.file_id        = name->dos.file_id;
309                 info->all_info2.out.ea_size        = name->dos.ea_size;
310                 info->all_info2.out.access_mask    = 0; /* only set by qfileinfo */
311                 info->all_info2.out.position       = 0; /* only set by qfileinfo */
312                 info->all_info2.out.mode           = 0; /* only set by qfileinfo */
313                 /* windows wants the full path on disk for this
314                    result, but I really don't want to expose that on
315                    the wire, so I'll give the path with a share
316                    prefix, which is a good approximation */
317                 info->all_info2.out.fname.s = talloc_asprintf(req, "\\%s\\%s",
318                                                               pvfs->share_name, 
319                                                               name->original_name);
320                 NT_STATUS_HAVE_NO_MEMORY(info->all_info2.out.fname.s);
321                 return NT_STATUS_OK;
322         }
323
324         return NT_STATUS_INVALID_LEVEL;
325 }
326
327 /*
328   return info on a pathname
329 */
330 NTSTATUS pvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
331                         struct ntvfs_request *req, union smb_fileinfo *info)
332 {
333         struct pvfs_state *pvfs = ntvfs->private_data;
334         struct pvfs_filename *name;
335         NTSTATUS status;
336
337         /* resolve the cifs name to a posix name */
338         status = pvfs_resolve_name(pvfs, req, info->generic.in.file.path, PVFS_RESOLVE_STREAMS, &name);
339         if (!NT_STATUS_IS_OK(status)) {
340                 return status;
341         }
342
343         if (!name->stream_exists) {
344                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
345         }
346
347         status = pvfs_can_stat(pvfs, req, name);
348         if (!NT_STATUS_IS_OK(status)) {
349                 return status;
350         }
351
352         status = pvfs_access_check_simple(pvfs, req, name, 
353                                           pvfs_fileinfo_access(info));
354         if (!NT_STATUS_IS_OK(status)) {
355                 return status;
356         }
357
358         status = pvfs_map_fileinfo(pvfs, req, name, info, -1);
359
360         return status;
361 }
362
363 /*
364   query info on a open file
365 */
366 NTSTATUS pvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
367                         struct ntvfs_request *req, union smb_fileinfo *info)
368 {
369         struct pvfs_state *pvfs = ntvfs->private_data;
370         struct pvfs_file *f;
371         struct pvfs_file_handle *h;
372         NTSTATUS status;
373         uint32_t access_needed;
374
375         f = pvfs_find_fd(pvfs, req, info->generic.in.file.ntvfs);
376         if (!f) {
377                 return NT_STATUS_INVALID_HANDLE;
378         }
379         h = f->handle;
380
381         access_needed = pvfs_fileinfo_access(info);
382         if ((f->access_mask & access_needed) != access_needed) {
383                 return NT_STATUS_ACCESS_DENIED;
384         }
385
386         /* update the file information */
387         status = pvfs_resolve_name_handle(pvfs, h);
388         if (!NT_STATUS_IS_OK(status)) {
389                 return status;
390         }
391         
392         status = pvfs_map_fileinfo(pvfs, req, h->name, info, h->fd);
393
394         /* a qfileinfo can fill in a bit more info than a qpathinfo -
395            now modify the levels that need to be fixed up */
396         switch (info->generic.level) {
397         case RAW_FILEINFO_STANDARD_INFO:
398         case RAW_FILEINFO_STANDARD_INFORMATION:
399                 if (pvfs_delete_on_close_set(pvfs, h)) {
400                         info->standard_info.out.delete_pending = 1;
401                         info->standard_info.out.nlink--;
402                 }
403                 break;
404
405         case RAW_FILEINFO_ALL_INFO:
406         case RAW_FILEINFO_ALL_INFORMATION:
407                 if (pvfs_delete_on_close_set(pvfs, h)) {
408                         info->all_info.out.delete_pending = 1;
409                         info->all_info.out.nlink--;
410                 }
411                 break;
412
413         case RAW_FILEINFO_POSITION_INFORMATION:
414                 info->position_information.out.position = h->position;
415                 break;
416
417         case RAW_FILEINFO_ACCESS_INFORMATION:
418                 info->access_information.out.access_flags = f->access_mask;
419                 break;
420
421         case RAW_FILEINFO_MODE_INFORMATION:
422                 info->mode_information.out.mode = h->mode;
423                 break;
424
425         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
426                 if (pvfs_delete_on_close_set(pvfs, h)) {
427                         info->all_info2.out.delete_pending = 1;
428                         info->all_info2.out.nlink--;
429                 }
430                 info->all_info2.out.position    = h->position;
431                 info->all_info2.out.access_mask = f->access_mask;
432                 info->all_info2.out.mode        = h->mode;
433                 break;
434
435         default:
436                 break;
437         }
438         
439         return status;
440 }