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