s4:service_task: prevent a segfault if task->msg_ctx is not initialized yet
[Samba/gebeck_regimport.git] / source4 / libcli / raw / rawsetfileinfo.c
blobfec99d30b9578115b1e361af0f0cfa87790e7bbc
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
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.
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.
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/>.
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "librpc/gen_ndr/ndr_security.h"
29 Handle setfileinfo/setpathinfo passthu constructions
31 bool smb_raw_setfileinfo_passthru(TALLOC_CTX *mem_ctx,
32 enum smb_setfileinfo_level level,
33 union smb_setfileinfo *parms,
34 DATA_BLOB *blob)
36 unsigned int len;
38 #define NEED_BLOB(n) do { \
39 *blob = data_blob_talloc(mem_ctx, NULL, n); \
40 if (blob->data == NULL && n != 0) return false; \
41 } while (0)
43 switch (level) {
44 case RAW_SFILEINFO_BASIC_INFORMATION:
45 NEED_BLOB(40);
46 smbcli_push_nttime(blob->data, 0, parms->basic_info.in.create_time);
47 smbcli_push_nttime(blob->data, 8, parms->basic_info.in.access_time);
48 smbcli_push_nttime(blob->data, 16, parms->basic_info.in.write_time);
49 smbcli_push_nttime(blob->data, 24, parms->basic_info.in.change_time);
50 SIVAL(blob->data, 32, parms->basic_info.in.attrib);
51 SIVAL(blob->data, 36, 0); /* padding */
52 return true;
54 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
55 NEED_BLOB(4);
56 SIVAL(blob->data, 0, parms->disposition_info.in.delete_on_close);
57 return true;
59 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
60 NEED_BLOB(8);
61 SBVAL(blob->data, 0, parms->allocation_info.in.alloc_size);
62 return true;
64 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
65 NEED_BLOB(8);
66 SBVAL(blob->data, 0, parms->end_of_file_info.in.size);
67 return true;
69 case RAW_SFILEINFO_RENAME_INFORMATION:
70 NEED_BLOB(12);
71 SIVAL(blob->data, 0, parms->rename_information.in.overwrite);
72 SIVAL(blob->data, 4, parms->rename_information.in.root_fid);
73 len = smbcli_blob_append_string(NULL, mem_ctx, blob,
74 parms->rename_information.in.new_name,
75 STR_UNICODE|STR_TERMINATE);
76 SIVAL(blob->data, 8, len - 2);
77 return true;
79 case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
80 NEED_BLOB(20);
81 SIVAL(blob->data, 0, parms->rename_information.in.overwrite);
82 SIVAL(blob->data, 4, 0);
83 SBVAL(blob->data, 8, parms->rename_information.in.root_fid);
84 len = smbcli_blob_append_string(NULL, mem_ctx, blob,
85 parms->rename_information.in.new_name,
86 STR_UNICODE|STR_TERMINATE);
87 SIVAL(blob->data, 16, len - 2);
88 return true;
90 case RAW_SFILEINFO_POSITION_INFORMATION:
91 NEED_BLOB(8);
92 SBVAL(blob->data, 0, parms->position_information.in.position);
93 return true;
95 case RAW_SFILEINFO_MODE_INFORMATION:
96 NEED_BLOB(4);
97 SIVAL(blob->data, 0, parms->mode_information.in.mode);
98 return true;
100 case RAW_FILEINFO_SEC_DESC: {
101 enum ndr_err_code ndr_err;
103 ndr_err = ndr_push_struct_blob(blob, mem_ctx, parms->set_secdesc.in.sd,
104 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 return false;
109 return true;
112 case RAW_SFILEINFO_FULL_EA_INFORMATION:
113 printf("num_eas=%d\n", parms->full_ea_information.in.eas.num_eas);
114 NEED_BLOB(ea_list_size_chained(
115 parms->full_ea_information.in.eas.num_eas,
116 parms->full_ea_information.in.eas.eas, 4));
117 ea_put_list_chained(blob->data,
118 parms->full_ea_information.in.eas.num_eas,
119 parms->full_ea_information.in.eas.eas, 4);
120 return true;
122 /* Unhandled levels */
123 case RAW_SFILEINFO_PIPE_INFORMATION:
124 case RAW_SFILEINFO_VALID_DATA_INFORMATION:
125 case RAW_SFILEINFO_SHORT_NAME_INFORMATION:
126 case RAW_SFILEINFO_1025:
127 case RAW_SFILEINFO_1027:
128 case RAW_SFILEINFO_1029:
129 case RAW_SFILEINFO_1030:
130 case RAW_SFILEINFO_1031:
131 case RAW_SFILEINFO_1032:
132 case RAW_SFILEINFO_1036:
133 case RAW_SFILEINFO_1041:
134 case RAW_SFILEINFO_1042:
135 case RAW_SFILEINFO_1043:
136 case RAW_SFILEINFO_1044:
137 break;
139 default:
140 DEBUG(0,("Unhandled setfileinfo passthru level %d\n", level));
141 return false;
144 return false;
148 Handle setfileinfo/setpathinfo trans2 backend.
150 static bool smb_raw_setinfo_backend(struct smbcli_tree *tree,
151 TALLOC_CTX *mem_ctx,
152 union smb_setfileinfo *parms,
153 DATA_BLOB *blob)
155 switch (parms->generic.level) {
156 case RAW_SFILEINFO_GENERIC:
157 case RAW_SFILEINFO_SETATTR:
158 case RAW_SFILEINFO_SETATTRE:
159 case RAW_SFILEINFO_SEC_DESC:
160 /* not handled here */
161 return false;
163 case RAW_SFILEINFO_STANDARD:
164 NEED_BLOB(12);
165 raw_push_dos_date2(tree->session->transport,
166 blob->data, 0, parms->standard.in.create_time);
167 raw_push_dos_date2(tree->session->transport,
168 blob->data, 4, parms->standard.in.access_time);
169 raw_push_dos_date2(tree->session->transport,
170 blob->data, 8, parms->standard.in.write_time);
171 return true;
173 case RAW_SFILEINFO_EA_SET:
174 NEED_BLOB(ea_list_size(parms->ea_set.in.num_eas, parms->ea_set.in.eas));
175 ea_put_list(blob->data, parms->ea_set.in.num_eas, parms->ea_set.in.eas);
176 return true;
178 case RAW_SFILEINFO_BASIC_INFO:
179 case RAW_SFILEINFO_BASIC_INFORMATION:
180 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_BASIC_INFORMATION,
181 parms, blob);
183 case RAW_SFILEINFO_UNIX_BASIC:
184 NEED_BLOB(100);
185 SBVAL(blob->data, 0, parms->unix_basic.in.end_of_file);
186 SBVAL(blob->data, 8, parms->unix_basic.in.num_bytes);
187 smbcli_push_nttime(blob->data, 16, parms->unix_basic.in.status_change_time);
188 smbcli_push_nttime(blob->data, 24, parms->unix_basic.in.access_time);
189 smbcli_push_nttime(blob->data, 32, parms->unix_basic.in.change_time);
190 SBVAL(blob->data, 40, parms->unix_basic.in.uid);
191 SBVAL(blob->data, 48, parms->unix_basic.in.gid);
192 SIVAL(blob->data, 56, parms->unix_basic.in.file_type);
193 SBVAL(blob->data, 60, parms->unix_basic.in.dev_major);
194 SBVAL(blob->data, 68, parms->unix_basic.in.dev_minor);
195 SBVAL(blob->data, 76, parms->unix_basic.in.unique_id);
196 SBVAL(blob->data, 84, parms->unix_basic.in.permissions);
197 SBVAL(blob->data, 92, parms->unix_basic.in.nlink);
198 return true;
200 case RAW_SFILEINFO_UNIX_INFO2:
201 NEED_BLOB(116);
202 SBVAL(blob->data, 0, parms->unix_info2.in.end_of_file);
203 SBVAL(blob->data, 8, parms->unix_info2.in.num_bytes);
204 smbcli_push_nttime(blob->data, 16, parms->unix_info2.in.status_change_time);
205 smbcli_push_nttime(blob->data, 24, parms->unix_info2.in.access_time);
206 smbcli_push_nttime(blob->data, 32, parms->unix_info2.in.change_time);
207 SBVAL(blob->data, 40,parms->unix_info2.in.uid);
208 SBVAL(blob->data, 48,parms->unix_info2.in.gid);
209 SIVAL(blob->data, 56,parms->unix_info2.in.file_type);
210 SBVAL(blob->data, 60,parms->unix_info2.in.dev_major);
211 SBVAL(blob->data, 68,parms->unix_info2.in.dev_minor);
212 SBVAL(blob->data, 76,parms->unix_info2.in.unique_id);
213 SBVAL(blob->data, 84,parms->unix_info2.in.permissions);
214 SBVAL(blob->data, 92,parms->unix_info2.in.nlink);
215 smbcli_push_nttime(blob->data, 100, parms->unix_info2.in.create_time);
216 SIVAL(blob->data, 108, parms->unix_info2.in.file_flags);
217 SIVAL(blob->data, 112, parms->unix_info2.in.flags_mask);
218 return true;
220 case RAW_SFILEINFO_DISPOSITION_INFO:
221 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
222 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_DISPOSITION_INFORMATION,
223 parms, blob);
225 case RAW_SFILEINFO_ALLOCATION_INFO:
226 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
227 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_ALLOCATION_INFORMATION,
228 parms, blob);
230 case RAW_SFILEINFO_END_OF_FILE_INFO:
231 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
232 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_END_OF_FILE_INFORMATION,
233 parms, blob);
235 case RAW_SFILEINFO_RENAME_INFORMATION:
236 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_RENAME_INFORMATION,
237 parms, blob);
239 case RAW_SFILEINFO_POSITION_INFORMATION:
240 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_POSITION_INFORMATION,
241 parms, blob);
243 case RAW_SFILEINFO_MODE_INFORMATION:
244 return smb_raw_setfileinfo_passthru(mem_ctx, RAW_SFILEINFO_MODE_INFORMATION,
245 parms, blob);
247 /* Unhandled passthru levels */
248 case RAW_SFILEINFO_PIPE_INFORMATION:
249 case RAW_SFILEINFO_VALID_DATA_INFORMATION:
250 case RAW_SFILEINFO_SHORT_NAME_INFORMATION:
251 case RAW_SFILEINFO_FULL_EA_INFORMATION:
252 case RAW_SFILEINFO_1025:
253 case RAW_SFILEINFO_1027:
254 case RAW_SFILEINFO_1029:
255 case RAW_SFILEINFO_1030:
256 case RAW_SFILEINFO_1031:
257 case RAW_SFILEINFO_1032:
258 case RAW_SFILEINFO_1036:
259 case RAW_SFILEINFO_1041:
260 case RAW_SFILEINFO_1042:
261 case RAW_SFILEINFO_1043:
262 case RAW_SFILEINFO_1044:
263 return smb_raw_setfileinfo_passthru(mem_ctx, parms->generic.level,
264 parms, blob);
266 /* Unhandled levels */
267 case RAW_SFILEINFO_UNIX_LINK:
268 case RAW_SFILEINFO_UNIX_HLINK:
269 case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
270 case RAW_SFILEINFO_LINK_INFORMATION:
271 break;
274 return false;
277 /****************************************************************************
278 Very raw set file info - takes data blob (async send)
279 ****************************************************************************/
280 static struct smbcli_request *smb_raw_setfileinfo_blob_send(struct smbcli_tree *tree,
281 TALLOC_CTX *mem_ctx,
282 uint16_t fnum,
283 uint16_t info_level,
284 DATA_BLOB *blob)
286 struct smb_trans2 tp;
287 uint16_t setup = TRANSACT2_SETFILEINFO;
289 tp.in.max_setup = 0;
290 tp.in.flags = 0;
291 tp.in.timeout = 0;
292 tp.in.setup_count = 1;
293 tp.in.max_param = 2;
294 tp.in.max_data = 0;
295 tp.in.setup = &setup;
297 tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
298 if (!tp.in.params.data) {
299 return NULL;
301 SSVAL(tp.in.params.data, 0, fnum);
302 SSVAL(tp.in.params.data, 2, info_level);
303 SSVAL(tp.in.params.data, 4, 0); /* reserved */
305 tp.in.data = *blob;
307 return smb_raw_trans2_send(tree, &tp);
310 /****************************************************************************
311 Very raw set path info - takes data blob
312 ****************************************************************************/
313 static struct smbcli_request *smb_raw_setpathinfo_blob_send(struct smbcli_tree *tree,
314 TALLOC_CTX *mem_ctx,
315 const char *fname,
316 uint16_t info_level,
317 DATA_BLOB *blob)
319 struct smb_trans2 tp;
320 uint16_t setup = TRANSACT2_SETPATHINFO;
322 tp.in.max_setup = 0;
323 tp.in.flags = 0;
324 tp.in.timeout = 0;
325 tp.in.setup_count = 1;
326 tp.in.max_param = 2;
327 tp.in.max_data = 0;
328 tp.in.setup = &setup;
330 tp.in.params = data_blob_talloc(mem_ctx, NULL, 6);
331 if (!tp.in.params.data) {
332 return NULL;
334 SSVAL(tp.in.params.data, 0, info_level);
335 SIVAL(tp.in.params.data, 2, 0);
336 smbcli_blob_append_string(tree->session, mem_ctx,
337 &tp.in.params,
338 fname, STR_TERMINATE);
340 tp.in.data = *blob;
342 return smb_raw_trans2_send(tree, &tp);
345 /****************************************************************************
346 Handle setattr (async send)
347 ****************************************************************************/
348 static struct smbcli_request *smb_raw_setattr_send(struct smbcli_tree *tree,
349 union smb_setfileinfo *parms)
351 struct smbcli_request *req;
353 req = smbcli_request_setup(tree, SMBsetatr, 8, 0);
354 if (!req) return NULL;
356 SSVAL(req->out.vwv, VWV(0), parms->setattr.in.attrib);
357 raw_push_dos_date3(tree->session->transport,
358 req->out.vwv, VWV(1), parms->setattr.in.write_time);
359 memset(req->out.vwv + VWV(3), 0, 10); /* reserved */
360 smbcli_req_append_ascii4(req, parms->setattr.in.file.path, STR_TERMINATE);
361 smbcli_req_append_ascii4(req, "", STR_TERMINATE);
363 if (!smbcli_request_send(req)) {
364 smbcli_request_destroy(req);
365 return NULL;
368 return req;
371 /****************************************************************************
372 Handle setattrE. (async send)
373 ****************************************************************************/
374 static struct smbcli_request *smb_raw_setattrE_send(struct smbcli_tree *tree,
375 union smb_setfileinfo *parms)
377 struct smbcli_request *req;
379 req = smbcli_request_setup(tree, SMBsetattrE, 7, 0);
380 if (!req) return NULL;
382 SSVAL(req->out.vwv, VWV(0), parms->setattre.in.file.fnum);
383 raw_push_dos_date2(tree->session->transport,
384 req->out.vwv, VWV(1), parms->setattre.in.create_time);
385 raw_push_dos_date2(tree->session->transport,
386 req->out.vwv, VWV(3), parms->setattre.in.access_time);
387 raw_push_dos_date2(tree->session->transport,
388 req->out.vwv, VWV(5), parms->setattre.in.write_time);
390 if (!smbcli_request_send(req)) {
391 smbcli_request_destroy(req);
392 return NULL;
395 return req;
398 /****************************************************************************
399 Set file info (async send)
400 ****************************************************************************/
401 struct smbcli_request *smb_raw_setfileinfo_send(struct smbcli_tree *tree,
402 union smb_setfileinfo *parms)
404 DATA_BLOB blob;
405 TALLOC_CTX *mem_ctx;
406 struct smbcli_request *req;
408 if (parms->generic.level == RAW_SFILEINFO_SETATTRE) {
409 return smb_raw_setattrE_send(tree, parms);
411 if (parms->generic.level == RAW_SFILEINFO_SEC_DESC) {
412 return smb_raw_set_secdesc_send(tree, parms);
414 if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
415 return NULL;
418 mem_ctx = talloc_init("setpathinfo");
419 if (!mem_ctx) return NULL;
421 if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
422 talloc_free(mem_ctx);
423 return NULL;
426 /* send request and process the output */
427 req = smb_raw_setfileinfo_blob_send(tree,
428 mem_ctx,
429 parms->generic.in.file.fnum,
430 parms->generic.level,
431 &blob);
433 talloc_free(mem_ctx);
434 return req;
437 /****************************************************************************
438 Set file info (async send)
439 ****************************************************************************/
440 _PUBLIC_ NTSTATUS smb_raw_setfileinfo(struct smbcli_tree *tree,
441 union smb_setfileinfo *parms)
443 struct smbcli_request *req = smb_raw_setfileinfo_send(tree, parms);
444 return smbcli_request_simple_recv(req);
448 /****************************************************************************
449 Set path info (async send)
450 ****************************************************************************/
451 _PUBLIC_ struct smbcli_request *smb_raw_setpathinfo_send(struct smbcli_tree *tree,
452 union smb_setfileinfo *parms)
454 DATA_BLOB blob;
455 TALLOC_CTX *mem_ctx;
456 struct smbcli_request *req;
458 if (parms->generic.level == RAW_SFILEINFO_SETATTR) {
459 return smb_raw_setattr_send(tree, parms);
461 if (parms->generic.level >= RAW_SFILEINFO_GENERIC) {
462 return NULL;
465 mem_ctx = talloc_init("setpathinfo");
466 if (!mem_ctx) return NULL;
468 if (!smb_raw_setinfo_backend(tree, mem_ctx, parms, &blob)) {
469 talloc_free(mem_ctx);
470 return NULL;
473 /* send request and process the output */
474 req = smb_raw_setpathinfo_blob_send(tree,
475 mem_ctx,
476 parms->generic.in.file.path,
477 parms->generic.level,
478 &blob);
480 talloc_free(mem_ctx);
481 return req;
484 /****************************************************************************
485 Set path info (sync interface)
486 ****************************************************************************/
487 _PUBLIC_ NTSTATUS smb_raw_setpathinfo(struct smbcli_tree *tree,
488 union smb_setfileinfo *parms)
490 struct smbcli_request *req = smb_raw_setpathinfo_send(tree, parms);
491 return smbcli_request_simple_recv(req);