More async calls in libsmb/clifile.c
[Samba.git] / source3 / libsmb / clifile.c
blobe055a88000ba058d10edd7b58a888b57bc480db2
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
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.
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.
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/>.
21 #include "includes.h"
23 /****************************************************************************
24 Hard/Symlink a file (UNIX extensions).
25 Creates new name (sym)linked to oldname.
26 ****************************************************************************/
28 static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
30 unsigned int data_len = 0;
31 unsigned int param_len = 0;
32 uint16_t setup = TRANSACT2_SETPATHINFO;
33 char *param;
34 char *data;
35 char *rparam=NULL, *rdata=NULL;
36 char *p;
37 size_t oldlen = 2*(strlen(oldname)+1);
38 size_t newlen = 2*(strlen(newname)+1);
40 param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
42 if (!param) {
43 return false;
46 data = SMB_MALLOC_ARRAY(char, oldlen+2);
48 if (!data) {
49 SAFE_FREE(param);
50 return false;
53 SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
54 SIVAL(param,2,0);
55 p = &param[6];
57 p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
58 param_len = PTR_DIFF(p, param);
60 p = data;
61 p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
62 data_len = PTR_DIFF(p, data);
64 if (!cli_send_trans(cli, SMBtrans2,
65 NULL, /* name */
66 -1, 0, /* fid, flags */
67 &setup, 1, 0, /* setup, length, max */
68 param, param_len, 2, /* param, length, max */
69 data, data_len, cli->max_xmit /* data, length, max */
70 )) {
71 SAFE_FREE(data);
72 SAFE_FREE(param);
73 return false;
76 SAFE_FREE(data);
77 SAFE_FREE(param);
79 if (!cli_receive_trans(cli, SMBtrans2,
80 &rparam, &param_len,
81 &rdata, &data_len)) {
82 return false;
85 SAFE_FREE(data);
86 SAFE_FREE(param);
87 SAFE_FREE(rdata);
88 SAFE_FREE(rparam);
90 return true;
93 /****************************************************************************
94 Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
97 uint32_t unix_perms_to_wire(mode_t perms)
99 unsigned int ret = 0;
101 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
102 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
103 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
104 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
105 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
106 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
107 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
108 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
109 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
110 #ifdef S_ISVTX
111 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
112 #endif
113 #ifdef S_ISGID
114 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
115 #endif
116 #ifdef S_ISUID
117 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
118 #endif
119 return ret;
122 /****************************************************************************
123 Map wire permissions to standard UNIX.
124 ****************************************************************************/
126 mode_t wire_perms_to_unix(uint32_t perms)
128 mode_t ret = (mode_t)0;
130 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
131 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
132 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
133 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
134 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
135 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
136 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
137 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
138 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
139 #ifdef S_ISVTX
140 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
141 #endif
142 #ifdef S_ISGID
143 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
144 #endif
145 #ifdef S_ISUID
146 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
147 #endif
148 return ret;
151 /****************************************************************************
152 Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
155 static mode_t unix_filetype_from_wire(uint32_t wire_type)
157 switch (wire_type) {
158 case UNIX_TYPE_FILE:
159 return S_IFREG;
160 case UNIX_TYPE_DIR:
161 return S_IFDIR;
162 #ifdef S_IFLNK
163 case UNIX_TYPE_SYMLINK:
164 return S_IFLNK;
165 #endif
166 #ifdef S_IFCHR
167 case UNIX_TYPE_CHARDEV:
168 return S_IFCHR;
169 #endif
170 #ifdef S_IFBLK
171 case UNIX_TYPE_BLKDEV:
172 return S_IFBLK;
173 #endif
174 #ifdef S_IFIFO
175 case UNIX_TYPE_FIFO:
176 return S_IFIFO;
177 #endif
178 #ifdef S_IFSOCK
179 case UNIX_TYPE_SOCKET:
180 return S_IFSOCK;
181 #endif
182 default:
183 return (mode_t)0;
187 /****************************************************************************
188 Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
191 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
193 unsigned int param_len = 0;
194 unsigned int data_len = 0;
195 uint16_t setup = TRANSACT2_QPATHINFO;
196 char *param;
197 size_t nlen = 2*(strlen(name)+1);
198 char *rparam=NULL, *rdata=NULL;
199 char *p;
201 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
202 if (!param) {
203 return false;
206 p = param;
207 memset(p, '\0', 6);
208 SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
209 p += 6;
210 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
211 param_len = PTR_DIFF(p, param);
213 if (!cli_send_trans(cli, SMBtrans2,
214 NULL, /* name */
215 -1, 0, /* fid, flags */
216 &setup, 1, 0, /* setup, length, max */
217 param, param_len, 2, /* param, length, max */
218 NULL, 0, cli->max_xmit /* data, length, max */
219 )) {
220 SAFE_FREE(param);
221 return false;
224 SAFE_FREE(param);
226 if (!cli_receive_trans(cli, SMBtrans2,
227 &rparam, &param_len,
228 &rdata, &data_len)) {
229 return false;
232 if (data_len < 6) {
233 SAFE_FREE(rdata);
234 SAFE_FREE(rparam);
235 return false;
238 SAFE_FREE(rparam);
239 *retbuf = rdata;
240 *prb_size = (size_t)data_len;
242 return true;
245 /****************************************************************************
246 Stat a file (UNIX extensions).
247 ****************************************************************************/
249 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
251 unsigned int param_len = 0;
252 unsigned int data_len = 0;
253 uint16_t setup = TRANSACT2_QPATHINFO;
254 char *param;
255 size_t nlen = 2*(strlen(name)+1);
256 char *rparam=NULL, *rdata=NULL;
257 char *p;
259 ZERO_STRUCTP(sbuf);
261 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
262 if (!param) {
263 return false;
265 p = param;
266 memset(p, '\0', 6);
267 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
268 p += 6;
269 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
270 param_len = PTR_DIFF(p, param);
272 if (!cli_send_trans(cli, SMBtrans2,
273 NULL, /* name */
274 -1, 0, /* fid, flags */
275 &setup, 1, 0, /* setup, length, max */
276 param, param_len, 2, /* param, length, max */
277 NULL, 0, cli->max_xmit /* data, length, max */
278 )) {
279 SAFE_FREE(param);
280 return false;
283 SAFE_FREE(param);
285 if (!cli_receive_trans(cli, SMBtrans2,
286 &rparam, &param_len,
287 &rdata, &data_len)) {
288 return false;
291 if (data_len < 96) {
292 SAFE_FREE(rdata);
293 SAFE_FREE(rparam);
294 return false;
297 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
298 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
301 #else
302 /* assume 512 byte blocks */
303 sbuf->st_blocks /= 512;
304 #endif
305 set_ctimespec(sbuf, interpret_long_date(rdata + 16)); /* time of last change */
306 set_atimespec(sbuf, interpret_long_date(rdata + 24)); /* time of last access */
307 set_mtimespec(sbuf, interpret_long_date(rdata + 32)); /* time of last modification */
309 sbuf->st_uid = (uid_t) IVAL(rdata,40); /* user ID of owner */
310 sbuf->st_gid = (gid_t) IVAL(rdata,48); /* group ID of owner */
311 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
312 #if defined(HAVE_MAKEDEV)
314 uint32_t dev_major = IVAL(rdata,60);
315 uint32_t dev_minor = IVAL(rdata,68);
316 sbuf->st_rdev = makedev(dev_major, dev_minor);
318 #endif
319 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
320 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
321 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
323 SAFE_FREE(rdata);
324 SAFE_FREE(rparam);
326 return true;
329 /****************************************************************************
330 Symlink a file (UNIX extensions).
331 ****************************************************************************/
333 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
335 return cli_link_internal(cli, oldname, newname, False);
338 /****************************************************************************
339 Hard a file (UNIX extensions).
340 ****************************************************************************/
342 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
344 return cli_link_internal(cli, oldname, newname, True);
347 /****************************************************************************
348 Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
351 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
353 unsigned int data_len = 0;
354 unsigned int param_len = 0;
355 uint16_t setup = TRANSACT2_SETPATHINFO;
356 size_t nlen = 2*(strlen(fname)+1);
357 char *param;
358 char data[100];
359 char *rparam=NULL, *rdata=NULL;
360 char *p;
362 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
363 if (!param) {
364 return false;
366 memset(param, '\0', 6);
367 memset(data, 0, sizeof(data));
369 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
370 p = &param[6];
372 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
373 param_len = PTR_DIFF(p, param);
375 memset(data, 0xff, 40); /* Set all sizes/times to no change. */
377 SIVAL(data,40,uid);
378 SIVAL(data,48,gid);
379 SIVAL(data,84,mode);
381 data_len = 100;
383 if (!cli_send_trans(cli, SMBtrans2,
384 NULL, /* name */
385 -1, 0, /* fid, flags */
386 &setup, 1, 0, /* setup, length, max */
387 param, param_len, 2, /* param, length, max */
388 (char *)&data, data_len, cli->max_xmit /* data, length, max */
389 )) {
390 SAFE_FREE(param);
391 return False;
394 SAFE_FREE(param);
396 if (!cli_receive_trans(cli, SMBtrans2,
397 &rparam, &param_len,
398 &rdata, &data_len)) {
399 return false;
402 SAFE_FREE(rdata);
403 SAFE_FREE(rparam);
405 return true;
408 /****************************************************************************
409 chmod a file (UNIX extensions).
410 ****************************************************************************/
412 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
414 return cli_unix_chmod_chown_internal(cli, fname,
415 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
418 /****************************************************************************
419 chown a file (UNIX extensions).
420 ****************************************************************************/
422 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
424 return cli_unix_chmod_chown_internal(cli, fname,
425 SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
428 /****************************************************************************
429 Rename a file.
430 ****************************************************************************/
432 static void cli_rename_done(struct tevent_req *subreq);
434 struct cli_rename_state {
435 uint16_t vwv[1];
438 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
439 struct event_context *ev,
440 struct cli_state *cli,
441 const char *fname_src,
442 const char *fname_dst)
444 struct tevent_req *req = NULL, *subreq = NULL;
445 struct cli_rename_state *state = NULL;
446 uint8_t additional_flags = 0;
447 uint8_t *bytes = NULL;
449 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
450 if (req == NULL) {
451 return NULL;
454 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
456 bytes = talloc_array(state, uint8_t, 1);
457 if (tevent_req_nomem(bytes, req)) {
458 return tevent_req_post(req, ev);
460 bytes[0] = 4;
461 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
462 strlen(fname_src)+1, NULL);
463 if (tevent_req_nomem(bytes, req)) {
464 return tevent_req_post(req, ev);
467 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
468 talloc_get_size(bytes)+1);
469 if (tevent_req_nomem(bytes, req)) {
470 return tevent_req_post(req, ev);
473 bytes[talloc_get_size(bytes)-1] = 4;
474 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
475 strlen(fname_dst)+1, NULL);
476 if (tevent_req_nomem(bytes, req)) {
477 return tevent_req_post(req, ev);
480 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
481 1, state->vwv, talloc_get_size(bytes), bytes);
482 if (tevent_req_nomem(subreq, req)) {
483 return tevent_req_post(req, ev);
485 tevent_req_set_callback(subreq, cli_rename_done, req);
486 return req;
489 static void cli_rename_done(struct tevent_req *subreq)
491 struct tevent_req *req = tevent_req_callback_data(
492 subreq, struct tevent_req);
493 NTSTATUS status;
495 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
496 TALLOC_FREE(subreq);
497 if (!NT_STATUS_IS_OK(status)) {
498 tevent_req_nterror(req, status);
499 return;
501 tevent_req_done(req);
504 NTSTATUS cli_rename_recv(struct tevent_req *req)
506 return tevent_req_simple_recv_ntstatus(req);
509 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
511 TALLOC_CTX *frame = talloc_stackframe();
512 struct event_context *ev;
513 struct tevent_req *req;
514 NTSTATUS status = NT_STATUS_OK;
516 if (cli_has_async_calls(cli)) {
518 * Can't use sync call while an async call is in flight
520 status = NT_STATUS_INVALID_PARAMETER;
521 goto fail;
524 ev = event_context_init(frame);
525 if (ev == NULL) {
526 status = NT_STATUS_NO_MEMORY;
527 goto fail;
530 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
531 if (req == NULL) {
532 status = NT_STATUS_NO_MEMORY;
533 goto fail;
536 if (!tevent_req_poll(req, ev)) {
537 status = map_nt_error_from_unix(errno);
538 goto fail;
541 status = cli_rename_recv(req);
543 fail:
544 TALLOC_FREE(frame);
545 if (!NT_STATUS_IS_OK(status)) {
546 cli_set_error(cli, status);
548 return status;
551 /****************************************************************************
552 NT Rename a file.
553 ****************************************************************************/
555 static void cli_ntrename_done(struct tevent_req *subreq);
557 struct cli_ntrename_state {
558 uint16_t vwv[4];
561 static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
562 struct event_context *ev,
563 struct cli_state *cli,
564 const char *fname_src,
565 const char *fname_dst,
566 uint16_t rename_flag)
568 struct tevent_req *req = NULL, *subreq = NULL;
569 struct cli_ntrename_state *state = NULL;
570 uint8_t additional_flags = 0;
571 uint8_t *bytes = NULL;
573 req = tevent_req_create(mem_ctx, &state, struct cli_ntrename_state);
574 if (req == NULL) {
575 return NULL;
578 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
579 SSVAL(state->vwv+1, 0, rename_flag);
581 bytes = talloc_array(state, uint8_t, 1);
582 if (tevent_req_nomem(bytes, req)) {
583 return tevent_req_post(req, ev);
585 bytes[0] = 4;
586 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
587 strlen(fname_src)+1, NULL);
588 if (tevent_req_nomem(bytes, req)) {
589 return tevent_req_post(req, ev);
592 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
593 talloc_get_size(bytes)+1);
594 if (tevent_req_nomem(bytes, req)) {
595 return tevent_req_post(req, ev);
598 bytes[talloc_get_size(bytes)-1] = 4;
599 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
600 strlen(fname_dst)+1, NULL);
601 if (tevent_req_nomem(bytes, req)) {
602 return tevent_req_post(req, ev);
605 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
606 4, state->vwv, talloc_get_size(bytes), bytes);
607 if (tevent_req_nomem(subreq, req)) {
608 return tevent_req_post(req, ev);
610 tevent_req_set_callback(subreq, cli_ntrename_done, req);
611 return req;
614 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
615 struct event_context *ev,
616 struct cli_state *cli,
617 const char *fname_src,
618 const char *fname_dst)
620 return cli_ntrename_send_internal(mem_ctx,
622 cli,
623 fname_src,
624 fname_dst,
625 RENAME_FLAG_RENAME);
628 static void cli_ntrename_done(struct tevent_req *subreq)
630 struct tevent_req *req = tevent_req_callback_data(
631 subreq, struct tevent_req);
632 NTSTATUS status;
634 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
635 TALLOC_FREE(subreq);
636 if (!NT_STATUS_IS_OK(status)) {
637 tevent_req_nterror(req, status);
638 return;
640 tevent_req_done(req);
643 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
645 return tevent_req_simple_recv_ntstatus(req);
648 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
650 TALLOC_CTX *frame = talloc_stackframe();
651 struct event_context *ev;
652 struct tevent_req *req;
653 NTSTATUS status = NT_STATUS_OK;
655 if (cli_has_async_calls(cli)) {
657 * Can't use sync call while an async call is in flight
659 status = NT_STATUS_INVALID_PARAMETER;
660 goto fail;
663 ev = event_context_init(frame);
664 if (ev == NULL) {
665 status = NT_STATUS_NO_MEMORY;
666 goto fail;
669 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
670 if (req == NULL) {
671 status = NT_STATUS_NO_MEMORY;
672 goto fail;
675 if (!tevent_req_poll(req, ev)) {
676 status = map_nt_error_from_unix(errno);
677 goto fail;
680 status = cli_ntrename_recv(req);
682 fail:
683 TALLOC_FREE(frame);
684 if (!NT_STATUS_IS_OK(status)) {
685 cli_set_error(cli, status);
687 return status;
690 /****************************************************************************
691 NT hardlink a file.
692 ****************************************************************************/
694 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
695 struct event_context *ev,
696 struct cli_state *cli,
697 const char *fname_src,
698 const char *fname_dst)
700 return cli_ntrename_send_internal(mem_ctx,
702 cli,
703 fname_src,
704 fname_dst,
705 RENAME_FLAG_HARD_LINK);
708 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
710 return tevent_req_simple_recv_ntstatus(req);
713 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
715 TALLOC_CTX *frame = talloc_stackframe();
716 struct event_context *ev;
717 struct tevent_req *req;
718 NTSTATUS status = NT_STATUS_OK;
720 if (cli_has_async_calls(cli)) {
722 * Can't use sync call while an async call is in flight
724 status = NT_STATUS_INVALID_PARAMETER;
725 goto fail;
728 ev = event_context_init(frame);
729 if (ev == NULL) {
730 status = NT_STATUS_NO_MEMORY;
731 goto fail;
734 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
735 if (req == NULL) {
736 status = NT_STATUS_NO_MEMORY;
737 goto fail;
740 if (!tevent_req_poll(req, ev)) {
741 status = map_nt_error_from_unix(errno);
742 goto fail;
745 status = cli_nt_hardlink_recv(req);
747 fail:
748 TALLOC_FREE(frame);
749 if (!NT_STATUS_IS_OK(status)) {
750 cli_set_error(cli, status);
752 return status;
755 /****************************************************************************
756 Delete a file.
757 ****************************************************************************/
759 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16_t attrs)
761 char *p;
763 memset(cli->outbuf,'\0',smb_size);
764 memset(cli->inbuf,'\0',smb_size);
766 cli_set_message(cli->outbuf,1, 0, true);
768 SCVAL(cli->outbuf,smb_com,SMBunlink);
769 SSVAL(cli->outbuf,smb_tid,cli->cnum);
770 cli_setup_packet(cli);
772 SSVAL(cli->outbuf,smb_vwv0, attrs);
774 p = smb_buf(cli->outbuf);
775 *p++ = 4;
776 p += clistr_push(cli, p, fname,
777 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
779 cli_setup_bcc(cli, p);
780 cli_send_smb(cli);
781 if (!cli_receive_smb(cli)) {
782 return false;
785 if (cli_is_error(cli)) {
786 return false;
789 return true;
792 /****************************************************************************
793 Delete a file.
794 ****************************************************************************/
796 bool cli_unlink(struct cli_state *cli, const char *fname)
798 return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
801 /****************************************************************************
802 Create a directory.
803 ****************************************************************************/
805 static void cli_mkdir_done(struct tevent_req *subreq);
807 struct cli_mkdir_state {
808 int dummy;
811 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
812 struct event_context *ev,
813 struct cli_state *cli,
814 const char *dname)
816 struct tevent_req *req = NULL, *subreq = NULL;
817 struct cli_mkdir_state *state = NULL;
818 uint8_t additional_flags = 0;
819 uint8_t *bytes = NULL;
821 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
822 if (req == NULL) {
823 return NULL;
826 bytes = talloc_array(state, uint8_t, 1);
827 if (tevent_req_nomem(bytes, req)) {
828 return tevent_req_post(req, ev);
830 bytes[0] = 4;
831 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
832 strlen(dname)+1, NULL);
834 if (tevent_req_nomem(bytes, req)) {
835 return tevent_req_post(req, ev);
838 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
839 0, NULL, talloc_get_size(bytes), bytes);
840 if (tevent_req_nomem(subreq, req)) {
841 return tevent_req_post(req, ev);
843 tevent_req_set_callback(subreq, cli_mkdir_done, req);
844 return req;
847 static void cli_mkdir_done(struct tevent_req *subreq)
849 struct tevent_req *req = tevent_req_callback_data(
850 subreq, struct tevent_req);
851 NTSTATUS status;
853 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
854 TALLOC_FREE(subreq);
855 if (!NT_STATUS_IS_OK(status)) {
856 tevent_req_nterror(req, status);
857 return;
859 tevent_req_done(req);
862 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
864 return tevent_req_simple_recv_ntstatus(req);
867 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
869 TALLOC_CTX *frame = talloc_stackframe();
870 struct event_context *ev;
871 struct tevent_req *req;
872 NTSTATUS status = NT_STATUS_OK;
874 if (cli_has_async_calls(cli)) {
876 * Can't use sync call while an async call is in flight
878 status = NT_STATUS_INVALID_PARAMETER;
879 goto fail;
882 ev = event_context_init(frame);
883 if (ev == NULL) {
884 status = NT_STATUS_NO_MEMORY;
885 goto fail;
888 req = cli_mkdir_send(frame, ev, cli, dname);
889 if (req == NULL) {
890 status = NT_STATUS_NO_MEMORY;
891 goto fail;
894 if (!tevent_req_poll(req, ev)) {
895 status = map_nt_error_from_unix(errno);
896 goto fail;
899 status = cli_mkdir_recv(req);
901 fail:
902 TALLOC_FREE(frame);
903 if (!NT_STATUS_IS_OK(status)) {
904 cli_set_error(cli, status);
906 return status;
909 /****************************************************************************
910 Remove a directory.
911 ****************************************************************************/
913 static void cli_rmdir_done(struct tevent_req *subreq);
915 struct cli_rmdir_state {
916 int dummy;
919 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
920 struct event_context *ev,
921 struct cli_state *cli,
922 const char *dname)
924 struct tevent_req *req = NULL, *subreq = NULL;
925 struct cli_rmdir_state *state = NULL;
926 uint8_t additional_flags = 0;
927 uint8_t *bytes = NULL;
929 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
930 if (req == NULL) {
931 return NULL;
934 bytes = talloc_array(state, uint8_t, 1);
935 if (tevent_req_nomem(bytes, req)) {
936 return tevent_req_post(req, ev);
938 bytes[0] = 4;
939 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
940 strlen(dname)+1, NULL);
942 if (tevent_req_nomem(bytes, req)) {
943 return tevent_req_post(req, ev);
946 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
947 0, NULL, talloc_get_size(bytes), bytes);
948 if (tevent_req_nomem(subreq, req)) {
949 return tevent_req_post(req, ev);
951 tevent_req_set_callback(subreq, cli_rmdir_done, req);
952 return req;
955 static void cli_rmdir_done(struct tevent_req *subreq)
957 struct tevent_req *req = tevent_req_callback_data(
958 subreq, struct tevent_req);
959 NTSTATUS status;
961 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
962 TALLOC_FREE(subreq);
963 if (!NT_STATUS_IS_OK(status)) {
964 tevent_req_nterror(req, status);
965 return;
967 tevent_req_done(req);
970 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
972 return tevent_req_simple_recv_ntstatus(req);
975 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
977 TALLOC_CTX *frame = talloc_stackframe();
978 struct event_context *ev;
979 struct tevent_req *req;
980 NTSTATUS status = NT_STATUS_OK;
982 if (cli_has_async_calls(cli)) {
984 * Can't use sync call while an async call is in flight
986 status = NT_STATUS_INVALID_PARAMETER;
987 goto fail;
990 ev = event_context_init(frame);
991 if (ev == NULL) {
992 status = NT_STATUS_NO_MEMORY;
993 goto fail;
996 req = cli_rmdir_send(frame, ev, cli, dname);
997 if (req == NULL) {
998 status = NT_STATUS_NO_MEMORY;
999 goto fail;
1002 if (!tevent_req_poll(req, ev)) {
1003 status = map_nt_error_from_unix(errno);
1004 goto fail;
1007 status = cli_rmdir_recv(req);
1009 fail:
1010 TALLOC_FREE(frame);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 cli_set_error(cli, status);
1014 return status;
1017 /****************************************************************************
1018 Set or clear the delete on close flag.
1019 ****************************************************************************/
1021 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
1023 unsigned int data_len = 1;
1024 unsigned int param_len = 6;
1025 uint16_t setup = TRANSACT2_SETFILEINFO;
1026 char param[6];
1027 unsigned char data;
1028 char *rparam=NULL, *rdata=NULL;
1030 memset(param, 0, param_len);
1031 SSVAL(param,0,fnum);
1032 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1034 data = flag ? 1 : 0;
1036 if (!cli_send_trans(cli, SMBtrans2,
1037 NULL, /* name */
1038 -1, 0, /* fid, flags */
1039 &setup, 1, 0, /* setup, length, max */
1040 param, param_len, 2, /* param, length, max */
1041 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1042 )) {
1043 return false;
1046 if (!cli_receive_trans(cli, SMBtrans2,
1047 &rparam, &param_len,
1048 &rdata, &data_len)) {
1049 return false;
1052 SAFE_FREE(rdata);
1053 SAFE_FREE(rparam);
1055 return true;
1058 /****************************************************************************
1059 Open a file - exposing the full horror of the NT API :-).
1060 Used in smbtorture.
1061 ****************************************************************************/
1063 int cli_nt_create_full(struct cli_state *cli, const char *fname,
1064 uint32_t CreatFlags, uint32_t DesiredAccess,
1065 uint32_t FileAttributes, uint32_t ShareAccess,
1066 uint32_t CreateDisposition, uint32_t CreateOptions,
1067 uint8_t SecurityFlags)
1069 char *p;
1070 int len;
1072 memset(cli->outbuf,'\0',smb_size);
1073 memset(cli->inbuf,'\0',smb_size);
1075 cli_set_message(cli->outbuf,24,0, true);
1077 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
1078 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1079 cli_setup_packet(cli);
1081 SSVAL(cli->outbuf,smb_vwv0,0xFF);
1082 if (cli->use_oplocks)
1083 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1085 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
1086 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
1087 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
1088 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
1089 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
1090 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
1091 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
1092 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
1093 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
1095 p = smb_buf(cli->outbuf);
1096 /* this alignment and termination is critical for netapp filers. Don't change */
1097 p += clistr_align_out(cli, p, 0);
1098 len = clistr_push(cli, p, fname,
1099 cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
1100 p += len;
1101 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
1102 /* sigh. this copes with broken netapp filer behaviour */
1103 p += clistr_push(cli, p, "",
1104 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1106 cli_setup_bcc(cli, p);
1108 cli_send_smb(cli);
1109 if (!cli_receive_smb(cli)) {
1110 return -1;
1113 if (cli_is_error(cli)) {
1114 return -1;
1117 return SVAL(cli->inbuf,smb_vwv2 + 1);
1120 struct cli_ntcreate_state {
1121 uint16_t vwv[24];
1122 uint16_t fnum;
1125 static void cli_ntcreate_done(struct tevent_req *subreq);
1127 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1128 struct event_context *ev,
1129 struct cli_state *cli,
1130 const char *fname,
1131 uint32_t CreatFlags,
1132 uint32_t DesiredAccess,
1133 uint32_t FileAttributes,
1134 uint32_t ShareAccess,
1135 uint32_t CreateDisposition,
1136 uint32_t CreateOptions,
1137 uint8_t SecurityFlags)
1139 struct tevent_req *req, *subreq;
1140 struct cli_ntcreate_state *state;
1141 uint16_t *vwv;
1142 uint8_t *bytes;
1143 size_t converted_len;
1145 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1146 if (req == NULL) {
1147 return NULL;
1149 vwv = state->vwv;
1151 SCVAL(vwv+0, 0, 0xFF);
1152 SCVAL(vwv+0, 1, 0);
1153 SSVAL(vwv+1, 0, 0);
1154 SCVAL(vwv+2, 0, 0);
1156 if (cli->use_oplocks) {
1157 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1159 SIVAL(vwv+3, 1, CreatFlags);
1160 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1161 SIVAL(vwv+7, 1, DesiredAccess);
1162 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1163 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1164 SIVAL(vwv+13, 1, FileAttributes);
1165 SIVAL(vwv+15, 1, ShareAccess);
1166 SIVAL(vwv+17, 1, CreateDisposition);
1167 SIVAL(vwv+19, 1, CreateOptions);
1168 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1169 SCVAL(vwv+23, 1, SecurityFlags);
1171 bytes = talloc_array(state, uint8_t, 0);
1172 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1173 fname, strlen(fname)+1,
1174 &converted_len);
1176 /* sigh. this copes with broken netapp filer behaviour */
1177 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1179 if (tevent_req_nomem(bytes, req)) {
1180 return tevent_req_post(req, ev);
1183 SIVAL(vwv+2, 1, converted_len);
1185 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1186 talloc_get_size(bytes), bytes);
1187 if (tevent_req_nomem(subreq, req)) {
1188 return tevent_req_post(req, ev);
1190 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1191 return req;
1194 static void cli_ntcreate_done(struct tevent_req *subreq)
1196 struct tevent_req *req = tevent_req_callback_data(
1197 subreq, struct tevent_req);
1198 struct cli_ntcreate_state *state = tevent_req_data(
1199 req, struct cli_ntcreate_state);
1200 uint8_t wct;
1201 uint16_t *vwv;
1202 uint32_t num_bytes;
1203 uint8_t *bytes;
1204 NTSTATUS status;
1206 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1207 if (!NT_STATUS_IS_OK(status)) {
1208 TALLOC_FREE(subreq);
1209 tevent_req_nterror(req, status);
1210 return;
1212 state->fnum = SVAL(vwv+2, 1);
1213 tevent_req_done(req);
1216 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1218 struct cli_ntcreate_state *state = tevent_req_data(
1219 req, struct cli_ntcreate_state);
1220 NTSTATUS status;
1222 if (tevent_req_is_nterror(req, &status)) {
1223 return status;
1225 *pfnum = state->fnum;
1226 return NT_STATUS_OK;
1229 NTSTATUS cli_ntcreate(struct cli_state *cli,
1230 const char *fname,
1231 uint32_t CreatFlags,
1232 uint32_t DesiredAccess,
1233 uint32_t FileAttributes,
1234 uint32_t ShareAccess,
1235 uint32_t CreateDisposition,
1236 uint32_t CreateOptions,
1237 uint8_t SecurityFlags,
1238 uint16_t *pfid)
1240 TALLOC_CTX *frame = talloc_stackframe();
1241 struct event_context *ev;
1242 struct tevent_req *req;
1243 NTSTATUS status = NT_STATUS_OK;
1245 if (cli_has_async_calls(cli)) {
1247 * Can't use sync call while an async call is in flight
1249 status = NT_STATUS_INVALID_PARAMETER;
1250 goto fail;
1253 ev = event_context_init(frame);
1254 if (ev == NULL) {
1255 status = NT_STATUS_NO_MEMORY;
1256 goto fail;
1259 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1260 DesiredAccess, FileAttributes, ShareAccess,
1261 CreateDisposition, CreateOptions,
1262 SecurityFlags);
1263 if (req == NULL) {
1264 status = NT_STATUS_NO_MEMORY;
1265 goto fail;
1268 if (!tevent_req_poll(req, ev)) {
1269 status = map_nt_error_from_unix(errno);
1270 goto fail;
1273 status = cli_ntcreate_recv(req, pfid);
1274 fail:
1275 TALLOC_FREE(frame);
1276 if (!NT_STATUS_IS_OK(status)) {
1277 cli_set_error(cli, status);
1279 return status;
1282 /****************************************************************************
1283 Open a file.
1284 ****************************************************************************/
1286 int cli_nt_create(struct cli_state *cli, const char *fname, uint32_t DesiredAccess)
1288 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
1289 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
1292 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1293 const char *str, size_t str_len,
1294 size_t *pconverted_size)
1296 size_t buflen;
1297 char *converted;
1298 size_t converted_size;
1300 if (buf == NULL) {
1301 return NULL;
1304 buflen = talloc_get_size(buf);
1306 * We're pushing into an SMB buffer, align odd
1308 if (ucs2 && (buflen % 2 == 0)) {
1309 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1310 if (buf == NULL) {
1311 return NULL;
1313 buf[buflen] = '\0';
1314 buflen += 1;
1317 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1318 ucs2 ? CH_UTF16LE : CH_DOS,
1319 str, str_len, &converted,
1320 &converted_size, true)) {
1321 return NULL;
1324 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1325 buflen + converted_size);
1326 if (buf == NULL) {
1327 TALLOC_FREE(converted);
1328 return NULL;
1331 memcpy(buf + buflen, converted, converted_size);
1333 TALLOC_FREE(converted);
1335 if (pconverted_size) {
1336 *pconverted_size = converted_size;
1339 return buf;
1342 /****************************************************************************
1343 Open a file
1344 WARNING: if you open with O_WRONLY then getattrE won't work!
1345 ****************************************************************************/
1347 struct cli_open_state {
1348 uint16_t vwv[15];
1349 int fnum;
1350 struct iovec bytes;
1353 static void cli_open_done(struct tevent_req *subreq);
1355 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1356 struct event_context *ev,
1357 struct cli_state *cli, const char *fname,
1358 int flags, int share_mode,
1359 struct tevent_req **psmbreq)
1361 struct tevent_req *req, *subreq;
1362 struct cli_open_state *state;
1363 unsigned openfn;
1364 unsigned accessmode;
1365 uint8_t additional_flags;
1366 uint8_t *bytes;
1368 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1369 if (req == NULL) {
1370 return NULL;
1373 openfn = 0;
1374 if (flags & O_CREAT) {
1375 openfn |= (1<<4);
1377 if (!(flags & O_EXCL)) {
1378 if (flags & O_TRUNC)
1379 openfn |= (1<<1);
1380 else
1381 openfn |= (1<<0);
1384 accessmode = (share_mode<<4);
1386 if ((flags & O_ACCMODE) == O_RDWR) {
1387 accessmode |= 2;
1388 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1389 accessmode |= 1;
1392 #if defined(O_SYNC)
1393 if ((flags & O_SYNC) == O_SYNC) {
1394 accessmode |= (1<<14);
1396 #endif /* O_SYNC */
1398 if (share_mode == DENY_FCB) {
1399 accessmode = 0xFF;
1402 SCVAL(state->vwv + 0, 0, 0xFF);
1403 SCVAL(state->vwv + 0, 1, 0);
1404 SSVAL(state->vwv + 1, 0, 0);
1405 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1406 SSVAL(state->vwv + 3, 0, accessmode);
1407 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1408 SSVAL(state->vwv + 5, 0, 0);
1409 SIVAL(state->vwv + 6, 0, 0);
1410 SSVAL(state->vwv + 8, 0, openfn);
1411 SIVAL(state->vwv + 9, 0, 0);
1412 SIVAL(state->vwv + 11, 0, 0);
1413 SIVAL(state->vwv + 13, 0, 0);
1415 additional_flags = 0;
1417 if (cli->use_oplocks) {
1418 /* if using oplocks then ask for a batch oplock via
1419 core and extended methods */
1420 additional_flags =
1421 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1422 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1425 bytes = talloc_array(state, uint8_t, 0);
1426 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1427 strlen(fname)+1, NULL);
1429 if (tevent_req_nomem(bytes, req)) {
1430 return tevent_req_post(req, ev);
1433 state->bytes.iov_base = bytes;
1434 state->bytes.iov_len = talloc_get_size(bytes);
1436 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1437 15, state->vwv, 1, &state->bytes);
1438 if (subreq == NULL) {
1439 TALLOC_FREE(req);
1440 return NULL;
1442 tevent_req_set_callback(subreq, cli_open_done, req);
1443 *psmbreq = subreq;
1444 return req;
1447 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1448 struct cli_state *cli, const char *fname,
1449 int flags, int share_mode)
1451 struct tevent_req *req, *subreq;
1453 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1454 &subreq);
1455 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1456 TALLOC_FREE(req);
1457 return NULL;
1459 return req;
1462 static void cli_open_done(struct tevent_req *subreq)
1464 struct tevent_req *req = tevent_req_callback_data(
1465 subreq, struct tevent_req);
1466 struct cli_open_state *state = tevent_req_data(
1467 req, struct cli_open_state);
1468 uint8_t wct;
1469 uint16_t *vwv;
1470 NTSTATUS status;
1472 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1473 if (!NT_STATUS_IS_OK(status)) {
1474 TALLOC_FREE(subreq);
1475 tevent_req_nterror(req, status);
1476 return;
1478 state->fnum = SVAL(vwv+2, 0);
1479 tevent_req_done(req);
1482 NTSTATUS cli_open_recv(struct tevent_req *req, int *fnum)
1484 struct cli_open_state *state = tevent_req_data(
1485 req, struct cli_open_state);
1486 NTSTATUS status;
1488 if (tevent_req_is_nterror(req, &status)) {
1489 return status;
1491 *fnum = state->fnum;
1492 return NT_STATUS_OK;
1495 int cli_open(struct cli_state *cli, const char *fname, int flags,
1496 int share_mode)
1498 TALLOC_CTX *frame = talloc_stackframe();
1499 struct event_context *ev;
1500 struct tevent_req *req;
1501 NTSTATUS status = NT_STATUS_OK;
1502 int result = -1;
1504 if (cli_has_async_calls(cli)) {
1506 * Can't use sync call while an async call is in flight
1508 status = NT_STATUS_INVALID_PARAMETER;
1509 goto fail;
1512 ev = event_context_init(frame);
1513 if (ev == NULL) {
1514 status = NT_STATUS_NO_MEMORY;
1515 goto fail;
1518 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1519 if (req == NULL) {
1520 status = NT_STATUS_NO_MEMORY;
1521 goto fail;
1524 if (!tevent_req_poll(req, ev)) {
1525 status = map_nt_error_from_unix(errno);
1526 goto fail;
1529 cli_open_recv(req, &result);
1530 fail:
1531 TALLOC_FREE(frame);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 cli_set_error(cli, status);
1535 return result;
1538 /****************************************************************************
1539 Close a file.
1540 ****************************************************************************/
1542 struct cli_close_state {
1543 uint16_t vwv[3];
1546 static void cli_close_done(struct tevent_req *subreq);
1548 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1549 struct event_context *ev,
1550 struct cli_state *cli, int fnum,
1551 struct tevent_req **psubreq)
1553 struct tevent_req *req, *subreq;
1554 struct cli_close_state *state;
1556 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1557 if (req == NULL) {
1558 return NULL;
1560 SSVAL(state->vwv+0, 0, fnum);
1561 SIVALS(state->vwv+1, 0, -1);
1563 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1564 0, NULL);
1565 if (subreq == NULL) {
1566 TALLOC_FREE(req);
1567 return NULL;
1569 tevent_req_set_callback(subreq, cli_close_done, req);
1570 *psubreq = subreq;
1571 return req;
1574 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1575 struct event_context *ev,
1576 struct cli_state *cli, int fnum)
1578 struct tevent_req *req, *subreq;
1580 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1581 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1582 TALLOC_FREE(req);
1583 return NULL;
1585 return req;
1588 static void cli_close_done(struct tevent_req *subreq)
1590 struct tevent_req *req = tevent_req_callback_data(
1591 subreq, struct tevent_req);
1592 NTSTATUS status;
1594 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1595 TALLOC_FREE(subreq);
1596 if (!NT_STATUS_IS_OK(status)) {
1597 tevent_req_nterror(req, status);
1598 return;
1600 tevent_req_done(req);
1603 NTSTATUS cli_close_recv(struct tevent_req *req)
1605 return tevent_req_simple_recv_ntstatus(req);
1608 bool cli_close(struct cli_state *cli, int fnum)
1610 TALLOC_CTX *frame = talloc_stackframe();
1611 struct event_context *ev;
1612 struct tevent_req *req;
1613 NTSTATUS status = NT_STATUS_OK;
1614 bool result = false;
1616 if (cli_has_async_calls(cli)) {
1618 * Can't use sync call while an async call is in flight
1620 status = NT_STATUS_INVALID_PARAMETER;
1621 goto fail;
1624 ev = event_context_init(frame);
1625 if (ev == NULL) {
1626 status = NT_STATUS_NO_MEMORY;
1627 goto fail;
1630 req = cli_close_send(frame, ev, cli, fnum);
1631 if (req == NULL) {
1632 status = NT_STATUS_NO_MEMORY;
1633 goto fail;
1636 if (!tevent_req_poll(req, ev)) {
1637 status = map_nt_error_from_unix(errno);
1638 goto fail;
1641 result = NT_STATUS_IS_OK(cli_close_recv(req));
1642 fail:
1643 TALLOC_FREE(frame);
1644 if (!NT_STATUS_IS_OK(status)) {
1645 cli_set_error(cli, status);
1647 return result;
1650 /****************************************************************************
1651 Truncate a file to a specified size
1652 ****************************************************************************/
1654 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1656 unsigned int param_len = 6;
1657 unsigned int data_len = 8;
1658 uint16_t setup = TRANSACT2_SETFILEINFO;
1659 char param[6];
1660 unsigned char data[8];
1661 char *rparam=NULL, *rdata=NULL;
1662 int saved_timeout = cli->timeout;
1664 SSVAL(param,0,fnum);
1665 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1666 SSVAL(param,4,0);
1668 SBVAL(data, 0, size);
1670 if (!cli_send_trans(cli, SMBtrans2,
1671 NULL, /* name */
1672 -1, 0, /* fid, flags */
1673 &setup, 1, 0, /* setup, length, max */
1674 param, param_len, 2, /* param, length, max */
1675 (char *)&data, data_len,/* data, length, ... */
1676 cli->max_xmit)) { /* ... max */
1677 cli->timeout = saved_timeout;
1678 return False;
1681 if (!cli_receive_trans(cli, SMBtrans2,
1682 &rparam, &param_len,
1683 &rdata, &data_len)) {
1684 cli->timeout = saved_timeout;
1685 SAFE_FREE(rdata);
1686 SAFE_FREE(rparam);
1687 return False;
1690 cli->timeout = saved_timeout;
1692 SAFE_FREE(rdata);
1693 SAFE_FREE(rparam);
1695 return True;
1699 /****************************************************************************
1700 send a lock with a specified locktype
1701 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1702 ****************************************************************************/
1704 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1705 uint32_t offset, uint32_t len,
1706 int timeout, unsigned char locktype)
1708 char *p;
1709 int saved_timeout = cli->timeout;
1711 memset(cli->outbuf,'\0',smb_size);
1712 memset(cli->inbuf,'\0', smb_size);
1714 cli_set_message(cli->outbuf,8,0,True);
1716 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1717 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1718 cli_setup_packet(cli);
1720 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1721 SSVAL(cli->outbuf,smb_vwv2,fnum);
1722 SCVAL(cli->outbuf,smb_vwv3,locktype);
1723 SIVALS(cli->outbuf, smb_vwv4, timeout);
1724 SSVAL(cli->outbuf,smb_vwv6,0);
1725 SSVAL(cli->outbuf,smb_vwv7,1);
1727 p = smb_buf(cli->outbuf);
1728 SSVAL(p, 0, cli->pid);
1729 SIVAL(p, 2, offset);
1730 SIVAL(p, 6, len);
1732 p += 10;
1734 cli_setup_bcc(cli, p);
1736 cli_send_smb(cli);
1738 if (timeout != 0) {
1739 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1742 if (!cli_receive_smb(cli)) {
1743 cli->timeout = saved_timeout;
1744 return NT_STATUS_UNSUCCESSFUL;
1747 cli->timeout = saved_timeout;
1749 return cli_nt_error(cli);
1752 /****************************************************************************
1753 Lock a file.
1754 note that timeout is in units of 2 milliseconds
1755 ****************************************************************************/
1757 bool cli_lock(struct cli_state *cli, int fnum,
1758 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1760 char *p;
1761 int saved_timeout = cli->timeout;
1763 memset(cli->outbuf,'\0',smb_size);
1764 memset(cli->inbuf,'\0', smb_size);
1766 cli_set_message(cli->outbuf,8,0,True);
1768 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1769 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1770 cli_setup_packet(cli);
1772 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1773 SSVAL(cli->outbuf,smb_vwv2,fnum);
1774 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1775 SIVALS(cli->outbuf, smb_vwv4, timeout);
1776 SSVAL(cli->outbuf,smb_vwv6,0);
1777 SSVAL(cli->outbuf,smb_vwv7,1);
1779 p = smb_buf(cli->outbuf);
1780 SSVAL(p, 0, cli->pid);
1781 SIVAL(p, 2, offset);
1782 SIVAL(p, 6, len);
1784 p += 10;
1786 cli_setup_bcc(cli, p);
1788 cli_send_smb(cli);
1790 if (timeout != 0) {
1791 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1794 if (!cli_receive_smb(cli)) {
1795 cli->timeout = saved_timeout;
1796 return False;
1799 cli->timeout = saved_timeout;
1801 if (cli_is_error(cli)) {
1802 return False;
1805 return True;
1808 /****************************************************************************
1809 Unlock a file.
1810 ****************************************************************************/
1812 bool cli_unlock(struct cli_state *cli, int fnum, uint32_t offset, uint32_t len)
1814 char *p;
1816 memset(cli->outbuf,'\0',smb_size);
1817 memset(cli->inbuf,'\0',smb_size);
1819 cli_set_message(cli->outbuf,8,0,True);
1821 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1822 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1823 cli_setup_packet(cli);
1825 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1826 SSVAL(cli->outbuf,smb_vwv2,fnum);
1827 SCVAL(cli->outbuf,smb_vwv3,0);
1828 SIVALS(cli->outbuf, smb_vwv4, 0);
1829 SSVAL(cli->outbuf,smb_vwv6,1);
1830 SSVAL(cli->outbuf,smb_vwv7,0);
1832 p = smb_buf(cli->outbuf);
1833 SSVAL(p, 0, cli->pid);
1834 SIVAL(p, 2, offset);
1835 SIVAL(p, 6, len);
1836 p += 10;
1837 cli_setup_bcc(cli, p);
1838 cli_send_smb(cli);
1839 if (!cli_receive_smb(cli)) {
1840 return False;
1843 if (cli_is_error(cli)) {
1844 return False;
1847 return True;
1850 /****************************************************************************
1851 Lock a file with 64 bit offsets.
1852 ****************************************************************************/
1854 bool cli_lock64(struct cli_state *cli, int fnum,
1855 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1857 char *p;
1858 int saved_timeout = cli->timeout;
1859 int ltype;
1861 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1862 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1865 ltype = (lock_type == READ_LOCK? 1 : 0);
1866 ltype |= LOCKING_ANDX_LARGE_FILES;
1868 memset(cli->outbuf,'\0',smb_size);
1869 memset(cli->inbuf,'\0', smb_size);
1871 cli_set_message(cli->outbuf,8,0,True);
1873 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1874 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1875 cli_setup_packet(cli);
1877 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1878 SSVAL(cli->outbuf,smb_vwv2,fnum);
1879 SCVAL(cli->outbuf,smb_vwv3,ltype);
1880 SIVALS(cli->outbuf, smb_vwv4, timeout);
1881 SSVAL(cli->outbuf,smb_vwv6,0);
1882 SSVAL(cli->outbuf,smb_vwv7,1);
1884 p = smb_buf(cli->outbuf);
1885 SIVAL(p, 0, cli->pid);
1886 SOFF_T_R(p, 4, offset);
1887 SOFF_T_R(p, 12, len);
1888 p += 20;
1890 cli_setup_bcc(cli, p);
1891 cli_send_smb(cli);
1893 if (timeout != 0) {
1894 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1897 if (!cli_receive_smb(cli)) {
1898 cli->timeout = saved_timeout;
1899 return False;
1902 cli->timeout = saved_timeout;
1904 if (cli_is_error(cli)) {
1905 return False;
1908 return True;
1911 /****************************************************************************
1912 Unlock a file with 64 bit offsets.
1913 ****************************************************************************/
1915 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1917 char *p;
1919 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1920 return cli_unlock(cli, fnum, offset, len);
1923 memset(cli->outbuf,'\0',smb_size);
1924 memset(cli->inbuf,'\0',smb_size);
1926 cli_set_message(cli->outbuf,8,0,True);
1928 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1929 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1930 cli_setup_packet(cli);
1932 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1933 SSVAL(cli->outbuf,smb_vwv2,fnum);
1934 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1935 SIVALS(cli->outbuf, smb_vwv4, 0);
1936 SSVAL(cli->outbuf,smb_vwv6,1);
1937 SSVAL(cli->outbuf,smb_vwv7,0);
1939 p = smb_buf(cli->outbuf);
1940 SIVAL(p, 0, cli->pid);
1941 SOFF_T_R(p, 4, offset);
1942 SOFF_T_R(p, 12, len);
1943 p += 20;
1944 cli_setup_bcc(cli, p);
1945 cli_send_smb(cli);
1946 if (!cli_receive_smb(cli)) {
1947 return False;
1950 if (cli_is_error(cli)) {
1951 return False;
1954 return True;
1957 /****************************************************************************
1958 Get/unlock a POSIX lock on a file - internal function.
1959 ****************************************************************************/
1961 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1962 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1964 unsigned int param_len = 4;
1965 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1966 uint16_t setup = TRANSACT2_SETFILEINFO;
1967 char param[4];
1968 unsigned char data[POSIX_LOCK_DATA_SIZE];
1969 char *rparam=NULL, *rdata=NULL;
1970 int saved_timeout = cli->timeout;
1972 SSVAL(param,0,fnum);
1973 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1975 switch (lock_type) {
1976 case READ_LOCK:
1977 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1978 break;
1979 case WRITE_LOCK:
1980 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1981 break;
1982 case UNLOCK_LOCK:
1983 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1984 break;
1985 default:
1986 return False;
1989 if (wait_lock) {
1990 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1991 cli->timeout = 0x7FFFFFFF;
1992 } else {
1993 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1996 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1997 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1998 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2000 if (!cli_send_trans(cli, SMBtrans2,
2001 NULL, /* name */
2002 -1, 0, /* fid, flags */
2003 &setup, 1, 0, /* setup, length, max */
2004 param, param_len, 2, /* param, length, max */
2005 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2006 )) {
2007 cli->timeout = saved_timeout;
2008 return False;
2011 if (!cli_receive_trans(cli, SMBtrans2,
2012 &rparam, &param_len,
2013 &rdata, &data_len)) {
2014 cli->timeout = saved_timeout;
2015 SAFE_FREE(rdata);
2016 SAFE_FREE(rparam);
2017 return False;
2020 cli->timeout = saved_timeout;
2022 SAFE_FREE(rdata);
2023 SAFE_FREE(rparam);
2025 return True;
2028 /****************************************************************************
2029 POSIX Lock a file.
2030 ****************************************************************************/
2032 bool cli_posix_lock(struct cli_state *cli, int fnum,
2033 uint64_t offset, uint64_t len,
2034 bool wait_lock, enum brl_type lock_type)
2036 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2037 return False;
2039 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2042 /****************************************************************************
2043 POSIX Unlock a file.
2044 ****************************************************************************/
2046 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
2048 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2051 /****************************************************************************
2052 POSIX Get any lock covering a file.
2053 ****************************************************************************/
2055 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
2057 return True;
2060 /****************************************************************************
2061 Do a SMBgetattrE call.
2062 ****************************************************************************/
2064 bool cli_getattrE(struct cli_state *cli, int fd,
2065 uint16_t *attr, SMB_OFF_T *size,
2066 time_t *change_time,
2067 time_t *access_time,
2068 time_t *write_time)
2070 memset(cli->outbuf,'\0',smb_size);
2071 memset(cli->inbuf,'\0',smb_size);
2073 cli_set_message(cli->outbuf,1,0,True);
2075 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
2076 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2077 cli_setup_packet(cli);
2079 SSVAL(cli->outbuf,smb_vwv0,fd);
2081 cli_send_smb(cli);
2082 if (!cli_receive_smb(cli)) {
2083 return False;
2086 if (cli_is_error(cli)) {
2087 return False;
2090 if (size) {
2091 *size = IVAL(cli->inbuf, smb_vwv6);
2094 if (attr) {
2095 *attr = SVAL(cli->inbuf,smb_vwv10);
2098 if (change_time) {
2099 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
2102 if (access_time) {
2103 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
2106 if (write_time) {
2107 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
2110 return True;
2113 /****************************************************************************
2114 Do a SMBgetatr call
2115 ****************************************************************************/
2117 bool cli_getatr(struct cli_state *cli, const char *fname,
2118 uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
2120 char *p;
2122 memset(cli->outbuf,'\0',smb_size);
2123 memset(cli->inbuf,'\0',smb_size);
2125 cli_set_message(cli->outbuf,0,0,True);
2127 SCVAL(cli->outbuf,smb_com,SMBgetatr);
2128 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2129 cli_setup_packet(cli);
2131 p = smb_buf(cli->outbuf);
2132 *p++ = 4;
2133 p += clistr_push(cli, p, fname,
2134 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2136 cli_setup_bcc(cli, p);
2138 cli_send_smb(cli);
2139 if (!cli_receive_smb(cli)) {
2140 return False;
2143 if (cli_is_error(cli)) {
2144 return False;
2147 if (size) {
2148 *size = IVAL(cli->inbuf, smb_vwv3);
2151 if (write_time) {
2152 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
2155 if (attr) {
2156 *attr = SVAL(cli->inbuf,smb_vwv0);
2159 return True;
2162 /****************************************************************************
2163 Do a SMBsetattrE call.
2164 ****************************************************************************/
2166 bool cli_setattrE(struct cli_state *cli, int fd,
2167 time_t change_time,
2168 time_t access_time,
2169 time_t write_time)
2172 char *p;
2174 memset(cli->outbuf,'\0',smb_size);
2175 memset(cli->inbuf,'\0',smb_size);
2177 cli_set_message(cli->outbuf,7,0,True);
2179 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
2180 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2181 cli_setup_packet(cli);
2183 SSVAL(cli->outbuf,smb_vwv0, fd);
2184 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
2185 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
2186 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
2188 p = smb_buf(cli->outbuf);
2189 *p++ = 4;
2191 cli_setup_bcc(cli, p);
2193 cli_send_smb(cli);
2194 if (!cli_receive_smb(cli)) {
2195 return False;
2198 if (cli_is_error(cli)) {
2199 return False;
2202 return True;
2205 /****************************************************************************
2206 Do a SMBsetatr call.
2207 ****************************************************************************/
2209 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
2211 char *p;
2213 memset(cli->outbuf,'\0',smb_size);
2214 memset(cli->inbuf,'\0',smb_size);
2216 cli_set_message(cli->outbuf,8,0,True);
2218 SCVAL(cli->outbuf,smb_com,SMBsetatr);
2219 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2220 cli_setup_packet(cli);
2222 SSVAL(cli->outbuf,smb_vwv0, attr);
2223 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
2225 p = smb_buf(cli->outbuf);
2226 *p++ = 4;
2227 p += clistr_push(cli, p, fname,
2228 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2229 *p++ = 4;
2231 cli_setup_bcc(cli, p);
2233 cli_send_smb(cli);
2234 if (!cli_receive_smb(cli)) {
2235 return False;
2238 if (cli_is_error(cli)) {
2239 return False;
2242 return True;
2245 /****************************************************************************
2246 Check for existance of a dir.
2247 ****************************************************************************/
2249 static void cli_chkpath_done(struct tevent_req *subreq);
2251 struct cli_chkpath_state {
2252 int dummy;
2255 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2256 struct event_context *ev,
2257 struct cli_state *cli,
2258 const char *fname)
2260 struct tevent_req *req = NULL, *subreq = NULL;
2261 struct cli_chkpath_state *state = NULL;
2262 uint8_t additional_flags = 0;
2263 uint8_t *bytes = NULL;
2265 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2266 if (req == NULL) {
2267 return NULL;
2270 bytes = talloc_array(state, uint8_t, 1);
2271 if (tevent_req_nomem(bytes, req)) {
2272 return tevent_req_post(req, ev);
2274 bytes[0] = 4;
2275 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2276 strlen(fname)+1, NULL);
2278 if (tevent_req_nomem(bytes, req)) {
2279 return tevent_req_post(req, ev);
2282 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2283 0, NULL, talloc_get_size(bytes), bytes);
2284 if (tevent_req_nomem(subreq, req)) {
2285 return tevent_req_post(req, ev);
2287 tevent_req_set_callback(subreq, cli_chkpath_done, req);
2288 return req;
2291 static void cli_chkpath_done(struct tevent_req *subreq)
2293 struct tevent_req *req = tevent_req_callback_data(
2294 subreq, struct tevent_req);
2295 NTSTATUS status;
2297 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2298 TALLOC_FREE(subreq);
2299 if (!NT_STATUS_IS_OK(status)) {
2300 tevent_req_nterror(req, status);
2301 return;
2303 tevent_req_done(req);
2306 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2308 return tevent_req_simple_recv_ntstatus(req);
2311 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2313 TALLOC_CTX *frame = talloc_stackframe();
2314 struct event_context *ev = NULL;
2315 struct tevent_req *req = NULL;
2316 char *path2 = NULL;
2317 NTSTATUS status = NT_STATUS_OK;
2319 if (cli_has_async_calls(cli)) {
2321 * Can't use sync call while an async call is in flight
2323 status = NT_STATUS_INVALID_PARAMETER;
2324 goto fail;
2327 path2 = talloc_strdup(frame, path);
2328 if (!path2) {
2329 status = NT_STATUS_NO_MEMORY;
2330 goto fail;
2332 trim_char(path2,'\0','\\');
2333 if (!*path2) {
2334 path2 = talloc_strdup(frame, "\\");
2335 if (!path2) {
2336 status = NT_STATUS_NO_MEMORY;
2337 goto fail;
2341 ev = event_context_init(frame);
2342 if (ev == NULL) {
2343 status = NT_STATUS_NO_MEMORY;
2344 goto fail;
2347 req = cli_chkpath_send(frame, ev, cli, path2);
2348 if (req == NULL) {
2349 status = NT_STATUS_NO_MEMORY;
2350 goto fail;
2353 if (!tevent_req_poll(req, ev)) {
2354 status = map_nt_error_from_unix(errno);
2355 goto fail;
2358 status = cli_chkpath_recv(req);
2360 fail:
2361 TALLOC_FREE(frame);
2362 if (!NT_STATUS_IS_OK(status)) {
2363 cli_set_error(cli, status);
2365 return status;
2368 /****************************************************************************
2369 Query disk space.
2370 ****************************************************************************/
2372 static void cli_dskattr_done(struct tevent_req *subreq);
2374 struct cli_dskattr_state {
2375 int bsize;
2376 int total;
2377 int avail;
2380 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2381 struct event_context *ev,
2382 struct cli_state *cli)
2384 struct tevent_req *req = NULL, *subreq = NULL;
2385 struct cli_dskattr_state *state = NULL;
2386 uint8_t additional_flags = 0;
2388 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2389 if (req == NULL) {
2390 return NULL;
2393 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2394 0, NULL, 0, NULL);
2395 if (tevent_req_nomem(subreq, req)) {
2396 return tevent_req_post(req, ev);
2398 tevent_req_set_callback(subreq, cli_dskattr_done, req);
2399 return req;
2402 static void cli_dskattr_done(struct tevent_req *subreq)
2404 struct tevent_req *req = tevent_req_callback_data(
2405 subreq, struct tevent_req);
2406 struct cli_dskattr_state *state = tevent_req_data(
2407 req, struct cli_dskattr_state);
2408 uint8_t wct;
2409 uint16_t *vwv = NULL;
2410 NTSTATUS status;
2412 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 tevent_req_nterror(req, status);
2415 return;
2417 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2418 state->total = SVAL(vwv+0, 0);
2419 state->avail = SVAL(vwv+3, 0);
2420 TALLOC_FREE(subreq);
2421 tevent_req_done(req);
2424 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2426 struct cli_dskattr_state *state = tevent_req_data(
2427 req, struct cli_dskattr_state);
2428 NTSTATUS status;
2430 if (tevent_req_is_nterror(req, &status)) {
2431 return status;
2433 *bsize = state->bsize;
2434 *total = state->total;
2435 *avail = state->avail;
2436 return NT_STATUS_OK;
2439 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2441 TALLOC_CTX *frame = talloc_stackframe();
2442 struct event_context *ev = NULL;
2443 struct tevent_req *req = NULL;
2444 NTSTATUS status = NT_STATUS_OK;
2446 if (cli_has_async_calls(cli)) {
2448 * Can't use sync call while an async call is in flight
2450 status = NT_STATUS_INVALID_PARAMETER;
2451 goto fail;
2454 ev = event_context_init(frame);
2455 if (ev == NULL) {
2456 status = NT_STATUS_NO_MEMORY;
2457 goto fail;
2460 req = cli_dskattr_send(frame, ev, cli);
2461 if (req == NULL) {
2462 status = NT_STATUS_NO_MEMORY;
2463 goto fail;
2466 if (!tevent_req_poll(req, ev)) {
2467 status = map_nt_error_from_unix(errno);
2468 goto fail;
2471 status = cli_dskattr_recv(req, bsize, total, avail);
2473 fail:
2474 TALLOC_FREE(frame);
2475 if (!NT_STATUS_IS_OK(status)) {
2476 cli_set_error(cli, status);
2478 return status;
2481 /****************************************************************************
2482 Create and open a temporary file.
2483 ****************************************************************************/
2485 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2487 int len;
2488 char *p;
2490 memset(cli->outbuf,'\0',smb_size);
2491 memset(cli->inbuf,'\0',smb_size);
2493 cli_set_message(cli->outbuf,3,0,True);
2495 SCVAL(cli->outbuf,smb_com,SMBctemp);
2496 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2497 cli_setup_packet(cli);
2499 SSVAL(cli->outbuf,smb_vwv0,0);
2500 SIVALS(cli->outbuf,smb_vwv1,-1);
2502 p = smb_buf(cli->outbuf);
2503 *p++ = 4;
2504 p += clistr_push(cli, p, path,
2505 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2507 cli_setup_bcc(cli, p);
2509 cli_send_smb(cli);
2510 if (!cli_receive_smb(cli)) {
2511 return -1;
2514 if (cli_is_error(cli)) {
2515 return -1;
2518 /* despite the spec, the result has a -1, followed by
2519 length, followed by name */
2520 p = smb_buf(cli->inbuf);
2521 p += 4;
2522 len = smb_buflen(cli->inbuf) - 4;
2523 if (len <= 0 || len > PATH_MAX) return -1;
2525 if (tmp_path) {
2526 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2527 if (!path2) {
2528 return -1;
2530 clistr_pull(cli->inbuf, path2, p,
2531 len+1, len, STR_ASCII);
2532 *tmp_path = path2;
2535 return SVAL(cli->inbuf,smb_vwv0);
2539 send a raw ioctl - used by the torture code
2541 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32_t code, DATA_BLOB *blob)
2543 memset(cli->outbuf,'\0',smb_size);
2544 memset(cli->inbuf,'\0',smb_size);
2546 cli_set_message(cli->outbuf, 3, 0, True);
2547 SCVAL(cli->outbuf,smb_com,SMBioctl);
2548 cli_setup_packet(cli);
2550 SSVAL(cli->outbuf, smb_vwv0, fnum);
2551 SSVAL(cli->outbuf, smb_vwv1, code>>16);
2552 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2554 cli_send_smb(cli);
2555 if (!cli_receive_smb(cli)) {
2556 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2559 if (cli_is_error(cli)) {
2560 return cli_nt_error(cli);
2563 *blob = data_blob_null;
2565 return NT_STATUS_OK;
2568 /*********************************************************
2569 Set an extended attribute utility fn.
2570 *********************************************************/
2572 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2573 const char *ea_name, const char *ea_val, size_t ea_len)
2575 unsigned int data_len = 0;
2576 char *data = NULL;
2577 char *rparam=NULL, *rdata=NULL;
2578 char *p;
2579 size_t ea_namelen = strlen(ea_name);
2581 if (ea_namelen == 0 && ea_len == 0) {
2582 data_len = 4;
2583 data = (char *)SMB_MALLOC(data_len);
2584 if (!data) {
2585 return False;
2587 p = data;
2588 SIVAL(p,0,data_len);
2589 } else {
2590 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2591 data = (char *)SMB_MALLOC(data_len);
2592 if (!data) {
2593 return False;
2595 p = data;
2596 SIVAL(p,0,data_len);
2597 p += 4;
2598 SCVAL(p, 0, 0); /* EA flags. */
2599 SCVAL(p, 1, ea_namelen);
2600 SSVAL(p, 2, ea_len);
2601 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2602 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2605 if (!cli_send_trans(cli, SMBtrans2,
2606 NULL, /* name */
2607 -1, 0, /* fid, flags */
2608 &setup, 1, 0, /* setup, length, max */
2609 param, param_len, 2, /* param, length, max */
2610 data, data_len, cli->max_xmit /* data, length, max */
2611 )) {
2612 SAFE_FREE(data);
2613 return False;
2616 if (!cli_receive_trans(cli, SMBtrans2,
2617 &rparam, &param_len,
2618 &rdata, &data_len)) {
2619 SAFE_FREE(data);
2620 return false;
2623 SAFE_FREE(data);
2624 SAFE_FREE(rdata);
2625 SAFE_FREE(rparam);
2627 return True;
2630 /*********************************************************
2631 Set an extended attribute on a pathname.
2632 *********************************************************/
2634 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2636 uint16_t setup = TRANSACT2_SETPATHINFO;
2637 unsigned int param_len = 0;
2638 char *param;
2639 size_t srclen = 2*(strlen(path)+1);
2640 char *p;
2641 bool ret;
2643 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2644 if (!param) {
2645 return false;
2647 memset(param, '\0', 6);
2648 SSVAL(param,0,SMB_INFO_SET_EA);
2649 p = &param[6];
2651 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2652 param_len = PTR_DIFF(p, param);
2654 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2655 SAFE_FREE(param);
2656 return ret;
2659 /*********************************************************
2660 Set an extended attribute on an fnum.
2661 *********************************************************/
2663 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2665 char param[6];
2666 uint16_t setup = TRANSACT2_SETFILEINFO;
2668 memset(param, 0, 6);
2669 SSVAL(param,0,fnum);
2670 SSVAL(param,2,SMB_INFO_SET_EA);
2672 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2675 /*********************************************************
2676 Get an extended attribute list utility fn.
2677 *********************************************************/
2679 static bool cli_get_ea_list(struct cli_state *cli,
2680 uint16_t setup, char *param, unsigned int param_len,
2681 TALLOC_CTX *ctx,
2682 size_t *pnum_eas,
2683 struct ea_struct **pea_list)
2685 unsigned int data_len = 0;
2686 unsigned int rparam_len, rdata_len;
2687 char *rparam=NULL, *rdata=NULL;
2688 char *p;
2689 size_t ea_size;
2690 size_t num_eas;
2691 bool ret = False;
2692 struct ea_struct *ea_list;
2694 *pnum_eas = 0;
2695 if (pea_list) {
2696 *pea_list = NULL;
2699 if (!cli_send_trans(cli, SMBtrans2,
2700 NULL, /* Name */
2701 -1, 0, /* fid, flags */
2702 &setup, 1, 0, /* setup, length, max */
2703 param, param_len, 10, /* param, length, max */
2704 NULL, data_len, cli->max_xmit /* data, length, max */
2705 )) {
2706 return False;
2709 if (!cli_receive_trans(cli, SMBtrans2,
2710 &rparam, &rparam_len,
2711 &rdata, &rdata_len)) {
2712 return False;
2715 if (!rdata || rdata_len < 4) {
2716 goto out;
2719 ea_size = (size_t)IVAL(rdata,0);
2720 if (ea_size > rdata_len) {
2721 goto out;
2724 if (ea_size == 0) {
2725 /* No EA's present. */
2726 ret = True;
2727 goto out;
2730 p = rdata + 4;
2731 ea_size -= 4;
2733 /* Validate the EA list and count it. */
2734 for (num_eas = 0; ea_size >= 4; num_eas++) {
2735 unsigned int ea_namelen = CVAL(p,1);
2736 unsigned int ea_valuelen = SVAL(p,2);
2737 if (ea_namelen == 0) {
2738 goto out;
2740 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2741 goto out;
2743 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2744 p += 4 + ea_namelen + 1 + ea_valuelen;
2747 if (num_eas == 0) {
2748 ret = True;
2749 goto out;
2752 *pnum_eas = num_eas;
2753 if (!pea_list) {
2754 /* Caller only wants number of EA's. */
2755 ret = True;
2756 goto out;
2759 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2760 if (!ea_list) {
2761 goto out;
2764 ea_size = (size_t)IVAL(rdata,0);
2765 p = rdata + 4;
2767 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2768 struct ea_struct *ea = &ea_list[num_eas];
2769 fstring unix_ea_name;
2770 unsigned int ea_namelen = CVAL(p,1);
2771 unsigned int ea_valuelen = SVAL(p,2);
2773 ea->flags = CVAL(p,0);
2774 unix_ea_name[0] = '\0';
2775 pull_ascii_fstring(unix_ea_name, p + 4);
2776 ea->name = talloc_strdup(ctx, unix_ea_name);
2777 /* Ensure the value is null terminated (in case it's a string). */
2778 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2779 if (!ea->value.data) {
2780 goto out;
2782 if (ea_valuelen) {
2783 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2785 ea->value.data[ea_valuelen] = 0;
2786 ea->value.length--;
2787 p += 4 + ea_namelen + 1 + ea_valuelen;
2790 *pea_list = ea_list;
2791 ret = True;
2793 out :
2795 SAFE_FREE(rdata);
2796 SAFE_FREE(rparam);
2797 return ret;
2800 /*********************************************************
2801 Get an extended attribute list from a pathname.
2802 *********************************************************/
2804 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2805 TALLOC_CTX *ctx,
2806 size_t *pnum_eas,
2807 struct ea_struct **pea_list)
2809 uint16_t setup = TRANSACT2_QPATHINFO;
2810 unsigned int param_len = 0;
2811 char *param;
2812 char *p;
2813 size_t srclen = 2*(strlen(path)+1);
2814 bool ret;
2816 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2817 if (!param) {
2818 return false;
2820 p = param;
2821 memset(p, 0, 6);
2822 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2823 p += 6;
2824 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2825 param_len = PTR_DIFF(p, param);
2827 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2828 SAFE_FREE(param);
2829 return ret;
2832 /*********************************************************
2833 Get an extended attribute list from an fnum.
2834 *********************************************************/
2836 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2837 TALLOC_CTX *ctx,
2838 size_t *pnum_eas,
2839 struct ea_struct **pea_list)
2841 uint16_t setup = TRANSACT2_QFILEINFO;
2842 char param[6];
2844 memset(param, 0, 6);
2845 SSVAL(param,0,fnum);
2846 SSVAL(param,2,SMB_INFO_SET_EA);
2848 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2851 /****************************************************************************
2852 Convert open "flags" arg to uint32_t on wire.
2853 ****************************************************************************/
2855 static uint32_t open_flags_to_wire(int flags)
2857 int open_mode = flags & O_ACCMODE;
2858 uint32_t ret = 0;
2860 switch (open_mode) {
2861 case O_WRONLY:
2862 ret |= SMB_O_WRONLY;
2863 break;
2864 case O_RDWR:
2865 ret |= SMB_O_RDWR;
2866 break;
2867 default:
2868 case O_RDONLY:
2869 ret |= SMB_O_RDONLY;
2870 break;
2873 if (flags & O_CREAT) {
2874 ret |= SMB_O_CREAT;
2876 if (flags & O_EXCL) {
2877 ret |= SMB_O_EXCL;
2879 if (flags & O_TRUNC) {
2880 ret |= SMB_O_TRUNC;
2882 #if defined(O_SYNC)
2883 if (flags & O_SYNC) {
2884 ret |= SMB_O_SYNC;
2886 #endif /* O_SYNC */
2887 if (flags & O_APPEND) {
2888 ret |= SMB_O_APPEND;
2890 #if defined(O_DIRECT)
2891 if (flags & O_DIRECT) {
2892 ret |= SMB_O_DIRECT;
2894 #endif
2895 #if defined(O_DIRECTORY)
2896 if (flags & O_DIRECTORY) {
2897 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2898 ret |= SMB_O_DIRECTORY;
2900 #endif
2901 return ret;
2904 /****************************************************************************
2905 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2906 ****************************************************************************/
2908 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2910 unsigned int data_len = 0;
2911 unsigned int param_len = 0;
2912 uint16_t setup = TRANSACT2_SETPATHINFO;
2913 char *param;
2914 char data[18];
2915 char *rparam=NULL, *rdata=NULL;
2916 char *p;
2917 int fnum = -1;
2918 uint32_t wire_flags = open_flags_to_wire(flags);
2919 size_t srclen = 2*(strlen(fname)+1);
2921 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2922 if (!param) {
2923 return false;
2925 memset(param, '\0', 6);
2926 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2927 p = &param[6];
2929 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2930 param_len = PTR_DIFF(p, param);
2932 if (is_dir) {
2933 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2934 wire_flags |= SMB_O_DIRECTORY;
2937 p = data;
2938 SIVAL(p,0,0); /* No oplock. */
2939 SIVAL(p,4,wire_flags);
2940 SIVAL(p,8,unix_perms_to_wire(mode));
2941 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2942 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2944 data_len = 18;
2946 if (!cli_send_trans(cli, SMBtrans2,
2947 NULL, /* name */
2948 -1, 0, /* fid, flags */
2949 &setup, 1, 0, /* setup, length, max */
2950 param, param_len, 2, /* param, length, max */
2951 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2952 )) {
2953 SAFE_FREE(param);
2954 return -1;
2957 SAFE_FREE(param);
2959 if (!cli_receive_trans(cli, SMBtrans2,
2960 &rparam, &param_len,
2961 &rdata, &data_len)) {
2962 return -1;
2965 fnum = SVAL(rdata,2);
2967 SAFE_FREE(rdata);
2968 SAFE_FREE(rparam);
2970 return fnum;
2973 /****************************************************************************
2974 open - POSIX semantics.
2975 ****************************************************************************/
2977 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2979 return cli_posix_open_internal(cli, fname, flags, mode, False);
2982 /****************************************************************************
2983 mkdir - POSIX semantics.
2984 ****************************************************************************/
2986 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2988 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2991 /****************************************************************************
2992 unlink or rmdir - POSIX semantics.
2993 ****************************************************************************/
2995 struct unlink_state {
2996 int dummy;
2999 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3001 struct tevent_req *req = tevent_req_callback_data(
3002 subreq, struct tevent_req);
3003 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3004 NTSTATUS status;
3006 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3007 TALLOC_FREE(subreq);
3008 if (!NT_STATUS_IS_OK(status)) {
3009 tevent_req_nterror(req, status);
3010 return;
3012 tevent_req_done(req);
3015 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3016 struct event_context *ev,
3017 struct cli_state *cli,
3018 const char *fname,
3019 bool is_dir)
3021 struct tevent_req *req = NULL, *subreq = NULL;
3022 struct unlink_state *state = NULL;
3023 uint16_t setup;
3024 uint8_t *param = NULL;
3025 uint8_t data[2];
3027 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3028 if (req == NULL) {
3029 return NULL;
3032 /* Setup setup word. */
3033 SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
3035 /* Setup param array. */
3036 param = talloc_array(state, uint8_t, 6);
3037 if (tevent_req_nomem(data, req)) {
3038 return tevent_req_post(req, ev);
3040 memset(param, '\0', 6);
3041 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3043 param = smb_bytes_push_str(param, cli_ucs2(cli), fname,
3044 strlen(fname)+1, NULL);
3046 if (tevent_req_nomem(param, req)) {
3047 return tevent_req_post(req, ev);
3050 /* Setup data word. */
3051 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3052 SMB_POSIX_UNLINK_FILE_TARGET);
3054 subreq = cli_trans_send(state, /* mem ctx. */
3055 ev, /* event ctx. */
3056 cli, /* cli_state. */
3057 SMBtrans2, /* cmd. */
3058 NULL, /* pipe name. */
3059 -1, /* fid. */
3060 0, /* function. */
3061 0, /* flags. */
3062 &setup, /* setup. */
3063 2, /* num setup. */
3064 0, /* max setup. */
3065 param, /* param. */
3066 talloc_get_size(param), /* num param. */
3067 0, /* max param. */
3068 data, /* data. */
3069 2, /* num data. */
3070 0); /* max data. */
3072 if (tevent_req_nomem(subreq, req)) {
3073 return tevent_req_post(req, ev);
3075 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3076 return req;
3079 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3080 struct event_context *ev,
3081 struct cli_state *cli,
3082 const char *fname)
3084 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3087 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3089 NTSTATUS status;
3091 if (tevent_req_is_nterror(req, &status)) {
3092 return status;
3094 return NT_STATUS_OK;
3097 /****************************************************************************
3098 unlink - POSIX semantics.
3099 ****************************************************************************/
3101 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3103 TALLOC_CTX *frame = talloc_stackframe();
3104 struct event_context *ev = NULL;
3105 struct tevent_req *req = NULL;
3106 NTSTATUS status = NT_STATUS_OK;
3108 if (cli_has_async_calls(cli)) {
3110 * Can't use sync call while an async call is in flight
3112 status = NT_STATUS_INVALID_PARAMETER;
3113 goto fail;
3116 ev = event_context_init(frame);
3117 if (ev == NULL) {
3118 status = NT_STATUS_NO_MEMORY;
3119 goto fail;
3122 req = cli_posix_unlink_send(frame,
3124 cli,
3125 fname);
3126 if (req == NULL) {
3127 status = NT_STATUS_NO_MEMORY;
3128 goto fail;
3131 if (!tevent_req_poll(req, ev)) {
3132 status = map_nt_error_from_unix(errno);
3133 goto fail;
3136 status = cli_posix_unlink_recv(req, frame);
3138 fail:
3139 TALLOC_FREE(frame);
3140 if (!NT_STATUS_IS_OK(status)) {
3141 cli_set_error(cli, status);
3143 return status;
3146 /****************************************************************************
3147 rmdir - POSIX semantics.
3148 ****************************************************************************/
3150 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3151 struct event_context *ev,
3152 struct cli_state *cli,
3153 const char *fname)
3155 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3158 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3160 NTSTATUS status;
3162 if (tevent_req_is_nterror(req, &status)) {
3163 return status;
3165 return NT_STATUS_OK;
3168 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3170 TALLOC_CTX *frame = talloc_stackframe();
3171 struct event_context *ev = NULL;
3172 struct tevent_req *req = NULL;
3173 NTSTATUS status = NT_STATUS_OK;
3175 if (cli_has_async_calls(cli)) {
3177 * Can't use sync call while an async call is in flight
3179 status = NT_STATUS_INVALID_PARAMETER;
3180 goto fail;
3183 ev = event_context_init(frame);
3184 if (ev == NULL) {
3185 status = NT_STATUS_NO_MEMORY;
3186 goto fail;
3189 req = cli_posix_rmdir_send(frame,
3191 cli,
3192 fname);
3193 if (req == NULL) {
3194 status = NT_STATUS_NO_MEMORY;
3195 goto fail;
3198 if (!tevent_req_poll(req, ev)) {
3199 status = map_nt_error_from_unix(errno);
3200 goto fail;
3203 status = cli_posix_rmdir_recv(req, frame);
3205 fail:
3206 TALLOC_FREE(frame);
3207 if (!NT_STATUS_IS_OK(status)) {
3208 cli_set_error(cli, status);
3210 return status;