r11665: started to put some meat on the structure used for the SMB2 library
[kai/samba.git] / source4 / libcli / smb2 / request.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client request handling
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "include/dlinklist.h"
27 #include "lib/events/events.h"
28
29 /*
30   initialise a smb2 request
31 */
32 struct smb2_request *smb2_request_init(struct smb2_transport *transport, 
33                                        uint16_t opcode, uint32_t body_size)
34 {
35         struct smb2_request *req;
36
37         req = talloc(transport, struct smb2_request);
38         if (req == NULL) return NULL;
39
40         req->state     = SMB2_REQUEST_INIT;
41         req->transport = transport;
42         req->seqnum    = transport->seqnum++;
43         req->status    = NT_STATUS_OK;
44         req->async.fn  = NULL;
45         req->next = req->prev = NULL;
46
47         ZERO_STRUCT(req->in);
48         
49         req->out.allocated = SMB2_HDR_BODY+NBT_HDR_SIZE+body_size;
50         req->out.buffer    = talloc_size(req, req->out.allocated);
51         if (req->out.buffer == NULL) {
52                 talloc_free(req);
53                 return NULL;
54         }
55
56         req->out.size      = SMB2_HDR_BODY+NBT_HDR_SIZE + body_size;
57         req->out.hdr       = req->out.buffer + NBT_HDR_SIZE;
58         req->out.body      = req->out.hdr + SMB2_HDR_BODY;
59         req->out.body_size = body_size;
60         req->out.ptr       = req->out.body;
61
62         SIVAL(req->out.hdr, 0,                SMB2_MAGIC);
63         SSVAL(req->out.hdr, SMB2_HDR_LENGTH,  SMB2_HDR_BODY);
64         SSVAL(req->out.hdr, SMB2_HDR_PAD1,    0);
65         SIVAL(req->out.hdr, SMB2_HDR_STATUS,  0);
66         SSVAL(req->out.hdr, SMB2_HDR_OPCODE,  opcode);
67         SSVAL(req->out.hdr, SMB2_HDR_PAD2,    0);
68         SIVAL(req->out.hdr, SMB2_HDR_FLAGS,   0);
69         SIVAL(req->out.hdr, SMB2_HDR_UNKNOWN, 0);
70         SBVAL(req->out.hdr, SMB2_HDR_SEQNUM,  req->seqnum);
71         SIVAL(req->out.hdr, SMB2_HDR_PID,     0);
72         SIVAL(req->out.hdr, SMB2_HDR_TID,     0);
73         SIVAL(req->out.hdr, SMB2_HDR_UID,     0);
74         SIVAL(req->out.hdr, SMB2_HDR_UID2,    0);
75         memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
76
77         return req;
78 }
79
80 /* destroy a request structure and return final status */
81 NTSTATUS smb2_request_destroy(struct smb2_request *req)
82 {
83         NTSTATUS status;
84
85         /* this is the error code we give the application for when a
86            _send() call fails completely */
87         if (!req) return NT_STATUS_UNSUCCESSFUL;
88
89         if (req->transport) {
90                 /* remove it from the list of pending requests (a null op if
91                    its not in the list) */
92                 DLIST_REMOVE(req->transport->pending_recv, req);
93         }
94
95         if (req->state == SMBCLI_REQUEST_ERROR &&
96             NT_STATUS_IS_OK(req->status)) {
97                 req->status = NT_STATUS_INTERNAL_ERROR;
98         }
99
100         status = req->status;
101         talloc_free(req);
102         return status;
103 }
104
105 /*
106   receive a response to a packet
107 */
108 BOOL smb2_request_receive(struct smb2_request *req)
109 {
110         /* req can be NULL when a send has failed. This eliminates lots of NULL
111            checks in each module */
112         if (!req) return False;
113
114         /* keep receiving packets until this one is replied to */
115         while (req->state <= SMB2_REQUEST_RECV) {
116                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
117                         return False;
118                 }
119         }
120
121         return req->state == SMB2_REQUEST_DONE;
122 }
123
124 /* Return true if the last packet was in error */
125 BOOL smb2_request_is_error(struct smb2_request *req)
126 {
127         return NT_STATUS_IS_ERR(req->status);
128 }
129
130 /*
131   check if a range in the reply body is out of bounds
132 */
133 BOOL smb2_oob(struct smb2_request *req, const uint8_t *ptr, uint_t size)
134 {
135         /* be careful with wraparound! */
136         if (ptr < req->in.body ||
137             ptr >= req->in.body + req->in.body_size ||
138             size > req->in.body_size ||
139             ptr + size > req->in.body + req->in.body_size) {
140                 return True;
141         }
142         return False;
143 }
144
145 /*
146   pull a data blob from the body of a reply
147 */
148 DATA_BLOB smb2_pull_blob(struct smb2_request *req, uint8_t *ptr, uint_t size)
149 {
150         if (smb2_oob(req, ptr, size)) {
151                 return data_blob(NULL, 0);
152         }
153         return data_blob_talloc(req, ptr, size);
154 }
155