Simplify async programming a bit with helper routines
[kai/samba.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Hard/Symlink a file (UNIX extensions).
25  Creates new name (sym)linked to oldname.
26 ****************************************************************************/
27
28 static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
29 {
30         unsigned int data_len = 0;
31         unsigned int param_len = 0;
32         uint16 setup = TRANSACT2_SETPATHINFO;
33         char *param;
34         char *data;
35         char *rparam=NULL, *rdata=NULL;
36         char *p;
37         size_t oldlen = 2*(strlen(oldname)+1);
38         size_t newlen = 2*(strlen(newname)+1);
39
40         param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
41
42         if (!param) {
43                 return false;
44         }
45
46         data = SMB_MALLOC_ARRAY(char, oldlen+2);
47
48         if (!data) {
49                 SAFE_FREE(param);
50                 return false;
51         }
52
53         SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
54         SIVAL(param,2,0);
55         p = &param[6];
56
57         p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
58         param_len = PTR_DIFF(p, param);
59
60         p = data;
61         p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
62         data_len = PTR_DIFF(p, data);
63
64         if (!cli_send_trans(cli, SMBtrans2,
65                         NULL,                        /* name */
66                         -1, 0,                          /* fid, flags */
67                         &setup, 1, 0,                   /* setup, length, max */
68                         param, param_len, 2,            /* param, length, max */
69                         data,  data_len, cli->max_xmit /* data, length, max */
70                         )) {
71                 SAFE_FREE(data);
72                 SAFE_FREE(param);
73                 return false;
74         }
75
76         SAFE_FREE(data);
77         SAFE_FREE(param);
78
79         if (!cli_receive_trans(cli, SMBtrans2,
80                         &rparam, &param_len,
81                         &rdata, &data_len)) {
82                         return false;
83         }
84
85         SAFE_FREE(data);
86         SAFE_FREE(param);
87         SAFE_FREE(rdata);
88         SAFE_FREE(rparam);
89
90         return true;
91 }
92
93 /****************************************************************************
94  Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
96
97 uint32 unix_perms_to_wire(mode_t perms)
98 {
99         unsigned int ret = 0;
100
101         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
102         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
103         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
104         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
105         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
106         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
107         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
108         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
109         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
110 #ifdef S_ISVTX
111         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
112 #endif
113 #ifdef S_ISGID
114         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
115 #endif
116 #ifdef S_ISUID
117         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
118 #endif
119         return ret;
120 }
121
122 /****************************************************************************
123  Map wire permissions to standard UNIX.
124 ****************************************************************************/
125
126 mode_t wire_perms_to_unix(uint32 perms)
127 {
128         mode_t ret = (mode_t)0;
129
130         ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
131         ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
132         ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
133         ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
134         ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
135         ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
136         ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
137         ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
138         ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
139 #ifdef S_ISVTX
140         ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
141 #endif
142 #ifdef S_ISGID
143         ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
144 #endif
145 #ifdef S_ISUID
146         ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
147 #endif
148         return ret;
149 }
150
151 /****************************************************************************
152  Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
154
155 static mode_t unix_filetype_from_wire(uint32 wire_type)
156 {
157         switch (wire_type) {
158                 case UNIX_TYPE_FILE:
159                         return S_IFREG;
160                 case UNIX_TYPE_DIR:
161                         return S_IFDIR;
162 #ifdef S_IFLNK
163                 case UNIX_TYPE_SYMLINK:
164                         return S_IFLNK;
165 #endif
166 #ifdef S_IFCHR
167                 case UNIX_TYPE_CHARDEV:
168                         return S_IFCHR;
169 #endif
170 #ifdef S_IFBLK
171                 case UNIX_TYPE_BLKDEV:
172                         return S_IFBLK;
173 #endif
174 #ifdef S_IFIFO
175                 case UNIX_TYPE_FIFO:
176                         return S_IFIFO;
177 #endif
178 #ifdef S_IFSOCK
179                 case UNIX_TYPE_SOCKET:
180                         return S_IFSOCK;
181 #endif
182                 default:
183                         return (mode_t)0;
184         }
185 }
186
187 /****************************************************************************
188  Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
190
191 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
192 {
193         unsigned int param_len = 0;
194         unsigned int data_len = 0;
195         uint16 setup = TRANSACT2_QPATHINFO;
196         char *param;
197         size_t nlen = 2*(strlen(name)+1);
198         char *rparam=NULL, *rdata=NULL;
199         char *p;
200
201         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
202         if (!param) {
203                 return false;
204         }
205
206         p = param;
207         memset(p, '\0', 6);
208         SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
209         p += 6;
210         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
211         param_len = PTR_DIFF(p, param);
212
213         if (!cli_send_trans(cli, SMBtrans2,
214                 NULL,                        /* name */
215                 -1, 0,                       /* fid, flags */
216                 &setup, 1, 0,                /* setup, length, max */
217                 param, param_len, 2,         /* param, length, max */
218                 NULL,  0, cli->max_xmit      /* data, length, max */
219                 )) {
220                 SAFE_FREE(param);
221                 return false;
222         }
223
224         SAFE_FREE(param);
225
226         if (!cli_receive_trans(cli, SMBtrans2,
227                         &rparam, &param_len,
228                         &rdata, &data_len)) {
229                 return false;
230         }
231
232         if (data_len < 6) {
233                 SAFE_FREE(rdata);
234                 SAFE_FREE(rparam);
235                 return false;
236         }
237
238         SAFE_FREE(rparam);
239         *retbuf = rdata;
240         *prb_size = (size_t)data_len;
241
242         return true;
243 }
244
245 /****************************************************************************
246  Stat a file (UNIX extensions).
247 ****************************************************************************/
248
249 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
250 {
251         unsigned int param_len = 0;
252         unsigned int data_len = 0;
253         uint16 setup = TRANSACT2_QPATHINFO;
254         char *param;
255         size_t nlen = 2*(strlen(name)+1);
256         char *rparam=NULL, *rdata=NULL;
257         char *p;
258
259         ZERO_STRUCTP(sbuf);
260
261         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
262         if (!param) {
263                 return false;
264         }
265         p = param;
266         memset(p, '\0', 6);
267         SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
268         p += 6;
269         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
270         param_len = PTR_DIFF(p, param);
271
272         if (!cli_send_trans(cli, SMBtrans2,
273                         NULL,                        /* name */
274                         -1, 0,                       /* fid, flags */
275                         &setup, 1, 0,                /* setup, length, max */
276                         param, param_len, 2,         /* param, length, max */
277                         NULL,  0, cli->max_xmit      /* data, length, max */
278                         )) {
279                 SAFE_FREE(param);
280                 return false;
281         }
282
283         SAFE_FREE(param);
284
285         if (!cli_receive_trans(cli, SMBtrans2,
286                         &rparam, &param_len,
287                         &rdata, &data_len)) {
288                 return false;
289         }
290
291         if (data_len < 96) {
292                 SAFE_FREE(rdata);
293                 SAFE_FREE(rparam);
294                 return false;
295         }
296
297         sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
298         sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300         sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
301 #else
302         /* assume 512 byte blocks */
303         sbuf->st_blocks /= 512;
304 #endif
305         set_ctimespec(sbuf, interpret_long_date(rdata + 16));    /* time of last change */
306         set_atimespec(sbuf, interpret_long_date(rdata + 24));    /* time of last access */
307         set_mtimespec(sbuf, interpret_long_date(rdata + 32));    /* time of last modification */
308
309         sbuf->st_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
310         sbuf->st_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
311         sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
312 #if defined(HAVE_MAKEDEV)
313         {
314                 uint32 dev_major = IVAL(rdata,60);
315                 uint32 dev_minor = IVAL(rdata,68);
316                 sbuf->st_rdev = makedev(dev_major, dev_minor);
317         }
318 #endif
319         sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
320         sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
321         sbuf->st_nlink = IVAL(rdata,92);    /* number of hard links */
322
323         SAFE_FREE(rdata);
324         SAFE_FREE(rparam);
325
326         return true;
327 }
328
329 /****************************************************************************
330  Symlink a file (UNIX extensions).
331 ****************************************************************************/
332
333 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
334 {
335         return cli_link_internal(cli, oldname, newname, False);
336 }
337
338 /****************************************************************************
339  Hard a file (UNIX extensions).
340 ****************************************************************************/
341
342 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
343 {
344         return cli_link_internal(cli, oldname, newname, True);
345 }
346
347 /****************************************************************************
348  Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
350
351 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
352 {
353         unsigned int data_len = 0;
354         unsigned int param_len = 0;
355         uint16 setup = TRANSACT2_SETPATHINFO;
356         size_t nlen = 2*(strlen(fname)+1);
357         char *param;
358         char data[100];
359         char *rparam=NULL, *rdata=NULL;
360         char *p;
361
362         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
363         if (!param) {
364                 return false;
365         }
366         memset(param, '\0', 6);
367         memset(data, 0, sizeof(data));
368
369         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
370         p = &param[6];
371
372         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
373         param_len = PTR_DIFF(p, param);
374
375         memset(data, 0xff, 40); /* Set all sizes/times to no change. */
376
377         SIVAL(data,40,uid);
378         SIVAL(data,48,gid);
379         SIVAL(data,84,mode);
380
381         data_len = 100;
382
383         if (!cli_send_trans(cli, SMBtrans2,
384                         NULL,                        /* name */
385                         -1, 0,                          /* fid, flags */
386                         &setup, 1, 0,                   /* setup, length, max */
387                         param, param_len, 2,            /* param, length, max */
388                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
389                         )) {
390                 SAFE_FREE(param);
391                 return False;
392         }
393
394         SAFE_FREE(param);
395
396         if (!cli_receive_trans(cli, SMBtrans2,
397                         &rparam, &param_len,
398                         &rdata, &data_len)) {
399                 return false;
400         }
401
402         SAFE_FREE(rdata);
403         SAFE_FREE(rparam);
404
405         return true;
406 }
407
408 /****************************************************************************
409  chmod a file (UNIX extensions).
410 ****************************************************************************/
411
412 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
413 {
414         return cli_unix_chmod_chown_internal(cli, fname,
415                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
416 }
417
418 /****************************************************************************
419  chown a file (UNIX extensions).
420 ****************************************************************************/
421
422 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
423 {
424         return cli_unix_chmod_chown_internal(cli, fname,
425                         SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
426 }
427
428 /****************************************************************************
429  Rename a file.
430 ****************************************************************************/
431
432 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
433 {
434         char *p;
435
436         memset(cli->outbuf,'\0',smb_size);
437         memset(cli->inbuf,'\0',smb_size);
438
439         cli_set_message(cli->outbuf,1, 0, true);
440
441         SCVAL(cli->outbuf,smb_com,SMBmv);
442         SSVAL(cli->outbuf,smb_tid,cli->cnum);
443         cli_setup_packet(cli);
444
445         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
446
447         p = smb_buf(cli->outbuf);
448         *p++ = 4;
449         p += clistr_push(cli, p, fname_src,
450                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
451         *p++ = 4;
452         p += clistr_push(cli, p, fname_dst,
453                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
454
455         cli_setup_bcc(cli, p);
456
457         cli_send_smb(cli);
458         if (!cli_receive_smb(cli)) {
459                 return false;
460         }
461
462         if (cli_is_error(cli)) {
463                 return false;
464         }
465
466         return true;
467 }
468
469 /****************************************************************************
470  NT Rename a file.
471 ****************************************************************************/
472
473 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
474 {
475         char *p;
476
477         memset(cli->outbuf,'\0',smb_size);
478         memset(cli->inbuf,'\0',smb_size);
479
480         cli_set_message(cli->outbuf, 4, 0, true);
481
482         SCVAL(cli->outbuf,smb_com,SMBntrename);
483         SSVAL(cli->outbuf,smb_tid,cli->cnum);
484         cli_setup_packet(cli);
485
486         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
487         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
488
489         p = smb_buf(cli->outbuf);
490         *p++ = 4;
491         p += clistr_push(cli, p, fname_src,
492                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
493         *p++ = 4;
494         p += clistr_push(cli, p, fname_dst,
495                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
496
497         cli_setup_bcc(cli, p);
498
499         cli_send_smb(cli);
500         if (!cli_receive_smb(cli)) {
501                 return false;
502         }
503
504         if (cli_is_error(cli)) {
505                 return false;
506         }
507
508         return true;
509 }
510
511 /****************************************************************************
512  NT hardlink a file.
513 ****************************************************************************/
514
515 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
516 {
517         char *p;
518
519         memset(cli->outbuf,'\0',smb_size);
520         memset(cli->inbuf,'\0',smb_size);
521
522         cli_set_message(cli->outbuf, 4, 0, true);
523
524         SCVAL(cli->outbuf,smb_com,SMBntrename);
525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
526         cli_setup_packet(cli);
527
528         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
529         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
530
531         p = smb_buf(cli->outbuf);
532         *p++ = 4;
533         p += clistr_push(cli, p, fname_src,
534                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
535         *p++ = 4;
536         p += clistr_push(cli, p, fname_dst,
537                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
538
539         cli_setup_bcc(cli, p);
540
541         cli_send_smb(cli);
542         if (!cli_receive_smb(cli)) {
543                 return false;
544         }
545
546         if (cli_is_error(cli)) {
547                 return false;
548         }
549
550         return true;
551 }
552
553 /****************************************************************************
554  Delete a file.
555 ****************************************************************************/
556
557 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
558 {
559         char *p;
560
561         memset(cli->outbuf,'\0',smb_size);
562         memset(cli->inbuf,'\0',smb_size);
563
564         cli_set_message(cli->outbuf,1, 0, true);
565
566         SCVAL(cli->outbuf,smb_com,SMBunlink);
567         SSVAL(cli->outbuf,smb_tid,cli->cnum);
568         cli_setup_packet(cli);
569
570         SSVAL(cli->outbuf,smb_vwv0, attrs);
571
572         p = smb_buf(cli->outbuf);
573         *p++ = 4;
574         p += clistr_push(cli, p, fname,
575                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
576
577         cli_setup_bcc(cli, p);
578         cli_send_smb(cli);
579         if (!cli_receive_smb(cli)) {
580                 return false;
581         }
582
583         if (cli_is_error(cli)) {
584                 return false;
585         }
586
587         return true;
588 }
589
590 /****************************************************************************
591  Delete a file.
592 ****************************************************************************/
593
594 bool cli_unlink(struct cli_state *cli, const char *fname)
595 {
596         return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
597 }
598
599 /****************************************************************************
600  Create a directory.
601 ****************************************************************************/
602
603 bool cli_mkdir(struct cli_state *cli, const char *dname)
604 {
605         char *p;
606
607         memset(cli->outbuf,'\0',smb_size);
608         memset(cli->inbuf,'\0',smb_size);
609
610         cli_set_message(cli->outbuf,0, 0, true);
611
612         SCVAL(cli->outbuf,smb_com,SMBmkdir);
613         SSVAL(cli->outbuf,smb_tid,cli->cnum);
614         cli_setup_packet(cli);
615
616         p = smb_buf(cli->outbuf);
617         *p++ = 4;
618         p += clistr_push(cli, p, dname,
619                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
620
621         cli_setup_bcc(cli, p);
622
623         cli_send_smb(cli);
624         if (!cli_receive_smb(cli)) {
625                 return False;
626         }
627
628         if (cli_is_error(cli)) {
629                 return False;
630         }
631
632         return True;
633 }
634
635 /****************************************************************************
636  Remove a directory.
637 ****************************************************************************/
638
639 bool cli_rmdir(struct cli_state *cli, const char *dname)
640 {
641         char *p;
642
643         memset(cli->outbuf,'\0',smb_size);
644         memset(cli->inbuf,'\0',smb_size);
645
646         cli_set_message(cli->outbuf,0, 0, true);
647
648         SCVAL(cli->outbuf,smb_com,SMBrmdir);
649         SSVAL(cli->outbuf,smb_tid,cli->cnum);
650         cli_setup_packet(cli);
651
652         p = smb_buf(cli->outbuf);
653         *p++ = 4;
654         p += clistr_push(cli, p, dname,
655                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
656
657         cli_setup_bcc(cli, p);
658
659         cli_send_smb(cli);
660         if (!cli_receive_smb(cli)) {
661                 return false;
662         }
663
664         if (cli_is_error(cli)) {
665                 return false;
666         }
667
668         return true;
669 }
670
671 /****************************************************************************
672  Set or clear the delete on close flag.
673 ****************************************************************************/
674
675 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
676 {
677         unsigned int data_len = 1;
678         unsigned int param_len = 6;
679         uint16 setup = TRANSACT2_SETFILEINFO;
680         char param[6];
681         unsigned char data;
682         char *rparam=NULL, *rdata=NULL;
683
684         memset(param, 0, param_len);
685         SSVAL(param,0,fnum);
686         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
687
688         data = flag ? 1 : 0;
689
690         if (!cli_send_trans(cli, SMBtrans2,
691                         NULL,                        /* name */
692                         -1, 0,                          /* fid, flags */
693                         &setup, 1, 0,                   /* setup, length, max */
694                         param, param_len, 2,            /* param, length, max */
695                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
696                         )) {
697                 return false;
698         }
699
700         if (!cli_receive_trans(cli, SMBtrans2,
701                         &rparam, &param_len,
702                         &rdata, &data_len)) {
703                 return false;
704         }
705
706         SAFE_FREE(rdata);
707         SAFE_FREE(rparam);
708
709         return true;
710 }
711
712 /****************************************************************************
713  Open a file - exposing the full horror of the NT API :-).
714  Used in smbtorture.
715 ****************************************************************************/
716
717 int cli_nt_create_full(struct cli_state *cli, const char *fname,
718                        uint32 CreatFlags, uint32 DesiredAccess,
719                        uint32 FileAttributes, uint32 ShareAccess,
720                        uint32 CreateDisposition, uint32 CreateOptions,
721                        uint8 SecurityFlags)
722 {
723         char *p;
724         int len;
725
726         memset(cli->outbuf,'\0',smb_size);
727         memset(cli->inbuf,'\0',smb_size);
728
729         cli_set_message(cli->outbuf,24,0, true);
730
731         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
732         SSVAL(cli->outbuf,smb_tid,cli->cnum);
733         cli_setup_packet(cli);
734
735         SSVAL(cli->outbuf,smb_vwv0,0xFF);
736         if (cli->use_oplocks)
737                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
738
739         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
740         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
741         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
742         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
743         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
744         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
745         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
746         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
747         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
748
749         p = smb_buf(cli->outbuf);
750         /* this alignment and termination is critical for netapp filers. Don't change */
751         p += clistr_align_out(cli, p, 0);
752         len = clistr_push(cli, p, fname,
753                         cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
754         p += len;
755         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
756         /* sigh. this copes with broken netapp filer behaviour */
757         p += clistr_push(cli, p, "",
758                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
759
760         cli_setup_bcc(cli, p);
761
762         cli_send_smb(cli);
763         if (!cli_receive_smb(cli)) {
764                 return -1;
765         }
766
767         if (cli_is_error(cli)) {
768                 return -1;
769         }
770
771         return SVAL(cli->inbuf,smb_vwv2 + 1);
772 }
773
774 /****************************************************************************
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 = 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         uint8_t wct;
906         uint16_t *vwv;
907         uint16_t num_bytes;
908         uint8_t *bytes;
909         NTSTATUS status;
910
911         if (async_req_is_error(req, &status)) {
912                 return status;
913         }
914
915         status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
916         if (!NT_STATUS_IS_OK(status)) {
917                 return status;
918         }
919
920         if (wct < 3) {
921                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
922         }
923
924         *fnum = SVAL(vwv+2, 0);
925
926         return NT_STATUS_OK;
927 }
928
929 int cli_open(struct cli_state *cli, const char *fname, int flags,
930              int share_mode)
931 {
932         TALLOC_CTX *frame = talloc_stackframe();
933         struct event_context *ev;
934         struct async_req *req;
935         int result = -1;
936
937         if (cli->fd_event != NULL) {
938                 /*
939                  * Can't use sync call while an async call is in flight
940                  */
941                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
942                 goto fail;
943         }
944
945         ev = event_context_init(frame);
946         if (ev == NULL) {
947                 goto fail;
948         }
949
950         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
951         if (req == NULL) {
952                 goto fail;
953         }
954
955         while (req->state < ASYNC_REQ_DONE) {
956                 event_loop_once(ev);
957         }
958
959         cli_open_recv(req, &result);
960  fail:
961         TALLOC_FREE(frame);
962         return result;
963 }
964
965 /****************************************************************************
966  Close a file.
967 ****************************************************************************/
968
969 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
970                                  struct cli_state *cli, int fnum)
971 {
972         uint16_t vwv[3];
973
974         SSVAL(vwv+0, 0, fnum);
975         SIVALS(vwv+1, 0, -1);
976
977         return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv,
978                                 0, NULL);
979 }
980
981 NTSTATUS cli_close_recv(struct async_req *req)
982 {
983         uint8_t wct;
984         uint16_t *vwv;
985         uint16_t num_bytes;
986         uint8_t *bytes;
987         NTSTATUS status;
988
989         if (async_req_is_error(req, &status)) {
990                 return status;
991         }
992
993         return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
994 }
995
996 bool cli_close(struct cli_state *cli, int fnum)
997 {
998         TALLOC_CTX *frame = talloc_stackframe();
999         struct event_context *ev;
1000         struct async_req *req;
1001         bool result = false;
1002
1003         if (cli->fd_event != NULL) {
1004                 /*
1005                  * Can't use sync call while an async call is in flight
1006                  */
1007                 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1008                 goto fail;
1009         }
1010
1011         ev = event_context_init(frame);
1012         if (ev == NULL) {
1013                 goto fail;
1014         }
1015
1016         req = cli_close_send(frame, ev, cli, fnum);
1017         if (req == NULL) {
1018                 goto fail;
1019         }
1020
1021         while (req->state < ASYNC_REQ_DONE) {
1022                 event_loop_once(ev);
1023         }
1024
1025         result = NT_STATUS_IS_OK(cli_close_recv(req));
1026  fail:
1027         TALLOC_FREE(frame);
1028         return result;
1029 }
1030
1031 /****************************************************************************
1032  Truncate a file to a specified size
1033 ****************************************************************************/
1034
1035 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1036 {
1037         unsigned int param_len = 6;
1038         unsigned int data_len = 8;
1039         uint16 setup = TRANSACT2_SETFILEINFO;
1040         char param[6];
1041         unsigned char data[8];
1042         char *rparam=NULL, *rdata=NULL;
1043         int saved_timeout = cli->timeout;
1044
1045         SSVAL(param,0,fnum);
1046         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1047         SSVAL(param,4,0);
1048
1049         SBVAL(data, 0, size);
1050
1051         if (!cli_send_trans(cli, SMBtrans2,
1052                             NULL,                    /* name */
1053                             -1, 0,                   /* fid, flags */
1054                             &setup, 1, 0,            /* setup, length, max */
1055                             param, param_len, 2,     /* param, length, max */
1056                             (char *)&data,  data_len,/* data, length, ... */
1057                             cli->max_xmit)) {        /* ... max */
1058                 cli->timeout = saved_timeout;
1059                 return False;
1060         }
1061
1062         if (!cli_receive_trans(cli, SMBtrans2,
1063                                 &rparam, &param_len,
1064                                 &rdata, &data_len)) {
1065                 cli->timeout = saved_timeout;
1066                 SAFE_FREE(rdata);
1067                 SAFE_FREE(rparam);
1068                 return False;
1069         }
1070
1071         cli->timeout = saved_timeout;
1072
1073         SAFE_FREE(rdata);
1074         SAFE_FREE(rparam);
1075
1076         return True;
1077 }
1078
1079
1080 /****************************************************************************
1081  send a lock with a specified locktype
1082  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1083 ****************************************************************************/
1084
1085 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1086                       uint32 offset, uint32 len,
1087                       int timeout, unsigned char locktype)
1088 {
1089         char *p;
1090         int saved_timeout = cli->timeout;
1091
1092         memset(cli->outbuf,'\0',smb_size);
1093         memset(cli->inbuf,'\0', smb_size);
1094
1095         cli_set_message(cli->outbuf,8,0,True);
1096
1097         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1098         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1099         cli_setup_packet(cli);
1100
1101         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1102         SSVAL(cli->outbuf,smb_vwv2,fnum);
1103         SCVAL(cli->outbuf,smb_vwv3,locktype);
1104         SIVALS(cli->outbuf, smb_vwv4, timeout);
1105         SSVAL(cli->outbuf,smb_vwv6,0);
1106         SSVAL(cli->outbuf,smb_vwv7,1);
1107
1108         p = smb_buf(cli->outbuf);
1109         SSVAL(p, 0, cli->pid);
1110         SIVAL(p, 2, offset);
1111         SIVAL(p, 6, len);
1112
1113         p += 10;
1114
1115         cli_setup_bcc(cli, p);
1116
1117         cli_send_smb(cli);
1118
1119         if (timeout != 0) {
1120                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1121         }
1122
1123         if (!cli_receive_smb(cli)) {
1124                 cli->timeout = saved_timeout;
1125                 return NT_STATUS_UNSUCCESSFUL;
1126         }
1127
1128         cli->timeout = saved_timeout;
1129
1130         return cli_nt_error(cli);
1131 }
1132
1133 /****************************************************************************
1134  Lock a file.
1135  note that timeout is in units of 2 milliseconds
1136 ****************************************************************************/
1137
1138 bool cli_lock(struct cli_state *cli, int fnum,
1139               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1140 {
1141         char *p;
1142         int saved_timeout = cli->timeout;
1143
1144         memset(cli->outbuf,'\0',smb_size);
1145         memset(cli->inbuf,'\0', smb_size);
1146
1147         cli_set_message(cli->outbuf,8,0,True);
1148
1149         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1150         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1151         cli_setup_packet(cli);
1152
1153         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1154         SSVAL(cli->outbuf,smb_vwv2,fnum);
1155         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1156         SIVALS(cli->outbuf, smb_vwv4, timeout);
1157         SSVAL(cli->outbuf,smb_vwv6,0);
1158         SSVAL(cli->outbuf,smb_vwv7,1);
1159
1160         p = smb_buf(cli->outbuf);
1161         SSVAL(p, 0, cli->pid);
1162         SIVAL(p, 2, offset);
1163         SIVAL(p, 6, len);
1164
1165         p += 10;
1166
1167         cli_setup_bcc(cli, p);
1168
1169         cli_send_smb(cli);
1170
1171         if (timeout != 0) {
1172                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1173         }
1174
1175         if (!cli_receive_smb(cli)) {
1176                 cli->timeout = saved_timeout;
1177                 return False;
1178         }
1179
1180         cli->timeout = saved_timeout;
1181
1182         if (cli_is_error(cli)) {
1183                 return False;
1184         }
1185
1186         return True;
1187 }
1188
1189 /****************************************************************************
1190  Unlock a file.
1191 ****************************************************************************/
1192
1193 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1194 {
1195         char *p;
1196
1197         memset(cli->outbuf,'\0',smb_size);
1198         memset(cli->inbuf,'\0',smb_size);
1199
1200         cli_set_message(cli->outbuf,8,0,True);
1201
1202         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1203         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1204         cli_setup_packet(cli);
1205
1206         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1207         SSVAL(cli->outbuf,smb_vwv2,fnum);
1208         SCVAL(cli->outbuf,smb_vwv3,0);
1209         SIVALS(cli->outbuf, smb_vwv4, 0);
1210         SSVAL(cli->outbuf,smb_vwv6,1);
1211         SSVAL(cli->outbuf,smb_vwv7,0);
1212
1213         p = smb_buf(cli->outbuf);
1214         SSVAL(p, 0, cli->pid);
1215         SIVAL(p, 2, offset);
1216         SIVAL(p, 6, len);
1217         p += 10;
1218         cli_setup_bcc(cli, p);
1219         cli_send_smb(cli);
1220         if (!cli_receive_smb(cli)) {
1221                 return False;
1222         }
1223
1224         if (cli_is_error(cli)) {
1225                 return False;
1226         }
1227
1228         return True;
1229 }
1230
1231 /****************************************************************************
1232  Lock a file with 64 bit offsets.
1233 ****************************************************************************/
1234
1235 bool cli_lock64(struct cli_state *cli, int fnum,
1236                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1237 {
1238         char *p;
1239         int saved_timeout = cli->timeout;
1240         int ltype;
1241
1242         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1243                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1244         }
1245
1246         ltype = (lock_type == READ_LOCK? 1 : 0);
1247         ltype |= LOCKING_ANDX_LARGE_FILES;
1248
1249         memset(cli->outbuf,'\0',smb_size);
1250         memset(cli->inbuf,'\0', smb_size);
1251
1252         cli_set_message(cli->outbuf,8,0,True);
1253
1254         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1255         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1256         cli_setup_packet(cli);
1257
1258         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1259         SSVAL(cli->outbuf,smb_vwv2,fnum);
1260         SCVAL(cli->outbuf,smb_vwv3,ltype);
1261         SIVALS(cli->outbuf, smb_vwv4, timeout);
1262         SSVAL(cli->outbuf,smb_vwv6,0);
1263         SSVAL(cli->outbuf,smb_vwv7,1);
1264
1265         p = smb_buf(cli->outbuf);
1266         SIVAL(p, 0, cli->pid);
1267         SOFF_T_R(p, 4, offset);
1268         SOFF_T_R(p, 12, len);
1269         p += 20;
1270
1271         cli_setup_bcc(cli, p);
1272         cli_send_smb(cli);
1273
1274         if (timeout != 0) {
1275                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1276         }
1277
1278         if (!cli_receive_smb(cli)) {
1279                 cli->timeout = saved_timeout;
1280                 return False;
1281         }
1282
1283         cli->timeout = saved_timeout;
1284
1285         if (cli_is_error(cli)) {
1286                 return False;
1287         }
1288
1289         return True;
1290 }
1291
1292 /****************************************************************************
1293  Unlock a file with 64 bit offsets.
1294 ****************************************************************************/
1295
1296 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1297 {
1298         char *p;
1299
1300         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1301                 return cli_unlock(cli, fnum, offset, len);
1302         }
1303
1304         memset(cli->outbuf,'\0',smb_size);
1305         memset(cli->inbuf,'\0',smb_size);
1306
1307         cli_set_message(cli->outbuf,8,0,True);
1308
1309         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1310         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1311         cli_setup_packet(cli);
1312
1313         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1314         SSVAL(cli->outbuf,smb_vwv2,fnum);
1315         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1316         SIVALS(cli->outbuf, smb_vwv4, 0);
1317         SSVAL(cli->outbuf,smb_vwv6,1);
1318         SSVAL(cli->outbuf,smb_vwv7,0);
1319
1320         p = smb_buf(cli->outbuf);
1321         SIVAL(p, 0, cli->pid);
1322         SOFF_T_R(p, 4, offset);
1323         SOFF_T_R(p, 12, len);
1324         p += 20;
1325         cli_setup_bcc(cli, p);
1326         cli_send_smb(cli);
1327         if (!cli_receive_smb(cli)) {
1328                 return False;
1329         }
1330
1331         if (cli_is_error(cli)) {
1332                 return False;
1333         }
1334
1335         return True;
1336 }
1337
1338 /****************************************************************************
1339  Get/unlock a POSIX lock on a file - internal function.
1340 ****************************************************************************/
1341
1342 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1343                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1344 {
1345         unsigned int param_len = 4;
1346         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1347         uint16 setup = TRANSACT2_SETFILEINFO;
1348         char param[4];
1349         unsigned char data[POSIX_LOCK_DATA_SIZE];
1350         char *rparam=NULL, *rdata=NULL;
1351         int saved_timeout = cli->timeout;
1352
1353         SSVAL(param,0,fnum);
1354         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1355
1356         switch (lock_type) {
1357                 case READ_LOCK:
1358                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1359                         break;
1360                 case WRITE_LOCK:
1361                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1362                         break;
1363                 case UNLOCK_LOCK:
1364                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1365                         break;
1366                 default:
1367                         return False;
1368         }
1369
1370         if (wait_lock) {
1371                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1372                 cli->timeout = 0x7FFFFFFF;
1373         } else {
1374                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1375         }
1376
1377         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1378         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1379         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1380
1381         if (!cli_send_trans(cli, SMBtrans2,
1382                         NULL,                        /* name */
1383                         -1, 0,                          /* fid, flags */
1384                         &setup, 1, 0,                   /* setup, length, max */
1385                         param, param_len, 2,            /* param, length, max */
1386                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1387                         )) {
1388                 cli->timeout = saved_timeout;
1389                 return False;
1390         }
1391
1392         if (!cli_receive_trans(cli, SMBtrans2,
1393                                 &rparam, &param_len,
1394                                 &rdata, &data_len)) {
1395                 cli->timeout = saved_timeout;
1396                 SAFE_FREE(rdata);
1397                 SAFE_FREE(rparam);
1398                 return False;
1399         }
1400
1401         cli->timeout = saved_timeout;
1402
1403         SAFE_FREE(rdata);
1404         SAFE_FREE(rparam);
1405
1406         return True;
1407 }
1408
1409 /****************************************************************************
1410  POSIX Lock a file.
1411 ****************************************************************************/
1412
1413 bool cli_posix_lock(struct cli_state *cli, int fnum,
1414                         uint64_t offset, uint64_t len,
1415                         bool wait_lock, enum brl_type lock_type)
1416 {
1417         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1418                 return False;
1419         }
1420         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1421 }
1422
1423 /****************************************************************************
1424  POSIX Unlock a file.
1425 ****************************************************************************/
1426
1427 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1428 {
1429         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1430 }
1431
1432 /****************************************************************************
1433  POSIX Get any lock covering a file.
1434 ****************************************************************************/
1435
1436 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1437 {
1438         return True;
1439 }
1440
1441 /****************************************************************************
1442  Do a SMBgetattrE call.
1443 ****************************************************************************/
1444
1445 bool cli_getattrE(struct cli_state *cli, int fd,
1446                   uint16 *attr, SMB_OFF_T *size,
1447                   time_t *change_time,
1448                   time_t *access_time,
1449                   time_t *write_time)
1450 {
1451         memset(cli->outbuf,'\0',smb_size);
1452         memset(cli->inbuf,'\0',smb_size);
1453
1454         cli_set_message(cli->outbuf,1,0,True);
1455
1456         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1457         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1458         cli_setup_packet(cli);
1459
1460         SSVAL(cli->outbuf,smb_vwv0,fd);
1461
1462         cli_send_smb(cli);
1463         if (!cli_receive_smb(cli)) {
1464                 return False;
1465         }
1466
1467         if (cli_is_error(cli)) {
1468                 return False;
1469         }
1470
1471         if (size) {
1472                 *size = IVAL(cli->inbuf, smb_vwv6);
1473         }
1474
1475         if (attr) {
1476                 *attr = SVAL(cli->inbuf,smb_vwv10);
1477         }
1478
1479         if (change_time) {
1480                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1481         }
1482
1483         if (access_time) {
1484                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1485         }
1486
1487         if (write_time) {
1488                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1489         }
1490
1491         return True;
1492 }
1493
1494 /****************************************************************************
1495  Do a SMBgetatr call
1496 ****************************************************************************/
1497
1498 bool cli_getatr(struct cli_state *cli, const char *fname,
1499                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1500 {
1501         char *p;
1502
1503         memset(cli->outbuf,'\0',smb_size);
1504         memset(cli->inbuf,'\0',smb_size);
1505
1506         cli_set_message(cli->outbuf,0,0,True);
1507
1508         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1509         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1510         cli_setup_packet(cli);
1511
1512         p = smb_buf(cli->outbuf);
1513         *p++ = 4;
1514         p += clistr_push(cli, p, fname,
1515                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1516
1517         cli_setup_bcc(cli, p);
1518
1519         cli_send_smb(cli);
1520         if (!cli_receive_smb(cli)) {
1521                 return False;
1522         }
1523
1524         if (cli_is_error(cli)) {
1525                 return False;
1526         }
1527
1528         if (size) {
1529                 *size = IVAL(cli->inbuf, smb_vwv3);
1530         }
1531
1532         if (write_time) {
1533                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1534         }
1535
1536         if (attr) {
1537                 *attr = SVAL(cli->inbuf,smb_vwv0);
1538         }
1539
1540         return True;
1541 }
1542
1543 /****************************************************************************
1544  Do a SMBsetattrE call.
1545 ****************************************************************************/
1546
1547 bool cli_setattrE(struct cli_state *cli, int fd,
1548                   time_t change_time,
1549                   time_t access_time,
1550                   time_t write_time)
1551
1552 {
1553         char *p;
1554
1555         memset(cli->outbuf,'\0',smb_size);
1556         memset(cli->inbuf,'\0',smb_size);
1557
1558         cli_set_message(cli->outbuf,7,0,True);
1559
1560         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1561         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1562         cli_setup_packet(cli);
1563
1564         SSVAL(cli->outbuf,smb_vwv0, fd);
1565         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1566         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1567         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1568
1569         p = smb_buf(cli->outbuf);
1570         *p++ = 4;
1571
1572         cli_setup_bcc(cli, p);
1573
1574         cli_send_smb(cli);
1575         if (!cli_receive_smb(cli)) {
1576                 return False;
1577         }
1578
1579         if (cli_is_error(cli)) {
1580                 return False;
1581         }
1582
1583         return True;
1584 }
1585
1586 /****************************************************************************
1587  Do a SMBsetatr call.
1588 ****************************************************************************/
1589
1590 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1591 {
1592         char *p;
1593
1594         memset(cli->outbuf,'\0',smb_size);
1595         memset(cli->inbuf,'\0',smb_size);
1596
1597         cli_set_message(cli->outbuf,8,0,True);
1598
1599         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1600         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1601         cli_setup_packet(cli);
1602
1603         SSVAL(cli->outbuf,smb_vwv0, attr);
1604         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1605
1606         p = smb_buf(cli->outbuf);
1607         *p++ = 4;
1608         p += clistr_push(cli, p, fname,
1609                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1610         *p++ = 4;
1611
1612         cli_setup_bcc(cli, p);
1613
1614         cli_send_smb(cli);
1615         if (!cli_receive_smb(cli)) {
1616                 return False;
1617         }
1618
1619         if (cli_is_error(cli)) {
1620                 return False;
1621         }
1622
1623         return True;
1624 }
1625
1626 /****************************************************************************
1627  Check for existance of a dir.
1628 ****************************************************************************/
1629
1630 bool cli_chkpath(struct cli_state *cli, const char *path)
1631 {
1632         char *path2 = NULL;
1633         char *p;
1634         TALLOC_CTX *frame = talloc_stackframe();
1635
1636         path2 = talloc_strdup(frame, path);
1637         if (!path2) {
1638                 TALLOC_FREE(frame);
1639                 return false;
1640         }
1641         trim_char(path2,'\0','\\');
1642         if (!*path2) {
1643                 path2 = talloc_strdup(frame, "\\");
1644                 if (!path2) {
1645                         TALLOC_FREE(frame);
1646                         return false;
1647                 }
1648         }
1649
1650         memset(cli->outbuf,'\0',smb_size);
1651         cli_set_message(cli->outbuf,0,0,True);
1652         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1653         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1654         cli_setup_packet(cli);
1655         p = smb_buf(cli->outbuf);
1656         *p++ = 4;
1657         p += clistr_push(cli, p, path2,
1658                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1659
1660         cli_setup_bcc(cli, p);
1661
1662         cli_send_smb(cli);
1663         if (!cli_receive_smb(cli)) {
1664                 TALLOC_FREE(frame);
1665                 return False;
1666         }
1667
1668         TALLOC_FREE(frame);
1669
1670         if (cli_is_error(cli)) return False;
1671
1672         return True;
1673 }
1674
1675 /****************************************************************************
1676  Query disk space.
1677 ****************************************************************************/
1678
1679 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1680 {
1681         memset(cli->outbuf,'\0',smb_size);
1682         cli_set_message(cli->outbuf,0,0,True);
1683         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1684         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1685         cli_setup_packet(cli);
1686
1687         cli_send_smb(cli);
1688         if (!cli_receive_smb(cli)) {
1689                 return False;
1690         }
1691
1692         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1693         *total = SVAL(cli->inbuf,smb_vwv0);
1694         *avail = SVAL(cli->inbuf,smb_vwv3);
1695
1696         return True;
1697 }
1698
1699 /****************************************************************************
1700  Create and open a temporary file.
1701 ****************************************************************************/
1702
1703 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1704 {
1705         int len;
1706         char *p;
1707
1708         memset(cli->outbuf,'\0',smb_size);
1709         memset(cli->inbuf,'\0',smb_size);
1710
1711         cli_set_message(cli->outbuf,3,0,True);
1712
1713         SCVAL(cli->outbuf,smb_com,SMBctemp);
1714         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1715         cli_setup_packet(cli);
1716
1717         SSVAL(cli->outbuf,smb_vwv0,0);
1718         SIVALS(cli->outbuf,smb_vwv1,-1);
1719
1720         p = smb_buf(cli->outbuf);
1721         *p++ = 4;
1722         p += clistr_push(cli, p, path,
1723                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1724
1725         cli_setup_bcc(cli, p);
1726
1727         cli_send_smb(cli);
1728         if (!cli_receive_smb(cli)) {
1729                 return -1;
1730         }
1731
1732         if (cli_is_error(cli)) {
1733                 return -1;
1734         }
1735
1736         /* despite the spec, the result has a -1, followed by
1737            length, followed by name */
1738         p = smb_buf(cli->inbuf);
1739         p += 4;
1740         len = smb_buflen(cli->inbuf) - 4;
1741         if (len <= 0 || len > PATH_MAX) return -1;
1742
1743         if (tmp_path) {
1744                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1745                 if (!path2) {
1746                         return -1;
1747                 }
1748                 clistr_pull(cli, path2, p,
1749                             len+1, len, STR_ASCII);
1750                 *tmp_path = path2;
1751         }
1752
1753         return SVAL(cli->inbuf,smb_vwv0);
1754 }
1755
1756 /*
1757    send a raw ioctl - used by the torture code
1758 */
1759 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1760 {
1761         memset(cli->outbuf,'\0',smb_size);
1762         memset(cli->inbuf,'\0',smb_size);
1763
1764         cli_set_message(cli->outbuf, 3, 0, True);
1765         SCVAL(cli->outbuf,smb_com,SMBioctl);
1766         cli_setup_packet(cli);
1767
1768         SSVAL(cli->outbuf, smb_vwv0, fnum);
1769         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1770         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1771
1772         cli_send_smb(cli);
1773         if (!cli_receive_smb(cli)) {
1774                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1775         }
1776
1777         if (cli_is_error(cli)) {
1778                 return cli_nt_error(cli);
1779         }
1780
1781         *blob = data_blob_null;
1782
1783         return NT_STATUS_OK;
1784 }
1785
1786 /*********************************************************
1787  Set an extended attribute utility fn.
1788 *********************************************************/
1789
1790 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1791                         const char *ea_name, const char *ea_val, size_t ea_len)
1792 {
1793         unsigned int data_len = 0;
1794         char *data = NULL;
1795         char *rparam=NULL, *rdata=NULL;
1796         char *p;
1797         size_t ea_namelen = strlen(ea_name);
1798
1799         if (ea_namelen == 0 && ea_len == 0) {
1800                 data_len = 4;
1801                 data = (char *)SMB_MALLOC(data_len);
1802                 if (!data) {
1803                         return False;
1804                 }
1805                 p = data;
1806                 SIVAL(p,0,data_len);
1807         } else {
1808                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1809                 data = (char *)SMB_MALLOC(data_len);
1810                 if (!data) {
1811                         return False;
1812                 }
1813                 p = data;
1814                 SIVAL(p,0,data_len);
1815                 p += 4;
1816                 SCVAL(p, 0, 0); /* EA flags. */
1817                 SCVAL(p, 1, ea_namelen);
1818                 SSVAL(p, 2, ea_len);
1819                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1820                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1821         }
1822
1823         if (!cli_send_trans(cli, SMBtrans2,
1824                         NULL,                        /* name */
1825                         -1, 0,                          /* fid, flags */
1826                         &setup, 1, 0,                   /* setup, length, max */
1827                         param, param_len, 2,            /* param, length, max */
1828                         data,  data_len, cli->max_xmit /* data, length, max */
1829                         )) {
1830                 SAFE_FREE(data);
1831                 return False;
1832         }
1833
1834         if (!cli_receive_trans(cli, SMBtrans2,
1835                         &rparam, &param_len,
1836                         &rdata, &data_len)) {
1837                         SAFE_FREE(data);
1838                 return false;
1839         }
1840
1841         SAFE_FREE(data);
1842         SAFE_FREE(rdata);
1843         SAFE_FREE(rparam);
1844
1845         return True;
1846 }
1847
1848 /*********************************************************
1849  Set an extended attribute on a pathname.
1850 *********************************************************/
1851
1852 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1853 {
1854         uint16 setup = TRANSACT2_SETPATHINFO;
1855         unsigned int param_len = 0;
1856         char *param;
1857         size_t srclen = 2*(strlen(path)+1);
1858         char *p;
1859         bool ret;
1860
1861         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1862         if (!param) {
1863                 return false;
1864         }
1865         memset(param, '\0', 6);
1866         SSVAL(param,0,SMB_INFO_SET_EA);
1867         p = &param[6];
1868
1869         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1870         param_len = PTR_DIFF(p, param);
1871
1872         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1873         SAFE_FREE(param);
1874         return ret;
1875 }
1876
1877 /*********************************************************
1878  Set an extended attribute on an fnum.
1879 *********************************************************/
1880
1881 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1882 {
1883         char param[6];
1884         uint16 setup = TRANSACT2_SETFILEINFO;
1885
1886         memset(param, 0, 6);
1887         SSVAL(param,0,fnum);
1888         SSVAL(param,2,SMB_INFO_SET_EA);
1889
1890         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1891 }
1892
1893 /*********************************************************
1894  Get an extended attribute list utility fn.
1895 *********************************************************/
1896
1897 static bool cli_get_ea_list(struct cli_state *cli,
1898                 uint16 setup, char *param, unsigned int param_len,
1899                 TALLOC_CTX *ctx,
1900                 size_t *pnum_eas,
1901                 struct ea_struct **pea_list)
1902 {
1903         unsigned int data_len = 0;
1904         unsigned int rparam_len, rdata_len;
1905         char *rparam=NULL, *rdata=NULL;
1906         char *p;
1907         size_t ea_size;
1908         size_t num_eas;
1909         bool ret = False;
1910         struct ea_struct *ea_list;
1911
1912         *pnum_eas = 0;
1913         if (pea_list) {
1914                 *pea_list = NULL;
1915         }
1916
1917         if (!cli_send_trans(cli, SMBtrans2,
1918                         NULL,           /* Name */
1919                         -1, 0,          /* fid, flags */
1920                         &setup, 1, 0,   /* setup, length, max */
1921                         param, param_len, 10, /* param, length, max */
1922                         NULL, data_len, cli->max_xmit /* data, length, max */
1923                                 )) {
1924                 return False;
1925         }
1926
1927         if (!cli_receive_trans(cli, SMBtrans2,
1928                         &rparam, &rparam_len,
1929                         &rdata, &rdata_len)) {
1930                 return False;
1931         }
1932
1933         if (!rdata || rdata_len < 4) {
1934                 goto out;
1935         }
1936
1937         ea_size = (size_t)IVAL(rdata,0);
1938         if (ea_size > rdata_len) {
1939                 goto out;
1940         }
1941
1942         if (ea_size == 0) {
1943                 /* No EA's present. */
1944                 ret = True;
1945                 goto out;
1946         }
1947
1948         p = rdata + 4;
1949         ea_size -= 4;
1950
1951         /* Validate the EA list and count it. */
1952         for (num_eas = 0; ea_size >= 4; num_eas++) {
1953                 unsigned int ea_namelen = CVAL(p,1);
1954                 unsigned int ea_valuelen = SVAL(p,2);
1955                 if (ea_namelen == 0) {
1956                         goto out;
1957                 }
1958                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1959                         goto out;
1960                 }
1961                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1962                 p += 4 + ea_namelen + 1 + ea_valuelen;
1963         }
1964
1965         if (num_eas == 0) {
1966                 ret = True;
1967                 goto out;
1968         }
1969
1970         *pnum_eas = num_eas;
1971         if (!pea_list) {
1972                 /* Caller only wants number of EA's. */
1973                 ret = True;
1974                 goto out;
1975         }
1976
1977         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1978         if (!ea_list) {
1979                 goto out;
1980         }
1981
1982         ea_size = (size_t)IVAL(rdata,0);
1983         p = rdata + 4;
1984
1985         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1986                 struct ea_struct *ea = &ea_list[num_eas];
1987                 fstring unix_ea_name;
1988                 unsigned int ea_namelen = CVAL(p,1);
1989                 unsigned int ea_valuelen = SVAL(p,2);
1990
1991                 ea->flags = CVAL(p,0);
1992                 unix_ea_name[0] = '\0';
1993                 pull_ascii_fstring(unix_ea_name, p + 4);
1994                 ea->name = talloc_strdup(ctx, unix_ea_name);
1995                 /* Ensure the value is null terminated (in case it's a string). */
1996                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1997                 if (!ea->value.data) {
1998                         goto out;
1999                 }
2000                 if (ea_valuelen) {
2001                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2002                 }
2003                 ea->value.data[ea_valuelen] = 0;
2004                 ea->value.length--;
2005                 p += 4 + ea_namelen + 1 + ea_valuelen;
2006         }
2007
2008         *pea_list = ea_list;
2009         ret = True;
2010
2011  out :
2012
2013         SAFE_FREE(rdata);
2014         SAFE_FREE(rparam);
2015         return ret;
2016 }
2017
2018 /*********************************************************
2019  Get an extended attribute list from a pathname.
2020 *********************************************************/
2021
2022 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2023                 TALLOC_CTX *ctx,
2024                 size_t *pnum_eas,
2025                 struct ea_struct **pea_list)
2026 {
2027         uint16 setup = TRANSACT2_QPATHINFO;
2028         unsigned int param_len = 0;
2029         char *param;
2030         char *p;
2031         size_t srclen = 2*(strlen(path)+1);
2032         bool ret;
2033
2034         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2035         if (!param) {
2036                 return false;
2037         }
2038         p = param;
2039         memset(p, 0, 6);
2040         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2041         p += 6;
2042         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2043         param_len = PTR_DIFF(p, param);
2044
2045         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2046         SAFE_FREE(param);
2047         return ret;
2048 }
2049
2050 /*********************************************************
2051  Get an extended attribute list from an fnum.
2052 *********************************************************/
2053
2054 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2055                 TALLOC_CTX *ctx,
2056                 size_t *pnum_eas,
2057                 struct ea_struct **pea_list)
2058 {
2059         uint16 setup = TRANSACT2_QFILEINFO;
2060         char param[6];
2061
2062         memset(param, 0, 6);
2063         SSVAL(param,0,fnum);
2064         SSVAL(param,2,SMB_INFO_SET_EA);
2065
2066         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2067 }
2068
2069 /****************************************************************************
2070  Convert open "flags" arg to uint32 on wire.
2071 ****************************************************************************/
2072
2073 static uint32 open_flags_to_wire(int flags)
2074 {
2075         int open_mode = flags & O_ACCMODE;
2076         uint32 ret = 0;
2077
2078         switch (open_mode) {
2079                 case O_WRONLY:
2080                         ret |= SMB_O_WRONLY;
2081                         break;
2082                 case O_RDWR:
2083                         ret |= SMB_O_RDWR;
2084                         break;
2085                 default:
2086                 case O_RDONLY:
2087                         ret |= SMB_O_RDONLY;
2088                         break;
2089         }
2090
2091         if (flags & O_CREAT) {
2092                 ret |= SMB_O_CREAT;
2093         }
2094         if (flags & O_EXCL) {
2095                 ret |= SMB_O_EXCL;
2096         }
2097         if (flags & O_TRUNC) {
2098                 ret |= SMB_O_TRUNC;
2099         }
2100 #if defined(O_SYNC)
2101         if (flags & O_SYNC) {
2102                 ret |= SMB_O_SYNC;
2103         }
2104 #endif /* O_SYNC */
2105         if (flags & O_APPEND) {
2106                 ret |= SMB_O_APPEND;
2107         }
2108 #if defined(O_DIRECT)
2109         if (flags & O_DIRECT) {
2110                 ret |= SMB_O_DIRECT;
2111         }
2112 #endif
2113 #if defined(O_DIRECTORY)
2114         if (flags & O_DIRECTORY) {
2115                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2116                 ret |= SMB_O_DIRECTORY;
2117         }
2118 #endif
2119         return ret;
2120 }
2121
2122 /****************************************************************************
2123  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2124 ****************************************************************************/
2125
2126 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2127 {
2128         unsigned int data_len = 0;
2129         unsigned int param_len = 0;
2130         uint16 setup = TRANSACT2_SETPATHINFO;
2131         char *param;
2132         char data[18];
2133         char *rparam=NULL, *rdata=NULL;
2134         char *p;
2135         int fnum = -1;
2136         uint32 wire_flags = open_flags_to_wire(flags);
2137         size_t srclen = 2*(strlen(fname)+1);
2138
2139         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2140         if (!param) {
2141                 return false;
2142         }
2143         memset(param, '\0', 6);
2144         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2145         p = &param[6];
2146
2147         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2148         param_len = PTR_DIFF(p, param);
2149
2150         if (is_dir) {
2151                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2152                 wire_flags |= SMB_O_DIRECTORY;
2153         }
2154
2155         p = data;
2156         SIVAL(p,0,0); /* No oplock. */
2157         SIVAL(p,4,wire_flags);
2158         SIVAL(p,8,unix_perms_to_wire(mode));
2159         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2160         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2161
2162         data_len = 18;
2163
2164         if (!cli_send_trans(cli, SMBtrans2,
2165                         NULL,                        /* name */
2166                         -1, 0,                          /* fid, flags */
2167                         &setup, 1, 0,                   /* setup, length, max */
2168                         param, param_len, 2,            /* param, length, max */
2169                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2170                         )) {
2171                 SAFE_FREE(param);
2172                 return -1;
2173         }
2174
2175         SAFE_FREE(param);
2176
2177         if (!cli_receive_trans(cli, SMBtrans2,
2178                 &rparam, &param_len,
2179                 &rdata, &data_len)) {
2180                         return -1;
2181         }
2182
2183         fnum = SVAL(rdata,2);
2184
2185         SAFE_FREE(rdata);
2186         SAFE_FREE(rparam);
2187
2188         return fnum;
2189 }
2190
2191 /****************************************************************************
2192  open - POSIX semantics.
2193 ****************************************************************************/
2194
2195 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2196 {
2197         return cli_posix_open_internal(cli, fname, flags, mode, False);
2198 }
2199
2200 /****************************************************************************
2201  mkdir - POSIX semantics.
2202 ****************************************************************************/
2203
2204 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2205 {
2206         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2207 }
2208
2209 /****************************************************************************
2210  unlink or rmdir - POSIX semantics.
2211 ****************************************************************************/
2212
2213 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2214 {
2215         unsigned int data_len = 0;
2216         unsigned int param_len = 0;
2217         uint16 setup = TRANSACT2_SETPATHINFO;
2218         char *param;
2219         char data[2];
2220         char *rparam=NULL, *rdata=NULL;
2221         char *p;
2222         size_t srclen = 2*(strlen(fname)+1);
2223
2224         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2225         if (!param) {
2226                 return false;
2227         }
2228         memset(param, '\0', 6);
2229         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2230         p = &param[6];
2231
2232         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2233         param_len = PTR_DIFF(p, param);
2234
2235         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2236                         SMB_POSIX_UNLINK_FILE_TARGET);
2237         data_len = 2;
2238
2239         if (!cli_send_trans(cli, SMBtrans2,
2240                         NULL,                        /* name */
2241                         -1, 0,                          /* fid, flags */
2242                         &setup, 1, 0,                   /* setup, length, max */
2243                         param, param_len, 2,            /* param, length, max */
2244                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2245                         )) {
2246                 SAFE_FREE(param);
2247                 return False;
2248         }
2249
2250         SAFE_FREE(param);
2251
2252         if (!cli_receive_trans(cli, SMBtrans2,
2253                 &rparam, &param_len,
2254                 &rdata, &data_len)) {
2255                         return False;
2256         }
2257
2258         SAFE_FREE(rdata);
2259         SAFE_FREE(rparam);
2260
2261         return True;
2262 }
2263
2264 /****************************************************************************
2265  unlink - POSIX semantics.
2266 ****************************************************************************/
2267
2268 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2269 {
2270         return cli_posix_unlink_internal(cli, fname, False);
2271 }
2272
2273 /****************************************************************************
2274  rmdir - POSIX semantics.
2275 ****************************************************************************/
2276
2277 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2278 {
2279         return cli_posix_unlink_internal(cli, fname, True);
2280 }