s3:libsmb: Return early if dir is NULL
[Samba.git] / source3 / libsmb / libsmb_dir.c
blobf1596b743efc55b41b87ea7f16822fef918c5d9f
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 = NULL;
1046 if (!context || !context->internal->initialized) {
1047 errno = EINVAL;
1048 return -1;
1051 if (dir == NULL) {
1052 return 0;
1055 frame = talloc_stackframe();
1057 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1058 errno = EBADF;
1059 TALLOC_FREE(frame);
1060 return -1;
1063 remove_dir(dir); /* Clean it up */
1064 remove_dirplus(dir);
1066 DLIST_REMOVE(context->internal->files, dir);
1068 SAFE_FREE(dir->fname);
1069 SAFE_FREE(dir); /* Free the space too */
1071 TALLOC_FREE(frame);
1072 return 0;
1076 static int
1077 smbc_readdir_internal(SMBCCTX * context,
1078 struct smbc_dirent *dest,
1079 struct smbc_dirent *src,
1080 int max_namebuf_len)
1082 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
1083 int remaining_len;
1085 /* url-encode the name. get back remaining buffer space */
1086 remaining_len =
1087 smbc_urlencode(dest->name, src->name, max_namebuf_len);
1089 /* -1 means no null termination. */
1090 if (remaining_len < 0) {
1091 return -1;
1094 /* We now know the name length */
1095 dest->namelen = strlen(dest->name);
1097 if (dest->namelen + 1 < 1) {
1098 /* Integer wrap. */
1099 return -1;
1102 if (dest->namelen + 1 >= max_namebuf_len) {
1103 /* Out of space for comment. */
1104 return -1;
1107 /* Save the pointer to the beginning of the comment */
1108 dest->comment = dest->name + dest->namelen + 1;
1110 if (remaining_len < 1) {
1111 /* No room for comment null termination. */
1112 return -1;
1115 /* Copy the comment */
1116 strlcpy(dest->comment, src->comment, remaining_len);
1118 /* Save other fields */
1119 dest->smbc_type = src->smbc_type;
1120 dest->commentlen = strlen(dest->comment);
1121 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
1122 (char *) dest);
1123 } else {
1125 /* No encoding. Just copy the entry as is. */
1126 if (src->dirlen > max_namebuf_len) {
1127 return -1;
1129 memcpy(dest, src, src->dirlen);
1130 if (src->namelen + 1 < 1) {
1131 /* Integer wrap */
1132 return -1;
1134 if (src->namelen + 1 >= max_namebuf_len) {
1135 /* Comment off the end. */
1136 return -1;
1138 dest->comment = (char *)(&dest->name + src->namelen + 1);
1140 return 0;
1144 * Routine to get a directory entry
1147 struct smbc_dirent *
1148 SMBC_readdir_ctx(SMBCCTX *context,
1149 SMBCFILE *dir)
1151 int maxlen;
1152 int ret;
1153 struct smbc_dirent *dirp, *dirent;
1154 TALLOC_CTX *frame = talloc_stackframe();
1156 /* Check that all is ok first ... */
1158 if (!context || !context->internal->initialized) {
1160 errno = EINVAL;
1161 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1162 TALLOC_FREE(frame);
1163 return NULL;
1167 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1169 errno = EBADF;
1170 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1171 TALLOC_FREE(frame);
1172 return NULL;
1176 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1178 errno = ENOTDIR;
1179 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1180 TALLOC_FREE(frame);
1181 return NULL;
1185 if (!dir->dir_next) {
1186 TALLOC_FREE(frame);
1187 return NULL;
1190 dirent = dir->dir_next->dirent;
1191 if (!dirent) {
1193 errno = ENOENT;
1194 TALLOC_FREE(frame);
1195 return NULL;
1199 dirp = &context->internal->dirent;
1200 maxlen = sizeof(context->internal->_dirent_name);
1202 ret = smbc_readdir_internal(context, dirp, dirent, maxlen);
1203 if (ret == -1) {
1204 errno = EINVAL;
1205 TALLOC_FREE(frame);
1206 return NULL;
1209 dir->dir_next = dir->dir_next->next;
1212 * If we are returning file entries, we
1213 * have a duplicate list in dirplus.
1215 * Update dirplus_next also so readdir and
1216 * readdirplus are kept in sync.
1218 if (dir->dirplus_list != NULL) {
1219 dir->dirplus_next = dir->dirplus_next->next;
1222 TALLOC_FREE(frame);
1223 return dirp;
1227 * Routine to get a directory entry with all attributes
1230 const struct libsmb_file_info *
1231 SMBC_readdirplus_ctx(SMBCCTX *context,
1232 SMBCFILE *dir)
1234 struct libsmb_file_info *smb_finfo = NULL;
1235 TALLOC_CTX *frame = talloc_stackframe();
1237 /* Check that all is ok first ... */
1239 if (context == NULL || !context->internal->initialized) {
1240 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1241 TALLOC_FREE(frame);
1242 errno = EINVAL;
1243 return NULL;
1246 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1247 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1248 TALLOC_FREE(frame);
1249 errno = EBADF;
1250 return NULL;
1253 if (dir->dirplus_next == NULL) {
1254 TALLOC_FREE(frame);
1255 return NULL;
1258 smb_finfo = dir->dirplus_next->smb_finfo;
1259 if (smb_finfo == NULL) {
1260 TALLOC_FREE(frame);
1261 errno = ENOENT;
1262 return NULL;
1264 dir->dirplus_next = dir->dirplus_next->next;
1267 * If we are returning file entries, we
1268 * have a duplicate list in dir_list
1270 * Update dir_next also so readdir and
1271 * readdirplus are kept in sync.
1273 if (dir->dir_list) {
1274 dir->dir_next = dir->dir_next->next;
1277 TALLOC_FREE(frame);
1278 return smb_finfo;
1282 * Routine to get a directory entry plus a filled in stat structure if
1283 * requested.
1286 const struct libsmb_file_info *SMBC_readdirplus2_ctx(SMBCCTX *context,
1287 SMBCFILE *dir,
1288 struct stat *st)
1290 struct libsmb_file_info *smb_finfo = NULL;
1291 struct smbc_dirplus_list *dp_list = NULL;
1292 ino_t ino;
1293 char *full_pathname = NULL;
1294 char *workgroup = NULL;
1295 char *server = NULL;
1296 uint16_t port = 0;
1297 char *share = NULL;
1298 char *path = NULL;
1299 char *user = NULL;
1300 char *password = NULL;
1301 char *options = NULL;
1302 int rc;
1303 TALLOC_CTX *frame = NULL;
1306 * Allow caller to pass in NULL for stat pointer if
1307 * required. This makes this call identical to
1308 * smbc_readdirplus().
1311 if (st == NULL) {
1312 return SMBC_readdirplus_ctx(context, dir);
1315 frame = talloc_stackframe();
1317 /* Check that all is ok first ... */
1318 if (context == NULL || !context->internal->initialized) {
1319 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1320 TALLOC_FREE(frame);
1321 errno = EINVAL;
1322 return NULL;
1325 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1326 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1327 TALLOC_FREE(frame);
1328 errno = EBADF;
1329 return NULL;
1332 dp_list = dir->dirplus_next;
1333 if (dp_list == NULL) {
1334 TALLOC_FREE(frame);
1335 return NULL;
1338 ino = (ino_t)dp_list->ino;
1340 smb_finfo = dp_list->smb_finfo;
1341 if (smb_finfo == NULL) {
1342 TALLOC_FREE(frame);
1343 errno = ENOENT;
1344 return NULL;
1347 full_pathname = talloc_asprintf(frame,
1348 "%s/%s",
1349 dir->fname,
1350 smb_finfo->name);
1351 if (full_pathname == NULL) {
1352 TALLOC_FREE(frame);
1353 errno = ENOENT;
1354 return NULL;
1357 rc = SMBC_parse_path(frame,
1358 context,
1359 full_pathname,
1360 &workgroup,
1361 &server,
1362 &port,
1363 &share,
1364 &path,
1365 &user,
1366 &password,
1367 &options);
1368 if (rc != 0) {
1369 TALLOC_FREE(frame);
1370 errno = ENOENT;
1371 return NULL;
1374 setup_stat(st,
1375 path,
1376 smb_finfo->size,
1377 smb_finfo->attrs,
1378 ino,
1379 dir->srv->dev,
1380 smb_finfo->atime_ts,
1381 smb_finfo->ctime_ts,
1382 smb_finfo->mtime_ts);
1384 TALLOC_FREE(full_pathname);
1386 dir->dirplus_next = dir->dirplus_next->next;
1389 * If we are returning file entries, we
1390 * have a duplicate list in dir_list
1392 * Update dir_next also so readdir and
1393 * readdirplus are kept in sync.
1395 if (dir->dir_list) {
1396 dir->dir_next = dir->dir_next->next;
1399 TALLOC_FREE(frame);
1400 return smb_finfo;
1404 * Routine to get directory entries
1408 SMBC_getdents_ctx(SMBCCTX *context,
1409 SMBCFILE *dir,
1410 struct smbc_dirent *dirp,
1411 int count)
1413 int rem = count;
1414 int reqd;
1415 int maxlen;
1416 char *ndir = (char *)dirp;
1417 struct smbc_dir_list *dirlist;
1418 TALLOC_CTX *frame = talloc_stackframe();
1420 /* Check that all is ok first ... */
1422 if (!context || !context->internal->initialized) {
1424 errno = EINVAL;
1425 TALLOC_FREE(frame);
1426 return -1;
1430 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1432 errno = EBADF;
1433 TALLOC_FREE(frame);
1434 return -1;
1438 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1440 errno = ENOTDIR;
1441 TALLOC_FREE(frame);
1442 return -1;
1447 * Now, retrieve the number of entries that will fit in what was passed
1448 * We have to figure out if the info is in the list, or we need to
1449 * send a request to the server to get the info.
1452 while ((dirlist = dir->dir_next)) {
1453 int ret;
1454 struct smbc_dirent *dirent;
1455 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1457 if (!dirlist->dirent) {
1459 errno = ENOENT; /* Bad error */
1460 TALLOC_FREE(frame);
1461 return -1;
1465 /* Do urlencoding of next entry, if so selected */
1466 dirent = &context->internal->dirent;
1467 maxlen = sizeof(context->internal->_dirent_name);
1468 ret = smbc_readdir_internal(context, dirent,
1469 dirlist->dirent, maxlen);
1470 if (ret == -1) {
1471 errno = EINVAL;
1472 TALLOC_FREE(frame);
1473 return -1;
1476 reqd = dirent->dirlen;
1478 if (rem < reqd) {
1480 if (rem < count) { /* We managed to copy something */
1482 errno = 0;
1483 TALLOC_FREE(frame);
1484 return count - rem;
1487 else { /* Nothing copied ... */
1489 errno = EINVAL; /* Not enough space ... */
1490 TALLOC_FREE(frame);
1491 return -1;
1497 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1499 currentEntry->comment = &currentEntry->name[0] +
1500 dirent->namelen + 1;
1502 ndir += reqd;
1503 rem -= reqd;
1505 /* Try and align the struct for the next entry
1506 on a valid pointer boundary by appending zeros */
1507 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1508 *ndir = '\0';
1509 rem--;
1510 ndir++;
1511 currentEntry->dirlen++;
1514 dir->dir_next = dirlist = dirlist -> next;
1517 * If we are returning file entries, we
1518 * have a duplicate list in dirplus.
1520 * Update dirplus_next also so readdir and
1521 * readdirplus are kept in sync.
1523 if (dir->dirplus_list != NULL) {
1524 dir->dirplus_next = dir->dirplus_next->next;
1528 TALLOC_FREE(frame);
1530 if (rem == count)
1531 return 0;
1532 else
1533 return count - rem;
1538 * Routine to create a directory ...
1542 SMBC_mkdir_ctx(SMBCCTX *context,
1543 const char *fname,
1544 mode_t mode)
1546 SMBCSRV *srv = NULL;
1547 char *server = NULL;
1548 char *share = NULL;
1549 char *user = NULL;
1550 char *password = NULL;
1551 char *workgroup = NULL;
1552 char *path = NULL;
1553 char *targetpath = NULL;
1554 uint16_t port = 0;
1555 struct cli_state *targetcli = NULL;
1556 struct cli_credentials *creds = NULL;
1557 TALLOC_CTX *frame = talloc_stackframe();
1558 NTSTATUS status;
1560 if (!context || !context->internal->initialized) {
1561 errno = EINVAL;
1562 TALLOC_FREE(frame);
1563 return -1;
1566 if (!fname) {
1567 errno = EINVAL;
1568 TALLOC_FREE(frame);
1569 return -1;
1572 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1574 if (SMBC_parse_path(frame,
1575 context,
1576 fname,
1577 &workgroup,
1578 &server,
1579 &port,
1580 &share,
1581 &path,
1582 &user,
1583 &password,
1584 NULL)) {
1585 errno = EINVAL;
1586 TALLOC_FREE(frame);
1587 return -1;
1590 if (!user || user[0] == (char)0) {
1591 user = talloc_strdup(frame, smbc_getUser(context));
1592 if (!user) {
1593 errno = ENOMEM;
1594 TALLOC_FREE(frame);
1595 return -1;
1599 srv = SMBC_server(frame, context, True,
1600 server, port, share, &workgroup, &user, &password);
1602 if (!srv) {
1604 TALLOC_FREE(frame);
1605 return -1; /* errno set by SMBC_server */
1609 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
1611 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1612 status = cli_resolve_path(frame, "",
1613 creds,
1614 srv->cli, path, &targetcli, &targetpath);
1615 if (!NT_STATUS_IS_OK(status)) {
1616 d_printf("Could not resolve %s\n", path);
1617 errno = ENOENT;
1618 TALLOC_FREE(frame);
1619 return -1;
1621 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1623 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1624 errno = SMBC_errno(context, targetcli);
1625 TALLOC_FREE(frame);
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 = get_cmdline_auth_info_creds(context->internal->auth_info),
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 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1739 errno = SMBC_errno(context, targetcli);
1741 if (errno == EACCES) { /* Check if the dir empty or not */
1743 /* Local storage to avoid buffer overflows */
1744 char *lpath;
1745 bool smbc_rmdir_dirempty = true;
1747 lpath = talloc_asprintf(frame, "%s\\*",
1748 targetpath);
1749 if (!lpath) {
1750 errno = ENOMEM;
1751 TALLOC_FREE(frame);
1752 return -1;
1755 status = cli_list(targetcli, lpath,
1756 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1757 rmdir_list_fn,
1758 &smbc_rmdir_dirempty);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 /* Fix errno to ignore latest error ... */
1762 DEBUG(5, ("smbc_rmdir: "
1763 "cli_list returned an error: %d\n",
1764 SMBC_errno(context, targetcli)));
1765 errno = EACCES;
1769 if (smbc_rmdir_dirempty)
1770 errno = EACCES;
1771 else
1772 errno = ENOTEMPTY;
1776 TALLOC_FREE(frame);
1777 return -1;
1781 TALLOC_FREE(frame);
1782 return 0;
1787 * Routine to return the current directory position
1790 off_t
1791 SMBC_telldir_ctx(SMBCCTX *context,
1792 SMBCFILE *dir)
1794 TALLOC_CTX *frame = talloc_stackframe();
1796 if (!context || !context->internal->initialized) {
1798 errno = EINVAL;
1799 TALLOC_FREE(frame);
1800 return -1;
1804 if (!SMBC_dlist_contains(context->internal->files, dir)) {
1806 errno = EBADF;
1807 TALLOC_FREE(frame);
1808 return -1;
1812 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1814 errno = ENOTDIR;
1815 TALLOC_FREE(frame);
1816 return -1;
1820 /* See if we're already at the end. */
1821 if (dir->dir_next == NULL) {
1822 /* We are. */
1823 TALLOC_FREE(frame);
1824 return -1;
1828 * We return the pointer here as the offset
1830 TALLOC_FREE(frame);
1831 return (off_t)(long)dir->dir_next->dirent;
1835 * A routine to run down the list and see if the entry is OK
1836 * Modifies the dir list and the dirplus list (if it exists)
1837 * to point at the correct next entry on success.
1840 static bool update_dir_ents(SMBCFILE *dir, struct smbc_dirent *dirent)
1842 struct smbc_dir_list *tmp_dir = dir->dir_list;
1843 struct smbc_dirplus_list *tmp_dirplus = dir->dirplus_list;
1846 * Run down the list looking for what we want.
1847 * If we're enumerating files both dir_list
1848 * and dirplus_list contain the same entry
1849 * list, as they were seeded from the same
1850 * cli_list callback.
1852 * If we're enumerating servers then
1853 * dirplus_list will be NULL, so don't
1854 * update in that case.
1857 while (tmp_dir != NULL) {
1858 if (tmp_dir->dirent == dirent) {
1859 dir->dir_next = tmp_dir;
1860 if (tmp_dirplus != NULL) {
1861 dir->dirplus_next = tmp_dirplus;
1863 return true;
1865 tmp_dir = tmp_dir->next;
1866 if (tmp_dirplus != NULL) {
1867 tmp_dirplus = tmp_dirplus->next;
1870 return false;
1874 * Routine to seek on a directory
1878 SMBC_lseekdir_ctx(SMBCCTX *context,
1879 SMBCFILE *dir,
1880 off_t offset)
1882 long int l_offset = offset; /* Handle problems of size */
1883 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1884 TALLOC_CTX *frame = talloc_stackframe();
1885 bool ok;
1887 if (!context || !context->internal->initialized) {
1889 errno = EINVAL;
1890 TALLOC_FREE(frame);
1891 return -1;
1895 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1897 errno = ENOTDIR;
1898 TALLOC_FREE(frame);
1899 return -1;
1903 /* Now, check what we were passed and see if it is OK ... */
1905 if (dirent == NULL) { /* Seek to the begining of the list */
1907 dir->dir_next = dir->dir_list;
1909 /* Do the same for dirplus. */
1910 dir->dirplus_next = dir->dirplus_list;
1912 TALLOC_FREE(frame);
1913 return 0;
1917 if (offset == -1) { /* Seek to the end of the list */
1918 dir->dir_next = NULL;
1920 /* Do the same for dirplus. */
1921 dir->dirplus_next = NULL;
1923 TALLOC_FREE(frame);
1924 return 0;
1928 * Run down the list and make sure that the entry is OK.
1929 * Update the position of both dir and dirplus lists.
1932 ok = update_dir_ents(dir, dirent);
1933 if (!ok) {
1934 errno = EINVAL; /* Bad entry */
1935 TALLOC_FREE(frame);
1936 return -1;
1939 TALLOC_FREE(frame);
1940 return 0;
1944 * Routine to fstat a dir
1948 SMBC_fstatdir_ctx(SMBCCTX *context,
1949 SMBCFILE *dir,
1950 struct stat *st)
1953 if (!context || !context->internal->initialized) {
1955 errno = EINVAL;
1956 return -1;
1959 /* No code yet ... */
1960 return 0;
1964 SMBC_chmod_ctx(SMBCCTX *context,
1965 const char *fname,
1966 mode_t newmode)
1968 SMBCSRV *srv = NULL;
1969 char *server = NULL;
1970 char *share = NULL;
1971 char *user = NULL;
1972 char *password = NULL;
1973 char *workgroup = NULL;
1974 char *targetpath = NULL;
1975 struct cli_state *targetcli = NULL;
1976 char *path = NULL;
1977 uint32_t attr;
1978 uint16_t port = 0;
1979 struct cli_credentials *creds = NULL;
1980 TALLOC_CTX *frame = talloc_stackframe();
1981 NTSTATUS status;
1983 if (!context || !context->internal->initialized) {
1985 errno = EINVAL; /* Best I can think of ... */
1986 TALLOC_FREE(frame);
1987 return -1;
1990 if (!fname) {
1991 errno = EINVAL;
1992 TALLOC_FREE(frame);
1993 return -1;
1996 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1998 if (SMBC_parse_path(frame,
1999 context,
2000 fname,
2001 &workgroup,
2002 &server,
2003 &port,
2004 &share,
2005 &path,
2006 &user,
2007 &password,
2008 NULL)) {
2009 errno = EINVAL;
2010 TALLOC_FREE(frame);
2011 return -1;
2014 if (!user || user[0] == (char)0) {
2015 user = talloc_strdup(frame, smbc_getUser(context));
2016 if (!user) {
2017 errno = ENOMEM;
2018 TALLOC_FREE(frame);
2019 return -1;
2023 srv = SMBC_server(frame, context, True,
2024 server, port, share, &workgroup, &user, &password);
2026 if (!srv) {
2027 TALLOC_FREE(frame);
2028 return -1; /* errno set by SMBC_server */
2031 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
2033 /*d_printf(">>>unlink: resolving %s\n", path);*/
2034 status = cli_resolve_path(frame, "",
2035 creds,
2036 srv->cli, path, &targetcli, &targetpath);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 d_printf("Could not resolve %s\n", path);
2039 errno = ENOENT;
2040 TALLOC_FREE(frame);
2041 return -1;
2044 attr = 0;
2046 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) attr |= FILE_ATTRIBUTE_READONLY;
2047 if ((newmode & S_IXUSR) && lp_map_archive(-1)) attr |= FILE_ATTRIBUTE_ARCHIVE;
2048 if ((newmode & S_IXGRP) && lp_map_system(-1)) attr |= FILE_ATTRIBUTE_SYSTEM;
2049 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) attr |= FILE_ATTRIBUTE_HIDDEN;
2051 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, attr, 0))) {
2052 errno = SMBC_errno(context, targetcli);
2053 TALLOC_FREE(frame);
2054 return -1;
2057 TALLOC_FREE(frame);
2058 return 0;
2062 SMBC_utimes_ctx(SMBCCTX *context,
2063 const char *fname,
2064 struct timeval *tbuf)
2066 SMBCSRV *srv = NULL;
2067 char *server = NULL;
2068 char *share = NULL;
2069 char *user = NULL;
2070 char *password = NULL;
2071 char *workgroup = NULL;
2072 char *path = NULL;
2073 struct timespec access_time, write_time;
2074 uint16_t port = 0;
2075 TALLOC_CTX *frame = talloc_stackframe();
2076 bool ok;
2078 if (!context || !context->internal->initialized) {
2080 errno = EINVAL; /* Best I can think of ... */
2081 TALLOC_FREE(frame);
2082 return -1;
2085 if (!fname) {
2086 errno = EINVAL;
2087 TALLOC_FREE(frame);
2088 return -1;
2091 if (tbuf == NULL) {
2092 access_time = write_time = timespec_current();
2093 } else {
2094 access_time = convert_timeval_to_timespec(tbuf[0]);
2095 write_time = convert_timeval_to_timespec(tbuf[1]);
2098 if (DEBUGLVL(4)) {
2099 struct timeval_buf abuf, wbuf;
2101 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2102 fname,
2103 timespec_string_buf(&access_time, false, &abuf),
2104 timespec_string_buf(&write_time, false, &wbuf));
2107 if (SMBC_parse_path(frame,
2108 context,
2109 fname,
2110 &workgroup,
2111 &server,
2112 &port,
2113 &share,
2114 &path,
2115 &user,
2116 &password,
2117 NULL)) {
2118 errno = EINVAL;
2119 TALLOC_FREE(frame);
2120 return -1;
2123 if (!user || user[0] == (char)0) {
2124 user = talloc_strdup(frame, smbc_getUser(context));
2125 if (!user) {
2126 errno = ENOMEM;
2127 TALLOC_FREE(frame);
2128 return -1;
2132 srv = SMBC_server(frame, context, True,
2133 server, port, share, &workgroup, &user, &password);
2135 if (!srv) {
2136 TALLOC_FREE(frame);
2137 return -1; /* errno set by SMBC_server */
2140 ok = SMBC_setatr(
2141 context,
2142 srv,
2143 path,
2144 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2145 access_time,
2146 write_time,
2147 (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
2149 if (!ok) {
2150 TALLOC_FREE(frame);
2151 return -1; /* errno set by SMBC_setatr */
2154 TALLOC_FREE(frame);
2155 return 0;
2159 * Routine to unlink() a file
2163 SMBC_unlink_ctx(SMBCCTX *context,
2164 const char *fname)
2166 char *server = NULL;
2167 char *share = NULL;
2168 char *user = NULL;
2169 char *password = NULL;
2170 char *workgroup = NULL;
2171 char *path = NULL;
2172 char *targetpath = NULL;
2173 uint16_t port = 0;
2174 struct cli_state *targetcli = NULL;
2175 SMBCSRV *srv = NULL;
2176 struct cli_credentials *creds = NULL;
2177 TALLOC_CTX *frame = talloc_stackframe();
2178 NTSTATUS status;
2180 if (!context || !context->internal->initialized) {
2182 errno = EINVAL; /* Best I can think of ... */
2183 TALLOC_FREE(frame);
2184 return -1;
2188 if (!fname) {
2189 errno = EINVAL;
2190 TALLOC_FREE(frame);
2191 return -1;
2195 if (SMBC_parse_path(frame,
2196 context,
2197 fname,
2198 &workgroup,
2199 &server,
2200 &port,
2201 &share,
2202 &path,
2203 &user,
2204 &password,
2205 NULL)) {
2206 errno = EINVAL;
2207 TALLOC_FREE(frame);
2208 return -1;
2211 if (!user || user[0] == (char)0) {
2212 user = talloc_strdup(frame, smbc_getUser(context));
2213 if (!user) {
2214 errno = ENOMEM;
2215 TALLOC_FREE(frame);
2216 return -1;
2220 srv = SMBC_server(frame, context, True,
2221 server, port, share, &workgroup, &user, &password);
2223 if (!srv) {
2224 TALLOC_FREE(frame);
2225 return -1; /* SMBC_server sets errno */
2229 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
2231 /*d_printf(">>>unlink: resolving %s\n", path);*/
2232 status = cli_resolve_path(frame, "",
2233 creds,
2234 srv->cli, path, &targetcli, &targetpath);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 d_printf("Could not resolve %s\n", path);
2237 errno = ENOENT;
2238 TALLOC_FREE(frame);
2239 return -1;
2241 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2243 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
2245 errno = SMBC_errno(context, targetcli);
2247 if (errno == EACCES) { /* Check if the file is a directory */
2249 int saverr = errno;
2250 struct stat sb = {0};
2251 bool ok;
2253 ok = SMBC_getatr(context, srv, path, &sb);
2254 if (!ok) {
2255 /* Hmmm, bad error ... What? */
2257 errno = SMBC_errno(context, targetcli);
2258 TALLOC_FREE(frame);
2259 return -1;
2262 else {
2264 if (S_ISDIR(sb.st_mode))
2265 errno = EISDIR;
2266 else
2267 errno = saverr; /* Restore this */
2272 TALLOC_FREE(frame);
2273 return -1;
2277 TALLOC_FREE(frame);
2278 return 0; /* Success ... */
2283 * Routine to rename() a file
2287 SMBC_rename_ctx(SMBCCTX *ocontext,
2288 const char *oname,
2289 SMBCCTX *ncontext,
2290 const char *nname)
2292 char *server1 = NULL;
2293 char *share1 = NULL;
2294 char *server2 = NULL;
2295 char *share2 = NULL;
2296 char *user1 = NULL;
2297 char *user2 = NULL;
2298 char *password1 = NULL;
2299 char *password2 = NULL;
2300 char *workgroup = NULL;
2301 char *path1 = NULL;
2302 char *path2 = NULL;
2303 char *targetpath1 = NULL;
2304 char *targetpath2 = NULL;
2305 struct cli_state *targetcli1 = NULL;
2306 struct cli_state *targetcli2 = NULL;
2307 SMBCSRV *srv = NULL;
2308 uint16_t port1 = 0;
2309 uint16_t port2 = 0;
2310 struct cli_credentials *ocreds = NULL;
2311 struct cli_credentials *ncreds = NULL;
2312 TALLOC_CTX *frame = talloc_stackframe();
2313 NTSTATUS status;
2315 if (!ocontext || !ncontext ||
2316 !ocontext->internal->initialized ||
2317 !ncontext->internal->initialized) {
2319 errno = EINVAL; /* Best I can think of ... */
2320 TALLOC_FREE(frame);
2321 return -1;
2324 if (!oname || !nname) {
2325 errno = EINVAL;
2326 TALLOC_FREE(frame);
2327 return -1;
2330 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
2332 if (SMBC_parse_path(frame,
2333 ocontext,
2334 oname,
2335 &workgroup,
2336 &server1,
2337 &port1,
2338 &share1,
2339 &path1,
2340 &user1,
2341 &password1,
2342 NULL)) {
2343 errno = EINVAL;
2344 TALLOC_FREE(frame);
2345 return -1;
2348 if (!user1 || user1[0] == (char)0) {
2349 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
2350 if (!user1) {
2351 errno = ENOMEM;
2352 TALLOC_FREE(frame);
2353 return -1;
2357 if (SMBC_parse_path(frame,
2358 ncontext,
2359 nname,
2360 NULL,
2361 &server2,
2362 &port2,
2363 &share2,
2364 &path2,
2365 &user2,
2366 &password2,
2367 NULL)) {
2368 errno = EINVAL;
2369 TALLOC_FREE(frame);
2370 return -1;
2373 if (!user2 || user2[0] == (char)0) {
2374 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
2375 if (!user2) {
2376 errno = ENOMEM;
2377 TALLOC_FREE(frame);
2378 return -1;
2382 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2383 strcmp(user1, user2)) {
2384 /* Can't rename across file systems, or users?? */
2385 errno = EXDEV;
2386 TALLOC_FREE(frame);
2387 return -1;
2390 srv = SMBC_server(frame, ocontext, True,
2391 server1, port1, share1, &workgroup, &user1, &password1);
2392 if (!srv) {
2393 TALLOC_FREE(frame);
2394 return -1;
2398 /* set the credentials to make DFS work */
2399 smbc_set_credentials_with_fallback(ocontext,
2400 workgroup,
2401 user1,
2402 password1);
2404 /*d_printf(">>>rename: resolving %s\n", path1);*/
2405 ocreds = get_cmdline_auth_info_creds(ocontext->internal->auth_info);
2407 status = cli_resolve_path(frame, "",
2408 ocreds,
2409 srv->cli, path1, &targetcli1, &targetpath1);
2410 if (!NT_STATUS_IS_OK(status)) {
2411 d_printf("Could not resolve %s\n", path1);
2412 errno = ENOENT;
2413 TALLOC_FREE(frame);
2414 return -1;
2417 /* set the credentials to make DFS work */
2418 smbc_set_credentials_with_fallback(ncontext,
2419 workgroup,
2420 user2,
2421 password2);
2423 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2424 /*d_printf(">>>rename: resolving %s\n", path2);*/
2425 ncreds = get_cmdline_auth_info_creds(ncontext->internal->auth_info);
2427 status = cli_resolve_path(frame, "",
2428 ncreds,
2429 srv->cli, path2, &targetcli2, &targetpath2);
2430 if (!NT_STATUS_IS_OK(status)) {
2431 d_printf("Could not resolve %s\n", path2);
2432 errno = ENOENT;
2433 TALLOC_FREE(frame);
2434 return -1;
2436 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2438 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2439 strcmp(targetcli1->share, targetcli2->share))
2441 /* can't rename across file systems */
2442 errno = EXDEV;
2443 TALLOC_FREE(frame);
2444 return -1;
2447 if (!NT_STATUS_IS_OK(
2448 cli_rename(targetcli1, targetpath1, targetpath2, false))) {
2449 int eno = SMBC_errno(ocontext, targetcli1);
2451 if (eno != EEXIST ||
2452 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2,
2453 FILE_ATTRIBUTE_SYSTEM |
2454 FILE_ATTRIBUTE_HIDDEN)) ||
2455 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
2456 targetpath2, false))) {
2458 errno = eno;
2459 TALLOC_FREE(frame);
2460 return -1;
2465 TALLOC_FREE(frame);
2466 return 0; /* Success */
2469 struct smbc_notify_cb_state {
2470 struct tevent_context *ev;
2471 struct cli_state *cli;
2472 uint16_t fnum;
2473 bool recursive;
2474 uint32_t completion_filter;
2475 unsigned callback_timeout_ms;
2476 smbc_notify_callback_fn cb;
2477 void *private_data;
2480 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2481 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2483 static struct tevent_req *smbc_notify_cb_send(
2484 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2485 uint16_t fnum, bool recursive, uint32_t completion_filter,
2486 unsigned callback_timeout_ms,
2487 smbc_notify_callback_fn cb, void *private_data)
2489 struct tevent_req *req, *subreq;
2490 struct smbc_notify_cb_state *state;
2492 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2493 if (req == NULL) {
2494 return NULL;
2496 state->ev = ev;
2497 state->cli = cli;
2498 state->fnum = fnum;
2499 state->recursive = recursive;
2500 state->completion_filter = completion_filter;
2501 state->callback_timeout_ms = callback_timeout_ms;
2502 state->cb = cb;
2503 state->private_data = private_data;
2505 subreq = cli_notify_send(
2506 state, state->ev, state->cli, state->fnum, 1000,
2507 state->completion_filter, state->recursive);
2508 if (tevent_req_nomem(subreq, req)) {
2509 return tevent_req_post(req, ev);
2511 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2513 if (state->callback_timeout_ms == 0) {
2514 return req;
2517 subreq = tevent_wakeup_send(
2518 state, state->ev,
2519 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2520 state->callback_timeout_ms*1000));
2521 if (tevent_req_nomem(subreq, req)) {
2522 return tevent_req_post(req, ev);
2524 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2526 return req;
2529 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2531 struct tevent_req *req = tevent_req_callback_data(
2532 subreq, struct tevent_req);
2533 struct smbc_notify_cb_state *state = tevent_req_data(
2534 req, struct smbc_notify_cb_state);
2535 uint32_t num_changes;
2536 struct notify_change *changes;
2537 NTSTATUS status;
2538 int cb_ret;
2540 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2541 TALLOC_FREE(subreq);
2542 if (tevent_req_nterror(req, status)) {
2543 return;
2547 struct smbc_notify_callback_action actions[num_changes];
2548 uint32_t i;
2550 for (i=0; i<num_changes; i++) {
2551 actions[i].action = changes[i].action;
2552 actions[i].filename = changes[i].name;
2555 cb_ret = state->cb(actions, num_changes, state->private_data);
2558 TALLOC_FREE(changes);
2560 if (cb_ret != 0) {
2561 tevent_req_done(req);
2562 return;
2565 subreq = cli_notify_send(
2566 state, state->ev, state->cli, state->fnum, 1000,
2567 state->completion_filter, state->recursive);
2568 if (tevent_req_nomem(subreq, req)) {
2569 return;
2571 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2574 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2576 struct tevent_req *req = tevent_req_callback_data(
2577 subreq, struct tevent_req);
2578 struct smbc_notify_cb_state *state = tevent_req_data(
2579 req, struct smbc_notify_cb_state);
2580 int cb_ret;
2581 bool ok;
2583 ok = tevent_wakeup_recv(subreq);
2584 TALLOC_FREE(subreq);
2585 if (!ok) {
2586 tevent_req_oom(req);
2587 return;
2590 cb_ret = state->cb(NULL, 0, state->private_data);
2591 if (cb_ret != 0) {
2592 tevent_req_done(req);
2593 return;
2596 subreq = tevent_wakeup_send(
2597 state, state->ev,
2598 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2599 state->callback_timeout_ms*1000));
2600 if (tevent_req_nomem(subreq, req)) {
2601 return;
2603 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2606 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2608 return tevent_req_simple_recv_ntstatus(req);
2611 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2612 bool recursive, uint32_t completion_filter,
2613 unsigned callback_timeout_ms,
2614 smbc_notify_callback_fn cb, void *private_data)
2616 TALLOC_CTX *frame = talloc_stackframe();
2617 struct tevent_context *ev;
2618 struct tevent_req *req;
2619 NTSTATUS status = NT_STATUS_NO_MEMORY;
2621 ev = samba_tevent_context_init(frame);
2622 if (ev == NULL) {
2623 goto fail;
2625 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2626 completion_filter,
2627 callback_timeout_ms, cb, private_data);
2628 if (req == NULL) {
2629 goto fail;
2631 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2632 goto fail;
2634 status = smbc_notify_cb_recv(req);
2635 TALLOC_FREE(req);
2636 fail:
2637 TALLOC_FREE(frame);
2638 return status;
2642 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2643 uint32_t completion_filter, unsigned callback_timeout_ms,
2644 smbc_notify_callback_fn cb, void *private_data)
2646 TALLOC_CTX *frame = talloc_stackframe();
2647 struct cli_state *cli;
2648 char *server = NULL;
2649 char *share = NULL;
2650 char *user = NULL;
2651 char *password = NULL;
2652 char *options = NULL;
2653 char *workgroup = NULL;
2654 char *path = NULL;
2655 uint16_t port;
2656 NTSTATUS status;
2657 uint16_t fnum;
2659 if ((context == NULL) || !context->internal->initialized) {
2660 TALLOC_FREE(frame);
2661 errno = EINVAL;
2662 return -1;
2664 if (!SMBC_dlist_contains(context->internal->files, dir)) {
2665 TALLOC_FREE(frame);
2666 errno = EBADF;
2667 return -1;
2670 if (SMBC_parse_path(frame,
2671 context,
2672 dir->fname,
2673 &workgroup,
2674 &server,
2675 &port,
2676 &share,
2677 &path,
2678 &user,
2679 &password,
2680 &options)) {
2681 DEBUG(4, ("no valid path\n"));
2682 TALLOC_FREE(frame);
2683 errno = EINVAL + 8194;
2684 return -1;
2687 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2688 "path='%s' options='%s'\n",
2689 dir->fname, server, share, path, options));
2691 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2692 (int)recursive, completion_filter));
2694 cli = dir->srv->cli;
2695 status = cli_ntcreate(
2696 cli, path, 0, FILE_READ_DATA, 0,
2697 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2698 FILE_OPEN, 0, 0, &fnum, NULL);
2699 if (!NT_STATUS_IS_OK(status)) {
2700 int err = SMBC_errno(context, cli);
2701 TALLOC_FREE(frame);
2702 errno = err;
2703 return -1;
2706 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2707 callback_timeout_ms, cb, private_data);
2708 if (!NT_STATUS_IS_OK(status)) {
2709 int err = SMBC_errno(context, cli);
2710 cli_close(cli, fnum);
2711 TALLOC_FREE(frame);
2712 errno = err;
2713 return -1;
2716 cli_close(cli, fnum);
2718 TALLOC_FREE(frame);
2719 return 0;