r4408: added the remaining access check hooks into pvfs. All calls should now have...
[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 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 "librpc/gen_ndr/ndr_xattr.h"
26
27
28 /*
29   determine what access bits are needed for a call
30 */
31 static uint32_t pvfs_fileinfo_access(enum smb_fileinfo_level level)
32 {
33         uint32_t needed;
34
35         switch (level) {
36         case RAW_FILEINFO_EA_LIST:
37         case RAW_FILEINFO_ALL_EAS:
38                 needed = SEC_FILE_READ_EA;
39                 break;
40
41         case RAW_FILEINFO_IS_NAME_VALID:
42                 needed = 0;
43                 break;
44
45         default:
46                 needed = SEC_FILE_READ_ATTRIBUTE;
47                 break;
48         }
49         return needed;  
50 }
51
52 /*
53   reply to a RAW_FILEINFO_EA_LIST call
54 */
55 NTSTATUS pvfs_query_ea_list(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
56                             struct pvfs_filename *name, int fd, 
57                             uint_t num_names,
58                             struct ea_name *names,
59                             struct smb_ea_list *eas)
60 {
61         NTSTATUS status;
62         int i;
63         struct xattr_DosEAs *ealist = talloc_p(mem_ctx, struct xattr_DosEAs);
64
65         ZERO_STRUCTP(eas);
66         status = pvfs_doseas_load(pvfs, name, fd, ealist);
67         if (!NT_STATUS_IS_OK(status)) {
68                 return status;
69         }
70         eas->eas = talloc_array_p(mem_ctx, struct ea_struct, num_names);
71         if (eas->eas == NULL) {
72                 return NT_STATUS_NO_MEMORY;
73         }
74         eas->num_eas = num_names;
75         for (i=0;i<num_names;i++) {
76                 int j;
77                 eas->eas[i].flags = 0;
78                 eas->eas[i].name.s = names[i].name.s;
79                 eas->eas[i].value = data_blob(NULL, 0);
80                 for (j=0;j<ealist->num_eas;j++) {
81                         if (StrCaseCmp(eas->eas[i].name.s, 
82                                        ealist->eas[j].name) == 0) {
83                                 eas->eas[i].value = ealist->eas[j].value;
84                                 break;
85                         }
86                 }
87         }
88         return NT_STATUS_OK;
89 }
90
91 /*
92   reply to a RAW_FILEINFO_ALL_EAS call
93 */
94 static NTSTATUS pvfs_query_all_eas(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
95                                    struct pvfs_filename *name, int fd, 
96                                    struct smb_ea_list *eas)
97 {
98         NTSTATUS status;
99         int i;
100         struct xattr_DosEAs *ealist = talloc_p(mem_ctx, struct xattr_DosEAs);
101
102         ZERO_STRUCTP(eas);
103         status = pvfs_doseas_load(pvfs, name, fd, ealist);
104         if (!NT_STATUS_IS_OK(status)) {
105                 return status;
106         }
107         eas->eas = talloc_array_p(mem_ctx, struct ea_struct, ealist->num_eas);
108         if (eas->eas == NULL) {
109                 return NT_STATUS_NO_MEMORY;
110         }
111         eas->num_eas = 0;
112         for (i=0;i<ealist->num_eas;i++) {
113                 eas->eas[eas->num_eas].flags = 0;
114                 eas->eas[eas->num_eas].name.s = ealist->eas[i].name;
115                 eas->eas[eas->num_eas].value = ealist->eas[i].value;
116                 eas->num_eas++;
117         }
118         return NT_STATUS_OK;
119 }
120
121 /*
122   approximately map a struct pvfs_filename to a generic fileinfo struct
123 */
124 static NTSTATUS pvfs_map_fileinfo(struct pvfs_state *pvfs, 
125                                   struct smbsrv_request *req,
126                                   struct pvfs_filename *name, union smb_fileinfo *info, 
127                                   int fd)
128 {
129         switch (info->generic.level) {
130         case RAW_FILEINFO_GENERIC:
131                 return NT_STATUS_INVALID_LEVEL;
132
133         case RAW_FILEINFO_GETATTR:
134                 info->getattr.out.attrib     = name->dos.attrib;
135                 info->getattr.out.size       = name->st.st_size;
136                 info->getattr.out.write_time = nt_time_to_unix(name->dos.write_time);
137                 return NT_STATUS_OK;
138
139         case RAW_FILEINFO_GETATTRE:
140         case RAW_FILEINFO_STANDARD:
141                 info->standard.out.create_time = nt_time_to_unix(name->dos.create_time);
142                 info->standard.out.access_time = nt_time_to_unix(name->dos.access_time);
143                 info->standard.out.write_time  = nt_time_to_unix(name->dos.write_time);
144                 info->standard.out.size        = name->st.st_size;
145                 info->standard.out.alloc_size  = name->dos.alloc_size;
146                 info->standard.out.attrib      = name->dos.attrib;
147                 return NT_STATUS_OK;
148
149         case RAW_FILEINFO_EA_SIZE:
150                 info->ea_size.out.create_time = nt_time_to_unix(name->dos.create_time);
151                 info->ea_size.out.access_time = nt_time_to_unix(name->dos.access_time);
152                 info->ea_size.out.write_time  = nt_time_to_unix(name->dos.write_time);
153                 info->ea_size.out.size        = name->st.st_size;
154                 info->ea_size.out.alloc_size  = name->dos.alloc_size;
155                 info->ea_size.out.attrib      = name->dos.attrib;
156                 info->ea_size.out.ea_size     = name->dos.ea_size;
157                 return NT_STATUS_OK;
158
159         case RAW_FILEINFO_EA_LIST:
160                 return pvfs_query_ea_list(pvfs, req, name, fd, 
161                                           info->ea_list.in.num_names,
162                                           info->ea_list.in.ea_names, 
163                                           &info->ea_list.out);
164
165         case RAW_FILEINFO_ALL_EAS:
166                 return pvfs_query_all_eas(pvfs, req, name, fd, &info->all_eas.out);
167
168         case RAW_FILEINFO_IS_NAME_VALID:
169                 return NT_STATUS_OK;
170
171         case RAW_FILEINFO_BASIC_INFO:
172         case RAW_FILEINFO_BASIC_INFORMATION:
173                 info->basic_info.out.create_time = name->dos.create_time;
174                 info->basic_info.out.access_time = name->dos.access_time;
175                 info->basic_info.out.write_time  = name->dos.write_time;
176                 info->basic_info.out.change_time = name->dos.change_time;
177                 info->basic_info.out.attrib      = name->dos.attrib;
178                 return NT_STATUS_OK;
179
180         case RAW_FILEINFO_STANDARD_INFO:
181         case RAW_FILEINFO_STANDARD_INFORMATION:
182                 info->standard_info.out.alloc_size     = name->dos.alloc_size;
183                 info->standard_info.out.size           = name->st.st_size;
184                 info->standard_info.out.nlink          = name->st.st_nlink;
185                 info->standard_info.out.delete_pending = 0;
186                 info->standard_info.out.directory   = 
187                         (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)? 1 : 0;
188                 return NT_STATUS_OK;
189
190         case RAW_FILEINFO_EA_INFO:
191         case RAW_FILEINFO_EA_INFORMATION:
192                 info->ea_info.out.ea_size = name->dos.ea_size;
193                 return NT_STATUS_OK;
194
195         case RAW_FILEINFO_NAME_INFO:
196         case RAW_FILEINFO_NAME_INFORMATION:
197                 info->name_info.out.fname.s = name->original_name;
198                 return NT_STATUS_OK;
199
200         case RAW_FILEINFO_ALL_INFO:
201         case RAW_FILEINFO_ALL_INFORMATION:
202                 info->all_info.out.create_time    = name->dos.create_time;
203                 info->all_info.out.access_time    = name->dos.access_time;
204                 info->all_info.out.write_time     = name->dos.write_time;
205                 info->all_info.out.change_time    = name->dos.change_time;
206                 info->all_info.out.attrib         = name->dos.attrib;
207                 info->all_info.out.alloc_size     = name->dos.alloc_size;
208                 info->all_info.out.size           = name->st.st_size;
209                 info->all_info.out.nlink          = name->st.st_nlink;
210                 info->all_info.out.delete_pending = 0;
211                 info->all_info.out.directory      = 
212                         (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)? 1 : 0;
213                 info->all_info.out.ea_size        = name->dos.ea_size;
214                 info->all_info.out.fname.s        = name->original_name;
215                 return NT_STATUS_OK;
216
217         case RAW_FILEINFO_ALT_NAME_INFO:
218         case RAW_FILEINFO_ALT_NAME_INFORMATION:
219                 info->name_info.out.fname.s = pvfs_short_name(pvfs, name, name);
220                 return NT_STATUS_OK;
221
222         case RAW_FILEINFO_STREAM_INFO:
223         case RAW_FILEINFO_STREAM_INFORMATION:
224                 return pvfs_stream_information(pvfs, req, name, fd, &info->stream_info.out);
225
226         case RAW_FILEINFO_COMPRESSION_INFO:
227         case RAW_FILEINFO_COMPRESSION_INFORMATION:
228                 info->compression_info.out.compressed_size = name->st.st_size;
229                 info->compression_info.out.format          = 0;
230                 info->compression_info.out.unit_shift      = 0;
231                 info->compression_info.out.chunk_shift     = 0;
232                 info->compression_info.out.cluster_shift   = 0;
233                 return NT_STATUS_OK;
234
235         case RAW_FILEINFO_INTERNAL_INFORMATION:
236                 info->internal_information.out.file_id = name->dos.file_id;
237                 return NT_STATUS_OK;
238
239         case RAW_FILEINFO_ACCESS_INFORMATION:
240                 info->access_information.out.access_flags = 0;
241                 return NT_STATUS_OK;
242
243         case RAW_FILEINFO_POSITION_INFORMATION:
244                 info->position_information.out.position = 0;
245                 return NT_STATUS_OK;
246
247         case RAW_FILEINFO_MODE_INFORMATION:
248                 info->mode_information.out.mode = 0;
249                 return NT_STATUS_OK;
250
251         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
252                 info->alignment_information.out.alignment_requirement = 0;
253                 return NT_STATUS_OK;
254
255         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
256                 info->network_open_information.out.create_time = name->dos.create_time;
257                 info->network_open_information.out.access_time = name->dos.access_time;
258                 info->network_open_information.out.write_time  = name->dos.write_time;
259                 info->network_open_information.out.change_time = name->dos.change_time;
260                 info->network_open_information.out.alloc_size  = name->dos.alloc_size;
261                 info->network_open_information.out.size        = name->st.st_size;
262                 info->network_open_information.out.attrib      = name->dos.attrib;
263                 return NT_STATUS_OK;
264
265         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
266                 info->attribute_tag_information.out.attrib      = name->dos.attrib;
267                 info->attribute_tag_information.out.reparse_tag = 0;
268                 return NT_STATUS_OK;
269
270         case RAW_FILEINFO_SEC_DESC:
271                 return pvfs_acl_query(pvfs, req, name, fd, info);
272         }
273
274         return NT_STATUS_INVALID_LEVEL;
275 }
276
277 /*
278   return info on a pathname
279 */
280 NTSTATUS pvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
281                         struct smbsrv_request *req, union smb_fileinfo *info)
282 {
283         struct pvfs_state *pvfs = ntvfs->private_data;
284         struct pvfs_filename *name;
285         NTSTATUS status;
286
287         /* resolve the cifs name to a posix name */
288         status = pvfs_resolve_name(pvfs, req, info->generic.in.fname, PVFS_RESOLVE_STREAMS, &name);
289         if (!NT_STATUS_IS_OK(status)) {
290                 return status;
291         }
292
293         if (!name->stream_exists) {
294                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
295         }
296
297         status = pvfs_access_check_simple(pvfs, req, name, 
298                                           pvfs_fileinfo_access(info->generic.level));
299         if (!NT_STATUS_IS_OK(status)) {
300                 return status;
301         }
302
303         status = pvfs_map_fileinfo(pvfs, req, name, info, -1);
304
305         return status;
306 }
307
308 /*
309   query info on a open file
310 */
311 NTSTATUS pvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
312                         struct smbsrv_request *req, union smb_fileinfo *info)
313 {
314         struct pvfs_state *pvfs = ntvfs->private_data;
315         struct pvfs_file *f;
316         struct pvfs_file_handle *h;
317         NTSTATUS status;
318         uint32_t access_needed;
319
320         f = pvfs_find_fd(pvfs, req, info->generic.in.fnum);
321         if (!f) {
322                 return NT_STATUS_INVALID_HANDLE;
323         }
324         h = f->handle;
325
326         access_needed = pvfs_fileinfo_access(info->generic.level);
327         if (!(f->access_mask & access_needed)) {
328                 return NT_STATUS_ACCESS_DENIED;
329         }
330
331         /* update the file information */
332         status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
333         if (!NT_STATUS_IS_OK(status)) {
334                 return status;
335         }
336         
337         status = pvfs_map_fileinfo(pvfs, req, h->name, info, h->fd);
338
339         /* a qfileinfo can fill in a bit more info than a qpathinfo -
340            now modify the levels that need to be fixed up */
341         switch (info->generic.level) {
342         case RAW_FILEINFO_STANDARD_INFO:
343         case RAW_FILEINFO_STANDARD_INFORMATION:
344                 if (h->create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
345                         info->standard_info.out.delete_pending = 1;
346                         info->standard_info.out.nlink--;
347                 }
348                 break;
349
350         case RAW_FILEINFO_ALL_INFO:
351         case RAW_FILEINFO_ALL_INFORMATION:
352                 if (h->create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
353                         info->all_info.out.delete_pending = 1;
354                         info->all_info.out.nlink--;
355                 }
356                 break;
357
358         case RAW_FILEINFO_POSITION_INFORMATION:
359                 info->position_information.out.position = h->position;
360                 break;
361
362         case RAW_FILEINFO_ACCESS_INFORMATION:
363                 info->access_information.out.access_flags = f->access_mask;
364                 break;
365
366         case RAW_FILEINFO_MODE_INFORMATION:
367                 info->mode_information.out.mode = h->mode;
368                 break;
369
370         default:
371                 break;
372         }
373         
374         return status;
375 }