r8184: fix build issue on Solaris in smbclient
[nivanova/samba-autobuild/.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27  Hard/Symlink a file (UNIX extensions).
28  Creates new name (sym)linked to oldname.
29 ****************************************************************************/
30
31 static BOOL cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, BOOL hard_link)
32 {
33         unsigned int data_len = 0;
34         unsigned int param_len = 0;
35         uint16 setup = TRANSACT2_SETPATHINFO;
36         char param[sizeof(pstring)+6];
37         pstring data;
38         char *rparam=NULL, *rdata=NULL;
39         char *p;
40         size_t oldlen = 2*(strlen(oldname)+1);
41         size_t newlen = 2*(strlen(newname)+1);
42
43         memset(param, 0, sizeof(param));
44         SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
45         p = &param[6];
46
47         p += clistr_push(cli, p, newname, MIN(newlen, sizeof(param)-6), STR_TERMINATE);
48         param_len = PTR_DIFF(p, param);
49
50         p = data;
51         p += clistr_push(cli, p, oldname, MIN(oldlen,sizeof(data)), STR_TERMINATE);
52         data_len = PTR_DIFF(p, data);
53
54         if (!cli_send_trans(cli, SMBtrans2,
55                 NULL,                        /* name */
56                 -1, 0,                          /* fid, flags */
57                 &setup, 1, 0,                   /* setup, length, max */
58                 param, param_len, 2,            /* param, length, max */
59                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
60                 )) {
61                         return False;
62         }
63
64         if (!cli_receive_trans(cli, SMBtrans2,
65                 &rparam, &param_len,
66                 &rdata, &data_len)) {
67                         return False;
68         }
69
70         SAFE_FREE(rdata);
71         SAFE_FREE(rparam);
72
73         return True;
74 }
75
76 /****************************************************************************
77  Map standard UNIX permissions onto wire representations.
78 ****************************************************************************/
79
80 uint32 unix_perms_to_wire(mode_t perms)
81 {
82         unsigned int ret = 0;
83
84         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
85         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
86         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
87         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
88         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
89         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
90         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
91         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
92         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
93 #ifdef S_ISVTX
94         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
95 #endif
96 #ifdef S_ISGID
97         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
98 #endif
99 #ifdef S_ISUID
100         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
101 #endif
102         return ret;
103 }
104
105 /****************************************************************************
106  Map wire permissions to standard UNIX.
107 ****************************************************************************/
108
109 mode_t wire_perms_to_unix(uint32 perms)
110 {
111         mode_t ret = (mode_t)0;
112
113         ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
114         ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
115         ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
116         ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
117         ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
118         ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
119         ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
120         ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
121         ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
122 #ifdef S_ISVTX
123         ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
124 #endif
125 #ifdef S_ISGID
126         ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
127 #endif
128 #ifdef S_ISUID
129         ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
130 #endif
131         return ret;
132 }
133
134 /****************************************************************************
135  Return the file type from the wire filetype for UNIX extensions.
136 ****************************************************************************/
137                                                                                                                 
138 static mode_t unix_filetype_from_wire(uint32 wire_type)
139 {
140         switch (wire_type) {
141                 case UNIX_TYPE_FILE:
142                         return S_IFREG;
143                 case UNIX_TYPE_DIR:
144                         return S_IFDIR;
145 #ifdef S_IFLNK
146                 case UNIX_TYPE_SYMLINK:
147                         return S_IFLNK;
148 #endif
149 #ifdef S_IFCHR
150                 case UNIX_TYPE_CHARDEV:
151                         return S_IFCHR;
152 #endif
153 #ifdef S_IFBLK
154                 case UNIX_TYPE_BLKDEV:
155                         return S_IFBLK;
156 #endif
157 #ifdef S_IFIFO
158                 case UNIX_TYPE_FIFO:
159                         return S_IFIFO;
160 #endif
161 #ifdef S_IFSOCK
162                 case UNIX_TYPE_SOCKET:
163                         return S_IFSOCK;
164 #endif
165                 default:
166                         return (mode_t)0;
167         }
168 }
169
170 /****************************************************************************
171  Do a POSIX getfacl (UNIX extensions).
172 ****************************************************************************/
173
174 BOOL cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
175 {
176         unsigned int param_len = 0;
177         unsigned int data_len = 0;
178         uint16 setup = TRANSACT2_QPATHINFO;
179         char param[sizeof(pstring)+6];
180         char *rparam=NULL, *rdata=NULL;
181         char *p;
182
183         p = param;
184         memset(p, 0, 6);
185         SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
186         p += 6;
187         p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
188         param_len = PTR_DIFF(p, param);
189
190         if (!cli_send_trans(cli, SMBtrans2,
191                 NULL,                        /* name */
192                 -1, 0,                       /* fid, flags */
193                 &setup, 1, 0,                /* setup, length, max */
194                 param, param_len, 2,         /* param, length, max */
195                 NULL,  0, cli->max_xmit      /* data, length, max */
196                 )) {
197                         return False;
198         }
199
200         if (!cli_receive_trans(cli, SMBtrans2,
201                 &rparam, &param_len,
202                 &rdata, &data_len)) {
203                         return False;
204         }
205
206         if (data_len < 6) {
207                 SAFE_FREE(rdata);
208                 SAFE_FREE(rparam);
209                 return False;
210         }
211
212         SAFE_FREE(rparam);
213         *retbuf = rdata;
214         *prb_size = (size_t)data_len;
215
216         return True;
217 }
218
219 /****************************************************************************
220  Stat a file (UNIX extensions).
221 ****************************************************************************/
222
223 BOOL cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
224 {
225         unsigned int param_len = 0;
226         unsigned int data_len = 0;
227         uint16 setup = TRANSACT2_QPATHINFO;
228         char param[sizeof(pstring)+6];
229         char *rparam=NULL, *rdata=NULL;
230         char *p;
231
232         ZERO_STRUCTP(sbuf);
233
234         p = param;
235         memset(p, 0, 6);
236         SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
237         p += 6;
238         p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
239         param_len = PTR_DIFF(p, param);
240
241         if (!cli_send_trans(cli, SMBtrans2,
242                 NULL,                        /* name */
243                 -1, 0,                       /* fid, flags */
244                 &setup, 1, 0,                /* setup, length, max */
245                 param, param_len, 2,         /* param, length, max */
246                 NULL,  0, cli->max_xmit      /* data, length, max */
247                 )) {
248                         return False;
249         }
250
251         if (!cli_receive_trans(cli, SMBtrans2,
252                 &rparam, &param_len,
253                 &rdata, &data_len)) {
254                         return False;
255         }
256
257         if (data_len < 96) {
258                 SAFE_FREE(rdata);
259                 SAFE_FREE(rparam);
260                 return False;
261         }
262
263         sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
264         sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
265 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
266         sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
267 #else
268         /* assume 512 byte blocks */
269         sbuf->st_blocks /= 512;
270 #endif
271         sbuf->st_ctime = interpret_long_date(rdata + 16);    /* time of last change */
272         sbuf->st_atime = interpret_long_date(rdata + 24);    /* time of last access */
273         sbuf->st_mtime = interpret_long_date(rdata + 32);    /* time of last modification */
274         sbuf->st_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
275         sbuf->st_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
276         sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
277 #if defined(HAVE_MAKEDEV)
278         {
279                 uint32 dev_major = IVAL(rdata,60);
280                 uint32 dev_minor = IVAL(rdata,68);
281                 sbuf->st_rdev = makedev(dev_major, dev_minor);
282         }
283 #endif
284         sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
285         sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
286         sbuf->st_nlink = IVAL(rdata,92);    /* number of hard links */
287
288         SAFE_FREE(rdata);
289         SAFE_FREE(rparam);
290
291         return True;
292 }
293
294 /****************************************************************************
295  Symlink a file (UNIX extensions).
296 ****************************************************************************/
297
298 BOOL cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
299 {
300         return cli_link_internal(cli, oldname, newname, False);
301 }
302
303 /****************************************************************************
304  Hard a file (UNIX extensions).
305 ****************************************************************************/
306
307 BOOL cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
308 {
309         return cli_link_internal(cli, oldname, newname, True);
310 }
311
312 /****************************************************************************
313  Chmod or chown a file internal (UNIX extensions).
314 ****************************************************************************/
315
316 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
317 {
318         unsigned int data_len = 0;
319         unsigned int param_len = 0;
320         uint16 setup = TRANSACT2_SETPATHINFO;
321         char param[sizeof(pstring)+6];
322         char data[100];
323         char *rparam=NULL, *rdata=NULL;
324         char *p;
325
326         memset(param, 0, sizeof(param));
327         memset(data, 0, sizeof(data));
328         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
329         p = &param[6];
330
331         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
332         param_len = PTR_DIFF(p, param);
333
334         SIVAL(data,40,uid);
335         SIVAL(data,48,gid);
336         SIVAL(data,84,mode);
337
338         data_len = 100;
339
340         if (!cli_send_trans(cli, SMBtrans2,
341                 NULL,                        /* name */
342                 -1, 0,                          /* fid, flags */
343                 &setup, 1, 0,                   /* setup, length, max */
344                 param, param_len, 2,            /* param, length, max */
345                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
346                 )) {
347                         return False;
348         }
349
350         if (!cli_receive_trans(cli, SMBtrans2,
351                 &rparam, &param_len,
352                 &rdata, &data_len)) {
353                         return False;
354         }
355
356         SAFE_FREE(rdata);
357         SAFE_FREE(rparam);
358
359         return True;
360 }
361
362 /****************************************************************************
363  chmod a file (UNIX extensions).
364 ****************************************************************************/
365
366 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
367 {
368         return cli_unix_chmod_chown_internal(cli, fname, 
369                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
370 }
371
372 /****************************************************************************
373  chown a file (UNIX extensions).
374 ****************************************************************************/
375
376 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
377 {
378         return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
379 }
380
381 /****************************************************************************
382  Rename a file.
383 ****************************************************************************/
384
385 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
386 {
387         char *p;
388
389         memset(cli->outbuf,'\0',smb_size);
390         memset(cli->inbuf,'\0',smb_size);
391
392         set_message(cli->outbuf,1, 0, True);
393
394         SCVAL(cli->outbuf,smb_com,SMBmv);
395         SSVAL(cli->outbuf,smb_tid,cli->cnum);
396         cli_setup_packet(cli);
397
398         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
399
400         p = smb_buf(cli->outbuf);
401         *p++ = 4;
402         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
403         *p++ = 4;
404         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
405
406         cli_setup_bcc(cli, p);
407
408         cli_send_smb(cli);
409         if (!cli_receive_smb(cli))
410                 return False;
411
412         if (cli_is_error(cli))
413                 return False;
414
415         return True;
416 }
417
418 /****************************************************************************
419  NT Rename a file.
420 ****************************************************************************/
421
422 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
423 {
424         char *p;
425
426         memset(cli->outbuf,'\0',smb_size);
427         memset(cli->inbuf,'\0',smb_size);
428
429         set_message(cli->outbuf, 4, 0, True);
430
431         SCVAL(cli->outbuf,smb_com,SMBntrename);
432         SSVAL(cli->outbuf,smb_tid,cli->cnum);
433         cli_setup_packet(cli);
434
435         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
436         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
437
438         p = smb_buf(cli->outbuf);
439         *p++ = 4;
440         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
441         *p++ = 4;
442         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
443
444         cli_setup_bcc(cli, p);
445
446         cli_send_smb(cli);
447         if (!cli_receive_smb(cli))
448                 return False;
449
450         if (cli_is_error(cli))
451                 return False;
452
453         return True;
454 }
455
456 /****************************************************************************
457  NT hardlink a file.
458 ****************************************************************************/
459
460 BOOL cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
461 {
462         char *p;
463
464         memset(cli->outbuf,'\0',smb_size);
465         memset(cli->inbuf,'\0',smb_size);
466
467         set_message(cli->outbuf, 4, 0, True);
468
469         SCVAL(cli->outbuf,smb_com,SMBntrename);
470         SSVAL(cli->outbuf,smb_tid,cli->cnum);
471         cli_setup_packet(cli);
472
473         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
474         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
475
476         p = smb_buf(cli->outbuf);
477         *p++ = 4;
478         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
479         *p++ = 4;
480         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
481
482         cli_setup_bcc(cli, p);
483
484         cli_send_smb(cli);
485         if (!cli_receive_smb(cli))
486                 return False;
487
488         if (cli_is_error(cli))
489                 return False;
490
491         return True;
492 }
493
494 /****************************************************************************
495  Delete a file.
496 ****************************************************************************/
497
498 BOOL cli_unlink(struct cli_state *cli, const char *fname)
499 {
500         char *p;
501
502         memset(cli->outbuf,'\0',smb_size);
503         memset(cli->inbuf,'\0',smb_size);
504
505         set_message(cli->outbuf,1, 0,True);
506
507         SCVAL(cli->outbuf,smb_com,SMBunlink);
508         SSVAL(cli->outbuf,smb_tid,cli->cnum);
509         cli_setup_packet(cli);
510
511         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
512   
513         p = smb_buf(cli->outbuf);
514         *p++ = 4;      
515         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
516
517         cli_setup_bcc(cli, p);
518         cli_send_smb(cli);
519         if (!cli_receive_smb(cli)) {
520                 return False;
521         }
522
523         if (cli_is_error(cli)) {
524                 return False;
525         }
526
527         return True;
528 }
529
530 /****************************************************************************
531  Create a directory.
532 ****************************************************************************/
533
534 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
535 {
536         char *p;
537
538         memset(cli->outbuf,'\0',smb_size);
539         memset(cli->inbuf,'\0',smb_size);
540
541         set_message(cli->outbuf,0, 0,True);
542
543         SCVAL(cli->outbuf,smb_com,SMBmkdir);
544         SSVAL(cli->outbuf,smb_tid,cli->cnum);
545         cli_setup_packet(cli);
546
547         p = smb_buf(cli->outbuf);
548         *p++ = 4;      
549         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
550
551         cli_setup_bcc(cli, p);
552
553         cli_send_smb(cli);
554         if (!cli_receive_smb(cli)) {
555                 return False;
556         }
557
558         if (cli_is_error(cli)) {
559                 return False;
560         }
561
562         return True;
563 }
564
565 /****************************************************************************
566  Remove a directory.
567 ****************************************************************************/
568
569 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
570 {
571         char *p;
572
573         memset(cli->outbuf,'\0',smb_size);
574         memset(cli->inbuf,'\0',smb_size);
575
576         set_message(cli->outbuf,0, 0, True);
577
578         SCVAL(cli->outbuf,smb_com,SMBrmdir);
579         SSVAL(cli->outbuf,smb_tid,cli->cnum);
580         cli_setup_packet(cli);
581
582         p = smb_buf(cli->outbuf);
583         *p++ = 4;      
584         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
585
586         cli_setup_bcc(cli, p);
587
588         cli_send_smb(cli);
589         if (!cli_receive_smb(cli)) {
590                 return False;
591         }
592
593         if (cli_is_error(cli)) {
594                 return False;
595         }
596
597         return True;
598 }
599
600 /****************************************************************************
601  Set or clear the delete on close flag.
602 ****************************************************************************/
603
604 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
605 {
606         unsigned int data_len = 1;
607         unsigned int param_len = 6;
608         uint16 setup = TRANSACT2_SETFILEINFO;
609         pstring param;
610         unsigned char data;
611         char *rparam=NULL, *rdata=NULL;
612
613         memset(param, 0, param_len);
614         SSVAL(param,0,fnum);
615         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
616
617         data = flag ? 1 : 0;
618
619         if (!cli_send_trans(cli, SMBtrans2,
620                                                 NULL,                        /* name */
621                                                 -1, 0,                          /* fid, flags */
622                                                 &setup, 1, 0,                   /* setup, length, max */
623                                                 param, param_len, 2,            /* param, length, max */
624                                                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
625                                                 )) {
626                 return False;
627         }
628
629         if (!cli_receive_trans(cli, SMBtrans2,
630                                                 &rparam, &param_len,
631                                                 &rdata, &data_len)) {
632                 return False;
633         }
634
635         SAFE_FREE(rdata);
636         SAFE_FREE(rparam);
637
638         return True;
639 }
640
641 /****************************************************************************
642  Open a file - exposing the full horror of the NT API :-).
643  Used in smbtorture.
644 ****************************************************************************/
645
646 int cli_nt_create_full(struct cli_state *cli, const char *fname, 
647                  uint32 CreatFlags, uint32 DesiredAccess,
648                  uint32 FileAttributes, uint32 ShareAccess,
649                  uint32 CreateDisposition, uint32 CreateOptions,
650                  uint8 SecuityFlags)
651 {
652         char *p;
653         int len;
654
655         memset(cli->outbuf,'\0',smb_size);
656         memset(cli->inbuf,'\0',smb_size);
657
658         set_message(cli->outbuf,24,0,True);
659
660         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
661         SSVAL(cli->outbuf,smb_tid,cli->cnum);
662         cli_setup_packet(cli);
663
664         SSVAL(cli->outbuf,smb_vwv0,0xFF);
665         if (cli->use_oplocks)
666                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
667         
668         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
669         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
670         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
671         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
672         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
673         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
674         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
675         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
676         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
677
678         p = smb_buf(cli->outbuf);
679         /* this alignment and termination is critical for netapp filers. Don't change */
680         p += clistr_align_out(cli, p, 0);
681         len = clistr_push(cli, p, fname, -1, 0);
682         p += len;
683         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
684         /* sigh. this copes with broken netapp filer behaviour */
685         p += clistr_push(cli, p, "", -1, STR_TERMINATE);
686
687         cli_setup_bcc(cli, p);
688
689         cli_send_smb(cli);
690         if (!cli_receive_smb(cli)) {
691                 return -1;
692         }
693
694         if (cli_is_error(cli)) {
695                 return -1;
696         }
697
698         return SVAL(cli->inbuf,smb_vwv2 + 1);
699 }
700
701 /****************************************************************************
702  Open a file.
703 ****************************************************************************/
704
705 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
706 {
707         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
708                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
709 }
710
711 /****************************************************************************
712  Open a file
713  WARNING: if you open with O_WRONLY then getattrE won't work!
714 ****************************************************************************/
715
716 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
717 {
718         char *p;
719         unsigned openfn=0;
720         unsigned accessmode=0;
721
722         if (flags & O_CREAT)
723                 openfn |= (1<<4);
724         if (!(flags & O_EXCL)) {
725                 if (flags & O_TRUNC)
726                         openfn |= (1<<1);
727                 else
728                         openfn |= (1<<0);
729         }
730
731         accessmode = (share_mode<<4);
732
733         if ((flags & O_ACCMODE) == O_RDWR) {
734                 accessmode |= 2;
735         } else if ((flags & O_ACCMODE) == O_WRONLY) {
736                 accessmode |= 1;
737         } 
738
739 #if defined(O_SYNC)
740         if ((flags & O_SYNC) == O_SYNC) {
741                 accessmode |= (1<<14);
742         }
743 #endif /* O_SYNC */
744
745         if (share_mode == DENY_FCB) {
746                 accessmode = 0xFF;
747         }
748
749         memset(cli->outbuf,'\0',smb_size);
750         memset(cli->inbuf,'\0',smb_size);
751
752         set_message(cli->outbuf,15,0,True);
753
754         SCVAL(cli->outbuf,smb_com,SMBopenX);
755         SSVAL(cli->outbuf,smb_tid,cli->cnum);
756         cli_setup_packet(cli);
757
758         SSVAL(cli->outbuf,smb_vwv0,0xFF);
759         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
760         SSVAL(cli->outbuf,smb_vwv3,accessmode);
761         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
762         SSVAL(cli->outbuf,smb_vwv5,0);
763         SSVAL(cli->outbuf,smb_vwv8,openfn);
764
765         if (cli->use_oplocks) {
766                 /* if using oplocks then ask for a batch oplock via
767                    core and extended methods */
768                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
769                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
770                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
771         }
772   
773         p = smb_buf(cli->outbuf);
774         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
775
776         cli_setup_bcc(cli, p);
777
778         cli_send_smb(cli);
779         if (!cli_receive_smb(cli)) {
780                 return -1;
781         }
782
783         if (cli_is_error(cli)) {
784                 return -1;
785         }
786
787         return SVAL(cli->inbuf,smb_vwv2);
788 }
789
790 /****************************************************************************
791  Close a file.
792 ****************************************************************************/
793
794 BOOL cli_close(struct cli_state *cli, int fnum)
795 {
796         memset(cli->outbuf,'\0',smb_size);
797         memset(cli->inbuf,'\0',smb_size);
798
799         set_message(cli->outbuf,3,0,True);
800
801         SCVAL(cli->outbuf,smb_com,SMBclose);
802         SSVAL(cli->outbuf,smb_tid,cli->cnum);
803         cli_setup_packet(cli);
804
805         SSVAL(cli->outbuf,smb_vwv0,fnum);
806         SIVALS(cli->outbuf,smb_vwv1,-1);
807
808         cli_send_smb(cli);
809         if (!cli_receive_smb(cli)) {
810                 return False;
811         }
812
813         return !cli_is_error(cli);
814 }
815
816
817 /****************************************************************************
818  send a lock with a specified locktype 
819  this is used for testing LOCKING_ANDX_CANCEL_LOCK
820 ****************************************************************************/
821 NTSTATUS cli_locktype(struct cli_state *cli, int fnum, 
822                       uint32 offset, uint32 len, int timeout, unsigned char locktype)
823 {
824         char *p;
825         int saved_timeout = cli->timeout;
826
827         memset(cli->outbuf,'\0',smb_size);
828         memset(cli->inbuf,'\0', smb_size);
829
830         set_message(cli->outbuf,8,0,True);
831
832         SCVAL(cli->outbuf,smb_com,SMBlockingX);
833         SSVAL(cli->outbuf,smb_tid,cli->cnum);
834         cli_setup_packet(cli);
835
836         SCVAL(cli->outbuf,smb_vwv0,0xFF);
837         SSVAL(cli->outbuf,smb_vwv2,fnum);
838         SCVAL(cli->outbuf,smb_vwv3,locktype);
839         SIVALS(cli->outbuf, smb_vwv4, timeout);
840         SSVAL(cli->outbuf,smb_vwv6,0);
841         SSVAL(cli->outbuf,smb_vwv7,1);
842
843         p = smb_buf(cli->outbuf);
844         SSVAL(p, 0, cli->pid);
845         SIVAL(p, 2, offset);
846         SIVAL(p, 6, len);
847
848         p += 10;
849
850         cli_setup_bcc(cli, p);
851
852         cli_send_smb(cli);
853
854         if (timeout != 0) {
855                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
856         }
857
858         if (!cli_receive_smb(cli)) {
859                 cli->timeout = saved_timeout;
860                 return NT_STATUS_UNSUCCESSFUL;
861         }
862
863         cli->timeout = saved_timeout;
864
865         return cli_nt_error(cli);
866 }
867
868
869 /****************************************************************************
870  Lock a file.
871  note that timeout is in units of 2 milliseconds
872 ****************************************************************************/
873 BOOL cli_lock(struct cli_state *cli, int fnum, 
874               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
875 {
876         char *p;
877         int saved_timeout = cli->timeout;
878
879         memset(cli->outbuf,'\0',smb_size);
880         memset(cli->inbuf,'\0', smb_size);
881
882         set_message(cli->outbuf,8,0,True);
883
884         SCVAL(cli->outbuf,smb_com,SMBlockingX);
885         SSVAL(cli->outbuf,smb_tid,cli->cnum);
886         cli_setup_packet(cli);
887
888         SCVAL(cli->outbuf,smb_vwv0,0xFF);
889         SSVAL(cli->outbuf,smb_vwv2,fnum);
890         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
891         SIVALS(cli->outbuf, smb_vwv4, timeout);
892         SSVAL(cli->outbuf,smb_vwv6,0);
893         SSVAL(cli->outbuf,smb_vwv7,1);
894
895         p = smb_buf(cli->outbuf);
896         SSVAL(p, 0, cli->pid);
897         SIVAL(p, 2, offset);
898         SIVAL(p, 6, len);
899
900         p += 10;
901
902         cli_setup_bcc(cli, p);
903
904         cli_send_smb(cli);
905
906         if (timeout != 0) {
907                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
908         }
909
910         if (!cli_receive_smb(cli)) {
911                 cli->timeout = saved_timeout;
912                 return False;
913         }
914
915         cli->timeout = saved_timeout;
916
917         if (cli_is_error(cli)) {
918                 return False;
919         }
920
921         return True;
922 }
923
924 /****************************************************************************
925  Unlock a file.
926 ****************************************************************************/
927
928 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
929 {
930         char *p;
931
932         memset(cli->outbuf,'\0',smb_size);
933         memset(cli->inbuf,'\0',smb_size);
934
935         set_message(cli->outbuf,8,0,True);
936
937         SCVAL(cli->outbuf,smb_com,SMBlockingX);
938         SSVAL(cli->outbuf,smb_tid,cli->cnum);
939         cli_setup_packet(cli);
940
941         SCVAL(cli->outbuf,smb_vwv0,0xFF);
942         SSVAL(cli->outbuf,smb_vwv2,fnum);
943         SCVAL(cli->outbuf,smb_vwv3,0);
944         SIVALS(cli->outbuf, smb_vwv4, 0);
945         SSVAL(cli->outbuf,smb_vwv6,1);
946         SSVAL(cli->outbuf,smb_vwv7,0);
947
948         p = smb_buf(cli->outbuf);
949         SSVAL(p, 0, cli->pid);
950         SIVAL(p, 2, offset);
951         SIVAL(p, 6, len);
952         p += 10;
953         cli_setup_bcc(cli, p);
954         cli_send_smb(cli);
955         if (!cli_receive_smb(cli)) {
956                 return False;
957         }
958
959         if (cli_is_error(cli)) {
960                 return False;
961         }
962
963         return True;
964 }
965
966 /****************************************************************************
967  Lock a file with 64 bit offsets.
968 ****************************************************************************/
969
970 BOOL cli_lock64(struct cli_state *cli, int fnum, 
971                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
972 {
973         char *p;
974         int saved_timeout = cli->timeout;
975         int ltype;
976
977         if (! (cli->capabilities & CAP_LARGE_FILES)) {
978                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
979         }
980
981         ltype = (lock_type == READ_LOCK? 1 : 0);
982         ltype |= LOCKING_ANDX_LARGE_FILES;
983
984         memset(cli->outbuf,'\0',smb_size);
985         memset(cli->inbuf,'\0', smb_size);
986
987         set_message(cli->outbuf,8,0,True);
988
989         SCVAL(cli->outbuf,smb_com,SMBlockingX);
990         SSVAL(cli->outbuf,smb_tid,cli->cnum);
991         cli_setup_packet(cli);
992
993         SCVAL(cli->outbuf,smb_vwv0,0xFF);
994         SSVAL(cli->outbuf,smb_vwv2,fnum);
995         SCVAL(cli->outbuf,smb_vwv3,ltype);
996         SIVALS(cli->outbuf, smb_vwv4, timeout);
997         SSVAL(cli->outbuf,smb_vwv6,0);
998         SSVAL(cli->outbuf,smb_vwv7,1);
999
1000         p = smb_buf(cli->outbuf);
1001         SIVAL(p, 0, cli->pid);
1002         SOFF_T_R(p, 4, offset);
1003         SOFF_T_R(p, 12, len);
1004         p += 20;
1005
1006         cli_setup_bcc(cli, p);
1007         cli_send_smb(cli);
1008
1009         if (timeout != 0) {
1010                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1011         }
1012
1013         if (!cli_receive_smb(cli)) {
1014                 cli->timeout = saved_timeout;
1015                 return False;
1016         }
1017
1018         cli->timeout = saved_timeout;
1019
1020         if (cli_is_error(cli)) {
1021                 return False;
1022         }
1023
1024         return True;
1025 }
1026
1027 /****************************************************************************
1028  Unlock a file with 64 bit offsets.
1029 ****************************************************************************/
1030
1031 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1032 {
1033         char *p;
1034
1035         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1036                 return cli_unlock(cli, fnum, offset, len);
1037         }
1038
1039         memset(cli->outbuf,'\0',smb_size);
1040         memset(cli->inbuf,'\0',smb_size);
1041
1042         set_message(cli->outbuf,8,0,True);
1043
1044         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1045         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1046         cli_setup_packet(cli);
1047
1048         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1049         SSVAL(cli->outbuf,smb_vwv2,fnum);
1050         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1051         SIVALS(cli->outbuf, smb_vwv4, 0);
1052         SSVAL(cli->outbuf,smb_vwv6,1);
1053         SSVAL(cli->outbuf,smb_vwv7,0);
1054
1055         p = smb_buf(cli->outbuf);
1056         SIVAL(p, 0, cli->pid);
1057         SOFF_T_R(p, 4, offset);
1058         SOFF_T_R(p, 12, len);
1059         p += 20;
1060         cli_setup_bcc(cli, p);
1061         cli_send_smb(cli);
1062         if (!cli_receive_smb(cli)) {
1063                 return False;
1064         }
1065
1066         if (cli_is_error(cli)) {
1067                 return False;
1068         }
1069
1070         return True;
1071 }
1072
1073
1074 /****************************************************************************
1075  Do a SMBgetattrE call.
1076 ****************************************************************************/
1077
1078 BOOL cli_getattrE(struct cli_state *cli, int fd, 
1079                   uint16 *attr, SMB_OFF_T *size, 
1080                   time_t *c_time, time_t *a_time, time_t *m_time)
1081 {
1082         memset(cli->outbuf,'\0',smb_size);
1083         memset(cli->inbuf,'\0',smb_size);
1084
1085         set_message(cli->outbuf,1,0,True);
1086
1087         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1088         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1089         cli_setup_packet(cli);
1090
1091         SSVAL(cli->outbuf,smb_vwv0,fd);
1092
1093         cli_send_smb(cli);
1094         if (!cli_receive_smb(cli)) {
1095                 return False;
1096         }
1097         
1098         if (cli_is_error(cli)) {
1099                 return False;
1100         }
1101
1102         if (size) {
1103                 *size = IVAL(cli->inbuf, smb_vwv6);
1104         }
1105
1106         if (attr) {
1107                 *attr = SVAL(cli->inbuf,smb_vwv10);
1108         }
1109
1110         if (c_time) {
1111                 *c_time = make_unix_date2(cli->inbuf+smb_vwv0);
1112         }
1113
1114         if (a_time) {
1115                 *a_time = make_unix_date2(cli->inbuf+smb_vwv2);
1116         }
1117
1118         if (m_time) {
1119                 *m_time = make_unix_date2(cli->inbuf+smb_vwv4);
1120         }
1121
1122         return True;
1123 }
1124
1125 /****************************************************************************
1126  Do a SMBgetatr call
1127 ****************************************************************************/
1128
1129 BOOL cli_getatr(struct cli_state *cli, const char *fname, 
1130                 uint16 *attr, SMB_OFF_T *size, time_t *t)
1131 {
1132         char *p;
1133
1134         memset(cli->outbuf,'\0',smb_size);
1135         memset(cli->inbuf,'\0',smb_size);
1136
1137         set_message(cli->outbuf,0,0,True);
1138
1139         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1140         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1141         cli_setup_packet(cli);
1142
1143         p = smb_buf(cli->outbuf);
1144         *p++ = 4;
1145         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1146
1147         cli_setup_bcc(cli, p);
1148
1149         cli_send_smb(cli);
1150         if (!cli_receive_smb(cli)) {
1151                 return False;
1152         }
1153         
1154         if (cli_is_error(cli)) {
1155                 return False;
1156         }
1157
1158         if (size) {
1159                 *size = IVAL(cli->inbuf, smb_vwv3);
1160         }
1161
1162         if (t) {
1163                 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1164         }
1165
1166         if (attr) {
1167                 *attr = SVAL(cli->inbuf,smb_vwv0);
1168         }
1169
1170
1171         return True;
1172 }
1173
1174 /****************************************************************************
1175  Do a SMBsetattrE call.
1176 ****************************************************************************/
1177
1178 BOOL cli_setattrE(struct cli_state *cli, int fd,
1179                   time_t c_time, time_t a_time, time_t m_time)
1180
1181 {
1182         char *p;
1183
1184         memset(cli->outbuf,'\0',smb_size);
1185         memset(cli->inbuf,'\0',smb_size);
1186
1187         set_message(cli->outbuf,7,0,True);
1188
1189         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1190         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1191         cli_setup_packet(cli);
1192
1193         SSVAL(cli->outbuf,smb_vwv0, fd);
1194         put_dos_date2(cli->outbuf,smb_vwv1, c_time);
1195         put_dos_date2(cli->outbuf,smb_vwv3, a_time);
1196         put_dos_date2(cli->outbuf,smb_vwv5, m_time);
1197
1198         p = smb_buf(cli->outbuf);
1199         *p++ = 4;
1200
1201         cli_setup_bcc(cli, p);
1202
1203         cli_send_smb(cli);
1204         if (!cli_receive_smb(cli)) {
1205                 return False;
1206         }
1207         
1208         if (cli_is_error(cli)) {
1209                 return False;
1210         }
1211
1212         return True;
1213 }
1214
1215 /****************************************************************************
1216  Do a SMBsetatr call.
1217 ****************************************************************************/
1218
1219 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1220 {
1221         char *p;
1222
1223         memset(cli->outbuf,'\0',smb_size);
1224         memset(cli->inbuf,'\0',smb_size);
1225
1226         set_message(cli->outbuf,8,0,True);
1227
1228         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1229         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1230         cli_setup_packet(cli);
1231
1232         SSVAL(cli->outbuf,smb_vwv0, attr);
1233         put_dos_date3(cli->outbuf,smb_vwv1, t);
1234
1235         p = smb_buf(cli->outbuf);
1236         *p++ = 4;
1237         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1238         *p++ = 4;
1239
1240         cli_setup_bcc(cli, p);
1241
1242         cli_send_smb(cli);
1243         if (!cli_receive_smb(cli)) {
1244                 return False;
1245         }
1246         
1247         if (cli_is_error(cli)) {
1248                 return False;
1249         }
1250
1251         return True;
1252 }
1253
1254 /****************************************************************************
1255  Check for existance of a dir.
1256 ****************************************************************************/
1257 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1258 {
1259         pstring path2;
1260         char *p;
1261         
1262         pstrcpy(path2,path);
1263         trim_char(path2,'\0','\\');
1264         if (!*path2)
1265                 *path2 = '\\';
1266         
1267         memset(cli->outbuf,'\0',smb_size);
1268         set_message(cli->outbuf,0,0,True);
1269         SCVAL(cli->outbuf,smb_com,SMBchkpth);
1270         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1271         cli_setup_packet(cli);
1272         p = smb_buf(cli->outbuf);
1273         *p++ = 4;
1274         p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1275
1276         cli_setup_bcc(cli, p);
1277
1278         cli_send_smb(cli);
1279         if (!cli_receive_smb(cli)) {
1280                 return False;
1281         }
1282
1283         if (cli_is_error(cli)) return False;
1284
1285         return True;
1286 }
1287
1288 /****************************************************************************
1289  Query disk space.
1290 ****************************************************************************/
1291
1292 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1293 {
1294         memset(cli->outbuf,'\0',smb_size);
1295         set_message(cli->outbuf,0,0,True);
1296         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1297         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1298         cli_setup_packet(cli);
1299
1300         cli_send_smb(cli);
1301         if (!cli_receive_smb(cli)) {
1302                 return False;
1303         }
1304
1305         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1306         *total = SVAL(cli->inbuf,smb_vwv0);
1307         *avail = SVAL(cli->inbuf,smb_vwv3);
1308         
1309         return True;
1310 }
1311
1312 /****************************************************************************
1313  Create and open a temporary file.
1314 ****************************************************************************/
1315
1316 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1317 {
1318         int len;
1319         char *p;
1320
1321         memset(cli->outbuf,'\0',smb_size);
1322         memset(cli->inbuf,'\0',smb_size);
1323
1324         set_message(cli->outbuf,3,0,True);
1325
1326         SCVAL(cli->outbuf,smb_com,SMBctemp);
1327         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1328         cli_setup_packet(cli);
1329
1330         SSVAL(cli->outbuf,smb_vwv0,0);
1331         SIVALS(cli->outbuf,smb_vwv1,-1);
1332
1333         p = smb_buf(cli->outbuf);
1334         *p++ = 4;
1335         p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1336
1337         cli_setup_bcc(cli, p);
1338
1339         cli_send_smb(cli);
1340         if (!cli_receive_smb(cli)) {
1341                 return -1;
1342         }
1343
1344         if (cli_is_error(cli)) {
1345                 return -1;
1346         }
1347
1348         /* despite the spec, the result has a -1, followed by
1349            length, followed by name */
1350         p = smb_buf(cli->inbuf);
1351         p += 4;
1352         len = smb_buflen(cli->inbuf) - 4;
1353         if (len <= 0) return -1;
1354
1355         if (tmp_path) {
1356                 pstring path2;
1357                 clistr_pull(cli, path2, p, 
1358                             sizeof(path2), len, STR_ASCII);
1359                 *tmp_path = SMB_STRDUP(path2);
1360         }
1361
1362         return SVAL(cli->inbuf,smb_vwv0);
1363 }
1364
1365
1366 /* 
1367    send a raw ioctl - used by the torture code
1368 */
1369 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1370 {
1371         memset(cli->outbuf,'\0',smb_size);
1372         memset(cli->inbuf,'\0',smb_size);
1373
1374         set_message(cli->outbuf, 3, 0, True);
1375         SCVAL(cli->outbuf,smb_com,SMBioctl);
1376         cli_setup_packet(cli);
1377
1378         SSVAL(cli->outbuf, smb_vwv0, fnum);
1379         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1380         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1381
1382         cli_send_smb(cli);
1383         if (!cli_receive_smb(cli)) {
1384                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1385         }
1386
1387         if (cli_is_error(cli)) {
1388                 return cli_nt_error(cli);
1389         }
1390
1391         *blob = data_blob(NULL, 0);
1392
1393         return NT_STATUS_OK;
1394 }
1395
1396 /*********************************************************
1397  Set an extended attribute utility fn.
1398 *********************************************************/
1399
1400 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1401                         const char *ea_name, const char *ea_val, size_t ea_len)
1402 {       
1403         unsigned int data_len = 0;
1404         char *data = NULL;
1405         char *rparam=NULL, *rdata=NULL;
1406         char *p;
1407         size_t ea_namelen = strlen(ea_name);
1408
1409         data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1410         data = SMB_MALLOC(data_len);
1411         if (!data) {
1412                 return False;
1413         }
1414         p = data;
1415         SIVAL(p,0,data_len);
1416         p += 4;
1417         SCVAL(p, 0, 0); /* EA flags. */
1418         SCVAL(p, 1, ea_namelen);
1419         SSVAL(p, 2, ea_len);
1420         memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1421         memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1422
1423         if (!cli_send_trans(cli, SMBtrans2,
1424                 NULL,                        /* name */
1425                 -1, 0,                          /* fid, flags */
1426                 &setup, 1, 0,                   /* setup, length, max */
1427                 param, param_len, 2,            /* param, length, max */
1428                 data,  data_len, cli->max_xmit /* data, length, max */
1429                 )) {
1430                         return False;
1431         }
1432
1433         if (!cli_receive_trans(cli, SMBtrans2,
1434                 &rparam, &param_len,
1435                 &rdata, &data_len)) {
1436                         return False;
1437         }
1438
1439         SAFE_FREE(data);
1440         SAFE_FREE(rdata);
1441         SAFE_FREE(rparam);
1442
1443         return True;
1444 }
1445
1446 /*********************************************************
1447  Set an extended attribute on a pathname.
1448 *********************************************************/
1449
1450 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1451 {
1452         uint16 setup = TRANSACT2_SETPATHINFO;
1453         unsigned int param_len = 0;
1454         char param[sizeof(pstring)+6];
1455         size_t srclen = 2*(strlen(path)+1);
1456         char *p;
1457
1458         memset(param, 0, sizeof(param));
1459         SSVAL(param,0,SMB_INFO_SET_EA);
1460         p = &param[6];
1461
1462         p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1463         param_len = PTR_DIFF(p, param);
1464
1465         return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1466 }
1467
1468 /*********************************************************
1469  Set an extended attribute on an fnum.
1470 *********************************************************/
1471
1472 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1473 {
1474         char param[6];
1475         uint16 setup = TRANSACT2_SETFILEINFO;
1476
1477         memset(param, 0, 6);
1478         SSVAL(param,0,fnum);
1479         SSVAL(param,2,SMB_INFO_SET_EA);
1480
1481         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1482 }
1483
1484 /*********************************************************
1485  Get an extended attribute list tility fn.
1486 *********************************************************/
1487
1488 static BOOL cli_get_ea_list(struct cli_state *cli,
1489                 uint16 setup, char *param, unsigned int param_len,
1490                 TALLOC_CTX *ctx,
1491                 size_t *pnum_eas,
1492                 struct ea_struct **pea_list)
1493 {
1494         unsigned int data_len = 0;
1495         unsigned int rparam_len, rdata_len;
1496         char *rparam=NULL, *rdata=NULL;
1497         char *p;
1498         size_t ea_size;
1499         size_t num_eas;
1500         BOOL ret = False;
1501         struct ea_struct *ea_list;
1502
1503         *pnum_eas = 0;
1504         *pea_list = NULL;
1505
1506         if (!cli_send_trans(cli, SMBtrans2,
1507                         NULL,           /* Name */
1508                         -1, 0,          /* fid, flags */
1509                         &setup, 1, 0,   /* setup, length, max */
1510                         param, param_len, 10, /* param, length, max */
1511                         NULL, data_len, cli->max_xmit /* data, length, max */
1512                                 )) {
1513                 return False;
1514         }
1515
1516         if (!cli_receive_trans(cli, SMBtrans2,
1517                         &rparam, &rparam_len,
1518                         &rdata, &rdata_len)) {
1519                 return False;
1520         }
1521
1522         if (!rdata || rdata_len < 4) {
1523                 goto out;
1524         }
1525
1526         ea_size = (size_t)IVAL(rdata,0);
1527         if (ea_size > rdata_len) {
1528                 goto out;
1529         }
1530
1531         if (ea_size == 0) {
1532                 /* No EA's present. */
1533                 ret = True;
1534                 goto out;
1535         }
1536
1537         p = rdata + 4;
1538         ea_size -= 4;
1539
1540         /* Validate the EA list and count it. */
1541         for (num_eas = 0; ea_size >= 4; num_eas++) {
1542                 unsigned int ea_namelen = CVAL(p,1);
1543                 unsigned int ea_valuelen = SVAL(p,2);
1544                 if (ea_namelen == 0) {
1545                         goto out;
1546                 }
1547                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1548                         goto out;
1549                 }
1550                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1551                 p += 4 + ea_namelen + 1 + ea_valuelen;
1552         }
1553
1554         if (num_eas == 0) {
1555                 ret = True;
1556                 goto out;
1557         }
1558
1559         *pnum_eas = num_eas;
1560         if (!pea_list) {
1561                 /* Caller only wants number of EA's. */
1562                 ret = True;
1563                 goto out;
1564         }
1565
1566         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1567         if (!ea_list) {
1568                 goto out;
1569         }
1570
1571         ea_size = (size_t)IVAL(rdata,0);
1572         p = rdata + 4;
1573
1574         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1575                 struct ea_struct *ea = &ea_list[num_eas];
1576                 fstring unix_ea_name;
1577                 unsigned int ea_namelen = CVAL(p,1);
1578                 unsigned int ea_valuelen = SVAL(p,2);
1579
1580                 ea->flags = CVAL(p,0);
1581                 unix_ea_name[0] = '\0';
1582                 pull_ascii_fstring(unix_ea_name, p + 4);
1583                 ea->name = talloc_strdup(ctx, unix_ea_name);
1584                 /* Ensure the value is null terminated (in case it's a string). */
1585                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1586                 if (!ea->value.data) {
1587                         goto out;
1588                 }
1589                 if (ea_valuelen) {
1590                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1591                 }
1592                 ea->value.data[ea_valuelen] = 0;
1593                 ea->value.length--;
1594                 p += 4 + ea_namelen + 1 + ea_valuelen;
1595         }
1596
1597         *pea_list = ea_list;
1598         ret = True;
1599
1600  out :
1601
1602         SAFE_FREE(rdata);
1603         SAFE_FREE(rparam);
1604         return ret;
1605 }
1606
1607 /*********************************************************
1608  Get an extended attribute list from a pathname.
1609 *********************************************************/
1610
1611 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1612                 TALLOC_CTX *ctx,
1613                 size_t *pnum_eas,
1614                 struct ea_struct **pea_list)
1615 {
1616         uint16 setup = TRANSACT2_QPATHINFO;
1617         unsigned int param_len = 0;
1618         char param[sizeof(pstring)+6];
1619         char *p;
1620
1621         p = param;
1622         memset(p, 0, 6);
1623         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1624         p += 6;
1625         p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1626         param_len = PTR_DIFF(p, param);
1627
1628         return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1629 }
1630
1631 /*********************************************************
1632  Get an extended attribute list from an fnum.
1633 *********************************************************/
1634
1635 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1636                 TALLOC_CTX *ctx,
1637                 size_t *pnum_eas,
1638                 struct ea_struct **pea_list)
1639 {
1640         uint16 setup = TRANSACT2_QFILEINFO;
1641         char param[6];
1642
1643         memset(param, 0, 6);
1644         SSVAL(param,0,fnum);
1645         SSVAL(param,2,SMB_INFO_SET_EA);
1646
1647         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1648 }