r17912: * mssed DNS SRV fix
[Samba.git] / source / libsmb / clifile.c
blobea6501dac89e8652c175a629b40a7b4cf4b2545e
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 /****************************************************************************
25 Hard/Symlink a file (UNIX extensions).
26 Creates new name (sym)linked to oldname.
27 ****************************************************************************/
29 static BOOL cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, BOOL hard_link)
31 unsigned int data_len = 0;
32 unsigned int param_len = 0;
33 uint16 setup = TRANSACT2_SETPATHINFO;
34 char param[sizeof(pstring)+6];
35 pstring data;
36 char *rparam=NULL, *rdata=NULL;
37 char *p;
38 size_t oldlen = 2*(strlen(oldname)+1);
39 size_t newlen = 2*(strlen(newname)+1);
41 memset(param, 0, sizeof(param));
42 SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
43 p = &param[6];
45 p += clistr_push(cli, p, newname, MIN(newlen, sizeof(param)-6), STR_TERMINATE);
46 param_len = PTR_DIFF(p, param);
48 p = data;
49 p += clistr_push(cli, p, oldname, MIN(oldlen,sizeof(data)), STR_TERMINATE);
50 data_len = PTR_DIFF(p, data);
52 if (!cli_send_trans(cli, SMBtrans2,
53 NULL, /* name */
54 -1, 0, /* fid, flags */
55 &setup, 1, 0, /* setup, length, max */
56 param, param_len, 2, /* param, length, max */
57 (char *)&data, data_len, cli->max_xmit /* data, length, max */
58 )) {
59 return False;
62 if (!cli_receive_trans(cli, SMBtrans2,
63 &rparam, &param_len,
64 &rdata, &data_len)) {
65 return False;
68 SAFE_FREE(rdata);
69 SAFE_FREE(rparam);
71 return True;
74 /****************************************************************************
75 Map standard UNIX permissions onto wire representations.
76 ****************************************************************************/
78 uint32 unix_perms_to_wire(mode_t perms)
80 unsigned int ret = 0;
82 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
83 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
84 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
85 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
86 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
87 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
88 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
89 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
90 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
91 #ifdef S_ISVTX
92 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
93 #endif
94 #ifdef S_ISGID
95 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
96 #endif
97 #ifdef S_ISUID
98 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
99 #endif
100 return ret;
103 /****************************************************************************
104 Map wire permissions to standard UNIX.
105 ****************************************************************************/
107 mode_t wire_perms_to_unix(uint32 perms)
109 mode_t ret = (mode_t)0;
111 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
112 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
113 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
114 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
115 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
116 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
117 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
118 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
119 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
120 #ifdef S_ISVTX
121 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
122 #endif
123 #ifdef S_ISGID
124 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
125 #endif
126 #ifdef S_ISUID
127 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
128 #endif
129 return ret;
132 /****************************************************************************
133 Return the file type from the wire filetype for UNIX extensions.
134 ****************************************************************************/
136 static mode_t unix_filetype_from_wire(uint32 wire_type)
138 switch (wire_type) {
139 case UNIX_TYPE_FILE:
140 return S_IFREG;
141 case UNIX_TYPE_DIR:
142 return S_IFDIR;
143 #ifdef S_IFLNK
144 case UNIX_TYPE_SYMLINK:
145 return S_IFLNK;
146 #endif
147 #ifdef S_IFCHR
148 case UNIX_TYPE_CHARDEV:
149 return S_IFCHR;
150 #endif
151 #ifdef S_IFBLK
152 case UNIX_TYPE_BLKDEV:
153 return S_IFBLK;
154 #endif
155 #ifdef S_IFIFO
156 case UNIX_TYPE_FIFO:
157 return S_IFIFO;
158 #endif
159 #ifdef S_IFSOCK
160 case UNIX_TYPE_SOCKET:
161 return S_IFSOCK;
162 #endif
163 default:
164 return (mode_t)0;
168 /****************************************************************************
169 Do a POSIX getfacl (UNIX extensions).
170 ****************************************************************************/
172 BOOL cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
174 unsigned int param_len = 0;
175 unsigned int data_len = 0;
176 uint16 setup = TRANSACT2_QPATHINFO;
177 char param[sizeof(pstring)+6];
178 char *rparam=NULL, *rdata=NULL;
179 char *p;
181 p = param;
182 memset(p, 0, 6);
183 SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
184 p += 6;
185 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
186 param_len = PTR_DIFF(p, param);
188 if (!cli_send_trans(cli, SMBtrans2,
189 NULL, /* name */
190 -1, 0, /* fid, flags */
191 &setup, 1, 0, /* setup, length, max */
192 param, param_len, 2, /* param, length, max */
193 NULL, 0, cli->max_xmit /* data, length, max */
194 )) {
195 return False;
198 if (!cli_receive_trans(cli, SMBtrans2,
199 &rparam, &param_len,
200 &rdata, &data_len)) {
201 return False;
204 if (data_len < 6) {
205 SAFE_FREE(rdata);
206 SAFE_FREE(rparam);
207 return False;
210 SAFE_FREE(rparam);
211 *retbuf = rdata;
212 *prb_size = (size_t)data_len;
214 return True;
217 /****************************************************************************
218 Stat a file (UNIX extensions).
219 ****************************************************************************/
221 BOOL cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
223 unsigned int param_len = 0;
224 unsigned int data_len = 0;
225 uint16 setup = TRANSACT2_QPATHINFO;
226 char param[sizeof(pstring)+6];
227 char *rparam=NULL, *rdata=NULL;
228 char *p;
230 ZERO_STRUCTP(sbuf);
232 p = param;
233 memset(p, 0, 6);
234 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
235 p += 6;
236 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
237 param_len = PTR_DIFF(p, param);
239 if (!cli_send_trans(cli, SMBtrans2,
240 NULL, /* name */
241 -1, 0, /* fid, flags */
242 &setup, 1, 0, /* setup, length, max */
243 param, param_len, 2, /* param, length, max */
244 NULL, 0, cli->max_xmit /* data, length, max */
245 )) {
246 return False;
249 if (!cli_receive_trans(cli, SMBtrans2,
250 &rparam, &param_len,
251 &rdata, &data_len)) {
252 return False;
255 if (data_len < 96) {
256 SAFE_FREE(rdata);
257 SAFE_FREE(rparam);
258 return False;
261 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
262 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
263 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
264 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
265 #else
266 /* assume 512 byte blocks */
267 sbuf->st_blocks /= 512;
268 #endif
269 sbuf->st_ctime = interpret_long_date(rdata + 16); /* time of last change */
270 sbuf->st_atime = interpret_long_date(rdata + 24); /* time of last access */
271 sbuf->st_mtime = interpret_long_date(rdata + 32); /* time of last modification */
272 sbuf->st_uid = (uid_t) IVAL(rdata,40); /* user ID of owner */
273 sbuf->st_gid = (gid_t) IVAL(rdata,48); /* group ID of owner */
274 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
275 #if defined(HAVE_MAKEDEV)
277 uint32 dev_major = IVAL(rdata,60);
278 uint32 dev_minor = IVAL(rdata,68);
279 sbuf->st_rdev = makedev(dev_major, dev_minor);
281 #endif
282 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
283 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
284 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
286 SAFE_FREE(rdata);
287 SAFE_FREE(rparam);
289 return True;
292 /****************************************************************************
293 Symlink a file (UNIX extensions).
294 ****************************************************************************/
296 BOOL cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
298 return cli_link_internal(cli, oldname, newname, False);
301 /****************************************************************************
302 Hard a file (UNIX extensions).
303 ****************************************************************************/
305 BOOL cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
307 return cli_link_internal(cli, oldname, newname, True);
310 /****************************************************************************
311 Chmod or chown a file internal (UNIX extensions).
312 ****************************************************************************/
314 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
316 unsigned int data_len = 0;
317 unsigned int param_len = 0;
318 uint16 setup = TRANSACT2_SETPATHINFO;
319 char param[sizeof(pstring)+6];
320 char data[100];
321 char *rparam=NULL, *rdata=NULL;
322 char *p;
324 memset(param, 0, sizeof(param));
325 memset(data, 0, sizeof(data));
326 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
327 p = &param[6];
329 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
330 param_len = PTR_DIFF(p, param);
332 SIVAL(data,40,uid);
333 SIVAL(data,48,gid);
334 SIVAL(data,84,mode);
336 data_len = 100;
338 if (!cli_send_trans(cli, SMBtrans2,
339 NULL, /* name */
340 -1, 0, /* fid, flags */
341 &setup, 1, 0, /* setup, length, max */
342 param, param_len, 2, /* param, length, max */
343 (char *)&data, data_len, cli->max_xmit /* data, length, max */
344 )) {
345 return False;
348 if (!cli_receive_trans(cli, SMBtrans2,
349 &rparam, &param_len,
350 &rdata, &data_len)) {
351 return False;
354 SAFE_FREE(rdata);
355 SAFE_FREE(rparam);
357 return True;
360 /****************************************************************************
361 chmod a file (UNIX extensions).
362 ****************************************************************************/
364 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
366 return cli_unix_chmod_chown_internal(cli, fname,
367 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
370 /****************************************************************************
371 chown a file (UNIX extensions).
372 ****************************************************************************/
374 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
376 return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
379 /****************************************************************************
380 Rename a file.
381 ****************************************************************************/
383 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
385 char *p;
387 memset(cli->outbuf,'\0',smb_size);
388 memset(cli->inbuf,'\0',smb_size);
390 set_message(cli->outbuf,1, 0, True);
392 SCVAL(cli->outbuf,smb_com,SMBmv);
393 SSVAL(cli->outbuf,smb_tid,cli->cnum);
394 cli_setup_packet(cli);
396 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
398 p = smb_buf(cli->outbuf);
399 *p++ = 4;
400 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
401 *p++ = 4;
402 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
404 cli_setup_bcc(cli, p);
406 cli_send_smb(cli);
407 if (!cli_receive_smb(cli))
408 return False;
410 if (cli_is_error(cli))
411 return False;
413 return True;
416 /****************************************************************************
417 NT Rename a file.
418 ****************************************************************************/
420 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
422 char *p;
424 memset(cli->outbuf,'\0',smb_size);
425 memset(cli->inbuf,'\0',smb_size);
427 set_message(cli->outbuf, 4, 0, True);
429 SCVAL(cli->outbuf,smb_com,SMBntrename);
430 SSVAL(cli->outbuf,smb_tid,cli->cnum);
431 cli_setup_packet(cli);
433 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
434 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
436 p = smb_buf(cli->outbuf);
437 *p++ = 4;
438 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
439 *p++ = 4;
440 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
442 cli_setup_bcc(cli, p);
444 cli_send_smb(cli);
445 if (!cli_receive_smb(cli))
446 return False;
448 if (cli_is_error(cli))
449 return False;
451 return True;
454 /****************************************************************************
455 NT hardlink a file.
456 ****************************************************************************/
458 BOOL cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
460 char *p;
462 memset(cli->outbuf,'\0',smb_size);
463 memset(cli->inbuf,'\0',smb_size);
465 set_message(cli->outbuf, 4, 0, True);
467 SCVAL(cli->outbuf,smb_com,SMBntrename);
468 SSVAL(cli->outbuf,smb_tid,cli->cnum);
469 cli_setup_packet(cli);
471 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
472 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
474 p = smb_buf(cli->outbuf);
475 *p++ = 4;
476 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
477 *p++ = 4;
478 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
480 cli_setup_bcc(cli, p);
482 cli_send_smb(cli);
483 if (!cli_receive_smb(cli))
484 return False;
486 if (cli_is_error(cli))
487 return False;
489 return True;
492 /****************************************************************************
493 Delete a file.
494 ****************************************************************************/
496 BOOL cli_unlink(struct cli_state *cli, const char *fname)
498 char *p;
500 memset(cli->outbuf,'\0',smb_size);
501 memset(cli->inbuf,'\0',smb_size);
503 set_message(cli->outbuf,1, 0,True);
505 SCVAL(cli->outbuf,smb_com,SMBunlink);
506 SSVAL(cli->outbuf,smb_tid,cli->cnum);
507 cli_setup_packet(cli);
509 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
511 p = smb_buf(cli->outbuf);
512 *p++ = 4;
513 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
515 cli_setup_bcc(cli, p);
516 cli_send_smb(cli);
517 if (!cli_receive_smb(cli)) {
518 return False;
521 if (cli_is_error(cli)) {
522 return False;
525 return True;
528 /****************************************************************************
529 Create a directory.
530 ****************************************************************************/
532 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
534 char *p;
536 memset(cli->outbuf,'\0',smb_size);
537 memset(cli->inbuf,'\0',smb_size);
539 set_message(cli->outbuf,0, 0,True);
541 SCVAL(cli->outbuf,smb_com,SMBmkdir);
542 SSVAL(cli->outbuf,smb_tid,cli->cnum);
543 cli_setup_packet(cli);
545 p = smb_buf(cli->outbuf);
546 *p++ = 4;
547 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
549 cli_setup_bcc(cli, p);
551 cli_send_smb(cli);
552 if (!cli_receive_smb(cli)) {
553 return False;
556 if (cli_is_error(cli)) {
557 return False;
560 return True;
563 /****************************************************************************
564 Remove a directory.
565 ****************************************************************************/
567 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
569 char *p;
571 memset(cli->outbuf,'\0',smb_size);
572 memset(cli->inbuf,'\0',smb_size);
574 set_message(cli->outbuf,0, 0, True);
576 SCVAL(cli->outbuf,smb_com,SMBrmdir);
577 SSVAL(cli->outbuf,smb_tid,cli->cnum);
578 cli_setup_packet(cli);
580 p = smb_buf(cli->outbuf);
581 *p++ = 4;
582 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
584 cli_setup_bcc(cli, p);
586 cli_send_smb(cli);
587 if (!cli_receive_smb(cli)) {
588 return False;
591 if (cli_is_error(cli)) {
592 return False;
595 return True;
598 /****************************************************************************
599 Set or clear the delete on close flag.
600 ****************************************************************************/
602 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
604 unsigned int data_len = 1;
605 unsigned int param_len = 6;
606 uint16 setup = TRANSACT2_SETFILEINFO;
607 pstring param;
608 unsigned char data;
609 char *rparam=NULL, *rdata=NULL;
611 memset(param, 0, param_len);
612 SSVAL(param,0,fnum);
613 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
615 data = flag ? 1 : 0;
617 if (!cli_send_trans(cli, SMBtrans2,
618 NULL, /* name */
619 -1, 0, /* fid, flags */
620 &setup, 1, 0, /* setup, length, max */
621 param, param_len, 2, /* param, length, max */
622 (char *)&data, data_len, cli->max_xmit /* data, length, max */
623 )) {
624 return False;
627 if (!cli_receive_trans(cli, SMBtrans2,
628 &rparam, &param_len,
629 &rdata, &data_len)) {
630 return False;
633 SAFE_FREE(rdata);
634 SAFE_FREE(rparam);
636 return True;
639 /****************************************************************************
640 Open a file - exposing the full horror of the NT API :-).
641 Used in smbtorture.
642 ****************************************************************************/
644 int cli_nt_create_full(struct cli_state *cli, const char *fname,
645 uint32 CreatFlags, uint32 DesiredAccess,
646 uint32 FileAttributes, uint32 ShareAccess,
647 uint32 CreateDisposition, uint32 CreateOptions,
648 uint8 SecuityFlags)
650 char *p;
651 int len;
653 memset(cli->outbuf,'\0',smb_size);
654 memset(cli->inbuf,'\0',smb_size);
656 set_message(cli->outbuf,24,0,True);
658 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
659 SSVAL(cli->outbuf,smb_tid,cli->cnum);
660 cli_setup_packet(cli);
662 SSVAL(cli->outbuf,smb_vwv0,0xFF);
663 if (cli->use_oplocks)
664 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
666 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
667 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
668 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
669 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
670 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
671 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
672 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
673 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
674 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
676 p = smb_buf(cli->outbuf);
677 /* this alignment and termination is critical for netapp filers. Don't change */
678 p += clistr_align_out(cli, p, 0);
679 len = clistr_push(cli, p, fname, -1, 0);
680 p += len;
681 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
682 /* sigh. this copes with broken netapp filer behaviour */
683 p += clistr_push(cli, p, "", -1, STR_TERMINATE);
685 cli_setup_bcc(cli, p);
687 cli_send_smb(cli);
688 if (!cli_receive_smb(cli)) {
689 return -1;
692 if (cli_is_error(cli)) {
693 return -1;
696 return SVAL(cli->inbuf,smb_vwv2 + 1);
699 /****************************************************************************
700 Open a file.
701 ****************************************************************************/
703 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
705 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
706 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
709 /****************************************************************************
710 Open a file
711 WARNING: if you open with O_WRONLY then getattrE won't work!
712 ****************************************************************************/
714 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
716 char *p;
717 unsigned openfn=0;
718 unsigned accessmode=0;
720 if (flags & O_CREAT)
721 openfn |= (1<<4);
722 if (!(flags & O_EXCL)) {
723 if (flags & O_TRUNC)
724 openfn |= (1<<1);
725 else
726 openfn |= (1<<0);
729 accessmode = (share_mode<<4);
731 if ((flags & O_ACCMODE) == O_RDWR) {
732 accessmode |= 2;
733 } else if ((flags & O_ACCMODE) == O_WRONLY) {
734 accessmode |= 1;
737 #if defined(O_SYNC)
738 if ((flags & O_SYNC) == O_SYNC) {
739 accessmode |= (1<<14);
741 #endif /* O_SYNC */
743 if (share_mode == DENY_FCB) {
744 accessmode = 0xFF;
747 memset(cli->outbuf,'\0',smb_size);
748 memset(cli->inbuf,'\0',smb_size);
750 set_message(cli->outbuf,15,0,True);
752 SCVAL(cli->outbuf,smb_com,SMBopenX);
753 SSVAL(cli->outbuf,smb_tid,cli->cnum);
754 cli_setup_packet(cli);
756 SSVAL(cli->outbuf,smb_vwv0,0xFF);
757 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
758 SSVAL(cli->outbuf,smb_vwv3,accessmode);
759 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
760 SSVAL(cli->outbuf,smb_vwv5,0);
761 SSVAL(cli->outbuf,smb_vwv8,openfn);
763 if (cli->use_oplocks) {
764 /* if using oplocks then ask for a batch oplock via
765 core and extended methods */
766 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
767 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
768 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
771 p = smb_buf(cli->outbuf);
772 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
774 cli_setup_bcc(cli, p);
776 cli_send_smb(cli);
777 if (!cli_receive_smb(cli)) {
778 return -1;
781 if (cli_is_error(cli)) {
782 return -1;
785 return SVAL(cli->inbuf,smb_vwv2);
788 /****************************************************************************
789 Close a file.
790 ****************************************************************************/
792 BOOL cli_close(struct cli_state *cli, int fnum)
794 memset(cli->outbuf,'\0',smb_size);
795 memset(cli->inbuf,'\0',smb_size);
797 set_message(cli->outbuf,3,0,True);
799 SCVAL(cli->outbuf,smb_com,SMBclose);
800 SSVAL(cli->outbuf,smb_tid,cli->cnum);
801 cli_setup_packet(cli);
803 SSVAL(cli->outbuf,smb_vwv0,fnum);
804 SIVALS(cli->outbuf,smb_vwv1,-1);
806 cli_send_smb(cli);
807 if (!cli_receive_smb(cli)) {
808 return False;
811 return !cli_is_error(cli);
815 /****************************************************************************
816 send a lock with a specified locktype
817 this is used for testing LOCKING_ANDX_CANCEL_LOCK
818 ****************************************************************************/
820 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
821 uint32 offset, uint32 len, int timeout, unsigned char locktype)
823 char *p;
824 int saved_timeout = cli->timeout;
826 memset(cli->outbuf,'\0',smb_size);
827 memset(cli->inbuf,'\0', smb_size);
829 set_message(cli->outbuf,8,0,True);
831 SCVAL(cli->outbuf,smb_com,SMBlockingX);
832 SSVAL(cli->outbuf,smb_tid,cli->cnum);
833 cli_setup_packet(cli);
835 SCVAL(cli->outbuf,smb_vwv0,0xFF);
836 SSVAL(cli->outbuf,smb_vwv2,fnum);
837 SCVAL(cli->outbuf,smb_vwv3,locktype);
838 SIVALS(cli->outbuf, smb_vwv4, timeout);
839 SSVAL(cli->outbuf,smb_vwv6,0);
840 SSVAL(cli->outbuf,smb_vwv7,1);
842 p = smb_buf(cli->outbuf);
843 SSVAL(p, 0, cli->pid);
844 SIVAL(p, 2, offset);
845 SIVAL(p, 6, len);
847 p += 10;
849 cli_setup_bcc(cli, p);
851 cli_send_smb(cli);
853 if (timeout != 0) {
854 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
857 if (!cli_receive_smb(cli)) {
858 cli->timeout = saved_timeout;
859 return NT_STATUS_UNSUCCESSFUL;
862 cli->timeout = saved_timeout;
864 return cli_nt_error(cli);
867 /****************************************************************************
868 Lock a file.
869 note that timeout is in units of 2 milliseconds
870 ****************************************************************************/
872 BOOL cli_lock(struct cli_state *cli, int fnum,
873 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
875 char *p;
876 int saved_timeout = cli->timeout;
878 memset(cli->outbuf,'\0',smb_size);
879 memset(cli->inbuf,'\0', smb_size);
881 set_message(cli->outbuf,8,0,True);
883 SCVAL(cli->outbuf,smb_com,SMBlockingX);
884 SSVAL(cli->outbuf,smb_tid,cli->cnum);
885 cli_setup_packet(cli);
887 SCVAL(cli->outbuf,smb_vwv0,0xFF);
888 SSVAL(cli->outbuf,smb_vwv2,fnum);
889 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
890 SIVALS(cli->outbuf, smb_vwv4, timeout);
891 SSVAL(cli->outbuf,smb_vwv6,0);
892 SSVAL(cli->outbuf,smb_vwv7,1);
894 p = smb_buf(cli->outbuf);
895 SSVAL(p, 0, cli->pid);
896 SIVAL(p, 2, offset);
897 SIVAL(p, 6, len);
899 p += 10;
901 cli_setup_bcc(cli, p);
903 cli_send_smb(cli);
905 if (timeout != 0) {
906 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
909 if (!cli_receive_smb(cli)) {
910 cli->timeout = saved_timeout;
911 return False;
914 cli->timeout = saved_timeout;
916 if (cli_is_error(cli)) {
917 return False;
920 return True;
923 /****************************************************************************
924 Unlock a file.
925 ****************************************************************************/
927 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
929 char *p;
931 memset(cli->outbuf,'\0',smb_size);
932 memset(cli->inbuf,'\0',smb_size);
934 set_message(cli->outbuf,8,0,True);
936 SCVAL(cli->outbuf,smb_com,SMBlockingX);
937 SSVAL(cli->outbuf,smb_tid,cli->cnum);
938 cli_setup_packet(cli);
940 SCVAL(cli->outbuf,smb_vwv0,0xFF);
941 SSVAL(cli->outbuf,smb_vwv2,fnum);
942 SCVAL(cli->outbuf,smb_vwv3,0);
943 SIVALS(cli->outbuf, smb_vwv4, 0);
944 SSVAL(cli->outbuf,smb_vwv6,1);
945 SSVAL(cli->outbuf,smb_vwv7,0);
947 p = smb_buf(cli->outbuf);
948 SSVAL(p, 0, cli->pid);
949 SIVAL(p, 2, offset);
950 SIVAL(p, 6, len);
951 p += 10;
952 cli_setup_bcc(cli, p);
953 cli_send_smb(cli);
954 if (!cli_receive_smb(cli)) {
955 return False;
958 if (cli_is_error(cli)) {
959 return False;
962 return True;
965 /****************************************************************************
966 Lock a file with 64 bit offsets.
967 ****************************************************************************/
969 BOOL cli_lock64(struct cli_state *cli, int fnum,
970 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
972 char *p;
973 int saved_timeout = cli->timeout;
974 int ltype;
976 if (! (cli->capabilities & CAP_LARGE_FILES)) {
977 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
980 ltype = (lock_type == READ_LOCK? 1 : 0);
981 ltype |= LOCKING_ANDX_LARGE_FILES;
983 memset(cli->outbuf,'\0',smb_size);
984 memset(cli->inbuf,'\0', smb_size);
986 set_message(cli->outbuf,8,0,True);
988 SCVAL(cli->outbuf,smb_com,SMBlockingX);
989 SSVAL(cli->outbuf,smb_tid,cli->cnum);
990 cli_setup_packet(cli);
992 SCVAL(cli->outbuf,smb_vwv0,0xFF);
993 SSVAL(cli->outbuf,smb_vwv2,fnum);
994 SCVAL(cli->outbuf,smb_vwv3,ltype);
995 SIVALS(cli->outbuf, smb_vwv4, timeout);
996 SSVAL(cli->outbuf,smb_vwv6,0);
997 SSVAL(cli->outbuf,smb_vwv7,1);
999 p = smb_buf(cli->outbuf);
1000 SIVAL(p, 0, cli->pid);
1001 SOFF_T_R(p, 4, offset);
1002 SOFF_T_R(p, 12, len);
1003 p += 20;
1005 cli_setup_bcc(cli, p);
1006 cli_send_smb(cli);
1008 if (timeout != 0) {
1009 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1012 if (!cli_receive_smb(cli)) {
1013 cli->timeout = saved_timeout;
1014 return False;
1017 cli->timeout = saved_timeout;
1019 if (cli_is_error(cli)) {
1020 return False;
1023 return True;
1026 /****************************************************************************
1027 Unlock a file with 64 bit offsets.
1028 ****************************************************************************/
1030 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1032 char *p;
1034 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1035 return cli_unlock(cli, fnum, offset, len);
1038 memset(cli->outbuf,'\0',smb_size);
1039 memset(cli->inbuf,'\0',smb_size);
1041 set_message(cli->outbuf,8,0,True);
1043 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1044 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1045 cli_setup_packet(cli);
1047 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1048 SSVAL(cli->outbuf,smb_vwv2,fnum);
1049 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1050 SIVALS(cli->outbuf, smb_vwv4, 0);
1051 SSVAL(cli->outbuf,smb_vwv6,1);
1052 SSVAL(cli->outbuf,smb_vwv7,0);
1054 p = smb_buf(cli->outbuf);
1055 SIVAL(p, 0, cli->pid);
1056 SOFF_T_R(p, 4, offset);
1057 SOFF_T_R(p, 12, len);
1058 p += 20;
1059 cli_setup_bcc(cli, p);
1060 cli_send_smb(cli);
1061 if (!cli_receive_smb(cli)) {
1062 return False;
1065 if (cli_is_error(cli)) {
1066 return False;
1069 return True;
1072 /****************************************************************************
1073 Get/unlock a POSIX lock on a file - internal function.
1074 ****************************************************************************/
1076 static BOOL cli_posix_lock_internal(struct cli_state *cli, int fnum,
1077 SMB_BIG_UINT offset, SMB_BIG_UINT len, BOOL wait_lock, enum brl_type lock_type)
1079 unsigned int param_len = 4;
1080 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1081 uint16 setup = TRANSACT2_SETFILEINFO;
1082 char param[4];
1083 unsigned char data[POSIX_LOCK_DATA_SIZE];
1084 char *rparam=NULL, *rdata=NULL;
1085 int saved_timeout = cli->timeout;
1087 SSVAL(param,0,fnum);
1088 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1090 switch (lock_type) {
1091 case READ_LOCK:
1092 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1093 break;
1094 case WRITE_LOCK:
1095 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1096 break;
1097 case UNLOCK_LOCK:
1098 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1099 break;
1100 default:
1101 return False;
1104 if (wait_lock) {
1105 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1106 cli->timeout = 0x7FFFFFFF;
1107 } else {
1108 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1111 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1112 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1113 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1115 if (!cli_send_trans(cli, SMBtrans2,
1116 NULL, /* name */
1117 -1, 0, /* fid, flags */
1118 &setup, 1, 0, /* setup, length, max */
1119 param, param_len, 2, /* param, length, max */
1120 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1121 )) {
1122 cli->timeout = saved_timeout;
1123 return False;
1126 if (!cli_receive_trans(cli, SMBtrans2,
1127 &rparam, &param_len,
1128 &rdata, &data_len)) {
1129 cli->timeout = saved_timeout;
1130 SAFE_FREE(rdata);
1131 SAFE_FREE(rparam);
1132 return False;
1135 cli->timeout = saved_timeout;
1137 SAFE_FREE(rdata);
1138 SAFE_FREE(rparam);
1140 return True;
1143 /****************************************************************************
1144 POSIX Lock a file.
1145 ****************************************************************************/
1147 BOOL cli_posix_lock(struct cli_state *cli, int fnum,
1148 SMB_BIG_UINT offset, SMB_BIG_UINT len,
1149 BOOL wait_lock, enum brl_type lock_type)
1151 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1152 return False;
1154 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1157 /****************************************************************************
1158 POSIX Unlock a file.
1159 ****************************************************************************/
1161 BOOL cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1163 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1166 /****************************************************************************
1167 POSIX Get any lock covering a file.
1168 ****************************************************************************/
1170 BOOL cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1172 return True;
1175 /****************************************************************************
1176 Do a SMBgetattrE call.
1177 ****************************************************************************/
1179 BOOL cli_getattrE(struct cli_state *cli, int fd,
1180 uint16 *attr, SMB_OFF_T *size,
1181 time_t *c_time, time_t *a_time, time_t *m_time)
1183 memset(cli->outbuf,'\0',smb_size);
1184 memset(cli->inbuf,'\0',smb_size);
1186 set_message(cli->outbuf,1,0,True);
1188 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1189 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1190 cli_setup_packet(cli);
1192 SSVAL(cli->outbuf,smb_vwv0,fd);
1194 cli_send_smb(cli);
1195 if (!cli_receive_smb(cli)) {
1196 return False;
1199 if (cli_is_error(cli)) {
1200 return False;
1203 if (size) {
1204 *size = IVAL(cli->inbuf, smb_vwv6);
1207 if (attr) {
1208 *attr = SVAL(cli->inbuf,smb_vwv10);
1211 if (c_time) {
1212 *c_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1215 if (a_time) {
1216 *a_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1219 if (m_time) {
1220 *m_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1223 return True;
1226 /****************************************************************************
1227 Do a SMBgetatr call
1228 ****************************************************************************/
1230 BOOL cli_getatr(struct cli_state *cli, const char *fname,
1231 uint16 *attr, SMB_OFF_T *size, time_t *t)
1233 char *p;
1235 memset(cli->outbuf,'\0',smb_size);
1236 memset(cli->inbuf,'\0',smb_size);
1238 set_message(cli->outbuf,0,0,True);
1240 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1241 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1242 cli_setup_packet(cli);
1244 p = smb_buf(cli->outbuf);
1245 *p++ = 4;
1246 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1248 cli_setup_bcc(cli, p);
1250 cli_send_smb(cli);
1251 if (!cli_receive_smb(cli)) {
1252 return False;
1255 if (cli_is_error(cli)) {
1256 return False;
1259 if (size) {
1260 *size = IVAL(cli->inbuf, smb_vwv3);
1263 if (t) {
1264 *t = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1267 if (attr) {
1268 *attr = SVAL(cli->inbuf,smb_vwv0);
1272 return True;
1275 /****************************************************************************
1276 Do a SMBsetattrE call.
1277 ****************************************************************************/
1279 BOOL cli_setattrE(struct cli_state *cli, int fd,
1280 time_t c_time, time_t a_time, time_t m_time)
1283 char *p;
1285 memset(cli->outbuf,'\0',smb_size);
1286 memset(cli->inbuf,'\0',smb_size);
1288 set_message(cli->outbuf,7,0,True);
1290 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1291 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1292 cli_setup_packet(cli);
1294 SSVAL(cli->outbuf,smb_vwv0, fd);
1295 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, c_time);
1296 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, a_time);
1297 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, m_time);
1299 p = smb_buf(cli->outbuf);
1300 *p++ = 4;
1302 cli_setup_bcc(cli, p);
1304 cli_send_smb(cli);
1305 if (!cli_receive_smb(cli)) {
1306 return False;
1309 if (cli_is_error(cli)) {
1310 return False;
1313 return True;
1316 /****************************************************************************
1317 Do a SMBsetatr call.
1318 ****************************************************************************/
1320 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1322 char *p;
1324 memset(cli->outbuf,'\0',smb_size);
1325 memset(cli->inbuf,'\0',smb_size);
1327 set_message(cli->outbuf,8,0,True);
1329 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1330 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1331 cli_setup_packet(cli);
1333 SSVAL(cli->outbuf,smb_vwv0, attr);
1334 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1336 p = smb_buf(cli->outbuf);
1337 *p++ = 4;
1338 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1339 *p++ = 4;
1341 cli_setup_bcc(cli, p);
1343 cli_send_smb(cli);
1344 if (!cli_receive_smb(cli)) {
1345 return False;
1348 if (cli_is_error(cli)) {
1349 return False;
1352 return True;
1355 /****************************************************************************
1356 Check for existance of a dir.
1357 ****************************************************************************/
1358 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1360 pstring path2;
1361 char *p;
1363 pstrcpy(path2,path);
1364 trim_char(path2,'\0','\\');
1365 if (!*path2)
1366 *path2 = '\\';
1368 memset(cli->outbuf,'\0',smb_size);
1369 set_message(cli->outbuf,0,0,True);
1370 SCVAL(cli->outbuf,smb_com,SMBchkpth);
1371 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1372 cli_setup_packet(cli);
1373 p = smb_buf(cli->outbuf);
1374 *p++ = 4;
1375 p += clistr_push(cli, p, path2, -1, 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)) return False;
1386 return True;
1389 /****************************************************************************
1390 Query disk space.
1391 ****************************************************************************/
1393 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1395 memset(cli->outbuf,'\0',smb_size);
1396 set_message(cli->outbuf,0,0,True);
1397 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1398 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1399 cli_setup_packet(cli);
1401 cli_send_smb(cli);
1402 if (!cli_receive_smb(cli)) {
1403 return False;
1406 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1407 *total = SVAL(cli->inbuf,smb_vwv0);
1408 *avail = SVAL(cli->inbuf,smb_vwv3);
1410 return True;
1413 /****************************************************************************
1414 Create and open a temporary file.
1415 ****************************************************************************/
1417 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1419 int len;
1420 char *p;
1422 memset(cli->outbuf,'\0',smb_size);
1423 memset(cli->inbuf,'\0',smb_size);
1425 set_message(cli->outbuf,3,0,True);
1427 SCVAL(cli->outbuf,smb_com,SMBctemp);
1428 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1429 cli_setup_packet(cli);
1431 SSVAL(cli->outbuf,smb_vwv0,0);
1432 SIVALS(cli->outbuf,smb_vwv1,-1);
1434 p = smb_buf(cli->outbuf);
1435 *p++ = 4;
1436 p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1438 cli_setup_bcc(cli, p);
1440 cli_send_smb(cli);
1441 if (!cli_receive_smb(cli)) {
1442 return -1;
1445 if (cli_is_error(cli)) {
1446 return -1;
1449 /* despite the spec, the result has a -1, followed by
1450 length, followed by name */
1451 p = smb_buf(cli->inbuf);
1452 p += 4;
1453 len = smb_buflen(cli->inbuf) - 4;
1454 if (len <= 0) return -1;
1456 if (tmp_path) {
1457 pstring path2;
1458 clistr_pull(cli, path2, p,
1459 sizeof(path2), len, STR_ASCII);
1460 *tmp_path = SMB_STRDUP(path2);
1463 return SVAL(cli->inbuf,smb_vwv0);
1468 send a raw ioctl - used by the torture code
1470 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1472 memset(cli->outbuf,'\0',smb_size);
1473 memset(cli->inbuf,'\0',smb_size);
1475 set_message(cli->outbuf, 3, 0, True);
1476 SCVAL(cli->outbuf,smb_com,SMBioctl);
1477 cli_setup_packet(cli);
1479 SSVAL(cli->outbuf, smb_vwv0, fnum);
1480 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1481 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1483 cli_send_smb(cli);
1484 if (!cli_receive_smb(cli)) {
1485 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1488 if (cli_is_error(cli)) {
1489 return cli_nt_error(cli);
1492 *blob = data_blob(NULL, 0);
1494 return NT_STATUS_OK;
1497 /*********************************************************
1498 Set an extended attribute utility fn.
1499 *********************************************************/
1501 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1502 const char *ea_name, const char *ea_val, size_t ea_len)
1504 unsigned int data_len = 0;
1505 char *data = NULL;
1506 char *rparam=NULL, *rdata=NULL;
1507 char *p;
1508 size_t ea_namelen = strlen(ea_name);
1510 if (ea_namelen == 0 && ea_len == 0) {
1511 data_len = 4;
1512 data = SMB_MALLOC(data_len);
1513 if (!data) {
1514 return False;
1516 p = data;
1517 SIVAL(p,0,data_len);
1518 } else {
1519 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1520 data = SMB_MALLOC(data_len);
1521 if (!data) {
1522 return False;
1524 p = data;
1525 SIVAL(p,0,data_len);
1526 p += 4;
1527 SCVAL(p, 0, 0); /* EA flags. */
1528 SCVAL(p, 1, ea_namelen);
1529 SSVAL(p, 2, ea_len);
1530 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1531 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1534 if (!cli_send_trans(cli, SMBtrans2,
1535 NULL, /* name */
1536 -1, 0, /* fid, flags */
1537 &setup, 1, 0, /* setup, length, max */
1538 param, param_len, 2, /* param, length, max */
1539 data, data_len, cli->max_xmit /* data, length, max */
1540 )) {
1541 return False;
1544 if (!cli_receive_trans(cli, SMBtrans2,
1545 &rparam, &param_len,
1546 &rdata, &data_len)) {
1547 return False;
1550 SAFE_FREE(data);
1551 SAFE_FREE(rdata);
1552 SAFE_FREE(rparam);
1554 return True;
1557 /*********************************************************
1558 Set an extended attribute on a pathname.
1559 *********************************************************/
1561 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1563 uint16 setup = TRANSACT2_SETPATHINFO;
1564 unsigned int param_len = 0;
1565 char param[sizeof(pstring)+6];
1566 size_t srclen = 2*(strlen(path)+1);
1567 char *p;
1569 memset(param, 0, sizeof(param));
1570 SSVAL(param,0,SMB_INFO_SET_EA);
1571 p = &param[6];
1573 p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1574 param_len = PTR_DIFF(p, param);
1576 return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1579 /*********************************************************
1580 Set an extended attribute on an fnum.
1581 *********************************************************/
1583 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1585 char param[6];
1586 uint16 setup = TRANSACT2_SETFILEINFO;
1588 memset(param, 0, 6);
1589 SSVAL(param,0,fnum);
1590 SSVAL(param,2,SMB_INFO_SET_EA);
1592 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1595 /*********************************************************
1596 Get an extended attribute list tility fn.
1597 *********************************************************/
1599 static BOOL cli_get_ea_list(struct cli_state *cli,
1600 uint16 setup, char *param, unsigned int param_len,
1601 TALLOC_CTX *ctx,
1602 size_t *pnum_eas,
1603 struct ea_struct **pea_list)
1605 unsigned int data_len = 0;
1606 unsigned int rparam_len, rdata_len;
1607 char *rparam=NULL, *rdata=NULL;
1608 char *p;
1609 size_t ea_size;
1610 size_t num_eas;
1611 BOOL ret = False;
1612 struct ea_struct *ea_list;
1614 *pnum_eas = 0;
1615 if (pea_list) {
1616 *pea_list = NULL;
1619 if (!cli_send_trans(cli, SMBtrans2,
1620 NULL, /* Name */
1621 -1, 0, /* fid, flags */
1622 &setup, 1, 0, /* setup, length, max */
1623 param, param_len, 10, /* param, length, max */
1624 NULL, data_len, cli->max_xmit /* data, length, max */
1625 )) {
1626 return False;
1629 if (!cli_receive_trans(cli, SMBtrans2,
1630 &rparam, &rparam_len,
1631 &rdata, &rdata_len)) {
1632 return False;
1635 if (!rdata || rdata_len < 4) {
1636 goto out;
1639 ea_size = (size_t)IVAL(rdata,0);
1640 if (ea_size > rdata_len) {
1641 goto out;
1644 if (ea_size == 0) {
1645 /* No EA's present. */
1646 ret = True;
1647 goto out;
1650 p = rdata + 4;
1651 ea_size -= 4;
1653 /* Validate the EA list and count it. */
1654 for (num_eas = 0; ea_size >= 4; num_eas++) {
1655 unsigned int ea_namelen = CVAL(p,1);
1656 unsigned int ea_valuelen = SVAL(p,2);
1657 if (ea_namelen == 0) {
1658 goto out;
1660 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1661 goto out;
1663 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1664 p += 4 + ea_namelen + 1 + ea_valuelen;
1667 if (num_eas == 0) {
1668 ret = True;
1669 goto out;
1672 *pnum_eas = num_eas;
1673 if (!pea_list) {
1674 /* Caller only wants number of EA's. */
1675 ret = True;
1676 goto out;
1679 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1680 if (!ea_list) {
1681 goto out;
1684 ea_size = (size_t)IVAL(rdata,0);
1685 p = rdata + 4;
1687 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1688 struct ea_struct *ea = &ea_list[num_eas];
1689 fstring unix_ea_name;
1690 unsigned int ea_namelen = CVAL(p,1);
1691 unsigned int ea_valuelen = SVAL(p,2);
1693 ea->flags = CVAL(p,0);
1694 unix_ea_name[0] = '\0';
1695 pull_ascii_fstring(unix_ea_name, p + 4);
1696 ea->name = talloc_strdup(ctx, unix_ea_name);
1697 /* Ensure the value is null terminated (in case it's a string). */
1698 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1699 if (!ea->value.data) {
1700 goto out;
1702 if (ea_valuelen) {
1703 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1705 ea->value.data[ea_valuelen] = 0;
1706 ea->value.length--;
1707 p += 4 + ea_namelen + 1 + ea_valuelen;
1710 *pea_list = ea_list;
1711 ret = True;
1713 out :
1715 SAFE_FREE(rdata);
1716 SAFE_FREE(rparam);
1717 return ret;
1720 /*********************************************************
1721 Get an extended attribute list from a pathname.
1722 *********************************************************/
1724 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1725 TALLOC_CTX *ctx,
1726 size_t *pnum_eas,
1727 struct ea_struct **pea_list)
1729 uint16 setup = TRANSACT2_QPATHINFO;
1730 unsigned int param_len = 0;
1731 char param[sizeof(pstring)+6];
1732 char *p;
1734 p = param;
1735 memset(p, 0, 6);
1736 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1737 p += 6;
1738 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1739 param_len = PTR_DIFF(p, param);
1741 return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1744 /*********************************************************
1745 Get an extended attribute list from an fnum.
1746 *********************************************************/
1748 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1749 TALLOC_CTX *ctx,
1750 size_t *pnum_eas,
1751 struct ea_struct **pea_list)
1753 uint16 setup = TRANSACT2_QFILEINFO;
1754 char param[6];
1756 memset(param, 0, 6);
1757 SSVAL(param,0,fnum);
1758 SSVAL(param,2,SMB_INFO_SET_EA);
1760 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);