1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Files[] structure handling
|
---|
4 | Copyright (C) Andrew Tridgell 1998
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "smbd/smbd.h"
|
---|
22 | #include "smbd/globals.h"
|
---|
23 | #include "libcli/security/security.h"
|
---|
24 | #include "util_tdb.h"
|
---|
25 |
|
---|
26 | #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
|
---|
27 |
|
---|
28 | #define FILE_HANDLE_OFFSET 0x1000
|
---|
29 |
|
---|
30 | /****************************************************************************
|
---|
31 | Return a unique number identifying this fsp over the life of this pid,
|
---|
32 | and try to make it as globally unique as possible.
|
---|
33 | See bug #8995 for the details.
|
---|
34 | ****************************************************************************/
|
---|
35 |
|
---|
36 | static unsigned long get_gen_count(struct smbd_server_connection *sconn)
|
---|
37 | {
|
---|
38 | /*
|
---|
39 | * While fsp->fh->gen_id is 'unsigned long' currently
|
---|
40 | * (which might by 8 bytes),
|
---|
41 | * there's some oplock code which truncates it to
|
---|
42 | * uint32_t(using IVAL()).
|
---|
43 | */
|
---|
44 | if (sconn->file_gen_counter == 0) {
|
---|
45 | sconn->file_gen_counter = generate_random();
|
---|
46 | }
|
---|
47 | sconn->file_gen_counter += 1;
|
---|
48 | if (sconn->file_gen_counter >= UINT32_MAX) {
|
---|
49 | sconn->file_gen_counter = 0;
|
---|
50 | }
|
---|
51 | if (sconn->file_gen_counter == 0) {
|
---|
52 | sconn->file_gen_counter += 1;
|
---|
53 | }
|
---|
54 | return sconn->file_gen_counter;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /****************************************************************************
|
---|
58 | Find first available file slot.
|
---|
59 | ****************************************************************************/
|
---|
60 |
|
---|
61 | NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
|
---|
62 | files_struct **result)
|
---|
63 | {
|
---|
64 | struct smbd_server_connection *sconn = conn->sconn;
|
---|
65 | int i;
|
---|
66 | files_struct *fsp;
|
---|
67 | NTSTATUS status;
|
---|
68 |
|
---|
69 | /* we want to give out file handles differently on each new
|
---|
70 | connection because of a common bug in MS clients where they try to
|
---|
71 | reuse a file descriptor from an earlier smb connection. This code
|
---|
72 | increases the chance that the errant client will get an error rather
|
---|
73 | than causing corruption */
|
---|
74 | if (sconn->first_file == 0) {
|
---|
75 | sconn->first_file = (sys_getpid() ^ (int)time(NULL));
|
---|
76 | sconn->first_file %= sconn->real_max_open_files;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* TODO: Port the id-tree implementation from Samba4 */
|
---|
80 |
|
---|
81 | i = bitmap_find(sconn->file_bmap, sconn->first_file);
|
---|
82 | if (i == -1) {
|
---|
83 | DEBUG(0,("ERROR! Out of file structures\n"));
|
---|
84 | /* TODO: We have to unconditionally return a DOS error here,
|
---|
85 | * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
|
---|
86 | * NTSTATUS negotiated */
|
---|
87 | return NT_STATUS_TOO_MANY_OPENED_FILES;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Make a child of the connection_struct as an fsp can't exist
|
---|
92 | * independent of a connection.
|
---|
93 | */
|
---|
94 | fsp = talloc_zero(conn, struct files_struct);
|
---|
95 | if (!fsp) {
|
---|
96 | return NT_STATUS_NO_MEMORY;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * This can't be a child of fsp because the file_handle can be ref'd
|
---|
101 | * when doing a dos/fcb open, which will then share the file_handle
|
---|
102 | * across multiple fsps.
|
---|
103 | */
|
---|
104 | fsp->fh = talloc_zero(conn, struct fd_handle);
|
---|
105 | if (!fsp->fh) {
|
---|
106 | TALLOC_FREE(fsp);
|
---|
107 | return NT_STATUS_NO_MEMORY;
|
---|
108 | }
|
---|
109 |
|
---|
110 | fsp->fh->ref_count = 1;
|
---|
111 | fsp->fh->fd = -1;
|
---|
112 |
|
---|
113 | fsp->conn = conn;
|
---|
114 | fsp->fh->gen_id = get_gen_count(sconn);
|
---|
115 | GetTimeOfDay(&fsp->open_time);
|
---|
116 |
|
---|
117 | sconn->first_file = (i+1) % (sconn->real_max_open_files);
|
---|
118 |
|
---|
119 | bitmap_set(sconn->file_bmap, i);
|
---|
120 | sconn->files_used += 1;
|
---|
121 |
|
---|
122 | fsp->fnum = i + FILE_HANDLE_OFFSET;
|
---|
123 | SMB_ASSERT(fsp->fnum < 65536);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Create an smb_filename with "" for the base_name. There are very
|
---|
127 | * few NULL checks, so make sure it's initialized with something. to
|
---|
128 | * be safe until an audit can be done.
|
---|
129 | */
|
---|
130 | status = create_synthetic_smb_fname(fsp, "", NULL, NULL,
|
---|
131 | &fsp->fsp_name);
|
---|
132 | if (!NT_STATUS_IS_OK(status)) {
|
---|
133 | TALLOC_FREE(fsp);
|
---|
134 | TALLOC_FREE(fsp->fh);
|
---|
135 | }
|
---|
136 |
|
---|
137 | DLIST_ADD(sconn->files, fsp);
|
---|
138 |
|
---|
139 | DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
|
---|
140 | i, fsp->fnum, sconn->files_used));
|
---|
141 |
|
---|
142 | if (req != NULL) {
|
---|
143 | req->chain_fsp = fsp;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /* A new fsp invalidates the positive and
|
---|
147 | negative fsp_fi_cache as the new fsp is pushed
|
---|
148 | at the start of the list and we search from
|
---|
149 | a cache hit to the *end* of the list. */
|
---|
150 |
|
---|
151 | ZERO_STRUCT(sconn->fsp_fi_cache);
|
---|
152 |
|
---|
153 | conn->num_files_open++;
|
---|
154 |
|
---|
155 | *result = fsp;
|
---|
156 | return NT_STATUS_OK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /****************************************************************************
|
---|
160 | Close all open files for a connection.
|
---|
161 | ****************************************************************************/
|
---|
162 |
|
---|
163 | void file_close_conn(connection_struct *conn)
|
---|
164 | {
|
---|
165 | files_struct *fsp, *next;
|
---|
166 |
|
---|
167 | for (fsp=conn->sconn->files; fsp; fsp=next) {
|
---|
168 | next = fsp->next;
|
---|
169 | if (fsp->conn == conn) {
|
---|
170 | close_file(NULL, fsp, SHUTDOWN_CLOSE);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /****************************************************************************
|
---|
176 | Close all open files for a pid and a vuid.
|
---|
177 | ****************************************************************************/
|
---|
178 |
|
---|
179 | void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
|
---|
180 | int vuid)
|
---|
181 | {
|
---|
182 | files_struct *fsp, *next;
|
---|
183 |
|
---|
184 | for (fsp=sconn->files;fsp;fsp=next) {
|
---|
185 | next = fsp->next;
|
---|
186 | if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
|
---|
187 | close_file(NULL, fsp, SHUTDOWN_CLOSE);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /****************************************************************************
|
---|
193 | Initialise file structures.
|
---|
194 | ****************************************************************************/
|
---|
195 |
|
---|
196 | bool file_init(struct smbd_server_connection *sconn)
|
---|
197 | {
|
---|
198 | int request_max_open_files = lp_max_open_files();
|
---|
199 | int real_lim;
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Set the max_open files to be the requested
|
---|
203 | * max plus a fudgefactor to allow for the extra
|
---|
204 | * fd's we need such as log files etc...
|
---|
205 | */
|
---|
206 | real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
|
---|
207 |
|
---|
208 | sconn->real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
|
---|
209 |
|
---|
210 | if (sconn->real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES
|
---|
211 | > 65536)
|
---|
212 | sconn->real_max_open_files =
|
---|
213 | 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
|
---|
214 |
|
---|
215 | if(sconn->real_max_open_files != request_max_open_files) {
|
---|
216 | DEBUG(1, ("file_init: Information only: requested %d "
|
---|
217 | "open files, %d are available.\n",
|
---|
218 | request_max_open_files, sconn->real_max_open_files));
|
---|
219 | }
|
---|
220 |
|
---|
221 | SMB_ASSERT(sconn->real_max_open_files > 100);
|
---|
222 |
|
---|
223 | sconn->file_bmap = bitmap_talloc(sconn, sconn->real_max_open_files);
|
---|
224 |
|
---|
225 | if (!sconn->file_bmap) {
|
---|
226 | return false;
|
---|
227 | }
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /****************************************************************************
|
---|
232 | Close files open by a specified vuid.
|
---|
233 | ****************************************************************************/
|
---|
234 |
|
---|
235 | void file_close_user(struct smbd_server_connection *sconn, int vuid)
|
---|
236 | {
|
---|
237 | files_struct *fsp, *next;
|
---|
238 |
|
---|
239 | for (fsp=sconn->files; fsp; fsp=next) {
|
---|
240 | next=fsp->next;
|
---|
241 | if (fsp->vuid == vuid) {
|
---|
242 | close_file(NULL, fsp, SHUTDOWN_CLOSE);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Walk the files table until "fn" returns non-NULL
|
---|
249 | */
|
---|
250 |
|
---|
251 | struct files_struct *files_forall(
|
---|
252 | struct smbd_server_connection *sconn,
|
---|
253 | struct files_struct *(*fn)(struct files_struct *fsp,
|
---|
254 | void *private_data),
|
---|
255 | void *private_data)
|
---|
256 | {
|
---|
257 | struct files_struct *fsp, *next;
|
---|
258 |
|
---|
259 | for (fsp = sconn->files; fsp; fsp = next) {
|
---|
260 | struct files_struct *ret;
|
---|
261 | next = fsp->next;
|
---|
262 | ret = fn(fsp, private_data);
|
---|
263 | if (ret != NULL) {
|
---|
264 | return ret;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | return NULL;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /****************************************************************************
|
---|
271 | Find a fsp given a file descriptor.
|
---|
272 | ****************************************************************************/
|
---|
273 |
|
---|
274 | files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
|
---|
275 | {
|
---|
276 | int count=0;
|
---|
277 | files_struct *fsp;
|
---|
278 |
|
---|
279 | for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
|
---|
280 | if (fsp->fh->fd == fd) {
|
---|
281 | if (count > 10) {
|
---|
282 | DLIST_PROMOTE(sconn->files, fsp);
|
---|
283 | }
|
---|
284 | return fsp;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | return NULL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /****************************************************************************
|
---|
292 | Find a fsp given a device, inode and file_id.
|
---|
293 | ****************************************************************************/
|
---|
294 |
|
---|
295 | files_struct *file_find_dif(struct smbd_server_connection *sconn,
|
---|
296 | struct file_id id, unsigned long gen_id)
|
---|
297 | {
|
---|
298 | int count=0;
|
---|
299 | files_struct *fsp;
|
---|
300 |
|
---|
301 | if (gen_id == 0) {
|
---|
302 | return NULL;
|
---|
303 | }
|
---|
304 |
|
---|
305 | for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
|
---|
306 | /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
|
---|
307 | if (file_id_equal(&fsp->file_id, &id) &&
|
---|
308 | fsp->fh->gen_id == gen_id ) {
|
---|
309 | if (count > 10) {
|
---|
310 | DLIST_PROMOTE(sconn->files, fsp);
|
---|
311 | }
|
---|
312 | /* Paranoia check. */
|
---|
313 | if ((fsp->fh->fd == -1) &&
|
---|
314 | (fsp->oplock_type != NO_OPLOCK) &&
|
---|
315 | (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
|
---|
316 | DEBUG(0,("file_find_dif: file %s file_id = "
|
---|
317 | "%s, gen = %u oplock_type = %u is a "
|
---|
318 | "stat open with oplock type !\n",
|
---|
319 | fsp_str_dbg(fsp),
|
---|
320 | file_id_string_tos(&fsp->file_id),
|
---|
321 | (unsigned int)fsp->fh->gen_id,
|
---|
322 | (unsigned int)fsp->oplock_type ));
|
---|
323 | smb_panic("file_find_dif");
|
---|
324 | }
|
---|
325 | return fsp;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | return NULL;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /****************************************************************************
|
---|
333 | Find the first fsp given a device and inode.
|
---|
334 | We use a singleton cache here to speed up searching from getfilepathinfo
|
---|
335 | calls.
|
---|
336 | ****************************************************************************/
|
---|
337 |
|
---|
338 | files_struct *file_find_di_first(struct smbd_server_connection *sconn,
|
---|
339 | struct file_id id)
|
---|
340 | {
|
---|
341 | files_struct *fsp;
|
---|
342 |
|
---|
343 | if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
|
---|
344 | /* Positive or negative cache hit. */
|
---|
345 | return sconn->fsp_fi_cache.fsp;
|
---|
346 | }
|
---|
347 |
|
---|
348 | sconn->fsp_fi_cache.id = id;
|
---|
349 |
|
---|
350 | for (fsp=sconn->files;fsp;fsp=fsp->next) {
|
---|
351 | if (file_id_equal(&fsp->file_id, &id)) {
|
---|
352 | /* Setup positive cache. */
|
---|
353 | sconn->fsp_fi_cache.fsp = fsp;
|
---|
354 | return fsp;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Setup negative cache. */
|
---|
359 | sconn->fsp_fi_cache.fsp = NULL;
|
---|
360 | return NULL;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /****************************************************************************
|
---|
364 | Find the next fsp having the same device and inode.
|
---|
365 | ****************************************************************************/
|
---|
366 |
|
---|
367 | files_struct *file_find_di_next(files_struct *start_fsp)
|
---|
368 | {
|
---|
369 | files_struct *fsp;
|
---|
370 |
|
---|
371 | for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
|
---|
372 | if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
|
---|
373 | return fsp;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | return NULL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /****************************************************************************
|
---|
381 | Find any fsp open with a pathname below that of an already open path.
|
---|
382 | ****************************************************************************/
|
---|
383 |
|
---|
384 | bool file_find_subpath(files_struct *dir_fsp)
|
---|
385 | {
|
---|
386 | files_struct *fsp;
|
---|
387 | size_t dlen;
|
---|
388 | char *d_fullname = NULL;
|
---|
389 |
|
---|
390 | d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
|
---|
391 | dir_fsp->conn->connectpath,
|
---|
392 | dir_fsp->fsp_name->base_name);
|
---|
393 |
|
---|
394 | if (!d_fullname) {
|
---|
395 | return false;
|
---|
396 | }
|
---|
397 |
|
---|
398 | dlen = strlen(d_fullname);
|
---|
399 |
|
---|
400 | for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
|
---|
401 | char *d1_fullname;
|
---|
402 |
|
---|
403 | if (fsp == dir_fsp) {
|
---|
404 | continue;
|
---|
405 | }
|
---|
406 |
|
---|
407 | d1_fullname = talloc_asprintf(talloc_tos(),
|
---|
408 | "%s/%s",
|
---|
409 | fsp->conn->connectpath,
|
---|
410 | fsp->fsp_name->base_name);
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * If the open file has a path that is a longer
|
---|
414 | * component, then it's a subpath.
|
---|
415 | */
|
---|
416 | if (strnequal(d_fullname, d1_fullname, dlen) &&
|
---|
417 | (d1_fullname[dlen] == '/')) {
|
---|
418 | TALLOC_FREE(d1_fullname);
|
---|
419 | TALLOC_FREE(d_fullname);
|
---|
420 | return true;
|
---|
421 | }
|
---|
422 | TALLOC_FREE(d1_fullname);
|
---|
423 | }
|
---|
424 |
|
---|
425 | TALLOC_FREE(d_fullname);
|
---|
426 | return false;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /****************************************************************************
|
---|
430 | Sync open files on a connection.
|
---|
431 | ****************************************************************************/
|
---|
432 |
|
---|
433 | void file_sync_all(connection_struct *conn)
|
---|
434 | {
|
---|
435 | files_struct *fsp, *next;
|
---|
436 |
|
---|
437 | for (fsp=conn->sconn->files; fsp; fsp=next) {
|
---|
438 | next=fsp->next;
|
---|
439 | if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
|
---|
440 | sync_file(conn, fsp, True /* write through */);
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | /****************************************************************************
|
---|
446 | Free up a fsp.
|
---|
447 | ****************************************************************************/
|
---|
448 |
|
---|
449 | void file_free(struct smb_request *req, files_struct *fsp)
|
---|
450 | {
|
---|
451 | struct smbd_server_connection *sconn = fsp->conn->sconn;
|
---|
452 |
|
---|
453 | DLIST_REMOVE(sconn->files, fsp);
|
---|
454 |
|
---|
455 | TALLOC_FREE(fsp->fake_file_handle);
|
---|
456 |
|
---|
457 | if (fsp->fh->ref_count == 1) {
|
---|
458 | TALLOC_FREE(fsp->fh);
|
---|
459 | } else {
|
---|
460 | fsp->fh->ref_count--;
|
---|
461 | }
|
---|
462 |
|
---|
463 | if (fsp->notify) {
|
---|
464 | if (fsp->is_directory) {
|
---|
465 | notify_remove_onelevel(fsp->conn->notify_ctx,
|
---|
466 | &fsp->file_id, fsp);
|
---|
467 | }
|
---|
468 | notify_remove(fsp->conn->notify_ctx, fsp);
|
---|
469 | TALLOC_FREE(fsp->notify);
|
---|
470 | }
|
---|
471 |
|
---|
472 | /* Ensure this event will never fire. */
|
---|
473 | TALLOC_FREE(fsp->oplock_timeout);
|
---|
474 |
|
---|
475 | /* Ensure this event will never fire. */
|
---|
476 | TALLOC_FREE(fsp->update_write_time_event);
|
---|
477 |
|
---|
478 | bitmap_clear(sconn->file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
|
---|
479 | sconn->files_used--;
|
---|
480 |
|
---|
481 | DEBUG(5,("freed files structure %d (%d used)\n",
|
---|
482 | fsp->fnum, sconn->files_used));
|
---|
483 |
|
---|
484 | fsp->conn->num_files_open--;
|
---|
485 |
|
---|
486 | if ((req != NULL) && (fsp == req->chain_fsp)) {
|
---|
487 | req->chain_fsp = NULL;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * Clear all possible chained fsp
|
---|
492 | * pointers in the SMB2 request queue.
|
---|
493 | */
|
---|
494 | if (req != NULL && req->smb2req) {
|
---|
495 | remove_smb2_chained_fsp(fsp);
|
---|
496 | }
|
---|
497 |
|
---|
498 | /* Closing a file can invalidate the positive cache. */
|
---|
499 | if (fsp == sconn->fsp_fi_cache.fsp) {
|
---|
500 | ZERO_STRUCT(sconn->fsp_fi_cache);
|
---|
501 | }
|
---|
502 |
|
---|
503 | /* Drop all remaining extensions. */
|
---|
504 | while (fsp->vfs_extension) {
|
---|
505 | vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
|
---|
506 | }
|
---|
507 |
|
---|
508 | /* this is paranoia, just in case someone tries to reuse the
|
---|
509 | information */
|
---|
510 | ZERO_STRUCTP(fsp);
|
---|
511 |
|
---|
512 | /* fsp->fsp_name is a talloc child and is free'd automatically. */
|
---|
513 | TALLOC_FREE(fsp);
|
---|
514 | }
|
---|
515 |
|
---|
516 | /****************************************************************************
|
---|
517 | Get an fsp from a 16 bit fnum.
|
---|
518 | ****************************************************************************/
|
---|
519 |
|
---|
520 | static struct files_struct *file_fnum(struct smbd_server_connection *sconn,
|
---|
521 | uint16 fnum)
|
---|
522 | {
|
---|
523 | files_struct *fsp;
|
---|
524 | int count=0;
|
---|
525 |
|
---|
526 | for (fsp=sconn->files; fsp; fsp=fsp->next, count++) {
|
---|
527 | if (fsp->fnum == fnum) {
|
---|
528 | if (count > 10) {
|
---|
529 | DLIST_PROMOTE(sconn->files, fsp);
|
---|
530 | }
|
---|
531 | return fsp;
|
---|
532 | }
|
---|
533 | }
|
---|
534 | return NULL;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /****************************************************************************
|
---|
538 | Get an fsp from a packet given a 16 bit fnum.
|
---|
539 | ****************************************************************************/
|
---|
540 |
|
---|
541 | files_struct *file_fsp(struct smb_request *req, uint16 fid)
|
---|
542 | {
|
---|
543 | files_struct *fsp;
|
---|
544 |
|
---|
545 | if (req == NULL) {
|
---|
546 | /*
|
---|
547 | * We should never get here. req==NULL could in theory
|
---|
548 | * only happen from internal opens with a non-zero
|
---|
549 | * root_dir_fid. Internal opens just don't do that, at
|
---|
550 | * least they are not supposed to do so. And if they
|
---|
551 | * start to do so, they better fake up a smb_request
|
---|
552 | * from which we get the right smbd_server_conn. While
|
---|
553 | * this should never happen, let's return NULL here.
|
---|
554 | */
|
---|
555 | return NULL;
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (req->chain_fsp != NULL) {
|
---|
559 | return req->chain_fsp;
|
---|
560 | }
|
---|
561 |
|
---|
562 | fsp = file_fnum(req->sconn, fid);
|
---|
563 | if (fsp != NULL) {
|
---|
564 | req->chain_fsp = fsp;
|
---|
565 | }
|
---|
566 | return fsp;
|
---|
567 | }
|
---|
568 |
|
---|
569 | uint64_t fsp_persistent_id(const struct files_struct *fsp)
|
---|
570 | {
|
---|
571 | uint64_t persistent_id;
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * This calculates a number that is most likely
|
---|
575 | * globally unique. In future we will have a database
|
---|
576 | * to make it completely unique.
|
---|
577 | *
|
---|
578 | * 32-bit random gen_id
|
---|
579 | * 16-bit truncated open_time
|
---|
580 | * 16-bit fnum (valatile_id)
|
---|
581 | */
|
---|
582 | persistent_id = fsp->fh->gen_id & UINT32_MAX;
|
---|
583 | persistent_id <<= 16;
|
---|
584 | persistent_id &= 0x0000FFFFFFFF0000LLU;
|
---|
585 | persistent_id |= fsp->open_time.tv_usec & UINT16_MAX;
|
---|
586 | persistent_id <<= 16;
|
---|
587 | persistent_id &= 0xFFFFFFFFFFFF0000LLU;
|
---|
588 | persistent_id |= fsp->fnum & UINT16_MAX;
|
---|
589 |
|
---|
590 | return persistent_id;
|
---|
591 | }
|
---|
592 |
|
---|
593 | struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
|
---|
594 | uint64_t persistent_id,
|
---|
595 | uint64_t volatile_id)
|
---|
596 | {
|
---|
597 | struct files_struct *fsp;
|
---|
598 | uint64_t fsp_persistent;
|
---|
599 |
|
---|
600 | if (smb2req->compat_chain_fsp != NULL) {
|
---|
601 | return smb2req->compat_chain_fsp;
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (volatile_id > UINT16_MAX) {
|
---|
605 | return NULL;
|
---|
606 | }
|
---|
607 |
|
---|
608 | fsp = file_fnum(smb2req->sconn, (uint16_t)volatile_id);
|
---|
609 | if (fsp == NULL) {
|
---|
610 | return NULL;
|
---|
611 | }
|
---|
612 | fsp_persistent = fsp_persistent_id(fsp);
|
---|
613 |
|
---|
614 | if (persistent_id != fsp_persistent) {
|
---|
615 | return NULL;
|
---|
616 | }
|
---|
617 |
|
---|
618 | if (smb2req->tcon == NULL) {
|
---|
619 | return NULL;
|
---|
620 | }
|
---|
621 |
|
---|
622 | if (smb2req->tcon->compat_conn != fsp->conn) {
|
---|
623 | return NULL;
|
---|
624 | }
|
---|
625 |
|
---|
626 | if (smb2req->session == NULL) {
|
---|
627 | return NULL;
|
---|
628 | }
|
---|
629 |
|
---|
630 | if (smb2req->session->vuid != fsp->vuid) {
|
---|
631 | return NULL;
|
---|
632 | }
|
---|
633 |
|
---|
634 | smb2req->compat_chain_fsp = fsp;
|
---|
635 | return fsp;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /****************************************************************************
|
---|
639 | Duplicate the file handle part for a DOS or FCB open.
|
---|
640 | ****************************************************************************/
|
---|
641 |
|
---|
642 | NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
|
---|
643 | uint32 access_mask, uint32 share_access,
|
---|
644 | uint32 create_options, files_struct *to)
|
---|
645 | {
|
---|
646 | TALLOC_FREE(to->fh);
|
---|
647 |
|
---|
648 | to->fh = from->fh;
|
---|
649 | to->fh->ref_count++;
|
---|
650 |
|
---|
651 | to->file_id = from->file_id;
|
---|
652 | to->initial_allocation_size = from->initial_allocation_size;
|
---|
653 | to->mode = from->mode;
|
---|
654 | to->file_pid = from->file_pid;
|
---|
655 | to->vuid = from->vuid;
|
---|
656 | to->open_time = from->open_time;
|
---|
657 | to->access_mask = access_mask;
|
---|
658 | to->share_access = share_access;
|
---|
659 | to->oplock_type = from->oplock_type;
|
---|
660 | to->can_lock = from->can_lock;
|
---|
661 | to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
|
---|
662 | if (!CAN_WRITE(from->conn)) {
|
---|
663 | to->can_write = False;
|
---|
664 | } else {
|
---|
665 | to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
|
---|
666 | }
|
---|
667 | to->modified = from->modified;
|
---|
668 | to->is_directory = from->is_directory;
|
---|
669 | to->aio_write_behind = from->aio_write_behind;
|
---|
670 |
|
---|
671 | if (from->print_file) {
|
---|
672 | to->print_file = talloc(to, struct print_file_data);
|
---|
673 | if (!to->print_file) return NT_STATUS_NO_MEMORY;
|
---|
674 | to->print_file->rap_jobid = from->print_file->rap_jobid;
|
---|
675 | } else {
|
---|
676 | to->print_file = NULL;
|
---|
677 | }
|
---|
678 |
|
---|
679 | return fsp_set_smb_fname(to, from->fsp_name);
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * Return a jenkins hash of a pathname on a connection.
|
---|
684 | */
|
---|
685 |
|
---|
686 | NTSTATUS file_name_hash(connection_struct *conn,
|
---|
687 | const char *name, uint32_t *p_name_hash)
|
---|
688 | {
|
---|
689 | TDB_DATA key;
|
---|
690 | char *fullpath = NULL;
|
---|
691 |
|
---|
692 | /* Set the hash of the full pathname. */
|
---|
693 | fullpath = talloc_asprintf(talloc_tos(),
|
---|
694 | "%s/%s",
|
---|
695 | conn->connectpath,
|
---|
696 | name);
|
---|
697 | if (!fullpath) {
|
---|
698 | return NT_STATUS_NO_MEMORY;
|
---|
699 | }
|
---|
700 | key = string_term_tdb_data(fullpath);
|
---|
701 | *p_name_hash = tdb_jenkins_hash(&key);
|
---|
702 |
|
---|
703 | DEBUG(10,("file_name_hash: %s hash 0x%x\n",
|
---|
704 | fullpath,
|
---|
705 | (unsigned int)*p_name_hash ));
|
---|
706 |
|
---|
707 | TALLOC_FREE(fullpath);
|
---|
708 | return NT_STATUS_OK;
|
---|
709 | }
|
---|
710 |
|
---|
711 | /**
|
---|
712 | * The only way that the fsp->fsp_name field should ever be set.
|
---|
713 | */
|
---|
714 | NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
|
---|
715 | const struct smb_filename *smb_fname_in)
|
---|
716 | {
|
---|
717 | NTSTATUS status;
|
---|
718 | struct smb_filename *smb_fname_new;
|
---|
719 |
|
---|
720 | status = copy_smb_filename(fsp, smb_fname_in, &smb_fname_new);
|
---|
721 | if (!NT_STATUS_IS_OK(status)) {
|
---|
722 | return status;
|
---|
723 | }
|
---|
724 |
|
---|
725 | TALLOC_FREE(fsp->fsp_name);
|
---|
726 | fsp->fsp_name = smb_fname_new;
|
---|
727 |
|
---|
728 | return file_name_hash(fsp->conn,
|
---|
729 | smb_fname_str_dbg(fsp->fsp_name),
|
---|
730 | &fsp->name_hash);
|
---|
731 | }
|
---|