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