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/>.
26 #include "libsmb/namequery.h"
27 #include "libsmb/libsmb.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
32 #include "libsmb/nmblib.h"
33 #include "../libcli/smb/smbXcli_base.h"
34 #include "../libcli/security/security.h"
35 #include "lib/util/tevent_ntstatus.h"
36 #include "lib/util/time_basic.h"
37 #include "lib/util/string_wrappers.h"
40 * Routine to open a directory
41 * We accept the URL syntax explained in SMBC_parse_path(), above.
44 static void remove_dirplus(SMBCFILE
*dir
)
46 struct smbc_dirplus_list
*d
= NULL
;
48 d
= dir
->dirplus_list
;
50 struct smbc_dirplus_list
*f
= d
;
53 SAFE_FREE(f
->smb_finfo
->short_name
);
54 SAFE_FREE(f
->smb_finfo
->name
);
55 SAFE_FREE(f
->smb_finfo
);
59 dir
->dirplus_list
= NULL
;
60 dir
->dirplus_end
= NULL
;
61 dir
->dirplus_next
= NULL
;
65 remove_dir(SMBCFILE
*dir
)
67 struct smbc_dir_list
*d
,*f
;
79 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
84 add_dirent(SMBCFILE
*dir
,
89 struct smbc_dirent
*dirent
;
91 int name_length
= (name
== NULL
? 0 : strlen(name
));
92 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
95 * Allocate space for the dirent, which must be increased by the
96 * size of the name and the comment and 1 each for the null terminator.
99 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
101 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
105 dir
->dir_error
= ENOMEM
;
110 ZERO_STRUCTP(dirent
);
112 if (dir
->dir_list
== NULL
) {
114 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
115 if (!dir
->dir_list
) {
118 dir
->dir_error
= ENOMEM
;
122 ZERO_STRUCTP(dir
->dir_list
);
124 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
128 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
130 if (!dir
->dir_end
->next
) {
133 dir
->dir_error
= ENOMEM
;
137 ZERO_STRUCTP(dir
->dir_end
->next
);
139 dir
->dir_end
= dir
->dir_end
->next
;
142 dir
->dir_end
->next
= NULL
;
143 dir
->dir_end
->dirent
= dirent
;
145 dirent
->smbc_type
= type
;
146 dirent
->namelen
= name_length
;
147 dirent
->commentlen
= comment_len
;
148 dirent
->dirlen
= size
;
151 * dirent->namelen + 1 includes the null (no null termination needed)
152 * Ditto for dirent->commentlen.
153 * The space for the two null bytes was allocated.
155 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
156 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
157 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
163 static int add_dirplus(SMBCFILE
*dir
, struct file_info
*finfo
)
165 struct smbc_dirplus_list
*new_entry
= NULL
;
166 struct libsmb_file_info
*info
= NULL
;
168 new_entry
= SMB_MALLOC_P(struct smbc_dirplus_list
);
169 if (new_entry
== NULL
) {
170 dir
->dir_error
= ENOMEM
;
173 ZERO_STRUCTP(new_entry
);
174 new_entry
->ino
= finfo
->ino
;
176 info
= SMB_MALLOC_P(struct libsmb_file_info
);
178 SAFE_FREE(new_entry
);
179 dir
->dir_error
= ENOMEM
;
185 info
->btime_ts
= finfo
->btime_ts
;
186 info
->atime_ts
= finfo
->atime_ts
;
187 info
->ctime_ts
= finfo
->ctime_ts
;
188 info
->mtime_ts
= finfo
->mtime_ts
;
189 info
->attrs
= finfo
->attr
;
190 info
->size
= finfo
->size
;
191 info
->name
= SMB_STRDUP(finfo
->name
);
192 if (info
->name
== NULL
) {
194 SAFE_FREE(new_entry
);
195 dir
->dir_error
= ENOMEM
;
199 if (finfo
->short_name
) {
200 info
->short_name
= SMB_STRDUP(finfo
->short_name
);
202 info
->short_name
= SMB_STRDUP("");
205 if (info
->short_name
== NULL
) {
206 SAFE_FREE(info
->name
);
208 SAFE_FREE(new_entry
);
209 dir
->dir_error
= ENOMEM
;
212 new_entry
->smb_finfo
= info
;
214 /* Now add to the list. */
215 if (dir
->dirplus_list
== NULL
) {
216 /* Empty list - point everything at new_entry. */
217 dir
->dirplus_list
= new_entry
;
218 dir
->dirplus_end
= new_entry
;
219 dir
->dirplus_next
= new_entry
;
221 /* Append to list but leave the ->next cursor alone. */
222 dir
->dirplus_end
->next
= new_entry
;
223 dir
->dirplus_end
= new_entry
;
230 list_unique_wg_fn(const char *name
,
235 SMBCFILE
*dir
= (SMBCFILE
*)state
;
236 struct smbc_dir_list
*dir_list
;
237 struct smbc_dirent
*dirent
;
241 dirent_type
= dir
->dir_type
;
243 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
244 /* An error occurred, what do we do? */
245 /* FIXME: Add some code here */
246 /* Change cli_NetServerEnum to take a fn
247 returning NTSTATUS... JRA. */
250 /* Point to the one just added */
251 dirent
= dir
->dir_end
->dirent
;
253 /* See if this was a duplicate */
254 for (dir_list
= dir
->dir_list
;
255 dir_list
!= dir
->dir_end
;
256 dir_list
= dir_list
->next
) {
258 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
259 /* Duplicate. End end of list need to be removed. */
263 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
264 /* Found the end of the list. Remove it. */
265 dir
->dir_end
= dir_list
;
266 free(dir_list
->next
);
268 dir_list
->next
= NULL
;
275 list_fn(const char *name
,
280 SMBCFILE
*dir
= (SMBCFILE
*)state
;
284 * We need to process the type a little ...
286 * Disk share = 0x00000000
287 * Print share = 0x00000001
288 * Comms share = 0x00000002 (obsolete?)
289 * IPC$ share = 0x00000003
291 * administrative shares:
292 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
295 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
299 dirent_type
= SMBC_FILE_SHARE
;
303 dirent_type
= SMBC_PRINTER_SHARE
;
307 dirent_type
= SMBC_COMMS_SHARE
;
312 dirent_type
= SMBC_IPC_SHARE
;
316 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
321 dirent_type
= dir
->dir_type
;
324 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
325 /* An error occurred, what do we do? */
326 /* FIXME: Add some code here */
327 /* Change cli_NetServerEnum to take a fn
328 returning NTSTATUS... JRA. */
333 dir_list_fn(struct file_info
*finfo
,
337 SMBCFILE
*dirp
= (SMBCFILE
*)state
;
340 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
341 (finfo
->attr
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
342 SMBCFILE
*dir
= (SMBCFILE
*)state
;
343 return map_nt_error_from_unix(dir
->dir_error
);
345 ret
= add_dirplus(dirp
, finfo
);
347 return map_nt_error_from_unix(dirp
->dir_error
);
353 net_share_enum_rpc(struct cli_state
*cli
,
354 void (*fn
)(const char *name
,
362 uint32_t preferred_len
= 0xffffffff;
364 struct srvsvc_NetShareInfoCtr info_ctr
;
365 struct srvsvc_NetShareCtr1 ctr1
;
367 fstring comment
= "";
368 struct rpc_pipe_client
*pipe_hnd
= NULL
;
370 uint32_t resume_handle
= 0;
371 uint32_t total_entries
= 0;
372 struct dcerpc_binding_handle
*b
;
374 /* Open the server service pipe */
375 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
377 if (!NT_STATUS_IS_OK(nt_status
)) {
378 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
382 ZERO_STRUCT(info_ctr
);
386 info_ctr
.ctr
.ctr1
= &ctr1
;
388 b
= pipe_hnd
->binding_handle
;
390 /* Issue the NetShareEnum RPC call and retrieve the response */
391 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
399 /* Was it successful? */
400 if (!NT_STATUS_IS_OK(nt_status
)) {
401 /* Nope. Go clean up. */
405 if (!W_ERROR_IS_OK(result
)) {
406 /* Nope. Go clean up. */
407 nt_status
= werror_to_ntstatus(result
);
411 if (total_entries
== 0) {
412 /* Nope. Go clean up. */
413 nt_status
= NT_STATUS_NOT_FOUND
;
417 /* For each returned entry... */
418 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
420 /* pull out the share name */
421 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
423 /* pull out the share's comment */
424 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
426 /* Get the type value */
427 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
429 /* Add this share to the list */
430 (*fn
)(name
, type
, comment
, state
);
434 /* Close the server service pipe */
435 TALLOC_FREE(pipe_hnd
);
437 /* Tell 'em if it worked */
443 * Verify that the options specified in a URL are valid
446 SMBC_check_options(char *server
,
451 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server
, share
, path
, options
));
455 /* No options at all is always ok */
456 if (! *options
) return 0;
458 /* Currently, we don't support any options. */
464 SMBC_opendir_ctx(SMBCCTX
*context
,
470 char *password
= NULL
;
471 char *options
= NULL
;
472 char *workgroup
= NULL
;
477 SMBCFILE
*dir
= NULL
;
478 struct sockaddr_storage rem_ss
;
479 TALLOC_CTX
*frame
= talloc_stackframe();
481 if (!context
|| !context
->internal
->initialized
) {
482 DEBUG(4, ("no valid context\n"));
484 errno
= EINVAL
+ 8192;
490 DEBUG(4, ("no valid fname\n"));
492 errno
= EINVAL
+ 8193;
496 if (SMBC_parse_path(frame
,
507 DEBUG(4, ("no valid path\n"));
509 errno
= EINVAL
+ 8194;
513 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
514 "path='%s' options='%s'\n",
515 fname
, server
, share
, path
, options
));
517 /* Ensure the options are valid */
518 if (SMBC_check_options(server
, share
, path
, options
)) {
519 DEBUG(4, ("unacceptable options (%s)\n", options
));
521 errno
= EINVAL
+ 8195;
525 if (!user
|| user
[0] == (char)0) {
526 user
= talloc_strdup(frame
, smbc_getUser(context
));
534 dir
= SMB_MALLOC_P(SMBCFILE
);
545 dir
->fname
= SMB_STRDUP(fname
);
546 if (dir
->fname
== NULL
) {
555 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
557 if (server
[0] == (char)0) {
561 size_t max_lmb_count
;
562 struct sockaddr_storage
*ip_list
;
563 struct sockaddr_storage server_addr
;
564 struct cli_credentials
*creds
= NULL
;
567 if (share
[0] != (char)0 || path
[0] != (char)0) {
570 SAFE_FREE(dir
->fname
);
574 errno
= EINVAL
+ 8196;
578 /* Determine how many local master browsers to query */
579 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
581 : smbc_getOptionBrowseMaxLmbCount(context
));
583 creds
= cli_credentials_init(frame
);
586 SAFE_FREE(dir
->fname
);
594 (void)cli_credentials_set_username(creds
, user
, CRED_SPECIFIED
);
595 (void)cli_credentials_set_password(creds
, password
, CRED_SPECIFIED
);
598 * We have server and share and path empty but options
599 * requesting that we scan all master browsers for their list
600 * of workgroups/domains. This implies that we must first try
601 * broadcast queries to find all master browsers, and if that
602 * doesn't work, then try our other methods which return only
603 * a single master browser.
607 status
= name_resolve_bcast(talloc_tos(),
612 if (!NT_STATUS_IS_OK(status
))
615 TALLOC_FREE(ip_list
);
617 if (!find_master_ip(workgroup
, &server_addr
)) {
620 SAFE_FREE(dir
->fname
);
628 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
629 talloc_tos(), &server_addr
,
630 sizeof(server_addr
));
631 if (ip_list
== NULL
) {
633 SAFE_FREE(dir
->fname
);
643 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
644 char addr
[INET6_ADDRSTRLEN
];
646 struct cli_state
*cli
= NULL
;
648 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
]);
649 DEBUG(99, ("Found master browser %zu of %zu: %s\n",
650 i
+1, MAX(count
, max_lmb_count
),
653 cli
= get_ipc_connect_master_ip(talloc_tos(),
657 /* cli == NULL is the master browser refused to talk or
658 could not be found */
663 workgroup
= talloc_strdup(frame
, wg_ptr
);
664 server
= talloc_strdup(frame
, smbXcli_conn_remote_name(cli
->conn
));
668 if (!workgroup
|| !server
) {
670 SAFE_FREE(dir
->fname
);
678 DEBUG(4, ("using workgroup %s %s\n",
682 * For each returned master browser IP address, get a
683 * connection to IPC$ on the server if we do not
684 * already have one, and determine the
685 * workgroups/domains that it knows about.
688 srv
= SMBC_server(frame
, context
, True
, server
, port
, "IPC$",
689 &workgroup
, &user
, &password
);
694 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
699 dir
->dir_type
= SMBC_WORKGROUP
;
701 /* Now, list the stuff ... */
703 if (!cli_NetServerEnum(srv
->cli
,
712 TALLOC_FREE(ip_list
);
715 * Server not an empty string ... Check the rest and see what
718 if (*share
== '\0') {
721 /* Should not have empty share with path */
723 SAFE_FREE(dir
->fname
);
727 errno
= EINVAL
+ 8197;
733 * We don't know if <server> is really a server name
734 * or is a workgroup/domain name. If we already have
735 * a server structure for it, we'll use it.
736 * Otherwise, check to see if <server><1D>,
737 * <server><1B>, or <server><20> translates. We check
738 * to see if <server> is an IP address first.
742 * See if we have an existing server. Do not
743 * establish a connection if one does not already
746 srv
= SMBC_server(frame
, context
, False
,
747 server
, port
, "IPC$",
748 &workgroup
, &user
, &password
);
751 * If no existing server and not an IP addr, look for
755 !is_ipaddress(server
) &&
756 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
757 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
759 * "server" is actually a workgroup name,
760 * not a server. Make this clear.
762 char *wgroup
= server
;
765 dir
->dir_type
= SMBC_SERVER
;
768 * Get the backup list ...
770 if (!name_status_find(wgroup
, 0, 0,
771 &rem_ss
, buserver
)) {
772 char addr
[INET6_ADDRSTRLEN
];
774 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
775 DEBUG(0,("Could not get name of "
776 "local/domain master browser "
777 "for workgroup %s from "
782 SAFE_FREE(dir
->fname
);
792 * Get a connection to IPC$ on the server if
793 * we do not already have one
795 srv
= SMBC_server(frame
, context
, True
,
796 buserver
, port
, "IPC$",
800 DEBUG(0, ("got no contact to IPC$\n"));
802 SAFE_FREE(dir
->fname
);
812 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
814 SAFE_FREE(dir
->fname
);
821 /* Now, list the servers ... */
822 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
827 SAFE_FREE(dir
->fname
);
834 (resolve_name(server
, &rem_ss
, 0x20, false))) {
838 * If we hadn't found the server, get one now
841 srv
= SMBC_server(frame
, context
, True
,
842 server
, port
, "IPC$",
849 SAFE_FREE(dir
->fname
);
857 dir
->dir_type
= SMBC_FILE_SHARE
;
860 /* List the shares ... */
862 status
= net_share_enum_rpc(srv
->cli
,
865 if (!NT_STATUS_IS_OK(status
) &&
866 smbXcli_conn_protocol(srv
->cli
->conn
) <=
869 * Only call cli_RNetShareEnum()
870 * on SMB1 connections, not SMB2+.
872 int rc
= cli_RNetShareEnum(srv
->cli
,
876 status
= cli_nt_error(srv
->cli
);
878 status
= NT_STATUS_OK
;
881 if (!NT_STATUS_IS_OK(status
)) {
883 * Set cli->raw_status so SMBC_errno()
884 * will correctly return the error.
886 srv
->cli
->raw_status
= status
;
888 SAFE_FREE(dir
->fname
);
892 errno
= map_errno_from_nt_status(
897 /* Neither the workgroup nor server exists */
898 errno
= ECONNREFUSED
;
900 SAFE_FREE(dir
->fname
);
910 * The server and share are specified ... work from
914 struct cli_state
*targetcli
;
915 struct cli_credentials
*creds
= NULL
;
918 /* We connect to the server and list the directory */
919 dir
->dir_type
= SMBC_FILE_SHARE
;
921 srv
= SMBC_server(frame
, context
, True
, server
, port
, share
,
922 &workgroup
, &user
, &password
);
926 SAFE_FREE(dir
->fname
);
935 /* Now, list the files ... */
937 path_len
= strlen(path
);
938 path
= talloc_asprintf_append(path
, "\\*");
941 SAFE_FREE(dir
->fname
);
948 creds
= context
->internal
->creds
;
950 status
= cli_resolve_path(
953 srv
->cli
, path
, &targetcli
, &targetpath
);
954 if (!NT_STATUS_IS_OK(status
)) {
955 d_printf("Could not resolve %s\n", path
);
957 SAFE_FREE(dir
->fname
);
964 status
= cli_list(targetcli
, targetpath
,
965 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
966 dir_list_fn
, (void *)dir
);
967 if (!NT_STATUS_IS_OK(status
)) {
970 SAFE_FREE(dir
->fname
);
973 saved_errno
= cli_status_to_errno(status
);
975 if (saved_errno
== EINVAL
) {
976 struct stat sb
= {0};
978 * See if they asked to opendir
979 * something other than a directory.
980 * If so, the converted error value we
981 * got would have been EINVAL rather
984 path
[path_len
] = '\0'; /* restore original path */
986 status
= SMBC_getatr(
991 if (NT_STATUS_IS_OK(status
) &&
992 !S_ISDIR(sb
.st_mode
)) {
994 /* It is. Correct the error value */
995 saved_errno
= ENOTDIR
;
1000 * If there was an error and the server is no
1003 if (cli_is_error(targetcli
) &&
1004 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
1006 /* ... then remove it. */
1007 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
1010 * We could not remove the
1011 * server completely, remove
1012 * it from the cache so we
1013 * will not get it again. It
1014 * will be removed when the
1015 * last file/dir is closed.
1017 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
1022 errno
= saved_errno
;
1029 DLIST_ADD(context
->internal
->files
, dir
);
1036 * Routine to close a directory
1040 SMBC_closedir_ctx(SMBCCTX
*context
,
1043 TALLOC_CTX
*frame
= NULL
;
1045 if (!context
|| !context
->internal
->initialized
) {
1054 frame
= talloc_stackframe();
1056 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1062 remove_dir(dir
); /* Clean it up */
1063 remove_dirplus(dir
);
1065 DLIST_REMOVE(context
->internal
->files
, dir
);
1067 SAFE_FREE(dir
->fname
);
1068 SAFE_FREE(dir
); /* Free the space too */
1076 smbc_readdir_internal(SMBCCTX
* context
,
1077 struct smbc_dirent
*dest
,
1078 struct smbc_dirent
*src
,
1079 int max_namebuf_len
)
1081 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
1084 /* url-encode the name. get back remaining buffer space */
1086 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
1088 /* -1 means no null termination. */
1089 if (remaining_len
< 0) {
1093 /* We now know the name length */
1094 dest
->namelen
= strlen(dest
->name
);
1096 if (dest
->namelen
+ 1 < 1) {
1101 if (dest
->namelen
+ 1 >= max_namebuf_len
) {
1102 /* Out of space for comment. */
1106 /* Save the pointer to the beginning of the comment */
1107 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
1109 if (remaining_len
< 1) {
1110 /* No room for comment null termination. */
1114 /* Copy the comment */
1115 strlcpy(dest
->comment
, src
->comment
, remaining_len
);
1117 /* Save other fields */
1118 dest
->smbc_type
= src
->smbc_type
;
1119 dest
->commentlen
= strlen(dest
->comment
);
1120 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
1124 /* No encoding. Just copy the entry as is. */
1125 if (src
->dirlen
> max_namebuf_len
) {
1128 memcpy(dest
, src
, src
->dirlen
);
1129 if (src
->namelen
+ 1 < 1) {
1133 if (src
->namelen
+ 1 >= max_namebuf_len
) {
1134 /* Comment off the end. */
1137 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
1143 * Routine to get a directory entry
1146 struct smbc_dirent
*
1147 SMBC_readdir_ctx(SMBCCTX
*context
,
1152 struct smbc_dirent
*dirp
, *dirent
;
1153 TALLOC_CTX
*frame
= talloc_stackframe();
1155 /* Check that all is ok first ... */
1157 if (!context
|| !context
->internal
->initialized
) {
1160 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1166 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1169 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1175 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1178 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1184 if (!dir
->dir_next
) {
1189 dirent
= dir
->dir_next
->dirent
;
1198 dirp
= &context
->internal
->dirent
;
1199 maxlen
= sizeof(context
->internal
->_dirent_name
);
1201 ret
= smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1208 dir
->dir_next
= dir
->dir_next
->next
;
1211 * If we are returning file entries, we
1212 * have a duplicate list in dirplus.
1214 * Update dirplus_next also so readdir and
1215 * readdirplus are kept in sync.
1217 if (dir
->dirplus_list
!= NULL
) {
1218 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1226 * Routine to get a directory entry with all attributes
1229 const struct libsmb_file_info
*
1230 SMBC_readdirplus_ctx(SMBCCTX
*context
,
1233 struct libsmb_file_info
*smb_finfo
= NULL
;
1234 TALLOC_CTX
*frame
= talloc_stackframe();
1236 /* Check that all is ok first ... */
1238 if (context
== NULL
|| !context
->internal
->initialized
) {
1239 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1245 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1246 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1252 if (dir
->dirplus_next
== NULL
) {
1257 smb_finfo
= dir
->dirplus_next
->smb_finfo
;
1258 if (smb_finfo
== NULL
) {
1263 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1266 * If we are returning file entries, we
1267 * have a duplicate list in dir_list
1269 * Update dir_next also so readdir and
1270 * readdirplus are kept in sync.
1272 if (dir
->dir_list
) {
1273 dir
->dir_next
= dir
->dir_next
->next
;
1281 * Routine to get a directory entry plus a filled in stat structure if
1285 const struct libsmb_file_info
*SMBC_readdirplus2_ctx(SMBCCTX
*context
,
1289 struct libsmb_file_info
*smb_finfo
= NULL
;
1290 struct smbc_dirplus_list
*dp_list
= NULL
;
1292 char *full_pathname
= NULL
;
1293 char *workgroup
= NULL
;
1294 char *server
= NULL
;
1299 char *password
= NULL
;
1300 char *options
= NULL
;
1302 TALLOC_CTX
*frame
= NULL
;
1305 * Allow caller to pass in NULL for stat pointer if
1306 * required. This makes this call identical to
1307 * smbc_readdirplus().
1311 return SMBC_readdirplus_ctx(context
, dir
);
1314 frame
= talloc_stackframe();
1316 /* Check that all is ok first ... */
1317 if (context
== NULL
|| !context
->internal
->initialized
) {
1318 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1324 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1325 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1331 dp_list
= dir
->dirplus_next
;
1332 if (dp_list
== NULL
) {
1337 ino
= (ino_t
)dp_list
->ino
;
1339 smb_finfo
= dp_list
->smb_finfo
;
1340 if (smb_finfo
== NULL
) {
1346 full_pathname
= talloc_asprintf(frame
,
1350 if (full_pathname
== NULL
) {
1356 rc
= SMBC_parse_path(frame
,
1379 smb_finfo
->atime_ts
,
1380 smb_finfo
->ctime_ts
,
1381 smb_finfo
->mtime_ts
);
1383 TALLOC_FREE(full_pathname
);
1385 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1388 * If we are returning file entries, we
1389 * have a duplicate list in dir_list
1391 * Update dir_next also so readdir and
1392 * readdirplus are kept in sync.
1394 if (dir
->dir_list
) {
1395 dir
->dir_next
= dir
->dir_next
->next
;
1403 * Routine to get directory entries
1407 SMBC_getdents_ctx(SMBCCTX
*context
,
1409 struct smbc_dirent
*dirp
,
1415 char *ndir
= (char *)dirp
;
1416 struct smbc_dir_list
*dirlist
;
1417 TALLOC_CTX
*frame
= talloc_stackframe();
1419 /* Check that all is ok first ... */
1421 if (!context
|| !context
->internal
->initialized
) {
1429 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1437 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1446 * Now, retrieve the number of entries that will fit in what was passed
1447 * We have to figure out if the info is in the list, or we need to
1448 * send a request to the server to get the info.
1451 while ((dirlist
= dir
->dir_next
)) {
1453 struct smbc_dirent
*dirent
;
1454 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1456 if (!dirlist
->dirent
) {
1458 errno
= ENOENT
; /* Bad error */
1464 /* Do urlencoding of next entry, if so selected */
1465 dirent
= &context
->internal
->dirent
;
1466 maxlen
= sizeof(context
->internal
->_dirent_name
);
1467 ret
= smbc_readdir_internal(context
, dirent
,
1468 dirlist
->dirent
, maxlen
);
1475 reqd
= dirent
->dirlen
;
1479 if (rem
< count
) { /* We managed to copy something */
1486 else { /* Nothing copied ... */
1488 errno
= EINVAL
; /* Not enough space ... */
1496 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1498 currentEntry
->comment
= ¤tEntry
->name
[0] +
1499 dirent
->namelen
+ 1;
1504 /* Try and align the struct for the next entry
1505 on a valid pointer boundary by appending zeros */
1506 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1510 currentEntry
->dirlen
++;
1513 dir
->dir_next
= dirlist
= dirlist
-> next
;
1516 * If we are returning file entries, we
1517 * have a duplicate list in dirplus.
1519 * Update dirplus_next also so readdir and
1520 * readdirplus are kept in sync.
1522 if (dir
->dirplus_list
!= NULL
) {
1523 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1537 * Routine to create a directory ...
1541 SMBC_mkdir_ctx(SMBCCTX
*context
,
1545 SMBCSRV
*srv
= NULL
;
1546 char *server
= NULL
;
1549 char *password
= NULL
;
1550 char *workgroup
= NULL
;
1552 char *targetpath
= NULL
;
1554 struct cli_state
*targetcli
= NULL
;
1555 struct cli_credentials
*creds
= NULL
;
1556 TALLOC_CTX
*frame
= talloc_stackframe();
1559 if (!context
|| !context
->internal
->initialized
) {
1571 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1573 if (SMBC_parse_path(frame
,
1589 if (!user
|| user
[0] == (char)0) {
1590 user
= talloc_strdup(frame
, smbc_getUser(context
));
1598 srv
= SMBC_server(frame
, context
, True
,
1599 server
, port
, share
, &workgroup
, &user
, &password
);
1604 return -1; /* errno set by SMBC_server */
1608 creds
= context
->internal
->creds
;
1610 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1611 status
= cli_resolve_path(frame
, "",
1613 srv
->cli
, path
, &targetcli
, &targetpath
);
1614 if (!NT_STATUS_IS_OK(status
)) {
1615 d_printf("Could not resolve %s\n", path
);
1620 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1622 status
= cli_mkdir(targetcli
, targetpath
);
1623 if (!NT_STATUS_IS_OK(status
)) {
1625 errno
= cli_status_to_errno(status
);
1636 * Our list function simply checks to see if a directory is not empty
1640 rmdir_list_fn(struct file_info
*finfo
,
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
,
1660 SMBCSRV
*srv
= NULL
;
1661 char *server
= NULL
;
1664 char *password
= NULL
;
1665 char *workgroup
= NULL
;
1667 char *targetpath
= NULL
;
1669 struct cli_state
*targetcli
= NULL
;
1670 struct cli_credentials
*creds
= NULL
;
1671 TALLOC_CTX
*frame
= talloc_stackframe();
1674 if (!context
|| !context
->internal
->initialized
) {
1686 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1688 if (SMBC_parse_path(frame
,
1704 if (!user
|| user
[0] == (char)0) {
1705 user
= talloc_strdup(frame
, smbc_getUser(context
));
1713 srv
= SMBC_server(frame
, context
, True
,
1714 server
, port
, share
, &workgroup
, &user
, &password
);
1719 return -1; /* errno set by SMBC_server */
1723 creds
= context
->internal
->creds
;
1725 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1726 status
= cli_resolve_path(frame
, "",
1728 srv
->cli
, path
, &targetcli
, &targetpath
);
1729 if (!NT_STATUS_IS_OK(status
)) {
1730 d_printf("Could not resolve %s\n", path
);
1735 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1737 status
= cli_rmdir(targetcli
, targetpath
);
1739 if (!NT_STATUS_IS_OK(status
)) {
1741 errno
= cli_status_to_errno(status
);
1743 if (errno
== EACCES
) { /* Check if the dir empty or not */
1745 /* Local storage to avoid buffer overflows */
1747 bool smbc_rmdir_dirempty
= true;
1749 lpath
= talloc_asprintf(frame
, "%s\\*",
1757 status
= cli_list(targetcli
, lpath
,
1758 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1760 &smbc_rmdir_dirempty
);
1762 if (!NT_STATUS_IS_OK(status
)) {
1763 /* Fix errno to ignore latest error ... */
1764 DBG_INFO("cli_list returned an error: %s\n",
1770 if (smbc_rmdir_dirempty
)
1788 * Routine to return the current directory position
1792 SMBC_telldir_ctx(SMBCCTX
*context
,
1795 TALLOC_CTX
*frame
= talloc_stackframe();
1797 if (!context
|| !context
->internal
->initialized
) {
1805 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1813 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1821 /* See if we're already at the end. */
1822 if (dir
->dir_next
== NULL
) {
1829 * We return the pointer here as the offset
1832 return (off_t
)(long)dir
->dir_next
->dirent
;
1836 * A routine to run down the list and see if the entry is OK
1837 * Modifies the dir list and the dirplus list (if it exists)
1838 * to point at the correct next entry on success.
1841 static bool update_dir_ents(SMBCFILE
*dir
, struct smbc_dirent
*dirent
)
1843 struct smbc_dir_list
*tmp_dir
= dir
->dir_list
;
1844 struct smbc_dirplus_list
*tmp_dirplus
= dir
->dirplus_list
;
1847 * Run down the list looking for what we want.
1848 * If we're enumerating files both dir_list
1849 * and dirplus_list contain the same entry
1850 * list, as they were seeded from the same
1851 * cli_list callback.
1853 * If we're enumerating servers then
1854 * dirplus_list will be NULL, so don't
1855 * update in that case.
1858 while (tmp_dir
!= NULL
) {
1859 if (tmp_dir
->dirent
== dirent
) {
1860 dir
->dir_next
= tmp_dir
;
1861 if (tmp_dirplus
!= NULL
) {
1862 dir
->dirplus_next
= tmp_dirplus
;
1866 tmp_dir
= tmp_dir
->next
;
1867 if (tmp_dirplus
!= NULL
) {
1868 tmp_dirplus
= tmp_dirplus
->next
;
1875 * Routine to seek on a directory
1879 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1883 long int l_offset
= offset
; /* Handle problems of size */
1884 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1885 TALLOC_CTX
*frame
= talloc_stackframe();
1888 if (!context
|| !context
->internal
->initialized
) {
1896 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1904 /* Now, check what we were passed and see if it is OK ... */
1906 if (dirent
== NULL
) { /* Seek to the beginning of the list */
1908 dir
->dir_next
= dir
->dir_list
;
1910 /* Do the same for dirplus. */
1911 dir
->dirplus_next
= dir
->dirplus_list
;
1918 if (offset
== -1) { /* Seek to the end of the list */
1919 dir
->dir_next
= NULL
;
1921 /* Do the same for dirplus. */
1922 dir
->dirplus_next
= NULL
;
1929 * Run down the list and make sure that the entry is OK.
1930 * Update the position of both dir and dirplus lists.
1933 ok
= update_dir_ents(dir
, dirent
);
1935 errno
= EINVAL
; /* Bad entry */
1945 * Routine to fstat a dir
1949 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1954 if (!context
|| !context
->internal
->initialized
) {
1960 /* No code yet ... */
1965 SMBC_chmod_ctx(SMBCCTX
*context
,
1969 SMBCSRV
*srv
= NULL
;
1970 char *server
= NULL
;
1973 char *password
= NULL
;
1974 char *workgroup
= NULL
;
1975 char *targetpath
= NULL
;
1976 struct cli_state
*targetcli
= NULL
;
1980 struct cli_credentials
*creds
= NULL
;
1981 TALLOC_CTX
*frame
= talloc_stackframe();
1984 if (!context
|| !context
->internal
->initialized
) {
1986 errno
= EINVAL
; /* Best I can think of ... */
1997 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1999 if (SMBC_parse_path(frame
,
2015 if (!user
|| user
[0] == (char)0) {
2016 user
= talloc_strdup(frame
, smbc_getUser(context
));
2024 srv
= SMBC_server(frame
, context
, True
,
2025 server
, port
, share
, &workgroup
, &user
, &password
);
2029 return -1; /* errno set by SMBC_server */
2032 creds
= context
->internal
->creds
;
2034 /*d_printf(">>>unlink: resolving %s\n", path);*/
2035 status
= cli_resolve_path(frame
, "",
2037 srv
->cli
, path
, &targetcli
, &targetpath
);
2038 if (!NT_STATUS_IS_OK(status
)) {
2039 d_printf("Could not resolve %s\n", path
);
2047 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) attr
|= FILE_ATTRIBUTE_READONLY
;
2048 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) attr
|= FILE_ATTRIBUTE_ARCHIVE
;
2049 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) attr
|= FILE_ATTRIBUTE_SYSTEM
;
2050 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) attr
|= FILE_ATTRIBUTE_HIDDEN
;
2052 status
= cli_setatr(targetcli
, targetpath
, attr
, 0);
2053 if (!NT_STATUS_IS_OK(status
)) {
2055 errno
= cli_status_to_errno(status
);
2064 SMBC_utimes_ctx(SMBCCTX
*context
,
2066 struct timeval
*tbuf
)
2068 SMBCSRV
*srv
= NULL
;
2069 char *server
= NULL
;
2072 char *password
= NULL
;
2073 char *workgroup
= NULL
;
2075 struct timespec access_time
, write_time
;
2077 TALLOC_CTX
*frame
= talloc_stackframe();
2080 if (!context
|| !context
->internal
->initialized
) {
2082 errno
= EINVAL
; /* Best I can think of ... */
2094 access_time
= write_time
= timespec_current();
2096 access_time
= convert_timeval_to_timespec(tbuf
[0]);
2097 write_time
= convert_timeval_to_timespec(tbuf
[1]);
2101 struct timeval_buf abuf
, wbuf
;
2103 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2105 timespec_string_buf(&access_time
, false, &abuf
),
2106 timespec_string_buf(&write_time
, false, &wbuf
));
2109 if (SMBC_parse_path(frame
,
2125 if (!user
|| user
[0] == (char)0) {
2126 user
= talloc_strdup(frame
, smbc_getUser(context
));
2134 srv
= SMBC_server(frame
, context
, True
,
2135 server
, port
, share
, &workgroup
, &user
, &password
);
2139 return -1; /* errno set by SMBC_server */
2146 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2149 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2153 return -1; /* errno set by SMBC_setatr */
2161 * Routine to unlink() a file
2165 SMBC_unlink_ctx(SMBCCTX
*context
,
2168 char *server
= NULL
;
2171 char *password
= NULL
;
2172 char *workgroup
= NULL
;
2174 char *targetpath
= NULL
;
2176 struct cli_state
*targetcli
= NULL
;
2177 SMBCSRV
*srv
= NULL
;
2178 struct cli_credentials
*creds
= NULL
;
2179 TALLOC_CTX
*frame
= talloc_stackframe();
2182 if (!context
|| !context
->internal
->initialized
) {
2184 errno
= EINVAL
; /* Best I can think of ... */
2197 if (SMBC_parse_path(frame
,
2213 if (!user
|| user
[0] == (char)0) {
2214 user
= talloc_strdup(frame
, smbc_getUser(context
));
2222 srv
= SMBC_server(frame
, context
, True
,
2223 server
, port
, share
, &workgroup
, &user
, &password
);
2227 return -1; /* SMBC_server sets errno */
2231 creds
= context
->internal
->creds
;
2233 /*d_printf(">>>unlink: resolving %s\n", path);*/
2234 status
= cli_resolve_path(frame
, "",
2236 srv
->cli
, path
, &targetcli
, &targetpath
);
2237 if (!NT_STATUS_IS_OK(status
)) {
2238 d_printf("Could not resolve %s\n", path
);
2243 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2245 status
= cli_unlink(
2248 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2250 if (!NT_STATUS_IS_OK(status
)) {
2252 errno
= cli_status_to_errno(status
);
2254 if (errno
== EACCES
) { /* Check if the file is a directory */
2257 struct stat sb
= {0};
2259 status
= SMBC_getatr(context
, srv
, path
, &sb
);
2260 if (!NT_STATUS_IS_OK(status
)) {
2261 /* Hmmm, bad error ... What? */
2264 errno
= cli_status_to_errno(status
);
2270 if (S_ISDIR(sb
.st_mode
))
2273 errno
= saverr
; /* Restore this */
2284 return 0; /* Success ... */
2289 * Routine to rename() a file
2293 SMBC_rename_ctx(SMBCCTX
*ocontext
,
2298 char *server1
= NULL
;
2299 char *share1
= NULL
;
2300 char *server2
= NULL
;
2301 char *share2
= NULL
;
2304 char *password1
= NULL
;
2305 char *password2
= NULL
;
2306 char *workgroup
= NULL
;
2309 char *targetpath1
= NULL
;
2310 char *targetpath2
= NULL
;
2311 struct cli_state
*targetcli1
= NULL
;
2312 struct cli_state
*targetcli2
= NULL
;
2313 SMBCSRV
*srv
= NULL
;
2316 struct cli_credentials
*ocreds
= NULL
;
2317 struct cli_credentials
*ncreds
= NULL
;
2318 TALLOC_CTX
*frame
= talloc_stackframe();
2321 if (!ocontext
|| !ncontext
||
2322 !ocontext
->internal
->initialized
||
2323 !ncontext
->internal
->initialized
) {
2325 errno
= EINVAL
; /* Best I can think of ... */
2330 if (!oname
|| !nname
) {
2336 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
2338 if (SMBC_parse_path(frame
,
2354 if (!user1
|| user1
[0] == (char)0) {
2355 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
2363 if (SMBC_parse_path(frame
,
2379 if (!user2
|| user2
[0] == (char)0) {
2380 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
2388 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2389 strcmp(user1
, user2
)) {
2390 /* Can't rename across file systems, or users?? */
2396 srv
= SMBC_server(frame
, ocontext
, True
,
2397 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2404 /* set the credentials to make DFS work */
2405 smbc_set_credentials_with_fallback(ocontext
,
2410 /*d_printf(">>>rename: resolving %s\n", path1);*/
2411 ocreds
= ocontext
->internal
->creds
;
2413 status
= cli_resolve_path(frame
, "",
2415 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2416 if (!NT_STATUS_IS_OK(status
)) {
2417 d_printf("Could not resolve %s\n", path1
);
2423 /* set the credentials to make DFS work */
2424 smbc_set_credentials_with_fallback(ncontext
,
2429 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2430 /*d_printf(">>>rename: resolving %s\n", path2);*/
2431 ncreds
= ncontext
->internal
->creds
;
2433 status
= cli_resolve_path(frame
, "",
2435 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2436 if (!NT_STATUS_IS_OK(status
)) {
2437 d_printf("Could not resolve %s\n", path2
);
2442 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2444 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2445 strcmp(targetcli1
->share
, targetcli2
->share
))
2447 /* can't rename across file systems */
2453 status
= cli_rename(targetcli1
, targetpath1
, targetpath2
, false);
2454 if (!NT_STATUS_IS_OK(status
)) {
2455 int eno
= cli_status_to_errno(status
);
2457 if (eno
!= EEXIST
||
2458 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2459 FILE_ATTRIBUTE_SYSTEM
|
2460 FILE_ATTRIBUTE_HIDDEN
)) ||
2461 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2462 targetpath2
, false))) {
2472 return 0; /* Success */
2475 struct smbc_notify_cb_state
{
2476 struct tevent_context
*ev
;
2477 struct cli_state
*cli
;
2480 uint32_t completion_filter
;
2481 unsigned callback_timeout_ms
;
2482 smbc_notify_callback_fn cb
;
2486 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2487 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2489 static struct tevent_req
*smbc_notify_cb_send(
2490 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2491 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2492 unsigned callback_timeout_ms
,
2493 smbc_notify_callback_fn cb
, void *private_data
)
2495 struct tevent_req
*req
, *subreq
;
2496 struct smbc_notify_cb_state
*state
;
2498 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2505 state
->recursive
= recursive
;
2506 state
->completion_filter
= completion_filter
;
2507 state
->callback_timeout_ms
= callback_timeout_ms
;
2509 state
->private_data
= private_data
;
2511 subreq
= cli_notify_send(
2512 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2513 state
->completion_filter
, state
->recursive
);
2514 if (tevent_req_nomem(subreq
, req
)) {
2515 return tevent_req_post(req
, ev
);
2517 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2519 if (state
->callback_timeout_ms
== 0) {
2523 subreq
= tevent_wakeup_send(
2525 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2526 state
->callback_timeout_ms
*1000));
2527 if (tevent_req_nomem(subreq
, req
)) {
2528 return tevent_req_post(req
, ev
);
2530 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2535 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2537 struct tevent_req
*req
= tevent_req_callback_data(
2538 subreq
, struct tevent_req
);
2539 struct smbc_notify_cb_state
*state
= tevent_req_data(
2540 req
, struct smbc_notify_cb_state
);
2541 uint32_t num_changes
;
2542 struct notify_change
*changes
;
2546 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2547 TALLOC_FREE(subreq
);
2548 if (tevent_req_nterror(req
, status
)) {
2553 struct smbc_notify_callback_action actions
[num_changes
];
2556 for (i
=0; i
<num_changes
; i
++) {
2557 actions
[i
].action
= changes
[i
].action
;
2558 actions
[i
].filename
= changes
[i
].name
;
2561 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2564 TALLOC_FREE(changes
);
2567 tevent_req_done(req
);
2571 subreq
= cli_notify_send(
2572 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2573 state
->completion_filter
, state
->recursive
);
2574 if (tevent_req_nomem(subreq
, req
)) {
2577 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2580 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2582 struct tevent_req
*req
= tevent_req_callback_data(
2583 subreq
, struct tevent_req
);
2584 struct smbc_notify_cb_state
*state
= tevent_req_data(
2585 req
, struct smbc_notify_cb_state
);
2589 ok
= tevent_wakeup_recv(subreq
);
2590 TALLOC_FREE(subreq
);
2592 tevent_req_oom(req
);
2596 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2598 tevent_req_done(req
);
2602 subreq
= tevent_wakeup_send(
2604 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2605 state
->callback_timeout_ms
*1000));
2606 if (tevent_req_nomem(subreq
, req
)) {
2609 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2612 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2614 return tevent_req_simple_recv_ntstatus(req
);
2617 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2618 bool recursive
, uint32_t completion_filter
,
2619 unsigned callback_timeout_ms
,
2620 smbc_notify_callback_fn cb
, void *private_data
)
2622 TALLOC_CTX
*frame
= talloc_stackframe();
2623 struct tevent_context
*ev
;
2624 struct tevent_req
*req
;
2625 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2627 ev
= samba_tevent_context_init(frame
);
2631 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2633 callback_timeout_ms
, cb
, private_data
);
2637 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2640 status
= smbc_notify_cb_recv(req
);
2648 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2649 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2650 smbc_notify_callback_fn cb
, void *private_data
)
2652 TALLOC_CTX
*frame
= talloc_stackframe();
2653 struct cli_state
*cli
;
2654 char *server
= NULL
;
2657 char *password
= NULL
;
2658 char *options
= NULL
;
2659 char *workgroup
= NULL
;
2665 if ((context
== NULL
) || !context
->internal
->initialized
) {
2670 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2676 if (SMBC_parse_path(frame
,
2687 DEBUG(4, ("no valid path\n"));
2689 errno
= EINVAL
+ 8194;
2693 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2694 "path='%s' options='%s'\n",
2695 dir
->fname
, server
, share
, path
, options
));
2697 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2698 (int)recursive
, completion_filter
));
2700 cli
= dir
->srv
->cli
;
2701 status
= cli_ntcreate(
2702 cli
, path
, 0, FILE_READ_DATA
, 0,
2703 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2704 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2705 if (!NT_STATUS_IS_OK(status
)) {
2707 errno
= cli_status_to_errno(status
);
2711 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2712 callback_timeout_ms
, cb
, private_data
);
2713 if (!NT_STATUS_IS_OK(status
)) {
2714 cli_close(cli
, fnum
);
2716 errno
= cli_status_to_errno(status
);
2720 cli_close(cli
, fnum
);