Merge branch 'master' of ssh://git.samba.org/data/git/samba into libcli-auth-merge...
[ira/wip.git] / source3 / libsmb / clirap.c
1 /*
2    Unix SMB/CIFS implementation.
3    client RAP calls
4    Copyright (C) Andrew Tridgell         1994-1998
5    Copyright (C) Gerald (Jerry) Carter   2004
6    Copyright (C) James Peach             2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "../libcli/auth/libcli_auth.h"
24
25 /****************************************************************************
26  Call a remote api
27 ****************************************************************************/
28
29 bool cli_api(struct cli_state *cli,
30              char *param, int prcnt, int mprcnt,
31              char *data, int drcnt, int mdrcnt,
32              char **rparam, unsigned int *rprcnt,
33              char **rdata, unsigned int *rdrcnt)
34 {
35         cli_send_trans(cli,SMBtrans,
36                  PIPE_LANMAN,             /* Name */
37                  0,0,                     /* fid, flags */
38                  NULL,0,0,                /* Setup, length, max */
39                  param, prcnt, mprcnt,    /* Params, length, max */
40                  data, drcnt, mdrcnt      /* Data, length, max */
41                 );
42
43         return (cli_receive_trans(cli,SMBtrans,
44                             rparam, rprcnt,
45                             rdata, rdrcnt));
46 }
47
48 /****************************************************************************
49  Perform a NetWkstaUserLogon.
50 ****************************************************************************/
51
52 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
53 {
54         char *rparam = NULL;
55         char *rdata = NULL;
56         char *p;
57         unsigned int rdrcnt,rprcnt;
58         char param[1024];
59
60         memset(param, 0, sizeof(param));
61
62         /* send a SMBtrans command with api NetWkstaUserLogon */
63         p = param;
64         SSVAL(p,0,132); /* api number */
65         p += 2;
66         strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
67         p = skip_string(param,sizeof(param),p);
68         strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
69         p = skip_string(param,sizeof(param),p);
70         SSVAL(p,0,1);
71         p += 2;
72         strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
73         strupper_m(p);
74         p += 21;
75         p++;
76         p += 15;
77         p++;
78         strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
79         strupper_m(p);
80         p += 16;
81         SSVAL(p, 0, CLI_BUFFER_SIZE);
82         p += 2;
83         SSVAL(p, 0, CLI_BUFFER_SIZE);
84         p += 2;
85
86         if (cli_api(cli,
87                     param, PTR_DIFF(p,param),1024,  /* param, length, max */
88                     NULL, 0, CLI_BUFFER_SIZE,           /* data, length, max */
89                     &rparam, &rprcnt,               /* return params, return size */
90                     &rdata, &rdrcnt                 /* return data, return size */
91                    )) {
92                 cli->rap_error = rparam? SVAL(rparam,0) : -1;
93                 p = rdata;
94
95                 if (cli->rap_error == 0) {
96                         DEBUG(4,("NetWkstaUserLogon success\n"));
97                         cli->privileges = SVAL(p, 24);
98                         /* The cli->eff_name field used to be set here
99                            but it wasn't used anywhere else. */
100                 } else {
101                         DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
102                 }
103         }
104
105         SAFE_FREE(rparam);
106         SAFE_FREE(rdata);
107         return (cli->rap_error == 0);
108 }
109
110 /****************************************************************************
111  Call a NetShareEnum - try and browse available connections on a host.
112 ****************************************************************************/
113
114 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
115 {
116         char *rparam = NULL;
117         char *rdata = NULL;
118         char *p;
119         unsigned int rdrcnt,rprcnt;
120         char param[1024];
121         int count = -1;
122
123         /* now send a SMBtrans command with api RNetShareEnum */
124         p = param;
125         SSVAL(p,0,0); /* api number */
126         p += 2;
127         strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
128         p = skip_string(param,sizeof(param),p);
129         strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
130         p = skip_string(param,sizeof(param),p);
131         SSVAL(p,0,1);
132         /*
133          * Win2k needs a *smaller* buffer than 0xFFFF here -
134          * it returns "out of server memory" with 0xFFFF !!! JRA.
135          */
136         SSVAL(p,2,0xFFE0);
137         p += 4;
138
139         if (cli_api(cli,
140                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
141                     NULL, 0, 0xFFE0,            /* data, length, maxlen - Win2k needs a small buffer here too ! */
142                     &rparam, &rprcnt,                /* return params, length */
143                     &rdata, &rdrcnt))                /* return data, length */
144                 {
145                         int res = rparam? SVAL(rparam,0) : -1;
146
147                         if (res == 0 || res == ERRmoredata) {
148                                 int converter=SVAL(rparam,2);
149                                 int i;
150                                 char *rdata_end = rdata + rdrcnt;
151
152                                 count=SVAL(rparam,4);
153                                 p = rdata;
154
155                                 for (i=0;i<count;i++,p+=20) {
156                                         char *sname;
157                                         int type;
158                                         int comment_offset;
159                                         const char *cmnt;
160                                         const char *p1;
161                                         char *s1, *s2;
162                                         size_t len;
163                                         TALLOC_CTX *frame = talloc_stackframe();
164
165                                         if (p + 20 > rdata_end) {
166                                                 TALLOC_FREE(frame);
167                                                 break;
168                                         }
169
170                                         sname = p;
171                                         type = SVAL(p,14);
172                                         comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
173                                         if (comment_offset < 0 ||
174                                                         comment_offset > (int)rdrcnt) {
175                                                 TALLOC_FREE(frame);
176                                                 break;
177                                         }
178                                         cmnt = comment_offset?(rdata+comment_offset):"";
179
180                                         /* Work out the comment length. */
181                                         for (p1 = cmnt, len = 0; *p1 &&
182                                                         p1 < rdata_end; len++)
183                                                 p1++;
184                                         if (!*p1) {
185                                                 len++;
186                                         }
187                                         pull_string_talloc(frame,rdata,0,
188                                                 &s1,sname,14,STR_ASCII);
189                                         pull_string_talloc(frame,rdata,0,
190                                                 &s2,cmnt,len,STR_ASCII);
191                                         if (!s1 || !s2) {
192                                                 TALLOC_FREE(frame);
193                                                 continue;
194                                         }
195
196                                         fn(s1, type, s2, state);
197
198                                         TALLOC_FREE(frame);
199                                 }
200                         } else {
201                                 DEBUG(4,("NetShareEnum res=%d\n", res));
202                         }
203                 } else {
204                         DEBUG(4,("NetShareEnum failed\n"));
205                 }
206
207         SAFE_FREE(rparam);
208         SAFE_FREE(rdata);
209
210         return count;
211 }
212
213 /****************************************************************************
214  Call a NetServerEnum for the specified workgroup and servertype mask.  This
215  function then calls the specified callback function for each name returned.
216
217  The callback function takes 4 arguments: the machine name, the server type,
218  the comment and a state pointer.
219 ****************************************************************************/
220
221 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
222                        void (*fn)(const char *, uint32, const char *, void *),
223                        void *state)
224 {
225         char *rparam = NULL;
226         char *rdata = NULL;
227         char *rdata_end = NULL;
228         unsigned int rdrcnt,rprcnt;
229         char *p;
230         char param[1024];
231         int uLevel = 1;
232         size_t len;
233         uint32 func = RAP_NetServerEnum2;
234         char *last_entry = NULL;
235         int total_cnt = 0;
236         int return_cnt = 0;
237         int res;
238
239         errno = 0; /* reset */
240
241         /*
242          * This may take more than one transaction, so we should loop until
243          * we no longer get a more data to process or we have all of the
244          * items.
245          */
246         do {
247                 /* send a SMBtrans command with api NetServerEnum */
248                 p = param;
249                 SIVAL(p,0,func); /* api number */
250                 p += 2;
251                 /* Next time through we need to use the continue api */
252                 func = RAP_NetServerEnum3;
253
254                 if (last_entry) {
255                         strlcpy(p,"WrLehDOz", sizeof(param)-PTR_DIFF(p,param));
256                 } else {
257                         strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
258                 }
259
260                 p = skip_string(param, sizeof(param), p);
261                 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
262
263                 p = skip_string(param, sizeof(param), p);
264                 SSVAL(p,0,uLevel);
265                 SSVAL(p,2,CLI_BUFFER_SIZE);
266                 p += 4;
267                 SIVAL(p,0,stype);
268                 p += 4;
269
270                 /* If we have more data, tell the server where
271                  * to continue from.
272                  */
273                 len = push_ascii(p,
274                                 last_entry ? last_entry : workgroup,
275                                 sizeof(param) - PTR_DIFF(p,param) - 1,
276                                 STR_TERMINATE|STR_UPPER);
277
278                 if (len == (size_t)-1) {
279                         SAFE_FREE(last_entry);
280                         return false;
281                 }
282                 p += len;
283
284                 if (!cli_api(cli,
285                         param, PTR_DIFF(p,param), 8, /* params, length, max */
286                         NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
287                             &rparam, &rprcnt, /* return params, return size */
288                             &rdata, &rdrcnt)) { /* return data, return size */
289
290                         /* break out of the loop on error */
291                         res = -1;
292                         break;
293                 }
294
295                 rdata_end = rdata + rdrcnt;
296                 res = rparam ? SVAL(rparam,0) : -1;
297
298                 if (res == 0 || res == ERRmoredata ||
299                     (res != -1 && cli_errno(cli) == 0)) {
300                         char *sname = NULL;
301                         int i, count;
302                         int converter=SVAL(rparam,2);
303
304                         /* Get the number of items returned in this buffer */
305                         count = SVAL(rparam, 4);
306
307                         /* The next field contains the number of items left,
308                          * including those returned in this buffer. So the
309                          * first time through this should contain all of the
310                          * entries.
311                          */
312                         if (total_cnt == 0) {
313                                 total_cnt = SVAL(rparam, 6);
314                         }
315
316                         /* Keep track of how many we have read */
317                         return_cnt += count;
318                         p = rdata;
319
320                         /* The last name in the previous NetServerEnum reply is
321                          * sent back to server in the NetServerEnum3 request
322                          * (last_entry). The next reply should repeat this entry
323                          * as the first element. We have no proof that this is
324                          * always true, but from traces that seems to be the
325                          * behavior from Window Servers. So first lets do a lot
326                          * of checking, just being paranoid. If the string
327                          * matches then we already saw this entry so skip it.
328                          *
329                          * NOTE: sv1_name field must be null terminated and has
330                          * a max size of 16 (NetBIOS Name).
331                          */
332                         if (last_entry && count && p &&
333                                 (strncmp(last_entry, p, 16) == 0)) {
334                             count -= 1; /* Skip this entry */
335                             return_cnt = -1; /* Not part of total, so don't count. */
336                             p = rdata + 26; /* Skip the whole record */
337                         }
338
339                         for (i = 0; i < count; i++, p += 26) {
340                                 int comment_offset;
341                                 const char *cmnt;
342                                 const char *p1;
343                                 char *s1, *s2;
344                                 TALLOC_CTX *frame = talloc_stackframe();
345
346                                 if (p + 26 > rdata_end) {
347                                         TALLOC_FREE(frame);
348                                         break;
349                                 }
350
351                                 sname = p;
352                                 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
353                                 cmnt = comment_offset?(rdata+comment_offset):"";
354
355                                 if (comment_offset < 0 || comment_offset > (int)rdrcnt) {
356                                         TALLOC_FREE(frame);
357                                         continue;
358                                 }
359
360                                 /* Work out the comment length. */
361                                 for (p1 = cmnt, len = 0; *p1 &&
362                                                 p1 < rdata_end; len++)
363                                         p1++;
364                                 if (!*p1) {
365                                         len++;
366                                 }
367
368                                 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
369
370                                 pull_string_talloc(frame,rdata,0,
371                                         &s1,sname,16,STR_ASCII);
372                                 pull_string_talloc(frame,rdata,0,
373                                         &s2,cmnt,len,STR_ASCII);
374
375                                 if (!s1 || !s2) {
376                                         TALLOC_FREE(frame);
377                                         continue;
378                                 }
379
380                                 fn(s1, stype, s2, state);
381                                 TALLOC_FREE(frame);
382                         }
383
384                         /* We are done with the old last entry, so now we can free it */
385                         if (last_entry) {
386                                 SAFE_FREE(last_entry); /* This will set it to null */
387                         }
388
389                         /* We always make a copy of  the last entry if we have one */
390                         if (sname) {
391                                 last_entry = smb_xstrdup(sname);
392                         }
393
394                         /* If we have more data, but no last entry then error out */
395                         if (!last_entry && (res == ERRmoredata)) {
396                                 errno = EINVAL;
397                                 res = 0;
398                         }
399
400                 }
401
402                 SAFE_FREE(rparam);
403                 SAFE_FREE(rdata);
404         } while ((res == ERRmoredata) && (total_cnt > return_cnt));
405
406         SAFE_FREE(rparam);
407         SAFE_FREE(rdata);
408         SAFE_FREE(last_entry);
409
410         if (res == -1) {
411                 errno = cli_errno(cli);
412         } else {
413                 if (!return_cnt) {
414                         /* this is a very special case, when the domain master for the
415                            work group isn't part of the work group itself, there is something
416                            wild going on */
417                         errno = ENOENT;
418                 }
419             }
420
421         return(return_cnt > 0);
422 }
423
424 /****************************************************************************
425  Send a SamOEMChangePassword command.
426 ****************************************************************************/
427
428 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
429                              const char *old_password)
430 {
431         char param[1024];
432         unsigned char data[532];
433         char *p = param;
434         unsigned char old_pw_hash[16];
435         unsigned char new_pw_hash[16];
436         unsigned int data_len;
437         unsigned int param_len = 0;
438         char *rparam = NULL;
439         char *rdata = NULL;
440         unsigned int rprcnt, rdrcnt;
441
442         if (strlen(user) >= sizeof(fstring)-1) {
443                 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
444                 return False;
445         }
446
447         SSVAL(p,0,214); /* SamOEMChangePassword command. */
448         p += 2;
449         strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
450         p = skip_string(param,sizeof(param),p);
451         strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
452         p = skip_string(param,sizeof(param),p);
453         strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
454         p = skip_string(param,sizeof(param),p);
455         SSVAL(p,0,532);
456         p += 2;
457
458         param_len = PTR_DIFF(p,param);
459
460         /*
461          * Get the Lanman hash of the old password, we
462          * use this as the key to make_oem_passwd_hash().
463          */
464         E_deshash(old_password, old_pw_hash);
465
466         encode_pw_buffer(data, new_password, STR_ASCII);
467
468 #ifdef DEBUG_PASSWORD
469         DEBUG(100,("make_oem_passwd_hash\n"));
470         dump_data(100, data, 516);
471 #endif
472         arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
473
474         /*
475          * Now place the old password hash in the data.
476          */
477         E_deshash(new_password, new_pw_hash);
478
479         E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
480
481         data_len = 532;
482
483         if (cli_send_trans(cli,SMBtrans,
484                     PIPE_LANMAN,                          /* name */
485                     0,0,                                  /* fid, flags */
486                     NULL,0,0,                             /* setup, length, max */
487                     param,param_len,2,                    /* param, length, max */
488                     (char *)data,data_len,0                       /* data, length, max */
489                    ) == False) {
490                 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
491                         user ));
492                 return False;
493         }
494
495         if (!cli_receive_trans(cli,SMBtrans,
496                        &rparam, &rprcnt,
497                        &rdata, &rdrcnt)) {
498                 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
499                         user ));
500                 return False;
501         }
502
503         if (rparam) {
504                 cli->rap_error = SVAL(rparam,0);
505         }
506
507         SAFE_FREE(rparam);
508         SAFE_FREE(rdata);
509
510         return (cli->rap_error == 0);
511 }
512
513 /****************************************************************************
514  Send a qpathinfo call.
515 ****************************************************************************/
516
517 bool cli_qpathinfo(struct cli_state *cli,
518                         const char *fname,
519                         time_t *change_time,
520                         time_t *access_time,
521                         time_t *write_time,
522                         SMB_OFF_T *size,
523                         uint16 *mode)
524 {
525         unsigned int data_len = 0;
526         unsigned int param_len = 0;
527         unsigned int rparam_len, rdata_len;
528         uint16 setup = TRANSACT2_QPATHINFO;
529         char *param;
530         char *rparam=NULL, *rdata=NULL;
531         int count=8;
532         bool ret;
533         time_t (*date_fn)(struct cli_state *, const void *);
534         char *p;
535         size_t nlen = 2*(strlen(fname)+1);
536
537         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
538         if (!param) {
539                 return false;
540         }
541         p = param;
542         memset(p, '\0', 6);
543         SSVAL(p, 0, SMB_INFO_STANDARD);
544         p += 6;
545         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
546         param_len = PTR_DIFF(p, param);
547
548         do {
549                 ret = (cli_send_trans(cli, SMBtrans2,
550                                       NULL,           /* Name */
551                                       -1, 0,          /* fid, flags */
552                                       &setup, 1, 0,   /* setup, length, max */
553                                       param, param_len, 10, /* param, length, max */
554                                       NULL, data_len, cli->max_xmit /* data, length, max */
555                                       ) &&
556                        cli_receive_trans(cli, SMBtrans2,
557                                          &rparam, &rparam_len,
558                                          &rdata, &rdata_len));
559                 if (!cli_is_dos_error(cli)) break;
560                 if (!ret) {
561                         /* we need to work around a Win95 bug - sometimes
562                            it gives ERRSRV/ERRerror temprarily */
563                         uint8 eclass;
564                         uint32 ecode;
565                         cli_dos_error(cli, &eclass, &ecode);
566                         if (eclass != ERRSRV || ecode != ERRerror) break;
567                         smb_msleep(100);
568                 }
569         } while (count-- && ret==False);
570
571         SAFE_FREE(param);
572         if (!ret || !rdata || rdata_len < 22) {
573                 return False;
574         }
575
576         if (cli->win95) {
577                 date_fn = cli_make_unix_date;
578         } else {
579                 date_fn = cli_make_unix_date2;
580         }
581
582         if (change_time) {
583                 *change_time = date_fn(cli, rdata+0);
584         }
585         if (access_time) {
586                 *access_time = date_fn(cli, rdata+4);
587         }
588         if (write_time) {
589                 *write_time = date_fn(cli, rdata+8);
590         }
591         if (size) {
592                 *size = IVAL(rdata, 12);
593         }
594         if (mode) {
595                 *mode = SVAL(rdata,l1_attrFile);
596         }
597
598         SAFE_FREE(rdata);
599         SAFE_FREE(rparam);
600         return True;
601 }
602
603 /****************************************************************************
604  Send a setpathinfo call.
605 ****************************************************************************/
606
607 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
608                      time_t create_time,
609                      time_t access_time,
610                      time_t write_time,
611                      time_t change_time,
612                      uint16 mode)
613 {
614         unsigned int data_len = 0;
615         unsigned int param_len = 0;
616         unsigned int rparam_len, rdata_len;
617         uint16 setup = TRANSACT2_SETPATHINFO;
618         char *param;
619         char data[40];
620         char *rparam=NULL, *rdata=NULL;
621         int count=8;
622         bool ret;
623         char *p;
624         size_t nlen = 2*(strlen(fname)+1);
625
626         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
627         if (!param) {
628                 return false;
629         }
630         memset(param, '\0', 6);
631         memset(data, 0, sizeof(data));
632
633         p = param;
634
635         /* Add the information level */
636         SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
637
638         /* Skip reserved */
639         p += 6;
640
641         /* Add the file name */
642         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
643
644         param_len = PTR_DIFF(p, param);
645
646         p = data;
647
648         /*
649          * Add the create, last access, modification, and status change times
650          */
651         put_long_date(p, create_time);
652         p += 8;
653
654         put_long_date(p, access_time);
655         p += 8;
656
657         put_long_date(p, write_time);
658         p += 8;
659
660         put_long_date(p, change_time);
661         p += 8;
662
663         /* Add attributes */
664         SIVAL(p, 0, mode);
665         p += 4;
666
667         /* Add padding */
668         SIVAL(p, 0, 0);
669         p += 4;
670
671         data_len = PTR_DIFF(p, data);
672
673         do {
674                 ret = (cli_send_trans(cli, SMBtrans2,
675                                       NULL,           /* Name */
676                                       -1, 0,          /* fid, flags */
677                                       &setup, 1, 0,   /* setup, length, max */
678                                       param, param_len, 10, /* param, length, max */
679                                       data, data_len, cli->max_xmit /* data, length, max */
680                                       ) &&
681                        cli_receive_trans(cli, SMBtrans2,
682                                          &rparam, &rparam_len,
683                                          &rdata, &rdata_len));
684                 if (!cli_is_dos_error(cli)) break;
685                 if (!ret) {
686                         /* we need to work around a Win95 bug - sometimes
687                            it gives ERRSRV/ERRerror temprarily */
688                         uint8 eclass;
689                         uint32 ecode;
690                         cli_dos_error(cli, &eclass, &ecode);
691                         if (eclass != ERRSRV || ecode != ERRerror) break;
692                         smb_msleep(100);
693                 }
694         } while (count-- && ret==False);
695
696         SAFE_FREE(param);
697         if (!ret) {
698                 return False;
699         }
700
701         SAFE_FREE(rdata);
702         SAFE_FREE(rparam);
703         return True;
704 }
705
706 /****************************************************************************
707  Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
708 ****************************************************************************/
709
710 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
711                     struct timespec *create_time,
712                     struct timespec *access_time,
713                     struct timespec *write_time,
714                     struct timespec *change_time,
715                     SMB_OFF_T *size, uint16 *mode,
716                     SMB_INO_T *ino)
717 {
718         unsigned int data_len = 0;
719         unsigned int param_len = 0;
720         uint16 setup = TRANSACT2_QPATHINFO;
721         char *param;
722         char *rparam=NULL, *rdata=NULL;
723         char *p;
724         size_t nlen = 2*(strlen(fname)+1);
725
726         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
727         if (!param) {
728                 return false;
729         }
730         p = param;
731         memset(param, '\0', 6);
732         SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
733         p += 6;
734         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
735
736         param_len = PTR_DIFF(p, param);
737
738         if (!cli_send_trans(cli, SMBtrans2,
739                             NULL,                         /* name */
740                             -1, 0,                        /* fid, flags */
741                             &setup, 1, 0,                 /* setup, length, max */
742                             param, param_len, 10,         /* param, length, max */
743                             NULL, data_len, cli->max_xmit /* data, length, max */
744                            )) {
745                 SAFE_FREE(param);
746                 return False;
747         }
748
749         SAFE_FREE(param);
750         if (!cli_receive_trans(cli, SMBtrans2,
751                                &rparam, &param_len,
752                                &rdata, &data_len)) {
753                 return False;
754         }
755
756         if (!rdata || data_len < 22) {
757                 return False;
758         }
759
760         if (create_time) {
761                 *create_time = interpret_long_date(rdata+0);
762         }
763         if (access_time) {
764                 *access_time = interpret_long_date(rdata+8);
765         }
766         if (write_time) {
767                 *write_time = interpret_long_date(rdata+16);
768         }
769         if (change_time) {
770                 *change_time = interpret_long_date(rdata+24);
771         }
772         if (mode) {
773                 *mode = SVAL(rdata, 32);
774         }
775         if (size) {
776                 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
777         }
778         if (ino) {
779                 *ino = IVAL(rdata, 64);
780         }
781
782         SAFE_FREE(rdata);
783         SAFE_FREE(rparam);
784         return True;
785 }
786
787 /****************************************************************************
788  Get the stream info
789 ****************************************************************************/
790
791 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
792                            TALLOC_CTX *mem_ctx,
793                            unsigned int *pnum_streams,
794                            struct stream_struct **pstreams)
795 {
796         unsigned int data_len = 0;
797         unsigned int param_len = 0;
798         uint16 setup = TRANSACT2_QPATHINFO;
799         char *param;
800         char *rparam=NULL, *rdata=NULL;
801         char *p;
802         unsigned int num_streams;
803         struct stream_struct *streams;
804         unsigned int ofs;
805         size_t namelen = 2*(strlen(fname)+1);
806
807         param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
808         if (param == NULL) {
809                 return false;
810         }
811         p = param;
812         memset(p, 0, 6);
813         SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
814         p += 6;
815         p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
816
817         param_len = PTR_DIFF(p, param);
818
819         if (!cli_send_trans(cli, SMBtrans2,
820                             NULL,                     /* name */
821                             -1, 0,                    /* fid, flags */
822                             &setup, 1, 0,             /* setup, len, max */
823                             param, param_len, 10,     /* param, len, max */
824                             NULL, data_len, cli->max_xmit /* data, len, max */
825                            )) {
826                 return false;
827         }
828
829         if (!cli_receive_trans(cli, SMBtrans2,
830                                &rparam, &param_len,
831                                &rdata, &data_len)) {
832                 return false;
833         }
834
835         if (!rdata) {
836                 SAFE_FREE(rparam);
837                 return false;
838         }
839
840         num_streams = 0;
841         streams = NULL;
842         ofs = 0;
843
844         while ((data_len > ofs) && (data_len - ofs >= 24)) {
845                 uint32_t nlen, len;
846                 size_t size;
847                 void *vstr;
848                 struct stream_struct *tmp;
849                 uint8_t *tmp_buf;
850
851                 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
852                                            struct stream_struct,
853                                            num_streams+1);
854
855                 if (tmp == NULL) {
856                         goto fail;
857                 }
858                 streams = tmp;
859
860                 nlen                      = IVAL(rdata, ofs + 0x04);
861
862                 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
863                         rdata, ofs + 0x08);
864                 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
865                         rdata, ofs + 0x10);
866
867                 if (nlen > data_len - (ofs + 24)) {
868                         goto fail;
869                 }
870
871                 /*
872                  * We need to null-terminate src, how do I do this with
873                  * convert_string_talloc??
874                  */
875
876                 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
877                 if (tmp_buf == NULL) {
878                         goto fail;
879                 }
880
881                 memcpy(tmp_buf, rdata+ofs+24, nlen);
882                 tmp_buf[nlen] = 0;
883                 tmp_buf[nlen+1] = 0;
884
885                 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
886                                            nlen+2, &vstr, &size, false))
887                 {
888                         TALLOC_FREE(tmp_buf);
889                         goto fail;
890                 }
891
892                 TALLOC_FREE(tmp_buf);
893                 streams[num_streams].name = (char *)vstr;
894                 num_streams++;
895
896                 len = IVAL(rdata, ofs);
897                 if (len > data_len - ofs) {
898                         goto fail;
899                 }
900                 if (len == 0) break;
901                 ofs += len;
902         }
903
904         SAFE_FREE(rdata);
905         SAFE_FREE(rparam);
906
907         *pnum_streams = num_streams;
908         *pstreams = streams;
909         return true;
910
911  fail:
912         TALLOC_FREE(streams);
913         SAFE_FREE(rdata);
914         SAFE_FREE(rparam);
915         return false;
916 }
917
918 /****************************************************************************
919  Send a qfileinfo QUERY_FILE_NAME_INFO call.
920 ****************************************************************************/
921
922 bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
923 {
924         unsigned int data_len = 0;
925         unsigned int param_len = 0;
926         uint16 setup = TRANSACT2_QFILEINFO;
927         char param[4];
928         char *rparam=NULL, *rdata=NULL;
929
930         param_len = 4;
931         SSVAL(param, 0, fnum);
932         SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
933
934         if (!cli_send_trans(cli, SMBtrans2,
935                             NULL,                         /* name */
936                             -1, 0,                        /* fid, flags */
937                             &setup, 1, 0,                 /* setup, length, max */
938                             param, param_len, 2,          /* param, length, max */
939                             NULL, data_len, cli->max_xmit /* data, length, max */
940                            )) {
941                 return False;
942         }
943
944         if (!cli_receive_trans(cli, SMBtrans2,
945                                &rparam, &param_len,
946                                &rdata, &data_len)) {
947                 return False;
948         }
949
950         if (!rdata || data_len < 4) {
951                 SAFE_FREE(rparam);
952                 SAFE_FREE(rdata);
953                 return False;
954         }
955
956         clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
957                     STR_UNICODE);
958
959         SAFE_FREE(rparam);
960         SAFE_FREE(rdata);
961
962         return True;
963 }
964
965 /****************************************************************************
966  Send a qfileinfo call.
967 ****************************************************************************/
968
969 bool cli_qfileinfo(struct cli_state *cli, int fnum,
970                    uint16 *mode, SMB_OFF_T *size,
971                    struct timespec *create_time,
972                    struct timespec *access_time,
973                    struct timespec *write_time,
974                    struct timespec *change_time,
975                    SMB_INO_T *ino)
976 {
977         unsigned int data_len = 0;
978         unsigned int param_len = 0;
979         uint16 setup;
980         uint8_t param[4];
981         uint8_t *rparam=NULL, *rdata=NULL;
982         NTSTATUS status;
983
984         /* if its a win95 server then fail this - win95 totally screws it
985            up */
986         if (cli->win95) return False;
987
988         param_len = 4;
989
990         SSVAL(param, 0, fnum);
991         SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
992
993         SSVAL(&setup, 0, TRANSACT2_QFILEINFO);
994
995         status = cli_trans(talloc_tos(), cli, SMBtrans2,
996                            NULL, -1, 0, 0, /* name, fid, function, flags */
997                            &setup, 1, 0,          /* setup, length, max */
998                            param, param_len, 2,   /* param, length, max */
999                            NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
1000                            NULL, NULL, /* rsetup, length */
1001                            &rparam, &param_len, /* rparam, length */
1002                            &rdata, &data_len);
1003
1004         if (!NT_STATUS_IS_OK(status)) {
1005                 return false;
1006         }
1007
1008         if (!rdata || data_len < 68) {
1009                 return False;
1010         }
1011
1012         if (create_time) {
1013                 *create_time = interpret_long_date((char *)rdata+0);
1014         }
1015         if (access_time) {
1016                 *access_time = interpret_long_date((char *)rdata+8);
1017         }
1018         if (write_time) {
1019                 *write_time = interpret_long_date((char *)rdata+16);
1020         }
1021         if (change_time) {
1022                 *change_time = interpret_long_date((char *)rdata+24);
1023         }
1024         if (mode) {
1025                 *mode = SVAL(rdata, 32);
1026         }
1027         if (size) {
1028                 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1029         }
1030         if (ino) {
1031                 *ino = IVAL(rdata, 64);
1032         }
1033
1034         TALLOC_FREE(rdata);
1035         TALLOC_FREE(rparam);
1036         return True;
1037 }
1038
1039 /****************************************************************************
1040  Send a qpathinfo BASIC_INFO call.
1041 ****************************************************************************/
1042
1043 bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1044                           SMB_STRUCT_STAT *sbuf, uint32 *attributes )
1045 {
1046         unsigned int param_len = 0;
1047         unsigned int data_len = 0;
1048         uint16 setup = TRANSACT2_QPATHINFO;
1049         char *param;
1050         char *rparam=NULL, *rdata=NULL;
1051         char *p;
1052         char *path;
1053         int len;
1054         size_t nlen;
1055         TALLOC_CTX *frame = talloc_stackframe();
1056
1057         path = talloc_strdup(frame, name);
1058         if (!path) {
1059                 TALLOC_FREE(frame);
1060                 return false;
1061         }
1062         /* cleanup */
1063
1064         len = strlen(path);
1065         if ( path[len-1] == '\\' || path[len-1] == '/') {
1066                 path[len-1] = '\0';
1067         }
1068         nlen = 2*(strlen(path)+1);
1069
1070         param = TALLOC_ARRAY(frame,char,6+nlen+2);
1071         if (!param) {
1072                 return false;
1073         }
1074         p = param;
1075         memset(param, '\0', 6);
1076
1077         SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1078         p += 6;
1079         p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1080         param_len = PTR_DIFF(p, param);
1081
1082
1083         if (!cli_send_trans(cli, SMBtrans2,
1084                         NULL,                        /* name */
1085                         -1, 0,                       /* fid, flags */
1086                         &setup, 1, 0,                /* setup, length, max */
1087                         param, param_len, 2,         /* param, length, max */
1088                         NULL,  0, cli->max_xmit      /* data, length, max */
1089                         )) {
1090                 TALLOC_FREE(frame);
1091                 return False;
1092         }
1093
1094         TALLOC_FREE(frame);
1095
1096         if (!cli_receive_trans(cli, SMBtrans2,
1097                 &rparam, &param_len,
1098                 &rdata, &data_len)) {
1099                         return False;
1100         }
1101
1102         if (data_len < 36) {
1103                 SAFE_FREE(rdata);
1104                 SAFE_FREE(rparam);
1105                 return False;
1106         }
1107
1108         set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
1109         set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
1110         set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
1111
1112         *attributes = IVAL( rdata, 32 );
1113
1114         SAFE_FREE(rparam);
1115         SAFE_FREE(rdata);
1116
1117         return True;
1118 }
1119
1120 /****************************************************************************
1121  Send a qfileinfo call.
1122 ****************************************************************************/
1123
1124 bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
1125 {
1126         unsigned int data_len = 0;
1127         unsigned int param_len = 0;
1128         uint16 setup = TRANSACT2_QFILEINFO;
1129         char param[4];
1130         char *rparam=NULL, *rdata=NULL;
1131
1132         *poutdata = NULL;
1133         *poutlen = 0;
1134
1135         /* if its a win95 server then fail this - win95 totally screws it
1136            up */
1137         if (cli->win95)
1138                 return False;
1139
1140         param_len = 4;
1141
1142         SSVAL(param, 0, fnum);
1143         SSVAL(param, 2, level);
1144
1145         if (!cli_send_trans(cli, SMBtrans2,
1146                             NULL,                           /* name */
1147                             -1, 0,                          /* fid, flags */
1148                             &setup, 1, 0,                   /* setup, length, max */
1149                             param, param_len, 2,            /* param, length, max */
1150                             NULL, data_len, cli->max_xmit   /* data, length, max */
1151                            )) {
1152                 return False;
1153         }
1154
1155         if (!cli_receive_trans(cli, SMBtrans2,
1156                                &rparam, &param_len,
1157                                &rdata, &data_len)) {
1158                 return False;
1159         }
1160
1161         *poutdata = (char *)memdup(rdata, data_len);
1162         if (!*poutdata) {
1163                 SAFE_FREE(rdata);
1164                 SAFE_FREE(rparam);
1165                 return False;
1166         }
1167
1168         *poutlen = data_len;
1169
1170         SAFE_FREE(rdata);
1171         SAFE_FREE(rparam);
1172         return True;
1173 }
1174
1175 /****************************************************************************
1176  Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1177 ****************************************************************************/
1178
1179 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1180 {
1181         unsigned int data_len = 0;
1182         unsigned int param_len = 0;
1183         uint16 setup = TRANSACT2_QPATHINFO;
1184         char *param;
1185         char *rparam=NULL, *rdata=NULL;
1186         int count=8;
1187         char *p;
1188         bool ret;
1189         unsigned int len;
1190         size_t nlen = 2*(strlen(fname)+1);
1191
1192         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1193         if (!param) {
1194                 return NT_STATUS_NO_MEMORY;
1195         }
1196         p = param;
1197         memset(param, '\0', 6);
1198         SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1199         p += 6;
1200         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1201         param_len = PTR_DIFF(p, param);
1202
1203         do {
1204                 ret = (cli_send_trans(cli, SMBtrans2,
1205                                       NULL,           /* Name */
1206                                       -1, 0,          /* fid, flags */
1207                                       &setup, 1, 0,   /* setup, length, max */
1208                                       param, param_len, 10, /* param, length, max */
1209                                       NULL, data_len, cli->max_xmit /* data, length, max */
1210                                       ) &&
1211                        cli_receive_trans(cli, SMBtrans2,
1212                                          &rparam, &param_len,
1213                                          &rdata, &data_len));
1214                 if (!ret && cli_is_dos_error(cli)) {
1215                         /* we need to work around a Win95 bug - sometimes
1216                            it gives ERRSRV/ERRerror temprarily */
1217                         uint8 eclass;
1218                         uint32 ecode;
1219                         cli_dos_error(cli, &eclass, &ecode);
1220                         if (eclass != ERRSRV || ecode != ERRerror) break;
1221                         smb_msleep(100);
1222                 }
1223         } while (count-- && ret==False);
1224
1225         SAFE_FREE(param);
1226
1227         if (!ret || !rdata || data_len < 4) {
1228                 return NT_STATUS_UNSUCCESSFUL;
1229         }
1230
1231         len = IVAL(rdata, 0);
1232
1233         if (len > data_len - 4) {
1234                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1235         }
1236
1237         clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1238                     STR_UNICODE);
1239
1240         SAFE_FREE(rdata);
1241         SAFE_FREE(rparam);
1242
1243         return NT_STATUS_OK;
1244 }