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