r25554: Convert last instances of BOOL, True and False to the standard types.
[bbaumbach/samba-autobuild/.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    Copyright (C) James Peach 2007
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 "libcli/raw/libcliraw.h"
24 #include "librpc/gen_ndr/ndr_security.h"
25
26
27 /*
28   Handle setfileinfo/setpathinfo passthu constructions
29 */
30 bool smb_raw_setfileinfo_passthru(TALLOC_CTX *mem_ctx,
31                                   enum smb_setfileinfo_level level,
32                                   union smb_setfileinfo *parms, 
33                                   DATA_BLOB *blob)
34 {       
35         uint_t len;
36
37 #define NEED_BLOB(n) do { \
38           *blob = data_blob_talloc(mem_ctx, NULL, n); \
39           if (blob->data == NULL) return false; \
40         } while (0)
41
42         switch (level) {
43         case RAW_SFILEINFO_BASIC_INFORMATION:
44                 NEED_BLOB(40);
45                 smbcli_push_nttime(blob->data,  0, parms->basic_info.in.create_time);
46                 smbcli_push_nttime(blob->data,  8, parms->basic_info.in.access_time);
47                 smbcli_push_nttime(blob->data, 16, parms->basic_info.in.write_time);
48                 smbcli_push_nttime(blob->data, 24, parms->basic_info.in.change_time);
49                 SIVAL(blob->data,           32, parms->basic_info.in.attrib);
50                 SIVAL(blob->data,           36, 0); /* padding */
51                 return true;
52
53         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
54                 NEED_BLOB(4);
55                 SIVAL(blob->data, 0, parms->disposition_info.in.delete_on_close);
56                 return true;
57
58         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
59                 NEED_BLOB(8);
60                 SBVAL(blob->data, 0, parms->allocation_info.in.alloc_size);
61                 return true;
62
63         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
64                 NEED_BLOB(8);
65                 SBVAL(blob->data, 0, parms->end_of_file_info.in.size);
66                 return true;
67
68         case RAW_SFILEINFO_RENAME_INFORMATION:
69                 NEED_BLOB(12);
70                 SIVAL(blob->data, 0, parms->rename_information.in.overwrite);
71                 SIVAL(blob->data, 4, parms->rename_information.in.root_fid);
72                 len = smbcli_blob_append_string(NULL, mem_ctx, blob,
73                                                 parms->rename_information.in.new_name, 
74                                                 STR_UNICODE|STR_TERMINATE);
75                 SIVAL(blob->data, 8, len - 2);
76                 return true;
77
78         case RAW_SFILEINFO_POSITION_INFORMATION:
79                 NEED_BLOB(8);
80                 SBVAL(blob->data, 0, parms->position_information.in.position);
81                 return true;
82
83         case RAW_SFILEINFO_MODE_INFORMATION:
84                 NEED_BLOB(4);
85                 SIVAL(blob->data, 0, parms->mode_information.in.mode);
86                 return true;
87
88         case RAW_FILEINFO_SEC_DESC: {
89                 NTSTATUS status;
90
91                 status = ndr_push_struct_blob(blob, mem_ctx,
92                                               parms->set_secdesc.in.sd,
93                                               (ndr_push_flags_fn_t)ndr_push_security_descriptor);
94                 if (!NT_STATUS_IS_OK(status)) return false;
95
96                 return true;
97         }
98
99                 /* Unhandled levels */
100         case RAW_SFILEINFO_1023:
101         case RAW_SFILEINFO_1025:
102         case RAW_SFILEINFO_1029:
103         case RAW_SFILEINFO_1032:
104         case RAW_SFILEINFO_1039:
105         case RAW_SFILEINFO_1040:
106                 break;
107
108         default:
109                 DEBUG(0,("Unhandled setfileinfo passthru level %d\n", level));
110                 return false;
111         }
112
113         return false;
114 }
115
116 /*
117   Handle setfileinfo/setpathinfo trans2 backend.
118 */
119 static bool smb_raw_setinfo_backend(struct smbcli_tree *tree,
120                                     TALLOC_CTX *mem_ctx,
121                                     union smb_setfileinfo *parms, 
122                                     DATA_BLOB *blob)
123 {       
124         switch (parms->generic.level) {
125         case RAW_SFILEINFO_GENERIC:
126         case RAW_SFILEINFO_SETATTR:
127         case RAW_SFILEINFO_SETATTRE:
128         case RAW_SFILEINFO_SEC_DESC:
129                 /* not handled here */
130                 return false;
131
132         case RAW_SFILEINFO_STANDARD:
133                 NEED_BLOB(12);
134                 raw_push_dos_date2(tree->session->transport, 
135                                   blob->data, 0, parms->standard.in.create_time);
136                 raw_push_dos_date2(tree->session->transport, 
137                                   blob->data, 4, parms->standard.in.access_time);
138                 raw_push_dos_date2(tree->session->transport, 
139                                   blob->data, 8, parms->standard.in.write_time);
140                 return true;
141
142         case RAW_SFILEINFO_EA_SET:
143                 NEED_BLOB(ea_list_size(parms->ea_set.in.num_eas, parms->ea_set.in.eas));
144                 ea_put_list(blob->data, parms->ea_set.in.num_eas, parms->ea_set.in.eas);
145                 return true;
146
147         case RAW_SFILEINFO_BASIC_INFO:
148         case RAW_SFILEINFO_BASIC_INFORMATION:
149                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_BASIC_INFORMATION, 
150                                                     parms, blob);
151
152         case RAW_SFILEINFO_UNIX_BASIC:
153                 NEED_BLOB(100);
154                 SBVAL(blob->data, 0, parms->unix_basic.in.end_of_file);
155                 SBVAL(blob->data, 8, parms->unix_basic.in.num_bytes);
156                 smbcli_push_nttime(blob->data, 16, parms->unix_basic.in.status_change_time);
157                 smbcli_push_nttime(blob->data, 24, parms->unix_basic.in.access_time);
158                 smbcli_push_nttime(blob->data, 32, parms->unix_basic.in.change_time);
159                 SBVAL(blob->data, 40, parms->unix_basic.in.uid);
160                 SBVAL(blob->data, 48, parms->unix_basic.in.gid);
161                 SIVAL(blob->data, 56, parms->unix_basic.in.file_type);
162                 SBVAL(blob->data, 60, parms->unix_basic.in.dev_major);
163                 SBVAL(blob->data, 68, parms->unix_basic.in.dev_minor);
164                 SBVAL(blob->data, 76, parms->unix_basic.in.unique_id);
165                 SBVAL(blob->data, 84, parms->unix_basic.in.permissions);
166                 SBVAL(blob->data, 92, parms->unix_basic.in.nlink);
167                 return true;
168
169         case RAW_SFILEINFO_UNIX_INFO2:
170                 NEED_BLOB(116);
171                 SBVAL(blob->data,   0, parms->unix_info2.in.end_of_file);
172                 SBVAL(blob->data,   8, parms->unix_info2.in.num_bytes);
173                 smbcli_push_nttime(blob->data, 16, parms->unix_info2.in.status_change_time);
174                 smbcli_push_nttime(blob->data, 24, parms->unix_info2.in.access_time);
175                 smbcli_push_nttime(blob->data, 32, parms->unix_info2.in.change_time);
176                 SBVAL(blob->data,  40,parms->unix_info2.in.uid);
177                 SBVAL(blob->data,  48,parms->unix_info2.in.gid);
178                 SIVAL(blob->data,  52,parms->unix_info2.in.file_type);
179                 SBVAL(blob->data,  60,parms->unix_info2.in.dev_major);
180                 SBVAL(blob->data,  68,parms->unix_info2.in.dev_minor);
181                 SBVAL(blob->data,  76,parms->unix_info2.in.unique_id);
182                 SBVAL(blob->data,  84,parms->unix_info2.in.permissions);
183                 SBVAL(blob->data,  92,parms->unix_info2.in.nlink);
184                 smbcli_push_nttime(blob->data, 100, parms->unix_info2.in.create_time);
185                 SIVAL(blob->data, 108, parms->unix_info2.in.file_flags);
186                 SIVAL(blob->data, 112, parms->unix_info2.in.flags_mask);
187                 return true;
188
189         case RAW_SFILEINFO_DISPOSITION_INFO:
190         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
191                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_DISPOSITION_INFORMATION,
192                                                     parms, blob);
193
194         case RAW_SFILEINFO_ALLOCATION_INFO:
195         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
196                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_ALLOCATION_INFORMATION,
197                                                     parms, blob);
198
199         case RAW_SFILEINFO_END_OF_FILE_INFO:
200         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
201                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_END_OF_FILE_INFORMATION,
202                                                     parms, blob);
203
204         case RAW_SFILEINFO_RENAME_INFORMATION:
205                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_RENAME_INFORMATION,
206                                                     parms, blob);
207
208         case RAW_SFILEINFO_POSITION_INFORMATION:
209                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_POSITION_INFORMATION,
210                                                     parms, blob);
211
212         case RAW_SFILEINFO_MODE_INFORMATION:
213                 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_MODE_INFORMATION,
214                                                     parms, blob);
215                 
216                 /* Unhandled passthru levels */
217         case RAW_SFILEINFO_1023:
218         case RAW_SFILEINFO_1025:
219         case RAW_SFILEINFO_1029:
220         case RAW_SFILEINFO_1032:
221         case RAW_SFILEINFO_1039:
222         case RAW_SFILEINFO_1040:
223                 return smb_raw_setfileinfo_passthru(mem_ctx, parms->generic.level,
224                                                     parms, blob);
225
226                 /* Unhandled levels */
227
228         case RAW_SFILEINFO_UNIX_LINK:
229         case RAW_SFILEINFO_UNIX_HLINK:
230                 break;
231         }
232
233         return false;
234 }
235
236 /****************************************************************************
237  Very raw set file info - takes data blob (async send)
238 ****************************************************************************/
239 static struct smbcli_request *smb_raw_setfileinfo_blob_send(struct smbcli_tree *tree,
240                                                          TALLOC_CTX *mem_ctx,
241                                                          uint16_t fnum,
242                                                          uint16_t info_level,
243                                                          DATA_BLOB *blob)
244 {
245         struct smb_trans2 tp;
246         uint16_t setup = TRANSACT2_SETFILEINFO;
247         
248         tp.in.max_setup = 0;
249         tp.in.flags = 0; 
250         tp.in.timeout = 0;
251         tp.in.setup_count = 1;
252         tp.in.max_param = 2;
253         tp.in.max_data = 0;
254         tp.in.setup = &setup;
255         
256         tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
257         if (!tp.in.params.data) {
258                 return NULL;
259         }
260         SSVAL(tp.in.params.data, 0, fnum);
261         SSVAL(tp.in.params.data, 2, info_level);
262         SSVAL(tp.in.params.data, 4, 0); /* reserved */
263
264         tp.in.data = *blob;
265
266         return smb_raw_trans2_send(tree, &tp);
267 }
268
269 /****************************************************************************
270  Very raw set path info - takes data blob
271 ****************************************************************************/
272 static struct smbcli_request *smb_raw_setpathinfo_blob_send(struct smbcli_tree *tree,
273                                                             TALLOC_CTX *mem_ctx,
274                                                             const char *fname,
275                                                             uint16_t info_level,
276                                                             DATA_BLOB *blob)
277 {
278         struct smb_trans2 tp;
279         uint16_t setup = TRANSACT2_SETPATHINFO;
280         
281         tp.in.max_setup = 0;
282         tp.in.flags = 0; 
283         tp.in.timeout = 0;
284         tp.in.setup_count = 1;
285         tp.in.max_param = 2;
286         tp.in.max_data = 0;
287         tp.in.setup = &setup;
288         
289         tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
290         if (!tp.in.params.data) {
291                 return NULL;
292         }
293         SSVAL(tp.in.params.data, 0, info_level);
294         SIVAL(tp.in.params.data, 2, 0);
295         smbcli_blob_append_string(tree->session, mem_ctx, 
296                                   &tp.in.params,
297                                   fname, STR_TERMINATE);
298
299         tp.in.data = *blob;
300
301         return smb_raw_trans2_send(tree, &tp);
302 }
303                 
304 /****************************************************************************
305  Handle setattr (async send)
306 ****************************************************************************/
307 static struct smbcli_request *smb_raw_setattr_send(struct smbcli_tree *tree,
308                                                 union smb_setfileinfo *parms)
309 {
310         struct smbcli_request *req;
311
312         req = smbcli_request_setup(tree, SMBsetatr, 8, 0);
313         if (!req) return NULL;
314         
315         SSVAL(req->out.vwv,         VWV(0), parms->setattr.in.attrib);
316         raw_push_dos_date3(tree->session->transport, 
317                           req->out.vwv, VWV(1), parms->setattr.in.write_time);
318         memset(req->out.vwv + VWV(3), 0, 10); /* reserved */
319         smbcli_req_append_ascii4(req, parms->setattr.in.file.path, STR_TERMINATE);
320         smbcli_req_append_ascii4(req, "", STR_TERMINATE);
321         
322         if (!smbcli_request_send(req)) {
323                 smbcli_request_destroy(req);
324                 return NULL;
325         }
326
327         return req;
328 }
329                 
330 /****************************************************************************
331  Handle setattrE. (async send)
332 ****************************************************************************/
333 static struct smbcli_request *smb_raw_setattrE_send(struct smbcli_tree *tree,
334                                                  union smb_setfileinfo *parms)
335 {
336         struct smbcli_request *req;
337
338         req = smbcli_request_setup(tree, SMBsetattrE, 7, 0);
339         if (!req) return NULL;
340         
341         SSVAL(req->out.vwv,         VWV(0), parms->setattre.in.file.fnum);
342         raw_push_dos_date2(tree->session->transport, 
343                           req->out.vwv, VWV(1), parms->setattre.in.create_time);
344         raw_push_dos_date2(tree->session->transport, 
345                           req->out.vwv, VWV(3), parms->setattre.in.access_time);
346         raw_push_dos_date2(tree->session->transport, 
347                           req->out.vwv, VWV(5), parms->setattre.in.write_time);
348
349         if (!smbcli_request_send(req)) {
350                 smbcli_request_destroy(req);
351                 return NULL;
352         }
353
354         return req;
355 }
356
357 /****************************************************************************
358  Set file info (async send)
359 ****************************************************************************/
360 struct smbcli_request *smb_raw_setfileinfo_send(struct smbcli_tree *tree,
361                                              union smb_setfileinfo *parms)
362 {
363         DATA_BLOB blob;
364         TALLOC_CTX *mem_ctx;
365         struct smbcli_request *req;
366
367         if (parms->generic.level == RAW_SFILEINFO_SETATTRE) {
368                 return smb_raw_setattrE_send(tree, parms);
369         }
370         if (parms->generic.level == RAW_SFILEINFO_SEC_DESC) {
371                 return smb_raw_set_secdesc_send(tree, parms);
372         }
373         if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
374                 return NULL;
375         }
376
377         mem_ctx = talloc_init("setpathinfo");
378         if (!mem_ctx) return NULL;
379
380         if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
381                 talloc_free(mem_ctx);
382                 return NULL;
383         }
384         
385         /* send request and process the output */
386         req = smb_raw_setfileinfo_blob_send(tree, 
387                                             mem_ctx,
388                                             parms->generic.in.file.fnum, 
389                                             parms->generic.level, 
390                                             &blob);
391
392         talloc_free(mem_ctx);
393         return req;
394 }
395
396 /****************************************************************************
397  Set file info (async send)
398 ****************************************************************************/
399 NTSTATUS smb_raw_setfileinfo(struct smbcli_tree *tree,
400                              union smb_setfileinfo *parms)
401 {
402         struct smbcli_request *req = smb_raw_setfileinfo_send(tree, parms);
403         return smbcli_request_simple_recv(req);
404 }
405
406
407 /****************************************************************************
408  Set path info (async send)
409 ****************************************************************************/
410 struct smbcli_request *smb_raw_setpathinfo_send(struct smbcli_tree *tree,
411                                              union smb_setfileinfo *parms)
412 {
413         DATA_BLOB blob;
414         TALLOC_CTX *mem_ctx;
415         struct smbcli_request *req;
416
417         if (parms->generic.level == RAW_SFILEINFO_SETATTR) {
418                 return smb_raw_setattr_send(tree, parms);
419         }
420         if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
421                 return NULL;
422         }
423
424         mem_ctx = talloc_init("setpathinfo");
425         if (!mem_ctx) return NULL;
426
427         if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
428                 talloc_free(mem_ctx);
429                 return NULL;
430         }
431
432         /* send request and process the output */
433         req = smb_raw_setpathinfo_blob_send(tree, 
434                                             mem_ctx,
435                                             parms->generic.in.file.path, 
436                                             parms->generic.level,
437                                             &blob);
438
439         talloc_free(mem_ctx);
440         return req;
441 }
442
443 /****************************************************************************
444  Set path info (sync interface)
445 ****************************************************************************/
446 NTSTATUS smb_raw_setpathinfo(struct smbcli_tree *tree,
447                              union smb_setfileinfo *parms)
448 {
449         struct smbcli_request *req = smb_raw_setpathinfo_send(tree, parms);
450         return smbcli_request_simple_recv(req);
451 }