r16091: Without this patch Samba3 will not accept this, and simply setting the "92" to
[samba.git] / source4 / libcli / raw / rawsetfileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_SFILEINFO_* calls
4    Copyright (C) James Myers 2003
5    Copyright (C) Andrew Tridgell 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24
25
26 /*
27   Handle setfileinfo/setpathinfo passthu constructions
28 */
29 BOOL smb_raw_setfileinfo_passthru(TALLOC_CTX *mem_ctx,
30                                   enum smb_setfileinfo_level level,
31                                   union smb_setfileinfo *parms, 
32                                   DATA_BLOB *blob)
33 {       
34         uint_t len;
35
36 #define NEED_BLOB(n) do { \
37           *blob = data_blob_talloc(mem_ctx, NULL, n); \
38           if (blob->data == NULL) return False; \
39         } while (0)
40
41         switch (level) {
42         case RAW_SFILEINFO_BASIC_INFORMATION:
43                 NEED_BLOB(40);
44                 smbcli_push_nttime(blob->data,  0, parms->basic_info.in.create_time);
45                 smbcli_push_nttime(blob->data,  8, parms->basic_info.in.access_time);
46                 smbcli_push_nttime(blob->data, 16, parms->basic_info.in.write_time);
47                 smbcli_push_nttime(blob->data, 24, parms->basic_info.in.change_time);
48                 SIVAL(blob->data,           32, parms->basic_info.in.attrib);
49                 SIVAL(blob->data,           36, 0); /* padding */
50                 return True;
51
52         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
53                 NEED_BLOB(4);
54                 SIVAL(blob->data, 0, parms->disposition_info.in.delete_on_close);
55                 return True;
56
57         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
58                 NEED_BLOB(8);
59                 SBVAL(blob->data, 0, parms->allocation_info.in.alloc_size);
60                 return True;
61
62         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
63                 NEED_BLOB(8);
64                 SBVAL(blob->data, 0, parms->end_of_file_info.in.size);
65                 return True;
66
67         case RAW_SFILEINFO_RENAME_INFORMATION:
68                 NEED_BLOB(12);
69                 SIVAL(blob->data, 0, parms->rename_information.in.overwrite);
70                 SIVAL(blob->data, 4, parms->rename_information.in.root_fid);
71                 len = smbcli_blob_append_string(NULL, mem_ctx, blob,
72                                                 parms->rename_information.in.new_name, 
73                                                 STR_UNICODE|STR_TERMINATE);
74                 SIVAL(blob->data, 8, len - 2);
75                 return True;
76
77         case RAW_SFILEINFO_POSITION_INFORMATION:
78                 NEED_BLOB(8);
79                 SBVAL(blob->data, 0, parms->position_information.in.position);
80                 return True;
81
82         case RAW_SFILEINFO_MODE_INFORMATION:
83                 NEED_BLOB(4);
84                 SIVAL(blob->data, 0, parms->mode_information.in.mode);
85                 return True;
86                 
87                 /* Unhandled levels */
88         case RAW_SFILEINFO_1023:
89         case RAW_SFILEINFO_1025:
90         case RAW_SFILEINFO_1029:
91         case RAW_SFILEINFO_1032:
92         case RAW_SFILEINFO_1039:
93         case RAW_SFILEINFO_1040:
94                 break;
95
96         default:
97                 DEBUG(0,("Unhandled setfileinfo passthru level %d\n", level));
98                 return False;
99         }
100
101         return False;
102 }
103
104 /*
105   Handle setfileinfo/setpathinfo trans2 backend.
106 */
107 static BOOL smb_raw_setinfo_backend(struct smbcli_tree *tree,
108                                     TALLOC_CTX *mem_ctx,
109                                     union smb_setfileinfo *parms, 
110                                     DATA_BLOB *blob)
111 {       
112         switch (parms->generic.level) {
113         case RAW_SFILEINFO_GENERIC:
114         case RAW_SFILEINFO_SETATTR:
115         case RAW_SFILEINFO_SETATTRE:
116         case RAW_SFILEINFO_SEC_DESC:
117                 /* not handled here */
118                 return False;
119
120         case RAW_SFILEINFO_STANDARD:
121                 NEED_BLOB(12);
122                 raw_push_dos_date2(tree->session->transport, 
123                                   blob->data, 0, parms->standard.in.create_time);
124                 raw_push_dos_date2(tree->session->transport, 
125                                   blob->data, 4, parms->standard.in.access_time);
126                 raw_push_dos_date2(tree->session->transport, 
127                                   blob->data, 8, parms->standard.in.write_time);
128                 return True;
129
130         case RAW_SFILEINFO_EA_SET:
131                 NEED_BLOB(ea_list_size(parms->ea_set.in.num_eas, parms->ea_set.in.eas));
132                 ea_put_list(blob->data, parms->ea_set.in.num_eas, parms->ea_set.in.eas);
133                 return True;
134
135         case RAW_SFILEINFO_BASIC_INFO:
136         case RAW_SFILEINFO_BASIC_INFORMATION:
137                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_BASIC_INFORMATION, 
138                                                     parms, blob);
139
140         case RAW_SFILEINFO_UNIX_BASIC:
141                 NEED_BLOB(100);
142                 SBVAL(blob->data, 0, parms->unix_basic.in.end_of_file);
143                 SBVAL(blob->data, 8, parms->unix_basic.in.num_bytes);
144                 smbcli_push_nttime(blob->data, 16, parms->unix_basic.in.status_change_time);
145                 smbcli_push_nttime(blob->data, 24, parms->unix_basic.in.access_time);
146                 smbcli_push_nttime(blob->data, 32, parms->unix_basic.in.change_time);
147                 SBVAL(blob->data, 40, parms->unix_basic.in.uid);
148                 SBVAL(blob->data, 48, parms->unix_basic.in.gid);
149                 SIVAL(blob->data, 56, parms->unix_basic.in.file_type);
150                 SBVAL(blob->data, 60, parms->unix_basic.in.dev_major);
151                 SBVAL(blob->data, 68, parms->unix_basic.in.dev_minor);
152                 SBVAL(blob->data, 76, parms->unix_basic.in.unique_id);
153                 SBVAL(blob->data, 84, parms->unix_basic.in.permissions);
154                 SBVAL(blob->data, 92, parms->unix_basic.in.nlink);
155                 return True;
156
157         case RAW_SFILEINFO_DISPOSITION_INFO:
158         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
159                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_DISPOSITION_INFORMATION,
160                                                     parms, blob);
161
162         case RAW_SFILEINFO_ALLOCATION_INFO:
163         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
164                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_ALLOCATION_INFORMATION,
165                                                     parms, blob);
166
167         case RAW_SFILEINFO_END_OF_FILE_INFO:
168         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
169                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_END_OF_FILE_INFORMATION,
170                                                     parms, blob);
171
172         case RAW_SFILEINFO_RENAME_INFORMATION:
173                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_RENAME_INFORMATION,
174                                                     parms, blob);
175
176         case RAW_SFILEINFO_POSITION_INFORMATION:
177                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_POSITION_INFORMATION,
178                                                     parms, blob);
179
180         case RAW_SFILEINFO_MODE_INFORMATION:
181                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_MODE_INFORMATION,
182                                                     parms, blob);
183                 
184                 /* Unhandled passthru levels */
185         case RAW_SFILEINFO_1023:
186         case RAW_SFILEINFO_1025:
187         case RAW_SFILEINFO_1029:
188         case RAW_SFILEINFO_1032:
189         case RAW_SFILEINFO_1039:
190         case RAW_SFILEINFO_1040:
191                 return smb_raw_setfileinfo_passthru(mem_ctx, parms->generic.level,
192                                                     parms, blob);
193
194                 /* Unhandled levels */
195
196         case RAW_SFILEINFO_UNIX_LINK:
197         case RAW_SFILEINFO_UNIX_HLINK:
198                 break;
199         }
200
201         return False;
202 }
203
204 /****************************************************************************
205  Very raw set file info - takes data blob (async send)
206 ****************************************************************************/
207 static struct smbcli_request *smb_raw_setfileinfo_blob_send(struct smbcli_tree *tree,
208                                                          TALLOC_CTX *mem_ctx,
209                                                          uint16_t fnum,
210                                                          uint16_t info_level,
211                                                          DATA_BLOB *blob)
212 {
213         struct smb_trans2 tp;
214         uint16_t setup = TRANSACT2_SETFILEINFO;
215         
216         tp.in.max_setup = 0;
217         tp.in.flags = 0; 
218         tp.in.timeout = 0;
219         tp.in.setup_count = 1;
220         tp.in.max_param = 2;
221         tp.in.max_data = 0;
222         tp.in.setup = &setup;
223         
224         tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
225         if (!tp.in.params.data) {
226                 return NULL;
227         }
228         SSVAL(tp.in.params.data, 0, fnum);
229         SSVAL(tp.in.params.data, 2, info_level);
230         SSVAL(tp.in.params.data, 4, 0); /* reserved */
231
232         tp.in.data = *blob;
233
234         return smb_raw_trans2_send(tree, &tp);
235 }
236
237 /****************************************************************************
238  Very raw set path info - takes data blob
239 ****************************************************************************/
240 static struct smbcli_request *smb_raw_setpathinfo_blob_send(struct smbcli_tree *tree,
241                                                             TALLOC_CTX *mem_ctx,
242                                                             const char *fname,
243                                                             uint16_t info_level,
244                                                             DATA_BLOB *blob)
245 {
246         struct smb_trans2 tp;
247         uint16_t setup = TRANSACT2_SETPATHINFO;
248         
249         tp.in.max_setup = 0;
250         tp.in.flags = 0; 
251         tp.in.timeout = 0;
252         tp.in.setup_count = 1;
253         tp.in.max_param = 2;
254         tp.in.max_data = 0;
255         tp.in.setup = &setup;
256         
257         tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
258         if (!tp.in.params.data) {
259                 return NULL;
260         }
261         SSVAL(tp.in.params.data, 0, info_level);
262         SIVAL(tp.in.params.data, 2, 0);
263         smbcli_blob_append_string(tree->session, mem_ctx, 
264                                   &tp.in.params,
265                                   fname, STR_TERMINATE);
266
267         tp.in.data = *blob;
268
269         return smb_raw_trans2_send(tree, &tp);
270 }
271                 
272 /****************************************************************************
273  Handle setattr (async send)
274 ****************************************************************************/
275 static struct smbcli_request *smb_raw_setattr_send(struct smbcli_tree *tree,
276                                                 union smb_setfileinfo *parms)
277 {
278         struct smbcli_request *req;
279
280         req = smbcli_request_setup(tree, SMBsetatr, 8, 0);
281         if (!req) return NULL;
282         
283         SSVAL(req->out.vwv,         VWV(0), parms->setattr.in.attrib);
284         raw_push_dos_date3(tree->session->transport, 
285                           req->out.vwv, VWV(1), parms->setattr.in.write_time);
286         memset(req->out.vwv + VWV(3), 0, 10); /* reserved */
287         smbcli_req_append_ascii4(req, parms->setattr.in.file.path, STR_TERMINATE);
288         smbcli_req_append_ascii4(req, "", STR_TERMINATE);
289         
290         if (!smbcli_request_send(req)) {
291                 smbcli_request_destroy(req);
292                 return NULL;
293         }
294
295         return req;
296 }
297                 
298 /****************************************************************************
299  Handle setattrE. (async send)
300 ****************************************************************************/
301 static struct smbcli_request *smb_raw_setattrE_send(struct smbcli_tree *tree,
302                                                  union smb_setfileinfo *parms)
303 {
304         struct smbcli_request *req;
305
306         req = smbcli_request_setup(tree, SMBsetattrE, 7, 0);
307         if (!req) return NULL;
308         
309         SSVAL(req->out.vwv,         VWV(0), parms->setattre.in.file.fnum);
310         raw_push_dos_date2(tree->session->transport, 
311                           req->out.vwv, VWV(1), parms->setattre.in.create_time);
312         raw_push_dos_date2(tree->session->transport, 
313                           req->out.vwv, VWV(3), parms->setattre.in.access_time);
314         raw_push_dos_date2(tree->session->transport, 
315                           req->out.vwv, VWV(5), parms->setattre.in.write_time);
316
317         if (!smbcli_request_send(req)) {
318                 smbcli_request_destroy(req);
319                 return NULL;
320         }
321
322         return req;
323 }
324
325 /****************************************************************************
326  Set file info (async send)
327 ****************************************************************************/
328 struct smbcli_request *smb_raw_setfileinfo_send(struct smbcli_tree *tree,
329                                              union smb_setfileinfo *parms)
330 {
331         DATA_BLOB blob;
332         TALLOC_CTX *mem_ctx;
333         struct smbcli_request *req;
334
335         if (parms->generic.level == RAW_SFILEINFO_SETATTRE) {
336                 return smb_raw_setattrE_send(tree, parms);
337         }
338         if (parms->generic.level == RAW_SFILEINFO_SEC_DESC) {
339                 return smb_raw_set_secdesc_send(tree, parms);
340         }
341         if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
342                 return NULL;
343         }
344
345         mem_ctx = talloc_init("setpathinfo");
346         if (!mem_ctx) return NULL;
347
348         if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
349                 talloc_free(mem_ctx);
350                 return NULL;
351         }
352         
353         /* send request and process the output */
354         req = smb_raw_setfileinfo_blob_send(tree, 
355                                             mem_ctx,
356                                             parms->generic.in.file.fnum, 
357                                             parms->generic.level, 
358                                             &blob);
359
360         talloc_free(mem_ctx);
361         return req;
362 }
363
364 /****************************************************************************
365  Set file info (async send)
366 ****************************************************************************/
367 NTSTATUS smb_raw_setfileinfo(struct smbcli_tree *tree,
368                              union smb_setfileinfo *parms)
369 {
370         struct smbcli_request *req = smb_raw_setfileinfo_send(tree, parms);
371         return smbcli_request_simple_recv(req);
372 }
373
374
375 /****************************************************************************
376  Set path info (async send)
377 ****************************************************************************/
378 struct smbcli_request *smb_raw_setpathinfo_send(struct smbcli_tree *tree,
379                                              union smb_setfileinfo *parms)
380 {
381         DATA_BLOB blob;
382         TALLOC_CTX *mem_ctx;
383         struct smbcli_request *req;
384
385         if (parms->generic.level == RAW_SFILEINFO_SETATTR) {
386                 return smb_raw_setattr_send(tree, parms);
387         }
388         if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
389                 return NULL;
390         }
391
392         mem_ctx = talloc_init("setpathinfo");
393         if (!mem_ctx) return NULL;
394
395         if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
396                 talloc_free(mem_ctx);
397                 return NULL;
398         }
399
400         /* send request and process the output */
401         req = smb_raw_setpathinfo_blob_send(tree, 
402                                             mem_ctx,
403                                             parms->generic.in.file.path, 
404                                             parms->generic.level,
405                                             &blob);
406
407         talloc_free(mem_ctx);
408         return req;
409 }
410
411 /****************************************************************************
412  Set path info (sync interface)
413 ****************************************************************************/
414 NTSTATUS smb_raw_setpathinfo(struct smbcli_tree *tree,
415                              union smb_setfileinfo *parms)
416 {
417         struct smbcli_request *req = smb_raw_setpathinfo_send(tree, parms);
418         return smbcli_request_simple_recv(req);
419 }