lib: talloc: Fix memlimit on pool realloc.
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob27d0fbd4db56a324e37a38d3d745f6a64ae8f3bd
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 "auth_info.h"
29 #include "libsmbclient.h"
30 #include "libsmb_internal.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
33 #include "libsmb/nmblib.h"
34 #include "../libcli/smb/smbXcli_base.h"
35 #include "../libcli/security/security.h"
36 #include "lib/util/tevent_ntstatus.h"
37 #include "lib/util/time_basic.h"
38 #include "lib/util/string_wrappers.h"
41 * Routine to open a directory
42 * We accept the URL syntax explained in SMBC_parse_path(), above.
45 static void remove_dirplus(SMBCFILE *dir)
47 struct smbc_dirplus_list *d = NULL;
49 d = dir->dirplus_list;
50 while (d != NULL) {
51 struct smbc_dirplus_list *f = d;
52 d = d->next;
54 SAFE_FREE(f->smb_finfo->short_name);
55 SAFE_FREE(f->smb_finfo->name);
56 SAFE_FREE(f->smb_finfo);
57 SAFE_FREE(f);
60 dir->dirplus_list = NULL;
61 dir->dirplus_end = NULL;
62 dir->dirplus_next = NULL;
65 static void
66 remove_dir(SMBCFILE *dir)
68 struct smbc_dir_list *d,*f;
70 d = dir->dir_list;
71 while (d) {
73 f = d; d = d->next;
75 SAFE_FREE(f->dirent);
76 SAFE_FREE(f);
80 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
84 static int
85 add_dirent(SMBCFILE *dir,
86 const char *name,
87 const char *comment,
88 uint32_t type)
90 struct smbc_dirent *dirent;
91 int size;
92 int name_length = (name == NULL ? 0 : strlen(name));
93 int comment_len = (comment == NULL ? 0 : strlen(comment));
96 * Allocate space for the dirent, which must be increased by the
97 * size of the name and the comment and 1 each for the null terminator.
100 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
102 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
104 if (!dirent) {
106 dir->dir_error = ENOMEM;
107 return -1;
111 ZERO_STRUCTP(dirent);
113 if (dir->dir_list == NULL) {
115 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
116 if (!dir->dir_list) {
118 SAFE_FREE(dirent);
119 dir->dir_error = ENOMEM;
120 return -1;
123 ZERO_STRUCTP(dir->dir_list);
125 dir->dir_end = dir->dir_next = dir->dir_list;
127 else {
129 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
131 if (!dir->dir_end->next) {
133 SAFE_FREE(dirent);
134 dir->dir_error = ENOMEM;
135 return -1;
138 ZERO_STRUCTP(dir->dir_end->next);
140 dir->dir_end = dir->dir_end->next;
143 dir->dir_end->next = NULL;
144 dir->dir_end->dirent = dirent;
146 dirent->smbc_type = type;
147 dirent->namelen = name_length;
148 dirent->commentlen = comment_len;
149 dirent->dirlen = size;
152 * dirent->namelen + 1 includes the null (no null termination needed)
153 * Ditto for dirent->commentlen.
154 * The space for the two null bytes was allocated.
156 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
157 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
158 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
160 return 0;
164 static int add_dirplus(SMBCFILE *dir, struct file_info *finfo)
166 struct smbc_dirplus_list *new_entry = NULL;
167 struct libsmb_file_info *info = NULL;
169 new_entry = SMB_MALLOC_P(struct smbc_dirplus_list);
170 if (new_entry == NULL) {
171 dir->dir_error = ENOMEM;
172 return -1;
174 ZERO_STRUCTP(new_entry);
175 new_entry->ino = finfo->ino;
177 info = SMB_MALLOC_P(struct libsmb_file_info);
178 if (info == NULL) {
179 SAFE_FREE(new_entry);
180 dir->dir_error = ENOMEM;
181 return -1;
184 ZERO_STRUCTP(info);
186 info->btime_ts = finfo->btime_ts;
187 info->atime_ts = finfo->atime_ts;
188 info->ctime_ts = finfo->ctime_ts;
189 info->mtime_ts = finfo->mtime_ts;
190 info->gid = finfo->gid;
191 info->attrs = finfo->attr;
192 info->size = finfo->size;
193 info->uid = finfo->uid;
194 info->name = SMB_STRDUP(finfo->name);
195 if (info->name == NULL) {
196 SAFE_FREE(info);
197 SAFE_FREE(new_entry);
198 dir->dir_error = ENOMEM;
199 return -1;
202 if (finfo->short_name) {
203 info->short_name = SMB_STRDUP(finfo->short_name);
204 } else {
205 info->short_name = SMB_STRDUP("");
208 if (info->short_name == NULL) {
209 SAFE_FREE(info->name);
210 SAFE_FREE(info);
211 SAFE_FREE(new_entry);
212 dir->dir_error = ENOMEM;
213 return -1;
215 new_entry->smb_finfo = info;
217 /* Now add to the list. */
218 if (dir->dirplus_list == NULL) {
219 /* Empty list - point everything at new_entry. */
220 dir->dirplus_list = new_entry;
221 dir->dirplus_end = new_entry;
222 dir->dirplus_next = new_entry;
223 } else {
224 /* Append to list but leave the ->next cursor alone. */
225 dir->dirplus_end->next = new_entry;
226 dir->dirplus_end = new_entry;
229 return 0;
232 static void
233 list_unique_wg_fn(const char *name,
234 uint32_t type,
235 const char *comment,
236 void *state)
238 SMBCFILE *dir = (SMBCFILE *)state;
239 struct smbc_dir_list *dir_list;
240 struct smbc_dirent *dirent;
241 int dirent_type;
242 int do_remove = 0;
244 dirent_type = dir->dir_type;
246 if (add_dirent(dir, name, comment, dirent_type) < 0) {
247 /* An error occurred, what do we do? */
248 /* FIXME: Add some code here */
249 /* Change cli_NetServerEnum to take a fn
250 returning NTSTATUS... JRA. */
253 /* Point to the one just added */
254 dirent = dir->dir_end->dirent;
256 /* See if this was a duplicate */
257 for (dir_list = dir->dir_list;
258 dir_list != dir->dir_end;
259 dir_list = dir_list->next) {
260 if (! do_remove &&
261 strcmp(dir_list->dirent->name, dirent->name) == 0) {
262 /* Duplicate. End end of list need to be removed. */
263 do_remove = 1;
266 if (do_remove && dir_list->next == dir->dir_end) {
267 /* Found the end of the list. Remove it. */
268 dir->dir_end = dir_list;
269 free(dir_list->next);
270 free(dirent);
271 dir_list->next = NULL;
272 break;
277 static void
278 list_fn(const char *name,
279 uint32_t type,
280 const char *comment,
281 void *state)
283 SMBCFILE *dir = (SMBCFILE *)state;
284 int dirent_type;
287 * We need to process the type a little ...
289 * Disk share = 0x00000000
290 * Print share = 0x00000001
291 * Comms share = 0x00000002 (obsolete?)
292 * IPC$ share = 0x00000003
294 * administrative shares:
295 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
298 if (dir->dir_type == SMBC_FILE_SHARE) {
299 switch (type) {
300 case 0 | 0x80000000:
301 case 0:
302 dirent_type = SMBC_FILE_SHARE;
303 break;
305 case 1:
306 dirent_type = SMBC_PRINTER_SHARE;
307 break;
309 case 2:
310 dirent_type = SMBC_COMMS_SHARE;
311 break;
313 case 3 | 0x80000000:
314 case 3:
315 dirent_type = SMBC_IPC_SHARE;
316 break;
318 default:
319 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
320 break;
323 else {
324 dirent_type = dir->dir_type;
327 if (add_dirent(dir, name, comment, dirent_type) < 0) {
328 /* An error occurred, what do we do? */
329 /* FIXME: Add some code here */
330 /* Change cli_NetServerEnum to take a fn
331 returning NTSTATUS... JRA. */
335 static NTSTATUS
336 dir_list_fn(struct file_info *finfo,
337 const char *mask,
338 void *state)
340 SMBCFILE *dirp = (SMBCFILE *)state;
341 int ret;
343 if (add_dirent((SMBCFILE *)state, finfo->name, "",
344 (finfo->attr&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
345 SMBCFILE *dir = (SMBCFILE *)state;
346 return map_nt_error_from_unix(dir->dir_error);
348 ret = add_dirplus(dirp, finfo);
349 if (ret < 0) {
350 return map_nt_error_from_unix(dirp->dir_error);
352 return NT_STATUS_OK;
355 static NTSTATUS
356 net_share_enum_rpc(struct cli_state *cli,
357 void (*fn)(const char *name,
358 uint32_t type,
359 const char *comment,
360 void *state),
361 void *state)
363 uint32_t i;
364 WERROR result;
365 uint32_t preferred_len = 0xffffffff;
366 uint32_t type;
367 struct srvsvc_NetShareInfoCtr info_ctr;
368 struct srvsvc_NetShareCtr1 ctr1;
369 fstring name = "";
370 fstring comment = "";
371 struct rpc_pipe_client *pipe_hnd = NULL;
372 NTSTATUS nt_status;
373 uint32_t resume_handle = 0;
374 uint32_t total_entries = 0;
375 struct dcerpc_binding_handle *b;
377 /* Open the server service pipe */
378 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
379 &pipe_hnd);
380 if (!NT_STATUS_IS_OK(nt_status)) {
381 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
382 goto done;
385 ZERO_STRUCT(info_ctr);
386 ZERO_STRUCT(ctr1);
388 info_ctr.level = 1;
389 info_ctr.ctr.ctr1 = &ctr1;
391 b = pipe_hnd->binding_handle;
393 /* Issue the NetShareEnum RPC call and retrieve the response */
394 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
395 pipe_hnd->desthost,
396 &info_ctr,
397 preferred_len,
398 &total_entries,
399 &resume_handle,
400 &result);
402 /* Was it successful? */
403 if (!NT_STATUS_IS_OK(nt_status)) {
404 /* Nope. Go clean up. */
405 goto done;
408 if (!W_ERROR_IS_OK(result)) {
409 /* Nope. Go clean up. */
410 nt_status = werror_to_ntstatus(result);
411 goto done;
414 if (total_entries == 0) {
415 /* Nope. Go clean up. */
416 nt_status = NT_STATUS_NOT_FOUND;
417 goto done;
420 /* For each returned entry... */
421 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
423 /* pull out the share name */
424 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
426 /* pull out the share's comment */
427 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
429 /* Get the type value */
430 type = info_ctr.ctr.ctr1->array[i].type;
432 /* Add this share to the list */
433 (*fn)(name, type, comment, state);
436 done:
437 /* Close the server service pipe */
438 TALLOC_FREE(pipe_hnd);
440 /* Tell 'em if it worked */
441 return nt_status;
446 * Verify that the options specified in a URL are valid
449 SMBC_check_options(char *server,
450 char *share,
451 char *path,
452 char *options)
454 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
455 "path='%s' options='%s'\n",
456 server, share, path, options));
458 /* No options at all is always ok */
459 if (! *options) return 0;
461 /* Currently, we don't support any options. */
462 return -1;
466 SMBCFILE *
467 SMBC_opendir_ctx(SMBCCTX *context,
468 const char *fname)
470 char *server = NULL;
471 char *share = NULL;
472 char *user = NULL;
473 char *password = NULL;
474 char *options = NULL;
475 char *workgroup = NULL;
476 char *path = NULL;
477 size_t path_len = 0;
478 uint16_t port = 0;
479 SMBCSRV *srv = NULL;
480 SMBCFILE *dir = NULL;
481 struct sockaddr_storage rem_ss;
482 TALLOC_CTX *frame = talloc_stackframe();
484 if (!context || !context->internal->initialized) {
485 DEBUG(4, ("no valid context\n"));
486 TALLOC_FREE(frame);
487 errno = EINVAL + 8192;
488 return NULL;
492 if (!fname) {
493 DEBUG(4, ("no valid fname\n"));
494 TALLOC_FREE(frame);
495 errno = EINVAL + 8193;
496 return NULL;
499 if (SMBC_parse_path(frame,
500 context,
501 fname,
502 &workgroup,
503 &server,
504 &port,
505 &share,
506 &path,
507 &user,
508 &password,
509 &options)) {
510 DEBUG(4, ("no valid path\n"));
511 TALLOC_FREE(frame);
512 errno = EINVAL + 8194;
513 return NULL;
516 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
517 "path='%s' options='%s'\n",
518 fname, server, share, path, options));
520 /* Ensure the options are valid */
521 if (SMBC_check_options(server, share, path, options)) {
522 DEBUG(4, ("unacceptable options (%s)\n", options));
523 TALLOC_FREE(frame);
524 errno = EINVAL + 8195;
525 return NULL;
528 if (!user || user[0] == (char)0) {
529 user = talloc_strdup(frame, smbc_getUser(context));
530 if (!user) {
531 TALLOC_FREE(frame);
532 errno = ENOMEM;
533 return NULL;
537 dir = SMB_MALLOC_P(SMBCFILE);
539 if (!dir) {
540 TALLOC_FREE(frame);
541 errno = ENOMEM;
542 return NULL;
545 ZERO_STRUCTP(dir);
547 dir->cli_fd = 0;
548 dir->fname = SMB_STRDUP(fname);
549 if (dir->fname == NULL) {
550 SAFE_FREE(dir);
551 TALLOC_FREE(frame);
552 errno = ENOMEM;
553 return NULL;
555 dir->srv = NULL;
556 dir->offset = 0;
557 dir->file = False;
558 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
560 if (server[0] == (char)0) {
562 size_t i;
563 size_t count = 0;
564 size_t max_lmb_count;
565 struct sockaddr_storage *ip_list;
566 struct sockaddr_storage server_addr;
567 struct user_auth_info *u_info;
568 NTSTATUS status;
570 if (share[0] != (char)0 || path[0] != (char)0) {
572 if (dir) {
573 SAFE_FREE(dir->fname);
574 SAFE_FREE(dir);
576 TALLOC_FREE(frame);
577 errno = EINVAL + 8196;
578 return NULL;
581 /* Determine how many local master browsers to query */
582 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
583 ? INT_MAX
584 : smbc_getOptionBrowseMaxLmbCount(context));
586 u_info = user_auth_info_init(frame);
587 if (u_info == NULL) {
588 if (dir) {
589 SAFE_FREE(dir->fname);
590 SAFE_FREE(dir);
592 TALLOC_FREE(frame);
593 errno = ENOMEM;
594 return NULL;
596 set_cmdline_auth_info_username(u_info, user);
597 set_cmdline_auth_info_password(u_info, password);
600 * We have server and share and path empty but options
601 * requesting that we scan all master browsers for their list
602 * of workgroups/domains. This implies that we must first try
603 * broadcast queries to find all master browsers, and if that
604 * doesn't work, then try our other methods which return only
605 * a single master browser.
608 ip_list = NULL;
609 status = name_resolve_bcast(talloc_tos(),
610 MSBROWSE,
612 &ip_list,
613 &count);
614 if (!NT_STATUS_IS_OK(status))
617 TALLOC_FREE(ip_list);
619 if (!find_master_ip(workgroup, &server_addr)) {
621 if (dir) {
622 SAFE_FREE(dir->fname);
623 SAFE_FREE(dir);
625 TALLOC_FREE(frame);
626 errno = ENOENT;
627 return NULL;
630 ip_list = (struct sockaddr_storage *)talloc_memdup(
631 talloc_tos(), &server_addr,
632 sizeof(server_addr));
633 if (ip_list == NULL) {
634 if (dir) {
635 SAFE_FREE(dir->fname);
636 SAFE_FREE(dir);
638 TALLOC_FREE(frame);
639 errno = ENOMEM;
640 return NULL;
642 count = 1;
645 for (i = 0; i < count && i < max_lmb_count; i++) {
646 char addr[INET6_ADDRSTRLEN];
647 char *wg_ptr = NULL;
648 struct cli_state *cli = NULL;
650 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
651 DEBUG(99, ("Found master browser %zu of %zu: %s\n",
652 i+1, MAX(count, max_lmb_count),
653 addr));
655 cli = get_ipc_connect_master_ip(talloc_tos(),
656 &ip_list[i],
657 u_info,
658 &wg_ptr);
659 /* cli == NULL is the master browser refused to talk or
660 could not be found */
661 if (!cli) {
662 continue;
665 workgroup = talloc_strdup(frame, wg_ptr);
666 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
668 cli_shutdown(cli);
670 if (!workgroup || !server) {
671 if (dir) {
672 SAFE_FREE(dir->fname);
673 SAFE_FREE(dir);
675 TALLOC_FREE(frame);
676 errno = ENOMEM;
677 return NULL;
680 DEBUG(4, ("using workgroup %s %s\n",
681 workgroup, server));
684 * For each returned master browser IP address, get a
685 * connection to IPC$ on the server if we do not
686 * already have one, and determine the
687 * workgroups/domains that it knows about.
690 srv = SMBC_server(frame, context, True, server, port, "IPC$",
691 &workgroup, &user, &password);
692 if (!srv) {
693 continue;
696 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
697 continue;
700 dir->srv = srv;
701 dir->dir_type = SMBC_WORKGROUP;
703 /* Now, list the stuff ... */
705 if (!cli_NetServerEnum(srv->cli,
706 workgroup,
707 SV_TYPE_DOMAIN_ENUM,
708 list_unique_wg_fn,
709 (void *)dir)) {
710 continue;
714 TALLOC_FREE(ip_list);
715 } else {
717 * Server not an empty string ... Check the rest and see what
718 * gives
720 if (*share == '\0') {
721 if (*path != '\0') {
723 /* Should not have empty share with path */
724 if (dir) {
725 SAFE_FREE(dir->fname);
726 SAFE_FREE(dir);
728 TALLOC_FREE(frame);
729 errno = EINVAL + 8197;
730 return NULL;
735 * We don't know if <server> is really a server name
736 * or is a workgroup/domain name. If we already have
737 * a server structure for it, we'll use it.
738 * Otherwise, check to see if <server><1D>,
739 * <server><1B>, or <server><20> translates. We check
740 * to see if <server> is an IP address first.
744 * See if we have an existing server. Do not
745 * establish a connection if one does not already
746 * exist.
748 srv = SMBC_server(frame, context, False,
749 server, port, "IPC$",
750 &workgroup, &user, &password);
753 * If no existing server and not an IP addr, look for
754 * LMB or DMB
756 if (!srv &&
757 !is_ipaddress(server) &&
758 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
759 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
761 * "server" is actually a workgroup name,
762 * not a server. Make this clear.
764 char *wgroup = server;
765 fstring buserver;
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 if (!cli_NetServerEnum(srv->cli, wgroup,
825 0x0000FFFE, list_fn,
826 (void *)dir)) {
828 if (dir) {
829 SAFE_FREE(dir->fname);
830 SAFE_FREE(dir);
832 TALLOC_FREE(frame);
833 return NULL;
835 } else if (srv ||
836 (resolve_name(server, &rem_ss, 0x20, false))) {
837 NTSTATUS status;
840 * If we hadn't found the server, get one now
842 if (!srv) {
843 srv = SMBC_server(frame, context, True,
844 server, port, "IPC$",
845 &workgroup,
846 &user, &password);
849 if (!srv) {
850 if (dir) {
851 SAFE_FREE(dir->fname);
852 SAFE_FREE(dir);
854 TALLOC_FREE(frame);
855 return NULL;
859 dir->dir_type = SMBC_FILE_SHARE;
860 dir->srv = srv;
862 /* List the shares ... */
864 status = net_share_enum_rpc(srv->cli,
865 list_fn,
866 (void *)dir);
867 if (!NT_STATUS_IS_OK(status) &&
868 smbXcli_conn_protocol(srv->cli->conn) <=
869 PROTOCOL_NT1) {
871 * Only call cli_RNetShareEnum()
872 * on SMB1 connections, not SMB2+.
874 int rc = cli_RNetShareEnum(srv->cli,
875 list_fn,
876 (void *)dir);
877 if (rc != 0) {
878 status = cli_nt_error(srv->cli);
879 } else {
880 status = NT_STATUS_OK;
883 if (!NT_STATUS_IS_OK(status)) {
885 * Set cli->raw_status so SMBC_errno()
886 * will correctly return the error.
888 srv->cli->raw_status = status;
889 if (dir != NULL) {
890 SAFE_FREE(dir->fname);
891 SAFE_FREE(dir);
893 TALLOC_FREE(frame);
894 errno = map_errno_from_nt_status(
895 status);
896 return NULL;
898 } else {
899 /* Neither the workgroup nor server exists */
900 errno = ECONNREFUSED;
901 if (dir) {
902 SAFE_FREE(dir->fname);
903 SAFE_FREE(dir);
905 TALLOC_FREE(frame);
906 return NULL;
910 else {
912 * The server and share are specified ... work from
913 * there ...
915 char *targetpath;
916 struct cli_state *targetcli;
917 struct cli_credentials *creds = NULL;
918 NTSTATUS status;
920 /* We connect to the server and list the directory */
921 dir->dir_type = SMBC_FILE_SHARE;
923 srv = SMBC_server(frame, context, True, server, port, share,
924 &workgroup, &user, &password);
926 if (!srv) {
927 if (dir) {
928 SAFE_FREE(dir->fname);
929 SAFE_FREE(dir);
931 TALLOC_FREE(frame);
932 return NULL;
935 dir->srv = srv;
937 /* Now, list the files ... */
939 path_len = strlen(path);
940 path = talloc_asprintf_append(path, "\\*");
941 if (!path) {
942 if (dir) {
943 SAFE_FREE(dir->fname);
944 SAFE_FREE(dir);
946 TALLOC_FREE(frame);
947 return NULL;
950 creds = get_cmdline_auth_info_creds(
951 context->internal->auth_info);
953 status = cli_resolve_path(
954 frame, "",
955 creds,
956 srv->cli, path, &targetcli, &targetpath);
957 if (!NT_STATUS_IS_OK(status)) {
958 d_printf("Could not resolve %s\n", path);
959 if (dir) {
960 SAFE_FREE(dir->fname);
961 SAFE_FREE(dir);
963 TALLOC_FREE(frame);
964 return NULL;
967 status = cli_list(targetcli, targetpath,
968 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
969 dir_list_fn, (void *)dir);
970 if (!NT_STATUS_IS_OK(status)) {
971 int saved_errno;
972 if (dir) {
973 SAFE_FREE(dir->fname);
974 SAFE_FREE(dir);
976 saved_errno = SMBC_errno(context, targetcli);
978 if (saved_errno == EINVAL) {
979 struct stat sb = {0};
981 * See if they asked to opendir
982 * something other than a directory.
983 * If so, the converted error value we
984 * got would have been EINVAL rather
985 * than ENOTDIR.
987 path[path_len] = '\0'; /* restore original path */
989 if (SMBC_getatr(context,
990 srv,
991 path,
992 &sb) &&
993 !S_ISDIR(sb.st_mode)) {
995 /* It is. Correct the error value */
996 saved_errno = ENOTDIR;
1001 * If there was an error and the server is no
1002 * good any more...
1004 if (cli_is_error(targetcli) &&
1005 smbc_getFunctionCheckServer(context)(context, srv)) {
1007 /* ... then remove it. */
1008 if (smbc_getFunctionRemoveUnusedServer(context)(context,
1009 srv)) {
1011 * We could not remove the
1012 * server completely, remove
1013 * it from the cache so we
1014 * will not get it again. It
1015 * will be removed when the
1016 * last file/dir is closed.
1018 smbc_getFunctionRemoveCachedServer(context)(context, srv);
1022 TALLOC_FREE(frame);
1023 errno = saved_errno;
1024 return NULL;
1030 DLIST_ADD(context->internal->files, dir);
1031 TALLOC_FREE(frame);
1032 return dir;
1037 * Routine to close a directory
1041 SMBC_closedir_ctx(SMBCCTX *context,
1042 SMBCFILE *dir)
1044 TALLOC_CTX *frame = talloc_stackframe();
1046 if (!context || !context->internal->initialized) {
1047 errno = EINVAL;
1048 TALLOC_FREE(frame);
1049 return -1;
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 if (dir) {
1065 SAFE_FREE(dir->fname);
1066 SAFE_FREE(dir); /* Free the space too */
1069 TALLOC_FREE(frame);
1070 return 0;
1074 static int
1075 smbc_readdir_internal(SMBCCTX * context,
1076 struct smbc_dirent *dest,
1077 struct smbc_dirent *src,
1078 int max_namebuf_len)
1080 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
1081 int remaining_len;
1083 /* url-encode the name. get back remaining buffer space */
1084 remaining_len =
1085 smbc_urlencode(dest->name, src->name, max_namebuf_len);
1087 /* -1 means no null termination. */
1088 if (remaining_len < 0) {
1089 return -1;
1092 /* We now know the name length */
1093 dest->namelen = strlen(dest->name);
1095 if (dest->namelen + 1 < 1) {
1096 /* Integer wrap. */
1097 return -1;
1100 if (dest->namelen + 1 >= max_namebuf_len) {
1101 /* Out of space for comment. */
1102 return -1;
1105 /* Save the pointer to the beginning of the comment */
1106 dest->comment = dest->name + dest->namelen + 1;
1108 if (remaining_len < 1) {
1109 /* No room for comment null termination. */
1110 return -1;
1113 /* Copy the comment */
1114 strlcpy(dest->comment, src->comment, remaining_len);
1116 /* Save other fields */
1117 dest->smbc_type = src->smbc_type;
1118 dest->commentlen = strlen(dest->comment);
1119 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
1120 (char *) dest);
1121 } else {
1123 /* No encoding. Just copy the entry as is. */
1124 if (src->dirlen > max_namebuf_len) {
1125 return -1;
1127 memcpy(dest, src, src->dirlen);
1128 if (src->namelen + 1 < 1) {
1129 /* Integer wrap */
1130 return -1;
1132 if (src->namelen + 1 >= max_namebuf_len) {
1133 /* Comment off the end. */
1134 return -1;
1136 dest->comment = (char *)(&dest->name + src->namelen + 1);
1138 return 0;
1142 * Routine to get a directory entry
1145 struct smbc_dirent *
1146 SMBC_readdir_ctx(SMBCCTX *context,
1147 SMBCFILE *dir)
1149 int maxlen;
1150 int ret;
1151 struct smbc_dirent *dirp, *dirent;
1152 TALLOC_CTX *frame = talloc_stackframe();
1154 /* Check that all is ok first ... */
1156 if (!context || !context->internal->initialized) {
1158 errno = EINVAL;
1159 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1160 TALLOC_FREE(frame);
1161 return NULL;
1165 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1167 errno = EBADF;
1168 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1169 TALLOC_FREE(frame);
1170 return NULL;
1174 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1176 errno = ENOTDIR;
1177 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1178 TALLOC_FREE(frame);
1179 return NULL;
1183 if (!dir->dir_next) {
1184 TALLOC_FREE(frame);
1185 return NULL;
1188 dirent = dir->dir_next->dirent;
1189 if (!dirent) {
1191 errno = ENOENT;
1192 TALLOC_FREE(frame);
1193 return NULL;
1197 dirp = &context->internal->dirent;
1198 maxlen = sizeof(context->internal->_dirent_name);
1200 ret = smbc_readdir_internal(context, dirp, dirent, maxlen);
1201 if (ret == -1) {
1202 errno = EINVAL;
1203 TALLOC_FREE(frame);
1204 return NULL;
1207 dir->dir_next = dir->dir_next->next;
1210 * If we are returning file entries, we
1211 * have a duplicate list in dirplus.
1213 * Update dirplus_next also so readdir and
1214 * readdirplus are kept in sync.
1216 if (dir->dirplus_list != NULL) {
1217 dir->dirplus_next = dir->dirplus_next->next;
1220 TALLOC_FREE(frame);
1221 return dirp;
1225 * Routine to get a directory entry with all attributes
1228 const struct libsmb_file_info *
1229 SMBC_readdirplus_ctx(SMBCCTX *context,
1230 SMBCFILE *dir)
1232 struct libsmb_file_info *smb_finfo = NULL;
1233 TALLOC_CTX *frame = talloc_stackframe();
1235 /* Check that all is ok first ... */
1237 if (context == NULL || !context->internal->initialized) {
1238 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1239 TALLOC_FREE(frame);
1240 errno = EINVAL;
1241 return NULL;
1244 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1245 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1246 TALLOC_FREE(frame);
1247 errno = EBADF;
1248 return NULL;
1251 if (dir->dirplus_next == NULL) {
1252 TALLOC_FREE(frame);
1253 return NULL;
1256 smb_finfo = dir->dirplus_next->smb_finfo;
1257 if (smb_finfo == NULL) {
1258 TALLOC_FREE(frame);
1259 errno = ENOENT;
1260 return NULL;
1262 dir->dirplus_next = dir->dirplus_next->next;
1265 * If we are returning file entries, we
1266 * have a duplicate list in dir_list
1268 * Update dir_next also so readdir and
1269 * readdirplus are kept in sync.
1271 if (dir->dir_list) {
1272 dir->dir_next = dir->dir_next->next;
1275 TALLOC_FREE(frame);
1276 return smb_finfo;
1280 * Routine to get a directory entry plus a filled in stat structure if
1281 * requested.
1284 const struct libsmb_file_info *SMBC_readdirplus2_ctx(SMBCCTX *context,
1285 SMBCFILE *dir,
1286 struct stat *st)
1288 struct libsmb_file_info *smb_finfo = NULL;
1289 struct smbc_dirplus_list *dp_list = NULL;
1290 ino_t ino;
1291 char *full_pathname = NULL;
1292 char *workgroup = NULL;
1293 char *server = NULL;
1294 uint16_t port = 0;
1295 char *share = NULL;
1296 char *path = NULL;
1297 char *user = NULL;
1298 char *password = NULL;
1299 char *options = NULL;
1300 int rc;
1301 TALLOC_CTX *frame = NULL;
1304 * Allow caller to pass in NULL for stat pointer if
1305 * required. This makes this call identical to
1306 * smbc_readdirplus().
1309 if (st == NULL) {
1310 return SMBC_readdirplus_ctx(context, dir);
1313 frame = talloc_stackframe();
1315 /* Check that all is ok first ... */
1316 if (context == NULL || !context->internal->initialized) {
1317 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1318 TALLOC_FREE(frame);
1319 errno = EINVAL;
1320 return NULL;
1323 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1324 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1325 TALLOC_FREE(frame);
1326 errno = EBADF;
1327 return NULL;
1330 dp_list = dir->dirplus_next;
1331 if (dp_list == NULL) {
1332 TALLOC_FREE(frame);
1333 return NULL;
1336 ino = (ino_t)dp_list->ino;
1338 smb_finfo = dp_list->smb_finfo;
1339 if (smb_finfo == NULL) {
1340 TALLOC_FREE(frame);
1341 errno = ENOENT;
1342 return NULL;
1345 full_pathname = talloc_asprintf(frame,
1346 "%s/%s",
1347 dir->fname,
1348 smb_finfo->name);
1349 if (full_pathname == NULL) {
1350 TALLOC_FREE(frame);
1351 errno = ENOENT;
1352 return NULL;
1355 rc = SMBC_parse_path(frame,
1356 context,
1357 full_pathname,
1358 &workgroup,
1359 &server,
1360 &port,
1361 &share,
1362 &path,
1363 &user,
1364 &password,
1365 &options);
1366 if (rc != 0) {
1367 TALLOC_FREE(frame);
1368 errno = ENOENT;
1369 return NULL;
1372 setup_stat(st,
1373 path,
1374 smb_finfo->size,
1375 smb_finfo->attrs,
1376 ino,
1377 dir->srv->dev,
1378 smb_finfo->atime_ts,
1379 smb_finfo->ctime_ts,
1380 smb_finfo->mtime_ts);
1382 TALLOC_FREE(full_pathname);
1384 dir->dirplus_next = dir->dirplus_next->next;
1387 * If we are returning file entries, we
1388 * have a duplicate list in dir_list
1390 * Update dir_next also so readdir and
1391 * readdirplus are kept in sync.
1393 if (dir->dir_list) {
1394 dir->dir_next = dir->dir_next->next;
1397 TALLOC_FREE(frame);
1398 return smb_finfo;
1402 * Routine to get directory entries
1406 SMBC_getdents_ctx(SMBCCTX *context,
1407 SMBCFILE *dir,
1408 struct smbc_dirent *dirp,
1409 int count)
1411 int rem = count;
1412 int reqd;
1413 int maxlen;
1414 char *ndir = (char *)dirp;
1415 struct smbc_dir_list *dirlist;
1416 TALLOC_CTX *frame = talloc_stackframe();
1418 /* Check that all is ok first ... */
1420 if (!context || !context->internal->initialized) {
1422 errno = EINVAL;
1423 TALLOC_FREE(frame);
1424 return -1;
1428 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1430 errno = EBADF;
1431 TALLOC_FREE(frame);
1432 return -1;
1436 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1438 errno = ENOTDIR;
1439 TALLOC_FREE(frame);
1440 return -1;
1445 * Now, retrieve the number of entries that will fit in what was passed
1446 * We have to figure out if the info is in the list, or we need to
1447 * send a request to the server to get the info.
1450 while ((dirlist = dir->dir_next)) {
1451 int ret;
1452 struct smbc_dirent *dirent;
1453 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1455 if (!dirlist->dirent) {
1457 errno = ENOENT; /* Bad error */
1458 TALLOC_FREE(frame);
1459 return -1;
1463 /* Do urlencoding of next entry, if so selected */
1464 dirent = &context->internal->dirent;
1465 maxlen = sizeof(context->internal->_dirent_name);
1466 ret = smbc_readdir_internal(context, dirent,
1467 dirlist->dirent, maxlen);
1468 if (ret == -1) {
1469 errno = EINVAL;
1470 TALLOC_FREE(frame);
1471 return -1;
1474 reqd = dirent->dirlen;
1476 if (rem < reqd) {
1478 if (rem < count) { /* We managed to copy something */
1480 errno = 0;
1481 TALLOC_FREE(frame);
1482 return count - rem;
1485 else { /* Nothing copied ... */
1487 errno = EINVAL; /* Not enough space ... */
1488 TALLOC_FREE(frame);
1489 return -1;
1495 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1497 currentEntry->comment = &currentEntry->name[0] +
1498 dirent->namelen + 1;
1500 ndir += reqd;
1501 rem -= reqd;
1503 /* Try and align the struct for the next entry
1504 on a valid pointer boundary by appending zeros */
1505 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1506 *ndir = '\0';
1507 rem--;
1508 ndir++;
1509 currentEntry->dirlen++;
1512 dir->dir_next = dirlist = dirlist -> next;
1515 * If we are returning file entries, we
1516 * have a duplicate list in dirplus.
1518 * Update dirplus_next also so readdir and
1519 * readdirplus are kept in sync.
1521 if (dir->dirplus_list != NULL) {
1522 dir->dirplus_next = dir->dirplus_next->next;
1526 TALLOC_FREE(frame);
1528 if (rem == count)
1529 return 0;
1530 else
1531 return count - rem;
1536 * Routine to create a directory ...
1540 SMBC_mkdir_ctx(SMBCCTX *context,
1541 const char *fname,
1542 mode_t mode)
1544 SMBCSRV *srv = NULL;
1545 char *server = NULL;
1546 char *share = NULL;
1547 char *user = NULL;
1548 char *password = NULL;
1549 char *workgroup = NULL;
1550 char *path = NULL;
1551 char *targetpath = NULL;
1552 uint16_t port = 0;
1553 struct cli_state *targetcli = NULL;
1554 struct cli_credentials *creds = NULL;
1555 TALLOC_CTX *frame = talloc_stackframe();
1556 NTSTATUS status;
1558 if (!context || !context->internal->initialized) {
1559 errno = EINVAL;
1560 TALLOC_FREE(frame);
1561 return -1;
1564 if (!fname) {
1565 errno = EINVAL;
1566 TALLOC_FREE(frame);
1567 return -1;
1570 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1572 if (SMBC_parse_path(frame,
1573 context,
1574 fname,
1575 &workgroup,
1576 &server,
1577 &port,
1578 &share,
1579 &path,
1580 &user,
1581 &password,
1582 NULL)) {
1583 errno = EINVAL;
1584 TALLOC_FREE(frame);
1585 return -1;
1588 if (!user || user[0] == (char)0) {
1589 user = talloc_strdup(frame, smbc_getUser(context));
1590 if (!user) {
1591 errno = ENOMEM;
1592 TALLOC_FREE(frame);
1593 return -1;
1597 srv = SMBC_server(frame, context, True,
1598 server, port, share, &workgroup, &user, &password);
1600 if (!srv) {
1602 TALLOC_FREE(frame);
1603 return -1; /* errno set by SMBC_server */
1607 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
1609 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1610 status = cli_resolve_path(frame, "",
1611 creds,
1612 srv->cli, path, &targetcli, &targetpath);
1613 if (!NT_STATUS_IS_OK(status)) {
1614 d_printf("Could not resolve %s\n", path);
1615 errno = ENOENT;
1616 TALLOC_FREE(frame);
1617 return -1;
1619 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1621 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1622 errno = SMBC_errno(context, targetcli);
1623 TALLOC_FREE(frame);
1624 return -1;
1628 TALLOC_FREE(frame);
1629 return 0;
1634 * Our list function simply checks to see if a directory is not empty
1637 static NTSTATUS
1638 rmdir_list_fn(struct file_info *finfo,
1639 const char *mask,
1640 void *state)
1642 if (strncmp(finfo->name, ".", 1) != 0 &&
1643 strncmp(finfo->name, "..", 2) != 0) {
1644 bool *smbc_rmdir_dirempty = (bool *)state;
1645 *smbc_rmdir_dirempty = false;
1647 return NT_STATUS_OK;
1651 * Routine to remove a directory
1655 SMBC_rmdir_ctx(SMBCCTX *context,
1656 const char *fname)
1658 SMBCSRV *srv = NULL;
1659 char *server = NULL;
1660 char *share = NULL;
1661 char *user = NULL;
1662 char *password = NULL;
1663 char *workgroup = NULL;
1664 char *path = NULL;
1665 char *targetpath = NULL;
1666 uint16_t port = 0;
1667 struct cli_state *targetcli = NULL;
1668 struct cli_credentials *creds = NULL;
1669 TALLOC_CTX *frame = talloc_stackframe();
1670 NTSTATUS status;
1672 if (!context || !context->internal->initialized) {
1673 errno = EINVAL;
1674 TALLOC_FREE(frame);
1675 return -1;
1678 if (!fname) {
1679 errno = EINVAL;
1680 TALLOC_FREE(frame);
1681 return -1;
1684 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1686 if (SMBC_parse_path(frame,
1687 context,
1688 fname,
1689 &workgroup,
1690 &server,
1691 &port,
1692 &share,
1693 &path,
1694 &user,
1695 &password,
1696 NULL)) {
1697 errno = EINVAL;
1698 TALLOC_FREE(frame);
1699 return -1;
1702 if (!user || user[0] == (char)0) {
1703 user = talloc_strdup(frame, smbc_getUser(context));
1704 if (!user) {
1705 errno = ENOMEM;
1706 TALLOC_FREE(frame);
1707 return -1;
1711 srv = SMBC_server(frame, context, True,
1712 server, port, share, &workgroup, &user, &password);
1714 if (!srv) {
1716 TALLOC_FREE(frame);
1717 return -1; /* errno set by SMBC_server */
1721 creds = get_cmdline_auth_info_creds(context->internal->auth_info),
1723 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1724 status = cli_resolve_path(frame, "",
1725 creds,
1726 srv->cli, path, &targetcli, &targetpath);
1727 if (!NT_STATUS_IS_OK(status)) {
1728 d_printf("Could not resolve %s\n", path);
1729 errno = ENOENT;
1730 TALLOC_FREE(frame);
1731 return -1;
1733 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1735 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1737 errno = SMBC_errno(context, targetcli);
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 DEBUG(5, ("smbc_rmdir: "
1761 "cli_list returned an error: %d\n",
1762 SMBC_errno(context, targetcli)));
1763 errno = EACCES;
1767 if (smbc_rmdir_dirempty)
1768 errno = EACCES;
1769 else
1770 errno = ENOTEMPTY;
1774 TALLOC_FREE(frame);
1775 return -1;
1779 TALLOC_FREE(frame);
1780 return 0;
1785 * Routine to return the current directory position
1788 off_t
1789 SMBC_telldir_ctx(SMBCCTX *context,
1790 SMBCFILE *dir)
1792 TALLOC_CTX *frame = talloc_stackframe();
1794 if (!context || !context->internal->initialized) {
1796 errno = EINVAL;
1797 TALLOC_FREE(frame);
1798 return -1;
1802 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1804 errno = EBADF;
1805 TALLOC_FREE(frame);
1806 return -1;
1810 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1812 errno = ENOTDIR;
1813 TALLOC_FREE(frame);
1814 return -1;
1818 /* See if we're already at the end. */
1819 if (dir->dir_next == NULL) {
1820 /* We are. */
1821 TALLOC_FREE(frame);
1822 return -1;
1826 * We return the pointer here as the offset
1828 TALLOC_FREE(frame);
1829 return (off_t)(long)dir->dir_next->dirent;
1833 * A routine to run down the list and see if the entry is OK
1834 * Modifies the dir list and the dirplus list (if it exists)
1835 * to point at the correct next entry on success.
1838 static bool update_dir_ents(SMBCFILE *dir, struct smbc_dirent *dirent)
1840 struct smbc_dir_list *tmp_dir = dir->dir_list;
1841 struct smbc_dirplus_list *tmp_dirplus = dir->dirplus_list;
1844 * Run down the list looking for what we want.
1845 * If we're enumerating files both dir_list
1846 * and dirplus_list contain the same entry
1847 * list, as they were seeded from the same
1848 * cli_list callback.
1850 * If we're enumerating servers then
1851 * dirplus_list will be NULL, so don't
1852 * update in that case.
1855 while (tmp_dir != NULL) {
1856 if (tmp_dir->dirent == dirent) {
1857 dir->dir_next = tmp_dir;
1858 if (tmp_dirplus != NULL) {
1859 dir->dirplus_next = tmp_dirplus;
1861 return true;
1863 tmp_dir = tmp_dir->next;
1864 if (tmp_dirplus != NULL) {
1865 tmp_dirplus = tmp_dirplus->next;
1868 return false;
1872 * Routine to seek on a directory
1876 SMBC_lseekdir_ctx(SMBCCTX *context,
1877 SMBCFILE *dir,
1878 off_t offset)
1880 long int l_offset = offset; /* Handle problems of size */
1881 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1882 TALLOC_CTX *frame = talloc_stackframe();
1883 bool ok;
1885 if (!context || !context->internal->initialized) {
1887 errno = EINVAL;
1888 TALLOC_FREE(frame);
1889 return -1;
1893 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1895 errno = ENOTDIR;
1896 TALLOC_FREE(frame);
1897 return -1;
1901 /* Now, check what we were passed and see if it is OK ... */
1903 if (dirent == NULL) { /* Seek to the begining of the list */
1905 dir->dir_next = dir->dir_list;
1907 /* Do the same for dirplus. */
1908 dir->dirplus_next = dir->dirplus_list;
1910 TALLOC_FREE(frame);
1911 return 0;
1915 if (offset == -1) { /* Seek to the end of the list */
1916 dir->dir_next = NULL;
1918 /* Do the same for dirplus. */
1919 dir->dirplus_next = NULL;
1921 TALLOC_FREE(frame);
1922 return 0;
1926 * Run down the list and make sure that the entry is OK.
1927 * Update the position of both dir and dirplus lists.
1930 ok = update_dir_ents(dir, dirent);
1931 if (!ok) {
1932 errno = EINVAL; /* Bad entry */
1933 TALLOC_FREE(frame);
1934 return -1;
1937 TALLOC_FREE(frame);
1938 return 0;
1942 * Routine to fstat a dir
1946 SMBC_fstatdir_ctx(SMBCCTX *context,
1947 SMBCFILE *dir,
1948 struct stat *st)
1951 if (!context || !context->internal->initialized) {
1953 errno = EINVAL;
1954 return -1;
1957 /* No code yet ... */
1958 return 0;
1962 SMBC_chmod_ctx(SMBCCTX *context,
1963 const char *fname,
1964 mode_t newmode)
1966 SMBCSRV *srv = NULL;
1967 char *server = NULL;
1968 char *share = NULL;
1969 char *user = NULL;
1970 char *password = NULL;
1971 char *workgroup = NULL;
1972 char *targetpath = NULL;
1973 struct cli_state *targetcli = NULL;
1974 char *path = NULL;
1975 uint32_t attr;
1976 uint16_t port = 0;
1977 struct cli_credentials *creds = NULL;
1978 TALLOC_CTX *frame = talloc_stackframe();
1979 NTSTATUS status;
1981 if (!context || !context->internal->initialized) {
1983 errno = EINVAL; /* Best I can think of ... */
1984 TALLOC_FREE(frame);
1985 return -1;
1988 if (!fname) {
1989 errno = EINVAL;
1990 TALLOC_FREE(frame);
1991 return -1;
1994 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1996 if (SMBC_parse_path(frame,
1997 context,
1998 fname,
1999 &workgroup,
2000 &server,
2001 &port,
2002 &share,
2003 &path,
2004 &user,
2005 &password,
2006 NULL)) {
2007 errno = EINVAL;
2008 TALLOC_FREE(frame);
2009 return -1;
2012 if (!user || user[0] == (char)0) {
2013 user = talloc_strdup(frame, smbc_getUser(context));
2014 if (!user) {
2015 errno = ENOMEM;
2016 TALLOC_FREE(frame);
2017 return -1;
2021 srv = SMBC_server(frame, context, True,
2022 server, port, share, &workgroup, &user, &password);
2024 if (!srv) {
2025 TALLOC_FREE(frame);
2026 return -1; /* errno set by SMBC_server */
2029 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
2031 /*d_printf(">>>unlink: resolving %s\n", path);*/
2032 status = cli_resolve_path(frame, "",
2033 creds,
2034 srv->cli, path, &targetcli, &targetpath);
2035 if (!NT_STATUS_IS_OK(status)) {
2036 d_printf("Could not resolve %s\n", path);
2037 errno = ENOENT;
2038 TALLOC_FREE(frame);
2039 return -1;
2042 attr = 0;
2044 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) attr |= FILE_ATTRIBUTE_READONLY;
2045 if ((newmode & S_IXUSR) && lp_map_archive(-1)) attr |= FILE_ATTRIBUTE_ARCHIVE;
2046 if ((newmode & S_IXGRP) && lp_map_system(-1)) attr |= FILE_ATTRIBUTE_SYSTEM;
2047 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) attr |= FILE_ATTRIBUTE_HIDDEN;
2049 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, attr, 0))) {
2050 errno = SMBC_errno(context, targetcli);
2051 TALLOC_FREE(frame);
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 = get_cmdline_auth_info_creds(context->internal->auth_info);
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 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
2243 errno = SMBC_errno(context, targetcli);
2245 if (errno == EACCES) { /* Check if the file is a directory */
2247 int saverr = errno;
2248 struct stat sb = {0};
2249 bool ok;
2251 ok = SMBC_getatr(context, srv, path, &sb);
2252 if (!ok) {
2253 /* Hmmm, bad error ... What? */
2255 errno = SMBC_errno(context, targetcli);
2256 TALLOC_FREE(frame);
2257 return -1;
2260 else {
2262 if (S_ISDIR(sb.st_mode))
2263 errno = EISDIR;
2264 else
2265 errno = saverr; /* Restore this */
2270 TALLOC_FREE(frame);
2271 return -1;
2275 TALLOC_FREE(frame);
2276 return 0; /* Success ... */
2281 * Routine to rename() a file
2285 SMBC_rename_ctx(SMBCCTX *ocontext,
2286 const char *oname,
2287 SMBCCTX *ncontext,
2288 const char *nname)
2290 char *server1 = NULL;
2291 char *share1 = NULL;
2292 char *server2 = NULL;
2293 char *share2 = NULL;
2294 char *user1 = NULL;
2295 char *user2 = NULL;
2296 char *password1 = NULL;
2297 char *password2 = NULL;
2298 char *workgroup = NULL;
2299 char *path1 = NULL;
2300 char *path2 = NULL;
2301 char *targetpath1 = NULL;
2302 char *targetpath2 = NULL;
2303 struct cli_state *targetcli1 = NULL;
2304 struct cli_state *targetcli2 = NULL;
2305 SMBCSRV *srv = NULL;
2306 uint16_t port1 = 0;
2307 uint16_t port2 = 0;
2308 struct cli_credentials *ocreds = NULL;
2309 struct cli_credentials *ncreds = NULL;
2310 TALLOC_CTX *frame = talloc_stackframe();
2311 NTSTATUS status;
2313 if (!ocontext || !ncontext ||
2314 !ocontext->internal->initialized ||
2315 !ncontext->internal->initialized) {
2317 errno = EINVAL; /* Best I can think of ... */
2318 TALLOC_FREE(frame);
2319 return -1;
2322 if (!oname || !nname) {
2323 errno = EINVAL;
2324 TALLOC_FREE(frame);
2325 return -1;
2328 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
2330 if (SMBC_parse_path(frame,
2331 ocontext,
2332 oname,
2333 &workgroup,
2334 &server1,
2335 &port1,
2336 &share1,
2337 &path1,
2338 &user1,
2339 &password1,
2340 NULL)) {
2341 errno = EINVAL;
2342 TALLOC_FREE(frame);
2343 return -1;
2346 if (!user1 || user1[0] == (char)0) {
2347 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
2348 if (!user1) {
2349 errno = ENOMEM;
2350 TALLOC_FREE(frame);
2351 return -1;
2355 if (SMBC_parse_path(frame,
2356 ncontext,
2357 nname,
2358 NULL,
2359 &server2,
2360 &port2,
2361 &share2,
2362 &path2,
2363 &user2,
2364 &password2,
2365 NULL)) {
2366 errno = EINVAL;
2367 TALLOC_FREE(frame);
2368 return -1;
2371 if (!user2 || user2[0] == (char)0) {
2372 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
2373 if (!user2) {
2374 errno = ENOMEM;
2375 TALLOC_FREE(frame);
2376 return -1;
2380 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2381 strcmp(user1, user2)) {
2382 /* Can't rename across file systems, or users?? */
2383 errno = EXDEV;
2384 TALLOC_FREE(frame);
2385 return -1;
2388 srv = SMBC_server(frame, ocontext, True,
2389 server1, port1, share1, &workgroup, &user1, &password1);
2390 if (!srv) {
2391 TALLOC_FREE(frame);
2392 return -1;
2396 /* set the credentials to make DFS work */
2397 smbc_set_credentials_with_fallback(ocontext,
2398 workgroup,
2399 user1,
2400 password1);
2402 /*d_printf(">>>rename: resolving %s\n", path1);*/
2403 ocreds = get_cmdline_auth_info_creds(ocontext->internal->auth_info);
2405 status = cli_resolve_path(frame, "",
2406 ocreds,
2407 srv->cli, path1, &targetcli1, &targetpath1);
2408 if (!NT_STATUS_IS_OK(status)) {
2409 d_printf("Could not resolve %s\n", path1);
2410 errno = ENOENT;
2411 TALLOC_FREE(frame);
2412 return -1;
2415 /* set the credentials to make DFS work */
2416 smbc_set_credentials_with_fallback(ncontext,
2417 workgroup,
2418 user2,
2419 password2);
2421 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2422 /*d_printf(">>>rename: resolving %s\n", path2);*/
2423 ncreds = get_cmdline_auth_info_creds(ncontext->internal->auth_info);
2425 status = cli_resolve_path(frame, "",
2426 ncreds,
2427 srv->cli, path2, &targetcli2, &targetpath2);
2428 if (!NT_STATUS_IS_OK(status)) {
2429 d_printf("Could not resolve %s\n", path2);
2430 errno = ENOENT;
2431 TALLOC_FREE(frame);
2432 return -1;
2434 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2436 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2437 strcmp(targetcli1->share, targetcli2->share))
2439 /* can't rename across file systems */
2440 errno = EXDEV;
2441 TALLOC_FREE(frame);
2442 return -1;
2445 if (!NT_STATUS_IS_OK(
2446 cli_rename(targetcli1, targetpath1, targetpath2, false))) {
2447 int eno = SMBC_errno(ocontext, targetcli1);
2449 if (eno != EEXIST ||
2450 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2,
2451 FILE_ATTRIBUTE_SYSTEM |
2452 FILE_ATTRIBUTE_HIDDEN)) ||
2453 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
2454 targetpath2, false))) {
2456 errno = eno;
2457 TALLOC_FREE(frame);
2458 return -1;
2463 TALLOC_FREE(frame);
2464 return 0; /* Success */
2467 struct smbc_notify_cb_state {
2468 struct tevent_context *ev;
2469 struct cli_state *cli;
2470 uint16_t fnum;
2471 bool recursive;
2472 uint32_t completion_filter;
2473 unsigned callback_timeout_ms;
2474 smbc_notify_callback_fn cb;
2475 void *private_data;
2478 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2479 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2481 static struct tevent_req *smbc_notify_cb_send(
2482 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2483 uint16_t fnum, bool recursive, uint32_t completion_filter,
2484 unsigned callback_timeout_ms,
2485 smbc_notify_callback_fn cb, void *private_data)
2487 struct tevent_req *req, *subreq;
2488 struct smbc_notify_cb_state *state;
2490 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2491 if (req == NULL) {
2492 return NULL;
2494 state->ev = ev;
2495 state->cli = cli;
2496 state->fnum = fnum;
2497 state->recursive = recursive;
2498 state->completion_filter = completion_filter;
2499 state->callback_timeout_ms = callback_timeout_ms;
2500 state->cb = cb;
2501 state->private_data = private_data;
2503 subreq = cli_notify_send(
2504 state, state->ev, state->cli, state->fnum, 1000,
2505 state->completion_filter, state->recursive);
2506 if (tevent_req_nomem(subreq, req)) {
2507 return tevent_req_post(req, ev);
2509 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2511 if (state->callback_timeout_ms == 0) {
2512 return req;
2515 subreq = tevent_wakeup_send(
2516 state, state->ev,
2517 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2518 state->callback_timeout_ms*1000));
2519 if (tevent_req_nomem(subreq, req)) {
2520 return tevent_req_post(req, ev);
2522 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2524 return req;
2527 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2529 struct tevent_req *req = tevent_req_callback_data(
2530 subreq, struct tevent_req);
2531 struct smbc_notify_cb_state *state = tevent_req_data(
2532 req, struct smbc_notify_cb_state);
2533 uint32_t num_changes;
2534 struct notify_change *changes;
2535 NTSTATUS status;
2536 int cb_ret;
2538 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2539 TALLOC_FREE(subreq);
2540 if (tevent_req_nterror(req, status)) {
2541 return;
2545 struct smbc_notify_callback_action actions[num_changes];
2546 uint32_t i;
2548 for (i=0; i<num_changes; i++) {
2549 actions[i].action = changes[i].action;
2550 actions[i].filename = changes[i].name;
2553 cb_ret = state->cb(actions, num_changes, state->private_data);
2556 TALLOC_FREE(changes);
2558 if (cb_ret != 0) {
2559 tevent_req_done(req);
2560 return;
2563 subreq = cli_notify_send(
2564 state, state->ev, state->cli, state->fnum, 1000,
2565 state->completion_filter, state->recursive);
2566 if (tevent_req_nomem(subreq, req)) {
2567 return;
2569 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2572 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2574 struct tevent_req *req = tevent_req_callback_data(
2575 subreq, struct tevent_req);
2576 struct smbc_notify_cb_state *state = tevent_req_data(
2577 req, struct smbc_notify_cb_state);
2578 int cb_ret;
2579 bool ok;
2581 ok = tevent_wakeup_recv(subreq);
2582 TALLOC_FREE(subreq);
2583 if (!ok) {
2584 tevent_req_oom(req);
2585 return;
2588 cb_ret = state->cb(NULL, 0, state->private_data);
2589 if (cb_ret != 0) {
2590 tevent_req_done(req);
2591 return;
2594 subreq = tevent_wakeup_send(
2595 state, state->ev,
2596 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2597 state->callback_timeout_ms*1000));
2598 if (tevent_req_nomem(subreq, req)) {
2599 return;
2601 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2604 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2606 return tevent_req_simple_recv_ntstatus(req);
2609 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2610 bool recursive, uint32_t completion_filter,
2611 unsigned callback_timeout_ms,
2612 smbc_notify_callback_fn cb, void *private_data)
2614 TALLOC_CTX *frame = talloc_stackframe();
2615 struct tevent_context *ev;
2616 struct tevent_req *req;
2617 NTSTATUS status = NT_STATUS_NO_MEMORY;
2619 ev = samba_tevent_context_init(frame);
2620 if (ev == NULL) {
2621 goto fail;
2623 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2624 completion_filter,
2625 callback_timeout_ms, cb, private_data);
2626 if (req == NULL) {
2627 goto fail;
2629 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2630 goto fail;
2632 status = smbc_notify_cb_recv(req);
2633 TALLOC_FREE(req);
2634 fail:
2635 TALLOC_FREE(frame);
2636 return status;
2640 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2641 uint32_t completion_filter, unsigned callback_timeout_ms,
2642 smbc_notify_callback_fn cb, void *private_data)
2644 TALLOC_CTX *frame = talloc_stackframe();
2645 struct cli_state *cli;
2646 char *server = NULL;
2647 char *share = NULL;
2648 char *user = NULL;
2649 char *password = NULL;
2650 char *options = NULL;
2651 char *workgroup = NULL;
2652 char *path = NULL;
2653 uint16_t port;
2654 NTSTATUS status;
2655 uint16_t fnum;
2657 if ((context == NULL) || !context->internal->initialized) {
2658 TALLOC_FREE(frame);
2659 errno = EINVAL;
2660 return -1;
2662 if (!SMBC_dlist_contains(context->internal->files, dir)) {
2663 TALLOC_FREE(frame);
2664 errno = EBADF;
2665 return -1;
2668 if (SMBC_parse_path(frame,
2669 context,
2670 dir->fname,
2671 &workgroup,
2672 &server,
2673 &port,
2674 &share,
2675 &path,
2676 &user,
2677 &password,
2678 &options)) {
2679 DEBUG(4, ("no valid path\n"));
2680 TALLOC_FREE(frame);
2681 errno = EINVAL + 8194;
2682 return -1;
2685 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2686 "path='%s' options='%s'\n",
2687 dir->fname, server, share, path, options));
2689 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2690 (int)recursive, completion_filter));
2692 cli = dir->srv->cli;
2693 status = cli_ntcreate(
2694 cli, path, 0, FILE_READ_DATA, 0,
2695 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2696 FILE_OPEN, 0, 0, &fnum, NULL);
2697 if (!NT_STATUS_IS_OK(status)) {
2698 int err = SMBC_errno(context, cli);
2699 TALLOC_FREE(frame);
2700 errno = err;
2701 return -1;
2704 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2705 callback_timeout_ms, cb, private_data);
2706 if (!NT_STATUS_IS_OK(status)) {
2707 int err = SMBC_errno(context, cli);
2708 cli_close(cli, fnum);
2709 TALLOC_FREE(frame);
2710 errno = err;
2711 return -1;
2714 cli_close(cli, fnum);
2716 TALLOC_FREE(frame);
2717 return 0;