smb1cli_conn_set_encryption talloc move s3-libsmb: Convert struct smb_trans_enc_state...
[metze/samba/wip.git] / libcli / smb / smbXcli_base.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async SMB client requests
4    Copyright (C) Volker Lendecke 2008
5    Copyright (C) Stefan Metzmacher 2011
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
34
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
38
39 struct smbXcli_conn {
40         int read_fd;
41         int write_fd;
42         struct sockaddr_storage local_ss;
43         struct sockaddr_storage remote_ss;
44         const char *remote_name;
45
46         struct tevent_queue *outgoing;
47         struct tevent_req **pending;
48         struct tevent_req *read_smb_req;
49
50         enum protocol_types protocol;
51         bool allow_signing;
52         bool desire_signing;
53         bool mandatory_signing;
54
55         /*
56          * The incoming dispatch function should return:
57          * - NT_STATUS_RETRY, if more incoming PDUs are expected.
58          * - NT_STATUS_OK, if no more processing is desired, e.g.
59          *                 the dispatch function called
60          *                 tevent_req_done().
61          * - All other return values disconnect the connection.
62          */
63         NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
64                                       TALLOC_CTX *tmp_mem,
65                                       uint8_t *inbuf);
66
67         struct {
68                 struct {
69                         uint32_t capabilities;
70                         uint32_t max_xmit;
71                 } client;
72
73                 struct {
74                         uint32_t capabilities;
75                         uint32_t max_xmit;
76                         uint16_t max_mux;
77                         uint16_t security_mode;
78                         bool readbraw;
79                         bool writebraw;
80                         bool lockread;
81                         bool writeunlock;
82                         uint32_t session_key;
83                         struct GUID guid;
84                         DATA_BLOB gss_blob;
85                         uint8_t challenge[8];
86                         const char *workgroup;
87                         const char *name;
88                         int time_zone;
89                         NTTIME system_time;
90                 } server;
91
92                 uint32_t capabilities;
93                 uint32_t max_xmit;
94
95                 uint16_t mid;
96
97                 struct smb_signing_state *signing;
98                 struct smb_trans_enc_state *trans_enc;
99
100                 struct tevent_req *read_braw_req;
101         } smb1;
102
103         struct {
104                 struct {
105                         uint32_t capabilities;
106                         uint16_t security_mode;
107                         struct GUID guid;
108                 } client;
109
110                 struct {
111                         uint32_t capabilities;
112                         uint16_t security_mode;
113                         struct GUID guid;
114                         uint32_t max_trans_size;
115                         uint32_t max_read_size;
116                         uint32_t max_write_size;
117                         NTTIME system_time;
118                         NTTIME start_time;
119                         DATA_BLOB gss_blob;
120                 } server;
121
122                 uint64_t mid;
123                 uint16_t cur_credits;
124                 uint16_t max_credits;
125         } smb2;
126
127         struct smbXcli_session *sessions;
128 };
129
130 struct smbXcli_session {
131         struct smbXcli_session *prev, *next;
132         struct smbXcli_conn *conn;
133
134         struct {
135                 uint64_t session_id;
136                 uint16_t session_flags;
137                 DATA_BLOB application_key;
138                 DATA_BLOB signing_key;
139                 bool should_sign;
140                 DATA_BLOB channel_signing_key;
141         } smb2;
142 };
143
144 struct smbXcli_req_state {
145         struct tevent_context *ev;
146         struct smbXcli_conn *conn;
147         struct smbXcli_session *session; /* maybe NULL */
148
149         uint8_t length_hdr[4];
150
151         bool one_way;
152
153         uint8_t *inbuf;
154
155         struct {
156                 /* Space for the header including the wct */
157                 uint8_t hdr[HDR_VWV];
158
159                 /*
160                  * For normal requests, smb1cli_req_send chooses a mid.
161                  * SecondaryV trans requests need to use the mid of the primary
162                  * request, so we need a place to store it.
163                  * Assume it is set if != 0.
164                  */
165                 uint16_t mid;
166
167                 uint16_t *vwv;
168                 uint8_t bytecount_buf[2];
169
170 #define MAX_SMB_IOV 10
171                 /* length_hdr, hdr, words, byte_count, buffers */
172                 struct iovec iov[1 + 3 + MAX_SMB_IOV];
173                 int iov_count;
174
175                 bool one_way_seqnum;
176                 uint32_t seqnum;
177                 struct tevent_req **chained_requests;
178
179                 uint8_t recv_cmd;
180                 NTSTATUS recv_status;
181                 /* always an array of 3 talloc elements */
182                 struct iovec *recv_iov;
183         } smb1;
184
185         struct {
186                 const uint8_t *fixed;
187                 uint16_t fixed_len;
188                 const uint8_t *dyn;
189                 uint32_t dyn_len;
190
191                 uint8_t hdr[64];
192                 uint8_t pad[7]; /* padding space for compounding */
193
194                 /* always an array of 3 talloc elements */
195                 struct iovec *recv_iov;
196
197                 uint16_t credit_charge;
198
199                 bool signing_skipped;
200                 bool notify_async;
201                 bool got_async;
202         } smb2;
203 };
204
205 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
206 {
207         /*
208          * NT_STATUS_OK, means we do not notify the callers
209          */
210         smbXcli_conn_disconnect(conn, NT_STATUS_OK);
211
212         while (conn->sessions) {
213                 conn->sessions->conn = NULL;
214                 DLIST_REMOVE(conn->sessions, conn->sessions);
215         }
216
217 <<<<<<< HEAD
218         if (conn->smb1.trans_enc) {
219                 TALLOC_FREE(conn->smb1.trans_enc);
220         }
221
222 =======
223 >>>>>>> 7efc635... s3-libsmb: Convert struct smb_trans_enc_state to talloc
224         return 0;
225 }
226
227 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
228                                          int fd,
229                                          const char *remote_name,
230                                          enum smb_signing_setting signing_state,
231                                          uint32_t smb1_capabilities,
232                                          struct GUID *client_guid,
233                                          uint32_t smb2_capabilities)
234 {
235         struct smbXcli_conn *conn = NULL;
236         void *ss = NULL;
237         struct sockaddr *sa = NULL;
238         socklen_t sa_length;
239         int ret;
240
241         conn = talloc_zero(mem_ctx, struct smbXcli_conn);
242         if (!conn) {
243                 return NULL;
244         }
245
246         conn->read_fd = fd;
247         conn->write_fd = dup(fd);
248         if (conn->write_fd == -1) {
249                 goto error;
250         }
251
252         conn->remote_name = talloc_strdup(conn, remote_name);
253         if (conn->remote_name == NULL) {
254                 goto error;
255         }
256
257
258         ss = (void *)&conn->local_ss;
259         sa = (struct sockaddr *)ss;
260         sa_length = sizeof(conn->local_ss);
261         ret = getsockname(fd, sa, &sa_length);
262         if (ret == -1) {
263                 goto error;
264         }
265         ss = (void *)&conn->remote_ss;
266         sa = (struct sockaddr *)ss;
267         sa_length = sizeof(conn->remote_ss);
268         ret = getpeername(fd, sa, &sa_length);
269         if (ret == -1) {
270                 goto error;
271         }
272
273         conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
274         if (conn->outgoing == NULL) {
275                 goto error;
276         }
277         conn->pending = NULL;
278
279         conn->protocol = PROTOCOL_NONE;
280
281         switch (signing_state) {
282         case SMB_SIGNING_OFF:
283                 /* never */
284                 conn->allow_signing = false;
285                 conn->desire_signing = false;
286                 conn->mandatory_signing = false;
287                 break;
288         case SMB_SIGNING_DEFAULT:
289         case SMB_SIGNING_IF_REQUIRED:
290                 /* if the server requires it */
291                 conn->allow_signing = true;
292                 conn->desire_signing = false;
293                 conn->mandatory_signing = false;
294                 break;
295         case SMB_SIGNING_REQUIRED:
296                 /* always */
297                 conn->allow_signing = true;
298                 conn->desire_signing = true;
299                 conn->mandatory_signing = true;
300                 break;
301         }
302
303         conn->smb1.client.capabilities = smb1_capabilities;
304         conn->smb1.client.max_xmit = UINT16_MAX;
305
306         conn->smb1.capabilities = conn->smb1.client.capabilities;
307         conn->smb1.max_xmit = 1024;
308
309         conn->smb1.mid = 1;
310
311         /* initialise signing */
312         conn->smb1.signing = smb_signing_init(conn,
313                                               conn->allow_signing,
314                                               conn->desire_signing,
315                                               conn->mandatory_signing);
316         if (!conn->smb1.signing) {
317                 goto error;
318         }
319
320         conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
321         if (conn->mandatory_signing) {
322                 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
323         }
324         if (client_guid) {
325                 conn->smb2.client.guid = *client_guid;
326         }
327         conn->smb2.client.capabilities = smb2_capabilities;
328
329         conn->smb2.cur_credits = 1;
330         conn->smb2.max_credits = 0;
331
332         talloc_set_destructor(conn, smbXcli_conn_destructor);
333         return conn;
334
335  error:
336         if (conn->write_fd != -1) {
337                 close(conn->write_fd);
338         }
339         TALLOC_FREE(conn);
340         return NULL;
341 }
342
343 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
344 {
345         if (conn == NULL) {
346                 return false;
347         }
348
349         if (conn->read_fd == -1) {
350                 return false;
351         }
352
353         return true;
354 }
355
356 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
357 {
358         return conn->protocol;
359 }
360
361 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
362 {
363         if (conn->protocol >= PROTOCOL_SMB2_02) {
364                 return true;
365         }
366
367         if (conn->smb1.capabilities & CAP_UNICODE) {
368                 return true;
369         }
370
371         return false;
372 }
373
374 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
375 {
376         set_socket_options(conn->read_fd, options);
377 }
378
379 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
380 {
381         return &conn->local_ss;
382 }
383
384 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
385 {
386         return &conn->remote_ss;
387 }
388
389 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
390 {
391         return conn->remote_name;
392 }
393
394 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
395 {
396         if (conn->protocol >= PROTOCOL_SMB2_02) {
397                 /*
398                  * TODO...
399                  */
400                 return 1;
401         }
402
403         return conn->smb1.server.max_mux;
404 }
405
406 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
407 {
408         if (conn->protocol >= PROTOCOL_SMB2_02) {
409                 return conn->smb2.server.system_time;
410         }
411
412         return conn->smb1.server.system_time;
413 }
414
415 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
416 {
417         if (conn->protocol >= PROTOCOL_SMB2_02) {
418                 return &conn->smb2.server.gss_blob;
419         }
420
421         return &conn->smb1.server.gss_blob;
422 }
423
424 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
425 {
426         if (conn->protocol >= PROTOCOL_SMB2_02) {
427                 return &conn->smb2.server.guid;
428         }
429
430         return &conn->smb1.server.guid;
431 }
432
433 struct smbXcli_conn_samba_suicide_state {
434         struct smbXcli_conn *conn;
435         struct iovec iov;
436         uint8_t buf[9];
437 };
438
439 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
440
441 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
442                                                    struct tevent_context *ev,
443                                                    struct smbXcli_conn *conn,
444                                                    uint8_t exitcode)
445 {
446         struct tevent_req *req, *subreq;
447         struct smbXcli_conn_samba_suicide_state *state;
448
449         req = tevent_req_create(mem_ctx, &state,
450                                 struct smbXcli_conn_samba_suicide_state);
451         if (req == NULL) {
452                 return NULL;
453         }
454         state->conn = conn;
455         SIVAL(state->buf, 4, 0x74697865);
456         SCVAL(state->buf, 8, exitcode);
457         _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
458
459         state->iov.iov_base = state->buf;
460         state->iov.iov_len = sizeof(state->buf);
461
462         subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
463                              false, &state->iov, 1);
464         if (tevent_req_nomem(subreq, req)) {
465                 return tevent_req_post(req, ev);
466         }
467         tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
468         return req;
469 }
470
471 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
472 {
473         struct tevent_req *req = tevent_req_callback_data(
474                 subreq, struct tevent_req);
475         struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
476                 req, struct smbXcli_conn_samba_suicide_state);
477         ssize_t nwritten;
478         int err;
479
480         nwritten = writev_recv(subreq, &err);
481         TALLOC_FREE(subreq);
482         if (nwritten == -1) {
483                 NTSTATUS status = map_nt_error_from_unix_common(err);
484                 smbXcli_conn_disconnect(state->conn, status);
485                 return;
486         }
487         tevent_req_done(req);
488 }
489
490 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
491 {
492         return tevent_req_simple_recv_ntstatus(req);
493 }
494
495 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
496                                     uint8_t exitcode)
497 {
498         TALLOC_CTX *frame = talloc_stackframe();
499         struct tevent_context *ev;
500         struct tevent_req *req;
501         NTSTATUS status = NT_STATUS_NO_MEMORY;
502         bool ok;
503
504         if (smbXcli_conn_has_async_calls(conn)) {
505                 /*
506                  * Can't use sync call while an async call is in flight
507                  */
508                 status = NT_STATUS_INVALID_PARAMETER_MIX;
509                 goto fail;
510         }
511         ev = tevent_context_init(frame);
512         if (ev == NULL) {
513                 goto fail;
514         }
515         req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
516         if (req == NULL) {
517                 goto fail;
518         }
519         ok = tevent_req_poll(req, ev);
520         if (!ok) {
521                 status = map_nt_error_from_unix_common(errno);
522                 goto fail;
523         }
524         status = smbXcli_conn_samba_suicide_recv(req);
525  fail:
526         TALLOC_FREE(frame);
527         return status;
528 }
529
530 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
531 {
532         return conn->smb1.capabilities;
533 }
534
535 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
536 {
537         return conn->smb1.max_xmit;
538 }
539
540 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
541 {
542         return conn->smb1.server.session_key;
543 }
544
545 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
546 {
547         return conn->smb1.server.challenge;
548 }
549
550 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
551 {
552         return conn->smb1.server.security_mode;
553 }
554
555 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
556 {
557         return conn->smb1.server.readbraw;
558 }
559
560 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
561 {
562         return conn->smb1.server.writebraw;
563 }
564
565 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
566 {
567         return conn->smb1.server.lockread;
568 }
569
570 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
571 {
572         return conn->smb1.server.writeunlock;
573 }
574
575 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
576 {
577         return conn->smb1.server.time_zone;
578 }
579
580 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
581                                    const DATA_BLOB user_session_key,
582                                    const DATA_BLOB response)
583 {
584         return smb_signing_activate(conn->smb1.signing,
585                                     user_session_key,
586                                     response);
587 }
588
589 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
590                                 const uint8_t *buf, uint32_t seqnum)
591 {
592         return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
593 }
594
595 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
596 {
597         return smb_signing_is_active(conn->smb1.signing);
598 }
599
600 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
601                                  struct smb_trans_enc_state **es)
602 {
603         /* Replace the old state, if any. */
604 <<<<<<< HEAD
605         if (conn->smb1.trans_enc) {
606                 TALLOC_FREE(conn->smb1.trans_enc);
607         }
608         conn->smb1.trans_enc = es;
609 =======
610         TALLOC_FREE(conn->smb1.trans_enc);
611         conn->smb1.trans_enc = talloc_move(conn, es);
612 >>>>>>> 7efc635... s3-libsmb: Convert struct smb_trans_enc_state to talloc
613 }
614
615 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
616 {
617         return common_encryption_on(conn->smb1.trans_enc);
618 }
619
620
621 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
622 {
623         uint32_t flags2 = SVAL(hdr, HDR_FLG2);
624         NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
625
626         if (NT_STATUS_IS_OK(status)) {
627                 return NT_STATUS_OK;
628         }
629
630         if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
631                 return status;
632         }
633
634         return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
635 }
636
637 /**
638  * Is the SMB command able to hold an AND_X successor
639  * @param[in] cmd       The SMB command in question
640  * @retval Can we add a chained request after "cmd"?
641  */
642 bool smb1cli_is_andx_req(uint8_t cmd)
643 {
644         switch (cmd) {
645         case SMBtconX:
646         case SMBlockingX:
647         case SMBopenX:
648         case SMBreadX:
649         case SMBwriteX:
650         case SMBsesssetupX:
651         case SMBulogoffX:
652         case SMBntcreateX:
653                 return true;
654                 break;
655         default:
656                 break;
657         }
658
659         return false;
660 }
661
662 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
663 {
664         size_t num_pending = talloc_array_length(conn->pending);
665         uint16_t result;
666
667         while (true) {
668                 size_t i;
669
670                 result = conn->smb1.mid++;
671                 if ((result == 0) || (result == 0xffff)) {
672                         continue;
673                 }
674
675                 for (i=0; i<num_pending; i++) {
676                         if (result == smb1cli_req_mid(conn->pending[i])) {
677                                 break;
678                         }
679                 }
680
681                 if (i == num_pending) {
682                         return result;
683                 }
684         }
685 }
686
687 void smbXcli_req_unset_pending(struct tevent_req *req)
688 {
689         struct smbXcli_req_state *state =
690                 tevent_req_data(req,
691                 struct smbXcli_req_state);
692         struct smbXcli_conn *conn = state->conn;
693         size_t num_pending = talloc_array_length(conn->pending);
694         size_t i;
695
696         if (state->smb1.mid != 0) {
697                 /*
698                  * This is a [nt]trans[2] request which waits
699                  * for more than one reply.
700                  */
701                 return;
702         }
703
704         talloc_set_destructor(req, NULL);
705
706         if (num_pending == 1) {
707                 /*
708                  * The pending read_smb tevent_req is a child of
709                  * conn->pending. So if nothing is pending anymore, we need to
710                  * delete the socket read fde.
711                  */
712                 TALLOC_FREE(conn->pending);
713                 conn->read_smb_req = NULL;
714                 return;
715         }
716
717         for (i=0; i<num_pending; i++) {
718                 if (req == conn->pending[i]) {
719                         break;
720                 }
721         }
722         if (i == num_pending) {
723                 /*
724                  * Something's seriously broken. Just returning here is the
725                  * right thing nevertheless, the point of this routine is to
726                  * remove ourselves from conn->pending.
727                  */
728                 return;
729         }
730
731         /*
732          * Remove ourselves from the conn->pending array
733          */
734         for (; i < (num_pending - 1); i++) {
735                 conn->pending[i] = conn->pending[i+1];
736         }
737
738         /*
739          * No NULL check here, we're shrinking by sizeof(void *), and
740          * talloc_realloc just adjusts the size for this.
741          */
742         conn->pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
743                                        num_pending - 1);
744         return;
745 }
746
747 static int smbXcli_req_destructor(struct tevent_req *req)
748 {
749         struct smbXcli_req_state *state =
750                 tevent_req_data(req,
751                 struct smbXcli_req_state);
752
753         /*
754          * Make sure we really remove it from
755          * the pending array on destruction.
756          */
757         state->smb1.mid = 0;
758         smbXcli_req_unset_pending(req);
759         return 0;
760 }
761
762 static bool smb1cli_req_cancel(struct tevent_req *req);
763 static bool smb2cli_req_cancel(struct tevent_req *req);
764
765 static bool smbXcli_req_cancel(struct tevent_req *req)
766 {
767         struct smbXcli_req_state *state =
768                 tevent_req_data(req,
769                 struct smbXcli_req_state);
770
771         if (!smbXcli_conn_is_connected(state->conn)) {
772                 return false;
773         }
774
775         if (state->conn->protocol == PROTOCOL_NONE) {
776                 return false;
777         }
778
779         if (state->conn->protocol >= PROTOCOL_SMB2_02) {
780                 return smb2cli_req_cancel(req);
781         }
782
783         return smb1cli_req_cancel(req);
784 }
785
786 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
787
788 bool smbXcli_req_set_pending(struct tevent_req *req)
789 {
790         struct smbXcli_req_state *state =
791                 tevent_req_data(req,
792                 struct smbXcli_req_state);
793         struct smbXcli_conn *conn;
794         struct tevent_req **pending;
795         size_t num_pending;
796
797         conn = state->conn;
798
799         if (!smbXcli_conn_is_connected(conn)) {
800                 return false;
801         }
802
803         num_pending = talloc_array_length(conn->pending);
804
805         pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
806                                  num_pending+1);
807         if (pending == NULL) {
808                 return false;
809         }
810         pending[num_pending] = req;
811         conn->pending = pending;
812         talloc_set_destructor(req, smbXcli_req_destructor);
813         tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
814
815         if (!smbXcli_conn_receive_next(conn)) {
816                 /*
817                  * the caller should notify the current request
818                  *
819                  * And all other pending requests get notified
820                  * by smbXcli_conn_disconnect().
821                  */
822                 smbXcli_req_unset_pending(req);
823                 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
824                 return false;
825         }
826
827         return true;
828 }
829
830 static void smbXcli_conn_received(struct tevent_req *subreq);
831
832 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
833 {
834         size_t num_pending = talloc_array_length(conn->pending);
835         struct tevent_req *req;
836         struct smbXcli_req_state *state;
837
838         if (conn->read_smb_req != NULL) {
839                 return true;
840         }
841
842         if (num_pending == 0) {
843                 if (conn->smb2.mid < UINT64_MAX) {
844                         /* no more pending requests, so we are done for now */
845                         return true;
846                 }
847
848                 /*
849                  * If there are no more SMB2 requests possible,
850                  * because we are out of message ids,
851                  * we need to disconnect.
852                  */
853                 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
854                 return true;
855         }
856
857         req = conn->pending[0];
858         state = tevent_req_data(req, struct smbXcli_req_state);
859
860         /*
861          * We're the first ones, add the read_smb request that waits for the
862          * answer from the server
863          */
864         conn->read_smb_req = read_smb_send(conn->pending,
865                                            state->ev,
866                                            conn->read_fd);
867         if (conn->read_smb_req == NULL) {
868                 return false;
869         }
870         tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
871         return true;
872 }
873
874 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
875 {
876         tevent_queue_stop(conn->outgoing);
877
878         if (conn->read_fd != -1) {
879                 close(conn->read_fd);
880         }
881         if (conn->write_fd != -1) {
882                 close(conn->write_fd);
883         }
884         conn->read_fd = -1;
885         conn->write_fd = -1;
886
887         /*
888          * Cancel all pending requests. We do not do a for-loop walking
889          * conn->pending because that array changes in
890          * smbXcli_req_unset_pending.
891          */
892         while (talloc_array_length(conn->pending) > 0) {
893                 struct tevent_req *req;
894                 struct smbXcli_req_state *state;
895                 struct tevent_req **chain;
896                 size_t num_chained;
897                 size_t i;
898
899                 req = conn->pending[0];
900                 state = tevent_req_data(req, struct smbXcli_req_state);
901
902                 if (state->smb1.chained_requests == NULL) {
903                         /*
904                          * We're dead. No point waiting for trans2
905                          * replies.
906                          */
907                         state->smb1.mid = 0;
908
909                         smbXcli_req_unset_pending(req);
910
911                         if (NT_STATUS_IS_OK(status)) {
912                                 /* do not notify the callers */
913                                 continue;
914                         }
915
916                         /*
917                          * we need to defer the callback, because we may notify
918                          * more then one caller.
919                          */
920                         tevent_req_defer_callback(req, state->ev);
921                         tevent_req_nterror(req, status);
922                         continue;
923                 }
924
925                 chain = talloc_move(conn, &state->smb1.chained_requests);
926                 num_chained = talloc_array_length(chain);
927
928                 for (i=0; i<num_chained; i++) {
929                         req = chain[i];
930                         state = tevent_req_data(req, struct smbXcli_req_state);
931
932                         /*
933                          * We're dead. No point waiting for trans2
934                          * replies.
935                          */
936                         state->smb1.mid = 0;
937
938                         smbXcli_req_unset_pending(req);
939
940                         if (NT_STATUS_IS_OK(status)) {
941                                 /* do not notify the callers */
942                                 continue;
943                         }
944
945                         /*
946                          * we need to defer the callback, because we may notify
947                          * more than one caller.
948                          */
949                         tevent_req_defer_callback(req, state->ev);
950                         tevent_req_nterror(req, status);
951                 }
952                 TALLOC_FREE(chain);
953         }
954 }
955
956 /*
957  * Fetch a smb request's mid. Only valid after the request has been sent by
958  * smb1cli_req_send().
959  */
960 uint16_t smb1cli_req_mid(struct tevent_req *req)
961 {
962         struct smbXcli_req_state *state =
963                 tevent_req_data(req,
964                 struct smbXcli_req_state);
965
966         if (state->smb1.mid != 0) {
967                 return state->smb1.mid;
968         }
969
970         return SVAL(state->smb1.hdr, HDR_MID);
971 }
972
973 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
974 {
975         struct smbXcli_req_state *state =
976                 tevent_req_data(req,
977                 struct smbXcli_req_state);
978
979         state->smb1.mid = mid;
980 }
981
982 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
983 {
984         struct smbXcli_req_state *state =
985                 tevent_req_data(req,
986                 struct smbXcli_req_state);
987
988         return state->smb1.seqnum;
989 }
990
991 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
992 {
993         struct smbXcli_req_state *state =
994                 tevent_req_data(req,
995                 struct smbXcli_req_state);
996
997         state->smb1.seqnum = seqnum;
998 }
999
1000 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1001 {
1002         size_t result = 0;
1003         int i;
1004         for (i=0; i<count; i++) {
1005                 result += iov[i].iov_len;
1006         }
1007         return result;
1008 }
1009
1010 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1011                                    const struct iovec *iov,
1012                                    int count)
1013 {
1014         size_t len = smbXcli_iov_len(iov, count);
1015         size_t copied;
1016         uint8_t *buf;
1017         int i;
1018
1019         buf = talloc_array(mem_ctx, uint8_t, len);
1020         if (buf == NULL) {
1021                 return NULL;
1022         }
1023         copied = 0;
1024         for (i=0; i<count; i++) {
1025                 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1026                 copied += iov[i].iov_len;
1027         }
1028         return buf;
1029 }
1030
1031 static void smb1cli_req_flags(enum protocol_types protocol,
1032                               uint32_t smb1_capabilities,
1033                               uint8_t smb_command,
1034                               uint8_t additional_flags,
1035                               uint8_t clear_flags,
1036                               uint8_t *_flags,
1037                               uint16_t additional_flags2,
1038                               uint16_t clear_flags2,
1039                               uint16_t *_flags2)
1040 {
1041         uint8_t flags = 0;
1042         uint16_t flags2 = 0;
1043
1044         if (protocol >= PROTOCOL_LANMAN1) {
1045                 flags |= FLAG_CASELESS_PATHNAMES;
1046                 flags |= FLAG_CANONICAL_PATHNAMES;
1047         }
1048
1049         if (protocol >= PROTOCOL_LANMAN2) {
1050                 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1051                 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1052         }
1053
1054         if (protocol >= PROTOCOL_NT1) {
1055                 flags2 |= FLAGS2_IS_LONG_NAME;
1056
1057                 if (smb1_capabilities & CAP_UNICODE) {
1058                         flags2 |= FLAGS2_UNICODE_STRINGS;
1059                 }
1060                 if (smb1_capabilities & CAP_STATUS32) {
1061                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1062                 }
1063                 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1064                         flags2 |= FLAGS2_EXTENDED_SECURITY;
1065                 }
1066         }
1067
1068         flags |= additional_flags;
1069         flags &= ~clear_flags;
1070         flags2 |= additional_flags2;
1071         flags2 &= ~clear_flags2;
1072
1073         *_flags = flags;
1074         *_flags2 = flags2;
1075 }
1076
1077 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1078
1079 static bool smb1cli_req_cancel(struct tevent_req *req)
1080 {
1081         struct smbXcli_req_state *state =
1082                 tevent_req_data(req,
1083                 struct smbXcli_req_state);
1084         uint8_t flags;
1085         uint16_t flags2;
1086         uint32_t pid;
1087         uint16_t tid;
1088         uint16_t uid;
1089         uint16_t mid;
1090         struct tevent_req *subreq;
1091         NTSTATUS status;
1092
1093         flags = CVAL(state->smb1.hdr, HDR_FLG);
1094         flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1095         pid  = SVAL(state->smb1.hdr, HDR_PID);
1096         pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1097         tid = SVAL(state->smb1.hdr, HDR_TID);
1098         uid = SVAL(state->smb1.hdr, HDR_UID);
1099         mid = SVAL(state->smb1.hdr, HDR_MID);
1100
1101         subreq = smb1cli_req_create(state, state->ev,
1102                                     state->conn,
1103                                     SMBntcancel,
1104                                     flags, 0,
1105                                     flags2, 0,
1106                                     0, /* timeout */
1107                                     pid, tid, uid,
1108                                     0, NULL, /* vwv */
1109                                     0, NULL); /* bytes */
1110         if (subreq == NULL) {
1111                 return false;
1112         }
1113         smb1cli_req_set_mid(subreq, mid);
1114
1115         status = smb1cli_req_chain_submit(&subreq, 1);
1116         if (!NT_STATUS_IS_OK(status)) {
1117                 TALLOC_FREE(subreq);
1118                 return false;
1119         }
1120         smb1cli_req_set_mid(subreq, 0);
1121
1122         tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1123
1124         return true;
1125 }
1126
1127 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1128 {
1129         /* we do not care about the result */
1130         TALLOC_FREE(subreq);
1131 }
1132
1133 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1134                                       struct tevent_context *ev,
1135                                       struct smbXcli_conn *conn,
1136                                       uint8_t smb_command,
1137                                       uint8_t additional_flags,
1138                                       uint8_t clear_flags,
1139                                       uint16_t additional_flags2,
1140                                       uint16_t clear_flags2,
1141                                       uint32_t timeout_msec,
1142                                       uint32_t pid,
1143                                       uint16_t tid,
1144                                       uint16_t uid,
1145                                       uint8_t wct, uint16_t *vwv,
1146                                       int iov_count,
1147                                       struct iovec *bytes_iov)
1148 {
1149         struct tevent_req *req;
1150         struct smbXcli_req_state *state;
1151         uint8_t flags = 0;
1152         uint16_t flags2 = 0;
1153
1154         if (iov_count > MAX_SMB_IOV) {
1155                 /*
1156                  * Should not happen :-)
1157                  */
1158                 return NULL;
1159         }
1160
1161         req = tevent_req_create(mem_ctx, &state,
1162                                 struct smbXcli_req_state);
1163         if (req == NULL) {
1164                 return NULL;
1165         }
1166         state->ev = ev;
1167         state->conn = conn;
1168
1169         state->smb1.recv_cmd = 0xFF;
1170         state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1171         state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1172         if (state->smb1.recv_iov == NULL) {
1173                 TALLOC_FREE(req);
1174                 return NULL;
1175         }
1176
1177         smb1cli_req_flags(conn->protocol,
1178                           conn->smb1.capabilities,
1179                           smb_command,
1180                           additional_flags,
1181                           clear_flags,
1182                           &flags,
1183                           additional_flags2,
1184                           clear_flags2,
1185                           &flags2);
1186
1187         SIVAL(state->smb1.hdr, 0,           SMB_MAGIC);
1188         SCVAL(state->smb1.hdr, HDR_COM,     smb_command);
1189         SIVAL(state->smb1.hdr, HDR_RCLS,    NT_STATUS_V(NT_STATUS_OK));
1190         SCVAL(state->smb1.hdr, HDR_FLG,     flags);
1191         SSVAL(state->smb1.hdr, HDR_FLG2,    flags2);
1192         SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1193         SSVAL(state->smb1.hdr, HDR_TID,     tid);
1194         SSVAL(state->smb1.hdr, HDR_PID,     pid);
1195         SSVAL(state->smb1.hdr, HDR_UID,     uid);
1196         SSVAL(state->smb1.hdr, HDR_MID,     0); /* this comes later */
1197         SCVAL(state->smb1.hdr, HDR_WCT,     wct);
1198
1199         state->smb1.vwv = vwv;
1200
1201         SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1202
1203         state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1204         state->smb1.iov[0].iov_len  = sizeof(state->length_hdr);
1205         state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1206         state->smb1.iov[1].iov_len  = sizeof(state->smb1.hdr);
1207         state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1208         state->smb1.iov[2].iov_len  = wct * sizeof(uint16_t);
1209         state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1210         state->smb1.iov[3].iov_len  = sizeof(uint16_t);
1211
1212         if (iov_count != 0) {
1213                 memcpy(&state->smb1.iov[4], bytes_iov,
1214                        iov_count * sizeof(*bytes_iov));
1215         }
1216         state->smb1.iov_count = iov_count + 4;
1217
1218         if (timeout_msec > 0) {
1219                 struct timeval endtime;
1220
1221                 endtime = timeval_current_ofs_msec(timeout_msec);
1222                 if (!tevent_req_set_endtime(req, ev, endtime)) {
1223                         return req;
1224                 }
1225         }
1226
1227         switch (smb_command) {
1228         case SMBtranss:
1229         case SMBtranss2:
1230         case SMBnttranss:
1231                 state->one_way = true;
1232                 break;
1233         case SMBntcancel:
1234                 state->one_way = true;
1235                 state->smb1.one_way_seqnum = true;
1236                 break;
1237         case SMBlockingX:
1238                 if ((wct == 8) &&
1239                     (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1240                         state->one_way = true;
1241                 }
1242                 break;
1243         }
1244
1245         return req;
1246 }
1247
1248 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1249                                    struct iovec *iov, int iov_count,
1250                                    uint32_t *seqnum,
1251                                    bool one_way_seqnum)
1252 {
1253         TALLOC_CTX *frame = NULL;
1254         uint8_t *buf;
1255
1256         /*
1257          * Obvious optimization: Make cli_calculate_sign_mac work with struct
1258          * iovec directly. MD5Update would do that just fine.
1259          */
1260
1261         if (iov_count < 4) {
1262                 return NT_STATUS_INVALID_PARAMETER_MIX;
1263         }
1264         if (iov[0].iov_len != NBT_HDR_SIZE) {
1265                 return NT_STATUS_INVALID_PARAMETER_MIX;
1266         }
1267         if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1268                 return NT_STATUS_INVALID_PARAMETER_MIX;
1269         }
1270         if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1271                 return NT_STATUS_INVALID_PARAMETER_MIX;
1272         }
1273         if (iov[3].iov_len != sizeof(uint16_t)) {
1274                 return NT_STATUS_INVALID_PARAMETER_MIX;
1275         }
1276
1277         frame = talloc_stackframe();
1278
1279         buf = smbXcli_iov_concat(frame, iov, iov_count);
1280         if (buf == NULL) {
1281                 return NT_STATUS_NO_MEMORY;
1282         }
1283
1284         *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1285                                           one_way_seqnum);
1286         smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1287         memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1288
1289         TALLOC_FREE(frame);
1290         return NT_STATUS_OK;
1291 }
1292
1293 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1294 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1295                                                TALLOC_CTX *tmp_mem,
1296                                                uint8_t *inbuf);
1297
1298 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1299                                           struct smbXcli_req_state *state,
1300                                           struct iovec *iov, int iov_count)
1301 {
1302         struct tevent_req *subreq;
1303         NTSTATUS status;
1304         uint8_t cmd;
1305         uint16_t mid;
1306
1307         if (!smbXcli_conn_is_connected(state->conn)) {
1308                 return NT_STATUS_CONNECTION_DISCONNECTED;
1309         }
1310
1311         if (state->conn->protocol > PROTOCOL_NT1) {
1312                 return NT_STATUS_REVISION_MISMATCH;
1313         }
1314
1315         if (iov_count < 4) {
1316                 return NT_STATUS_INVALID_PARAMETER_MIX;
1317         }
1318         if (iov[0].iov_len != NBT_HDR_SIZE) {
1319                 return NT_STATUS_INVALID_PARAMETER_MIX;
1320         }
1321         if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1322                 return NT_STATUS_INVALID_PARAMETER_MIX;
1323         }
1324         if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1325                 return NT_STATUS_INVALID_PARAMETER_MIX;
1326         }
1327         if (iov[3].iov_len != sizeof(uint16_t)) {
1328                 return NT_STATUS_INVALID_PARAMETER_MIX;
1329         }
1330
1331         cmd = CVAL(iov[1].iov_base, HDR_COM);
1332         if (cmd == SMBreadBraw) {
1333                 if (smbXcli_conn_has_async_calls(state->conn)) {
1334                         return NT_STATUS_INVALID_PARAMETER_MIX;
1335                 }
1336                 state->conn->smb1.read_braw_req = req;
1337         }
1338
1339         if (state->smb1.mid != 0) {
1340                 mid = state->smb1.mid;
1341         } else {
1342                 mid = smb1cli_alloc_mid(state->conn);
1343         }
1344         SSVAL(iov[1].iov_base, HDR_MID, mid);
1345
1346         _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1347
1348         status = smb1cli_conn_signv(state->conn, iov, iov_count,
1349                                     &state->smb1.seqnum,
1350                                     state->smb1.one_way_seqnum);
1351
1352         if (!NT_STATUS_IS_OK(status)) {
1353                 return status;
1354         }
1355
1356         /*
1357          * If we supported multiple encrytion contexts
1358          * here we'd look up based on tid.
1359          */
1360         if (common_encryption_on(state->conn->smb1.trans_enc)) {
1361                 char *buf, *enc_buf;
1362
1363                 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1364                 if (buf == NULL) {
1365                         return NT_STATUS_NO_MEMORY;
1366                 }
1367                 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1368                                                (char *)buf, &enc_buf);
1369                 TALLOC_FREE(buf);
1370                 if (!NT_STATUS_IS_OK(status)) {
1371                         DEBUG(0, ("Error in encrypting client message: %s\n",
1372                                   nt_errstr(status)));
1373                         return status;
1374                 }
1375                 buf = (char *)talloc_memdup(state, enc_buf,
1376                                             smb_len_nbt(enc_buf)+4);
1377                 SAFE_FREE(enc_buf);
1378                 if (buf == NULL) {
1379                         return NT_STATUS_NO_MEMORY;
1380                 }
1381                 iov[0].iov_base = (void *)buf;
1382                 iov[0].iov_len = talloc_get_size(buf);
1383                 iov_count = 1;
1384         }
1385
1386         if (state->conn->dispatch_incoming == NULL) {
1387                 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1388         }
1389
1390         tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1391
1392         subreq = writev_send(state, state->ev, state->conn->outgoing,
1393                              state->conn->write_fd, false, iov, iov_count);
1394         if (subreq == NULL) {
1395                 return NT_STATUS_NO_MEMORY;
1396         }
1397         tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1398         return NT_STATUS_OK;
1399 }
1400
1401 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1402                                     struct tevent_context *ev,
1403                                     struct smbXcli_conn *conn,
1404                                     uint8_t smb_command,
1405                                     uint8_t additional_flags,
1406                                     uint8_t clear_flags,
1407                                     uint16_t additional_flags2,
1408                                     uint16_t clear_flags2,
1409                                     uint32_t timeout_msec,
1410                                     uint32_t pid,
1411                                     uint16_t tid,
1412                                     uint16_t uid,
1413                                     uint8_t wct, uint16_t *vwv,
1414                                     uint32_t num_bytes,
1415                                     const uint8_t *bytes)
1416 {
1417         struct tevent_req *req;
1418         struct iovec iov;
1419         NTSTATUS status;
1420
1421         iov.iov_base = discard_const_p(void, bytes);
1422         iov.iov_len = num_bytes;
1423
1424         req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1425                                  additional_flags, clear_flags,
1426                                  additional_flags2, clear_flags2,
1427                                  timeout_msec,
1428                                  pid, tid, uid,
1429                                  wct, vwv, 1, &iov);
1430         if (req == NULL) {
1431                 return NULL;
1432         }
1433         if (!tevent_req_is_in_progress(req)) {
1434                 return tevent_req_post(req, ev);
1435         }
1436         status = smb1cli_req_chain_submit(&req, 1);
1437         if (tevent_req_nterror(req, status)) {
1438                 return tevent_req_post(req, ev);
1439         }
1440         return req;
1441 }
1442
1443 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1444 {
1445         struct tevent_req *req =
1446                 tevent_req_callback_data(subreq,
1447                 struct tevent_req);
1448         struct smbXcli_req_state *state =
1449                 tevent_req_data(req,
1450                 struct smbXcli_req_state);
1451         ssize_t nwritten;
1452         int err;
1453
1454         nwritten = writev_recv(subreq, &err);
1455         TALLOC_FREE(subreq);
1456         if (nwritten == -1) {
1457                 NTSTATUS status = map_nt_error_from_unix_common(err);
1458                 smbXcli_conn_disconnect(state->conn, status);
1459                 return;
1460         }
1461
1462         if (state->one_way) {
1463                 state->inbuf = NULL;
1464                 tevent_req_done(req);
1465                 return;
1466         }
1467
1468         if (!smbXcli_req_set_pending(req)) {
1469                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1470                 return;
1471         }
1472 }
1473
1474 static void smbXcli_conn_received(struct tevent_req *subreq)
1475 {
1476         struct smbXcli_conn *conn =
1477                 tevent_req_callback_data(subreq,
1478                 struct smbXcli_conn);
1479         TALLOC_CTX *frame = talloc_stackframe();
1480         NTSTATUS status;
1481         uint8_t *inbuf;
1482         ssize_t received;
1483         int err;
1484
1485         if (subreq != conn->read_smb_req) {
1486                 DEBUG(1, ("Internal error: cli_smb_received called with "
1487                           "unexpected subreq\n"));
1488                 status = NT_STATUS_INTERNAL_ERROR;
1489                 smbXcli_conn_disconnect(conn, status);
1490                 TALLOC_FREE(frame);
1491                 return;
1492         }
1493         conn->read_smb_req = NULL;
1494
1495         received = read_smb_recv(subreq, frame, &inbuf, &err);
1496         TALLOC_FREE(subreq);
1497         if (received == -1) {
1498                 status = map_nt_error_from_unix_common(err);
1499                 smbXcli_conn_disconnect(conn, status);
1500                 TALLOC_FREE(frame);
1501                 return;
1502         }
1503
1504         status = conn->dispatch_incoming(conn, frame, inbuf);
1505         TALLOC_FREE(frame);
1506         if (NT_STATUS_IS_OK(status)) {
1507                 /*
1508                  * We should not do any more processing
1509                  * as the dispatch function called
1510                  * tevent_req_done().
1511                  */
1512                 return;
1513         } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1514                 /*
1515                  * We got an error, so notify all pending requests
1516                  */
1517                 smbXcli_conn_disconnect(conn, status);
1518                 return;
1519         }
1520
1521         /*
1522          * We got NT_STATUS_RETRY, so we may ask for a
1523          * next incoming pdu.
1524          */
1525         if (!smbXcli_conn_receive_next(conn)) {
1526                 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1527         }
1528 }
1529
1530 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1531                                           struct iovec **piov, int *pnum_iov)
1532 {
1533         struct iovec *iov;
1534         int num_iov;
1535         size_t buflen;
1536         size_t taken;
1537         size_t remaining;
1538         uint8_t *hdr;
1539         uint8_t cmd;
1540         uint32_t wct_ofs;
1541
1542         buflen = smb_len_nbt(buf);
1543         taken = 0;
1544
1545         hdr = buf + NBT_HDR_SIZE;
1546
1547         if (buflen < MIN_SMB_SIZE) {
1548                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1549         }
1550
1551         /*
1552          * This returns iovec elements in the following order:
1553          *
1554          * - SMB header
1555          *
1556          * - Parameter Block
1557          * - Data Block
1558          *
1559          * - Parameter Block
1560          * - Data Block
1561          *
1562          * - Parameter Block
1563          * - Data Block
1564          */
1565         num_iov = 1;
1566
1567         iov = talloc_array(mem_ctx, struct iovec, num_iov);
1568         if (iov == NULL) {
1569                 return NT_STATUS_NO_MEMORY;
1570         }
1571         iov[0].iov_base = hdr;
1572         iov[0].iov_len = HDR_WCT;
1573         taken += HDR_WCT;
1574
1575         cmd = CVAL(hdr, HDR_COM);
1576         wct_ofs = HDR_WCT;
1577
1578         while (true) {
1579                 size_t len = buflen - taken;
1580                 struct iovec *cur;
1581                 struct iovec *iov_tmp;
1582                 uint8_t wct;
1583                 uint32_t bcc_ofs;
1584                 uint16_t bcc;
1585                 size_t needed;
1586
1587                 /*
1588                  * we need at least WCT and BCC
1589                  */
1590                 needed = sizeof(uint8_t) + sizeof(uint16_t);
1591                 if (len < needed) {
1592                         DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1593                                    __location__, (int)len, (int)needed));
1594                         goto inval;
1595                 }
1596
1597                 /*
1598                  * Now we check if the specified words are there
1599                  */
1600                 wct = CVAL(hdr, wct_ofs);
1601                 needed += wct * sizeof(uint16_t);
1602                 if (len < needed) {
1603                         DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1604                                    __location__, (int)len, (int)needed));
1605                         goto inval;
1606                 }
1607
1608                 /*
1609                  * Now we check if the specified bytes are there
1610                  */
1611                 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1612                 bcc = SVAL(hdr, bcc_ofs);
1613                 needed += bcc * sizeof(uint8_t);
1614                 if (len < needed) {
1615                         DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1616                                    __location__, (int)len, (int)needed));
1617                         goto inval;
1618                 }
1619
1620                 /*
1621                  * we allocate 2 iovec structures for words and bytes
1622                  */
1623                 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1624                                          num_iov + 2);
1625                 if (iov_tmp == NULL) {
1626                         TALLOC_FREE(iov);
1627                         return NT_STATUS_NO_MEMORY;
1628                 }
1629                 iov = iov_tmp;
1630                 cur = &iov[num_iov];
1631                 num_iov += 2;
1632
1633                 cur[0].iov_len = wct * sizeof(uint16_t);
1634                 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1635                 cur[1].iov_len = bcc * sizeof(uint8_t);
1636                 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1637
1638                 taken += needed;
1639
1640                 if (!smb1cli_is_andx_req(cmd)) {
1641                         /*
1642                          * If the current command does not have AndX chanining
1643                          * we are done.
1644                          */
1645                         break;
1646                 }
1647
1648                 if (wct == 0 && bcc == 0) {
1649                         /*
1650                          * An empty response also ends the chain,
1651                          * most likely with an error.
1652                          */
1653                         break;
1654                 }
1655
1656                 if (wct < 2) {
1657                         DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1658                                    __location__, (int)wct, (int)cmd));
1659                         goto inval;
1660                 }
1661                 cmd = CVAL(cur[0].iov_base, 0);
1662                 if (cmd == 0xFF) {
1663                         /*
1664                          * If it is the end of the chain we are also done.
1665                          */
1666                         break;
1667                 }
1668                 wct_ofs = SVAL(cur[0].iov_base, 2);
1669
1670                 if (wct_ofs < taken) {
1671                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
1672                 }
1673                 if (wct_ofs > buflen) {
1674                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
1675                 }
1676
1677                 /*
1678                  * we consumed everything up to the start of the next
1679                  * parameter block.
1680                  */
1681                 taken = wct_ofs;
1682         }
1683
1684         remaining = buflen - taken;
1685
1686         if (remaining > 0 && num_iov >= 3) {
1687                 /*
1688                  * The last DATA block gets the remaining
1689                  * bytes, this is needed to support
1690                  * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1691                  */
1692                 iov[num_iov-1].iov_len += remaining;
1693         }
1694
1695         *piov = iov;
1696         *pnum_iov = num_iov;
1697         return NT_STATUS_OK;
1698
1699 inval:
1700         TALLOC_FREE(iov);
1701         return NT_STATUS_INVALID_NETWORK_RESPONSE;
1702 }
1703
1704 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1705                                                TALLOC_CTX *tmp_mem,
1706                                                uint8_t *inbuf)
1707 {
1708         struct tevent_req *req;
1709         struct smbXcli_req_state *state;
1710         NTSTATUS status;
1711         size_t num_pending;
1712         size_t i;
1713         uint8_t cmd;
1714         uint16_t mid;
1715         bool oplock_break;
1716         const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1717         struct iovec *iov = NULL;
1718         int num_iov = 0;
1719         struct tevent_req **chain = NULL;
1720         size_t num_chained = 0;
1721         size_t num_responses = 0;
1722
1723         if (conn->smb1.read_braw_req != NULL) {
1724                 req = conn->smb1.read_braw_req;
1725                 conn->smb1.read_braw_req = NULL;
1726                 state = tevent_req_data(req, struct smbXcli_req_state);
1727
1728                 smbXcli_req_unset_pending(req);
1729
1730                 if (state->smb1.recv_iov == NULL) {
1731                         /*
1732                          * For requests with more than
1733                          * one response, we have to readd the
1734                          * recv_iov array.
1735                          */
1736                         state->smb1.recv_iov = talloc_zero_array(state,
1737                                                                  struct iovec,
1738                                                                  3);
1739                         if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1740                                 return NT_STATUS_OK;
1741                         }
1742                 }
1743
1744                 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1745                 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1746                 ZERO_STRUCT(state->smb1.recv_iov[1]);
1747                 ZERO_STRUCT(state->smb1.recv_iov[2]);
1748
1749                 state->smb1.recv_cmd = SMBreadBraw;
1750                 state->smb1.recv_status = NT_STATUS_OK;
1751                 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1752
1753                 tevent_req_done(req);
1754                 return NT_STATUS_OK;
1755         }
1756
1757         if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1758             && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1759                 DEBUG(10, ("Got non-SMB PDU\n"));
1760                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1761         }
1762
1763         /*
1764          * If we supported multiple encrytion contexts
1765          * here we'd look up based on tid.
1766          */
1767         if (common_encryption_on(conn->smb1.trans_enc)
1768             && (CVAL(inbuf, 0) == 0)) {
1769                 uint16_t enc_ctx_num;
1770
1771                 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1772                 if (!NT_STATUS_IS_OK(status)) {
1773                         DEBUG(10, ("get_enc_ctx_num returned %s\n",
1774                                    nt_errstr(status)));
1775                         return status;
1776                 }
1777
1778                 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1779                         DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1780                                    enc_ctx_num,
1781                                    conn->smb1.trans_enc->enc_ctx_num));
1782                         return NT_STATUS_INVALID_HANDLE;
1783                 }
1784
1785                 status = common_decrypt_buffer(conn->smb1.trans_enc,
1786                                                (char *)inbuf);
1787                 if (!NT_STATUS_IS_OK(status)) {
1788                         DEBUG(10, ("common_decrypt_buffer returned %s\n",
1789                                    nt_errstr(status)));
1790                         return status;
1791                 }
1792         }
1793
1794         mid = SVAL(inhdr, HDR_MID);
1795         num_pending = talloc_array_length(conn->pending);
1796
1797         for (i=0; i<num_pending; i++) {
1798                 if (mid == smb1cli_req_mid(conn->pending[i])) {
1799                         break;
1800                 }
1801         }
1802         if (i == num_pending) {
1803                 /* Dump unexpected reply */
1804                 return NT_STATUS_RETRY;
1805         }
1806
1807         oplock_break = false;
1808
1809         if (mid == 0xffff) {
1810                 /*
1811                  * Paranoia checks that this is really an oplock break request.
1812                  */
1813                 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1814                 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1815                 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1816                 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1817                 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1818
1819                 if (!oplock_break) {
1820                         /* Dump unexpected reply */
1821                         return NT_STATUS_RETRY;
1822                 }
1823         }
1824
1825         req = conn->pending[i];
1826         state = tevent_req_data(req, struct smbXcli_req_state);
1827
1828         if (!oplock_break /* oplock breaks are not signed */
1829             && !smb_signing_check_pdu(conn->smb1.signing,
1830                                       inbuf, state->smb1.seqnum+1)) {
1831                 DEBUG(10, ("cli_check_sign_mac failed\n"));
1832                 return NT_STATUS_ACCESS_DENIED;
1833         }
1834
1835         status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1836                                            &iov, &num_iov);
1837         if (!NT_STATUS_IS_OK(status)) {
1838                 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1839                           nt_errstr(status)));
1840                 return status;
1841         }
1842
1843         cmd = CVAL(inhdr, HDR_COM);
1844         status = smb1cli_pull_raw_error(inhdr);
1845
1846         if (state->smb1.chained_requests == NULL) {
1847                 if (num_iov != 3) {
1848                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
1849                 }
1850
1851                 smbXcli_req_unset_pending(req);
1852
1853                 if (state->smb1.recv_iov == NULL) {
1854                         /*
1855                          * For requests with more than
1856                          * one response, we have to readd the
1857                          * recv_iov array.
1858                          */
1859                         state->smb1.recv_iov = talloc_zero_array(state,
1860                                                                  struct iovec,
1861                                                                  3);
1862                         if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1863                                 return NT_STATUS_OK;
1864                         }
1865                 }
1866
1867                 state->smb1.recv_cmd = cmd;
1868                 state->smb1.recv_status = status;
1869                 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1870
1871                 state->smb1.recv_iov[0] = iov[0];
1872                 state->smb1.recv_iov[1] = iov[1];
1873                 state->smb1.recv_iov[2] = iov[2];
1874
1875                 if (talloc_array_length(conn->pending) == 0) {
1876                         tevent_req_done(req);
1877                         return NT_STATUS_OK;
1878                 }
1879
1880                 tevent_req_defer_callback(req, state->ev);
1881                 tevent_req_done(req);
1882                 return NT_STATUS_RETRY;
1883         }
1884
1885         chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1886         num_chained = talloc_array_length(chain);
1887         num_responses = (num_iov - 1)/2;
1888
1889         if (num_responses > num_chained) {
1890                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1891         }
1892
1893         for (i=0; i<num_chained; i++) {
1894                 size_t iov_idx = 1 + (i*2);
1895                 struct iovec *cur = &iov[iov_idx];
1896                 uint8_t *inbuf_ref;
1897
1898                 req = chain[i];
1899                 state = tevent_req_data(req, struct smbXcli_req_state);
1900
1901                 smbXcli_req_unset_pending(req);
1902
1903                 /*
1904                  * as we finish multiple requests here
1905                  * we need to defer the callbacks as
1906                  * they could destroy our current stack state.
1907                  */
1908                 tevent_req_defer_callback(req, state->ev);
1909
1910                 if (i >= num_responses) {
1911                         tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1912                         continue;
1913                 }
1914
1915                 if (state->smb1.recv_iov == NULL) {
1916                         /*
1917                          * For requests with more than
1918                          * one response, we have to readd the
1919                          * recv_iov array.
1920                          */
1921                         state->smb1.recv_iov = talloc_zero_array(state,
1922                                                                  struct iovec,
1923                                                                  3);
1924                         if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1925                                 continue;
1926                         }
1927                 }
1928
1929                 state->smb1.recv_cmd = cmd;
1930
1931                 if (i == (num_responses - 1)) {
1932                         /*
1933                          * The last request in the chain gets the status
1934                          */
1935                         state->smb1.recv_status = status;
1936                 } else {
1937                         cmd = CVAL(cur[0].iov_base, 0);
1938                         state->smb1.recv_status = NT_STATUS_OK;
1939                 }
1940
1941                 state->inbuf = inbuf;
1942
1943                 /*
1944                  * Note: here we use talloc_reference() in a way
1945                  *       that does not expose it to the caller.
1946                  */
1947                 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1948                 if (tevent_req_nomem(inbuf_ref, req)) {
1949                         continue;
1950                 }
1951
1952                 /* copy the related buffers */
1953                 state->smb1.recv_iov[0] = iov[0];
1954                 state->smb1.recv_iov[1] = cur[0];
1955                 state->smb1.recv_iov[2] = cur[1];
1956
1957                 tevent_req_done(req);
1958         }
1959
1960         return NT_STATUS_RETRY;
1961 }
1962
1963 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1964                           TALLOC_CTX *mem_ctx,
1965                           struct iovec **piov,
1966                           uint8_t **phdr,
1967                           uint8_t *pwct,
1968                           uint16_t **pvwv,
1969                           uint32_t *pvwv_offset,
1970                           uint32_t *pnum_bytes,
1971                           uint8_t **pbytes,
1972                           uint32_t *pbytes_offset,
1973                           uint8_t **pinbuf,
1974                           const struct smb1cli_req_expected_response *expected,
1975                           size_t num_expected)
1976 {
1977         struct smbXcli_req_state *state =
1978                 tevent_req_data(req,
1979                 struct smbXcli_req_state);
1980         NTSTATUS status = NT_STATUS_OK;
1981         struct iovec *recv_iov = NULL;
1982         uint8_t *hdr = NULL;
1983         uint8_t wct = 0;
1984         uint32_t vwv_offset = 0;
1985         uint16_t *vwv = NULL;
1986         uint32_t num_bytes = 0;
1987         uint32_t bytes_offset = 0;
1988         uint8_t *bytes = NULL;
1989         size_t i;
1990         bool found_status = false;
1991         bool found_size = false;
1992
1993         if (piov != NULL) {
1994                 *piov = NULL;
1995         }
1996         if (phdr != NULL) {
1997                 *phdr = 0;
1998         }
1999         if (pwct != NULL) {
2000                 *pwct = 0;
2001         }
2002         if (pvwv != NULL) {
2003                 *pvwv = NULL;
2004         }
2005         if (pvwv_offset != NULL) {
2006                 *pvwv_offset = 0;
2007         }
2008         if (pnum_bytes != NULL) {
2009                 *pnum_bytes = 0;
2010         }
2011         if (pbytes != NULL) {
2012                 *pbytes = NULL;
2013         }
2014         if (pbytes_offset != NULL) {
2015                 *pbytes_offset = 0;
2016         }
2017         if (pinbuf != NULL) {
2018                 *pinbuf = NULL;
2019         }
2020
2021         if (state->inbuf != NULL) {
2022                 recv_iov = state->smb1.recv_iov;
2023                 state->smb1.recv_iov = NULL;
2024                 if (state->smb1.recv_cmd != SMBreadBraw) {
2025                         hdr = (uint8_t *)recv_iov[0].iov_base;
2026                         wct = recv_iov[1].iov_len/2;
2027                         vwv = (uint16_t *)recv_iov[1].iov_base;
2028                         vwv_offset = PTR_DIFF(vwv, hdr);
2029                         num_bytes = recv_iov[2].iov_len;
2030                         bytes = (uint8_t *)recv_iov[2].iov_base;
2031                         bytes_offset = PTR_DIFF(bytes, hdr);
2032                 }
2033         }
2034
2035         if (tevent_req_is_nterror(req, &status)) {
2036                 for (i=0; i < num_expected; i++) {
2037                         if (NT_STATUS_EQUAL(status, expected[i].status)) {
2038                                 found_status = true;
2039                                 break;
2040                         }
2041                 }
2042
2043                 if (found_status) {
2044                         return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2045                 }
2046
2047                 return status;
2048         }
2049
2050         if (num_expected == 0) {
2051                 found_status = true;
2052                 found_size = true;
2053         }
2054
2055         status = state->smb1.recv_status;
2056
2057         for (i=0; i < num_expected; i++) {
2058                 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2059                         continue;
2060                 }
2061
2062                 found_status = true;
2063                 if (expected[i].wct == 0) {
2064                         found_size = true;
2065                         break;
2066                 }
2067
2068                 if (expected[i].wct == wct) {
2069                         found_size = true;
2070                         break;
2071                 }
2072         }
2073
2074         if (!found_status) {
2075                 return status;
2076         }
2077
2078         if (!found_size) {
2079                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2080         }
2081
2082         if (piov != NULL) {
2083                 *piov = talloc_move(mem_ctx, &recv_iov);
2084         }
2085
2086         if (phdr != NULL) {
2087                 *phdr = hdr;
2088         }
2089         if (pwct != NULL) {
2090                 *pwct = wct;
2091         }
2092         if (pvwv != NULL) {
2093                 *pvwv = vwv;
2094         }
2095         if (pvwv_offset != NULL) {
2096                 *pvwv_offset = vwv_offset;
2097         }
2098         if (pnum_bytes != NULL) {
2099                 *pnum_bytes = num_bytes;
2100         }
2101         if (pbytes != NULL) {
2102                 *pbytes = bytes;
2103         }
2104         if (pbytes_offset != NULL) {
2105                 *pbytes_offset = bytes_offset;
2106         }
2107         if (pinbuf != NULL) {
2108                 *pinbuf = state->inbuf;
2109         }
2110
2111         return status;
2112 }
2113
2114 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2115 {
2116         size_t wct_ofs;
2117         int i;
2118
2119         wct_ofs = HDR_WCT;
2120
2121         for (i=0; i<num_reqs; i++) {
2122                 struct smbXcli_req_state *state;
2123                 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2124                 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2125                                            state->smb1.iov_count-2);
2126                 wct_ofs = (wct_ofs + 3) & ~3;
2127         }
2128         return wct_ofs;
2129 }
2130
2131 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2132 {
2133         struct smbXcli_req_state *first_state =
2134                 tevent_req_data(reqs[0],
2135                 struct smbXcli_req_state);
2136         struct smbXcli_req_state *state;
2137         size_t wct_offset;
2138         size_t chain_padding = 0;
2139         int i, iovlen;
2140         struct iovec *iov = NULL;
2141         struct iovec *this_iov;
2142         NTSTATUS status;
2143         size_t nbt_len;
2144
2145         if (num_reqs == 1) {
2146                 return smb1cli_req_writev_submit(reqs[0], first_state,
2147                                                  first_state->smb1.iov,
2148                                                  first_state->smb1.iov_count);
2149         }
2150
2151         iovlen = 0;
2152         for (i=0; i<num_reqs; i++) {
2153                 if (!tevent_req_is_in_progress(reqs[i])) {
2154                         return NT_STATUS_INTERNAL_ERROR;
2155                 }
2156
2157                 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2158
2159                 if (state->smb1.iov_count < 4) {
2160                         return NT_STATUS_INVALID_PARAMETER_MIX;
2161                 }
2162
2163                 if (i == 0) {
2164                         /*
2165                          * The NBT and SMB header
2166                          */
2167                         iovlen += 2;
2168                 } else {
2169                         /*
2170                          * Chain padding
2171                          */
2172                         iovlen += 1;
2173                 }
2174
2175                 /*
2176                  * words and bytes
2177                  */
2178                 iovlen += state->smb1.iov_count - 2;
2179         }
2180
2181         iov = talloc_zero_array(first_state, struct iovec, iovlen);
2182         if (iov == NULL) {
2183                 return NT_STATUS_NO_MEMORY;
2184         }
2185
2186         first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2187                 first_state, reqs, sizeof(*reqs) * num_reqs);
2188         if (first_state->smb1.chained_requests == NULL) {
2189                 TALLOC_FREE(iov);
2190                 return NT_STATUS_NO_MEMORY;
2191         }
2192
2193         wct_offset = HDR_WCT;
2194         this_iov = iov;
2195
2196         for (i=0; i<num_reqs; i++) {
2197                 size_t next_padding = 0;
2198                 uint16_t *vwv;
2199
2200                 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2201
2202                 if (i < num_reqs-1) {
2203                         if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2204                             || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2205                                 TALLOC_FREE(iov);
2206                                 TALLOC_FREE(first_state->smb1.chained_requests);
2207                                 return NT_STATUS_INVALID_PARAMETER_MIX;
2208                         }
2209                 }
2210
2211                 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2212                                               state->smb1.iov_count-2) + 1;
2213                 if ((wct_offset % 4) != 0) {
2214                         next_padding = 4 - (wct_offset % 4);
2215                 }
2216                 wct_offset += next_padding;
2217                 vwv = state->smb1.vwv;
2218
2219                 if (i < num_reqs-1) {
2220                         struct smbXcli_req_state *next_state =
2221                                 tevent_req_data(reqs[i+1],
2222                                 struct smbXcli_req_state);
2223                         SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2224                         SCVAL(vwv+0, 1, 0);
2225                         SSVAL(vwv+1, 0, wct_offset);
2226                 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2227                         /* properly end the chain */
2228                         SCVAL(vwv+0, 0, 0xff);
2229                         SCVAL(vwv+0, 1, 0xff);
2230                         SSVAL(vwv+1, 0, 0);
2231                 }
2232
2233                 if (i == 0) {
2234                         /*
2235                          * The NBT and SMB header
2236                          */
2237                         this_iov[0] = state->smb1.iov[0];
2238                         this_iov[1] = state->smb1.iov[1];
2239                         this_iov += 2;
2240                 } else {
2241                         /*
2242                          * This one is a bit subtle. We have to add
2243                          * chain_padding bytes between the requests, and we
2244                          * have to also include the wct field of the
2245                          * subsequent requests. We use the subsequent header
2246                          * for the padding, it contains the wct field in its
2247                          * last byte.
2248                          */
2249                         this_iov[0].iov_len = chain_padding+1;
2250                         this_iov[0].iov_base = (void *)&state->smb1.hdr[
2251                                 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2252                         memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2253                         this_iov += 1;
2254                 }
2255
2256                 /*
2257                  * copy the words and bytes
2258                  */
2259                 memcpy(this_iov, state->smb1.iov+2,
2260                        sizeof(struct iovec) * (state->smb1.iov_count-2));
2261                 this_iov += state->smb1.iov_count - 2;
2262                 chain_padding = next_padding;
2263         }
2264
2265         nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2266         if (nbt_len > first_state->conn->smb1.max_xmit) {
2267                 TALLOC_FREE(iov);
2268                 TALLOC_FREE(first_state->smb1.chained_requests);
2269                 return NT_STATUS_INVALID_PARAMETER_MIX;
2270         }
2271
2272         status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2273         if (!NT_STATUS_IS_OK(status)) {
2274                 TALLOC_FREE(iov);
2275                 TALLOC_FREE(first_state->smb1.chained_requests);
2276                 return status;
2277         }
2278
2279         return NT_STATUS_OK;
2280 }
2281
2282 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2283 {
2284         return ((tevent_queue_length(conn->outgoing) != 0)
2285                 || (talloc_array_length(conn->pending) != 0));
2286 }
2287
2288 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2289 {
2290         return conn->smb2.server.capabilities;
2291 }
2292
2293 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2294 {
2295         return conn->smb2.server.security_mode;
2296 }
2297
2298 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2299 {
2300         return conn->smb2.server.max_trans_size;
2301 }
2302
2303 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2304 {
2305         return conn->smb2.server.max_read_size;
2306 }
2307
2308 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2309 {
2310         return conn->smb2.server.max_write_size;
2311 }
2312
2313 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2314                                   uint16_t max_credits)
2315 {
2316         conn->smb2.max_credits = max_credits;
2317 }
2318
2319 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2320
2321 static bool smb2cli_req_cancel(struct tevent_req *req)
2322 {
2323         struct smbXcli_req_state *state =
2324                 tevent_req_data(req,
2325                 struct smbXcli_req_state);
2326         uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2327         uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2328         uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2329         uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2330         uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2331         struct smbXcli_session *session = state->session;
2332         uint8_t *fixed = state->smb2.pad;
2333         uint16_t fixed_len = 4;
2334         struct tevent_req *subreq;
2335         struct smbXcli_req_state *substate;
2336         NTSTATUS status;
2337
2338         SSVAL(fixed, 0, 0x04);
2339         SSVAL(fixed, 2, 0);
2340
2341         subreq = smb2cli_req_create(state, state->ev,
2342                                     state->conn,
2343                                     SMB2_OP_CANCEL,
2344                                     flags, 0,
2345                                     0, /* timeout */
2346                                     pid, tid, session,
2347                                     fixed, fixed_len,
2348                                     NULL, 0);
2349         if (subreq == NULL) {
2350                 return false;
2351         }
2352         substate = tevent_req_data(subreq, struct smbXcli_req_state);
2353
2354         if (flags & SMB2_HDR_FLAG_ASYNC) {
2355                 mid = 0;
2356         }
2357
2358         SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2359         SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2360         SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2361         SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2362         SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2363
2364         status = smb2cli_req_compound_submit(&subreq, 1);
2365         if (!NT_STATUS_IS_OK(status)) {
2366                 TALLOC_FREE(subreq);
2367                 return false;
2368         }
2369
2370         tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2371
2372         return true;
2373 }
2374
2375 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2376 {
2377         /* we do not care about the result */
2378         TALLOC_FREE(subreq);
2379 }
2380
2381 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2382                                       struct tevent_context *ev,
2383                                       struct smbXcli_conn *conn,
2384                                       uint16_t cmd,
2385                                       uint32_t additional_flags,
2386                                       uint32_t clear_flags,
2387                                       uint32_t timeout_msec,
2388                                       uint32_t pid,
2389                                       uint32_t tid,
2390                                       struct smbXcli_session *session,
2391                                       const uint8_t *fixed,
2392                                       uint16_t fixed_len,
2393                                       const uint8_t *dyn,
2394                                       uint32_t dyn_len)
2395 {
2396         struct tevent_req *req;
2397         struct smbXcli_req_state *state;
2398         uint32_t flags = 0;
2399         uint64_t uid = 0;
2400
2401         req = tevent_req_create(mem_ctx, &state,
2402                                 struct smbXcli_req_state);
2403         if (req == NULL) {
2404                 return NULL;
2405         }
2406
2407         state->ev = ev;
2408         state->conn = conn;
2409         state->session = session;
2410
2411         if (session) {
2412                 uid = session->smb2.session_id;
2413         }
2414
2415         state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2416         if (state->smb2.recv_iov == NULL) {
2417                 TALLOC_FREE(req);
2418                 return NULL;
2419         }
2420
2421         flags |= additional_flags;
2422         flags &= ~clear_flags;
2423
2424         state->smb2.fixed = fixed;
2425         state->smb2.fixed_len = fixed_len;
2426         state->smb2.dyn = dyn;
2427         state->smb2.dyn_len = dyn_len;
2428
2429         SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID,    SMB2_MAGIC);
2430         SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH,         SMB2_HDR_BODY);
2431         SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE,         cmd);
2432         SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS,          flags);
2433         SIVAL(state->smb2.hdr, SMB2_HDR_PID,            pid);
2434         SIVAL(state->smb2.hdr, SMB2_HDR_TID,            tid);
2435         SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID,     uid);
2436
2437         switch (cmd) {
2438         case SMB2_OP_CANCEL:
2439                 state->one_way = true;
2440                 break;
2441         case SMB2_OP_BREAK:
2442                 /*
2443                  * If this is a dummy request, it will have
2444                  * UINT64_MAX as message id.
2445                  * If we send on break acknowledgement,
2446                  * this gets overwritten later.
2447                  */
2448                 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2449                 break;
2450         }
2451
2452         if (timeout_msec > 0) {
2453                 struct timeval endtime;
2454
2455                 endtime = timeval_current_ofs_msec(timeout_msec);
2456                 if (!tevent_req_set_endtime(req, ev, endtime)) {
2457                         return req;
2458                 }
2459         }
2460
2461         return req;
2462 }
2463
2464 void smb2cli_req_set_notify_async(struct tevent_req *req)
2465 {
2466         struct smbXcli_req_state *state =
2467                 tevent_req_data(req,
2468                 struct smbXcli_req_state);
2469
2470         state->smb2.notify_async = true;
2471 }
2472
2473 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2474 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2475                                                TALLOC_CTX *tmp_mem,
2476                                                uint8_t *inbuf);
2477
2478 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2479                                      int num_reqs)
2480 {
2481         struct smbXcli_req_state *state;
2482         struct tevent_req *subreq;
2483         struct iovec *iov;
2484         int i, num_iov, nbt_len;
2485
2486         /*
2487          * 1 for the nbt length
2488          * per request: HDR, fixed, dyn, padding
2489          * -1 because the last one does not need padding
2490          */
2491
2492         iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2493         if (iov == NULL) {
2494                 return NT_STATUS_NO_MEMORY;
2495         }
2496
2497         num_iov = 1;
2498         nbt_len = 0;
2499
2500         for (i=0; i<num_reqs; i++) {
2501                 int hdr_iov;
2502                 size_t reqlen;
2503                 bool ret;
2504                 uint16_t opcode;
2505                 uint64_t avail;
2506                 uint16_t charge;
2507                 uint16_t credits;
2508                 uint64_t mid;
2509                 const DATA_BLOB *signing_key = NULL;
2510
2511                 if (!tevent_req_is_in_progress(reqs[i])) {
2512                         return NT_STATUS_INTERNAL_ERROR;
2513                 }
2514
2515                 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2516
2517                 if (!smbXcli_conn_is_connected(state->conn)) {
2518                         return NT_STATUS_CONNECTION_DISCONNECTED;
2519                 }
2520
2521                 if ((state->conn->protocol != PROTOCOL_NONE) &&
2522                     (state->conn->protocol < PROTOCOL_SMB2_02)) {
2523                         return NT_STATUS_REVISION_MISMATCH;
2524                 }
2525
2526                 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2527                 if (opcode == SMB2_OP_CANCEL) {
2528                         goto skip_credits;
2529                 }
2530
2531                 avail = UINT64_MAX - state->conn->smb2.mid;
2532                 if (avail < 1) {
2533                         return NT_STATUS_CONNECTION_ABORTED;
2534                 }
2535
2536                 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2537                         charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2538                 } else {
2539                         charge = 1;
2540                 }
2541
2542                 charge = MAX(state->smb2.credit_charge, charge);
2543
2544                 avail = MIN(avail, state->conn->smb2.cur_credits);
2545                 if (avail < charge) {
2546                         return NT_STATUS_INTERNAL_ERROR;
2547                 }
2548
2549                 credits = 0;
2550                 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2551                         credits = state->conn->smb2.max_credits -
2552                                   state->conn->smb2.cur_credits;
2553                 }
2554                 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2555                         credits += 1;
2556                 }
2557
2558                 mid = state->conn->smb2.mid;
2559                 state->conn->smb2.mid += charge;
2560                 state->conn->smb2.cur_credits -= charge;
2561
2562                 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2563                         SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2564                 }
2565                 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2566                 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2567
2568 skip_credits:
2569                 hdr_iov = num_iov;
2570                 iov[num_iov].iov_base = state->smb2.hdr;
2571                 iov[num_iov].iov_len  = sizeof(state->smb2.hdr);
2572                 num_iov += 1;
2573
2574                 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2575                 iov[num_iov].iov_len  = state->smb2.fixed_len;
2576                 num_iov += 1;
2577
2578                 if (state->smb2.dyn != NULL) {
2579                         iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2580                         iov[num_iov].iov_len  = state->smb2.dyn_len;
2581                         num_iov += 1;
2582                 }
2583
2584                 reqlen  = sizeof(state->smb2.hdr);
2585                 reqlen += state->smb2.fixed_len;
2586                 reqlen += state->smb2.dyn_len;
2587
2588                 if (i < num_reqs-1) {
2589                         if ((reqlen % 8) > 0) {
2590                                 uint8_t pad = 8 - (reqlen % 8);
2591                                 iov[num_iov].iov_base = state->smb2.pad;
2592                                 iov[num_iov].iov_len = pad;
2593                                 num_iov += 1;
2594                                 reqlen += pad;
2595                         }
2596                         SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2597                 }
2598                 nbt_len += reqlen;
2599
2600                 if (state->session) {
2601                         bool should_sign = state->session->smb2.should_sign;
2602
2603                         if (opcode == SMB2_OP_SESSSETUP &&
2604                             state->session->smb2.signing_key.length != 0) {
2605                                 should_sign = true;
2606                         }
2607
2608                         /*
2609                          * We prefer the channel signing key if it is
2610                          * already there.
2611                          */
2612                         if (should_sign) {
2613                                 signing_key = &state->session->smb2.channel_signing_key;
2614                         }
2615
2616                         /*
2617                          * If it is a channel binding, we already have the main
2618                          * signing key and try that one.
2619                          */
2620                         if (signing_key && signing_key->length == 0) {
2621                                 signing_key = &state->session->smb2.signing_key;
2622                         }
2623
2624                         /*
2625                          * If we do not have any session key yet, we skip the
2626                          * signing of SMB2_OP_SESSSETUP requests.
2627                          */
2628                         if (signing_key && signing_key->length == 0) {
2629                                 signing_key = NULL;
2630                         }
2631                 }
2632
2633                 if (signing_key) {
2634                         NTSTATUS status;
2635
2636                         status = smb2_signing_sign_pdu(*signing_key,
2637                                                        state->session->conn->protocol,
2638                                                        &iov[hdr_iov], num_iov - hdr_iov);
2639                         if (!NT_STATUS_IS_OK(status)) {
2640                                 return status;
2641                         }
2642                 }
2643
2644                 ret = smbXcli_req_set_pending(reqs[i]);
2645                 if (!ret) {
2646                         return NT_STATUS_NO_MEMORY;
2647                 }
2648         }
2649
2650         state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2651         _smb_setlen_tcp(state->length_hdr, nbt_len);
2652         iov[0].iov_base = state->length_hdr;
2653         iov[0].iov_len  = sizeof(state->length_hdr);
2654
2655         if (state->conn->dispatch_incoming == NULL) {
2656                 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2657         }
2658
2659         subreq = writev_send(state, state->ev, state->conn->outgoing,
2660                              state->conn->write_fd, false, iov, num_iov);
2661         if (subreq == NULL) {
2662                 return NT_STATUS_NO_MEMORY;
2663         }
2664         tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2665         return NT_STATUS_OK;
2666 }
2667
2668 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2669 {
2670         struct smbXcli_req_state *state =
2671                 tevent_req_data(req,
2672                 struct smbXcli_req_state);
2673
2674         state->smb2.credit_charge = charge;
2675 }
2676
2677 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2678                                     struct tevent_context *ev,
2679                                     struct smbXcli_conn *conn,
2680                                     uint16_t cmd,
2681                                     uint32_t additional_flags,
2682                                     uint32_t clear_flags,
2683                                     uint32_t timeout_msec,
2684                                     uint32_t pid,
2685                                     uint32_t tid,
2686                                     struct smbXcli_session *session,
2687                                     const uint8_t *fixed,
2688                                     uint16_t fixed_len,
2689                                     const uint8_t *dyn,
2690                                     uint32_t dyn_len)
2691 {
2692         struct tevent_req *req;
2693         NTSTATUS status;
2694
2695         req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2696                                  additional_flags, clear_flags,
2697                                  timeout_msec,
2698                                  pid, tid, session,
2699                                  fixed, fixed_len, dyn, dyn_len);
2700         if (req == NULL) {
2701                 return NULL;
2702         }
2703         if (!tevent_req_is_in_progress(req)) {
2704                 return tevent_req_post(req, ev);
2705         }
2706         status = smb2cli_req_compound_submit(&req, 1);
2707         if (tevent_req_nterror(req, status)) {
2708                 return tevent_req_post(req, ev);
2709         }
2710         return req;
2711 }
2712
2713 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2714 {
2715         struct tevent_req *req =
2716                 tevent_req_callback_data(subreq,
2717                 struct tevent_req);
2718         struct smbXcli_req_state *state =
2719                 tevent_req_data(req,
2720                 struct smbXcli_req_state);
2721         ssize_t nwritten;
2722         int err;
2723
2724         nwritten = writev_recv(subreq, &err);
2725         TALLOC_FREE(subreq);
2726         if (nwritten == -1) {
2727                 /* here, we need to notify all pending requests */
2728                 NTSTATUS status = map_nt_error_from_unix_common(err);
2729                 smbXcli_conn_disconnect(state->conn, status);
2730                 return;
2731         }
2732 }
2733
2734 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2735                                              struct iovec **piov, int *pnum_iov)
2736 {
2737         struct iovec *iov;
2738         int num_iov;
2739         size_t buflen;
2740         size_t taken;
2741         uint8_t *first_hdr;
2742
2743         num_iov = 0;
2744
2745         iov = talloc_array(mem_ctx, struct iovec, num_iov);
2746         if (iov == NULL) {
2747                 return NT_STATUS_NO_MEMORY;
2748         }
2749
2750         buflen = smb_len_tcp(buf);
2751         taken = 0;
2752         first_hdr = buf + NBT_HDR_SIZE;
2753
2754         while (taken < buflen) {
2755                 size_t len = buflen - taken;
2756                 uint8_t *hdr = first_hdr + taken;
2757                 struct iovec *cur;
2758                 size_t full_size;
2759                 size_t next_command_ofs;
2760                 uint16_t body_size;
2761                 struct iovec *iov_tmp;
2762
2763                 /*
2764                  * We need the header plus the body length field
2765                  */
2766
2767                 if (len < SMB2_HDR_BODY + 2) {
2768                         DEBUG(10, ("%d bytes left, expected at least %d\n",
2769                                    (int)len, SMB2_HDR_BODY));
2770                         goto inval;
2771                 }
2772                 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2773                         DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2774                                    IVAL(hdr, 0)));
2775                         goto inval;
2776                 }
2777                 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2778                         DEBUG(10, ("Got HDR len %d, expected %d\n",
2779                                    SVAL(hdr, 4), SMB2_HDR_BODY));
2780                         goto inval;
2781                 }
2782
2783                 full_size = len;
2784                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2785                 body_size = SVAL(hdr, SMB2_HDR_BODY);
2786
2787                 if (next_command_ofs != 0) {
2788                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2789                                 goto inval;
2790                         }
2791                         if (next_command_ofs > full_size) {
2792                                 goto inval;
2793                         }
2794                         full_size = next_command_ofs;
2795                 }
2796                 if (body_size < 2) {
2797                         goto inval;
2798                 }
2799                 body_size &= 0xfffe;
2800
2801                 if (body_size > (full_size - SMB2_HDR_BODY)) {
2802                         goto inval;
2803                 }
2804
2805                 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2806                                          num_iov + 3);
2807                 if (iov_tmp == NULL) {
2808                         TALLOC_FREE(iov);
2809                         return NT_STATUS_NO_MEMORY;
2810                 }
2811                 iov = iov_tmp;
2812                 cur = &iov[num_iov];
2813                 num_iov += 3;
2814
2815                 cur[0].iov_base = hdr;
2816                 cur[0].iov_len  = SMB2_HDR_BODY;
2817                 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2818                 cur[1].iov_len  = body_size;
2819                 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2820                 cur[2].iov_len  = full_size - (SMB2_HDR_BODY + body_size);
2821
2822                 taken += full_size;
2823         }
2824
2825         *piov = iov;
2826         *pnum_iov = num_iov;
2827         return NT_STATUS_OK;
2828
2829 inval:
2830         TALLOC_FREE(iov);
2831         return NT_STATUS_INVALID_NETWORK_RESPONSE;
2832 }
2833
2834 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2835                                                     uint64_t mid)
2836 {
2837         size_t num_pending = talloc_array_length(conn->pending);
2838         size_t i;
2839
2840         for (i=0; i<num_pending; i++) {
2841                 struct tevent_req *req = conn->pending[i];
2842                 struct smbXcli_req_state *state =
2843                         tevent_req_data(req,
2844                         struct smbXcli_req_state);
2845
2846                 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2847                         return req;
2848                 }
2849         }
2850         return NULL;
2851 }
2852
2853 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2854                                                TALLOC_CTX *tmp_mem,
2855                                                uint8_t *inbuf)
2856 {
2857         struct tevent_req *req;
2858         struct smbXcli_req_state *state = NULL;
2859         struct iovec *iov;
2860         int i, num_iov;
2861         NTSTATUS status;
2862         bool defer = true;
2863         struct smbXcli_session *last_session = NULL;
2864
2865         status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2866                                               &iov, &num_iov);
2867         if (!NT_STATUS_IS_OK(status)) {
2868                 return status;
2869         }
2870
2871         for (i=0; i<num_iov; i+=3) {
2872                 uint8_t *inbuf_ref = NULL;
2873                 struct iovec *cur = &iov[i];
2874                 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2875                 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2876                 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2877                 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2878                 uint16_t req_opcode;
2879                 uint32_t req_flags;
2880                 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2881                 uint32_t new_credits;
2882                 struct smbXcli_session *session = NULL;
2883                 const DATA_BLOB *signing_key = NULL;
2884                 bool should_sign = false;
2885
2886                 new_credits = conn->smb2.cur_credits;
2887                 new_credits += credits;
2888                 if (new_credits > UINT16_MAX) {
2889                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
2890                 }
2891                 conn->smb2.cur_credits += credits;
2892
2893                 req = smb2cli_conn_find_pending(conn, mid);
2894                 if (req == NULL) {
2895                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
2896                 }
2897                 state = tevent_req_data(req, struct smbXcli_req_state);
2898
2899                 state->smb2.got_async = false;
2900
2901                 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2902                 if (opcode != req_opcode) {
2903                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
2904                 }
2905                 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2906
2907                 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2908                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
2909                 }
2910
2911                 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2912                 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2913                     NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2914                         uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2915
2916                         /*
2917                          * async interim responses are not signed,
2918                          * even if the SMB2_HDR_FLAG_SIGNED flag
2919                          * is set.
2920                          */
2921                         req_flags |= SMB2_HDR_FLAG_ASYNC;
2922                         SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2923                         SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2924
2925                         if (state->smb2.notify_async) {
2926                                 state->smb2.got_async = true;
2927                                 tevent_req_defer_callback(req, state->ev);
2928                                 tevent_req_notify_callback(req);
2929                         }
2930                         continue;
2931                 }
2932
2933                 session = state->session;
2934                 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2935                         session = last_session;
2936                 }
2937                 last_session = session;
2938
2939                 if (session) {
2940                         should_sign = session->smb2.should_sign;
2941                         if (opcode == SMB2_OP_SESSSETUP &&
2942                             session->smb2.signing_key.length != 0) {
2943                                 should_sign = true;
2944                         }
2945                 }
2946
2947                 if (should_sign) {
2948                         if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2949                                 return NT_STATUS_ACCESS_DENIED;
2950                         }
2951                 }
2952
2953                 if (flags & SMB2_HDR_FLAG_SIGNED) {
2954                         uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2955
2956                         if (session == NULL) {
2957                                 struct smbXcli_session *s;
2958
2959                                 s = state->conn->sessions;
2960                                 for (; s; s = s->next) {
2961                                         if (s->smb2.session_id != uid) {
2962                                                 continue;
2963                                         }
2964
2965                                         session = s;
2966                                         break;
2967                                 }
2968                         }
2969
2970                         if (session == NULL) {
2971                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2972                         }
2973
2974                         last_session = session;
2975                         signing_key = &session->smb2.channel_signing_key;
2976                 }
2977
2978                 if (opcode == SMB2_OP_SESSSETUP) {
2979                         /*
2980                          * We prefer the channel signing key, if it is
2981                          * already there.
2982                          *
2983                          * If we do not have a channel signing key yet,
2984                          * we try the main signing key, if it is not
2985                          * the final response.
2986                          */
2987                         if (signing_key && signing_key->length == 0 &&
2988                             !NT_STATUS_IS_OK(status)) {
2989                                 signing_key = &session->smb2.signing_key;
2990                         }
2991
2992                         if (signing_key && signing_key->length == 0) {
2993                                 /*
2994                                  * If we do not have a session key to
2995                                  * verify the signature, we defer the
2996                                  * signing check to the caller.
2997                                  *
2998                                  * The caller gets NT_STATUS_OK, it
2999                                  * has to call
3000                                  * smb2cli_session_set_session_key()
3001                                  * or
3002                                  * smb2cli_session_set_channel_key()
3003                                  * which will check the signature
3004                                  * with the channel signing key.
3005                                  */
3006                                 signing_key = NULL;
3007                         }
3008                 }
3009
3010                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3011                         /*
3012                          * if the server returns NT_STATUS_USER_SESSION_DELETED
3013                          * the response is not signed and we should
3014                          * propagate the NT_STATUS_USER_SESSION_DELETED
3015                          * status to the caller.
3016                          */
3017                         signing_key = NULL;
3018                 }
3019
3020                 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3021                     NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3022                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3023                         /*
3024                          * if the server returns
3025                          * NT_STATUS_NETWORK_NAME_DELETED
3026                          * NT_STATUS_FILE_CLOSED
3027                          * NT_STATUS_INVALID_PARAMETER
3028                          * the response might not be signed
3029                          * as this happens before the signing checks.
3030                          *
3031                          * If server echos the signature (or all zeros)
3032                          * we should report the status from the server
3033                          * to the caller.
3034                          */
3035                         if (signing_key) {
3036                                 int cmp;
3037
3038                                 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3039                                              state->smb2.hdr+SMB2_HDR_SIGNATURE,
3040                                              16);
3041                                 if (cmp == 0) {
3042                                         state->smb2.signing_skipped = true;
3043                                         signing_key = NULL;
3044                                 }
3045                         }
3046                         if (signing_key) {
3047                                 int cmp;
3048                                 static const uint8_t zeros[16];
3049
3050                                 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3051                                              zeros,
3052                                              16);
3053                                 if (cmp == 0) {
3054                                         state->smb2.signing_skipped = true;
3055                                         signing_key = NULL;
3056                                 }
3057                         }
3058                 }
3059
3060                 if (signing_key) {
3061                         status = smb2_signing_check_pdu(*signing_key,
3062                                                         state->conn->protocol,
3063                                                         cur, 3);
3064                         if (!NT_STATUS_IS_OK(status)) {
3065                                 /*
3066                                  * If the signing check fails, we disconnect
3067                                  * the connection.
3068                                  */
3069                                 return status;
3070                         }
3071                 }
3072
3073                 smbXcli_req_unset_pending(req);
3074
3075                 /*
3076                  * There might be more than one response
3077                  * we need to defer the notifications
3078                  */
3079                 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
3080                         defer = false;
3081                 }
3082
3083                 if (defer) {
3084                         tevent_req_defer_callback(req, state->ev);
3085                 }
3086
3087                 /*
3088                  * Note: here we use talloc_reference() in a way
3089                  *       that does not expose it to the caller.
3090                  */
3091                 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3092                 if (tevent_req_nomem(inbuf_ref, req)) {
3093                         continue;
3094                 }
3095
3096                 /* copy the related buffers */
3097                 state->smb2.recv_iov[0] = cur[0];
3098                 state->smb2.recv_iov[1] = cur[1];
3099                 state->smb2.recv_iov[2] = cur[2];
3100
3101                 tevent_req_done(req);
3102         }
3103
3104         if (defer) {
3105                 return NT_STATUS_RETRY;
3106         }
3107
3108         return NT_STATUS_OK;
3109 }
3110
3111 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3112                           struct iovec **piov,
3113                           const struct smb2cli_req_expected_response *expected,
3114                           size_t num_expected)
3115 {
3116         struct smbXcli_req_state *state =
3117                 tevent_req_data(req,
3118                 struct smbXcli_req_state);
3119         NTSTATUS status;
3120         size_t body_size;
3121         bool found_status = false;
3122         bool found_size = false;
3123         size_t i;
3124
3125         if (piov != NULL) {
3126                 *piov = NULL;
3127         }
3128
3129         if (state->smb2.got_async) {
3130                 return STATUS_PENDING;
3131         }
3132
3133         if (tevent_req_is_nterror(req, &status)) {
3134                 for (i=0; i < num_expected; i++) {
3135                         if (NT_STATUS_EQUAL(status, expected[i].status)) {
3136                                 found_status = true;
3137                                 break;
3138                         }
3139                 }
3140
3141                 if (found_status) {
3142                         return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3143                 }
3144
3145                 return status;
3146         }
3147
3148         if (num_expected == 0) {
3149                 found_status = true;
3150                 found_size = true;
3151         }
3152
3153         status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3154         body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3155
3156         for (i=0; i < num_expected; i++) {
3157                 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3158                         continue;
3159                 }
3160
3161                 found_status = true;
3162                 if (expected[i].body_size == 0) {
3163                         found_size = true;
3164                         break;
3165                 }
3166
3167                 if (expected[i].body_size == body_size) {
3168                         found_size = true;
3169                         break;
3170                 }
3171         }
3172
3173         if (!found_status) {
3174                 return status;
3175         }
3176
3177         if (state->smb2.signing_skipped) {
3178                 if (num_expected > 0) {
3179                         return NT_STATUS_ACCESS_DENIED;
3180                 }
3181                 if (!NT_STATUS_IS_ERR(status)) {
3182                         return NT_STATUS_ACCESS_DENIED;
3183                 }
3184         }
3185
3186         if (!found_size) {
3187                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3188         }
3189
3190         if (piov != NULL) {
3191                 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3192         }
3193
3194         return status;
3195 }
3196
3197 static const struct {
3198         enum protocol_types proto;
3199         const char *smb1_name;
3200 } smb1cli_prots[] = {
3201         {PROTOCOL_CORE,         "PC NETWORK PROGRAM 1.0"},
3202         {PROTOCOL_COREPLUS,     "MICROSOFT NETWORKS 1.03"},
3203         {PROTOCOL_LANMAN1,      "MICROSOFT NETWORKS 3.0"},
3204         {PROTOCOL_LANMAN1,      "LANMAN1.0"},
3205         {PROTOCOL_LANMAN2,      "LM1.2X002"},
3206         {PROTOCOL_LANMAN2,      "DOS LANMAN2.1"},
3207         {PROTOCOL_LANMAN2,      "LANMAN2.1"},
3208         {PROTOCOL_LANMAN2,      "Samba"},
3209         {PROTOCOL_NT1,          "NT LANMAN 1.0"},
3210         {PROTOCOL_NT1,          "NT LM 0.12"},
3211         {PROTOCOL_SMB2_02,      "SMB 2.002"},
3212         {PROTOCOL_SMB2_10,      "SMB 2.???"},
3213 };
3214
3215 static const struct {
3216         enum protocol_types proto;
3217         uint16_t smb2_dialect;
3218 } smb2cli_prots[] = {
3219         {PROTOCOL_SMB2_02,      SMB2_DIALECT_REVISION_202},
3220         {PROTOCOL_SMB2_10,      SMB2_DIALECT_REVISION_210},
3221         {PROTOCOL_SMB2_22,      SMB2_DIALECT_REVISION_222},
3222         {PROTOCOL_SMB2_24,      SMB2_DIALECT_REVISION_224},
3223         {PROTOCOL_SMB3_00,      SMB3_DIALECT_REVISION_300},
3224 };
3225
3226 struct smbXcli_negprot_state {
3227         struct smbXcli_conn *conn;
3228         struct tevent_context *ev;
3229         uint32_t timeout_msec;
3230         enum protocol_types min_protocol;
3231         enum protocol_types max_protocol;
3232
3233         struct {
3234                 uint8_t fixed[36];
3235                 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3236         } smb2;
3237 };
3238
3239 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3240 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3241 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3242 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3243 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3244 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3245                                                   TALLOC_CTX *frame,
3246                                                   uint8_t *inbuf);
3247
3248 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3249                                         struct tevent_context *ev,
3250                                         struct smbXcli_conn *conn,
3251                                         uint32_t timeout_msec,
3252                                         enum protocol_types min_protocol,
3253                                         enum protocol_types max_protocol)
3254 {
3255         struct tevent_req *req, *subreq;
3256         struct smbXcli_negprot_state *state;
3257
3258         req = tevent_req_create(mem_ctx, &state,
3259                                 struct smbXcli_negprot_state);
3260         if (req == NULL) {
3261                 return NULL;
3262         }
3263         state->conn = conn;
3264         state->ev = ev;
3265         state->timeout_msec = timeout_msec;
3266         state->min_protocol = min_protocol;
3267         state->max_protocol = max_protocol;
3268
3269         if (min_protocol == PROTOCOL_NONE) {
3270                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3271                 return tevent_req_post(req, ev);
3272         }
3273
3274         if (max_protocol == PROTOCOL_NONE) {
3275                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3276                 return tevent_req_post(req, ev);
3277         }
3278
3279         if (min_protocol > max_protocol) {
3280                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3281                 return tevent_req_post(req, ev);
3282         }
3283
3284         if ((min_protocol < PROTOCOL_SMB2_02) &&
3285             (max_protocol < PROTOCOL_SMB2_02)) {
3286                 /*
3287                  * SMB1 only...
3288                  */
3289                 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3290
3291                 subreq = smbXcli_negprot_smb1_subreq(state);
3292                 if (tevent_req_nomem(subreq, req)) {
3293                         return tevent_req_post(req, ev);
3294                 }
3295                 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3296                 return req;
3297         }
3298
3299         if ((min_protocol >= PROTOCOL_SMB2_02) &&
3300             (max_protocol >= PROTOCOL_SMB2_02)) {
3301                 /*
3302                  * SMB2 only...
3303                  */
3304                 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3305
3306                 subreq = smbXcli_negprot_smb2_subreq(state);
3307                 if (tevent_req_nomem(subreq, req)) {
3308                         return tevent_req_post(req, ev);
3309                 }
3310                 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3311                 return req;
3312         }
3313
3314         /*
3315          * We send an SMB1 negprot with the SMB2 dialects
3316          * and expect a SMB1 or a SMB2 response.
3317          *
3318          * smbXcli_negprot_dispatch_incoming() will fix the
3319          * callback to match protocol of the response.
3320          */
3321         conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3322
3323         subreq = smbXcli_negprot_smb1_subreq(state);
3324         if (tevent_req_nomem(subreq, req)) {
3325                 return tevent_req_post(req, ev);
3326         }
3327         tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3328         return req;
3329 }
3330
3331 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3332 {
3333         struct tevent_req *req =
3334                 tevent_req_callback_data(subreq,
3335                 struct tevent_req);
3336         NTSTATUS status;
3337
3338         /*
3339          * we just want the low level error
3340          */
3341         status = tevent_req_simple_recv_ntstatus(subreq);
3342         TALLOC_FREE(subreq);
3343         if (tevent_req_nterror(req, status)) {
3344                 return;
3345         }
3346
3347         /* this should never happen */
3348         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3349 }
3350
3351 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3352 {
3353         size_t i;
3354         DATA_BLOB bytes = data_blob_null;
3355         uint8_t flags;
3356         uint16_t flags2;
3357
3358         /* setup the protocol strings */
3359         for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3360                 uint8_t c = 2;
3361                 bool ok;
3362
3363                 if (smb1cli_prots[i].proto < state->min_protocol) {
3364                         continue;
3365                 }
3366
3367                 if (smb1cli_prots[i].proto > state->max_protocol) {
3368                         continue;
3369                 }
3370
3371                 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3372                 if (!ok) {
3373                         return NULL;
3374                 }
3375
3376                 /*
3377                  * We now it is already ascii and
3378                  * we want NULL termination.
3379                  */
3380                 ok = data_blob_append(state, &bytes,
3381                                       smb1cli_prots[i].smb1_name,
3382                                       strlen(smb1cli_prots[i].smb1_name)+1);
3383                 if (!ok) {
3384                         return NULL;
3385                 }
3386         }
3387
3388         smb1cli_req_flags(state->max_protocol,
3389                           state->conn->smb1.client.capabilities,
3390                           SMBnegprot,
3391                           0, 0, &flags,
3392                           0, 0, &flags2);
3393
3394         return smb1cli_req_send(state, state->ev, state->conn,
3395                                 SMBnegprot,
3396                                 flags, ~flags,
3397                                 flags2, ~flags2,
3398                                 state->timeout_msec,
3399                                 0xFFFE, 0, 0, /* pid, tid, uid */
3400                                 0, NULL, /* wct, vwv */
3401                                 bytes.length, bytes.data);
3402 }
3403
3404 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3405 {
3406         struct tevent_req *req =
3407                 tevent_req_callback_data(subreq,
3408                 struct tevent_req);
3409         struct smbXcli_negprot_state *state =
3410                 tevent_req_data(req,
3411                 struct smbXcli_negprot_state);
3412         struct smbXcli_conn *conn = state->conn;
3413         struct iovec *recv_iov = NULL;
3414         uint8_t *inhdr;
3415         uint8_t wct;
3416         uint16_t *vwv;
3417         uint32_t num_bytes;
3418         uint8_t *bytes;
3419         NTSTATUS status;
3420         uint16_t protnum;
3421         size_t i;
3422         size_t num_prots = 0;
3423         uint8_t flags;
3424         uint32_t client_capabilities = conn->smb1.client.capabilities;
3425         uint32_t both_capabilities;
3426         uint32_t server_capabilities = 0;
3427         uint32_t capabilities;
3428         uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3429         uint32_t server_max_xmit = 0;
3430         uint32_t max_xmit;
3431         uint32_t server_max_mux = 0;
3432         uint16_t server_security_mode = 0;
3433         uint32_t server_session_key = 0;
3434         bool server_readbraw = false;
3435         bool server_writebraw = false;
3436         bool server_lockread = false;
3437         bool server_writeunlock = false;
3438         struct GUID server_guid = GUID_zero();
3439         DATA_BLOB server_gss_blob = data_blob_null;
3440         uint8_t server_challenge[8];
3441         char *server_workgroup = NULL;
3442         char *server_name = NULL;
3443         int server_time_zone = 0;
3444         NTTIME server_system_time = 0;
3445         static const struct smb1cli_req_expected_response expected[] = {
3446         {
3447                 .status = NT_STATUS_OK,
3448                 .wct = 0x11, /* NT1 */
3449         },
3450         {
3451                 .status = NT_STATUS_OK,
3452                 .wct = 0x0D, /* LM */
3453         },
3454         {
3455                 .status = NT_STATUS_OK,
3456                 .wct = 0x01, /* CORE */
3457         }
3458         };
3459
3460         ZERO_STRUCT(server_challenge);
3461
3462         status = smb1cli_req_recv(subreq, state,
3463                                   &recv_iov,
3464                                   &inhdr,
3465                                   &wct,
3466                                   &vwv,
3467                                   NULL, /* pvwv_offset */
3468                                   &num_bytes,
3469                                   &bytes,
3470                                   NULL, /* pbytes_offset */
3471                                   NULL, /* pinbuf */
3472                                   expected, ARRAY_SIZE(expected));
3473         TALLOC_FREE(subreq);
3474         if (tevent_req_nterror(req, status)) {
3475                 return;
3476         }
3477
3478         flags = CVAL(inhdr, HDR_FLG);
3479
3480         protnum = SVAL(vwv, 0);
3481
3482         for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3483                 if (smb1cli_prots[i].proto < state->min_protocol) {
3484                         continue;
3485                 }
3486
3487                 if (smb1cli_prots[i].proto > state->max_protocol) {
3488                         continue;
3489                 }
3490
3491                 if (protnum != num_prots) {
3492                         num_prots++;
3493                         continue;
3494                 }
3495
3496                 conn->protocol = smb1cli_prots[i].proto;
3497                 break;
3498         }
3499
3500         if (conn->protocol == PROTOCOL_NONE) {
3501                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3502                 return;
3503         }
3504
3505         if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3506                 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3507                          "and the selected protocol level doesn't support it.\n"));
3508                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3509                 return;
3510         }
3511
3512         if (flags & FLAG_SUPPORT_LOCKREAD) {
3513                 server_lockread = true;
3514                 server_writeunlock = true;
3515         }
3516
3517         if (conn->protocol >= PROTOCOL_NT1) {
3518                 const char *client_signing = NULL;
3519                 bool server_mandatory = false;
3520                 bool server_allowed = false;
3521                 const char *server_signing = NULL;
3522                 bool ok;
3523                 uint8_t key_len;
3524
3525                 if (wct != 0x11) {
3526                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3527                         return;
3528                 }
3529
3530                 /* NT protocol */
3531                 server_security_mode = CVAL(vwv + 1, 0);
3532                 server_max_mux = SVAL(vwv + 1, 1);
3533                 server_max_xmit = IVAL(vwv + 3, 1);
3534                 server_session_key = IVAL(vwv + 7, 1);
3535                 server_time_zone = SVALS(vwv + 15, 1);
3536                 server_time_zone *= 60;
3537                 /* this time arrives in real GMT */
3538                 server_system_time = BVAL(vwv + 11, 1);
3539                 server_capabilities = IVAL(vwv + 9, 1);
3540
3541                 key_len = CVAL(vwv + 16, 1);
3542
3543                 if (server_capabilities & CAP_RAW_MODE) {
3544                         server_readbraw = true;
3545                         server_writebraw = true;
3546                 }
3547                 if (server_capabilities & CAP_LOCK_AND_READ) {
3548                         server_lockread = true;
3549                 }
3550
3551                 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3552                         DATA_BLOB blob1, blob2;
3553
3554                         if (num_bytes < 16) {
3555                                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3556                                 return;
3557                         }
3558
3559                         blob1 = data_blob_const(bytes, 16);
3560                         status = GUID_from_data_blob(&blob1, &server_guid);
3561                         if (tevent_req_nterror(req, status)) {
3562                                 return;
3563                         }
3564
3565                         blob1 = data_blob_const(bytes+16, num_bytes-16);
3566                         blob2 = data_blob_dup_talloc(state, blob1);
3567                         if (blob1.length > 0 &&
3568                             tevent_req_nomem(blob2.data, req)) {
3569                                 return;
3570                         }
3571                         server_gss_blob = blob2;
3572                 } else {
3573                         DATA_BLOB blob1, blob2;
3574
3575                         if (num_bytes < key_len) {
3576                                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3577                                 return;
3578                         }
3579
3580                         if (key_len != 0 && key_len != 8) {
3581                                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3582                                 return;
3583                         }
3584
3585                         if (key_len == 8) {
3586                                 memcpy(server_challenge, bytes, 8);
3587                         }
3588
3589                         blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3590                         blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3591                         if (blob1.length > 0) {
3592                                 size_t len;
3593
3594                                 len = utf16_len_n(blob1.data,
3595                                                   blob1.length);
3596                                 blob1.length = len;
3597
3598                                 ok = convert_string_talloc(state,
3599                                                            CH_UTF16LE,
3600                                                            CH_UNIX,
3601                                                            blob1.data,
3602                                                            blob1.length,
3603                                                            &server_workgroup,
3604                                                            &len);
3605                                 if (!ok) {
3606                                         status = map_nt_error_from_unix_common(errno);
3607                                         tevent_req_nterror(req, status);
3608                                         return;
3609                                 }
3610                         }
3611
3612                         blob2.data += blob1.length;
3613                         blob2.length -= blob1.length;
3614                         if (blob2.length > 0) {
3615                                 size_t len;
3616
3617                                 len = utf16_len_n(blob1.data,
3618                                                   blob1.length);
3619                                 blob1.length = len;
3620
3621                                 ok = convert_string_talloc(state,
3622                                                            CH_UTF16LE,
3623                                                            CH_UNIX,
3624                                                            blob2.data,
3625                                                            blob2.length,
3626                                                            &server_name,
3627                                                            &len);
3628                                 if (!ok) {
3629                                         status = map_nt_error_from_unix_common(errno);
3630                                         tevent_req_nterror(req, status);
3631                                         return;
3632                                 }
3633                         }
3634                 }
3635
3636                 client_signing = "disabled";
3637                 if (conn->allow_signing) {
3638                         client_signing = "allowed";
3639                 }
3640                 if (conn->mandatory_signing) {
3641                         client_signing = "required";
3642                 }
3643
3644                 server_signing = "not supported";
3645                 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3646                         server_signing = "supported";
3647                         server_allowed = true;
3648                 }
3649                 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3650                         server_signing = "required";
3651                         server_mandatory = true;
3652                 }
3653
3654                 ok = smb_signing_set_negotiated(conn->smb1.signing,
3655                                                 server_allowed,
3656                                                 server_mandatory);
3657                 if (!ok) {
3658                         DEBUG(1,("cli_negprot: SMB signing is required, "
3659                                  "but client[%s] and server[%s] mismatch\n",
3660                                  client_signing, server_signing));
3661                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3662                         return;
3663                 }
3664
3665         } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3666                 DATA_BLOB blob1;
3667                 uint8_t key_len;
3668                 time_t t;
3669
3670                 if (wct != 0x0D) {
3671                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3672                         return;
3673                 }
3674
3675                 server_security_mode = SVAL(vwv + 1, 0);
3676                 server_max_xmit = SVAL(vwv + 2, 0);
3677                 server_max_mux = SVAL(vwv + 3, 0);
3678                 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3679                 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3680                 server_session_key = IVAL(vwv + 6, 0);
3681                 server_time_zone = SVALS(vwv + 10, 0);
3682                 server_time_zone *= 60;
3683                 /* this time is converted to GMT by make_unix_date */
3684                 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3685                 unix_to_nt_time(&server_system_time, t);
3686                 key_len = SVAL(vwv + 11, 0);
3687
3688                 if (num_bytes < key_len) {
3689                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3690                         return;
3691                 }
3692
3693                 if (key_len != 0 && key_len != 8) {
3694                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3695                         return;
3696                 }
3697
3698                 if (key_len == 8) {
3699                         memcpy(server_challenge, bytes, 8);
3700                 }
3701
3702                 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3703                 if (blob1.length > 0) {
3704                         size_t len;
3705                         bool ok;
3706
3707                         len = utf16_len_n(blob1.data,
3708                                           blob1.length);
3709                         blob1.length = len;
3710
3711                         ok = convert_string_talloc(state,
3712                                                    CH_DOS,
3713                                                    CH_UNIX,
3714                                                    blob1.data,
3715                                                    blob1.length,
3716                                                    &server_workgroup,
3717                                                    &len);
3718                         if (!ok) {
3719                                 status = map_nt_error_from_unix_common(errno);
3720                                 tevent_req_nterror(req, status);
3721                                 return;
3722                         }
3723                 }
3724
3725         } else {
3726                 /* the old core protocol */
3727                 server_time_zone = get_time_zone(time(NULL));
3728                 server_max_xmit = 1024;
3729                 server_max_mux = 1;
3730         }
3731
3732         if (server_max_xmit < 1024) {
3733                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3734                 return;
3735         }
3736
3737         if (server_max_mux < 1) {
3738                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3739                 return;
3740         }
3741
3742         /*
3743          * Now calculate the negotiated capabilities
3744          * based on the mask for:
3745          * - client only flags
3746          * - flags used in both directions
3747          * - server only flags
3748          */
3749         both_capabilities = client_capabilities & server_capabilities;
3750         capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3751         capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3752         capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3753
3754         max_xmit = MIN(client_max_xmit, server_max_xmit);
3755
3756         conn->smb1.server.capabilities = server_capabilities;
3757         conn->smb1.capabilities = capabilities;
3758
3759         conn->smb1.server.max_xmit = server_max_xmit;
3760         conn->smb1.max_xmit = max_xmit;
3761
3762         conn->smb1.server.max_mux = server_max_mux;
3763
3764         conn->smb1.server.security_mode = server_security_mode;
3765
3766         conn->smb1.server.readbraw = server_readbraw;
3767         conn->smb1.server.writebraw = server_writebraw;
3768         conn->smb1.server.lockread = server_lockread;
3769         conn->smb1.server.writeunlock = server_writeunlock;
3770
3771         conn->smb1.server.session_key = server_session_key;
3772
3773         talloc_steal(conn, server_gss_blob.data);
3774         conn->smb1.server.gss_blob = server_gss_blob;
3775         conn->smb1.server.guid = server_guid;
3776         memcpy(conn->smb1.server.challenge, server_challenge, 8);
3777         conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3778         conn->smb1.server.name = talloc_move(conn, &server_name);
3779
3780         conn->smb1.server.time_zone = server_time_zone;
3781         conn->smb1.server.system_time = server_system_time;
3782
3783         tevent_req_done(req);
3784 }
3785
3786 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3787 {
3788         size_t i;
3789         uint8_t *buf;
3790         uint16_t dialect_count = 0;
3791
3792         buf = state->smb2.dyn;
3793         for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3794                 if (smb2cli_prots[i].proto < state->min_protocol) {
3795                         continue;
3796                 }
3797
3798                 if (smb2cli_prots[i].proto > state->max_protocol) {
3799                         continue;
3800                 }
3801
3802                 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3803                 dialect_count++;
3804         }
3805
3806         buf = state->smb2.fixed;
3807         SSVAL(buf, 0, 36);
3808         SSVAL(buf, 2, dialect_count);
3809         SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3810         SSVAL(buf, 6, 0);       /* Reserved */
3811         if (state->max_protocol >= PROTOCOL_SMB2_22) {
3812                 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3813         } else {
3814                 SIVAL(buf, 8, 0);       /* Capabilities */
3815         }
3816         if (state->max_protocol >= PROTOCOL_SMB2_10) {
3817                 NTSTATUS status;
3818                 DATA_BLOB blob;
3819
3820                 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3821                                           state, &blob);
3822                 if (!NT_STATUS_IS_OK(status)) {
3823                         return NULL;
3824                 }
3825                 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3826         } else {
3827                 memset(buf+12, 0, 16);  /* ClientGuid */
3828         }
3829         SBVAL(buf, 28, 0);      /* ClientStartTime */
3830
3831         return smb2cli_req_send(state, state->ev,
3832                                 state->conn, SMB2_OP_NEGPROT,
3833                                 0, 0, /* flags */
3834                                 state->timeout_msec,
3835                                 0xFEFF, 0, NULL, /* pid, tid, session */
3836                                 state->smb2.fixed, sizeof(state->smb2.fixed),
3837                                 state->smb2.dyn, dialect_count*2);
3838 }
3839
3840 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3841 {
3842         struct tevent_req *req =
3843                 tevent_req_callback_data(subreq,
3844                 struct tevent_req);
3845         struct smbXcli_negprot_state *state =
3846                 tevent_req_data(req,
3847                 struct smbXcli_negprot_state);
3848         struct smbXcli_conn *conn = state->conn;
3849         size_t security_offset, security_length;
3850         DATA_BLOB blob;
3851         NTSTATUS status;
3852         struct iovec *iov;
3853         uint8_t *body;
3854         size_t i;
3855         uint16_t dialect_revision;
3856         static const struct smb2cli_req_expected_response expected[] = {
3857         {
3858                 .status = NT_STATUS_OK,
3859                 .body_size = 0x41
3860         }
3861         };
3862
3863         status = smb2cli_req_recv(subreq, state, &iov,
3864                                   expected, ARRAY_SIZE(expected));
3865         TALLOC_FREE(subreq);
3866         if (tevent_req_nterror(req, status)) {
3867                 return;
3868         }
3869
3870         body = (uint8_t *)iov[1].iov_base;
3871
3872         dialect_revision = SVAL(body, 4);
3873
3874         for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3875                 if (smb2cli_prots[i].proto < state->min_protocol) {
3876                         continue;
3877                 }
3878
3879                 if (smb2cli_prots[i].proto > state->max_protocol) {
3880                         continue;
3881                 }
3882
3883                 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3884                         continue;
3885                 }
3886
3887                 conn->protocol = smb2cli_prots[i].proto;
3888                 break;
3889         }
3890
3891         if (conn->protocol == PROTOCOL_NONE) {
3892                 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3893                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3894                         return;
3895                 }
3896
3897                 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3898                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3899                         return;
3900                 }
3901
3902                 /* make sure we do not loop forever */
3903                 state->min_protocol = PROTOCOL_SMB2_02;
3904
3905                 /*
3906                  * send a SMB2 negprot, in order to negotiate
3907                  * the SMB2 dialect.
3908                  */
3909                 subreq = smbXcli_negprot_smb2_subreq(state);
3910                 if (tevent_req_nomem(subreq, req)) {
3911                         return;
3912                 }
3913                 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3914                 return;
3915         }
3916
3917         conn->smb2.server.security_mode = SVAL(body, 2);
3918
3919         blob = data_blob_const(body + 8, 16);
3920         status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3921         if (tevent_req_nterror(req, status)) {
3922                 return;
3923         }
3924
3925         conn->smb2.server.capabilities  = IVAL(body, 24);
3926         conn->smb2.server.max_trans_size= IVAL(body, 28);
3927         conn->smb2.server.max_read_size = IVAL(body, 32);
3928         conn->smb2.server.max_write_size= IVAL(body, 36);
3929         conn->smb2.server.system_time   = BVAL(body, 40);
3930         conn->smb2.server.start_time    = BVAL(body, 48);
3931
3932         security_offset = SVAL(body, 56);
3933         security_length = SVAL(body, 58);
3934
3935         if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3936                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3937                 return;
3938         }
3939
3940         if (security_length > iov[2].iov_len) {
3941                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3942                 return;
3943         }
3944
3945         conn->smb2.server.gss_blob = data_blob_talloc(conn,
3946                                                 iov[2].iov_base,
3947                                                 security_length);
3948         if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3949                 return;
3950         }
3951
3952         tevent_req_done(req);
3953 }
3954
3955 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3956                                                   TALLOC_CTX *tmp_mem,
3957                                                   uint8_t *inbuf)
3958 {
3959         size_t num_pending = talloc_array_length(conn->pending);
3960         struct tevent_req *subreq;
3961         struct smbXcli_req_state *substate;
3962         struct tevent_req *req;
3963         uint32_t protocol_magic = IVAL(inbuf, 4);
3964
3965         if (num_pending != 1) {
3966                 return NT_STATUS_INTERNAL_ERROR;
3967         }
3968
3969         subreq = conn->pending[0];
3970         substate = tevent_req_data(subreq, struct smbXcli_req_state);
3971         req = tevent_req_callback_data(subreq, struct tevent_req);
3972
3973         switch (protocol_magic) {
3974         case SMB_MAGIC:
3975                 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3976                 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3977                 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3978
3979         case SMB2_MAGIC:
3980                 if (substate->smb2.recv_iov == NULL) {
3981                         /*
3982                          * For the SMB1 negprot we have move it.
3983                          */
3984                         substate->smb2.recv_iov = substate->smb1.recv_iov;
3985                         substate->smb1.recv_iov = NULL;
3986                 }
3987
3988                 /*
3989                  * we got an SMB2 answer, which consumed sequence number 0
3990                  * so we need to use 1 as the next one
3991                  */
3992                 conn->smb2.mid = 1;
3993                 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3994                 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3995                 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3996         }
3997
3998         DEBUG(10, ("Got non-SMB PDU\n"));
3999         return NT_STATUS_INVALID_NETWORK_RESPONSE;
4000 }
4001
4002 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4003 {
4004         return tevent_req_simple_recv_ntstatus(req);
4005 }
4006
4007 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4008                          uint32_t timeout_msec,
4009                          enum protocol_types min_protocol,
4010                          enum protocol_types max_protocol)
4011 {
4012         TALLOC_CTX *frame = talloc_stackframe();
4013         struct tevent_context *ev;
4014         struct tevent_req *req;
4015         NTSTATUS status = NT_STATUS_NO_MEMORY;
4016         bool ok;
4017
4018         if (smbXcli_conn_has_async_calls(conn)) {
4019                 /*
4020                  * Can't use sync call while an async call is in flight
4021                  */
4022                 status = NT_STATUS_INVALID_PARAMETER_MIX;
4023                 goto fail;
4024         }
4025         ev = tevent_context_init(frame);
4026         if (ev == NULL) {
4027                 goto fail;
4028         }
4029         req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4030                                    min_protocol, max_protocol);
4031         if (req == NULL) {
4032                 goto fail;
4033         }
4034         ok = tevent_req_poll(req, ev);
4035         if (!ok) {
4036                 status = map_nt_error_from_unix_common(errno);
4037                 goto fail;
4038         }
4039         status = smbXcli_negprot_recv(req);
4040  fail:
4041         TALLOC_FREE(frame);
4042         return status;
4043 }
4044
4045 static int smbXcli_session_destructor(struct smbXcli_session *session)
4046 {
4047         if (session->conn == NULL) {
4048                 return 0;
4049         }
4050
4051         DLIST_REMOVE(session->conn->sessions, session);
4052         return 0;
4053 }
4054
4055 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4056                                                struct smbXcli_conn *conn)
4057 {
4058         struct smbXcli_session *session;
4059
4060         session = talloc_zero(mem_ctx, struct smbXcli_session);
4061         if (session == NULL) {
4062                 return NULL;
4063         }
4064         talloc_set_destructor(session, smbXcli_session_destructor);
4065
4066         DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4067         session->conn = conn;
4068
4069         return session;
4070 }
4071
4072 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4073 {
4074         struct smbXcli_conn *conn = session->conn;
4075         uint8_t security_mode = 0;
4076
4077         if (conn == NULL) {
4078                 return security_mode;
4079         }
4080
4081         security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4082         if (conn->mandatory_signing) {
4083                 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4084         }
4085
4086         return security_mode;
4087 }
4088
4089 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4090 {
4091         return session->smb2.session_id;
4092 }
4093
4094 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4095 {
4096         return session->smb2.session_flags;
4097 }
4098
4099 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4100                                          TALLOC_CTX *mem_ctx,
4101                                          DATA_BLOB *key)
4102 {
4103         *key = data_blob_null;
4104
4105         if (session->smb2.application_key.length == 0) {
4106                 return NT_STATUS_NO_USER_SESSION_KEY;
4107         }
4108
4109         *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4110         if (key->data == NULL) {
4111                 return NT_STATUS_NO_MEMORY;
4112         }
4113
4114         return NT_STATUS_OK;
4115 }
4116
4117 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4118                                       uint64_t session_id,
4119                                       uint16_t session_flags)
4120 {
4121         session->smb2.session_id = session_id;
4122         session->smb2.session_flags = session_flags;
4123 }
4124
4125 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4126                                          const DATA_BLOB _session_key,
4127                                          const struct iovec *recv_iov)
4128 {
4129         struct smbXcli_conn *conn = session->conn;
4130         uint16_t no_sign_flags;
4131         uint8_t session_key[16];
4132         NTSTATUS status;
4133
4134         if (conn == NULL) {
4135                 return NT_STATUS_INVALID_PARAMETER_MIX;
4136         }
4137
4138         no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4139
4140         if (session->smb2.session_flags & no_sign_flags) {
4141                 session->smb2.should_sign = false;
4142                 return NT_STATUS_OK;
4143         }
4144
4145         if (session->smb2.signing_key.length != 0) {
4146                 return NT_STATUS_INVALID_PARAMETER_MIX;
4147         }
4148
4149         ZERO_STRUCT(session_key);
4150         memcpy(session_key, _session_key.data,
4151                MIN(_session_key.length, sizeof(session_key)));
4152
4153         session->smb2.signing_key = data_blob_talloc(session,
4154                                                      session_key,
4155                                                      sizeof(session_key));
4156         if (session->smb2.signing_key.data == NULL) {
4157                 ZERO_STRUCT(session_key);
4158                 return NT_STATUS_NO_MEMORY;
4159         }
4160
4161         if (conn->protocol >= PROTOCOL_SMB2_24) {
4162                 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4163                 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4164
4165                 smb2_key_derivation(session_key, sizeof(session_key),
4166                                     label.data, label.length,
4167                                     context.data, context.length,
4168                                     session->smb2.signing_key.data);
4169         }
4170
4171         session->smb2.application_key = data_blob_dup_talloc(session,
4172                                                 session->smb2.signing_key);
4173         if (session->smb2.application_key.data == NULL) {
4174                 ZERO_STRUCT(session_key);
4175                 return NT_STATUS_NO_MEMORY;
4176         }
4177
4178         if (conn->protocol >= PROTOCOL_SMB2_24) {
4179                 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4180                 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4181
4182                 smb2_key_derivation(session_key, sizeof(session_key),
4183                                     label.data, label.length,
4184                                     context.data, context.length,
4185                                     session->smb2.application_key.data);
4186         }
4187         ZERO_STRUCT(session_key);
4188
4189         session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4190                                                 session->smb2.signing_key);
4191         if (session->smb2.channel_signing_key.data == NULL) {
4192                 return NT_STATUS_NO_MEMORY;
4193         }
4194
4195         status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4196                                         session->conn->protocol,
4197                                         recv_iov, 3);
4198         if (!NT_STATUS_IS_OK(status)) {
4199                 return status;
4200         }
4201
4202         session->smb2.should_sign = false;
4203
4204         if (conn->desire_signing) {
4205                 session->smb2.should_sign = true;
4206         }
4207
4208         if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4209                 session->smb2.should_sign = true;
4210         }
4211
4212         return NT_STATUS_OK;
4213 }
4214
4215 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4216                                         struct smbXcli_session *session1,
4217                                         struct smbXcli_conn *conn,
4218                                         struct smbXcli_session **_session2)
4219 {
4220         struct smbXcli_session *session2;
4221
4222         if (session1->smb2.signing_key.length == 0) {
4223                 return NT_STATUS_INVALID_PARAMETER_MIX;
4224         }
4225
4226         if (conn == NULL) {
4227                 return NT_STATUS_INVALID_PARAMETER_MIX;
4228         }
4229
4230         session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4231         if (session2 == NULL) {
4232                 return NT_STATUS_NO_MEMORY;
4233         }
4234         session2->smb2.session_id = session1->smb2.session_id;
4235         session2->smb2.session_flags = session1->smb2.session_flags;
4236
4237         session2->smb2.signing_key = data_blob_dup_talloc(session2,
4238                                                 session1->smb2.signing_key);
4239         if (session2->smb2.signing_key.data == NULL) {
4240                 return NT_STATUS_NO_MEMORY;
4241         }
4242
4243         session2->smb2.should_sign = session1->smb2.should_sign;
4244
4245         talloc_set_destructor(session2, smbXcli_session_destructor);
4246         DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4247         session2->conn = conn;
4248
4249         *_session2 = session2;
4250         return NT_STATUS_OK;
4251 }
4252
4253 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4254                                          const DATA_BLOB _channel_key,
4255                                          const struct iovec *recv_iov)
4256 {
4257         struct smbXcli_conn *conn = session->conn;
4258         uint8_t channel_key[16];
4259         NTSTATUS status;
4260
4261         if (conn == NULL) {
4262                 return NT_STATUS_INVALID_PARAMETER_MIX;
4263         }
4264
4265         if (session->smb2.channel_signing_key.length != 0) {
4266                 return NT_STATUS_INVALID_PARAMETER_MIX;
4267         }
4268
4269         ZERO_STRUCT(channel_key);
4270         memcpy(channel_key, _channel_key.data,
4271                MIN(_channel_key.length, sizeof(channel_key)));
4272
4273         session->smb2.channel_signing_key = data_blob_talloc(session,
4274                                                 channel_key,
4275                                                 sizeof(channel_key));
4276         if (session->smb2.channel_signing_key.data == NULL) {
4277                 ZERO_STRUCT(channel_key);
4278                 return NT_STATUS_NO_MEMORY;
4279         }
4280
4281         if (conn->protocol >= PROTOCOL_SMB2_24) {
4282                 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4283                 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4284
4285                 smb2_key_derivation(channel_key, sizeof(channel_key),
4286                                     label.data, label.length,
4287                                     context.data, context.length,
4288                                     session->smb2.channel_signing_key.data);
4289         }
4290         ZERO_STRUCT(channel_key);
4291
4292         status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4293                                         session->conn->protocol,
4294                                         recv_iov, 3);
4295         if (!NT_STATUS_IS_OK(status)) {
4296                 return status;
4297         }
4298
4299         return NT_STATUS_OK;
4300 }