1 /*
2    Unix SMB/CIFS implementation.
3    NTVFS structures and defines
4    Copyright (C) Andrew Tridgell			2003
5    Copyright (C) Stefan Metzmacher			2004
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 
22 #include "libcli/raw/interfaces.h"
23 #include "param/share.h"
24 
25 /* modules can use the following to determine if the interface has changed */
26 /* version 1 -> 0 - make module stacking easier -- metze */
27 #define NTVFS_INTERFACE_VERSION 0
28 
29 struct ntvfs_module_context;
30 struct ntvfs_request;
31 
32 /* each backend has to be one one of the following 3 basic types. In
33  * earlier versions of Samba backends needed to handle all types, now
34  * we implement them separately. */
35 enum ntvfs_type {NTVFS_DISK, NTVFS_PRINT, NTVFS_IPC};
36 
37 /* the ntvfs operations structure - contains function pointers to
38    the backend implementations of each operation */
39 struct ntvfs_ops {
40 	const char *name;
41 	enum ntvfs_type type;
42 
43 	/* initial setup */
44 	NTSTATUS (*connect)(struct ntvfs_module_context *ntvfs,
45 			    struct ntvfs_request *req,
46 			    const char *sharename);
47 	NTSTATUS (*disconnect)(struct ntvfs_module_context *ntvfs);
48 
49 	/* async_setup - called when a backend is processing a async request */
50 	NTSTATUS (*async_setup)(struct ntvfs_module_context *ntvfs,
51 				struct ntvfs_request *req,
52 				void *private);
53 
54 	/* filesystem operations */
55 	NTSTATUS (*fsinfo)(struct ntvfs_module_context *ntvfs,
56 			   struct ntvfs_request *req,
57 			   union smb_fsinfo *fs);
58 
59 	/* path operations */
60 	NTSTATUS (*unlink)(struct ntvfs_module_context *ntvfs,
61 			   struct ntvfs_request *req,
62 			   union smb_unlink *unl);
63 	NTSTATUS (*chkpath)(struct ntvfs_module_context *ntvfs,
64 			    struct ntvfs_request *req,
65 			    union smb_chkpath *cp);
66 	NTSTATUS (*qpathinfo)(struct ntvfs_module_context *ntvfs,
67 			      struct ntvfs_request *req,
68 			      union smb_fileinfo *st);
69 	NTSTATUS (*setpathinfo)(struct ntvfs_module_context *ntvfs,
70 				struct ntvfs_request *req,
71 				union smb_setfileinfo *st);
72 	NTSTATUS (*mkdir)(struct ntvfs_module_context *ntvfs,
73 			  struct ntvfs_request *req,
74 			  union smb_mkdir *md);
75 	NTSTATUS (*rmdir)(struct ntvfs_module_context *ntvfs,
76 			  struct ntvfs_request *req,
77 			  struct smb_rmdir *rd);
78 	NTSTATUS (*rename)(struct ntvfs_module_context *ntvfs,
79 			   struct ntvfs_request *req,
80 			   union smb_rename *ren);
81 	NTSTATUS (*copy)(struct ntvfs_module_context *ntvfs,
82 			 struct ntvfs_request *req,
83 			 struct smb_copy *cp);
84 	NTSTATUS (*open)(struct ntvfs_module_context *ntvfs,
85 			 struct ntvfs_request *req,
86 			 union smb_open *oi);
87 
88 	/* directory search */
89 	NTSTATUS (*search_first)(struct ntvfs_module_context *ntvfs,
90 				 struct ntvfs_request *req,
91 				 union smb_search_first *io, void *private,
92 				 BOOL (*callback)(void *private, union smb_search_data *file));
93 	NTSTATUS (*search_next)(struct ntvfs_module_context *ntvfs,
94 				struct ntvfs_request *req,
95 				union smb_search_next *io, void *private,
96 				BOOL (*callback)(void *private, union smb_search_data *file));
97 	NTSTATUS (*search_close)(struct ntvfs_module_context *ntvfs,
98 				 struct ntvfs_request *req,
99 				 union smb_search_close *io);
100 
101 	/* operations on open files */
102 	NTSTATUS (*ioctl)(struct ntvfs_module_context *ntvfs,
103 			  struct ntvfs_request *req,
104 			  union smb_ioctl *io);
105 	NTSTATUS (*read)(struct ntvfs_module_context *ntvfs,
106 			 struct ntvfs_request *req,
107 			 union smb_read *io);
108 	NTSTATUS (*write)(struct ntvfs_module_context *ntvfs,
109 			  struct ntvfs_request *req,
110 			  union smb_write *io);
111 	NTSTATUS (*seek)(struct ntvfs_module_context *ntvfs,
112 			 struct ntvfs_request *req,
113 			 union smb_seek *io);
114 	NTSTATUS (*flush)(struct ntvfs_module_context *ntvfs,
115 			  struct ntvfs_request *req,
116 			  union smb_flush *flush);
117 	NTSTATUS (*lock)(struct ntvfs_module_context *ntvfs,
118 			 struct ntvfs_request *req,
119 			 union smb_lock *lck);
120 	NTSTATUS (*qfileinfo)(struct ntvfs_module_context *ntvfs,
121 			      struct ntvfs_request *req,
122 			      union smb_fileinfo *info);
123 	NTSTATUS (*setfileinfo)(struct ntvfs_module_context *ntvfs,
124 				struct ntvfs_request *req,
125 				union smb_setfileinfo *info);
126 	NTSTATUS (*close)(struct ntvfs_module_context *ntvfs,
127 			  struct ntvfs_request *req,
128 			  union smb_close *io);
129 
130 	/* trans interface - used by IPC backend for pipes and RAP calls */
131 	NTSTATUS (*trans)(struct ntvfs_module_context *ntvfs,
132 			  struct ntvfs_request *req,
133 			  struct smb_trans2 *trans);
134 
135 	/* trans2 interface - only used by CIFS backend to prover complete passthru for testing */
136 	NTSTATUS (*trans2)(struct ntvfs_module_context *ntvfs,
137 			   struct ntvfs_request *req,
138 			   struct smb_trans2 *trans2);
139 
140 	/* change notify request */
141 	NTSTATUS (*notify)(struct ntvfs_module_context *ntvfs,
142 			   struct ntvfs_request *req,
143 			   union smb_notify *info);
144 
145 	/* cancel - cancels any pending async request */
146 	NTSTATUS (*cancel)(struct ntvfs_module_context *ntvfs,
147 			   struct ntvfs_request *req);
148 
149 	/* printing specific operations */
150 	NTSTATUS (*lpq)(struct ntvfs_module_context *ntvfs,
151 			struct ntvfs_request *req,
152 			union smb_lpq *lpq);
153 
154 	/* logoff - called when a vuid is closed */
155 	NTSTATUS (*logoff)(struct ntvfs_module_context *ntvfs,
156 			   struct ntvfs_request *req);
157 	NTSTATUS (*exit)(struct ntvfs_module_context *ntvfs,
158 			 struct ntvfs_request *req);
159 };
160 
161 struct ntvfs_module_context {
162 	struct ntvfs_module_context *prev, *next;
163 	struct ntvfs_context *ctx;
164 	int depth;
165 	const struct ntvfs_ops *ops;
166 	void *private_data;
167 };
168 
169 struct ntvfs_context {
170 	enum ntvfs_type type;
171 
172 	/* the reported filesystem type */
173 	char *fs_type;
174 
175 	/* the reported device type */
176 	char *dev_type;
177 
178 	enum protocol_types protocol;
179 
180 	/*
181 	 * linked list of module contexts
182 	 */
183 	struct ntvfs_module_context *modules;
184 
185 	struct share_config *config;
186 
187 	uint32_t server_id;
188 	struct event_context *event_ctx;
189 	struct messaging_context *msg_ctx;
190 
191 	struct {
192 		void *private_data;
193 		NTSTATUS (*handler)(void *private_data, struct ntvfs_handle *handle, uint8_t level);
194 	} oplock;
195 
196 	struct {
197 		void *private_data;
198 		struct socket_address *(*get_my_addr)(void *private_data, TALLOC_CTX *mem_ctx);
199 		struct socket_address *(*get_peer_addr)(void *private_data, TALLOC_CTX *mem_ctx);
200 	} client;
201 
202 	struct {
203 		void *private_data;
204 		NTSTATUS (*create_new)(void *private_data, struct ntvfs_request *req, struct ntvfs_handle **h);
205 		NTSTATUS (*make_valid)(void *private_data, struct ntvfs_handle *h);
206 		void (*destroy)(void *private_data, struct ntvfs_handle *h);
207 		struct ntvfs_handle *(*search_by_wire_key)(void *private_data,  struct ntvfs_request *req, const DATA_BLOB *key);
208 		DATA_BLOB (*get_wire_key)(void *private_data, struct ntvfs_handle *handle, TALLOC_CTX *mem_ctx);
209 	} handles;
210 };
211 
212 /* a set of flags to control handling of request structures */
213 #define NTVFS_ASYNC_STATE_ASYNC     (1<<1) /* the backend will answer this one later */
214 #define NTVFS_ASYNC_STATE_MAY_ASYNC (1<<2) /* the backend is allowed to answer async */
215 
216 /* the ntvfs_async_state structure allows backend functions to
217    delay replying to requests. To use this, the front end must
218    set send_fn to a function to be called by the backend
219    when the reply is finally ready to be sent. The backend
220    must set status to the status it wants in the
221    reply. The backend must set the NTVFS_ASYNC_STATE_ASYNC
222    control_flag on the request to indicate that it wishes to
223    delay the reply
224 
225    If NTVFS_ASYNC_STATE_MAY_ASYNC is not set then the backend cannot
226    ask for a delayed reply for this request
227 
228    note that the private_data pointer is private to the layer which alloced this struct
229 */
230 struct ntvfs_async_state {
231 	struct ntvfs_async_state *prev, *next;
232 	/* the async handling infos */
233 	unsigned int state;
234 	void *private_data;
235 	void (*send_fn)(struct ntvfs_request *);
236 	NTSTATUS status;
237 
238 	/* the passthru module's per session private data */
239 	struct ntvfs_module_context *ntvfs;
240 };
241 
242 struct ntvfs_request {
243 	/* the ntvfs_context this requests belongs to */
244 	struct ntvfs_context *ctx;
245 
246 	/* ntvfs per request async states */
247 	struct ntvfs_async_state *async_states;
248 
249 	/* the session_info, with security_token and maybe delegated credentials */
250 	struct auth_session_info *session_info;
251 
252 	/* the smb pid is needed for locking contexts */
253 	uint16_t smbpid;
254 
255 	/* some statictics for the management tools */
256 	struct {
257 		/* the system time when the request arrived */
258 		struct timeval request_time;
259 	} statistics;
260 
261 	struct {
262 		void *private_data;
263 	} frontend_data;
264 };
265 
266 struct ntvfs_handle {
267 	struct ntvfs_context *ctx;
268 
269 	struct auth_session_info *session_info;
270 
271 	uint16_t smbpid;
272 
273 	struct ntvfs_handle_data {
274 		struct ntvfs_handle_data *prev, *next;
275 		struct ntvfs_module_context *owner;
276 		void *private_data;/* this must be a valid talloc pointer */
277 	} *backend_data;
278 
279 	struct {
280 		void *private_data;
281 	} frontend_data;
282 };
283 
284 /* this structure is used by backends to determine the size of some critical types */
285 struct ntvfs_critical_sizes {
286 	int interface_version;
287 	int sizeof_ntvfs_critical_sizes;
288 	int sizeof_ntvfs_context;
289 	int sizeof_ntvfs_module_context;
290 	int sizeof_ntvfs_ops;
291 	int sizeof_ntvfs_async_state;
292 	int sizeof_ntvfs_request;
293 	int sizeof_ntvfs_handle;
294 	int sizeof_ntvfs_handle_data;
295 };
296 
297 #define NTVFS_CURRENT_CRITICAL_SIZES(c) \
298     struct ntvfs_critical_sizes c = { \
299 	.interface_version		= NTVFS_INTERFACE_VERSION, \
300 	.sizeof_ntvfs_critical_sizes	= sizeof(struct ntvfs_critical_sizes), \
301 	.sizeof_ntvfs_context		= sizeof(struct ntvfs_context), \
302 	.sizeof_ntvfs_module_context	= sizeof(struct ntvfs_module_context), \
303 	.sizeof_ntvfs_ops		= sizeof(struct ntvfs_ops), \
304 	.sizeof_ntvfs_async_state	= sizeof(struct ntvfs_async_state), \
305 	.sizeof_ntvfs_request		= sizeof(struct ntvfs_request), \
306 	.sizeof_ntvfs_handle		= sizeof(struct ntvfs_handle), \
307 	.sizeof_ntvfs_handle_data	= sizeof(struct ntvfs_handle_data), \
308     }
309 
310 struct messaging_context;
311 #include "librpc/gen_ndr/security.h"
312 #include "librpc/gen_ndr/notify.h"
313 #include "ntvfs/ntvfs_proto.h"
314