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