Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Main SMB reply routines
4 : Copyright (C) Andrew Tridgell 1992-1998
5 : Copyright (C) Andrew Bartlett 2001
6 : Copyright (C) Jeremy Allison 1992-2007.
7 : Copyright (C) Volker Lendecke 2007
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 : /*
23 : This file handles most of the reply_ calls that the server
24 : makes to handle specific protocols
25 : */
26 :
27 : #include "includes.h"
28 : #include "libsmb/namequery.h"
29 : #include "system/filesys.h"
30 : #include "printing.h"
31 : #include "locking/share_mode_lock.h"
32 : #include "smbd/smbd.h"
33 : #include "smbd/globals.h"
34 : #include "smbd/smbXsrv_open.h"
35 : #include "fake_file.h"
36 : #include "rpc_client/rpc_client.h"
37 : #include "../librpc/gen_ndr/ndr_spoolss_c.h"
38 : #include "rpc_client/cli_spoolss.h"
39 : #include "rpc_client/init_spoolss.h"
40 : #include "rpc_server/rpc_ncacn_np.h"
41 : #include "libcli/security/security.h"
42 : #include "libsmb/nmblib.h"
43 : #include "auth.h"
44 : #include "smbprofile.h"
45 : #include "../lib/tsocket/tsocket.h"
46 : #include "lib/util/tevent_ntstatus.h"
47 : #include "libcli/smb/smb_signing.h"
48 : #include "lib/util/sys_rw_data.h"
49 : #include "librpc/gen_ndr/open_files.h"
50 : #include "libcli/smb/smb2_posix.h"
51 : #include "lib/util/string_wrappers.h"
52 : #include "source3/printing/rap_jobid.h"
53 : #include "source3/lib/substitute.h"
54 :
55 : /****************************************************************************
56 : Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
57 : path or anything including wildcards.
58 : We're assuming here that '/' is not the second byte in any multibyte char
59 : set (a safe assumption). '\\' *may* be the second byte in a multibyte char
60 : set.
61 : ****************************************************************************/
62 :
63 : /* Custom version for processing POSIX paths. */
64 : #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
65 :
66 17700 : static NTSTATUS check_path_syntax_internal(char *path,
67 : bool posix_path)
68 : {
69 17700 : char *d = path;
70 17700 : const char *s = path;
71 17700 : NTSTATUS ret = NT_STATUS_OK;
72 17700 : bool start_of_name_component = True;
73 17700 : bool stream_started = false;
74 17700 : bool last_component_contains_wcard = false;
75 :
76 728038 : while (*s) {
77 710338 : if (stream_started) {
78 188 : switch (*s) {
79 0 : case '/':
80 : case '\\':
81 0 : return NT_STATUS_OBJECT_NAME_INVALID;
82 1 : case ':':
83 1 : if (s[1] == '\0') {
84 0 : return NT_STATUS_OBJECT_NAME_INVALID;
85 : }
86 1 : if (strchr_m(&s[1], ':')) {
87 0 : return NT_STATUS_OBJECT_NAME_INVALID;
88 : }
89 1 : break;
90 : }
91 : }
92 :
93 710338 : if ((*s == ':') && !posix_path && !stream_started) {
94 49 : if (last_component_contains_wcard) {
95 0 : return NT_STATUS_OBJECT_NAME_INVALID;
96 : }
97 : /* Stream names allow more characters than file names.
98 : We're overloading posix_path here to allow a wider
99 : range of characters. If stream_started is true this
100 : is still a Windows path even if posix_path is true.
101 : JRA.
102 : */
103 49 : stream_started = true;
104 49 : start_of_name_component = false;
105 49 : posix_path = true;
106 :
107 49 : if (s[1] == '\0') {
108 0 : return NT_STATUS_OBJECT_NAME_INVALID;
109 : }
110 : }
111 :
112 710338 : if (!stream_started && IS_PATH_SEP(*s,posix_path)) {
113 : /*
114 : * Safe to assume is not the second part of a mb char
115 : * as this is handled below.
116 : */
117 : /* Eat multiple '/' or '\\' */
118 105209 : while (IS_PATH_SEP(*s,posix_path)) {
119 52624 : s++;
120 : }
121 52585 : if ((d != path) && (*s != '\0')) {
122 : /* We only care about non-leading or trailing '/' or '\\' */
123 52580 : *d++ = '/';
124 : }
125 :
126 52585 : start_of_name_component = True;
127 : /* New component. */
128 52585 : last_component_contains_wcard = false;
129 52585 : continue;
130 : }
131 :
132 657753 : if (start_of_name_component) {
133 63300 : if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
134 : /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
135 :
136 : /*
137 : * No mb char starts with '.' so we're safe checking the directory separator here.
138 : */
139 :
140 : /* If we just added a '/' - delete it */
141 0 : if ((d > path) && (*(d-1) == '/')) {
142 0 : *(d-1) = '\0';
143 0 : d--;
144 : }
145 :
146 : /* Are we at the start ? Can't go back further if so. */
147 0 : if (d <= path) {
148 0 : ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
149 0 : break;
150 : }
151 : /* Go back one level... */
152 : /* We know this is safe as '/' cannot be part of a mb sequence. */
153 : /* NOTE - if this assumption is invalid we are not in good shape... */
154 : /* Decrement d first as d points to the *next* char to write into. */
155 0 : for (d--; d > path; d--) {
156 0 : if (*d == '/')
157 0 : break;
158 : }
159 0 : s += 2; /* Else go past the .. */
160 : /* We're still at the start of a name component, just the previous one. */
161 0 : continue;
162 :
163 63300 : } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
164 0 : if (posix_path) {
165 : /* Eat the '.' */
166 0 : s++;
167 0 : continue;
168 : }
169 : }
170 :
171 : }
172 :
173 657753 : if (!(*s & 0x80)) {
174 657753 : if (!posix_path) {
175 657516 : if (*s <= 0x1f || *s == '|') {
176 0 : return NT_STATUS_OBJECT_NAME_INVALID;
177 : }
178 657516 : switch (*s) {
179 0 : case '*':
180 : case '?':
181 : case '<':
182 : case '>':
183 : case '"':
184 0 : last_component_contains_wcard = true;
185 0 : break;
186 657516 : default:
187 657516 : break;
188 : }
189 : }
190 657753 : *d++ = *s++;
191 : } else {
192 : size_t siz;
193 : /* Get the size of the next MB character. */
194 0 : next_codepoint(s,&siz);
195 0 : switch(siz) {
196 0 : case 5:
197 0 : *d++ = *s++;
198 : FALL_THROUGH;
199 0 : case 4:
200 0 : *d++ = *s++;
201 : FALL_THROUGH;
202 0 : case 3:
203 0 : *d++ = *s++;
204 : FALL_THROUGH;
205 0 : case 2:
206 0 : *d++ = *s++;
207 : FALL_THROUGH;
208 0 : case 1:
209 0 : *d++ = *s++;
210 0 : break;
211 0 : default:
212 0 : DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
213 0 : *d = '\0';
214 0 : return NT_STATUS_INVALID_PARAMETER;
215 : }
216 : }
217 657753 : start_of_name_component = False;
218 : }
219 :
220 17700 : *d = '\0';
221 :
222 17700 : return ret;
223 : }
224 :
225 : /****************************************************************************
226 : Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
227 : No wildcards allowed.
228 : ****************************************************************************/
229 :
230 17700 : NTSTATUS check_path_syntax(char *path)
231 : {
232 17700 : return check_path_syntax_internal(path, false);
233 : }
234 :
235 : /****************************************************************************
236 : Check the path for a POSIX client.
237 : We're assuming here that '/' is not the second byte in any multibyte char
238 : set (a safe assumption).
239 : ****************************************************************************/
240 :
241 0 : NTSTATUS check_path_syntax_posix(char *path)
242 : {
243 0 : return check_path_syntax_internal(path, true);
244 : }
245 :
246 : /****************************************************************************
247 : Check the path for an SMB2 DFS path.
248 : SMB2 DFS paths look like hostname\share (followed by a possible \extrapath.
249 : Path returned from here must look like:
250 : hostname/share (followed by a possible /extrapath).
251 : ****************************************************************************/
252 :
253 1048 : static NTSTATUS check_path_syntax_smb2_msdfs(char *path)
254 : {
255 1048 : char *share = NULL;
256 1048 : char *remaining_path = NULL;
257 : /* No SMB2 names can start with '\\' */
258 1048 : if (path[0] == '\\') {
259 0 : return NT_STATUS_OBJECT_NAME_INVALID;
260 : }
261 : /*
262 : * smbclient libraries sometimes set the DFS flag and send
263 : * local pathnames. Cope with this by just calling
264 : * check_path_syntax() on the whole path if it doesn't
265 : * look like a DFS path, similar to what parse_dfs_path() does.
266 : */
267 : /* servername should be at path[0] */
268 1048 : share = strchr(path, '\\');
269 1048 : if (share == NULL) {
270 0 : return check_path_syntax(path);
271 : }
272 1048 : *share++ = '/';
273 1048 : remaining_path = strchr(share, '\\');
274 1048 : if (remaining_path == NULL) {
275 : /* Only hostname\share. We're done. */
276 84 : return NT_STATUS_OK;
277 : }
278 964 : *remaining_path++ = '/';
279 964 : return check_path_syntax(remaining_path);
280 : }
281 :
282 15071 : NTSTATUS check_path_syntax_smb2(char *path, bool dfs_path)
283 : {
284 15071 : if (dfs_path) {
285 1048 : return check_path_syntax_smb2_msdfs(path);
286 : } else {
287 14023 : return check_path_syntax(path);
288 : }
289 : }
290 :
291 : /****************************************************************************
292 : Pull a string and check the path allowing a wildcard - provide for error return.
293 : Passes in posix flag.
294 : ****************************************************************************/
295 :
296 4 : static size_t srvstr_get_path_internal(TALLOC_CTX *ctx,
297 : const char *base_ptr,
298 : uint16_t smb_flags2,
299 : char **pp_dest,
300 : const char *src,
301 : size_t src_len,
302 : int flags,
303 : bool posix_pathnames,
304 : NTSTATUS *err)
305 : {
306 : size_t ret;
307 4 : char *dst = NULL;
308 :
309 4 : *pp_dest = NULL;
310 :
311 4 : ret = srvstr_pull_talloc(ctx, base_ptr, smb_flags2, pp_dest, src,
312 : src_len, flags);
313 :
314 4 : if (!*pp_dest) {
315 0 : *err = NT_STATUS_INVALID_PARAMETER;
316 0 : return ret;
317 : }
318 :
319 4 : dst = *pp_dest;
320 :
321 4 : if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
322 : /*
323 : * A valid DFS path looks either like
324 : * /server/share
325 : * \server\share
326 : * (there may be more components after).
327 : * Either way it must have at least two separators.
328 : *
329 : * Ensure we end up as /server/share
330 : * so we don't need to special case
331 : * separator characters elsewhere in
332 : * the code.
333 : */
334 0 : char *server = NULL;
335 0 : char *share = NULL;
336 0 : char *remaining_path = NULL;
337 0 : char path_sep = 0;
338 0 : char *p = NULL;
339 :
340 0 : if (posix_pathnames && (dst[0] == '/')) {
341 0 : path_sep = dst[0];
342 0 : } else if (dst[0] == '\\') {
343 0 : path_sep = dst[0];
344 : }
345 :
346 0 : if (path_sep == 0) {
347 0 : goto local_path;
348 : }
349 : /*
350 : * May be a DFS path.
351 : * We need some heuristics here,
352 : * as clients differ on what constitutes
353 : * a well-formed DFS path. If the path
354 : * appears malformed, just fall back to
355 : * processing as a local path.
356 : */
357 0 : server = dst;
358 :
359 : /*
360 : * Cosmetic fix for Linux-only DFS clients.
361 : * The Linux kernel SMB1 client has a bug - it sends
362 : * DFS pathnames as:
363 : *
364 : * \\server\share\path
365 : *
366 : * Causing us to mis-parse server,share,remaining_path here
367 : * and jump into 'goto local_path' at 'share\path' instead
368 : * of 'path'.
369 : *
370 : * This doesn't cause an error as the limits on share names
371 : * are similar to those on pathnames.
372 : *
373 : * parse_dfs_path() which we call before filename parsing
374 : * copes with this by calling trim_char on the leading '\'
375 : * characters before processing.
376 : * Do the same here so logging of pathnames looks better.
377 : */
378 0 : if (server[1] == path_sep) {
379 0 : trim_char(&server[1], path_sep, '\0');
380 : }
381 :
382 : /*
383 : * Look to see if we also have /share following.
384 : */
385 0 : share = strchr(server+1, path_sep);
386 0 : if (share == NULL) {
387 0 : goto local_path;
388 : }
389 : /*
390 : * Ensure the server name does not contain
391 : * any possible path components by converting
392 : * them to _'s.
393 : */
394 0 : for (p = server + 1; p < share; p++) {
395 0 : if (*p == '/' || *p == '\\') {
396 0 : *p = '_';
397 : }
398 : }
399 : /*
400 : * It's a well formed DFS path with
401 : * at least server and share components.
402 : * Replace the slashes with '/' and
403 : * pass the remainder to local_path.
404 : */
405 0 : *server = '/';
406 0 : *share = '/';
407 : /*
408 : * Skip past share so we don't pass the
409 : * sharename into check_path_syntax().
410 : */
411 0 : remaining_path = strchr(share+1, path_sep);
412 0 : if (remaining_path == NULL) {
413 : /*
414 : * Ensure the share name does not contain
415 : * any possible path components by converting
416 : * them to _'s.
417 : */
418 0 : for (p = share + 1; *p; p++) {
419 0 : if (*p == '/' || *p == '\\') {
420 0 : *p = '_';
421 : }
422 : }
423 : /*
424 : * If no remaining path this was
425 : * a bare /server/share path. Just return.
426 : */
427 0 : *err = NT_STATUS_OK;
428 0 : return ret;
429 : }
430 : /*
431 : * Ensure the share name does not contain
432 : * any possible path components by converting
433 : * them to _'s.
434 : */
435 0 : for (p = share + 1; p < remaining_path; p++) {
436 0 : if (*p == '/' || *p == '\\') {
437 0 : *p = '_';
438 : }
439 : }
440 0 : *remaining_path = '/';
441 0 : dst = remaining_path + 1;
442 : /* dst now points at any following components. */
443 : }
444 :
445 4 : local_path:
446 :
447 4 : if (posix_pathnames) {
448 0 : *err = check_path_syntax_posix(dst);
449 : } else {
450 4 : *err = check_path_syntax(dst);
451 : }
452 :
453 4 : return ret;
454 : }
455 :
456 : /****************************************************************************
457 : Pull a string and check the path - provide for error return.
458 : ****************************************************************************/
459 :
460 4 : size_t srvstr_get_path(TALLOC_CTX *ctx,
461 : const char *base_ptr,
462 : uint16_t smb_flags2,
463 : char **pp_dest,
464 : const char *src,
465 : size_t src_len,
466 : int flags,
467 : NTSTATUS *err)
468 : {
469 4 : return srvstr_get_path_internal(ctx,
470 : base_ptr,
471 : smb_flags2,
472 : pp_dest,
473 : src,
474 : src_len,
475 : flags,
476 : false,
477 : err);
478 : }
479 :
480 : /****************************************************************************
481 : Pull a string and check the path - provide for error return.
482 : posix_pathnames version.
483 : ****************************************************************************/
484 :
485 0 : size_t srvstr_get_path_posix(TALLOC_CTX *ctx,
486 : const char *base_ptr,
487 : uint16_t smb_flags2,
488 : char **pp_dest,
489 : const char *src,
490 : size_t src_len,
491 : int flags,
492 : NTSTATUS *err)
493 : {
494 0 : return srvstr_get_path_internal(ctx,
495 : base_ptr,
496 : smb_flags2,
497 : pp_dest,
498 : src,
499 : src_len,
500 : flags,
501 : true,
502 : err);
503 : }
504 :
505 :
506 0 : size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
507 : char **pp_dest, const char *src, int flags,
508 : NTSTATUS *err)
509 : {
510 0 : ssize_t bufrem = smbreq_bufrem(req, src);
511 :
512 0 : if (bufrem == 0) {
513 0 : *err = NT_STATUS_INVALID_PARAMETER;
514 0 : return 0;
515 : }
516 :
517 0 : if (req->posix_pathnames) {
518 0 : return srvstr_get_path_internal(mem_ctx,
519 0 : (const char *)req->inbuf,
520 0 : req->flags2,
521 : pp_dest,
522 : src,
523 : bufrem,
524 : flags,
525 : true,
526 : err);
527 : } else {
528 0 : return srvstr_get_path_internal(mem_ctx,
529 0 : (const char *)req->inbuf,
530 0 : req->flags2,
531 : pp_dest,
532 : src,
533 : bufrem,
534 : flags,
535 : false,
536 : err);
537 : }
538 : }
539 :
540 : /**
541 : * pull a string from the smb_buf part of a packet. In this case the
542 : * string can either be null terminated or it can be terminated by the
543 : * end of the smbbuf area
544 : */
545 170 : size_t srvstr_pull_req_talloc(TALLOC_CTX *ctx, struct smb_request *req,
546 : char **dest, const uint8_t *src, int flags)
547 : {
548 170 : ssize_t bufrem = smbreq_bufrem(req, src);
549 :
550 170 : if (bufrem == 0) {
551 28 : *dest = NULL;
552 28 : return 0;
553 : }
554 :
555 142 : return pull_string_talloc(ctx, req->inbuf, req->flags2, dest, src,
556 : bufrem, flags);
557 : }
558 :
559 : /****************************************************************************
560 : Check if we have a correct fsp pointing to a quota fake file. Replacement for
561 : the CHECK_NTQUOTA_HANDLE_OK macro.
562 : ****************************************************************************/
563 :
564 0 : bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
565 : files_struct *fsp)
566 : {
567 0 : if ((fsp == NULL) || (conn == NULL)) {
568 0 : return false;
569 : }
570 :
571 0 : if ((conn != fsp->conn) || (req->vuid != fsp->vuid)) {
572 0 : return false;
573 : }
574 :
575 0 : if (fsp->fsp_flags.is_directory) {
576 0 : return false;
577 : }
578 :
579 0 : if (fsp->fake_file_handle == NULL) {
580 0 : return false;
581 : }
582 :
583 0 : if (fsp->fake_file_handle->type != FAKE_FILE_TYPE_QUOTA) {
584 0 : return false;
585 : }
586 :
587 0 : if (fsp->fake_file_handle->private_data == NULL) {
588 0 : return false;
589 : }
590 :
591 0 : return true;
592 : }
593 :
594 : /****************************************************************************
595 : Return the port number we've bound to on a socket.
596 : ****************************************************************************/
597 :
598 364 : static int get_socket_port(int fd)
599 : {
600 364 : struct samba_sockaddr saddr = {
601 : .sa_socklen = sizeof(struct sockaddr_storage),
602 : };
603 :
604 364 : if (fd == -1) {
605 0 : return -1;
606 : }
607 :
608 364 : if (getsockname(fd, &saddr.u.sa, &saddr.sa_socklen) < 0) {
609 0 : int level = (errno == ENOTCONN) ? 2 : 0;
610 0 : DEBUG(level, ("getsockname failed. Error was %s\n",
611 : strerror(errno)));
612 0 : return -1;
613 : }
614 :
615 : #if defined(HAVE_IPV6)
616 364 : if (saddr.u.sa.sa_family == AF_INET6) {
617 1 : return ntohs(saddr.u.in6.sin6_port);
618 : }
619 : #endif
620 363 : if (saddr.u.sa.sa_family == AF_INET) {
621 363 : return ntohs(saddr.u.in.sin_port);
622 : }
623 0 : return -1;
624 : }
625 :
626 364 : static bool netbios_session_retarget(struct smbXsrv_connection *xconn,
627 : const char *name, int name_type)
628 : {
629 : char *trim_name;
630 : char *trim_name_type;
631 : const char *retarget_parm;
632 : char *retarget;
633 : char *p;
634 364 : int retarget_type = 0x20;
635 364 : int retarget_port = NBT_SMB_PORT;
636 : struct sockaddr_storage retarget_addr;
637 : struct sockaddr_in *in_addr;
638 364 : bool ret = false;
639 : uint8_t outbuf[10];
640 :
641 364 : if (get_socket_port(xconn->transport.sock) != NBT_SMB_PORT) {
642 0 : return false;
643 : }
644 :
645 364 : trim_name = talloc_strdup(talloc_tos(), name);
646 364 : if (trim_name == NULL) {
647 0 : goto fail;
648 : }
649 364 : trim_char(trim_name, ' ', ' ');
650 :
651 364 : trim_name_type = talloc_asprintf(trim_name, "%s#%2.2x", trim_name,
652 : name_type);
653 364 : if (trim_name_type == NULL) {
654 0 : goto fail;
655 : }
656 :
657 364 : retarget_parm = lp_parm_const_string(-1, "netbios retarget",
658 : trim_name_type, NULL);
659 364 : if (retarget_parm == NULL) {
660 364 : retarget_parm = lp_parm_const_string(-1, "netbios retarget",
661 : trim_name, NULL);
662 : }
663 364 : if (retarget_parm == NULL) {
664 364 : goto fail;
665 : }
666 :
667 0 : retarget = talloc_strdup(trim_name, retarget_parm);
668 0 : if (retarget == NULL) {
669 0 : goto fail;
670 : }
671 :
672 0 : DEBUG(10, ("retargeting %s to %s\n", trim_name_type, retarget));
673 :
674 0 : p = strchr(retarget, ':');
675 0 : if (p != NULL) {
676 0 : *p++ = '\0';
677 0 : retarget_port = atoi(p);
678 : }
679 :
680 0 : p = strchr_m(retarget, '#');
681 0 : if (p != NULL) {
682 0 : *p++ = '\0';
683 0 : if (sscanf(p, "%x", &retarget_type) != 1) {
684 0 : goto fail;
685 : }
686 : }
687 :
688 0 : ret = resolve_name(retarget, &retarget_addr, retarget_type, false);
689 0 : if (!ret) {
690 0 : DEBUG(10, ("could not resolve %s\n", retarget));
691 0 : goto fail;
692 : }
693 :
694 0 : if (retarget_addr.ss_family != AF_INET) {
695 0 : DEBUG(10, ("Retarget target not an IPv4 addr\n"));
696 0 : goto fail;
697 : }
698 :
699 0 : in_addr = (struct sockaddr_in *)(void *)&retarget_addr;
700 :
701 0 : _smb_setlen(outbuf, 6);
702 0 : SCVAL(outbuf, 0, 0x84);
703 0 : *(uint32_t *)(outbuf+4) = in_addr->sin_addr.s_addr;
704 0 : *(uint16_t *)(outbuf+8) = htons(retarget_port);
705 :
706 0 : if (!smb1_srv_send(xconn, (char *)outbuf, false, 0, false,
707 : NULL)) {
708 0 : exit_server_cleanly("netbios_session_retarget: smb1_srv_send "
709 : "failed.");
710 : }
711 :
712 0 : ret = true;
713 364 : fail:
714 364 : TALLOC_FREE(trim_name);
715 364 : return ret;
716 : }
717 :
718 0 : static void reply_called_name_not_present(char *outbuf)
719 : {
720 0 : smb_setlen(outbuf, 1);
721 0 : SCVAL(outbuf, 0, 0x83);
722 0 : SCVAL(outbuf, 4, 0x82);
723 0 : }
724 :
725 : /****************************************************************************
726 : Reply to a (netbios-level) special message.
727 : ****************************************************************************/
728 :
729 364 : void reply_special(struct smbXsrv_connection *xconn, char *inbuf, size_t inbuf_size)
730 : {
731 364 : struct smbd_server_connection *sconn = xconn->client->sconn;
732 364 : int msg_type = CVAL(inbuf,0);
733 364 : int msg_flags = CVAL(inbuf,1);
734 : /*
735 : * We only really use 4 bytes of the outbuf, but for the smb_setlen
736 : * calculation & friends (smb1_srv_send uses that) we need the full smb
737 : * header.
738 : */
739 : char outbuf[smb_size];
740 :
741 364 : memset(outbuf, '\0', sizeof(outbuf));
742 :
743 364 : smb_setlen(outbuf,0);
744 :
745 364 : switch (msg_type) {
746 364 : case NBSSrequest: /* session request */
747 : {
748 : /* inbuf_size is guarenteed to be at least 4. */
749 : fstring name1,name2;
750 : int name_type1, name_type2;
751 : int name_len1, name_len2;
752 :
753 364 : *name1 = *name2 = 0;
754 :
755 364 : if (xconn->transport.nbt.got_session) {
756 0 : exit_server_cleanly("multiple session request not permitted");
757 : }
758 :
759 364 : SCVAL(outbuf,0,NBSSpositive);
760 364 : SCVAL(outbuf,3,0);
761 :
762 : /* inbuf_size is guaranteed to be at least 4. */
763 364 : name_len1 = name_len((unsigned char *)(inbuf+4),inbuf_size - 4);
764 364 : if (name_len1 <= 0 || name_len1 > inbuf_size - 4) {
765 0 : DEBUG(0,("Invalid name length in session request\n"));
766 0 : reply_called_name_not_present(outbuf);
767 0 : break;
768 : }
769 364 : name_len2 = name_len((unsigned char *)(inbuf+4+name_len1),inbuf_size - 4 - name_len1);
770 364 : if (name_len2 <= 0 || name_len2 > inbuf_size - 4 - name_len1) {
771 0 : DEBUG(0,("Invalid name length in session request\n"));
772 0 : reply_called_name_not_present(outbuf);
773 0 : break;
774 : }
775 :
776 364 : name_type1 = name_extract((unsigned char *)inbuf,
777 : inbuf_size,(unsigned int)4,name1);
778 364 : name_type2 = name_extract((unsigned char *)inbuf,
779 364 : inbuf_size,(unsigned int)(4 + name_len1),name2);
780 :
781 364 : if (name_type1 == -1 || name_type2 == -1) {
782 0 : DEBUG(0,("Invalid name type in session request\n"));
783 0 : reply_called_name_not_present(outbuf);
784 0 : break;
785 : }
786 :
787 364 : DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
788 : name1, name_type1, name2, name_type2));
789 :
790 364 : if (netbios_session_retarget(xconn, name1, name_type1)) {
791 0 : exit_server_cleanly("retargeted client");
792 : }
793 :
794 : /*
795 : * Windows NT/2k uses "*SMBSERVER" and XP uses
796 : * "*SMBSERV" arrggg!!!
797 : */
798 364 : if (strequal(name1, "*SMBSERVER ")
799 364 : || strequal(name1, "*SMBSERV ")) {
800 : char *raddr;
801 :
802 0 : raddr = tsocket_address_inet_addr_string(sconn->remote_address,
803 : talloc_tos());
804 0 : if (raddr == NULL) {
805 0 : exit_server_cleanly("could not allocate raddr");
806 : }
807 :
808 0 : fstrcpy(name1, raddr);
809 : }
810 :
811 364 : set_local_machine_name(name1, True);
812 364 : set_remote_machine_name(name2, True);
813 :
814 364 : if (is_ipaddress(sconn->remote_hostname)) {
815 364 : char *p = discard_const_p(char, sconn->remote_hostname);
816 :
817 364 : talloc_free(p);
818 :
819 364 : sconn->remote_hostname = talloc_strdup(sconn,
820 : get_remote_machine_name());
821 364 : if (sconn->remote_hostname == NULL) {
822 0 : exit_server_cleanly("could not copy remote name");
823 : }
824 364 : xconn->remote_hostname = sconn->remote_hostname;
825 : }
826 :
827 364 : DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
828 : get_local_machine_name(), get_remote_machine_name(),
829 : name_type2));
830 :
831 364 : if (name_type2 == 'R') {
832 : /* We are being asked for a pathworks session ---
833 : no thanks! */
834 0 : reply_called_name_not_present(outbuf);
835 0 : break;
836 : }
837 :
838 364 : reload_services(sconn, conn_snum_used, true);
839 364 : reopen_logs();
840 :
841 364 : xconn->transport.nbt.got_session = true;
842 364 : break;
843 : }
844 :
845 0 : case 0x89: /* session keepalive request
846 : (some old clients produce this?) */
847 0 : SCVAL(outbuf,0,NBSSkeepalive);
848 0 : SCVAL(outbuf,3,0);
849 0 : break;
850 :
851 0 : case NBSSpositive: /* positive session response */
852 : case NBSSnegative: /* negative session response */
853 : case NBSSretarget: /* retarget session response */
854 0 : DEBUG(0,("Unexpected session response\n"));
855 0 : break;
856 :
857 0 : case NBSSkeepalive: /* session keepalive */
858 : default:
859 0 : return;
860 : }
861 :
862 364 : DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
863 : msg_type, msg_flags));
864 :
865 364 : if (!smb1_srv_send(xconn, outbuf, false, 0, false, NULL)) {
866 1 : exit_server_cleanly("reply_special: smb1_srv_send failed.");
867 : }
868 :
869 363 : if (CVAL(outbuf, 0) != 0x82) {
870 0 : exit_server_cleanly("invalid netbios session");
871 : }
872 363 : return;
873 : }
874 :
875 : /*******************************************************************
876 : * unlink a file with all relevant access checks
877 : *******************************************************************/
878 :
879 0 : NTSTATUS unlink_internals(connection_struct *conn,
880 : struct smb_request *req,
881 : uint32_t dirtype,
882 : struct files_struct *dirfsp,
883 : struct smb_filename *smb_fname)
884 : {
885 : uint32_t fattr;
886 : files_struct *fsp;
887 0 : uint32_t dirtype_orig = dirtype;
888 : NTSTATUS status;
889 : int ret;
890 0 : struct smb2_create_blobs *posx = NULL;
891 :
892 0 : if (dirtype == 0) {
893 0 : dirtype = FILE_ATTRIBUTE_NORMAL;
894 : }
895 :
896 0 : DBG_DEBUG("%s, dirtype = %d\n",
897 : smb_fname_str_dbg(smb_fname),
898 : dirtype);
899 :
900 0 : if (!CAN_WRITE(conn)) {
901 0 : return NT_STATUS_MEDIA_WRITE_PROTECTED;
902 : }
903 :
904 0 : ret = vfs_stat(conn, smb_fname);
905 0 : if (ret != 0) {
906 0 : return map_nt_error_from_unix(errno);
907 : }
908 :
909 0 : fattr = fdos_mode(smb_fname->fsp);
910 :
911 0 : if (dirtype & FILE_ATTRIBUTE_NORMAL) {
912 0 : dirtype = FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY;
913 : }
914 :
915 0 : dirtype &= (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
916 0 : if (!dirtype) {
917 0 : return NT_STATUS_NO_SUCH_FILE;
918 : }
919 :
920 0 : if (!dir_check_ftype(fattr, dirtype)) {
921 0 : if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
922 0 : return NT_STATUS_FILE_IS_A_DIRECTORY;
923 : }
924 0 : return NT_STATUS_NO_SUCH_FILE;
925 : }
926 :
927 0 : if (dirtype_orig & 0x8000) {
928 : /* These will never be set for POSIX. */
929 0 : return NT_STATUS_NO_SUCH_FILE;
930 : }
931 :
932 : #if 0
933 : if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
934 : return NT_STATUS_FILE_IS_A_DIRECTORY;
935 : }
936 :
937 : if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
938 : return NT_STATUS_NO_SUCH_FILE;
939 : }
940 :
941 : if (dirtype & 0xFF00) {
942 : /* These will never be set for POSIX. */
943 : return NT_STATUS_NO_SUCH_FILE;
944 : }
945 :
946 : dirtype &= 0xFF;
947 : if (!dirtype) {
948 : return NT_STATUS_NO_SUCH_FILE;
949 : }
950 :
951 : /* Can't delete a directory. */
952 : if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
953 : return NT_STATUS_FILE_IS_A_DIRECTORY;
954 : }
955 : #endif
956 :
957 : #if 0 /* JRATEST */
958 : else if (dirtype & FILE_ATTRIBUTE_DIRECTORY) /* Asked for a directory and it isn't. */
959 : return NT_STATUS_OBJECT_NAME_INVALID;
960 : #endif /* JRATEST */
961 :
962 0 : if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
963 0 : status = make_smb2_posix_create_ctx(
964 : talloc_tos(), &posx, 0777);
965 0 : if (!NT_STATUS_IS_OK(status)) {
966 0 : DBG_WARNING("make_smb2_posix_create_ctx failed: %s\n",
967 : nt_errstr(status));
968 0 : return status;
969 : }
970 : }
971 :
972 : /* On open checks the open itself will check the share mode, so
973 : don't do it here as we'll get it wrong. */
974 :
975 0 : status = SMB_VFS_CREATE_FILE
976 : (conn, /* conn */
977 : req, /* req */
978 : dirfsp, /* dirfsp */
979 : smb_fname, /* fname */
980 : DELETE_ACCESS, /* access_mask */
981 : FILE_SHARE_NONE, /* share_access */
982 : FILE_OPEN, /* create_disposition*/
983 : FILE_NON_DIRECTORY_FILE, /* create_options */
984 : FILE_ATTRIBUTE_NORMAL, /* file_attributes */
985 : 0, /* oplock_request */
986 : NULL, /* lease */
987 : 0, /* allocation_size */
988 : 0, /* private_flags */
989 : NULL, /* sd */
990 : NULL, /* ea_list */
991 : &fsp, /* result */
992 : NULL, /* pinfo */
993 : posx, /* in_context_blobs */
994 : NULL); /* out_context_blobs */
995 :
996 0 : TALLOC_FREE(posx);
997 :
998 0 : if (!NT_STATUS_IS_OK(status)) {
999 0 : DBG_DEBUG("SMB_VFS_CREATEFILE failed: %s\n",
1000 : nt_errstr(status));
1001 0 : return status;
1002 : }
1003 :
1004 0 : status = can_set_delete_on_close(fsp, fattr);
1005 0 : if (!NT_STATUS_IS_OK(status)) {
1006 0 : DBG_DEBUG("can_set_delete_on_close for file %s - "
1007 : "(%s)\n",
1008 : smb_fname_str_dbg(smb_fname),
1009 : nt_errstr(status));
1010 0 : close_file_free(req, &fsp, NORMAL_CLOSE);
1011 0 : return status;
1012 : }
1013 :
1014 : /* The set is across all open files on this dev/inode pair. */
1015 0 : if (!set_delete_on_close(fsp, True,
1016 0 : conn->session_info->security_token,
1017 0 : conn->session_info->unix_token)) {
1018 0 : close_file_free(req, &fsp, NORMAL_CLOSE);
1019 0 : return NT_STATUS_ACCESS_DENIED;
1020 : }
1021 :
1022 0 : return close_file_free(req, &fsp, NORMAL_CLOSE);
1023 : }
1024 :
1025 : /****************************************************************************
1026 : Fake (read/write) sendfile. Returns -1 on read or write fail.
1027 : ****************************************************************************/
1028 :
1029 0 : ssize_t fake_sendfile(struct smbXsrv_connection *xconn, files_struct *fsp,
1030 : off_t startpos, size_t nread)
1031 : {
1032 : size_t bufsize;
1033 0 : size_t tosend = nread;
1034 : char *buf;
1035 :
1036 0 : if (nread == 0) {
1037 0 : return 0;
1038 : }
1039 :
1040 0 : bufsize = MIN(nread, 65536);
1041 :
1042 0 : if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
1043 0 : return -1;
1044 : }
1045 :
1046 0 : while (tosend > 0) {
1047 : ssize_t ret;
1048 : size_t cur_read;
1049 :
1050 0 : cur_read = MIN(tosend, bufsize);
1051 0 : ret = read_file(fsp,buf,startpos,cur_read);
1052 0 : if (ret == -1) {
1053 0 : SAFE_FREE(buf);
1054 0 : return -1;
1055 : }
1056 :
1057 : /* If we had a short read, fill with zeros. */
1058 0 : if (ret < cur_read) {
1059 0 : memset(buf + ret, '\0', cur_read - ret);
1060 : }
1061 :
1062 0 : ret = write_data(xconn->transport.sock, buf, cur_read);
1063 0 : if (ret != cur_read) {
1064 0 : int saved_errno = errno;
1065 : /*
1066 : * Try and give an error message saying what
1067 : * client failed.
1068 : */
1069 0 : DEBUG(0, ("write_data failed for client %s. "
1070 : "Error %s\n",
1071 : smbXsrv_connection_dbg(xconn),
1072 : strerror(saved_errno)));
1073 0 : SAFE_FREE(buf);
1074 0 : errno = saved_errno;
1075 0 : return -1;
1076 : }
1077 0 : tosend -= cur_read;
1078 0 : startpos += cur_read;
1079 : }
1080 :
1081 0 : SAFE_FREE(buf);
1082 0 : return (ssize_t)nread;
1083 : }
1084 :
1085 : /****************************************************************************
1086 : Deal with the case of sendfile reading less bytes from the file than
1087 : requested. Fill with zeros (all we can do). Returns 0 on success
1088 : ****************************************************************************/
1089 :
1090 0 : ssize_t sendfile_short_send(struct smbXsrv_connection *xconn,
1091 : files_struct *fsp,
1092 : ssize_t nread,
1093 : size_t headersize,
1094 : size_t smb_maxcnt)
1095 : {
1096 : #define SHORT_SEND_BUFSIZE 1024
1097 0 : if (nread < headersize) {
1098 0 : DEBUG(0,("sendfile_short_send: sendfile failed to send "
1099 : "header for file %s (%s). Terminating\n",
1100 : fsp_str_dbg(fsp), strerror(errno)));
1101 0 : return -1;
1102 : }
1103 :
1104 0 : nread -= headersize;
1105 :
1106 0 : if (nread < smb_maxcnt) {
1107 0 : char buf[SHORT_SEND_BUFSIZE] = { 0 };
1108 :
1109 0 : DEBUG(0,("sendfile_short_send: filling truncated file %s "
1110 : "with zeros !\n", fsp_str_dbg(fsp)));
1111 :
1112 0 : while (nread < smb_maxcnt) {
1113 : /*
1114 : * We asked for the real file size and told sendfile
1115 : * to not go beyond the end of the file. But it can
1116 : * happen that in between our fstat call and the
1117 : * sendfile call the file was truncated. This is very
1118 : * bad because we have already announced the larger
1119 : * number of bytes to the client.
1120 : *
1121 : * The best we can do now is to send 0-bytes, just as
1122 : * a read from a hole in a sparse file would do.
1123 : *
1124 : * This should happen rarely enough that I don't care
1125 : * about efficiency here :-)
1126 : */
1127 : size_t to_write;
1128 : ssize_t ret;
1129 :
1130 0 : to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
1131 0 : ret = write_data(xconn->transport.sock, buf, to_write);
1132 0 : if (ret != to_write) {
1133 0 : int saved_errno = errno;
1134 : /*
1135 : * Try and give an error message saying what
1136 : * client failed.
1137 : */
1138 0 : DEBUG(0, ("write_data failed for client %s. "
1139 : "Error %s\n",
1140 : smbXsrv_connection_dbg(xconn),
1141 : strerror(saved_errno)));
1142 0 : errno = saved_errno;
1143 0 : return -1;
1144 : }
1145 0 : nread += to_write;
1146 : }
1147 : }
1148 :
1149 0 : return 0;
1150 : }
1151 :
1152 : /*******************************************************************
1153 : Check if a user is allowed to rename a file.
1154 : ********************************************************************/
1155 :
1156 20 : static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
1157 : uint16_t dirtype)
1158 : {
1159 : NTSTATUS status;
1160 :
1161 20 : if (fsp->fsp_name->twrp != 0) {
1162 : /* Get the error right, this is what Windows returns. */
1163 0 : return NT_STATUS_NOT_SAME_DEVICE;
1164 : }
1165 :
1166 20 : if (!CAN_WRITE(conn)) {
1167 0 : return NT_STATUS_MEDIA_WRITE_PROTECTED;
1168 : }
1169 :
1170 20 : if ((dirtype & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) !=
1171 : (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1172 : /* Only bother to read the DOS attribute if we might deny the
1173 : rename on the grounds of attribute mismatch. */
1174 0 : uint32_t fmode = fdos_mode(fsp);
1175 0 : if ((fmode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1176 0 : return NT_STATUS_NO_SUCH_FILE;
1177 : }
1178 : }
1179 :
1180 20 : if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
1181 4 : if (fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) {
1182 0 : return NT_STATUS_OK;
1183 : }
1184 :
1185 : /* If no pathnames are open below this
1186 : directory, allow the rename. */
1187 :
1188 4 : if (lp_strict_rename(SNUM(conn))) {
1189 : /*
1190 : * Strict rename, check open file db.
1191 : */
1192 4 : if (have_file_open_below(fsp->conn, fsp->fsp_name)) {
1193 0 : return NT_STATUS_ACCESS_DENIED;
1194 : }
1195 0 : } else if (file_find_subpath(fsp)) {
1196 : /*
1197 : * No strict rename, just look in local process.
1198 : */
1199 0 : return NT_STATUS_ACCESS_DENIED;
1200 : }
1201 4 : return NT_STATUS_OK;
1202 : }
1203 :
1204 16 : status = check_any_access_fsp(fsp, DELETE_ACCESS | FILE_WRITE_ATTRIBUTES);
1205 16 : if (!NT_STATUS_IS_OK(status)) {
1206 0 : return status;
1207 : }
1208 16 : return NT_STATUS_OK;
1209 : }
1210 :
1211 : /****************************************************************************
1212 : Ensure open files have their names updated. Updated to notify other smbd's
1213 : asynchronously.
1214 : ****************************************************************************/
1215 :
1216 20 : static void rename_open_files(connection_struct *conn,
1217 : struct share_mode_lock *lck,
1218 : struct file_id id,
1219 : uint32_t orig_name_hash,
1220 : const struct smb_filename *smb_fname_dst)
1221 : {
1222 : files_struct *fsp;
1223 20 : bool did_rename = False;
1224 : NTSTATUS status;
1225 20 : uint32_t new_name_hash = 0;
1226 :
1227 40 : for(fsp = file_find_di_first(conn->sconn, id, false); fsp;
1228 20 : fsp = file_find_di_next(fsp, false)) {
1229 : SMB_STRUCT_STAT fsp_orig_sbuf;
1230 : struct file_id_buf idbuf;
1231 : /* fsp_name is a relative path under the fsp. To change this for other
1232 : sharepaths we need to manipulate relative paths. */
1233 : /* TODO - create the absolute path and manipulate the newname
1234 : relative to the sharepath. */
1235 20 : if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
1236 0 : continue;
1237 : }
1238 20 : if (fsp->name_hash != orig_name_hash) {
1239 0 : continue;
1240 : }
1241 20 : DBG_DEBUG("renaming file %s "
1242 : "(file_id %s) from %s -> %s\n",
1243 : fsp_fnum_dbg(fsp),
1244 : file_id_str_buf(fsp->file_id, &idbuf),
1245 : fsp_str_dbg(fsp),
1246 : smb_fname_str_dbg(smb_fname_dst));
1247 :
1248 : /*
1249 : * The incoming smb_fname_dst here has an
1250 : * invalid stat struct (it must not have
1251 : * existed for the rename to succeed).
1252 : * Preserve the existing stat from the
1253 : * open fsp after fsp_set_smb_fname()
1254 : * overwrites with the invalid stat.
1255 : *
1256 : * We will do an fstat before returning
1257 : * any of this metadata to the client anyway.
1258 : */
1259 20 : fsp_orig_sbuf = fsp->fsp_name->st;
1260 20 : status = fsp_set_smb_fname(fsp, smb_fname_dst);
1261 20 : if (NT_STATUS_IS_OK(status)) {
1262 20 : did_rename = True;
1263 20 : new_name_hash = fsp->name_hash;
1264 : /* Restore existing stat. */
1265 20 : fsp->fsp_name->st = fsp_orig_sbuf;
1266 : }
1267 : }
1268 :
1269 20 : if (!did_rename) {
1270 : struct file_id_buf idbuf;
1271 0 : DBG_DEBUG("no open files on file_id %s "
1272 : "for %s\n",
1273 : file_id_str_buf(id, &idbuf),
1274 : smb_fname_str_dbg(smb_fname_dst));
1275 : }
1276 :
1277 : /* Send messages to all smbd's (not ourself) that the name has changed. */
1278 20 : rename_share_filename(conn->sconn->msg_ctx, lck, id, conn->connectpath,
1279 : orig_name_hash, new_name_hash,
1280 : smb_fname_dst);
1281 :
1282 20 : }
1283 :
1284 : /****************************************************************************
1285 : We need to check if the source path is a parent directory of the destination
1286 : (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
1287 : refuse the rename with a sharing violation. Under UNIX the above call can
1288 : *succeed* if /foo/bar/baz is a symlink to another area in the share. We
1289 : probably need to check that the client is a Windows one before disallowing
1290 : this as a UNIX client (one with UNIX extensions) can know the source is a
1291 : symlink and make this decision intelligently. Found by an excellent bug
1292 : report from <AndyLiebman@aol.com>.
1293 : ****************************************************************************/
1294 :
1295 20 : static bool rename_path_prefix_equal(const struct smb_filename *smb_fname_src,
1296 : const struct smb_filename *smb_fname_dst)
1297 : {
1298 20 : const char *psrc = smb_fname_src->base_name;
1299 20 : const char *pdst = smb_fname_dst->base_name;
1300 : size_t slen;
1301 :
1302 20 : if (psrc[0] == '.' && psrc[1] == '/') {
1303 0 : psrc += 2;
1304 : }
1305 20 : if (pdst[0] == '.' && pdst[1] == '/') {
1306 0 : pdst += 2;
1307 : }
1308 20 : if ((slen = strlen(psrc)) > strlen(pdst)) {
1309 4 : return False;
1310 : }
1311 16 : return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
1312 : }
1313 :
1314 : /*
1315 : * Do the notify calls from a rename
1316 : */
1317 :
1318 20 : static void notify_rename(connection_struct *conn, bool is_dir,
1319 : const struct smb_filename *smb_fname_src,
1320 : const struct smb_filename *smb_fname_dst)
1321 : {
1322 20 : char *parent_dir_src = NULL;
1323 20 : char *parent_dir_dst = NULL;
1324 : uint32_t mask;
1325 :
1326 20 : mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
1327 20 : : FILE_NOTIFY_CHANGE_FILE_NAME;
1328 :
1329 20 : if (!parent_dirname(talloc_tos(), smb_fname_src->base_name,
1330 20 : &parent_dir_src, NULL) ||
1331 20 : !parent_dirname(talloc_tos(), smb_fname_dst->base_name,
1332 : &parent_dir_dst, NULL)) {
1333 0 : goto out;
1334 : }
1335 :
1336 20 : if (strcmp(parent_dir_src, parent_dir_dst) == 0) {
1337 16 : notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask,
1338 16 : smb_fname_src->base_name);
1339 16 : notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask,
1340 16 : smb_fname_dst->base_name);
1341 : }
1342 : else {
1343 4 : notify_fname(conn, NOTIFY_ACTION_REMOVED, mask,
1344 4 : smb_fname_src->base_name);
1345 4 : notify_fname(conn, NOTIFY_ACTION_ADDED, mask,
1346 4 : smb_fname_dst->base_name);
1347 : }
1348 :
1349 : /* this is a strange one. w2k3 gives an additional event for
1350 : CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
1351 : files, but not directories */
1352 20 : if (!is_dir) {
1353 16 : notify_fname(conn, NOTIFY_ACTION_MODIFIED,
1354 : FILE_NOTIFY_CHANGE_ATTRIBUTES
1355 : |FILE_NOTIFY_CHANGE_CREATION,
1356 16 : smb_fname_dst->base_name);
1357 : }
1358 4 : out:
1359 20 : TALLOC_FREE(parent_dir_src);
1360 20 : TALLOC_FREE(parent_dir_dst);
1361 20 : }
1362 :
1363 : /****************************************************************************
1364 : Returns an error if the parent directory for a filename is open in an
1365 : incompatible way.
1366 : ****************************************************************************/
1367 :
1368 21 : static NTSTATUS parent_dirname_compatible_open(connection_struct *conn,
1369 : const struct smb_filename *smb_fname_dst_in)
1370 : {
1371 21 : struct smb_filename *smb_fname_parent = NULL;
1372 : struct file_id id;
1373 21 : files_struct *fsp = NULL;
1374 : int ret;
1375 : NTSTATUS status;
1376 :
1377 21 : status = SMB_VFS_PARENT_PATHNAME(conn,
1378 : talloc_tos(),
1379 : smb_fname_dst_in,
1380 : &smb_fname_parent,
1381 : NULL);
1382 21 : if (!NT_STATUS_IS_OK(status)) {
1383 0 : return status;
1384 : }
1385 :
1386 21 : ret = vfs_stat(conn, smb_fname_parent);
1387 21 : if (ret == -1) {
1388 0 : return map_nt_error_from_unix(errno);
1389 : }
1390 :
1391 : /*
1392 : * We're only checking on this smbd here, mostly good
1393 : * enough.. and will pass tests.
1394 : */
1395 :
1396 21 : id = vfs_file_id_from_sbuf(conn, &smb_fname_parent->st);
1397 21 : for (fsp = file_find_di_first(conn->sconn, id, true); fsp;
1398 0 : fsp = file_find_di_next(fsp, true)) {
1399 0 : if (fsp->access_mask & DELETE_ACCESS) {
1400 0 : return NT_STATUS_SHARING_VIOLATION;
1401 : }
1402 : }
1403 21 : return NT_STATUS_OK;
1404 : }
1405 :
1406 : /****************************************************************************
1407 : Rename an open file - given an fsp.
1408 : ****************************************************************************/
1409 :
1410 21 : NTSTATUS rename_internals_fsp(connection_struct *conn,
1411 : files_struct *fsp,
1412 : struct files_struct *dst_dirfsp,
1413 : struct smb_filename *smb_fname_dst_in,
1414 : const char *dst_original_lcomp,
1415 : uint32_t attrs,
1416 : bool replace_if_exists)
1417 : {
1418 21 : TALLOC_CTX *ctx = talloc_tos();
1419 21 : struct smb_filename *parent_dir_fname_dst = NULL;
1420 21 : struct smb_filename *parent_dir_fname_dst_atname = NULL;
1421 21 : struct smb_filename *parent_dir_fname_src = NULL;
1422 21 : struct smb_filename *parent_dir_fname_src_atname = NULL;
1423 21 : struct smb_filename *smb_fname_dst = NULL;
1424 21 : NTSTATUS status = NT_STATUS_OK;
1425 21 : struct share_mode_lock *lck = NULL;
1426 21 : uint32_t access_mask = SEC_DIR_ADD_FILE;
1427 : bool dst_exists, old_is_stream, new_is_stream;
1428 : int ret;
1429 42 : bool case_sensitive = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1430 21 : true : conn->case_sensitive;
1431 42 : bool case_preserve = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1432 21 : true : conn->case_preserve;
1433 :
1434 21 : status = parent_dirname_compatible_open(conn, smb_fname_dst_in);
1435 21 : if (!NT_STATUS_IS_OK(status)) {
1436 0 : return status;
1437 : }
1438 :
1439 21 : if (file_has_open_streams(fsp)) {
1440 0 : return NT_STATUS_ACCESS_DENIED;
1441 : }
1442 :
1443 : /* Make a copy of the dst smb_fname structs */
1444 :
1445 21 : smb_fname_dst = cp_smb_filename(ctx, smb_fname_dst_in);
1446 21 : if (smb_fname_dst == NULL) {
1447 0 : status = NT_STATUS_NO_MEMORY;
1448 0 : goto out;
1449 : }
1450 :
1451 : /*
1452 : * Check for special case with case preserving and not
1453 : * case sensitive. If the new last component differs from the original
1454 : * last component only by case, then we should allow
1455 : * the rename (user is trying to change the case of the
1456 : * filename).
1457 : */
1458 42 : if (!case_sensitive && case_preserve &&
1459 21 : strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
1460 0 : strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
1461 0 : char *fname_dst_parent = NULL;
1462 0 : const char *fname_dst_lcomp = NULL;
1463 0 : char *orig_lcomp_path = NULL;
1464 0 : char *orig_lcomp_stream = NULL;
1465 0 : bool ok = true;
1466 :
1467 : /*
1468 : * Split off the last component of the processed
1469 : * destination name. We will compare this to
1470 : * the split components of dst_original_lcomp.
1471 : */
1472 0 : if (!parent_dirname(ctx,
1473 0 : smb_fname_dst->base_name,
1474 : &fname_dst_parent,
1475 : &fname_dst_lcomp)) {
1476 0 : status = NT_STATUS_NO_MEMORY;
1477 0 : goto out;
1478 : }
1479 :
1480 : /*
1481 : * The dst_original_lcomp component contains
1482 : * the last_component of the path + stream
1483 : * name (if a stream exists).
1484 : *
1485 : * Split off the stream name so we
1486 : * can check them separately.
1487 : */
1488 :
1489 0 : if (fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) {
1490 : /* POSIX - no stream component. */
1491 0 : orig_lcomp_path = talloc_strdup(ctx,
1492 : dst_original_lcomp);
1493 0 : if (orig_lcomp_path == NULL) {
1494 0 : ok = false;
1495 : }
1496 : } else {
1497 0 : ok = split_stream_filename(ctx,
1498 : dst_original_lcomp,
1499 : &orig_lcomp_path,
1500 : &orig_lcomp_stream);
1501 : }
1502 :
1503 0 : if (!ok) {
1504 0 : TALLOC_FREE(fname_dst_parent);
1505 0 : status = NT_STATUS_NO_MEMORY;
1506 0 : goto out;
1507 : }
1508 :
1509 : /* If the base names only differ by case, use original. */
1510 0 : if(!strcsequal(fname_dst_lcomp, orig_lcomp_path)) {
1511 : char *tmp;
1512 : /*
1513 : * Replace the modified last component with the
1514 : * original.
1515 : */
1516 0 : if (!ISDOT(fname_dst_parent)) {
1517 0 : tmp = talloc_asprintf(smb_fname_dst,
1518 : "%s/%s",
1519 : fname_dst_parent,
1520 : orig_lcomp_path);
1521 : } else {
1522 0 : tmp = talloc_strdup(smb_fname_dst,
1523 : orig_lcomp_path);
1524 : }
1525 0 : if (tmp == NULL) {
1526 0 : status = NT_STATUS_NO_MEMORY;
1527 0 : TALLOC_FREE(fname_dst_parent);
1528 0 : TALLOC_FREE(orig_lcomp_path);
1529 0 : TALLOC_FREE(orig_lcomp_stream);
1530 0 : goto out;
1531 : }
1532 0 : TALLOC_FREE(smb_fname_dst->base_name);
1533 0 : smb_fname_dst->base_name = tmp;
1534 : }
1535 :
1536 : /* If the stream_names only differ by case, use original. */
1537 0 : if(!strcsequal(smb_fname_dst->stream_name,
1538 : orig_lcomp_stream)) {
1539 : /* Use the original stream. */
1540 0 : char *tmp = talloc_strdup(smb_fname_dst,
1541 : orig_lcomp_stream);
1542 0 : if (tmp == NULL) {
1543 0 : status = NT_STATUS_NO_MEMORY;
1544 0 : TALLOC_FREE(fname_dst_parent);
1545 0 : TALLOC_FREE(orig_lcomp_path);
1546 0 : TALLOC_FREE(orig_lcomp_stream);
1547 0 : goto out;
1548 : }
1549 0 : TALLOC_FREE(smb_fname_dst->stream_name);
1550 0 : smb_fname_dst->stream_name = tmp;
1551 : }
1552 0 : TALLOC_FREE(fname_dst_parent);
1553 0 : TALLOC_FREE(orig_lcomp_path);
1554 0 : TALLOC_FREE(orig_lcomp_stream);
1555 : }
1556 :
1557 : /*
1558 : * If the src and dest names are identical - including case,
1559 : * don't do the rename, just return success.
1560 : */
1561 :
1562 21 : if (strcsequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
1563 0 : strcsequal(fsp->fsp_name->stream_name,
1564 0 : smb_fname_dst->stream_name)) {
1565 0 : DEBUG(3, ("rename_internals_fsp: identical names in rename %s "
1566 : "- returning success\n",
1567 : smb_fname_str_dbg(smb_fname_dst)));
1568 0 : status = NT_STATUS_OK;
1569 0 : goto out;
1570 : }
1571 :
1572 21 : old_is_stream = is_ntfs_stream_smb_fname(fsp->fsp_name);
1573 21 : new_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
1574 :
1575 : /* Return the correct error code if both names aren't streams. */
1576 21 : if (!old_is_stream && new_is_stream) {
1577 0 : status = NT_STATUS_OBJECT_NAME_INVALID;
1578 0 : goto out;
1579 : }
1580 :
1581 21 : if (old_is_stream && !new_is_stream) {
1582 0 : status = NT_STATUS_INVALID_PARAMETER;
1583 0 : goto out;
1584 : }
1585 :
1586 21 : dst_exists = vfs_stat(conn, smb_fname_dst) == 0;
1587 :
1588 21 : if(!replace_if_exists && dst_exists) {
1589 1 : DEBUG(3, ("rename_internals_fsp: dest exists doing rename "
1590 : "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
1591 : smb_fname_str_dbg(smb_fname_dst)));
1592 1 : status = NT_STATUS_OBJECT_NAME_COLLISION;
1593 1 : goto out;
1594 : }
1595 :
1596 : /*
1597 : * Drop the pathref fsp on the destination otherwise we trip upon in in
1598 : * the below check for open files check.
1599 : */
1600 20 : if (smb_fname_dst_in->fsp != NULL) {
1601 0 : fd_close(smb_fname_dst_in->fsp);
1602 0 : file_free(NULL, smb_fname_dst_in->fsp);
1603 0 : SMB_ASSERT(smb_fname_dst_in->fsp == NULL);
1604 : }
1605 :
1606 20 : if (dst_exists) {
1607 0 : struct file_id fileid = vfs_file_id_from_sbuf(conn,
1608 0 : &smb_fname_dst->st);
1609 0 : files_struct *dst_fsp = file_find_di_first(conn->sconn,
1610 : fileid, true);
1611 : /* The file can be open when renaming a stream */
1612 0 : if (dst_fsp && !new_is_stream) {
1613 0 : DEBUG(3, ("rename_internals_fsp: Target file open\n"));
1614 0 : status = NT_STATUS_ACCESS_DENIED;
1615 0 : goto out;
1616 : }
1617 : }
1618 :
1619 : /* Ensure we have a valid stat struct for the source. */
1620 20 : status = vfs_stat_fsp(fsp);
1621 20 : if (!NT_STATUS_IS_OK(status)) {
1622 0 : goto out;
1623 : }
1624 :
1625 20 : status = can_rename(conn, fsp, attrs);
1626 :
1627 20 : if (!NT_STATUS_IS_OK(status)) {
1628 0 : DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
1629 : nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
1630 : smb_fname_str_dbg(smb_fname_dst)));
1631 0 : if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
1632 0 : status = NT_STATUS_ACCESS_DENIED;
1633 0 : goto out;
1634 : }
1635 :
1636 20 : if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
1637 0 : status = NT_STATUS_ACCESS_DENIED;
1638 0 : goto out;
1639 : }
1640 :
1641 : /* Do we have rights to move into the destination ? */
1642 20 : if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
1643 : /* We're moving a directory. */
1644 4 : access_mask = SEC_DIR_ADD_SUBDIR;
1645 : }
1646 :
1647 : /*
1648 : * Get a pathref on the destination parent directory, so
1649 : * we can call check_parent_access_fsp().
1650 : */
1651 20 : status = parent_pathref(ctx,
1652 : conn->cwd_fsp,
1653 : smb_fname_dst,
1654 : &parent_dir_fname_dst,
1655 : &parent_dir_fname_dst_atname);
1656 20 : if (!NT_STATUS_IS_OK(status)) {
1657 0 : goto out;
1658 : }
1659 :
1660 20 : status = check_parent_access_fsp(parent_dir_fname_dst->fsp,
1661 : access_mask);
1662 20 : if (!NT_STATUS_IS_OK(status)) {
1663 0 : DBG_INFO("check_parent_access_fsp on "
1664 : "dst %s returned %s\n",
1665 : smb_fname_str_dbg(smb_fname_dst),
1666 : nt_errstr(status));
1667 0 : goto out;
1668 : }
1669 :
1670 : /*
1671 : * If the target existed, make sure the destination
1672 : * atname has the same stat struct.
1673 : */
1674 20 : parent_dir_fname_dst_atname->st = smb_fname_dst->st;
1675 :
1676 : /*
1677 : * It's very common that source and
1678 : * destination directories are the same.
1679 : * Optimize by not opening the
1680 : * second parent_pathref if we know
1681 : * this is the case.
1682 : */
1683 :
1684 20 : status = SMB_VFS_PARENT_PATHNAME(conn,
1685 : ctx,
1686 : fsp->fsp_name,
1687 : &parent_dir_fname_src,
1688 : &parent_dir_fname_src_atname);
1689 20 : if (!NT_STATUS_IS_OK(status)) {
1690 0 : goto out;
1691 : }
1692 :
1693 : /*
1694 : * We do a case-sensitive string comparison. We want to be *sure*
1695 : * this is the same path. The worst that can happen if
1696 : * the case doesn't match is we lose out on the optimization,
1697 : * the code still works.
1698 : *
1699 : * We can ignore twrp fields here. Rename is not allowed on
1700 : * shadow copy handles.
1701 : */
1702 :
1703 20 : if (strcmp(parent_dir_fname_src->base_name,
1704 20 : parent_dir_fname_dst->base_name) == 0) {
1705 : /*
1706 : * parent directory is the same for source
1707 : * and destination.
1708 : */
1709 : /* Reparent the src_atname to the parent_dir_dest fname. */
1710 16 : parent_dir_fname_src_atname = talloc_move(
1711 : parent_dir_fname_dst,
1712 : &parent_dir_fname_src_atname);
1713 : /* Free the unneeded duplicate parent name. */
1714 16 : TALLOC_FREE(parent_dir_fname_src);
1715 : /*
1716 : * And make the source parent name a copy of the
1717 : * destination parent name.
1718 : */
1719 16 : parent_dir_fname_src = parent_dir_fname_dst;
1720 :
1721 : /*
1722 : * Ensure we have a pathref fsp on the
1723 : * parent_dir_fname_src_atname to match the code in the else
1724 : * branch where we use parent_pathref().
1725 : */
1726 16 : status = reference_smb_fname_fsp_link(
1727 : parent_dir_fname_src_atname,
1728 16 : fsp->fsp_name);
1729 16 : if (!NT_STATUS_IS_OK(status)) {
1730 0 : goto out;
1731 : }
1732 : } else {
1733 : /*
1734 : * source and destination parent directories are
1735 : * different.
1736 : *
1737 : * Get a pathref on the source parent directory, so
1738 : * we can do a relative rename.
1739 : */
1740 4 : TALLOC_FREE(parent_dir_fname_src);
1741 4 : status = parent_pathref(ctx,
1742 : conn->cwd_fsp,
1743 4 : fsp->fsp_name,
1744 : &parent_dir_fname_src,
1745 : &parent_dir_fname_src_atname);
1746 4 : if (!NT_STATUS_IS_OK(status)) {
1747 0 : goto out;
1748 : }
1749 : }
1750 :
1751 : /*
1752 : * Some modules depend on the source smb_fname having a valid stat.
1753 : * The parent_dir_fname_src_atname is the relative name of the
1754 : * currently open file, so just copy the stat from the open fsp.
1755 : */
1756 20 : parent_dir_fname_src_atname->st = fsp->fsp_name->st;
1757 :
1758 20 : lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1759 :
1760 : /*
1761 : * We have the file open ourselves, so not being able to get the
1762 : * corresponding share mode lock is a fatal error.
1763 : */
1764 :
1765 20 : SMB_ASSERT(lck != NULL);
1766 :
1767 20 : ret = SMB_VFS_RENAMEAT(conn,
1768 : parent_dir_fname_src->fsp,
1769 : parent_dir_fname_src_atname,
1770 : parent_dir_fname_dst->fsp,
1771 : parent_dir_fname_dst_atname);
1772 20 : if (ret == 0) {
1773 20 : uint32_t create_options = fh_get_private_options(fsp->fh);
1774 :
1775 20 : DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
1776 : "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
1777 : smb_fname_str_dbg(smb_fname_dst)));
1778 :
1779 20 : notify_rename(conn,
1780 20 : fsp->fsp_flags.is_directory,
1781 20 : fsp->fsp_name,
1782 : smb_fname_dst);
1783 :
1784 20 : rename_open_files(conn, lck, fsp->file_id, fsp->name_hash,
1785 : smb_fname_dst);
1786 :
1787 20 : if (!fsp->fsp_flags.is_directory &&
1788 32 : !(fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) &&
1789 16 : (lp_map_archive(SNUM(conn)) ||
1790 0 : lp_store_dos_attributes(SNUM(conn))))
1791 : {
1792 : /*
1793 : * We must set the archive bit on the newly renamed
1794 : * file.
1795 : */
1796 16 : status = vfs_stat_fsp(fsp);
1797 16 : if (NT_STATUS_IS_OK(status)) {
1798 : uint32_t old_dosmode;
1799 16 : old_dosmode = fdos_mode(fsp);
1800 : /*
1801 : * We can use fsp->fsp_name here as it has
1802 : * already been changed to the new name.
1803 : */
1804 16 : SMB_ASSERT(fsp->fsp_name->fsp == fsp);
1805 16 : file_set_dosmode(conn,
1806 : fsp->fsp_name,
1807 : old_dosmode | FILE_ATTRIBUTE_ARCHIVE,
1808 : NULL,
1809 : true);
1810 : }
1811 : }
1812 :
1813 : /*
1814 : * A rename acts as a new file create w.r.t. allowing an initial delete
1815 : * on close, probably because in Windows there is a new handle to the
1816 : * new file. If initial delete on close was requested but not
1817 : * originally set, we need to set it here. This is probably not 100% correct,
1818 : * but will work for the CIFSFS client which in non-posix mode
1819 : * depends on these semantics. JRA.
1820 : */
1821 :
1822 20 : if (create_options & FILE_DELETE_ON_CLOSE) {
1823 0 : status = can_set_delete_on_close(fsp, 0);
1824 :
1825 0 : if (NT_STATUS_IS_OK(status)) {
1826 : /* Note that here we set the *initial* delete on close flag,
1827 : * not the regular one. The magic gets handled in close. */
1828 0 : fsp->fsp_flags.initial_delete_on_close = true;
1829 : }
1830 : }
1831 20 : TALLOC_FREE(lck);
1832 20 : status = NT_STATUS_OK;
1833 20 : goto out;
1834 : }
1835 :
1836 0 : TALLOC_FREE(lck);
1837 :
1838 0 : if (errno == ENOTDIR || errno == EISDIR) {
1839 0 : status = NT_STATUS_OBJECT_NAME_COLLISION;
1840 : } else {
1841 0 : status = map_nt_error_from_unix(errno);
1842 : }
1843 :
1844 0 : DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
1845 : nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
1846 : smb_fname_str_dbg(smb_fname_dst)));
1847 :
1848 21 : out:
1849 :
1850 : /*
1851 : * parent_dir_fname_src may be a copy of parent_dir_fname_dst.
1852 : * See the optimization for same source and destination directory
1853 : * above. Only free one in that case.
1854 : */
1855 21 : if (parent_dir_fname_src != parent_dir_fname_dst) {
1856 4 : TALLOC_FREE(parent_dir_fname_src);
1857 : }
1858 21 : TALLOC_FREE(parent_dir_fname_dst);
1859 21 : TALLOC_FREE(smb_fname_dst);
1860 :
1861 21 : return status;
1862 : }
1863 :
1864 : /****************************************************************************
1865 : The guts of the rename command, split out so it may be called by the NT SMB
1866 : code.
1867 : ****************************************************************************/
1868 :
1869 0 : NTSTATUS rename_internals(TALLOC_CTX *ctx,
1870 : connection_struct *conn,
1871 : struct smb_request *req,
1872 : struct files_struct *src_dirfsp,
1873 : struct smb_filename *smb_fname_src,
1874 : struct files_struct *dst_dirfsp,
1875 : struct smb_filename *smb_fname_dst,
1876 : const char *dst_original_lcomp,
1877 : uint32_t attrs,
1878 : bool replace_if_exists,
1879 : uint32_t access_mask)
1880 : {
1881 0 : NTSTATUS status = NT_STATUS_OK;
1882 0 : int create_options = 0;
1883 0 : struct smb2_create_blobs *posx = NULL;
1884 0 : struct files_struct *fsp = NULL;
1885 0 : bool posix_pathname = (smb_fname_src->flags & SMB_FILENAME_POSIX_PATH);
1886 0 : bool case_sensitive = posix_pathname ? true : conn->case_sensitive;
1887 0 : bool case_preserve = posix_pathname ? true : conn->case_preserve;
1888 0 : bool short_case_preserve = posix_pathname ? true :
1889 0 : conn->short_case_preserve;
1890 :
1891 0 : if (posix_pathname) {
1892 0 : status = make_smb2_posix_create_ctx(talloc_tos(), &posx, 0777);
1893 0 : if (!NT_STATUS_IS_OK(status)) {
1894 0 : DBG_WARNING("make_smb2_posix_create_ctx failed: %s\n",
1895 : nt_errstr(status));
1896 0 : goto out;
1897 : }
1898 : }
1899 :
1900 0 : DBG_NOTICE("case_sensitive = %d, "
1901 : "case_preserve = %d, short case preserve = %d, "
1902 : "directory = %s, newname = %s, "
1903 : "last_component_dest = %s\n",
1904 : case_sensitive, case_preserve,
1905 : short_case_preserve,
1906 : smb_fname_str_dbg(smb_fname_src),
1907 : smb_fname_str_dbg(smb_fname_dst),
1908 : dst_original_lcomp);
1909 :
1910 0 : ZERO_STRUCT(smb_fname_src->st);
1911 :
1912 0 : status = openat_pathref_fsp(conn->cwd_fsp, smb_fname_src);
1913 0 : if (!NT_STATUS_IS_OK(status)) {
1914 0 : if (!NT_STATUS_EQUAL(status,
1915 : NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1916 0 : goto out;
1917 : }
1918 : /*
1919 : * Possible symlink src.
1920 : */
1921 0 : if (!(smb_fname_src->flags & SMB_FILENAME_POSIX_PATH)) {
1922 0 : goto out;
1923 : }
1924 0 : if (!S_ISLNK(smb_fname_src->st.st_ex_mode)) {
1925 0 : goto out;
1926 : }
1927 : }
1928 :
1929 0 : if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1930 0 : create_options |= FILE_DIRECTORY_FILE;
1931 : }
1932 :
1933 0 : status = SMB_VFS_CREATE_FILE(
1934 : conn, /* conn */
1935 : req, /* req */
1936 : src_dirfsp, /* dirfsp */
1937 : smb_fname_src, /* fname */
1938 : access_mask, /* access_mask */
1939 : (FILE_SHARE_READ | /* share_access */
1940 : FILE_SHARE_WRITE),
1941 : FILE_OPEN, /* create_disposition*/
1942 : create_options, /* create_options */
1943 : 0, /* file_attributes */
1944 : 0, /* oplock_request */
1945 : NULL, /* lease */
1946 : 0, /* allocation_size */
1947 : 0, /* private_flags */
1948 : NULL, /* sd */
1949 : NULL, /* ea_list */
1950 : &fsp, /* result */
1951 : NULL, /* pinfo */
1952 : posx, /* in_context_blobs */
1953 : NULL); /* out_context_blobs */
1954 :
1955 0 : if (!NT_STATUS_IS_OK(status)) {
1956 0 : DBG_NOTICE("Could not open rename source %s: %s\n",
1957 : smb_fname_str_dbg(smb_fname_src),
1958 : nt_errstr(status));
1959 0 : goto out;
1960 : }
1961 :
1962 0 : status = rename_internals_fsp(conn,
1963 : fsp,
1964 : dst_dirfsp,
1965 : smb_fname_dst,
1966 : dst_original_lcomp,
1967 : attrs,
1968 : replace_if_exists);
1969 :
1970 0 : close_file_free(req, &fsp, NORMAL_CLOSE);
1971 :
1972 0 : DBG_NOTICE("Error %s rename %s -> %s\n",
1973 : nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1974 : smb_fname_str_dbg(smb_fname_dst));
1975 :
1976 0 : out:
1977 0 : TALLOC_FREE(posx);
1978 0 : return status;
1979 : }
1980 :
1981 : /*******************************************************************
1982 : Copy a file as part of a reply_copy.
1983 : ******************************************************************/
1984 :
1985 : /*
1986 : * TODO: check error codes on all callers
1987 : */
1988 :
1989 0 : NTSTATUS copy_file(TALLOC_CTX *ctx,
1990 : connection_struct *conn,
1991 : struct smb_filename *smb_fname_src,
1992 : struct smb_filename *smb_fname_dst,
1993 : uint32_t new_create_disposition)
1994 : {
1995 0 : struct smb_filename *smb_fname_dst_tmp = NULL;
1996 0 : off_t ret=-1;
1997 : files_struct *fsp1,*fsp2;
1998 : uint32_t dosattrs;
1999 : NTSTATUS status;
2000 :
2001 :
2002 0 : smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
2003 0 : if (smb_fname_dst_tmp == NULL) {
2004 0 : return NT_STATUS_NO_MEMORY;
2005 : }
2006 :
2007 0 : status = vfs_file_exist(conn, smb_fname_src);
2008 0 : if (!NT_STATUS_IS_OK(status)) {
2009 0 : goto out;
2010 : }
2011 :
2012 0 : status = openat_pathref_fsp(conn->cwd_fsp, smb_fname_src);
2013 0 : if (!NT_STATUS_IS_OK(status)) {
2014 0 : goto out;
2015 : }
2016 :
2017 : /* Open the src file for reading. */
2018 0 : status = SMB_VFS_CREATE_FILE(
2019 : conn, /* conn */
2020 : NULL, /* req */
2021 : NULL, /* dirfsp */
2022 : smb_fname_src, /* fname */
2023 : FILE_GENERIC_READ, /* access_mask */
2024 : FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
2025 : FILE_OPEN, /* create_disposition*/
2026 : 0, /* create_options */
2027 : FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2028 : INTERNAL_OPEN_ONLY, /* oplock_request */
2029 : NULL, /* lease */
2030 : 0, /* allocation_size */
2031 : 0, /* private_flags */
2032 : NULL, /* sd */
2033 : NULL, /* ea_list */
2034 : &fsp1, /* result */
2035 : NULL, /* psbuf */
2036 : NULL, NULL); /* create context */
2037 :
2038 0 : if (!NT_STATUS_IS_OK(status)) {
2039 0 : goto out;
2040 : }
2041 :
2042 0 : dosattrs = fdos_mode(fsp1);
2043 :
2044 0 : if (SMB_VFS_STAT(conn, smb_fname_dst_tmp) == -1) {
2045 0 : ZERO_STRUCTP(&smb_fname_dst_tmp->st);
2046 : }
2047 :
2048 0 : status = openat_pathref_fsp(conn->cwd_fsp, smb_fname_dst);
2049 0 : if (!NT_STATUS_IS_OK(status) &&
2050 0 : !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND))
2051 : {
2052 0 : goto out;
2053 : }
2054 :
2055 : /* Open the dst file for writing. */
2056 0 : status = SMB_VFS_CREATE_FILE(
2057 : conn, /* conn */
2058 : NULL, /* req */
2059 : NULL, /* dirfsp */
2060 : smb_fname_dst, /* fname */
2061 : FILE_GENERIC_WRITE, /* access_mask */
2062 : FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
2063 : new_create_disposition, /* create_disposition*/
2064 : 0, /* create_options */
2065 : dosattrs, /* file_attributes */
2066 : INTERNAL_OPEN_ONLY, /* oplock_request */
2067 : NULL, /* lease */
2068 : 0, /* allocation_size */
2069 : 0, /* private_flags */
2070 : NULL, /* sd */
2071 : NULL, /* ea_list */
2072 : &fsp2, /* result */
2073 : NULL, /* psbuf */
2074 : NULL, NULL); /* create context */
2075 :
2076 0 : if (!NT_STATUS_IS_OK(status)) {
2077 0 : close_file_free(NULL, &fsp1, ERROR_CLOSE);
2078 0 : goto out;
2079 : }
2080 :
2081 : /* Do the actual copy. */
2082 0 : if (smb_fname_src->st.st_ex_size) {
2083 0 : ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
2084 : } else {
2085 0 : ret = 0;
2086 : }
2087 :
2088 0 : close_file_free(NULL, &fsp1, NORMAL_CLOSE);
2089 :
2090 : /* Ensure the modtime is set correctly on the destination file. */
2091 0 : set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
2092 :
2093 : /*
2094 : * As we are opening fsp1 read-only we only expect
2095 : * an error on close on fsp2 if we are out of space.
2096 : * Thus we don't look at the error return from the
2097 : * close of fsp1.
2098 : */
2099 0 : status = close_file_free(NULL, &fsp2, NORMAL_CLOSE);
2100 :
2101 0 : if (!NT_STATUS_IS_OK(status)) {
2102 0 : goto out;
2103 : }
2104 :
2105 0 : if (ret != (off_t)smb_fname_src->st.st_ex_size) {
2106 0 : status = NT_STATUS_DISK_FULL;
2107 0 : goto out;
2108 : }
2109 :
2110 0 : status = NT_STATUS_OK;
2111 :
2112 0 : out:
2113 0 : TALLOC_FREE(smb_fname_dst_tmp);
2114 0 : return status;
2115 : }
2116 :
2117 : /****************************************************************************
2118 : Get a lock offset, dealing with large offset requests.
2119 : ****************************************************************************/
2120 :
2121 0 : uint64_t get_lock_offset(const uint8_t *data, int data_offset,
2122 : bool large_file_format)
2123 : {
2124 0 : uint64_t offset = 0;
2125 :
2126 0 : if(!large_file_format) {
2127 0 : offset = (uint64_t)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
2128 : } else {
2129 : /*
2130 : * No BVAL, this is reversed!
2131 : */
2132 0 : offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
2133 0 : ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
2134 : }
2135 :
2136 0 : return offset;
2137 : }
2138 :
2139 : struct smbd_do_unlocking_state {
2140 : struct files_struct *fsp;
2141 : uint16_t num_ulocks;
2142 : struct smbd_lock_element *ulocks;
2143 : NTSTATUS status;
2144 : };
2145 :
2146 8 : static void smbd_do_unlocking_fn(
2147 : struct share_mode_lock *lck,
2148 : void *private_data)
2149 : {
2150 8 : struct smbd_do_unlocking_state *state = private_data;
2151 8 : struct files_struct *fsp = state->fsp;
2152 : uint16_t i;
2153 :
2154 12 : for (i = 0; i < state->num_ulocks; i++) {
2155 8 : struct smbd_lock_element *e = &state->ulocks[i];
2156 :
2157 8 : DBG_DEBUG("unlock start=%"PRIu64", len=%"PRIu64" for "
2158 : "pid %"PRIu64", file %s\n",
2159 : e->offset,
2160 : e->count,
2161 : e->smblctx,
2162 : fsp_str_dbg(fsp));
2163 :
2164 8 : if (e->brltype != UNLOCK_LOCK) {
2165 : /* this can only happen with SMB2 */
2166 0 : state->status = NT_STATUS_INVALID_PARAMETER;
2167 0 : return;
2168 : }
2169 :
2170 8 : state->status = do_unlock(
2171 : fsp, e->smblctx, e->count, e->offset, e->lock_flav);
2172 :
2173 8 : DBG_DEBUG("do_unlock returned %s\n",
2174 : nt_errstr(state->status));
2175 :
2176 8 : if (!NT_STATUS_IS_OK(state->status)) {
2177 4 : return;
2178 : }
2179 : }
2180 :
2181 4 : share_mode_wakeup_waiters(fsp->file_id);
2182 : }
2183 :
2184 8 : NTSTATUS smbd_do_unlocking(struct smb_request *req,
2185 : files_struct *fsp,
2186 : uint16_t num_ulocks,
2187 : struct smbd_lock_element *ulocks)
2188 : {
2189 8 : struct smbd_do_unlocking_state state = {
2190 : .fsp = fsp,
2191 : .num_ulocks = num_ulocks,
2192 : .ulocks = ulocks,
2193 : };
2194 : NTSTATUS status;
2195 :
2196 8 : DBG_NOTICE("%s num_ulocks=%"PRIu16"\n", fsp_fnum_dbg(fsp), num_ulocks);
2197 :
2198 8 : status = share_mode_do_locked_vfs_allowed(
2199 : fsp->file_id, smbd_do_unlocking_fn, &state);
2200 :
2201 8 : if (!NT_STATUS_IS_OK(status)) {
2202 0 : DBG_DEBUG("share_mode_do_locked_vfs_allowed failed: %s\n",
2203 : nt_errstr(status));
2204 0 : return status;
2205 : }
2206 8 : if (!NT_STATUS_IS_OK(state.status)) {
2207 4 : DBG_DEBUG("smbd_do_unlocking_fn failed: %s\n",
2208 : nt_errstr(status));
2209 4 : return state.status;
2210 : }
2211 :
2212 4 : return NT_STATUS_OK;
2213 : }
|