40813f372932f877b6f4833bce0534f65cf869e6
[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/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 struct gensec_settings;
36
37 /* default timeout for all smb requests */
38 #define SMB_REQUEST_TIMEOUT 60
39
40 /* context that will be and has been negotiated between the client and server */
41 struct smbcli_negotiate {
42         /* 
43          * negotiated maximum transmit size - this is given to us by the server
44          */
45         uint32_t max_xmit;
46
47         /* maximum number of requests that can be multiplexed */
48         uint16_t max_mux;
49
50         /* the negotiatiated protocol */
51         enum protocol_types protocol;
52
53         uint8_t sec_mode;               /* security mode returned by negprot */
54         uint8_t key_len;
55         DATA_BLOB server_guid;      /* server_guid */
56         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
57         uint32_t sesskey;
58         
59         struct smb_signing_context sign_info;
60
61         /* capabilities that the server reported */
62         uint32_t capabilities;
63         
64         int server_zone;
65         time_t server_time;
66         unsigned int readbraw_supported:1;
67         unsigned int writebraw_supported:1;
68         unsigned int lockread_supported:1;
69
70         char *server_domain;
71 };
72         
73 /* this is the context for a SMB socket associated with the socket itself */
74 struct smbcli_socket {
75         struct socket_context *sock;
76
77         /* what port we ended up connected to */
78         int port;
79
80         /* the hostname we connected to */
81         const char *hostname;
82
83         /* the event handle for waiting for socket IO */
84         struct {
85                 struct tevent_context *ctx;
86                 struct tevent_fd *fde;
87                 struct tevent_timer *te;
88         } event;
89 };
90
91 /*
92   this structure allows applications to control the behaviour of the
93   client library
94 */
95 struct smbcli_options {
96         unsigned int use_oplocks:1;
97         unsigned int use_level2_oplocks:1;
98         unsigned int use_spnego:1;
99         unsigned int unicode:1;
100         unsigned int ntstatus_support:1;
101         int max_protocol;
102         uint32_t max_xmit;
103         uint16_t max_mux;
104         int request_timeout;
105         enum smb_signing_state signing;
106 };
107
108 /* this is the context for the client transport layer */
109 struct smbcli_transport {
110         /* socket level info */
111         struct smbcli_socket *socket;
112
113         /* the next mid to be allocated - needed for signing and
114            request matching */
115         uint16_t next_mid;
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         /* is a readbraw pending? we need to handle that case
124            specially on receiving packets */
125         unsigned int readbraw_pending:1;
126         
127         /* an idle function - if this is defined then it will be
128            called once every period microseconds while we are waiting
129            for a packet */
130         struct {
131                 void (*func)(struct smbcli_transport *, void *);
132                 void *private_data;
133                 unsigned int period;
134         } idle;
135
136         /* the error fields from the last message */
137         struct {
138                 enum {ETYPE_NONE, ETYPE_SMB, ETYPE_SOCKET, ETYPE_NBT} etype;
139                 union {
140                         NTSTATUS nt_status;
141                         enum {SOCKET_READ_TIMEOUT,
142                               SOCKET_READ_EOF,
143                               SOCKET_READ_ERROR,
144                               SOCKET_WRITE_ERROR,
145                               SOCKET_READ_BAD_SIG} socket_error;
146                         unsigned int nbt_error;
147                 } e;
148         } error;
149
150         struct {
151                 /* a oplock break request handler */
152                 bool (*handler)(struct smbcli_transport *transport, 
153                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private_data);
154                 /* private data passed to the oplock handler */
155                 void *private_data;
156         } oplock;
157
158         /* a list of async requests that are pending for receive on this connection */
159         struct smbcli_request *pending_recv;
160
161         /* remember the called name - some sub-protocols require us to
162            know the server name */
163         struct nbt_name called;
164
165         /* context of the stream -> packet parser */
166         struct packet_context *packet;
167 };
168
169 /* this is the context for the user */
170
171 /* this is the context for the session layer */
172 struct smbcli_session { 
173         /* transport layer info */
174         struct smbcli_transport *transport;
175         
176         /* after a session setup the server provides us with
177            a vuid identifying the security context */
178         uint16_t vuid;
179
180         /* default pid for this session */
181         uint32_t pid;
182
183         /* the flags2 for each packet - this allows
184            the user to control these for torture testing */
185         uint16_t flags2;
186
187         DATA_BLOB user_session_key;
188
189         /* the spnego context if we use extented security */
190         struct gensec_security *gensec;
191
192         struct smbcli_session_options {
193                 unsigned int lanman_auth:1;
194                 unsigned int ntlmv2_auth:1;
195                 unsigned int plaintext_auth:1;
196         } options;
197
198         const char *os;
199         const char *lanman;
200 };
201
202 /* 
203    smbcli_tree context: internal state for a tree connection. 
204  */
205 struct smbcli_tree {
206         /* session layer info */
207         struct smbcli_session *session;
208
209         uint16_t tid;                   /* tree id, aka cnum */
210         char *device;
211         char *fs_type;
212 };
213
214
215 /*
216   a client request moves between the following 4 states.
217 */
218 enum smbcli_request_state {SMBCLI_REQUEST_INIT, /* we are creating the request */
219                         SMBCLI_REQUEST_RECV, /* we are waiting for a matching reply */
220                         SMBCLI_REQUEST_DONE, /* the request is finished */
221                         SMBCLI_REQUEST_ERROR}; /* a packet or transport level error has occurred */
222
223 /* the context for a single SMB request. This is passed to any request-context 
224  * functions (similar to context.h, the server version).
225  * This will allow requests to be multi-threaded. */
226 struct smbcli_request {
227         /* allow a request to be part of a list of requests */
228         struct smbcli_request *next, *prev;
229
230         /* each request is in one of 4 possible states */
231         enum smbcli_request_state state;
232         
233         /* a request always has a transport context, nearly always has
234            a session context and usually has a tree context */
235         struct smbcli_transport *transport;
236         struct smbcli_session *session;
237         struct smbcli_tree *tree;
238
239         /* a receive helper, smbcli_transport_finish_recv will not call
240            req->async.fn callback handler unless the recv_helper returns
241            a value > SMBCLI_REQUEST_RECV. */
242         struct {
243                 enum smbcli_request_state (*fn)(struct smbcli_request *);
244                 void *private_data;
245         } recv_helper;
246
247         /* the flags2 from the SMB request, in raw form (host byte
248            order). Used to parse strings */
249         uint16_t flags2;
250
251         /* the NT status for this request. Set by packet receive code
252            or code detecting error. */
253         NTSTATUS status;
254         
255         /* the sequence number of this packet - used for signing */
256         unsigned int seq_num;
257
258         /* list of ntcancel request for this requests */
259         struct smbcli_request *ntcancel;
260
261         /* set if this is a one-way request, meaning we are not
262            expecting a reply from the server. */
263         unsigned int one_way_request:1;
264
265         /* set this when the request should only increment the signing
266            counter by one */
267         unsigned int sign_single_increment:1;
268
269         /* the caller wants to do the signing check */
270         bool sign_caller_checks;
271
272         /* give the caller a chance to prevent the talloc_free() in the _recv() function */
273         bool do_not_free;
274
275         /* the mid of this packet - used to match replies */
276         uint16_t mid;
277
278         struct smb_request_buffer in;
279         struct smb_request_buffer out;
280
281         /* information on what to do with a reply when it is received
282            asyncronously. If this is not setup when a reply is received then
283            the reply is discarded
284
285            The private pointer is private to the caller of the client
286            library (the application), not private to the library
287         */
288         struct {
289                 void (*fn)(struct smbcli_request *);
290                 void *private_data;
291         } async;
292 };
293
294 /* useful way of catching wct errors with file and line number */
295 #define SMBCLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
296       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
297       req->status = NT_STATUS_INVALID_PARAMETER; \
298       goto failed; \
299 }
300
301 #define SMBCLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
302       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
303       req->status = NT_STATUS_INVALID_PARAMETER; \
304       goto failed; \
305 }
306
307 #include "libcli/raw/interfaces.h" 
308
309 NTSTATUS smb_raw_read_recv(struct smbcli_request *req, union smb_read *parms);
310 struct smbcli_request *smb_raw_read_send(struct smbcli_tree *tree, union smb_read *parms);
311 NTSTATUS smb_raw_trans_recv(struct smbcli_request *req,
312                              TALLOC_CTX *mem_ctx,
313                              struct smb_trans2 *parms);
314 size_t smb_raw_max_trans_data(struct smbcli_tree *tree, size_t param_size);
315 struct smbcli_request *smb_raw_trans_send(struct smbcli_tree *tree, struct smb_trans2 *parms);
316 NTSTATUS smbcli_request_destroy(struct smbcli_request *req);
317 struct smbcli_request *smb_raw_write_send(struct smbcli_tree *tree, union smb_write *parms);
318 struct smbcli_request *smb_raw_close_send(struct smbcli_tree *tree, union smb_close *parms);
319 NTSTATUS smb_raw_open_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_open *parms);
320 struct smbcli_request *smb_raw_open_send(struct smbcli_tree *tree, union smb_open *parms);
321
322 bool smbcli_transport_process(struct smbcli_transport *transport);
323 const char *smbcli_errstr(struct smbcli_tree *tree);
324 NTSTATUS smb_raw_fsinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fsinfo *fsinfo);
325 NTSTATUS smb_raw_pathinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fileinfo *parms);
326 NTSTATUS smb_raw_shadow_data(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, struct smb_shadow_copy *info);
327 NTSTATUS smb_raw_fileinfo(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_fileinfo *parms);
328 struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session, TALLOC_CTX *parent_ctx, bool primary);
329 NTSTATUS smb_raw_tcon(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms);
330 void smbcli_oplock_handler(struct smbcli_transport *transport, 
331                         bool (*handler)(struct smbcli_transport *, uint16_t, uint16_t, uint8_t, void *),
332                         void *private_data);
333 void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
334                                    void (*idle_func)(struct smbcli_transport *, void *),
335                                    uint64_t period,
336                                    void *private_data);
337 NTSTATUS smbcli_request_simple_recv(struct smbcli_request *req);
338 bool smbcli_oplock_ack(struct smbcli_tree *tree, uint16_t fnum, uint16_t ack_level);
339 NTSTATUS smb_raw_open(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_open *parms);
340 NTSTATUS smb_raw_close(struct smbcli_tree *tree, union smb_close *parms);
341 NTSTATUS smb_raw_unlink(struct smbcli_tree *tree, union smb_unlink *parms);
342 NTSTATUS smb_raw_chkpath(struct smbcli_tree *tree, union smb_chkpath *parms);
343 NTSTATUS smb_raw_mkdir(struct smbcli_tree *tree, union smb_mkdir *parms);
344 NTSTATUS smb_raw_rmdir(struct smbcli_tree *tree, struct smb_rmdir *parms);
345 NTSTATUS smb_raw_rename(struct smbcli_tree *tree, union smb_rename *parms);
346 NTSTATUS smb_raw_seek(struct smbcli_tree *tree, union smb_seek *parms);
347 NTSTATUS smb_raw_read(struct smbcli_tree *tree, union smb_read *parms);
348 NTSTATUS smb_raw_write(struct smbcli_tree *tree, union smb_write *parms);
349 NTSTATUS smb_raw_lock(struct smbcli_tree *tree, union smb_lock *parms);
350 NTSTATUS smb_raw_setpathinfo(struct smbcli_tree *tree, union smb_setfileinfo *parms);
351 NTSTATUS smb_raw_setfileinfo(struct smbcli_tree *tree, union smb_setfileinfo *parms);
352
353 struct smbcli_request *smb_raw_changenotify_send(struct smbcli_tree *tree, union smb_notify *parms);
354 NTSTATUS smb_raw_changenotify_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_notify *parms);
355
356 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree);
357 NTSTATUS smbcli_nt_error(struct smbcli_tree *tree);
358 NTSTATUS smb_raw_exit(struct smbcli_session *session);
359 NTSTATUS smb_raw_pathinfo_recv(struct smbcli_request *req,
360                                TALLOC_CTX *mem_ctx,
361                                union smb_fileinfo *parms);
362 struct smbcli_request *smb_raw_pathinfo_send(struct smbcli_tree *tree,
363                                              union smb_fileinfo *parms);
364 struct smbcli_request *smb_raw_setpathinfo_send(struct smbcli_tree *tree,
365                                              union smb_setfileinfo *parms);
366 struct smbcli_request *smb_raw_echo_send(struct smbcli_transport *transport,
367                                          struct smb_echo *p);
368 NTSTATUS smb_raw_search_first(struct smbcli_tree *tree,
369                               TALLOC_CTX *mem_ctx,
370                               union smb_search_first *io, void *private_data,
371                               smbcli_search_callback callback);
372 NTSTATUS smb_raw_flush(struct smbcli_tree *tree, union smb_flush *parms);
373
374 NTSTATUS smb_raw_trans(struct smbcli_tree *tree,
375                        TALLOC_CTX *mem_ctx,
376                        struct smb_trans2 *parms);
377
378 struct smbcli_socket *smbcli_sock_connect_byname(const char *host, const char **ports,
379                                                  TALLOC_CTX *mem_ctx,
380                                                  struct resolve_context *resolve_ctx,
381                                                  struct tevent_context *event_ctx,
382                                                  const char *socket_options);
383 void smbcli_sock_dead(struct smbcli_socket *sock);
384
385 #endif /* __LIBCLI_RAW__H__ */