s3/docs: Fix typo.
[Samba.git] / source / libsmb / clifile.c
blob26cf6b09886482f67c7515cb378b19dedaa9405a
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 /****************************************************************************
775 Open a file.
776 ****************************************************************************/
778 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
780 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
781 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
784 /****************************************************************************
785 Open a file
786 WARNING: if you open with O_WRONLY then getattrE won't work!
787 ****************************************************************************/
789 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
791 char *p;
792 unsigned openfn=0;
793 unsigned accessmode=0;
795 if (flags & O_CREAT)
796 openfn |= (1<<4);
797 if (!(flags & O_EXCL)) {
798 if (flags & O_TRUNC)
799 openfn |= (1<<1);
800 else
801 openfn |= (1<<0);
804 accessmode = (share_mode<<4);
806 if ((flags & O_ACCMODE) == O_RDWR) {
807 accessmode |= 2;
808 } else if ((flags & O_ACCMODE) == O_WRONLY) {
809 accessmode |= 1;
812 #if defined(O_SYNC)
813 if ((flags & O_SYNC) == O_SYNC) {
814 accessmode |= (1<<14);
816 #endif /* O_SYNC */
818 if (share_mode == DENY_FCB) {
819 accessmode = 0xFF;
822 memset(cli->outbuf,'\0',smb_size);
823 memset(cli->inbuf,'\0',smb_size);
825 cli_set_message(cli->outbuf,15,0, true);
827 SCVAL(cli->outbuf,smb_com,SMBopenX);
828 SSVAL(cli->outbuf,smb_tid,cli->cnum);
829 cli_setup_packet(cli);
831 SSVAL(cli->outbuf,smb_vwv0,0xFF);
832 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
833 SSVAL(cli->outbuf,smb_vwv3,accessmode);
834 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
835 SSVAL(cli->outbuf,smb_vwv5,0);
836 SSVAL(cli->outbuf,smb_vwv8,openfn);
838 if (cli->use_oplocks) {
839 /* if using oplocks then ask for a batch oplock via
840 core and extended methods */
841 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
842 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
843 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
846 p = smb_buf(cli->outbuf);
847 p += clistr_push(cli, p, fname,
848 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
850 cli_setup_bcc(cli, p);
852 cli_send_smb(cli);
853 if (!cli_receive_smb(cli)) {
854 return -1;
857 if (cli_is_error(cli)) {
858 return -1;
861 return SVAL(cli->inbuf,smb_vwv2);
864 /****************************************************************************
865 Close a file.
866 ****************************************************************************/
868 bool cli_close(struct cli_state *cli, int fnum)
870 memset(cli->outbuf,'\0',smb_size);
871 memset(cli->inbuf,'\0',smb_size);
873 cli_set_message(cli->outbuf,3,0,True);
875 SCVAL(cli->outbuf,smb_com,SMBclose);
876 SSVAL(cli->outbuf,smb_tid,cli->cnum);
877 cli_setup_packet(cli);
879 SSVAL(cli->outbuf,smb_vwv0,fnum);
880 SIVALS(cli->outbuf,smb_vwv1,-1);
882 cli_send_smb(cli);
883 if (!cli_receive_smb(cli)) {
884 return False;
887 return !cli_is_error(cli);
891 /****************************************************************************
892 Truncate a file to a specified size
893 ****************************************************************************/
895 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
897 unsigned int param_len = 6;
898 unsigned int data_len = 8;
899 uint16 setup = TRANSACT2_SETFILEINFO;
900 char param[6];
901 unsigned char data[8];
902 char *rparam=NULL, *rdata=NULL;
903 int saved_timeout = cli->timeout;
905 SSVAL(param,0,fnum);
906 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
907 SSVAL(param,4,0);
909 SBVAL(data, 0, size);
911 if (!cli_send_trans(cli, SMBtrans2,
912 NULL, /* name */
913 -1, 0, /* fid, flags */
914 &setup, 1, 0, /* setup, length, max */
915 param, param_len, 2, /* param, length, max */
916 (char *)&data, data_len,/* data, length, ... */
917 cli->max_xmit)) { /* ... max */
918 cli->timeout = saved_timeout;
919 return False;
922 if (!cli_receive_trans(cli, SMBtrans2,
923 &rparam, &param_len,
924 &rdata, &data_len)) {
925 cli->timeout = saved_timeout;
926 SAFE_FREE(rdata);
927 SAFE_FREE(rparam);
928 return False;
931 cli->timeout = saved_timeout;
933 SAFE_FREE(rdata);
934 SAFE_FREE(rparam);
936 return True;
940 /****************************************************************************
941 send a lock with a specified locktype
942 this is used for testing LOCKING_ANDX_CANCEL_LOCK
943 ****************************************************************************/
945 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
946 uint32 offset, uint32 len,
947 int timeout, unsigned char locktype)
949 char *p;
950 int saved_timeout = cli->timeout;
952 memset(cli->outbuf,'\0',smb_size);
953 memset(cli->inbuf,'\0', smb_size);
955 cli_set_message(cli->outbuf,8,0,True);
957 SCVAL(cli->outbuf,smb_com,SMBlockingX);
958 SSVAL(cli->outbuf,smb_tid,cli->cnum);
959 cli_setup_packet(cli);
961 SCVAL(cli->outbuf,smb_vwv0,0xFF);
962 SSVAL(cli->outbuf,smb_vwv2,fnum);
963 SCVAL(cli->outbuf,smb_vwv3,locktype);
964 SIVALS(cli->outbuf, smb_vwv4, timeout);
965 SSVAL(cli->outbuf,smb_vwv6,0);
966 SSVAL(cli->outbuf,smb_vwv7,1);
968 p = smb_buf(cli->outbuf);
969 SSVAL(p, 0, cli->pid);
970 SIVAL(p, 2, offset);
971 SIVAL(p, 6, len);
973 p += 10;
975 cli_setup_bcc(cli, p);
977 cli_send_smb(cli);
979 if (timeout != 0) {
980 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
983 if (!cli_receive_smb(cli)) {
984 cli->timeout = saved_timeout;
985 return NT_STATUS_UNSUCCESSFUL;
988 cli->timeout = saved_timeout;
990 return cli_nt_error(cli);
993 /****************************************************************************
994 Lock a file.
995 note that timeout is in units of 2 milliseconds
996 ****************************************************************************/
998 bool cli_lock(struct cli_state *cli, int fnum,
999 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1001 char *p;
1002 int saved_timeout = cli->timeout;
1004 memset(cli->outbuf,'\0',smb_size);
1005 memset(cli->inbuf,'\0', smb_size);
1007 cli_set_message(cli->outbuf,8,0,True);
1009 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1010 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1011 cli_setup_packet(cli);
1013 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1014 SSVAL(cli->outbuf,smb_vwv2,fnum);
1015 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1016 SIVALS(cli->outbuf, smb_vwv4, timeout);
1017 SSVAL(cli->outbuf,smb_vwv6,0);
1018 SSVAL(cli->outbuf,smb_vwv7,1);
1020 p = smb_buf(cli->outbuf);
1021 SSVAL(p, 0, cli->pid);
1022 SIVAL(p, 2, offset);
1023 SIVAL(p, 6, len);
1025 p += 10;
1027 cli_setup_bcc(cli, p);
1029 cli_send_smb(cli);
1031 if (timeout != 0) {
1032 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1035 if (!cli_receive_smb(cli)) {
1036 cli->timeout = saved_timeout;
1037 return False;
1040 cli->timeout = saved_timeout;
1042 if (cli_is_error(cli)) {
1043 return False;
1046 return True;
1049 /****************************************************************************
1050 Unlock a file.
1051 ****************************************************************************/
1053 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1055 char *p;
1057 memset(cli->outbuf,'\0',smb_size);
1058 memset(cli->inbuf,'\0',smb_size);
1060 cli_set_message(cli->outbuf,8,0,True);
1062 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1063 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1064 cli_setup_packet(cli);
1066 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1067 SSVAL(cli->outbuf,smb_vwv2,fnum);
1068 SCVAL(cli->outbuf,smb_vwv3,0);
1069 SIVALS(cli->outbuf, smb_vwv4, 0);
1070 SSVAL(cli->outbuf,smb_vwv6,1);
1071 SSVAL(cli->outbuf,smb_vwv7,0);
1073 p = smb_buf(cli->outbuf);
1074 SSVAL(p, 0, cli->pid);
1075 SIVAL(p, 2, offset);
1076 SIVAL(p, 6, len);
1077 p += 10;
1078 cli_setup_bcc(cli, p);
1079 cli_send_smb(cli);
1080 if (!cli_receive_smb(cli)) {
1081 return False;
1084 if (cli_is_error(cli)) {
1085 return False;
1088 return True;
1091 /****************************************************************************
1092 Lock a file with 64 bit offsets.
1093 ****************************************************************************/
1095 bool cli_lock64(struct cli_state *cli, int fnum,
1096 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1098 char *p;
1099 int saved_timeout = cli->timeout;
1100 int ltype;
1102 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1103 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1106 ltype = (lock_type == READ_LOCK? 1 : 0);
1107 ltype |= LOCKING_ANDX_LARGE_FILES;
1109 memset(cli->outbuf,'\0',smb_size);
1110 memset(cli->inbuf,'\0', smb_size);
1112 cli_set_message(cli->outbuf,8,0,True);
1114 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1115 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1116 cli_setup_packet(cli);
1118 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1119 SSVAL(cli->outbuf,smb_vwv2,fnum);
1120 SCVAL(cli->outbuf,smb_vwv3,ltype);
1121 SIVALS(cli->outbuf, smb_vwv4, timeout);
1122 SSVAL(cli->outbuf,smb_vwv6,0);
1123 SSVAL(cli->outbuf,smb_vwv7,1);
1125 p = smb_buf(cli->outbuf);
1126 SIVAL(p, 0, cli->pid);
1127 SOFF_T_R(p, 4, offset);
1128 SOFF_T_R(p, 12, len);
1129 p += 20;
1131 cli_setup_bcc(cli, p);
1132 cli_send_smb(cli);
1134 if (timeout != 0) {
1135 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1138 if (!cli_receive_smb(cli)) {
1139 cli->timeout = saved_timeout;
1140 return False;
1143 cli->timeout = saved_timeout;
1145 if (cli_is_error(cli)) {
1146 return False;
1149 return True;
1152 /****************************************************************************
1153 Unlock a file with 64 bit offsets.
1154 ****************************************************************************/
1156 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1158 char *p;
1160 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1161 return cli_unlock(cli, fnum, offset, len);
1164 memset(cli->outbuf,'\0',smb_size);
1165 memset(cli->inbuf,'\0',smb_size);
1167 cli_set_message(cli->outbuf,8,0,True);
1169 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1170 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1171 cli_setup_packet(cli);
1173 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1174 SSVAL(cli->outbuf,smb_vwv2,fnum);
1175 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1176 SIVALS(cli->outbuf, smb_vwv4, 0);
1177 SSVAL(cli->outbuf,smb_vwv6,1);
1178 SSVAL(cli->outbuf,smb_vwv7,0);
1180 p = smb_buf(cli->outbuf);
1181 SIVAL(p, 0, cli->pid);
1182 SOFF_T_R(p, 4, offset);
1183 SOFF_T_R(p, 12, len);
1184 p += 20;
1185 cli_setup_bcc(cli, p);
1186 cli_send_smb(cli);
1187 if (!cli_receive_smb(cli)) {
1188 return False;
1191 if (cli_is_error(cli)) {
1192 return False;
1195 return True;
1198 /****************************************************************************
1199 Get/unlock a POSIX lock on a file - internal function.
1200 ****************************************************************************/
1202 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1203 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1205 unsigned int param_len = 4;
1206 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1207 uint16 setup = TRANSACT2_SETFILEINFO;
1208 char param[4];
1209 unsigned char data[POSIX_LOCK_DATA_SIZE];
1210 char *rparam=NULL, *rdata=NULL;
1211 int saved_timeout = cli->timeout;
1213 SSVAL(param,0,fnum);
1214 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1216 switch (lock_type) {
1217 case READ_LOCK:
1218 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1219 break;
1220 case WRITE_LOCK:
1221 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1222 break;
1223 case UNLOCK_LOCK:
1224 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1225 break;
1226 default:
1227 return False;
1230 if (wait_lock) {
1231 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1232 cli->timeout = 0x7FFFFFFF;
1233 } else {
1234 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1237 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1238 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1239 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1241 if (!cli_send_trans(cli, SMBtrans2,
1242 NULL, /* name */
1243 -1, 0, /* fid, flags */
1244 &setup, 1, 0, /* setup, length, max */
1245 param, param_len, 2, /* param, length, max */
1246 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1247 )) {
1248 cli->timeout = saved_timeout;
1249 return False;
1252 if (!cli_receive_trans(cli, SMBtrans2,
1253 &rparam, &param_len,
1254 &rdata, &data_len)) {
1255 cli->timeout = saved_timeout;
1256 SAFE_FREE(rdata);
1257 SAFE_FREE(rparam);
1258 return False;
1261 cli->timeout = saved_timeout;
1263 SAFE_FREE(rdata);
1264 SAFE_FREE(rparam);
1266 return True;
1269 /****************************************************************************
1270 POSIX Lock a file.
1271 ****************************************************************************/
1273 bool cli_posix_lock(struct cli_state *cli, int fnum,
1274 SMB_BIG_UINT offset, SMB_BIG_UINT len,
1275 bool wait_lock, enum brl_type lock_type)
1277 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1278 return False;
1280 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1283 /****************************************************************************
1284 POSIX Unlock a file.
1285 ****************************************************************************/
1287 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1289 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1292 /****************************************************************************
1293 POSIX Get any lock covering a file.
1294 ****************************************************************************/
1296 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1298 return True;
1301 /****************************************************************************
1302 Do a SMBgetattrE call.
1303 ****************************************************************************/
1305 bool cli_getattrE(struct cli_state *cli, int fd,
1306 uint16 *attr, SMB_OFF_T *size,
1307 time_t *change_time,
1308 time_t *access_time,
1309 time_t *write_time)
1311 memset(cli->outbuf,'\0',smb_size);
1312 memset(cli->inbuf,'\0',smb_size);
1314 cli_set_message(cli->outbuf,1,0,True);
1316 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1317 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1318 cli_setup_packet(cli);
1320 SSVAL(cli->outbuf,smb_vwv0,fd);
1322 cli_send_smb(cli);
1323 if (!cli_receive_smb(cli)) {
1324 return False;
1327 if (cli_is_error(cli)) {
1328 return False;
1331 if (size) {
1332 *size = IVAL(cli->inbuf, smb_vwv6);
1335 if (attr) {
1336 *attr = SVAL(cli->inbuf,smb_vwv10);
1339 if (change_time) {
1340 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1343 if (access_time) {
1344 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1347 if (write_time) {
1348 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1351 return True;
1354 /****************************************************************************
1355 Do a SMBgetatr call
1356 ****************************************************************************/
1358 bool cli_getatr(struct cli_state *cli, const char *fname,
1359 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1361 char *p;
1363 memset(cli->outbuf,'\0',smb_size);
1364 memset(cli->inbuf,'\0',smb_size);
1366 cli_set_message(cli->outbuf,0,0,True);
1368 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1369 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1370 cli_setup_packet(cli);
1372 p = smb_buf(cli->outbuf);
1373 *p++ = 4;
1374 p += clistr_push(cli, p, fname,
1375 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1377 cli_setup_bcc(cli, p);
1379 cli_send_smb(cli);
1380 if (!cli_receive_smb(cli)) {
1381 return False;
1384 if (cli_is_error(cli)) {
1385 return False;
1388 if (size) {
1389 *size = IVAL(cli->inbuf, smb_vwv3);
1392 if (write_time) {
1393 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1396 if (attr) {
1397 *attr = SVAL(cli->inbuf,smb_vwv0);
1400 return True;
1403 /****************************************************************************
1404 Do a SMBsetattrE call.
1405 ****************************************************************************/
1407 bool cli_setattrE(struct cli_state *cli, int fd,
1408 time_t change_time,
1409 time_t access_time,
1410 time_t write_time)
1413 char *p;
1415 memset(cli->outbuf,'\0',smb_size);
1416 memset(cli->inbuf,'\0',smb_size);
1418 cli_set_message(cli->outbuf,7,0,True);
1420 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1421 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1422 cli_setup_packet(cli);
1424 SSVAL(cli->outbuf,smb_vwv0, fd);
1425 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1426 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1427 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1429 p = smb_buf(cli->outbuf);
1430 *p++ = 4;
1432 cli_setup_bcc(cli, p);
1434 cli_send_smb(cli);
1435 if (!cli_receive_smb(cli)) {
1436 return False;
1439 if (cli_is_error(cli)) {
1440 return False;
1443 return True;
1446 /****************************************************************************
1447 Do a SMBsetatr call.
1448 ****************************************************************************/
1450 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1452 char *p;
1454 memset(cli->outbuf,'\0',smb_size);
1455 memset(cli->inbuf,'\0',smb_size);
1457 cli_set_message(cli->outbuf,8,0,True);
1459 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1460 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1461 cli_setup_packet(cli);
1463 SSVAL(cli->outbuf,smb_vwv0, attr);
1464 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1466 p = smb_buf(cli->outbuf);
1467 *p++ = 4;
1468 p += clistr_push(cli, p, fname,
1469 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1470 *p++ = 4;
1472 cli_setup_bcc(cli, p);
1474 cli_send_smb(cli);
1475 if (!cli_receive_smb(cli)) {
1476 return False;
1479 if (cli_is_error(cli)) {
1480 return False;
1483 return True;
1486 /****************************************************************************
1487 Check for existance of a dir.
1488 ****************************************************************************/
1490 bool cli_chkpath(struct cli_state *cli, const char *path)
1492 char *path2 = NULL;
1493 char *p;
1494 TALLOC_CTX *frame = talloc_stackframe();
1496 path2 = talloc_strdup(frame, path);
1497 if (!path2) {
1498 TALLOC_FREE(frame);
1499 return false;
1501 trim_char(path2,'\0','\\');
1502 if (!*path2) {
1503 path2 = talloc_strdup(frame, "\\");
1504 if (!path2) {
1505 TALLOC_FREE(frame);
1506 return false;
1510 memset(cli->outbuf,'\0',smb_size);
1511 cli_set_message(cli->outbuf,0,0,True);
1512 SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1513 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1514 cli_setup_packet(cli);
1515 p = smb_buf(cli->outbuf);
1516 *p++ = 4;
1517 p += clistr_push(cli, p, path2,
1518 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1520 cli_setup_bcc(cli, p);
1522 cli_send_smb(cli);
1523 if (!cli_receive_smb(cli)) {
1524 TALLOC_FREE(frame);
1525 return False;
1528 TALLOC_FREE(frame);
1530 if (cli_is_error(cli)) return False;
1532 return True;
1535 /****************************************************************************
1536 Query disk space.
1537 ****************************************************************************/
1539 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1541 memset(cli->outbuf,'\0',smb_size);
1542 cli_set_message(cli->outbuf,0,0,True);
1543 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1544 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1545 cli_setup_packet(cli);
1547 cli_send_smb(cli);
1548 if (!cli_receive_smb(cli)) {
1549 return False;
1552 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1553 *total = SVAL(cli->inbuf,smb_vwv0);
1554 *avail = SVAL(cli->inbuf,smb_vwv3);
1556 return True;
1559 /****************************************************************************
1560 Create and open a temporary file.
1561 ****************************************************************************/
1563 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1565 int len;
1566 char *p;
1568 memset(cli->outbuf,'\0',smb_size);
1569 memset(cli->inbuf,'\0',smb_size);
1571 cli_set_message(cli->outbuf,3,0,True);
1573 SCVAL(cli->outbuf,smb_com,SMBctemp);
1574 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1575 cli_setup_packet(cli);
1577 SSVAL(cli->outbuf,smb_vwv0,0);
1578 SIVALS(cli->outbuf,smb_vwv1,-1);
1580 p = smb_buf(cli->outbuf);
1581 *p++ = 4;
1582 p += clistr_push(cli, p, path,
1583 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1585 cli_setup_bcc(cli, p);
1587 cli_send_smb(cli);
1588 if (!cli_receive_smb(cli)) {
1589 return -1;
1592 if (cli_is_error(cli)) {
1593 return -1;
1596 /* despite the spec, the result has a -1, followed by
1597 length, followed by name */
1598 p = smb_buf(cli->inbuf);
1599 p += 4;
1600 len = smb_buflen(cli->inbuf) - 4;
1601 if (len <= 0 || len > PATH_MAX) return -1;
1603 if (tmp_path) {
1604 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1605 if (!path2) {
1606 return -1;
1608 clistr_pull(cli, path2, p,
1609 len+1, len, STR_ASCII);
1610 *tmp_path = path2;
1613 return SVAL(cli->inbuf,smb_vwv0);
1617 send a raw ioctl - used by the torture code
1619 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1621 memset(cli->outbuf,'\0',smb_size);
1622 memset(cli->inbuf,'\0',smb_size);
1624 cli_set_message(cli->outbuf, 3, 0, True);
1625 SCVAL(cli->outbuf,smb_com,SMBioctl);
1626 cli_setup_packet(cli);
1628 SSVAL(cli->outbuf, smb_vwv0, fnum);
1629 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1630 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1632 cli_send_smb(cli);
1633 if (!cli_receive_smb(cli)) {
1634 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1637 if (cli_is_error(cli)) {
1638 return cli_nt_error(cli);
1641 *blob = data_blob_null;
1643 return NT_STATUS_OK;
1646 /*********************************************************
1647 Set an extended attribute utility fn.
1648 *********************************************************/
1650 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1651 const char *ea_name, const char *ea_val, size_t ea_len)
1653 unsigned int data_len = 0;
1654 char *data = NULL;
1655 char *rparam=NULL, *rdata=NULL;
1656 char *p;
1657 size_t ea_namelen = strlen(ea_name);
1659 if (ea_namelen == 0 && ea_len == 0) {
1660 data_len = 4;
1661 data = (char *)SMB_MALLOC(data_len);
1662 if (!data) {
1663 return False;
1665 p = data;
1666 SIVAL(p,0,data_len);
1667 } else {
1668 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1669 data = (char *)SMB_MALLOC(data_len);
1670 if (!data) {
1671 return False;
1673 p = data;
1674 SIVAL(p,0,data_len);
1675 p += 4;
1676 SCVAL(p, 0, 0); /* EA flags. */
1677 SCVAL(p, 1, ea_namelen);
1678 SSVAL(p, 2, ea_len);
1679 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1680 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1683 if (!cli_send_trans(cli, SMBtrans2,
1684 NULL, /* name */
1685 -1, 0, /* fid, flags */
1686 &setup, 1, 0, /* setup, length, max */
1687 param, param_len, 2, /* param, length, max */
1688 data, data_len, cli->max_xmit /* data, length, max */
1689 )) {
1690 SAFE_FREE(data);
1691 return False;
1694 if (!cli_receive_trans(cli, SMBtrans2,
1695 &rparam, &param_len,
1696 &rdata, &data_len)) {
1697 SAFE_FREE(data);
1698 return false;
1701 SAFE_FREE(data);
1702 SAFE_FREE(rdata);
1703 SAFE_FREE(rparam);
1705 return True;
1708 /*********************************************************
1709 Set an extended attribute on a pathname.
1710 *********************************************************/
1712 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1714 uint16 setup = TRANSACT2_SETPATHINFO;
1715 unsigned int param_len = 0;
1716 char *param;
1717 size_t srclen = 2*(strlen(path)+1);
1718 char *p;
1719 bool ret;
1721 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1722 if (!param) {
1723 return false;
1725 memset(param, '\0', 6);
1726 SSVAL(param,0,SMB_INFO_SET_EA);
1727 p = &param[6];
1729 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1730 param_len = PTR_DIFF(p, param);
1732 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1733 SAFE_FREE(param);
1734 return ret;
1737 /*********************************************************
1738 Set an extended attribute on an fnum.
1739 *********************************************************/
1741 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1743 char param[6];
1744 uint16 setup = TRANSACT2_SETFILEINFO;
1746 memset(param, 0, 6);
1747 SSVAL(param,0,fnum);
1748 SSVAL(param,2,SMB_INFO_SET_EA);
1750 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1753 /*********************************************************
1754 Get an extended attribute list tility fn.
1755 *********************************************************/
1757 static bool cli_get_ea_list(struct cli_state *cli,
1758 uint16 setup, char *param, unsigned int param_len,
1759 TALLOC_CTX *ctx,
1760 size_t *pnum_eas,
1761 struct ea_struct **pea_list)
1763 unsigned int data_len = 0;
1764 unsigned int rparam_len, rdata_len;
1765 char *rparam=NULL, *rdata=NULL;
1766 char *p;
1767 size_t ea_size;
1768 size_t num_eas;
1769 bool ret = False;
1770 struct ea_struct *ea_list;
1772 *pnum_eas = 0;
1773 if (pea_list) {
1774 *pea_list = NULL;
1777 if (!cli_send_trans(cli, SMBtrans2,
1778 NULL, /* Name */
1779 -1, 0, /* fid, flags */
1780 &setup, 1, 0, /* setup, length, max */
1781 param, param_len, 10, /* param, length, max */
1782 NULL, data_len, cli->max_xmit /* data, length, max */
1783 )) {
1784 return False;
1787 if (!cli_receive_trans(cli, SMBtrans2,
1788 &rparam, &rparam_len,
1789 &rdata, &rdata_len)) {
1790 return False;
1793 if (!rdata || rdata_len < 4) {
1794 goto out;
1797 ea_size = (size_t)IVAL(rdata,0);
1798 if (ea_size > rdata_len) {
1799 goto out;
1802 if (ea_size == 0) {
1803 /* No EA's present. */
1804 ret = True;
1805 goto out;
1808 p = rdata + 4;
1809 ea_size -= 4;
1811 /* Validate the EA list and count it. */
1812 for (num_eas = 0; ea_size >= 4; num_eas++) {
1813 unsigned int ea_namelen = CVAL(p,1);
1814 unsigned int ea_valuelen = SVAL(p,2);
1815 if (ea_namelen == 0) {
1816 goto out;
1818 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1819 goto out;
1821 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1822 p += 4 + ea_namelen + 1 + ea_valuelen;
1825 if (num_eas == 0) {
1826 ret = True;
1827 goto out;
1830 *pnum_eas = num_eas;
1831 if (!pea_list) {
1832 /* Caller only wants number of EA's. */
1833 ret = True;
1834 goto out;
1837 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1838 if (!ea_list) {
1839 goto out;
1842 ea_size = (size_t)IVAL(rdata,0);
1843 p = rdata + 4;
1845 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1846 struct ea_struct *ea = &ea_list[num_eas];
1847 fstring unix_ea_name;
1848 unsigned int ea_namelen = CVAL(p,1);
1849 unsigned int ea_valuelen = SVAL(p,2);
1851 ea->flags = CVAL(p,0);
1852 unix_ea_name[0] = '\0';
1853 pull_ascii_fstring(unix_ea_name, p + 4);
1854 ea->name = talloc_strdup(ctx, unix_ea_name);
1855 /* Ensure the value is null terminated (in case it's a string). */
1856 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1857 if (!ea->value.data) {
1858 goto out;
1860 if (ea_valuelen) {
1861 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1863 ea->value.data[ea_valuelen] = 0;
1864 ea->value.length--;
1865 p += 4 + ea_namelen + 1 + ea_valuelen;
1868 *pea_list = ea_list;
1869 ret = True;
1871 out :
1873 SAFE_FREE(rdata);
1874 SAFE_FREE(rparam);
1875 return ret;
1878 /*********************************************************
1879 Get an extended attribute list from a pathname.
1880 *********************************************************/
1882 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
1883 TALLOC_CTX *ctx,
1884 size_t *pnum_eas,
1885 struct ea_struct **pea_list)
1887 uint16 setup = TRANSACT2_QPATHINFO;
1888 unsigned int param_len = 0;
1889 char *param;
1890 char *p;
1891 size_t srclen = 2*(strlen(path)+1);
1892 bool ret;
1894 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1895 if (!param) {
1896 return false;
1898 p = param;
1899 memset(p, 0, 6);
1900 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1901 p += 6;
1902 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1903 param_len = PTR_DIFF(p, param);
1905 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1906 SAFE_FREE(param);
1907 return ret;
1910 /*********************************************************
1911 Get an extended attribute list from an fnum.
1912 *********************************************************/
1914 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1915 TALLOC_CTX *ctx,
1916 size_t *pnum_eas,
1917 struct ea_struct **pea_list)
1919 uint16 setup = TRANSACT2_QFILEINFO;
1920 char param[6];
1922 memset(param, 0, 6);
1923 SSVAL(param,0,fnum);
1924 SSVAL(param,2,SMB_INFO_SET_EA);
1926 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1929 /****************************************************************************
1930 Convert open "flags" arg to uint32 on wire.
1931 ****************************************************************************/
1933 static uint32 open_flags_to_wire(int flags)
1935 int open_mode = flags & O_ACCMODE;
1936 uint32 ret = 0;
1938 switch (open_mode) {
1939 case O_WRONLY:
1940 ret |= SMB_O_WRONLY;
1941 break;
1942 case O_RDWR:
1943 ret |= SMB_O_RDWR;
1944 break;
1945 default:
1946 case O_RDONLY:
1947 ret |= SMB_O_RDONLY;
1948 break;
1951 if (flags & O_CREAT) {
1952 ret |= SMB_O_CREAT;
1954 if (flags & O_EXCL) {
1955 ret |= SMB_O_EXCL;
1957 if (flags & O_TRUNC) {
1958 ret |= SMB_O_TRUNC;
1960 #if defined(O_SYNC)
1961 if (flags & O_SYNC) {
1962 ret |= SMB_O_SYNC;
1964 #endif /* O_SYNC */
1965 if (flags & O_APPEND) {
1966 ret |= SMB_O_APPEND;
1968 #if defined(O_DIRECT)
1969 if (flags & O_DIRECT) {
1970 ret |= SMB_O_DIRECT;
1972 #endif
1973 #if defined(O_DIRECTORY)
1974 if (flags & O_DIRECTORY) {
1975 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1976 ret |= SMB_O_DIRECTORY;
1978 #endif
1979 return ret;
1982 /****************************************************************************
1983 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
1984 ****************************************************************************/
1986 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
1988 unsigned int data_len = 0;
1989 unsigned int param_len = 0;
1990 uint16 setup = TRANSACT2_SETPATHINFO;
1991 char *param;
1992 char data[18];
1993 char *rparam=NULL, *rdata=NULL;
1994 char *p;
1995 int fnum = -1;
1996 uint32 wire_flags = open_flags_to_wire(flags);
1997 size_t srclen = 2*(strlen(fname)+1);
1999 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2000 if (!param) {
2001 return false;
2003 memset(param, '\0', 6);
2004 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2005 p = &param[6];
2007 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2008 param_len = PTR_DIFF(p, param);
2010 if (is_dir) {
2011 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2012 wire_flags |= SMB_O_DIRECTORY;
2015 p = data;
2016 SIVAL(p,0,0); /* No oplock. */
2017 SIVAL(p,4,wire_flags);
2018 SIVAL(p,8,unix_perms_to_wire(mode));
2019 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2020 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2022 data_len = 18;
2024 if (!cli_send_trans(cli, SMBtrans2,
2025 NULL, /* name */
2026 -1, 0, /* fid, flags */
2027 &setup, 1, 0, /* setup, length, max */
2028 param, param_len, 2, /* param, length, max */
2029 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2030 )) {
2031 SAFE_FREE(param);
2032 return -1;
2035 SAFE_FREE(param);
2037 if (!cli_receive_trans(cli, SMBtrans2,
2038 &rparam, &param_len,
2039 &rdata, &data_len)) {
2040 return -1;
2043 fnum = SVAL(rdata,2);
2045 SAFE_FREE(rdata);
2046 SAFE_FREE(rparam);
2048 return fnum;
2051 /****************************************************************************
2052 open - POSIX semantics.
2053 ****************************************************************************/
2055 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2057 return cli_posix_open_internal(cli, fname, flags, mode, False);
2060 /****************************************************************************
2061 mkdir - POSIX semantics.
2062 ****************************************************************************/
2064 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2066 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2069 /****************************************************************************
2070 unlink or rmdir - POSIX semantics.
2071 ****************************************************************************/
2073 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2075 unsigned int data_len = 0;
2076 unsigned int param_len = 0;
2077 uint16 setup = TRANSACT2_SETPATHINFO;
2078 char *param;
2079 char data[2];
2080 char *rparam=NULL, *rdata=NULL;
2081 char *p;
2082 size_t srclen = 2*(strlen(fname)+1);
2084 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2085 if (!param) {
2086 return false;
2088 memset(param, '\0', 6);
2089 SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2090 p = &param[6];
2092 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2093 param_len = PTR_DIFF(p, param);
2095 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2096 SMB_POSIX_UNLINK_FILE_TARGET);
2097 data_len = 2;
2099 if (!cli_send_trans(cli, SMBtrans2,
2100 NULL, /* name */
2101 -1, 0, /* fid, flags */
2102 &setup, 1, 0, /* setup, length, max */
2103 param, param_len, 2, /* param, length, max */
2104 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2105 )) {
2106 SAFE_FREE(param);
2107 return False;
2110 SAFE_FREE(param);
2112 if (!cli_receive_trans(cli, SMBtrans2,
2113 &rparam, &param_len,
2114 &rdata, &data_len)) {
2115 return False;
2118 SAFE_FREE(rdata);
2119 SAFE_FREE(rparam);
2121 return True;
2124 /****************************************************************************
2125 unlink - POSIX semantics.
2126 ****************************************************************************/
2128 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2130 return cli_posix_unlink_internal(cli, fname, False);
2133 /****************************************************************************
2134 rmdir - POSIX semantics.
2135 ****************************************************************************/
2137 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2139 return cli_posix_unlink_internal(cli, fname, True);