eb7c10ed9d310d17df58a6bcc11c9979c8938117
[kai/samba.git] / source4 / libcli / smb2 / smb2.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client library header
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 struct smb2_options {
24         uint32_t timeout;
25 };
26
27 /*
28   information returned from the negotiate response
29 */
30 struct smb2_negotiate {
31         DATA_BLOB secblob;
32 };
33
34 /* this is the context for the smb2 transport layer */
35 struct smb2_transport {
36         /* socket level info */
37         struct smbcli_socket *socket;
38
39         struct smb2_options options;
40         struct smb2_negotiate negotiate;
41
42         /* next seqnum to allocate */
43         uint64_t seqnum;
44
45         /* a list of requests that are pending for receive on this
46            connection */
47         struct smb2_request *pending_recv;
48
49         /* context of the stream -> packet parser */
50         struct packet_context *packet;
51 };
52
53
54 /*
55   SMB2 tree context
56 */
57 struct smb2_tree {
58         struct smb2_session *session;
59         uint32_t tid;
60 };
61
62 /*
63   SMB2 session context
64 */
65 struct smb2_session {
66         struct smb2_transport *transport;
67         struct gensec_security *gensec;
68         uint64_t uid;
69         DATA_BLOB session_key;
70 };
71
72
73 struct smb2_request_buffer {
74         /* the raw SMB2 buffer, including the 4 byte length header */
75         uint8_t *buffer;
76         
77         /* the size of the raw buffer, including 4 byte header */
78         uint_t size;
79         
80         /* how much has been allocated - on reply the buffer is over-allocated to 
81            prevent too many realloc() calls 
82         */
83         uint_t allocated;
84         
85         /* the start of the SMB2 header - this is always buffer+4 */
86         uint8_t *hdr;
87         
88         /* the packet body */
89         uint8_t *body;
90         uint_t body_size;
91
92         /* this point to the next dynamic byte that can be used
93          * this will be moved when some dynamic data is pushed
94          */
95         uint8_t *dynamic;
96 };
97
98
99 /*
100   a client request moves between the following 4 states.
101 */
102 enum smb2_request_state {SMB2_REQUEST_INIT, /* we are creating the request */
103                         SMB2_REQUEST_RECV, /* we are waiting for a matching reply */
104                         SMB2_REQUEST_DONE, /* the request is finished */
105                         SMB2_REQUEST_ERROR}; /* a packet or transport level error has occurred */
106
107 /* the context for a single SMB2 request */
108 struct smb2_request {
109         /* allow a request to be part of a list of requests */
110         struct smb2_request *next, *prev;
111
112         /* each request is in one of 3 possible states */
113         enum smb2_request_state state;
114         
115         struct smb2_transport *transport;
116         struct smb2_session   *session;
117         struct smb2_tree      *tree;
118
119         uint64_t seqnum;
120
121         /* the NT status for this request. Set by packet receive code
122            or code detecting error. */
123         NTSTATUS status;
124         
125         struct smb2_request_buffer in;
126         struct smb2_request_buffer out;
127
128         /* information on what to do with a reply when it is received
129            asyncronously. If this is not setup when a reply is received then
130            the reply is discarded
131
132            The private pointer is private to the caller of the client
133            library (the application), not private to the library
134         */
135         struct {
136                 void (*fn)(struct smb2_request *);
137                 void *private;
138         } async;
139 };
140
141
142 #define SMB2_MIN_SIZE 0x42
143
144 /* offsets into header elements */
145 #define SMB2_HDR_LENGTH  0x04
146 #define SMB2_HDR_PAD1    0x06
147 #define SMB2_HDR_STATUS  0x08
148 #define SMB2_HDR_OPCODE  0x0c
149 #define SMB2_HDR_PAD2    0x0e
150 #define SMB2_HDR_FLAGS   0x10
151 #define SMB2_HDR_UNKNOWN 0x14
152 #define SMB2_HDR_SEQNUM  0x18
153 #define SMB2_HDR_PID     0x20
154 #define SMB2_HDR_TID     0x24
155 #define SMB2_HDR_UID     0x28 /* 64 bit */
156 #define SMB2_HDR_SIG     0x30 /* guess ... */
157 #define SMB2_HDR_BODY    0x40
158
159 /* SMB2 opcodes */
160 #define SMB2_OP_NEGPROT   0x00
161 #define SMB2_OP_SESSSETUP 0x01
162 #define SMB2_OP_LOGOFF    0x02
163 #define SMB2_OP_TCON      0x03
164 #define SMB2_OP_TDIS      0x04
165 #define SMB2_OP_CREATE    0x05
166 #define SMB2_OP_CLOSE     0x06
167 #define SMB2_OP_FLUSH     0x07
168 #define SMB2_OP_READ      0x08
169 #define SMB2_OP_WRITE     0x09
170 #define SMB2_OP_LOCK      0x0a
171 #define SMB2_OP_IOCTL     0x0b
172 #define SMB2_OP_CANCEL    0x0c
173 #define SMB2_OP_KEEPALIVE 0x0d
174 #define SMB2_OP_FIND      0x0e
175 #define SMB2_OP_NOTIFY    0x0f
176 #define SMB2_OP_GETINFO   0x10
177 #define SMB2_OP_SETINFO   0x11
178 #define SMB2_OP_BREAK     0x12
179
180 #define SMB2_MAGIC 0x424D53FE /* 0xFE 'S' 'M' 'B' */
181
182 /*
183   check that a body has the expected size
184 */
185 #define SMB2_CHECK_PACKET_RECV(req, size, dynamic) do { \
186         uint_t is_size = req->in.body_size; \
187         uint16_t field_size = SVAL(req->in.body, 0); \
188         uint16_t want_size = ((dynamic)?(size)+1:(size)); \
189         if (is_size < (size)) { \
190                 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
191                          __location__, is_size, want_size)); \
192                 return NT_STATUS_BUFFER_TOO_SMALL; \
193         }\
194         if (field_size != want_size) { \
195                 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
196                          __location__, field_size, want_size)); \
197                 return NT_STATUS_INVALID_PARAMETER; \
198         } \
199 } while (0)
200
201 #include "libcli/smb2/smb2_proto.h"