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