s4:libcli/raw: add smb3_capabilities to struct smbcli_options
[samba.git] / source4 / 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/smb/smb_common.h"
26 #include "libcli/raw/request.h"
27 #include "librpc/gen_ndr/nbt.h"
28 #include "libcli/raw/interfaces.h"
29 #include "libcli/smb/smb2_negotiate_context.h"
30
31 struct smbcli_tree;  /* forward declare */
32 struct smbcli_request;  /* forward declare */
33 struct smbcli_session;  /* forward declare */
34 struct smbcli_transport;  /* forward declare */
35
36 struct resolve_context;
37 struct cli_credentials;
38 struct gensec_settings;
39
40 /* default timeout for all smb requests */
41 #define SMB_REQUEST_TIMEOUT 60
42
43 /* context that will be and has been negotiated between the client and server */
44 struct smbcli_negotiate {
45         /* 
46          * negotiated maximum transmit size - this is given to us by the server
47          */
48         uint32_t max_xmit;
49
50         /* maximum number of requests that can be multiplexed */
51         uint16_t max_mux;
52
53         /* the negotiatiated protocol */
54         enum protocol_types protocol;
55
56         uint8_t sec_mode;               /* security mode returned by negprot */
57         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
58         uint32_t sesskey;
59
60         /* capabilities that the server reported */
61         uint32_t capabilities;
62         
63         int server_zone;
64         time_t server_time;
65
66         unsigned int readbraw_supported:1;
67         unsigned int writebraw_supported:1;
68         unsigned int lockread_supported:1;
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 tevent_context *ctx;
84                 struct tevent_fd *fde;
85                 struct tevent_timer *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         unsigned int use_oplocks:1;
95         unsigned int use_level2_oplocks:1;
96         unsigned int use_spnego:1;
97         unsigned int unicode:1;
98         unsigned int ntstatus_support:1;
99         int min_protocol;
100         int max_protocol;
101         uint32_t max_xmit;
102         uint16_t max_mux;
103         int request_timeout;
104         enum smb_signing_setting signing;
105         uint32_t smb2_capabilities;
106         struct GUID client_guid;
107         uint64_t max_credits;
108         unsigned int only_negprot;
109         struct smb311_capabilities smb3_capabilities;
110 };
111
112 /* this is the context for the client transport layer */
113 struct smbcli_transport {
114         struct tevent_context *ev; /* TODO: remove this !!! */
115         struct smbXcli_conn *conn;
116
117         /* negotiated protocol information */
118         struct smbcli_negotiate negotiate;
119
120         /* options to control the behaviour of the client code */
121         struct smbcli_options options;
122
123         /* an idle function - if this is defined then it will be
124            called once every period microseconds while we are waiting
125            for a packet */
126         struct {
127                 void (*func)(struct smbcli_transport *, void *);
128                 void *private_data;
129                 unsigned int period;
130                 struct tevent_timer *te;
131         } idle;
132
133         /* the error fields from the last message */
134         struct {
135                 enum {ETYPE_NONE, ETYPE_SMB, ETYPE_SOCKET, ETYPE_NBT} etype;
136                 union {
137                         NTSTATUS nt_status;
138                         enum {SOCKET_READ_TIMEOUT,
139                               SOCKET_READ_EOF,
140                               SOCKET_READ_ERROR,
141                               SOCKET_WRITE_ERROR,
142                               SOCKET_READ_BAD_SIG} socket_error;
143                         unsigned int nbt_error;
144                 } e;
145         } error;
146
147         struct {
148                 /* a oplock break request handler */
149                 bool (*handler)(struct smbcli_transport *transport, 
150                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private_data);
151                 /* private data passed to the oplock handler */
152                 void *private_data;
153         } oplock;
154         struct tevent_req *break_subreq;
155 };
156
157 /* this is the context for the user */
158
159 /* this is the context for the session layer */
160 struct smbcli_session { 
161         /* transport layer info */
162         struct smbcli_transport *transport;
163         
164         /* after a session setup the server provides us with
165            a vuid identifying the security context */
166         struct smbXcli_session *smbXcli;
167         uint16_t vuid;
168
169         /* default pid for this session */
170         uint32_t pid;
171
172         /* the flags2 for each packet - this allows
173            the user to control these for torture testing */
174         uint16_t flags2;
175
176         /* the spnego context if we use extented security */
177         struct gensec_security *gensec;
178
179         struct smbcli_session_options {
180                 unsigned int lanman_auth:1;
181                 unsigned int ntlmv2_auth:1;
182                 unsigned int plaintext_auth:1;
183         } options;
184
185         const char *os;
186         const char *lanman;
187 };
188
189 /* 
190    smbcli_tree context: internal state for a tree connection. 
191  */
192 struct smbcli_tree {
193         /* session layer info */
194         struct smbcli_session *session;
195
196         struct smbXcli_tcon *smbXcli;
197         uint16_t tid;                   /* tree id, aka cnum */
198         char *device;
199         char *fs_type;
200 };
201
202
203 /*
204   a client request moves between the following 4 states.
205 */
206 enum smbcli_request_state {SMBCLI_REQUEST_INIT, /* we are creating the request */
207                         SMBCLI_REQUEST_RECV, /* we are waiting for a matching reply */
208                         SMBCLI_REQUEST_DONE, /* the request is finished */
209                         SMBCLI_REQUEST_ERROR}; /* a packet or transport level error has occurred */
210
211 /* the context for a single SMB request. This is passed to any request-context 
212  * functions (similar to context.h, the server version).
213  * This will allow requests to be multi-threaded. */
214 struct smbcli_request {
215         /* smbXcli_req */
216         struct tevent_req *subreqs[2];
217
218         /* each request is in one of 4 possible states */
219         enum smbcli_request_state state;
220         
221         /* a request always has a transport context, nearly always has
222            a session context and usually has a tree context */
223         struct smbcli_transport *transport;
224         struct smbcli_session *session;
225         struct smbcli_tree *tree;
226
227         /* the flags2 from the SMB request, in raw form (host byte
228            order). Used to parse strings */
229         uint16_t flags2;
230
231         /* the NT status for this request. Set by packet receive code
232            or code detecting error. */
233         NTSTATUS status;
234
235         /* the caller wants to do the signing check */
236         bool sign_caller_checks;
237
238         /* give the caller a chance to prevent the talloc_free() in the _recv() function */
239         bool do_not_free;
240
241         struct smb_request_buffer in;
242         struct smb_request_buffer out;
243
244         struct smb_trans2 trans2;
245         struct smb_nttrans nttrans;
246
247         /* information on what to do with a reply when it is received
248            asyncronously. If this is not setup when a reply is received then
249            the reply is discarded
250
251            The private pointer is private to the caller of the client
252            library (the application), not private to the library
253         */
254         struct {
255                 void (*fn)(struct smbcli_request *);
256                 void *private_data;
257         } async;
258 };
259
260 /* useful way of catching wct errors with file and line number */
261 #define SMBCLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
262       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
263       req->status = NT_STATUS_INVALID_PARAMETER; \
264       goto failed; \
265 }
266
267 #define SMBCLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
268       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
269       req->status = NT_STATUS_INVALID_PARAMETER; \
270       goto failed; \
271 }
272
273 NTSTATUS smb_raw_read_recv(struct smbcli_request *req, union smb_read *parms);
274 struct smbcli_request *smb_raw_read_send(struct smbcli_tree *tree, union smb_read *parms);
275 NTSTATUS smb_raw_trans_recv(struct smbcli_request *req,
276                              TALLOC_CTX *mem_ctx,
277                              struct smb_trans2 *parms);
278 struct smbcli_request *smb_raw_trans_send(struct smbcli_tree *tree, struct smb_trans2 *parms);
279 NTSTATUS smbcli_request_destroy(struct smbcli_request *req);
280 struct smbcli_request *smb_raw_write_send(struct smbcli_tree *tree, union smb_write *parms);
281 NTSTATUS smb_raw_write_recv(struct smbcli_request *req, union smb_write *parms);
282 struct smbcli_request *smb_raw_close_send(struct smbcli_tree *tree, union smb_close *parms);
283 NTSTATUS smb_raw_open_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_open *parms);
284 struct smbcli_request *smb_raw_open_send(struct smbcli_tree *tree, union smb_open *parms);
285
286 bool smbcli_transport_process(struct smbcli_transport *transport);
287 const char *smbcli_errstr(struct smbcli_tree *tree);
288 NTSTATUS smb_raw_fsinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fsinfo *fsinfo);
289 NTSTATUS smb_raw_setfsinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_setfsinfo *set_fsinfo);
290 NTSTATUS smb_raw_pathinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fileinfo *parms);
291 NTSTATUS smb_raw_shadow_data(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, struct smb_shadow_copy *info);
292 NTSTATUS smb_raw_fileinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fileinfo *parms);
293 struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session, TALLOC_CTX *parent_ctx, bool primary);
294 NTSTATUS smb_raw_tcon(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms);
295 void smbcli_oplock_handler(struct smbcli_transport *transport, 
296                         bool (*handler)(struct smbcli_transport *, uint16_t, uint16_t, uint8_t, void *),
297                         void *private_data);
298 void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
299                                    void (*idle_func)(struct smbcli_transport *, void *),
300                                    uint64_t period,
301                                    void *private_data);
302 NTSTATUS smbcli_request_simple_recv(struct smbcli_request *req);
303 bool smbcli_oplock_ack(struct smbcli_tree *tree, uint16_t fnum, uint16_t ack_level);
304 NTSTATUS smb_raw_open(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_open *parms);
305 NTSTATUS smb_raw_close(struct smbcli_tree *tree, union smb_close *parms);
306 NTSTATUS smb_raw_unlink(struct smbcli_tree *tree, union smb_unlink *parms);
307 NTSTATUS smb_raw_chkpath(struct smbcli_tree *tree, union smb_chkpath *parms);
308 NTSTATUS smb_raw_mkdir(struct smbcli_tree *tree, union smb_mkdir *parms);
309 NTSTATUS smb_raw_rmdir(struct smbcli_tree *tree, struct smb_rmdir *parms);
310 NTSTATUS smb_raw_rename(struct smbcli_tree *tree, union smb_rename *parms);
311 NTSTATUS smb_raw_seek(struct smbcli_tree *tree, union smb_seek *parms);
312 NTSTATUS smb_raw_read(struct smbcli_tree *tree, union smb_read *parms);
313 NTSTATUS smb_raw_write(struct smbcli_tree *tree, union smb_write *parms);
314 NTSTATUS smb_raw_lock(struct smbcli_tree *tree, union smb_lock *parms);
315 NTSTATUS smb_raw_setpathinfo(struct smbcli_tree *tree, union smb_setfileinfo *parms);
316 NTSTATUS smb_raw_setfileinfo(struct smbcli_tree *tree, union smb_setfileinfo *parms);
317
318 struct smbcli_request *smb_raw_changenotify_send(struct smbcli_tree *tree, union smb_notify *parms);
319 NTSTATUS smb_raw_changenotify_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_notify *parms);
320
321 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree);
322 NTSTATUS smbcli_nt_error(struct smbcli_tree *tree);
323 NTSTATUS smb_raw_exit(struct smbcli_session *session);
324 NTSTATUS smb_raw_pathinfo_recv(struct smbcli_request *req,
325                                TALLOC_CTX *mem_ctx,
326                                union smb_fileinfo *parms);
327 struct smbcli_request *smb_raw_pathinfo_send(struct smbcli_tree *tree,
328                                              union smb_fileinfo *parms);
329 struct smbcli_request *smb_raw_setpathinfo_send(struct smbcli_tree *tree,
330                                              union smb_setfileinfo *parms);
331 struct smbcli_request *smb_raw_echo_send(struct smbcli_transport *transport,
332                                          struct smb_echo *p);
333 NTSTATUS smb_raw_search_first(struct smbcli_tree *tree,
334                               TALLOC_CTX *mem_ctx,
335                               union smb_search_first *io, void *private_data,
336                               smbcli_search_callback callback);
337 NTSTATUS smb_raw_flush(struct smbcli_tree *tree, union smb_flush *parms);
338
339 NTSTATUS smb_raw_trans(struct smbcli_tree *tree,
340                        TALLOC_CTX *mem_ctx,
341                        struct smb_trans2 *parms);
342
343 #endif /* __LIBCLI_RAW__H__ */