r3466: split out request.h, signing.h, and smb_server.h
[tprouty/samba.git] / source4 / libcli / raw / libcliraw.h
1 /*
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6    Copyright (C) Jeremy Allison 1998
7    Copyright (C) James Myers 2003 <myersjj@samba.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "request.h"
25
26 struct smbcli_tree;  /* forward declare */
27 struct smbcli_request;  /* forward declare */
28 struct smbcli_session;  /* forward declare */
29 struct smbcli_transport;  /* forward declare */
30
31 /* context that will be and has been negotiated between the client and server */
32 struct smbcli_negotiate {
33         /* 
34          * negotiated maximum transmit size - this is given to us by the server
35          */
36         uint32_t max_xmit;
37
38         /* maximum number of requests that can be multiplexed */
39         uint16_t max_mux;
40
41         /* the negotiatiated protocol */
42         enum protocol_types protocol;
43
44         uint8_t sec_mode;               /* security mode returned by negprot */
45         uint8_t key_len;
46         DATA_BLOB server_guid;      /* server_guid */
47         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
48         uint32_t sesskey;
49         
50         struct smb_signing_context sign_info;
51
52         /* capabilities that the server reported */
53         uint32_t capabilities;
54         
55         int server_zone;
56         time_t server_time;
57         uint_t readbraw_supported:1;
58         uint_t writebraw_supported:1;
59
60         char *server_domain;
61 };
62         
63 /* this is the context for a SMB socket associated with the socket itself */
64 struct smbcli_socket {
65         struct ipv4_addr dest_ip;
66         /* dest hostname (which may or may not be a DNS name) */
67         char *hostname;
68
69         /* the port used */
70         int port;
71         
72         struct socket_context *sock;
73
74         /* a count of the number of packets we have received. We
75          * actually only care about zero/non-zero at this stage */
76         uint_t pkt_count;
77
78         /* the network address of the client */
79         char *client_addr;
80         
81         /* timeout for socket operations in milliseconds. */
82         int timeout;
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 };
96
97 /* this is the context for the client transport layer */
98 struct smbcli_transport {
99         /* socket level info */
100         struct smbcli_socket *socket;
101
102         /* the next mid to be allocated - needed for signing and
103            request matching */
104         uint16_t next_mid;
105         
106         /* negotiated protocol information */
107         struct smbcli_negotiate negotiate;
108
109         /* options to control the behaviour of the client code */
110         struct smbcli_options options;
111
112         /* is a readbraw pending? we need to handle that case
113            specially on receiving packets */
114         uint_t readbraw_pending:1;
115         
116         /* an idle function - if this is defined then it will be
117            called once every period seconds while we are waiting
118            for a packet */
119         struct {
120                 void (*func)(struct smbcli_transport *, void *);
121                 void *private;
122                 uint_t period;
123         } idle;
124
125         /* the error fields from the last message */
126         struct {
127                 enum {ETYPE_NONE, ETYPE_DOS, ETYPE_NT, ETYPE_SOCKET, ETYPE_NBT} etype;
128                 union {
129                         struct {
130                                 uint8_t eclass;
131                                 uint16_t ecode;
132                         } dos;
133                         NTSTATUS nt_status;
134                         enum {SOCKET_READ_TIMEOUT,
135                               SOCKET_READ_EOF,
136                               SOCKET_READ_ERROR,
137                               SOCKET_WRITE_ERROR,
138                               SOCKET_READ_BAD_SIG} socket_error;
139                         uint_t nbt_error;
140                 } e;
141         } error;
142
143         struct {
144                 /* a oplock break request handler */
145                 BOOL (*handler)(struct smbcli_transport *transport, 
146                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private);
147                 /* private data passed to the oplock handler */
148                 void *private;
149         } oplock;
150
151         /* a list of async requests that are pending for send on this connection */
152         struct smbcli_request *pending_send;
153
154         /* a list of async requests that are pending for receive on this connection */
155         struct smbcli_request *pending_recv;
156
157         /* remember the called name - some sub-protocols require us to
158            know the server name */
159         struct nmb_name called;
160
161         /* a buffer for partially received SMB packets. */
162         struct {
163                 uint8_t header[NBT_HDR_SIZE];
164                 size_t req_size;
165                 size_t received;
166                 uint8_t *buffer;
167         } recv_buffer;
168
169         /* the event handle for waiting for socket IO */
170         struct {
171                 struct event_context *ctx;
172                 struct fd_event *fde;
173                 struct timed_event *te;
174         } event;
175 };
176
177 /* this is the context for the user */
178
179 /* this is the context for the session layer */
180 struct smbcli_session { 
181         /* transport layer info */
182         struct smbcli_transport *transport;
183         
184         /* after a session setup the server provides us with
185            a vuid identifying the security context */
186         uint16_t vuid;
187
188         /* default pid for this session */
189         uint32_t pid;
190
191         /* the flags2 for each packet - this allows
192            the user to control these for torture testing */
193         uint16_t flags2;
194
195         DATA_BLOB user_session_key;
196
197         /* the spnego context if we use extented security */
198         struct gensec_security *gensec;
199 };
200
201 /* 
202    smbcli_tree context: internal state for a tree connection. 
203  */
204 struct smbcli_tree {
205         /* session layer info */
206         struct smbcli_session *session;
207
208         uint16_t tid;                   /* tree id, aka cnum */
209         char *device;
210         char *fs_type;
211 };
212
213
214 /*
215   a client request moves between the following 4 states.
216 */
217 enum smbcli_request_state {SMBCLI_REQUEST_INIT, /* we are creating the request */
218                         SMBCLI_REQUEST_SEND, /* the request is in the outgoing socket Q */
219                         SMBCLI_REQUEST_RECV, /* we are waiting for a matching reply */
220                         SMBCLI_REQUEST_DONE, /* the request is finished */
221                         SMBCLI_REQUEST_ERROR}; /* a packet or transport level error has occurred */
222
223 /* the context for a single SMB request. This is passed to any request-context 
224  * functions (similar to context.h, the server version).
225  * This will allow requests to be multi-threaded. */
226 struct smbcli_request {
227         /* allow a request to be part of a list of requests */
228         struct smbcli_request *next, *prev;
229
230         /* each request is in one of 4 possible states */
231         enum smbcli_request_state state;
232         
233         /* a request always has a transport context, nearly always has
234            a session context and usually has a tree context */
235         struct smbcli_transport *transport;
236         struct smbcli_session *session;
237         struct smbcli_tree *tree;
238
239         /* the flags2 from the SMB request, in raw form (host byte
240            order). Used to parse strings */
241         uint16_t flags2;
242
243         /* the NT status for this request. Set by packet receive code
244            or code detecting error. */
245         NTSTATUS status;
246         
247         /* the sequence number of this packet - used for signing */
248         uint_t seq_num;
249
250         /* set if this is a one-way request, meaning we are not
251            expecting a reply from the server. */
252         uint_t one_way_request:1;
253
254         /* set this when the request should only increment the signing
255            counter by one */
256         uint_t sign_single_increment:1;
257
258         /* the mid of this packet - used to match replies */
259         uint16_t mid;
260
261         struct request_buffer in;
262         struct request_buffer out;
263
264         /* information on what to do with a reply when it is received
265            asyncronously. If this is not setup when a reply is received then
266            the reply is discarded
267
268            The private pointer is private to the caller of the client
269            library (the application), not private to the library
270         */
271         struct {
272                 void (*fn)(struct smbcli_request *);
273                 void *private;
274         } async;
275 };
276
277 /* useful way of catching wct errors with file and line number */
278 #define SMBCLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
279       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
280       req->status = NT_STATUS_INVALID_PARAMETER; \
281       goto failed; \
282 }
283
284 #define SMBCLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
285       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
286       req->status = NT_STATUS_INVALID_PARAMETER; \
287       goto failed; \
288 }