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 status
= cli_NetServerEnum(srv
->cli
,
708 if (!NT_STATUS_IS_OK(status
)) {
713 TALLOC_FREE(ip_list
);
716 * Server not an empty string ... Check the rest and see what
719 if (*share
== '\0') {
722 /* Should not have empty share with path */
724 SAFE_FREE(dir
->fname
);
728 errno
= EINVAL
+ 8197;
734 * We don't know if <server> is really a server name
735 * or is a workgroup/domain name. If we already have
736 * a server structure for it, we'll use it.
737 * Otherwise, check to see if <server><1D>,
738 * <server><1B>, or <server><20> translates. We check
739 * to see if <server> is an IP address first.
743 * See if we have an existing server. Do not
744 * establish a connection if one does not already
747 srv
= SMBC_server(frame
, context
, False
,
748 server
, port
, "IPC$",
749 &workgroup
, &user
, &password
);
752 * If no existing server and not an IP addr, look for
756 !is_ipaddress(server
) &&
757 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
758 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
760 * "server" is actually a workgroup name,
761 * not a server. Make this clear.
763 char *wgroup
= server
;
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 "
784 SAFE_FREE(dir
->fname
);
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$",
802 DEBUG(0, ("got no contact to IPC$\n"));
804 SAFE_FREE(dir
->fname
);
814 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
816 SAFE_FREE(dir
->fname
);
823 /* Now, list the servers ... */
824 status
= cli_NetServerEnum(srv
->cli
,
829 if (!NT_STATUS_IS_OK(status
)) {
831 SAFE_FREE(dir
->fname
);
838 (resolve_name(server
, &rem_ss
, 0x20, false))) {
842 * If we hadn't found the server, get one now
845 srv
= SMBC_server(frame
, context
, True
,
846 server
, port
, "IPC$",
853 SAFE_FREE(dir
->fname
);
861 dir
->dir_type
= SMBC_FILE_SHARE
;
864 /* List the shares ... */
866 status
= net_share_enum_rpc(srv
->cli
,
869 if (!NT_STATUS_IS_OK(status
) &&
870 smbXcli_conn_protocol(srv
->cli
->conn
) <=
873 * Only call cli_RNetShareEnum()
874 * on SMB1 connections, not SMB2+.
876 status
= cli_RNetShareEnum(
881 if (!NT_STATUS_IS_OK(status
)) {
883 SAFE_FREE(dir
->fname
);
887 errno
= map_errno_from_nt_status(
892 /* Neither the workgroup nor server exists */
893 errno
= ECONNREFUSED
;
895 SAFE_FREE(dir
->fname
);
905 * The server and share are specified ... work from
909 struct cli_state
*targetcli
;
910 struct cli_credentials
*creds
= NULL
;
913 /* We connect to the server and list the directory */
914 dir
->dir_type
= SMBC_FILE_SHARE
;
916 srv
= SMBC_server(frame
, context
, True
, server
, port
, share
,
917 &workgroup
, &user
, &password
);
921 SAFE_FREE(dir
->fname
);
930 /* Now, list the files ... */
932 path_len
= strlen(path
);
933 path
= talloc_asprintf_append(path
, "\\*");
936 SAFE_FREE(dir
->fname
);
943 creds
= context
->internal
->creds
;
945 status
= cli_resolve_path(
948 srv
->cli
, path
, &targetcli
, &targetpath
);
949 if (!NT_STATUS_IS_OK(status
)) {
950 d_printf("Could not resolve %s\n", path
);
952 SAFE_FREE(dir
->fname
);
959 status
= cli_list(targetcli
, targetpath
,
960 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
961 dir_list_fn
, (void *)dir
);
962 if (!NT_STATUS_IS_OK(status
)) {
965 SAFE_FREE(dir
->fname
);
968 saved_errno
= cli_status_to_errno(status
);
970 if (saved_errno
== EINVAL
) {
971 struct stat sb
= {0};
973 * See if they asked to opendir
974 * something other than a directory.
975 * If so, the converted error value we
976 * got would have been EINVAL rather
979 path
[path_len
] = '\0'; /* restore original path */
981 status
= SMBC_getatr(
986 if (NT_STATUS_IS_OK(status
) &&
987 !S_ISDIR(sb
.st_mode
)) {
989 /* It is. Correct the error value */
990 saved_errno
= ENOTDIR
;
995 * There was an error (we're in the
996 * !NT_STATUS_IS_OK branch) and the
997 * server good any more...
999 if (smbc_getFunctionCheckServer(
1000 context
)(context
, srv
)) {
1002 /* ... then remove it. */
1003 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
1006 * We could not remove the
1007 * server completely, remove
1008 * it from the cache so we
1009 * will not get it again. It
1010 * will be removed when the
1011 * last file/dir is closed.
1013 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
1018 errno
= saved_errno
;
1025 DLIST_ADD(context
->internal
->files
, dir
);
1032 * Routine to close a directory
1036 SMBC_closedir_ctx(SMBCCTX
*context
,
1039 TALLOC_CTX
*frame
= NULL
;
1041 if (!context
|| !context
->internal
->initialized
) {
1050 frame
= talloc_stackframe();
1052 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1058 remove_dir(dir
); /* Clean it up */
1059 remove_dirplus(dir
);
1061 DLIST_REMOVE(context
->internal
->files
, dir
);
1063 SAFE_FREE(dir
->fname
);
1064 SAFE_FREE(dir
); /* Free the space too */
1072 smbc_readdir_internal(SMBCCTX
* context
,
1073 struct smbc_dirent
*dest
,
1074 struct smbc_dirent
*src
,
1075 int max_namebuf_len
)
1077 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
1080 /* url-encode the name. get back remaining buffer space */
1082 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
1084 /* -1 means no null termination. */
1085 if (remaining_len
< 0) {
1089 /* We now know the name length */
1090 dest
->namelen
= strlen(dest
->name
);
1092 if (dest
->namelen
+ 1 < 1) {
1097 if (dest
->namelen
+ 1 >= max_namebuf_len
) {
1098 /* Out of space for comment. */
1102 /* Save the pointer to the beginning of the comment */
1103 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
1105 if (remaining_len
< 1) {
1106 /* No room for comment null termination. */
1110 /* Copy the comment */
1111 strlcpy(dest
->comment
, src
->comment
, remaining_len
);
1113 /* Save other fields */
1114 dest
->smbc_type
= src
->smbc_type
;
1115 dest
->commentlen
= strlen(dest
->comment
);
1116 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
1120 /* No encoding. Just copy the entry as is. */
1121 if (src
->dirlen
> max_namebuf_len
) {
1124 memcpy(dest
, src
, src
->dirlen
);
1125 if (src
->namelen
+ 1 < 1) {
1129 if (src
->namelen
+ 1 >= max_namebuf_len
) {
1130 /* Comment off the end. */
1133 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
1139 * Routine to get a directory entry
1142 struct smbc_dirent
*
1143 SMBC_readdir_ctx(SMBCCTX
*context
,
1148 struct smbc_dirent
*dirp
, *dirent
;
1149 TALLOC_CTX
*frame
= talloc_stackframe();
1151 /* Check that all is ok first ... */
1153 if (!context
|| !context
->internal
->initialized
) {
1156 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1162 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1165 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1171 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1174 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1180 if (!dir
->dir_next
) {
1185 dirent
= dir
->dir_next
->dirent
;
1194 dirp
= &context
->internal
->dirent
;
1195 maxlen
= sizeof(context
->internal
->_dirent_name
);
1197 ret
= smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1204 dir
->dir_next
= dir
->dir_next
->next
;
1207 * If we are returning file entries, we
1208 * have a duplicate list in dirplus.
1210 * Update dirplus_next also so readdir and
1211 * readdirplus are kept in sync.
1213 if (dir
->dirplus_list
!= NULL
) {
1214 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1222 * Routine to get a directory entry with all attributes
1225 const struct libsmb_file_info
*
1226 SMBC_readdirplus_ctx(SMBCCTX
*context
,
1229 struct libsmb_file_info
*smb_finfo
= NULL
;
1230 TALLOC_CTX
*frame
= talloc_stackframe();
1232 /* Check that all is ok first ... */
1234 if (context
== NULL
|| !context
->internal
->initialized
) {
1235 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1241 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1242 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1248 if (dir
->dirplus_next
== NULL
) {
1253 smb_finfo
= dir
->dirplus_next
->smb_finfo
;
1254 if (smb_finfo
== NULL
) {
1259 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1262 * If we are returning file entries, we
1263 * have a duplicate list in dir_list
1265 * Update dir_next also so readdir and
1266 * readdirplus are kept in sync.
1268 if (dir
->dir_list
) {
1269 dir
->dir_next
= dir
->dir_next
->next
;
1277 * Routine to get a directory entry plus a filled in stat structure if
1281 const struct libsmb_file_info
*SMBC_readdirplus2_ctx(SMBCCTX
*context
,
1285 struct libsmb_file_info
*smb_finfo
= NULL
;
1286 struct smbc_dirplus_list
*dp_list
= NULL
;
1288 char *full_pathname
= NULL
;
1289 char *workgroup
= NULL
;
1290 char *server
= NULL
;
1295 char *password
= NULL
;
1296 char *options
= NULL
;
1298 TALLOC_CTX
*frame
= NULL
;
1301 * Allow caller to pass in NULL for stat pointer if
1302 * required. This makes this call identical to
1303 * smbc_readdirplus().
1307 return SMBC_readdirplus_ctx(context
, dir
);
1310 frame
= talloc_stackframe();
1312 /* Check that all is ok first ... */
1313 if (context
== NULL
|| !context
->internal
->initialized
) {
1314 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1320 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1321 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1327 dp_list
= dir
->dirplus_next
;
1328 if (dp_list
== NULL
) {
1333 ino
= (ino_t
)dp_list
->ino
;
1335 smb_finfo
= dp_list
->smb_finfo
;
1336 if (smb_finfo
== NULL
) {
1342 full_pathname
= talloc_asprintf(frame
,
1346 if (full_pathname
== NULL
) {
1352 rc
= SMBC_parse_path(frame
,
1375 smb_finfo
->atime_ts
,
1376 smb_finfo
->ctime_ts
,
1377 smb_finfo
->mtime_ts
);
1379 TALLOC_FREE(full_pathname
);
1381 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1384 * If we are returning file entries, we
1385 * have a duplicate list in dir_list
1387 * Update dir_next also so readdir and
1388 * readdirplus are kept in sync.
1390 if (dir
->dir_list
) {
1391 dir
->dir_next
= dir
->dir_next
->next
;
1399 * Routine to get directory entries
1403 SMBC_getdents_ctx(SMBCCTX
*context
,
1405 struct smbc_dirent
*dirp
,
1411 char *ndir
= (char *)dirp
;
1412 struct smbc_dir_list
*dirlist
;
1413 TALLOC_CTX
*frame
= talloc_stackframe();
1415 /* Check that all is ok first ... */
1417 if (!context
|| !context
->internal
->initialized
) {
1425 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1433 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1442 * Now, retrieve the number of entries that will fit in what was passed
1443 * We have to figure out if the info is in the list, or we need to
1444 * send a request to the server to get the info.
1447 while ((dirlist
= dir
->dir_next
)) {
1449 struct smbc_dirent
*dirent
;
1450 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1452 if (!dirlist
->dirent
) {
1454 errno
= ENOENT
; /* Bad error */
1460 /* Do urlencoding of next entry, if so selected */
1461 dirent
= &context
->internal
->dirent
;
1462 maxlen
= sizeof(context
->internal
->_dirent_name
);
1463 ret
= smbc_readdir_internal(context
, dirent
,
1464 dirlist
->dirent
, maxlen
);
1471 reqd
= dirent
->dirlen
;
1475 if (rem
< count
) { /* We managed to copy something */
1482 else { /* Nothing copied ... */
1484 errno
= EINVAL
; /* Not enough space ... */
1492 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1494 currentEntry
->comment
= ¤tEntry
->name
[0] +
1495 dirent
->namelen
+ 1;
1500 /* Try and align the struct for the next entry
1501 on a valid pointer boundary by appending zeros */
1502 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1506 currentEntry
->dirlen
++;
1509 dir
->dir_next
= dirlist
= dirlist
-> next
;
1512 * If we are returning file entries, we
1513 * have a duplicate list in dirplus.
1515 * Update dirplus_next also so readdir and
1516 * readdirplus are kept in sync.
1518 if (dir
->dirplus_list
!= NULL
) {
1519 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1533 * Routine to create a directory ...
1537 SMBC_mkdir_ctx(SMBCCTX
*context
,
1541 SMBCSRV
*srv
= NULL
;
1542 char *server
= NULL
;
1545 char *password
= NULL
;
1546 char *workgroup
= NULL
;
1548 char *targetpath
= NULL
;
1550 struct cli_state
*targetcli
= NULL
;
1551 struct cli_credentials
*creds
= NULL
;
1552 TALLOC_CTX
*frame
= talloc_stackframe();
1555 if (!context
|| !context
->internal
->initialized
) {
1567 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1569 if (SMBC_parse_path(frame
,
1585 if (!user
|| user
[0] == (char)0) {
1586 user
= talloc_strdup(frame
, smbc_getUser(context
));
1594 srv
= SMBC_server(frame
, context
, True
,
1595 server
, port
, share
, &workgroup
, &user
, &password
);
1600 return -1; /* errno set by SMBC_server */
1604 creds
= context
->internal
->creds
;
1606 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1607 status
= cli_resolve_path(frame
, "",
1609 srv
->cli
, path
, &targetcli
, &targetpath
);
1610 if (!NT_STATUS_IS_OK(status
)) {
1611 d_printf("Could not resolve %s\n", path
);
1616 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1618 status
= cli_mkdir(targetcli
, targetpath
);
1619 if (!NT_STATUS_IS_OK(status
)) {
1621 errno
= cli_status_to_errno(status
);
1632 * Our list function simply checks to see if a directory is not empty
1636 rmdir_list_fn(struct file_info
*finfo
,
1640 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1641 strncmp(finfo
->name
, "..", 2) != 0) {
1642 bool *smbc_rmdir_dirempty
= (bool *)state
;
1643 *smbc_rmdir_dirempty
= false;
1645 return NT_STATUS_OK
;
1649 * Routine to remove a directory
1653 SMBC_rmdir_ctx(SMBCCTX
*context
,
1656 SMBCSRV
*srv
= NULL
;
1657 char *server
= NULL
;
1660 char *password
= NULL
;
1661 char *workgroup
= NULL
;
1663 char *targetpath
= NULL
;
1665 struct cli_state
*targetcli
= NULL
;
1666 struct cli_credentials
*creds
= NULL
;
1667 TALLOC_CTX
*frame
= talloc_stackframe();
1670 if (!context
|| !context
->internal
->initialized
) {
1682 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1684 if (SMBC_parse_path(frame
,
1700 if (!user
|| user
[0] == (char)0) {
1701 user
= talloc_strdup(frame
, smbc_getUser(context
));
1709 srv
= SMBC_server(frame
, context
, True
,
1710 server
, port
, share
, &workgroup
, &user
, &password
);
1715 return -1; /* errno set by SMBC_server */
1719 creds
= context
->internal
->creds
;
1721 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1722 status
= cli_resolve_path(frame
, "",
1724 srv
->cli
, path
, &targetcli
, &targetpath
);
1725 if (!NT_STATUS_IS_OK(status
)) {
1726 d_printf("Could not resolve %s\n", path
);
1731 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1733 status
= cli_rmdir(targetcli
, targetpath
);
1735 if (!NT_STATUS_IS_OK(status
)) {
1737 errno
= cli_status_to_errno(status
);
1739 if (errno
== EACCES
) { /* Check if the dir empty or not */
1741 /* Local storage to avoid buffer overflows */
1743 bool smbc_rmdir_dirempty
= true;
1745 lpath
= talloc_asprintf(frame
, "%s\\*",
1753 status
= cli_list(targetcli
, lpath
,
1754 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1756 &smbc_rmdir_dirempty
);
1758 if (!NT_STATUS_IS_OK(status
)) {
1759 /* Fix errno to ignore latest error ... */
1760 DBG_INFO("cli_list returned an error: %s\n",
1766 if (smbc_rmdir_dirempty
)
1784 * Routine to return the current directory position
1788 SMBC_telldir_ctx(SMBCCTX
*context
,
1791 TALLOC_CTX
*frame
= talloc_stackframe();
1793 if (!context
|| !context
->internal
->initialized
) {
1801 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1809 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1817 /* See if we're already at the end. */
1818 if (dir
->dir_next
== NULL
) {
1825 * We return the pointer here as the offset
1828 return (off_t
)(long)dir
->dir_next
->dirent
;
1832 * A routine to run down the list and see if the entry is OK
1833 * Modifies the dir list and the dirplus list (if it exists)
1834 * to point at the correct next entry on success.
1837 static bool update_dir_ents(SMBCFILE
*dir
, struct smbc_dirent
*dirent
)
1839 struct smbc_dir_list
*tmp_dir
= dir
->dir_list
;
1840 struct smbc_dirplus_list
*tmp_dirplus
= dir
->dirplus_list
;
1843 * Run down the list looking for what we want.
1844 * If we're enumerating files both dir_list
1845 * and dirplus_list contain the same entry
1846 * list, as they were seeded from the same
1847 * cli_list callback.
1849 * If we're enumerating servers then
1850 * dirplus_list will be NULL, so don't
1851 * update in that case.
1854 while (tmp_dir
!= NULL
) {
1855 if (tmp_dir
->dirent
== dirent
) {
1856 dir
->dir_next
= tmp_dir
;
1857 if (tmp_dirplus
!= NULL
) {
1858 dir
->dirplus_next
= tmp_dirplus
;
1862 tmp_dir
= tmp_dir
->next
;
1863 if (tmp_dirplus
!= NULL
) {
1864 tmp_dirplus
= tmp_dirplus
->next
;
1871 * Routine to seek on a directory
1875 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1879 long int l_offset
= offset
; /* Handle problems of size */
1880 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1881 TALLOC_CTX
*frame
= talloc_stackframe();
1884 if (!context
|| !context
->internal
->initialized
) {
1892 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1900 /* Now, check what we were passed and see if it is OK ... */
1902 if (dirent
== NULL
) { /* Seek to the beginning of the list */
1904 dir
->dir_next
= dir
->dir_list
;
1906 /* Do the same for dirplus. */
1907 dir
->dirplus_next
= dir
->dirplus_list
;
1914 if (offset
== -1) { /* Seek to the end of the list */
1915 dir
->dir_next
= NULL
;
1917 /* Do the same for dirplus. */
1918 dir
->dirplus_next
= NULL
;
1925 * Run down the list and make sure that the entry is OK.
1926 * Update the position of both dir and dirplus lists.
1929 ok
= update_dir_ents(dir
, dirent
);
1931 errno
= EINVAL
; /* Bad entry */
1941 * Routine to fstat a dir
1945 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1950 if (!context
|| !context
->internal
->initialized
) {
1956 /* No code yet ... */
1961 SMBC_chmod_ctx(SMBCCTX
*context
,
1965 SMBCSRV
*srv
= NULL
;
1966 char *server
= NULL
;
1969 char *password
= NULL
;
1970 char *workgroup
= NULL
;
1971 char *targetpath
= NULL
;
1972 struct cli_state
*targetcli
= NULL
;
1976 struct cli_credentials
*creds
= NULL
;
1977 TALLOC_CTX
*frame
= talloc_stackframe();
1980 if (!context
|| !context
->internal
->initialized
) {
1982 errno
= EINVAL
; /* Best I can think of ... */
1993 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1995 if (SMBC_parse_path(frame
,
2011 if (!user
|| user
[0] == (char)0) {
2012 user
= talloc_strdup(frame
, smbc_getUser(context
));
2020 srv
= SMBC_server(frame
, context
, True
,
2021 server
, port
, share
, &workgroup
, &user
, &password
);
2025 return -1; /* errno set by SMBC_server */
2028 creds
= context
->internal
->creds
;
2030 /*d_printf(">>>unlink: resolving %s\n", path);*/
2031 status
= cli_resolve_path(frame
, "",
2033 srv
->cli
, path
, &targetcli
, &targetpath
);
2034 if (!NT_STATUS_IS_OK(status
)) {
2035 d_printf("Could not resolve %s\n", path
);
2043 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) attr
|= FILE_ATTRIBUTE_READONLY
;
2044 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) attr
|= FILE_ATTRIBUTE_ARCHIVE
;
2045 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) attr
|= FILE_ATTRIBUTE_SYSTEM
;
2046 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) attr
|= FILE_ATTRIBUTE_HIDDEN
;
2048 status
= cli_setatr(targetcli
, targetpath
, attr
, 0);
2049 if (!NT_STATUS_IS_OK(status
)) {
2051 errno
= cli_status_to_errno(status
);
2060 SMBC_utimes_ctx(SMBCCTX
*context
,
2062 struct timeval
*tbuf
)
2064 SMBCSRV
*srv
= NULL
;
2065 char *server
= NULL
;
2068 char *password
= NULL
;
2069 char *workgroup
= NULL
;
2071 struct timespec access_time
, write_time
;
2073 TALLOC_CTX
*frame
= talloc_stackframe();
2076 if (!context
|| !context
->internal
->initialized
) {
2078 errno
= EINVAL
; /* Best I can think of ... */
2090 access_time
= write_time
= timespec_current();
2092 access_time
= convert_timeval_to_timespec(tbuf
[0]);
2093 write_time
= convert_timeval_to_timespec(tbuf
[1]);
2097 struct timeval_buf abuf
, wbuf
;
2099 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2101 timespec_string_buf(&access_time
, false, &abuf
),
2102 timespec_string_buf(&write_time
, false, &wbuf
));
2105 if (SMBC_parse_path(frame
,
2121 if (!user
|| user
[0] == (char)0) {
2122 user
= talloc_strdup(frame
, smbc_getUser(context
));
2130 srv
= SMBC_server(frame
, context
, True
,
2131 server
, port
, share
, &workgroup
, &user
, &password
);
2135 return -1; /* errno set by SMBC_server */
2142 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2145 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2149 return -1; /* errno set by SMBC_setatr */
2157 * Routine to unlink() a file
2161 SMBC_unlink_ctx(SMBCCTX
*context
,
2164 char *server
= NULL
;
2167 char *password
= NULL
;
2168 char *workgroup
= NULL
;
2170 char *targetpath
= NULL
;
2172 struct cli_state
*targetcli
= NULL
;
2173 SMBCSRV
*srv
= NULL
;
2174 struct cli_credentials
*creds
= NULL
;
2175 TALLOC_CTX
*frame
= talloc_stackframe();
2178 if (!context
|| !context
->internal
->initialized
) {
2180 errno
= EINVAL
; /* Best I can think of ... */
2193 if (SMBC_parse_path(frame
,
2209 if (!user
|| user
[0] == (char)0) {
2210 user
= talloc_strdup(frame
, smbc_getUser(context
));
2218 srv
= SMBC_server(frame
, context
, True
,
2219 server
, port
, share
, &workgroup
, &user
, &password
);
2223 return -1; /* SMBC_server sets errno */
2227 creds
= context
->internal
->creds
;
2229 /*d_printf(">>>unlink: resolving %s\n", path);*/
2230 status
= cli_resolve_path(frame
, "",
2232 srv
->cli
, path
, &targetcli
, &targetpath
);
2233 if (!NT_STATUS_IS_OK(status
)) {
2234 d_printf("Could not resolve %s\n", path
);
2239 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2241 status
= cli_unlink(
2244 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2246 if (!NT_STATUS_IS_OK(status
)) {
2248 errno
= cli_status_to_errno(status
);
2250 if (errno
== EACCES
) { /* Check if the file is a directory */
2253 struct stat sb
= {0};
2255 status
= SMBC_getatr(context
, srv
, path
, &sb
);
2256 if (!NT_STATUS_IS_OK(status
)) {
2257 /* Hmmm, bad error ... What? */
2260 errno
= cli_status_to_errno(status
);
2266 if (S_ISDIR(sb
.st_mode
))
2269 errno
= saverr
; /* Restore this */
2280 return 0; /* Success ... */
2285 * Routine to rename() a file
2289 SMBC_rename_ctx(SMBCCTX
*ocontext
,
2294 char *server1
= NULL
;
2295 char *share1
= NULL
;
2296 char *server2
= NULL
;
2297 char *share2
= NULL
;
2300 char *password1
= NULL
;
2301 char *password2
= NULL
;
2302 char *workgroup
= NULL
;
2305 char *targetpath1
= NULL
;
2306 char *targetpath2
= NULL
;
2307 struct cli_state
*targetcli1
= NULL
;
2308 struct cli_state
*targetcli2
= NULL
;
2309 SMBCSRV
*srv
= NULL
;
2312 struct cli_credentials
*ocreds
= NULL
;
2313 struct cli_credentials
*ncreds
= NULL
;
2314 TALLOC_CTX
*frame
= talloc_stackframe();
2317 if (!ocontext
|| !ncontext
||
2318 !ocontext
->internal
->initialized
||
2319 !ncontext
->internal
->initialized
) {
2321 errno
= EINVAL
; /* Best I can think of ... */
2326 if (!oname
|| !nname
) {
2332 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
2334 if (SMBC_parse_path(frame
,
2350 if (!user1
|| user1
[0] == (char)0) {
2351 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
2359 if (SMBC_parse_path(frame
,
2375 if (!user2
|| user2
[0] == (char)0) {
2376 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
2384 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2385 strcmp(user1
, user2
)) {
2386 /* Can't rename across file systems, or users?? */
2392 srv
= SMBC_server(frame
, ocontext
, True
,
2393 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2400 /* set the credentials to make DFS work */
2401 smbc_set_credentials_with_fallback(ocontext
,
2406 /*d_printf(">>>rename: resolving %s\n", path1);*/
2407 ocreds
= ocontext
->internal
->creds
;
2409 status
= cli_resolve_path(frame
, "",
2411 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2412 if (!NT_STATUS_IS_OK(status
)) {
2413 d_printf("Could not resolve %s\n", path1
);
2419 /* set the credentials to make DFS work */
2420 smbc_set_credentials_with_fallback(ncontext
,
2425 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2426 /*d_printf(">>>rename: resolving %s\n", path2);*/
2427 ncreds
= ncontext
->internal
->creds
;
2429 status
= cli_resolve_path(frame
, "",
2431 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2432 if (!NT_STATUS_IS_OK(status
)) {
2433 d_printf("Could not resolve %s\n", path2
);
2438 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2440 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2441 strcmp(targetcli1
->share
, targetcli2
->share
))
2443 /* can't rename across file systems */
2449 status
= cli_rename(targetcli1
, targetpath1
, targetpath2
, false);
2450 if (!NT_STATUS_IS_OK(status
)) {
2451 int eno
= cli_status_to_errno(status
);
2453 if (eno
!= EEXIST
||
2454 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2455 FILE_ATTRIBUTE_SYSTEM
|
2456 FILE_ATTRIBUTE_HIDDEN
)) ||
2457 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2458 targetpath2
, false))) {
2468 return 0; /* Success */
2471 struct smbc_notify_cb_state
{
2472 struct tevent_context
*ev
;
2473 struct cli_state
*cli
;
2476 uint32_t completion_filter
;
2477 unsigned callback_timeout_ms
;
2478 smbc_notify_callback_fn cb
;
2482 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2483 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2485 static struct tevent_req
*smbc_notify_cb_send(
2486 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2487 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2488 unsigned callback_timeout_ms
,
2489 smbc_notify_callback_fn cb
, void *private_data
)
2491 struct tevent_req
*req
, *subreq
;
2492 struct smbc_notify_cb_state
*state
;
2494 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2501 state
->recursive
= recursive
;
2502 state
->completion_filter
= completion_filter
;
2503 state
->callback_timeout_ms
= callback_timeout_ms
;
2505 state
->private_data
= private_data
;
2507 subreq
= cli_notify_send(
2508 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2509 state
->completion_filter
, state
->recursive
);
2510 if (tevent_req_nomem(subreq
, req
)) {
2511 return tevent_req_post(req
, ev
);
2513 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2515 if (state
->callback_timeout_ms
== 0) {
2519 subreq
= tevent_wakeup_send(
2521 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2522 state
->callback_timeout_ms
*1000));
2523 if (tevent_req_nomem(subreq
, req
)) {
2524 return tevent_req_post(req
, ev
);
2526 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2531 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2533 struct tevent_req
*req
= tevent_req_callback_data(
2534 subreq
, struct tevent_req
);
2535 struct smbc_notify_cb_state
*state
= tevent_req_data(
2536 req
, struct smbc_notify_cb_state
);
2537 uint32_t num_changes
;
2538 struct notify_change
*changes
;
2542 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2543 TALLOC_FREE(subreq
);
2544 if (tevent_req_nterror(req
, status
)) {
2549 struct smbc_notify_callback_action actions
[num_changes
];
2552 for (i
=0; i
<num_changes
; i
++) {
2553 actions
[i
].action
= changes
[i
].action
;
2554 actions
[i
].filename
= changes
[i
].name
;
2557 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2560 TALLOC_FREE(changes
);
2563 tevent_req_done(req
);
2567 subreq
= cli_notify_send(
2568 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2569 state
->completion_filter
, state
->recursive
);
2570 if (tevent_req_nomem(subreq
, req
)) {
2573 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2576 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2578 struct tevent_req
*req
= tevent_req_callback_data(
2579 subreq
, struct tevent_req
);
2580 struct smbc_notify_cb_state
*state
= tevent_req_data(
2581 req
, struct smbc_notify_cb_state
);
2585 ok
= tevent_wakeup_recv(subreq
);
2586 TALLOC_FREE(subreq
);
2588 tevent_req_oom(req
);
2592 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2594 tevent_req_done(req
);
2598 subreq
= tevent_wakeup_send(
2600 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2601 state
->callback_timeout_ms
*1000));
2602 if (tevent_req_nomem(subreq
, req
)) {
2605 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2608 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2610 return tevent_req_simple_recv_ntstatus(req
);
2613 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2614 bool recursive
, uint32_t completion_filter
,
2615 unsigned callback_timeout_ms
,
2616 smbc_notify_callback_fn cb
, void *private_data
)
2618 TALLOC_CTX
*frame
= talloc_stackframe();
2619 struct tevent_context
*ev
;
2620 struct tevent_req
*req
;
2621 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2623 ev
= samba_tevent_context_init(frame
);
2627 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2629 callback_timeout_ms
, cb
, private_data
);
2633 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2636 status
= smbc_notify_cb_recv(req
);
2644 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2645 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2646 smbc_notify_callback_fn cb
, void *private_data
)
2648 TALLOC_CTX
*frame
= talloc_stackframe();
2649 struct cli_state
*cli
;
2650 char *server
= NULL
;
2653 char *password
= NULL
;
2654 char *options
= NULL
;
2655 char *workgroup
= NULL
;
2661 if ((context
== NULL
) || !context
->internal
->initialized
) {
2666 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2672 if (SMBC_parse_path(frame
,
2683 DEBUG(4, ("no valid path\n"));
2685 errno
= EINVAL
+ 8194;
2689 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2690 "path='%s' options='%s'\n",
2691 dir
->fname
, server
, share
, path
, options
));
2693 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2694 (int)recursive
, completion_filter
));
2696 cli
= dir
->srv
->cli
;
2697 status
= cli_ntcreate(
2698 cli
, path
, 0, FILE_READ_DATA
, 0,
2699 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2700 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2701 if (!NT_STATUS_IS_OK(status
)) {
2703 errno
= cli_status_to_errno(status
);
2707 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2708 callback_timeout_ms
, cb
, private_data
);
2709 if (!NT_STATUS_IS_OK(status
)) {
2710 cli_close(cli
, fnum
);
2712 errno
= cli_status_to_errno(status
);
2716 cli_close(cli
, fnum
);