f115a18d231cd9109dc8f7d850a8280a19e656d5
[bbaumbach/samba-autobuild/.git] / source4 / libcli / raw / libcliraw.h
1 /*
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
4
5    Copyright (C) Andrew Tridgell 2002-2004
6    Copyright (C) James Myers 2003 <myersjj@samba.org>
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 "libcli/raw/request.h"
24 #include "smb.h"
25 #include "librpc/gen_ndr/nbt.h"
26
27 struct smbcli_tree;  /* forward declare */
28 struct smbcli_request;  /* forward declare */
29 struct smbcli_session;  /* forward declare */
30 struct smbcli_transport;  /* forward declare */
31
32 /* default timeout for all smb requests */
33 #define SMB_REQUEST_TIMEOUT 60
34
35 /* context that will be and has been negotiated between the client and server */
36 struct smbcli_negotiate {
37         /* 
38          * negotiated maximum transmit size - this is given to us by the server
39          */
40         uint32_t max_xmit;
41
42         /* maximum number of requests that can be multiplexed */
43         uint16_t max_mux;
44
45         /* the negotiatiated protocol */
46         enum protocol_types protocol;
47
48         uint8_t sec_mode;               /* security mode returned by negprot */
49         uint8_t key_len;
50         DATA_BLOB server_guid;      /* server_guid */
51         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
52         uint32_t sesskey;
53         
54         struct smb_signing_context sign_info;
55
56         /* capabilities that the server reported */
57         uint32_t capabilities;
58         
59         int server_zone;
60         time_t server_time;
61         uint_t readbraw_supported:1;
62         uint_t writebraw_supported:1;
63
64         char *server_domain;
65 };
66         
67 /* this is the context for a SMB socket associated with the socket itself */
68 struct smbcli_socket {
69         struct socket_context *sock;
70
71         /* what port we ended up connected to */
72         int port;
73
74         /* the hostname we connected to */
75         const char *hostname;
76
77         /* the event handle for waiting for socket IO */
78         struct {
79                 struct event_context *ctx;
80                 struct fd_event *fde;
81                 struct timed_event *te;
82         } event;
83 };
84
85 /*
86   this structure allows applications to control the behaviour of the
87   client library
88 */
89 struct smbcli_options {
90         uint_t use_oplocks:1;
91         uint_t use_level2_oplocks:1;
92         uint_t use_spnego:1;
93         uint32_t max_xmit;
94         uint16_t max_mux;
95         int request_timeout;
96 };
97
98 /* this is the context for the client transport layer */
99 struct smbcli_transport {
100         /* socket level info */
101         struct smbcli_socket *socket;
102
103         /* the next mid to be allocated - needed for signing and
104            request matching */
105         uint16_t next_mid;
106         
107         /* negotiated protocol information */
108         struct smbcli_negotiate negotiate;
109
110         /* options to control the behaviour of the client code */
111         struct smbcli_options options;
112
113         /* is a readbraw pending? we need to handle that case
114            specially on receiving packets */
115         uint_t readbraw_pending:1;
116         
117         /* an idle function - if this is defined then it will be
118            called once every period microseconds while we are waiting
119            for a packet */
120         struct {
121                 void (*func)(struct smbcli_transport *, void *);
122                 void *private;
123                 uint_t period;
124         } idle;
125
126         /* the error fields from the last message */
127         struct {
128                 enum {ETYPE_NONE, ETYPE_SMB, ETYPE_SOCKET, ETYPE_NBT} etype;
129                 union {
130                         NTSTATUS nt_status;
131                         enum {SOCKET_READ_TIMEOUT,
132                               SOCKET_READ_EOF,
133                               SOCKET_READ_ERROR,
134                               SOCKET_WRITE_ERROR,
135                               SOCKET_READ_BAD_SIG} socket_error;
136                         uint_t nbt_error;
137                 } e;
138         } error;
139
140         struct {
141                 /* a oplock break request handler */
142                 BOOL (*handler)(struct smbcli_transport *transport, 
143                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private);
144                 /* private data passed to the oplock handler */
145                 void *private;
146         } oplock;
147
148         /* a list of async requests that are pending for receive on this connection */
149         struct smbcli_request *pending_recv;
150
151         /* remember the called name - some sub-protocols require us to
152            know the server name */
153         struct nbt_name called;
154
155         /* context of the stream -> packet parser */
156         struct packet_context *packet;
157 };
158
159 /* this is the context for the user */
160
161 /* this is the context for the session layer */
162 struct smbcli_session { 
163         /* transport layer info */
164         struct smbcli_transport *transport;
165         
166         /* after a session setup the server provides us with
167            a vuid identifying the security context */
168         uint16_t vuid;
169
170         /* default pid for this session */
171         uint32_t pid;
172
173         /* the flags2 for each packet - this allows
174            the user to control these for torture testing */
175         uint16_t flags2;
176
177         DATA_BLOB user_session_key;
178
179         /* the spnego context if we use extented security */
180         struct gensec_security *gensec;
181 };
182
183 /* 
184    smbcli_tree context: internal state for a tree connection. 
185  */
186 struct smbcli_tree {
187         /* session layer info */
188         struct smbcli_session *session;
189
190         uint16_t tid;                   /* tree id, aka cnum */
191         char *device;
192         char *fs_type;
193 };
194
195
196 /*
197   a client request moves between the following 4 states.
198 */
199 enum smbcli_request_state {SMBCLI_REQUEST_INIT, /* we are creating the request */
200                         SMBCLI_REQUEST_RECV, /* we are waiting for a matching reply */
201                         SMBCLI_REQUEST_DONE, /* the request is finished */
202                         SMBCLI_REQUEST_ERROR}; /* a packet or transport level error has occurred */
203
204 /* the context for a single SMB request. This is passed to any request-context 
205  * functions (similar to context.h, the server version).
206  * This will allow requests to be multi-threaded. */
207 struct smbcli_request {
208         /* allow a request to be part of a list of requests */
209         struct smbcli_request *next, *prev;
210
211         /* each request is in one of 4 possible states */
212         enum smbcli_request_state state;
213         
214         /* a request always has a transport context, nearly always has
215            a session context and usually has a tree context */
216         struct smbcli_transport *transport;
217         struct smbcli_session *session;
218         struct smbcli_tree *tree;
219
220         /* the flags2 from the SMB request, in raw form (host byte
221            order). Used to parse strings */
222         uint16_t flags2;
223
224         /* the NT status for this request. Set by packet receive code
225            or code detecting error. */
226         NTSTATUS status;
227         
228         /* the sequence number of this packet - used for signing */
229         uint_t seq_num;
230
231         /* list of ntcancel request for this requests */
232         struct smbcli_request *ntcancel;
233
234         /* set if this is a one-way request, meaning we are not
235            expecting a reply from the server. */
236         uint_t one_way_request:1;
237
238         /* set this when the request should only increment the signing
239            counter by one */
240         uint_t sign_single_increment:1;
241
242         /* the mid of this packet - used to match replies */
243         uint16_t mid;
244
245         struct request_buffer in;
246         struct request_buffer out;
247
248         /* information on what to do with a reply when it is received
249            asyncronously. If this is not setup when a reply is received then
250            the reply is discarded
251
252            The private pointer is private to the caller of the client
253            library (the application), not private to the library
254         */
255         struct {
256                 void (*fn)(struct smbcli_request *);
257                 void *private;
258         } async;
259 };
260
261 /* useful way of catching wct errors with file and line number */
262 #define SMBCLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
263       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
264       req->status = NT_STATUS_INVALID_PARAMETER; \
265       goto failed; \
266 }
267
268 #define SMBCLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
269       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
270       req->status = NT_STATUS_INVALID_PARAMETER; \
271       goto failed; \
272 }
273
274 #include "libcli/raw/raw_proto.h"