ctdb-failover: Split statd_callout add-client/del-client
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob6e4dd329616a941189e4a502a3af434fcffc97ee
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 if (!cli_NetServerEnum(srv->cli,
704 workgroup,
705 SV_TYPE_DOMAIN_ENUM,
706 list_unique_wg_fn,
707 (void *)dir)) {
708 continue;
712 TALLOC_FREE(ip_list);
713 } else {
715 * Server not an empty string ... Check the rest and see what
716 * gives
718 if (*share == '\0') {
719 if (*path != '\0') {
721 /* Should not have empty share with path */
722 if (dir) {
723 SAFE_FREE(dir->fname);
724 SAFE_FREE(dir);
726 TALLOC_FREE(frame);
727 errno = EINVAL + 8197;
728 return NULL;
733 * We don't know if <server> is really a server name
734 * or is a workgroup/domain name. If we already have
735 * a server structure for it, we'll use it.
736 * Otherwise, check to see if <server><1D>,
737 * <server><1B>, or <server><20> translates. We check
738 * to see if <server> is an IP address first.
742 * See if we have an existing server. Do not
743 * establish a connection if one does not already
744 * exist.
746 srv = SMBC_server(frame, context, False,
747 server, port, "IPC$",
748 &workgroup, &user, &password);
751 * If no existing server and not an IP addr, look for
752 * LMB or DMB
754 if (!srv &&
755 !is_ipaddress(server) &&
756 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
757 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
759 * "server" is actually a workgroup name,
760 * not a server. Make this clear.
762 char *wgroup = server;
763 fstring buserver;
765 dir->dir_type = SMBC_SERVER;
768 * Get the backup list ...
770 if (!name_status_find(wgroup, 0, 0,
771 &rem_ss, buserver)) {
772 char addr[INET6_ADDRSTRLEN];
774 print_sockaddr(addr, sizeof(addr), &rem_ss);
775 DEBUG(0,("Could not get name of "
776 "local/domain master browser "
777 "for workgroup %s from "
778 "address %s\n",
779 wgroup,
780 addr));
781 if (dir) {
782 SAFE_FREE(dir->fname);
783 SAFE_FREE(dir);
785 TALLOC_FREE(frame);
786 errno = EPERM;
787 return NULL;
792 * Get a connection to IPC$ on the server if
793 * we do not already have one
795 srv = SMBC_server(frame, context, True,
796 buserver, port, "IPC$",
797 &workgroup,
798 &user, &password);
799 if (!srv) {
800 DEBUG(0, ("got no contact to IPC$\n"));
801 if (dir) {
802 SAFE_FREE(dir->fname);
803 SAFE_FREE(dir);
805 TALLOC_FREE(frame);
806 return NULL;
810 dir->srv = srv;
812 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
813 if (dir) {
814 SAFE_FREE(dir->fname);
815 SAFE_FREE(dir);
817 TALLOC_FREE(frame);
818 return NULL;
821 /* Now, list the servers ... */
822 if (!cli_NetServerEnum(srv->cli, wgroup,
823 0x0000FFFE, list_fn,
824 (void *)dir)) {
826 if (dir) {
827 SAFE_FREE(dir->fname);
828 SAFE_FREE(dir);
830 TALLOC_FREE(frame);
831 return NULL;
833 } else if (srv ||
834 (resolve_name(server, &rem_ss, 0x20, false))) {
835 NTSTATUS status;
838 * If we hadn't found the server, get one now
840 if (!srv) {
841 srv = SMBC_server(frame, context, True,
842 server, port, "IPC$",
843 &workgroup,
844 &user, &password);
847 if (!srv) {
848 if (dir) {
849 SAFE_FREE(dir->fname);
850 SAFE_FREE(dir);
852 TALLOC_FREE(frame);
853 return NULL;
857 dir->dir_type = SMBC_FILE_SHARE;
858 dir->srv = srv;
860 /* List the shares ... */
862 status = net_share_enum_rpc(srv->cli,
863 list_fn,
864 (void *)dir);
865 if (!NT_STATUS_IS_OK(status) &&
866 smbXcli_conn_protocol(srv->cli->conn) <=
867 PROTOCOL_NT1) {
869 * Only call cli_RNetShareEnum()
870 * on SMB1 connections, not SMB2+.
872 int rc = cli_RNetShareEnum(srv->cli,
873 list_fn,
874 (void *)dir);
875 if (rc != 0) {
876 status = cli_nt_error(srv->cli);
877 } else {
878 status = NT_STATUS_OK;
881 if (!NT_STATUS_IS_OK(status)) {
883 * Set cli->raw_status so SMBC_errno()
884 * will correctly return the error.
886 srv->cli->raw_status = status;
887 if (dir != NULL) {
888 SAFE_FREE(dir->fname);
889 SAFE_FREE(dir);
891 TALLOC_FREE(frame);
892 errno = map_errno_from_nt_status(
893 status);
894 return NULL;
896 } else {
897 /* Neither the workgroup nor server exists */
898 errno = ECONNREFUSED;
899 if (dir) {
900 SAFE_FREE(dir->fname);
901 SAFE_FREE(dir);
903 TALLOC_FREE(frame);
904 return NULL;
908 else {
910 * The server and share are specified ... work from
911 * there ...
913 char *targetpath;
914 struct cli_state *targetcli;
915 struct cli_credentials *creds = NULL;
916 NTSTATUS status;
918 /* We connect to the server and list the directory */
919 dir->dir_type = SMBC_FILE_SHARE;
921 srv = SMBC_server(frame, context, True, server, port, share,
922 &workgroup, &user, &password);
924 if (!srv) {
925 if (dir) {
926 SAFE_FREE(dir->fname);
927 SAFE_FREE(dir);
929 TALLOC_FREE(frame);
930 return NULL;
933 dir->srv = srv;
935 /* Now, list the files ... */
937 path_len = strlen(path);
938 path = talloc_asprintf_append(path, "\\*");
939 if (!path) {
940 if (dir) {
941 SAFE_FREE(dir->fname);
942 SAFE_FREE(dir);
944 TALLOC_FREE(frame);
945 return NULL;
948 creds = context->internal->creds;
950 status = cli_resolve_path(
951 frame, "",
952 creds,
953 srv->cli, path, &targetcli, &targetpath);
954 if (!NT_STATUS_IS_OK(status)) {
955 d_printf("Could not resolve %s\n", path);
956 if (dir) {
957 SAFE_FREE(dir->fname);
958 SAFE_FREE(dir);
960 TALLOC_FREE(frame);
961 return NULL;
964 status = cli_list(targetcli, targetpath,
965 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
966 dir_list_fn, (void *)dir);
967 if (!NT_STATUS_IS_OK(status)) {
968 int saved_errno;
969 if (dir) {
970 SAFE_FREE(dir->fname);
971 SAFE_FREE(dir);
973 saved_errno = cli_status_to_errno(status);
975 if (saved_errno == EINVAL) {
976 struct stat sb = {0};
978 * See if they asked to opendir
979 * something other than a directory.
980 * If so, the converted error value we
981 * got would have been EINVAL rather
982 * than ENOTDIR.
984 path[path_len] = '\0'; /* restore original path */
986 status = SMBC_getatr(
987 context,
988 srv,
989 path,
990 &sb);
991 if (NT_STATUS_IS_OK(status) &&
992 !S_ISDIR(sb.st_mode)) {
994 /* It is. Correct the error value */
995 saved_errno = ENOTDIR;
1000 * If there was an error and the server is no
1001 * good any more...
1003 if (cli_is_error(targetcli) &&
1004 smbc_getFunctionCheckServer(context)(context, srv)) {
1006 /* ... then remove it. */
1007 if (smbc_getFunctionRemoveUnusedServer(context)(context,
1008 srv)) {
1010 * We could not remove the
1011 * server completely, remove
1012 * it from the cache so we
1013 * will not get it again. It
1014 * will be removed when the
1015 * last file/dir is closed.
1017 smbc_getFunctionRemoveCachedServer(context)(context, srv);
1021 TALLOC_FREE(frame);
1022 errno = saved_errno;
1023 return NULL;
1029 DLIST_ADD(context->internal->files, dir);
1030 TALLOC_FREE(frame);
1031 return dir;
1036 * Routine to close a directory
1040 SMBC_closedir_ctx(SMBCCTX *context,
1041 SMBCFILE *dir)
1043 TALLOC_CTX *frame = NULL;
1045 if (!context || !context->internal->initialized) {
1046 errno = EINVAL;
1047 return -1;
1050 if (dir == NULL) {
1051 return 0;
1054 frame = talloc_stackframe();
1056 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1057 errno = EBADF;
1058 TALLOC_FREE(frame);
1059 return -1;
1062 remove_dir(dir); /* Clean it up */
1063 remove_dirplus(dir);
1065 DLIST_REMOVE(context->internal->files, dir);
1067 SAFE_FREE(dir->fname);
1068 SAFE_FREE(dir); /* Free the space too */
1070 TALLOC_FREE(frame);
1071 return 0;
1075 static int
1076 smbc_readdir_internal(SMBCCTX * context,
1077 struct smbc_dirent *dest,
1078 struct smbc_dirent *src,
1079 int max_namebuf_len)
1081 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
1082 int remaining_len;
1084 /* url-encode the name. get back remaining buffer space */
1085 remaining_len =
1086 smbc_urlencode(dest->name, src->name, max_namebuf_len);
1088 /* -1 means no null termination. */
1089 if (remaining_len < 0) {
1090 return -1;
1093 /* We now know the name length */
1094 dest->namelen = strlen(dest->name);
1096 if (dest->namelen + 1 < 1) {
1097 /* Integer wrap. */
1098 return -1;
1101 if (dest->namelen + 1 >= max_namebuf_len) {
1102 /* Out of space for comment. */
1103 return -1;
1106 /* Save the pointer to the beginning of the comment */
1107 dest->comment = dest->name + dest->namelen + 1;
1109 if (remaining_len < 1) {
1110 /* No room for comment null termination. */
1111 return -1;
1114 /* Copy the comment */
1115 strlcpy(dest->comment, src->comment, remaining_len);
1117 /* Save other fields */
1118 dest->smbc_type = src->smbc_type;
1119 dest->commentlen = strlen(dest->comment);
1120 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
1121 (char *) dest);
1122 } else {
1124 /* No encoding. Just copy the entry as is. */
1125 if (src->dirlen > max_namebuf_len) {
1126 return -1;
1128 memcpy(dest, src, src->dirlen);
1129 if (src->namelen + 1 < 1) {
1130 /* Integer wrap */
1131 return -1;
1133 if (src->namelen + 1 >= max_namebuf_len) {
1134 /* Comment off the end. */
1135 return -1;
1137 dest->comment = (char *)(&dest->name + src->namelen + 1);
1139 return 0;
1143 * Routine to get a directory entry
1146 struct smbc_dirent *
1147 SMBC_readdir_ctx(SMBCCTX *context,
1148 SMBCFILE *dir)
1150 int maxlen;
1151 int ret;
1152 struct smbc_dirent *dirp, *dirent;
1153 TALLOC_CTX *frame = talloc_stackframe();
1155 /* Check that all is ok first ... */
1157 if (!context || !context->internal->initialized) {
1159 errno = EINVAL;
1160 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1161 TALLOC_FREE(frame);
1162 return NULL;
1166 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1168 errno = EBADF;
1169 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1170 TALLOC_FREE(frame);
1171 return NULL;
1175 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1177 errno = ENOTDIR;
1178 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1179 TALLOC_FREE(frame);
1180 return NULL;
1184 if (!dir->dir_next) {
1185 TALLOC_FREE(frame);
1186 return NULL;
1189 dirent = dir->dir_next->dirent;
1190 if (!dirent) {
1192 errno = ENOENT;
1193 TALLOC_FREE(frame);
1194 return NULL;
1198 dirp = &context->internal->dirent;
1199 maxlen = sizeof(context->internal->_dirent_name);
1201 ret = smbc_readdir_internal(context, dirp, dirent, maxlen);
1202 if (ret == -1) {
1203 errno = EINVAL;
1204 TALLOC_FREE(frame);
1205 return NULL;
1208 dir->dir_next = dir->dir_next->next;
1211 * If we are returning file entries, we
1212 * have a duplicate list in dirplus.
1214 * Update dirplus_next also so readdir and
1215 * readdirplus are kept in sync.
1217 if (dir->dirplus_list != NULL) {
1218 dir->dirplus_next = dir->dirplus_next->next;
1221 TALLOC_FREE(frame);
1222 return dirp;
1226 * Routine to get a directory entry with all attributes
1229 const struct libsmb_file_info *
1230 SMBC_readdirplus_ctx(SMBCCTX *context,
1231 SMBCFILE *dir)
1233 struct libsmb_file_info *smb_finfo = NULL;
1234 TALLOC_CTX *frame = talloc_stackframe();
1236 /* Check that all is ok first ... */
1238 if (context == NULL || !context->internal->initialized) {
1239 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1240 TALLOC_FREE(frame);
1241 errno = EINVAL;
1242 return NULL;
1245 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1246 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1247 TALLOC_FREE(frame);
1248 errno = EBADF;
1249 return NULL;
1252 if (dir->dirplus_next == NULL) {
1253 TALLOC_FREE(frame);
1254 return NULL;
1257 smb_finfo = dir->dirplus_next->smb_finfo;
1258 if (smb_finfo == NULL) {
1259 TALLOC_FREE(frame);
1260 errno = ENOENT;
1261 return NULL;
1263 dir->dirplus_next = dir->dirplus_next->next;
1266 * If we are returning file entries, we
1267 * have a duplicate list in dir_list
1269 * Update dir_next also so readdir and
1270 * readdirplus are kept in sync.
1272 if (dir->dir_list) {
1273 dir->dir_next = dir->dir_next->next;
1276 TALLOC_FREE(frame);
1277 return smb_finfo;
1281 * Routine to get a directory entry plus a filled in stat structure if
1282 * requested.
1285 const struct libsmb_file_info *SMBC_readdirplus2_ctx(SMBCCTX *context,
1286 SMBCFILE *dir,
1287 struct stat *st)
1289 struct libsmb_file_info *smb_finfo = NULL;
1290 struct smbc_dirplus_list *dp_list = NULL;
1291 ino_t ino;
1292 char *full_pathname = NULL;
1293 char *workgroup = NULL;
1294 char *server = NULL;
1295 uint16_t port = 0;
1296 char *share = NULL;
1297 char *path = NULL;
1298 char *user = NULL;
1299 char *password = NULL;
1300 char *options = NULL;
1301 int rc;
1302 TALLOC_CTX *frame = NULL;
1305 * Allow caller to pass in NULL for stat pointer if
1306 * required. This makes this call identical to
1307 * smbc_readdirplus().
1310 if (st == NULL) {
1311 return SMBC_readdirplus_ctx(context, dir);
1314 frame = talloc_stackframe();
1316 /* Check that all is ok first ... */
1317 if (context == NULL || !context->internal->initialized) {
1318 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1319 TALLOC_FREE(frame);
1320 errno = EINVAL;
1321 return NULL;
1324 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1325 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1326 TALLOC_FREE(frame);
1327 errno = EBADF;
1328 return NULL;
1331 dp_list = dir->dirplus_next;
1332 if (dp_list == NULL) {
1333 TALLOC_FREE(frame);
1334 return NULL;
1337 ino = (ino_t)dp_list->ino;
1339 smb_finfo = dp_list->smb_finfo;
1340 if (smb_finfo == NULL) {
1341 TALLOC_FREE(frame);
1342 errno = ENOENT;
1343 return NULL;
1346 full_pathname = talloc_asprintf(frame,
1347 "%s/%s",
1348 dir->fname,
1349 smb_finfo->name);
1350 if (full_pathname == NULL) {
1351 TALLOC_FREE(frame);
1352 errno = ENOENT;
1353 return NULL;
1356 rc = SMBC_parse_path(frame,
1357 context,
1358 full_pathname,
1359 &workgroup,
1360 &server,
1361 &port,
1362 &share,
1363 &path,
1364 &user,
1365 &password,
1366 &options);
1367 if (rc != 0) {
1368 TALLOC_FREE(frame);
1369 errno = ENOENT;
1370 return NULL;
1373 setup_stat(st,
1374 path,
1375 smb_finfo->size,
1376 smb_finfo->attrs,
1377 ino,
1378 dir->srv->dev,
1379 smb_finfo->atime_ts,
1380 smb_finfo->ctime_ts,
1381 smb_finfo->mtime_ts);
1383 TALLOC_FREE(full_pathname);
1385 dir->dirplus_next = dir->dirplus_next->next;
1388 * If we are returning file entries, we
1389 * have a duplicate list in dir_list
1391 * Update dir_next also so readdir and
1392 * readdirplus are kept in sync.
1394 if (dir->dir_list) {
1395 dir->dir_next = dir->dir_next->next;
1398 TALLOC_FREE(frame);
1399 return smb_finfo;
1403 * Routine to get directory entries
1407 SMBC_getdents_ctx(SMBCCTX *context,
1408 SMBCFILE *dir,
1409 struct smbc_dirent *dirp,
1410 int count)
1412 int rem = count;
1413 int reqd;
1414 int maxlen;
1415 char *ndir = (char *)dirp;
1416 struct smbc_dir_list *dirlist;
1417 TALLOC_CTX *frame = talloc_stackframe();
1419 /* Check that all is ok first ... */
1421 if (!context || !context->internal->initialized) {
1423 errno = EINVAL;
1424 TALLOC_FREE(frame);
1425 return -1;
1429 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1431 errno = EBADF;
1432 TALLOC_FREE(frame);
1433 return -1;
1437 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1439 errno = ENOTDIR;
1440 TALLOC_FREE(frame);
1441 return -1;
1446 * Now, retrieve the number of entries that will fit in what was passed
1447 * We have to figure out if the info is in the list, or we need to
1448 * send a request to the server to get the info.
1451 while ((dirlist = dir->dir_next)) {
1452 int ret;
1453 struct smbc_dirent *dirent;
1454 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1456 if (!dirlist->dirent) {
1458 errno = ENOENT; /* Bad error */
1459 TALLOC_FREE(frame);
1460 return -1;
1464 /* Do urlencoding of next entry, if so selected */
1465 dirent = &context->internal->dirent;
1466 maxlen = sizeof(context->internal->_dirent_name);
1467 ret = smbc_readdir_internal(context, dirent,
1468 dirlist->dirent, maxlen);
1469 if (ret == -1) {
1470 errno = EINVAL;
1471 TALLOC_FREE(frame);
1472 return -1;
1475 reqd = dirent->dirlen;
1477 if (rem < reqd) {
1479 if (rem < count) { /* We managed to copy something */
1481 errno = 0;
1482 TALLOC_FREE(frame);
1483 return count - rem;
1486 else { /* Nothing copied ... */
1488 errno = EINVAL; /* Not enough space ... */
1489 TALLOC_FREE(frame);
1490 return -1;
1496 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1498 currentEntry->comment = &currentEntry->name[0] +
1499 dirent->namelen + 1;
1501 ndir += reqd;
1502 rem -= reqd;
1504 /* Try and align the struct for the next entry
1505 on a valid pointer boundary by appending zeros */
1506 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1507 *ndir = '\0';
1508 rem--;
1509 ndir++;
1510 currentEntry->dirlen++;
1513 dir->dir_next = dirlist = dirlist -> next;
1516 * If we are returning file entries, we
1517 * have a duplicate list in dirplus.
1519 * Update dirplus_next also so readdir and
1520 * readdirplus are kept in sync.
1522 if (dir->dirplus_list != NULL) {
1523 dir->dirplus_next = dir->dirplus_next->next;
1527 TALLOC_FREE(frame);
1529 if (rem == count)
1530 return 0;
1531 else
1532 return count - rem;
1537 * Routine to create a directory ...
1541 SMBC_mkdir_ctx(SMBCCTX *context,
1542 const char *fname,
1543 mode_t mode)
1545 SMBCSRV *srv = NULL;
1546 char *server = NULL;
1547 char *share = NULL;
1548 char *user = NULL;
1549 char *password = NULL;
1550 char *workgroup = NULL;
1551 char *path = NULL;
1552 char *targetpath = NULL;
1553 uint16_t port = 0;
1554 struct cli_state *targetcli = NULL;
1555 struct cli_credentials *creds = NULL;
1556 TALLOC_CTX *frame = talloc_stackframe();
1557 NTSTATUS status;
1559 if (!context || !context->internal->initialized) {
1560 errno = EINVAL;
1561 TALLOC_FREE(frame);
1562 return -1;
1565 if (!fname) {
1566 errno = EINVAL;
1567 TALLOC_FREE(frame);
1568 return -1;
1571 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1573 if (SMBC_parse_path(frame,
1574 context,
1575 fname,
1576 &workgroup,
1577 &server,
1578 &port,
1579 &share,
1580 &path,
1581 &user,
1582 &password,
1583 NULL)) {
1584 errno = EINVAL;
1585 TALLOC_FREE(frame);
1586 return -1;
1589 if (!user || user[0] == (char)0) {
1590 user = talloc_strdup(frame, smbc_getUser(context));
1591 if (!user) {
1592 errno = ENOMEM;
1593 TALLOC_FREE(frame);
1594 return -1;
1598 srv = SMBC_server(frame, context, True,
1599 server, port, share, &workgroup, &user, &password);
1601 if (!srv) {
1603 TALLOC_FREE(frame);
1604 return -1; /* errno set by SMBC_server */
1608 creds = context->internal->creds;
1610 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1611 status = cli_resolve_path(frame, "",
1612 creds,
1613 srv->cli, path, &targetcli, &targetpath);
1614 if (!NT_STATUS_IS_OK(status)) {
1615 d_printf("Could not resolve %s\n", path);
1616 errno = ENOENT;
1617 TALLOC_FREE(frame);
1618 return -1;
1620 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1622 status = cli_mkdir(targetcli, targetpath);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 TALLOC_FREE(frame);
1625 errno = cli_status_to_errno(status);
1626 return -1;
1630 TALLOC_FREE(frame);
1631 return 0;
1636 * Our list function simply checks to see if a directory is not empty
1639 static NTSTATUS
1640 rmdir_list_fn(struct file_info *finfo,
1641 const char *mask,
1642 void *state)
1644 if (strncmp(finfo->name, ".", 1) != 0 &&
1645 strncmp(finfo->name, "..", 2) != 0) {
1646 bool *smbc_rmdir_dirempty = (bool *)state;
1647 *smbc_rmdir_dirempty = false;
1649 return NT_STATUS_OK;
1653 * Routine to remove a directory
1657 SMBC_rmdir_ctx(SMBCCTX *context,
1658 const char *fname)
1660 SMBCSRV *srv = NULL;
1661 char *server = NULL;
1662 char *share = NULL;
1663 char *user = NULL;
1664 char *password = NULL;
1665 char *workgroup = NULL;
1666 char *path = NULL;
1667 char *targetpath = NULL;
1668 uint16_t port = 0;
1669 struct cli_state *targetcli = NULL;
1670 struct cli_credentials *creds = NULL;
1671 TALLOC_CTX *frame = talloc_stackframe();
1672 NTSTATUS status;
1674 if (!context || !context->internal->initialized) {
1675 errno = EINVAL;
1676 TALLOC_FREE(frame);
1677 return -1;
1680 if (!fname) {
1681 errno = EINVAL;
1682 TALLOC_FREE(frame);
1683 return -1;
1686 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1688 if (SMBC_parse_path(frame,
1689 context,
1690 fname,
1691 &workgroup,
1692 &server,
1693 &port,
1694 &share,
1695 &path,
1696 &user,
1697 &password,
1698 NULL)) {
1699 errno = EINVAL;
1700 TALLOC_FREE(frame);
1701 return -1;
1704 if (!user || user[0] == (char)0) {
1705 user = talloc_strdup(frame, smbc_getUser(context));
1706 if (!user) {
1707 errno = ENOMEM;
1708 TALLOC_FREE(frame);
1709 return -1;
1713 srv = SMBC_server(frame, context, True,
1714 server, port, share, &workgroup, &user, &password);
1716 if (!srv) {
1718 TALLOC_FREE(frame);
1719 return -1; /* errno set by SMBC_server */
1723 creds = context->internal->creds;
1725 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1726 status = cli_resolve_path(frame, "",
1727 creds,
1728 srv->cli, path, &targetcli, &targetpath);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 d_printf("Could not resolve %s\n", path);
1731 errno = ENOENT;
1732 TALLOC_FREE(frame);
1733 return -1;
1735 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1737 status = cli_rmdir(targetcli, targetpath);
1739 if (!NT_STATUS_IS_OK(status)) {
1741 errno = cli_status_to_errno(status);
1743 if (errno == EACCES) { /* Check if the dir empty or not */
1745 /* Local storage to avoid buffer overflows */
1746 char *lpath;
1747 bool smbc_rmdir_dirempty = true;
1749 lpath = talloc_asprintf(frame, "%s\\*",
1750 targetpath);
1751 if (!lpath) {
1752 errno = ENOMEM;
1753 TALLOC_FREE(frame);
1754 return -1;
1757 status = cli_list(targetcli, lpath,
1758 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1759 rmdir_list_fn,
1760 &smbc_rmdir_dirempty);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 /* Fix errno to ignore latest error ... */
1764 DBG_INFO("cli_list returned an error: %s\n",
1765 nt_errstr(status));
1766 errno = EACCES;
1770 if (smbc_rmdir_dirempty)
1771 errno = EACCES;
1772 else
1773 errno = ENOTEMPTY;
1777 TALLOC_FREE(frame);
1778 return -1;
1782 TALLOC_FREE(frame);
1783 return 0;
1788 * Routine to return the current directory position
1791 off_t
1792 SMBC_telldir_ctx(SMBCCTX *context,
1793 SMBCFILE *dir)
1795 TALLOC_CTX *frame = talloc_stackframe();
1797 if (!context || !context->internal->initialized) {
1799 errno = EINVAL;
1800 TALLOC_FREE(frame);
1801 return -1;
1805 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1807 errno = EBADF;
1808 TALLOC_FREE(frame);
1809 return -1;
1813 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1815 errno = ENOTDIR;
1816 TALLOC_FREE(frame);
1817 return -1;
1821 /* See if we're already at the end. */
1822 if (dir->dir_next == NULL) {
1823 /* We are. */
1824 TALLOC_FREE(frame);
1825 return -1;
1829 * We return the pointer here as the offset
1831 TALLOC_FREE(frame);
1832 return (off_t)(long)dir->dir_next->dirent;
1836 * A routine to run down the list and see if the entry is OK
1837 * Modifies the dir list and the dirplus list (if it exists)
1838 * to point at the correct next entry on success.
1841 static bool update_dir_ents(SMBCFILE *dir, struct smbc_dirent *dirent)
1843 struct smbc_dir_list *tmp_dir = dir->dir_list;
1844 struct smbc_dirplus_list *tmp_dirplus = dir->dirplus_list;
1847 * Run down the list looking for what we want.
1848 * If we're enumerating files both dir_list
1849 * and dirplus_list contain the same entry
1850 * list, as they were seeded from the same
1851 * cli_list callback.
1853 * If we're enumerating servers then
1854 * dirplus_list will be NULL, so don't
1855 * update in that case.
1858 while (tmp_dir != NULL) {
1859 if (tmp_dir->dirent == dirent) {
1860 dir->dir_next = tmp_dir;
1861 if (tmp_dirplus != NULL) {
1862 dir->dirplus_next = tmp_dirplus;
1864 return true;
1866 tmp_dir = tmp_dir->next;
1867 if (tmp_dirplus != NULL) {
1868 tmp_dirplus = tmp_dirplus->next;
1871 return false;
1875 * Routine to seek on a directory
1879 SMBC_lseekdir_ctx(SMBCCTX *context,
1880 SMBCFILE *dir,
1881 off_t offset)
1883 long int l_offset = offset; /* Handle problems of size */
1884 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1885 TALLOC_CTX *frame = talloc_stackframe();
1886 bool ok;
1888 if (!context || !context->internal->initialized) {
1890 errno = EINVAL;
1891 TALLOC_FREE(frame);
1892 return -1;
1896 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1898 errno = ENOTDIR;
1899 TALLOC_FREE(frame);
1900 return -1;
1904 /* Now, check what we were passed and see if it is OK ... */
1906 if (dirent == NULL) { /* Seek to the beginning of the list */
1908 dir->dir_next = dir->dir_list;
1910 /* Do the same for dirplus. */
1911 dir->dirplus_next = dir->dirplus_list;
1913 TALLOC_FREE(frame);
1914 return 0;
1918 if (offset == -1) { /* Seek to the end of the list */
1919 dir->dir_next = NULL;
1921 /* Do the same for dirplus. */
1922 dir->dirplus_next = NULL;
1924 TALLOC_FREE(frame);
1925 return 0;
1929 * Run down the list and make sure that the entry is OK.
1930 * Update the position of both dir and dirplus lists.
1933 ok = update_dir_ents(dir, dirent);
1934 if (!ok) {
1935 errno = EINVAL; /* Bad entry */
1936 TALLOC_FREE(frame);
1937 return -1;
1940 TALLOC_FREE(frame);
1941 return 0;
1945 * Routine to fstat a dir
1949 SMBC_fstatdir_ctx(SMBCCTX *context,
1950 SMBCFILE *dir,
1951 struct stat *st)
1954 if (!context || !context->internal->initialized) {
1956 errno = EINVAL;
1957 return -1;
1960 /* No code yet ... */
1961 return 0;
1965 SMBC_chmod_ctx(SMBCCTX *context,
1966 const char *fname,
1967 mode_t newmode)
1969 SMBCSRV *srv = NULL;
1970 char *server = NULL;
1971 char *share = NULL;
1972 char *user = NULL;
1973 char *password = NULL;
1974 char *workgroup = NULL;
1975 char *targetpath = NULL;
1976 struct cli_state *targetcli = NULL;
1977 char *path = NULL;
1978 uint32_t attr;
1979 uint16_t port = 0;
1980 struct cli_credentials *creds = NULL;
1981 TALLOC_CTX *frame = talloc_stackframe();
1982 NTSTATUS status;
1984 if (!context || !context->internal->initialized) {
1986 errno = EINVAL; /* Best I can think of ... */
1987 TALLOC_FREE(frame);
1988 return -1;
1991 if (!fname) {
1992 errno = EINVAL;
1993 TALLOC_FREE(frame);
1994 return -1;
1997 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1999 if (SMBC_parse_path(frame,
2000 context,
2001 fname,
2002 &workgroup,
2003 &server,
2004 &port,
2005 &share,
2006 &path,
2007 &user,
2008 &password,
2009 NULL)) {
2010 errno = EINVAL;
2011 TALLOC_FREE(frame);
2012 return -1;
2015 if (!user || user[0] == (char)0) {
2016 user = talloc_strdup(frame, smbc_getUser(context));
2017 if (!user) {
2018 errno = ENOMEM;
2019 TALLOC_FREE(frame);
2020 return -1;
2024 srv = SMBC_server(frame, context, True,
2025 server, port, share, &workgroup, &user, &password);
2027 if (!srv) {
2028 TALLOC_FREE(frame);
2029 return -1; /* errno set by SMBC_server */
2032 creds = context->internal->creds;
2034 /*d_printf(">>>unlink: resolving %s\n", path);*/
2035 status = cli_resolve_path(frame, "",
2036 creds,
2037 srv->cli, path, &targetcli, &targetpath);
2038 if (!NT_STATUS_IS_OK(status)) {
2039 d_printf("Could not resolve %s\n", path);
2040 errno = ENOENT;
2041 TALLOC_FREE(frame);
2042 return -1;
2045 attr = 0;
2047 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) attr |= FILE_ATTRIBUTE_READONLY;
2048 if ((newmode & S_IXUSR) && lp_map_archive(-1)) attr |= FILE_ATTRIBUTE_ARCHIVE;
2049 if ((newmode & S_IXGRP) && lp_map_system(-1)) attr |= FILE_ATTRIBUTE_SYSTEM;
2050 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) attr |= FILE_ATTRIBUTE_HIDDEN;
2052 status = cli_setatr(targetcli, targetpath, attr, 0);
2053 if (!NT_STATUS_IS_OK(status)) {
2054 TALLOC_FREE(frame);
2055 errno = cli_status_to_errno(status);
2056 return -1;
2059 TALLOC_FREE(frame);
2060 return 0;
2064 SMBC_utimes_ctx(SMBCCTX *context,
2065 const char *fname,
2066 struct timeval *tbuf)
2068 SMBCSRV *srv = NULL;
2069 char *server = NULL;
2070 char *share = NULL;
2071 char *user = NULL;
2072 char *password = NULL;
2073 char *workgroup = NULL;
2074 char *path = NULL;
2075 struct timespec access_time, write_time;
2076 uint16_t port = 0;
2077 TALLOC_CTX *frame = talloc_stackframe();
2078 bool ok;
2080 if (!context || !context->internal->initialized) {
2082 errno = EINVAL; /* Best I can think of ... */
2083 TALLOC_FREE(frame);
2084 return -1;
2087 if (!fname) {
2088 errno = EINVAL;
2089 TALLOC_FREE(frame);
2090 return -1;
2093 if (tbuf == NULL) {
2094 access_time = write_time = timespec_current();
2095 } else {
2096 access_time = convert_timeval_to_timespec(tbuf[0]);
2097 write_time = convert_timeval_to_timespec(tbuf[1]);
2100 if (DEBUGLVL(4)) {
2101 struct timeval_buf abuf, wbuf;
2103 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2104 fname,
2105 timespec_string_buf(&access_time, false, &abuf),
2106 timespec_string_buf(&write_time, false, &wbuf));
2109 if (SMBC_parse_path(frame,
2110 context,
2111 fname,
2112 &workgroup,
2113 &server,
2114 &port,
2115 &share,
2116 &path,
2117 &user,
2118 &password,
2119 NULL)) {
2120 errno = EINVAL;
2121 TALLOC_FREE(frame);
2122 return -1;
2125 if (!user || user[0] == (char)0) {
2126 user = talloc_strdup(frame, smbc_getUser(context));
2127 if (!user) {
2128 errno = ENOMEM;
2129 TALLOC_FREE(frame);
2130 return -1;
2134 srv = SMBC_server(frame, context, True,
2135 server, port, share, &workgroup, &user, &password);
2137 if (!srv) {
2138 TALLOC_FREE(frame);
2139 return -1; /* errno set by SMBC_server */
2142 ok = SMBC_setatr(
2143 context,
2144 srv,
2145 path,
2146 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2147 access_time,
2148 write_time,
2149 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2151 if (!ok) {
2152 TALLOC_FREE(frame);
2153 return -1; /* errno set by SMBC_setatr */
2156 TALLOC_FREE(frame);
2157 return 0;
2161 * Routine to unlink() a file
2165 SMBC_unlink_ctx(SMBCCTX *context,
2166 const char *fname)
2168 char *server = NULL;
2169 char *share = NULL;
2170 char *user = NULL;
2171 char *password = NULL;
2172 char *workgroup = NULL;
2173 char *path = NULL;
2174 char *targetpath = NULL;
2175 uint16_t port = 0;
2176 struct cli_state *targetcli = NULL;
2177 SMBCSRV *srv = NULL;
2178 struct cli_credentials *creds = NULL;
2179 TALLOC_CTX *frame = talloc_stackframe();
2180 NTSTATUS status;
2182 if (!context || !context->internal->initialized) {
2184 errno = EINVAL; /* Best I can think of ... */
2185 TALLOC_FREE(frame);
2186 return -1;
2190 if (!fname) {
2191 errno = EINVAL;
2192 TALLOC_FREE(frame);
2193 return -1;
2197 if (SMBC_parse_path(frame,
2198 context,
2199 fname,
2200 &workgroup,
2201 &server,
2202 &port,
2203 &share,
2204 &path,
2205 &user,
2206 &password,
2207 NULL)) {
2208 errno = EINVAL;
2209 TALLOC_FREE(frame);
2210 return -1;
2213 if (!user || user[0] == (char)0) {
2214 user = talloc_strdup(frame, smbc_getUser(context));
2215 if (!user) {
2216 errno = ENOMEM;
2217 TALLOC_FREE(frame);
2218 return -1;
2222 srv = SMBC_server(frame, context, True,
2223 server, port, share, &workgroup, &user, &password);
2225 if (!srv) {
2226 TALLOC_FREE(frame);
2227 return -1; /* SMBC_server sets errno */
2231 creds = context->internal->creds;
2233 /*d_printf(">>>unlink: resolving %s\n", path);*/
2234 status = cli_resolve_path(frame, "",
2235 creds,
2236 srv->cli, path, &targetcli, &targetpath);
2237 if (!NT_STATUS_IS_OK(status)) {
2238 d_printf("Could not resolve %s\n", path);
2239 errno = ENOENT;
2240 TALLOC_FREE(frame);
2241 return -1;
2243 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2245 status = cli_unlink(
2246 targetcli,
2247 targetpath,
2248 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2250 if (!NT_STATUS_IS_OK(status)) {
2252 errno = cli_status_to_errno(status);
2254 if (errno == EACCES) { /* Check if the file is a directory */
2256 int saverr = errno;
2257 struct stat sb = {0};
2259 status = SMBC_getatr(context, srv, path, &sb);
2260 if (!NT_STATUS_IS_OK(status)) {
2261 /* Hmmm, bad error ... What? */
2263 TALLOC_FREE(frame);
2264 errno = cli_status_to_errno(status);
2265 return -1;
2268 else {
2270 if (S_ISDIR(sb.st_mode))
2271 errno = EISDIR;
2272 else
2273 errno = saverr; /* Restore this */
2278 TALLOC_FREE(frame);
2279 return -1;
2283 TALLOC_FREE(frame);
2284 return 0; /* Success ... */
2289 * Routine to rename() a file
2293 SMBC_rename_ctx(SMBCCTX *ocontext,
2294 const char *oname,
2295 SMBCCTX *ncontext,
2296 const char *nname)
2298 char *server1 = NULL;
2299 char *share1 = NULL;
2300 char *server2 = NULL;
2301 char *share2 = NULL;
2302 char *user1 = NULL;
2303 char *user2 = NULL;
2304 char *password1 = NULL;
2305 char *password2 = NULL;
2306 char *workgroup = NULL;
2307 char *path1 = NULL;
2308 char *path2 = NULL;
2309 char *targetpath1 = NULL;
2310 char *targetpath2 = NULL;
2311 struct cli_state *targetcli1 = NULL;
2312 struct cli_state *targetcli2 = NULL;
2313 SMBCSRV *srv = NULL;
2314 uint16_t port1 = 0;
2315 uint16_t port2 = 0;
2316 struct cli_credentials *ocreds = NULL;
2317 struct cli_credentials *ncreds = NULL;
2318 TALLOC_CTX *frame = talloc_stackframe();
2319 NTSTATUS status;
2321 if (!ocontext || !ncontext ||
2322 !ocontext->internal->initialized ||
2323 !ncontext->internal->initialized) {
2325 errno = EINVAL; /* Best I can think of ... */
2326 TALLOC_FREE(frame);
2327 return -1;
2330 if (!oname || !nname) {
2331 errno = EINVAL;
2332 TALLOC_FREE(frame);
2333 return -1;
2336 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
2338 if (SMBC_parse_path(frame,
2339 ocontext,
2340 oname,
2341 &workgroup,
2342 &server1,
2343 &port1,
2344 &share1,
2345 &path1,
2346 &user1,
2347 &password1,
2348 NULL)) {
2349 errno = EINVAL;
2350 TALLOC_FREE(frame);
2351 return -1;
2354 if (!user1 || user1[0] == (char)0) {
2355 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
2356 if (!user1) {
2357 errno = ENOMEM;
2358 TALLOC_FREE(frame);
2359 return -1;
2363 if (SMBC_parse_path(frame,
2364 ncontext,
2365 nname,
2366 NULL,
2367 &server2,
2368 &port2,
2369 &share2,
2370 &path2,
2371 &user2,
2372 &password2,
2373 NULL)) {
2374 errno = EINVAL;
2375 TALLOC_FREE(frame);
2376 return -1;
2379 if (!user2 || user2[0] == (char)0) {
2380 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
2381 if (!user2) {
2382 errno = ENOMEM;
2383 TALLOC_FREE(frame);
2384 return -1;
2388 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2389 strcmp(user1, user2)) {
2390 /* Can't rename across file systems, or users?? */
2391 errno = EXDEV;
2392 TALLOC_FREE(frame);
2393 return -1;
2396 srv = SMBC_server(frame, ocontext, True,
2397 server1, port1, share1, &workgroup, &user1, &password1);
2398 if (!srv) {
2399 TALLOC_FREE(frame);
2400 return -1;
2404 /* set the credentials to make DFS work */
2405 smbc_set_credentials_with_fallback(ocontext,
2406 workgroup,
2407 user1,
2408 password1);
2410 /*d_printf(">>>rename: resolving %s\n", path1);*/
2411 ocreds = ocontext->internal->creds;
2413 status = cli_resolve_path(frame, "",
2414 ocreds,
2415 srv->cli, path1, &targetcli1, &targetpath1);
2416 if (!NT_STATUS_IS_OK(status)) {
2417 d_printf("Could not resolve %s\n", path1);
2418 errno = ENOENT;
2419 TALLOC_FREE(frame);
2420 return -1;
2423 /* set the credentials to make DFS work */
2424 smbc_set_credentials_with_fallback(ncontext,
2425 workgroup,
2426 user2,
2427 password2);
2429 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2430 /*d_printf(">>>rename: resolving %s\n", path2);*/
2431 ncreds = ncontext->internal->creds;
2433 status = cli_resolve_path(frame, "",
2434 ncreds,
2435 srv->cli, path2, &targetcli2, &targetpath2);
2436 if (!NT_STATUS_IS_OK(status)) {
2437 d_printf("Could not resolve %s\n", path2);
2438 errno = ENOENT;
2439 TALLOC_FREE(frame);
2440 return -1;
2442 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2444 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2445 strcmp(targetcli1->share, targetcli2->share))
2447 /* can't rename across file systems */
2448 errno = EXDEV;
2449 TALLOC_FREE(frame);
2450 return -1;
2453 status = cli_rename(targetcli1, targetpath1, targetpath2, false);
2454 if (!NT_STATUS_IS_OK(status)) {
2455 int eno = cli_status_to_errno(status);
2457 if (eno != EEXIST ||
2458 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2,
2459 FILE_ATTRIBUTE_SYSTEM |
2460 FILE_ATTRIBUTE_HIDDEN)) ||
2461 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
2462 targetpath2, false))) {
2464 errno = eno;
2465 TALLOC_FREE(frame);
2466 return -1;
2471 TALLOC_FREE(frame);
2472 return 0; /* Success */
2475 struct smbc_notify_cb_state {
2476 struct tevent_context *ev;
2477 struct cli_state *cli;
2478 uint16_t fnum;
2479 bool recursive;
2480 uint32_t completion_filter;
2481 unsigned callback_timeout_ms;
2482 smbc_notify_callback_fn cb;
2483 void *private_data;
2486 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2487 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2489 static struct tevent_req *smbc_notify_cb_send(
2490 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2491 uint16_t fnum, bool recursive, uint32_t completion_filter,
2492 unsigned callback_timeout_ms,
2493 smbc_notify_callback_fn cb, void *private_data)
2495 struct tevent_req *req, *subreq;
2496 struct smbc_notify_cb_state *state;
2498 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2499 if (req == NULL) {
2500 return NULL;
2502 state->ev = ev;
2503 state->cli = cli;
2504 state->fnum = fnum;
2505 state->recursive = recursive;
2506 state->completion_filter = completion_filter;
2507 state->callback_timeout_ms = callback_timeout_ms;
2508 state->cb = cb;
2509 state->private_data = private_data;
2511 subreq = cli_notify_send(
2512 state, state->ev, state->cli, state->fnum, 1000,
2513 state->completion_filter, state->recursive);
2514 if (tevent_req_nomem(subreq, req)) {
2515 return tevent_req_post(req, ev);
2517 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2519 if (state->callback_timeout_ms == 0) {
2520 return req;
2523 subreq = tevent_wakeup_send(
2524 state, state->ev,
2525 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2526 state->callback_timeout_ms*1000));
2527 if (tevent_req_nomem(subreq, req)) {
2528 return tevent_req_post(req, ev);
2530 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2532 return req;
2535 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2537 struct tevent_req *req = tevent_req_callback_data(
2538 subreq, struct tevent_req);
2539 struct smbc_notify_cb_state *state = tevent_req_data(
2540 req, struct smbc_notify_cb_state);
2541 uint32_t num_changes;
2542 struct notify_change *changes;
2543 NTSTATUS status;
2544 int cb_ret;
2546 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2547 TALLOC_FREE(subreq);
2548 if (tevent_req_nterror(req, status)) {
2549 return;
2553 struct smbc_notify_callback_action actions[num_changes];
2554 uint32_t i;
2556 for (i=0; i<num_changes; i++) {
2557 actions[i].action = changes[i].action;
2558 actions[i].filename = changes[i].name;
2561 cb_ret = state->cb(actions, num_changes, state->private_data);
2564 TALLOC_FREE(changes);
2566 if (cb_ret != 0) {
2567 tevent_req_done(req);
2568 return;
2571 subreq = cli_notify_send(
2572 state, state->ev, state->cli, state->fnum, 1000,
2573 state->completion_filter, state->recursive);
2574 if (tevent_req_nomem(subreq, req)) {
2575 return;
2577 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2580 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2582 struct tevent_req *req = tevent_req_callback_data(
2583 subreq, struct tevent_req);
2584 struct smbc_notify_cb_state *state = tevent_req_data(
2585 req, struct smbc_notify_cb_state);
2586 int cb_ret;
2587 bool ok;
2589 ok = tevent_wakeup_recv(subreq);
2590 TALLOC_FREE(subreq);
2591 if (!ok) {
2592 tevent_req_oom(req);
2593 return;
2596 cb_ret = state->cb(NULL, 0, state->private_data);
2597 if (cb_ret != 0) {
2598 tevent_req_done(req);
2599 return;
2602 subreq = tevent_wakeup_send(
2603 state, state->ev,
2604 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2605 state->callback_timeout_ms*1000));
2606 if (tevent_req_nomem(subreq, req)) {
2607 return;
2609 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2612 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2614 return tevent_req_simple_recv_ntstatus(req);
2617 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2618 bool recursive, uint32_t completion_filter,
2619 unsigned callback_timeout_ms,
2620 smbc_notify_callback_fn cb, void *private_data)
2622 TALLOC_CTX *frame = talloc_stackframe();
2623 struct tevent_context *ev;
2624 struct tevent_req *req;
2625 NTSTATUS status = NT_STATUS_NO_MEMORY;
2627 ev = samba_tevent_context_init(frame);
2628 if (ev == NULL) {
2629 goto fail;
2631 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2632 completion_filter,
2633 callback_timeout_ms, cb, private_data);
2634 if (req == NULL) {
2635 goto fail;
2637 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2638 goto fail;
2640 status = smbc_notify_cb_recv(req);
2641 TALLOC_FREE(req);
2642 fail:
2643 TALLOC_FREE(frame);
2644 return status;
2648 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2649 uint32_t completion_filter, unsigned callback_timeout_ms,
2650 smbc_notify_callback_fn cb, void *private_data)
2652 TALLOC_CTX *frame = talloc_stackframe();
2653 struct cli_state *cli;
2654 char *server = NULL;
2655 char *share = NULL;
2656 char *user = NULL;
2657 char *password = NULL;
2658 char *options = NULL;
2659 char *workgroup = NULL;
2660 char *path = NULL;
2661 uint16_t port;
2662 NTSTATUS status;
2663 uint16_t fnum;
2665 if ((context == NULL) || !context->internal->initialized) {
2666 TALLOC_FREE(frame);
2667 errno = EINVAL;
2668 return -1;
2670 if (!SMBC_dlist_contains(context->internal->files, dir)) {
2671 TALLOC_FREE(frame);
2672 errno = EBADF;
2673 return -1;
2676 if (SMBC_parse_path(frame,
2677 context,
2678 dir->fname,
2679 &workgroup,
2680 &server,
2681 &port,
2682 &share,
2683 &path,
2684 &user,
2685 &password,
2686 &options)) {
2687 DEBUG(4, ("no valid path\n"));
2688 TALLOC_FREE(frame);
2689 errno = EINVAL + 8194;
2690 return -1;
2693 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2694 "path='%s' options='%s'\n",
2695 dir->fname, server, share, path, options));
2697 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2698 (int)recursive, completion_filter));
2700 cli = dir->srv->cli;
2701 status = cli_ntcreate(
2702 cli, path, 0, FILE_READ_DATA, 0,
2703 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2704 FILE_OPEN, 0, 0, &fnum, NULL);
2705 if (!NT_STATUS_IS_OK(status)) {
2706 TALLOC_FREE(frame);
2707 errno = cli_status_to_errno(status);
2708 return -1;
2711 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2712 callback_timeout_ms, cb, private_data);
2713 if (!NT_STATUS_IS_OK(status)) {
2714 cli_close(cli, fnum);
2715 TALLOC_FREE(frame);
2716 errno = cli_status_to_errno(status);
2717 return -1;
2720 cli_close(cli, fnum);
2722 TALLOC_FREE(frame);
2723 return 0;