s3: QNX doesn't know uint - replace with uint_t
[Samba.git] / source3 / libsmb / clifile.c
blobd9fdfb692776f4b6c9aed693b80845c11d92ef55
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
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 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 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 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 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 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 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 dev_major = IVAL(rdata,60);
315 uint32 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 mode, uint32 uid, uint32 gid)
353 unsigned int data_len = 0;
354 unsigned int param_len = 0;
355 uint16 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 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
434 char *p;
436 memset(cli->outbuf,'\0',smb_size);
437 memset(cli->inbuf,'\0',smb_size);
439 cli_set_message(cli->outbuf,1, 0, true);
441 SCVAL(cli->outbuf,smb_com,SMBmv);
442 SSVAL(cli->outbuf,smb_tid,cli->cnum);
443 cli_setup_packet(cli);
445 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
447 p = smb_buf(cli->outbuf);
448 *p++ = 4;
449 p += clistr_push(cli, p, fname_src,
450 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
451 *p++ = 4;
452 p += clistr_push(cli, p, fname_dst,
453 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
455 cli_setup_bcc(cli, p);
457 cli_send_smb(cli);
458 if (!cli_receive_smb(cli)) {
459 return false;
462 if (cli_is_error(cli)) {
463 return false;
466 return true;
469 /****************************************************************************
470 NT Rename a file.
471 ****************************************************************************/
473 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
475 char *p;
477 memset(cli->outbuf,'\0',smb_size);
478 memset(cli->inbuf,'\0',smb_size);
480 cli_set_message(cli->outbuf, 4, 0, true);
482 SCVAL(cli->outbuf,smb_com,SMBntrename);
483 SSVAL(cli->outbuf,smb_tid,cli->cnum);
484 cli_setup_packet(cli);
486 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
487 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
489 p = smb_buf(cli->outbuf);
490 *p++ = 4;
491 p += clistr_push(cli, p, fname_src,
492 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
493 *p++ = 4;
494 p += clistr_push(cli, p, fname_dst,
495 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
497 cli_setup_bcc(cli, p);
499 cli_send_smb(cli);
500 if (!cli_receive_smb(cli)) {
501 return false;
504 if (cli_is_error(cli)) {
505 return false;
508 return true;
511 /****************************************************************************
512 NT hardlink a file.
513 ****************************************************************************/
515 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
517 char *p;
519 memset(cli->outbuf,'\0',smb_size);
520 memset(cli->inbuf,'\0',smb_size);
522 cli_set_message(cli->outbuf, 4, 0, true);
524 SCVAL(cli->outbuf,smb_com,SMBntrename);
525 SSVAL(cli->outbuf,smb_tid,cli->cnum);
526 cli_setup_packet(cli);
528 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
529 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
531 p = smb_buf(cli->outbuf);
532 *p++ = 4;
533 p += clistr_push(cli, p, fname_src,
534 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
535 *p++ = 4;
536 p += clistr_push(cli, p, fname_dst,
537 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
539 cli_setup_bcc(cli, p);
541 cli_send_smb(cli);
542 if (!cli_receive_smb(cli)) {
543 return false;
546 if (cli_is_error(cli)) {
547 return false;
550 return true;
553 /****************************************************************************
554 Delete a file.
555 ****************************************************************************/
557 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
559 char *p;
561 memset(cli->outbuf,'\0',smb_size);
562 memset(cli->inbuf,'\0',smb_size);
564 cli_set_message(cli->outbuf,1, 0, true);
566 SCVAL(cli->outbuf,smb_com,SMBunlink);
567 SSVAL(cli->outbuf,smb_tid,cli->cnum);
568 cli_setup_packet(cli);
570 SSVAL(cli->outbuf,smb_vwv0, attrs);
572 p = smb_buf(cli->outbuf);
573 *p++ = 4;
574 p += clistr_push(cli, p, fname,
575 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
577 cli_setup_bcc(cli, p);
578 cli_send_smb(cli);
579 if (!cli_receive_smb(cli)) {
580 return false;
583 if (cli_is_error(cli)) {
584 return false;
587 return true;
590 /****************************************************************************
591 Delete a file.
592 ****************************************************************************/
594 bool cli_unlink(struct cli_state *cli, const char *fname)
596 return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
599 /****************************************************************************
600 Create a directory.
601 ****************************************************************************/
603 bool cli_mkdir(struct cli_state *cli, const char *dname)
605 char *p;
607 memset(cli->outbuf,'\0',smb_size);
608 memset(cli->inbuf,'\0',smb_size);
610 cli_set_message(cli->outbuf,0, 0, true);
612 SCVAL(cli->outbuf,smb_com,SMBmkdir);
613 SSVAL(cli->outbuf,smb_tid,cli->cnum);
614 cli_setup_packet(cli);
616 p = smb_buf(cli->outbuf);
617 *p++ = 4;
618 p += clistr_push(cli, p, dname,
619 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
621 cli_setup_bcc(cli, p);
623 cli_send_smb(cli);
624 if (!cli_receive_smb(cli)) {
625 return False;
628 if (cli_is_error(cli)) {
629 return False;
632 return True;
635 /****************************************************************************
636 Remove a directory.
637 ****************************************************************************/
639 bool cli_rmdir(struct cli_state *cli, const char *dname)
641 char *p;
643 memset(cli->outbuf,'\0',smb_size);
644 memset(cli->inbuf,'\0',smb_size);
646 cli_set_message(cli->outbuf,0, 0, true);
648 SCVAL(cli->outbuf,smb_com,SMBrmdir);
649 SSVAL(cli->outbuf,smb_tid,cli->cnum);
650 cli_setup_packet(cli);
652 p = smb_buf(cli->outbuf);
653 *p++ = 4;
654 p += clistr_push(cli, p, dname,
655 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
657 cli_setup_bcc(cli, p);
659 cli_send_smb(cli);
660 if (!cli_receive_smb(cli)) {
661 return false;
664 if (cli_is_error(cli)) {
665 return false;
668 return true;
671 /****************************************************************************
672 Set or clear the delete on close flag.
673 ****************************************************************************/
675 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
677 unsigned int data_len = 1;
678 unsigned int param_len = 6;
679 uint16 setup = TRANSACT2_SETFILEINFO;
680 char param[6];
681 unsigned char data;
682 char *rparam=NULL, *rdata=NULL;
684 memset(param, 0, param_len);
685 SSVAL(param,0,fnum);
686 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
688 data = flag ? 1 : 0;
690 if (!cli_send_trans(cli, SMBtrans2,
691 NULL, /* name */
692 -1, 0, /* fid, flags */
693 &setup, 1, 0, /* setup, length, max */
694 param, param_len, 2, /* param, length, max */
695 (char *)&data, data_len, cli->max_xmit /* data, length, max */
696 )) {
697 return false;
700 if (!cli_receive_trans(cli, SMBtrans2,
701 &rparam, &param_len,
702 &rdata, &data_len)) {
703 return false;
706 SAFE_FREE(rdata);
707 SAFE_FREE(rparam);
709 return true;
712 /****************************************************************************
713 Open a file - exposing the full horror of the NT API :-).
714 Used in smbtorture.
715 ****************************************************************************/
717 int cli_nt_create_full(struct cli_state *cli, const char *fname,
718 uint32 CreatFlags, uint32 DesiredAccess,
719 uint32 FileAttributes, uint32 ShareAccess,
720 uint32 CreateDisposition, uint32 CreateOptions,
721 uint8 SecurityFlags)
723 char *p;
724 int len;
726 memset(cli->outbuf,'\0',smb_size);
727 memset(cli->inbuf,'\0',smb_size);
729 cli_set_message(cli->outbuf,24,0, true);
731 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
732 SSVAL(cli->outbuf,smb_tid,cli->cnum);
733 cli_setup_packet(cli);
735 SSVAL(cli->outbuf,smb_vwv0,0xFF);
736 if (cli->use_oplocks)
737 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
739 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
740 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
741 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
742 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
743 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
744 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
745 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
746 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
747 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
749 p = smb_buf(cli->outbuf);
750 /* this alignment and termination is critical for netapp filers. Don't change */
751 p += clistr_align_out(cli, p, 0);
752 len = clistr_push(cli, p, fname,
753 cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
754 p += len;
755 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
756 /* sigh. this copes with broken netapp filer behaviour */
757 p += clistr_push(cli, p, "",
758 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
760 cli_setup_bcc(cli, p);
762 cli_send_smb(cli);
763 if (!cli_receive_smb(cli)) {
764 return -1;
767 if (cli_is_error(cli)) {
768 return -1;
771 return SVAL(cli->inbuf,smb_vwv2 + 1);
774 struct async_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
775 struct event_context *ev,
776 struct cli_state *cli,
777 const char *fname,
778 uint32_t CreatFlags,
779 uint32_t DesiredAccess,
780 uint32_t FileAttributes,
781 uint32_t ShareAccess,
782 uint32_t CreateDisposition,
783 uint32_t CreateOptions,
784 uint8_t SecurityFlags)
786 struct async_req *result;
787 uint8_t *bytes;
788 size_t converted_len;
789 uint16_t vwv[24];
791 SCVAL(vwv+0, 0, 0xFF);
792 SCVAL(vwv+0, 1, 0);
793 SSVAL(vwv+1, 0, 0);
794 SCVAL(vwv+2, 0, 0);
796 if (cli->use_oplocks) {
797 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
799 SIVAL(vwv+3, 1, CreatFlags);
800 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
801 SIVAL(vwv+7, 1, DesiredAccess);
802 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
803 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
804 SIVAL(vwv+13, 1, FileAttributes);
805 SIVAL(vwv+15, 1, ShareAccess);
806 SIVAL(vwv+17, 1, CreateDisposition);
807 SIVAL(vwv+19, 1, CreateOptions);
808 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
809 SCVAL(vwv+23, 1, SecurityFlags);
811 bytes = talloc_array(talloc_tos(), uint8_t, 0);
812 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
813 fname, strlen(fname)+1,
814 &converted_len);
816 /* sigh. this copes with broken netapp filer behaviour */
817 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
819 if (bytes == NULL) {
820 return NULL;
823 SSVAL(vwv+2, 1, converted_len);
825 result = cli_request_send(mem_ctx, ev, cli, SMBntcreateX, 0,
826 24, vwv, 0, talloc_get_size(bytes), bytes);
827 TALLOC_FREE(bytes);
828 return result;
831 NTSTATUS cli_ntcreate_recv(struct async_req *req, uint16_t *pfnum)
833 uint8_t wct;
834 uint16_t *vwv;
835 uint16_t num_bytes;
836 uint8_t *bytes;
837 NTSTATUS status;
839 if (async_req_is_nterror(req, &status)) {
840 return status;
843 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
844 if (!NT_STATUS_IS_OK(status)) {
845 return status;
848 if (wct < 3) {
849 return NT_STATUS_INVALID_NETWORK_RESPONSE;
852 *pfnum = SVAL(vwv+2, 1);
854 return NT_STATUS_OK;
857 NTSTATUS cli_ntcreate(struct cli_state *cli,
858 const char *fname,
859 uint32_t CreatFlags,
860 uint32_t DesiredAccess,
861 uint32_t FileAttributes,
862 uint32_t ShareAccess,
863 uint32_t CreateDisposition,
864 uint32_t CreateOptions,
865 uint8_t SecurityFlags,
866 uint16_t *pfid)
868 TALLOC_CTX *frame = talloc_stackframe();
869 struct event_context *ev;
870 struct async_req *req;
871 NTSTATUS status;
873 if (cli->fd_event != NULL) {
875 * Can't use sync call while an async call is in flight
877 status = NT_STATUS_INVALID_PARAMETER;
878 goto fail;
881 ev = event_context_init(frame);
882 if (ev == NULL) {
883 status = NT_STATUS_NO_MEMORY;
884 goto fail;
887 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
888 DesiredAccess, FileAttributes, ShareAccess,
889 CreateDisposition, CreateOptions,
890 SecurityFlags);
891 if (req == NULL) {
892 status = NT_STATUS_NO_MEMORY;
893 goto fail;
896 while (req->state < ASYNC_REQ_DONE) {
897 event_loop_once(ev);
900 status = cli_ntcreate_recv(req, pfid);
901 fail:
902 TALLOC_FREE(frame);
903 return status;
906 /****************************************************************************
907 Open a file.
908 ****************************************************************************/
910 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
912 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
913 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
916 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
917 const char *str, size_t str_len,
918 size_t *pconverted_size)
920 size_t buflen;
921 char *converted;
922 size_t converted_size;
924 if (buf == NULL) {
925 return NULL;
928 buflen = talloc_get_size(buf);
930 * We're pushing into an SMB buffer, align odd
932 if (ucs2 && (buflen % 2 == 0)) {
933 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
934 if (buf == NULL) {
935 return NULL;
937 buf[buflen] = '\0';
938 buflen += 1;
941 if (!convert_string_allocate(talloc_tos(), CH_UNIX,
942 ucs2 ? CH_UTF16LE : CH_DOS,
943 str, str_len, &converted,
944 &converted_size, true)) {
945 return NULL;
948 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
949 buflen + converted_size);
950 if (buf == NULL) {
951 TALLOC_FREE(converted);
952 return NULL;
955 memcpy(buf + buflen, converted, converted_size);
957 TALLOC_FREE(converted);
959 if (pconverted_size) {
960 *pconverted_size = converted_size;
963 return buf;
966 /****************************************************************************
967 Open a file
968 WARNING: if you open with O_WRONLY then getattrE won't work!
969 ****************************************************************************/
971 struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
972 struct cli_state *cli,
973 const char *fname, int flags, int share_mode)
975 unsigned openfn = 0;
976 unsigned accessmode = 0;
977 uint8_t additional_flags = 0;
978 uint8_t *bytes;
979 uint16_t vwv[15];
980 struct async_req *result;
982 if (flags & O_CREAT) {
983 openfn |= (1<<4);
985 if (!(flags & O_EXCL)) {
986 if (flags & O_TRUNC)
987 openfn |= (1<<1);
988 else
989 openfn |= (1<<0);
992 accessmode = (share_mode<<4);
994 if ((flags & O_ACCMODE) == O_RDWR) {
995 accessmode |= 2;
996 } else if ((flags & O_ACCMODE) == O_WRONLY) {
997 accessmode |= 1;
1000 #if defined(O_SYNC)
1001 if ((flags & O_SYNC) == O_SYNC) {
1002 accessmode |= (1<<14);
1004 #endif /* O_SYNC */
1006 if (share_mode == DENY_FCB) {
1007 accessmode = 0xFF;
1010 SCVAL(vwv + 0, 0, 0xFF);
1011 SCVAL(vwv + 0, 1, 0);
1012 SSVAL(vwv + 1, 0, 0);
1013 SSVAL(vwv + 2, 0, 0); /* no additional info */
1014 SSVAL(vwv + 3, 0, accessmode);
1015 SSVAL(vwv + 4, 0, aSYSTEM | aHIDDEN);
1016 SSVAL(vwv + 5, 0, 0);
1017 SIVAL(vwv + 6, 0, 0);
1018 SSVAL(vwv + 8, 0, openfn);
1019 SIVAL(vwv + 9, 0, 0);
1020 SIVAL(vwv + 11, 0, 0);
1021 SIVAL(vwv + 13, 0, 0);
1023 if (cli->use_oplocks) {
1024 /* if using oplocks then ask for a batch oplock via
1025 core and extended methods */
1026 additional_flags =
1027 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1028 SSVAL(vwv+2, 0, SVAL(vwv+2, 0) | 6);
1031 bytes = talloc_array(talloc_tos(), uint8_t, 0);
1032 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1033 strlen(fname)+1, NULL);
1034 if (bytes == NULL) {
1035 return NULL;
1038 result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
1039 15, vwv, 0, talloc_get_size(bytes), bytes);
1040 TALLOC_FREE(bytes);
1041 return result;
1044 NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
1046 uint8_t wct;
1047 uint16_t *vwv;
1048 uint16_t num_bytes;
1049 uint8_t *bytes;
1050 NTSTATUS status;
1052 if (async_req_is_nterror(req, &status)) {
1053 return status;
1056 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1057 if (!NT_STATUS_IS_OK(status)) {
1058 return status;
1061 if (wct < 3) {
1062 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1065 *fnum = SVAL(vwv+2, 0);
1067 return NT_STATUS_OK;
1070 int cli_open(struct cli_state *cli, const char *fname, int flags,
1071 int share_mode)
1073 TALLOC_CTX *frame = talloc_stackframe();
1074 struct event_context *ev;
1075 struct async_req *req;
1076 int result = -1;
1078 if (cli->fd_event != NULL) {
1080 * Can't use sync call while an async call is in flight
1082 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1083 goto fail;
1086 ev = event_context_init(frame);
1087 if (ev == NULL) {
1088 goto fail;
1091 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1092 if (req == NULL) {
1093 goto fail;
1096 while (req->state < ASYNC_REQ_DONE) {
1097 event_loop_once(ev);
1100 cli_open_recv(req, &result);
1101 fail:
1102 TALLOC_FREE(frame);
1103 return result;
1106 /****************************************************************************
1107 Close a file.
1108 ****************************************************************************/
1110 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1111 struct cli_state *cli, int fnum)
1113 uint16_t vwv[3];
1115 SSVAL(vwv+0, 0, fnum);
1116 SIVALS(vwv+1, 0, -1);
1118 return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
1119 0, NULL);
1122 NTSTATUS cli_close_recv(struct async_req *req)
1124 uint8_t wct;
1125 uint16_t *vwv;
1126 uint16_t num_bytes;
1127 uint8_t *bytes;
1128 NTSTATUS status;
1130 if (async_req_is_nterror(req, &status)) {
1131 return status;
1134 return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1137 bool cli_close(struct cli_state *cli, int fnum)
1139 TALLOC_CTX *frame = talloc_stackframe();
1140 struct event_context *ev;
1141 struct async_req *req;
1142 bool result = false;
1144 if (cli->fd_event != NULL) {
1146 * Can't use sync call while an async call is in flight
1148 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1149 goto fail;
1152 ev = event_context_init(frame);
1153 if (ev == NULL) {
1154 goto fail;
1157 req = cli_close_send(frame, ev, cli, fnum);
1158 if (req == NULL) {
1159 goto fail;
1162 while (req->state < ASYNC_REQ_DONE) {
1163 event_loop_once(ev);
1166 result = NT_STATUS_IS_OK(cli_close_recv(req));
1167 fail:
1168 TALLOC_FREE(frame);
1169 return result;
1172 /****************************************************************************
1173 Truncate a file to a specified size
1174 ****************************************************************************/
1176 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1178 unsigned int param_len = 6;
1179 unsigned int data_len = 8;
1180 uint16 setup = TRANSACT2_SETFILEINFO;
1181 char param[6];
1182 unsigned char data[8];
1183 char *rparam=NULL, *rdata=NULL;
1184 int saved_timeout = cli->timeout;
1186 SSVAL(param,0,fnum);
1187 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1188 SSVAL(param,4,0);
1190 SBVAL(data, 0, size);
1192 if (!cli_send_trans(cli, SMBtrans2,
1193 NULL, /* name */
1194 -1, 0, /* fid, flags */
1195 &setup, 1, 0, /* setup, length, max */
1196 param, param_len, 2, /* param, length, max */
1197 (char *)&data, data_len,/* data, length, ... */
1198 cli->max_xmit)) { /* ... max */
1199 cli->timeout = saved_timeout;
1200 return False;
1203 if (!cli_receive_trans(cli, SMBtrans2,
1204 &rparam, &param_len,
1205 &rdata, &data_len)) {
1206 cli->timeout = saved_timeout;
1207 SAFE_FREE(rdata);
1208 SAFE_FREE(rparam);
1209 return False;
1212 cli->timeout = saved_timeout;
1214 SAFE_FREE(rdata);
1215 SAFE_FREE(rparam);
1217 return True;
1221 /****************************************************************************
1222 send a lock with a specified locktype
1223 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1224 ****************************************************************************/
1226 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1227 uint32 offset, uint32 len,
1228 int timeout, unsigned char locktype)
1230 char *p;
1231 int saved_timeout = cli->timeout;
1233 memset(cli->outbuf,'\0',smb_size);
1234 memset(cli->inbuf,'\0', smb_size);
1236 cli_set_message(cli->outbuf,8,0,True);
1238 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1239 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1240 cli_setup_packet(cli);
1242 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1243 SSVAL(cli->outbuf,smb_vwv2,fnum);
1244 SCVAL(cli->outbuf,smb_vwv3,locktype);
1245 SIVALS(cli->outbuf, smb_vwv4, timeout);
1246 SSVAL(cli->outbuf,smb_vwv6,0);
1247 SSVAL(cli->outbuf,smb_vwv7,1);
1249 p = smb_buf(cli->outbuf);
1250 SSVAL(p, 0, cli->pid);
1251 SIVAL(p, 2, offset);
1252 SIVAL(p, 6, len);
1254 p += 10;
1256 cli_setup_bcc(cli, p);
1258 cli_send_smb(cli);
1260 if (timeout != 0) {
1261 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1264 if (!cli_receive_smb(cli)) {
1265 cli->timeout = saved_timeout;
1266 return NT_STATUS_UNSUCCESSFUL;
1269 cli->timeout = saved_timeout;
1271 return cli_nt_error(cli);
1274 /****************************************************************************
1275 Lock a file.
1276 note that timeout is in units of 2 milliseconds
1277 ****************************************************************************/
1279 bool cli_lock(struct cli_state *cli, int fnum,
1280 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1282 char *p;
1283 int saved_timeout = cli->timeout;
1285 memset(cli->outbuf,'\0',smb_size);
1286 memset(cli->inbuf,'\0', smb_size);
1288 cli_set_message(cli->outbuf,8,0,True);
1290 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1291 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1292 cli_setup_packet(cli);
1294 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1295 SSVAL(cli->outbuf,smb_vwv2,fnum);
1296 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1297 SIVALS(cli->outbuf, smb_vwv4, timeout);
1298 SSVAL(cli->outbuf,smb_vwv6,0);
1299 SSVAL(cli->outbuf,smb_vwv7,1);
1301 p = smb_buf(cli->outbuf);
1302 SSVAL(p, 0, cli->pid);
1303 SIVAL(p, 2, offset);
1304 SIVAL(p, 6, len);
1306 p += 10;
1308 cli_setup_bcc(cli, p);
1310 cli_send_smb(cli);
1312 if (timeout != 0) {
1313 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1316 if (!cli_receive_smb(cli)) {
1317 cli->timeout = saved_timeout;
1318 return False;
1321 cli->timeout = saved_timeout;
1323 if (cli_is_error(cli)) {
1324 return False;
1327 return True;
1330 /****************************************************************************
1331 Unlock a file.
1332 ****************************************************************************/
1334 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1336 char *p;
1338 memset(cli->outbuf,'\0',smb_size);
1339 memset(cli->inbuf,'\0',smb_size);
1341 cli_set_message(cli->outbuf,8,0,True);
1343 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1344 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1345 cli_setup_packet(cli);
1347 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1348 SSVAL(cli->outbuf,smb_vwv2,fnum);
1349 SCVAL(cli->outbuf,smb_vwv3,0);
1350 SIVALS(cli->outbuf, smb_vwv4, 0);
1351 SSVAL(cli->outbuf,smb_vwv6,1);
1352 SSVAL(cli->outbuf,smb_vwv7,0);
1354 p = smb_buf(cli->outbuf);
1355 SSVAL(p, 0, cli->pid);
1356 SIVAL(p, 2, offset);
1357 SIVAL(p, 6, len);
1358 p += 10;
1359 cli_setup_bcc(cli, p);
1360 cli_send_smb(cli);
1361 if (!cli_receive_smb(cli)) {
1362 return False;
1365 if (cli_is_error(cli)) {
1366 return False;
1369 return True;
1372 /****************************************************************************
1373 Lock a file with 64 bit offsets.
1374 ****************************************************************************/
1376 bool cli_lock64(struct cli_state *cli, int fnum,
1377 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1379 char *p;
1380 int saved_timeout = cli->timeout;
1381 int ltype;
1383 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1384 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1387 ltype = (lock_type == READ_LOCK? 1 : 0);
1388 ltype |= LOCKING_ANDX_LARGE_FILES;
1390 memset(cli->outbuf,'\0',smb_size);
1391 memset(cli->inbuf,'\0', smb_size);
1393 cli_set_message(cli->outbuf,8,0,True);
1395 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1396 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1397 cli_setup_packet(cli);
1399 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1400 SSVAL(cli->outbuf,smb_vwv2,fnum);
1401 SCVAL(cli->outbuf,smb_vwv3,ltype);
1402 SIVALS(cli->outbuf, smb_vwv4, timeout);
1403 SSVAL(cli->outbuf,smb_vwv6,0);
1404 SSVAL(cli->outbuf,smb_vwv7,1);
1406 p = smb_buf(cli->outbuf);
1407 SIVAL(p, 0, cli->pid);
1408 SOFF_T_R(p, 4, offset);
1409 SOFF_T_R(p, 12, len);
1410 p += 20;
1412 cli_setup_bcc(cli, p);
1413 cli_send_smb(cli);
1415 if (timeout != 0) {
1416 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1419 if (!cli_receive_smb(cli)) {
1420 cli->timeout = saved_timeout;
1421 return False;
1424 cli->timeout = saved_timeout;
1426 if (cli_is_error(cli)) {
1427 return False;
1430 return True;
1433 /****************************************************************************
1434 Unlock a file with 64 bit offsets.
1435 ****************************************************************************/
1437 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1439 char *p;
1441 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1442 return cli_unlock(cli, fnum, offset, len);
1445 memset(cli->outbuf,'\0',smb_size);
1446 memset(cli->inbuf,'\0',smb_size);
1448 cli_set_message(cli->outbuf,8,0,True);
1450 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1451 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1452 cli_setup_packet(cli);
1454 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1455 SSVAL(cli->outbuf,smb_vwv2,fnum);
1456 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1457 SIVALS(cli->outbuf, smb_vwv4, 0);
1458 SSVAL(cli->outbuf,smb_vwv6,1);
1459 SSVAL(cli->outbuf,smb_vwv7,0);
1461 p = smb_buf(cli->outbuf);
1462 SIVAL(p, 0, cli->pid);
1463 SOFF_T_R(p, 4, offset);
1464 SOFF_T_R(p, 12, len);
1465 p += 20;
1466 cli_setup_bcc(cli, p);
1467 cli_send_smb(cli);
1468 if (!cli_receive_smb(cli)) {
1469 return False;
1472 if (cli_is_error(cli)) {
1473 return False;
1476 return True;
1479 /****************************************************************************
1480 Get/unlock a POSIX lock on a file - internal function.
1481 ****************************************************************************/
1483 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1484 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1486 unsigned int param_len = 4;
1487 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1488 uint16 setup = TRANSACT2_SETFILEINFO;
1489 char param[4];
1490 unsigned char data[POSIX_LOCK_DATA_SIZE];
1491 char *rparam=NULL, *rdata=NULL;
1492 int saved_timeout = cli->timeout;
1494 SSVAL(param,0,fnum);
1495 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1497 switch (lock_type) {
1498 case READ_LOCK:
1499 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1500 break;
1501 case WRITE_LOCK:
1502 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1503 break;
1504 case UNLOCK_LOCK:
1505 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1506 break;
1507 default:
1508 return False;
1511 if (wait_lock) {
1512 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1513 cli->timeout = 0x7FFFFFFF;
1514 } else {
1515 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1518 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1519 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1520 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1522 if (!cli_send_trans(cli, SMBtrans2,
1523 NULL, /* name */
1524 -1, 0, /* fid, flags */
1525 &setup, 1, 0, /* setup, length, max */
1526 param, param_len, 2, /* param, length, max */
1527 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1528 )) {
1529 cli->timeout = saved_timeout;
1530 return False;
1533 if (!cli_receive_trans(cli, SMBtrans2,
1534 &rparam, &param_len,
1535 &rdata, &data_len)) {
1536 cli->timeout = saved_timeout;
1537 SAFE_FREE(rdata);
1538 SAFE_FREE(rparam);
1539 return False;
1542 cli->timeout = saved_timeout;
1544 SAFE_FREE(rdata);
1545 SAFE_FREE(rparam);
1547 return True;
1550 /****************************************************************************
1551 POSIX Lock a file.
1552 ****************************************************************************/
1554 bool cli_posix_lock(struct cli_state *cli, int fnum,
1555 uint64_t offset, uint64_t len,
1556 bool wait_lock, enum brl_type lock_type)
1558 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1559 return False;
1561 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1564 /****************************************************************************
1565 POSIX Unlock a file.
1566 ****************************************************************************/
1568 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1570 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1573 /****************************************************************************
1574 POSIX Get any lock covering a file.
1575 ****************************************************************************/
1577 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1579 return True;
1582 /****************************************************************************
1583 Do a SMBgetattrE call.
1584 ****************************************************************************/
1586 bool cli_getattrE(struct cli_state *cli, int fd,
1587 uint16 *attr, SMB_OFF_T *size,
1588 time_t *change_time,
1589 time_t *access_time,
1590 time_t *write_time)
1592 memset(cli->outbuf,'\0',smb_size);
1593 memset(cli->inbuf,'\0',smb_size);
1595 cli_set_message(cli->outbuf,1,0,True);
1597 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1598 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1599 cli_setup_packet(cli);
1601 SSVAL(cli->outbuf,smb_vwv0,fd);
1603 cli_send_smb(cli);
1604 if (!cli_receive_smb(cli)) {
1605 return False;
1608 if (cli_is_error(cli)) {
1609 return False;
1612 if (size) {
1613 *size = IVAL(cli->inbuf, smb_vwv6);
1616 if (attr) {
1617 *attr = SVAL(cli->inbuf,smb_vwv10);
1620 if (change_time) {
1621 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1624 if (access_time) {
1625 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1628 if (write_time) {
1629 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1632 return True;
1635 /****************************************************************************
1636 Do a SMBgetatr call
1637 ****************************************************************************/
1639 bool cli_getatr(struct cli_state *cli, const char *fname,
1640 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1642 char *p;
1644 memset(cli->outbuf,'\0',smb_size);
1645 memset(cli->inbuf,'\0',smb_size);
1647 cli_set_message(cli->outbuf,0,0,True);
1649 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1650 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1651 cli_setup_packet(cli);
1653 p = smb_buf(cli->outbuf);
1654 *p++ = 4;
1655 p += clistr_push(cli, p, fname,
1656 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1658 cli_setup_bcc(cli, p);
1660 cli_send_smb(cli);
1661 if (!cli_receive_smb(cli)) {
1662 return False;
1665 if (cli_is_error(cli)) {
1666 return False;
1669 if (size) {
1670 *size = IVAL(cli->inbuf, smb_vwv3);
1673 if (write_time) {
1674 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1677 if (attr) {
1678 *attr = SVAL(cli->inbuf,smb_vwv0);
1681 return True;
1684 /****************************************************************************
1685 Do a SMBsetattrE call.
1686 ****************************************************************************/
1688 bool cli_setattrE(struct cli_state *cli, int fd,
1689 time_t change_time,
1690 time_t access_time,
1691 time_t write_time)
1694 char *p;
1696 memset(cli->outbuf,'\0',smb_size);
1697 memset(cli->inbuf,'\0',smb_size);
1699 cli_set_message(cli->outbuf,7,0,True);
1701 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1702 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1703 cli_setup_packet(cli);
1705 SSVAL(cli->outbuf,smb_vwv0, fd);
1706 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1707 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1708 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1710 p = smb_buf(cli->outbuf);
1711 *p++ = 4;
1713 cli_setup_bcc(cli, p);
1715 cli_send_smb(cli);
1716 if (!cli_receive_smb(cli)) {
1717 return False;
1720 if (cli_is_error(cli)) {
1721 return False;
1724 return True;
1727 /****************************************************************************
1728 Do a SMBsetatr call.
1729 ****************************************************************************/
1731 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1733 char *p;
1735 memset(cli->outbuf,'\0',smb_size);
1736 memset(cli->inbuf,'\0',smb_size);
1738 cli_set_message(cli->outbuf,8,0,True);
1740 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1741 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1742 cli_setup_packet(cli);
1744 SSVAL(cli->outbuf,smb_vwv0, attr);
1745 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1747 p = smb_buf(cli->outbuf);
1748 *p++ = 4;
1749 p += clistr_push(cli, p, fname,
1750 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1751 *p++ = 4;
1753 cli_setup_bcc(cli, p);
1755 cli_send_smb(cli);
1756 if (!cli_receive_smb(cli)) {
1757 return False;
1760 if (cli_is_error(cli)) {
1761 return False;
1764 return True;
1767 /****************************************************************************
1768 Check for existance of a dir.
1769 ****************************************************************************/
1771 bool cli_chkpath(struct cli_state *cli, const char *path)
1773 char *path2 = NULL;
1774 char *p;
1775 TALLOC_CTX *frame = talloc_stackframe();
1777 path2 = talloc_strdup(frame, path);
1778 if (!path2) {
1779 TALLOC_FREE(frame);
1780 return false;
1782 trim_char(path2,'\0','\\');
1783 if (!*path2) {
1784 path2 = talloc_strdup(frame, "\\");
1785 if (!path2) {
1786 TALLOC_FREE(frame);
1787 return false;
1791 memset(cli->outbuf,'\0',smb_size);
1792 cli_set_message(cli->outbuf,0,0,True);
1793 SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1794 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1795 cli_setup_packet(cli);
1796 p = smb_buf(cli->outbuf);
1797 *p++ = 4;
1798 p += clistr_push(cli, p, path2,
1799 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1801 cli_setup_bcc(cli, p);
1803 cli_send_smb(cli);
1804 if (!cli_receive_smb(cli)) {
1805 TALLOC_FREE(frame);
1806 return False;
1809 TALLOC_FREE(frame);
1811 if (cli_is_error(cli)) return False;
1813 return True;
1816 /****************************************************************************
1817 Query disk space.
1818 ****************************************************************************/
1820 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1822 memset(cli->outbuf,'\0',smb_size);
1823 cli_set_message(cli->outbuf,0,0,True);
1824 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1825 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1826 cli_setup_packet(cli);
1828 cli_send_smb(cli);
1829 if (!cli_receive_smb(cli)) {
1830 return False;
1833 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1834 *total = SVAL(cli->inbuf,smb_vwv0);
1835 *avail = SVAL(cli->inbuf,smb_vwv3);
1837 return True;
1840 /****************************************************************************
1841 Create and open a temporary file.
1842 ****************************************************************************/
1844 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1846 int len;
1847 char *p;
1849 memset(cli->outbuf,'\0',smb_size);
1850 memset(cli->inbuf,'\0',smb_size);
1852 cli_set_message(cli->outbuf,3,0,True);
1854 SCVAL(cli->outbuf,smb_com,SMBctemp);
1855 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1856 cli_setup_packet(cli);
1858 SSVAL(cli->outbuf,smb_vwv0,0);
1859 SIVALS(cli->outbuf,smb_vwv1,-1);
1861 p = smb_buf(cli->outbuf);
1862 *p++ = 4;
1863 p += clistr_push(cli, p, path,
1864 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1866 cli_setup_bcc(cli, p);
1868 cli_send_smb(cli);
1869 if (!cli_receive_smb(cli)) {
1870 return -1;
1873 if (cli_is_error(cli)) {
1874 return -1;
1877 /* despite the spec, the result has a -1, followed by
1878 length, followed by name */
1879 p = smb_buf(cli->inbuf);
1880 p += 4;
1881 len = smb_buflen(cli->inbuf) - 4;
1882 if (len <= 0 || len > PATH_MAX) return -1;
1884 if (tmp_path) {
1885 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1886 if (!path2) {
1887 return -1;
1889 clistr_pull(cli->inbuf, path2, p,
1890 len+1, len, STR_ASCII);
1891 *tmp_path = path2;
1894 return SVAL(cli->inbuf,smb_vwv0);
1898 send a raw ioctl - used by the torture code
1900 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1902 memset(cli->outbuf,'\0',smb_size);
1903 memset(cli->inbuf,'\0',smb_size);
1905 cli_set_message(cli->outbuf, 3, 0, True);
1906 SCVAL(cli->outbuf,smb_com,SMBioctl);
1907 cli_setup_packet(cli);
1909 SSVAL(cli->outbuf, smb_vwv0, fnum);
1910 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1911 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1913 cli_send_smb(cli);
1914 if (!cli_receive_smb(cli)) {
1915 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1918 if (cli_is_error(cli)) {
1919 return cli_nt_error(cli);
1922 *blob = data_blob_null;
1924 return NT_STATUS_OK;
1927 /*********************************************************
1928 Set an extended attribute utility fn.
1929 *********************************************************/
1931 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1932 const char *ea_name, const char *ea_val, size_t ea_len)
1934 unsigned int data_len = 0;
1935 char *data = NULL;
1936 char *rparam=NULL, *rdata=NULL;
1937 char *p;
1938 size_t ea_namelen = strlen(ea_name);
1940 if (ea_namelen == 0 && ea_len == 0) {
1941 data_len = 4;
1942 data = (char *)SMB_MALLOC(data_len);
1943 if (!data) {
1944 return False;
1946 p = data;
1947 SIVAL(p,0,data_len);
1948 } else {
1949 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1950 data = (char *)SMB_MALLOC(data_len);
1951 if (!data) {
1952 return False;
1954 p = data;
1955 SIVAL(p,0,data_len);
1956 p += 4;
1957 SCVAL(p, 0, 0); /* EA flags. */
1958 SCVAL(p, 1, ea_namelen);
1959 SSVAL(p, 2, ea_len);
1960 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1961 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1964 if (!cli_send_trans(cli, SMBtrans2,
1965 NULL, /* name */
1966 -1, 0, /* fid, flags */
1967 &setup, 1, 0, /* setup, length, max */
1968 param, param_len, 2, /* param, length, max */
1969 data, data_len, cli->max_xmit /* data, length, max */
1970 )) {
1971 SAFE_FREE(data);
1972 return False;
1975 if (!cli_receive_trans(cli, SMBtrans2,
1976 &rparam, &param_len,
1977 &rdata, &data_len)) {
1978 SAFE_FREE(data);
1979 return false;
1982 SAFE_FREE(data);
1983 SAFE_FREE(rdata);
1984 SAFE_FREE(rparam);
1986 return True;
1989 /*********************************************************
1990 Set an extended attribute on a pathname.
1991 *********************************************************/
1993 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1995 uint16 setup = TRANSACT2_SETPATHINFO;
1996 unsigned int param_len = 0;
1997 char *param;
1998 size_t srclen = 2*(strlen(path)+1);
1999 char *p;
2000 bool ret;
2002 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2003 if (!param) {
2004 return false;
2006 memset(param, '\0', 6);
2007 SSVAL(param,0,SMB_INFO_SET_EA);
2008 p = &param[6];
2010 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2011 param_len = PTR_DIFF(p, param);
2013 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2014 SAFE_FREE(param);
2015 return ret;
2018 /*********************************************************
2019 Set an extended attribute on an fnum.
2020 *********************************************************/
2022 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2024 char param[6];
2025 uint16 setup = TRANSACT2_SETFILEINFO;
2027 memset(param, 0, 6);
2028 SSVAL(param,0,fnum);
2029 SSVAL(param,2,SMB_INFO_SET_EA);
2031 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2034 /*********************************************************
2035 Get an extended attribute list utility fn.
2036 *********************************************************/
2038 static bool cli_get_ea_list(struct cli_state *cli,
2039 uint16 setup, char *param, unsigned int param_len,
2040 TALLOC_CTX *ctx,
2041 size_t *pnum_eas,
2042 struct ea_struct **pea_list)
2044 unsigned int data_len = 0;
2045 unsigned int rparam_len, rdata_len;
2046 char *rparam=NULL, *rdata=NULL;
2047 char *p;
2048 size_t ea_size;
2049 size_t num_eas;
2050 bool ret = False;
2051 struct ea_struct *ea_list;
2053 *pnum_eas = 0;
2054 if (pea_list) {
2055 *pea_list = NULL;
2058 if (!cli_send_trans(cli, SMBtrans2,
2059 NULL, /* Name */
2060 -1, 0, /* fid, flags */
2061 &setup, 1, 0, /* setup, length, max */
2062 param, param_len, 10, /* param, length, max */
2063 NULL, data_len, cli->max_xmit /* data, length, max */
2064 )) {
2065 return False;
2068 if (!cli_receive_trans(cli, SMBtrans2,
2069 &rparam, &rparam_len,
2070 &rdata, &rdata_len)) {
2071 return False;
2074 if (!rdata || rdata_len < 4) {
2075 goto out;
2078 ea_size = (size_t)IVAL(rdata,0);
2079 if (ea_size > rdata_len) {
2080 goto out;
2083 if (ea_size == 0) {
2084 /* No EA's present. */
2085 ret = True;
2086 goto out;
2089 p = rdata + 4;
2090 ea_size -= 4;
2092 /* Validate the EA list and count it. */
2093 for (num_eas = 0; ea_size >= 4; num_eas++) {
2094 unsigned int ea_namelen = CVAL(p,1);
2095 unsigned int ea_valuelen = SVAL(p,2);
2096 if (ea_namelen == 0) {
2097 goto out;
2099 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2100 goto out;
2102 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2103 p += 4 + ea_namelen + 1 + ea_valuelen;
2106 if (num_eas == 0) {
2107 ret = True;
2108 goto out;
2111 *pnum_eas = num_eas;
2112 if (!pea_list) {
2113 /* Caller only wants number of EA's. */
2114 ret = True;
2115 goto out;
2118 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2119 if (!ea_list) {
2120 goto out;
2123 ea_size = (size_t)IVAL(rdata,0);
2124 p = rdata + 4;
2126 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2127 struct ea_struct *ea = &ea_list[num_eas];
2128 fstring unix_ea_name;
2129 unsigned int ea_namelen = CVAL(p,1);
2130 unsigned int ea_valuelen = SVAL(p,2);
2132 ea->flags = CVAL(p,0);
2133 unix_ea_name[0] = '\0';
2134 pull_ascii_fstring(unix_ea_name, p + 4);
2135 ea->name = talloc_strdup(ctx, unix_ea_name);
2136 /* Ensure the value is null terminated (in case it's a string). */
2137 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2138 if (!ea->value.data) {
2139 goto out;
2141 if (ea_valuelen) {
2142 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2144 ea->value.data[ea_valuelen] = 0;
2145 ea->value.length--;
2146 p += 4 + ea_namelen + 1 + ea_valuelen;
2149 *pea_list = ea_list;
2150 ret = True;
2152 out :
2154 SAFE_FREE(rdata);
2155 SAFE_FREE(rparam);
2156 return ret;
2159 /*********************************************************
2160 Get an extended attribute list from a pathname.
2161 *********************************************************/
2163 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2164 TALLOC_CTX *ctx,
2165 size_t *pnum_eas,
2166 struct ea_struct **pea_list)
2168 uint16 setup = TRANSACT2_QPATHINFO;
2169 unsigned int param_len = 0;
2170 char *param;
2171 char *p;
2172 size_t srclen = 2*(strlen(path)+1);
2173 bool ret;
2175 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2176 if (!param) {
2177 return false;
2179 p = param;
2180 memset(p, 0, 6);
2181 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2182 p += 6;
2183 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2184 param_len = PTR_DIFF(p, param);
2186 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2187 SAFE_FREE(param);
2188 return ret;
2191 /*********************************************************
2192 Get an extended attribute list from an fnum.
2193 *********************************************************/
2195 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2196 TALLOC_CTX *ctx,
2197 size_t *pnum_eas,
2198 struct ea_struct **pea_list)
2200 uint16 setup = TRANSACT2_QFILEINFO;
2201 char param[6];
2203 memset(param, 0, 6);
2204 SSVAL(param,0,fnum);
2205 SSVAL(param,2,SMB_INFO_SET_EA);
2207 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2210 /****************************************************************************
2211 Convert open "flags" arg to uint32 on wire.
2212 ****************************************************************************/
2214 static uint32 open_flags_to_wire(int flags)
2216 int open_mode = flags & O_ACCMODE;
2217 uint32 ret = 0;
2219 switch (open_mode) {
2220 case O_WRONLY:
2221 ret |= SMB_O_WRONLY;
2222 break;
2223 case O_RDWR:
2224 ret |= SMB_O_RDWR;
2225 break;
2226 default:
2227 case O_RDONLY:
2228 ret |= SMB_O_RDONLY;
2229 break;
2232 if (flags & O_CREAT) {
2233 ret |= SMB_O_CREAT;
2235 if (flags & O_EXCL) {
2236 ret |= SMB_O_EXCL;
2238 if (flags & O_TRUNC) {
2239 ret |= SMB_O_TRUNC;
2241 #if defined(O_SYNC)
2242 if (flags & O_SYNC) {
2243 ret |= SMB_O_SYNC;
2245 #endif /* O_SYNC */
2246 if (flags & O_APPEND) {
2247 ret |= SMB_O_APPEND;
2249 #if defined(O_DIRECT)
2250 if (flags & O_DIRECT) {
2251 ret |= SMB_O_DIRECT;
2253 #endif
2254 #if defined(O_DIRECTORY)
2255 if (flags & O_DIRECTORY) {
2256 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2257 ret |= SMB_O_DIRECTORY;
2259 #endif
2260 return ret;
2263 /****************************************************************************
2264 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2265 ****************************************************************************/
2267 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2269 unsigned int data_len = 0;
2270 unsigned int param_len = 0;
2271 uint16 setup = TRANSACT2_SETPATHINFO;
2272 char *param;
2273 char data[18];
2274 char *rparam=NULL, *rdata=NULL;
2275 char *p;
2276 int fnum = -1;
2277 uint32 wire_flags = open_flags_to_wire(flags);
2278 size_t srclen = 2*(strlen(fname)+1);
2280 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2281 if (!param) {
2282 return false;
2284 memset(param, '\0', 6);
2285 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2286 p = &param[6];
2288 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2289 param_len = PTR_DIFF(p, param);
2291 if (is_dir) {
2292 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2293 wire_flags |= SMB_O_DIRECTORY;
2296 p = data;
2297 SIVAL(p,0,0); /* No oplock. */
2298 SIVAL(p,4,wire_flags);
2299 SIVAL(p,8,unix_perms_to_wire(mode));
2300 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2301 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2303 data_len = 18;
2305 if (!cli_send_trans(cli, SMBtrans2,
2306 NULL, /* name */
2307 -1, 0, /* fid, flags */
2308 &setup, 1, 0, /* setup, length, max */
2309 param, param_len, 2, /* param, length, max */
2310 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2311 )) {
2312 SAFE_FREE(param);
2313 return -1;
2316 SAFE_FREE(param);
2318 if (!cli_receive_trans(cli, SMBtrans2,
2319 &rparam, &param_len,
2320 &rdata, &data_len)) {
2321 return -1;
2324 fnum = SVAL(rdata,2);
2326 SAFE_FREE(rdata);
2327 SAFE_FREE(rparam);
2329 return fnum;
2332 /****************************************************************************
2333 open - POSIX semantics.
2334 ****************************************************************************/
2336 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2338 return cli_posix_open_internal(cli, fname, flags, mode, False);
2341 /****************************************************************************
2342 mkdir - POSIX semantics.
2343 ****************************************************************************/
2345 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2347 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2350 /****************************************************************************
2351 unlink or rmdir - POSIX semantics.
2352 ****************************************************************************/
2354 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2356 unsigned int data_len = 0;
2357 unsigned int param_len = 0;
2358 uint16 setup = TRANSACT2_SETPATHINFO;
2359 char *param;
2360 char data[2];
2361 char *rparam=NULL, *rdata=NULL;
2362 char *p;
2363 size_t srclen = 2*(strlen(fname)+1);
2365 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2366 if (!param) {
2367 return false;
2369 memset(param, '\0', 6);
2370 SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2371 p = &param[6];
2373 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2374 param_len = PTR_DIFF(p, param);
2376 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2377 SMB_POSIX_UNLINK_FILE_TARGET);
2378 data_len = 2;
2380 if (!cli_send_trans(cli, SMBtrans2,
2381 NULL, /* name */
2382 -1, 0, /* fid, flags */
2383 &setup, 1, 0, /* setup, length, max */
2384 param, param_len, 2, /* param, length, max */
2385 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2386 )) {
2387 SAFE_FREE(param);
2388 return False;
2391 SAFE_FREE(param);
2393 if (!cli_receive_trans(cli, SMBtrans2,
2394 &rparam, &param_len,
2395 &rdata, &data_len)) {
2396 return False;
2399 SAFE_FREE(rdata);
2400 SAFE_FREE(rparam);
2402 return True;
2405 /****************************************************************************
2406 unlink - POSIX semantics.
2407 ****************************************************************************/
2409 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2411 return cli_posix_unlink_internal(cli, fname, False);
2414 /****************************************************************************
2415 rmdir - POSIX semantics.
2416 ****************************************************************************/
2418 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2420 return cli_posix_unlink_internal(cli, fname, True);