ctdb-server: Clean up connection tracking functions
[samba4-gss.git] / source3 / libsmb / libsmb_dir.c
blob59bee575673ef19daba04991fe8ce1be3998a208
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/namequery.h"
27 #include "libsmb/libsmb.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
32 #include "libsmb/nmblib.h"
33 #include "../libcli/smb/smbXcli_base.h"
34 #include "../libcli/security/security.h"
35 #include "lib/util/tevent_ntstatus.h"
36 #include "lib/util/time_basic.h"
37 #include "lib/util/string_wrappers.h"
40 * Routine to open a directory
41 * We accept the URL syntax explained in SMBC_parse_path(), above.
44 static void remove_dirplus(SMBCFILE *dir)
46 struct smbc_dirplus_list *d = NULL;
48 d = dir->dirplus_list;
49 while (d != NULL) {
50 struct smbc_dirplus_list *f = d;
51 d = d->next;
53 SAFE_FREE(f->smb_finfo->short_name);
54 SAFE_FREE(f->smb_finfo->name);
55 SAFE_FREE(f->smb_finfo);
56 SAFE_FREE(f);
59 dir->dirplus_list = NULL;
60 dir->dirplus_end = NULL;
61 dir->dirplus_next = NULL;
64 static void
65 remove_dir(SMBCFILE *dir)
67 struct smbc_dir_list *d,*f;
69 d = dir->dir_list;
70 while (d) {
72 f = d; d = d->next;
74 SAFE_FREE(f->dirent);
75 SAFE_FREE(f);
79 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
83 static int
84 add_dirent(SMBCFILE *dir,
85 const char *name,
86 const char *comment,
87 uint32_t type)
89 struct smbc_dirent *dirent;
90 int size;
91 int name_length = (name == NULL ? 0 : strlen(name));
92 int comment_len = (comment == NULL ? 0 : strlen(comment));
95 * Allocate space for the dirent, which must be increased by the
96 * size of the name and the comment and 1 each for the null terminator.
99 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
101 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
103 if (!dirent) {
105 dir->dir_error = ENOMEM;
106 return -1;
110 ZERO_STRUCTP(dirent);
112 if (dir->dir_list == NULL) {
114 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
115 if (!dir->dir_list) {
117 SAFE_FREE(dirent);
118 dir->dir_error = ENOMEM;
119 return -1;
122 ZERO_STRUCTP(dir->dir_list);
124 dir->dir_end = dir->dir_next = dir->dir_list;
126 else {
128 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
130 if (!dir->dir_end->next) {
132 SAFE_FREE(dirent);
133 dir->dir_error = ENOMEM;
134 return -1;
137 ZERO_STRUCTP(dir->dir_end->next);
139 dir->dir_end = dir->dir_end->next;
142 dir->dir_end->next = NULL;
143 dir->dir_end->dirent = dirent;
145 dirent->smbc_type = type;
146 dirent->namelen = name_length;
147 dirent->commentlen = comment_len;
148 dirent->dirlen = size;
151 * dirent->namelen + 1 includes the null (no null termination needed)
152 * Ditto for dirent->commentlen.
153 * The space for the two null bytes was allocated.
155 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
156 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
157 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
159 return 0;
163 static int add_dirplus(SMBCFILE *dir, struct file_info *finfo)
165 struct smbc_dirplus_list *new_entry = NULL;
166 struct libsmb_file_info *info = NULL;
168 new_entry = SMB_MALLOC_P(struct smbc_dirplus_list);
169 if (new_entry == NULL) {
170 dir->dir_error = ENOMEM;
171 return -1;
173 ZERO_STRUCTP(new_entry);
174 new_entry->ino = finfo->ino;
176 info = SMB_MALLOC_P(struct libsmb_file_info);
177 if (info == NULL) {
178 SAFE_FREE(new_entry);
179 dir->dir_error = ENOMEM;
180 return -1;
183 ZERO_STRUCTP(info);
185 info->btime_ts = finfo->btime_ts;
186 info->atime_ts = finfo->atime_ts;
187 info->ctime_ts = finfo->ctime_ts;
188 info->mtime_ts = finfo->mtime_ts;
189 info->attrs = finfo->attr;
190 info->size = finfo->size;
191 info->name = SMB_STRDUP(finfo->name);
192 if (info->name == NULL) {
193 SAFE_FREE(info);
194 SAFE_FREE(new_entry);
195 dir->dir_error = ENOMEM;
196 return -1;
199 if (finfo->short_name) {
200 info->short_name = SMB_STRDUP(finfo->short_name);
201 } else {
202 info->short_name = SMB_STRDUP("");
205 if (info->short_name == NULL) {
206 SAFE_FREE(info->name);
207 SAFE_FREE(info);
208 SAFE_FREE(new_entry);
209 dir->dir_error = ENOMEM;
210 return -1;
212 new_entry->smb_finfo = info;
214 /* Now add to the list. */
215 if (dir->dirplus_list == NULL) {
216 /* Empty list - point everything at new_entry. */
217 dir->dirplus_list = new_entry;
218 dir->dirplus_end = new_entry;
219 dir->dirplus_next = new_entry;
220 } else {
221 /* Append to list but leave the ->next cursor alone. */
222 dir->dirplus_end->next = new_entry;
223 dir->dirplus_end = new_entry;
226 return 0;
229 static void
230 list_unique_wg_fn(const char *name,
231 uint32_t type,
232 const char *comment,
233 void *state)
235 SMBCFILE *dir = (SMBCFILE *)state;
236 struct smbc_dir_list *dir_list;
237 struct smbc_dirent *dirent;
238 int dirent_type;
239 int do_remove = 0;
241 dirent_type = dir->dir_type;
243 if (add_dirent(dir, name, comment, dirent_type) < 0) {
244 /* An error occurred, what do we do? */
245 /* FIXME: Add some code here */
246 /* Change cli_NetServerEnum to take a fn
247 returning NTSTATUS... JRA. */
250 /* Point to the one just added */
251 dirent = dir->dir_end->dirent;
253 /* See if this was a duplicate */
254 for (dir_list = dir->dir_list;
255 dir_list != dir->dir_end;
256 dir_list = dir_list->next) {
257 if (! do_remove &&
258 strcmp(dir_list->dirent->name, dirent->name) == 0) {
259 /* Duplicate. End end of list need to be removed. */
260 do_remove = 1;
263 if (do_remove && dir_list->next == dir->dir_end) {
264 /* Found the end of the list. Remove it. */
265 dir->dir_end = dir_list;
266 free(dir_list->next);
267 free(dirent);
268 dir_list->next = NULL;
269 break;
274 static void
275 list_fn(const char *name,
276 uint32_t type,
277 const char *comment,
278 void *state)
280 SMBCFILE *dir = (SMBCFILE *)state;
281 int dirent_type;
284 * We need to process the type a little ...
286 * Disk share = 0x00000000
287 * Print share = 0x00000001
288 * Comms share = 0x00000002 (obsolete?)
289 * IPC$ share = 0x00000003
291 * administrative shares:
292 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
295 if (dir->dir_type == SMBC_FILE_SHARE) {
296 switch (type) {
297 case 0 | 0x80000000:
298 case 0:
299 dirent_type = SMBC_FILE_SHARE;
300 break;
302 case 1:
303 dirent_type = SMBC_PRINTER_SHARE;
304 break;
306 case 2:
307 dirent_type = SMBC_COMMS_SHARE;
308 break;
310 case 3 | 0x80000000:
311 case 3:
312 dirent_type = SMBC_IPC_SHARE;
313 break;
315 default:
316 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
317 break;
320 else {
321 dirent_type = dir->dir_type;
324 if (add_dirent(dir, name, comment, dirent_type) < 0) {
325 /* An error occurred, what do we do? */
326 /* FIXME: Add some code here */
327 /* Change cli_NetServerEnum to take a fn
328 returning NTSTATUS... JRA. */
332 static NTSTATUS
333 dir_list_fn(struct file_info *finfo,
334 const char *mask,
335 void *state)
337 SMBCFILE *dirp = (SMBCFILE *)state;
338 int ret;
340 if (add_dirent((SMBCFILE *)state, finfo->name, "",
341 (finfo->attr&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
342 SMBCFILE *dir = (SMBCFILE *)state;
343 return map_nt_error_from_unix(dir->dir_error);
345 ret = add_dirplus(dirp, finfo);
346 if (ret < 0) {
347 return map_nt_error_from_unix(dirp->dir_error);
349 return NT_STATUS_OK;
352 static NTSTATUS
353 net_share_enum_rpc(struct cli_state *cli,
354 void (*fn)(const char *name,
355 uint32_t type,
356 const char *comment,
357 void *state),
358 void *state)
360 uint32_t i;
361 WERROR result;
362 uint32_t preferred_len = 0xffffffff;
363 uint32_t type;
364 struct srvsvc_NetShareInfoCtr info_ctr;
365 struct srvsvc_NetShareCtr1 ctr1;
366 fstring name = "";
367 fstring comment = "";
368 struct rpc_pipe_client *pipe_hnd = NULL;
369 NTSTATUS nt_status;
370 uint32_t resume_handle = 0;
371 uint32_t total_entries = 0;
372 struct dcerpc_binding_handle *b;
374 /* Open the server service pipe */
375 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
376 &pipe_hnd);
377 if (!NT_STATUS_IS_OK(nt_status)) {
378 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
379 goto done;
382 ZERO_STRUCT(info_ctr);
383 ZERO_STRUCT(ctr1);
385 info_ctr.level = 1;
386 info_ctr.ctr.ctr1 = &ctr1;
388 b = pipe_hnd->binding_handle;
390 /* Issue the NetShareEnum RPC call and retrieve the response */
391 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
392 pipe_hnd->desthost,
393 &info_ctr,
394 preferred_len,
395 &total_entries,
396 &resume_handle,
397 &result);
399 /* Was it successful? */
400 if (!NT_STATUS_IS_OK(nt_status)) {
401 /* Nope. Go clean up. */
402 goto done;
405 if (!W_ERROR_IS_OK(result)) {
406 /* Nope. Go clean up. */
407 nt_status = werror_to_ntstatus(result);
408 goto done;
411 if (total_entries == 0) {
412 /* Nope. Go clean up. */
413 nt_status = NT_STATUS_NOT_FOUND;
414 goto done;
417 /* For each returned entry... */
418 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
420 /* pull out the share name */
421 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
423 /* pull out the share's comment */
424 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
426 /* Get the type value */
427 type = info_ctr.ctr.ctr1->array[i].type;
429 /* Add this share to the list */
430 (*fn)(name, type, comment, state);
433 done:
434 /* Close the server service pipe */
435 TALLOC_FREE(pipe_hnd);
437 /* Tell 'em if it worked */
438 return nt_status;
443 * Verify that the options specified in a URL are valid
446 SMBC_check_options(char *server,
447 char *share,
448 char *path,
449 char *options)
451 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server, share, path, options));
455 /* No options at all is always ok */
456 if (! *options) return 0;
458 /* Currently, we don't support any options. */
459 return -1;
463 SMBCFILE *
464 SMBC_opendir_ctx(SMBCCTX *context,
465 const char *fname)
467 char *server = NULL;
468 char *share = NULL;
469 char *user = NULL;
470 char *password = NULL;
471 char *options = NULL;
472 char *workgroup = NULL;
473 char *path = NULL;
474 size_t path_len = 0;
475 uint16_t port = 0;
476 SMBCSRV *srv = NULL;
477 SMBCFILE *dir = NULL;
478 struct sockaddr_storage rem_ss;
479 TALLOC_CTX *frame = talloc_stackframe();
481 if (!context || !context->internal->initialized) {
482 DEBUG(4, ("no valid context\n"));
483 TALLOC_FREE(frame);
484 errno = EINVAL + 8192;
485 return NULL;
489 if (!fname) {
490 DEBUG(4, ("no valid fname\n"));
491 TALLOC_FREE(frame);
492 errno = EINVAL + 8193;
493 return NULL;
496 if (SMBC_parse_path(frame,
497 context,
498 fname,
499 &workgroup,
500 &server,
501 &port,
502 &share,
503 &path,
504 &user,
505 &password,
506 &options)) {
507 DEBUG(4, ("no valid path\n"));
508 TALLOC_FREE(frame);
509 errno = EINVAL + 8194;
510 return NULL;
513 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
514 "path='%s' options='%s'\n",
515 fname, server, share, path, options));
517 /* Ensure the options are valid */
518 if (SMBC_check_options(server, share, path, options)) {
519 DEBUG(4, ("unacceptable options (%s)\n", options));
520 TALLOC_FREE(frame);
521 errno = EINVAL + 8195;
522 return NULL;
525 if (!user || user[0] == (char)0) {
526 user = talloc_strdup(frame, smbc_getUser(context));
527 if (!user) {
528 TALLOC_FREE(frame);
529 errno = ENOMEM;
530 return NULL;
534 dir = SMB_MALLOC_P(SMBCFILE);
536 if (!dir) {
537 TALLOC_FREE(frame);
538 errno = ENOMEM;
539 return NULL;
542 ZERO_STRUCTP(dir);
544 dir->cli_fd = 0;
545 dir->fname = SMB_STRDUP(fname);
546 if (dir->fname == NULL) {
547 SAFE_FREE(dir);
548 TALLOC_FREE(frame);
549 errno = ENOMEM;
550 return NULL;
552 dir->srv = NULL;
553 dir->offset = 0;
554 dir->file = False;
555 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
557 if (server[0] == (char)0) {
559 size_t i;
560 size_t count = 0;
561 size_t max_lmb_count;
562 struct sockaddr_storage *ip_list;
563 struct sockaddr_storage server_addr;
564 struct cli_credentials *creds = NULL;
565 NTSTATUS status;
567 if (share[0] != (char)0 || path[0] != (char)0) {
569 if (dir) {
570 SAFE_FREE(dir->fname);
571 SAFE_FREE(dir);
573 TALLOC_FREE(frame);
574 errno = EINVAL + 8196;
575 return NULL;
578 /* Determine how many local master browsers to query */
579 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
580 ? INT_MAX
581 : smbc_getOptionBrowseMaxLmbCount(context));
583 creds = cli_credentials_init(frame);
584 if (creds == NULL) {
585 if (dir) {
586 SAFE_FREE(dir->fname);
587 SAFE_FREE(dir);
589 TALLOC_FREE(frame);
590 errno = ENOMEM;
591 return NULL;
594 (void)cli_credentials_set_username(creds, user, CRED_SPECIFIED);
595 (void)cli_credentials_set_password(creds, password, CRED_SPECIFIED);
598 * We have server and share and path empty but options
599 * requesting that we scan all master browsers for their list
600 * of workgroups/domains. This implies that we must first try
601 * broadcast queries to find all master browsers, and if that
602 * doesn't work, then try our other methods which return only
603 * a single master browser.
606 ip_list = NULL;
607 status = name_resolve_bcast(talloc_tos(),
608 MSBROWSE,
610 &ip_list,
611 &count);
612 if (!NT_STATUS_IS_OK(status))
615 TALLOC_FREE(ip_list);
617 if (!find_master_ip(workgroup, &server_addr)) {
619 if (dir) {
620 SAFE_FREE(dir->fname);
621 SAFE_FREE(dir);
623 TALLOC_FREE(frame);
624 errno = ENOENT;
625 return NULL;
628 ip_list = (struct sockaddr_storage *)talloc_memdup(
629 talloc_tos(), &server_addr,
630 sizeof(server_addr));
631 if (ip_list == NULL) {
632 if (dir) {
633 SAFE_FREE(dir->fname);
634 SAFE_FREE(dir);
636 TALLOC_FREE(frame);
637 errno = ENOMEM;
638 return NULL;
640 count = 1;
643 for (i = 0; i < count && i < max_lmb_count; i++) {
644 char addr[INET6_ADDRSTRLEN];
645 char *wg_ptr = NULL;
646 struct cli_state *cli = NULL;
648 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
649 DEBUG(99, ("Found master browser %zu of %zu: %s\n",
650 i+1, MAX(count, max_lmb_count),
651 addr));
653 cli = get_ipc_connect_master_ip(talloc_tos(),
654 &ip_list[i],
655 creds,
656 &wg_ptr);
657 /* cli == NULL is the master browser refused to talk or
658 could not be found */
659 if (!cli) {
660 continue;
663 workgroup = talloc_strdup(frame, wg_ptr);
664 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
666 cli_shutdown(cli);
668 if (!workgroup || !server) {
669 if (dir) {
670 SAFE_FREE(dir->fname);
671 SAFE_FREE(dir);
673 TALLOC_FREE(frame);
674 errno = ENOMEM;
675 return NULL;
678 DEBUG(4, ("using workgroup %s %s\n",
679 workgroup, server));
682 * For each returned master browser IP address, get a
683 * connection to IPC$ on the server if we do not
684 * already have one, and determine the
685 * workgroups/domains that it knows about.
688 srv = SMBC_server(frame, context, True, server, port, "IPC$",
689 &workgroup, &user, &password);
690 if (!srv) {
691 continue;
694 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
695 continue;
698 dir->srv = srv;
699 dir->dir_type = SMBC_WORKGROUP;
701 /* Now, list the stuff ... */
703 status = cli_NetServerEnum(srv->cli,
704 workgroup,
705 SV_TYPE_DOMAIN_ENUM,
706 list_unique_wg_fn,
707 (void *)dir);
708 if (!NT_STATUS_IS_OK(status)) {
709 continue;
713 TALLOC_FREE(ip_list);
714 } else {
716 * Server not an empty string ... Check the rest and see what
717 * gives
719 if (*share == '\0') {
720 if (*path != '\0') {
722 /* Should not have empty share with path */
723 if (dir) {
724 SAFE_FREE(dir->fname);
725 SAFE_FREE(dir);
727 TALLOC_FREE(frame);
728 errno = EINVAL + 8197;
729 return NULL;
734 * We don't know if <server> is really a server name
735 * or is a workgroup/domain name. If we already have
736 * a server structure for it, we'll use it.
737 * Otherwise, check to see if <server><1D>,
738 * <server><1B>, or <server><20> translates. We check
739 * to see if <server> is an IP address first.
743 * See if we have an existing server. Do not
744 * establish a connection if one does not already
745 * exist.
747 srv = SMBC_server(frame, context, False,
748 server, port, "IPC$",
749 &workgroup, &user, &password);
752 * If no existing server and not an IP addr, look for
753 * LMB or DMB
755 if (!srv &&
756 !is_ipaddress(server) &&
757 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
758 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
760 * "server" is actually a workgroup name,
761 * not a server. Make this clear.
763 char *wgroup = server;
764 fstring buserver;
765 NTSTATUS status;
767 dir->dir_type = SMBC_SERVER;
770 * Get the backup list ...
772 if (!name_status_find(wgroup, 0, 0,
773 &rem_ss, buserver)) {
774 char addr[INET6_ADDRSTRLEN];
776 print_sockaddr(addr, sizeof(addr), &rem_ss);
777 DEBUG(0,("Could not get name of "
778 "local/domain master browser "
779 "for workgroup %s from "
780 "address %s\n",
781 wgroup,
782 addr));
783 if (dir) {
784 SAFE_FREE(dir->fname);
785 SAFE_FREE(dir);
787 TALLOC_FREE(frame);
788 errno = EPERM;
789 return NULL;
794 * Get a connection to IPC$ on the server if
795 * we do not already have one
797 srv = SMBC_server(frame, context, True,
798 buserver, port, "IPC$",
799 &workgroup,
800 &user, &password);
801 if (!srv) {
802 DEBUG(0, ("got no contact to IPC$\n"));
803 if (dir) {
804 SAFE_FREE(dir->fname);
805 SAFE_FREE(dir);
807 TALLOC_FREE(frame);
808 return NULL;
812 dir->srv = srv;
814 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
815 if (dir) {
816 SAFE_FREE(dir->fname);
817 SAFE_FREE(dir);
819 TALLOC_FREE(frame);
820 return NULL;
823 /* Now, list the servers ... */
824 status = cli_NetServerEnum(srv->cli,
825 wgroup,
826 0x0000FFFE,
827 list_fn,
828 (void *)dir);
829 if (!NT_STATUS_IS_OK(status)) {
830 if (dir) {
831 SAFE_FREE(dir->fname);
832 SAFE_FREE(dir);
834 TALLOC_FREE(frame);
835 return NULL;
837 } else if (srv ||
838 (resolve_name(server, &rem_ss, 0x20, false))) {
839 NTSTATUS status;
842 * If we hadn't found the server, get one now
844 if (!srv) {
845 srv = SMBC_server(frame, context, True,
846 server, port, "IPC$",
847 &workgroup,
848 &user, &password);
851 if (!srv) {
852 if (dir) {
853 SAFE_FREE(dir->fname);
854 SAFE_FREE(dir);
856 TALLOC_FREE(frame);
857 return NULL;
861 dir->dir_type = SMBC_FILE_SHARE;
862 dir->srv = srv;
864 /* List the shares ... */
866 status = net_share_enum_rpc(srv->cli,
867 list_fn,
868 (void *)dir);
869 if (!NT_STATUS_IS_OK(status) &&
870 smbXcli_conn_protocol(srv->cli->conn) <=
871 PROTOCOL_NT1) {
873 * Only call cli_RNetShareEnum()
874 * on SMB1 connections, not SMB2+.
876 status = cli_RNetShareEnum(
877 srv->cli,
878 list_fn,
879 (void *)dir);
881 if (!NT_STATUS_IS_OK(status)) {
882 if (dir != NULL) {
883 SAFE_FREE(dir->fname);
884 SAFE_FREE(dir);
886 TALLOC_FREE(frame);
887 errno = map_errno_from_nt_status(
888 status);
889 return NULL;
891 } else {
892 /* Neither the workgroup nor server exists */
893 errno = ECONNREFUSED;
894 if (dir) {
895 SAFE_FREE(dir->fname);
896 SAFE_FREE(dir);
898 TALLOC_FREE(frame);
899 return NULL;
903 else {
905 * The server and share are specified ... work from
906 * there ...
908 char *targetpath;
909 struct cli_state *targetcli;
910 struct cli_credentials *creds = NULL;
911 NTSTATUS status;
913 /* We connect to the server and list the directory */
914 dir->dir_type = SMBC_FILE_SHARE;
916 srv = SMBC_server(frame, context, True, server, port, share,
917 &workgroup, &user, &password);
919 if (!srv) {
920 if (dir) {
921 SAFE_FREE(dir->fname);
922 SAFE_FREE(dir);
924 TALLOC_FREE(frame);
925 return NULL;
928 dir->srv = srv;
930 /* Now, list the files ... */
932 path_len = strlen(path);
933 path = talloc_asprintf_append(path, "\\*");
934 if (!path) {
935 if (dir) {
936 SAFE_FREE(dir->fname);
937 SAFE_FREE(dir);
939 TALLOC_FREE(frame);
940 return NULL;
943 creds = context->internal->creds;
945 status = cli_resolve_path(
946 frame, "",
947 creds,
948 srv->cli, path, &targetcli, &targetpath);
949 if (!NT_STATUS_IS_OK(status)) {
950 d_printf("Could not resolve %s\n", path);
951 if (dir) {
952 SAFE_FREE(dir->fname);
953 SAFE_FREE(dir);
955 TALLOC_FREE(frame);
956 return NULL;
959 status = cli_list(targetcli, targetpath,
960 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
961 dir_list_fn, (void *)dir);
962 if (!NT_STATUS_IS_OK(status)) {
963 int saved_errno;
964 if (dir) {
965 SAFE_FREE(dir->fname);
966 SAFE_FREE(dir);
968 saved_errno = cli_status_to_errno(status);
970 if (saved_errno == EINVAL) {
971 struct stat sb = {0};
973 * See if they asked to opendir
974 * something other than a directory.
975 * If so, the converted error value we
976 * got would have been EINVAL rather
977 * than ENOTDIR.
979 path[path_len] = '\0'; /* restore original path */
981 status = SMBC_getatr(
982 context,
983 srv,
984 path,
985 &sb);
986 if (NT_STATUS_IS_OK(status) &&
987 !S_ISDIR(sb.st_mode)) {
989 /* It is. Correct the error value */
990 saved_errno = ENOTDIR;
995 * There was an error (we're in the
996 * !NT_STATUS_IS_OK branch) and the
997 * server good any more...
999 if (smbc_getFunctionCheckServer(
1000 context)(context, srv)) {
1002 /* ... then remove it. */
1003 if (smbc_getFunctionRemoveUnusedServer(context)(context,
1004 srv)) {
1006 * We could not remove the
1007 * server completely, remove
1008 * it from the cache so we
1009 * will not get it again. It
1010 * will be removed when the
1011 * last file/dir is closed.
1013 smbc_getFunctionRemoveCachedServer(context)(context, srv);
1017 TALLOC_FREE(frame);
1018 errno = saved_errno;
1019 return NULL;
1025 DLIST_ADD(context->internal->files, dir);
1026 TALLOC_FREE(frame);
1027 return dir;
1032 * Routine to close a directory
1036 SMBC_closedir_ctx(SMBCCTX *context,
1037 SMBCFILE *dir)
1039 TALLOC_CTX *frame = NULL;
1041 if (!context || !context->internal->initialized) {
1042 errno = EINVAL;
1043 return -1;
1046 if (dir == NULL) {
1047 return 0;
1050 frame = talloc_stackframe();
1052 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1053 errno = EBADF;
1054 TALLOC_FREE(frame);
1055 return -1;
1058 remove_dir(dir); /* Clean it up */
1059 remove_dirplus(dir);
1061 DLIST_REMOVE(context->internal->files, dir);
1063 SAFE_FREE(dir->fname);
1064 SAFE_FREE(dir); /* Free the space too */
1066 TALLOC_FREE(frame);
1067 return 0;
1071 static int
1072 smbc_readdir_internal(SMBCCTX * context,
1073 struct smbc_dirent *dest,
1074 struct smbc_dirent *src,
1075 int max_namebuf_len)
1077 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
1078 int remaining_len;
1080 /* url-encode the name. get back remaining buffer space */
1081 remaining_len =
1082 smbc_urlencode(dest->name, src->name, max_namebuf_len);
1084 /* -1 means no null termination. */
1085 if (remaining_len < 0) {
1086 return -1;
1089 /* We now know the name length */
1090 dest->namelen = strlen(dest->name);
1092 if (dest->namelen + 1 < 1) {
1093 /* Integer wrap. */
1094 return -1;
1097 if (dest->namelen + 1 >= max_namebuf_len) {
1098 /* Out of space for comment. */
1099 return -1;
1102 /* Save the pointer to the beginning of the comment */
1103 dest->comment = dest->name + dest->namelen + 1;
1105 if (remaining_len < 1) {
1106 /* No room for comment null termination. */
1107 return -1;
1110 /* Copy the comment */
1111 strlcpy(dest->comment, src->comment, remaining_len);
1113 /* Save other fields */
1114 dest->smbc_type = src->smbc_type;
1115 dest->commentlen = strlen(dest->comment);
1116 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
1117 (char *) dest);
1118 } else {
1120 /* No encoding. Just copy the entry as is. */
1121 if (src->dirlen > max_namebuf_len) {
1122 return -1;
1124 memcpy(dest, src, src->dirlen);
1125 if (src->namelen + 1 < 1) {
1126 /* Integer wrap */
1127 return -1;
1129 if (src->namelen + 1 >= max_namebuf_len) {
1130 /* Comment off the end. */
1131 return -1;
1133 dest->comment = (char *)(&dest->name + src->namelen + 1);
1135 return 0;
1139 * Routine to get a directory entry
1142 struct smbc_dirent *
1143 SMBC_readdir_ctx(SMBCCTX *context,
1144 SMBCFILE *dir)
1146 int maxlen;
1147 int ret;
1148 struct smbc_dirent *dirp, *dirent;
1149 TALLOC_CTX *frame = talloc_stackframe();
1151 /* Check that all is ok first ... */
1153 if (!context || !context->internal->initialized) {
1155 errno = EINVAL;
1156 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1157 TALLOC_FREE(frame);
1158 return NULL;
1162 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1164 errno = EBADF;
1165 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1166 TALLOC_FREE(frame);
1167 return NULL;
1171 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1173 errno = ENOTDIR;
1174 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1175 TALLOC_FREE(frame);
1176 return NULL;
1180 if (!dir->dir_next) {
1181 TALLOC_FREE(frame);
1182 return NULL;
1185 dirent = dir->dir_next->dirent;
1186 if (!dirent) {
1188 errno = ENOENT;
1189 TALLOC_FREE(frame);
1190 return NULL;
1194 dirp = &context->internal->dirent;
1195 maxlen = sizeof(context->internal->_dirent_name);
1197 ret = smbc_readdir_internal(context, dirp, dirent, maxlen);
1198 if (ret == -1) {
1199 errno = EINVAL;
1200 TALLOC_FREE(frame);
1201 return NULL;
1204 dir->dir_next = dir->dir_next->next;
1207 * If we are returning file entries, we
1208 * have a duplicate list in dirplus.
1210 * Update dirplus_next also so readdir and
1211 * readdirplus are kept in sync.
1213 if (dir->dirplus_list != NULL) {
1214 dir->dirplus_next = dir->dirplus_next->next;
1217 TALLOC_FREE(frame);
1218 return dirp;
1222 * Routine to get a directory entry with all attributes
1225 const struct libsmb_file_info *
1226 SMBC_readdirplus_ctx(SMBCCTX *context,
1227 SMBCFILE *dir)
1229 struct libsmb_file_info *smb_finfo = NULL;
1230 TALLOC_CTX *frame = talloc_stackframe();
1232 /* Check that all is ok first ... */
1234 if (context == NULL || !context->internal->initialized) {
1235 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1236 TALLOC_FREE(frame);
1237 errno = EINVAL;
1238 return NULL;
1241 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1242 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1243 TALLOC_FREE(frame);
1244 errno = EBADF;
1245 return NULL;
1248 if (dir->dirplus_next == NULL) {
1249 TALLOC_FREE(frame);
1250 return NULL;
1253 smb_finfo = dir->dirplus_next->smb_finfo;
1254 if (smb_finfo == NULL) {
1255 TALLOC_FREE(frame);
1256 errno = ENOENT;
1257 return NULL;
1259 dir->dirplus_next = dir->dirplus_next->next;
1262 * If we are returning file entries, we
1263 * have a duplicate list in dir_list
1265 * Update dir_next also so readdir and
1266 * readdirplus are kept in sync.
1268 if (dir->dir_list) {
1269 dir->dir_next = dir->dir_next->next;
1272 TALLOC_FREE(frame);
1273 return smb_finfo;
1277 * Routine to get a directory entry plus a filled in stat structure if
1278 * requested.
1281 const struct libsmb_file_info *SMBC_readdirplus2_ctx(SMBCCTX *context,
1282 SMBCFILE *dir,
1283 struct stat *st)
1285 struct libsmb_file_info *smb_finfo = NULL;
1286 struct smbc_dirplus_list *dp_list = NULL;
1287 ino_t ino;
1288 char *full_pathname = NULL;
1289 char *workgroup = NULL;
1290 char *server = NULL;
1291 uint16_t port = 0;
1292 char *share = NULL;
1293 char *path = NULL;
1294 char *user = NULL;
1295 char *password = NULL;
1296 char *options = NULL;
1297 int rc;
1298 TALLOC_CTX *frame = NULL;
1301 * Allow caller to pass in NULL for stat pointer if
1302 * required. This makes this call identical to
1303 * smbc_readdirplus().
1306 if (st == NULL) {
1307 return SMBC_readdirplus_ctx(context, dir);
1310 frame = talloc_stackframe();
1312 /* Check that all is ok first ... */
1313 if (context == NULL || !context->internal->initialized) {
1314 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1315 TALLOC_FREE(frame);
1316 errno = EINVAL;
1317 return NULL;
1320 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1321 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1322 TALLOC_FREE(frame);
1323 errno = EBADF;
1324 return NULL;
1327 dp_list = dir->dirplus_next;
1328 if (dp_list == NULL) {
1329 TALLOC_FREE(frame);
1330 return NULL;
1333 ino = (ino_t)dp_list->ino;
1335 smb_finfo = dp_list->smb_finfo;
1336 if (smb_finfo == NULL) {
1337 TALLOC_FREE(frame);
1338 errno = ENOENT;
1339 return NULL;
1342 full_pathname = talloc_asprintf(frame,
1343 "%s/%s",
1344 dir->fname,
1345 smb_finfo->name);
1346 if (full_pathname == NULL) {
1347 TALLOC_FREE(frame);
1348 errno = ENOENT;
1349 return NULL;
1352 rc = SMBC_parse_path(frame,
1353 context,
1354 full_pathname,
1355 &workgroup,
1356 &server,
1357 &port,
1358 &share,
1359 &path,
1360 &user,
1361 &password,
1362 &options);
1363 if (rc != 0) {
1364 TALLOC_FREE(frame);
1365 errno = ENOENT;
1366 return NULL;
1369 setup_stat(st,
1370 path,
1371 smb_finfo->size,
1372 smb_finfo->attrs,
1373 ino,
1374 dir->srv->dev,
1375 smb_finfo->atime_ts,
1376 smb_finfo->ctime_ts,
1377 smb_finfo->mtime_ts);
1379 TALLOC_FREE(full_pathname);
1381 dir->dirplus_next = dir->dirplus_next->next;
1384 * If we are returning file entries, we
1385 * have a duplicate list in dir_list
1387 * Update dir_next also so readdir and
1388 * readdirplus are kept in sync.
1390 if (dir->dir_list) {
1391 dir->dir_next = dir->dir_next->next;
1394 TALLOC_FREE(frame);
1395 return smb_finfo;
1399 * Routine to get directory entries
1403 SMBC_getdents_ctx(SMBCCTX *context,
1404 SMBCFILE *dir,
1405 struct smbc_dirent *dirp,
1406 int count)
1408 int rem = count;
1409 int reqd;
1410 int maxlen;
1411 char *ndir = (char *)dirp;
1412 struct smbc_dir_list *dirlist;
1413 TALLOC_CTX *frame = talloc_stackframe();
1415 /* Check that all is ok first ... */
1417 if (!context || !context->internal->initialized) {
1419 errno = EINVAL;
1420 TALLOC_FREE(frame);
1421 return -1;
1425 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1427 errno = EBADF;
1428 TALLOC_FREE(frame);
1429 return -1;
1433 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1435 errno = ENOTDIR;
1436 TALLOC_FREE(frame);
1437 return -1;
1442 * Now, retrieve the number of entries that will fit in what was passed
1443 * We have to figure out if the info is in the list, or we need to
1444 * send a request to the server to get the info.
1447 while ((dirlist = dir->dir_next)) {
1448 int ret;
1449 struct smbc_dirent *dirent;
1450 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1452 if (!dirlist->dirent) {
1454 errno = ENOENT; /* Bad error */
1455 TALLOC_FREE(frame);
1456 return -1;
1460 /* Do urlencoding of next entry, if so selected */
1461 dirent = &context->internal->dirent;
1462 maxlen = sizeof(context->internal->_dirent_name);
1463 ret = smbc_readdir_internal(context, dirent,
1464 dirlist->dirent, maxlen);
1465 if (ret == -1) {
1466 errno = EINVAL;
1467 TALLOC_FREE(frame);
1468 return -1;
1471 reqd = dirent->dirlen;
1473 if (rem < reqd) {
1475 if (rem < count) { /* We managed to copy something */
1477 errno = 0;
1478 TALLOC_FREE(frame);
1479 return count - rem;
1482 else { /* Nothing copied ... */
1484 errno = EINVAL; /* Not enough space ... */
1485 TALLOC_FREE(frame);
1486 return -1;
1492 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1494 currentEntry->comment = &currentEntry->name[0] +
1495 dirent->namelen + 1;
1497 ndir += reqd;
1498 rem -= reqd;
1500 /* Try and align the struct for the next entry
1501 on a valid pointer boundary by appending zeros */
1502 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1503 *ndir = '\0';
1504 rem--;
1505 ndir++;
1506 currentEntry->dirlen++;
1509 dir->dir_next = dirlist = dirlist -> next;
1512 * If we are returning file entries, we
1513 * have a duplicate list in dirplus.
1515 * Update dirplus_next also so readdir and
1516 * readdirplus are kept in sync.
1518 if (dir->dirplus_list != NULL) {
1519 dir->dirplus_next = dir->dirplus_next->next;
1523 TALLOC_FREE(frame);
1525 if (rem == count)
1526 return 0;
1527 else
1528 return count - rem;
1533 * Routine to create a directory ...
1537 SMBC_mkdir_ctx(SMBCCTX *context,
1538 const char *fname,
1539 mode_t mode)
1541 SMBCSRV *srv = NULL;
1542 char *server = NULL;
1543 char *share = NULL;
1544 char *user = NULL;
1545 char *password = NULL;
1546 char *workgroup = NULL;
1547 char *path = NULL;
1548 char *targetpath = NULL;
1549 uint16_t port = 0;
1550 struct cli_state *targetcli = NULL;
1551 struct cli_credentials *creds = NULL;
1552 TALLOC_CTX *frame = talloc_stackframe();
1553 NTSTATUS status;
1555 if (!context || !context->internal->initialized) {
1556 errno = EINVAL;
1557 TALLOC_FREE(frame);
1558 return -1;
1561 if (!fname) {
1562 errno = EINVAL;
1563 TALLOC_FREE(frame);
1564 return -1;
1567 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1569 if (SMBC_parse_path(frame,
1570 context,
1571 fname,
1572 &workgroup,
1573 &server,
1574 &port,
1575 &share,
1576 &path,
1577 &user,
1578 &password,
1579 NULL)) {
1580 errno = EINVAL;
1581 TALLOC_FREE(frame);
1582 return -1;
1585 if (!user || user[0] == (char)0) {
1586 user = talloc_strdup(frame, smbc_getUser(context));
1587 if (!user) {
1588 errno = ENOMEM;
1589 TALLOC_FREE(frame);
1590 return -1;
1594 srv = SMBC_server(frame, context, True,
1595 server, port, share, &workgroup, &user, &password);
1597 if (!srv) {
1599 TALLOC_FREE(frame);
1600 return -1; /* errno set by SMBC_server */
1604 creds = context->internal->creds;
1606 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1607 status = cli_resolve_path(frame, "",
1608 creds,
1609 srv->cli, path, &targetcli, &targetpath);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 d_printf("Could not resolve %s\n", path);
1612 errno = ENOENT;
1613 TALLOC_FREE(frame);
1614 return -1;
1616 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1618 status = cli_mkdir(targetcli, targetpath);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 TALLOC_FREE(frame);
1621 errno = cli_status_to_errno(status);
1622 return -1;
1626 TALLOC_FREE(frame);
1627 return 0;
1632 * Our list function simply checks to see if a directory is not empty
1635 static NTSTATUS
1636 rmdir_list_fn(struct file_info *finfo,
1637 const char *mask,
1638 void *state)
1640 if (strncmp(finfo->name, ".", 1) != 0 &&
1641 strncmp(finfo->name, "..", 2) != 0) {
1642 bool *smbc_rmdir_dirempty = (bool *)state;
1643 *smbc_rmdir_dirempty = false;
1645 return NT_STATUS_OK;
1649 * Routine to remove a directory
1653 SMBC_rmdir_ctx(SMBCCTX *context,
1654 const char *fname)
1656 SMBCSRV *srv = NULL;
1657 char *server = NULL;
1658 char *share = NULL;
1659 char *user = NULL;
1660 char *password = NULL;
1661 char *workgroup = NULL;
1662 char *path = NULL;
1663 char *targetpath = NULL;
1664 uint16_t port = 0;
1665 struct cli_state *targetcli = NULL;
1666 struct cli_credentials *creds = NULL;
1667 TALLOC_CTX *frame = talloc_stackframe();
1668 NTSTATUS status;
1670 if (!context || !context->internal->initialized) {
1671 errno = EINVAL;
1672 TALLOC_FREE(frame);
1673 return -1;
1676 if (!fname) {
1677 errno = EINVAL;
1678 TALLOC_FREE(frame);
1679 return -1;
1682 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1684 if (SMBC_parse_path(frame,
1685 context,
1686 fname,
1687 &workgroup,
1688 &server,
1689 &port,
1690 &share,
1691 &path,
1692 &user,
1693 &password,
1694 NULL)) {
1695 errno = EINVAL;
1696 TALLOC_FREE(frame);
1697 return -1;
1700 if (!user || user[0] == (char)0) {
1701 user = talloc_strdup(frame, smbc_getUser(context));
1702 if (!user) {
1703 errno = ENOMEM;
1704 TALLOC_FREE(frame);
1705 return -1;
1709 srv = SMBC_server(frame, context, True,
1710 server, port, share, &workgroup, &user, &password);
1712 if (!srv) {
1714 TALLOC_FREE(frame);
1715 return -1; /* errno set by SMBC_server */
1719 creds = context->internal->creds;
1721 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1722 status = cli_resolve_path(frame, "",
1723 creds,
1724 srv->cli, path, &targetcli, &targetpath);
1725 if (!NT_STATUS_IS_OK(status)) {
1726 d_printf("Could not resolve %s\n", path);
1727 errno = ENOENT;
1728 TALLOC_FREE(frame);
1729 return -1;
1731 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1733 status = cli_rmdir(targetcli, targetpath);
1735 if (!NT_STATUS_IS_OK(status)) {
1737 errno = cli_status_to_errno(status);
1739 if (errno == EACCES) { /* Check if the dir empty or not */
1741 /* Local storage to avoid buffer overflows */
1742 char *lpath;
1743 bool smbc_rmdir_dirempty = true;
1745 lpath = talloc_asprintf(frame, "%s\\*",
1746 targetpath);
1747 if (!lpath) {
1748 errno = ENOMEM;
1749 TALLOC_FREE(frame);
1750 return -1;
1753 status = cli_list(targetcli, lpath,
1754 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1755 rmdir_list_fn,
1756 &smbc_rmdir_dirempty);
1758 if (!NT_STATUS_IS_OK(status)) {
1759 /* Fix errno to ignore latest error ... */
1760 DBG_INFO("cli_list returned an error: %s\n",
1761 nt_errstr(status));
1762 errno = EACCES;
1766 if (smbc_rmdir_dirempty)
1767 errno = EACCES;
1768 else
1769 errno = ENOTEMPTY;
1773 TALLOC_FREE(frame);
1774 return -1;
1778 TALLOC_FREE(frame);
1779 return 0;
1784 * Routine to return the current directory position
1787 off_t
1788 SMBC_telldir_ctx(SMBCCTX *context,
1789 SMBCFILE *dir)
1791 TALLOC_CTX *frame = talloc_stackframe();
1793 if (!context || !context->internal->initialized) {
1795 errno = EINVAL;
1796 TALLOC_FREE(frame);
1797 return -1;
1801 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1803 errno = EBADF;
1804 TALLOC_FREE(frame);
1805 return -1;
1809 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1811 errno = ENOTDIR;
1812 TALLOC_FREE(frame);
1813 return -1;
1817 /* See if we're already at the end. */
1818 if (dir->dir_next == NULL) {
1819 /* We are. */
1820 TALLOC_FREE(frame);
1821 return -1;
1825 * We return the pointer here as the offset
1827 TALLOC_FREE(frame);
1828 return (off_t)(long)dir->dir_next->dirent;
1832 * A routine to run down the list and see if the entry is OK
1833 * Modifies the dir list and the dirplus list (if it exists)
1834 * to point at the correct next entry on success.
1837 static bool update_dir_ents(SMBCFILE *dir, struct smbc_dirent *dirent)
1839 struct smbc_dir_list *tmp_dir = dir->dir_list;
1840 struct smbc_dirplus_list *tmp_dirplus = dir->dirplus_list;
1843 * Run down the list looking for what we want.
1844 * If we're enumerating files both dir_list
1845 * and dirplus_list contain the same entry
1846 * list, as they were seeded from the same
1847 * cli_list callback.
1849 * If we're enumerating servers then
1850 * dirplus_list will be NULL, so don't
1851 * update in that case.
1854 while (tmp_dir != NULL) {
1855 if (tmp_dir->dirent == dirent) {
1856 dir->dir_next = tmp_dir;
1857 if (tmp_dirplus != NULL) {
1858 dir->dirplus_next = tmp_dirplus;
1860 return true;
1862 tmp_dir = tmp_dir->next;
1863 if (tmp_dirplus != NULL) {
1864 tmp_dirplus = tmp_dirplus->next;
1867 return false;
1871 * Routine to seek on a directory
1875 SMBC_lseekdir_ctx(SMBCCTX *context,
1876 SMBCFILE *dir,
1877 off_t offset)
1879 long int l_offset = offset; /* Handle problems of size */
1880 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1881 TALLOC_CTX *frame = talloc_stackframe();
1882 bool ok;
1884 if (!context || !context->internal->initialized) {
1886 errno = EINVAL;
1887 TALLOC_FREE(frame);
1888 return -1;
1892 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1894 errno = ENOTDIR;
1895 TALLOC_FREE(frame);
1896 return -1;
1900 /* Now, check what we were passed and see if it is OK ... */
1902 if (dirent == NULL) { /* Seek to the beginning of the list */
1904 dir->dir_next = dir->dir_list;
1906 /* Do the same for dirplus. */
1907 dir->dirplus_next = dir->dirplus_list;
1909 TALLOC_FREE(frame);
1910 return 0;
1914 if (offset == -1) { /* Seek to the end of the list */
1915 dir->dir_next = NULL;
1917 /* Do the same for dirplus. */
1918 dir->dirplus_next = NULL;
1920 TALLOC_FREE(frame);
1921 return 0;
1925 * Run down the list and make sure that the entry is OK.
1926 * Update the position of both dir and dirplus lists.
1929 ok = update_dir_ents(dir, dirent);
1930 if (!ok) {
1931 errno = EINVAL; /* Bad entry */
1932 TALLOC_FREE(frame);
1933 return -1;
1936 TALLOC_FREE(frame);
1937 return 0;
1941 * Routine to fstat a dir
1945 SMBC_fstatdir_ctx(SMBCCTX *context,
1946 SMBCFILE *dir,
1947 struct stat *st)
1950 if (!context || !context->internal->initialized) {
1952 errno = EINVAL;
1953 return -1;
1956 /* No code yet ... */
1957 return 0;
1961 SMBC_chmod_ctx(SMBCCTX *context,
1962 const char *fname,
1963 mode_t newmode)
1965 SMBCSRV *srv = NULL;
1966 char *server = NULL;
1967 char *share = NULL;
1968 char *user = NULL;
1969 char *password = NULL;
1970 char *workgroup = NULL;
1971 char *targetpath = NULL;
1972 struct cli_state *targetcli = NULL;
1973 char *path = NULL;
1974 uint32_t attr;
1975 uint16_t port = 0;
1976 struct cli_credentials *creds = NULL;
1977 TALLOC_CTX *frame = talloc_stackframe();
1978 NTSTATUS status;
1980 if (!context || !context->internal->initialized) {
1982 errno = EINVAL; /* Best I can think of ... */
1983 TALLOC_FREE(frame);
1984 return -1;
1987 if (!fname) {
1988 errno = EINVAL;
1989 TALLOC_FREE(frame);
1990 return -1;
1993 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1995 if (SMBC_parse_path(frame,
1996 context,
1997 fname,
1998 &workgroup,
1999 &server,
2000 &port,
2001 &share,
2002 &path,
2003 &user,
2004 &password,
2005 NULL)) {
2006 errno = EINVAL;
2007 TALLOC_FREE(frame);
2008 return -1;
2011 if (!user || user[0] == (char)0) {
2012 user = talloc_strdup(frame, smbc_getUser(context));
2013 if (!user) {
2014 errno = ENOMEM;
2015 TALLOC_FREE(frame);
2016 return -1;
2020 srv = SMBC_server(frame, context, True,
2021 server, port, share, &workgroup, &user, &password);
2023 if (!srv) {
2024 TALLOC_FREE(frame);
2025 return -1; /* errno set by SMBC_server */
2028 creds = context->internal->creds;
2030 /*d_printf(">>>unlink: resolving %s\n", path);*/
2031 status = cli_resolve_path(frame, "",
2032 creds,
2033 srv->cli, path, &targetcli, &targetpath);
2034 if (!NT_STATUS_IS_OK(status)) {
2035 d_printf("Could not resolve %s\n", path);
2036 errno = ENOENT;
2037 TALLOC_FREE(frame);
2038 return -1;
2041 attr = 0;
2043 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) attr |= FILE_ATTRIBUTE_READONLY;
2044 if ((newmode & S_IXUSR) && lp_map_archive(-1)) attr |= FILE_ATTRIBUTE_ARCHIVE;
2045 if ((newmode & S_IXGRP) && lp_map_system(-1)) attr |= FILE_ATTRIBUTE_SYSTEM;
2046 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) attr |= FILE_ATTRIBUTE_HIDDEN;
2048 status = cli_setatr(targetcli, targetpath, attr, 0);
2049 if (!NT_STATUS_IS_OK(status)) {
2050 TALLOC_FREE(frame);
2051 errno = cli_status_to_errno(status);
2052 return -1;
2055 TALLOC_FREE(frame);
2056 return 0;
2060 SMBC_utimes_ctx(SMBCCTX *context,
2061 const char *fname,
2062 struct timeval *tbuf)
2064 SMBCSRV *srv = NULL;
2065 char *server = NULL;
2066 char *share = NULL;
2067 char *user = NULL;
2068 char *password = NULL;
2069 char *workgroup = NULL;
2070 char *path = NULL;
2071 struct timespec access_time, write_time;
2072 uint16_t port = 0;
2073 TALLOC_CTX *frame = talloc_stackframe();
2074 bool ok;
2076 if (!context || !context->internal->initialized) {
2078 errno = EINVAL; /* Best I can think of ... */
2079 TALLOC_FREE(frame);
2080 return -1;
2083 if (!fname) {
2084 errno = EINVAL;
2085 TALLOC_FREE(frame);
2086 return -1;
2089 if (tbuf == NULL) {
2090 access_time = write_time = timespec_current();
2091 } else {
2092 access_time = convert_timeval_to_timespec(tbuf[0]);
2093 write_time = convert_timeval_to_timespec(tbuf[1]);
2096 if (DEBUGLVL(4)) {
2097 struct timeval_buf abuf, wbuf;
2099 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2100 fname,
2101 timespec_string_buf(&access_time, false, &abuf),
2102 timespec_string_buf(&write_time, false, &wbuf));
2105 if (SMBC_parse_path(frame,
2106 context,
2107 fname,
2108 &workgroup,
2109 &server,
2110 &port,
2111 &share,
2112 &path,
2113 &user,
2114 &password,
2115 NULL)) {
2116 errno = EINVAL;
2117 TALLOC_FREE(frame);
2118 return -1;
2121 if (!user || user[0] == (char)0) {
2122 user = talloc_strdup(frame, smbc_getUser(context));
2123 if (!user) {
2124 errno = ENOMEM;
2125 TALLOC_FREE(frame);
2126 return -1;
2130 srv = SMBC_server(frame, context, True,
2131 server, port, share, &workgroup, &user, &password);
2133 if (!srv) {
2134 TALLOC_FREE(frame);
2135 return -1; /* errno set by SMBC_server */
2138 ok = SMBC_setatr(
2139 context,
2140 srv,
2141 path,
2142 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2143 access_time,
2144 write_time,
2145 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2147 if (!ok) {
2148 TALLOC_FREE(frame);
2149 return -1; /* errno set by SMBC_setatr */
2152 TALLOC_FREE(frame);
2153 return 0;
2157 * Routine to unlink() a file
2161 SMBC_unlink_ctx(SMBCCTX *context,
2162 const char *fname)
2164 char *server = NULL;
2165 char *share = NULL;
2166 char *user = NULL;
2167 char *password = NULL;
2168 char *workgroup = NULL;
2169 char *path = NULL;
2170 char *targetpath = NULL;
2171 uint16_t port = 0;
2172 struct cli_state *targetcli = NULL;
2173 SMBCSRV *srv = NULL;
2174 struct cli_credentials *creds = NULL;
2175 TALLOC_CTX *frame = talloc_stackframe();
2176 NTSTATUS status;
2178 if (!context || !context->internal->initialized) {
2180 errno = EINVAL; /* Best I can think of ... */
2181 TALLOC_FREE(frame);
2182 return -1;
2186 if (!fname) {
2187 errno = EINVAL;
2188 TALLOC_FREE(frame);
2189 return -1;
2193 if (SMBC_parse_path(frame,
2194 context,
2195 fname,
2196 &workgroup,
2197 &server,
2198 &port,
2199 &share,
2200 &path,
2201 &user,
2202 &password,
2203 NULL)) {
2204 errno = EINVAL;
2205 TALLOC_FREE(frame);
2206 return -1;
2209 if (!user || user[0] == (char)0) {
2210 user = talloc_strdup(frame, smbc_getUser(context));
2211 if (!user) {
2212 errno = ENOMEM;
2213 TALLOC_FREE(frame);
2214 return -1;
2218 srv = SMBC_server(frame, context, True,
2219 server, port, share, &workgroup, &user, &password);
2221 if (!srv) {
2222 TALLOC_FREE(frame);
2223 return -1; /* SMBC_server sets errno */
2227 creds = context->internal->creds;
2229 /*d_printf(">>>unlink: resolving %s\n", path);*/
2230 status = cli_resolve_path(frame, "",
2231 creds,
2232 srv->cli, path, &targetcli, &targetpath);
2233 if (!NT_STATUS_IS_OK(status)) {
2234 d_printf("Could not resolve %s\n", path);
2235 errno = ENOENT;
2236 TALLOC_FREE(frame);
2237 return -1;
2239 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2241 status = cli_unlink(
2242 targetcli,
2243 targetpath,
2244 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2246 if (!NT_STATUS_IS_OK(status)) {
2248 errno = cli_status_to_errno(status);
2250 if (errno == EACCES) { /* Check if the file is a directory */
2252 int saverr = errno;
2253 struct stat sb = {0};
2255 status = SMBC_getatr(context, srv, path, &sb);
2256 if (!NT_STATUS_IS_OK(status)) {
2257 /* Hmmm, bad error ... What? */
2259 TALLOC_FREE(frame);
2260 errno = cli_status_to_errno(status);
2261 return -1;
2264 else {
2266 if (S_ISDIR(sb.st_mode))
2267 errno = EISDIR;
2268 else
2269 errno = saverr; /* Restore this */
2274 TALLOC_FREE(frame);
2275 return -1;
2279 TALLOC_FREE(frame);
2280 return 0; /* Success ... */
2285 * Routine to rename() a file
2289 SMBC_rename_ctx(SMBCCTX *ocontext,
2290 const char *oname,
2291 SMBCCTX *ncontext,
2292 const char *nname)
2294 char *server1 = NULL;
2295 char *share1 = NULL;
2296 char *server2 = NULL;
2297 char *share2 = NULL;
2298 char *user1 = NULL;
2299 char *user2 = NULL;
2300 char *password1 = NULL;
2301 char *password2 = NULL;
2302 char *workgroup = NULL;
2303 char *path1 = NULL;
2304 char *path2 = NULL;
2305 char *targetpath1 = NULL;
2306 char *targetpath2 = NULL;
2307 struct cli_state *targetcli1 = NULL;
2308 struct cli_state *targetcli2 = NULL;
2309 SMBCSRV *srv = NULL;
2310 uint16_t port1 = 0;
2311 uint16_t port2 = 0;
2312 struct cli_credentials *ocreds = NULL;
2313 struct cli_credentials *ncreds = NULL;
2314 TALLOC_CTX *frame = talloc_stackframe();
2315 NTSTATUS status;
2317 if (!ocontext || !ncontext ||
2318 !ocontext->internal->initialized ||
2319 !ncontext->internal->initialized) {
2321 errno = EINVAL; /* Best I can think of ... */
2322 TALLOC_FREE(frame);
2323 return -1;
2326 if (!oname || !nname) {
2327 errno = EINVAL;
2328 TALLOC_FREE(frame);
2329 return -1;
2332 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
2334 if (SMBC_parse_path(frame,
2335 ocontext,
2336 oname,
2337 &workgroup,
2338 &server1,
2339 &port1,
2340 &share1,
2341 &path1,
2342 &user1,
2343 &password1,
2344 NULL)) {
2345 errno = EINVAL;
2346 TALLOC_FREE(frame);
2347 return -1;
2350 if (!user1 || user1[0] == (char)0) {
2351 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
2352 if (!user1) {
2353 errno = ENOMEM;
2354 TALLOC_FREE(frame);
2355 return -1;
2359 if (SMBC_parse_path(frame,
2360 ncontext,
2361 nname,
2362 NULL,
2363 &server2,
2364 &port2,
2365 &share2,
2366 &path2,
2367 &user2,
2368 &password2,
2369 NULL)) {
2370 errno = EINVAL;
2371 TALLOC_FREE(frame);
2372 return -1;
2375 if (!user2 || user2[0] == (char)0) {
2376 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
2377 if (!user2) {
2378 errno = ENOMEM;
2379 TALLOC_FREE(frame);
2380 return -1;
2384 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2385 strcmp(user1, user2)) {
2386 /* Can't rename across file systems, or users?? */
2387 errno = EXDEV;
2388 TALLOC_FREE(frame);
2389 return -1;
2392 srv = SMBC_server(frame, ocontext, True,
2393 server1, port1, share1, &workgroup, &user1, &password1);
2394 if (!srv) {
2395 TALLOC_FREE(frame);
2396 return -1;
2400 /* set the credentials to make DFS work */
2401 smbc_set_credentials_with_fallback(ocontext,
2402 workgroup,
2403 user1,
2404 password1);
2406 /*d_printf(">>>rename: resolving %s\n", path1);*/
2407 ocreds = ocontext->internal->creds;
2409 status = cli_resolve_path(frame, "",
2410 ocreds,
2411 srv->cli, path1, &targetcli1, &targetpath1);
2412 if (!NT_STATUS_IS_OK(status)) {
2413 d_printf("Could not resolve %s\n", path1);
2414 errno = ENOENT;
2415 TALLOC_FREE(frame);
2416 return -1;
2419 /* set the credentials to make DFS work */
2420 smbc_set_credentials_with_fallback(ncontext,
2421 workgroup,
2422 user2,
2423 password2);
2425 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2426 /*d_printf(">>>rename: resolving %s\n", path2);*/
2427 ncreds = ncontext->internal->creds;
2429 status = cli_resolve_path(frame, "",
2430 ncreds,
2431 srv->cli, path2, &targetcli2, &targetpath2);
2432 if (!NT_STATUS_IS_OK(status)) {
2433 d_printf("Could not resolve %s\n", path2);
2434 errno = ENOENT;
2435 TALLOC_FREE(frame);
2436 return -1;
2438 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2440 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2441 strcmp(targetcli1->share, targetcli2->share))
2443 /* can't rename across file systems */
2444 errno = EXDEV;
2445 TALLOC_FREE(frame);
2446 return -1;
2449 status = cli_rename(targetcli1, targetpath1, targetpath2, false);
2450 if (!NT_STATUS_IS_OK(status)) {
2451 int eno = cli_status_to_errno(status);
2453 if (eno != EEXIST ||
2454 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2,
2455 FILE_ATTRIBUTE_SYSTEM |
2456 FILE_ATTRIBUTE_HIDDEN)) ||
2457 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
2458 targetpath2, false))) {
2460 errno = eno;
2461 TALLOC_FREE(frame);
2462 return -1;
2467 TALLOC_FREE(frame);
2468 return 0; /* Success */
2471 struct smbc_notify_cb_state {
2472 struct tevent_context *ev;
2473 struct cli_state *cli;
2474 uint16_t fnum;
2475 bool recursive;
2476 uint32_t completion_filter;
2477 unsigned callback_timeout_ms;
2478 smbc_notify_callback_fn cb;
2479 void *private_data;
2482 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2483 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2485 static struct tevent_req *smbc_notify_cb_send(
2486 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2487 uint16_t fnum, bool recursive, uint32_t completion_filter,
2488 unsigned callback_timeout_ms,
2489 smbc_notify_callback_fn cb, void *private_data)
2491 struct tevent_req *req, *subreq;
2492 struct smbc_notify_cb_state *state;
2494 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2495 if (req == NULL) {
2496 return NULL;
2498 state->ev = ev;
2499 state->cli = cli;
2500 state->fnum = fnum;
2501 state->recursive = recursive;
2502 state->completion_filter = completion_filter;
2503 state->callback_timeout_ms = callback_timeout_ms;
2504 state->cb = cb;
2505 state->private_data = private_data;
2507 subreq = cli_notify_send(
2508 state, state->ev, state->cli, state->fnum, 1000,
2509 state->completion_filter, state->recursive);
2510 if (tevent_req_nomem(subreq, req)) {
2511 return tevent_req_post(req, ev);
2513 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2515 if (state->callback_timeout_ms == 0) {
2516 return req;
2519 subreq = tevent_wakeup_send(
2520 state, state->ev,
2521 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2522 state->callback_timeout_ms*1000));
2523 if (tevent_req_nomem(subreq, req)) {
2524 return tevent_req_post(req, ev);
2526 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2528 return req;
2531 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2533 struct tevent_req *req = tevent_req_callback_data(
2534 subreq, struct tevent_req);
2535 struct smbc_notify_cb_state *state = tevent_req_data(
2536 req, struct smbc_notify_cb_state);
2537 uint32_t num_changes;
2538 struct notify_change *changes;
2539 NTSTATUS status;
2540 int cb_ret;
2542 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2543 TALLOC_FREE(subreq);
2544 if (tevent_req_nterror(req, status)) {
2545 return;
2549 struct smbc_notify_callback_action actions[num_changes];
2550 uint32_t i;
2552 for (i=0; i<num_changes; i++) {
2553 actions[i].action = changes[i].action;
2554 actions[i].filename = changes[i].name;
2557 cb_ret = state->cb(actions, num_changes, state->private_data);
2560 TALLOC_FREE(changes);
2562 if (cb_ret != 0) {
2563 tevent_req_done(req);
2564 return;
2567 subreq = cli_notify_send(
2568 state, state->ev, state->cli, state->fnum, 1000,
2569 state->completion_filter, state->recursive);
2570 if (tevent_req_nomem(subreq, req)) {
2571 return;
2573 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2576 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2578 struct tevent_req *req = tevent_req_callback_data(
2579 subreq, struct tevent_req);
2580 struct smbc_notify_cb_state *state = tevent_req_data(
2581 req, struct smbc_notify_cb_state);
2582 int cb_ret;
2583 bool ok;
2585 ok = tevent_wakeup_recv(subreq);
2586 TALLOC_FREE(subreq);
2587 if (!ok) {
2588 tevent_req_oom(req);
2589 return;
2592 cb_ret = state->cb(NULL, 0, state->private_data);
2593 if (cb_ret != 0) {
2594 tevent_req_done(req);
2595 return;
2598 subreq = tevent_wakeup_send(
2599 state, state->ev,
2600 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2601 state->callback_timeout_ms*1000));
2602 if (tevent_req_nomem(subreq, req)) {
2603 return;
2605 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2608 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2610 return tevent_req_simple_recv_ntstatus(req);
2613 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2614 bool recursive, uint32_t completion_filter,
2615 unsigned callback_timeout_ms,
2616 smbc_notify_callback_fn cb, void *private_data)
2618 TALLOC_CTX *frame = talloc_stackframe();
2619 struct tevent_context *ev;
2620 struct tevent_req *req;
2621 NTSTATUS status = NT_STATUS_NO_MEMORY;
2623 ev = samba_tevent_context_init(frame);
2624 if (ev == NULL) {
2625 goto fail;
2627 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2628 completion_filter,
2629 callback_timeout_ms, cb, private_data);
2630 if (req == NULL) {
2631 goto fail;
2633 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2634 goto fail;
2636 status = smbc_notify_cb_recv(req);
2637 TALLOC_FREE(req);
2638 fail:
2639 TALLOC_FREE(frame);
2640 return status;
2644 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2645 uint32_t completion_filter, unsigned callback_timeout_ms,
2646 smbc_notify_callback_fn cb, void *private_data)
2648 TALLOC_CTX *frame = talloc_stackframe();
2649 struct cli_state *cli;
2650 char *server = NULL;
2651 char *share = NULL;
2652 char *user = NULL;
2653 char *password = NULL;
2654 char *options = NULL;
2655 char *workgroup = NULL;
2656 char *path = NULL;
2657 uint16_t port;
2658 NTSTATUS status;
2659 uint16_t fnum;
2661 if ((context == NULL) || !context->internal->initialized) {
2662 TALLOC_FREE(frame);
2663 errno = EINVAL;
2664 return -1;
2666 if (!SMBC_dlist_contains(context->internal->files, dir)) {
2667 TALLOC_FREE(frame);
2668 errno = EBADF;
2669 return -1;
2672 if (SMBC_parse_path(frame,
2673 context,
2674 dir->fname,
2675 &workgroup,
2676 &server,
2677 &port,
2678 &share,
2679 &path,
2680 &user,
2681 &password,
2682 &options)) {
2683 DEBUG(4, ("no valid path\n"));
2684 TALLOC_FREE(frame);
2685 errno = EINVAL + 8194;
2686 return -1;
2689 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2690 "path='%s' options='%s'\n",
2691 dir->fname, server, share, path, options));
2693 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2694 (int)recursive, completion_filter));
2696 cli = dir->srv->cli;
2697 status = cli_ntcreate(
2698 cli, path, 0, FILE_READ_DATA, 0,
2699 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2700 FILE_OPEN, 0, 0, &fnum, NULL);
2701 if (!NT_STATUS_IS_OK(status)) {
2702 TALLOC_FREE(frame);
2703 errno = cli_status_to_errno(status);
2704 return -1;
2707 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2708 callback_timeout_ms, cb, private_data);
2709 if (!NT_STATUS_IS_OK(status)) {
2710 cli_close(cli, fnum);
2711 TALLOC_FREE(frame);
2712 errno = cli_status_to_errno(status);
2713 return -1;
2716 cli_close(cli, fnum);
2718 TALLOC_FREE(frame);
2719 return 0;