a4e70c53cde9f4a4621b9f4bf21f36f8279075b7
[jra/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-2009
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_t 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_t 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_t 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_t 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_t 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_t 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_t dev_major = IVAL(rdata,60);
315                 uint32_t 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_t mode, uint32_t uid, uint32_t gid)
352 {
353         unsigned int data_len = 0;
354         unsigned int param_len = 0;
355         uint16_t 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 static void cli_rename_done(struct tevent_req *subreq);
433
434 struct cli_rename_state {
435         uint16_t vwv[1];
436 };
437
438 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
439                                 struct event_context *ev,
440                                 struct cli_state *cli,
441                                 const char *fname_src,
442                                 const char *fname_dst)
443 {
444         struct tevent_req *req = NULL, *subreq = NULL;
445         struct cli_rename_state *state = NULL;
446         uint8_t additional_flags = 0;
447         uint8_t *bytes = NULL;
448
449         req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
450         if (req == NULL) {
451                 return NULL;
452         }
453
454         SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
455
456         bytes = talloc_array(state, uint8_t, 1);
457         if (tevent_req_nomem(bytes, req)) {
458                 return tevent_req_post(req, ev);
459         }
460         bytes[0] = 4;
461         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
462                                    strlen(fname_src)+1, NULL);
463         if (tevent_req_nomem(bytes, req)) {
464                 return tevent_req_post(req, ev);
465         }
466
467         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
468                         talloc_get_size(bytes)+1);
469         if (tevent_req_nomem(bytes, req)) {
470                 return tevent_req_post(req, ev);
471         }
472
473         bytes[talloc_get_size(bytes)-1] = 4;
474         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
475                                    strlen(fname_dst)+1, NULL);
476         if (tevent_req_nomem(bytes, req)) {
477                 return tevent_req_post(req, ev);
478         }
479
480         subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
481                               1, state->vwv, talloc_get_size(bytes), bytes);
482         if (tevent_req_nomem(subreq, req)) {
483                 return tevent_req_post(req, ev);
484         }
485         tevent_req_set_callback(subreq, cli_rename_done, req);
486         return req;
487 }
488
489 static void cli_rename_done(struct tevent_req *subreq)
490 {
491         struct tevent_req *req = tevent_req_callback_data(
492                                 subreq, struct tevent_req);
493         NTSTATUS status;
494
495         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
496         TALLOC_FREE(subreq);
497         if (!NT_STATUS_IS_OK(status)) {
498                 tevent_req_nterror(req, status);
499                 return;
500         }
501         tevent_req_done(req);
502 }
503
504 NTSTATUS cli_rename_recv(struct tevent_req *req)
505 {
506         return tevent_req_simple_recv_ntstatus(req);
507 }
508
509 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
510 {
511         TALLOC_CTX *frame = talloc_stackframe();
512         struct event_context *ev;
513         struct tevent_req *req;
514         NTSTATUS status = NT_STATUS_OK;
515
516         if (cli_has_async_calls(cli)) {
517                 /*
518                  * Can't use sync call while an async call is in flight
519                  */
520                 status = NT_STATUS_INVALID_PARAMETER;
521                 goto fail;
522         }
523
524         ev = event_context_init(frame);
525         if (ev == NULL) {
526                 status = NT_STATUS_NO_MEMORY;
527                 goto fail;
528         }
529
530         req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
531         if (req == NULL) {
532                 status = NT_STATUS_NO_MEMORY;
533                 goto fail;
534         }
535
536         if (!tevent_req_poll(req, ev)) {
537                 status = map_nt_error_from_unix(errno);
538                 goto fail;
539         }
540
541         status = cli_rename_recv(req);
542
543  fail:
544         TALLOC_FREE(frame);
545         if (!NT_STATUS_IS_OK(status)) {
546                 cli_set_error(cli, status);
547         }
548         return status;
549 }
550
551 /****************************************************************************
552  NT Rename a file.
553 ****************************************************************************/
554
555 static void cli_ntrename_done(struct tevent_req *subreq);
556
557 struct cli_ntrename_state {
558         uint16_t vwv[4];
559 };
560
561 static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
562                                 struct event_context *ev,
563                                 struct cli_state *cli,
564                                 const char *fname_src,
565                                 const char *fname_dst,
566                                 uint16_t rename_flag)
567 {
568         struct tevent_req *req = NULL, *subreq = NULL;
569         struct cli_ntrename_state *state = NULL;
570         uint8_t additional_flags = 0;
571         uint8_t *bytes = NULL;
572
573         req = tevent_req_create(mem_ctx, &state, struct cli_ntrename_state);
574         if (req == NULL) {
575                 return NULL;
576         }
577
578         SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
579         SSVAL(state->vwv+1, 0, rename_flag);
580
581         bytes = talloc_array(state, uint8_t, 1);
582         if (tevent_req_nomem(bytes, req)) {
583                 return tevent_req_post(req, ev);
584         }
585         bytes[0] = 4;
586         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
587                                    strlen(fname_src)+1, NULL);
588         if (tevent_req_nomem(bytes, req)) {
589                 return tevent_req_post(req, ev);
590         }
591
592         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
593                         talloc_get_size(bytes)+1);
594         if (tevent_req_nomem(bytes, req)) {
595                 return tevent_req_post(req, ev);
596         }
597
598         bytes[talloc_get_size(bytes)-1] = 4;
599         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
600                                    strlen(fname_dst)+1, NULL);
601         if (tevent_req_nomem(bytes, req)) {
602                 return tevent_req_post(req, ev);
603         }
604
605         subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
606                               4, state->vwv, talloc_get_size(bytes), bytes);
607         if (tevent_req_nomem(subreq, req)) {
608                 return tevent_req_post(req, ev);
609         }
610         tevent_req_set_callback(subreq, cli_ntrename_done, req);
611         return req;
612 }
613
614 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
615                                 struct event_context *ev,
616                                 struct cli_state *cli,
617                                 const char *fname_src,
618                                 const char *fname_dst)
619 {
620         return cli_ntrename_send_internal(mem_ctx,
621                                         ev,
622                                         cli,
623                                         fname_src,
624                                         fname_dst,
625                                         RENAME_FLAG_RENAME);
626 }
627
628 static void cli_ntrename_done(struct tevent_req *subreq)
629 {
630         struct tevent_req *req = tevent_req_callback_data(
631                                 subreq, struct tevent_req);
632         NTSTATUS status;
633
634         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
635         TALLOC_FREE(subreq);
636         if (!NT_STATUS_IS_OK(status)) {
637                 tevent_req_nterror(req, status);
638                 return;
639         }
640         tevent_req_done(req);
641 }
642
643 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
644 {
645         return tevent_req_simple_recv_ntstatus(req);
646 }
647
648 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
649 {
650         TALLOC_CTX *frame = talloc_stackframe();
651         struct event_context *ev;
652         struct tevent_req *req;
653         NTSTATUS status = NT_STATUS_OK;
654
655         if (cli_has_async_calls(cli)) {
656                 /*
657                  * Can't use sync call while an async call is in flight
658                  */
659                 status = NT_STATUS_INVALID_PARAMETER;
660                 goto fail;
661         }
662
663         ev = event_context_init(frame);
664         if (ev == NULL) {
665                 status = NT_STATUS_NO_MEMORY;
666                 goto fail;
667         }
668
669         req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
670         if (req == NULL) {
671                 status = NT_STATUS_NO_MEMORY;
672                 goto fail;
673         }
674
675         if (!tevent_req_poll(req, ev)) {
676                 status = map_nt_error_from_unix(errno);
677                 goto fail;
678         }
679
680         status = cli_ntrename_recv(req);
681
682  fail:
683         TALLOC_FREE(frame);
684         if (!NT_STATUS_IS_OK(status)) {
685                 cli_set_error(cli, status);
686         }
687         return status;
688 }
689
690 /****************************************************************************
691  NT hardlink a file.
692 ****************************************************************************/
693
694 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
695                                 struct event_context *ev,
696                                 struct cli_state *cli,
697                                 const char *fname_src,
698                                 const char *fname_dst)
699 {
700         return cli_ntrename_send_internal(mem_ctx,
701                                         ev,
702                                         cli,
703                                         fname_src,
704                                         fname_dst,
705                                         RENAME_FLAG_HARD_LINK);
706 }
707
708 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
709 {
710         return tevent_req_simple_recv_ntstatus(req);
711 }
712
713 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
714 {
715         TALLOC_CTX *frame = talloc_stackframe();
716         struct event_context *ev;
717         struct tevent_req *req;
718         NTSTATUS status = NT_STATUS_OK;
719
720         if (cli_has_async_calls(cli)) {
721                 /*
722                  * Can't use sync call while an async call is in flight
723                  */
724                 status = NT_STATUS_INVALID_PARAMETER;
725                 goto fail;
726         }
727
728         ev = event_context_init(frame);
729         if (ev == NULL) {
730                 status = NT_STATUS_NO_MEMORY;
731                 goto fail;
732         }
733
734         req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
735         if (req == NULL) {
736                 status = NT_STATUS_NO_MEMORY;
737                 goto fail;
738         }
739
740         if (!tevent_req_poll(req, ev)) {
741                 status = map_nt_error_from_unix(errno);
742                 goto fail;
743         }
744
745         status = cli_nt_hardlink_recv(req);
746
747  fail:
748         TALLOC_FREE(frame);
749         if (!NT_STATUS_IS_OK(status)) {
750                 cli_set_error(cli, status);
751         }
752         return status;
753 }
754
755 /****************************************************************************
756  Delete a file.
757 ****************************************************************************/
758
759 static void cli_unlink_done(struct tevent_req *subreq);
760
761 struct cli_unlink_state {
762         uint16_t vwv[1];
763 };
764
765 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
766                                 struct event_context *ev,
767                                 struct cli_state *cli,
768                                 const char *fname,
769                                 uint16_t mayhave_attrs)
770 {
771         struct tevent_req *req = NULL, *subreq = NULL;
772         struct cli_unlink_state *state = NULL;
773         uint8_t additional_flags = 0;
774         uint8_t *bytes = NULL;
775
776         req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
777         if (req == NULL) {
778                 return NULL;
779         }
780
781         SSVAL(state->vwv+0, 0, mayhave_attrs);
782
783         bytes = talloc_array(state, uint8_t, 1);
784         if (tevent_req_nomem(bytes, req)) {
785                 return tevent_req_post(req, ev);
786         }
787         bytes[0] = 4;
788         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
789                                    strlen(fname)+1, NULL);
790
791         if (tevent_req_nomem(bytes, req)) {
792                 return tevent_req_post(req, ev);
793         }
794
795         subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
796                                 1, state->vwv, talloc_get_size(bytes), bytes);
797         if (tevent_req_nomem(subreq, req)) {
798                 return tevent_req_post(req, ev);
799         }
800         tevent_req_set_callback(subreq, cli_unlink_done, req);
801         return req;
802 }
803
804 static void cli_unlink_done(struct tevent_req *subreq)
805 {
806         struct tevent_req *req = tevent_req_callback_data(
807                 subreq, struct tevent_req);
808         NTSTATUS status;
809
810         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
811         TALLOC_FREE(subreq);
812         if (!NT_STATUS_IS_OK(status)) {
813                 tevent_req_nterror(req, status);
814                 return;
815         }
816         tevent_req_done(req);
817 }
818
819 NTSTATUS cli_unlink_recv(struct tevent_req *req)
820 {
821         return tevent_req_simple_recv_ntstatus(req);
822 }
823
824 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
825 {
826         TALLOC_CTX *frame = talloc_stackframe();
827         struct event_context *ev;
828         struct tevent_req *req;
829         NTSTATUS status = NT_STATUS_OK;
830
831         if (cli_has_async_calls(cli)) {
832                 /*
833                  * Can't use sync call while an async call is in flight
834                  */
835                 status = NT_STATUS_INVALID_PARAMETER;
836                 goto fail;
837         }
838
839         ev = event_context_init(frame);
840         if (ev == NULL) {
841                 status = NT_STATUS_NO_MEMORY;
842                 goto fail;
843         }
844
845         req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
846         if (req == NULL) {
847                 status = NT_STATUS_NO_MEMORY;
848                 goto fail;
849         }
850
851         if (!tevent_req_poll(req, ev)) {
852                 status = map_nt_error_from_unix(errno);
853                 goto fail;
854         }
855
856         status = cli_unlink_recv(req);
857
858  fail:
859         TALLOC_FREE(frame);
860         if (!NT_STATUS_IS_OK(status)) {
861                 cli_set_error(cli, status);
862         }
863         return status;
864 }
865
866 /****************************************************************************
867  Create a directory.
868 ****************************************************************************/
869
870 static void cli_mkdir_done(struct tevent_req *subreq);
871
872 struct cli_mkdir_state {
873         int dummy;
874 };
875
876 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
877                                   struct event_context *ev,
878                                   struct cli_state *cli,
879                                   const char *dname)
880 {
881         struct tevent_req *req = NULL, *subreq = NULL;
882         struct cli_mkdir_state *state = NULL;
883         uint8_t additional_flags = 0;
884         uint8_t *bytes = NULL;
885
886         req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
887         if (req == NULL) {
888                 return NULL;
889         }
890
891         bytes = talloc_array(state, uint8_t, 1);
892         if (tevent_req_nomem(bytes, req)) {
893                 return tevent_req_post(req, ev);
894         }
895         bytes[0] = 4;
896         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
897                                    strlen(dname)+1, NULL);
898
899         if (tevent_req_nomem(bytes, req)) {
900                 return tevent_req_post(req, ev);
901         }
902
903         subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
904                               0, NULL, talloc_get_size(bytes), bytes);
905         if (tevent_req_nomem(subreq, req)) {
906                 return tevent_req_post(req, ev);
907         }
908         tevent_req_set_callback(subreq, cli_mkdir_done, req);
909         return req;
910 }
911
912 static void cli_mkdir_done(struct tevent_req *subreq)
913 {
914         struct tevent_req *req = tevent_req_callback_data(
915                 subreq, struct tevent_req);
916         NTSTATUS status;
917
918         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
919         TALLOC_FREE(subreq);
920         if (!NT_STATUS_IS_OK(status)) {
921                 tevent_req_nterror(req, status);
922                 return;
923         }
924         tevent_req_done(req);
925 }
926
927 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
928 {
929         return tevent_req_simple_recv_ntstatus(req);
930 }
931
932 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
933 {
934         TALLOC_CTX *frame = talloc_stackframe();
935         struct event_context *ev;
936         struct tevent_req *req;
937         NTSTATUS status = NT_STATUS_OK;
938
939         if (cli_has_async_calls(cli)) {
940                 /*
941                  * Can't use sync call while an async call is in flight
942                  */
943                 status = NT_STATUS_INVALID_PARAMETER;
944                 goto fail;
945         }
946
947         ev = event_context_init(frame);
948         if (ev == NULL) {
949                 status = NT_STATUS_NO_MEMORY;
950                 goto fail;
951         }
952
953         req = cli_mkdir_send(frame, ev, cli, dname);
954         if (req == NULL) {
955                 status = NT_STATUS_NO_MEMORY;
956                 goto fail;
957         }
958
959         if (!tevent_req_poll(req, ev)) {
960                 status = map_nt_error_from_unix(errno);
961                 goto fail;
962         }
963
964         status = cli_mkdir_recv(req);
965
966  fail:
967         TALLOC_FREE(frame);
968         if (!NT_STATUS_IS_OK(status)) {
969                 cli_set_error(cli, status);
970         }
971         return status;
972 }
973
974 /****************************************************************************
975  Remove a directory.
976 ****************************************************************************/
977
978 static void cli_rmdir_done(struct tevent_req *subreq);
979
980 struct cli_rmdir_state {
981         int dummy;
982 };
983
984 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
985                                   struct event_context *ev,
986                                   struct cli_state *cli,
987                                   const char *dname)
988 {
989         struct tevent_req *req = NULL, *subreq = NULL;
990         struct cli_rmdir_state *state = NULL;
991         uint8_t additional_flags = 0;
992         uint8_t *bytes = NULL;
993
994         req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
995         if (req == NULL) {
996                 return NULL;
997         }
998
999         bytes = talloc_array(state, uint8_t, 1);
1000         if (tevent_req_nomem(bytes, req)) {
1001                 return tevent_req_post(req, ev);
1002         }
1003         bytes[0] = 4;
1004         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1005                                    strlen(dname)+1, NULL);
1006
1007         if (tevent_req_nomem(bytes, req)) {
1008                 return tevent_req_post(req, ev);
1009         }
1010
1011         subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1012                               0, NULL, talloc_get_size(bytes), bytes);
1013         if (tevent_req_nomem(subreq, req)) {
1014                 return tevent_req_post(req, ev);
1015         }
1016         tevent_req_set_callback(subreq, cli_rmdir_done, req);
1017         return req;
1018 }
1019
1020 static void cli_rmdir_done(struct tevent_req *subreq)
1021 {
1022         struct tevent_req *req = tevent_req_callback_data(
1023                 subreq, struct tevent_req);
1024         NTSTATUS status;
1025
1026         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1027         TALLOC_FREE(subreq);
1028         if (!NT_STATUS_IS_OK(status)) {
1029                 tevent_req_nterror(req, status);
1030                 return;
1031         }
1032         tevent_req_done(req);
1033 }
1034
1035 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1036 {
1037         return tevent_req_simple_recv_ntstatus(req);
1038 }
1039
1040 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1041 {
1042         TALLOC_CTX *frame = talloc_stackframe();
1043         struct event_context *ev;
1044         struct tevent_req *req;
1045         NTSTATUS status = NT_STATUS_OK;
1046
1047         if (cli_has_async_calls(cli)) {
1048                 /*
1049                  * Can't use sync call while an async call is in flight
1050                  */
1051                 status = NT_STATUS_INVALID_PARAMETER;
1052                 goto fail;
1053         }
1054
1055         ev = event_context_init(frame);
1056         if (ev == NULL) {
1057                 status = NT_STATUS_NO_MEMORY;
1058                 goto fail;
1059         }
1060
1061         req = cli_rmdir_send(frame, ev, cli, dname);
1062         if (req == NULL) {
1063                 status = NT_STATUS_NO_MEMORY;
1064                 goto fail;
1065         }
1066
1067         if (!tevent_req_poll(req, ev)) {
1068                 status = map_nt_error_from_unix(errno);
1069                 goto fail;
1070         }
1071
1072         status = cli_rmdir_recv(req);
1073
1074  fail:
1075         TALLOC_FREE(frame);
1076         if (!NT_STATUS_IS_OK(status)) {
1077                 cli_set_error(cli, status);
1078         }
1079         return status;
1080 }
1081
1082 /****************************************************************************
1083  Set or clear the delete on close flag.
1084 ****************************************************************************/
1085
1086 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1087 {
1088         unsigned int data_len = 1;
1089         unsigned int param_len = 6;
1090         uint16_t setup = TRANSACT2_SETFILEINFO;
1091         char param[6];
1092         unsigned char data;
1093         char *rparam=NULL, *rdata=NULL;
1094
1095         memset(param, 0, param_len);
1096         SSVAL(param,0,fnum);
1097         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1098
1099         data = flag ? 1 : 0;
1100
1101         if (!cli_send_trans(cli, SMBtrans2,
1102                         NULL,                        /* name */
1103                         -1, 0,                          /* fid, flags */
1104                         &setup, 1, 0,                   /* setup, length, max */
1105                         param, param_len, 2,            /* param, length, max */
1106                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1107                         )) {
1108                 return false;
1109         }
1110
1111         if (!cli_receive_trans(cli, SMBtrans2,
1112                         &rparam, &param_len,
1113                         &rdata, &data_len)) {
1114                 return false;
1115         }
1116
1117         SAFE_FREE(rdata);
1118         SAFE_FREE(rparam);
1119
1120         return true;
1121 }
1122
1123 struct cli_ntcreate_state {
1124         uint16_t vwv[24];
1125         uint16_t fnum;
1126 };
1127
1128 static void cli_ntcreate_done(struct tevent_req *subreq);
1129
1130 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1131                                      struct event_context *ev,
1132                                      struct cli_state *cli,
1133                                      const char *fname,
1134                                      uint32_t CreatFlags,
1135                                      uint32_t DesiredAccess,
1136                                      uint32_t FileAttributes,
1137                                      uint32_t ShareAccess,
1138                                      uint32_t CreateDisposition,
1139                                      uint32_t CreateOptions,
1140                                      uint8_t SecurityFlags)
1141 {
1142         struct tevent_req *req, *subreq;
1143         struct cli_ntcreate_state *state;
1144         uint16_t *vwv;
1145         uint8_t *bytes;
1146         size_t converted_len;
1147
1148         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1149         if (req == NULL) {
1150                 return NULL;
1151         }
1152         vwv = state->vwv;
1153
1154         SCVAL(vwv+0, 0, 0xFF);
1155         SCVAL(vwv+0, 1, 0);
1156         SSVAL(vwv+1, 0, 0);
1157         SCVAL(vwv+2, 0, 0);
1158
1159         if (cli->use_oplocks) {
1160                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1161         }
1162         SIVAL(vwv+3, 1, CreatFlags);
1163         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
1164         SIVAL(vwv+7, 1, DesiredAccess);
1165         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
1166         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
1167         SIVAL(vwv+13, 1, FileAttributes);
1168         SIVAL(vwv+15, 1, ShareAccess);
1169         SIVAL(vwv+17, 1, CreateDisposition);
1170         SIVAL(vwv+19, 1, CreateOptions);
1171         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1172         SCVAL(vwv+23, 1, SecurityFlags);
1173
1174         bytes = talloc_array(state, uint8_t, 0);
1175         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1176                                    fname, strlen(fname)+1,
1177                                    &converted_len);
1178
1179         /* sigh. this copes with broken netapp filer behaviour */
1180         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1181
1182         if (tevent_req_nomem(bytes, req)) {
1183                 return tevent_req_post(req, ev);
1184         }
1185
1186         SIVAL(vwv+2, 1, converted_len);
1187
1188         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1189                               talloc_get_size(bytes), bytes);
1190         if (tevent_req_nomem(subreq, req)) {
1191                 return tevent_req_post(req, ev);
1192         }
1193         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1194         return req;
1195 }
1196
1197 static void cli_ntcreate_done(struct tevent_req *subreq)
1198 {
1199         struct tevent_req *req = tevent_req_callback_data(
1200                 subreq, struct tevent_req);
1201         struct cli_ntcreate_state *state = tevent_req_data(
1202                 req, struct cli_ntcreate_state);
1203         uint8_t wct;
1204         uint16_t *vwv;
1205         uint32_t num_bytes;
1206         uint8_t *bytes;
1207         NTSTATUS status;
1208
1209         status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1210         if (!NT_STATUS_IS_OK(status)) {
1211                 TALLOC_FREE(subreq);
1212                 tevent_req_nterror(req, status);
1213                 return;
1214         }
1215         state->fnum = SVAL(vwv+2, 1);
1216         tevent_req_done(req);
1217 }
1218
1219 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1220 {
1221         struct cli_ntcreate_state *state = tevent_req_data(
1222                 req, struct cli_ntcreate_state);
1223         NTSTATUS status;
1224
1225         if (tevent_req_is_nterror(req, &status)) {
1226                 return status;
1227         }
1228         *pfnum = state->fnum;
1229         return NT_STATUS_OK;
1230 }
1231
1232 NTSTATUS cli_ntcreate(struct cli_state *cli,
1233                       const char *fname,
1234                       uint32_t CreatFlags,
1235                       uint32_t DesiredAccess,
1236                       uint32_t FileAttributes,
1237                       uint32_t ShareAccess,
1238                       uint32_t CreateDisposition,
1239                       uint32_t CreateOptions,
1240                       uint8_t SecurityFlags,
1241                       uint16_t *pfid)
1242 {
1243         TALLOC_CTX *frame = talloc_stackframe();
1244         struct event_context *ev;
1245         struct tevent_req *req;
1246         NTSTATUS status = NT_STATUS_OK;
1247
1248         if (cli_has_async_calls(cli)) {
1249                 /*
1250                  * Can't use sync call while an async call is in flight
1251                  */
1252                 status = NT_STATUS_INVALID_PARAMETER;
1253                 goto fail;
1254         }
1255
1256         ev = event_context_init(frame);
1257         if (ev == NULL) {
1258                 status = NT_STATUS_NO_MEMORY;
1259                 goto fail;
1260         }
1261
1262         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1263                                 DesiredAccess, FileAttributes, ShareAccess,
1264                                 CreateDisposition, CreateOptions,
1265                                 SecurityFlags);
1266         if (req == NULL) {
1267                 status = NT_STATUS_NO_MEMORY;
1268                 goto fail;
1269         }
1270
1271         if (!tevent_req_poll(req, ev)) {
1272                 status = map_nt_error_from_unix(errno);
1273                 goto fail;
1274         }
1275
1276         status = cli_ntcreate_recv(req, pfid);
1277  fail:
1278         TALLOC_FREE(frame);
1279         if (!NT_STATUS_IS_OK(status)) {
1280                 cli_set_error(cli, status);
1281         }
1282         return status;
1283 }
1284
1285 /***********************************************************
1286  Common function for pushing stings, used by smb_bytes_push_str()
1287  and trans_bytes_push_str(). Only difference is the align_odd
1288  parameter setting.
1289 ***********************************************************/
1290
1291 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
1292                                 const char *str, size_t str_len,
1293                                 bool align_odd,
1294                                 size_t *pconverted_size)
1295 {
1296         size_t buflen;
1297         char *converted;
1298         size_t converted_size;
1299
1300         if (buf == NULL) {
1301                 return NULL;
1302         }
1303
1304         buflen = talloc_get_size(buf);
1305
1306         if (align_odd && ucs2 && (buflen % 2 == 0)) {
1307                 /*
1308                  * We're pushing into an SMB buffer, align odd
1309                  */
1310                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1311                 if (buf == NULL) {
1312                         return NULL;
1313                 }
1314                 buf[buflen] = '\0';
1315                 buflen += 1;
1316         }
1317
1318         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1319                                    ucs2 ? CH_UTF16LE : CH_DOS,
1320                                    str, str_len, &converted,
1321                                    &converted_size, true)) {
1322                 return NULL;
1323         }
1324
1325         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1326                                    buflen + converted_size);
1327         if (buf == NULL) {
1328                 TALLOC_FREE(converted);
1329                 return NULL;
1330         }
1331
1332         memcpy(buf + buflen, converted, converted_size);
1333
1334         TALLOC_FREE(converted);
1335
1336         if (pconverted_size) {
1337                 *pconverted_size = converted_size;
1338         }
1339
1340         return buf;
1341 }
1342
1343 /***********************************************************
1344  Push a string into an SMB buffer, with odd byte alignment
1345  if it's a UCS2 string.
1346 ***********************************************************/
1347
1348 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1349                             const char *str, size_t str_len,
1350                             size_t *pconverted_size)
1351 {
1352         return internal_bytes_push_str(buf, ucs2, str, str_len,
1353                         true, pconverted_size);
1354 }
1355
1356 /***********************************************************
1357  Same as smb_bytes_push_str(), but without the odd byte
1358  align for ucs2 (we're pushing into a param or data block).
1359  static for now, although this will probably change when
1360  other modules use async trans calls.
1361 ***********************************************************/
1362
1363 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
1364                             const char *str, size_t str_len,
1365                             size_t *pconverted_size)
1366 {
1367         return internal_bytes_push_str(buf, ucs2, str, str_len,
1368                         false, pconverted_size);
1369 }
1370
1371 /****************************************************************************
1372  Open a file
1373  WARNING: if you open with O_WRONLY then getattrE won't work!
1374 ****************************************************************************/
1375
1376 struct cli_open_state {
1377         uint16_t vwv[15];
1378         uint16_t fnum;
1379         struct iovec bytes;
1380 };
1381
1382 static void cli_open_done(struct tevent_req *subreq);
1383
1384 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1385                                    struct event_context *ev,
1386                                    struct cli_state *cli, const char *fname,
1387                                    int flags, int share_mode,
1388                                    struct tevent_req **psmbreq)
1389 {
1390         struct tevent_req *req, *subreq;
1391         struct cli_open_state *state;
1392         unsigned openfn;
1393         unsigned accessmode;
1394         uint8_t additional_flags;
1395         uint8_t *bytes;
1396
1397         req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1398         if (req == NULL) {
1399                 return NULL;
1400         }
1401
1402         openfn = 0;
1403         if (flags & O_CREAT) {
1404                 openfn |= (1<<4);
1405         }
1406         if (!(flags & O_EXCL)) {
1407                 if (flags & O_TRUNC)
1408                         openfn |= (1<<1);
1409                 else
1410                         openfn |= (1<<0);
1411         }
1412
1413         accessmode = (share_mode<<4);
1414
1415         if ((flags & O_ACCMODE) == O_RDWR) {
1416                 accessmode |= 2;
1417         } else if ((flags & O_ACCMODE) == O_WRONLY) {
1418                 accessmode |= 1;
1419         }
1420
1421 #if defined(O_SYNC)
1422         if ((flags & O_SYNC) == O_SYNC) {
1423                 accessmode |= (1<<14);
1424         }
1425 #endif /* O_SYNC */
1426
1427         if (share_mode == DENY_FCB) {
1428                 accessmode = 0xFF;
1429         }
1430
1431         SCVAL(state->vwv + 0, 0, 0xFF);
1432         SCVAL(state->vwv + 0, 1, 0);
1433         SSVAL(state->vwv + 1, 0, 0);
1434         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
1435         SSVAL(state->vwv + 3, 0, accessmode);
1436         SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1437         SSVAL(state->vwv + 5, 0, 0);
1438         SIVAL(state->vwv + 6, 0, 0);
1439         SSVAL(state->vwv + 8, 0, openfn);
1440         SIVAL(state->vwv + 9, 0, 0);
1441         SIVAL(state->vwv + 11, 0, 0);
1442         SIVAL(state->vwv + 13, 0, 0);
1443
1444         additional_flags = 0;
1445
1446         if (cli->use_oplocks) {
1447                 /* if using oplocks then ask for a batch oplock via
1448                    core and extended methods */
1449                 additional_flags =
1450                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1451                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1452         }
1453
1454         bytes = talloc_array(state, uint8_t, 0);
1455         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1456                                    strlen(fname)+1, NULL);
1457
1458         if (tevent_req_nomem(bytes, req)) {
1459                 return tevent_req_post(req, ev);
1460         }
1461
1462         state->bytes.iov_base = bytes;
1463         state->bytes.iov_len = talloc_get_size(bytes);
1464
1465         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1466                                     15, state->vwv, 1, &state->bytes);
1467         if (subreq == NULL) {
1468                 TALLOC_FREE(req);
1469                 return NULL;
1470         }
1471         tevent_req_set_callback(subreq, cli_open_done, req);
1472         *psmbreq = subreq;
1473         return req;
1474 }
1475
1476 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1477                                  struct cli_state *cli, const char *fname,
1478                                  int flags, int share_mode)
1479 {
1480         struct tevent_req *req, *subreq;
1481
1482         req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1483                               &subreq);
1484         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1485                 TALLOC_FREE(req);
1486                 return NULL;
1487         }
1488         return req;
1489 }
1490
1491 static void cli_open_done(struct tevent_req *subreq)
1492 {
1493         struct tevent_req *req = tevent_req_callback_data(
1494                 subreq, struct tevent_req);
1495         struct cli_open_state *state = tevent_req_data(
1496                 req, struct cli_open_state);
1497         uint8_t wct;
1498         uint16_t *vwv;
1499         NTSTATUS status;
1500
1501         status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1502         if (!NT_STATUS_IS_OK(status)) {
1503                 TALLOC_FREE(subreq);
1504                 tevent_req_nterror(req, status);
1505                 return;
1506         }
1507         state->fnum = SVAL(vwv+2, 0);
1508         tevent_req_done(req);
1509 }
1510
1511 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1512 {
1513         struct cli_open_state *state = tevent_req_data(
1514                 req, struct cli_open_state);
1515         NTSTATUS status;
1516
1517         if (tevent_req_is_nterror(req, &status)) {
1518                 return status;
1519         }
1520         *pfnum = state->fnum;
1521         return NT_STATUS_OK;
1522 }
1523
1524 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1525              int share_mode, uint16_t *pfnum)
1526 {
1527         TALLOC_CTX *frame = talloc_stackframe();
1528         struct event_context *ev;
1529         struct tevent_req *req;
1530         NTSTATUS status = NT_STATUS_OK;
1531
1532         if (cli_has_async_calls(cli)) {
1533                 /*
1534                  * Can't use sync call while an async call is in flight
1535                  */
1536                 status = NT_STATUS_INVALID_PARAMETER;
1537                 goto fail;
1538         }
1539
1540         ev = event_context_init(frame);
1541         if (ev == NULL) {
1542                 status = NT_STATUS_NO_MEMORY;
1543                 goto fail;
1544         }
1545
1546         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1547         if (req == NULL) {
1548                 status = NT_STATUS_NO_MEMORY;
1549                 goto fail;
1550         }
1551
1552         if (!tevent_req_poll(req, ev)) {
1553                 status = map_nt_error_from_unix(errno);
1554                 goto fail;
1555         }
1556
1557         status = cli_open_recv(req, pfnum);
1558  fail:
1559         TALLOC_FREE(frame);
1560         if (!NT_STATUS_IS_OK(status)) {
1561                 cli_set_error(cli, status);
1562         }
1563         return status;
1564 }
1565
1566 /****************************************************************************
1567  Close a file.
1568 ****************************************************************************/
1569
1570 struct cli_close_state {
1571         uint16_t vwv[3];
1572 };
1573
1574 static void cli_close_done(struct tevent_req *subreq);
1575
1576 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1577                                 struct event_context *ev,
1578                                 struct cli_state *cli,
1579                                 uint16_t fnum,
1580                                 struct tevent_req **psubreq)
1581 {
1582         struct tevent_req *req, *subreq;
1583         struct cli_close_state *state;
1584
1585         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1586         if (req == NULL) {
1587                 return NULL;
1588         }
1589         SSVAL(state->vwv+0, 0, fnum);
1590         SIVALS(state->vwv+1, 0, -1);
1591
1592         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1593                                     0, NULL);
1594         if (subreq == NULL) {
1595                 TALLOC_FREE(req);
1596                 return NULL;
1597         }
1598         tevent_req_set_callback(subreq, cli_close_done, req);
1599         *psubreq = subreq;
1600         return req;
1601 }
1602
1603 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1604                                 struct event_context *ev,
1605                                 struct cli_state *cli,
1606                                 uint16_t fnum)
1607 {
1608         struct tevent_req *req, *subreq;
1609
1610         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1611         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1612                 TALLOC_FREE(req);
1613                 return NULL;
1614         }
1615         return req;
1616 }
1617
1618 static void cli_close_done(struct tevent_req *subreq)
1619 {
1620         struct tevent_req *req = tevent_req_callback_data(
1621                 subreq, struct tevent_req);
1622         NTSTATUS status;
1623
1624         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1625         TALLOC_FREE(subreq);
1626         if (!NT_STATUS_IS_OK(status)) {
1627                 tevent_req_nterror(req, status);
1628                 return;
1629         }
1630         tevent_req_done(req);
1631 }
1632
1633 NTSTATUS cli_close_recv(struct tevent_req *req)
1634 {
1635         return tevent_req_simple_recv_ntstatus(req);
1636 }
1637
1638 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
1639 {
1640         TALLOC_CTX *frame = talloc_stackframe();
1641         struct event_context *ev;
1642         struct tevent_req *req;
1643         NTSTATUS status = NT_STATUS_OK;
1644
1645         if (cli_has_async_calls(cli)) {
1646                 /*
1647                  * Can't use sync call while an async call is in flight
1648                  */
1649                 status = NT_STATUS_INVALID_PARAMETER;
1650                 goto fail;
1651         }
1652
1653         ev = event_context_init(frame);
1654         if (ev == NULL) {
1655                 status = NT_STATUS_NO_MEMORY;
1656                 goto fail;
1657         }
1658
1659         req = cli_close_send(frame, ev, cli, fnum);
1660         if (req == NULL) {
1661                 status = NT_STATUS_NO_MEMORY;
1662                 goto fail;
1663         }
1664
1665         if (!tevent_req_poll(req, ev)) {
1666                 status = map_nt_error_from_unix(errno);
1667                 goto fail;
1668         }
1669
1670         status = cli_close_recv(req);
1671  fail:
1672         TALLOC_FREE(frame);
1673         if (!NT_STATUS_IS_OK(status)) {
1674                 cli_set_error(cli, status);
1675         }
1676         return status;
1677 }
1678
1679 /****************************************************************************
1680  Truncate a file to a specified size
1681 ****************************************************************************/
1682
1683 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
1684 {
1685         unsigned int param_len = 6;
1686         unsigned int data_len = 8;
1687         uint16_t setup = TRANSACT2_SETFILEINFO;
1688         char param[6];
1689         unsigned char data[8];
1690         char *rparam=NULL, *rdata=NULL;
1691         int saved_timeout = cli->timeout;
1692
1693         SSVAL(param,0,fnum);
1694         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1695         SSVAL(param,4,0);
1696
1697         SBVAL(data, 0, size);
1698
1699         if (!cli_send_trans(cli, SMBtrans2,
1700                             NULL,                    /* name */
1701                             -1, 0,                   /* fid, flags */
1702                             &setup, 1, 0,            /* setup, length, max */
1703                             param, param_len, 2,     /* param, length, max */
1704                             (char *)&data,  data_len,/* data, length, ... */
1705                             cli->max_xmit)) {        /* ... max */
1706                 cli->timeout = saved_timeout;
1707                 return False;
1708         }
1709
1710         if (!cli_receive_trans(cli, SMBtrans2,
1711                                 &rparam, &param_len,
1712                                 &rdata, &data_len)) {
1713                 cli->timeout = saved_timeout;
1714                 SAFE_FREE(rdata);
1715                 SAFE_FREE(rparam);
1716                 return False;
1717         }
1718
1719         cli->timeout = saved_timeout;
1720
1721         SAFE_FREE(rdata);
1722         SAFE_FREE(rparam);
1723
1724         return True;
1725 }
1726
1727
1728 /****************************************************************************
1729  send a lock with a specified locktype
1730  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1731 ****************************************************************************/
1732
1733 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
1734                       uint32_t offset, uint32_t len,
1735                       int timeout, unsigned char locktype)
1736 {
1737         char *p;
1738         int saved_timeout = cli->timeout;
1739
1740         memset(cli->outbuf,'\0',smb_size);
1741         memset(cli->inbuf,'\0', smb_size);
1742
1743         cli_set_message(cli->outbuf,8,0,True);
1744
1745         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1746         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1747         cli_setup_packet(cli);
1748
1749         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1750         SSVAL(cli->outbuf,smb_vwv2,fnum);
1751         SCVAL(cli->outbuf,smb_vwv3,locktype);
1752         SIVALS(cli->outbuf, smb_vwv4, timeout);
1753         SSVAL(cli->outbuf,smb_vwv6,0);
1754         SSVAL(cli->outbuf,smb_vwv7,1);
1755
1756         p = smb_buf(cli->outbuf);
1757         SSVAL(p, 0, cli->pid);
1758         SIVAL(p, 2, offset);
1759         SIVAL(p, 6, len);
1760
1761         p += 10;
1762
1763         cli_setup_bcc(cli, p);
1764
1765         cli_send_smb(cli);
1766
1767         if (timeout != 0) {
1768                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1769         }
1770
1771         if (!cli_receive_smb(cli)) {
1772                 cli->timeout = saved_timeout;
1773                 return NT_STATUS_UNSUCCESSFUL;
1774         }
1775
1776         cli->timeout = saved_timeout;
1777
1778         return cli_nt_error(cli);
1779 }
1780
1781 /****************************************************************************
1782  Lock a file.
1783  note that timeout is in units of 2 milliseconds
1784 ****************************************************************************/
1785
1786 bool cli_lock(struct cli_state *cli, uint16_t fnum,
1787               uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1788 {
1789         char *p;
1790         int saved_timeout = cli->timeout;
1791
1792         memset(cli->outbuf,'\0',smb_size);
1793         memset(cli->inbuf,'\0', smb_size);
1794
1795         cli_set_message(cli->outbuf,8,0,True);
1796
1797         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1798         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1799         cli_setup_packet(cli);
1800
1801         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1802         SSVAL(cli->outbuf,smb_vwv2,fnum);
1803         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1804         SIVALS(cli->outbuf, smb_vwv4, timeout);
1805         SSVAL(cli->outbuf,smb_vwv6,0);
1806         SSVAL(cli->outbuf,smb_vwv7,1);
1807
1808         p = smb_buf(cli->outbuf);
1809         SSVAL(p, 0, cli->pid);
1810         SIVAL(p, 2, offset);
1811         SIVAL(p, 6, len);
1812
1813         p += 10;
1814
1815         cli_setup_bcc(cli, p);
1816
1817         cli_send_smb(cli);
1818
1819         if (timeout != 0) {
1820                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1821         }
1822
1823         if (!cli_receive_smb(cli)) {
1824                 cli->timeout = saved_timeout;
1825                 return False;
1826         }
1827
1828         cli->timeout = saved_timeout;
1829
1830         if (cli_is_error(cli)) {
1831                 return False;
1832         }
1833
1834         return True;
1835 }
1836
1837 /****************************************************************************
1838  Unlock a file.
1839 ****************************************************************************/
1840
1841 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
1842 {
1843         char *p;
1844
1845         memset(cli->outbuf,'\0',smb_size);
1846         memset(cli->inbuf,'\0',smb_size);
1847
1848         cli_set_message(cli->outbuf,8,0,True);
1849
1850         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1851         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1852         cli_setup_packet(cli);
1853
1854         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1855         SSVAL(cli->outbuf,smb_vwv2,fnum);
1856         SCVAL(cli->outbuf,smb_vwv3,0);
1857         SIVALS(cli->outbuf, smb_vwv4, 0);
1858         SSVAL(cli->outbuf,smb_vwv6,1);
1859         SSVAL(cli->outbuf,smb_vwv7,0);
1860
1861         p = smb_buf(cli->outbuf);
1862         SSVAL(p, 0, cli->pid);
1863         SIVAL(p, 2, offset);
1864         SIVAL(p, 6, len);
1865         p += 10;
1866         cli_setup_bcc(cli, p);
1867         cli_send_smb(cli);
1868         if (!cli_receive_smb(cli)) {
1869                 return False;
1870         }
1871
1872         if (cli_is_error(cli)) {
1873                 return False;
1874         }
1875
1876         return True;
1877 }
1878
1879 /****************************************************************************
1880  Lock a file with 64 bit offsets.
1881 ****************************************************************************/
1882
1883 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
1884                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1885 {
1886         char *p;
1887         int saved_timeout = cli->timeout;
1888         int ltype;
1889
1890         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1891                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1892         }
1893
1894         ltype = (lock_type == READ_LOCK? 1 : 0);
1895         ltype |= LOCKING_ANDX_LARGE_FILES;
1896
1897         memset(cli->outbuf,'\0',smb_size);
1898         memset(cli->inbuf,'\0', smb_size);
1899
1900         cli_set_message(cli->outbuf,8,0,True);
1901
1902         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1903         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1904         cli_setup_packet(cli);
1905
1906         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1907         SSVAL(cli->outbuf,smb_vwv2,fnum);
1908         SCVAL(cli->outbuf,smb_vwv3,ltype);
1909         SIVALS(cli->outbuf, smb_vwv4, timeout);
1910         SSVAL(cli->outbuf,smb_vwv6,0);
1911         SSVAL(cli->outbuf,smb_vwv7,1);
1912
1913         p = smb_buf(cli->outbuf);
1914         SIVAL(p, 0, cli->pid);
1915         SOFF_T_R(p, 4, offset);
1916         SOFF_T_R(p, 12, len);
1917         p += 20;
1918
1919         cli_setup_bcc(cli, p);
1920         cli_send_smb(cli);
1921
1922         if (timeout != 0) {
1923                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1924         }
1925
1926         if (!cli_receive_smb(cli)) {
1927                 cli->timeout = saved_timeout;
1928                 return False;
1929         }
1930
1931         cli->timeout = saved_timeout;
1932
1933         if (cli_is_error(cli)) {
1934                 return False;
1935         }
1936
1937         return True;
1938 }
1939
1940 /****************************************************************************
1941  Unlock a file with 64 bit offsets.
1942 ****************************************************************************/
1943
1944 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
1945 {
1946         char *p;
1947
1948         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1949                 return cli_unlock(cli, fnum, offset, len);
1950         }
1951
1952         memset(cli->outbuf,'\0',smb_size);
1953         memset(cli->inbuf,'\0',smb_size);
1954
1955         cli_set_message(cli->outbuf,8,0,True);
1956
1957         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1958         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1959         cli_setup_packet(cli);
1960
1961         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1962         SSVAL(cli->outbuf,smb_vwv2,fnum);
1963         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1964         SIVALS(cli->outbuf, smb_vwv4, 0);
1965         SSVAL(cli->outbuf,smb_vwv6,1);
1966         SSVAL(cli->outbuf,smb_vwv7,0);
1967
1968         p = smb_buf(cli->outbuf);
1969         SIVAL(p, 0, cli->pid);
1970         SOFF_T_R(p, 4, offset);
1971         SOFF_T_R(p, 12, len);
1972         p += 20;
1973         cli_setup_bcc(cli, p);
1974         cli_send_smb(cli);
1975         if (!cli_receive_smb(cli)) {
1976                 return False;
1977         }
1978
1979         if (cli_is_error(cli)) {
1980                 return False;
1981         }
1982
1983         return True;
1984 }
1985
1986 /****************************************************************************
1987  Get/unlock a POSIX lock on a file - internal function.
1988 ****************************************************************************/
1989
1990 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
1991                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1992 {
1993         unsigned int param_len = 4;
1994         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1995         uint16_t setup = TRANSACT2_SETFILEINFO;
1996         char param[4];
1997         unsigned char data[POSIX_LOCK_DATA_SIZE];
1998         char *rparam=NULL, *rdata=NULL;
1999         int saved_timeout = cli->timeout;
2000
2001         SSVAL(param,0,fnum);
2002         SSVAL(param,2,SMB_SET_POSIX_LOCK);
2003
2004         switch (lock_type) {
2005                 case READ_LOCK:
2006                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2007                         break;
2008                 case WRITE_LOCK:
2009                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2010                         break;
2011                 case UNLOCK_LOCK:
2012                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2013                         break;
2014                 default:
2015                         return False;
2016         }
2017
2018         if (wait_lock) {
2019                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2020                 cli->timeout = 0x7FFFFFFF;
2021         } else {
2022                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2023         }
2024
2025         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2026         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2027         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2028
2029         if (!cli_send_trans(cli, SMBtrans2,
2030                         NULL,                        /* name */
2031                         -1, 0,                          /* fid, flags */
2032                         &setup, 1, 0,                   /* setup, length, max */
2033                         param, param_len, 2,            /* param, length, max */
2034                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2035                         )) {
2036                 cli->timeout = saved_timeout;
2037                 return False;
2038         }
2039
2040         if (!cli_receive_trans(cli, SMBtrans2,
2041                                 &rparam, &param_len,
2042                                 &rdata, &data_len)) {
2043                 cli->timeout = saved_timeout;
2044                 SAFE_FREE(rdata);
2045                 SAFE_FREE(rparam);
2046                 return False;
2047         }
2048
2049         cli->timeout = saved_timeout;
2050
2051         SAFE_FREE(rdata);
2052         SAFE_FREE(rparam);
2053
2054         return True;
2055 }
2056
2057 /****************************************************************************
2058  POSIX Lock a file.
2059 ****************************************************************************/
2060
2061 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2062                         uint64_t offset, uint64_t len,
2063                         bool wait_lock, enum brl_type lock_type)
2064 {
2065         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2066                 return False;
2067         }
2068         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2069 }
2070
2071 /****************************************************************************
2072  POSIX Unlock a file.
2073 ****************************************************************************/
2074
2075 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2076 {
2077         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2078 }
2079
2080 /****************************************************************************
2081  POSIX Get any lock covering a file.
2082 ****************************************************************************/
2083
2084 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2085 {
2086         return True;
2087 }
2088
2089 /****************************************************************************
2090  Do a SMBgetattrE call.
2091 ****************************************************************************/
2092
2093 bool cli_getattrE(struct cli_state *cli, int fd,
2094                   uint16_t *attr, SMB_OFF_T *size,
2095                   time_t *change_time,
2096                   time_t *access_time,
2097                   time_t *write_time)
2098 {
2099         memset(cli->outbuf,'\0',smb_size);
2100         memset(cli->inbuf,'\0',smb_size);
2101
2102         cli_set_message(cli->outbuf,1,0,True);
2103
2104         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
2105         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2106         cli_setup_packet(cli);
2107
2108         SSVAL(cli->outbuf,smb_vwv0,fd);
2109
2110         cli_send_smb(cli);
2111         if (!cli_receive_smb(cli)) {
2112                 return False;
2113         }
2114
2115         if (cli_is_error(cli)) {
2116                 return False;
2117         }
2118
2119         if (size) {
2120                 *size = IVAL(cli->inbuf, smb_vwv6);
2121         }
2122
2123         if (attr) {
2124                 *attr = SVAL(cli->inbuf,smb_vwv10);
2125         }
2126
2127         if (change_time) {
2128                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
2129         }
2130
2131         if (access_time) {
2132                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
2133         }
2134
2135         if (write_time) {
2136                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
2137         }
2138
2139         return True;
2140 }
2141
2142 /****************************************************************************
2143  Do a SMBgetatr call
2144 ****************************************************************************/
2145
2146 bool cli_getatr(struct cli_state *cli, const char *fname,
2147                 uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
2148 {
2149         char *p;
2150
2151         memset(cli->outbuf,'\0',smb_size);
2152         memset(cli->inbuf,'\0',smb_size);
2153
2154         cli_set_message(cli->outbuf,0,0,True);
2155
2156         SCVAL(cli->outbuf,smb_com,SMBgetatr);
2157         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2158         cli_setup_packet(cli);
2159
2160         p = smb_buf(cli->outbuf);
2161         *p++ = 4;
2162         p += clistr_push(cli, p, fname,
2163                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2164
2165         cli_setup_bcc(cli, p);
2166
2167         cli_send_smb(cli);
2168         if (!cli_receive_smb(cli)) {
2169                 return False;
2170         }
2171
2172         if (cli_is_error(cli)) {
2173                 return False;
2174         }
2175
2176         if (size) {
2177                 *size = IVAL(cli->inbuf, smb_vwv3);
2178         }
2179
2180         if (write_time) {
2181                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
2182         }
2183
2184         if (attr) {
2185                 *attr = SVAL(cli->inbuf,smb_vwv0);
2186         }
2187
2188         return True;
2189 }
2190
2191 /****************************************************************************
2192  Do a SMBsetattrE call.
2193 ****************************************************************************/
2194
2195 bool cli_setattrE(struct cli_state *cli, int fd,
2196                   time_t change_time,
2197                   time_t access_time,
2198                   time_t write_time)
2199
2200 {
2201         char *p;
2202
2203         memset(cli->outbuf,'\0',smb_size);
2204         memset(cli->inbuf,'\0',smb_size);
2205
2206         cli_set_message(cli->outbuf,7,0,True);
2207
2208         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
2209         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2210         cli_setup_packet(cli);
2211
2212         SSVAL(cli->outbuf,smb_vwv0, fd);
2213         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
2214         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
2215         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
2216
2217         p = smb_buf(cli->outbuf);
2218         *p++ = 4;
2219
2220         cli_setup_bcc(cli, p);
2221
2222         cli_send_smb(cli);
2223         if (!cli_receive_smb(cli)) {
2224                 return False;
2225         }
2226
2227         if (cli_is_error(cli)) {
2228                 return False;
2229         }
2230
2231         return True;
2232 }
2233
2234 /****************************************************************************
2235  Do a SMBsetatr call.
2236 ****************************************************************************/
2237
2238 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
2239 {
2240         char *p;
2241
2242         memset(cli->outbuf,'\0',smb_size);
2243         memset(cli->inbuf,'\0',smb_size);
2244
2245         cli_set_message(cli->outbuf,8,0,True);
2246
2247         SCVAL(cli->outbuf,smb_com,SMBsetatr);
2248         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2249         cli_setup_packet(cli);
2250
2251         SSVAL(cli->outbuf,smb_vwv0, attr);
2252         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
2253
2254         p = smb_buf(cli->outbuf);
2255         *p++ = 4;
2256         p += clistr_push(cli, p, fname,
2257                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2258         *p++ = 4;
2259
2260         cli_setup_bcc(cli, p);
2261
2262         cli_send_smb(cli);
2263         if (!cli_receive_smb(cli)) {
2264                 return False;
2265         }
2266
2267         if (cli_is_error(cli)) {
2268                 return False;
2269         }
2270
2271         return True;
2272 }
2273
2274 /****************************************************************************
2275  Check for existance of a dir.
2276 ****************************************************************************/
2277
2278 static void cli_chkpath_done(struct tevent_req *subreq);
2279
2280 struct cli_chkpath_state {
2281         int dummy;
2282 };
2283
2284 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2285                                   struct event_context *ev,
2286                                   struct cli_state *cli,
2287                                   const char *fname)
2288 {
2289         struct tevent_req *req = NULL, *subreq = NULL;
2290         struct cli_chkpath_state *state = NULL;
2291         uint8_t additional_flags = 0;
2292         uint8_t *bytes = NULL;
2293
2294         req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2295         if (req == NULL) {
2296                 return NULL;
2297         }
2298
2299         bytes = talloc_array(state, uint8_t, 1);
2300         if (tevent_req_nomem(bytes, req)) {
2301                 return tevent_req_post(req, ev);
2302         }
2303         bytes[0] = 4;
2304         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2305                                    strlen(fname)+1, NULL);
2306
2307         if (tevent_req_nomem(bytes, req)) {
2308                 return tevent_req_post(req, ev);
2309         }
2310
2311         subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2312                               0, NULL, talloc_get_size(bytes), bytes);
2313         if (tevent_req_nomem(subreq, req)) {
2314                 return tevent_req_post(req, ev);
2315         }
2316         tevent_req_set_callback(subreq, cli_chkpath_done, req);
2317         return req;
2318 }
2319
2320 static void cli_chkpath_done(struct tevent_req *subreq)
2321 {
2322         struct tevent_req *req = tevent_req_callback_data(
2323                 subreq, struct tevent_req);
2324         NTSTATUS status;
2325
2326         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2327         TALLOC_FREE(subreq);
2328         if (!NT_STATUS_IS_OK(status)) {
2329                 tevent_req_nterror(req, status);
2330                 return;
2331         }
2332         tevent_req_done(req);
2333 }
2334
2335 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2336 {
2337         return tevent_req_simple_recv_ntstatus(req);
2338 }
2339
2340 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2341 {
2342         TALLOC_CTX *frame = talloc_stackframe();
2343         struct event_context *ev = NULL;
2344         struct tevent_req *req = NULL;
2345         char *path2 = NULL;
2346         NTSTATUS status = NT_STATUS_OK;
2347
2348         if (cli_has_async_calls(cli)) {
2349                 /*
2350                  * Can't use sync call while an async call is in flight
2351                  */
2352                 status = NT_STATUS_INVALID_PARAMETER;
2353                 goto fail;
2354         }
2355
2356         path2 = talloc_strdup(frame, path);
2357         if (!path2) {
2358                 status = NT_STATUS_NO_MEMORY;
2359                 goto fail;
2360         }
2361         trim_char(path2,'\0','\\');
2362         if (!*path2) {
2363                 path2 = talloc_strdup(frame, "\\");
2364                 if (!path2) {
2365                         status = NT_STATUS_NO_MEMORY;
2366                         goto fail;
2367                 }
2368         }
2369
2370         ev = event_context_init(frame);
2371         if (ev == NULL) {
2372                 status = NT_STATUS_NO_MEMORY;
2373                 goto fail;
2374         }
2375
2376         req = cli_chkpath_send(frame, ev, cli, path2);
2377         if (req == NULL) {
2378                 status = NT_STATUS_NO_MEMORY;
2379                 goto fail;
2380         }
2381
2382         if (!tevent_req_poll(req, ev)) {
2383                 status = map_nt_error_from_unix(errno);
2384                 goto fail;
2385         }
2386
2387         status = cli_chkpath_recv(req);
2388
2389  fail:
2390         TALLOC_FREE(frame);
2391         if (!NT_STATUS_IS_OK(status)) {
2392                 cli_set_error(cli, status);
2393         }
2394         return status;
2395 }
2396
2397 /****************************************************************************
2398  Query disk space.
2399 ****************************************************************************/
2400
2401 static void cli_dskattr_done(struct tevent_req *subreq);
2402
2403 struct cli_dskattr_state {
2404         int bsize;
2405         int total;
2406         int avail;
2407 };
2408
2409 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2410                                   struct event_context *ev,
2411                                   struct cli_state *cli)
2412 {
2413         struct tevent_req *req = NULL, *subreq = NULL;
2414         struct cli_dskattr_state *state = NULL;
2415         uint8_t additional_flags = 0;
2416
2417         req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2418         if (req == NULL) {
2419                 return NULL;
2420         }
2421
2422         subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2423                               0, NULL, 0, NULL);
2424         if (tevent_req_nomem(subreq, req)) {
2425                 return tevent_req_post(req, ev);
2426         }
2427         tevent_req_set_callback(subreq, cli_dskattr_done, req);
2428         return req;
2429 }
2430
2431 static void cli_dskattr_done(struct tevent_req *subreq)
2432 {
2433         struct tevent_req *req = tevent_req_callback_data(
2434                 subreq, struct tevent_req);
2435         struct cli_dskattr_state *state = tevent_req_data(
2436                 req, struct cli_dskattr_state);
2437         uint8_t wct;
2438         uint16_t *vwv = NULL;
2439         NTSTATUS status;
2440
2441         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2442         if (!NT_STATUS_IS_OK(status)) {
2443                 tevent_req_nterror(req, status);
2444                 return;
2445         }
2446         state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2447         state->total = SVAL(vwv+0, 0);
2448         state->avail = SVAL(vwv+3, 0);
2449         TALLOC_FREE(subreq);
2450         tevent_req_done(req);
2451 }
2452
2453 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2454 {
2455         struct cli_dskattr_state *state = tevent_req_data(
2456                                 req, struct cli_dskattr_state);
2457         NTSTATUS status;
2458
2459         if (tevent_req_is_nterror(req, &status)) {
2460                 return status;
2461         }
2462         *bsize = state->bsize;
2463         *total = state->total;
2464         *avail = state->avail;
2465         return NT_STATUS_OK;
2466 }
2467
2468 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2469 {
2470         TALLOC_CTX *frame = talloc_stackframe();
2471         struct event_context *ev = NULL;
2472         struct tevent_req *req = NULL;
2473         NTSTATUS status = NT_STATUS_OK;
2474
2475         if (cli_has_async_calls(cli)) {
2476                 /*
2477                  * Can't use sync call while an async call is in flight
2478                  */
2479                 status = NT_STATUS_INVALID_PARAMETER;
2480                 goto fail;
2481         }
2482
2483         ev = event_context_init(frame);
2484         if (ev == NULL) {
2485                 status = NT_STATUS_NO_MEMORY;
2486                 goto fail;
2487         }
2488
2489         req = cli_dskattr_send(frame, ev, cli);
2490         if (req == NULL) {
2491                 status = NT_STATUS_NO_MEMORY;
2492                 goto fail;
2493         }
2494
2495         if (!tevent_req_poll(req, ev)) {
2496                 status = map_nt_error_from_unix(errno);
2497                 goto fail;
2498         }
2499
2500         status = cli_dskattr_recv(req, bsize, total, avail);
2501
2502  fail:
2503         TALLOC_FREE(frame);
2504         if (!NT_STATUS_IS_OK(status)) {
2505                 cli_set_error(cli, status);
2506         }
2507         return status;
2508 }
2509
2510 /****************************************************************************
2511  Create and open a temporary file.
2512 ****************************************************************************/
2513
2514 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2515 {
2516         int len;
2517         char *p;
2518
2519         memset(cli->outbuf,'\0',smb_size);
2520         memset(cli->inbuf,'\0',smb_size);
2521
2522         cli_set_message(cli->outbuf,3,0,True);
2523
2524         SCVAL(cli->outbuf,smb_com,SMBctemp);
2525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2526         cli_setup_packet(cli);
2527
2528         SSVAL(cli->outbuf,smb_vwv0,0);
2529         SIVALS(cli->outbuf,smb_vwv1,-1);
2530
2531         p = smb_buf(cli->outbuf);
2532         *p++ = 4;
2533         p += clistr_push(cli, p, path,
2534                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2535
2536         cli_setup_bcc(cli, p);
2537
2538         cli_send_smb(cli);
2539         if (!cli_receive_smb(cli)) {
2540                 return -1;
2541         }
2542
2543         if (cli_is_error(cli)) {
2544                 return -1;
2545         }
2546
2547         /* despite the spec, the result has a -1, followed by
2548            length, followed by name */
2549         p = smb_buf(cli->inbuf);
2550         p += 4;
2551         len = smb_buflen(cli->inbuf) - 4;
2552         if (len <= 0 || len > PATH_MAX) return -1;
2553
2554         if (tmp_path) {
2555                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2556                 if (!path2) {
2557                         return -1;
2558                 }
2559                 clistr_pull(cli->inbuf, path2, p,
2560                             len+1, len, STR_ASCII);
2561                 *tmp_path = path2;
2562         }
2563
2564         return SVAL(cli->inbuf,smb_vwv0);
2565 }
2566
2567 /*
2568    send a raw ioctl - used by the torture code
2569 */
2570 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
2571 {
2572         memset(cli->outbuf,'\0',smb_size);
2573         memset(cli->inbuf,'\0',smb_size);
2574
2575         cli_set_message(cli->outbuf, 3, 0, True);
2576         SCVAL(cli->outbuf,smb_com,SMBioctl);
2577         cli_setup_packet(cli);
2578
2579         SSVAL(cli->outbuf, smb_vwv0, fnum);
2580         SSVAL(cli->outbuf, smb_vwv1, code>>16);
2581         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2582
2583         cli_send_smb(cli);
2584         if (!cli_receive_smb(cli)) {
2585                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2586         }
2587
2588         if (cli_is_error(cli)) {
2589                 return cli_nt_error(cli);
2590         }
2591
2592         *blob = data_blob_null;
2593
2594         return NT_STATUS_OK;
2595 }
2596
2597 /*********************************************************
2598  Set an extended attribute utility fn.
2599 *********************************************************/
2600
2601 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2602                         const char *ea_name, const char *ea_val, size_t ea_len)
2603 {
2604         unsigned int data_len = 0;
2605         char *data = NULL;
2606         char *rparam=NULL, *rdata=NULL;
2607         char *p;
2608         size_t ea_namelen = strlen(ea_name);
2609
2610         if (ea_namelen == 0 && ea_len == 0) {
2611                 data_len = 4;
2612                 data = (char *)SMB_MALLOC(data_len);
2613                 if (!data) {
2614                         return False;
2615                 }
2616                 p = data;
2617                 SIVAL(p,0,data_len);
2618         } else {
2619                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2620                 data = (char *)SMB_MALLOC(data_len);
2621                 if (!data) {
2622                         return False;
2623                 }
2624                 p = data;
2625                 SIVAL(p,0,data_len);
2626                 p += 4;
2627                 SCVAL(p, 0, 0); /* EA flags. */
2628                 SCVAL(p, 1, ea_namelen);
2629                 SSVAL(p, 2, ea_len);
2630                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2631                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2632         }
2633
2634         if (!cli_send_trans(cli, SMBtrans2,
2635                         NULL,                        /* name */
2636                         -1, 0,                          /* fid, flags */
2637                         &setup, 1, 0,                   /* setup, length, max */
2638                         param, param_len, 2,            /* param, length, max */
2639                         data,  data_len, cli->max_xmit /* data, length, max */
2640                         )) {
2641                 SAFE_FREE(data);
2642                 return False;
2643         }
2644
2645         if (!cli_receive_trans(cli, SMBtrans2,
2646                         &rparam, &param_len,
2647                         &rdata, &data_len)) {
2648                         SAFE_FREE(data);
2649                 return false;
2650         }
2651
2652         SAFE_FREE(data);
2653         SAFE_FREE(rdata);
2654         SAFE_FREE(rparam);
2655
2656         return True;
2657 }
2658
2659 /*********************************************************
2660  Set an extended attribute on a pathname.
2661 *********************************************************/
2662
2663 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2664 {
2665         uint16_t setup = TRANSACT2_SETPATHINFO;
2666         unsigned int param_len = 0;
2667         char *param;
2668         size_t srclen = 2*(strlen(path)+1);
2669         char *p;
2670         bool ret;
2671
2672         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2673         if (!param) {
2674                 return false;
2675         }
2676         memset(param, '\0', 6);
2677         SSVAL(param,0,SMB_INFO_SET_EA);
2678         p = &param[6];
2679
2680         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2681         param_len = PTR_DIFF(p, param);
2682
2683         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2684         SAFE_FREE(param);
2685         return ret;
2686 }
2687
2688 /*********************************************************
2689  Set an extended attribute on an fnum.
2690 *********************************************************/
2691
2692 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2693 {
2694         char param[6];
2695         uint16_t setup = TRANSACT2_SETFILEINFO;
2696
2697         memset(param, 0, 6);
2698         SSVAL(param,0,fnum);
2699         SSVAL(param,2,SMB_INFO_SET_EA);
2700
2701         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2702 }
2703
2704 /*********************************************************
2705  Get an extended attribute list utility fn.
2706 *********************************************************/
2707
2708 static bool cli_get_ea_list(struct cli_state *cli,
2709                 uint16_t setup, char *param, unsigned int param_len,
2710                 TALLOC_CTX *ctx,
2711                 size_t *pnum_eas,
2712                 struct ea_struct **pea_list)
2713 {
2714         unsigned int data_len = 0;
2715         unsigned int rparam_len, rdata_len;
2716         char *rparam=NULL, *rdata=NULL;
2717         char *p;
2718         size_t ea_size;
2719         size_t num_eas;
2720         bool ret = False;
2721         struct ea_struct *ea_list;
2722
2723         *pnum_eas = 0;
2724         if (pea_list) {
2725                 *pea_list = NULL;
2726         }
2727
2728         if (!cli_send_trans(cli, SMBtrans2,
2729                         NULL,           /* Name */
2730                         -1, 0,          /* fid, flags */
2731                         &setup, 1, 0,   /* setup, length, max */
2732                         param, param_len, 10, /* param, length, max */
2733                         NULL, data_len, cli->max_xmit /* data, length, max */
2734                                 )) {
2735                 return False;
2736         }
2737
2738         if (!cli_receive_trans(cli, SMBtrans2,
2739                         &rparam, &rparam_len,
2740                         &rdata, &rdata_len)) {
2741                 return False;
2742         }
2743
2744         if (!rdata || rdata_len < 4) {
2745                 goto out;
2746         }
2747
2748         ea_size = (size_t)IVAL(rdata,0);
2749         if (ea_size > rdata_len) {
2750                 goto out;
2751         }
2752
2753         if (ea_size == 0) {
2754                 /* No EA's present. */
2755                 ret = True;
2756                 goto out;
2757         }
2758
2759         p = rdata + 4;
2760         ea_size -= 4;
2761
2762         /* Validate the EA list and count it. */
2763         for (num_eas = 0; ea_size >= 4; num_eas++) {
2764                 unsigned int ea_namelen = CVAL(p,1);
2765                 unsigned int ea_valuelen = SVAL(p,2);
2766                 if (ea_namelen == 0) {
2767                         goto out;
2768                 }
2769                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2770                         goto out;
2771                 }
2772                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2773                 p += 4 + ea_namelen + 1 + ea_valuelen;
2774         }
2775
2776         if (num_eas == 0) {
2777                 ret = True;
2778                 goto out;
2779         }
2780
2781         *pnum_eas = num_eas;
2782         if (!pea_list) {
2783                 /* Caller only wants number of EA's. */
2784                 ret = True;
2785                 goto out;
2786         }
2787
2788         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2789         if (!ea_list) {
2790                 goto out;
2791         }
2792
2793         ea_size = (size_t)IVAL(rdata,0);
2794         p = rdata + 4;
2795
2796         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2797                 struct ea_struct *ea = &ea_list[num_eas];
2798                 fstring unix_ea_name;
2799                 unsigned int ea_namelen = CVAL(p,1);
2800                 unsigned int ea_valuelen = SVAL(p,2);
2801
2802                 ea->flags = CVAL(p,0);
2803                 unix_ea_name[0] = '\0';
2804                 pull_ascii_fstring(unix_ea_name, p + 4);
2805                 ea->name = talloc_strdup(ctx, unix_ea_name);
2806                 /* Ensure the value is null terminated (in case it's a string). */
2807                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2808                 if (!ea->value.data) {
2809                         goto out;
2810                 }
2811                 if (ea_valuelen) {
2812                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2813                 }
2814                 ea->value.data[ea_valuelen] = 0;
2815                 ea->value.length--;
2816                 p += 4 + ea_namelen + 1 + ea_valuelen;
2817         }
2818
2819         *pea_list = ea_list;
2820         ret = True;
2821
2822  out :
2823
2824         SAFE_FREE(rdata);
2825         SAFE_FREE(rparam);
2826         return ret;
2827 }
2828
2829 /*********************************************************
2830  Get an extended attribute list from a pathname.
2831 *********************************************************/
2832
2833 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2834                 TALLOC_CTX *ctx,
2835                 size_t *pnum_eas,
2836                 struct ea_struct **pea_list)
2837 {
2838         uint16_t setup = TRANSACT2_QPATHINFO;
2839         unsigned int param_len = 0;
2840         char *param;
2841         char *p;
2842         size_t srclen = 2*(strlen(path)+1);
2843         bool ret;
2844
2845         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2846         if (!param) {
2847                 return false;
2848         }
2849         p = param;
2850         memset(p, 0, 6);
2851         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2852         p += 6;
2853         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2854         param_len = PTR_DIFF(p, param);
2855
2856         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2857         SAFE_FREE(param);
2858         return ret;
2859 }
2860
2861 /*********************************************************
2862  Get an extended attribute list from an fnum.
2863 *********************************************************/
2864
2865 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
2866                 TALLOC_CTX *ctx,
2867                 size_t *pnum_eas,
2868                 struct ea_struct **pea_list)
2869 {
2870         uint16_t setup = TRANSACT2_QFILEINFO;
2871         char param[6];
2872
2873         memset(param, 0, 6);
2874         SSVAL(param,0,fnum);
2875         SSVAL(param,2,SMB_INFO_SET_EA);
2876
2877         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2878 }
2879
2880 /****************************************************************************
2881  Convert open "flags" arg to uint32_t on wire.
2882 ****************************************************************************/
2883
2884 static uint32_t open_flags_to_wire(int flags)
2885 {
2886         int open_mode = flags & O_ACCMODE;
2887         uint32_t ret = 0;
2888
2889         switch (open_mode) {
2890                 case O_WRONLY:
2891                         ret |= SMB_O_WRONLY;
2892                         break;
2893                 case O_RDWR:
2894                         ret |= SMB_O_RDWR;
2895                         break;
2896                 default:
2897                 case O_RDONLY:
2898                         ret |= SMB_O_RDONLY;
2899                         break;
2900         }
2901
2902         if (flags & O_CREAT) {
2903                 ret |= SMB_O_CREAT;
2904         }
2905         if (flags & O_EXCL) {
2906                 ret |= SMB_O_EXCL;
2907         }
2908         if (flags & O_TRUNC) {
2909                 ret |= SMB_O_TRUNC;
2910         }
2911 #if defined(O_SYNC)
2912         if (flags & O_SYNC) {
2913                 ret |= SMB_O_SYNC;
2914         }
2915 #endif /* O_SYNC */
2916         if (flags & O_APPEND) {
2917                 ret |= SMB_O_APPEND;
2918         }
2919 #if defined(O_DIRECT)
2920         if (flags & O_DIRECT) {
2921                 ret |= SMB_O_DIRECT;
2922         }
2923 #endif
2924 #if defined(O_DIRECTORY)
2925         if (flags & O_DIRECTORY) {
2926                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2927                 ret |= SMB_O_DIRECTORY;
2928         }
2929 #endif
2930         return ret;
2931 }
2932
2933 /****************************************************************************
2934  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2935 ****************************************************************************/
2936
2937 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2938 {
2939         unsigned int data_len = 0;
2940         unsigned int param_len = 0;
2941         uint16_t setup = TRANSACT2_SETPATHINFO;
2942         char *param;
2943         char data[18];
2944         char *rparam=NULL, *rdata=NULL;
2945         char *p;
2946         uint16_t fnum = (uint16_t)-1;
2947         uint32_t wire_flags = open_flags_to_wire(flags);
2948         size_t srclen = 2*(strlen(fname)+1);
2949
2950         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2951         if (!param) {
2952                 return false;
2953         }
2954         memset(param, '\0', 6);
2955         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2956         p = &param[6];
2957
2958         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2959         param_len = PTR_DIFF(p, param);
2960
2961         if (is_dir) {
2962                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2963                 wire_flags |= SMB_O_DIRECTORY;
2964         }
2965
2966         p = data;
2967         SIVAL(p,0,0); /* No oplock. */
2968         SIVAL(p,4,wire_flags);
2969         SIVAL(p,8,unix_perms_to_wire(mode));
2970         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2971         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2972
2973         data_len = 18;
2974
2975         if (!cli_send_trans(cli, SMBtrans2,
2976                         NULL,                        /* name */
2977                         -1, 0,                          /* fid, flags */
2978                         &setup, 1, 0,                   /* setup, length, max */
2979                         param, param_len, 0,            /* param, length, max */
2980                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2981                         )) {
2982                 SAFE_FREE(param);
2983                 return -1;
2984         }
2985
2986         SAFE_FREE(param);
2987
2988         if (!cli_receive_trans(cli, SMBtrans2,
2989                 &rparam, &param_len,
2990                 &rdata, &data_len)) {
2991                         return -1;
2992         }
2993
2994         fnum = SVAL(rdata,2);
2995
2996         SAFE_FREE(rdata);
2997         SAFE_FREE(rparam);
2998
2999         return fnum;
3000 }
3001
3002 /****************************************************************************
3003  open - POSIX semantics.
3004 ****************************************************************************/
3005
3006 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
3007 {
3008         return cli_posix_open_internal(cli, fname, flags, mode, False);
3009 }
3010
3011 /****************************************************************************
3012  mkdir - POSIX semantics.
3013 ****************************************************************************/
3014
3015 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
3016 {
3017         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
3018 }
3019
3020 /****************************************************************************
3021  unlink or rmdir - POSIX semantics.
3022 ****************************************************************************/
3023
3024 struct unlink_state {
3025         int dummy;
3026 };
3027
3028 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3029 {
3030         struct tevent_req *req = tevent_req_callback_data(
3031                                 subreq, struct tevent_req);
3032         struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3033         NTSTATUS status;
3034
3035         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3036         TALLOC_FREE(subreq);
3037         if (!NT_STATUS_IS_OK(status)) {
3038                 tevent_req_nterror(req, status);
3039                 return;
3040         }
3041         tevent_req_done(req);
3042 }
3043
3044 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3045                                         struct event_context *ev,
3046                                         struct cli_state *cli,
3047                                         const char *fname,
3048                                         bool is_dir)
3049 {
3050         struct tevent_req *req = NULL, *subreq = NULL;
3051         struct unlink_state *state = NULL;
3052         uint16_t setup;
3053         uint8_t *param = NULL;
3054         uint8_t data[2];
3055
3056         req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3057         if (req == NULL) {
3058                 return NULL;
3059         }
3060
3061         /* Setup setup word. */
3062         SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
3063
3064         /* Setup param array. */
3065         param = talloc_array(state, uint8_t, 6);
3066         if (tevent_req_nomem(data, req)) {
3067                 return tevent_req_post(req, ev);
3068         }
3069         memset(param, '\0', 6);
3070         SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3071
3072         param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
3073                                    strlen(fname)+1, NULL);
3074
3075         if (tevent_req_nomem(param, req)) {
3076                 return tevent_req_post(req, ev);
3077         }
3078
3079         /* Setup data word. */
3080         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3081                         SMB_POSIX_UNLINK_FILE_TARGET);
3082
3083         subreq = cli_trans_send(state,                  /* mem ctx. */
3084                                 ev,                     /* event ctx. */
3085                                 cli,                    /* cli_state. */
3086                                 SMBtrans2,              /* cmd. */
3087                                 NULL,                   /* pipe name. */
3088                                 -1,                     /* fid. */
3089                                 0,                      /* function. */
3090                                 0,                      /* flags. */
3091                                 &setup,                 /* setup. */
3092                                 1,                      /* num setup uint16_t words. */
3093                                 0,                      /* max returned setup. */
3094                                 param,                  /* param. */
3095                                 talloc_get_size(param), /* num param. */
3096                                 2,                      /* max returned param. */
3097                                 data,                   /* data. */
3098                                 2,                      /* num data. */
3099                                 0);                     /* max returned data. */
3100
3101         if (tevent_req_nomem(subreq, req)) {
3102                 return tevent_req_post(req, ev);
3103         }
3104         tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3105         return req;
3106 }
3107
3108 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3109                                         struct event_context *ev,
3110                                         struct cli_state *cli,
3111                                         const char *fname)
3112 {
3113         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3114 }
3115
3116 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3117 {
3118         NTSTATUS status;
3119
3120         if (tevent_req_is_nterror(req, &status)) {
3121                 return status;
3122         }
3123         return NT_STATUS_OK;
3124 }
3125
3126 /****************************************************************************
3127  unlink - POSIX semantics.
3128 ****************************************************************************/
3129
3130 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3131 {
3132         TALLOC_CTX *frame = talloc_stackframe();
3133         struct event_context *ev = NULL;
3134         struct tevent_req *req = NULL;
3135         NTSTATUS status = NT_STATUS_OK;
3136
3137         if (cli_has_async_calls(cli)) {
3138                 /*
3139                  * Can't use sync call while an async call is in flight
3140                  */
3141                 status = NT_STATUS_INVALID_PARAMETER;
3142                 goto fail;
3143         }
3144
3145         ev = event_context_init(frame);
3146         if (ev == NULL) {
3147                 status = NT_STATUS_NO_MEMORY;
3148                 goto fail;
3149         }
3150
3151         req = cli_posix_unlink_send(frame,
3152                                 ev,
3153                                 cli,
3154                                 fname);
3155         if (req == NULL) {
3156                 status = NT_STATUS_NO_MEMORY;
3157                 goto fail;
3158         }
3159
3160         if (!tevent_req_poll(req, ev)) {
3161                 status = map_nt_error_from_unix(errno);
3162                 goto fail;
3163         }
3164
3165         status = cli_posix_unlink_recv(req, frame);
3166
3167  fail:
3168         TALLOC_FREE(frame);
3169         if (!NT_STATUS_IS_OK(status)) {
3170                 cli_set_error(cli, status);
3171         }
3172         return status;
3173 }
3174
3175 /****************************************************************************
3176  rmdir - POSIX semantics.
3177 ****************************************************************************/
3178
3179 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3180                                         struct event_context *ev,
3181                                         struct cli_state *cli,
3182                                         const char *fname)
3183 {
3184         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3185 }
3186
3187 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3188 {
3189         NTSTATUS status;
3190
3191         if (tevent_req_is_nterror(req, &status)) {
3192                 return status;
3193         }
3194         return NT_STATUS_OK;
3195 }
3196
3197 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3198 {
3199         TALLOC_CTX *frame = talloc_stackframe();
3200         struct event_context *ev = NULL;
3201         struct tevent_req *req = NULL;
3202         NTSTATUS status = NT_STATUS_OK;
3203
3204         if (cli_has_async_calls(cli)) {
3205                 /*
3206                  * Can't use sync call while an async call is in flight
3207                  */
3208                 status = NT_STATUS_INVALID_PARAMETER;
3209                 goto fail;
3210         }
3211
3212         ev = event_context_init(frame);
3213         if (ev == NULL) {
3214                 status = NT_STATUS_NO_MEMORY;
3215                 goto fail;
3216         }
3217
3218         req = cli_posix_rmdir_send(frame,
3219                                 ev,
3220                                 cli,
3221                                 fname);
3222         if (req == NULL) {
3223                 status = NT_STATUS_NO_MEMORY;
3224                 goto fail;
3225         }
3226
3227         if (!tevent_req_poll(req, ev)) {
3228                 status = map_nt_error_from_unix(errno);
3229                 goto fail;
3230         }
3231
3232         status = cli_posix_rmdir_recv(req, frame);
3233
3234  fail:
3235         TALLOC_FREE(frame);
3236         if (!NT_STATUS_IS_OK(status)) {
3237                 cli_set_error(cli, status);
3238         }
3239         return status;
3240 }