r1432: - Move the various Gtk-specific parts from the registry code into a directory...
[samba.git] / source / include / cli_context.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 #ifndef _CLI_CONTEXT_H
25 #define _CLI_CONTEXT_H
26
27 struct cli_tree;  /* forward declare */
28 struct cli_request;  /* forward declare */
29 struct cli_session;  /* forward declare */
30 struct cli_transport;  /* forward declare */
31
32 /* context that will be and has been negotiated between the client and server */
33 struct cli_negotiate {
34         /* 
35          * negotiated maximum transmit size - this is given to us by the server
36          */
37         uint32_t max_xmit;
38
39         /* maximum number of requests that can be multiplexed */
40         uint16_t max_mux;
41
42         /* the negotiatiated protocol */
43         enum protocol_types protocol;
44
45         uint8_t sec_mode;               /* security mode returned by negprot */
46         uint8_t key_len;
47         DATA_BLOB server_guid;      /* server_guid */
48         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
49         uint32_t sesskey;
50         
51         struct {
52                 void (*sign_outgoing_message)(struct cli_request *req);
53                 BOOL (*check_incoming_message)(struct cli_request *req);
54                 void (*free_signing_context)(struct cli_transport *transport);
55                 void *signing_context;
56                 BOOL doing_signing;
57         } sign_info;
58
59         /* capabilities that the server reported */
60         uint32_t capabilities;
61         
62         int16_t server_zone;
63         time_t server_time;
64         uint_t readbraw_supported:1;
65         uint_t writebraw_supported:1;
66
67         const char *server_domain;
68 };
69         
70 /* this is the context for a SMB socket associated with the socket itself */
71 struct cli_socket {
72         TALLOC_CTX *mem_ctx;    /* life of socket pool */
73
74         /* when the reference count reaches zero then the socket is destroyed */
75         int reference_count;
76
77         struct in_addr dest_ip;
78
79         /* the port used */
80         int port;
81         
82         /* the open file descriptor */
83         int fd;
84
85         /* a count of the number of packets we have received. We
86          * actually only care about zero/non-zero at this stage */
87         uint_t pkt_count;
88
89         /* the network address of the client */
90         char *client_addr;
91         
92         /* timeout for socket operations in milliseconds. */
93         int timeout;
94 };
95
96 /*
97   this structure allows applications to control the behaviour of the
98   client library
99 */
100 struct cli_options {
101         uint_t use_oplocks:1;
102         uint_t use_level2_oplocks:1;
103         uint_t use_spnego:1;
104 };
105
106 /* this is the context for the client transport layer */
107 struct cli_transport {
108         TALLOC_CTX *mem_ctx;
109
110         /* when the reference count reaches zero then the transport is destroyed */
111         int reference_count;
112
113         /* socket level info */
114         struct cli_socket *socket;
115
116         /* the next mid to be allocated - needed for signing and
117            request matching */
118         uint16_t next_mid;
119         
120         /* negotiated protocol information */
121         struct cli_negotiate negotiate;
122
123         /* options to control the behaviour of the client code */
124         struct cli_options options;
125
126         /* is a readbraw pending? we need to handle that case
127            specially on receiving packets */
128         uint_t readbraw_pending:1;
129         
130         /* an idle function - if this is defined then it will be
131            called once every period milliseconds while we are waiting
132            for a packet */
133         struct {
134                 void (*func)(struct cli_transport *, void *);
135                 void *private;
136                 uint_t period;
137         } idle;
138
139         /* the error fields from the last message */
140         struct {
141                 enum {ETYPE_NONE, ETYPE_DOS, ETYPE_NT, ETYPE_SOCKET, ETYPE_NBT} etype;
142                 union {
143                         struct {
144                                 uint8_t eclass;
145                                 uint16_t ecode;
146                         } dos;
147                         NTSTATUS nt_status;
148                         enum socket_error socket_error;
149                         uint_t nbt_error;
150                 } e;
151         } error;
152
153         struct {
154                 /* a oplock break request handler */
155                 BOOL (*handler)(struct cli_transport *transport, 
156                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private);
157                 /* private data passed to the oplock handler */
158                 void *private;
159         } oplock;
160
161         /* a list of async requests that are pending on this connection */
162         struct cli_request *pending_requests;
163
164         /* remember the called name - some sub-protocols require us to
165            know the server name */
166         struct nmb_name called;
167 };
168
169 /* this is the context for the user */
170
171 /* this is the context for the session layer */
172 struct cli_session {    
173         TALLOC_CTX *mem_ctx;    /* life of session */
174
175         /* when the reference count reaches zero then the session is destroyed */
176         int reference_count;    
177         
178         /* transport layer info */
179         struct cli_transport *transport;
180         
181         /* after a session setup the server provides us with
182            a vuid identifying the security context */
183         uint16_t vuid;
184
185         /* default pid for this session */
186         uint32_t pid;
187
188         DATA_BLOB user_session_key;
189
190         /* the spnego context if we use extented security */
191         struct gensec_security *gensec;
192 };
193
194 /* 
195    cli_tree context: internal state for a tree connection. 
196  */
197 struct cli_tree {
198         /* life of tree tree */
199         TALLOC_CTX *mem_ctx;
200
201         /* when the reference count reaches zero then the tree is destroyed */
202         int reference_count;    
203
204         /* session layer info */
205         struct cli_session *session;
206
207         uint16_t tid;                   /* tree id, aka cnum */
208         char *device;
209         char *fs_type;
210 };
211
212 /* the context for a single SMB request. This is passed to any request-context 
213  * functions (similar to context.h, the server version).
214  * This will allow requests to be multi-threaded. */
215 struct cli_request {
216         /* allow a request to be part of a list of requests */
217         struct cli_request *next, *prev;
218
219         /* a talloc context for the lifetime of this request */
220         TALLOC_CTX *mem_ctx;
221         
222         /* a request always has a transport context, nearly always has
223            a session context and usually has a tree context */
224         struct cli_transport *transport;
225         struct cli_session *session;
226         struct cli_tree *tree;
227
228         /* the flags2 from the SMB request, in raw form (host byte
229            order). Used to parse strings */
230         uint16_t flags2;
231
232         /* the NT status for this request. Set by packet receive code
233            or code detecting error. */
234         NTSTATUS status;
235         
236         /* the sequence number of this packet - used for signing */
237         uint_t seq_num;
238
239         /* set if this is a one-way request, meaning we are not
240            expecting a reply from the server. */
241         uint_t one_way_request:1;
242
243         /* the mid of this packet - used to match replies */
244         uint16_t mid;
245
246         struct {
247                 /* the raw SMB buffer, including the 4 byte length header */
248                 char *buffer;
249                 
250                 /* the size of the raw buffer, including 4 byte header */
251                 uint_t size;
252
253                 /* how much has been allocated - on reply the buffer is over-allocated to 
254                    prevent too many realloc() calls 
255                 */
256                 uint_t allocated;
257
258                 /* the start of the SMB header - this is always buffer+4 */
259                 char *hdr;
260
261                 /* the command words and command word count. vwv points
262                    into the raw buffer */
263                 char *vwv;
264                 uint_t wct;
265
266                 /* the data buffer and size. data points into the raw buffer */
267                 char *data;
268                 uint_t data_size;
269
270                 /* ptr is used as a moving pointer into the data area
271                  * of the packet. The reason its here and not a local
272                  * variable in each function is that when a realloc of
273                  * a send packet is done we need to move this
274                  * pointer */
275                 char *ptr;
276         } in, out;
277
278         /* information on what to do with a reply when it is received
279            asyncronously. If this is not setup when a reply is received then
280            the reply is discarded
281
282            The private pointer is private to the caller of the client
283            library (the application), not private to the library
284         */
285         struct {
286                 void (*fn)(struct cli_request *);
287                 void *private;
288         } async;
289 };
290
291 /* 
292    cli_state: internal state used in libcli library for single-threaded callers, 
293    i.e. a single session on a single socket. 
294  */
295 struct cli_state {
296         TALLOC_CTX *mem_ctx;    /* life of client pool */
297         struct cli_transport *transport;
298         struct cli_session *session;
299         struct cli_tree *tree;
300         struct substitute_context substitute;
301 };
302
303 /* useful way of catching wct errors with file and line number */
304 #define CLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
305       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
306       req->status = NT_STATUS_INVALID_PARAMETER; \
307       goto failed; \
308 }
309
310 #define CLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
311       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
312       req->status = NT_STATUS_INVALID_PARAMETER; \
313       goto failed; \
314 }
315
316 #endif /* _CLI_CONTEXT_H */