Split up async_req into a generic and a NTSTATUS specific part
[tprouty/samba.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2002
6    
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Hard/Symlink a file (UNIX extensions).
25  Creates new name (sym)linked to oldname.
26 ****************************************************************************/
27
28 static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
29 {
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);
39
40         param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
41
42         if (!param) {
43                 return false;
44         }
45
46         data = SMB_MALLOC_ARRAY(char, oldlen+2);
47
48         if (!data) {
49                 SAFE_FREE(param);
50                 return false;
51         }
52
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];
56
57         p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
58         param_len = PTR_DIFF(p, param);
59
60         p = data;
61         p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
62         data_len = PTR_DIFF(p, data);
63
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;
74         }
75
76         SAFE_FREE(data);
77         SAFE_FREE(param);
78
79         if (!cli_receive_trans(cli, SMBtrans2,
80                         &rparam, &param_len,
81                         &rdata, &data_len)) {
82                         return false;
83         }
84
85         SAFE_FREE(data);
86         SAFE_FREE(param);
87         SAFE_FREE(rdata);
88         SAFE_FREE(rparam);
89
90         return true;
91 }
92
93 /****************************************************************************
94  Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
96
97 uint32 unix_perms_to_wire(mode_t perms)
98 {
99         unsigned int ret = 0;
100
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;
120 }
121
122 /****************************************************************************
123  Map wire permissions to standard UNIX.
124 ****************************************************************************/
125
126 mode_t wire_perms_to_unix(uint32 perms)
127 {
128         mode_t ret = (mode_t)0;
129
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;
149 }
150
151 /****************************************************************************
152  Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
154
155 static mode_t unix_filetype_from_wire(uint32 wire_type)
156 {
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;
184         }
185 }
186
187 /****************************************************************************
188  Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
190
191 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
192 {
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;
200
201         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
202         if (!param) {
203                 return false;
204         }
205
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);
212
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;
222         }
223
224         SAFE_FREE(param);
225
226         if (!cli_receive_trans(cli, SMBtrans2,
227                         &rparam, &param_len,
228                         &rdata, &data_len)) {
229                 return false;
230         }
231
232         if (data_len < 6) {
233                 SAFE_FREE(rdata);
234                 SAFE_FREE(rparam);
235                 return false;
236         }
237
238         SAFE_FREE(rparam);
239         *retbuf = rdata;
240         *prb_size = (size_t)data_len;
241
242         return true;
243 }
244
245 /****************************************************************************
246  Stat a file (UNIX extensions).
247 ****************************************************************************/
248
249 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
250 {
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;
258
259         ZERO_STRUCTP(sbuf);
260
261         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
262         if (!param) {
263                 return false;
264         }
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);
271
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;
281         }
282
283         SAFE_FREE(param);
284
285         if (!cli_receive_trans(cli, SMBtrans2,
286                         &rparam, &param_len,
287                         &rdata, &data_len)) {
288                 return false;
289         }
290
291         if (data_len < 96) {
292                 SAFE_FREE(rdata);
293                 SAFE_FREE(rparam);
294                 return false;
295         }
296
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 */
308
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)
313         {
314                 uint32 dev_major = IVAL(rdata,60);
315                 uint32 dev_minor = IVAL(rdata,68);
316                 sbuf->st_rdev = makedev(dev_major, dev_minor);
317         }
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 */
322
323         SAFE_FREE(rdata);
324         SAFE_FREE(rparam);
325
326         return true;
327 }
328
329 /****************************************************************************
330  Symlink a file (UNIX extensions).
331 ****************************************************************************/
332
333 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
334 {
335         return cli_link_internal(cli, oldname, newname, False);
336 }
337
338 /****************************************************************************
339  Hard a file (UNIX extensions).
340 ****************************************************************************/
341
342 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
343 {
344         return cli_link_internal(cli, oldname, newname, True);
345 }
346
347 /****************************************************************************
348  Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
350
351 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
352 {
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;
361
362         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
363         if (!param) {
364                 return false;
365         }
366         memset(param, '\0', 6);
367         memset(data, 0, sizeof(data));
368
369         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
370         p = &param[6];
371
372         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
373         param_len = PTR_DIFF(p, param);
374
375         memset(data, 0xff, 40); /* Set all sizes/times to no change. */
376
377         SIVAL(data,40,uid);
378         SIVAL(data,48,gid);
379         SIVAL(data,84,mode);
380
381         data_len = 100;
382
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;
392         }
393
394         SAFE_FREE(param);
395
396         if (!cli_receive_trans(cli, SMBtrans2,
397                         &rparam, &param_len,
398                         &rdata, &data_len)) {
399                 return false;
400         }
401
402         SAFE_FREE(rdata);
403         SAFE_FREE(rparam);
404
405         return true;
406 }
407
408 /****************************************************************************
409  chmod a file (UNIX extensions).
410 ****************************************************************************/
411
412 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
413 {
414         return cli_unix_chmod_chown_internal(cli, fname,
415                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
416 }
417
418 /****************************************************************************
419  chown a file (UNIX extensions).
420 ****************************************************************************/
421
422 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
423 {
424         return cli_unix_chmod_chown_internal(cli, fname,
425                         SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
426 }
427
428 /****************************************************************************
429  Rename a file.
430 ****************************************************************************/
431
432 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
433 {
434         char *p;
435
436         memset(cli->outbuf,'\0',smb_size);
437         memset(cli->inbuf,'\0',smb_size);
438
439         cli_set_message(cli->outbuf,1, 0, true);
440
441         SCVAL(cli->outbuf,smb_com,SMBmv);
442         SSVAL(cli->outbuf,smb_tid,cli->cnum);
443         cli_setup_packet(cli);
444
445         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
446
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);
454
455         cli_setup_bcc(cli, p);
456
457         cli_send_smb(cli);
458         if (!cli_receive_smb(cli)) {
459                 return false;
460         }
461
462         if (cli_is_error(cli)) {
463                 return false;
464         }
465
466         return true;
467 }
468
469 /****************************************************************************
470  NT Rename a file.
471 ****************************************************************************/
472
473 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
474 {
475         char *p;
476
477         memset(cli->outbuf,'\0',smb_size);
478         memset(cli->inbuf,'\0',smb_size);
479
480         cli_set_message(cli->outbuf, 4, 0, true);
481
482         SCVAL(cli->outbuf,smb_com,SMBntrename);
483         SSVAL(cli->outbuf,smb_tid,cli->cnum);
484         cli_setup_packet(cli);
485
486         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
487         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
488
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);
496
497         cli_setup_bcc(cli, p);
498
499         cli_send_smb(cli);
500         if (!cli_receive_smb(cli)) {
501                 return false;
502         }
503
504         if (cli_is_error(cli)) {
505                 return false;
506         }
507
508         return true;
509 }
510
511 /****************************************************************************
512  NT hardlink a file.
513 ****************************************************************************/
514
515 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
516 {
517         char *p;
518
519         memset(cli->outbuf,'\0',smb_size);
520         memset(cli->inbuf,'\0',smb_size);
521
522         cli_set_message(cli->outbuf, 4, 0, true);
523
524         SCVAL(cli->outbuf,smb_com,SMBntrename);
525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
526         cli_setup_packet(cli);
527
528         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
529         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
530
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);
538
539         cli_setup_bcc(cli, p);
540
541         cli_send_smb(cli);
542         if (!cli_receive_smb(cli)) {
543                 return false;
544         }
545
546         if (cli_is_error(cli)) {
547                 return false;
548         }
549
550         return true;
551 }
552
553 /****************************************************************************
554  Delete a file.
555 ****************************************************************************/
556
557 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
558 {
559         char *p;
560
561         memset(cli->outbuf,'\0',smb_size);
562         memset(cli->inbuf,'\0',smb_size);
563
564         cli_set_message(cli->outbuf,1, 0, true);
565
566         SCVAL(cli->outbuf,smb_com,SMBunlink);
567         SSVAL(cli->outbuf,smb_tid,cli->cnum);
568         cli_setup_packet(cli);
569
570         SSVAL(cli->outbuf,smb_vwv0, attrs);
571
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);
576
577         cli_setup_bcc(cli, p);
578         cli_send_smb(cli);
579         if (!cli_receive_smb(cli)) {
580                 return false;
581         }
582
583         if (cli_is_error(cli)) {
584                 return false;
585         }
586
587         return true;
588 }
589
590 /****************************************************************************
591  Delete a file.
592 ****************************************************************************/
593
594 bool cli_unlink(struct cli_state *cli, const char *fname)
595 {
596         return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
597 }
598
599 /****************************************************************************
600  Create a directory.
601 ****************************************************************************/
602
603 bool cli_mkdir(struct cli_state *cli, const char *dname)
604 {
605         char *p;
606
607         memset(cli->outbuf,'\0',smb_size);
608         memset(cli->inbuf,'\0',smb_size);
609
610         cli_set_message(cli->outbuf,0, 0, true);
611
612         SCVAL(cli->outbuf,smb_com,SMBmkdir);
613         SSVAL(cli->outbuf,smb_tid,cli->cnum);
614         cli_setup_packet(cli);
615
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);
620
621         cli_setup_bcc(cli, p);
622
623         cli_send_smb(cli);
624         if (!cli_receive_smb(cli)) {
625                 return False;
626         }
627
628         if (cli_is_error(cli)) {
629                 return False;
630         }
631
632         return True;
633 }
634
635 /****************************************************************************
636  Remove a directory.
637 ****************************************************************************/
638
639 bool cli_rmdir(struct cli_state *cli, const char *dname)
640 {
641         char *p;
642
643         memset(cli->outbuf,'\0',smb_size);
644         memset(cli->inbuf,'\0',smb_size);
645
646         cli_set_message(cli->outbuf,0, 0, true);
647
648         SCVAL(cli->outbuf,smb_com,SMBrmdir);
649         SSVAL(cli->outbuf,smb_tid,cli->cnum);
650         cli_setup_packet(cli);
651
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);
656
657         cli_setup_bcc(cli, p);
658
659         cli_send_smb(cli);
660         if (!cli_receive_smb(cli)) {
661                 return false;
662         }
663
664         if (cli_is_error(cli)) {
665                 return false;
666         }
667
668         return true;
669 }
670
671 /****************************************************************************
672  Set or clear the delete on close flag.
673 ****************************************************************************/
674
675 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
676 {
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;
683
684         memset(param, 0, param_len);
685         SSVAL(param,0,fnum);
686         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
687
688         data = flag ? 1 : 0;
689
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;
698         }
699
700         if (!cli_receive_trans(cli, SMBtrans2,
701                         &rparam, &param_len,
702                         &rdata, &data_len)) {
703                 return false;
704         }
705
706         SAFE_FREE(rdata);
707         SAFE_FREE(rparam);
708
709         return true;
710 }
711
712 /****************************************************************************
713  Open a file - exposing the full horror of the NT API :-).
714  Used in smbtorture.
715 ****************************************************************************/
716
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)
722 {
723         char *p;
724         int len;
725
726         memset(cli->outbuf,'\0',smb_size);
727         memset(cli->inbuf,'\0',smb_size);
728
729         cli_set_message(cli->outbuf,24,0, true);
730
731         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
732         SSVAL(cli->outbuf,smb_tid,cli->cnum);
733         cli_setup_packet(cli);
734
735         SSVAL(cli->outbuf,smb_vwv0,0xFF);
736         if (cli->use_oplocks)
737                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
738
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);
748
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);
759
760         cli_setup_bcc(cli, p);
761
762         cli_send_smb(cli);
763         if (!cli_receive_smb(cli)) {
764                 return -1;
765         }
766
767         if (cli_is_error(cli)) {
768                 return -1;
769         }
770
771         return SVAL(cli->inbuf,smb_vwv2 + 1);
772 }
773
774 struct async_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
775                                     struct event_context *ev,
776                                     struct cli_state *cli,
777                                     const char *fname,
778                                     uint32_t CreatFlags,
779                                     uint32_t DesiredAccess,
780                                     uint32_t FileAttributes,
781                                     uint32_t ShareAccess,
782                                     uint32_t CreateDisposition,
783                                     uint32_t CreateOptions,
784                                     uint8_t SecurityFlags)
785 {
786         struct async_req *result;
787         uint8_t *bytes;
788         size_t converted_len;
789         uint16_t vwv[24];
790
791         SCVAL(vwv+0, 0, 0xFF);
792         SCVAL(vwv+0, 1, 0);
793         SSVAL(vwv+1, 0, 0);
794         SCVAL(vwv+2, 0, 0);
795
796         if (cli->use_oplocks) {
797                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
798         }
799         SIVAL(vwv+3, 1, CreatFlags);
800         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
801         SIVAL(vwv+7, 1, DesiredAccess);
802         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
803         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
804         SIVAL(vwv+13, 1, FileAttributes);
805         SIVAL(vwv+15, 1, ShareAccess);
806         SIVAL(vwv+17, 1, CreateDisposition);
807         SIVAL(vwv+19, 1, CreateOptions);
808         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
809         SCVAL(vwv+23, 1, SecurityFlags);
810
811         bytes = talloc_array(talloc_tos(), uint8_t, 0);
812         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
813                                    fname, strlen(fname)+1,
814                                    &converted_len);
815
816         /* sigh. this copes with broken netapp filer behaviour */
817         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
818
819         if (bytes == NULL) {
820                 return NULL;
821         }
822
823         SIVAL(vwv+2, 1, converted_len);
824
825         result = cli_request_send(mem_ctx, ev, cli, SMBntcreateX, 0,
826                                   24, vwv, 0, talloc_get_size(bytes), bytes);
827         TALLOC_FREE(bytes);
828         return result;
829 }
830
831 NTSTATUS cli_ntcreate_recv(struct async_req *req, uint16_t *pfnum)
832 {
833         uint8_t wct;
834         uint16_t *vwv;
835         uint16_t num_bytes;
836         uint8_t *bytes;
837         NTSTATUS status;
838
839         if (async_req_is_nterror(req, &status)) {
840                 return status;
841         }
842
843         status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
844         if (!NT_STATUS_IS_OK(status)) {
845                 return status;
846         }
847
848         if (wct < 3) {
849                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
850         }
851
852         *pfnum = SVAL(vwv+2, 1);
853
854         return NT_STATUS_OK;
855 }
856
857 NTSTATUS cli_ntcreate(struct cli_state *cli,
858                       const char *fname,
859                       uint32_t CreatFlags,
860                       uint32_t DesiredAccess,
861                       uint32_t FileAttributes,
862                       uint32_t ShareAccess,
863                       uint32_t CreateDisposition,
864                       uint32_t CreateOptions,
865                       uint8_t SecurityFlags,
866                       uint16_t *pfid)
867 {
868         TALLOC_CTX *frame = talloc_stackframe();
869         struct event_context *ev;
870         struct async_req *req;
871         NTSTATUS status;
872
873         if (cli->fd_event != NULL) {
874                 /*
875                  * Can't use sync call while an async call is in flight
876                  */
877                 status = NT_STATUS_INVALID_PARAMETER;
878                 goto fail;
879         }
880
881         ev = event_context_init(frame);
882         if (ev == NULL) {
883                 status = NT_STATUS_NO_MEMORY;
884                 goto fail;
885         }
886
887         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
888                                 DesiredAccess, FileAttributes, ShareAccess,
889                                 CreateDisposition, CreateOptions,
890                                 SecurityFlags);
891         if (req == NULL) {
892                 status = NT_STATUS_NO_MEMORY;
893                 goto fail;
894         }
895
896         while (req->state < ASYNC_REQ_DONE) {
897                 event_loop_once(ev);
898         }
899
900         status = cli_ntcreate_recv(req, pfid);
901  fail:
902         TALLOC_FREE(frame);
903         return status;
904 }
905
906 /****************************************************************************
907  Open a file.
908 ****************************************************************************/
909
910 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
911 {
912         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
913                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
914 }
915
916 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
917                             const char *str, size_t str_len,
918                             size_t *pconverted_size)
919 {
920         size_t buflen;
921         char *converted;
922         size_t converted_size;
923
924         if (buf == NULL) {
925                 return NULL;
926         }
927
928         buflen = talloc_get_size(buf);
929         /*
930          * We're pushing into an SMB buffer, align odd
931          */
932         if (ucs2 && (buflen % 2 == 0)) {
933                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
934                 if (buf == NULL) {
935                         return NULL;
936                 }
937                 buf[buflen] = '\0';
938                 buflen += 1;
939         }
940
941         if (!convert_string_allocate(talloc_tos(), CH_UNIX,
942                                      ucs2 ? CH_UTF16LE : CH_DOS,
943                                      str, str_len, &converted,
944                                      &converted_size, true)) {
945                 return NULL;
946         }
947
948         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
949                                    buflen + converted_size);
950         if (buf == NULL) {
951                 TALLOC_FREE(converted);
952                 return NULL;
953         }
954
955         memcpy(buf + buflen, converted, converted_size);
956
957         TALLOC_FREE(converted);
958
959         if (pconverted_size) {
960                 *pconverted_size = converted_size;
961         }
962
963         return buf;
964 }
965
966 /****************************************************************************
967  Open a file
968  WARNING: if you open with O_WRONLY then getattrE won't work!
969 ****************************************************************************/
970
971 struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
972                                 struct cli_state *cli,
973                                 const char *fname, int flags, int share_mode)
974 {
975         unsigned openfn = 0;
976         unsigned accessmode = 0;
977         uint8_t additional_flags = 0;
978         uint8_t *bytes;
979         uint16_t vwv[15];
980         struct async_req *result;
981
982         if (flags & O_CREAT) {
983                 openfn |= (1<<4);
984         }
985         if (!(flags & O_EXCL)) {
986                 if (flags & O_TRUNC)
987                         openfn |= (1<<1);
988                 else
989                         openfn |= (1<<0);
990         }
991
992         accessmode = (share_mode<<4);
993
994         if ((flags & O_ACCMODE) == O_RDWR) {
995                 accessmode |= 2;
996         } else if ((flags & O_ACCMODE) == O_WRONLY) {
997                 accessmode |= 1;
998         }
999
1000 #if defined(O_SYNC)
1001         if ((flags & O_SYNC) == O_SYNC) {
1002                 accessmode |= (1<<14);
1003         }
1004 #endif /* O_SYNC */
1005
1006         if (share_mode == DENY_FCB) {
1007                 accessmode = 0xFF;
1008         }
1009
1010         SCVAL(vwv + 0, 0, 0xFF);
1011         SCVAL(vwv + 0, 1, 0);
1012         SSVAL(vwv + 1, 0, 0);
1013         SSVAL(vwv + 2, 0, 0);  /* no additional info */
1014         SSVAL(vwv + 3, 0, accessmode);
1015         SSVAL(vwv + 4, 0, aSYSTEM | aHIDDEN);
1016         SSVAL(vwv + 5, 0, 0);
1017         SIVAL(vwv + 6, 0, 0);
1018         SSVAL(vwv + 8, 0, openfn);
1019         SIVAL(vwv + 9, 0, 0);
1020         SIVAL(vwv + 11, 0, 0);
1021         SIVAL(vwv + 13, 0, 0);
1022
1023         if (cli->use_oplocks) {
1024                 /* if using oplocks then ask for a batch oplock via
1025                    core and extended methods */
1026                 additional_flags =
1027                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1028                 SSVAL(vwv+2, 0, SVAL(vwv+2, 0) | 6);
1029         }
1030
1031         bytes = talloc_array(talloc_tos(), uint8_t, 0);
1032         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1033                                    strlen(fname)+1, NULL);
1034         if (bytes == NULL) {
1035                 return NULL;
1036         }
1037
1038         result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
1039                                   15, vwv, 0, talloc_get_size(bytes), bytes);
1040         TALLOC_FREE(bytes);
1041         return result;
1042 }
1043
1044 NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
1045 {
1046         uint8_t wct;
1047         uint16_t *vwv;
1048         uint16_t num_bytes;
1049         uint8_t *bytes;
1050         NTSTATUS status;
1051
1052         if (async_req_is_nterror(req, &status)) {
1053                 return status;
1054         }
1055
1056         status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1057         if (!NT_STATUS_IS_OK(status)) {
1058                 return status;
1059         }
1060
1061         if (wct < 3) {
1062                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1063         }
1064
1065         *fnum = SVAL(vwv+2, 0);
1066
1067         return NT_STATUS_OK;
1068 }
1069
1070 int cli_open(struct cli_state *cli, const char *fname, int flags,
1071              int share_mode)
1072 {
1073         TALLOC_CTX *frame = talloc_stackframe();
1074         struct event_context *ev;
1075         struct async_req *req;
1076         int result = -1;
1077
1078         if (cli->fd_event != NULL) {
1079                 /*
1080                  * Can't use sync call while an async call is in flight
1081                  */
1082                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1083                 goto fail;
1084         }
1085
1086         ev = event_context_init(frame);
1087         if (ev == NULL) {
1088                 goto fail;
1089         }
1090
1091         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1092         if (req == NULL) {
1093                 goto fail;
1094         }
1095
1096         while (req->state < ASYNC_REQ_DONE) {
1097                 event_loop_once(ev);
1098         }
1099
1100         cli_open_recv(req, &result);
1101  fail:
1102         TALLOC_FREE(frame);
1103         return result;
1104 }
1105
1106 /****************************************************************************
1107  Close a file.
1108 ****************************************************************************/
1109
1110 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1111                                  struct cli_state *cli, int fnum)
1112 {
1113         uint16_t vwv[3];
1114
1115         SSVAL(vwv+0, 0, fnum);
1116         SIVALS(vwv+1, 0, -1);
1117
1118         return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
1119                                 0, NULL);
1120 }
1121
1122 NTSTATUS cli_close_recv(struct async_req *req)
1123 {
1124         uint8_t wct;
1125         uint16_t *vwv;
1126         uint16_t num_bytes;
1127         uint8_t *bytes;
1128         NTSTATUS status;
1129
1130         if (async_req_is_nterror(req, &status)) {
1131                 return status;
1132         }
1133
1134         return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1135 }
1136
1137 bool cli_close(struct cli_state *cli, int fnum)
1138 {
1139         TALLOC_CTX *frame = talloc_stackframe();
1140         struct event_context *ev;
1141         struct async_req *req;
1142         bool result = false;
1143
1144         if (cli->fd_event != NULL) {
1145                 /*
1146                  * Can't use sync call while an async call is in flight
1147                  */
1148                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1149                 goto fail;
1150         }
1151
1152         ev = event_context_init(frame);
1153         if (ev == NULL) {
1154                 goto fail;
1155         }
1156
1157         req = cli_close_send(frame, ev, cli, fnum);
1158         if (req == NULL) {
1159                 goto fail;
1160         }
1161
1162         while (req->state < ASYNC_REQ_DONE) {
1163                 event_loop_once(ev);
1164         }
1165
1166         result = NT_STATUS_IS_OK(cli_close_recv(req));
1167  fail:
1168         TALLOC_FREE(frame);
1169         return result;
1170 }
1171
1172 /****************************************************************************
1173  Truncate a file to a specified size
1174 ****************************************************************************/
1175
1176 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1177 {
1178         unsigned int param_len = 6;
1179         unsigned int data_len = 8;
1180         uint16 setup = TRANSACT2_SETFILEINFO;
1181         char param[6];
1182         unsigned char data[8];
1183         char *rparam=NULL, *rdata=NULL;
1184         int saved_timeout = cli->timeout;
1185
1186         SSVAL(param,0,fnum);
1187         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1188         SSVAL(param,4,0);
1189
1190         SBVAL(data, 0, size);
1191
1192         if (!cli_send_trans(cli, SMBtrans2,
1193                             NULL,                    /* name */
1194                             -1, 0,                   /* fid, flags */
1195                             &setup, 1, 0,            /* setup, length, max */
1196                             param, param_len, 2,     /* param, length, max */
1197                             (char *)&data,  data_len,/* data, length, ... */
1198                             cli->max_xmit)) {        /* ... max */
1199                 cli->timeout = saved_timeout;
1200                 return False;
1201         }
1202
1203         if (!cli_receive_trans(cli, SMBtrans2,
1204                                 &rparam, &param_len,
1205                                 &rdata, &data_len)) {
1206                 cli->timeout = saved_timeout;
1207                 SAFE_FREE(rdata);
1208                 SAFE_FREE(rparam);
1209                 return False;
1210         }
1211
1212         cli->timeout = saved_timeout;
1213
1214         SAFE_FREE(rdata);
1215         SAFE_FREE(rparam);
1216
1217         return True;
1218 }
1219
1220
1221 /****************************************************************************
1222  send a lock with a specified locktype
1223  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1224 ****************************************************************************/
1225
1226 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1227                       uint32 offset, uint32 len,
1228                       int timeout, unsigned char locktype)
1229 {
1230         char *p;
1231         int saved_timeout = cli->timeout;
1232
1233         memset(cli->outbuf,'\0',smb_size);
1234         memset(cli->inbuf,'\0', smb_size);
1235
1236         cli_set_message(cli->outbuf,8,0,True);
1237
1238         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1239         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1240         cli_setup_packet(cli);
1241
1242         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1243         SSVAL(cli->outbuf,smb_vwv2,fnum);
1244         SCVAL(cli->outbuf,smb_vwv3,locktype);
1245         SIVALS(cli->outbuf, smb_vwv4, timeout);
1246         SSVAL(cli->outbuf,smb_vwv6,0);
1247         SSVAL(cli->outbuf,smb_vwv7,1);
1248
1249         p = smb_buf(cli->outbuf);
1250         SSVAL(p, 0, cli->pid);
1251         SIVAL(p, 2, offset);
1252         SIVAL(p, 6, len);
1253
1254         p += 10;
1255
1256         cli_setup_bcc(cli, p);
1257
1258         cli_send_smb(cli);
1259
1260         if (timeout != 0) {
1261                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1262         }
1263
1264         if (!cli_receive_smb(cli)) {
1265                 cli->timeout = saved_timeout;
1266                 return NT_STATUS_UNSUCCESSFUL;
1267         }
1268
1269         cli->timeout = saved_timeout;
1270
1271         return cli_nt_error(cli);
1272 }
1273
1274 /****************************************************************************
1275  Lock a file.
1276  note that timeout is in units of 2 milliseconds
1277 ****************************************************************************/
1278
1279 bool cli_lock(struct cli_state *cli, int fnum,
1280               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1281 {
1282         char *p;
1283         int saved_timeout = cli->timeout;
1284
1285         memset(cli->outbuf,'\0',smb_size);
1286         memset(cli->inbuf,'\0', smb_size);
1287
1288         cli_set_message(cli->outbuf,8,0,True);
1289
1290         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1291         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1292         cli_setup_packet(cli);
1293
1294         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1295         SSVAL(cli->outbuf,smb_vwv2,fnum);
1296         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1297         SIVALS(cli->outbuf, smb_vwv4, timeout);
1298         SSVAL(cli->outbuf,smb_vwv6,0);
1299         SSVAL(cli->outbuf,smb_vwv7,1);
1300
1301         p = smb_buf(cli->outbuf);
1302         SSVAL(p, 0, cli->pid);
1303         SIVAL(p, 2, offset);
1304         SIVAL(p, 6, len);
1305
1306         p += 10;
1307
1308         cli_setup_bcc(cli, p);
1309
1310         cli_send_smb(cli);
1311
1312         if (timeout != 0) {
1313                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1314         }
1315
1316         if (!cli_receive_smb(cli)) {
1317                 cli->timeout = saved_timeout;
1318                 return False;
1319         }
1320
1321         cli->timeout = saved_timeout;
1322
1323         if (cli_is_error(cli)) {
1324                 return False;
1325         }
1326
1327         return True;
1328 }
1329
1330 /****************************************************************************
1331  Unlock a file.
1332 ****************************************************************************/
1333
1334 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1335 {
1336         char *p;
1337
1338         memset(cli->outbuf,'\0',smb_size);
1339         memset(cli->inbuf,'\0',smb_size);
1340
1341         cli_set_message(cli->outbuf,8,0,True);
1342
1343         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1344         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1345         cli_setup_packet(cli);
1346
1347         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1348         SSVAL(cli->outbuf,smb_vwv2,fnum);
1349         SCVAL(cli->outbuf,smb_vwv3,0);
1350         SIVALS(cli->outbuf, smb_vwv4, 0);
1351         SSVAL(cli->outbuf,smb_vwv6,1);
1352         SSVAL(cli->outbuf,smb_vwv7,0);
1353
1354         p = smb_buf(cli->outbuf);
1355         SSVAL(p, 0, cli->pid);
1356         SIVAL(p, 2, offset);
1357         SIVAL(p, 6, len);
1358         p += 10;
1359         cli_setup_bcc(cli, p);
1360         cli_send_smb(cli);
1361         if (!cli_receive_smb(cli)) {
1362                 return False;
1363         }
1364
1365         if (cli_is_error(cli)) {
1366                 return False;
1367         }
1368
1369         return True;
1370 }
1371
1372 /****************************************************************************
1373  Lock a file with 64 bit offsets.
1374 ****************************************************************************/
1375
1376 bool cli_lock64(struct cli_state *cli, int fnum,
1377                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1378 {
1379         char *p;
1380         int saved_timeout = cli->timeout;
1381         int ltype;
1382
1383         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1384                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1385         }
1386
1387         ltype = (lock_type == READ_LOCK? 1 : 0);
1388         ltype |= LOCKING_ANDX_LARGE_FILES;
1389
1390         memset(cli->outbuf,'\0',smb_size);
1391         memset(cli->inbuf,'\0', smb_size);
1392
1393         cli_set_message(cli->outbuf,8,0,True);
1394
1395         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1396         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1397         cli_setup_packet(cli);
1398
1399         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1400         SSVAL(cli->outbuf,smb_vwv2,fnum);
1401         SCVAL(cli->outbuf,smb_vwv3,ltype);
1402         SIVALS(cli->outbuf, smb_vwv4, timeout);
1403         SSVAL(cli->outbuf,smb_vwv6,0);
1404         SSVAL(cli->outbuf,smb_vwv7,1);
1405
1406         p = smb_buf(cli->outbuf);
1407         SIVAL(p, 0, cli->pid);
1408         SOFF_T_R(p, 4, offset);
1409         SOFF_T_R(p, 12, len);
1410         p += 20;
1411
1412         cli_setup_bcc(cli, p);
1413         cli_send_smb(cli);
1414
1415         if (timeout != 0) {
1416                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1417         }
1418
1419         if (!cli_receive_smb(cli)) {
1420                 cli->timeout = saved_timeout;
1421                 return False;
1422         }
1423
1424         cli->timeout = saved_timeout;
1425
1426         if (cli_is_error(cli)) {
1427                 return False;
1428         }
1429
1430         return True;
1431 }
1432
1433 /****************************************************************************
1434  Unlock a file with 64 bit offsets.
1435 ****************************************************************************/
1436
1437 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1438 {
1439         char *p;
1440
1441         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1442                 return cli_unlock(cli, fnum, offset, len);
1443         }
1444
1445         memset(cli->outbuf,'\0',smb_size);
1446         memset(cli->inbuf,'\0',smb_size);
1447
1448         cli_set_message(cli->outbuf,8,0,True);
1449
1450         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1451         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1452         cli_setup_packet(cli);
1453
1454         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1455         SSVAL(cli->outbuf,smb_vwv2,fnum);
1456         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1457         SIVALS(cli->outbuf, smb_vwv4, 0);
1458         SSVAL(cli->outbuf,smb_vwv6,1);
1459         SSVAL(cli->outbuf,smb_vwv7,0);
1460
1461         p = smb_buf(cli->outbuf);
1462         SIVAL(p, 0, cli->pid);
1463         SOFF_T_R(p, 4, offset);
1464         SOFF_T_R(p, 12, len);
1465         p += 20;
1466         cli_setup_bcc(cli, p);
1467         cli_send_smb(cli);
1468         if (!cli_receive_smb(cli)) {
1469                 return False;
1470         }
1471
1472         if (cli_is_error(cli)) {
1473                 return False;
1474         }
1475
1476         return True;
1477 }
1478
1479 /****************************************************************************
1480  Get/unlock a POSIX lock on a file - internal function.
1481 ****************************************************************************/
1482
1483 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1484                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1485 {
1486         unsigned int param_len = 4;
1487         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1488         uint16 setup = TRANSACT2_SETFILEINFO;
1489         char param[4];
1490         unsigned char data[POSIX_LOCK_DATA_SIZE];
1491         char *rparam=NULL, *rdata=NULL;
1492         int saved_timeout = cli->timeout;
1493
1494         SSVAL(param,0,fnum);
1495         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1496
1497         switch (lock_type) {
1498                 case READ_LOCK:
1499                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1500                         break;
1501                 case WRITE_LOCK:
1502                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1503                         break;
1504                 case UNLOCK_LOCK:
1505                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1506                         break;
1507                 default:
1508                         return False;
1509         }
1510
1511         if (wait_lock) {
1512                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1513                 cli->timeout = 0x7FFFFFFF;
1514         } else {
1515                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1516         }
1517
1518         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1519         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1520         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1521
1522         if (!cli_send_trans(cli, SMBtrans2,
1523                         NULL,                        /* name */
1524                         -1, 0,                          /* fid, flags */
1525                         &setup, 1, 0,                   /* setup, length, max */
1526                         param, param_len, 2,            /* param, length, max */
1527                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1528                         )) {
1529                 cli->timeout = saved_timeout;
1530                 return False;
1531         }
1532
1533         if (!cli_receive_trans(cli, SMBtrans2,
1534                                 &rparam, &param_len,
1535                                 &rdata, &data_len)) {
1536                 cli->timeout = saved_timeout;
1537                 SAFE_FREE(rdata);
1538                 SAFE_FREE(rparam);
1539                 return False;
1540         }
1541
1542         cli->timeout = saved_timeout;
1543
1544         SAFE_FREE(rdata);
1545         SAFE_FREE(rparam);
1546
1547         return True;
1548 }
1549
1550 /****************************************************************************
1551  POSIX Lock a file.
1552 ****************************************************************************/
1553
1554 bool cli_posix_lock(struct cli_state *cli, int fnum,
1555                         uint64_t offset, uint64_t len,
1556                         bool wait_lock, enum brl_type lock_type)
1557 {
1558         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1559                 return False;
1560         }
1561         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1562 }
1563
1564 /****************************************************************************
1565  POSIX Unlock a file.
1566 ****************************************************************************/
1567
1568 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1569 {
1570         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1571 }
1572
1573 /****************************************************************************
1574  POSIX Get any lock covering a file.
1575 ****************************************************************************/
1576
1577 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1578 {
1579         return True;
1580 }
1581
1582 /****************************************************************************
1583  Do a SMBgetattrE call.
1584 ****************************************************************************/
1585
1586 bool cli_getattrE(struct cli_state *cli, int fd,
1587                   uint16 *attr, SMB_OFF_T *size,
1588                   time_t *change_time,
1589                   time_t *access_time,
1590                   time_t *write_time)
1591 {
1592         memset(cli->outbuf,'\0',smb_size);
1593         memset(cli->inbuf,'\0',smb_size);
1594
1595         cli_set_message(cli->outbuf,1,0,True);
1596
1597         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1598         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1599         cli_setup_packet(cli);
1600
1601         SSVAL(cli->outbuf,smb_vwv0,fd);
1602
1603         cli_send_smb(cli);
1604         if (!cli_receive_smb(cli)) {
1605                 return False;
1606         }
1607
1608         if (cli_is_error(cli)) {
1609                 return False;
1610         }
1611
1612         if (size) {
1613                 *size = IVAL(cli->inbuf, smb_vwv6);
1614         }
1615
1616         if (attr) {
1617                 *attr = SVAL(cli->inbuf,smb_vwv10);
1618         }
1619
1620         if (change_time) {
1621                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1622         }
1623
1624         if (access_time) {
1625                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1626         }
1627
1628         if (write_time) {
1629                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1630         }
1631
1632         return True;
1633 }
1634
1635 /****************************************************************************
1636  Do a SMBgetatr call
1637 ****************************************************************************/
1638
1639 bool cli_getatr(struct cli_state *cli, const char *fname,
1640                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1641 {
1642         char *p;
1643
1644         memset(cli->outbuf,'\0',smb_size);
1645         memset(cli->inbuf,'\0',smb_size);
1646
1647         cli_set_message(cli->outbuf,0,0,True);
1648
1649         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1650         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1651         cli_setup_packet(cli);
1652
1653         p = smb_buf(cli->outbuf);
1654         *p++ = 4;
1655         p += clistr_push(cli, p, fname,
1656                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1657
1658         cli_setup_bcc(cli, p);
1659
1660         cli_send_smb(cli);
1661         if (!cli_receive_smb(cli)) {
1662                 return False;
1663         }
1664
1665         if (cli_is_error(cli)) {
1666                 return False;
1667         }
1668
1669         if (size) {
1670                 *size = IVAL(cli->inbuf, smb_vwv3);
1671         }
1672
1673         if (write_time) {
1674                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1675         }
1676
1677         if (attr) {
1678                 *attr = SVAL(cli->inbuf,smb_vwv0);
1679         }
1680
1681         return True;
1682 }
1683
1684 /****************************************************************************
1685  Do a SMBsetattrE call.
1686 ****************************************************************************/
1687
1688 bool cli_setattrE(struct cli_state *cli, int fd,
1689                   time_t change_time,
1690                   time_t access_time,
1691                   time_t write_time)
1692
1693 {
1694         char *p;
1695
1696         memset(cli->outbuf,'\0',smb_size);
1697         memset(cli->inbuf,'\0',smb_size);
1698
1699         cli_set_message(cli->outbuf,7,0,True);
1700
1701         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1702         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1703         cli_setup_packet(cli);
1704
1705         SSVAL(cli->outbuf,smb_vwv0, fd);
1706         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1707         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1708         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1709
1710         p = smb_buf(cli->outbuf);
1711         *p++ = 4;
1712
1713         cli_setup_bcc(cli, p);
1714
1715         cli_send_smb(cli);
1716         if (!cli_receive_smb(cli)) {
1717                 return False;
1718         }
1719
1720         if (cli_is_error(cli)) {
1721                 return False;
1722         }
1723
1724         return True;
1725 }
1726
1727 /****************************************************************************
1728  Do a SMBsetatr call.
1729 ****************************************************************************/
1730
1731 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1732 {
1733         char *p;
1734
1735         memset(cli->outbuf,'\0',smb_size);
1736         memset(cli->inbuf,'\0',smb_size);
1737
1738         cli_set_message(cli->outbuf,8,0,True);
1739
1740         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1741         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1742         cli_setup_packet(cli);
1743
1744         SSVAL(cli->outbuf,smb_vwv0, attr);
1745         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1746
1747         p = smb_buf(cli->outbuf);
1748         *p++ = 4;
1749         p += clistr_push(cli, p, fname,
1750                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1751         *p++ = 4;
1752
1753         cli_setup_bcc(cli, p);
1754
1755         cli_send_smb(cli);
1756         if (!cli_receive_smb(cli)) {
1757                 return False;
1758         }
1759
1760         if (cli_is_error(cli)) {
1761                 return False;
1762         }
1763
1764         return True;
1765 }
1766
1767 /****************************************************************************
1768  Check for existance of a dir.
1769 ****************************************************************************/
1770
1771 bool cli_chkpath(struct cli_state *cli, const char *path)
1772 {
1773         char *path2 = NULL;
1774         char *p;
1775         TALLOC_CTX *frame = talloc_stackframe();
1776
1777         path2 = talloc_strdup(frame, path);
1778         if (!path2) {
1779                 TALLOC_FREE(frame);
1780                 return false;
1781         }
1782         trim_char(path2,'\0','\\');
1783         if (!*path2) {
1784                 path2 = talloc_strdup(frame, "\\");
1785                 if (!path2) {
1786                         TALLOC_FREE(frame);
1787                         return false;
1788                 }
1789         }
1790
1791         memset(cli->outbuf,'\0',smb_size);
1792         cli_set_message(cli->outbuf,0,0,True);
1793         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1794         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1795         cli_setup_packet(cli);
1796         p = smb_buf(cli->outbuf);
1797         *p++ = 4;
1798         p += clistr_push(cli, p, path2,
1799                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1800
1801         cli_setup_bcc(cli, p);
1802
1803         cli_send_smb(cli);
1804         if (!cli_receive_smb(cli)) {
1805                 TALLOC_FREE(frame);
1806                 return False;
1807         }
1808
1809         TALLOC_FREE(frame);
1810
1811         if (cli_is_error(cli)) return False;
1812
1813         return True;
1814 }
1815
1816 /****************************************************************************
1817  Query disk space.
1818 ****************************************************************************/
1819
1820 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1821 {
1822         memset(cli->outbuf,'\0',smb_size);
1823         cli_set_message(cli->outbuf,0,0,True);
1824         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1825         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1826         cli_setup_packet(cli);
1827
1828         cli_send_smb(cli);
1829         if (!cli_receive_smb(cli)) {
1830                 return False;
1831         }
1832
1833         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1834         *total = SVAL(cli->inbuf,smb_vwv0);
1835         *avail = SVAL(cli->inbuf,smb_vwv3);
1836
1837         return True;
1838 }
1839
1840 /****************************************************************************
1841  Create and open a temporary file.
1842 ****************************************************************************/
1843
1844 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1845 {
1846         int len;
1847         char *p;
1848
1849         memset(cli->outbuf,'\0',smb_size);
1850         memset(cli->inbuf,'\0',smb_size);
1851
1852         cli_set_message(cli->outbuf,3,0,True);
1853
1854         SCVAL(cli->outbuf,smb_com,SMBctemp);
1855         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1856         cli_setup_packet(cli);
1857
1858         SSVAL(cli->outbuf,smb_vwv0,0);
1859         SIVALS(cli->outbuf,smb_vwv1,-1);
1860
1861         p = smb_buf(cli->outbuf);
1862         *p++ = 4;
1863         p += clistr_push(cli, p, path,
1864                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1865
1866         cli_setup_bcc(cli, p);
1867
1868         cli_send_smb(cli);
1869         if (!cli_receive_smb(cli)) {
1870                 return -1;
1871         }
1872
1873         if (cli_is_error(cli)) {
1874                 return -1;
1875         }
1876
1877         /* despite the spec, the result has a -1, followed by
1878            length, followed by name */
1879         p = smb_buf(cli->inbuf);
1880         p += 4;
1881         len = smb_buflen(cli->inbuf) - 4;
1882         if (len <= 0 || len > PATH_MAX) return -1;
1883
1884         if (tmp_path) {
1885                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1886                 if (!path2) {
1887                         return -1;
1888                 }
1889                 clistr_pull(cli->inbuf, path2, p,
1890                             len+1, len, STR_ASCII);
1891                 *tmp_path = path2;
1892         }
1893
1894         return SVAL(cli->inbuf,smb_vwv0);
1895 }
1896
1897 /*
1898    send a raw ioctl - used by the torture code
1899 */
1900 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1901 {
1902         memset(cli->outbuf,'\0',smb_size);
1903         memset(cli->inbuf,'\0',smb_size);
1904
1905         cli_set_message(cli->outbuf, 3, 0, True);
1906         SCVAL(cli->outbuf,smb_com,SMBioctl);
1907         cli_setup_packet(cli);
1908
1909         SSVAL(cli->outbuf, smb_vwv0, fnum);
1910         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1911         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1912
1913         cli_send_smb(cli);
1914         if (!cli_receive_smb(cli)) {
1915                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1916         }
1917
1918         if (cli_is_error(cli)) {
1919                 return cli_nt_error(cli);
1920         }
1921
1922         *blob = data_blob_null;
1923
1924         return NT_STATUS_OK;
1925 }
1926
1927 /*********************************************************
1928  Set an extended attribute utility fn.
1929 *********************************************************/
1930
1931 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1932                         const char *ea_name, const char *ea_val, size_t ea_len)
1933 {
1934         unsigned int data_len = 0;
1935         char *data = NULL;
1936         char *rparam=NULL, *rdata=NULL;
1937         char *p;
1938         size_t ea_namelen = strlen(ea_name);
1939
1940         if (ea_namelen == 0 && ea_len == 0) {
1941                 data_len = 4;
1942                 data = (char *)SMB_MALLOC(data_len);
1943                 if (!data) {
1944                         return False;
1945                 }
1946                 p = data;
1947                 SIVAL(p,0,data_len);
1948         } else {
1949                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1950                 data = (char *)SMB_MALLOC(data_len);
1951                 if (!data) {
1952                         return False;
1953                 }
1954                 p = data;
1955                 SIVAL(p,0,data_len);
1956                 p += 4;
1957                 SCVAL(p, 0, 0); /* EA flags. */
1958                 SCVAL(p, 1, ea_namelen);
1959                 SSVAL(p, 2, ea_len);
1960                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1961                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1962         }
1963
1964         if (!cli_send_trans(cli, SMBtrans2,
1965                         NULL,                        /* name */
1966                         -1, 0,                          /* fid, flags */
1967                         &setup, 1, 0,                   /* setup, length, max */
1968                         param, param_len, 2,            /* param, length, max */
1969                         data,  data_len, cli->max_xmit /* data, length, max */
1970                         )) {
1971                 SAFE_FREE(data);
1972                 return False;
1973         }
1974
1975         if (!cli_receive_trans(cli, SMBtrans2,
1976                         &rparam, &param_len,
1977                         &rdata, &data_len)) {
1978                         SAFE_FREE(data);
1979                 return false;
1980         }
1981
1982         SAFE_FREE(data);
1983         SAFE_FREE(rdata);
1984         SAFE_FREE(rparam);
1985
1986         return True;
1987 }
1988
1989 /*********************************************************
1990  Set an extended attribute on a pathname.
1991 *********************************************************/
1992
1993 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1994 {
1995         uint16 setup = TRANSACT2_SETPATHINFO;
1996         unsigned int param_len = 0;
1997         char *param;
1998         size_t srclen = 2*(strlen(path)+1);
1999         char *p;
2000         bool ret;
2001
2002         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2003         if (!param) {
2004                 return false;
2005         }
2006         memset(param, '\0', 6);
2007         SSVAL(param,0,SMB_INFO_SET_EA);
2008         p = &param[6];
2009
2010         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2011         param_len = PTR_DIFF(p, param);
2012
2013         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2014         SAFE_FREE(param);
2015         return ret;
2016 }
2017
2018 /*********************************************************
2019  Set an extended attribute on an fnum.
2020 *********************************************************/
2021
2022 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2023 {
2024         char param[6];
2025         uint16 setup = TRANSACT2_SETFILEINFO;
2026
2027         memset(param, 0, 6);
2028         SSVAL(param,0,fnum);
2029         SSVAL(param,2,SMB_INFO_SET_EA);
2030
2031         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2032 }
2033
2034 /*********************************************************
2035  Get an extended attribute list utility fn.
2036 *********************************************************/
2037
2038 static bool cli_get_ea_list(struct cli_state *cli,
2039                 uint16 setup, char *param, unsigned int param_len,
2040                 TALLOC_CTX *ctx,
2041                 size_t *pnum_eas,
2042                 struct ea_struct **pea_list)
2043 {
2044         unsigned int data_len = 0;
2045         unsigned int rparam_len, rdata_len;
2046         char *rparam=NULL, *rdata=NULL;
2047         char *p;
2048         size_t ea_size;
2049         size_t num_eas;
2050         bool ret = False;
2051         struct ea_struct *ea_list;
2052
2053         *pnum_eas = 0;
2054         if (pea_list) {
2055                 *pea_list = NULL;
2056         }
2057
2058         if (!cli_send_trans(cli, SMBtrans2,
2059                         NULL,           /* Name */
2060                         -1, 0,          /* fid, flags */
2061                         &setup, 1, 0,   /* setup, length, max */
2062                         param, param_len, 10, /* param, length, max */
2063                         NULL, data_len, cli->max_xmit /* data, length, max */
2064                                 )) {
2065                 return False;
2066         }
2067
2068         if (!cli_receive_trans(cli, SMBtrans2,
2069                         &rparam, &rparam_len,
2070                         &rdata, &rdata_len)) {
2071                 return False;
2072         }
2073
2074         if (!rdata || rdata_len < 4) {
2075                 goto out;
2076         }
2077
2078         ea_size = (size_t)IVAL(rdata,0);
2079         if (ea_size > rdata_len) {
2080                 goto out;
2081         }
2082
2083         if (ea_size == 0) {
2084                 /* No EA's present. */
2085                 ret = True;
2086                 goto out;
2087         }
2088
2089         p = rdata + 4;
2090         ea_size -= 4;
2091
2092         /* Validate the EA list and count it. */
2093         for (num_eas = 0; ea_size >= 4; num_eas++) {
2094                 unsigned int ea_namelen = CVAL(p,1);
2095                 unsigned int ea_valuelen = SVAL(p,2);
2096                 if (ea_namelen == 0) {
2097                         goto out;
2098                 }
2099                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2100                         goto out;
2101                 }
2102                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2103                 p += 4 + ea_namelen + 1 + ea_valuelen;
2104         }
2105
2106         if (num_eas == 0) {
2107                 ret = True;
2108                 goto out;
2109         }
2110
2111         *pnum_eas = num_eas;
2112         if (!pea_list) {
2113                 /* Caller only wants number of EA's. */
2114                 ret = True;
2115                 goto out;
2116         }
2117
2118         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2119         if (!ea_list) {
2120                 goto out;
2121         }
2122
2123         ea_size = (size_t)IVAL(rdata,0);
2124         p = rdata + 4;
2125
2126         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2127                 struct ea_struct *ea = &ea_list[num_eas];
2128                 fstring unix_ea_name;
2129                 unsigned int ea_namelen = CVAL(p,1);
2130                 unsigned int ea_valuelen = SVAL(p,2);
2131
2132                 ea->flags = CVAL(p,0);
2133                 unix_ea_name[0] = '\0';
2134                 pull_ascii_fstring(unix_ea_name, p + 4);
2135                 ea->name = talloc_strdup(ctx, unix_ea_name);
2136                 /* Ensure the value is null terminated (in case it's a string). */
2137                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2138                 if (!ea->value.data) {
2139                         goto out;
2140                 }
2141                 if (ea_valuelen) {
2142                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2143                 }
2144                 ea->value.data[ea_valuelen] = 0;
2145                 ea->value.length--;
2146                 p += 4 + ea_namelen + 1 + ea_valuelen;
2147         }
2148
2149         *pea_list = ea_list;
2150         ret = True;
2151
2152  out :
2153
2154         SAFE_FREE(rdata);
2155         SAFE_FREE(rparam);
2156         return ret;
2157 }
2158
2159 /*********************************************************
2160  Get an extended attribute list from a pathname.
2161 *********************************************************/
2162
2163 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2164                 TALLOC_CTX *ctx,
2165                 size_t *pnum_eas,
2166                 struct ea_struct **pea_list)
2167 {
2168         uint16 setup = TRANSACT2_QPATHINFO;
2169         unsigned int param_len = 0;
2170         char *param;
2171         char *p;
2172         size_t srclen = 2*(strlen(path)+1);
2173         bool ret;
2174
2175         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2176         if (!param) {
2177                 return false;
2178         }
2179         p = param;
2180         memset(p, 0, 6);
2181         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2182         p += 6;
2183         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2184         param_len = PTR_DIFF(p, param);
2185
2186         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2187         SAFE_FREE(param);
2188         return ret;
2189 }
2190
2191 /*********************************************************
2192  Get an extended attribute list from an fnum.
2193 *********************************************************/
2194
2195 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2196                 TALLOC_CTX *ctx,
2197                 size_t *pnum_eas,
2198                 struct ea_struct **pea_list)
2199 {
2200         uint16 setup = TRANSACT2_QFILEINFO;
2201         char param[6];
2202
2203         memset(param, 0, 6);
2204         SSVAL(param,0,fnum);
2205         SSVAL(param,2,SMB_INFO_SET_EA);
2206
2207         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2208 }
2209
2210 /****************************************************************************
2211  Convert open "flags" arg to uint32 on wire.
2212 ****************************************************************************/
2213
2214 static uint32 open_flags_to_wire(int flags)
2215 {
2216         int open_mode = flags & O_ACCMODE;
2217         uint32 ret = 0;
2218
2219         switch (open_mode) {
2220                 case O_WRONLY:
2221                         ret |= SMB_O_WRONLY;
2222                         break;
2223                 case O_RDWR:
2224                         ret |= SMB_O_RDWR;
2225                         break;
2226                 default:
2227                 case O_RDONLY:
2228                         ret |= SMB_O_RDONLY;
2229                         break;
2230         }
2231
2232         if (flags & O_CREAT) {
2233                 ret |= SMB_O_CREAT;
2234         }
2235         if (flags & O_EXCL) {
2236                 ret |= SMB_O_EXCL;
2237         }
2238         if (flags & O_TRUNC) {
2239                 ret |= SMB_O_TRUNC;
2240         }
2241 #if defined(O_SYNC)
2242         if (flags & O_SYNC) {
2243                 ret |= SMB_O_SYNC;
2244         }
2245 #endif /* O_SYNC */
2246         if (flags & O_APPEND) {
2247                 ret |= SMB_O_APPEND;
2248         }
2249 #if defined(O_DIRECT)
2250         if (flags & O_DIRECT) {
2251                 ret |= SMB_O_DIRECT;
2252         }
2253 #endif
2254 #if defined(O_DIRECTORY)
2255         if (flags & O_DIRECTORY) {
2256                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2257                 ret |= SMB_O_DIRECTORY;
2258         }
2259 #endif
2260         return ret;
2261 }
2262
2263 /****************************************************************************
2264  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2265 ****************************************************************************/
2266
2267 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2268 {
2269         unsigned int data_len = 0;
2270         unsigned int param_len = 0;
2271         uint16 setup = TRANSACT2_SETPATHINFO;
2272         char *param;
2273         char data[18];
2274         char *rparam=NULL, *rdata=NULL;
2275         char *p;
2276         int fnum = -1;
2277         uint32 wire_flags = open_flags_to_wire(flags);
2278         size_t srclen = 2*(strlen(fname)+1);
2279
2280         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2281         if (!param) {
2282                 return false;
2283         }
2284         memset(param, '\0', 6);
2285         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2286         p = &param[6];
2287
2288         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2289         param_len = PTR_DIFF(p, param);
2290
2291         if (is_dir) {
2292                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2293                 wire_flags |= SMB_O_DIRECTORY;
2294         }
2295
2296         p = data;
2297         SIVAL(p,0,0); /* No oplock. */
2298         SIVAL(p,4,wire_flags);
2299         SIVAL(p,8,unix_perms_to_wire(mode));
2300         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2301         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2302
2303         data_len = 18;
2304
2305         if (!cli_send_trans(cli, SMBtrans2,
2306                         NULL,                        /* name */
2307                         -1, 0,                          /* fid, flags */
2308                         &setup, 1, 0,                   /* setup, length, max */
2309                         param, param_len, 2,            /* param, length, max */
2310                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2311                         )) {
2312                 SAFE_FREE(param);
2313                 return -1;
2314         }
2315
2316         SAFE_FREE(param);
2317
2318         if (!cli_receive_trans(cli, SMBtrans2,
2319                 &rparam, &param_len,
2320                 &rdata, &data_len)) {
2321                         return -1;
2322         }
2323
2324         fnum = SVAL(rdata,2);
2325
2326         SAFE_FREE(rdata);
2327         SAFE_FREE(rparam);
2328
2329         return fnum;
2330 }
2331
2332 /****************************************************************************
2333  open - POSIX semantics.
2334 ****************************************************************************/
2335
2336 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2337 {
2338         return cli_posix_open_internal(cli, fname, flags, mode, False);
2339 }
2340
2341 /****************************************************************************
2342  mkdir - POSIX semantics.
2343 ****************************************************************************/
2344
2345 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2346 {
2347         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2348 }
2349
2350 /****************************************************************************
2351  unlink or rmdir - POSIX semantics.
2352 ****************************************************************************/
2353
2354 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2355 {
2356         unsigned int data_len = 0;
2357         unsigned int param_len = 0;
2358         uint16 setup = TRANSACT2_SETPATHINFO;
2359         char *param;
2360         char data[2];
2361         char *rparam=NULL, *rdata=NULL;
2362         char *p;
2363         size_t srclen = 2*(strlen(fname)+1);
2364
2365         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2366         if (!param) {
2367                 return false;
2368         }
2369         memset(param, '\0', 6);
2370         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2371         p = &param[6];
2372
2373         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2374         param_len = PTR_DIFF(p, param);
2375
2376         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2377                         SMB_POSIX_UNLINK_FILE_TARGET);
2378         data_len = 2;
2379
2380         if (!cli_send_trans(cli, SMBtrans2,
2381                         NULL,                        /* name */
2382                         -1, 0,                          /* fid, flags */
2383                         &setup, 1, 0,                   /* setup, length, max */
2384                         param, param_len, 2,            /* param, length, max */
2385                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2386                         )) {
2387                 SAFE_FREE(param);
2388                 return False;
2389         }
2390
2391         SAFE_FREE(param);
2392
2393         if (!cli_receive_trans(cli, SMBtrans2,
2394                 &rparam, &param_len,
2395                 &rdata, &data_len)) {
2396                         return False;
2397         }
2398
2399         SAFE_FREE(rdata);
2400         SAFE_FREE(rparam);
2401
2402         return True;
2403 }
2404
2405 /****************************************************************************
2406  unlink - POSIX semantics.
2407 ****************************************************************************/
2408
2409 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2410 {
2411         return cli_posix_unlink_internal(cli, fname, False);
2412 }
2413
2414 /****************************************************************************
2415  rmdir - POSIX semantics.
2416 ****************************************************************************/
2417
2418 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2419 {
2420         return cli_posix_unlink_internal(cli, fname, True);
2421 }