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