s3: libsmbclient: Fix cli_setpathinfo_basic() to treat mode == -1 as no change.
[samba.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 #include "../lib/crypto/arcfour.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "async_smb.h"
28 #include "libsmb/libsmb.h"
29 #include "libsmb/clirap.h"
30 #include "trans2.h"
31 #include "../libcli/smb/smbXcli_base.h"
32
33 #define PIPE_LANMAN   "\\PIPE\\LANMAN"
34
35 /****************************************************************************
36  Call a remote api
37 ****************************************************************************/
38
39 bool cli_api(struct cli_state *cli,
40              char *param, int prcnt, int mprcnt,
41              char *data, int drcnt, int mdrcnt,
42              char **rparam, unsigned int *rprcnt,
43              char **rdata, unsigned int *rdrcnt)
44 {
45         NTSTATUS status;
46
47         uint8_t *my_rparam, *my_rdata;
48         uint32_t num_my_rparam, num_my_rdata;
49
50         status = cli_trans(talloc_tos(), cli, SMBtrans,
51                            PIPE_LANMAN, 0, /* name, fid */
52                            0, 0,           /* function, flags */
53                            NULL, 0, 0,     /* setup */
54                            (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
55                            (uint8_t *)data, drcnt, mdrcnt,  /* Data, length, max */
56                            NULL,                 /* recv_flags2 */
57                            NULL, 0, NULL,        /* rsetup */
58                            &my_rparam, 0, &num_my_rparam,
59                            &my_rdata, 0, &num_my_rdata);
60         if (!NT_STATUS_IS_OK(status)) {
61                 return false;
62         }
63
64         /*
65          * I know this memcpy massively hurts, but there are just tons
66          * of callers of cli_api that eventually need changing to
67          * talloc
68          */
69
70         *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
71         if (*rparam == NULL) {
72                 goto fail;
73         }
74         *rprcnt = num_my_rparam;
75         TALLOC_FREE(my_rparam);
76
77         *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
78         if (*rdata == NULL) {
79                 goto fail;
80         }
81         *rdrcnt = num_my_rdata;
82         TALLOC_FREE(my_rdata);
83
84         return true;
85 fail:
86         TALLOC_FREE(my_rdata);
87         TALLOC_FREE(my_rparam);
88         *rparam = NULL;
89         *rprcnt = 0;
90         *rdata = NULL;
91         *rdrcnt = 0;
92         return false;
93 }
94
95 /****************************************************************************
96  Perform a NetWkstaUserLogon.
97 ****************************************************************************/
98
99 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
100 {
101         char *rparam = NULL;
102         char *rdata = NULL;
103         char *p;
104         unsigned int rdrcnt,rprcnt;
105         char param[1024];
106
107         memset(param, 0, sizeof(param));
108
109         /* send a SMBtrans command with api NetWkstaUserLogon */
110         p = param;
111         SSVAL(p,0,132); /* api number */
112         p += 2;
113         strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
114         p = skip_string(param,sizeof(param),p);
115         strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
116         p = skip_string(param,sizeof(param),p);
117         SSVAL(p,0,1);
118         p += 2;
119         strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
120         if (!strupper_m(p)) {
121                 return false;
122         }
123         p += 21;
124         p++;
125         p += 15;
126         p++;
127         strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
128         if (!strupper_m(p)) {
129                 return false;
130         }
131         p += 16;
132         SSVAL(p, 0, CLI_BUFFER_SIZE);
133         p += 2;
134         SSVAL(p, 0, CLI_BUFFER_SIZE);
135         p += 2;
136
137         if (cli_api(cli,
138                     param, PTR_DIFF(p,param),1024,  /* param, length, max */
139                     NULL, 0, CLI_BUFFER_SIZE,           /* data, length, max */
140                     &rparam, &rprcnt,               /* return params, return size */
141                     &rdata, &rdrcnt                 /* return data, return size */
142                    )) {
143                 cli->rap_error = rparam? SVAL(rparam,0) : -1;
144                 p = rdata;
145
146                 if (cli->rap_error == 0) {
147                         DEBUG(4,("NetWkstaUserLogon success\n"));
148                         /*
149                          * The cli->privileges = SVAL(p, 24); field was set here
150                          * but it was not use anywhere else.
151                          */
152                         /* The cli->eff_name field used to be set here
153                            but it wasn't used anywhere else. */
154                 } else {
155                         DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
156                 }
157         }
158
159         SAFE_FREE(rparam);
160         SAFE_FREE(rdata);
161         return (cli->rap_error == 0);
162 }
163
164 /****************************************************************************
165  Call a NetShareEnum - try and browse available connections on a host.
166 ****************************************************************************/
167
168 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32_t, const char *, void *), void *state)
169 {
170         char *rparam = NULL;
171         char *rdata = NULL;
172         char *p;
173         unsigned int rdrcnt,rprcnt;
174         char param[1024];
175         int count = -1;
176
177         /* now send a SMBtrans command with api RNetShareEnum */
178         p = param;
179         SSVAL(p,0,0); /* api number */
180         p += 2;
181         strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
182         p = skip_string(param,sizeof(param),p);
183         strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
184         p = skip_string(param,sizeof(param),p);
185         SSVAL(p,0,1);
186         /*
187          * Win2k needs a *smaller* buffer than 0xFFFF here -
188          * it returns "out of server memory" with 0xFFFF !!! JRA.
189          */
190         SSVAL(p,2,0xFFE0);
191         p += 4;
192
193         if (cli_api(cli,
194                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
195                     NULL, 0, 0xFFE0,            /* data, length, maxlen - Win2k needs a small buffer here too ! */
196                     &rparam, &rprcnt,                /* return params, length */
197                     &rdata, &rdrcnt))                /* return data, length */
198                 {
199                         int res = rparam? SVAL(rparam,0) : -1;
200
201                         if (res == 0 || res == ERRmoredata) {
202                                 int converter=SVAL(rparam,2);
203                                 int i;
204                                 char *rdata_end = rdata + rdrcnt;
205
206                                 count=SVAL(rparam,4);
207                                 p = rdata;
208
209                                 for (i=0;i<count;i++,p+=20) {
210                                         char *sname;
211                                         int type;
212                                         int comment_offset;
213                                         const char *cmnt;
214                                         const char *p1;
215                                         char *s1, *s2;
216                                         size_t len;
217                                         TALLOC_CTX *frame = talloc_stackframe();
218
219                                         if (p + 20 > rdata_end) {
220                                                 TALLOC_FREE(frame);
221                                                 break;
222                                         }
223
224                                         sname = p;
225                                         type = SVAL(p,14);
226                                         comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
227                                         if (comment_offset < 0 ||
228                                                         comment_offset > (int)rdrcnt) {
229                                                 TALLOC_FREE(frame);
230                                                 break;
231                                         }
232                                         cmnt = comment_offset?(rdata+comment_offset):"";
233
234                                         /* Work out the comment length. */
235                                         for (p1 = cmnt, len = 0; *p1 &&
236                                                         p1 < rdata_end; len++)
237                                                 p1++;
238                                         if (!*p1) {
239                                                 len++;
240                                         }
241                                         pull_string_talloc(frame,rdata,0,
242                                                 &s1,sname,14,STR_ASCII);
243                                         pull_string_talloc(frame,rdata,0,
244                                                 &s2,cmnt,len,STR_ASCII);
245                                         if (!s1 || !s2) {
246                                                 TALLOC_FREE(frame);
247                                                 continue;
248                                         }
249
250                                         fn(s1, type, s2, state);
251
252                                         TALLOC_FREE(frame);
253                                 }
254                         } else {
255                                 DEBUG(4,("NetShareEnum res=%d\n", res));
256                         }
257                 } else {
258                         DEBUG(4,("NetShareEnum failed\n"));
259                 }
260
261         SAFE_FREE(rparam);
262         SAFE_FREE(rdata);
263
264         return count;
265 }
266
267 /****************************************************************************
268  Call a NetServerEnum for the specified workgroup and servertype mask.  This
269  function then calls the specified callback function for each name returned.
270
271  The callback function takes 4 arguments: the machine name, the server type,
272  the comment and a state pointer.
273 ****************************************************************************/
274
275 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32_t stype,
276                        void (*fn)(const char *, uint32_t, const char *, void *),
277                        void *state)
278 {
279         char *rparam = NULL;
280         char *rdata = NULL;
281         char *rdata_end = NULL;
282         unsigned int rdrcnt,rprcnt;
283         char *p;
284         char param[1024];
285         int uLevel = 1;
286         size_t len;
287         uint32_t func = RAP_NetServerEnum2;
288         char *last_entry = NULL;
289         int total_cnt = 0;
290         int return_cnt = 0;
291         int res;
292
293         errno = 0; /* reset */
294
295         /*
296          * This may take more than one transaction, so we should loop until
297          * we no longer get a more data to process or we have all of the
298          * items.
299          */
300         do {
301                 /* send a SMBtrans command with api NetServerEnum */
302                 p = param;
303                 SIVAL(p,0,func); /* api number */
304                 p += 2;
305
306                 if (func == RAP_NetServerEnum3) {
307                         strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
308                 } else {
309                         strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
310                 }
311
312                 p = skip_string(param, sizeof(param), p);
313                 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
314
315                 p = skip_string(param, sizeof(param), p);
316                 SSVAL(p,0,uLevel);
317                 SSVAL(p,2,CLI_BUFFER_SIZE);
318                 p += 4;
319                 SIVAL(p,0,stype);
320                 p += 4;
321
322                 /* If we have more data, tell the server where
323                  * to continue from.
324                  */
325                 len = push_ascii(p,
326                                 workgroup,
327                                 sizeof(param) - PTR_DIFF(p,param) - 1,
328                                 STR_TERMINATE|STR_UPPER);
329
330                 if (len == 0) {
331                         SAFE_FREE(last_entry);
332                         return false;
333                 }
334                 p += len;
335
336                 if (func == RAP_NetServerEnum3) {
337                         len = push_ascii(p,
338                                         last_entry ? last_entry : "",
339                                         sizeof(param) - PTR_DIFF(p,param) - 1,
340                                         STR_TERMINATE);
341
342                         if (len == 0) {
343                                 SAFE_FREE(last_entry);
344                                 return false;
345                         }
346                         p += len;
347                 }
348
349                 /* Next time through we need to use the continue api */
350                 func = RAP_NetServerEnum3;
351
352                 if (!cli_api(cli,
353                         param, PTR_DIFF(p,param), 8, /* params, length, max */
354                         NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
355                             &rparam, &rprcnt, /* return params, return size */
356                             &rdata, &rdrcnt)) { /* return data, return size */
357
358                         /* break out of the loop on error */
359                         res = -1;
360                         break;
361                 }
362
363                 rdata_end = rdata + rdrcnt;
364                 res = rparam ? SVAL(rparam,0) : -1;
365
366                 if (res == 0 || res == ERRmoredata ||
367                     (res != -1 && cli_errno(cli) == 0)) {
368                         char *sname = NULL;
369                         int i, count;
370                         int converter=SVAL(rparam,2);
371
372                         /* Get the number of items returned in this buffer */
373                         count = SVAL(rparam, 4);
374
375                         /* The next field contains the number of items left,
376                          * including those returned in this buffer. So the
377                          * first time through this should contain all of the
378                          * entries.
379                          */
380                         if (total_cnt == 0) {
381                                 total_cnt = SVAL(rparam, 6);
382                         }
383
384                         /* Keep track of how many we have read */
385                         return_cnt += count;
386                         p = rdata;
387
388                         /* The last name in the previous NetServerEnum reply is
389                          * sent back to server in the NetServerEnum3 request
390                          * (last_entry). The next reply should repeat this entry
391                          * as the first element. We have no proof that this is
392                          * always true, but from traces that seems to be the
393                          * behavior from Window Servers. So first lets do a lot
394                          * of checking, just being paranoid. If the string
395                          * matches then we already saw this entry so skip it.
396                          *
397                          * NOTE: sv1_name field must be null terminated and has
398                          * a max size of 16 (NetBIOS Name).
399                          */
400                         if (last_entry && count && p &&
401                                 (strncmp(last_entry, p, 16) == 0)) {
402                             count -= 1; /* Skip this entry */
403                             return_cnt = -1; /* Not part of total, so don't count. */
404                             p = rdata + 26; /* Skip the whole record */
405                         }
406
407                         for (i = 0; i < count; i++, p += 26) {
408                                 int comment_offset;
409                                 const char *cmnt;
410                                 const char *p1;
411                                 char *s1, *s2;
412                                 TALLOC_CTX *frame = talloc_stackframe();
413                                 uint32_t entry_stype;
414
415                                 if (p + 26 > rdata_end) {
416                                         TALLOC_FREE(frame);
417                                         break;
418                                 }
419
420                                 sname = p;
421                                 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
422                                 cmnt = comment_offset?(rdata+comment_offset):"";
423
424                                 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
425                                         TALLOC_FREE(frame);
426                                         continue;
427                                 }
428
429                                 /* Work out the comment length. */
430                                 for (p1 = cmnt, len = 0; *p1 &&
431                                                 p1 < rdata_end; len++)
432                                         p1++;
433                                 if (!*p1) {
434                                         len++;
435                                 }
436
437                                 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
438
439                                 pull_string_talloc(frame,rdata,0,
440                                         &s1,sname,16,STR_ASCII);
441                                 pull_string_talloc(frame,rdata,0,
442                                         &s2,cmnt,len,STR_ASCII);
443
444                                 if (!s1 || !s2) {
445                                         TALLOC_FREE(frame);
446                                         continue;
447                                 }
448
449                                 fn(s1, entry_stype, s2, state);
450                                 TALLOC_FREE(frame);
451                         }
452
453                         /* We are done with the old last entry, so now we can free it */
454                         if (last_entry) {
455                                 SAFE_FREE(last_entry); /* This will set it to null */
456                         }
457
458                         /* We always make a copy of  the last entry if we have one */
459                         if (sname) {
460                                 last_entry = smb_xstrdup(sname);
461                         }
462
463                         /* If we have more data, but no last entry then error out */
464                         if (!last_entry && (res == ERRmoredata)) {
465                                 errno = EINVAL;
466                                 res = 0;
467                         }
468
469                 }
470
471                 SAFE_FREE(rparam);
472                 SAFE_FREE(rdata);
473         } while ((res == ERRmoredata) && (total_cnt > return_cnt));
474
475         SAFE_FREE(rparam);
476         SAFE_FREE(rdata);
477         SAFE_FREE(last_entry);
478
479         if (res == -1) {
480                 errno = cli_errno(cli);
481         } else {
482                 if (!return_cnt) {
483                         /* this is a very special case, when the domain master for the
484                            work group isn't part of the work group itself, there is something
485                            wild going on */
486                         errno = ENOENT;
487                 }
488             }
489
490         return(return_cnt > 0);
491 }
492
493 /****************************************************************************
494  Send a SamOEMChangePassword command.
495 ****************************************************************************/
496
497 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
498                              const char *old_password)
499 {
500         char param[1024];
501         unsigned char data[532];
502         char *p = param;
503         unsigned char old_pw_hash[16];
504         unsigned char new_pw_hash[16];
505         unsigned int data_len;
506         unsigned int param_len = 0;
507         char *rparam = NULL;
508         char *rdata = NULL;
509         unsigned int rprcnt, rdrcnt;
510
511         if (strlen(user) >= sizeof(fstring)-1) {
512                 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
513                 return False;
514         }
515
516         SSVAL(p,0,214); /* SamOEMChangePassword command. */
517         p += 2;
518         strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
519         p = skip_string(param,sizeof(param),p);
520         strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
521         p = skip_string(param,sizeof(param),p);
522         strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
523         p = skip_string(param,sizeof(param),p);
524         SSVAL(p,0,532);
525         p += 2;
526
527         param_len = PTR_DIFF(p,param);
528
529         /*
530          * Get the Lanman hash of the old password, we
531          * use this as the key to make_oem_passwd_hash().
532          */
533         E_deshash(old_password, old_pw_hash);
534
535         encode_pw_buffer(data, new_password, STR_ASCII);
536
537 #ifdef DEBUG_PASSWORD
538         DEBUG(100,("make_oem_passwd_hash\n"));
539         dump_data(100, data, 516);
540 #endif
541         arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
542
543         /*
544          * Now place the old password hash in the data.
545          */
546         E_deshash(new_password, new_pw_hash);
547
548         E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
549
550         data_len = 532;
551
552         if (!cli_api(cli,
553                      param, param_len, 4,               /* param, length, max */
554                      (char *)data, data_len, 0,         /* data, length, max */
555                      &rparam, &rprcnt,
556                      &rdata, &rdrcnt)) {
557                 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
558                         user ));
559                 return False;
560         }
561
562         if (rparam) {
563                 cli->rap_error = SVAL(rparam,0);
564         }
565
566         SAFE_FREE(rparam);
567         SAFE_FREE(rdata);
568
569         return (cli->rap_error == 0);
570 }
571
572 /****************************************************************************
573  Send a qpathinfo call.
574 ****************************************************************************/
575
576 struct cli_qpathinfo1_state {
577         struct cli_state *cli;
578         uint32_t num_data;
579         uint8_t *data;
580 };
581
582 static void cli_qpathinfo1_done(struct tevent_req *subreq);
583
584 struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
585                                        struct tevent_context *ev,
586                                        struct cli_state *cli,
587                                        const char *fname)
588 {
589         struct tevent_req *req = NULL, *subreq = NULL;
590         struct cli_qpathinfo1_state *state = NULL;
591
592         req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo1_state);
593         if (req == NULL) {
594                 return NULL;
595         }
596         state->cli = cli;
597         subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_INFO_STANDARD,
598                                     22, CLI_BUFFER_SIZE);
599         if (tevent_req_nomem(subreq, req)) {
600                 return tevent_req_post(req, ev);
601         }
602         tevent_req_set_callback(subreq, cli_qpathinfo1_done, req);
603         return req;
604 }
605
606 static void cli_qpathinfo1_done(struct tevent_req *subreq)
607 {
608         struct tevent_req *req = tevent_req_callback_data(
609                 subreq, struct tevent_req);
610         struct cli_qpathinfo1_state *state = tevent_req_data(
611                 req, struct cli_qpathinfo1_state);
612         NTSTATUS status;
613
614         status = cli_qpathinfo_recv(subreq, state, &state->data,
615                                     &state->num_data);
616         TALLOC_FREE(subreq);
617         if (!NT_STATUS_IS_OK(status)) {
618                 tevent_req_nterror(req, status);
619                 return;
620         }
621         tevent_req_done(req);
622 }
623
624 NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
625                              time_t *change_time,
626                              time_t *access_time,
627                              time_t *write_time,
628                              off_t *size,
629                              uint16_t *mode)
630 {
631         struct cli_qpathinfo1_state *state = tevent_req_data(
632                 req, struct cli_qpathinfo1_state);
633         NTSTATUS status;
634
635         time_t (*date_fn)(const void *buf, int serverzone);
636
637         if (tevent_req_is_nterror(req, &status)) {
638                 return status;
639         }
640
641         if (state->cli->win95) {
642                 date_fn = make_unix_date;
643         } else {
644                 date_fn = make_unix_date2;
645         }
646
647         if (change_time) {
648                 *change_time = date_fn(state->data+0, smb1cli_conn_server_time_zone(state->cli->conn));
649         }
650         if (access_time) {
651                 *access_time = date_fn(state->data+4, smb1cli_conn_server_time_zone(state->cli->conn));
652         }
653         if (write_time) {
654                 *write_time = date_fn(state->data+8, smb1cli_conn_server_time_zone(state->cli->conn));
655         }
656         if (size) {
657                 *size = IVAL(state->data, 12);
658         }
659         if (mode) {
660                 *mode = SVAL(state->data, l1_attrFile);
661         }
662         return NT_STATUS_OK;
663 }
664
665 NTSTATUS cli_qpathinfo1(struct cli_state *cli,
666                         const char *fname,
667                         time_t *change_time,
668                         time_t *access_time,
669                         time_t *write_time,
670                         off_t *size,
671                         uint16_t *mode)
672 {
673         TALLOC_CTX *frame = talloc_stackframe();
674         struct tevent_context *ev;
675         struct tevent_req *req;
676         NTSTATUS status = NT_STATUS_NO_MEMORY;
677
678         if (smbXcli_conn_has_async_calls(cli->conn)) {
679                 /*
680                  * Can't use sync call while an async call is in flight
681                  */
682                 status = NT_STATUS_INVALID_PARAMETER;
683                 goto fail;
684         }
685         ev = samba_tevent_context_init(frame);
686         if (ev == NULL) {
687                 goto fail;
688         }
689         req = cli_qpathinfo1_send(frame, ev, cli, fname);
690         if (req == NULL) {
691                 goto fail;
692         }
693         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
694                 goto fail;
695         }
696         status = cli_qpathinfo1_recv(req, change_time, access_time,
697                                      write_time, size, mode);
698  fail:
699         TALLOC_FREE(frame);
700         return status;
701 }
702
703 /****************************************************************************
704  Send a setpathinfo call.
705 ****************************************************************************/
706
707 NTSTATUS cli_setpathinfo_basic(struct cli_state *cli, const char *fname,
708                                time_t create_time,
709                                time_t access_time,
710                                time_t write_time,
711                                time_t change_time,
712                                uint16_t mode)
713 {
714         unsigned int data_len = 0;
715         char data[40];
716         char *p;
717
718         p = data;
719
720         /*
721          * Add the create, last access, modification, and status change times
722          */
723         put_long_date(p, create_time);
724         p += 8;
725
726         put_long_date(p, access_time);
727         p += 8;
728
729         put_long_date(p, write_time);
730         p += 8;
731
732         put_long_date(p, change_time);
733         p += 8;
734
735         if (mode == (uint16_t)-1 || mode == FILE_ATTRIBUTE_NORMAL) {
736                 /* No change. */
737                 mode = 0;
738         } else if (mode == 0) {
739                 /* Clear all existing attributes. */
740                 mode = FILE_ATTRIBUTE_NORMAL;
741         }
742
743         /* Add attributes */
744         SIVAL(p, 0, mode);
745
746         p += 4;
747
748         /* Add padding */
749         SIVAL(p, 0, 0);
750         p += 4;
751
752         data_len = PTR_DIFF(p, data);
753
754         return cli_setpathinfo(cli, SMB_FILE_BASIC_INFORMATION, fname,
755                                (uint8_t *)data, data_len);
756 }
757
758 /****************************************************************************
759  Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
760 ****************************************************************************/
761
762 struct cli_qpathinfo2_state {
763         uint32_t num_data;
764         uint8_t *data;
765 };
766
767 static void cli_qpathinfo2_done(struct tevent_req *subreq);
768
769 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
770                                        struct tevent_context *ev,
771                                        struct cli_state *cli,
772                                        const char *fname)
773 {
774         struct tevent_req *req = NULL, *subreq = NULL;
775         struct cli_qpathinfo2_state *state = NULL;
776
777         req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
778         if (req == NULL) {
779                 return NULL;
780         }
781         subreq = cli_qpathinfo_send(state, ev, cli, fname,
782                                     SMB_QUERY_FILE_ALL_INFO,
783                                     68, CLI_BUFFER_SIZE);
784         if (tevent_req_nomem(subreq, req)) {
785                 return tevent_req_post(req, ev);
786         }
787         tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
788         return req;
789 }
790
791 static void cli_qpathinfo2_done(struct tevent_req *subreq)
792 {
793         struct tevent_req *req = tevent_req_callback_data(
794                 subreq, struct tevent_req);
795         struct cli_qpathinfo2_state *state = tevent_req_data(
796                 req, struct cli_qpathinfo2_state);
797         NTSTATUS status;
798
799         status = cli_qpathinfo_recv(subreq, state, &state->data,
800                                     &state->num_data);
801         TALLOC_FREE(subreq);
802         if (!NT_STATUS_IS_OK(status)) {
803                 tevent_req_nterror(req, status);
804                 return;
805         }
806         tevent_req_done(req);
807 }
808
809 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
810                              struct timespec *create_time,
811                              struct timespec *access_time,
812                              struct timespec *write_time,
813                              struct timespec *change_time,
814                              off_t *size, uint16_t *mode,
815                              SMB_INO_T *ino)
816 {
817         struct cli_qpathinfo2_state *state = tevent_req_data(
818                 req, struct cli_qpathinfo2_state);
819         NTSTATUS status;
820
821         if (tevent_req_is_nterror(req, &status)) {
822                 return status;
823         }
824
825         if (create_time) {
826                 *create_time = interpret_long_date((char *)state->data+0);
827         }
828         if (access_time) {
829                 *access_time = interpret_long_date((char *)state->data+8);
830         }
831         if (write_time) {
832                 *write_time = interpret_long_date((char *)state->data+16);
833         }
834         if (change_time) {
835                 *change_time = interpret_long_date((char *)state->data+24);
836         }
837         if (mode) {
838                 *mode = SVAL(state->data, 32);
839         }
840         if (size) {
841                 *size = IVAL2_TO_SMB_BIG_UINT(state->data,48);
842         }
843         if (ino) {
844                 *ino = IVAL(state->data, 64);
845         }
846         return NT_STATUS_OK;
847 }
848
849 NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
850                         struct timespec *create_time,
851                         struct timespec *access_time,
852                         struct timespec *write_time,
853                         struct timespec *change_time,
854                         off_t *size, uint16_t *mode,
855                         SMB_INO_T *ino)
856 {
857         TALLOC_CTX *frame = NULL;
858         struct tevent_context *ev;
859         struct tevent_req *req;
860         NTSTATUS status = NT_STATUS_NO_MEMORY;
861
862         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
863                 return cli_smb2_qpathinfo2(cli,
864                                         fname,
865                                         create_time,
866                                         access_time,
867                                         write_time,
868                                         change_time,
869                                         size,
870                                         mode,
871                                         ino);
872         }
873
874         frame = talloc_stackframe();
875
876         if (smbXcli_conn_has_async_calls(cli->conn)) {
877                 /*
878                  * Can't use sync call while an async call is in flight
879                  */
880                 status = NT_STATUS_INVALID_PARAMETER;
881                 goto fail;
882         }
883         ev = samba_tevent_context_init(frame);
884         if (ev == NULL) {
885                 goto fail;
886         }
887         req = cli_qpathinfo2_send(frame, ev, cli, fname);
888         if (req == NULL) {
889                 goto fail;
890         }
891         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
892                 goto fail;
893         }
894         status = cli_qpathinfo2_recv(req, create_time, access_time,
895                                      write_time, change_time, size, mode, ino);
896  fail:
897         TALLOC_FREE(frame);
898         return status;
899 }
900
901 /****************************************************************************
902  Get the stream info
903 ****************************************************************************/
904
905 struct cli_qpathinfo_streams_state {
906         uint32_t num_data;
907         uint8_t *data;
908 };
909
910 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
911
912 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
913                                               struct tevent_context *ev,
914                                               struct cli_state *cli,
915                                               const char *fname)
916 {
917         struct tevent_req *req = NULL, *subreq = NULL;
918         struct cli_qpathinfo_streams_state *state = NULL;
919
920         req = tevent_req_create(mem_ctx, &state,
921                                 struct cli_qpathinfo_streams_state);
922         if (req == NULL) {
923                 return NULL;
924         }
925         subreq = cli_qpathinfo_send(state, ev, cli, fname,
926                                     SMB_FILE_STREAM_INFORMATION,
927                                     0, CLI_BUFFER_SIZE);
928         if (tevent_req_nomem(subreq, req)) {
929                 return tevent_req_post(req, ev);
930         }
931         tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
932         return req;
933 }
934
935 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
936 {
937         struct tevent_req *req = tevent_req_callback_data(
938                 subreq, struct tevent_req);
939         struct cli_qpathinfo_streams_state *state = tevent_req_data(
940                 req, struct cli_qpathinfo_streams_state);
941         NTSTATUS status;
942
943         status = cli_qpathinfo_recv(subreq, state, &state->data,
944                                     &state->num_data);
945         TALLOC_FREE(subreq);
946         if (!NT_STATUS_IS_OK(status)) {
947                 tevent_req_nterror(req, status);
948                 return;
949         }
950         tevent_req_done(req);
951 }
952
953 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
954                                     TALLOC_CTX *mem_ctx,
955                                     unsigned int *pnum_streams,
956                                     struct stream_struct **pstreams)
957 {
958         struct cli_qpathinfo_streams_state *state = tevent_req_data(
959                 req, struct cli_qpathinfo_streams_state);
960         NTSTATUS status;
961
962         if (tevent_req_is_nterror(req, &status)) {
963                 return status;
964         }
965         if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
966                                 pnum_streams, pstreams)) {
967                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
968         }
969         return NT_STATUS_OK;
970 }
971
972 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
973                                TALLOC_CTX *mem_ctx,
974                                unsigned int *pnum_streams,
975                                struct stream_struct **pstreams)
976 {
977         TALLOC_CTX *frame = NULL;
978         struct tevent_context *ev;
979         struct tevent_req *req;
980         NTSTATUS status = NT_STATUS_NO_MEMORY;
981
982         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
983                 return cli_smb2_qpathinfo_streams(cli,
984                                         fname,
985                                         mem_ctx,
986                                         pnum_streams,
987                                         pstreams);
988         }
989
990         frame = talloc_stackframe();
991
992         if (smbXcli_conn_has_async_calls(cli->conn)) {
993                 /*
994                  * Can't use sync call while an async call is in flight
995                  */
996                 status = NT_STATUS_INVALID_PARAMETER;
997                 goto fail;
998         }
999         ev = samba_tevent_context_init(frame);
1000         if (ev == NULL) {
1001                 goto fail;
1002         }
1003         req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
1004         if (req == NULL) {
1005                 goto fail;
1006         }
1007         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1008                 goto fail;
1009         }
1010         status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1011                                             pstreams);
1012  fail:
1013         TALLOC_FREE(frame);
1014         return status;
1015 }
1016
1017 bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1018                                size_t data_len,
1019                                unsigned int *pnum_streams,
1020                                struct stream_struct **pstreams)
1021 {
1022         unsigned int num_streams;
1023         struct stream_struct *streams;
1024         unsigned int ofs;
1025
1026         num_streams = 0;
1027         streams = NULL;
1028         ofs = 0;
1029
1030         while ((data_len > ofs) && (data_len - ofs >= 24)) {
1031                 uint32_t nlen, len;
1032                 size_t size;
1033                 void *vstr;
1034                 struct stream_struct *tmp;
1035                 uint8_t *tmp_buf;
1036
1037                 tmp = talloc_realloc(mem_ctx, streams,
1038                                            struct stream_struct,
1039                                            num_streams+1);
1040
1041                 if (tmp == NULL) {
1042                         goto fail;
1043                 }
1044                 streams = tmp;
1045
1046                 nlen                      = IVAL(rdata, ofs + 0x04);
1047
1048                 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1049                         rdata, ofs + 0x08);
1050                 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1051                         rdata, ofs + 0x10);
1052
1053                 if (nlen > data_len - (ofs + 24)) {
1054                         goto fail;
1055                 }
1056
1057                 /*
1058                  * We need to null-terminate src, how do I do this with
1059                  * convert_string_talloc??
1060                  */
1061
1062                 tmp_buf = talloc_array(streams, uint8_t, nlen+2);
1063                 if (tmp_buf == NULL) {
1064                         goto fail;
1065                 }
1066
1067                 memcpy(tmp_buf, rdata+ofs+24, nlen);
1068                 tmp_buf[nlen] = 0;
1069                 tmp_buf[nlen+1] = 0;
1070
1071                 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1072                                            nlen+2, &vstr, &size))
1073                 {
1074                         TALLOC_FREE(tmp_buf);
1075                         goto fail;
1076                 }
1077
1078                 TALLOC_FREE(tmp_buf);
1079                 streams[num_streams].name = (char *)vstr;
1080                 num_streams++;
1081
1082                 len = IVAL(rdata, ofs);
1083                 if (len > data_len - ofs) {
1084                         goto fail;
1085                 }
1086                 if (len == 0) break;
1087                 ofs += len;
1088         }
1089
1090         *pnum_streams = num_streams;
1091         *pstreams = streams;
1092         return true;
1093
1094  fail:
1095         TALLOC_FREE(streams);
1096         return false;
1097 }
1098
1099 /****************************************************************************
1100  Send a qfileinfo QUERY_FILE_NAME_INFO call.
1101 ****************************************************************************/
1102
1103 NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum,
1104                        TALLOC_CTX *mem_ctx, char **_name)
1105 {
1106         uint16_t recv_flags2;
1107         uint8_t *rdata;
1108         uint32_t num_rdata;
1109         NTSTATUS status;
1110         char *name = NULL;
1111         uint32_t namelen;
1112
1113         status = cli_qfileinfo(talloc_tos(), cli, fnum,
1114                                SMB_QUERY_FILE_NAME_INFO,
1115                                4, CLI_BUFFER_SIZE, &recv_flags2,
1116                                &rdata, &num_rdata);
1117         if (!NT_STATUS_IS_OK(status)) {
1118                 return status;
1119         }
1120
1121         namelen = IVAL(rdata, 0);
1122         if (namelen > (num_rdata - 4)) {
1123                 TALLOC_FREE(rdata);
1124                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1125         }
1126
1127         clistr_pull_talloc(mem_ctx,
1128                            (const char *)rdata,
1129                            recv_flags2,
1130                            &name,
1131                            rdata + 4,
1132                            namelen,
1133                            STR_UNICODE);
1134         if (name == NULL) {
1135                 status = map_nt_error_from_unix(errno);
1136                 TALLOC_FREE(rdata);
1137                 return status;
1138         }
1139
1140         *_name = name;
1141         TALLOC_FREE(rdata);
1142         return NT_STATUS_OK;
1143 }
1144
1145 /****************************************************************************
1146  Send a qfileinfo call.
1147 ****************************************************************************/
1148
1149 NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
1150                              uint16_t *mode, off_t *size,
1151                              struct timespec *create_time,
1152                              struct timespec *access_time,
1153                              struct timespec *write_time,
1154                              struct timespec *change_time,
1155                              SMB_INO_T *ino)
1156 {
1157         uint8_t *rdata;
1158         uint32_t num_rdata;
1159         NTSTATUS status;
1160
1161         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1162                 return cli_smb2_qfileinfo_basic(cli,
1163                                                 fnum,
1164                                                 mode,
1165                                                 size,
1166                                                 create_time,
1167                                                 access_time,
1168                                                 write_time,
1169                                                 change_time,
1170                                                 ino);
1171         }
1172
1173         /* if its a win95 server then fail this - win95 totally screws it
1174            up */
1175         if (cli->win95) {
1176                 return NT_STATUS_NOT_SUPPORTED;
1177         }
1178
1179         status = cli_qfileinfo(talloc_tos(), cli, fnum,
1180                                SMB_QUERY_FILE_ALL_INFO,
1181                                68, CLI_BUFFER_SIZE,
1182                                NULL,
1183                                &rdata, &num_rdata);
1184         if (!NT_STATUS_IS_OK(status)) {
1185                 return status;
1186         }
1187
1188         if (create_time) {
1189                 *create_time = interpret_long_date((char *)rdata+0);
1190         }
1191         if (access_time) {
1192                 *access_time = interpret_long_date((char *)rdata+8);
1193         }
1194         if (write_time) {
1195                 *write_time = interpret_long_date((char *)rdata+16);
1196         }
1197         if (change_time) {
1198                 *change_time = interpret_long_date((char *)rdata+24);
1199         }
1200         if (mode) {
1201                 *mode = SVAL(rdata, 32);
1202         }
1203         if (size) {
1204                 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1205         }
1206         if (ino) {
1207                 *ino = IVAL(rdata, 64);
1208         }
1209
1210         TALLOC_FREE(rdata);
1211         return NT_STATUS_OK;
1212 }
1213
1214 /****************************************************************************
1215  Send a qpathinfo BASIC_INFO call.
1216 ****************************************************************************/
1217
1218 struct cli_qpathinfo_basic_state {
1219         uint32_t num_data;
1220         uint8_t *data;
1221 };
1222
1223 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1224
1225 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1226                                             struct tevent_context *ev,
1227                                             struct cli_state *cli,
1228                                             const char *fname)
1229 {
1230         struct tevent_req *req = NULL, *subreq = NULL;
1231         struct cli_qpathinfo_basic_state *state = NULL;
1232
1233         req = tevent_req_create(mem_ctx, &state,
1234                                 struct cli_qpathinfo_basic_state);
1235         if (req == NULL) {
1236                 return NULL;
1237         }
1238         subreq = cli_qpathinfo_send(state, ev, cli, fname,
1239                                     SMB_QUERY_FILE_BASIC_INFO,
1240                                     36, CLI_BUFFER_SIZE);
1241         if (tevent_req_nomem(subreq, req)) {
1242                 return tevent_req_post(req, ev);
1243         }
1244         tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1245         return req;
1246 }
1247
1248 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1249 {
1250         struct tevent_req *req = tevent_req_callback_data(
1251                 subreq, struct tevent_req);
1252         struct cli_qpathinfo_basic_state *state = tevent_req_data(
1253                 req, struct cli_qpathinfo_basic_state);
1254         NTSTATUS status;
1255
1256         status = cli_qpathinfo_recv(subreq, state, &state->data,
1257                                     &state->num_data);
1258         TALLOC_FREE(subreq);
1259         if (!NT_STATUS_IS_OK(status)) {
1260                 tevent_req_nterror(req, status);
1261                 return;
1262         }
1263         tevent_req_done(req);
1264 }
1265
1266 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1267                                   SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1268 {
1269         struct cli_qpathinfo_basic_state *state = tevent_req_data(
1270                 req, struct cli_qpathinfo_basic_state);
1271         NTSTATUS status;
1272
1273         if (tevent_req_is_nterror(req, &status)) {
1274                 return status;
1275         }
1276
1277         sbuf->st_ex_btime = interpret_long_date((char *)state->data);
1278         sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
1279         sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
1280         sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
1281         *attributes = IVAL(state->data, 32);
1282         return NT_STATUS_OK;
1283 }
1284
1285 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1286                              SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1287 {
1288         TALLOC_CTX *frame = NULL;
1289         struct tevent_context *ev;
1290         struct tevent_req *req;
1291         NTSTATUS status = NT_STATUS_NO_MEMORY;
1292
1293         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1294                 return cli_smb2_qpathinfo_basic(cli,
1295                                                 name,
1296                                                 sbuf,
1297                                                 attributes);
1298         }
1299
1300         frame = talloc_stackframe();
1301
1302         if (smbXcli_conn_has_async_calls(cli->conn)) {
1303                 /*
1304                  * Can't use sync call while an async call is in flight
1305                  */
1306                 status = NT_STATUS_INVALID_PARAMETER;
1307                 goto fail;
1308         }
1309         ev = samba_tevent_context_init(frame);
1310         if (ev == NULL) {
1311                 goto fail;
1312         }
1313         req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1314         if (req == NULL) {
1315                 goto fail;
1316         }
1317         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1318                 goto fail;
1319         }
1320         status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1321  fail:
1322         TALLOC_FREE(frame);
1323         return status;
1324 }
1325
1326 /****************************************************************************
1327  Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1328 ****************************************************************************/
1329
1330 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1331 {
1332         uint8_t *rdata;
1333         uint32_t num_rdata;
1334         unsigned int len;
1335         char *converted = NULL;
1336         size_t converted_size = 0;
1337         NTSTATUS status;
1338
1339         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1340                 return cli_smb2_qpathinfo_alt_name(cli,
1341                                                 fname,
1342                                                 alt_name);
1343         }
1344
1345         status = cli_qpathinfo(talloc_tos(), cli, fname,
1346                                SMB_QUERY_FILE_ALT_NAME_INFO,
1347                                4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1348         if (!NT_STATUS_IS_OK(status)) {
1349                 return status;
1350         }
1351
1352         len = IVAL(rdata, 0);
1353
1354         if (len > num_rdata - 4) {
1355                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1356         }
1357
1358         /* The returned data is a pushed string, not raw data. */
1359         if (!convert_string_talloc(talloc_tos(),
1360                                    smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
1361                                    CH_UNIX,
1362                                    rdata + 4,
1363                                    len,
1364                                    &converted,
1365                                    &converted_size)) {
1366                 return NT_STATUS_NO_MEMORY;
1367         }
1368         fstrcpy(alt_name, converted);
1369
1370         TALLOC_FREE(converted);
1371         TALLOC_FREE(rdata);
1372
1373         return NT_STATUS_OK;
1374 }
1375
1376 /****************************************************************************
1377  Send a qpathinfo SMB_QUERY_FILE_STADNDARD_INFO call.
1378 ****************************************************************************/
1379
1380 NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
1381                                 uint64_t *allocated, uint64_t *size,
1382                                 uint32_t *nlinks,
1383                                 bool *is_del_pending, bool *is_dir)
1384 {
1385         uint8_t *rdata;
1386         uint32_t num_rdata;
1387         NTSTATUS status;
1388
1389         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1390                 return NT_STATUS_NOT_IMPLEMENTED;
1391         }
1392
1393         status = cli_qpathinfo(talloc_tos(), cli, fname,
1394                                SMB_QUERY_FILE_STANDARD_INFO,
1395                                24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1396         if (!NT_STATUS_IS_OK(status)) {
1397                 return status;
1398         }
1399
1400         if (allocated) {
1401                 *allocated = BVAL(rdata, 0);
1402         }
1403
1404         if (size) {
1405                 *size = BVAL(rdata, 8);
1406         }
1407
1408         if (nlinks) {
1409                 *nlinks = IVAL(rdata, 16);
1410         }
1411
1412         if (is_del_pending) {
1413                 *is_del_pending = CVAL(rdata, 20);
1414         }
1415
1416         if (is_dir) {
1417                 *is_dir = CVAL(rdata, 20);
1418         }
1419
1420         TALLOC_FREE(rdata);
1421
1422         return NT_STATUS_OK;
1423 }
1424
1425
1426 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1427 NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
1428                         struct timespec *create_time,
1429                         struct timespec *access_time,
1430                         struct timespec *write_time,
1431                         struct timespec *change_time,
1432                         off_t *size, uint16_t *mode,
1433                         SMB_INO_T *ino)
1434 {
1435         NTSTATUS status = NT_STATUS_OK;
1436         SMB_STRUCT_STAT st = { 0 };
1437         uint32_t attr;
1438         uint64_t pos;
1439
1440         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1441                 return cli_qpathinfo2(cli, fname,
1442                                       create_time, access_time, write_time, change_time,
1443                                       size, mode, ino);
1444         }
1445
1446         if (create_time || access_time || write_time || change_time || mode) {
1447                 status = cli_qpathinfo_basic(cli, fname, &st, &attr);
1448                 if (!NT_STATUS_IS_OK(status)) {
1449                         return status;
1450                 }
1451         }
1452
1453         if (size) {
1454                 status = cli_qpathinfo_standard(cli, fname,
1455                                                 NULL, &pos, NULL, NULL, NULL);
1456                 if (!NT_STATUS_IS_OK(status)) {
1457                         return status;
1458                 }
1459
1460                 *size = pos;
1461         }
1462
1463         if (create_time) {
1464                 *create_time = st.st_ex_btime;
1465         }
1466         if (access_time) {
1467                 *access_time = st.st_ex_atime;
1468         }
1469         if (write_time) {
1470                 *write_time = st.st_ex_mtime;
1471         }
1472         if (change_time) {
1473                 *change_time = st.st_ex_ctime;
1474         }
1475         if (mode) {
1476                 *mode = attr;
1477         }
1478         if (ino) {
1479                 *ino = 0;
1480         }
1481
1482         return NT_STATUS_OK;
1483 }