source: trunk/server/source3/libsmb/clifile.c@ 902

Last change on this file since 902 was 831, checked in by Silvan Scherrer, 12 years ago

Samba 3.6: remove some @todo

File size: 132.2 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
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 3 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, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "system/filesys.h"
23#include "libsmb/libsmb.h"
24#include "../lib/util/tevent_ntstatus.h"
25#include "async_smb.h"
26#include "libsmb/clirap.h"
27#include "trans2.h"
28#include "ntioctl.h"
29
30/***********************************************************
31 Common function for pushing stings, used by smb_bytes_push_str()
32 and trans_bytes_push_str(). Only difference is the align_odd
33 parameter setting.
34***********************************************************/
35
36static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
37 const char *str, size_t str_len,
38 bool align_odd,
39 size_t *pconverted_size)
40{
41 size_t buflen;
42 char *converted;
43 size_t converted_size;
44
45 if (buf == NULL) {
46 return NULL;
47 }
48
49 buflen = talloc_get_size(buf);
50
51 if (align_odd && ucs2 && (buflen % 2 == 0)) {
52 /*
53 * We're pushing into an SMB buffer, align odd
54 */
55 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
56 if (buf == NULL) {
57 return NULL;
58 }
59 buf[buflen] = '\0';
60 buflen += 1;
61 }
62
63 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
64 ucs2 ? CH_UTF16LE : CH_DOS,
65 str, str_len, &converted,
66 &converted_size, true)) {
67 return NULL;
68 }
69
70 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
71 buflen + converted_size);
72 if (buf == NULL) {
73 TALLOC_FREE(converted);
74 return NULL;
75 }
76
77 memcpy(buf + buflen, converted, converted_size);
78
79 TALLOC_FREE(converted);
80
81 if (pconverted_size) {
82 *pconverted_size = converted_size;
83 }
84
85 return buf;
86}
87
88/***********************************************************
89 Push a string into an SMB buffer, with odd byte alignment
90 if it's a UCS2 string.
91***********************************************************/
92
93uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
94 const char *str, size_t str_len,
95 size_t *pconverted_size)
96{
97 return internal_bytes_push_str(buf, ucs2, str, str_len,
98 true, pconverted_size);
99}
100
101uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
102 const uint8_t *bytes, size_t num_bytes)
103{
104 size_t buflen;
105
106 if (buf == NULL) {
107 return NULL;
108 }
109 buflen = talloc_get_size(buf);
110
111 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
112 buflen + 1 + num_bytes);
113 if (buf == NULL) {
114 return NULL;
115 }
116 buf[buflen] = prefix;
117 memcpy(&buf[buflen+1], bytes, num_bytes);
118 return buf;
119}
120
121/***********************************************************
122 Same as smb_bytes_push_str(), but without the odd byte
123 align for ucs2 (we're pushing into a param or data block).
124 static for now, although this will probably change when
125 other modules use async trans calls.
126***********************************************************/
127
128static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
129 const char *str, size_t str_len,
130 size_t *pconverted_size)
131{
132 return internal_bytes_push_str(buf, ucs2, str, str_len,
133 false, pconverted_size);
134}
135
136struct cli_setpathinfo_state {
137 uint16_t setup;
138 uint8_t *param;
139};
140
141static void cli_setpathinfo_done(struct tevent_req *subreq);
142
143struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
144 struct tevent_context *ev,
145 struct cli_state *cli,
146 uint16_t level,
147 const char *path,
148 uint8_t *data,
149 size_t data_len)
150{
151 struct tevent_req *req, *subreq;
152 struct cli_setpathinfo_state *state;
153
154 req = tevent_req_create(mem_ctx, &state,
155 struct cli_setpathinfo_state);
156 if (req == NULL) {
157 return NULL;
158 }
159
160 /* Setup setup word. */
161 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
162
163 /* Setup param array. */
164 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
165 if (tevent_req_nomem(state->param, req)) {
166 return tevent_req_post(req, ev);
167 }
168 SSVAL(state->param, 0, level);
169
170 state->param = trans2_bytes_push_str(
171 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
172 if (tevent_req_nomem(state->param, req)) {
173 return tevent_req_post(req, ev);
174 }
175
176 subreq = cli_trans_send(
177 state, /* mem ctx. */
178 ev, /* event ctx. */
179 cli, /* cli_state. */
180 SMBtrans2, /* cmd. */
181 NULL, /* pipe name. */
182 -1, /* fid. */
183 0, /* function. */
184 0, /* flags. */
185 &state->setup, /* setup. */
186 1, /* num setup uint16_t words. */
187 0, /* max returned setup. */
188 state->param, /* param. */
189 talloc_get_size(state->param), /* num param. */
190 2, /* max returned param. */
191 data, /* data. */
192 data_len, /* num data. */
193 0); /* max returned data. */
194
195 if (tevent_req_nomem(subreq, req)) {
196 return tevent_req_post(req, ev);
197 }
198 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
199 return req;
200}
201
202static void cli_setpathinfo_done(struct tevent_req *subreq)
203{
204 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
205 NULL, 0, NULL, NULL, 0, NULL);
206 tevent_req_simple_finish_ntstatus(subreq, status);
207}
208
209NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
210{
211 return tevent_req_simple_recv_ntstatus(req);
212}
213
214NTSTATUS cli_setpathinfo(struct cli_state *cli,
215 uint16_t level,
216 const char *path,
217 uint8_t *data,
218 size_t data_len)
219{
220 TALLOC_CTX *frame = talloc_stackframe();
221 struct tevent_context *ev;
222 struct tevent_req *req;
223 NTSTATUS status = NT_STATUS_NO_MEMORY;
224
225 if (cli_has_async_calls(cli)) {
226 /*
227 * Can't use sync call while an async call is in flight
228 */
229 status = NT_STATUS_INVALID_PARAMETER;
230 goto fail;
231 }
232 ev = tevent_context_init(frame);
233 if (ev == NULL) {
234 goto fail;
235 }
236 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
237 if (req == NULL) {
238 goto fail;
239 }
240 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241 goto fail;
242 }
243 status = cli_setpathinfo_recv(req);
244 fail:
245 TALLOC_FREE(frame);
246 return status;
247}
248
249/****************************************************************************
250 Hard/Symlink a file (UNIX extensions).
251 Creates new name (sym)linked to oldname.
252****************************************************************************/
253
254struct cli_posix_link_internal_state {
255 uint8_t *data;
256};
257
258static void cli_posix_link_internal_done(struct tevent_req *subreq);
259
260static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
261 struct event_context *ev,
262 struct cli_state *cli,
263 uint16_t level,
264 const char *oldname,
265 const char *newname)
266{
267 struct tevent_req *req = NULL, *subreq = NULL;
268 struct cli_posix_link_internal_state *state = NULL;
269
270 req = tevent_req_create(mem_ctx, &state,
271 struct cli_posix_link_internal_state);
272 if (req == NULL) {
273 return NULL;
274 }
275
276 /* Setup data array. */
277 state->data = talloc_array(state, uint8_t, 0);
278 if (tevent_req_nomem(state->data, req)) {
279 return tevent_req_post(req, ev);
280 }
281 state->data = trans2_bytes_push_str(
282 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
283
284 subreq = cli_setpathinfo_send(
285 state, ev, cli, level, newname,
286 state->data, talloc_get_size(state->data));
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
289 }
290 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
291 return req;
292}
293
294static void cli_posix_link_internal_done(struct tevent_req *subreq)
295{
296 NTSTATUS status = cli_setpathinfo_recv(subreq);
297 tevent_req_simple_finish_ntstatus(subreq, status);
298}
299
300/****************************************************************************
301 Symlink a file (UNIX extensions).
302****************************************************************************/
303
304struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
305 struct event_context *ev,
306 struct cli_state *cli,
307 const char *oldname,
308 const char *newname)
309{
310 return cli_posix_link_internal_send(
311 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
312}
313
314NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
315{
316 return tevent_req_simple_recv_ntstatus(req);
317}
318
319NTSTATUS cli_posix_symlink(struct cli_state *cli,
320 const char *oldname,
321 const char *newname)
322{
323 TALLOC_CTX *frame = talloc_stackframe();
324 struct event_context *ev = NULL;
325 struct tevent_req *req = NULL;
326 NTSTATUS status = NT_STATUS_OK;
327
328 if (cli_has_async_calls(cli)) {
329 /*
330 * Can't use sync call while an async call is in flight
331 */
332 status = NT_STATUS_INVALID_PARAMETER;
333 goto fail;
334 }
335
336 ev = event_context_init(frame);
337 if (ev == NULL) {
338 status = NT_STATUS_NO_MEMORY;
339 goto fail;
340 }
341
342 req = cli_posix_symlink_send(frame,
343 ev,
344 cli,
345 oldname,
346 newname);
347 if (req == NULL) {
348 status = NT_STATUS_NO_MEMORY;
349 goto fail;
350 }
351
352 if (!tevent_req_poll(req, ev)) {
353 status = map_nt_error_from_unix(errno);
354 goto fail;
355 }
356
357 status = cli_posix_symlink_recv(req);
358
359 fail:
360 TALLOC_FREE(frame);
361 return status;
362}
363
364/****************************************************************************
365 Read a POSIX symlink.
366****************************************************************************/
367
368struct readlink_state {
369 uint8_t *data;
370 uint32_t num_data;
371};
372
373static void cli_posix_readlink_done(struct tevent_req *subreq);
374
375struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
376 struct event_context *ev,
377 struct cli_state *cli,
378 const char *fname,
379 size_t len)
380{
381 struct tevent_req *req = NULL, *subreq = NULL;
382 struct readlink_state *state = NULL;
383 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
384
385 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
386 if (req == NULL) {
387 return NULL;
388 }
389
390 /*
391 * Len is in bytes, we need it in UCS2 units.
392 */
393 if ((2*len < len) || (maxbytelen < len)) {
394 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
395 return tevent_req_post(req, ev);
396 }
397
398 subreq = cli_qpathinfo_send(state, ev, cli, fname,
399 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
400 if (tevent_req_nomem(subreq, req)) {
401 return tevent_req_post(req, ev);
402 }
403 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
404 return req;
405}
406
407static void cli_posix_readlink_done(struct tevent_req *subreq)
408{
409 struct tevent_req *req = tevent_req_callback_data(
410 subreq, struct tevent_req);
411 struct readlink_state *state = tevent_req_data(
412 req, struct readlink_state);
413 NTSTATUS status;
414
415 status = cli_qpathinfo_recv(subreq, state, &state->data,
416 &state->num_data);
417 TALLOC_FREE(subreq);
418 if (tevent_req_nterror(req, status)) {
419 return;
420 }
421 /*
422 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
423 */
424 if (state->data[state->num_data-1] != '\0') {
425 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
426 return;
427 }
428 tevent_req_done(req);
429}
430
431NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
432 char *retpath, size_t len)
433{
434 NTSTATUS status;
435 char *converted = NULL;
436 size_t converted_size = 0;
437 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
438
439 if (tevent_req_is_nterror(req, &status)) {
440 return status;
441 }
442 /* The returned data is a pushed string, not raw data. */
443 if (!convert_string_talloc(state,
444 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
445 CH_UNIX,
446 state->data,
447 state->num_data,
448 &converted,
449 &converted_size,
450 true)) {
451 return NT_STATUS_NO_MEMORY;
452 }
453
454 len = MIN(len,converted_size);
455 if (len == 0) {
456 return NT_STATUS_DATA_ERROR;
457 }
458 memcpy(retpath, converted, len);
459 return NT_STATUS_OK;
460}
461
462NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
463 char *linkpath, size_t len)
464{
465 TALLOC_CTX *frame = talloc_stackframe();
466 struct event_context *ev = NULL;
467 struct tevent_req *req = NULL;
468 NTSTATUS status = NT_STATUS_OK;
469
470 if (cli_has_async_calls(cli)) {
471 /*
472 * Can't use sync call while an async call is in flight
473 */
474 status = NT_STATUS_INVALID_PARAMETER;
475 goto fail;
476 }
477
478 ev = event_context_init(frame);
479 if (ev == NULL) {
480 status = NT_STATUS_NO_MEMORY;
481 goto fail;
482 }
483
484 req = cli_posix_readlink_send(frame,
485 ev,
486 cli,
487 fname,
488 len);
489 if (req == NULL) {
490 status = NT_STATUS_NO_MEMORY;
491 goto fail;
492 }
493
494 if (!tevent_req_poll(req, ev)) {
495 status = map_nt_error_from_unix(errno);
496 goto fail;
497 }
498
499 status = cli_posix_readlink_recv(req, cli, linkpath, len);
500
501 fail:
502 TALLOC_FREE(frame);
503 return status;
504}
505
506/****************************************************************************
507 Hard link a file (UNIX extensions).
508****************************************************************************/
509
510struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
511 struct event_context *ev,
512 struct cli_state *cli,
513 const char *oldname,
514 const char *newname)
515{
516 return cli_posix_link_internal_send(
517 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
518}
519
520NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
521{
522 return tevent_req_simple_recv_ntstatus(req);
523}
524
525NTSTATUS cli_posix_hardlink(struct cli_state *cli,
526 const char *oldname,
527 const char *newname)
528{
529 TALLOC_CTX *frame = talloc_stackframe();
530 struct event_context *ev = NULL;
531 struct tevent_req *req = NULL;
532 NTSTATUS status = NT_STATUS_OK;
533
534 if (cli_has_async_calls(cli)) {
535 /*
536 * Can't use sync call while an async call is in flight
537 */
538 status = NT_STATUS_INVALID_PARAMETER;
539 goto fail;
540 }
541
542 ev = event_context_init(frame);
543 if (ev == NULL) {
544 status = NT_STATUS_NO_MEMORY;
545 goto fail;
546 }
547
548 req = cli_posix_hardlink_send(frame,
549 ev,
550 cli,
551 oldname,
552 newname);
553 if (req == NULL) {
554 status = NT_STATUS_NO_MEMORY;
555 goto fail;
556 }
557
558 if (!tevent_req_poll(req, ev)) {
559 status = map_nt_error_from_unix(errno);
560 goto fail;
561 }
562
563 status = cli_posix_hardlink_recv(req);
564
565 fail:
566 TALLOC_FREE(frame);
567 return status;
568}
569
570/****************************************************************************
571 Map standard UNIX permissions onto wire representations.
572****************************************************************************/
573
574uint32_t unix_perms_to_wire(mode_t perms)
575{
576 unsigned int ret = 0;
577
578 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
579 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
580 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
581 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
582 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
583 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
584 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
585 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
586 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
587#ifdef S_ISVTX
588 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
589#endif
590#ifdef S_ISGID
591 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
592#endif
593#ifdef S_ISUID
594 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
595#endif
596 return ret;
597}
598
599/****************************************************************************
600 Map wire permissions to standard UNIX.
601****************************************************************************/
602
603mode_t wire_perms_to_unix(uint32_t perms)
604{
605 mode_t ret = (mode_t)0;
606
607 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
608 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
609 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
610 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
611 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
612 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
613 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
614 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
615 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
616#ifdef S_ISVTX
617 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
618#endif
619#ifdef S_ISGID
620 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
621#endif
622#ifdef S_ISUID
623 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
624#endif
625 return ret;
626}
627
628/****************************************************************************
629 Return the file type from the wire filetype for UNIX extensions.
630****************************************************************************/
631
632static mode_t unix_filetype_from_wire(uint32_t wire_type)
633{
634 switch (wire_type) {
635 case UNIX_TYPE_FILE:
636 return S_IFREG;
637 case UNIX_TYPE_DIR:
638 return S_IFDIR;
639#ifdef S_IFLNK
640 case UNIX_TYPE_SYMLINK:
641 return S_IFLNK;
642#endif
643#ifdef S_IFCHR
644 case UNIX_TYPE_CHARDEV:
645 return S_IFCHR;
646#endif
647#ifdef S_IFBLK
648 case UNIX_TYPE_BLKDEV:
649 return S_IFBLK;
650#endif
651#ifdef S_IFIFO
652 case UNIX_TYPE_FIFO:
653 return S_IFIFO;
654#endif
655#ifdef S_IFSOCK
656 case UNIX_TYPE_SOCKET:
657 return S_IFSOCK;
658#endif
659 default:
660 return (mode_t)0;
661 }
662}
663
664/****************************************************************************
665 Do a POSIX getfacl (UNIX extensions).
666****************************************************************************/
667
668struct getfacl_state {
669 uint32_t num_data;
670 uint8_t *data;
671};
672
673static void cli_posix_getfacl_done(struct tevent_req *subreq);
674
675struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
676 struct event_context *ev,
677 struct cli_state *cli,
678 const char *fname)
679{
680 struct tevent_req *req = NULL, *subreq = NULL;
681 struct getfacl_state *state = NULL;
682
683 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
684 if (req == NULL) {
685 return NULL;
686 }
687 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
688 0, cli->max_xmit);
689 if (tevent_req_nomem(subreq, req)) {
690 return tevent_req_post(req, ev);
691 }
692 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
693 return req;
694}
695
696static void cli_posix_getfacl_done(struct tevent_req *subreq)
697{
698 struct tevent_req *req = tevent_req_callback_data(
699 subreq, struct tevent_req);
700 struct getfacl_state *state = tevent_req_data(
701 req, struct getfacl_state);
702 NTSTATUS status;
703
704 status = cli_qpathinfo_recv(subreq, state, &state->data,
705 &state->num_data);
706 TALLOC_FREE(subreq);
707 if (tevent_req_nterror(req, status)) {
708 return;
709 }
710 tevent_req_done(req);
711}
712
713NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
714 TALLOC_CTX *mem_ctx,
715 size_t *prb_size,
716 char **retbuf)
717{
718 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
719 NTSTATUS status;
720
721 if (tevent_req_is_nterror(req, &status)) {
722 return status;
723 }
724 *prb_size = (size_t)state->num_data;
725 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
726 return NT_STATUS_OK;
727}
728
729NTSTATUS cli_posix_getfacl(struct cli_state *cli,
730 const char *fname,
731 TALLOC_CTX *mem_ctx,
732 size_t *prb_size,
733 char **retbuf)
734{
735 TALLOC_CTX *frame = talloc_stackframe();
736 struct event_context *ev = NULL;
737 struct tevent_req *req = NULL;
738 NTSTATUS status = NT_STATUS_OK;
739
740 if (cli_has_async_calls(cli)) {
741 /*
742 * Can't use sync call while an async call is in flight
743 */
744 status = NT_STATUS_INVALID_PARAMETER;
745 goto fail;
746 }
747
748 ev = event_context_init(frame);
749 if (ev == NULL) {
750 status = NT_STATUS_NO_MEMORY;
751 goto fail;
752 }
753
754 req = cli_posix_getfacl_send(frame,
755 ev,
756 cli,
757 fname);
758 if (req == NULL) {
759 status = NT_STATUS_NO_MEMORY;
760 goto fail;
761 }
762
763 if (!tevent_req_poll(req, ev)) {
764 status = map_nt_error_from_unix(errno);
765 goto fail;
766 }
767
768 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
769
770 fail:
771 TALLOC_FREE(frame);
772 return status;
773}
774
775/****************************************************************************
776 Stat a file (UNIX extensions).
777****************************************************************************/
778
779struct stat_state {
780 uint32_t num_data;
781 uint8_t *data;
782};
783
784static void cli_posix_stat_done(struct tevent_req *subreq);
785
786struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
787 struct event_context *ev,
788 struct cli_state *cli,
789 const char *fname)
790{
791 struct tevent_req *req = NULL, *subreq = NULL;
792 struct stat_state *state = NULL;
793
794 req = tevent_req_create(mem_ctx, &state, struct stat_state);
795 if (req == NULL) {
796 return NULL;
797 }
798 subreq = cli_qpathinfo_send(state, ev, cli, fname,
799 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
800 if (tevent_req_nomem(subreq, req)) {
801 return tevent_req_post(req, ev);
802 }
803 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
804 return req;
805}
806
807static void cli_posix_stat_done(struct tevent_req *subreq)
808{
809 struct tevent_req *req = tevent_req_callback_data(
810 subreq, struct tevent_req);
811 struct stat_state *state = tevent_req_data(req, struct stat_state);
812 NTSTATUS status;
813
814 status = cli_qpathinfo_recv(subreq, state, &state->data,
815 &state->num_data);
816 TALLOC_FREE(subreq);
817 if (tevent_req_nterror(req, status)) {
818 return;
819 }
820 tevent_req_done(req);
821}
822
823NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
824 SMB_STRUCT_STAT *sbuf)
825{
826 struct stat_state *state = tevent_req_data(req, struct stat_state);
827 NTSTATUS status;
828
829 if (tevent_req_is_nterror(req, &status)) {
830 return status;
831 }
832
833 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
834 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
835#if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
836 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
837#else
838 /* assume 512 byte blocks */
839 sbuf->st_ex_blocks /= 512;
840#endif
841 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
842 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
843 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
844
845 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
846 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
847 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
848#if defined(HAVE_MAKEDEV)
849 {
850 uint32_t dev_major = IVAL(state->data,60);
851 uint32_t dev_minor = IVAL(state->data,68);
852 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
853 }
854#endif
855 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
856 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
857 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
858
859 return NT_STATUS_OK;
860}
861
862NTSTATUS cli_posix_stat(struct cli_state *cli,
863 const char *fname,
864 SMB_STRUCT_STAT *sbuf)
865{
866 TALLOC_CTX *frame = talloc_stackframe();
867 struct event_context *ev = NULL;
868 struct tevent_req *req = NULL;
869 NTSTATUS status = NT_STATUS_OK;
870
871 if (cli_has_async_calls(cli)) {
872 /*
873 * Can't use sync call while an async call is in flight
874 */
875 status = NT_STATUS_INVALID_PARAMETER;
876 goto fail;
877 }
878
879 ev = event_context_init(frame);
880 if (ev == NULL) {
881 status = NT_STATUS_NO_MEMORY;
882 goto fail;
883 }
884
885 req = cli_posix_stat_send(frame,
886 ev,
887 cli,
888 fname);
889 if (req == NULL) {
890 status = NT_STATUS_NO_MEMORY;
891 goto fail;
892 }
893
894 if (!tevent_req_poll(req, ev)) {
895 status = map_nt_error_from_unix(errno);
896 goto fail;
897 }
898
899 status = cli_posix_stat_recv(req, sbuf);
900
901 fail:
902 TALLOC_FREE(frame);
903 return status;
904}
905
906/****************************************************************************
907 Chmod or chown a file internal (UNIX extensions).
908****************************************************************************/
909
910struct cli_posix_chown_chmod_internal_state {
911 uint8_t data[100];
912};
913
914static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
915
916static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
917 struct event_context *ev,
918 struct cli_state *cli,
919 const char *fname,
920 uint32_t mode,
921 uint32_t uid,
922 uint32_t gid)
923{
924 struct tevent_req *req = NULL, *subreq = NULL;
925 struct cli_posix_chown_chmod_internal_state *state = NULL;
926
927 req = tevent_req_create(mem_ctx, &state,
928 struct cli_posix_chown_chmod_internal_state);
929 if (req == NULL) {
930 return NULL;
931 }
932
933 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
934 memset(&state->data[40], '\0', 60);
935 SIVAL(state->data,40,uid);
936 SIVAL(state->data,48,gid);
937 SIVAL(state->data,84,mode);
938
939 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
940 fname, state->data, sizeof(state->data));
941 if (tevent_req_nomem(subreq, req)) {
942 return tevent_req_post(req, ev);
943 }
944 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
945 req);
946 return req;
947}
948
949static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
950{
951 NTSTATUS status = cli_setpathinfo_recv(subreq);
952 tevent_req_simple_finish_ntstatus(subreq, status);
953}
954
955/****************************************************************************
956 chmod a file (UNIX extensions).
957****************************************************************************/
958
959struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
960 struct event_context *ev,
961 struct cli_state *cli,
962 const char *fname,
963 mode_t mode)
964{
965 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
966 fname,
967 unix_perms_to_wire(mode),
968 SMB_UID_NO_CHANGE,
969 SMB_GID_NO_CHANGE);
970}
971
972NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
973{
974 return tevent_req_simple_recv_ntstatus(req);
975}
976
977NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
978{
979 TALLOC_CTX *frame = talloc_stackframe();
980 struct event_context *ev = NULL;
981 struct tevent_req *req = NULL;
982 NTSTATUS status = NT_STATUS_OK;
983
984 if (cli_has_async_calls(cli)) {
985 /*
986 * Can't use sync call while an async call is in flight
987 */
988 status = NT_STATUS_INVALID_PARAMETER;
989 goto fail;
990 }
991
992 ev = event_context_init(frame);
993 if (ev == NULL) {
994 status = NT_STATUS_NO_MEMORY;
995 goto fail;
996 }
997
998 req = cli_posix_chmod_send(frame,
999 ev,
1000 cli,
1001 fname,
1002 mode);
1003 if (req == NULL) {
1004 status = NT_STATUS_NO_MEMORY;
1005 goto fail;
1006 }
1007
1008 if (!tevent_req_poll(req, ev)) {
1009 status = map_nt_error_from_unix(errno);
1010 goto fail;
1011 }
1012
1013 status = cli_posix_chmod_recv(req);
1014
1015 fail:
1016 TALLOC_FREE(frame);
1017 return status;
1018}
1019
1020/****************************************************************************
1021 chown a file (UNIX extensions).
1022****************************************************************************/
1023
1024struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1025 struct event_context *ev,
1026 struct cli_state *cli,
1027 const char *fname,
1028 uid_t uid,
1029 gid_t gid)
1030{
1031 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1032 fname,
1033 SMB_MODE_NO_CHANGE,
1034 (uint32_t)uid,
1035 (uint32_t)gid);
1036}
1037
1038NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1039{
1040 return tevent_req_simple_recv_ntstatus(req);
1041}
1042
1043NTSTATUS cli_posix_chown(struct cli_state *cli,
1044 const char *fname,
1045 uid_t uid,
1046 gid_t gid)
1047{
1048 TALLOC_CTX *frame = talloc_stackframe();
1049 struct event_context *ev = NULL;
1050 struct tevent_req *req = NULL;
1051 NTSTATUS status = NT_STATUS_OK;
1052
1053 if (cli_has_async_calls(cli)) {
1054 /*
1055 * Can't use sync call while an async call is in flight
1056 */
1057 status = NT_STATUS_INVALID_PARAMETER;
1058 goto fail;
1059 }
1060
1061 ev = event_context_init(frame);
1062 if (ev == NULL) {
1063 status = NT_STATUS_NO_MEMORY;
1064 goto fail;
1065 }
1066
1067 req = cli_posix_chown_send(frame,
1068 ev,
1069 cli,
1070 fname,
1071 uid,
1072 gid);
1073 if (req == NULL) {
1074 status = NT_STATUS_NO_MEMORY;
1075 goto fail;
1076 }
1077
1078 if (!tevent_req_poll(req, ev)) {
1079 status = map_nt_error_from_unix(errno);
1080 goto fail;
1081 }
1082
1083 status = cli_posix_chown_recv(req);
1084
1085 fail:
1086 TALLOC_FREE(frame);
1087 return status;
1088}
1089
1090/****************************************************************************
1091 Rename a file.
1092****************************************************************************/
1093
1094static void cli_rename_done(struct tevent_req *subreq);
1095
1096struct cli_rename_state {
1097 uint16_t vwv[1];
1098};
1099
1100struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1101 struct event_context *ev,
1102 struct cli_state *cli,
1103 const char *fname_src,
1104 const char *fname_dst)
1105{
1106 struct tevent_req *req = NULL, *subreq = NULL;
1107 struct cli_rename_state *state = NULL;
1108 uint8_t additional_flags = 0;
1109 uint8_t *bytes = NULL;
1110
1111 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1112 if (req == NULL) {
1113 return NULL;
1114 }
1115
1116 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1117
1118 bytes = talloc_array(state, uint8_t, 1);
1119 if (tevent_req_nomem(bytes, req)) {
1120 return tevent_req_post(req, ev);
1121 }
1122 bytes[0] = 4;
1123 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1124 strlen(fname_src)+1, NULL);
1125 if (tevent_req_nomem(bytes, req)) {
1126 return tevent_req_post(req, ev);
1127 }
1128
1129 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1130 talloc_get_size(bytes)+1);
1131 if (tevent_req_nomem(bytes, req)) {
1132 return tevent_req_post(req, ev);
1133 }
1134
1135 bytes[talloc_get_size(bytes)-1] = 4;
1136 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1137 strlen(fname_dst)+1, NULL);
1138 if (tevent_req_nomem(bytes, req)) {
1139 return tevent_req_post(req, ev);
1140 }
1141
1142 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1143 1, state->vwv, talloc_get_size(bytes), bytes);
1144 if (tevent_req_nomem(subreq, req)) {
1145 return tevent_req_post(req, ev);
1146 }
1147 tevent_req_set_callback(subreq, cli_rename_done, req);
1148 return req;
1149}
1150
1151static void cli_rename_done(struct tevent_req *subreq)
1152{
1153 struct tevent_req *req = tevent_req_callback_data(
1154 subreq, struct tevent_req);
1155 NTSTATUS status;
1156
1157 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1158 TALLOC_FREE(subreq);
1159 if (tevent_req_nterror(req, status)) {
1160 return;
1161 }
1162 tevent_req_done(req);
1163}
1164
1165NTSTATUS cli_rename_recv(struct tevent_req *req)
1166{
1167 return tevent_req_simple_recv_ntstatus(req);
1168}
1169
1170NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1171{
1172 TALLOC_CTX *frame = talloc_stackframe();
1173 struct event_context *ev;
1174 struct tevent_req *req;
1175 NTSTATUS status = NT_STATUS_OK;
1176
1177 if (cli_has_async_calls(cli)) {
1178 /*
1179 * Can't use sync call while an async call is in flight
1180 */
1181 status = NT_STATUS_INVALID_PARAMETER;
1182 goto fail;
1183 }
1184
1185 ev = event_context_init(frame);
1186 if (ev == NULL) {
1187 status = NT_STATUS_NO_MEMORY;
1188 goto fail;
1189 }
1190
1191 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1192 if (req == NULL) {
1193 status = NT_STATUS_NO_MEMORY;
1194 goto fail;
1195 }
1196
1197 if (!tevent_req_poll(req, ev)) {
1198 status = map_nt_error_from_unix(errno);
1199 goto fail;
1200 }
1201
1202 status = cli_rename_recv(req);
1203
1204 fail:
1205 TALLOC_FREE(frame);
1206 return status;
1207}
1208
1209/****************************************************************************
1210 NT Rename a file.
1211****************************************************************************/
1212
1213static void cli_ntrename_internal_done(struct tevent_req *subreq);
1214
1215struct cli_ntrename_internal_state {
1216 uint16_t vwv[4];
1217};
1218
1219static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1220 struct event_context *ev,
1221 struct cli_state *cli,
1222 const char *fname_src,
1223 const char *fname_dst,
1224 uint16_t rename_flag)
1225{
1226 struct tevent_req *req = NULL, *subreq = NULL;
1227 struct cli_ntrename_internal_state *state = NULL;
1228 uint8_t additional_flags = 0;
1229 uint8_t *bytes = NULL;
1230
1231 req = tevent_req_create(mem_ctx, &state,
1232 struct cli_ntrename_internal_state);
1233 if (req == NULL) {
1234 return NULL;
1235 }
1236
1237 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1238 SSVAL(state->vwv+1, 0, rename_flag);
1239
1240 bytes = talloc_array(state, uint8_t, 1);
1241 if (tevent_req_nomem(bytes, req)) {
1242 return tevent_req_post(req, ev);
1243 }
1244 bytes[0] = 4;
1245 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1246 strlen(fname_src)+1, NULL);
1247 if (tevent_req_nomem(bytes, req)) {
1248 return tevent_req_post(req, ev);
1249 }
1250
1251 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1252 talloc_get_size(bytes)+1);
1253 if (tevent_req_nomem(bytes, req)) {
1254 return tevent_req_post(req, ev);
1255 }
1256
1257 bytes[talloc_get_size(bytes)-1] = 4;
1258 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1259 strlen(fname_dst)+1, NULL);
1260 if (tevent_req_nomem(bytes, req)) {
1261 return tevent_req_post(req, ev);
1262 }
1263
1264 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1265 4, state->vwv, talloc_get_size(bytes), bytes);
1266 if (tevent_req_nomem(subreq, req)) {
1267 return tevent_req_post(req, ev);
1268 }
1269 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1270 return req;
1271}
1272
1273static void cli_ntrename_internal_done(struct tevent_req *subreq)
1274{
1275 struct tevent_req *req = tevent_req_callback_data(
1276 subreq, struct tevent_req);
1277 NTSTATUS status;
1278
1279 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1280 TALLOC_FREE(subreq);
1281 if (tevent_req_nterror(req, status)) {
1282 return;
1283 }
1284 tevent_req_done(req);
1285}
1286
1287static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1288{
1289 return tevent_req_simple_recv_ntstatus(req);
1290}
1291
1292struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1293 struct event_context *ev,
1294 struct cli_state *cli,
1295 const char *fname_src,
1296 const char *fname_dst)
1297{
1298 return cli_ntrename_internal_send(mem_ctx,
1299 ev,
1300 cli,
1301 fname_src,
1302 fname_dst,
1303 RENAME_FLAG_RENAME);
1304}
1305
1306NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1307{
1308 return cli_ntrename_internal_recv(req);
1309}
1310
1311NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1312{
1313 TALLOC_CTX *frame = talloc_stackframe();
1314 struct event_context *ev;
1315 struct tevent_req *req;
1316 NTSTATUS status = NT_STATUS_OK;
1317
1318 if (cli_has_async_calls(cli)) {
1319 /*
1320 * Can't use sync call while an async call is in flight
1321 */
1322 status = NT_STATUS_INVALID_PARAMETER;
1323 goto fail;
1324 }
1325
1326 ev = event_context_init(frame);
1327 if (ev == NULL) {
1328 status = NT_STATUS_NO_MEMORY;
1329 goto fail;
1330 }
1331
1332 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1333 if (req == NULL) {
1334 status = NT_STATUS_NO_MEMORY;
1335 goto fail;
1336 }
1337
1338 if (!tevent_req_poll(req, ev)) {
1339 status = map_nt_error_from_unix(errno);
1340 goto fail;
1341 }
1342
1343 status = cli_ntrename_recv(req);
1344
1345 fail:
1346 TALLOC_FREE(frame);
1347 return status;
1348}
1349
1350/****************************************************************************
1351 NT hardlink a file.
1352****************************************************************************/
1353
1354struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1355 struct event_context *ev,
1356 struct cli_state *cli,
1357 const char *fname_src,
1358 const char *fname_dst)
1359{
1360 return cli_ntrename_internal_send(mem_ctx,
1361 ev,
1362 cli,
1363 fname_src,
1364 fname_dst,
1365 RENAME_FLAG_HARD_LINK);
1366}
1367
1368NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1369{
1370 return cli_ntrename_internal_recv(req);
1371}
1372
1373NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1374{
1375 TALLOC_CTX *frame = talloc_stackframe();
1376 struct event_context *ev;
1377 struct tevent_req *req;
1378 NTSTATUS status = NT_STATUS_OK;
1379
1380 if (cli_has_async_calls(cli)) {
1381 /*
1382 * Can't use sync call while an async call is in flight
1383 */
1384 status = NT_STATUS_INVALID_PARAMETER;
1385 goto fail;
1386 }
1387
1388 ev = event_context_init(frame);
1389 if (ev == NULL) {
1390 status = NT_STATUS_NO_MEMORY;
1391 goto fail;
1392 }
1393
1394 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1395 if (req == NULL) {
1396 status = NT_STATUS_NO_MEMORY;
1397 goto fail;
1398 }
1399
1400 if (!tevent_req_poll(req, ev)) {
1401 status = map_nt_error_from_unix(errno);
1402 goto fail;
1403 }
1404
1405 status = cli_nt_hardlink_recv(req);
1406
1407 fail:
1408 TALLOC_FREE(frame);
1409 return status;
1410}
1411
1412/****************************************************************************
1413 Delete a file.
1414****************************************************************************/
1415
1416static void cli_unlink_done(struct tevent_req *subreq);
1417
1418struct cli_unlink_state {
1419 uint16_t vwv[1];
1420};
1421
1422struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1423 struct event_context *ev,
1424 struct cli_state *cli,
1425 const char *fname,
1426 uint16_t mayhave_attrs)
1427{
1428 struct tevent_req *req = NULL, *subreq = NULL;
1429 struct cli_unlink_state *state = NULL;
1430 uint8_t additional_flags = 0;
1431 uint8_t *bytes = NULL;
1432
1433 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1434 if (req == NULL) {
1435 return NULL;
1436 }
1437
1438 SSVAL(state->vwv+0, 0, mayhave_attrs);
1439
1440 bytes = talloc_array(state, uint8_t, 1);
1441 if (tevent_req_nomem(bytes, req)) {
1442 return tevent_req_post(req, ev);
1443 }
1444 bytes[0] = 4;
1445 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1446 strlen(fname)+1, NULL);
1447
1448 if (tevent_req_nomem(bytes, req)) {
1449 return tevent_req_post(req, ev);
1450 }
1451
1452 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1453 1, state->vwv, talloc_get_size(bytes), bytes);
1454 if (tevent_req_nomem(subreq, req)) {
1455 return tevent_req_post(req, ev);
1456 }
1457 tevent_req_set_callback(subreq, cli_unlink_done, req);
1458 return req;
1459}
1460
1461static void cli_unlink_done(struct tevent_req *subreq)
1462{
1463 struct tevent_req *req = tevent_req_callback_data(
1464 subreq, struct tevent_req);
1465 NTSTATUS status;
1466
1467 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1468 TALLOC_FREE(subreq);
1469 if (tevent_req_nterror(req, status)) {
1470 return;
1471 }
1472 tevent_req_done(req);
1473}
1474
1475NTSTATUS cli_unlink_recv(struct tevent_req *req)
1476{
1477 return tevent_req_simple_recv_ntstatus(req);
1478}
1479
1480NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1481{
1482 TALLOC_CTX *frame = talloc_stackframe();
1483 struct event_context *ev;
1484 struct tevent_req *req;
1485 NTSTATUS status = NT_STATUS_OK;
1486
1487 if (cli_has_async_calls(cli)) {
1488 /*
1489 * Can't use sync call while an async call is in flight
1490 */
1491 status = NT_STATUS_INVALID_PARAMETER;
1492 goto fail;
1493 }
1494
1495 ev = event_context_init(frame);
1496 if (ev == NULL) {
1497 status = NT_STATUS_NO_MEMORY;
1498 goto fail;
1499 }
1500
1501 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1502 if (req == NULL) {
1503 status = NT_STATUS_NO_MEMORY;
1504 goto fail;
1505 }
1506
1507 if (!tevent_req_poll(req, ev)) {
1508 status = map_nt_error_from_unix(errno);
1509 goto fail;
1510 }
1511
1512 status = cli_unlink_recv(req);
1513
1514 fail:
1515 TALLOC_FREE(frame);
1516 return status;
1517}
1518
1519/****************************************************************************
1520 Create a directory.
1521****************************************************************************/
1522
1523static void cli_mkdir_done(struct tevent_req *subreq);
1524
1525struct cli_mkdir_state {
1526 int dummy;
1527};
1528
1529struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1530 struct event_context *ev,
1531 struct cli_state *cli,
1532 const char *dname)
1533{
1534 struct tevent_req *req = NULL, *subreq = NULL;
1535 struct cli_mkdir_state *state = NULL;
1536 uint8_t additional_flags = 0;
1537 uint8_t *bytes = NULL;
1538
1539 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1540 if (req == NULL) {
1541 return NULL;
1542 }
1543
1544 bytes = talloc_array(state, uint8_t, 1);
1545 if (tevent_req_nomem(bytes, req)) {
1546 return tevent_req_post(req, ev);
1547 }
1548 bytes[0] = 4;
1549 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1550 strlen(dname)+1, NULL);
1551
1552 if (tevent_req_nomem(bytes, req)) {
1553 return tevent_req_post(req, ev);
1554 }
1555
1556 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1557 0, NULL, talloc_get_size(bytes), bytes);
1558 if (tevent_req_nomem(subreq, req)) {
1559 return tevent_req_post(req, ev);
1560 }
1561 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1562 return req;
1563}
1564
1565static void cli_mkdir_done(struct tevent_req *subreq)
1566{
1567 struct tevent_req *req = tevent_req_callback_data(
1568 subreq, struct tevent_req);
1569 NTSTATUS status;
1570
1571 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1572 TALLOC_FREE(subreq);
1573 if (tevent_req_nterror(req, status)) {
1574 return;
1575 }
1576 tevent_req_done(req);
1577}
1578
1579NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1580{
1581 return tevent_req_simple_recv_ntstatus(req);
1582}
1583
1584NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1585{
1586 TALLOC_CTX *frame = talloc_stackframe();
1587 struct event_context *ev;
1588 struct tevent_req *req;
1589 NTSTATUS status = NT_STATUS_OK;
1590
1591 if (cli_has_async_calls(cli)) {
1592 /*
1593 * Can't use sync call while an async call is in flight
1594 */
1595 status = NT_STATUS_INVALID_PARAMETER;
1596 goto fail;
1597 }
1598
1599 ev = event_context_init(frame);
1600 if (ev == NULL) {
1601 status = NT_STATUS_NO_MEMORY;
1602 goto fail;
1603 }
1604
1605 req = cli_mkdir_send(frame, ev, cli, dname);
1606 if (req == NULL) {
1607 status = NT_STATUS_NO_MEMORY;
1608 goto fail;
1609 }
1610
1611 if (!tevent_req_poll(req, ev)) {
1612 status = map_nt_error_from_unix(errno);
1613 goto fail;
1614 }
1615
1616 status = cli_mkdir_recv(req);
1617
1618 fail:
1619 TALLOC_FREE(frame);
1620 return status;
1621}
1622
1623/****************************************************************************
1624 Remove a directory.
1625****************************************************************************/
1626
1627static void cli_rmdir_done(struct tevent_req *subreq);
1628
1629struct cli_rmdir_state {
1630 int dummy;
1631};
1632
1633struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1634 struct event_context *ev,
1635 struct cli_state *cli,
1636 const char *dname)
1637{
1638 struct tevent_req *req = NULL, *subreq = NULL;
1639 struct cli_rmdir_state *state = NULL;
1640 uint8_t additional_flags = 0;
1641 uint8_t *bytes = NULL;
1642
1643 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1644 if (req == NULL) {
1645 return NULL;
1646 }
1647
1648 bytes = talloc_array(state, uint8_t, 1);
1649 if (tevent_req_nomem(bytes, req)) {
1650 return tevent_req_post(req, ev);
1651 }
1652 bytes[0] = 4;
1653 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1654 strlen(dname)+1, NULL);
1655
1656 if (tevent_req_nomem(bytes, req)) {
1657 return tevent_req_post(req, ev);
1658 }
1659
1660 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1661 0, NULL, talloc_get_size(bytes), bytes);
1662 if (tevent_req_nomem(subreq, req)) {
1663 return tevent_req_post(req, ev);
1664 }
1665 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1666 return req;
1667}
1668
1669static void cli_rmdir_done(struct tevent_req *subreq)
1670{
1671 struct tevent_req *req = tevent_req_callback_data(
1672 subreq, struct tevent_req);
1673 NTSTATUS status;
1674
1675 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1676 TALLOC_FREE(subreq);
1677 if (tevent_req_nterror(req, status)) {
1678 return;
1679 }
1680 tevent_req_done(req);
1681}
1682
1683NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1684{
1685 return tevent_req_simple_recv_ntstatus(req);
1686}
1687
1688NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1689{
1690 TALLOC_CTX *frame = talloc_stackframe();
1691 struct event_context *ev;
1692 struct tevent_req *req;
1693 NTSTATUS status = NT_STATUS_OK;
1694
1695 if (cli_has_async_calls(cli)) {
1696 /*
1697 * Can't use sync call while an async call is in flight
1698 */
1699 status = NT_STATUS_INVALID_PARAMETER;
1700 goto fail;
1701 }
1702
1703 ev = event_context_init(frame);
1704 if (ev == NULL) {
1705 status = NT_STATUS_NO_MEMORY;
1706 goto fail;
1707 }
1708
1709 req = cli_rmdir_send(frame, ev, cli, dname);
1710 if (req == NULL) {
1711 status = NT_STATUS_NO_MEMORY;
1712 goto fail;
1713 }
1714
1715 if (!tevent_req_poll(req, ev)) {
1716 status = map_nt_error_from_unix(errno);
1717 goto fail;
1718 }
1719
1720 status = cli_rmdir_recv(req);
1721
1722 fail:
1723 TALLOC_FREE(frame);
1724 return status;
1725}
1726
1727/****************************************************************************
1728 Set or clear the delete on close flag.
1729****************************************************************************/
1730
1731struct doc_state {
1732 uint16_t setup;
1733 uint8_t param[6];
1734 uint8_t data[1];
1735};
1736
1737static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1738{
1739 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1740 NULL, 0, NULL, NULL, 0, NULL);
1741 tevent_req_simple_finish_ntstatus(subreq, status);
1742}
1743
1744struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1745 struct event_context *ev,
1746 struct cli_state *cli,
1747 uint16_t fnum,
1748 bool flag)
1749{
1750 struct tevent_req *req = NULL, *subreq = NULL;
1751 struct doc_state *state = NULL;
1752
1753 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1754 if (req == NULL) {
1755 return NULL;
1756 }
1757
1758 /* Setup setup word. */
1759 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1760
1761 /* Setup param array. */
1762 SSVAL(state->param,0,fnum);
1763 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1764
1765 /* Setup data array. */
1766 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1767
1768 subreq = cli_trans_send(state, /* mem ctx. */
1769 ev, /* event ctx. */
1770 cli, /* cli_state. */
1771 SMBtrans2, /* cmd. */
1772 NULL, /* pipe name. */
1773 -1, /* fid. */
1774 0, /* function. */
1775 0, /* flags. */
1776 &state->setup, /* setup. */
1777 1, /* num setup uint16_t words. */
1778 0, /* max returned setup. */
1779 state->param, /* param. */
1780 6, /* num param. */
1781 2, /* max returned param. */
1782 state->data, /* data. */
1783 1, /* num data. */
1784 0); /* max returned data. */
1785
1786 if (tevent_req_nomem(subreq, req)) {
1787 return tevent_req_post(req, ev);
1788 }
1789 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1790 return req;
1791}
1792
1793NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1794{
1795 return tevent_req_simple_recv_ntstatus(req);
1796}
1797
1798NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1799{
1800 TALLOC_CTX *frame = talloc_stackframe();
1801 struct event_context *ev = NULL;
1802 struct tevent_req *req = NULL;
1803 NTSTATUS status = NT_STATUS_OK;
1804
1805 if (cli_has_async_calls(cli)) {
1806 /*
1807 * Can't use sync call while an async call is in flight
1808 */
1809 status = NT_STATUS_INVALID_PARAMETER;
1810 goto fail;
1811 }
1812
1813 ev = event_context_init(frame);
1814 if (ev == NULL) {
1815 status = NT_STATUS_NO_MEMORY;
1816 goto fail;
1817 }
1818
1819 req = cli_nt_delete_on_close_send(frame,
1820 ev,
1821 cli,
1822 fnum,
1823 flag);
1824 if (req == NULL) {
1825 status = NT_STATUS_NO_MEMORY;
1826 goto fail;
1827 }
1828
1829 if (!tevent_req_poll(req, ev)) {
1830 status = map_nt_error_from_unix(errno);
1831 goto fail;
1832 }
1833
1834 status = cli_nt_delete_on_close_recv(req);
1835
1836 fail:
1837 TALLOC_FREE(frame);
1838 return status;
1839}
1840
1841struct cli_ntcreate_state {
1842 uint16_t vwv[24];
1843 uint16_t fnum;
1844};
1845
1846static void cli_ntcreate_done(struct tevent_req *subreq);
1847
1848struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1849 struct event_context *ev,
1850 struct cli_state *cli,
1851 const char *fname,
1852 uint32_t CreatFlags,
1853 uint32_t DesiredAccess,
1854 uint32_t FileAttributes,
1855 uint32_t ShareAccess,
1856 uint32_t CreateDisposition,
1857 uint32_t CreateOptions,
1858 uint8_t SecurityFlags)
1859{
1860 struct tevent_req *req, *subreq;
1861 struct cli_ntcreate_state *state;
1862 uint16_t *vwv;
1863 uint8_t *bytes;
1864 size_t converted_len;
1865
1866 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1867 if (req == NULL) {
1868 return NULL;
1869 }
1870
1871 vwv = state->vwv;
1872
1873 SCVAL(vwv+0, 0, 0xFF);
1874 SCVAL(vwv+0, 1, 0);
1875 SSVAL(vwv+1, 0, 0);
1876 SCVAL(vwv+2, 0, 0);
1877
1878 if (cli->use_oplocks) {
1879 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1880 }
1881 SIVAL(vwv+3, 1, CreatFlags);
1882 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1883 SIVAL(vwv+7, 1, DesiredAccess);
1884 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1885 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1886 SIVAL(vwv+13, 1, FileAttributes);
1887 SIVAL(vwv+15, 1, ShareAccess);
1888 SIVAL(vwv+17, 1, CreateDisposition);
1889 SIVAL(vwv+19, 1, CreateOptions);
1890 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1891 SCVAL(vwv+23, 1, SecurityFlags);
1892
1893 bytes = talloc_array(state, uint8_t, 0);
1894 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1895 fname, strlen(fname)+1,
1896 &converted_len);
1897
1898 /* sigh. this copes with broken netapp filer behaviour */
1899 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1900
1901 if (tevent_req_nomem(bytes, req)) {
1902 return tevent_req_post(req, ev);
1903 }
1904
1905 SSVAL(vwv+2, 1, converted_len);
1906
1907 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1908 talloc_get_size(bytes), bytes);
1909 if (tevent_req_nomem(subreq, req)) {
1910 return tevent_req_post(req, ev);
1911 }
1912 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1913 return req;
1914}
1915
1916static void cli_ntcreate_done(struct tevent_req *subreq)
1917{
1918 struct tevent_req *req = tevent_req_callback_data(
1919 subreq, struct tevent_req);
1920 struct cli_ntcreate_state *state = tevent_req_data(
1921 req, struct cli_ntcreate_state);
1922 uint8_t wct;
1923 uint16_t *vwv;
1924 uint32_t num_bytes;
1925 uint8_t *bytes;
1926 uint8_t *inbuf;
1927 NTSTATUS status;
1928
1929 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1930 &num_bytes, &bytes);
1931 TALLOC_FREE(subreq);
1932 if (tevent_req_nterror(req, status)) {
1933 return;
1934 }
1935 state->fnum = SVAL(vwv+2, 1);
1936 tevent_req_done(req);
1937}
1938
1939NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1940{
1941 struct cli_ntcreate_state *state = tevent_req_data(
1942 req, struct cli_ntcreate_state);
1943 NTSTATUS status;
1944
1945 if (tevent_req_is_nterror(req, &status)) {
1946 return status;
1947 }
1948 *pfnum = state->fnum;
1949 return NT_STATUS_OK;
1950}
1951
1952NTSTATUS cli_ntcreate(struct cli_state *cli,
1953 const char *fname,
1954 uint32_t CreatFlags,
1955 uint32_t DesiredAccess,
1956 uint32_t FileAttributes,
1957 uint32_t ShareAccess,
1958 uint32_t CreateDisposition,
1959 uint32_t CreateOptions,
1960 uint8_t SecurityFlags,
1961 uint16_t *pfid)
1962{
1963 TALLOC_CTX *frame = talloc_stackframe();
1964 struct event_context *ev;
1965 struct tevent_req *req;
1966 NTSTATUS status = NT_STATUS_OK;
1967
1968 if (cli_has_async_calls(cli)) {
1969 /*
1970 * Can't use sync call while an async call is in flight
1971 */
1972 status = NT_STATUS_INVALID_PARAMETER;
1973 goto fail;
1974 }
1975
1976 ev = event_context_init(frame);
1977 if (ev == NULL) {
1978 status = NT_STATUS_NO_MEMORY;
1979 goto fail;
1980 }
1981
1982 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1983 DesiredAccess, FileAttributes, ShareAccess,
1984 CreateDisposition, CreateOptions,
1985 SecurityFlags);
1986 if (req == NULL) {
1987 status = NT_STATUS_NO_MEMORY;
1988 goto fail;
1989 }
1990
1991 if (!tevent_req_poll(req, ev)) {
1992 status = map_nt_error_from_unix(errno);
1993 goto fail;
1994 }
1995
1996 status = cli_ntcreate_recv(req, pfid);
1997 fail:
1998 TALLOC_FREE(frame);
1999 return status;
2000}
2001
2002/****************************************************************************
2003 Open a file
2004 WARNING: if you open with O_WRONLY then getattrE won't work!
2005****************************************************************************/
2006
2007struct cli_open_state {
2008 struct tevent_context *ev;
2009 struct cli_state *cli;
2010 const char *fname;
2011 uint16_t vwv[15];
2012 uint16_t fnum;
2013 unsigned openfn;
2014 unsigned dos_deny;
2015 uint8_t additional_flags;
2016 struct iovec bytes;
2017};
2018
2019static void cli_open_done(struct tevent_req *subreq);
2020static void cli_open_ntcreate_done(struct tevent_req *subreq);
2021
2022struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2023 struct event_context *ev,
2024 struct cli_state *cli, const char *fname,
2025 int flags, int share_mode,
2026 struct tevent_req **psmbreq)
2027{
2028 struct tevent_req *req, *subreq;
2029 struct cli_open_state *state;
2030 uint8_t *bytes;
2031
2032 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2033 if (req == NULL) {
2034 return NULL;
2035 }
2036 state->ev = ev;
2037 state->cli = cli;
2038 state->fname = fname;
2039
2040 if (flags & O_CREAT) {
2041 state->openfn |= (1<<4);
2042 }
2043 if (!(flags & O_EXCL)) {
2044 if (flags & O_TRUNC)
2045 state->openfn |= (1<<1);
2046 else
2047 state->openfn |= (1<<0);
2048 }
2049
2050 state->dos_deny = (share_mode<<4);
2051
2052 if ((flags & O_ACCMODE) == O_RDWR) {
2053 state->dos_deny |= 2;
2054 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2055 state->dos_deny |= 1;
2056 }
2057
2058#if defined(O_SYNC)
2059 if ((flags & O_SYNC) == O_SYNC) {
2060 state->dos_deny |= (1<<14);
2061 }
2062#endif /* O_SYNC */
2063
2064 if (share_mode == DENY_FCB) {
2065 state->dos_deny = 0xFF;
2066 }
2067
2068 SCVAL(state->vwv + 0, 0, 0xFF);
2069 SCVAL(state->vwv + 0, 1, 0);
2070 SSVAL(state->vwv + 1, 0, 0);
2071 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2072 SSVAL(state->vwv + 3, 0, state->dos_deny);
2073 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2074 SSVAL(state->vwv + 5, 0, 0);
2075 SIVAL(state->vwv + 6, 0, 0);
2076 SSVAL(state->vwv + 8, 0, state->openfn);
2077 SIVAL(state->vwv + 9, 0, 0);
2078 SIVAL(state->vwv + 11, 0, 0);
2079 SIVAL(state->vwv + 13, 0, 0);
2080
2081 if (cli->use_oplocks) {
2082 /* if using oplocks then ask for a batch oplock via
2083 core and extended methods */
2084 state->additional_flags =
2085 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2086 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2087 }
2088
2089 bytes = talloc_array(state, uint8_t, 0);
2090 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2091 strlen(fname)+1, NULL);
2092
2093 if (tevent_req_nomem(bytes, req)) {
2094 return tevent_req_post(req, ev);
2095 }
2096
2097 state->bytes.iov_base = (void *)bytes;
2098 state->bytes.iov_len = talloc_get_size(bytes);
2099
2100 subreq = cli_smb_req_create(state, ev, cli, SMBopenX,
2101 state->additional_flags,
2102 15, state->vwv, 1, &state->bytes);
2103 if (subreq == NULL) {
2104 TALLOC_FREE(req);
2105 return NULL;
2106 }
2107 tevent_req_set_callback(subreq, cli_open_done, req);
2108 *psmbreq = subreq;
2109 return req;
2110}
2111
2112struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2113 struct cli_state *cli, const char *fname,
2114 int flags, int share_mode)
2115{
2116 struct tevent_req *req, *subreq;
2117 NTSTATUS status;
2118
2119 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2120 &subreq);
2121 if (req == NULL) {
2122 return NULL;
2123 }
2124
2125 status = cli_smb_req_send(subreq);
2126 if (tevent_req_nterror(req, status)) {
2127 return tevent_req_post(req, ev);
2128 }
2129 return req;
2130}
2131
2132static void cli_open_done(struct tevent_req *subreq)
2133{
2134 struct tevent_req *req = tevent_req_callback_data(
2135 subreq, struct tevent_req);
2136 struct cli_open_state *state = tevent_req_data(
2137 req, struct cli_open_state);
2138 uint8_t wct;
2139 uint16_t *vwv;
2140 uint8_t *inbuf;
2141 NTSTATUS status;
2142 uint32_t access_mask, share_mode, create_disposition, create_options;
2143
2144 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2145 NULL);
2146 TALLOC_FREE(subreq);
2147
2148 if (NT_STATUS_IS_OK(status)) {
2149 state->fnum = SVAL(vwv+2, 0);
2150 tevent_req_done(req);
2151 return;
2152 }
2153
2154 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
2155 tevent_req_nterror(req, status);
2156 return;
2157 }
2158
2159 /*
2160 * For the new shiny OS/X Lion SMB server, try a ntcreate
2161 * fallback.
2162 */
2163
2164 if (!map_open_params_to_ntcreate(state->fname, state->dos_deny,
2165 state->openfn, &access_mask,
2166 &share_mode, &create_disposition,
2167 &create_options, NULL)) {
2168 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
2169 return;
2170 }
2171
2172 subreq = cli_ntcreate_send(state, state->ev, state->cli,
2173 state->fname, 0, access_mask,
2174 0, share_mode, create_disposition,
2175 create_options, 0);
2176 if (tevent_req_nomem(subreq, req)) {
2177 return;
2178 }
2179 tevent_req_set_callback(subreq, cli_open_ntcreate_done, req);
2180}
2181
2182static void cli_open_ntcreate_done(struct tevent_req *subreq)
2183{
2184 struct tevent_req *req = tevent_req_callback_data(
2185 subreq, struct tevent_req);
2186 struct cli_open_state *state = tevent_req_data(
2187 req, struct cli_open_state);
2188 NTSTATUS status;
2189
2190 status = cli_ntcreate_recv(subreq, &state->fnum);
2191 TALLOC_FREE(subreq);
2192 if (tevent_req_nterror(req, status)) {
2193 return;
2194 }
2195 tevent_req_done(req);
2196}
2197
2198NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2199{
2200 struct cli_open_state *state = tevent_req_data(
2201 req, struct cli_open_state);
2202 NTSTATUS status;
2203
2204 if (tevent_req_is_nterror(req, &status)) {
2205 return status;
2206 }
2207 *pfnum = state->fnum;
2208 return NT_STATUS_OK;
2209}
2210
2211NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2212 int share_mode, uint16_t *pfnum)
2213{
2214 TALLOC_CTX *frame = talloc_stackframe();
2215 struct event_context *ev;
2216 struct tevent_req *req;
2217 NTSTATUS status = NT_STATUS_OK;
2218
2219 if (cli_has_async_calls(cli)) {
2220 /*
2221 * Can't use sync call while an async call is in flight
2222 */
2223 status = NT_STATUS_INVALID_PARAMETER;
2224 goto fail;
2225 }
2226
2227 ev = event_context_init(frame);
2228 if (ev == NULL) {
2229 status = NT_STATUS_NO_MEMORY;
2230 goto fail;
2231 }
2232
2233 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2234 if (req == NULL) {
2235 status = NT_STATUS_NO_MEMORY;
2236 goto fail;
2237 }
2238
2239 if (!tevent_req_poll(req, ev)) {
2240 status = map_nt_error_from_unix(errno);
2241 goto fail;
2242 }
2243
2244 status = cli_open_recv(req, pfnum);
2245 fail:
2246 TALLOC_FREE(frame);
2247 return status;
2248}
2249
2250/****************************************************************************
2251 Close a file.
2252****************************************************************************/
2253
2254struct cli_close_state {
2255 uint16_t vwv[3];
2256};
2257
2258static void cli_close_done(struct tevent_req *subreq);
2259
2260struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2261 struct event_context *ev,
2262 struct cli_state *cli,
2263 uint16_t fnum,
2264 struct tevent_req **psubreq)
2265{
2266 struct tevent_req *req, *subreq;
2267 struct cli_close_state *state;
2268
2269 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2270 if (req == NULL) {
2271 return NULL;
2272 }
2273
2274 SSVAL(state->vwv+0, 0, fnum);
2275 SIVALS(state->vwv+1, 0, -1);
2276
2277 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2278 0, NULL);
2279 if (subreq == NULL) {
2280 TALLOC_FREE(req);
2281 return NULL;
2282 }
2283 tevent_req_set_callback(subreq, cli_close_done, req);
2284 *psubreq = subreq;
2285 return req;
2286}
2287
2288struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2289 struct event_context *ev,
2290 struct cli_state *cli,
2291 uint16_t fnum)
2292{
2293 struct tevent_req *req, *subreq;
2294 NTSTATUS status;
2295
2296 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2297 if (req == NULL) {
2298 return NULL;
2299 }
2300
2301 status = cli_smb_req_send(subreq);
2302 if (tevent_req_nterror(req, status)) {
2303 return tevent_req_post(req, ev);
2304 }
2305 return req;
2306}
2307
2308static void cli_close_done(struct tevent_req *subreq)
2309{
2310 struct tevent_req *req = tevent_req_callback_data(
2311 subreq, struct tevent_req);
2312 NTSTATUS status;
2313
2314 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2315 TALLOC_FREE(subreq);
2316 if (tevent_req_nterror(req, status)) {
2317 return;
2318 }
2319 tevent_req_done(req);
2320}
2321
2322NTSTATUS cli_close_recv(struct tevent_req *req)
2323{
2324 return tevent_req_simple_recv_ntstatus(req);
2325}
2326
2327NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2328{
2329 TALLOC_CTX *frame = talloc_stackframe();
2330 struct event_context *ev;
2331 struct tevent_req *req;
2332 NTSTATUS status = NT_STATUS_OK;
2333
2334 if (cli_has_async_calls(cli)) {
2335 /*
2336 * Can't use sync call while an async call is in flight
2337 */
2338 status = NT_STATUS_INVALID_PARAMETER;
2339 goto fail;
2340 }
2341
2342 ev = event_context_init(frame);
2343 if (ev == NULL) {
2344 status = NT_STATUS_NO_MEMORY;
2345 goto fail;
2346 }
2347
2348 req = cli_close_send(frame, ev, cli, fnum);
2349 if (req == NULL) {
2350 status = NT_STATUS_NO_MEMORY;
2351 goto fail;
2352 }
2353
2354 if (!tevent_req_poll(req, ev)) {
2355 status = map_nt_error_from_unix(errno);
2356 goto fail;
2357 }
2358
2359 status = cli_close_recv(req);
2360 fail:
2361 TALLOC_FREE(frame);
2362 return status;
2363}
2364
2365/****************************************************************************
2366 Truncate a file to a specified size
2367****************************************************************************/
2368
2369struct ftrunc_state {
2370 uint16_t setup;
2371 uint8_t param[6];
2372 uint8_t data[8];
2373};
2374
2375static void cli_ftruncate_done(struct tevent_req *subreq)
2376{
2377 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2378 NULL, 0, NULL, NULL, 0, NULL);
2379 tevent_req_simple_finish_ntstatus(subreq, status);
2380}
2381
2382struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2383 struct event_context *ev,
2384 struct cli_state *cli,
2385 uint16_t fnum,
2386 uint64_t size)
2387{
2388 struct tevent_req *req = NULL, *subreq = NULL;
2389 struct ftrunc_state *state = NULL;
2390
2391 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2392 if (req == NULL) {
2393 return NULL;
2394 }
2395
2396 /* Setup setup word. */
2397 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2398
2399 /* Setup param array. */
2400 SSVAL(state->param,0,fnum);
2401 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2402 SSVAL(state->param,4,0);
2403
2404 /* Setup data array. */
2405 SBVAL(state->data, 0, size);
2406
2407 subreq = cli_trans_send(state, /* mem ctx. */
2408 ev, /* event ctx. */
2409 cli, /* cli_state. */
2410 SMBtrans2, /* cmd. */
2411 NULL, /* pipe name. */
2412 -1, /* fid. */
2413 0, /* function. */
2414 0, /* flags. */
2415 &state->setup, /* setup. */
2416 1, /* num setup uint16_t words. */
2417 0, /* max returned setup. */
2418 state->param, /* param. */
2419 6, /* num param. */
2420 2, /* max returned param. */
2421 state->data, /* data. */
2422 8, /* num data. */
2423 0); /* max returned data. */
2424
2425 if (tevent_req_nomem(subreq, req)) {
2426 return tevent_req_post(req, ev);
2427 }
2428 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2429 return req;
2430}
2431
2432NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2433{
2434 return tevent_req_simple_recv_ntstatus(req);
2435}
2436
2437NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2438{
2439 TALLOC_CTX *frame = talloc_stackframe();
2440 struct event_context *ev = NULL;
2441 struct tevent_req *req = NULL;
2442 NTSTATUS status = NT_STATUS_OK;
2443
2444 if (cli_has_async_calls(cli)) {
2445 /*
2446 * Can't use sync call while an async call is in flight
2447 */
2448 status = NT_STATUS_INVALID_PARAMETER;
2449 goto fail;
2450 }
2451
2452 ev = event_context_init(frame);
2453 if (ev == NULL) {
2454 status = NT_STATUS_NO_MEMORY;
2455 goto fail;
2456 }
2457
2458 req = cli_ftruncate_send(frame,
2459 ev,
2460 cli,
2461 fnum,
2462 size);
2463 if (req == NULL) {
2464 status = NT_STATUS_NO_MEMORY;
2465 goto fail;
2466 }
2467
2468 if (!tevent_req_poll(req, ev)) {
2469 status = map_nt_error_from_unix(errno);
2470 goto fail;
2471 }
2472
2473 status = cli_ftruncate_recv(req);
2474
2475 fail:
2476 TALLOC_FREE(frame);
2477 return status;
2478}
2479
2480/****************************************************************************
2481 send a lock with a specified locktype
2482 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2483****************************************************************************/
2484
2485NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2486 uint32_t offset, uint32_t len,
2487 int timeout, unsigned char locktype)
2488{
2489 uint16_t vwv[8];
2490 uint8_t bytes[10];
2491 NTSTATUS status;
2492 int saved_timeout;
2493
2494 SCVAL(vwv + 0, 0, 0xff);
2495 SCVAL(vwv + 0, 1, 0);
2496 SSVAL(vwv + 1, 0, 0);
2497 SSVAL(vwv + 2, 0, fnum);
2498 SCVAL(vwv + 3, 0, locktype);
2499 SCVAL(vwv + 3, 1, 0);
2500 SIVALS(vwv + 4, 0, timeout);
2501 SSVAL(vwv + 6, 0, 0);
2502 SSVAL(vwv + 7, 0, 1);
2503
2504 SSVAL(bytes, 0, cli->pid);
2505 SIVAL(bytes, 2, offset);
2506 SIVAL(bytes, 6, len);
2507
2508 saved_timeout = cli->timeout;
2509
2510 if (timeout != 0) {
2511 cli->timeout = (timeout == -1)
2512 ? 0x7FFFFFFF : (timeout + 2*1000);
2513 }
2514
2515 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2516 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2517
2518 cli->timeout = saved_timeout;
2519
2520 return status;
2521}
2522
2523/****************************************************************************
2524 Lock a file.
2525 note that timeout is in units of 2 milliseconds
2526****************************************************************************/
2527
2528bool cli_lock(struct cli_state *cli, uint16_t fnum,
2529 uint32_t offset, uint32_t len, int timeout,
2530 enum brl_type lock_type)
2531{
2532 NTSTATUS status;
2533
2534 status = cli_locktype(cli, fnum, offset, len, timeout,
2535 (lock_type == READ_LOCK? 1 : 0));
2536 return NT_STATUS_IS_OK(status);
2537}
2538
2539/****************************************************************************
2540 Unlock a file.
2541****************************************************************************/
2542
2543struct cli_unlock_state {
2544 uint16_t vwv[8];
2545 uint8_t data[10];
2546};
2547
2548static void cli_unlock_done(struct tevent_req *subreq);
2549
2550struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2551 struct event_context *ev,
2552 struct cli_state *cli,
2553 uint16_t fnum,
2554 uint64_t offset,
2555 uint64_t len)
2556
2557{
2558 struct tevent_req *req = NULL, *subreq = NULL;
2559 struct cli_unlock_state *state = NULL;
2560 uint8_t additional_flags = 0;
2561
2562 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2563 if (req == NULL) {
2564 return NULL;
2565 }
2566
2567 SCVAL(state->vwv+0, 0, 0xFF);
2568 SSVAL(state->vwv+2, 0, fnum);
2569 SCVAL(state->vwv+3, 0, 0);
2570 SIVALS(state->vwv+4, 0, 0);
2571 SSVAL(state->vwv+6, 0, 1);
2572 SSVAL(state->vwv+7, 0, 0);
2573
2574 SSVAL(state->data, 0, cli->pid);
2575 SIVAL(state->data, 2, offset);
2576 SIVAL(state->data, 6, len);
2577
2578 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2579 8, state->vwv, 10, state->data);
2580 if (tevent_req_nomem(subreq, req)) {
2581 return tevent_req_post(req, ev);
2582 }
2583 tevent_req_set_callback(subreq, cli_unlock_done, req);
2584 return req;
2585}
2586
2587static void cli_unlock_done(struct tevent_req *subreq)
2588{
2589 struct tevent_req *req = tevent_req_callback_data(
2590 subreq, struct tevent_req);
2591 NTSTATUS status;
2592
2593 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2594 TALLOC_FREE(subreq);
2595 if (tevent_req_nterror(req, status)) {
2596 return;
2597 }
2598 tevent_req_done(req);
2599}
2600
2601NTSTATUS cli_unlock_recv(struct tevent_req *req)
2602{
2603 return tevent_req_simple_recv_ntstatus(req);
2604}
2605
2606NTSTATUS cli_unlock(struct cli_state *cli,
2607 uint16_t fnum,
2608 uint32_t offset,
2609 uint32_t len)
2610{
2611 TALLOC_CTX *frame = talloc_stackframe();
2612 struct event_context *ev;
2613 struct tevent_req *req;
2614 NTSTATUS status = NT_STATUS_OK;
2615
2616 if (cli_has_async_calls(cli)) {
2617 /*
2618 * Can't use sync call while an async call is in flight
2619 */
2620 status = NT_STATUS_INVALID_PARAMETER;
2621 goto fail;
2622 }
2623
2624 ev = event_context_init(frame);
2625 if (ev == NULL) {
2626 status = NT_STATUS_NO_MEMORY;
2627 goto fail;
2628 }
2629
2630 req = cli_unlock_send(frame, ev, cli,
2631 fnum, offset, len);
2632 if (req == NULL) {
2633 status = NT_STATUS_NO_MEMORY;
2634 goto fail;
2635 }
2636
2637 if (!tevent_req_poll(req, ev)) {
2638 status = map_nt_error_from_unix(errno);
2639 goto fail;
2640 }
2641
2642 status = cli_unlock_recv(req);
2643
2644 fail:
2645 TALLOC_FREE(frame);
2646 return status;
2647}
2648
2649/****************************************************************************
2650 Lock a file with 64 bit offsets.
2651****************************************************************************/
2652
2653bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2654 uint64_t offset, uint64_t len, int timeout,
2655 enum brl_type lock_type)
2656{
2657 uint16_t vwv[8];
2658 uint8_t bytes[20];
2659 int saved_timeout = cli->timeout;
2660 int ltype;
2661 NTSTATUS status;
2662
2663 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2664 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2665 }
2666
2667 ltype = (lock_type == READ_LOCK? 1 : 0);
2668 ltype |= LOCKING_ANDX_LARGE_FILES;
2669
2670 SCVAL(vwv + 0, 0, 0xff);
2671 SCVAL(vwv + 0, 1, 0);
2672 SSVAL(vwv + 1, 0, 0);
2673 SSVAL(vwv + 2, 0, fnum);
2674 SCVAL(vwv + 3, 0, ltype);
2675 SCVAL(vwv + 3, 1, 0);
2676 SIVALS(vwv + 4, 0, timeout);
2677 SSVAL(vwv + 6, 0, 0);
2678 SSVAL(vwv + 7, 0, 1);
2679
2680 SIVAL(bytes, 0, cli->pid);
2681 SOFF_T_R(bytes, 4, offset);
2682 SOFF_T_R(bytes, 12, len);
2683
2684 saved_timeout = cli->timeout;
2685
2686 if (timeout != 0) {
2687 cli->timeout = (timeout == -1)
2688 ? 0x7FFFFFFF : (timeout + 2*1000);
2689 }
2690
2691 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2692 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2693
2694 cli->timeout = saved_timeout;
2695
2696 return NT_STATUS_IS_OK(status);
2697}
2698
2699/****************************************************************************
2700 Unlock a file with 64 bit offsets.
2701****************************************************************************/
2702
2703struct cli_unlock64_state {
2704 uint16_t vwv[8];
2705 uint8_t data[20];
2706};
2707
2708static void cli_unlock64_done(struct tevent_req *subreq);
2709
2710struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2711 struct event_context *ev,
2712 struct cli_state *cli,
2713 uint16_t fnum,
2714 uint64_t offset,
2715 uint64_t len)
2716
2717{
2718 struct tevent_req *req = NULL, *subreq = NULL;
2719 struct cli_unlock64_state *state = NULL;
2720 uint8_t additional_flags = 0;
2721
2722 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2723 if (req == NULL) {
2724 return NULL;
2725 }
2726
2727 SCVAL(state->vwv+0, 0, 0xff);
2728 SSVAL(state->vwv+2, 0, fnum);
2729 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2730 SIVALS(state->vwv+4, 0, 0);
2731 SSVAL(state->vwv+6, 0, 1);
2732 SSVAL(state->vwv+7, 0, 0);
2733
2734 SIVAL(state->data, 0, cli->pid);
2735 SOFF_T_R(state->data, 4, offset);
2736 SOFF_T_R(state->data, 12, len);
2737
2738 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2739 8, state->vwv, 20, state->data);
2740 if (tevent_req_nomem(subreq, req)) {
2741 return tevent_req_post(req, ev);
2742 }
2743 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2744 return req;
2745}
2746
2747static void cli_unlock64_done(struct tevent_req *subreq)
2748{
2749 struct tevent_req *req = tevent_req_callback_data(
2750 subreq, struct tevent_req);
2751 NTSTATUS status;
2752
2753 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2754 TALLOC_FREE(subreq);
2755 if (tevent_req_nterror(req, status)) {
2756 return;
2757 }
2758 tevent_req_done(req);
2759}
2760
2761NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2762{
2763 return tevent_req_simple_recv_ntstatus(req);
2764}
2765
2766NTSTATUS cli_unlock64(struct cli_state *cli,
2767 uint16_t fnum,
2768 uint64_t offset,
2769 uint64_t len)
2770{
2771 TALLOC_CTX *frame = talloc_stackframe();
2772 struct event_context *ev;
2773 struct tevent_req *req;
2774 NTSTATUS status = NT_STATUS_OK;
2775
2776 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2777 return cli_unlock(cli, fnum, offset, len);
2778 }
2779
2780 if (cli_has_async_calls(cli)) {
2781 /*
2782 * Can't use sync call while an async call is in flight
2783 */
2784 status = NT_STATUS_INVALID_PARAMETER;
2785 goto fail;
2786 }
2787
2788 ev = event_context_init(frame);
2789 if (ev == NULL) {
2790 status = NT_STATUS_NO_MEMORY;
2791 goto fail;
2792 }
2793
2794 req = cli_unlock64_send(frame, ev, cli,
2795 fnum, offset, len);
2796 if (req == NULL) {
2797 status = NT_STATUS_NO_MEMORY;
2798 goto fail;
2799 }
2800
2801 if (!tevent_req_poll(req, ev)) {
2802 status = map_nt_error_from_unix(errno);
2803 goto fail;
2804 }
2805
2806 status = cli_unlock64_recv(req);
2807
2808 fail:
2809 TALLOC_FREE(frame);
2810 return status;
2811}
2812
2813/****************************************************************************
2814 Get/unlock a POSIX lock on a file - internal function.
2815****************************************************************************/
2816
2817struct posix_lock_state {
2818 uint16_t setup;
2819 uint8_t param[4];
2820 uint8_t data[POSIX_LOCK_DATA_SIZE];
2821};
2822
2823static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2824{
2825 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2826 NULL, 0, NULL, NULL, 0, NULL);
2827 tevent_req_simple_finish_ntstatus(subreq, status);
2828}
2829
2830static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2831 struct event_context *ev,
2832 struct cli_state *cli,
2833 uint16_t fnum,
2834 uint64_t offset,
2835 uint64_t len,
2836 bool wait_lock,
2837 enum brl_type lock_type)
2838{
2839 struct tevent_req *req = NULL, *subreq = NULL;
2840 struct posix_lock_state *state = NULL;
2841
2842 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2843 if (req == NULL) {
2844 return NULL;
2845 }
2846
2847 /* Setup setup word. */
2848 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2849
2850 /* Setup param array. */
2851 SSVAL(&state->param, 0, fnum);
2852 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2853
2854 /* Setup data array. */
2855 switch (lock_type) {
2856 case READ_LOCK:
2857 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2858 POSIX_LOCK_TYPE_READ);
2859 break;
2860 case WRITE_LOCK:
2861 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2862 POSIX_LOCK_TYPE_WRITE);
2863 break;
2864 case UNLOCK_LOCK:
2865 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2866 POSIX_LOCK_TYPE_UNLOCK);
2867 break;
2868 default:
2869 return NULL;
2870 }
2871
2872 if (wait_lock) {
2873 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2874 POSIX_LOCK_FLAG_WAIT);
2875 } else {
2876 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2877 POSIX_LOCK_FLAG_NOWAIT);
2878 }
2879
2880 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2881 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2882 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2883
2884 subreq = cli_trans_send(state, /* mem ctx. */
2885 ev, /* event ctx. */
2886 cli, /* cli_state. */
2887 SMBtrans2, /* cmd. */
2888 NULL, /* pipe name. */
2889 -1, /* fid. */
2890 0, /* function. */
2891 0, /* flags. */
2892 &state->setup, /* setup. */
2893 1, /* num setup uint16_t words. */
2894 0, /* max returned setup. */
2895 state->param, /* param. */
2896 4, /* num param. */
2897 2, /* max returned param. */
2898 state->data, /* data. */
2899 POSIX_LOCK_DATA_SIZE, /* num data. */
2900 0); /* max returned data. */
2901
2902 if (tevent_req_nomem(subreq, req)) {
2903 return tevent_req_post(req, ev);
2904 }
2905 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2906 return req;
2907}
2908
2909/****************************************************************************
2910 POSIX Lock a file.
2911****************************************************************************/
2912
2913struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2914 struct event_context *ev,
2915 struct cli_state *cli,
2916 uint16_t fnum,
2917 uint64_t offset,
2918 uint64_t len,
2919 bool wait_lock,
2920 enum brl_type lock_type)
2921{
2922 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2923 wait_lock, lock_type);
2924}
2925
2926NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2927{
2928 return tevent_req_simple_recv_ntstatus(req);
2929}
2930
2931NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2932 uint64_t offset, uint64_t len,
2933 bool wait_lock, enum brl_type lock_type)
2934{
2935 TALLOC_CTX *frame = talloc_stackframe();
2936 struct event_context *ev = NULL;
2937 struct tevent_req *req = NULL;
2938 NTSTATUS status = NT_STATUS_OK;
2939
2940 if (cli_has_async_calls(cli)) {
2941 /*
2942 * Can't use sync call while an async call is in flight
2943 */
2944 status = NT_STATUS_INVALID_PARAMETER;
2945 goto fail;
2946 }
2947
2948 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2949 status = NT_STATUS_INVALID_PARAMETER;
2950 goto fail;
2951 }
2952
2953 ev = event_context_init(frame);
2954 if (ev == NULL) {
2955 status = NT_STATUS_NO_MEMORY;
2956 goto fail;
2957 }
2958
2959 req = cli_posix_lock_send(frame,
2960 ev,
2961 cli,
2962 fnum,
2963 offset,
2964 len,
2965 wait_lock,
2966 lock_type);
2967 if (req == NULL) {
2968 status = NT_STATUS_NO_MEMORY;
2969 goto fail;
2970 }
2971
2972 if (!tevent_req_poll(req, ev)) {
2973 status = map_nt_error_from_unix(errno);
2974 goto fail;
2975 }
2976
2977 status = cli_posix_lock_recv(req);
2978
2979 fail:
2980 TALLOC_FREE(frame);
2981 return status;
2982}
2983
2984/****************************************************************************
2985 POSIX Unlock a file.
2986****************************************************************************/
2987
2988struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
2989 struct event_context *ev,
2990 struct cli_state *cli,
2991 uint16_t fnum,
2992 uint64_t offset,
2993 uint64_t len)
2994{
2995 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2996 false, UNLOCK_LOCK);
2997}
2998
2999NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3000{
3001 return tevent_req_simple_recv_ntstatus(req);
3002}
3003
3004NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3005{
3006 TALLOC_CTX *frame = talloc_stackframe();
3007 struct event_context *ev = NULL;
3008 struct tevent_req *req = NULL;
3009 NTSTATUS status = NT_STATUS_OK;
3010
3011 if (cli_has_async_calls(cli)) {
3012 /*
3013 * Can't use sync call while an async call is in flight
3014 */
3015 status = NT_STATUS_INVALID_PARAMETER;
3016 goto fail;
3017 }
3018
3019 ev = event_context_init(frame);
3020 if (ev == NULL) {
3021 status = NT_STATUS_NO_MEMORY;
3022 goto fail;
3023 }
3024
3025 req = cli_posix_unlock_send(frame,
3026 ev,
3027 cli,
3028 fnum,
3029 offset,
3030 len);
3031 if (req == NULL) {
3032 status = NT_STATUS_NO_MEMORY;
3033 goto fail;
3034 }
3035
3036 if (!tevent_req_poll(req, ev)) {
3037 status = map_nt_error_from_unix(errno);
3038 goto fail;
3039 }
3040
3041 status = cli_posix_unlock_recv(req);
3042
3043 fail:
3044 TALLOC_FREE(frame);
3045 return status;
3046}
3047
3048/****************************************************************************
3049 Do a SMBgetattrE call.
3050****************************************************************************/
3051
3052static void cli_getattrE_done(struct tevent_req *subreq);
3053
3054struct cli_getattrE_state {
3055 uint16_t vwv[1];
3056 int zone_offset;
3057 uint16_t attr;
3058 SMB_OFF_T size;
3059 time_t change_time;
3060 time_t access_time;
3061 time_t write_time;
3062};
3063
3064struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3065 struct event_context *ev,
3066 struct cli_state *cli,
3067 uint16_t fnum)
3068{
3069 struct tevent_req *req = NULL, *subreq = NULL;
3070 struct cli_getattrE_state *state = NULL;
3071 uint8_t additional_flags = 0;
3072
3073 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3074 if (req == NULL) {
3075 return NULL;
3076 }
3077
3078 state->zone_offset = cli->serverzone;
3079 SSVAL(state->vwv+0,0,fnum);
3080
3081 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3082 1, state->vwv, 0, NULL);
3083 if (tevent_req_nomem(subreq, req)) {
3084 return tevent_req_post(req, ev);
3085 }
3086 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3087 return req;
3088}
3089
3090static void cli_getattrE_done(struct tevent_req *subreq)
3091{
3092 struct tevent_req *req = tevent_req_callback_data(
3093 subreq, struct tevent_req);
3094 struct cli_getattrE_state *state = tevent_req_data(
3095 req, struct cli_getattrE_state);
3096 uint8_t wct;
3097 uint16_t *vwv = NULL;
3098 uint8_t *inbuf;
3099 NTSTATUS status;
3100
3101 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3102 NULL, NULL);
3103 TALLOC_FREE(subreq);
3104 if (tevent_req_nterror(req, status)) {
3105 return;
3106 }
3107
3108 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3109 state->attr = SVAL(vwv+10,0);
3110 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3111 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3112 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3113
3114 tevent_req_done(req);
3115}
3116
3117NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3118 uint16_t *attr,
3119 SMB_OFF_T *size,
3120 time_t *change_time,
3121 time_t *access_time,
3122 time_t *write_time)
3123{
3124 struct cli_getattrE_state *state = tevent_req_data(
3125 req, struct cli_getattrE_state);
3126 NTSTATUS status;
3127
3128 if (tevent_req_is_nterror(req, &status)) {
3129 return status;
3130 }
3131 if (attr) {
3132 *attr = state->attr;
3133 }
3134 if (size) {
3135 *size = state->size;
3136 }
3137 if (change_time) {
3138 *change_time = state->change_time;
3139 }
3140 if (access_time) {
3141 *access_time = state->access_time;
3142 }
3143 if (write_time) {
3144 *write_time = state->write_time;
3145 }
3146 return NT_STATUS_OK;
3147}
3148
3149NTSTATUS cli_getattrE(struct cli_state *cli,
3150 uint16_t fnum,
3151 uint16_t *attr,
3152 SMB_OFF_T *size,
3153 time_t *change_time,
3154 time_t *access_time,
3155 time_t *write_time)
3156{
3157 TALLOC_CTX *frame = talloc_stackframe();
3158 struct event_context *ev = NULL;
3159 struct tevent_req *req = NULL;
3160 NTSTATUS status = NT_STATUS_OK;
3161
3162 if (cli_has_async_calls(cli)) {
3163 /*
3164 * Can't use sync call while an async call is in flight
3165 */
3166 status = NT_STATUS_INVALID_PARAMETER;
3167 goto fail;
3168 }
3169
3170 ev = event_context_init(frame);
3171 if (ev == NULL) {
3172 status = NT_STATUS_NO_MEMORY;
3173 goto fail;
3174 }
3175
3176 req = cli_getattrE_send(frame, ev, cli, fnum);
3177 if (req == NULL) {
3178 status = NT_STATUS_NO_MEMORY;
3179 goto fail;
3180 }
3181
3182 if (!tevent_req_poll(req, ev)) {
3183 status = map_nt_error_from_unix(errno);
3184 goto fail;
3185 }
3186
3187 status = cli_getattrE_recv(req,
3188 attr,
3189 size,
3190 change_time,
3191 access_time,
3192 write_time);
3193
3194 fail:
3195 TALLOC_FREE(frame);
3196 return status;
3197}
3198
3199/****************************************************************************
3200 Do a SMBgetatr call
3201****************************************************************************/
3202
3203static void cli_getatr_done(struct tevent_req *subreq);
3204
3205struct cli_getatr_state {
3206 int zone_offset;
3207 uint16_t attr;
3208 SMB_OFF_T size;
3209 time_t write_time;
3210};
3211
3212struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3213 struct event_context *ev,
3214 struct cli_state *cli,
3215 const char *fname)
3216{
3217 struct tevent_req *req = NULL, *subreq = NULL;
3218 struct cli_getatr_state *state = NULL;
3219 uint8_t additional_flags = 0;
3220 uint8_t *bytes = NULL;
3221
3222 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3223 if (req == NULL) {
3224 return NULL;
3225 }
3226
3227 state->zone_offset = cli->serverzone;
3228
3229 bytes = talloc_array(state, uint8_t, 1);
3230 if (tevent_req_nomem(bytes, req)) {
3231 return tevent_req_post(req, ev);
3232 }
3233 bytes[0] = 4;
3234 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3235 strlen(fname)+1, NULL);
3236
3237 if (tevent_req_nomem(bytes, req)) {
3238 return tevent_req_post(req, ev);
3239 }
3240
3241 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3242 0, NULL, talloc_get_size(bytes), bytes);
3243 if (tevent_req_nomem(subreq, req)) {
3244 return tevent_req_post(req, ev);
3245 }
3246 tevent_req_set_callback(subreq, cli_getatr_done, req);
3247 return req;
3248}
3249
3250static void cli_getatr_done(struct tevent_req *subreq)
3251{
3252 struct tevent_req *req = tevent_req_callback_data(
3253 subreq, struct tevent_req);
3254 struct cli_getatr_state *state = tevent_req_data(
3255 req, struct cli_getatr_state);
3256 uint8_t wct;
3257 uint16_t *vwv = NULL;
3258 uint8_t *inbuf;
3259 NTSTATUS status;
3260
3261 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3262 NULL);
3263 TALLOC_FREE(subreq);
3264 if (tevent_req_nterror(req, status)) {
3265 return;
3266 }
3267
3268 state->attr = SVAL(vwv+0,0);
3269 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3270 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3271
3272 tevent_req_done(req);
3273}
3274
3275NTSTATUS cli_getatr_recv(struct tevent_req *req,
3276 uint16_t *attr,
3277 SMB_OFF_T *size,
3278 time_t *write_time)
3279{
3280 struct cli_getatr_state *state = tevent_req_data(
3281 req, struct cli_getatr_state);
3282 NTSTATUS status;
3283
3284 if (tevent_req_is_nterror(req, &status)) {
3285 return status;
3286 }
3287 if (attr) {
3288 *attr = state->attr;
3289 }
3290 if (size) {
3291 *size = state->size;
3292 }
3293 if (write_time) {
3294 *write_time = state->write_time;
3295 }
3296 return NT_STATUS_OK;
3297}
3298
3299NTSTATUS cli_getatr(struct cli_state *cli,
3300 const char *fname,
3301 uint16_t *attr,
3302 SMB_OFF_T *size,
3303 time_t *write_time)
3304{
3305 TALLOC_CTX *frame = talloc_stackframe();
3306 struct event_context *ev = NULL;
3307 struct tevent_req *req = NULL;
3308 NTSTATUS status = NT_STATUS_OK;
3309
3310 if (cli_has_async_calls(cli)) {
3311 /*
3312 * Can't use sync call while an async call is in flight
3313 */
3314 status = NT_STATUS_INVALID_PARAMETER;
3315 goto fail;
3316 }
3317
3318 ev = event_context_init(frame);
3319 if (ev == NULL) {
3320 status = NT_STATUS_NO_MEMORY;
3321 goto fail;
3322 }
3323
3324 req = cli_getatr_send(frame, ev, cli, fname);
3325 if (req == NULL) {
3326 status = NT_STATUS_NO_MEMORY;
3327 goto fail;
3328 }
3329
3330 if (!tevent_req_poll(req, ev)) {
3331 status = map_nt_error_from_unix(errno);
3332 goto fail;
3333 }
3334
3335 status = cli_getatr_recv(req,
3336 attr,
3337 size,
3338 write_time);
3339
3340 fail:
3341 TALLOC_FREE(frame);
3342 return status;
3343}
3344
3345/****************************************************************************
3346 Do a SMBsetattrE call.
3347****************************************************************************/
3348
3349static void cli_setattrE_done(struct tevent_req *subreq);
3350
3351struct cli_setattrE_state {
3352 uint16_t vwv[7];
3353};
3354
3355struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3356 struct event_context *ev,
3357 struct cli_state *cli,
3358 uint16_t fnum,
3359 time_t change_time,
3360 time_t access_time,
3361 time_t write_time)
3362{
3363 struct tevent_req *req = NULL, *subreq = NULL;
3364 struct cli_setattrE_state *state = NULL;
3365 uint8_t additional_flags = 0;
3366
3367 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3368 if (req == NULL) {
3369 return NULL;
3370 }
3371
3372 SSVAL(state->vwv+0, 0, fnum);
3373 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3374 cli->serverzone);
3375 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3376 cli->serverzone);
3377 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3378 cli->serverzone);
3379
3380 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3381 7, state->vwv, 0, NULL);
3382 if (tevent_req_nomem(subreq, req)) {
3383 return tevent_req_post(req, ev);
3384 }
3385 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3386 return req;
3387}
3388
3389static void cli_setattrE_done(struct tevent_req *subreq)
3390{
3391 struct tevent_req *req = tevent_req_callback_data(
3392 subreq, struct tevent_req);
3393 NTSTATUS status;
3394
3395 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3396 TALLOC_FREE(subreq);
3397 if (tevent_req_nterror(req, status)) {
3398 return;
3399 }
3400 tevent_req_done(req);
3401}
3402
3403NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3404{
3405 return tevent_req_simple_recv_ntstatus(req);
3406}
3407
3408NTSTATUS cli_setattrE(struct cli_state *cli,
3409 uint16_t fnum,
3410 time_t change_time,
3411 time_t access_time,
3412 time_t write_time)
3413{
3414 TALLOC_CTX *frame = talloc_stackframe();
3415 struct event_context *ev = NULL;
3416 struct tevent_req *req = NULL;
3417 NTSTATUS status = NT_STATUS_OK;
3418
3419 if (cli_has_async_calls(cli)) {
3420 /*
3421 * Can't use sync call while an async call is in flight
3422 */
3423 status = NT_STATUS_INVALID_PARAMETER;
3424 goto fail;
3425 }
3426
3427 ev = event_context_init(frame);
3428 if (ev == NULL) {
3429 status = NT_STATUS_NO_MEMORY;
3430 goto fail;
3431 }
3432
3433 req = cli_setattrE_send(frame, ev,
3434 cli,
3435 fnum,
3436 change_time,
3437 access_time,
3438 write_time);
3439
3440 if (req == NULL) {
3441 status = NT_STATUS_NO_MEMORY;
3442 goto fail;
3443 }
3444
3445 if (!tevent_req_poll(req, ev)) {
3446 status = map_nt_error_from_unix(errno);
3447 goto fail;
3448 }
3449
3450 status = cli_setattrE_recv(req);
3451
3452 fail:
3453 TALLOC_FREE(frame);
3454 return status;
3455}
3456
3457/****************************************************************************
3458 Do a SMBsetatr call.
3459****************************************************************************/
3460
3461static void cli_setatr_done(struct tevent_req *subreq);
3462
3463struct cli_setatr_state {
3464 uint16_t vwv[8];
3465};
3466
3467struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3468 struct event_context *ev,
3469 struct cli_state *cli,
3470 const char *fname,
3471 uint16_t attr,
3472 time_t mtime)
3473{
3474 struct tevent_req *req = NULL, *subreq = NULL;
3475 struct cli_setatr_state *state = NULL;
3476 uint8_t additional_flags = 0;
3477 uint8_t *bytes = NULL;
3478
3479 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3480 if (req == NULL) {
3481 return NULL;
3482 }
3483
3484 SSVAL(state->vwv+0, 0, attr);
3485 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3486
3487 bytes = talloc_array(state, uint8_t, 1);
3488 if (tevent_req_nomem(bytes, req)) {
3489 return tevent_req_post(req, ev);
3490 }
3491 bytes[0] = 4;
3492 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3493 strlen(fname)+1, NULL);
3494 if (tevent_req_nomem(bytes, req)) {
3495 return tevent_req_post(req, ev);
3496 }
3497 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3498 talloc_get_size(bytes)+1);
3499 if (tevent_req_nomem(bytes, req)) {
3500 return tevent_req_post(req, ev);
3501 }
3502
3503 bytes[talloc_get_size(bytes)-1] = 4;
3504 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3505 1, NULL);
3506 if (tevent_req_nomem(bytes, req)) {
3507 return tevent_req_post(req, ev);
3508 }
3509
3510 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3511 8, state->vwv, talloc_get_size(bytes), bytes);
3512 if (tevent_req_nomem(subreq, req)) {
3513 return tevent_req_post(req, ev);
3514 }
3515 tevent_req_set_callback(subreq, cli_setatr_done, req);
3516 return req;
3517}
3518
3519static void cli_setatr_done(struct tevent_req *subreq)
3520{
3521 struct tevent_req *req = tevent_req_callback_data(
3522 subreq, struct tevent_req);
3523 NTSTATUS status;
3524
3525 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3526 TALLOC_FREE(subreq);
3527 if (tevent_req_nterror(req, status)) {
3528 return;
3529 }
3530 tevent_req_done(req);
3531}
3532
3533NTSTATUS cli_setatr_recv(struct tevent_req *req)
3534{
3535 return tevent_req_simple_recv_ntstatus(req);
3536}
3537
3538NTSTATUS cli_setatr(struct cli_state *cli,
3539 const char *fname,
3540 uint16_t attr,
3541 time_t mtime)
3542{
3543 TALLOC_CTX *frame = talloc_stackframe();
3544 struct event_context *ev = NULL;
3545 struct tevent_req *req = NULL;
3546 NTSTATUS status = NT_STATUS_OK;
3547
3548 if (cli_has_async_calls(cli)) {
3549 /*
3550 * Can't use sync call while an async call is in flight
3551 */
3552 status = NT_STATUS_INVALID_PARAMETER;
3553 goto fail;
3554 }
3555
3556 ev = event_context_init(frame);
3557 if (ev == NULL) {
3558 status = NT_STATUS_NO_MEMORY;
3559 goto fail;
3560 }
3561
3562 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3563 if (req == NULL) {
3564 status = NT_STATUS_NO_MEMORY;
3565 goto fail;
3566 }
3567
3568 if (!tevent_req_poll(req, ev)) {
3569 status = map_nt_error_from_unix(errno);
3570 goto fail;
3571 }
3572
3573 status = cli_setatr_recv(req);
3574
3575 fail:
3576 TALLOC_FREE(frame);
3577 return status;
3578}
3579
3580/****************************************************************************
3581 Check for existance of a dir.
3582****************************************************************************/
3583
3584static void cli_chkpath_done(struct tevent_req *subreq);
3585
3586struct cli_chkpath_state {
3587 int dummy;
3588};
3589
3590struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3591 struct event_context *ev,
3592 struct cli_state *cli,
3593 const char *fname)
3594{
3595 struct tevent_req *req = NULL, *subreq = NULL;
3596 struct cli_chkpath_state *state = NULL;
3597 uint8_t additional_flags = 0;
3598 uint8_t *bytes = NULL;
3599
3600 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3601 if (req == NULL) {
3602 return NULL;
3603 }
3604
3605 bytes = talloc_array(state, uint8_t, 1);
3606 if (tevent_req_nomem(bytes, req)) {
3607 return tevent_req_post(req, ev);
3608 }
3609 bytes[0] = 4;
3610 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3611 strlen(fname)+1, NULL);
3612
3613 if (tevent_req_nomem(bytes, req)) {
3614 return tevent_req_post(req, ev);
3615 }
3616
3617 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3618 0, NULL, talloc_get_size(bytes), bytes);
3619 if (tevent_req_nomem(subreq, req)) {
3620 return tevent_req_post(req, ev);
3621 }
3622 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3623 return req;
3624}
3625
3626static void cli_chkpath_done(struct tevent_req *subreq)
3627{
3628 struct tevent_req *req = tevent_req_callback_data(
3629 subreq, struct tevent_req);
3630 NTSTATUS status;
3631
3632 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3633 TALLOC_FREE(subreq);
3634 if (tevent_req_nterror(req, status)) {
3635 return;
3636 }
3637 tevent_req_done(req);
3638}
3639
3640NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3641{
3642 return tevent_req_simple_recv_ntstatus(req);
3643}
3644
3645NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3646{
3647 TALLOC_CTX *frame = talloc_stackframe();
3648 struct event_context *ev = NULL;
3649 struct tevent_req *req = NULL;
3650 char *path2 = NULL;
3651 NTSTATUS status = NT_STATUS_OK;
3652
3653 if (cli_has_async_calls(cli)) {
3654 /*
3655 * Can't use sync call while an async call is in flight
3656 */
3657 status = NT_STATUS_INVALID_PARAMETER;
3658 goto fail;
3659 }
3660
3661 path2 = talloc_strdup(frame, path);
3662 if (!path2) {
3663 status = NT_STATUS_NO_MEMORY;
3664 goto fail;
3665 }
3666 trim_char(path2,'\0','\\');
3667 if (!*path2) {
3668 path2 = talloc_strdup(frame, "\\");
3669 if (!path2) {
3670 status = NT_STATUS_NO_MEMORY;
3671 goto fail;
3672 }
3673 }
3674
3675 ev = event_context_init(frame);
3676 if (ev == NULL) {
3677 status = NT_STATUS_NO_MEMORY;
3678 goto fail;
3679 }
3680
3681 req = cli_chkpath_send(frame, ev, cli, path2);
3682 if (req == NULL) {
3683 status = NT_STATUS_NO_MEMORY;
3684 goto fail;
3685 }
3686
3687 if (!tevent_req_poll(req, ev)) {
3688 status = map_nt_error_from_unix(errno);
3689 goto fail;
3690 }
3691
3692 status = cli_chkpath_recv(req);
3693
3694 fail:
3695 TALLOC_FREE(frame);
3696 return status;
3697}
3698
3699/****************************************************************************
3700 Query disk space.
3701****************************************************************************/
3702
3703static void cli_dskattr_done(struct tevent_req *subreq);
3704
3705struct cli_dskattr_state {
3706 int bsize;
3707 int total;
3708 int avail;
3709};
3710
3711struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3712 struct event_context *ev,
3713 struct cli_state *cli)
3714{
3715 struct tevent_req *req = NULL, *subreq = NULL;
3716 struct cli_dskattr_state *state = NULL;
3717 uint8_t additional_flags = 0;
3718
3719 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3720 if (req == NULL) {
3721 return NULL;
3722 }
3723
3724 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3725 0, NULL, 0, NULL);
3726 if (tevent_req_nomem(subreq, req)) {
3727 return tevent_req_post(req, ev);
3728 }
3729 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3730 return req;
3731}
3732
3733static void cli_dskattr_done(struct tevent_req *subreq)
3734{
3735 struct tevent_req *req = tevent_req_callback_data(
3736 subreq, struct tevent_req);
3737 struct cli_dskattr_state *state = tevent_req_data(
3738 req, struct cli_dskattr_state);
3739 uint8_t wct;
3740 uint16_t *vwv = NULL;
3741 uint8_t *inbuf;
3742 NTSTATUS status;
3743
3744 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3745 NULL);
3746 TALLOC_FREE(subreq);
3747 if (tevent_req_nterror(req, status)) {
3748 return;
3749 }
3750 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3751 state->total = SVAL(vwv+0, 0);
3752 state->avail = SVAL(vwv+3, 0);
3753 tevent_req_done(req);
3754}
3755
3756NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3757{
3758 struct cli_dskattr_state *state = tevent_req_data(
3759 req, struct cli_dskattr_state);
3760 NTSTATUS status;
3761
3762 if (tevent_req_is_nterror(req, &status)) {
3763 return status;
3764 }
3765 *bsize = state->bsize;
3766 *total = state->total;
3767 *avail = state->avail;
3768 return NT_STATUS_OK;
3769}
3770
3771NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3772{
3773 TALLOC_CTX *frame = talloc_stackframe();
3774 struct event_context *ev = NULL;
3775 struct tevent_req *req = NULL;
3776 NTSTATUS status = NT_STATUS_OK;
3777
3778 if (cli_has_async_calls(cli)) {
3779 /*
3780 * Can't use sync call while an async call is in flight
3781 */
3782 status = NT_STATUS_INVALID_PARAMETER;
3783 goto fail;
3784 }
3785
3786 ev = event_context_init(frame);
3787 if (ev == NULL) {
3788 status = NT_STATUS_NO_MEMORY;
3789 goto fail;
3790 }
3791
3792 req = cli_dskattr_send(frame, ev, cli);
3793 if (req == NULL) {
3794 status = NT_STATUS_NO_MEMORY;
3795 goto fail;
3796 }
3797
3798 if (!tevent_req_poll(req, ev)) {
3799 status = map_nt_error_from_unix(errno);
3800 goto fail;
3801 }
3802
3803 status = cli_dskattr_recv(req, bsize, total, avail);
3804
3805 fail:
3806 TALLOC_FREE(frame);
3807 return status;
3808}
3809
3810/****************************************************************************
3811 Create and open a temporary file.
3812****************************************************************************/
3813
3814static void cli_ctemp_done(struct tevent_req *subreq);
3815
3816struct ctemp_state {
3817 uint16_t vwv[3];
3818 char *ret_path;
3819 uint16_t fnum;
3820};
3821
3822struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3823 struct event_context *ev,
3824 struct cli_state *cli,
3825 const char *path)
3826{
3827 struct tevent_req *req = NULL, *subreq = NULL;
3828 struct ctemp_state *state = NULL;
3829 uint8_t additional_flags = 0;
3830 uint8_t *bytes = NULL;
3831
3832 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3833 if (req == NULL) {
3834 return NULL;
3835 }
3836
3837 SSVAL(state->vwv,0,0);
3838 SIVALS(state->vwv+1,0,-1);
3839
3840 bytes = talloc_array(state, uint8_t, 1);
3841 if (tevent_req_nomem(bytes, req)) {
3842 return tevent_req_post(req, ev);
3843 }
3844 bytes[0] = 4;
3845 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3846 strlen(path)+1, NULL);
3847 if (tevent_req_nomem(bytes, req)) {
3848 return tevent_req_post(req, ev);
3849 }
3850
3851 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3852 3, state->vwv, talloc_get_size(bytes), bytes);
3853 if (tevent_req_nomem(subreq, req)) {
3854 return tevent_req_post(req, ev);
3855 }
3856 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3857 return req;
3858}
3859
3860static void cli_ctemp_done(struct tevent_req *subreq)
3861{
3862 struct tevent_req *req = tevent_req_callback_data(
3863 subreq, struct tevent_req);
3864 struct ctemp_state *state = tevent_req_data(
3865 req, struct ctemp_state);
3866 NTSTATUS status;
3867 uint8_t wcnt;
3868 uint16_t *vwv;
3869 uint32_t num_bytes = 0;
3870 uint8_t *bytes = NULL;
3871 uint8_t *inbuf;
3872
3873 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3874 &num_bytes, &bytes);
3875 TALLOC_FREE(subreq);
3876 if (tevent_req_nterror(req, status)) {
3877 return;
3878 }
3879
3880 state->fnum = SVAL(vwv+0, 0);
3881
3882 /* From W2K3, the result is just the ASCII name */
3883 if (num_bytes < 2) {
3884 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3885 return;
3886 }
3887
3888 if (pull_string_talloc(state,
3889 NULL,
3890 0,
3891 &state->ret_path,
3892 bytes,
3893 num_bytes,
3894 STR_ASCII) == 0) {
3895 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3896 return;
3897 }
3898 tevent_req_done(req);
3899}
3900
3901NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3902 TALLOC_CTX *ctx,
3903 uint16_t *pfnum,
3904 char **outfile)
3905{
3906 struct ctemp_state *state = tevent_req_data(req,
3907 struct ctemp_state);
3908 NTSTATUS status;
3909
3910 if (tevent_req_is_nterror(req, &status)) {
3911 return status;
3912 }
3913 *pfnum = state->fnum;
3914 *outfile = talloc_strdup(ctx, state->ret_path);
3915 if (!*outfile) {
3916 return NT_STATUS_NO_MEMORY;
3917 }
3918 return NT_STATUS_OK;
3919}
3920
3921NTSTATUS cli_ctemp(struct cli_state *cli,
3922 TALLOC_CTX *ctx,
3923 const char *path,
3924 uint16_t *pfnum,
3925 char **out_path)
3926{
3927 TALLOC_CTX *frame = talloc_stackframe();
3928 struct event_context *ev;
3929 struct tevent_req *req;
3930 NTSTATUS status = NT_STATUS_OK;
3931
3932 if (cli_has_async_calls(cli)) {
3933 /*
3934 * Can't use sync call while an async call is in flight
3935 */
3936 status = NT_STATUS_INVALID_PARAMETER;
3937 goto fail;
3938 }
3939
3940 ev = event_context_init(frame);
3941 if (ev == NULL) {
3942 status = NT_STATUS_NO_MEMORY;
3943 goto fail;
3944 }
3945
3946 req = cli_ctemp_send(frame, ev, cli, path);
3947 if (req == NULL) {
3948 status = NT_STATUS_NO_MEMORY;
3949 goto fail;
3950 }
3951
3952 if (!tevent_req_poll(req, ev)) {
3953 status = map_nt_error_from_unix(errno);
3954 goto fail;
3955 }
3956
3957 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3958
3959 fail:
3960 TALLOC_FREE(frame);
3961 return status;
3962}
3963
3964/*
3965 send a raw ioctl - used by the torture code
3966*/
3967NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3968{
3969 uint16_t vwv[3];
3970 NTSTATUS status;
3971
3972 SSVAL(vwv+0, 0, fnum);
3973 SSVAL(vwv+1, 0, code>>16);
3974 SSVAL(vwv+2, 0, (code&0xFFFF));
3975
3976 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
3977 NULL, 0, NULL, NULL, NULL, NULL);
3978 if (!NT_STATUS_IS_OK(status)) {
3979 return status;
3980 }
3981 *blob = data_blob_null;
3982 return NT_STATUS_OK;
3983}
3984
3985/*********************************************************
3986 Set an extended attribute utility fn.
3987*********************************************************/
3988
3989static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
3990 uint8_t *param, unsigned int param_len,
3991 const char *ea_name,
3992 const char *ea_val, size_t ea_len)
3993{
3994 uint16_t setup[1];
3995 unsigned int data_len = 0;
3996 uint8_t *data = NULL;
3997 char *p;
3998 size_t ea_namelen = strlen(ea_name);
3999 NTSTATUS status;
4000
4001 SSVAL(setup, 0, setup_val);
4002
4003 if (ea_namelen == 0 && ea_len == 0) {
4004 data_len = 4;
4005 data = (uint8_t *)SMB_MALLOC(data_len);
4006 if (!data) {
4007 return NT_STATUS_NO_MEMORY;
4008 }
4009 p = (char *)data;
4010 SIVAL(p,0,data_len);
4011 } else {
4012 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4013 data = (uint8_t *)SMB_MALLOC(data_len);
4014 if (!data) {
4015 return NT_STATUS_NO_MEMORY;
4016 }
4017 p = (char *)data;
4018 SIVAL(p,0,data_len);
4019 p += 4;
4020 SCVAL(p, 0, 0); /* EA flags. */
4021 SCVAL(p, 1, ea_namelen);
4022 SSVAL(p, 2, ea_len);
4023 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4024 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4025 }
4026
4027 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4028 setup, 1, 0,
4029 param, param_len, 2,
4030 data, data_len, cli->max_xmit,
4031 NULL,
4032 NULL, 0, NULL, /* rsetup */
4033 NULL, 0, NULL, /* rparam */
4034 NULL, 0, NULL); /* rdata */
4035 SAFE_FREE(data);
4036 return status;
4037}
4038
4039/*********************************************************
4040 Set an extended attribute on a pathname.
4041*********************************************************/
4042
4043NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4044 const char *ea_name, const char *ea_val,
4045 size_t ea_len)
4046{
4047 unsigned int param_len = 0;
4048 uint8_t *param;
4049 size_t srclen = 2*(strlen(path)+1);
4050 char *p;
4051 NTSTATUS status;
4052
4053 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4054 if (!param) {
4055 return NT_STATUS_NO_MEMORY;
4056 }
4057 memset(param, '\0', 6);
4058 SSVAL(param,0,SMB_INFO_SET_EA);
4059 p = (char *)(&param[6]);
4060
4061 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4062 param_len = PTR_DIFF(p, param);
4063
4064 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4065 ea_name, ea_val, ea_len);
4066 SAFE_FREE(param);
4067 return status;
4068}
4069
4070/*********************************************************
4071 Set an extended attribute on an fnum.
4072*********************************************************/
4073
4074NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4075 const char *ea_name, const char *ea_val,
4076 size_t ea_len)
4077{
4078 uint8_t param[6];
4079
4080 memset(param, 0, 6);
4081 SSVAL(param,0,fnum);
4082 SSVAL(param,2,SMB_INFO_SET_EA);
4083
4084 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4085 ea_name, ea_val, ea_len);
4086}
4087
4088/*********************************************************
4089 Get an extended attribute list utility fn.
4090*********************************************************/
4091
4092static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4093 size_t rdata_len,
4094 size_t *pnum_eas, struct ea_struct **pea_list)
4095{
4096 struct ea_struct *ea_list = NULL;
4097 size_t num_eas;
4098 size_t ea_size;
4099 const uint8_t *p;
4100
4101 if (rdata_len < 4) {
4102 return false;
4103 }
4104
4105 ea_size = (size_t)IVAL(rdata,0);
4106 if (ea_size > rdata_len) {
4107 return false;
4108 }
4109
4110 if (ea_size == 0) {
4111 /* No EA's present. */
4112 *pnum_eas = 0;
4113 *pea_list = NULL;
4114 return true;
4115 }
4116
4117 p = rdata + 4;
4118 ea_size -= 4;
4119
4120 /* Validate the EA list and count it. */
4121 for (num_eas = 0; ea_size >= 4; num_eas++) {
4122 unsigned int ea_namelen = CVAL(p,1);
4123 unsigned int ea_valuelen = SVAL(p,2);
4124 if (ea_namelen == 0) {
4125 return false;
4126 }
4127 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4128 return false;
4129 }
4130 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4131 p += 4 + ea_namelen + 1 + ea_valuelen;
4132 }
4133
4134 if (num_eas == 0) {
4135 *pnum_eas = 0;
4136 *pea_list = NULL;
4137 return true;
4138 }
4139
4140 *pnum_eas = num_eas;
4141 if (!pea_list) {
4142 /* Caller only wants number of EA's. */
4143 return true;
4144 }
4145
4146 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4147 if (!ea_list) {
4148 return false;
4149 }
4150
4151 ea_size = (size_t)IVAL(rdata,0);
4152 p = rdata + 4;
4153
4154 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4155 struct ea_struct *ea = &ea_list[num_eas];
4156 fstring unix_ea_name;
4157 unsigned int ea_namelen = CVAL(p,1);
4158 unsigned int ea_valuelen = SVAL(p,2);
4159
4160 ea->flags = CVAL(p,0);
4161 unix_ea_name[0] = '\0';
4162 pull_ascii_fstring(unix_ea_name, p + 4);
4163 ea->name = talloc_strdup(ea_list, unix_ea_name);
4164 if (!ea->name) {
4165 goto fail;
4166 }
4167 /* Ensure the value is null terminated (in case it's a string). */
4168 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4169 if (!ea->value.data) {
4170 goto fail;
4171 }
4172 if (ea_valuelen) {
4173 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4174 }
4175 ea->value.data[ea_valuelen] = 0;
4176 ea->value.length--;
4177 p += 4 + ea_namelen + 1 + ea_valuelen;
4178 }
4179
4180 *pea_list = ea_list;
4181 return true;
4182
4183fail:
4184 TALLOC_FREE(ea_list);
4185 return false;
4186}
4187
4188/*********************************************************
4189 Get an extended attribute list from a pathname.
4190*********************************************************/
4191
4192struct cli_get_ea_list_path_state {
4193 uint32_t num_data;
4194 uint8_t *data;
4195};
4196
4197static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4198
4199struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4200 struct tevent_context *ev,
4201 struct cli_state *cli,
4202 const char *fname)
4203{
4204 struct tevent_req *req, *subreq;
4205 struct cli_get_ea_list_path_state *state;
4206
4207 req = tevent_req_create(mem_ctx, &state,
4208 struct cli_get_ea_list_path_state);
4209 if (req == NULL) {
4210 return NULL;
4211 }
4212 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4213 SMB_INFO_QUERY_ALL_EAS, 4,
4214 cli->max_xmit);
4215// @todo_port in earlier port the above cli->max_xmit was changed to CLI_BUFFER_SIZE
4216// @todo_port is that still needed? if no regressions are found please remove it
4217 if (tevent_req_nomem(subreq, req)) {
4218 return tevent_req_post(req, ev);
4219 }
4220 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4221 return req;
4222}
4223
4224static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4225{
4226 struct tevent_req *req = tevent_req_callback_data(
4227 subreq, struct tevent_req);
4228 struct cli_get_ea_list_path_state *state = tevent_req_data(
4229 req, struct cli_get_ea_list_path_state);
4230 NTSTATUS status;
4231
4232 status = cli_qpathinfo_recv(subreq, state, &state->data,
4233 &state->num_data);
4234 TALLOC_FREE(subreq);
4235 if (tevent_req_nterror(req, status)) {
4236 return;
4237 }
4238 tevent_req_done(req);
4239}
4240
4241NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4242 size_t *pnum_eas, struct ea_struct **peas)
4243{
4244 struct cli_get_ea_list_path_state *state = tevent_req_data(
4245 req, struct cli_get_ea_list_path_state);
4246 NTSTATUS status;
4247
4248 if (tevent_req_is_nterror(req, &status)) {
4249 return status;
4250 }
4251 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4252 pnum_eas, peas)) {
4253 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4254 }
4255 return NT_STATUS_OK;
4256}
4257
4258NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4259 TALLOC_CTX *ctx,
4260 size_t *pnum_eas,
4261 struct ea_struct **pea_list)
4262{
4263 TALLOC_CTX *frame = talloc_stackframe();
4264 struct event_context *ev = NULL;
4265 struct tevent_req *req = NULL;
4266 NTSTATUS status = NT_STATUS_NO_MEMORY;
4267
4268 if (cli_has_async_calls(cli)) {
4269 /*
4270 * Can't use sync call while an async call is in flight
4271 */
4272 status = NT_STATUS_INVALID_PARAMETER;
4273 goto fail;
4274 }
4275 ev = event_context_init(frame);
4276 if (ev == NULL) {
4277 goto fail;
4278 }
4279 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4280 if (req == NULL) {
4281 goto fail;
4282 }
4283 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4284 goto fail;
4285 }
4286 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4287 fail:
4288 TALLOC_FREE(frame);
4289 return status;
4290}
4291
4292/****************************************************************************
4293 Convert open "flags" arg to uint32_t on wire.
4294****************************************************************************/
4295
4296static uint32_t open_flags_to_wire(int flags)
4297{
4298 int open_mode = flags & O_ACCMODE;
4299 uint32_t ret = 0;
4300
4301 switch (open_mode) {
4302 case O_WRONLY:
4303 ret |= SMB_O_WRONLY;
4304 break;
4305 case O_RDWR:
4306 ret |= SMB_O_RDWR;
4307 break;
4308 default:
4309 case O_RDONLY:
4310 ret |= SMB_O_RDONLY;
4311 break;
4312 }
4313
4314 if (flags & O_CREAT) {
4315 ret |= SMB_O_CREAT;
4316 }
4317 if (flags & O_EXCL) {
4318 ret |= SMB_O_EXCL;
4319 }
4320 if (flags & O_TRUNC) {
4321 ret |= SMB_O_TRUNC;
4322 }
4323#if defined(O_SYNC)
4324 if (flags & O_SYNC) {
4325 ret |= SMB_O_SYNC;
4326 }
4327#endif /* O_SYNC */
4328 if (flags & O_APPEND) {
4329 ret |= SMB_O_APPEND;
4330 }
4331#if defined(O_DIRECT)
4332 if (flags & O_DIRECT) {
4333 ret |= SMB_O_DIRECT;
4334 }
4335#endif
4336#if defined(O_DIRECTORY)
4337 if (flags & O_DIRECTORY) {
4338 ret |= SMB_O_DIRECTORY;
4339 }
4340#endif
4341 return ret;
4342}
4343
4344/****************************************************************************
4345 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4346****************************************************************************/
4347
4348struct posix_open_state {
4349 uint16_t setup;
4350 uint8_t *param;
4351 uint8_t data[18];
4352 uint16_t fnum; /* Out */
4353};
4354
4355static void cli_posix_open_internal_done(struct tevent_req *subreq)
4356{
4357 struct tevent_req *req = tevent_req_callback_data(
4358 subreq, struct tevent_req);
4359 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4360 NTSTATUS status;
4361 uint8_t *data;
4362 uint32_t num_data;
4363
4364 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4365 NULL, 0, NULL, &data, 12, &num_data);
4366 TALLOC_FREE(subreq);
4367 if (tevent_req_nterror(req, status)) {
4368 return;
4369 }
4370 state->fnum = SVAL(data,2);
4371 tevent_req_done(req);
4372}
4373
4374static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4375 struct event_context *ev,
4376 struct cli_state *cli,
4377 const char *fname,
4378 int flags,
4379 mode_t mode,
4380 bool is_dir)
4381{
4382 struct tevent_req *req = NULL, *subreq = NULL;
4383 struct posix_open_state *state = NULL;
4384 uint32_t wire_flags = open_flags_to_wire(flags);
4385
4386 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4387 if (req == NULL) {
4388 return NULL;
4389 }
4390
4391 /* Setup setup word. */
4392 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4393
4394 /* Setup param array. */
4395 state->param = talloc_array(state, uint8_t, 6);
4396 if (tevent_req_nomem(state->param, req)) {
4397 return tevent_req_post(req, ev);
4398 }
4399 memset(state->param, '\0', 6);
4400 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4401
4402 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4403 strlen(fname)+1, NULL);
4404
4405 if (tevent_req_nomem(state->param, req)) {
4406 return tevent_req_post(req, ev);
4407 }
4408
4409 /* Setup data words. */
4410 if (is_dir) {
4411 wire_flags |= SMB_O_DIRECTORY;
4412 }
4413
4414 SIVAL(state->data,0,0); /* No oplock. */
4415 SIVAL(state->data,4,wire_flags);
4416 SIVAL(state->data,8,unix_perms_to_wire(mode));
4417 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4418 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4419
4420 subreq = cli_trans_send(state, /* mem ctx. */
4421 ev, /* event ctx. */
4422 cli, /* cli_state. */
4423 SMBtrans2, /* cmd. */
4424 NULL, /* pipe name. */
4425 -1, /* fid. */
4426 0, /* function. */
4427 0, /* flags. */
4428 &state->setup, /* setup. */
4429 1, /* num setup uint16_t words. */
4430 0, /* max returned setup. */
4431 state->param, /* param. */
4432 talloc_get_size(state->param),/* num param. */
4433 2, /* max returned param. */
4434 state->data, /* data. */
4435 18, /* num data. */
4436 12); /* max returned data. */
4437
4438 if (tevent_req_nomem(subreq, req)) {
4439 return tevent_req_post(req, ev);
4440 }
4441 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4442 return req;
4443}
4444
4445struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4446 struct event_context *ev,
4447 struct cli_state *cli,
4448 const char *fname,
4449 int flags,
4450 mode_t mode)
4451{
4452 return cli_posix_open_internal_send(mem_ctx, ev,
4453 cli, fname, flags, mode, false);
4454}
4455
4456NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4457{
4458 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4459 NTSTATUS status;
4460
4461 if (tevent_req_is_nterror(req, &status)) {
4462 return status;
4463 }
4464 *pfnum = state->fnum;
4465 return NT_STATUS_OK;
4466}
4467
4468/****************************************************************************
4469 Open - POSIX semantics. Doesn't request oplock.
4470****************************************************************************/
4471
4472NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4473 int flags, mode_t mode, uint16_t *pfnum)
4474{
4475
4476 TALLOC_CTX *frame = talloc_stackframe();
4477 struct event_context *ev = NULL;
4478 struct tevent_req *req = NULL;
4479 NTSTATUS status = NT_STATUS_OK;
4480
4481 if (cli_has_async_calls(cli)) {
4482 /*
4483 * Can't use sync call while an async call is in flight
4484 */
4485 status = NT_STATUS_INVALID_PARAMETER;
4486 goto fail;
4487 }
4488
4489 ev = event_context_init(frame);
4490 if (ev == NULL) {
4491 status = NT_STATUS_NO_MEMORY;
4492 goto fail;
4493 }
4494
4495 req = cli_posix_open_send(frame,
4496 ev,
4497 cli,
4498 fname,
4499 flags,
4500 mode);
4501 if (req == NULL) {
4502 status = NT_STATUS_NO_MEMORY;
4503 goto fail;
4504 }
4505
4506 if (!tevent_req_poll(req, ev)) {
4507 status = map_nt_error_from_unix(errno);
4508 goto fail;
4509 }
4510
4511 status = cli_posix_open_recv(req, pfnum);
4512
4513 fail:
4514 TALLOC_FREE(frame);
4515 return status;
4516}
4517
4518struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4519 struct event_context *ev,
4520 struct cli_state *cli,
4521 const char *fname,
4522 mode_t mode)
4523{
4524 return cli_posix_open_internal_send(mem_ctx, ev,
4525 cli, fname, O_CREAT, mode, true);
4526}
4527
4528NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4529{
4530 return tevent_req_simple_recv_ntstatus(req);
4531}
4532
4533NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4534{
4535 TALLOC_CTX *frame = talloc_stackframe();
4536 struct event_context *ev = NULL;
4537 struct tevent_req *req = NULL;
4538 NTSTATUS status = NT_STATUS_OK;
4539
4540 if (cli_has_async_calls(cli)) {
4541 /*
4542 * Can't use sync call while an async call is in flight
4543 */
4544 status = NT_STATUS_INVALID_PARAMETER;
4545 goto fail;
4546 }
4547
4548 ev = event_context_init(frame);
4549 if (ev == NULL) {
4550 status = NT_STATUS_NO_MEMORY;
4551 goto fail;
4552 }
4553
4554 req = cli_posix_mkdir_send(frame,
4555 ev,
4556 cli,
4557 fname,
4558 mode);
4559 if (req == NULL) {
4560 status = NT_STATUS_NO_MEMORY;
4561 goto fail;
4562 }
4563
4564 if (!tevent_req_poll(req, ev)) {
4565 status = map_nt_error_from_unix(errno);
4566 goto fail;
4567 }
4568
4569 status = cli_posix_mkdir_recv(req);
4570
4571 fail:
4572 TALLOC_FREE(frame);
4573 return status;
4574}
4575
4576/****************************************************************************
4577 unlink or rmdir - POSIX semantics.
4578****************************************************************************/
4579
4580struct cli_posix_unlink_internal_state {
4581 uint8_t data[2];
4582};
4583
4584static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4585
4586static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4587 struct event_context *ev,
4588 struct cli_state *cli,
4589 const char *fname,
4590 uint16_t level)
4591{
4592 struct tevent_req *req = NULL, *subreq = NULL;
4593 struct cli_posix_unlink_internal_state *state = NULL;
4594
4595 req = tevent_req_create(mem_ctx, &state,
4596 struct cli_posix_unlink_internal_state);
4597 if (req == NULL) {
4598 return NULL;
4599 }
4600
4601 /* Setup data word. */
4602 SSVAL(state->data, 0, level);
4603
4604 subreq = cli_setpathinfo_send(state, ev, cli,
4605 SMB_POSIX_PATH_UNLINK,
4606 fname,
4607 state->data, sizeof(state->data));
4608 if (tevent_req_nomem(subreq, req)) {
4609 return tevent_req_post(req, ev);
4610 }
4611 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4612 return req;
4613}
4614
4615static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4616{
4617 NTSTATUS status = cli_setpathinfo_recv(subreq);
4618 tevent_req_simple_finish_ntstatus(subreq, status);
4619}
4620
4621struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4622 struct event_context *ev,
4623 struct cli_state *cli,
4624 const char *fname)
4625{
4626 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4627 SMB_POSIX_UNLINK_FILE_TARGET);
4628}
4629
4630NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4631{
4632 return tevent_req_simple_recv_ntstatus(req);
4633}
4634
4635/****************************************************************************
4636 unlink - POSIX semantics.
4637****************************************************************************/
4638
4639NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4640{
4641 TALLOC_CTX *frame = talloc_stackframe();
4642 struct event_context *ev = NULL;
4643 struct tevent_req *req = NULL;
4644 NTSTATUS status = NT_STATUS_OK;
4645
4646 if (cli_has_async_calls(cli)) {
4647 /*
4648 * Can't use sync call while an async call is in flight
4649 */
4650 status = NT_STATUS_INVALID_PARAMETER;
4651 goto fail;
4652 }
4653
4654 ev = event_context_init(frame);
4655 if (ev == NULL) {
4656 status = NT_STATUS_NO_MEMORY;
4657 goto fail;
4658 }
4659
4660 req = cli_posix_unlink_send(frame,
4661 ev,
4662 cli,
4663 fname);
4664 if (req == NULL) {
4665 status = NT_STATUS_NO_MEMORY;
4666 goto fail;
4667 }
4668
4669 if (!tevent_req_poll(req, ev)) {
4670 status = map_nt_error_from_unix(errno);
4671 goto fail;
4672 }
4673
4674 status = cli_posix_unlink_recv(req);
4675
4676 fail:
4677 TALLOC_FREE(frame);
4678 return status;
4679}
4680
4681/****************************************************************************
4682 rmdir - POSIX semantics.
4683****************************************************************************/
4684
4685struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4686 struct event_context *ev,
4687 struct cli_state *cli,
4688 const char *fname)
4689{
4690 return cli_posix_unlink_internal_send(
4691 mem_ctx, ev, cli, fname,
4692 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4693}
4694
4695NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4696{
4697 return tevent_req_simple_recv_ntstatus(req);
4698}
4699
4700NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4701{
4702 TALLOC_CTX *frame = talloc_stackframe();
4703 struct event_context *ev = NULL;
4704 struct tevent_req *req = NULL;
4705 NTSTATUS status = NT_STATUS_OK;
4706
4707 if (cli_has_async_calls(cli)) {
4708 /*
4709 * Can't use sync call while an async call is in flight
4710 */
4711 status = NT_STATUS_INVALID_PARAMETER;
4712 goto fail;
4713 }
4714
4715 ev = event_context_init(frame);
4716 if (ev == NULL) {
4717 status = NT_STATUS_NO_MEMORY;
4718 goto fail;
4719 }
4720
4721 req = cli_posix_rmdir_send(frame,
4722 ev,
4723 cli,
4724 fname);
4725 if (req == NULL) {
4726 status = NT_STATUS_NO_MEMORY;
4727 goto fail;
4728 }
4729
4730 if (!tevent_req_poll(req, ev)) {
4731 status = map_nt_error_from_unix(errno);
4732 goto fail;
4733 }
4734
4735 status = cli_posix_rmdir_recv(req, frame);
4736
4737 fail:
4738 TALLOC_FREE(frame);
4739 return status;
4740}
4741
4742/****************************************************************************
4743 filechangenotify
4744****************************************************************************/
4745
4746struct cli_notify_state {
4747 uint8_t setup[8];
4748 uint32_t num_changes;
4749 struct notify_change *changes;
4750};
4751
4752static void cli_notify_done(struct tevent_req *subreq);
4753
4754struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4755 struct tevent_context *ev,
4756 struct cli_state *cli, uint16_t fnum,
4757 uint32_t buffer_size,
4758 uint32_t completion_filter, bool recursive)
4759{
4760 struct tevent_req *req, *subreq;
4761 struct cli_notify_state *state;
4762
4763 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4764 if (req == NULL) {
4765 return NULL;
4766 }
4767
4768 SIVAL(state->setup, 0, completion_filter);
4769 SSVAL(state->setup, 4, fnum);
4770 SSVAL(state->setup, 6, recursive);
4771
4772 subreq = cli_trans_send(
4773 state, /* mem ctx. */
4774 ev, /* event ctx. */
4775 cli, /* cli_state. */
4776 SMBnttrans, /* cmd. */
4777 NULL, /* pipe name. */
4778 -1, /* fid. */
4779 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4780 0, /* flags. */
4781 (uint16_t *)state->setup, /* setup. */
4782 4, /* num setup uint16_t words. */
4783 0, /* max returned setup. */
4784 NULL, /* param. */
4785 0, /* num param. */
4786 buffer_size, /* max returned param. */
4787 NULL, /* data. */
4788 0, /* num data. */
4789 0); /* max returned data. */
4790
4791 if (tevent_req_nomem(subreq, req)) {
4792 return tevent_req_post(req, ev);
4793 }
4794 tevent_req_set_callback(subreq, cli_notify_done, req);
4795 return req;
4796}
4797
4798static void cli_notify_done(struct tevent_req *subreq)
4799{
4800 struct tevent_req *req = tevent_req_callback_data(
4801 subreq, struct tevent_req);
4802 struct cli_notify_state *state = tevent_req_data(
4803 req, struct cli_notify_state);
4804 NTSTATUS status;
4805 uint8_t *params;
4806 uint32_t i, ofs, num_params;
4807 uint16_t flags2;
4808
4809 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4810 &params, 0, &num_params, NULL, 0, NULL);
4811 TALLOC_FREE(subreq);
4812 if (tevent_req_nterror(req, status)) {
4813 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4814 return;
4815 }
4816
4817 state->num_changes = 0;
4818 ofs = 0;
4819
4820 while (num_params - ofs > 12) {
4821 uint32_t len = IVAL(params, ofs);
4822 state->num_changes += 1;
4823
4824 if ((len == 0) || (ofs+len >= num_params)) {
4825 break;
4826 }
4827 ofs += len;
4828 }
4829
4830 state->changes = talloc_array(state, struct notify_change,
4831 state->num_changes);
4832 if (tevent_req_nomem(state->changes, req)) {
4833 TALLOC_FREE(params);
4834 return;
4835 }
4836
4837 ofs = 0;
4838
4839 for (i=0; i<state->num_changes; i++) {
4840 uint32_t next = IVAL(params, ofs);
4841 uint32_t len = IVAL(params, ofs+8);
4842 ssize_t ret;
4843 char *name;
4844
4845 if ((next != 0) && (len+12 != next)) {
4846 TALLOC_FREE(params);
4847 tevent_req_nterror(
4848 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4849 return;
4850 }
4851
4852 state->changes[i].action = IVAL(params, ofs+4);
4853 ret = clistr_pull_talloc(params, (char *)params, flags2,
4854 &name, params+ofs+12, len,
4855 STR_TERMINATE|STR_UNICODE);
4856 if (ret == -1) {
4857 TALLOC_FREE(params);
4858 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4859 return;
4860 }
4861 state->changes[i].name = name;
4862 ofs += next;
4863 }
4864
4865 TALLOC_FREE(params);
4866 tevent_req_done(req);
4867}
4868
4869NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4870 uint32_t *pnum_changes,
4871 struct notify_change **pchanges)
4872{
4873 struct cli_notify_state *state = tevent_req_data(
4874 req, struct cli_notify_state);
4875 NTSTATUS status;
4876
4877 if (tevent_req_is_nterror(req, &status)) {
4878 return status;
4879 }
4880
4881 *pnum_changes = state->num_changes;
4882 *pchanges = talloc_move(mem_ctx, &state->changes);
4883 return NT_STATUS_OK;
4884}
4885
4886struct cli_qpathinfo_state {
4887 uint8_t *param;
4888 uint8_t *data;
4889 uint16_t setup[1];
4890 uint32_t min_rdata;
4891 uint8_t *rdata;
4892 uint32_t num_rdata;
4893};
4894
4895static void cli_qpathinfo_done(struct tevent_req *subreq);
4896
4897struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4898 struct tevent_context *ev,
4899 struct cli_state *cli, const char *fname,
4900 uint16_t level, uint32_t min_rdata,
4901 uint32_t max_rdata)
4902{
4903 struct tevent_req *req, *subreq;
4904 struct cli_qpathinfo_state *state;
4905
4906 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4907 if (req == NULL) {
4908 return NULL;
4909 }
4910 state->min_rdata = min_rdata;
4911 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4912
4913 state->param = talloc_zero_array(state, uint8_t, 6);
4914 if (tevent_req_nomem(state->param, req)) {
4915 return tevent_req_post(req, ev);
4916 }
4917 SSVAL(state->param, 0, level);
4918 state->param = trans2_bytes_push_str(
4919 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4920 if (tevent_req_nomem(state->param, req)) {
4921 return tevent_req_post(req, ev);
4922 }
4923
4924 subreq = cli_trans_send(
4925 state, /* mem ctx. */
4926 ev, /* event ctx. */
4927 cli, /* cli_state. */
4928 SMBtrans2, /* cmd. */
4929 NULL, /* pipe name. */
4930 -1, /* fid. */
4931 0, /* function. */
4932 0, /* flags. */
4933 state->setup, /* setup. */
4934 1, /* num setup uint16_t words. */
4935 0, /* max returned setup. */
4936 state->param, /* param. */
4937 talloc_get_size(state->param), /* num param. */
4938 2, /* max returned param. */
4939 NULL, /* data. */
4940 0, /* num data. */
4941 max_rdata); /* max returned data. */
4942
4943 if (tevent_req_nomem(subreq, req)) {
4944 return tevent_req_post(req, ev);
4945 }
4946 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4947 return req;
4948}
4949
4950static void cli_qpathinfo_done(struct tevent_req *subreq)
4951{
4952 struct tevent_req *req = tevent_req_callback_data(
4953 subreq, struct tevent_req);
4954 struct cli_qpathinfo_state *state = tevent_req_data(
4955 req, struct cli_qpathinfo_state);
4956 NTSTATUS status;
4957
4958 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4959 NULL, 0, NULL,
4960 &state->rdata, state->min_rdata,
4961 &state->num_rdata);
4962 if (tevent_req_nterror(req, status)) {
4963 return;
4964 }
4965 tevent_req_done(req);
4966}
4967
4968NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4969 uint8_t **rdata, uint32_t *num_rdata)
4970{
4971 struct cli_qpathinfo_state *state = tevent_req_data(
4972 req, struct cli_qpathinfo_state);
4973 NTSTATUS status;
4974
4975 if (tevent_req_is_nterror(req, &status)) {
4976 return status;
4977 }
4978 if (rdata != NULL) {
4979 *rdata = talloc_move(mem_ctx, &state->rdata);
4980 } else {
4981 TALLOC_FREE(state->rdata);
4982 }
4983 if (num_rdata != NULL) {
4984 *num_rdata = state->num_rdata;
4985 }
4986 return NT_STATUS_OK;
4987}
4988
4989NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
4990 const char *fname, uint16_t level, uint32_t min_rdata,
4991 uint32_t max_rdata,
4992 uint8_t **rdata, uint32_t *num_rdata)
4993{
4994 TALLOC_CTX *frame = talloc_stackframe();
4995 struct event_context *ev;
4996 struct tevent_req *req;
4997 NTSTATUS status = NT_STATUS_NO_MEMORY;
4998
4999 if (cli_has_async_calls(cli)) {
5000 /*
5001 * Can't use sync call while an async call is in flight
5002 */
5003 status = NT_STATUS_INVALID_PARAMETER;
5004 goto fail;
5005 }
5006 ev = event_context_init(frame);
5007 if (ev == NULL) {
5008 goto fail;
5009 }
5010 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5011 max_rdata);
5012 if (req == NULL) {
5013 goto fail;
5014 }
5015 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5016 goto fail;
5017 }
5018 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5019 fail:
5020 TALLOC_FREE(frame);
5021 return status;
5022}
5023
5024struct cli_qfileinfo_state {
5025 uint16_t setup[1];
5026 uint8_t param[4];
5027 uint8_t *data;
5028 uint32_t min_rdata;
5029 uint8_t *rdata;
5030 uint32_t num_rdata;
5031};
5032
5033static void cli_qfileinfo_done(struct tevent_req *subreq);
5034
5035struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5036 struct tevent_context *ev,
5037 struct cli_state *cli, uint16_t fnum,
5038 uint16_t level, uint32_t min_rdata,
5039 uint32_t max_rdata)
5040{
5041 struct tevent_req *req, *subreq;
5042 struct cli_qfileinfo_state *state;
5043
5044 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5045 if (req == NULL) {
5046 return NULL;
5047 }
5048 state->min_rdata = min_rdata;
5049 SSVAL(state->param, 0, fnum);
5050 SSVAL(state->param, 2, level);
5051 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5052
5053 subreq = cli_trans_send(
5054 state, /* mem ctx. */
5055 ev, /* event ctx. */
5056 cli, /* cli_state. */
5057 SMBtrans2, /* cmd. */
5058 NULL, /* pipe name. */
5059 -1, /* fid. */
5060 0, /* function. */
5061 0, /* flags. */
5062 state->setup, /* setup. */
5063 1, /* num setup uint16_t words. */
5064 0, /* max returned setup. */
5065 state->param, /* param. */
5066 sizeof(state->param), /* num param. */
5067 2, /* max returned param. */
5068 NULL, /* data. */
5069 0, /* num data. */
5070 max_rdata); /* max returned data. */
5071
5072 if (tevent_req_nomem(subreq, req)) {
5073 return tevent_req_post(req, ev);
5074 }
5075 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5076 return req;
5077}
5078
5079static void cli_qfileinfo_done(struct tevent_req *subreq)
5080{
5081 struct tevent_req *req = tevent_req_callback_data(
5082 subreq, struct tevent_req);
5083 struct cli_qfileinfo_state *state = tevent_req_data(
5084 req, struct cli_qfileinfo_state);
5085 NTSTATUS status;
5086
5087 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5088 NULL, 0, NULL,
5089 &state->rdata, state->min_rdata,
5090 &state->num_rdata);
5091 if (tevent_req_nterror(req, status)) {
5092 return;
5093 }
5094 tevent_req_done(req);
5095}
5096
5097NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5098 uint8_t **rdata, uint32_t *num_rdata)
5099{
5100 struct cli_qfileinfo_state *state = tevent_req_data(
5101 req, struct cli_qfileinfo_state);
5102 NTSTATUS status;
5103
5104 if (tevent_req_is_nterror(req, &status)) {
5105 return status;
5106 }
5107 if (rdata != NULL) {
5108 *rdata = talloc_move(mem_ctx, &state->rdata);
5109 } else {
5110 TALLOC_FREE(state->rdata);
5111 }
5112 if (num_rdata != NULL) {
5113 *num_rdata = state->num_rdata;
5114 }
5115 return NT_STATUS_OK;
5116}
5117
5118NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5119 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5120 uint32_t max_rdata,
5121 uint8_t **rdata, uint32_t *num_rdata)
5122{
5123 TALLOC_CTX *frame = talloc_stackframe();
5124 struct event_context *ev;
5125 struct tevent_req *req;
5126 NTSTATUS status = NT_STATUS_NO_MEMORY;
5127
5128 if (cli_has_async_calls(cli)) {
5129 /*
5130 * Can't use sync call while an async call is in flight
5131 */
5132 status = NT_STATUS_INVALID_PARAMETER;
5133 goto fail;
5134 }
5135 ev = event_context_init(frame);
5136 if (ev == NULL) {
5137 goto fail;
5138 }
5139 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5140 max_rdata);
5141 if (req == NULL) {
5142 goto fail;
5143 }
5144 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5145 goto fail;
5146 }
5147 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5148 fail:
5149 TALLOC_FREE(frame);
5150 return status;
5151}
5152
5153struct cli_flush_state {
5154 uint16_t vwv[1];
5155};
5156
5157static void cli_flush_done(struct tevent_req *subreq);
5158
5159struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5160 struct event_context *ev,
5161 struct cli_state *cli,
5162 uint16_t fnum)
5163{
5164 struct tevent_req *req, *subreq;
5165 struct cli_flush_state *state;
5166
5167 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5168 if (req == NULL) {
5169 return NULL;
5170 }
5171 SSVAL(state->vwv + 0, 0, fnum);
5172
5173 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5174 0, NULL);
5175 if (tevent_req_nomem(subreq, req)) {
5176 return tevent_req_post(req, ev);
5177 }
5178 tevent_req_set_callback(subreq, cli_flush_done, req);
5179 return req;
5180}
5181
5182static void cli_flush_done(struct tevent_req *subreq)
5183{
5184 struct tevent_req *req = tevent_req_callback_data(
5185 subreq, struct tevent_req);
5186 NTSTATUS status;
5187
5188 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5189 TALLOC_FREE(subreq);
5190 if (tevent_req_nterror(req, status)) {
5191 return;
5192 }
5193 tevent_req_done(req);
5194}
5195
5196NTSTATUS cli_flush_recv(struct tevent_req *req)
5197{
5198 return tevent_req_simple_recv_ntstatus(req);
5199}
5200
5201NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5202{
5203 TALLOC_CTX *frame = talloc_stackframe();
5204 struct event_context *ev;
5205 struct tevent_req *req;
5206 NTSTATUS status = NT_STATUS_NO_MEMORY;
5207
5208 if (cli_has_async_calls(cli)) {
5209 /*
5210 * Can't use sync call while an async call is in flight
5211 */
5212 status = NT_STATUS_INVALID_PARAMETER;
5213 goto fail;
5214 }
5215 ev = event_context_init(frame);
5216 if (ev == NULL) {
5217 goto fail;
5218 }
5219 req = cli_flush_send(frame, ev, cli, fnum);
5220 if (req == NULL) {
5221 goto fail;
5222 }
5223 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5224 goto fail;
5225 }
5226 status = cli_flush_recv(req);
5227 fail:
5228 TALLOC_FREE(frame);
5229 return status;
5230}
5231
5232struct cli_shadow_copy_data_state {
5233 uint16_t setup[4];
5234 uint8_t *data;
5235 uint32_t num_data;
5236 bool get_names;
5237};
5238
5239static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5240
5241struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5242 struct tevent_context *ev,
5243 struct cli_state *cli,
5244 uint16_t fnum,
5245 bool get_names)
5246{
5247 struct tevent_req *req, *subreq;
5248 struct cli_shadow_copy_data_state *state;
5249 uint32_t ret_size;
5250
5251 req = tevent_req_create(mem_ctx, &state,
5252 struct cli_shadow_copy_data_state);
5253 if (req == NULL) {
5254 return NULL;
5255 }
5256 state->get_names = get_names;
5257 ret_size = get_names ? cli->max_xmit : 16;
5258
5259 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5260 SSVAL(state->setup + 2, 0, fnum);
5261 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5262 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5263
5264 subreq = cli_trans_send(
5265 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5266 state->setup, ARRAY_SIZE(state->setup), 0,
5267 NULL, 0, 0,
5268 NULL, 0, ret_size);
5269 if (tevent_req_nomem(subreq, req)) {
5270 return tevent_req_post(req, ev);
5271 }
5272 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5273 return req;
5274}
5275
5276static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5277{
5278 struct tevent_req *req = tevent_req_callback_data(
5279 subreq, struct tevent_req);
5280 struct cli_shadow_copy_data_state *state = tevent_req_data(
5281 req, struct cli_shadow_copy_data_state);
5282 NTSTATUS status;
5283
5284 status = cli_trans_recv(subreq, state, NULL,
5285 NULL, 0, NULL, /* setup */
5286 NULL, 0, NULL, /* param */
5287 &state->data, 12, &state->num_data);
5288 TALLOC_FREE(subreq);
5289 if (tevent_req_nterror(req, status)) {
5290 return;
5291 }
5292 tevent_req_done(req);
5293}
5294
5295NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5296 char ***pnames, int *pnum_names)
5297{
5298 struct cli_shadow_copy_data_state *state = tevent_req_data(
5299 req, struct cli_shadow_copy_data_state);
5300 char **names;
5301 int i, num_names;
5302 uint32_t dlength;
5303 NTSTATUS status;
5304
5305 if (tevent_req_is_nterror(req, &status)) {
5306 return status;
5307 }
5308 num_names = IVAL(state->data, 4);
5309 dlength = IVAL(state->data, 8);
5310
5311 if (!state->get_names) {
5312 *pnum_names = num_names;
5313 return NT_STATUS_OK;
5314 }
5315
5316 if (dlength+12 > state->num_data) {
5317 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5318 }
5319 names = talloc_array(mem_ctx, char *, num_names);
5320 if (names == NULL) {
5321 return NT_STATUS_NO_MEMORY;
5322 }
5323
5324 for (i=0; i<num_names; i++) {
5325 bool ret;
5326 uint8_t *src;
5327 size_t converted_size;
5328
5329 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5330 ret = convert_string_talloc(
5331 names, CH_UTF16LE, CH_UNIX,
5332 src, 2 * sizeof(SHADOW_COPY_LABEL),
5333 &names[i], &converted_size, True);
5334 if (!ret) {
5335 TALLOC_FREE(names);
5336 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5337 }
5338 }
5339 *pnum_names = num_names;
5340 *pnames = names;
5341 return NT_STATUS_OK;
5342}
5343
5344NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5345 uint16_t fnum, bool get_names,
5346 char ***pnames, int *pnum_names)
5347{
5348 TALLOC_CTX *frame = talloc_stackframe();
5349 struct event_context *ev;
5350 struct tevent_req *req;
5351 NTSTATUS status = NT_STATUS_NO_MEMORY;
5352
5353 if (cli_has_async_calls(cli)) {
5354 /*
5355 * Can't use sync call while an async call is in flight
5356 */
5357 status = NT_STATUS_INVALID_PARAMETER;
5358 goto fail;
5359 }
5360 ev = event_context_init(frame);
5361 if (ev == NULL) {
5362 goto fail;
5363 }
5364 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5365 if (req == NULL) {
5366 goto fail;
5367 }
5368 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5369 goto fail;
5370 }
5371 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5372 fail:
5373 TALLOC_FREE(frame);
5374 return status;
5375}
Note: See TracBrowser for help on using the repository browser.