r9400: updating release notes
[Samba.git] / source / libsmb / clifile.c
bloba7e6fbfe3f1858dc0202ad085ee2d389b9277682
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 ****************************************************************************/
819 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
820 uint32 offset, uint32 len, int timeout, unsigned char locktype)
822 char *p;
823 int saved_timeout = cli->timeout;
825 memset(cli->outbuf,'\0',smb_size);
826 memset(cli->inbuf,'\0', smb_size);
828 set_message(cli->outbuf,8,0,True);
830 SCVAL(cli->outbuf,smb_com,SMBlockingX);
831 SSVAL(cli->outbuf,smb_tid,cli->cnum);
832 cli_setup_packet(cli);
834 SCVAL(cli->outbuf,smb_vwv0,0xFF);
835 SSVAL(cli->outbuf,smb_vwv2,fnum);
836 SCVAL(cli->outbuf,smb_vwv3,locktype);
837 SIVALS(cli->outbuf, smb_vwv4, timeout);
838 SSVAL(cli->outbuf,smb_vwv6,0);
839 SSVAL(cli->outbuf,smb_vwv7,1);
841 p = smb_buf(cli->outbuf);
842 SSVAL(p, 0, cli->pid);
843 SIVAL(p, 2, offset);
844 SIVAL(p, 6, len);
846 p += 10;
848 cli_setup_bcc(cli, p);
850 cli_send_smb(cli);
852 if (timeout != 0) {
853 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
856 if (!cli_receive_smb(cli)) {
857 cli->timeout = saved_timeout;
858 return NT_STATUS_UNSUCCESSFUL;
861 cli->timeout = saved_timeout;
863 return cli_nt_error(cli);
867 /****************************************************************************
868 Lock a file.
869 note that timeout is in units of 2 milliseconds
870 ****************************************************************************/
871 BOOL cli_lock(struct cli_state *cli, int fnum,
872 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
874 char *p;
875 int saved_timeout = cli->timeout;
877 memset(cli->outbuf,'\0',smb_size);
878 memset(cli->inbuf,'\0', smb_size);
880 set_message(cli->outbuf,8,0,True);
882 SCVAL(cli->outbuf,smb_com,SMBlockingX);
883 SSVAL(cli->outbuf,smb_tid,cli->cnum);
884 cli_setup_packet(cli);
886 SCVAL(cli->outbuf,smb_vwv0,0xFF);
887 SSVAL(cli->outbuf,smb_vwv2,fnum);
888 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
889 SIVALS(cli->outbuf, smb_vwv4, timeout);
890 SSVAL(cli->outbuf,smb_vwv6,0);
891 SSVAL(cli->outbuf,smb_vwv7,1);
893 p = smb_buf(cli->outbuf);
894 SSVAL(p, 0, cli->pid);
895 SIVAL(p, 2, offset);
896 SIVAL(p, 6, len);
898 p += 10;
900 cli_setup_bcc(cli, p);
902 cli_send_smb(cli);
904 if (timeout != 0) {
905 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
908 if (!cli_receive_smb(cli)) {
909 cli->timeout = saved_timeout;
910 return False;
913 cli->timeout = saved_timeout;
915 if (cli_is_error(cli)) {
916 return False;
919 return True;
922 /****************************************************************************
923 Unlock a file.
924 ****************************************************************************/
926 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
928 char *p;
930 memset(cli->outbuf,'\0',smb_size);
931 memset(cli->inbuf,'\0',smb_size);
933 set_message(cli->outbuf,8,0,True);
935 SCVAL(cli->outbuf,smb_com,SMBlockingX);
936 SSVAL(cli->outbuf,smb_tid,cli->cnum);
937 cli_setup_packet(cli);
939 SCVAL(cli->outbuf,smb_vwv0,0xFF);
940 SSVAL(cli->outbuf,smb_vwv2,fnum);
941 SCVAL(cli->outbuf,smb_vwv3,0);
942 SIVALS(cli->outbuf, smb_vwv4, 0);
943 SSVAL(cli->outbuf,smb_vwv6,1);
944 SSVAL(cli->outbuf,smb_vwv7,0);
946 p = smb_buf(cli->outbuf);
947 SSVAL(p, 0, cli->pid);
948 SIVAL(p, 2, offset);
949 SIVAL(p, 6, len);
950 p += 10;
951 cli_setup_bcc(cli, p);
952 cli_send_smb(cli);
953 if (!cli_receive_smb(cli)) {
954 return False;
957 if (cli_is_error(cli)) {
958 return False;
961 return True;
964 /****************************************************************************
965 Lock a file with 64 bit offsets.
966 ****************************************************************************/
968 BOOL cli_lock64(struct cli_state *cli, int fnum,
969 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
971 char *p;
972 int saved_timeout = cli->timeout;
973 int ltype;
975 if (! (cli->capabilities & CAP_LARGE_FILES)) {
976 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
979 ltype = (lock_type == READ_LOCK? 1 : 0);
980 ltype |= LOCKING_ANDX_LARGE_FILES;
982 memset(cli->outbuf,'\0',smb_size);
983 memset(cli->inbuf,'\0', smb_size);
985 set_message(cli->outbuf,8,0,True);
987 SCVAL(cli->outbuf,smb_com,SMBlockingX);
988 SSVAL(cli->outbuf,smb_tid,cli->cnum);
989 cli_setup_packet(cli);
991 SCVAL(cli->outbuf,smb_vwv0,0xFF);
992 SSVAL(cli->outbuf,smb_vwv2,fnum);
993 SCVAL(cli->outbuf,smb_vwv3,ltype);
994 SIVALS(cli->outbuf, smb_vwv4, timeout);
995 SSVAL(cli->outbuf,smb_vwv6,0);
996 SSVAL(cli->outbuf,smb_vwv7,1);
998 p = smb_buf(cli->outbuf);
999 SIVAL(p, 0, cli->pid);
1000 SOFF_T_R(p, 4, offset);
1001 SOFF_T_R(p, 12, len);
1002 p += 20;
1004 cli_setup_bcc(cli, p);
1005 cli_send_smb(cli);
1007 if (timeout != 0) {
1008 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1011 if (!cli_receive_smb(cli)) {
1012 cli->timeout = saved_timeout;
1013 return False;
1016 cli->timeout = saved_timeout;
1018 if (cli_is_error(cli)) {
1019 return False;
1022 return True;
1025 /****************************************************************************
1026 Unlock a file with 64 bit offsets.
1027 ****************************************************************************/
1029 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1031 char *p;
1033 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1034 return cli_unlock(cli, fnum, offset, len);
1037 memset(cli->outbuf,'\0',smb_size);
1038 memset(cli->inbuf,'\0',smb_size);
1040 set_message(cli->outbuf,8,0,True);
1042 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1043 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1044 cli_setup_packet(cli);
1046 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1047 SSVAL(cli->outbuf,smb_vwv2,fnum);
1048 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1049 SIVALS(cli->outbuf, smb_vwv4, 0);
1050 SSVAL(cli->outbuf,smb_vwv6,1);
1051 SSVAL(cli->outbuf,smb_vwv7,0);
1053 p = smb_buf(cli->outbuf);
1054 SIVAL(p, 0, cli->pid);
1055 SOFF_T_R(p, 4, offset);
1056 SOFF_T_R(p, 12, len);
1057 p += 20;
1058 cli_setup_bcc(cli, p);
1059 cli_send_smb(cli);
1060 if (!cli_receive_smb(cli)) {
1061 return False;
1064 if (cli_is_error(cli)) {
1065 return False;
1068 return True;
1072 /****************************************************************************
1073 Do a SMBgetattrE call.
1074 ****************************************************************************/
1076 BOOL cli_getattrE(struct cli_state *cli, int fd,
1077 uint16 *attr, SMB_OFF_T *size,
1078 time_t *c_time, time_t *a_time, time_t *m_time)
1080 memset(cli->outbuf,'\0',smb_size);
1081 memset(cli->inbuf,'\0',smb_size);
1083 set_message(cli->outbuf,1,0,True);
1085 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1086 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1087 cli_setup_packet(cli);
1089 SSVAL(cli->outbuf,smb_vwv0,fd);
1091 cli_send_smb(cli);
1092 if (!cli_receive_smb(cli)) {
1093 return False;
1096 if (cli_is_error(cli)) {
1097 return False;
1100 if (size) {
1101 *size = IVAL(cli->inbuf, smb_vwv6);
1104 if (attr) {
1105 *attr = SVAL(cli->inbuf,smb_vwv10);
1108 if (c_time) {
1109 *c_time = make_unix_date2(cli->inbuf+smb_vwv0);
1112 if (a_time) {
1113 *a_time = make_unix_date2(cli->inbuf+smb_vwv2);
1116 if (m_time) {
1117 *m_time = make_unix_date2(cli->inbuf+smb_vwv4);
1120 return True;
1123 /****************************************************************************
1124 Do a SMBgetatr call
1125 ****************************************************************************/
1127 BOOL cli_getatr(struct cli_state *cli, const char *fname,
1128 uint16 *attr, SMB_OFF_T *size, time_t *t)
1130 char *p;
1132 memset(cli->outbuf,'\0',smb_size);
1133 memset(cli->inbuf,'\0',smb_size);
1135 set_message(cli->outbuf,0,0,True);
1137 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1138 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1139 cli_setup_packet(cli);
1141 p = smb_buf(cli->outbuf);
1142 *p++ = 4;
1143 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1145 cli_setup_bcc(cli, p);
1147 cli_send_smb(cli);
1148 if (!cli_receive_smb(cli)) {
1149 return False;
1152 if (cli_is_error(cli)) {
1153 return False;
1156 if (size) {
1157 *size = IVAL(cli->inbuf, smb_vwv3);
1160 if (t) {
1161 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1164 if (attr) {
1165 *attr = SVAL(cli->inbuf,smb_vwv0);
1169 return True;
1172 /****************************************************************************
1173 Do a SMBsetattrE call.
1174 ****************************************************************************/
1176 BOOL cli_setattrE(struct cli_state *cli, int fd,
1177 time_t c_time, time_t a_time, time_t m_time)
1180 char *p;
1182 memset(cli->outbuf,'\0',smb_size);
1183 memset(cli->inbuf,'\0',smb_size);
1185 set_message(cli->outbuf,7,0,True);
1187 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1188 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1189 cli_setup_packet(cli);
1191 SSVAL(cli->outbuf,smb_vwv0, fd);
1192 put_dos_date2(cli->outbuf,smb_vwv1, c_time);
1193 put_dos_date2(cli->outbuf,smb_vwv3, a_time);
1194 put_dos_date2(cli->outbuf,smb_vwv5, m_time);
1196 p = smb_buf(cli->outbuf);
1197 *p++ = 4;
1199 cli_setup_bcc(cli, p);
1201 cli_send_smb(cli);
1202 if (!cli_receive_smb(cli)) {
1203 return False;
1206 if (cli_is_error(cli)) {
1207 return False;
1210 return True;
1213 /****************************************************************************
1214 Do a SMBsetatr call.
1215 ****************************************************************************/
1217 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1219 char *p;
1221 memset(cli->outbuf,'\0',smb_size);
1222 memset(cli->inbuf,'\0',smb_size);
1224 set_message(cli->outbuf,8,0,True);
1226 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1227 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1228 cli_setup_packet(cli);
1230 SSVAL(cli->outbuf,smb_vwv0, attr);
1231 put_dos_date3(cli->outbuf,smb_vwv1, t);
1233 p = smb_buf(cli->outbuf);
1234 *p++ = 4;
1235 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1236 *p++ = 4;
1238 cli_setup_bcc(cli, p);
1240 cli_send_smb(cli);
1241 if (!cli_receive_smb(cli)) {
1242 return False;
1245 if (cli_is_error(cli)) {
1246 return False;
1249 return True;
1252 /****************************************************************************
1253 Check for existance of a dir.
1254 ****************************************************************************/
1255 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1257 pstring path2;
1258 char *p;
1260 pstrcpy(path2,path);
1261 trim_char(path2,'\0','\\');
1262 if (!*path2)
1263 *path2 = '\\';
1265 memset(cli->outbuf,'\0',smb_size);
1266 set_message(cli->outbuf,0,0,True);
1267 SCVAL(cli->outbuf,smb_com,SMBchkpth);
1268 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1269 cli_setup_packet(cli);
1270 p = smb_buf(cli->outbuf);
1271 *p++ = 4;
1272 p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1274 cli_setup_bcc(cli, p);
1276 cli_send_smb(cli);
1277 if (!cli_receive_smb(cli)) {
1278 return False;
1281 if (cli_is_error(cli)) return False;
1283 return True;
1286 /****************************************************************************
1287 Query disk space.
1288 ****************************************************************************/
1290 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1292 memset(cli->outbuf,'\0',smb_size);
1293 set_message(cli->outbuf,0,0,True);
1294 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1295 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1296 cli_setup_packet(cli);
1298 cli_send_smb(cli);
1299 if (!cli_receive_smb(cli)) {
1300 return False;
1303 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1304 *total = SVAL(cli->inbuf,smb_vwv0);
1305 *avail = SVAL(cli->inbuf,smb_vwv3);
1307 return True;
1310 /****************************************************************************
1311 Create and open a temporary file.
1312 ****************************************************************************/
1314 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1316 int len;
1317 char *p;
1319 memset(cli->outbuf,'\0',smb_size);
1320 memset(cli->inbuf,'\0',smb_size);
1322 set_message(cli->outbuf,3,0,True);
1324 SCVAL(cli->outbuf,smb_com,SMBctemp);
1325 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1326 cli_setup_packet(cli);
1328 SSVAL(cli->outbuf,smb_vwv0,0);
1329 SIVALS(cli->outbuf,smb_vwv1,-1);
1331 p = smb_buf(cli->outbuf);
1332 *p++ = 4;
1333 p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1335 cli_setup_bcc(cli, p);
1337 cli_send_smb(cli);
1338 if (!cli_receive_smb(cli)) {
1339 return -1;
1342 if (cli_is_error(cli)) {
1343 return -1;
1346 /* despite the spec, the result has a -1, followed by
1347 length, followed by name */
1348 p = smb_buf(cli->inbuf);
1349 p += 4;
1350 len = smb_buflen(cli->inbuf) - 4;
1351 if (len <= 0) return -1;
1353 if (tmp_path) {
1354 pstring path2;
1355 clistr_pull(cli, path2, p,
1356 sizeof(path2), len, STR_ASCII);
1357 *tmp_path = SMB_STRDUP(path2);
1360 return SVAL(cli->inbuf,smb_vwv0);
1365 send a raw ioctl - used by the torture code
1367 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1369 memset(cli->outbuf,'\0',smb_size);
1370 memset(cli->inbuf,'\0',smb_size);
1372 set_message(cli->outbuf, 3, 0, True);
1373 SCVAL(cli->outbuf,smb_com,SMBioctl);
1374 cli_setup_packet(cli);
1376 SSVAL(cli->outbuf, smb_vwv0, fnum);
1377 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1378 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1380 cli_send_smb(cli);
1381 if (!cli_receive_smb(cli)) {
1382 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1385 if (cli_is_error(cli)) {
1386 return cli_nt_error(cli);
1389 *blob = data_blob(NULL, 0);
1391 return NT_STATUS_OK;
1394 /*********************************************************
1395 Set an extended attribute utility fn.
1396 *********************************************************/
1398 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1399 const char *ea_name, const char *ea_val, size_t ea_len)
1401 unsigned int data_len = 0;
1402 char *data = NULL;
1403 char *rparam=NULL, *rdata=NULL;
1404 char *p;
1405 size_t ea_namelen = strlen(ea_name);
1407 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1408 data = SMB_MALLOC(data_len);
1409 if (!data) {
1410 return False;
1412 p = data;
1413 SIVAL(p,0,data_len);
1414 p += 4;
1415 SCVAL(p, 0, 0); /* EA flags. */
1416 SCVAL(p, 1, ea_namelen);
1417 SSVAL(p, 2, ea_len);
1418 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1419 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1421 if (!cli_send_trans(cli, SMBtrans2,
1422 NULL, /* name */
1423 -1, 0, /* fid, flags */
1424 &setup, 1, 0, /* setup, length, max */
1425 param, param_len, 2, /* param, length, max */
1426 data, data_len, cli->max_xmit /* data, length, max */
1427 )) {
1428 return False;
1431 if (!cli_receive_trans(cli, SMBtrans2,
1432 &rparam, &param_len,
1433 &rdata, &data_len)) {
1434 return False;
1437 SAFE_FREE(data);
1438 SAFE_FREE(rdata);
1439 SAFE_FREE(rparam);
1441 return True;
1444 /*********************************************************
1445 Set an extended attribute on a pathname.
1446 *********************************************************/
1448 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1450 uint16 setup = TRANSACT2_SETPATHINFO;
1451 unsigned int param_len = 0;
1452 char param[sizeof(pstring)+6];
1453 size_t srclen = 2*(strlen(path)+1);
1454 char *p;
1456 memset(param, 0, sizeof(param));
1457 SSVAL(param,0,SMB_INFO_SET_EA);
1458 p = &param[6];
1460 p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1461 param_len = PTR_DIFF(p, param);
1463 return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1466 /*********************************************************
1467 Set an extended attribute on an fnum.
1468 *********************************************************/
1470 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1472 char param[6];
1473 uint16 setup = TRANSACT2_SETFILEINFO;
1475 memset(param, 0, 6);
1476 SSVAL(param,0,fnum);
1477 SSVAL(param,2,SMB_INFO_SET_EA);
1479 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1482 /*********************************************************
1483 Get an extended attribute list tility fn.
1484 *********************************************************/
1486 static BOOL cli_get_ea_list(struct cli_state *cli,
1487 uint16 setup, char *param, unsigned int param_len,
1488 TALLOC_CTX *ctx,
1489 size_t *pnum_eas,
1490 struct ea_struct **pea_list)
1492 unsigned int data_len = 0;
1493 unsigned int rparam_len, rdata_len;
1494 char *rparam=NULL, *rdata=NULL;
1495 char *p;
1496 size_t ea_size;
1497 size_t num_eas;
1498 BOOL ret = False;
1499 struct ea_struct *ea_list;
1501 *pnum_eas = 0;
1502 *pea_list = NULL;
1504 if (!cli_send_trans(cli, SMBtrans2,
1505 NULL, /* Name */
1506 -1, 0, /* fid, flags */
1507 &setup, 1, 0, /* setup, length, max */
1508 param, param_len, 10, /* param, length, max */
1509 NULL, data_len, cli->max_xmit /* data, length, max */
1510 )) {
1511 return False;
1514 if (!cli_receive_trans(cli, SMBtrans2,
1515 &rparam, &rparam_len,
1516 &rdata, &rdata_len)) {
1517 return False;
1520 if (!rdata || rdata_len < 4) {
1521 goto out;
1524 ea_size = (size_t)IVAL(rdata,0);
1525 if (ea_size > rdata_len) {
1526 goto out;
1529 if (ea_size == 0) {
1530 /* No EA's present. */
1531 ret = True;
1532 goto out;
1535 p = rdata + 4;
1536 ea_size -= 4;
1538 /* Validate the EA list and count it. */
1539 for (num_eas = 0; ea_size >= 4; num_eas++) {
1540 unsigned int ea_namelen = CVAL(p,1);
1541 unsigned int ea_valuelen = SVAL(p,2);
1542 if (ea_namelen == 0) {
1543 goto out;
1545 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1546 goto out;
1548 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1549 p += 4 + ea_namelen + 1 + ea_valuelen;
1552 if (num_eas == 0) {
1553 ret = True;
1554 goto out;
1557 *pnum_eas = num_eas;
1558 if (!pea_list) {
1559 /* Caller only wants number of EA's. */
1560 ret = True;
1561 goto out;
1564 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1565 if (!ea_list) {
1566 goto out;
1569 ea_size = (size_t)IVAL(rdata,0);
1570 p = rdata + 4;
1572 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1573 struct ea_struct *ea = &ea_list[num_eas];
1574 fstring unix_ea_name;
1575 unsigned int ea_namelen = CVAL(p,1);
1576 unsigned int ea_valuelen = SVAL(p,2);
1578 ea->flags = CVAL(p,0);
1579 unix_ea_name[0] = '\0';
1580 pull_ascii_fstring(unix_ea_name, p + 4);
1581 ea->name = talloc_strdup(ctx, unix_ea_name);
1582 /* Ensure the value is null terminated (in case it's a string). */
1583 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1584 if (!ea->value.data) {
1585 goto out;
1587 if (ea_valuelen) {
1588 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1590 ea->value.data[ea_valuelen] = 0;
1591 ea->value.length--;
1592 p += 4 + ea_namelen + 1 + ea_valuelen;
1595 *pea_list = ea_list;
1596 ret = True;
1598 out :
1600 SAFE_FREE(rdata);
1601 SAFE_FREE(rparam);
1602 return ret;
1605 /*********************************************************
1606 Get an extended attribute list from a pathname.
1607 *********************************************************/
1609 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1610 TALLOC_CTX *ctx,
1611 size_t *pnum_eas,
1612 struct ea_struct **pea_list)
1614 uint16 setup = TRANSACT2_QPATHINFO;
1615 unsigned int param_len = 0;
1616 char param[sizeof(pstring)+6];
1617 char *p;
1619 p = param;
1620 memset(p, 0, 6);
1621 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1622 p += 6;
1623 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1624 param_len = PTR_DIFF(p, param);
1626 return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1629 /*********************************************************
1630 Get an extended attribute list from an fnum.
1631 *********************************************************/
1633 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1634 TALLOC_CTX *ctx,
1635 size_t *pnum_eas,
1636 struct ea_struct **pea_list)
1638 uint16 setup = TRANSACT2_QFILEINFO;
1639 char param[6];
1641 memset(param, 0, 6);
1642 SSVAL(param,0,fnum);
1643 SSVAL(param,2,SMB_INFO_SET_EA);
1645 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);