summaryrefslogtreecommitdiffstats
path: root/source4/libcli/smb2/smb2.h
blob: 88e651a396c696c069139c2787a049b040ebe367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* 
   Unix SMB/CIFS implementation.

   SMB2 client library header

   Copyright (C) Andrew Tridgell 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __LIBCLI_SMB2_SMB2_H__
#define __LIBCLI_SMB2_SMB2_H__

#include "libcli/raw/request.h"
#include "libcli/raw/libcliraw.h"

struct smb2_handle;
struct smb2_lease_break;

struct smb2_request_buffer {
	/* the raw SMB2 buffer, including the 4 byte length header */
	uint8_t *buffer;

	/* the size of the raw buffer, including 4 byte header */
	size_t size;

	/* how much has been allocated - on reply the buffer is over-allocated to
	   prevent too many realloc() calls
	*/
	size_t allocated;

	/* the start of the SMB2 header - this is always buffer+4 */
	uint8_t *hdr;

	/* the packet body */
	uint8_t *body;
	size_t body_fixed;
	size_t body_size;

	/* this point to the next dynamic byte that can be used
	 * this will be moved when some dynamic data is pushed
	 */
	uint8_t *dynamic;

	/* this is used to range check and align strings and buffers */
	struct request_bufinfo bufinfo;
};

/* this is the context for the smb2 transport layer */
struct smb2_transport {
	struct tevent_context *ev; /* TODO: remove this !!! */
	struct smbXcli_conn *conn;

	/* the details for coumpounded requests */
	struct {
		bool related;
		struct tevent_req **reqs;
	} compound;

	/* an idle function - if this is defined then it will be
	   called once every period microseconds while we are waiting
	   for a packet */
	struct {
		void (*func)(struct smb2_transport *, void *);
		void *private_data;
		unsigned int period;
		struct tevent_timer *te;
	} idle;

	struct {
		/* a oplock break request handler */
		bool (*handler)(struct smb2_transport *transport,
				const struct smb2_handle *handle,
				uint8_t level, void *private_data);
		/* private data passed to the oplock handler */
		void *private_data;
	} oplock;

	struct {
		/* a lease break request handler */
		bool (*handler)(struct smb2_transport *transport,
				const struct smb2_lease_break *lease_break,
				void *private_data);
		/* private data passed to the oplock handler */
		void *private_data;
	} lease;
	struct tevent_req *break_subreq;

	struct smbcli_options options;
};


/*
  SMB2 LSA state
*/
struct smb2lsa_state {
	struct dcerpc_binding_handle *binding_handle;
	struct smb2_tree *ipc_tree;
	struct policy_handle handle;
};


/*
  SMB2 tree context
*/
struct smb2_tree {
	struct smb2_session *session;
	struct smbXcli_tcon *smbXcli;
	struct smb2lsa_state *lsa;
};

/*
  SMB2 session context
*/
struct smb2_session {
	struct smb2_transport *transport;
	struct gensec_security *gensec;
	struct smbXcli_session *smbXcli;
	bool needs_bind;
};



/*
  a client request moves between the following 4 states.
*/
enum smb2_request_state {SMB2_REQUEST_INIT, /* we are creating the request */
			SMB2_REQUEST_RECV, /* we are waiting for a matching reply */
			SMB2_REQUEST_DONE, /* the request is finished */
			SMB2_REQUEST_ERROR}; /* a packet or transport level error has occurred */

/* the context for a single SMB2 request */
struct smb2_request {
	/* each request is in one of 3 possible states */
	enum smb2_request_state state;

	struct tevent_req *subreq;

	struct smb2_transport *transport;
	struct smb2_session   *session;
	struct smb2_tree      *tree;

	struct {
		bool can_cancel;
	} cancel;

	/* the NT status for this request. Set by packet receive code
	   or code detecting error. */
	NTSTATUS status;

	struct smb2_request_buffer in;
	struct smb2_request_buffer out;
	struct iovec *recv_iov;

	uint16_t credit_charge;

	/* information on what to do with a reply when it is received
	   asynchronously. If this is not setup when a reply is received then
	   the reply is discarded

	   The private pointer is private to the caller of the client
	   library (the application), not private to the library
	*/
	struct {
		void (*fn)(struct smb2_request *);
		void *private_data;
	} async;
};


#define SMB2_MIN_SIZE 0x42
#define SMB2_MIN_SIZE_NO_BODY 0x40

/*
  check that a body has the expected size
*/
#define SMB2_CHECK_PACKET_RECV(req, size, dynamic) do { \
	size_t is_size = req->in.body_size; \
	uint16_t field_size = SVAL(req->in.body, 0); \
	uint16_t want_size = ((dynamic)?(size)+1:(size)); \
	if (is_size < (size)) { \
		DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
			 __location__, (unsigned)is_size, (unsigned)want_size)); \
		return NT_STATUS_BUFFER_TOO_SMALL; \
	}\
	if (field_size != want_size) { \
		DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
			 __location__, (unsigned)field_size, (unsigned)want_size)); \
		return NT_STATUS_INVALID_PARAMETER; \
	} \
} while (0)

#endif