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