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