54c59473e6c46451a26e1f8277859cabcdd804dd
[vlendec/samba-autobuild/.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-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, int 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 /****************************************************************************
1124  Open a file - exposing the full horror of the NT API :-).
1125  Used in smbtorture.
1126 ****************************************************************************/
1127
1128 int cli_nt_create_full(struct cli_state *cli, const char *fname,
1129                        uint32_t CreatFlags, uint32_t DesiredAccess,
1130                        uint32_t FileAttributes, uint32_t ShareAccess,
1131                        uint32_t CreateDisposition, uint32_t CreateOptions,
1132                        uint8_t SecurityFlags)
1133 {
1134         char *p;
1135         int len;
1136
1137         memset(cli->outbuf,'\0',smb_size);
1138         memset(cli->inbuf,'\0',smb_size);
1139
1140         cli_set_message(cli->outbuf,24,0, true);
1141
1142         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
1143         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1144         cli_setup_packet(cli);
1145
1146         SSVAL(cli->outbuf,smb_vwv0,0xFF);
1147         if (cli->use_oplocks)
1148                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1149
1150         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
1151         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
1152         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
1153         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
1154         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
1155         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
1156         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
1157         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
1158         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
1159
1160         p = smb_buf(cli->outbuf);
1161         /* this alignment and termination is critical for netapp filers. Don't change */
1162         p += clistr_align_out(cli, p, 0);
1163         len = clistr_push(cli, p, fname,
1164                         cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
1165         p += len;
1166         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
1167         /* sigh. this copes with broken netapp filer behaviour */
1168         p += clistr_push(cli, p, "",
1169                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1170
1171         cli_setup_bcc(cli, p);
1172
1173         cli_send_smb(cli);
1174         if (!cli_receive_smb(cli)) {
1175                 return -1;
1176         }
1177
1178         if (cli_is_error(cli)) {
1179                 return -1;
1180         }
1181
1182         return SVAL(cli->inbuf,smb_vwv2 + 1);
1183 }
1184
1185 struct cli_ntcreate_state {
1186         uint16_t vwv[24];
1187         uint16_t fnum;
1188 };
1189
1190 static void cli_ntcreate_done(struct tevent_req *subreq);
1191
1192 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1193                                      struct event_context *ev,
1194                                      struct cli_state *cli,
1195                                      const char *fname,
1196                                      uint32_t CreatFlags,
1197                                      uint32_t DesiredAccess,
1198                                      uint32_t FileAttributes,
1199                                      uint32_t ShareAccess,
1200                                      uint32_t CreateDisposition,
1201                                      uint32_t CreateOptions,
1202                                      uint8_t SecurityFlags)
1203 {
1204         struct tevent_req *req, *subreq;
1205         struct cli_ntcreate_state *state;
1206         uint16_t *vwv;
1207         uint8_t *bytes;
1208         size_t converted_len;
1209
1210         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1211         if (req == NULL) {
1212                 return NULL;
1213         }
1214         vwv = state->vwv;
1215
1216         SCVAL(vwv+0, 0, 0xFF);
1217         SCVAL(vwv+0, 1, 0);
1218         SSVAL(vwv+1, 0, 0);
1219         SCVAL(vwv+2, 0, 0);
1220
1221         if (cli->use_oplocks) {
1222                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1223         }
1224         SIVAL(vwv+3, 1, CreatFlags);
1225         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
1226         SIVAL(vwv+7, 1, DesiredAccess);
1227         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
1228         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
1229         SIVAL(vwv+13, 1, FileAttributes);
1230         SIVAL(vwv+15, 1, ShareAccess);
1231         SIVAL(vwv+17, 1, CreateDisposition);
1232         SIVAL(vwv+19, 1, CreateOptions);
1233         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1234         SCVAL(vwv+23, 1, SecurityFlags);
1235
1236         bytes = talloc_array(state, uint8_t, 0);
1237         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1238                                    fname, strlen(fname)+1,
1239                                    &converted_len);
1240
1241         /* sigh. this copes with broken netapp filer behaviour */
1242         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1243
1244         if (tevent_req_nomem(bytes, req)) {
1245                 return tevent_req_post(req, ev);
1246         }
1247
1248         SIVAL(vwv+2, 1, converted_len);
1249
1250         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1251                               talloc_get_size(bytes), bytes);
1252         if (tevent_req_nomem(subreq, req)) {
1253                 return tevent_req_post(req, ev);
1254         }
1255         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1256         return req;
1257 }
1258
1259 static void cli_ntcreate_done(struct tevent_req *subreq)
1260 {
1261         struct tevent_req *req = tevent_req_callback_data(
1262                 subreq, struct tevent_req);
1263         struct cli_ntcreate_state *state = tevent_req_data(
1264                 req, struct cli_ntcreate_state);
1265         uint8_t wct;
1266         uint16_t *vwv;
1267         uint32_t num_bytes;
1268         uint8_t *bytes;
1269         NTSTATUS status;
1270
1271         status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1272         if (!NT_STATUS_IS_OK(status)) {
1273                 TALLOC_FREE(subreq);
1274                 tevent_req_nterror(req, status);
1275                 return;
1276         }
1277         state->fnum = SVAL(vwv+2, 1);
1278         tevent_req_done(req);
1279 }
1280
1281 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1282 {
1283         struct cli_ntcreate_state *state = tevent_req_data(
1284                 req, struct cli_ntcreate_state);
1285         NTSTATUS status;
1286
1287         if (tevent_req_is_nterror(req, &status)) {
1288                 return status;
1289         }
1290         *pfnum = state->fnum;
1291         return NT_STATUS_OK;
1292 }
1293
1294 NTSTATUS cli_ntcreate(struct cli_state *cli,
1295                       const char *fname,
1296                       uint32_t CreatFlags,
1297                       uint32_t DesiredAccess,
1298                       uint32_t FileAttributes,
1299                       uint32_t ShareAccess,
1300                       uint32_t CreateDisposition,
1301                       uint32_t CreateOptions,
1302                       uint8_t SecurityFlags,
1303                       uint16_t *pfid)
1304 {
1305         TALLOC_CTX *frame = talloc_stackframe();
1306         struct event_context *ev;
1307         struct tevent_req *req;
1308         NTSTATUS status = NT_STATUS_OK;
1309
1310         if (cli_has_async_calls(cli)) {
1311                 /*
1312                  * Can't use sync call while an async call is in flight
1313                  */
1314                 status = NT_STATUS_INVALID_PARAMETER;
1315                 goto fail;
1316         }
1317
1318         ev = event_context_init(frame);
1319         if (ev == NULL) {
1320                 status = NT_STATUS_NO_MEMORY;
1321                 goto fail;
1322         }
1323
1324         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1325                                 DesiredAccess, FileAttributes, ShareAccess,
1326                                 CreateDisposition, CreateOptions,
1327                                 SecurityFlags);
1328         if (req == NULL) {
1329                 status = NT_STATUS_NO_MEMORY;
1330                 goto fail;
1331         }
1332
1333         if (!tevent_req_poll(req, ev)) {
1334                 status = map_nt_error_from_unix(errno);
1335                 goto fail;
1336         }
1337
1338         status = cli_ntcreate_recv(req, pfid);
1339  fail:
1340         TALLOC_FREE(frame);
1341         if (!NT_STATUS_IS_OK(status)) {
1342                 cli_set_error(cli, status);
1343         }
1344         return status;
1345 }
1346
1347 /****************************************************************************
1348  Open a file.
1349 ****************************************************************************/
1350
1351 int cli_nt_create(struct cli_state *cli, const char *fname, uint32_t DesiredAccess)
1352 {
1353         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
1354                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
1355 }
1356
1357 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1358                             const char *str, size_t str_len,
1359                             size_t *pconverted_size)
1360 {
1361         size_t buflen;
1362         char *converted;
1363         size_t converted_size;
1364
1365         if (buf == NULL) {
1366                 return NULL;
1367         }
1368
1369         buflen = talloc_get_size(buf);
1370         /*
1371          * We're pushing into an SMB buffer, align odd
1372          */
1373         if (ucs2 && (buflen % 2 == 0)) {
1374                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1375                 if (buf == NULL) {
1376                         return NULL;
1377                 }
1378                 buf[buflen] = '\0';
1379                 buflen += 1;
1380         }
1381
1382         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1383                                    ucs2 ? CH_UTF16LE : CH_DOS,
1384                                    str, str_len, &converted,
1385                                    &converted_size, true)) {
1386                 return NULL;
1387         }
1388
1389         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1390                                    buflen + converted_size);
1391         if (buf == NULL) {
1392                 TALLOC_FREE(converted);
1393                 return NULL;
1394         }
1395
1396         memcpy(buf + buflen, converted, converted_size);
1397
1398         TALLOC_FREE(converted);
1399
1400         if (pconverted_size) {
1401                 *pconverted_size = converted_size;
1402         }
1403
1404         return buf;
1405 }
1406
1407 /****************************************************************************
1408  Open a file
1409  WARNING: if you open with O_WRONLY then getattrE won't work!
1410 ****************************************************************************/
1411
1412 struct cli_open_state {
1413         uint16_t vwv[15];
1414         int fnum;
1415         struct iovec bytes;
1416 };
1417
1418 static void cli_open_done(struct tevent_req *subreq);
1419
1420 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1421                                    struct event_context *ev,
1422                                    struct cli_state *cli, const char *fname,
1423                                    int flags, int share_mode,
1424                                    struct tevent_req **psmbreq)
1425 {
1426         struct tevent_req *req, *subreq;
1427         struct cli_open_state *state;
1428         unsigned openfn;
1429         unsigned accessmode;
1430         uint8_t additional_flags;
1431         uint8_t *bytes;
1432
1433         req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1434         if (req == NULL) {
1435                 return NULL;
1436         }
1437
1438         openfn = 0;
1439         if (flags & O_CREAT) {
1440                 openfn |= (1<<4);
1441         }
1442         if (!(flags & O_EXCL)) {
1443                 if (flags & O_TRUNC)
1444                         openfn |= (1<<1);
1445                 else
1446                         openfn |= (1<<0);
1447         }
1448
1449         accessmode = (share_mode<<4);
1450
1451         if ((flags & O_ACCMODE) == O_RDWR) {
1452                 accessmode |= 2;
1453         } else if ((flags & O_ACCMODE) == O_WRONLY) {
1454                 accessmode |= 1;
1455         }
1456
1457 #if defined(O_SYNC)
1458         if ((flags & O_SYNC) == O_SYNC) {
1459                 accessmode |= (1<<14);
1460         }
1461 #endif /* O_SYNC */
1462
1463         if (share_mode == DENY_FCB) {
1464                 accessmode = 0xFF;
1465         }
1466
1467         SCVAL(state->vwv + 0, 0, 0xFF);
1468         SCVAL(state->vwv + 0, 1, 0);
1469         SSVAL(state->vwv + 1, 0, 0);
1470         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
1471         SSVAL(state->vwv + 3, 0, accessmode);
1472         SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1473         SSVAL(state->vwv + 5, 0, 0);
1474         SIVAL(state->vwv + 6, 0, 0);
1475         SSVAL(state->vwv + 8, 0, openfn);
1476         SIVAL(state->vwv + 9, 0, 0);
1477         SIVAL(state->vwv + 11, 0, 0);
1478         SIVAL(state->vwv + 13, 0, 0);
1479
1480         additional_flags = 0;
1481
1482         if (cli->use_oplocks) {
1483                 /* if using oplocks then ask for a batch oplock via
1484                    core and extended methods */
1485                 additional_flags =
1486                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1487                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1488         }
1489
1490         bytes = talloc_array(state, uint8_t, 0);
1491         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1492                                    strlen(fname)+1, NULL);
1493
1494         if (tevent_req_nomem(bytes, req)) {
1495                 return tevent_req_post(req, ev);
1496         }
1497
1498         state->bytes.iov_base = bytes;
1499         state->bytes.iov_len = talloc_get_size(bytes);
1500
1501         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1502                                     15, state->vwv, 1, &state->bytes);
1503         if (subreq == NULL) {
1504                 TALLOC_FREE(req);
1505                 return NULL;
1506         }
1507         tevent_req_set_callback(subreq, cli_open_done, req);
1508         *psmbreq = subreq;
1509         return req;
1510 }
1511
1512 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1513                                  struct cli_state *cli, const char *fname,
1514                                  int flags, int share_mode)
1515 {
1516         struct tevent_req *req, *subreq;
1517
1518         req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1519                               &subreq);
1520         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1521                 TALLOC_FREE(req);
1522                 return NULL;
1523         }
1524         return req;
1525 }
1526
1527 static void cli_open_done(struct tevent_req *subreq)
1528 {
1529         struct tevent_req *req = tevent_req_callback_data(
1530                 subreq, struct tevent_req);
1531         struct cli_open_state *state = tevent_req_data(
1532                 req, struct cli_open_state);
1533         uint8_t wct;
1534         uint16_t *vwv;
1535         NTSTATUS status;
1536
1537         status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1538         if (!NT_STATUS_IS_OK(status)) {
1539                 TALLOC_FREE(subreq);
1540                 tevent_req_nterror(req, status);
1541                 return;
1542         }
1543         state->fnum = SVAL(vwv+2, 0);
1544         tevent_req_done(req);
1545 }
1546
1547 NTSTATUS cli_open_recv(struct tevent_req *req, int *fnum)
1548 {
1549         struct cli_open_state *state = tevent_req_data(
1550                 req, struct cli_open_state);
1551         NTSTATUS status;
1552
1553         if (tevent_req_is_nterror(req, &status)) {
1554                 return status;
1555         }
1556         *fnum = state->fnum;
1557         return NT_STATUS_OK;
1558 }
1559
1560 int cli_open(struct cli_state *cli, const char *fname, int flags,
1561              int share_mode)
1562 {
1563         TALLOC_CTX *frame = talloc_stackframe();
1564         struct event_context *ev;
1565         struct tevent_req *req;
1566         NTSTATUS status = NT_STATUS_OK;
1567         int result = -1;
1568
1569         if (cli_has_async_calls(cli)) {
1570                 /*
1571                  * Can't use sync call while an async call is in flight
1572                  */
1573                 status = NT_STATUS_INVALID_PARAMETER;
1574                 goto fail;
1575         }
1576
1577         ev = event_context_init(frame);
1578         if (ev == NULL) {
1579                 status = NT_STATUS_NO_MEMORY;
1580                 goto fail;
1581         }
1582
1583         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1584         if (req == NULL) {
1585                 status = NT_STATUS_NO_MEMORY;
1586                 goto fail;
1587         }
1588
1589         if (!tevent_req_poll(req, ev)) {
1590                 status = map_nt_error_from_unix(errno);
1591                 goto fail;
1592         }
1593
1594         cli_open_recv(req, &result);
1595  fail:
1596         TALLOC_FREE(frame);
1597         if (!NT_STATUS_IS_OK(status)) {
1598                 cli_set_error(cli, status);
1599         }
1600         return result;
1601 }
1602
1603 /****************************************************************************
1604  Close a file.
1605 ****************************************************************************/
1606
1607 struct cli_close_state {
1608         uint16_t vwv[3];
1609 };
1610
1611 static void cli_close_done(struct tevent_req *subreq);
1612
1613 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1614                                     struct event_context *ev,
1615                                     struct cli_state *cli, int fnum,
1616                                     struct tevent_req **psubreq)
1617 {
1618         struct tevent_req *req, *subreq;
1619         struct cli_close_state *state;
1620
1621         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1622         if (req == NULL) {
1623                 return NULL;
1624         }
1625         SSVAL(state->vwv+0, 0, fnum);
1626         SIVALS(state->vwv+1, 0, -1);
1627
1628         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1629                                     0, NULL);
1630         if (subreq == NULL) {
1631                 TALLOC_FREE(req);
1632                 return NULL;
1633         }
1634         tevent_req_set_callback(subreq, cli_close_done, req);
1635         *psubreq = subreq;
1636         return req;
1637 }
1638
1639 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1640                                   struct event_context *ev,
1641                                   struct cli_state *cli, int fnum)
1642 {
1643         struct tevent_req *req, *subreq;
1644
1645         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1646         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1647                 TALLOC_FREE(req);
1648                 return NULL;
1649         }
1650         return req;
1651 }
1652
1653 static void cli_close_done(struct tevent_req *subreq)
1654 {
1655         struct tevent_req *req = tevent_req_callback_data(
1656                 subreq, struct tevent_req);
1657         NTSTATUS status;
1658
1659         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1660         TALLOC_FREE(subreq);
1661         if (!NT_STATUS_IS_OK(status)) {
1662                 tevent_req_nterror(req, status);
1663                 return;
1664         }
1665         tevent_req_done(req);
1666 }
1667
1668 NTSTATUS cli_close_recv(struct tevent_req *req)
1669 {
1670         return tevent_req_simple_recv_ntstatus(req);
1671 }
1672
1673 bool cli_close(struct cli_state *cli, int fnum)
1674 {
1675         TALLOC_CTX *frame = talloc_stackframe();
1676         struct event_context *ev;
1677         struct tevent_req *req;
1678         NTSTATUS status = NT_STATUS_OK;
1679         bool result = false;
1680
1681         if (cli_has_async_calls(cli)) {
1682                 /*
1683                  * Can't use sync call while an async call is in flight
1684                  */
1685                 status = NT_STATUS_INVALID_PARAMETER;
1686                 goto fail;
1687         }
1688
1689         ev = event_context_init(frame);
1690         if (ev == NULL) {
1691                 status = NT_STATUS_NO_MEMORY;
1692                 goto fail;
1693         }
1694
1695         req = cli_close_send(frame, ev, cli, fnum);
1696         if (req == NULL) {
1697                 status = NT_STATUS_NO_MEMORY;
1698                 goto fail;
1699         }
1700
1701         if (!tevent_req_poll(req, ev)) {
1702                 status = map_nt_error_from_unix(errno);
1703                 goto fail;
1704         }
1705
1706         result = NT_STATUS_IS_OK(cli_close_recv(req));
1707  fail:
1708         TALLOC_FREE(frame);
1709         if (!NT_STATUS_IS_OK(status)) {
1710                 cli_set_error(cli, status);
1711         }
1712         return result;
1713 }
1714
1715 /****************************************************************************
1716  Truncate a file to a specified size
1717 ****************************************************************************/
1718
1719 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1720 {
1721         unsigned int param_len = 6;
1722         unsigned int data_len = 8;
1723         uint16_t setup = TRANSACT2_SETFILEINFO;
1724         char param[6];
1725         unsigned char data[8];
1726         char *rparam=NULL, *rdata=NULL;
1727         int saved_timeout = cli->timeout;
1728
1729         SSVAL(param,0,fnum);
1730         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1731         SSVAL(param,4,0);
1732
1733         SBVAL(data, 0, size);
1734
1735         if (!cli_send_trans(cli, SMBtrans2,
1736                             NULL,                    /* name */
1737                             -1, 0,                   /* fid, flags */
1738                             &setup, 1, 0,            /* setup, length, max */
1739                             param, param_len, 2,     /* param, length, max */
1740                             (char *)&data,  data_len,/* data, length, ... */
1741                             cli->max_xmit)) {        /* ... max */
1742                 cli->timeout = saved_timeout;
1743                 return False;
1744         }
1745
1746         if (!cli_receive_trans(cli, SMBtrans2,
1747                                 &rparam, &param_len,
1748                                 &rdata, &data_len)) {
1749                 cli->timeout = saved_timeout;
1750                 SAFE_FREE(rdata);
1751                 SAFE_FREE(rparam);
1752                 return False;
1753         }
1754
1755         cli->timeout = saved_timeout;
1756
1757         SAFE_FREE(rdata);
1758         SAFE_FREE(rparam);
1759
1760         return True;
1761 }
1762
1763
1764 /****************************************************************************
1765  send a lock with a specified locktype
1766  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1767 ****************************************************************************/
1768
1769 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1770                       uint32_t offset, uint32_t len,
1771                       int timeout, unsigned char locktype)
1772 {
1773         char *p;
1774         int saved_timeout = cli->timeout;
1775
1776         memset(cli->outbuf,'\0',smb_size);
1777         memset(cli->inbuf,'\0', smb_size);
1778
1779         cli_set_message(cli->outbuf,8,0,True);
1780
1781         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1782         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1783         cli_setup_packet(cli);
1784
1785         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1786         SSVAL(cli->outbuf,smb_vwv2,fnum);
1787         SCVAL(cli->outbuf,smb_vwv3,locktype);
1788         SIVALS(cli->outbuf, smb_vwv4, timeout);
1789         SSVAL(cli->outbuf,smb_vwv6,0);
1790         SSVAL(cli->outbuf,smb_vwv7,1);
1791
1792         p = smb_buf(cli->outbuf);
1793         SSVAL(p, 0, cli->pid);
1794         SIVAL(p, 2, offset);
1795         SIVAL(p, 6, len);
1796
1797         p += 10;
1798
1799         cli_setup_bcc(cli, p);
1800
1801         cli_send_smb(cli);
1802
1803         if (timeout != 0) {
1804                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1805         }
1806
1807         if (!cli_receive_smb(cli)) {
1808                 cli->timeout = saved_timeout;
1809                 return NT_STATUS_UNSUCCESSFUL;
1810         }
1811
1812         cli->timeout = saved_timeout;
1813
1814         return cli_nt_error(cli);
1815 }
1816
1817 /****************************************************************************
1818  Lock a file.
1819  note that timeout is in units of 2 milliseconds
1820 ****************************************************************************/
1821
1822 bool cli_lock(struct cli_state *cli, int fnum,
1823               uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1824 {
1825         char *p;
1826         int saved_timeout = cli->timeout;
1827
1828         memset(cli->outbuf,'\0',smb_size);
1829         memset(cli->inbuf,'\0', smb_size);
1830
1831         cli_set_message(cli->outbuf,8,0,True);
1832
1833         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1834         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1835         cli_setup_packet(cli);
1836
1837         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1838         SSVAL(cli->outbuf,smb_vwv2,fnum);
1839         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1840         SIVALS(cli->outbuf, smb_vwv4, timeout);
1841         SSVAL(cli->outbuf,smb_vwv6,0);
1842         SSVAL(cli->outbuf,smb_vwv7,1);
1843
1844         p = smb_buf(cli->outbuf);
1845         SSVAL(p, 0, cli->pid);
1846         SIVAL(p, 2, offset);
1847         SIVAL(p, 6, len);
1848
1849         p += 10;
1850
1851         cli_setup_bcc(cli, p);
1852
1853         cli_send_smb(cli);
1854
1855         if (timeout != 0) {
1856                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1857         }
1858
1859         if (!cli_receive_smb(cli)) {
1860                 cli->timeout = saved_timeout;
1861                 return False;
1862         }
1863
1864         cli->timeout = saved_timeout;
1865
1866         if (cli_is_error(cli)) {
1867                 return False;
1868         }
1869
1870         return True;
1871 }
1872
1873 /****************************************************************************
1874  Unlock a file.
1875 ****************************************************************************/
1876
1877 bool cli_unlock(struct cli_state *cli, int fnum, uint32_t offset, uint32_t len)
1878 {
1879         char *p;
1880
1881         memset(cli->outbuf,'\0',smb_size);
1882         memset(cli->inbuf,'\0',smb_size);
1883
1884         cli_set_message(cli->outbuf,8,0,True);
1885
1886         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1887         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1888         cli_setup_packet(cli);
1889
1890         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1891         SSVAL(cli->outbuf,smb_vwv2,fnum);
1892         SCVAL(cli->outbuf,smb_vwv3,0);
1893         SIVALS(cli->outbuf, smb_vwv4, 0);
1894         SSVAL(cli->outbuf,smb_vwv6,1);
1895         SSVAL(cli->outbuf,smb_vwv7,0);
1896
1897         p = smb_buf(cli->outbuf);
1898         SSVAL(p, 0, cli->pid);
1899         SIVAL(p, 2, offset);
1900         SIVAL(p, 6, len);
1901         p += 10;
1902         cli_setup_bcc(cli, p);
1903         cli_send_smb(cli);
1904         if (!cli_receive_smb(cli)) {
1905                 return False;
1906         }
1907
1908         if (cli_is_error(cli)) {
1909                 return False;
1910         }
1911
1912         return True;
1913 }
1914
1915 /****************************************************************************
1916  Lock a file with 64 bit offsets.
1917 ****************************************************************************/
1918
1919 bool cli_lock64(struct cli_state *cli, int fnum,
1920                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1921 {
1922         char *p;
1923         int saved_timeout = cli->timeout;
1924         int ltype;
1925
1926         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1927                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1928         }
1929
1930         ltype = (lock_type == READ_LOCK? 1 : 0);
1931         ltype |= LOCKING_ANDX_LARGE_FILES;
1932
1933         memset(cli->outbuf,'\0',smb_size);
1934         memset(cli->inbuf,'\0', smb_size);
1935
1936         cli_set_message(cli->outbuf,8,0,True);
1937
1938         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1939         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1940         cli_setup_packet(cli);
1941
1942         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1943         SSVAL(cli->outbuf,smb_vwv2,fnum);
1944         SCVAL(cli->outbuf,smb_vwv3,ltype);
1945         SIVALS(cli->outbuf, smb_vwv4, timeout);
1946         SSVAL(cli->outbuf,smb_vwv6,0);
1947         SSVAL(cli->outbuf,smb_vwv7,1);
1948
1949         p = smb_buf(cli->outbuf);
1950         SIVAL(p, 0, cli->pid);
1951         SOFF_T_R(p, 4, offset);
1952         SOFF_T_R(p, 12, len);
1953         p += 20;
1954
1955         cli_setup_bcc(cli, p);
1956         cli_send_smb(cli);
1957
1958         if (timeout != 0) {
1959                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1960         }
1961
1962         if (!cli_receive_smb(cli)) {
1963                 cli->timeout = saved_timeout;
1964                 return False;
1965         }
1966
1967         cli->timeout = saved_timeout;
1968
1969         if (cli_is_error(cli)) {
1970                 return False;
1971         }
1972
1973         return True;
1974 }
1975
1976 /****************************************************************************
1977  Unlock a file with 64 bit offsets.
1978 ****************************************************************************/
1979
1980 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1981 {
1982         char *p;
1983
1984         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1985                 return cli_unlock(cli, fnum, offset, len);
1986         }
1987
1988         memset(cli->outbuf,'\0',smb_size);
1989         memset(cli->inbuf,'\0',smb_size);
1990
1991         cli_set_message(cli->outbuf,8,0,True);
1992
1993         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1994         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1995         cli_setup_packet(cli);
1996
1997         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1998         SSVAL(cli->outbuf,smb_vwv2,fnum);
1999         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2000         SIVALS(cli->outbuf, smb_vwv4, 0);
2001         SSVAL(cli->outbuf,smb_vwv6,1);
2002         SSVAL(cli->outbuf,smb_vwv7,0);
2003
2004         p = smb_buf(cli->outbuf);
2005         SIVAL(p, 0, cli->pid);
2006         SOFF_T_R(p, 4, offset);
2007         SOFF_T_R(p, 12, len);
2008         p += 20;
2009         cli_setup_bcc(cli, p);
2010         cli_send_smb(cli);
2011         if (!cli_receive_smb(cli)) {
2012                 return False;
2013         }
2014
2015         if (cli_is_error(cli)) {
2016                 return False;
2017         }
2018
2019         return True;
2020 }
2021
2022 /****************************************************************************
2023  Get/unlock a POSIX lock on a file - internal function.
2024 ****************************************************************************/
2025
2026 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
2027                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2028 {
2029         unsigned int param_len = 4;
2030         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2031         uint16_t setup = TRANSACT2_SETFILEINFO;
2032         char param[4];
2033         unsigned char data[POSIX_LOCK_DATA_SIZE];
2034         char *rparam=NULL, *rdata=NULL;
2035         int saved_timeout = cli->timeout;
2036
2037         SSVAL(param,0,fnum);
2038         SSVAL(param,2,SMB_SET_POSIX_LOCK);
2039
2040         switch (lock_type) {
2041                 case READ_LOCK:
2042                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2043                         break;
2044                 case WRITE_LOCK:
2045                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2046                         break;
2047                 case UNLOCK_LOCK:
2048                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2049                         break;
2050                 default:
2051                         return False;
2052         }
2053
2054         if (wait_lock) {
2055                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2056                 cli->timeout = 0x7FFFFFFF;
2057         } else {
2058                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2059         }
2060
2061         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2062         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2063         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2064
2065         if (!cli_send_trans(cli, SMBtrans2,
2066                         NULL,                        /* name */
2067                         -1, 0,                          /* fid, flags */
2068                         &setup, 1, 0,                   /* setup, length, max */
2069                         param, param_len, 2,            /* param, length, max */
2070                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2071                         )) {
2072                 cli->timeout = saved_timeout;
2073                 return False;
2074         }
2075
2076         if (!cli_receive_trans(cli, SMBtrans2,
2077                                 &rparam, &param_len,
2078                                 &rdata, &data_len)) {
2079                 cli->timeout = saved_timeout;
2080                 SAFE_FREE(rdata);
2081                 SAFE_FREE(rparam);
2082                 return False;
2083         }
2084
2085         cli->timeout = saved_timeout;
2086
2087         SAFE_FREE(rdata);
2088         SAFE_FREE(rparam);
2089
2090         return True;
2091 }
2092
2093 /****************************************************************************
2094  POSIX Lock a file.
2095 ****************************************************************************/
2096
2097 bool cli_posix_lock(struct cli_state *cli, int fnum,
2098                         uint64_t offset, uint64_t len,
2099                         bool wait_lock, enum brl_type lock_type)
2100 {
2101         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2102                 return False;
2103         }
2104         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2105 }
2106
2107 /****************************************************************************
2108  POSIX Unlock a file.
2109 ****************************************************************************/
2110
2111 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
2112 {
2113         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2114 }
2115
2116 /****************************************************************************
2117  POSIX Get any lock covering a file.
2118 ****************************************************************************/
2119
2120 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
2121 {
2122         return True;
2123 }
2124
2125 /****************************************************************************
2126  Do a SMBgetattrE call.
2127 ****************************************************************************/
2128
2129 bool cli_getattrE(struct cli_state *cli, int fd,
2130                   uint16_t *attr, SMB_OFF_T *size,
2131                   time_t *change_time,
2132                   time_t *access_time,
2133                   time_t *write_time)
2134 {
2135         memset(cli->outbuf,'\0',smb_size);
2136         memset(cli->inbuf,'\0',smb_size);
2137
2138         cli_set_message(cli->outbuf,1,0,True);
2139
2140         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
2141         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2142         cli_setup_packet(cli);
2143
2144         SSVAL(cli->outbuf,smb_vwv0,fd);
2145
2146         cli_send_smb(cli);
2147         if (!cli_receive_smb(cli)) {
2148                 return False;
2149         }
2150
2151         if (cli_is_error(cli)) {
2152                 return False;
2153         }
2154
2155         if (size) {
2156                 *size = IVAL(cli->inbuf, smb_vwv6);
2157         }
2158
2159         if (attr) {
2160                 *attr = SVAL(cli->inbuf,smb_vwv10);
2161         }
2162
2163         if (change_time) {
2164                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
2165         }
2166
2167         if (access_time) {
2168                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
2169         }
2170
2171         if (write_time) {
2172                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
2173         }
2174
2175         return True;
2176 }
2177
2178 /****************************************************************************
2179  Do a SMBgetatr call
2180 ****************************************************************************/
2181
2182 bool cli_getatr(struct cli_state *cli, const char *fname,
2183                 uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
2184 {
2185         char *p;
2186
2187         memset(cli->outbuf,'\0',smb_size);
2188         memset(cli->inbuf,'\0',smb_size);
2189
2190         cli_set_message(cli->outbuf,0,0,True);
2191
2192         SCVAL(cli->outbuf,smb_com,SMBgetatr);
2193         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2194         cli_setup_packet(cli);
2195
2196         p = smb_buf(cli->outbuf);
2197         *p++ = 4;
2198         p += clistr_push(cli, p, fname,
2199                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2200
2201         cli_setup_bcc(cli, p);
2202
2203         cli_send_smb(cli);
2204         if (!cli_receive_smb(cli)) {
2205                 return False;
2206         }
2207
2208         if (cli_is_error(cli)) {
2209                 return False;
2210         }
2211
2212         if (size) {
2213                 *size = IVAL(cli->inbuf, smb_vwv3);
2214         }
2215
2216         if (write_time) {
2217                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
2218         }
2219
2220         if (attr) {
2221                 *attr = SVAL(cli->inbuf,smb_vwv0);
2222         }
2223
2224         return True;
2225 }
2226
2227 /****************************************************************************
2228  Do a SMBsetattrE call.
2229 ****************************************************************************/
2230
2231 bool cli_setattrE(struct cli_state *cli, int fd,
2232                   time_t change_time,
2233                   time_t access_time,
2234                   time_t write_time)
2235
2236 {
2237         char *p;
2238
2239         memset(cli->outbuf,'\0',smb_size);
2240         memset(cli->inbuf,'\0',smb_size);
2241
2242         cli_set_message(cli->outbuf,7,0,True);
2243
2244         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
2245         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2246         cli_setup_packet(cli);
2247
2248         SSVAL(cli->outbuf,smb_vwv0, fd);
2249         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
2250         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
2251         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
2252
2253         p = smb_buf(cli->outbuf);
2254         *p++ = 4;
2255
2256         cli_setup_bcc(cli, p);
2257
2258         cli_send_smb(cli);
2259         if (!cli_receive_smb(cli)) {
2260                 return False;
2261         }
2262
2263         if (cli_is_error(cli)) {
2264                 return False;
2265         }
2266
2267         return True;
2268 }
2269
2270 /****************************************************************************
2271  Do a SMBsetatr call.
2272 ****************************************************************************/
2273
2274 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
2275 {
2276         char *p;
2277
2278         memset(cli->outbuf,'\0',smb_size);
2279         memset(cli->inbuf,'\0',smb_size);
2280
2281         cli_set_message(cli->outbuf,8,0,True);
2282
2283         SCVAL(cli->outbuf,smb_com,SMBsetatr);
2284         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2285         cli_setup_packet(cli);
2286
2287         SSVAL(cli->outbuf,smb_vwv0, attr);
2288         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
2289
2290         p = smb_buf(cli->outbuf);
2291         *p++ = 4;
2292         p += clistr_push(cli, p, fname,
2293                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2294         *p++ = 4;
2295
2296         cli_setup_bcc(cli, p);
2297
2298         cli_send_smb(cli);
2299         if (!cli_receive_smb(cli)) {
2300                 return False;
2301         }
2302
2303         if (cli_is_error(cli)) {
2304                 return False;
2305         }
2306
2307         return True;
2308 }
2309
2310 /****************************************************************************
2311  Check for existance of a dir.
2312 ****************************************************************************/
2313
2314 static void cli_chkpath_done(struct tevent_req *subreq);
2315
2316 struct cli_chkpath_state {
2317         int dummy;
2318 };
2319
2320 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2321                                   struct event_context *ev,
2322                                   struct cli_state *cli,
2323                                   const char *fname)
2324 {
2325         struct tevent_req *req = NULL, *subreq = NULL;
2326         struct cli_chkpath_state *state = NULL;
2327         uint8_t additional_flags = 0;
2328         uint8_t *bytes = NULL;
2329
2330         req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2331         if (req == NULL) {
2332                 return NULL;
2333         }
2334
2335         bytes = talloc_array(state, uint8_t, 1);
2336         if (tevent_req_nomem(bytes, req)) {
2337                 return tevent_req_post(req, ev);
2338         }
2339         bytes[0] = 4;
2340         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2341                                    strlen(fname)+1, NULL);
2342
2343         if (tevent_req_nomem(bytes, req)) {
2344                 return tevent_req_post(req, ev);
2345         }
2346
2347         subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2348                               0, NULL, talloc_get_size(bytes), bytes);
2349         if (tevent_req_nomem(subreq, req)) {
2350                 return tevent_req_post(req, ev);
2351         }
2352         tevent_req_set_callback(subreq, cli_chkpath_done, req);
2353         return req;
2354 }
2355
2356 static void cli_chkpath_done(struct tevent_req *subreq)
2357 {
2358         struct tevent_req *req = tevent_req_callback_data(
2359                 subreq, struct tevent_req);
2360         NTSTATUS status;
2361
2362         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2363         TALLOC_FREE(subreq);
2364         if (!NT_STATUS_IS_OK(status)) {
2365                 tevent_req_nterror(req, status);
2366                 return;
2367         }
2368         tevent_req_done(req);
2369 }
2370
2371 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2372 {
2373         return tevent_req_simple_recv_ntstatus(req);
2374 }
2375
2376 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2377 {
2378         TALLOC_CTX *frame = talloc_stackframe();
2379         struct event_context *ev = NULL;
2380         struct tevent_req *req = NULL;
2381         char *path2 = NULL;
2382         NTSTATUS status = NT_STATUS_OK;
2383
2384         if (cli_has_async_calls(cli)) {
2385                 /*
2386                  * Can't use sync call while an async call is in flight
2387                  */
2388                 status = NT_STATUS_INVALID_PARAMETER;
2389                 goto fail;
2390         }
2391
2392         path2 = talloc_strdup(frame, path);
2393         if (!path2) {
2394                 status = NT_STATUS_NO_MEMORY;
2395                 goto fail;
2396         }
2397         trim_char(path2,'\0','\\');
2398         if (!*path2) {
2399                 path2 = talloc_strdup(frame, "\\");
2400                 if (!path2) {
2401                         status = NT_STATUS_NO_MEMORY;
2402                         goto fail;
2403                 }
2404         }
2405
2406         ev = event_context_init(frame);
2407         if (ev == NULL) {
2408                 status = NT_STATUS_NO_MEMORY;
2409                 goto fail;
2410         }
2411
2412         req = cli_chkpath_send(frame, ev, cli, path2);
2413         if (req == NULL) {
2414                 status = NT_STATUS_NO_MEMORY;
2415                 goto fail;
2416         }
2417
2418         if (!tevent_req_poll(req, ev)) {
2419                 status = map_nt_error_from_unix(errno);
2420                 goto fail;
2421         }
2422
2423         status = cli_chkpath_recv(req);
2424
2425  fail:
2426         TALLOC_FREE(frame);
2427         if (!NT_STATUS_IS_OK(status)) {
2428                 cli_set_error(cli, status);
2429         }
2430         return status;
2431 }
2432
2433 /****************************************************************************
2434  Query disk space.
2435 ****************************************************************************/
2436
2437 static void cli_dskattr_done(struct tevent_req *subreq);
2438
2439 struct cli_dskattr_state {
2440         int bsize;
2441         int total;
2442         int avail;
2443 };
2444
2445 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2446                                   struct event_context *ev,
2447                                   struct cli_state *cli)
2448 {
2449         struct tevent_req *req = NULL, *subreq = NULL;
2450         struct cli_dskattr_state *state = NULL;
2451         uint8_t additional_flags = 0;
2452
2453         req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2454         if (req == NULL) {
2455                 return NULL;
2456         }
2457
2458         subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2459                               0, NULL, 0, NULL);
2460         if (tevent_req_nomem(subreq, req)) {
2461                 return tevent_req_post(req, ev);
2462         }
2463         tevent_req_set_callback(subreq, cli_dskattr_done, req);
2464         return req;
2465 }
2466
2467 static void cli_dskattr_done(struct tevent_req *subreq)
2468 {
2469         struct tevent_req *req = tevent_req_callback_data(
2470                 subreq, struct tevent_req);
2471         struct cli_dskattr_state *state = tevent_req_data(
2472                 req, struct cli_dskattr_state);
2473         uint8_t wct;
2474         uint16_t *vwv = NULL;
2475         NTSTATUS status;
2476
2477         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2478         if (!NT_STATUS_IS_OK(status)) {
2479                 tevent_req_nterror(req, status);
2480                 return;
2481         }
2482         state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2483         state->total = SVAL(vwv+0, 0);
2484         state->avail = SVAL(vwv+3, 0);
2485         TALLOC_FREE(subreq);
2486         tevent_req_done(req);
2487 }
2488
2489 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2490 {
2491         struct cli_dskattr_state *state = tevent_req_data(
2492                                 req, struct cli_dskattr_state);
2493         NTSTATUS status;
2494
2495         if (tevent_req_is_nterror(req, &status)) {
2496                 return status;
2497         }
2498         *bsize = state->bsize;
2499         *total = state->total;
2500         *avail = state->avail;
2501         return NT_STATUS_OK;
2502 }
2503
2504 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2505 {
2506         TALLOC_CTX *frame = talloc_stackframe();
2507         struct event_context *ev = NULL;
2508         struct tevent_req *req = NULL;
2509         NTSTATUS status = NT_STATUS_OK;
2510
2511         if (cli_has_async_calls(cli)) {
2512                 /*
2513                  * Can't use sync call while an async call is in flight
2514                  */
2515                 status = NT_STATUS_INVALID_PARAMETER;
2516                 goto fail;
2517         }
2518
2519         ev = event_context_init(frame);
2520         if (ev == NULL) {
2521                 status = NT_STATUS_NO_MEMORY;
2522                 goto fail;
2523         }
2524
2525         req = cli_dskattr_send(frame, ev, cli);
2526         if (req == NULL) {
2527                 status = NT_STATUS_NO_MEMORY;
2528                 goto fail;
2529         }
2530
2531         if (!tevent_req_poll(req, ev)) {
2532                 status = map_nt_error_from_unix(errno);
2533                 goto fail;
2534         }
2535
2536         status = cli_dskattr_recv(req, bsize, total, avail);
2537
2538  fail:
2539         TALLOC_FREE(frame);
2540         if (!NT_STATUS_IS_OK(status)) {
2541                 cli_set_error(cli, status);
2542         }
2543         return status;
2544 }
2545
2546 /****************************************************************************
2547  Create and open a temporary file.
2548 ****************************************************************************/
2549
2550 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2551 {
2552         int len;
2553         char *p;
2554
2555         memset(cli->outbuf,'\0',smb_size);
2556         memset(cli->inbuf,'\0',smb_size);
2557
2558         cli_set_message(cli->outbuf,3,0,True);
2559
2560         SCVAL(cli->outbuf,smb_com,SMBctemp);
2561         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2562         cli_setup_packet(cli);
2563
2564         SSVAL(cli->outbuf,smb_vwv0,0);
2565         SIVALS(cli->outbuf,smb_vwv1,-1);
2566
2567         p = smb_buf(cli->outbuf);
2568         *p++ = 4;
2569         p += clistr_push(cli, p, path,
2570                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2571
2572         cli_setup_bcc(cli, p);
2573
2574         cli_send_smb(cli);
2575         if (!cli_receive_smb(cli)) {
2576                 return -1;
2577         }
2578
2579         if (cli_is_error(cli)) {
2580                 return -1;
2581         }
2582
2583         /* despite the spec, the result has a -1, followed by
2584            length, followed by name */
2585         p = smb_buf(cli->inbuf);
2586         p += 4;
2587         len = smb_buflen(cli->inbuf) - 4;
2588         if (len <= 0 || len > PATH_MAX) return -1;
2589
2590         if (tmp_path) {
2591                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2592                 if (!path2) {
2593                         return -1;
2594                 }
2595                 clistr_pull(cli->inbuf, path2, p,
2596                             len+1, len, STR_ASCII);
2597                 *tmp_path = path2;
2598         }
2599
2600         return SVAL(cli->inbuf,smb_vwv0);
2601 }
2602
2603 /*
2604    send a raw ioctl - used by the torture code
2605 */
2606 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32_t code, DATA_BLOB *blob)
2607 {
2608         memset(cli->outbuf,'\0',smb_size);
2609         memset(cli->inbuf,'\0',smb_size);
2610
2611         cli_set_message(cli->outbuf, 3, 0, True);
2612         SCVAL(cli->outbuf,smb_com,SMBioctl);
2613         cli_setup_packet(cli);
2614
2615         SSVAL(cli->outbuf, smb_vwv0, fnum);
2616         SSVAL(cli->outbuf, smb_vwv1, code>>16);
2617         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2618
2619         cli_send_smb(cli);
2620         if (!cli_receive_smb(cli)) {
2621                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2622         }
2623
2624         if (cli_is_error(cli)) {
2625                 return cli_nt_error(cli);
2626         }
2627
2628         *blob = data_blob_null;
2629
2630         return NT_STATUS_OK;
2631 }
2632
2633 /*********************************************************
2634  Set an extended attribute utility fn.
2635 *********************************************************/
2636
2637 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2638                         const char *ea_name, const char *ea_val, size_t ea_len)
2639 {
2640         unsigned int data_len = 0;
2641         char *data = NULL;
2642         char *rparam=NULL, *rdata=NULL;
2643         char *p;
2644         size_t ea_namelen = strlen(ea_name);
2645
2646         if (ea_namelen == 0 && ea_len == 0) {
2647                 data_len = 4;
2648                 data = (char *)SMB_MALLOC(data_len);
2649                 if (!data) {
2650                         return False;
2651                 }
2652                 p = data;
2653                 SIVAL(p,0,data_len);
2654         } else {
2655                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2656                 data = (char *)SMB_MALLOC(data_len);
2657                 if (!data) {
2658                         return False;
2659                 }
2660                 p = data;
2661                 SIVAL(p,0,data_len);
2662                 p += 4;
2663                 SCVAL(p, 0, 0); /* EA flags. */
2664                 SCVAL(p, 1, ea_namelen);
2665                 SSVAL(p, 2, ea_len);
2666                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2667                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2668         }
2669
2670         if (!cli_send_trans(cli, SMBtrans2,
2671                         NULL,                        /* name */
2672                         -1, 0,                          /* fid, flags */
2673                         &setup, 1, 0,                   /* setup, length, max */
2674                         param, param_len, 2,            /* param, length, max */
2675                         data,  data_len, cli->max_xmit /* data, length, max */
2676                         )) {
2677                 SAFE_FREE(data);
2678                 return False;
2679         }
2680
2681         if (!cli_receive_trans(cli, SMBtrans2,
2682                         &rparam, &param_len,
2683                         &rdata, &data_len)) {
2684                         SAFE_FREE(data);
2685                 return false;
2686         }
2687
2688         SAFE_FREE(data);
2689         SAFE_FREE(rdata);
2690         SAFE_FREE(rparam);
2691
2692         return True;
2693 }
2694
2695 /*********************************************************
2696  Set an extended attribute on a pathname.
2697 *********************************************************/
2698
2699 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2700 {
2701         uint16_t setup = TRANSACT2_SETPATHINFO;
2702         unsigned int param_len = 0;
2703         char *param;
2704         size_t srclen = 2*(strlen(path)+1);
2705         char *p;
2706         bool ret;
2707
2708         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2709         if (!param) {
2710                 return false;
2711         }
2712         memset(param, '\0', 6);
2713         SSVAL(param,0,SMB_INFO_SET_EA);
2714         p = &param[6];
2715
2716         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2717         param_len = PTR_DIFF(p, param);
2718
2719         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2720         SAFE_FREE(param);
2721         return ret;
2722 }
2723
2724 /*********************************************************
2725  Set an extended attribute on an fnum.
2726 *********************************************************/
2727
2728 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2729 {
2730         char param[6];
2731         uint16_t setup = TRANSACT2_SETFILEINFO;
2732
2733         memset(param, 0, 6);
2734         SSVAL(param,0,fnum);
2735         SSVAL(param,2,SMB_INFO_SET_EA);
2736
2737         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2738 }
2739
2740 /*********************************************************
2741  Get an extended attribute list utility fn.
2742 *********************************************************/
2743
2744 static bool cli_get_ea_list(struct cli_state *cli,
2745                 uint16_t setup, char *param, unsigned int param_len,
2746                 TALLOC_CTX *ctx,
2747                 size_t *pnum_eas,
2748                 struct ea_struct **pea_list)
2749 {
2750         unsigned int data_len = 0;
2751         unsigned int rparam_len, rdata_len;
2752         char *rparam=NULL, *rdata=NULL;
2753         char *p;
2754         size_t ea_size;
2755         size_t num_eas;
2756         bool ret = False;
2757         struct ea_struct *ea_list;
2758
2759         *pnum_eas = 0;
2760         if (pea_list) {
2761                 *pea_list = NULL;
2762         }
2763
2764         if (!cli_send_trans(cli, SMBtrans2,
2765                         NULL,           /* Name */
2766                         -1, 0,          /* fid, flags */
2767                         &setup, 1, 0,   /* setup, length, max */
2768                         param, param_len, 10, /* param, length, max */
2769                         NULL, data_len, cli->max_xmit /* data, length, max */
2770                                 )) {
2771                 return False;
2772         }
2773
2774         if (!cli_receive_trans(cli, SMBtrans2,
2775                         &rparam, &rparam_len,
2776                         &rdata, &rdata_len)) {
2777                 return False;
2778         }
2779
2780         if (!rdata || rdata_len < 4) {
2781                 goto out;
2782         }
2783
2784         ea_size = (size_t)IVAL(rdata,0);
2785         if (ea_size > rdata_len) {
2786                 goto out;
2787         }
2788
2789         if (ea_size == 0) {
2790                 /* No EA's present. */
2791                 ret = True;
2792                 goto out;
2793         }
2794
2795         p = rdata + 4;
2796         ea_size -= 4;
2797
2798         /* Validate the EA list and count it. */
2799         for (num_eas = 0; ea_size >= 4; num_eas++) {
2800                 unsigned int ea_namelen = CVAL(p,1);
2801                 unsigned int ea_valuelen = SVAL(p,2);
2802                 if (ea_namelen == 0) {
2803                         goto out;
2804                 }
2805                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2806                         goto out;
2807                 }
2808                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2809                 p += 4 + ea_namelen + 1 + ea_valuelen;
2810         }
2811
2812         if (num_eas == 0) {
2813                 ret = True;
2814                 goto out;
2815         }
2816
2817         *pnum_eas = num_eas;
2818         if (!pea_list) {
2819                 /* Caller only wants number of EA's. */
2820                 ret = True;
2821                 goto out;
2822         }
2823
2824         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2825         if (!ea_list) {
2826                 goto out;
2827         }
2828
2829         ea_size = (size_t)IVAL(rdata,0);
2830         p = rdata + 4;
2831
2832         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2833                 struct ea_struct *ea = &ea_list[num_eas];
2834                 fstring unix_ea_name;
2835                 unsigned int ea_namelen = CVAL(p,1);
2836                 unsigned int ea_valuelen = SVAL(p,2);
2837
2838                 ea->flags = CVAL(p,0);
2839                 unix_ea_name[0] = '\0';
2840                 pull_ascii_fstring(unix_ea_name, p + 4);
2841                 ea->name = talloc_strdup(ctx, unix_ea_name);
2842                 /* Ensure the value is null terminated (in case it's a string). */
2843                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2844                 if (!ea->value.data) {
2845                         goto out;
2846                 }
2847                 if (ea_valuelen) {
2848                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2849                 }
2850                 ea->value.data[ea_valuelen] = 0;
2851                 ea->value.length--;
2852                 p += 4 + ea_namelen + 1 + ea_valuelen;
2853         }
2854
2855         *pea_list = ea_list;
2856         ret = True;
2857
2858  out :
2859
2860         SAFE_FREE(rdata);
2861         SAFE_FREE(rparam);
2862         return ret;
2863 }
2864
2865 /*********************************************************
2866  Get an extended attribute list from a pathname.
2867 *********************************************************/
2868
2869 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2870                 TALLOC_CTX *ctx,
2871                 size_t *pnum_eas,
2872                 struct ea_struct **pea_list)
2873 {
2874         uint16_t setup = TRANSACT2_QPATHINFO;
2875         unsigned int param_len = 0;
2876         char *param;
2877         char *p;
2878         size_t srclen = 2*(strlen(path)+1);
2879         bool ret;
2880
2881         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2882         if (!param) {
2883                 return false;
2884         }
2885         p = param;
2886         memset(p, 0, 6);
2887         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2888         p += 6;
2889         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2890         param_len = PTR_DIFF(p, param);
2891
2892         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2893         SAFE_FREE(param);
2894         return ret;
2895 }
2896
2897 /*********************************************************
2898  Get an extended attribute list from an fnum.
2899 *********************************************************/
2900
2901 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2902                 TALLOC_CTX *ctx,
2903                 size_t *pnum_eas,
2904                 struct ea_struct **pea_list)
2905 {
2906         uint16_t setup = TRANSACT2_QFILEINFO;
2907         char param[6];
2908
2909         memset(param, 0, 6);
2910         SSVAL(param,0,fnum);
2911         SSVAL(param,2,SMB_INFO_SET_EA);
2912
2913         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2914 }
2915
2916 /****************************************************************************
2917  Convert open "flags" arg to uint32_t on wire.
2918 ****************************************************************************/
2919
2920 static uint32_t open_flags_to_wire(int flags)
2921 {
2922         int open_mode = flags & O_ACCMODE;
2923         uint32_t ret = 0;
2924
2925         switch (open_mode) {
2926                 case O_WRONLY:
2927                         ret |= SMB_O_WRONLY;
2928                         break;
2929                 case O_RDWR:
2930                         ret |= SMB_O_RDWR;
2931                         break;
2932                 default:
2933                 case O_RDONLY:
2934                         ret |= SMB_O_RDONLY;
2935                         break;
2936         }
2937
2938         if (flags & O_CREAT) {
2939                 ret |= SMB_O_CREAT;
2940         }
2941         if (flags & O_EXCL) {
2942                 ret |= SMB_O_EXCL;
2943         }
2944         if (flags & O_TRUNC) {
2945                 ret |= SMB_O_TRUNC;
2946         }
2947 #if defined(O_SYNC)
2948         if (flags & O_SYNC) {
2949                 ret |= SMB_O_SYNC;
2950         }
2951 #endif /* O_SYNC */
2952         if (flags & O_APPEND) {
2953                 ret |= SMB_O_APPEND;
2954         }
2955 #if defined(O_DIRECT)
2956         if (flags & O_DIRECT) {
2957                 ret |= SMB_O_DIRECT;
2958         }
2959 #endif
2960 #if defined(O_DIRECTORY)
2961         if (flags & O_DIRECTORY) {
2962                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2963                 ret |= SMB_O_DIRECTORY;
2964         }
2965 #endif
2966         return ret;
2967 }
2968
2969 /****************************************************************************
2970  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2971 ****************************************************************************/
2972
2973 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2974 {
2975         unsigned int data_len = 0;
2976         unsigned int param_len = 0;
2977         uint16_t setup = TRANSACT2_SETPATHINFO;
2978         char *param;
2979         char data[18];
2980         char *rparam=NULL, *rdata=NULL;
2981         char *p;
2982         int fnum = -1;
2983         uint32_t wire_flags = open_flags_to_wire(flags);
2984         size_t srclen = 2*(strlen(fname)+1);
2985
2986         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2987         if (!param) {
2988                 return false;
2989         }
2990         memset(param, '\0', 6);
2991         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2992         p = &param[6];
2993
2994         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2995         param_len = PTR_DIFF(p, param);
2996
2997         if (is_dir) {
2998                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2999                 wire_flags |= SMB_O_DIRECTORY;
3000         }
3001
3002         p = data;
3003         SIVAL(p,0,0); /* No oplock. */
3004         SIVAL(p,4,wire_flags);
3005         SIVAL(p,8,unix_perms_to_wire(mode));
3006         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
3007         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
3008
3009         data_len = 18;
3010
3011         if (!cli_send_trans(cli, SMBtrans2,
3012                         NULL,                        /* name */
3013                         -1, 0,                          /* fid, flags */
3014                         &setup, 1, 0,                   /* setup, length, max */
3015                         param, param_len, 2,            /* param, length, max */
3016                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
3017                         )) {
3018                 SAFE_FREE(param);
3019                 return -1;
3020         }
3021
3022         SAFE_FREE(param);
3023
3024         if (!cli_receive_trans(cli, SMBtrans2,
3025                 &rparam, &param_len,
3026                 &rdata, &data_len)) {
3027                         return -1;
3028         }
3029
3030         fnum = SVAL(rdata,2);
3031
3032         SAFE_FREE(rdata);
3033         SAFE_FREE(rparam);
3034
3035         return fnum;
3036 }
3037
3038 /****************************************************************************
3039  open - POSIX semantics.
3040 ****************************************************************************/
3041
3042 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
3043 {
3044         return cli_posix_open_internal(cli, fname, flags, mode, False);
3045 }
3046
3047 /****************************************************************************
3048  mkdir - POSIX semantics.
3049 ****************************************************************************/
3050
3051 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
3052 {
3053         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
3054 }
3055
3056 /****************************************************************************
3057  unlink or rmdir - POSIX semantics.
3058 ****************************************************************************/
3059
3060 struct unlink_state {
3061         int dummy;
3062 };
3063
3064 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3065 {
3066         struct tevent_req *req = tevent_req_callback_data(
3067                                 subreq, struct tevent_req);
3068         struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3069         NTSTATUS status;
3070
3071         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3072         TALLOC_FREE(subreq);
3073         if (!NT_STATUS_IS_OK(status)) {
3074                 tevent_req_nterror(req, status);
3075                 return;
3076         }
3077         tevent_req_done(req);
3078 }
3079
3080 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3081                                         struct event_context *ev,
3082                                         struct cli_state *cli,
3083                                         const char *fname,
3084                                         bool is_dir)
3085 {
3086         struct tevent_req *req = NULL, *subreq = NULL;
3087         struct unlink_state *state = NULL;
3088         uint16_t setup;
3089         uint8_t *param = NULL;
3090         uint8_t data[2];
3091
3092         req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3093         if (req == NULL) {
3094                 return NULL;
3095         }
3096
3097         /* Setup setup word. */
3098         SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
3099
3100         /* Setup param array. */
3101         param = talloc_array(state, uint8_t, 6);
3102         if (tevent_req_nomem(data, req)) {
3103                 return tevent_req_post(req, ev);
3104         }
3105         memset(param, '\0', 6);
3106         SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3107
3108         param = smb_bytes_push_str(param, cli_ucs2(cli), fname,
3109                                    strlen(fname)+1, NULL);
3110
3111         if (tevent_req_nomem(param, req)) {
3112                 return tevent_req_post(req, ev);
3113         }
3114
3115         /* Setup data word. */
3116         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3117                         SMB_POSIX_UNLINK_FILE_TARGET);
3118
3119         subreq = cli_trans_send(state,                  /* mem ctx. */
3120                                 ev,                     /* event ctx. */
3121                                 cli,                    /* cli_state. */
3122                                 SMBtrans2,              /* cmd. */
3123                                 NULL,                   /* pipe name. */
3124                                 -1,                     /* fid. */
3125                                 0,                      /* function. */
3126                                 0,                      /* flags. */
3127                                 &setup,                 /* setup. */
3128                                 2,                      /* num setup. */
3129                                 0,                      /* max setup. */
3130                                 param,                  /* param. */
3131                                 talloc_get_size(param), /* num param. */
3132                                 0,                      /* max param. */
3133                                 data,                   /* data. */
3134                                 2,                      /* num data. */
3135                                 0);                     /* max data. */
3136
3137         if (tevent_req_nomem(subreq, req)) {
3138                 return tevent_req_post(req, ev);
3139         }
3140         tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3141         return req;
3142 }
3143
3144 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3145                                         struct event_context *ev,
3146                                         struct cli_state *cli,
3147                                         const char *fname)
3148 {
3149         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3150 }
3151
3152 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3153 {
3154         NTSTATUS status;
3155
3156         if (tevent_req_is_nterror(req, &status)) {
3157                 return status;
3158         }
3159         return NT_STATUS_OK;
3160 }
3161
3162 /****************************************************************************
3163  unlink - POSIX semantics.
3164 ****************************************************************************/
3165
3166 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3167 {
3168         TALLOC_CTX *frame = talloc_stackframe();
3169         struct event_context *ev = NULL;
3170         struct tevent_req *req = NULL;
3171         NTSTATUS status = NT_STATUS_OK;
3172
3173         if (cli_has_async_calls(cli)) {
3174                 /*
3175                  * Can't use sync call while an async call is in flight
3176                  */
3177                 status = NT_STATUS_INVALID_PARAMETER;
3178                 goto fail;
3179         }
3180
3181         ev = event_context_init(frame);
3182         if (ev == NULL) {
3183                 status = NT_STATUS_NO_MEMORY;
3184                 goto fail;
3185         }
3186
3187         req = cli_posix_unlink_send(frame,
3188                                 ev,
3189                                 cli,
3190                                 fname);
3191         if (req == NULL) {
3192                 status = NT_STATUS_NO_MEMORY;
3193                 goto fail;
3194         }
3195
3196         if (!tevent_req_poll(req, ev)) {
3197                 status = map_nt_error_from_unix(errno);
3198                 goto fail;
3199         }
3200
3201         status = cli_posix_unlink_recv(req, frame);
3202
3203  fail:
3204         TALLOC_FREE(frame);
3205         if (!NT_STATUS_IS_OK(status)) {
3206                 cli_set_error(cli, status);
3207         }
3208         return status;
3209 }
3210
3211 /****************************************************************************
3212  rmdir - POSIX semantics.
3213 ****************************************************************************/
3214
3215 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3216                                         struct event_context *ev,
3217                                         struct cli_state *cli,
3218                                         const char *fname)
3219 {
3220         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3221 }
3222
3223 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3224 {
3225         NTSTATUS status;
3226
3227         if (tevent_req_is_nterror(req, &status)) {
3228                 return status;
3229         }
3230         return NT_STATUS_OK;
3231 }
3232
3233 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3234 {
3235         TALLOC_CTX *frame = talloc_stackframe();
3236         struct event_context *ev = NULL;
3237         struct tevent_req *req = NULL;
3238         NTSTATUS status = NT_STATUS_OK;
3239
3240         if (cli_has_async_calls(cli)) {
3241                 /*
3242                  * Can't use sync call while an async call is in flight
3243                  */
3244                 status = NT_STATUS_INVALID_PARAMETER;
3245                 goto fail;
3246         }
3247
3248         ev = event_context_init(frame);
3249         if (ev == NULL) {
3250                 status = NT_STATUS_NO_MEMORY;
3251                 goto fail;
3252         }
3253
3254         req = cli_posix_rmdir_send(frame,
3255                                 ev,
3256                                 cli,
3257                                 fname);
3258         if (req == NULL) {
3259                 status = NT_STATUS_NO_MEMORY;
3260                 goto fail;
3261         }
3262
3263         if (!tevent_req_poll(req, ev)) {
3264                 status = map_nt_error_from_unix(errno);
3265                 goto fail;
3266         }
3267
3268         status = cli_posix_rmdir_recv(req, frame);
3269
3270  fail:
3271         TALLOC_FREE(frame);
3272         if (!NT_STATUS_IS_OK(status)) {
3273                 cli_set_error(cli, status);
3274         }
3275         return status;
3276 }