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