librpc: rerun make idl
[Samba/gbeck.git] / source3 / libsmb / async_smb.c
blobe579d1c9f0bcc32b7cdfda27ae2fbe6390f721ea
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 static void cli_state_handler(struct event_context *event_ctx,
23 struct fd_event *event, uint16 flags, void *p);
25 /**
26 * Fetch an error out of a NBT packet
27 * @param[in] buf The SMB packet
28 * @retval The error, converted to NTSTATUS
31 NTSTATUS cli_pull_error(char *buf)
33 uint32_t flags2 = SVAL(buf, smb_flg2);
35 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
36 return NT_STATUS(IVAL(buf, smb_rcls));
39 /* if the client uses dos errors, but there is no error,
40 we should return no error here, otherwise it looks
41 like an unknown bad NT_STATUS. jmcd */
42 if (CVAL(buf, smb_rcls) == 0)
43 return NT_STATUS_OK;
45 return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
48 /**
49 * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
50 * @param[in] cli The client connection that just received an error
51 * @param[in] status The error to set on "cli"
54 void cli_set_error(struct cli_state *cli, NTSTATUS status)
56 uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
58 if (NT_STATUS_IS_DOS(status)) {
59 SSVAL(cli->inbuf, smb_flg2,
60 flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
61 SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
62 SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
63 return;
66 SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
67 SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
68 return;
71 /**
72 * Allocate a new mid
73 * @param[in] cli The client connection
74 * @retval The new, unused mid
77 static uint16_t cli_new_mid(struct cli_state *cli)
79 uint16_t result;
80 struct cli_request *req;
82 while (true) {
83 result = cli->mid++;
84 if (result == 0) {
85 continue;
88 for (req = cli->outstanding_requests; req; req = req->next) {
89 if (result == req->mid) {
90 break;
94 if (req == NULL) {
95 return result;
101 * Print an async req that happens to be a cli_request
102 * @param[in] mem_ctx The TALLOC_CTX to put the result on
103 * @param[in] req The request to print
104 * @retval The string representation of "req"
107 static char *cli_request_print(TALLOC_CTX *mem_ctx, struct async_req *req)
109 char *result = async_req_print(mem_ctx, req);
110 struct cli_request *cli_req = talloc_get_type_abort(
111 req->private_data, struct cli_request);
113 if (result == NULL) {
114 return NULL;
117 return talloc_asprintf_append_buffer(
118 result, "mid=%d\n", cli_req->mid);
122 * Destroy a cli_request
123 * @param[in] req The cli_request to kill
124 * @retval Can't fail
127 static int cli_request_destructor(struct cli_request *req)
129 if (req->enc_state != NULL) {
130 common_free_enc_buffer(req->enc_state, (char *)req->outbuf);
132 DLIST_REMOVE(req->cli->outstanding_requests, req);
133 if (req->cli->outstanding_requests == NULL) {
134 TALLOC_FREE(req->cli->fd_event);
136 return 0;
140 * Are there already requests waiting in the chain_accumulator?
141 * @param[in] cli The cli_state we want to check
142 * @retval reply :-)
145 bool cli_in_chain(struct cli_state *cli)
147 if (cli->chain_accumulator == NULL) {
148 return false;
151 return (cli->chain_accumulator->num_async != 0);
155 * @brief Find the smb_cmd offset of the last command pushed
156 * @param[in] buf The buffer we're building up
157 * @retval Where can we put our next andx cmd?
159 * While chaining requests, the "next" request we're looking at needs to put
160 * its SMB_Command before the data the previous request already built up added
161 * to the chain. Find the offset to the place where we have to put our cmd.
164 static bool find_andx_cmd_ofs(uint8_t *buf, size_t *pofs)
166 uint8_t cmd;
167 size_t ofs;
169 cmd = CVAL(buf, smb_com);
171 SMB_ASSERT(is_andx_req(cmd));
173 ofs = smb_vwv0;
175 while (CVAL(buf, ofs) != 0xff) {
177 if (!is_andx_req(CVAL(buf, ofs))) {
178 return false;
182 * ofs is from start of smb header, so add the 4 length
183 * bytes. The next cmd is right after the wct field.
185 ofs = SVAL(buf, ofs+2) + 4 + 1;
187 SMB_ASSERT(ofs+4 < talloc_get_size(buf));
190 *pofs = ofs;
191 return true;
195 * @brief Do the smb chaining at a buffer level
196 * @param[in] poutbuf Pointer to the talloc'ed buffer to be modified
197 * @param[in] smb_command The command that we want to issue
198 * @param[in] wct How many words?
199 * @param[in] vwv The words, already in network order
200 * @param[in] bytes_alignment How shall we align "bytes"?
201 * @param[in] num_bytes How many bytes?
202 * @param[in] bytes The data the request ships
204 * smb_splice_chain() adds the vwv and bytes to the request already present in
205 * *poutbuf.
208 bool smb_splice_chain(uint8_t **poutbuf, uint8_t smb_command,
209 uint8_t wct, const uint16_t *vwv,
210 size_t bytes_alignment,
211 uint32_t num_bytes, const uint8_t *bytes)
213 uint8_t *outbuf;
214 size_t old_size, new_size;
215 size_t ofs;
216 size_t chain_padding = 0;
217 size_t bytes_padding = 0;
218 bool first_request;
220 old_size = talloc_get_size(*poutbuf);
223 * old_size == smb_wct means we're pushing the first request in for
224 * libsmb/
227 first_request = (old_size == smb_wct);
229 if (!first_request && ((old_size % 4) != 0)) {
231 * Align the wct field of subsequent requests to a 4-byte
232 * boundary
234 chain_padding = 4 - (old_size % 4);
238 * After the old request comes the new wct field (1 byte), the vwv's
239 * and the num_bytes field. After at we might need to align the bytes
240 * given to us to "bytes_alignment", increasing the num_bytes value.
243 new_size = old_size + chain_padding + 1 + wct * sizeof(uint16_t) + 2;
245 if ((bytes_alignment != 0) && ((new_size % bytes_alignment) != 0)) {
246 bytes_padding = bytes_alignment - (new_size % bytes_alignment);
249 new_size += bytes_padding + num_bytes;
251 if ((smb_command != SMBwriteX) && (new_size > 0xffff)) {
252 DEBUG(1, ("splice_chain: %u bytes won't fit\n",
253 (unsigned)new_size));
254 return false;
257 outbuf = TALLOC_REALLOC_ARRAY(NULL, *poutbuf, uint8_t, new_size);
258 if (outbuf == NULL) {
259 DEBUG(0, ("talloc failed\n"));
260 return false;
262 *poutbuf = outbuf;
264 if (first_request) {
265 SCVAL(outbuf, smb_com, smb_command);
266 } else {
267 size_t andx_cmd_ofs;
269 if (!find_andx_cmd_ofs(outbuf, &andx_cmd_ofs)) {
270 DEBUG(1, ("invalid command chain\n"));
271 *poutbuf = TALLOC_REALLOC_ARRAY(
272 NULL, *poutbuf, uint8_t, old_size);
273 return false;
276 if (chain_padding != 0) {
277 memset(outbuf + old_size, 0, chain_padding);
278 old_size += chain_padding;
281 SCVAL(outbuf, andx_cmd_ofs, smb_command);
282 SSVAL(outbuf, andx_cmd_ofs + 2, old_size - 4);
285 ofs = old_size;
288 * Push the chained request:
290 * wct field
293 SCVAL(outbuf, ofs, wct);
294 ofs += 1;
297 * vwv array
300 memcpy(outbuf + ofs, vwv, sizeof(uint16_t) * wct);
301 ofs += sizeof(uint16_t) * wct;
304 * bcc (byte count)
307 SSVAL(outbuf, ofs, num_bytes + bytes_padding);
308 ofs += sizeof(uint16_t);
311 * padding
314 if (bytes_padding != 0) {
315 memset(outbuf + ofs, 0, bytes_padding);
316 ofs += bytes_padding;
320 * The bytes field
323 memcpy(outbuf + ofs, bytes, num_bytes);
325 return true;
329 * @brief Destroy an async_req that is the visible part of a cli_request
330 * @param[in] req The request to kill
331 * @retval Return 0 to make talloc happy
333 * This destructor is a bit tricky: Because a cli_request can host more than
334 * one async_req for chained requests, we need to make sure that the
335 * "cli_request" that we were part of is correctly destroyed at the right
336 * time. This is done by NULLing out ourself from the "async" member of our
337 * "cli_request". If there is none left, then also TALLOC_FREE() the
338 * cli_request, which was a talloc child of the client connection cli_state.
341 static int cli_async_req_destructor(struct async_req *req)
343 struct cli_request *cli_req = talloc_get_type_abort(
344 req->private_data, struct cli_request);
345 int i, pending;
346 bool found = false;
348 pending = 0;
350 for (i=0; i<cli_req->num_async; i++) {
351 if (cli_req->async[i] == req) {
352 cli_req->async[i] = NULL;
353 found = true;
355 if (cli_req->async[i] != NULL) {
356 pending += 1;
360 SMB_ASSERT(found);
362 if (pending == 0) {
363 TALLOC_FREE(cli_req);
366 return 0;
370 * @brief Chain up a request
371 * @param[in] mem_ctx The TALLOC_CTX for the result
372 * @param[in] ev The event context that will call us back
373 * @param[in] cli The cli_state we queue the request up for
374 * @param[in] smb_command The command that we want to issue
375 * @param[in] additional_flags open_and_x wants to add oplock header flags
376 * @param[in] wct How many words?
377 * @param[in] vwv The words, already in network order
378 * @param[in] bytes_alignment How shall we align "bytes"?
379 * @param[in] num_bytes How many bytes?
380 * @param[in] bytes The data the request ships
382 * cli_request_chain() is the core of the SMB request marshalling routine. It
383 * will create a new async_req structure in the cli->chain_accumulator->async
384 * array and marshall the smb_cmd, the vwv array and the bytes into
385 * cli->chain_accumulator->outbuf.
388 static struct async_req *cli_request_chain(TALLOC_CTX *mem_ctx,
389 struct event_context *ev,
390 struct cli_state *cli,
391 uint8_t smb_command,
392 uint8_t additional_flags,
393 uint8_t wct, const uint16_t *vwv,
394 size_t bytes_alignment,
395 uint32_t num_bytes,
396 const uint8_t *bytes)
398 struct async_req **tmp_reqs;
399 struct cli_request *req;
401 req = cli->chain_accumulator;
403 tmp_reqs = TALLOC_REALLOC_ARRAY(req, req->async, struct async_req *,
404 req->num_async + 1);
405 if (tmp_reqs == NULL) {
406 DEBUG(0, ("talloc failed\n"));
407 return NULL;
409 req->async = tmp_reqs;
410 req->num_async += 1;
412 req->async[req->num_async-1] = async_req_new(mem_ctx);
413 if (req->async[req->num_async-1] == NULL) {
414 DEBUG(0, ("async_req_new failed\n"));
415 req->num_async -= 1;
416 return NULL;
418 req->async[req->num_async-1]->private_data = req;
419 req->async[req->num_async-1]->print = cli_request_print;
420 talloc_set_destructor(req->async[req->num_async-1],
421 cli_async_req_destructor);
423 if (!smb_splice_chain(&req->outbuf, smb_command, wct, vwv,
424 bytes_alignment, num_bytes, bytes)) {
425 goto fail;
428 return req->async[req->num_async-1];
430 fail:
431 TALLOC_FREE(req->async[req->num_async-1]);
432 req->num_async -= 1;
433 return NULL;
437 * @brief prepare a cli_state to accept a chain of requests
438 * @param[in] cli The cli_state we want to queue up in
439 * @param[in] ev The event_context that will call us back for the socket
440 * @param[in] size_hint How many bytes are expected, just an optimization
441 * @retval Did we have enough memory?
443 * cli_chain_cork() sets up a new cli_request in cli->chain_accumulator. If
444 * cli is used in an async fashion, i.e. if we have outstanding requests, then
445 * we do not have to create a fd event. If cli is used only with the sync
446 * helpers, we need to create the fd_event here.
448 * If you want to issue a chained request to the server, do a
449 * cli_chain_cork(), then do you cli_open_send(), cli_read_and_x_send(),
450 * cli_close_send() and so on. The async requests that come out of
451 * cli_xxx_send() are normal async requests with the difference that they
452 * won't be shipped individually. But the event_context will still trigger the
453 * req->async.fn to be called on every single request.
455 * You have to take care yourself that you only issue chainable requests in
456 * the middle of the chain.
459 bool cli_chain_cork(struct cli_state *cli, struct event_context *ev,
460 size_t size_hint)
462 struct cli_request *req = NULL;
464 SMB_ASSERT(cli->chain_accumulator == NULL);
466 if (cli->fd == -1) {
467 DEBUG(10, ("cli->fd closed\n"));
468 return false;
471 if (cli->fd_event == NULL) {
472 SMB_ASSERT(cli->outstanding_requests == NULL);
473 cli->fd_event = event_add_fd(ev, cli, cli->fd,
474 EVENT_FD_READ,
475 cli_state_handler, cli);
476 if (cli->fd_event == NULL) {
477 return false;
481 req = talloc(cli, struct cli_request);
482 if (req == NULL) {
483 goto fail;
485 req->cli = cli;
487 if (size_hint == 0) {
488 size_hint = 100;
490 req->outbuf = talloc_array(req, uint8_t, smb_wct + size_hint);
491 if (req->outbuf == NULL) {
492 goto fail;
494 req->outbuf = TALLOC_REALLOC_ARRAY(NULL, req->outbuf, uint8_t,
495 smb_wct);
497 req->num_async = 0;
498 req->async = NULL;
500 req->enc_state = NULL;
501 req->recv_helper.fn = NULL;
503 SSVAL(req->outbuf, smb_tid, cli->cnum);
504 cli_setup_packet_buf(cli, (char *)req->outbuf);
506 req->mid = cli_new_mid(cli);
508 cli->chain_accumulator = req;
510 DEBUG(10, ("cli_chain_cork: mid=%d\n", req->mid));
512 return true;
513 fail:
514 TALLOC_FREE(req);
515 if (cli->outstanding_requests == NULL) {
516 TALLOC_FREE(cli->fd_event);
518 return false;
522 * Ship a request queued up via cli_request_chain()
523 * @param[in] cl The connection
526 void cli_chain_uncork(struct cli_state *cli)
528 struct cli_request *req = cli->chain_accumulator;
529 size_t smblen;
531 SMB_ASSERT(req != NULL);
533 DLIST_ADD_END(cli->outstanding_requests, req, struct cli_request *);
534 talloc_set_destructor(req, cli_request_destructor);
536 cli->chain_accumulator = NULL;
538 SSVAL(req->outbuf, smb_mid, req->mid);
540 smblen = talloc_get_size(req->outbuf) - 4;
542 smb_setlen((char *)req->outbuf, smblen);
544 if (smblen > 0x1ffff) {
546 * This is a POSIX 14 word large write. Overwrite just the
547 * size field, the '0xFFSMB' has been set by smb_setlen which
548 * _smb_setlen_large does not do.
550 _smb_setlen_large(((char *)req->outbuf), smblen);
553 cli_calculate_sign_mac(cli, (char *)req->outbuf);
555 if (cli_encryption_on(cli)) {
556 NTSTATUS status;
557 char *enc_buf;
559 status = cli_encrypt_message(cli, (char *)req->outbuf,
560 &enc_buf);
561 if (!NT_STATUS_IS_OK(status)) {
562 DEBUG(0, ("Error in encrypting client message. "
563 "Error %s\n", nt_errstr(status)));
564 TALLOC_FREE(req);
565 return;
567 req->outbuf = (uint8_t *)enc_buf;
568 req->enc_state = cli->trans_enc_state;
571 req->sent = 0;
573 event_fd_set_writeable(cli->fd_event);
577 * @brief Send a request to the server
578 * @param[in] mem_ctx The TALLOC_CTX for the result
579 * @param[in] ev The event context that will call us back
580 * @param[in] cli The cli_state we queue the request up for
581 * @param[in] smb_command The command that we want to issue
582 * @param[in] additional_flags open_and_x wants to add oplock header flags
583 * @param[in] wct How many words?
584 * @param[in] vwv The words, already in network order
585 * @param[in] bytes_alignment How shall we align "bytes"?
586 * @param[in] num_bytes How many bytes?
587 * @param[in] bytes The data the request ships
589 * This is the generic routine to be used by the cli_xxx_send routines.
592 struct async_req *cli_request_send(TALLOC_CTX *mem_ctx,
593 struct event_context *ev,
594 struct cli_state *cli,
595 uint8_t smb_command,
596 uint8_t additional_flags,
597 uint8_t wct, const uint16_t *vwv,
598 size_t bytes_alignment,
599 uint32_t num_bytes, const uint8_t *bytes)
601 struct async_req *result;
602 bool uncork = false;
604 if (cli->chain_accumulator == NULL) {
605 if (!cli_chain_cork(cli, ev,
606 wct * sizeof(uint16_t) + num_bytes + 3)) {
607 DEBUG(1, ("cli_chain_cork failed\n"));
608 return NULL;
610 uncork = true;
613 result = cli_request_chain(mem_ctx, ev, cli, smb_command,
614 additional_flags, wct, vwv, bytes_alignment,
615 num_bytes, bytes);
617 if (result == NULL) {
618 DEBUG(1, ("cli_request_chain failed\n"));
621 if (uncork) {
622 cli_chain_uncork(cli);
625 return result;
629 * Calculate the current ofs to wct for requests like write&x
630 * @param[in] req The smb request we're currently building
631 * @retval how many bytes offset have we accumulated?
634 uint16_t cli_wct_ofs(const struct cli_state *cli)
636 size_t buf_size;
638 if (cli->chain_accumulator == NULL) {
639 return smb_wct - 4;
642 buf_size = talloc_get_size(cli->chain_accumulator->outbuf);
644 if (buf_size == smb_wct) {
645 return smb_wct - 4;
649 * Add alignment for subsequent requests
652 if ((buf_size % 4) != 0) {
653 buf_size += (4 - (buf_size % 4));
656 return buf_size - 4;
660 * Figure out if there is an andx command behind the current one
661 * @param[in] buf The smb buffer to look at
662 * @param[in] ofs The offset to the wct field that is followed by the cmd
663 * @retval Is there a command following?
666 static bool have_andx_command(const char *buf, uint16_t ofs)
668 uint8_t wct;
669 size_t buflen = talloc_get_size(buf);
671 if ((ofs == buflen-1) || (ofs == buflen)) {
672 return false;
675 wct = CVAL(buf, ofs);
676 if (wct < 2) {
678 * Not enough space for the command and a following pointer
680 return false;
682 return (CVAL(buf, ofs+1) != 0xff);
686 * @brief Pull reply data out of a request
687 * @param[in] req The request that we just received a reply for
688 * @param[out] pwct How many words did the server send?
689 * @param[out] pvwv The words themselves
690 * @param[out] pnum_bytes How many bytes did the server send?
691 * @param[out] pbytes The bytes themselves
692 * @retval Was the reply formally correct?
695 NTSTATUS cli_pull_reply(struct async_req *req,
696 uint8_t *pwct, uint16_t **pvwv,
697 uint16_t *pnum_bytes, uint8_t **pbytes)
699 struct cli_request *cli_req = talloc_get_type_abort(
700 req->private_data, struct cli_request);
701 uint8_t wct, cmd;
702 uint16_t num_bytes;
703 size_t wct_ofs, bytes_offset;
704 int i, j;
705 NTSTATUS status;
707 for (i = 0; i < cli_req->num_async; i++) {
708 if (req == cli_req->async[i]) {
709 break;
713 if (i == cli_req->num_async) {
714 cli_set_error(cli_req->cli, NT_STATUS_INVALID_PARAMETER);
715 return NT_STATUS_INVALID_PARAMETER;
719 * The status we pull here is only relevant for the last reply in the
720 * chain.
723 status = cli_pull_error(cli_req->inbuf);
725 if (i == 0) {
726 if (NT_STATUS_IS_ERR(status)
727 && !have_andx_command(cli_req->inbuf, smb_wct)) {
728 cli_set_error(cli_req->cli, status);
729 return status;
731 wct_ofs = smb_wct;
732 goto done;
735 cmd = CVAL(cli_req->inbuf, smb_com);
736 wct_ofs = smb_wct;
738 for (j = 0; j < i; j++) {
739 if (j < i-1) {
740 if (cmd == 0xff) {
741 return NT_STATUS_REQUEST_ABORTED;
743 if (!is_andx_req(cmd)) {
744 return NT_STATUS_INVALID_NETWORK_RESPONSE;
748 if (!have_andx_command(cli_req->inbuf, wct_ofs)) {
750 * This request was not completed because a previous
751 * request in the chain had received an error.
753 return NT_STATUS_REQUEST_ABORTED;
756 wct_ofs = SVAL(cli_req->inbuf, wct_ofs + 3);
759 * Skip the all-present length field. No overflow, we've just
760 * put a 16-bit value into a size_t.
762 wct_ofs += 4;
764 if (wct_ofs+2 > talloc_get_size(cli_req->inbuf)) {
765 return NT_STATUS_INVALID_NETWORK_RESPONSE;
768 cmd = CVAL(cli_req->inbuf, wct_ofs + 1);
771 if (!have_andx_command(cli_req->inbuf, wct_ofs)
772 && NT_STATUS_IS_ERR(status)) {
774 * The last command takes the error code. All further commands
775 * down the requested chain will get a
776 * NT_STATUS_REQUEST_ABORTED.
778 return status;
781 done:
782 wct = CVAL(cli_req->inbuf, wct_ofs);
784 bytes_offset = wct_ofs + 1 + wct * sizeof(uint16_t);
785 num_bytes = SVAL(cli_req->inbuf, bytes_offset);
788 * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
789 * is a 16-bit value. So bytes_offset being size_t should be far from
790 * wrapping.
793 if ((bytes_offset + 2 > talloc_get_size(cli_req->inbuf))
794 || (bytes_offset > 0xffff)) {
795 return NT_STATUS_INVALID_NETWORK_RESPONSE;
798 *pwct = wct;
799 *pvwv = (uint16_t *)(cli_req->inbuf + wct_ofs + 1);
800 *pnum_bytes = num_bytes;
801 *pbytes = (uint8_t *)cli_req->inbuf + bytes_offset + 2;
803 return NT_STATUS_OK;
807 * Decrypt a PDU, check the signature
808 * @param[in] cli The cli_state that received something
809 * @param[in] pdu The incoming bytes
810 * @retval error code
814 static NTSTATUS validate_smb_crypto(struct cli_state *cli, char *pdu)
816 NTSTATUS status;
818 if ((IVAL(pdu, 4) != 0x424d53ff) /* 0xFF"SMB" */
819 && (SVAL(pdu, 4) != 0x45ff)) /* 0xFF"E" */ {
820 DEBUG(10, ("Got non-SMB PDU\n"));
821 return NT_STATUS_INVALID_NETWORK_RESPONSE;
824 if (cli_encryption_on(cli) && CVAL(pdu, 0) == 0) {
825 uint16_t enc_ctx_num;
827 status = get_enc_ctx_num((uint8_t *)pdu, &enc_ctx_num);
828 if (!NT_STATUS_IS_OK(status)) {
829 DEBUG(10, ("get_enc_ctx_num returned %s\n",
830 nt_errstr(status)));
831 return status;
834 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
835 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
836 enc_ctx_num,
837 cli->trans_enc_state->enc_ctx_num));
838 return NT_STATUS_INVALID_HANDLE;
841 status = common_decrypt_buffer(cli->trans_enc_state, pdu);
842 if (!NT_STATUS_IS_OK(status)) {
843 DEBUG(10, ("common_decrypt_buffer returned %s\n",
844 nt_errstr(status)));
845 return status;
849 if (!cli_check_sign_mac(cli, pdu)) {
850 DEBUG(10, ("cli_check_sign_mac failed\n"));
851 return NT_STATUS_ACCESS_DENIED;
854 return NT_STATUS_OK;
858 * A PDU has arrived on cli->evt_inbuf
859 * @param[in] cli The cli_state that received something
862 static void handle_incoming_pdu(struct cli_state *cli)
864 struct cli_request *req;
865 uint16_t mid;
866 size_t raw_pdu_len, buf_len, pdu_len, rest_len;
867 char *pdu;
868 int i;
869 NTSTATUS status;
871 int num_async;
874 * The encrypted PDU len might differ from the unencrypted one
876 raw_pdu_len = smb_len(cli->evt_inbuf) + 4;
877 buf_len = talloc_get_size(cli->evt_inbuf);
878 rest_len = buf_len - raw_pdu_len;
880 if (buf_len == raw_pdu_len) {
882 * Optimal case: Exactly one PDU was in the socket buffer
884 pdu = cli->evt_inbuf;
885 cli->evt_inbuf = NULL;
887 else {
888 DEBUG(11, ("buf_len = %d, raw_pdu_len = %d, splitting "
889 "buffer\n", (int)buf_len, (int)raw_pdu_len));
891 if (raw_pdu_len < rest_len) {
893 * The PDU is shorter, talloc_memdup that one.
895 pdu = (char *)talloc_memdup(
896 cli, cli->evt_inbuf, raw_pdu_len);
898 memmove(cli->evt_inbuf, cli->evt_inbuf + raw_pdu_len,
899 buf_len - raw_pdu_len);
901 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(
902 NULL, cli->evt_inbuf, char, rest_len);
904 if (pdu == NULL) {
905 status = NT_STATUS_NO_MEMORY;
906 goto invalidate_requests;
909 else {
911 * The PDU is larger than the rest, talloc_memdup the
912 * rest
914 pdu = cli->evt_inbuf;
916 cli->evt_inbuf = (char *)talloc_memdup(
917 cli, pdu + raw_pdu_len, rest_len);
919 if (cli->evt_inbuf == NULL) {
920 status = NT_STATUS_NO_MEMORY;
921 goto invalidate_requests;
926 status = validate_smb_crypto(cli, pdu);
927 if (!NT_STATUS_IS_OK(status)) {
928 goto invalidate_requests;
931 mid = SVAL(pdu, smb_mid);
933 DEBUG(10, ("handle_incoming_pdu: got mid %d\n", mid));
935 for (req = cli->outstanding_requests; req; req = req->next) {
936 if (req->mid == mid) {
937 break;
941 pdu_len = smb_len(pdu) + 4;
943 if (req == NULL) {
944 DEBUG(3, ("Request for mid %d not found, dumping PDU\n", mid));
946 TALLOC_FREE(pdu);
947 return;
950 req->inbuf = talloc_move(req, &pdu);
953 * Freeing the last async_req will free the req (see
954 * cli_async_req_destructor). So make a copy of req->num_async, we
955 * can't reference it in the last round.
958 num_async = req->num_async;
960 for (i=0; i<num_async; i++) {
962 * A request might have been talloc_free()'ed before we arrive
963 * here. It will have removed itself from req->async via its
964 * destructor cli_async_req_destructor().
966 if (req->async[i] != NULL) {
967 if (req->recv_helper.fn != NULL) {
968 req->recv_helper.fn(req->async[i]);
969 } else {
970 async_req_done(req->async[i]);
974 return;
976 invalidate_requests:
978 DEBUG(10, ("handle_incoming_pdu: Aborting with %s\n",
979 nt_errstr(status)));
981 for (req = cli->outstanding_requests; req; req = req->next) {
982 async_req_nterror(req->async[0], status);
984 return;
988 * fd event callback. This is the basic connection to the socket
989 * @param[in] event_ctx The event context that called us
990 * @param[in] event The event that fired
991 * @param[in] flags EVENT_FD_READ | EVENT_FD_WRITE
992 * @param[in] p private_data, in this case the cli_state
995 static void cli_state_handler(struct event_context *event_ctx,
996 struct fd_event *event, uint16 flags, void *p)
998 struct cli_state *cli = (struct cli_state *)p;
999 struct cli_request *req, *next;
1000 NTSTATUS status;
1002 DEBUG(11, ("cli_state_handler called with flags %d\n", flags));
1004 if (flags & EVENT_FD_WRITE) {
1005 size_t to_send;
1006 ssize_t sent;
1008 for (req = cli->outstanding_requests; req; req = req->next) {
1009 to_send = smb_len(req->outbuf)+4;
1010 if (to_send > req->sent) {
1011 break;
1015 if (req == NULL) {
1016 if (cli->fd_event != NULL) {
1017 event_fd_set_not_writeable(cli->fd_event);
1019 return;
1022 sent = sys_send(cli->fd, req->outbuf + req->sent,
1023 to_send - req->sent, 0);
1025 if (sent < 0) {
1026 status = map_nt_error_from_unix(errno);
1027 goto sock_error;
1030 req->sent += sent;
1032 if (req->sent == to_send) {
1033 return;
1037 if (flags & EVENT_FD_READ) {
1038 int res, available;
1039 size_t old_size, new_size;
1040 char *tmp;
1042 res = ioctl(cli->fd, FIONREAD, &available);
1043 if (res == -1) {
1044 DEBUG(10, ("ioctl(FIONREAD) failed: %s\n",
1045 strerror(errno)));
1046 status = map_nt_error_from_unix(errno);
1047 goto sock_error;
1050 if (available == 0) {
1051 /* EOF */
1052 status = NT_STATUS_END_OF_FILE;
1053 goto sock_error;
1056 old_size = talloc_get_size(cli->evt_inbuf);
1057 new_size = old_size + available;
1059 if (new_size < old_size) {
1060 /* wrap */
1061 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1062 goto sock_error;
1065 tmp = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf, char,
1066 new_size);
1067 if (tmp == NULL) {
1068 /* nomem */
1069 status = NT_STATUS_NO_MEMORY;
1070 goto sock_error;
1072 cli->evt_inbuf = tmp;
1074 res = sys_recv(cli->fd, cli->evt_inbuf + old_size, available, 0);
1075 if (res == -1) {
1076 DEBUG(10, ("recv failed: %s\n", strerror(errno)));
1077 status = map_nt_error_from_unix(errno);
1078 goto sock_error;
1081 DEBUG(11, ("cli_state_handler: received %d bytes, "
1082 "smb_len(evt_inbuf) = %d\n", (int)res,
1083 smb_len(cli->evt_inbuf)));
1085 /* recv *might* have returned less than announced */
1086 new_size = old_size + res;
1088 /* shrink, so I don't expect errors here */
1089 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf,
1090 char, new_size);
1092 while ((cli->evt_inbuf != NULL)
1093 && ((smb_len(cli->evt_inbuf) + 4) <= new_size)) {
1095 * we've got a complete NBT level PDU in evt_inbuf
1097 handle_incoming_pdu(cli);
1098 new_size = talloc_get_size(cli->evt_inbuf);
1102 return;
1104 sock_error:
1106 for (req = cli->outstanding_requests; req; req = next) {
1107 int i, num_async;
1109 next = req->next;
1110 num_async = req->num_async;
1112 for (i=0; i<num_async; i++) {
1113 async_req_nterror(req->async[i], status);
1116 TALLOC_FREE(cli->fd_event);
1117 close(cli->fd);
1118 cli->fd = -1;