Remove cli->event_ctx, pass it explicitly
[nivanova/samba-autobuild/.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                         (char *)&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 /****************************************************************************
775  Open a file.
776 ****************************************************************************/
777
778 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
779 {
780         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
781                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
782 }
783
784 static uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
785 {
786         size_t buflen = talloc_get_size(buf);
787         char *converted;
788         size_t converted_size;
789
790         /*
791          * We're pushing into an SMB buffer, align odd
792          */
793         if (ucs2 && (buflen % 2 == 0)) {
794                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
795                 if (buf == NULL) {
796                         return NULL;
797                 }
798                 buf[buflen] = '\0';
799                 buflen += 1;
800         }
801
802         if (!convert_string_allocate(talloc_tos(), CH_UNIX,
803                                      ucs2 ? CH_UTF16LE : CH_DOS,
804                                      str, strlen(str)+1, &converted,
805                                      &converted_size, true)) {
806                 return NULL;
807         }
808
809         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
810                                    buflen + converted_size);
811         if (buf == NULL) {
812                 return NULL;
813         }
814
815         memcpy(buf + buflen, converted, converted_size);
816
817         TALLOC_FREE(converted);
818         return buf;
819 }
820
821 /****************************************************************************
822  Open a file
823  WARNING: if you open with O_WRONLY then getattrE won't work!
824 ****************************************************************************/
825
826 struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
827                                 struct cli_state *cli,
828                                 const char *fname, int flags, int share_mode)
829 {
830         unsigned openfn = 0;
831         unsigned accessmode = 0;
832         uint8_t additional_flags = 0;
833         uint8_t *bytes;
834         uint16_t vwv[15];
835         struct async_req *result;
836
837         if (flags & O_CREAT) {
838                 openfn |= (1<<4);
839         }
840         if (!(flags & O_EXCL)) {
841                 if (flags & O_TRUNC)
842                         openfn |= (1<<1);
843                 else
844                         openfn |= (1<<0);
845         }
846
847         accessmode = (share_mode<<4);
848
849         if ((flags & O_ACCMODE) == O_RDWR) {
850                 accessmode |= 2;
851         } else if ((flags & O_ACCMODE) == O_WRONLY) {
852                 accessmode |= 1;
853         }
854
855 #if defined(O_SYNC)
856         if ((flags & O_SYNC) == O_SYNC) {
857                 accessmode |= (1<<14);
858         }
859 #endif /* O_SYNC */
860
861         if (share_mode == DENY_FCB) {
862                 accessmode = 0xFF;
863         }
864
865         SCVAL(vwv + 0, 0, 0xFF);
866         SCVAL(vwv + 0, 1, 0);
867         SSVAL(vwv + 1, 0, 0);
868         SSVAL(vwv + 2, 0, 0);  /* no additional info */
869         SSVAL(vwv + 3, 0, accessmode);
870         SSVAL(vwv + 4, 0, aSYSTEM | aHIDDEN);
871         SSVAL(vwv + 5, 0, 0);
872         SIVAL(vwv + 6, 0, 0);
873         SSVAL(vwv + 8, 0, openfn);
874         SIVAL(vwv + 9, 0, 0);
875         SIVAL(vwv + 11, 0, 0);
876         SIVAL(vwv + 13, 0, 0);
877
878         if (cli->use_oplocks) {
879                 /* if using oplocks then ask for a batch oplock via
880                    core and extended methods */
881                 additional_flags =
882                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
883                 SSVAL(vwv+2, 0, SVAL(vwv+2, 0) | 6);
884         }
885
886         bytes = talloc_array(talloc_tos(), uint8_t, 0);
887         if (bytes == NULL) {
888                 return NULL;
889         }
890
891         bytes = smb_bytes_push_str(
892                 bytes, (cli->capabilities & CAP_UNICODE) != 0, fname);
893         if (bytes == NULL) {
894                 return NULL;
895         }
896
897         result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
898                                   15, vwv, talloc_get_size(bytes), bytes);
899         TALLOC_FREE(bytes);
900         return result;
901 }
902
903 NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
904 {
905         struct cli_request *cli_req = cli_request_get(req);
906         NTSTATUS status;
907
908         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
909         if (req->state == ASYNC_REQ_ERROR) {
910                 return req->status;
911         }
912
913         status = cli_pull_error(cli_req->inbuf);
914         if (!NT_STATUS_IS_OK(status)) {
915                 return status;
916         }
917
918         *fnum = SVAL(cli_req->inbuf, smb_vwv2);
919
920         return NT_STATUS_OK;
921 }
922
923 int cli_open(struct cli_state *cli, const char *fname, int flags,
924              int share_mode)
925 {
926         TALLOC_CTX *frame = talloc_stackframe();
927         struct event_context *ev;
928         struct async_req *req;
929         int result = -1;
930
931         if (cli->fd_event != NULL) {
932                 /*
933                  * Can't use sync call while an async call is in flight
934                  */
935                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
936                 goto fail;
937         }
938
939         ev = event_context_init(frame);
940         if (ev == NULL) {
941                 goto fail;
942         }
943
944         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
945         if (req == NULL) {
946                 goto fail;
947         }
948
949         while (req->state < ASYNC_REQ_DONE) {
950                 event_loop_once(ev);
951         }
952
953         cli_open_recv(req, &result);
954  fail:
955         TALLOC_FREE(frame);
956         return result;
957 }
958
959 /****************************************************************************
960  Close a file.
961 ****************************************************************************/
962
963 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
964                                  struct cli_state *cli, int fnum)
965 {
966         uint16_t vwv[3];
967
968         SSVAL(vwv+0, 0, fnum);
969         SIVALS(vwv+1, 0, -1);
970
971         return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv,
972                                 0, NULL);
973 }
974
975 NTSTATUS cli_close_recv(struct async_req *req)
976 {
977         struct cli_request *cli_req = cli_request_get(req);
978
979         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
980         if (req->state == ASYNC_REQ_ERROR) {
981                 return req->status;
982         }
983
984         return cli_pull_error(cli_req->inbuf);
985 }
986
987 bool cli_close(struct cli_state *cli, int fnum)
988 {
989         TALLOC_CTX *frame = talloc_stackframe();
990         struct event_context *ev;
991         struct async_req *req;
992         bool result = false;
993
994         if (cli->fd_event != NULL) {
995                 /*
996                  * Can't use sync call while an async call is in flight
997                  */
998                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
999                 goto fail;
1000         }
1001
1002         ev = event_context_init(frame);
1003         if (ev == NULL) {
1004                 goto fail;
1005         }
1006
1007         req = cli_close_send(frame, ev, cli, fnum);
1008         if (req == NULL) {
1009                 goto fail;
1010         }
1011
1012         while (req->state < ASYNC_REQ_DONE) {
1013                 event_loop_once(ev);
1014         }
1015
1016         result = NT_STATUS_IS_OK(cli_close_recv(req));
1017  fail:
1018         TALLOC_FREE(frame);
1019         return result;
1020 }
1021
1022 /****************************************************************************
1023  Truncate a file to a specified size
1024 ****************************************************************************/
1025
1026 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1027 {
1028         unsigned int param_len = 6;
1029         unsigned int data_len = 8;
1030         uint16 setup = TRANSACT2_SETFILEINFO;
1031         char param[6];
1032         unsigned char data[8];
1033         char *rparam=NULL, *rdata=NULL;
1034         int saved_timeout = cli->timeout;
1035
1036         SSVAL(param,0,fnum);
1037         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1038         SSVAL(param,4,0);
1039
1040         SBVAL(data, 0, size);
1041
1042         if (!cli_send_trans(cli, SMBtrans2,
1043                             NULL,                    /* name */
1044                             -1, 0,                   /* fid, flags */
1045                             &setup, 1, 0,            /* setup, length, max */
1046                             param, param_len, 2,     /* param, length, max */
1047                             (char *)&data,  data_len,/* data, length, ... */
1048                             cli->max_xmit)) {        /* ... max */
1049                 cli->timeout = saved_timeout;
1050                 return False;
1051         }
1052
1053         if (!cli_receive_trans(cli, SMBtrans2,
1054                                 &rparam, &param_len,
1055                                 &rdata, &data_len)) {
1056                 cli->timeout = saved_timeout;
1057                 SAFE_FREE(rdata);
1058                 SAFE_FREE(rparam);
1059                 return False;
1060         }
1061
1062         cli->timeout = saved_timeout;
1063
1064         SAFE_FREE(rdata);
1065         SAFE_FREE(rparam);
1066
1067         return True;
1068 }
1069
1070
1071 /****************************************************************************
1072  send a lock with a specified locktype
1073  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1074 ****************************************************************************/
1075
1076 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1077                       uint32 offset, uint32 len,
1078                       int timeout, unsigned char locktype)
1079 {
1080         char *p;
1081         int saved_timeout = cli->timeout;
1082
1083         memset(cli->outbuf,'\0',smb_size);
1084         memset(cli->inbuf,'\0', smb_size);
1085
1086         cli_set_message(cli->outbuf,8,0,True);
1087
1088         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1089         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1090         cli_setup_packet(cli);
1091
1092         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1093         SSVAL(cli->outbuf,smb_vwv2,fnum);
1094         SCVAL(cli->outbuf,smb_vwv3,locktype);
1095         SIVALS(cli->outbuf, smb_vwv4, timeout);
1096         SSVAL(cli->outbuf,smb_vwv6,0);
1097         SSVAL(cli->outbuf,smb_vwv7,1);
1098
1099         p = smb_buf(cli->outbuf);
1100         SSVAL(p, 0, cli->pid);
1101         SIVAL(p, 2, offset);
1102         SIVAL(p, 6, len);
1103
1104         p += 10;
1105
1106         cli_setup_bcc(cli, p);
1107
1108         cli_send_smb(cli);
1109
1110         if (timeout != 0) {
1111                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1112         }
1113
1114         if (!cli_receive_smb(cli)) {
1115                 cli->timeout = saved_timeout;
1116                 return NT_STATUS_UNSUCCESSFUL;
1117         }
1118
1119         cli->timeout = saved_timeout;
1120
1121         return cli_nt_error(cli);
1122 }
1123
1124 /****************************************************************************
1125  Lock a file.
1126  note that timeout is in units of 2 milliseconds
1127 ****************************************************************************/
1128
1129 bool cli_lock(struct cli_state *cli, int fnum,
1130               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1131 {
1132         char *p;
1133         int saved_timeout = cli->timeout;
1134
1135         memset(cli->outbuf,'\0',smb_size);
1136         memset(cli->inbuf,'\0', smb_size);
1137
1138         cli_set_message(cli->outbuf,8,0,True);
1139
1140         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1141         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1142         cli_setup_packet(cli);
1143
1144         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1145         SSVAL(cli->outbuf,smb_vwv2,fnum);
1146         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1147         SIVALS(cli->outbuf, smb_vwv4, timeout);
1148         SSVAL(cli->outbuf,smb_vwv6,0);
1149         SSVAL(cli->outbuf,smb_vwv7,1);
1150
1151         p = smb_buf(cli->outbuf);
1152         SSVAL(p, 0, cli->pid);
1153         SIVAL(p, 2, offset);
1154         SIVAL(p, 6, len);
1155
1156         p += 10;
1157
1158         cli_setup_bcc(cli, p);
1159
1160         cli_send_smb(cli);
1161
1162         if (timeout != 0) {
1163                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1164         }
1165
1166         if (!cli_receive_smb(cli)) {
1167                 cli->timeout = saved_timeout;
1168                 return False;
1169         }
1170
1171         cli->timeout = saved_timeout;
1172
1173         if (cli_is_error(cli)) {
1174                 return False;
1175         }
1176
1177         return True;
1178 }
1179
1180 /****************************************************************************
1181  Unlock a file.
1182 ****************************************************************************/
1183
1184 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1185 {
1186         char *p;
1187
1188         memset(cli->outbuf,'\0',smb_size);
1189         memset(cli->inbuf,'\0',smb_size);
1190
1191         cli_set_message(cli->outbuf,8,0,True);
1192
1193         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1194         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1195         cli_setup_packet(cli);
1196
1197         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1198         SSVAL(cli->outbuf,smb_vwv2,fnum);
1199         SCVAL(cli->outbuf,smb_vwv3,0);
1200         SIVALS(cli->outbuf, smb_vwv4, 0);
1201         SSVAL(cli->outbuf,smb_vwv6,1);
1202         SSVAL(cli->outbuf,smb_vwv7,0);
1203
1204         p = smb_buf(cli->outbuf);
1205         SSVAL(p, 0, cli->pid);
1206         SIVAL(p, 2, offset);
1207         SIVAL(p, 6, len);
1208         p += 10;
1209         cli_setup_bcc(cli, p);
1210         cli_send_smb(cli);
1211         if (!cli_receive_smb(cli)) {
1212                 return False;
1213         }
1214
1215         if (cli_is_error(cli)) {
1216                 return False;
1217         }
1218
1219         return True;
1220 }
1221
1222 /****************************************************************************
1223  Lock a file with 64 bit offsets.
1224 ****************************************************************************/
1225
1226 bool cli_lock64(struct cli_state *cli, int fnum,
1227                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1228 {
1229         char *p;
1230         int saved_timeout = cli->timeout;
1231         int ltype;
1232
1233         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1234                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1235         }
1236
1237         ltype = (lock_type == READ_LOCK? 1 : 0);
1238         ltype |= LOCKING_ANDX_LARGE_FILES;
1239
1240         memset(cli->outbuf,'\0',smb_size);
1241         memset(cli->inbuf,'\0', smb_size);
1242
1243         cli_set_message(cli->outbuf,8,0,True);
1244
1245         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1246         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1247         cli_setup_packet(cli);
1248
1249         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1250         SSVAL(cli->outbuf,smb_vwv2,fnum);
1251         SCVAL(cli->outbuf,smb_vwv3,ltype);
1252         SIVALS(cli->outbuf, smb_vwv4, timeout);
1253         SSVAL(cli->outbuf,smb_vwv6,0);
1254         SSVAL(cli->outbuf,smb_vwv7,1);
1255
1256         p = smb_buf(cli->outbuf);
1257         SIVAL(p, 0, cli->pid);
1258         SOFF_T_R(p, 4, offset);
1259         SOFF_T_R(p, 12, len);
1260         p += 20;
1261
1262         cli_setup_bcc(cli, p);
1263         cli_send_smb(cli);
1264
1265         if (timeout != 0) {
1266                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1267         }
1268
1269         if (!cli_receive_smb(cli)) {
1270                 cli->timeout = saved_timeout;
1271                 return False;
1272         }
1273
1274         cli->timeout = saved_timeout;
1275
1276         if (cli_is_error(cli)) {
1277                 return False;
1278         }
1279
1280         return True;
1281 }
1282
1283 /****************************************************************************
1284  Unlock a file with 64 bit offsets.
1285 ****************************************************************************/
1286
1287 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1288 {
1289         char *p;
1290
1291         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1292                 return cli_unlock(cli, fnum, offset, len);
1293         }
1294
1295         memset(cli->outbuf,'\0',smb_size);
1296         memset(cli->inbuf,'\0',smb_size);
1297
1298         cli_set_message(cli->outbuf,8,0,True);
1299
1300         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1301         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1302         cli_setup_packet(cli);
1303
1304         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1305         SSVAL(cli->outbuf,smb_vwv2,fnum);
1306         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1307         SIVALS(cli->outbuf, smb_vwv4, 0);
1308         SSVAL(cli->outbuf,smb_vwv6,1);
1309         SSVAL(cli->outbuf,smb_vwv7,0);
1310
1311         p = smb_buf(cli->outbuf);
1312         SIVAL(p, 0, cli->pid);
1313         SOFF_T_R(p, 4, offset);
1314         SOFF_T_R(p, 12, len);
1315         p += 20;
1316         cli_setup_bcc(cli, p);
1317         cli_send_smb(cli);
1318         if (!cli_receive_smb(cli)) {
1319                 return False;
1320         }
1321
1322         if (cli_is_error(cli)) {
1323                 return False;
1324         }
1325
1326         return True;
1327 }
1328
1329 /****************************************************************************
1330  Get/unlock a POSIX lock on a file - internal function.
1331 ****************************************************************************/
1332
1333 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1334                 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1335 {
1336         unsigned int param_len = 4;
1337         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1338         uint16 setup = TRANSACT2_SETFILEINFO;
1339         char param[4];
1340         unsigned char data[POSIX_LOCK_DATA_SIZE];
1341         char *rparam=NULL, *rdata=NULL;
1342         int saved_timeout = cli->timeout;
1343
1344         SSVAL(param,0,fnum);
1345         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1346
1347         switch (lock_type) {
1348                 case READ_LOCK:
1349                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1350                         break;
1351                 case WRITE_LOCK:
1352                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1353                         break;
1354                 case UNLOCK_LOCK:
1355                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1356                         break;
1357                 default:
1358                         return False;
1359         }
1360
1361         if (wait_lock) {
1362                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1363                 cli->timeout = 0x7FFFFFFF;
1364         } else {
1365                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1366         }
1367
1368         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1369         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1370         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1371
1372         if (!cli_send_trans(cli, SMBtrans2,
1373                         NULL,                        /* name */
1374                         -1, 0,                          /* fid, flags */
1375                         &setup, 1, 0,                   /* setup, length, max */
1376                         param, param_len, 2,            /* param, length, max */
1377                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1378                         )) {
1379                 cli->timeout = saved_timeout;
1380                 return False;
1381         }
1382
1383         if (!cli_receive_trans(cli, SMBtrans2,
1384                                 &rparam, &param_len,
1385                                 &rdata, &data_len)) {
1386                 cli->timeout = saved_timeout;
1387                 SAFE_FREE(rdata);
1388                 SAFE_FREE(rparam);
1389                 return False;
1390         }
1391
1392         cli->timeout = saved_timeout;
1393
1394         SAFE_FREE(rdata);
1395         SAFE_FREE(rparam);
1396
1397         return True;
1398 }
1399
1400 /****************************************************************************
1401  POSIX Lock a file.
1402 ****************************************************************************/
1403
1404 bool cli_posix_lock(struct cli_state *cli, int fnum,
1405                         SMB_BIG_UINT offset, SMB_BIG_UINT len,
1406                         bool wait_lock, enum brl_type lock_type)
1407 {
1408         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1409                 return False;
1410         }
1411         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1412 }
1413
1414 /****************************************************************************
1415  POSIX Unlock a file.
1416 ****************************************************************************/
1417
1418 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1419 {
1420         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1421 }
1422
1423 /****************************************************************************
1424  POSIX Get any lock covering a file.
1425 ****************************************************************************/
1426
1427 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1428 {
1429         return True;
1430 }
1431
1432 /****************************************************************************
1433  Do a SMBgetattrE call.
1434 ****************************************************************************/
1435
1436 bool cli_getattrE(struct cli_state *cli, int fd,
1437                   uint16 *attr, SMB_OFF_T *size,
1438                   time_t *change_time,
1439                   time_t *access_time,
1440                   time_t *write_time)
1441 {
1442         memset(cli->outbuf,'\0',smb_size);
1443         memset(cli->inbuf,'\0',smb_size);
1444
1445         cli_set_message(cli->outbuf,1,0,True);
1446
1447         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1448         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1449         cli_setup_packet(cli);
1450
1451         SSVAL(cli->outbuf,smb_vwv0,fd);
1452
1453         cli_send_smb(cli);
1454         if (!cli_receive_smb(cli)) {
1455                 return False;
1456         }
1457
1458         if (cli_is_error(cli)) {
1459                 return False;
1460         }
1461
1462         if (size) {
1463                 *size = IVAL(cli->inbuf, smb_vwv6);
1464         }
1465
1466         if (attr) {
1467                 *attr = SVAL(cli->inbuf,smb_vwv10);
1468         }
1469
1470         if (change_time) {
1471                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1472         }
1473
1474         if (access_time) {
1475                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1476         }
1477
1478         if (write_time) {
1479                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1480         }
1481
1482         return True;
1483 }
1484
1485 /****************************************************************************
1486  Do a SMBgetatr call
1487 ****************************************************************************/
1488
1489 bool cli_getatr(struct cli_state *cli, const char *fname,
1490                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1491 {
1492         char *p;
1493
1494         memset(cli->outbuf,'\0',smb_size);
1495         memset(cli->inbuf,'\0',smb_size);
1496
1497         cli_set_message(cli->outbuf,0,0,True);
1498
1499         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1500         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1501         cli_setup_packet(cli);
1502
1503         p = smb_buf(cli->outbuf);
1504         *p++ = 4;
1505         p += clistr_push(cli, p, fname,
1506                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1507
1508         cli_setup_bcc(cli, p);
1509
1510         cli_send_smb(cli);
1511         if (!cli_receive_smb(cli)) {
1512                 return False;
1513         }
1514
1515         if (cli_is_error(cli)) {
1516                 return False;
1517         }
1518
1519         if (size) {
1520                 *size = IVAL(cli->inbuf, smb_vwv3);
1521         }
1522
1523         if (write_time) {
1524                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1525         }
1526
1527         if (attr) {
1528                 *attr = SVAL(cli->inbuf,smb_vwv0);
1529         }
1530
1531         return True;
1532 }
1533
1534 /****************************************************************************
1535  Do a SMBsetattrE call.
1536 ****************************************************************************/
1537
1538 bool cli_setattrE(struct cli_state *cli, int fd,
1539                   time_t change_time,
1540                   time_t access_time,
1541                   time_t write_time)
1542
1543 {
1544         char *p;
1545
1546         memset(cli->outbuf,'\0',smb_size);
1547         memset(cli->inbuf,'\0',smb_size);
1548
1549         cli_set_message(cli->outbuf,7,0,True);
1550
1551         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1552         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1553         cli_setup_packet(cli);
1554
1555         SSVAL(cli->outbuf,smb_vwv0, fd);
1556         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1557         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1558         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1559
1560         p = smb_buf(cli->outbuf);
1561         *p++ = 4;
1562
1563         cli_setup_bcc(cli, p);
1564
1565         cli_send_smb(cli);
1566         if (!cli_receive_smb(cli)) {
1567                 return False;
1568         }
1569
1570         if (cli_is_error(cli)) {
1571                 return False;
1572         }
1573
1574         return True;
1575 }
1576
1577 /****************************************************************************
1578  Do a SMBsetatr call.
1579 ****************************************************************************/
1580
1581 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1582 {
1583         char *p;
1584
1585         memset(cli->outbuf,'\0',smb_size);
1586         memset(cli->inbuf,'\0',smb_size);
1587
1588         cli_set_message(cli->outbuf,8,0,True);
1589
1590         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1591         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1592         cli_setup_packet(cli);
1593
1594         SSVAL(cli->outbuf,smb_vwv0, attr);
1595         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1596
1597         p = smb_buf(cli->outbuf);
1598         *p++ = 4;
1599         p += clistr_push(cli, p, fname,
1600                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1601         *p++ = 4;
1602
1603         cli_setup_bcc(cli, p);
1604
1605         cli_send_smb(cli);
1606         if (!cli_receive_smb(cli)) {
1607                 return False;
1608         }
1609
1610         if (cli_is_error(cli)) {
1611                 return False;
1612         }
1613
1614         return True;
1615 }
1616
1617 /****************************************************************************
1618  Check for existance of a dir.
1619 ****************************************************************************/
1620
1621 bool cli_chkpath(struct cli_state *cli, const char *path)
1622 {
1623         char *path2 = NULL;
1624         char *p;
1625         TALLOC_CTX *frame = talloc_stackframe();
1626
1627         path2 = talloc_strdup(frame, path);
1628         if (!path2) {
1629                 TALLOC_FREE(frame);
1630                 return false;
1631         }
1632         trim_char(path2,'\0','\\');
1633         if (!*path2) {
1634                 path2 = talloc_strdup(frame, "\\");
1635                 if (!path2) {
1636                         TALLOC_FREE(frame);
1637                         return false;
1638                 }
1639         }
1640
1641         memset(cli->outbuf,'\0',smb_size);
1642         cli_set_message(cli->outbuf,0,0,True);
1643         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1644         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1645         cli_setup_packet(cli);
1646         p = smb_buf(cli->outbuf);
1647         *p++ = 4;
1648         p += clistr_push(cli, p, path2,
1649                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1650
1651         cli_setup_bcc(cli, p);
1652
1653         cli_send_smb(cli);
1654         if (!cli_receive_smb(cli)) {
1655                 TALLOC_FREE(frame);
1656                 return False;
1657         }
1658
1659         TALLOC_FREE(frame);
1660
1661         if (cli_is_error(cli)) return False;
1662
1663         return True;
1664 }
1665
1666 /****************************************************************************
1667  Query disk space.
1668 ****************************************************************************/
1669
1670 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1671 {
1672         memset(cli->outbuf,'\0',smb_size);
1673         cli_set_message(cli->outbuf,0,0,True);
1674         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1675         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1676         cli_setup_packet(cli);
1677
1678         cli_send_smb(cli);
1679         if (!cli_receive_smb(cli)) {
1680                 return False;
1681         }
1682
1683         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1684         *total = SVAL(cli->inbuf,smb_vwv0);
1685         *avail = SVAL(cli->inbuf,smb_vwv3);
1686
1687         return True;
1688 }
1689
1690 /****************************************************************************
1691  Create and open a temporary file.
1692 ****************************************************************************/
1693
1694 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1695 {
1696         int len;
1697         char *p;
1698
1699         memset(cli->outbuf,'\0',smb_size);
1700         memset(cli->inbuf,'\0',smb_size);
1701
1702         cli_set_message(cli->outbuf,3,0,True);
1703
1704         SCVAL(cli->outbuf,smb_com,SMBctemp);
1705         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1706         cli_setup_packet(cli);
1707
1708         SSVAL(cli->outbuf,smb_vwv0,0);
1709         SIVALS(cli->outbuf,smb_vwv1,-1);
1710
1711         p = smb_buf(cli->outbuf);
1712         *p++ = 4;
1713         p += clistr_push(cli, p, path,
1714                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1715
1716         cli_setup_bcc(cli, p);
1717
1718         cli_send_smb(cli);
1719         if (!cli_receive_smb(cli)) {
1720                 return -1;
1721         }
1722
1723         if (cli_is_error(cli)) {
1724                 return -1;
1725         }
1726
1727         /* despite the spec, the result has a -1, followed by
1728            length, followed by name */
1729         p = smb_buf(cli->inbuf);
1730         p += 4;
1731         len = smb_buflen(cli->inbuf) - 4;
1732         if (len <= 0 || len > PATH_MAX) return -1;
1733
1734         if (tmp_path) {
1735                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1736                 if (!path2) {
1737                         return -1;
1738                 }
1739                 clistr_pull(cli, path2, p,
1740                             len+1, len, STR_ASCII);
1741                 *tmp_path = path2;
1742         }
1743
1744         return SVAL(cli->inbuf,smb_vwv0);
1745 }
1746
1747 /*
1748    send a raw ioctl - used by the torture code
1749 */
1750 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1751 {
1752         memset(cli->outbuf,'\0',smb_size);
1753         memset(cli->inbuf,'\0',smb_size);
1754
1755         cli_set_message(cli->outbuf, 3, 0, True);
1756         SCVAL(cli->outbuf,smb_com,SMBioctl);
1757         cli_setup_packet(cli);
1758
1759         SSVAL(cli->outbuf, smb_vwv0, fnum);
1760         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1761         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1762
1763         cli_send_smb(cli);
1764         if (!cli_receive_smb(cli)) {
1765                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1766         }
1767
1768         if (cli_is_error(cli)) {
1769                 return cli_nt_error(cli);
1770         }
1771
1772         *blob = data_blob_null;
1773
1774         return NT_STATUS_OK;
1775 }
1776
1777 /*********************************************************
1778  Set an extended attribute utility fn.
1779 *********************************************************/
1780
1781 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1782                         const char *ea_name, const char *ea_val, size_t ea_len)
1783 {
1784         unsigned int data_len = 0;
1785         char *data = NULL;
1786         char *rparam=NULL, *rdata=NULL;
1787         char *p;
1788         size_t ea_namelen = strlen(ea_name);
1789
1790         if (ea_namelen == 0 && ea_len == 0) {
1791                 data_len = 4;
1792                 data = (char *)SMB_MALLOC(data_len);
1793                 if (!data) {
1794                         return False;
1795                 }
1796                 p = data;
1797                 SIVAL(p,0,data_len);
1798         } else {
1799                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1800                 data = (char *)SMB_MALLOC(data_len);
1801                 if (!data) {
1802                         return False;
1803                 }
1804                 p = data;
1805                 SIVAL(p,0,data_len);
1806                 p += 4;
1807                 SCVAL(p, 0, 0); /* EA flags. */
1808                 SCVAL(p, 1, ea_namelen);
1809                 SSVAL(p, 2, ea_len);
1810                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1811                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1812         }
1813
1814         if (!cli_send_trans(cli, SMBtrans2,
1815                         NULL,                        /* name */
1816                         -1, 0,                          /* fid, flags */
1817                         &setup, 1, 0,                   /* setup, length, max */
1818                         param, param_len, 2,            /* param, length, max */
1819                         data,  data_len, cli->max_xmit /* data, length, max */
1820                         )) {
1821                 SAFE_FREE(data);
1822                 return False;
1823         }
1824
1825         if (!cli_receive_trans(cli, SMBtrans2,
1826                         &rparam, &param_len,
1827                         &rdata, &data_len)) {
1828                         SAFE_FREE(data);
1829                 return false;
1830         }
1831
1832         SAFE_FREE(data);
1833         SAFE_FREE(rdata);
1834         SAFE_FREE(rparam);
1835
1836         return True;
1837 }
1838
1839 /*********************************************************
1840  Set an extended attribute on a pathname.
1841 *********************************************************/
1842
1843 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1844 {
1845         uint16 setup = TRANSACT2_SETPATHINFO;
1846         unsigned int param_len = 0;
1847         char *param;
1848         size_t srclen = 2*(strlen(path)+1);
1849         char *p;
1850         bool ret;
1851
1852         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1853         if (!param) {
1854                 return false;
1855         }
1856         memset(param, '\0', 6);
1857         SSVAL(param,0,SMB_INFO_SET_EA);
1858         p = &param[6];
1859
1860         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1861         param_len = PTR_DIFF(p, param);
1862
1863         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1864         SAFE_FREE(param);
1865         return ret;
1866 }
1867
1868 /*********************************************************
1869  Set an extended attribute on an fnum.
1870 *********************************************************/
1871
1872 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1873 {
1874         char param[6];
1875         uint16 setup = TRANSACT2_SETFILEINFO;
1876
1877         memset(param, 0, 6);
1878         SSVAL(param,0,fnum);
1879         SSVAL(param,2,SMB_INFO_SET_EA);
1880
1881         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1882 }
1883
1884 /*********************************************************
1885  Get an extended attribute list tility fn.
1886 *********************************************************/
1887
1888 static bool cli_get_ea_list(struct cli_state *cli,
1889                 uint16 setup, char *param, unsigned int param_len,
1890                 TALLOC_CTX *ctx,
1891                 size_t *pnum_eas,
1892                 struct ea_struct **pea_list)
1893 {
1894         unsigned int data_len = 0;
1895         unsigned int rparam_len, rdata_len;
1896         char *rparam=NULL, *rdata=NULL;
1897         char *p;
1898         size_t ea_size;
1899         size_t num_eas;
1900         bool ret = False;
1901         struct ea_struct *ea_list;
1902
1903         *pnum_eas = 0;
1904         if (pea_list) {
1905                 *pea_list = NULL;
1906         }
1907
1908         if (!cli_send_trans(cli, SMBtrans2,
1909                         NULL,           /* Name */
1910                         -1, 0,          /* fid, flags */
1911                         &setup, 1, 0,   /* setup, length, max */
1912                         param, param_len, 10, /* param, length, max */
1913                         NULL, data_len, cli->max_xmit /* data, length, max */
1914                                 )) {
1915                 return False;
1916         }
1917
1918         if (!cli_receive_trans(cli, SMBtrans2,
1919                         &rparam, &rparam_len,
1920                         &rdata, &rdata_len)) {
1921                 return False;
1922         }
1923
1924         if (!rdata || rdata_len < 4) {
1925                 goto out;
1926         }
1927
1928         ea_size = (size_t)IVAL(rdata,0);
1929         if (ea_size > rdata_len) {
1930                 goto out;
1931         }
1932
1933         if (ea_size == 0) {
1934                 /* No EA's present. */
1935                 ret = True;
1936                 goto out;
1937         }
1938
1939         p = rdata + 4;
1940         ea_size -= 4;
1941
1942         /* Validate the EA list and count it. */
1943         for (num_eas = 0; ea_size >= 4; num_eas++) {
1944                 unsigned int ea_namelen = CVAL(p,1);
1945                 unsigned int ea_valuelen = SVAL(p,2);
1946                 if (ea_namelen == 0) {
1947                         goto out;
1948                 }
1949                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1950                         goto out;
1951                 }
1952                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1953                 p += 4 + ea_namelen + 1 + ea_valuelen;
1954         }
1955
1956         if (num_eas == 0) {
1957                 ret = True;
1958                 goto out;
1959         }
1960
1961         *pnum_eas = num_eas;
1962         if (!pea_list) {
1963                 /* Caller only wants number of EA's. */
1964                 ret = True;
1965                 goto out;
1966         }
1967
1968         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1969         if (!ea_list) {
1970                 goto out;
1971         }
1972
1973         ea_size = (size_t)IVAL(rdata,0);
1974         p = rdata + 4;
1975
1976         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1977                 struct ea_struct *ea = &ea_list[num_eas];
1978                 fstring unix_ea_name;
1979                 unsigned int ea_namelen = CVAL(p,1);
1980                 unsigned int ea_valuelen = SVAL(p,2);
1981
1982                 ea->flags = CVAL(p,0);
1983                 unix_ea_name[0] = '\0';
1984                 pull_ascii_fstring(unix_ea_name, p + 4);
1985                 ea->name = talloc_strdup(ctx, unix_ea_name);
1986                 /* Ensure the value is null terminated (in case it's a string). */
1987                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1988                 if (!ea->value.data) {
1989                         goto out;
1990                 }
1991                 if (ea_valuelen) {
1992                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1993                 }
1994                 ea->value.data[ea_valuelen] = 0;
1995                 ea->value.length--;
1996                 p += 4 + ea_namelen + 1 + ea_valuelen;
1997         }
1998
1999         *pea_list = ea_list;
2000         ret = True;
2001
2002  out :
2003
2004         SAFE_FREE(rdata);
2005         SAFE_FREE(rparam);
2006         return ret;
2007 }
2008
2009 /*********************************************************
2010  Get an extended attribute list from a pathname.
2011 *********************************************************/
2012
2013 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2014                 TALLOC_CTX *ctx,
2015                 size_t *pnum_eas,
2016                 struct ea_struct **pea_list)
2017 {
2018         uint16 setup = TRANSACT2_QPATHINFO;
2019         unsigned int param_len = 0;
2020         char *param;
2021         char *p;
2022         size_t srclen = 2*(strlen(path)+1);
2023         bool ret;
2024
2025         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2026         if (!param) {
2027                 return false;
2028         }
2029         p = param;
2030         memset(p, 0, 6);
2031         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2032         p += 6;
2033         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2034         param_len = PTR_DIFF(p, param);
2035
2036         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2037         SAFE_FREE(param);
2038         return ret;
2039 }
2040
2041 /*********************************************************
2042  Get an extended attribute list from an fnum.
2043 *********************************************************/
2044
2045 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2046                 TALLOC_CTX *ctx,
2047                 size_t *pnum_eas,
2048                 struct ea_struct **pea_list)
2049 {
2050         uint16 setup = TRANSACT2_QFILEINFO;
2051         char param[6];
2052
2053         memset(param, 0, 6);
2054         SSVAL(param,0,fnum);
2055         SSVAL(param,2,SMB_INFO_SET_EA);
2056
2057         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2058 }
2059
2060 /****************************************************************************
2061  Convert open "flags" arg to uint32 on wire.
2062 ****************************************************************************/
2063
2064 static uint32 open_flags_to_wire(int flags)
2065 {
2066         int open_mode = flags & O_ACCMODE;
2067         uint32 ret = 0;
2068
2069         switch (open_mode) {
2070                 case O_WRONLY:
2071                         ret |= SMB_O_WRONLY;
2072                         break;
2073                 case O_RDWR:
2074                         ret |= SMB_O_RDWR;
2075                         break;
2076                 default:
2077                 case O_RDONLY:
2078                         ret |= SMB_O_RDONLY;
2079                         break;
2080         }
2081
2082         if (flags & O_CREAT) {
2083                 ret |= SMB_O_CREAT;
2084         }
2085         if (flags & O_EXCL) {
2086                 ret |= SMB_O_EXCL;
2087         }
2088         if (flags & O_TRUNC) {
2089                 ret |= SMB_O_TRUNC;
2090         }
2091 #if defined(O_SYNC)
2092         if (flags & O_SYNC) {
2093                 ret |= SMB_O_SYNC;
2094         }
2095 #endif /* O_SYNC */
2096         if (flags & O_APPEND) {
2097                 ret |= SMB_O_APPEND;
2098         }
2099 #if defined(O_DIRECT)
2100         if (flags & O_DIRECT) {
2101                 ret |= SMB_O_DIRECT;
2102         }
2103 #endif
2104 #if defined(O_DIRECTORY)
2105         if (flags & O_DIRECTORY) {
2106                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2107                 ret |= SMB_O_DIRECTORY;
2108         }
2109 #endif
2110         return ret;
2111 }
2112
2113 /****************************************************************************
2114  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2115 ****************************************************************************/
2116
2117 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2118 {
2119         unsigned int data_len = 0;
2120         unsigned int param_len = 0;
2121         uint16 setup = TRANSACT2_SETPATHINFO;
2122         char *param;
2123         char data[18];
2124         char *rparam=NULL, *rdata=NULL;
2125         char *p;
2126         int fnum = -1;
2127         uint32 wire_flags = open_flags_to_wire(flags);
2128         size_t srclen = 2*(strlen(fname)+1);
2129
2130         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2131         if (!param) {
2132                 return false;
2133         }
2134         memset(param, '\0', 6);
2135         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2136         p = &param[6];
2137
2138         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2139         param_len = PTR_DIFF(p, param);
2140
2141         if (is_dir) {
2142                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2143                 wire_flags |= SMB_O_DIRECTORY;
2144         }
2145
2146         p = data;
2147         SIVAL(p,0,0); /* No oplock. */
2148         SIVAL(p,4,wire_flags);
2149         SIVAL(p,8,unix_perms_to_wire(mode));
2150         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2151         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2152
2153         data_len = 18;
2154
2155         if (!cli_send_trans(cli, SMBtrans2,
2156                         NULL,                        /* name */
2157                         -1, 0,                          /* fid, flags */
2158                         &setup, 1, 0,                   /* setup, length, max */
2159                         param, param_len, 2,            /* param, length, max */
2160                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2161                         )) {
2162                 SAFE_FREE(param);
2163                 return -1;
2164         }
2165
2166         SAFE_FREE(param);
2167
2168         if (!cli_receive_trans(cli, SMBtrans2,
2169                 &rparam, &param_len,
2170                 &rdata, &data_len)) {
2171                         return -1;
2172         }
2173
2174         fnum = SVAL(rdata,2);
2175
2176         SAFE_FREE(rdata);
2177         SAFE_FREE(rparam);
2178
2179         return fnum;
2180 }
2181
2182 /****************************************************************************
2183  open - POSIX semantics.
2184 ****************************************************************************/
2185
2186 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2187 {
2188         return cli_posix_open_internal(cli, fname, flags, mode, False);
2189 }
2190
2191 /****************************************************************************
2192  mkdir - POSIX semantics.
2193 ****************************************************************************/
2194
2195 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2196 {
2197         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2198 }
2199
2200 /****************************************************************************
2201  unlink or rmdir - POSIX semantics.
2202 ****************************************************************************/
2203
2204 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2205 {
2206         unsigned int data_len = 0;
2207         unsigned int param_len = 0;
2208         uint16 setup = TRANSACT2_SETPATHINFO;
2209         char *param;
2210         char data[2];
2211         char *rparam=NULL, *rdata=NULL;
2212         char *p;
2213         size_t srclen = 2*(strlen(fname)+1);
2214
2215         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2216         if (!param) {
2217                 return false;
2218         }
2219         memset(param, '\0', 6);
2220         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2221         p = &param[6];
2222
2223         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2224         param_len = PTR_DIFF(p, param);
2225
2226         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2227                         SMB_POSIX_UNLINK_FILE_TARGET);
2228         data_len = 2;
2229
2230         if (!cli_send_trans(cli, SMBtrans2,
2231                         NULL,                        /* name */
2232                         -1, 0,                          /* fid, flags */
2233                         &setup, 1, 0,                   /* setup, length, max */
2234                         param, param_len, 2,            /* param, length, max */
2235                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2236                         )) {
2237                 SAFE_FREE(param);
2238                 return False;
2239         }
2240
2241         SAFE_FREE(param);
2242
2243         if (!cli_receive_trans(cli, SMBtrans2,
2244                 &rparam, &param_len,
2245                 &rdata, &data_len)) {
2246                         return False;
2247         }
2248
2249         SAFE_FREE(rdata);
2250         SAFE_FREE(rparam);
2251
2252         return True;
2253 }
2254
2255 /****************************************************************************
2256  unlink - POSIX semantics.
2257 ****************************************************************************/
2258
2259 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2260 {
2261         return cli_posix_unlink_internal(cli, fname, False);
2262 }
2263
2264 /****************************************************************************
2265  rmdir - POSIX semantics.
2266 ****************************************************************************/
2267
2268 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2269 {
2270         return cli_posix_unlink_internal(cli, fname, True);
2271 }