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
->gid
= finfo
->gid
;
190 info
->attrs
= finfo
->attr
;
191 info
->size
= finfo
->size
;
192 info
->uid
= finfo
->uid
;
193 info
->name
= SMB_STRDUP(finfo
->name
);
194 if (info
->name
== NULL
) {
196 SAFE_FREE(new_entry
);
197 dir
->dir_error
= ENOMEM
;
201 if (finfo
->short_name
) {
202 info
->short_name
= SMB_STRDUP(finfo
->short_name
);
204 info
->short_name
= SMB_STRDUP("");
207 if (info
->short_name
== NULL
) {
208 SAFE_FREE(info
->name
);
210 SAFE_FREE(new_entry
);
211 dir
->dir_error
= ENOMEM
;
214 new_entry
->smb_finfo
= info
;
216 /* Now add to the list. */
217 if (dir
->dirplus_list
== NULL
) {
218 /* Empty list - point everything at new_entry. */
219 dir
->dirplus_list
= new_entry
;
220 dir
->dirplus_end
= new_entry
;
221 dir
->dirplus_next
= new_entry
;
223 /* Append to list but leave the ->next cursor alone. */
224 dir
->dirplus_end
->next
= new_entry
;
225 dir
->dirplus_end
= new_entry
;
232 list_unique_wg_fn(const char *name
,
237 SMBCFILE
*dir
= (SMBCFILE
*)state
;
238 struct smbc_dir_list
*dir_list
;
239 struct smbc_dirent
*dirent
;
243 dirent_type
= dir
->dir_type
;
245 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
246 /* An error occurred, what do we do? */
247 /* FIXME: Add some code here */
248 /* Change cli_NetServerEnum to take a fn
249 returning NTSTATUS... JRA. */
252 /* Point to the one just added */
253 dirent
= dir
->dir_end
->dirent
;
255 /* See if this was a duplicate */
256 for (dir_list
= dir
->dir_list
;
257 dir_list
!= dir
->dir_end
;
258 dir_list
= dir_list
->next
) {
260 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
261 /* Duplicate. End end of list need to be removed. */
265 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
266 /* Found the end of the list. Remove it. */
267 dir
->dir_end
= dir_list
;
268 free(dir_list
->next
);
270 dir_list
->next
= NULL
;
277 list_fn(const char *name
,
282 SMBCFILE
*dir
= (SMBCFILE
*)state
;
286 * We need to process the type a little ...
288 * Disk share = 0x00000000
289 * Print share = 0x00000001
290 * Comms share = 0x00000002 (obsolete?)
291 * IPC$ share = 0x00000003
293 * administrative shares:
294 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
297 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
301 dirent_type
= SMBC_FILE_SHARE
;
305 dirent_type
= SMBC_PRINTER_SHARE
;
309 dirent_type
= SMBC_COMMS_SHARE
;
314 dirent_type
= SMBC_IPC_SHARE
;
318 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
323 dirent_type
= dir
->dir_type
;
326 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
327 /* An error occurred, what do we do? */
328 /* FIXME: Add some code here */
329 /* Change cli_NetServerEnum to take a fn
330 returning NTSTATUS... JRA. */
335 dir_list_fn(struct file_info
*finfo
,
339 SMBCFILE
*dirp
= (SMBCFILE
*)state
;
342 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
343 (finfo
->attr
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
344 SMBCFILE
*dir
= (SMBCFILE
*)state
;
345 return map_nt_error_from_unix(dir
->dir_error
);
347 ret
= add_dirplus(dirp
, finfo
);
349 return map_nt_error_from_unix(dirp
->dir_error
);
355 net_share_enum_rpc(struct cli_state
*cli
,
356 void (*fn
)(const char *name
,
364 uint32_t preferred_len
= 0xffffffff;
366 struct srvsvc_NetShareInfoCtr info_ctr
;
367 struct srvsvc_NetShareCtr1 ctr1
;
369 fstring comment
= "";
370 struct rpc_pipe_client
*pipe_hnd
= NULL
;
372 uint32_t resume_handle
= 0;
373 uint32_t total_entries
= 0;
374 struct dcerpc_binding_handle
*b
;
376 /* Open the server service pipe */
377 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
379 if (!NT_STATUS_IS_OK(nt_status
)) {
380 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
384 ZERO_STRUCT(info_ctr
);
388 info_ctr
.ctr
.ctr1
= &ctr1
;
390 b
= pipe_hnd
->binding_handle
;
392 /* Issue the NetShareEnum RPC call and retrieve the response */
393 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
401 /* Was it successful? */
402 if (!NT_STATUS_IS_OK(nt_status
)) {
403 /* Nope. Go clean up. */
407 if (!W_ERROR_IS_OK(result
)) {
408 /* Nope. Go clean up. */
409 nt_status
= werror_to_ntstatus(result
);
413 if (total_entries
== 0) {
414 /* Nope. Go clean up. */
415 nt_status
= NT_STATUS_NOT_FOUND
;
419 /* For each returned entry... */
420 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
422 /* pull out the share name */
423 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
425 /* pull out the share's comment */
426 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
428 /* Get the type value */
429 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
431 /* Add this share to the list */
432 (*fn
)(name
, type
, comment
, state
);
436 /* Close the server service pipe */
437 TALLOC_FREE(pipe_hnd
);
439 /* Tell 'em if it worked */
445 * Verify that the options specified in a URL are valid
448 SMBC_check_options(char *server
,
453 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
454 "path='%s' options='%s'\n",
455 server
, share
, path
, options
));
457 /* No options at all is always ok */
458 if (! *options
) return 0;
460 /* Currently, we don't support any options. */
466 SMBC_opendir_ctx(SMBCCTX
*context
,
472 char *password
= NULL
;
473 char *options
= NULL
;
474 char *workgroup
= NULL
;
479 SMBCFILE
*dir
= NULL
;
480 struct sockaddr_storage rem_ss
;
481 TALLOC_CTX
*frame
= talloc_stackframe();
483 if (!context
|| !context
->internal
->initialized
) {
484 DEBUG(4, ("no valid context\n"));
486 errno
= EINVAL
+ 8192;
492 DEBUG(4, ("no valid fname\n"));
494 errno
= EINVAL
+ 8193;
498 if (SMBC_parse_path(frame
,
509 DEBUG(4, ("no valid path\n"));
511 errno
= EINVAL
+ 8194;
515 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
516 "path='%s' options='%s'\n",
517 fname
, server
, share
, path
, options
));
519 /* Ensure the options are valid */
520 if (SMBC_check_options(server
, share
, path
, options
)) {
521 DEBUG(4, ("unacceptable options (%s)\n", options
));
523 errno
= EINVAL
+ 8195;
527 if (!user
|| user
[0] == (char)0) {
528 user
= talloc_strdup(frame
, smbc_getUser(context
));
536 dir
= SMB_MALLOC_P(SMBCFILE
);
547 dir
->fname
= SMB_STRDUP(fname
);
548 if (dir
->fname
== NULL
) {
557 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
559 if (server
[0] == (char)0) {
563 size_t max_lmb_count
;
564 struct sockaddr_storage
*ip_list
;
565 struct sockaddr_storage server_addr
;
566 struct cli_credentials
*creds
= NULL
;
569 if (share
[0] != (char)0 || path
[0] != (char)0) {
572 SAFE_FREE(dir
->fname
);
576 errno
= EINVAL
+ 8196;
580 /* Determine how many local master browsers to query */
581 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
583 : smbc_getOptionBrowseMaxLmbCount(context
));
585 creds
= cli_credentials_init(frame
);
588 SAFE_FREE(dir
->fname
);
596 (void)cli_credentials_set_username(creds
, user
, CRED_SPECIFIED
);
597 (void)cli_credentials_set_password(creds
, password
, CRED_SPECIFIED
);
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.
609 status
= name_resolve_bcast(talloc_tos(),
614 if (!NT_STATUS_IS_OK(status
))
617 TALLOC_FREE(ip_list
);
619 if (!find_master_ip(workgroup
, &server_addr
)) {
622 SAFE_FREE(dir
->fname
);
630 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
631 talloc_tos(), &server_addr
,
632 sizeof(server_addr
));
633 if (ip_list
== NULL
) {
635 SAFE_FREE(dir
->fname
);
645 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
646 char addr
[INET6_ADDRSTRLEN
];
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
),
655 cli
= get_ipc_connect_master_ip(talloc_tos(),
659 /* cli == NULL is the master browser refused to talk or
660 could not be found */
665 workgroup
= talloc_strdup(frame
, wg_ptr
);
666 server
= talloc_strdup(frame
, smbXcli_conn_remote_name(cli
->conn
));
670 if (!workgroup
|| !server
) {
672 SAFE_FREE(dir
->fname
);
680 DEBUG(4, ("using workgroup %s %s\n",
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
);
696 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
701 dir
->dir_type
= SMBC_WORKGROUP
;
703 /* Now, list the stuff ... */
705 if (!cli_NetServerEnum(srv
->cli
,
714 TALLOC_FREE(ip_list
);
717 * Server not an empty string ... Check the rest and see what
720 if (*share
== '\0') {
723 /* Should not have empty share with path */
725 SAFE_FREE(dir
->fname
);
729 errno
= EINVAL
+ 8197;
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
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
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
;
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 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
829 SAFE_FREE(dir
->fname
);
836 (resolve_name(server
, &rem_ss
, 0x20, false))) {
840 * If we hadn't found the server, get one now
843 srv
= SMBC_server(frame
, context
, True
,
844 server
, port
, "IPC$",
851 SAFE_FREE(dir
->fname
);
859 dir
->dir_type
= SMBC_FILE_SHARE
;
862 /* List the shares ... */
864 status
= net_share_enum_rpc(srv
->cli
,
867 if (!NT_STATUS_IS_OK(status
) &&
868 smbXcli_conn_protocol(srv
->cli
->conn
) <=
871 * Only call cli_RNetShareEnum()
872 * on SMB1 connections, not SMB2+.
874 int rc
= cli_RNetShareEnum(srv
->cli
,
878 status
= cli_nt_error(srv
->cli
);
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
;
890 SAFE_FREE(dir
->fname
);
894 errno
= map_errno_from_nt_status(
899 /* Neither the workgroup nor server exists */
900 errno
= ECONNREFUSED
;
902 SAFE_FREE(dir
->fname
);
912 * The server and share are specified ... work from
916 struct cli_state
*targetcli
;
917 struct cli_credentials
*creds
= NULL
;
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
);
928 SAFE_FREE(dir
->fname
);
937 /* Now, list the files ... */
939 path_len
= strlen(path
);
940 path
= talloc_asprintf_append(path
, "\\*");
943 SAFE_FREE(dir
->fname
);
950 creds
= context
->internal
->creds
;
952 status
= cli_resolve_path(
955 srv
->cli
, path
, &targetcli
, &targetpath
);
956 if (!NT_STATUS_IS_OK(status
)) {
957 d_printf("Could not resolve %s\n", path
);
959 SAFE_FREE(dir
->fname
);
966 status
= cli_list(targetcli
, targetpath
,
967 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
968 dir_list_fn
, (void *)dir
);
969 if (!NT_STATUS_IS_OK(status
)) {
972 SAFE_FREE(dir
->fname
);
975 saved_errno
= cli_status_to_errno(status
);
977 if (saved_errno
== EINVAL
) {
978 struct stat sb
= {0};
980 * See if they asked to opendir
981 * something other than a directory.
982 * If so, the converted error value we
983 * got would have been EINVAL rather
986 path
[path_len
] = '\0'; /* restore original path */
988 status
= SMBC_getatr(
993 if (NT_STATUS_IS_OK(status
) &&
994 !S_ISDIR(sb
.st_mode
)) {
996 /* It is. Correct the error value */
997 saved_errno
= ENOTDIR
;
1002 * If there was an error and the server is no
1005 if (cli_is_error(targetcli
) &&
1006 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
1008 /* ... then remove it. */
1009 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
1012 * We could not remove the
1013 * server completely, remove
1014 * it from the cache so we
1015 * will not get it again. It
1016 * will be removed when the
1017 * last file/dir is closed.
1019 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
1024 errno
= saved_errno
;
1031 DLIST_ADD(context
->internal
->files
, dir
);
1038 * Routine to close a directory
1042 SMBC_closedir_ctx(SMBCCTX
*context
,
1045 TALLOC_CTX
*frame
= NULL
;
1047 if (!context
|| !context
->internal
->initialized
) {
1056 frame
= talloc_stackframe();
1058 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1064 remove_dir(dir
); /* Clean it up */
1065 remove_dirplus(dir
);
1067 DLIST_REMOVE(context
->internal
->files
, dir
);
1069 SAFE_FREE(dir
->fname
);
1070 SAFE_FREE(dir
); /* Free the space too */
1078 smbc_readdir_internal(SMBCCTX
* context
,
1079 struct smbc_dirent
*dest
,
1080 struct smbc_dirent
*src
,
1081 int max_namebuf_len
)
1083 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
1086 /* url-encode the name. get back remaining buffer space */
1088 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
1090 /* -1 means no null termination. */
1091 if (remaining_len
< 0) {
1095 /* We now know the name length */
1096 dest
->namelen
= strlen(dest
->name
);
1098 if (dest
->namelen
+ 1 < 1) {
1103 if (dest
->namelen
+ 1 >= max_namebuf_len
) {
1104 /* Out of space for comment. */
1108 /* Save the pointer to the beginning of the comment */
1109 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
1111 if (remaining_len
< 1) {
1112 /* No room for comment null termination. */
1116 /* Copy the comment */
1117 strlcpy(dest
->comment
, src
->comment
, remaining_len
);
1119 /* Save other fields */
1120 dest
->smbc_type
= src
->smbc_type
;
1121 dest
->commentlen
= strlen(dest
->comment
);
1122 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
1126 /* No encoding. Just copy the entry as is. */
1127 if (src
->dirlen
> max_namebuf_len
) {
1130 memcpy(dest
, src
, src
->dirlen
);
1131 if (src
->namelen
+ 1 < 1) {
1135 if (src
->namelen
+ 1 >= max_namebuf_len
) {
1136 /* Comment off the end. */
1139 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
1145 * Routine to get a directory entry
1148 struct smbc_dirent
*
1149 SMBC_readdir_ctx(SMBCCTX
*context
,
1154 struct smbc_dirent
*dirp
, *dirent
;
1155 TALLOC_CTX
*frame
= talloc_stackframe();
1157 /* Check that all is ok first ... */
1159 if (!context
|| !context
->internal
->initialized
) {
1162 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1168 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1171 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1177 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1180 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1186 if (!dir
->dir_next
) {
1191 dirent
= dir
->dir_next
->dirent
;
1200 dirp
= &context
->internal
->dirent
;
1201 maxlen
= sizeof(context
->internal
->_dirent_name
);
1203 ret
= smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1210 dir
->dir_next
= dir
->dir_next
->next
;
1213 * If we are returning file entries, we
1214 * have a duplicate list in dirplus.
1216 * Update dirplus_next also so readdir and
1217 * readdirplus are kept in sync.
1219 if (dir
->dirplus_list
!= NULL
) {
1220 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1228 * Routine to get a directory entry with all attributes
1231 const struct libsmb_file_info
*
1232 SMBC_readdirplus_ctx(SMBCCTX
*context
,
1235 struct libsmb_file_info
*smb_finfo
= NULL
;
1236 TALLOC_CTX
*frame
= talloc_stackframe();
1238 /* Check that all is ok first ... */
1240 if (context
== NULL
|| !context
->internal
->initialized
) {
1241 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1247 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1248 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1254 if (dir
->dirplus_next
== NULL
) {
1259 smb_finfo
= dir
->dirplus_next
->smb_finfo
;
1260 if (smb_finfo
== NULL
) {
1265 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1268 * If we are returning file entries, we
1269 * have a duplicate list in dir_list
1271 * Update dir_next also so readdir and
1272 * readdirplus are kept in sync.
1274 if (dir
->dir_list
) {
1275 dir
->dir_next
= dir
->dir_next
->next
;
1283 * Routine to get a directory entry plus a filled in stat structure if
1287 const struct libsmb_file_info
*SMBC_readdirplus2_ctx(SMBCCTX
*context
,
1291 struct libsmb_file_info
*smb_finfo
= NULL
;
1292 struct smbc_dirplus_list
*dp_list
= NULL
;
1294 char *full_pathname
= NULL
;
1295 char *workgroup
= NULL
;
1296 char *server
= NULL
;
1301 char *password
= NULL
;
1302 char *options
= NULL
;
1304 TALLOC_CTX
*frame
= NULL
;
1307 * Allow caller to pass in NULL for stat pointer if
1308 * required. This makes this call identical to
1309 * smbc_readdirplus().
1313 return SMBC_readdirplus_ctx(context
, dir
);
1316 frame
= talloc_stackframe();
1318 /* Check that all is ok first ... */
1319 if (context
== NULL
|| !context
->internal
->initialized
) {
1320 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1326 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1327 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1333 dp_list
= dir
->dirplus_next
;
1334 if (dp_list
== NULL
) {
1339 ino
= (ino_t
)dp_list
->ino
;
1341 smb_finfo
= dp_list
->smb_finfo
;
1342 if (smb_finfo
== NULL
) {
1348 full_pathname
= talloc_asprintf(frame
,
1352 if (full_pathname
== NULL
) {
1358 rc
= SMBC_parse_path(frame
,
1381 smb_finfo
->atime_ts
,
1382 smb_finfo
->ctime_ts
,
1383 smb_finfo
->mtime_ts
);
1385 TALLOC_FREE(full_pathname
);
1387 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1390 * If we are returning file entries, we
1391 * have a duplicate list in dir_list
1393 * Update dir_next also so readdir and
1394 * readdirplus are kept in sync.
1396 if (dir
->dir_list
) {
1397 dir
->dir_next
= dir
->dir_next
->next
;
1405 * Routine to get directory entries
1409 SMBC_getdents_ctx(SMBCCTX
*context
,
1411 struct smbc_dirent
*dirp
,
1417 char *ndir
= (char *)dirp
;
1418 struct smbc_dir_list
*dirlist
;
1419 TALLOC_CTX
*frame
= talloc_stackframe();
1421 /* Check that all is ok first ... */
1423 if (!context
|| !context
->internal
->initialized
) {
1431 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1439 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1448 * Now, retrieve the number of entries that will fit in what was passed
1449 * We have to figure out if the info is in the list, or we need to
1450 * send a request to the server to get the info.
1453 while ((dirlist
= dir
->dir_next
)) {
1455 struct smbc_dirent
*dirent
;
1456 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1458 if (!dirlist
->dirent
) {
1460 errno
= ENOENT
; /* Bad error */
1466 /* Do urlencoding of next entry, if so selected */
1467 dirent
= &context
->internal
->dirent
;
1468 maxlen
= sizeof(context
->internal
->_dirent_name
);
1469 ret
= smbc_readdir_internal(context
, dirent
,
1470 dirlist
->dirent
, maxlen
);
1477 reqd
= dirent
->dirlen
;
1481 if (rem
< count
) { /* We managed to copy something */
1488 else { /* Nothing copied ... */
1490 errno
= EINVAL
; /* Not enough space ... */
1498 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1500 currentEntry
->comment
= ¤tEntry
->name
[0] +
1501 dirent
->namelen
+ 1;
1506 /* Try and align the struct for the next entry
1507 on a valid pointer boundary by appending zeros */
1508 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1512 currentEntry
->dirlen
++;
1515 dir
->dir_next
= dirlist
= dirlist
-> next
;
1518 * If we are returning file entries, we
1519 * have a duplicate list in dirplus.
1521 * Update dirplus_next also so readdir and
1522 * readdirplus are kept in sync.
1524 if (dir
->dirplus_list
!= NULL
) {
1525 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1539 * Routine to create a directory ...
1543 SMBC_mkdir_ctx(SMBCCTX
*context
,
1547 SMBCSRV
*srv
= NULL
;
1548 char *server
= NULL
;
1551 char *password
= NULL
;
1552 char *workgroup
= NULL
;
1554 char *targetpath
= NULL
;
1556 struct cli_state
*targetcli
= NULL
;
1557 struct cli_credentials
*creds
= NULL
;
1558 TALLOC_CTX
*frame
= talloc_stackframe();
1561 if (!context
|| !context
->internal
->initialized
) {
1573 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1575 if (SMBC_parse_path(frame
,
1591 if (!user
|| user
[0] == (char)0) {
1592 user
= talloc_strdup(frame
, smbc_getUser(context
));
1600 srv
= SMBC_server(frame
, context
, True
,
1601 server
, port
, share
, &workgroup
, &user
, &password
);
1606 return -1; /* errno set by SMBC_server */
1610 creds
= context
->internal
->creds
;
1612 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1613 status
= cli_resolve_path(frame
, "",
1615 srv
->cli
, path
, &targetcli
, &targetpath
);
1616 if (!NT_STATUS_IS_OK(status
)) {
1617 d_printf("Could not resolve %s\n", path
);
1622 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1624 status
= cli_mkdir(targetcli
, targetpath
);
1625 if (!NT_STATUS_IS_OK(status
)) {
1627 errno
= cli_status_to_errno(status
);
1638 * Our list function simply checks to see if a directory is not empty
1642 rmdir_list_fn(struct file_info
*finfo
,
1646 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1647 strncmp(finfo
->name
, "..", 2) != 0) {
1648 bool *smbc_rmdir_dirempty
= (bool *)state
;
1649 *smbc_rmdir_dirempty
= false;
1651 return NT_STATUS_OK
;
1655 * Routine to remove a directory
1659 SMBC_rmdir_ctx(SMBCCTX
*context
,
1662 SMBCSRV
*srv
= NULL
;
1663 char *server
= NULL
;
1666 char *password
= NULL
;
1667 char *workgroup
= NULL
;
1669 char *targetpath
= NULL
;
1671 struct cli_state
*targetcli
= NULL
;
1672 struct cli_credentials
*creds
= NULL
;
1673 TALLOC_CTX
*frame
= talloc_stackframe();
1676 if (!context
|| !context
->internal
->initialized
) {
1688 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1690 if (SMBC_parse_path(frame
,
1706 if (!user
|| user
[0] == (char)0) {
1707 user
= talloc_strdup(frame
, smbc_getUser(context
));
1715 srv
= SMBC_server(frame
, context
, True
,
1716 server
, port
, share
, &workgroup
, &user
, &password
);
1721 return -1; /* errno set by SMBC_server */
1725 creds
= context
->internal
->creds
;
1727 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1728 status
= cli_resolve_path(frame
, "",
1730 srv
->cli
, path
, &targetcli
, &targetpath
);
1731 if (!NT_STATUS_IS_OK(status
)) {
1732 d_printf("Could not resolve %s\n", path
);
1737 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1739 status
= cli_rmdir(targetcli
, targetpath
);
1741 if (!NT_STATUS_IS_OK(status
)) {
1743 errno
= cli_status_to_errno(status
);
1745 if (errno
== EACCES
) { /* Check if the dir empty or not */
1747 /* Local storage to avoid buffer overflows */
1749 bool smbc_rmdir_dirempty
= true;
1751 lpath
= talloc_asprintf(frame
, "%s\\*",
1759 status
= cli_list(targetcli
, lpath
,
1760 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1762 &smbc_rmdir_dirempty
);
1764 if (!NT_STATUS_IS_OK(status
)) {
1765 /* Fix errno to ignore latest error ... */
1766 DBG_INFO("cli_list returned an error: %s\n",
1772 if (smbc_rmdir_dirempty
)
1790 * Routine to return the current directory position
1794 SMBC_telldir_ctx(SMBCCTX
*context
,
1797 TALLOC_CTX
*frame
= talloc_stackframe();
1799 if (!context
|| !context
->internal
->initialized
) {
1807 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1815 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1823 /* See if we're already at the end. */
1824 if (dir
->dir_next
== NULL
) {
1831 * We return the pointer here as the offset
1834 return (off_t
)(long)dir
->dir_next
->dirent
;
1838 * A routine to run down the list and see if the entry is OK
1839 * Modifies the dir list and the dirplus list (if it exists)
1840 * to point at the correct next entry on success.
1843 static bool update_dir_ents(SMBCFILE
*dir
, struct smbc_dirent
*dirent
)
1845 struct smbc_dir_list
*tmp_dir
= dir
->dir_list
;
1846 struct smbc_dirplus_list
*tmp_dirplus
= dir
->dirplus_list
;
1849 * Run down the list looking for what we want.
1850 * If we're enumerating files both dir_list
1851 * and dirplus_list contain the same entry
1852 * list, as they were seeded from the same
1853 * cli_list callback.
1855 * If we're enumerating servers then
1856 * dirplus_list will be NULL, so don't
1857 * update in that case.
1860 while (tmp_dir
!= NULL
) {
1861 if (tmp_dir
->dirent
== dirent
) {
1862 dir
->dir_next
= tmp_dir
;
1863 if (tmp_dirplus
!= NULL
) {
1864 dir
->dirplus_next
= tmp_dirplus
;
1868 tmp_dir
= tmp_dir
->next
;
1869 if (tmp_dirplus
!= NULL
) {
1870 tmp_dirplus
= tmp_dirplus
->next
;
1877 * Routine to seek on a directory
1881 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1885 long int l_offset
= offset
; /* Handle problems of size */
1886 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1887 TALLOC_CTX
*frame
= talloc_stackframe();
1890 if (!context
|| !context
->internal
->initialized
) {
1898 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1906 /* Now, check what we were passed and see if it is OK ... */
1908 if (dirent
== NULL
) { /* Seek to the beginning of the list */
1910 dir
->dir_next
= dir
->dir_list
;
1912 /* Do the same for dirplus. */
1913 dir
->dirplus_next
= dir
->dirplus_list
;
1920 if (offset
== -1) { /* Seek to the end of the list */
1921 dir
->dir_next
= NULL
;
1923 /* Do the same for dirplus. */
1924 dir
->dirplus_next
= NULL
;
1931 * Run down the list and make sure that the entry is OK.
1932 * Update the position of both dir and dirplus lists.
1935 ok
= update_dir_ents(dir
, dirent
);
1937 errno
= EINVAL
; /* Bad entry */
1947 * Routine to fstat a dir
1951 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1956 if (!context
|| !context
->internal
->initialized
) {
1962 /* No code yet ... */
1967 SMBC_chmod_ctx(SMBCCTX
*context
,
1971 SMBCSRV
*srv
= NULL
;
1972 char *server
= NULL
;
1975 char *password
= NULL
;
1976 char *workgroup
= NULL
;
1977 char *targetpath
= NULL
;
1978 struct cli_state
*targetcli
= NULL
;
1982 struct cli_credentials
*creds
= NULL
;
1983 TALLOC_CTX
*frame
= talloc_stackframe();
1986 if (!context
|| !context
->internal
->initialized
) {
1988 errno
= EINVAL
; /* Best I can think of ... */
1999 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
2001 if (SMBC_parse_path(frame
,
2017 if (!user
|| user
[0] == (char)0) {
2018 user
= talloc_strdup(frame
, smbc_getUser(context
));
2026 srv
= SMBC_server(frame
, context
, True
,
2027 server
, port
, share
, &workgroup
, &user
, &password
);
2031 return -1; /* errno set by SMBC_server */
2034 creds
= context
->internal
->creds
;
2036 /*d_printf(">>>unlink: resolving %s\n", path);*/
2037 status
= cli_resolve_path(frame
, "",
2039 srv
->cli
, path
, &targetcli
, &targetpath
);
2040 if (!NT_STATUS_IS_OK(status
)) {
2041 d_printf("Could not resolve %s\n", path
);
2049 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) attr
|= FILE_ATTRIBUTE_READONLY
;
2050 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) attr
|= FILE_ATTRIBUTE_ARCHIVE
;
2051 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) attr
|= FILE_ATTRIBUTE_SYSTEM
;
2052 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) attr
|= FILE_ATTRIBUTE_HIDDEN
;
2054 status
= cli_setatr(targetcli
, targetpath
, attr
, 0);
2055 if (!NT_STATUS_IS_OK(status
)) {
2057 errno
= cli_status_to_errno(status
);
2066 SMBC_utimes_ctx(SMBCCTX
*context
,
2068 struct timeval
*tbuf
)
2070 SMBCSRV
*srv
= NULL
;
2071 char *server
= NULL
;
2074 char *password
= NULL
;
2075 char *workgroup
= NULL
;
2077 struct timespec access_time
, write_time
;
2079 TALLOC_CTX
*frame
= talloc_stackframe();
2082 if (!context
|| !context
->internal
->initialized
) {
2084 errno
= EINVAL
; /* Best I can think of ... */
2096 access_time
= write_time
= timespec_current();
2098 access_time
= convert_timeval_to_timespec(tbuf
[0]);
2099 write_time
= convert_timeval_to_timespec(tbuf
[1]);
2103 struct timeval_buf abuf
, wbuf
;
2105 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2107 timespec_string_buf(&access_time
, false, &abuf
),
2108 timespec_string_buf(&write_time
, false, &wbuf
));
2111 if (SMBC_parse_path(frame
,
2127 if (!user
|| user
[0] == (char)0) {
2128 user
= talloc_strdup(frame
, smbc_getUser(context
));
2136 srv
= SMBC_server(frame
, context
, True
,
2137 server
, port
, share
, &workgroup
, &user
, &password
);
2141 return -1; /* errno set by SMBC_server */
2148 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2151 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2155 return -1; /* errno set by SMBC_setatr */
2163 * Routine to unlink() a file
2167 SMBC_unlink_ctx(SMBCCTX
*context
,
2170 char *server
= NULL
;
2173 char *password
= NULL
;
2174 char *workgroup
= NULL
;
2176 char *targetpath
= NULL
;
2178 struct cli_state
*targetcli
= NULL
;
2179 SMBCSRV
*srv
= NULL
;
2180 struct cli_credentials
*creds
= NULL
;
2181 TALLOC_CTX
*frame
= talloc_stackframe();
2184 if (!context
|| !context
->internal
->initialized
) {
2186 errno
= EINVAL
; /* Best I can think of ... */
2199 if (SMBC_parse_path(frame
,
2215 if (!user
|| user
[0] == (char)0) {
2216 user
= talloc_strdup(frame
, smbc_getUser(context
));
2224 srv
= SMBC_server(frame
, context
, True
,
2225 server
, port
, share
, &workgroup
, &user
, &password
);
2229 return -1; /* SMBC_server sets errno */
2233 creds
= context
->internal
->creds
;
2235 /*d_printf(">>>unlink: resolving %s\n", path);*/
2236 status
= cli_resolve_path(frame
, "",
2238 srv
->cli
, path
, &targetcli
, &targetpath
);
2239 if (!NT_STATUS_IS_OK(status
)) {
2240 d_printf("Could not resolve %s\n", path
);
2245 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2247 status
= cli_unlink(
2250 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2252 if (!NT_STATUS_IS_OK(status
)) {
2254 errno
= cli_status_to_errno(status
);
2256 if (errno
== EACCES
) { /* Check if the file is a directory */
2259 struct stat sb
= {0};
2261 status
= SMBC_getatr(context
, srv
, path
, &sb
);
2262 if (!NT_STATUS_IS_OK(status
)) {
2263 /* Hmmm, bad error ... What? */
2266 errno
= cli_status_to_errno(status
);
2272 if (S_ISDIR(sb
.st_mode
))
2275 errno
= saverr
; /* Restore this */
2286 return 0; /* Success ... */
2291 * Routine to rename() a file
2295 SMBC_rename_ctx(SMBCCTX
*ocontext
,
2300 char *server1
= NULL
;
2301 char *share1
= NULL
;
2302 char *server2
= NULL
;
2303 char *share2
= NULL
;
2306 char *password1
= NULL
;
2307 char *password2
= NULL
;
2308 char *workgroup
= NULL
;
2311 char *targetpath1
= NULL
;
2312 char *targetpath2
= NULL
;
2313 struct cli_state
*targetcli1
= NULL
;
2314 struct cli_state
*targetcli2
= NULL
;
2315 SMBCSRV
*srv
= NULL
;
2318 struct cli_credentials
*ocreds
= NULL
;
2319 struct cli_credentials
*ncreds
= NULL
;
2320 TALLOC_CTX
*frame
= talloc_stackframe();
2323 if (!ocontext
|| !ncontext
||
2324 !ocontext
->internal
->initialized
||
2325 !ncontext
->internal
->initialized
) {
2327 errno
= EINVAL
; /* Best I can think of ... */
2332 if (!oname
|| !nname
) {
2338 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
2340 if (SMBC_parse_path(frame
,
2356 if (!user1
|| user1
[0] == (char)0) {
2357 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
2365 if (SMBC_parse_path(frame
,
2381 if (!user2
|| user2
[0] == (char)0) {
2382 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
2390 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2391 strcmp(user1
, user2
)) {
2392 /* Can't rename across file systems, or users?? */
2398 srv
= SMBC_server(frame
, ocontext
, True
,
2399 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2406 /* set the credentials to make DFS work */
2407 smbc_set_credentials_with_fallback(ocontext
,
2412 /*d_printf(">>>rename: resolving %s\n", path1);*/
2413 ocreds
= ocontext
->internal
->creds
;
2415 status
= cli_resolve_path(frame
, "",
2417 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2418 if (!NT_STATUS_IS_OK(status
)) {
2419 d_printf("Could not resolve %s\n", path1
);
2425 /* set the credentials to make DFS work */
2426 smbc_set_credentials_with_fallback(ncontext
,
2431 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2432 /*d_printf(">>>rename: resolving %s\n", path2);*/
2433 ncreds
= ncontext
->internal
->creds
;
2435 status
= cli_resolve_path(frame
, "",
2437 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2438 if (!NT_STATUS_IS_OK(status
)) {
2439 d_printf("Could not resolve %s\n", path2
);
2444 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2446 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2447 strcmp(targetcli1
->share
, targetcli2
->share
))
2449 /* can't rename across file systems */
2455 status
= cli_rename(targetcli1
, targetpath1
, targetpath2
, false);
2456 if (!NT_STATUS_IS_OK(status
)) {
2457 int eno
= cli_status_to_errno(status
);
2459 if (eno
!= EEXIST
||
2460 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2461 FILE_ATTRIBUTE_SYSTEM
|
2462 FILE_ATTRIBUTE_HIDDEN
)) ||
2463 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2464 targetpath2
, false))) {
2474 return 0; /* Success */
2477 struct smbc_notify_cb_state
{
2478 struct tevent_context
*ev
;
2479 struct cli_state
*cli
;
2482 uint32_t completion_filter
;
2483 unsigned callback_timeout_ms
;
2484 smbc_notify_callback_fn cb
;
2488 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2489 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2491 static struct tevent_req
*smbc_notify_cb_send(
2492 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2493 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2494 unsigned callback_timeout_ms
,
2495 smbc_notify_callback_fn cb
, void *private_data
)
2497 struct tevent_req
*req
, *subreq
;
2498 struct smbc_notify_cb_state
*state
;
2500 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2507 state
->recursive
= recursive
;
2508 state
->completion_filter
= completion_filter
;
2509 state
->callback_timeout_ms
= callback_timeout_ms
;
2511 state
->private_data
= private_data
;
2513 subreq
= cli_notify_send(
2514 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2515 state
->completion_filter
, state
->recursive
);
2516 if (tevent_req_nomem(subreq
, req
)) {
2517 return tevent_req_post(req
, ev
);
2519 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2521 if (state
->callback_timeout_ms
== 0) {
2525 subreq
= tevent_wakeup_send(
2527 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2528 state
->callback_timeout_ms
*1000));
2529 if (tevent_req_nomem(subreq
, req
)) {
2530 return tevent_req_post(req
, ev
);
2532 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2537 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2539 struct tevent_req
*req
= tevent_req_callback_data(
2540 subreq
, struct tevent_req
);
2541 struct smbc_notify_cb_state
*state
= tevent_req_data(
2542 req
, struct smbc_notify_cb_state
);
2543 uint32_t num_changes
;
2544 struct notify_change
*changes
;
2548 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2549 TALLOC_FREE(subreq
);
2550 if (tevent_req_nterror(req
, status
)) {
2555 struct smbc_notify_callback_action actions
[num_changes
];
2558 for (i
=0; i
<num_changes
; i
++) {
2559 actions
[i
].action
= changes
[i
].action
;
2560 actions
[i
].filename
= changes
[i
].name
;
2563 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2566 TALLOC_FREE(changes
);
2569 tevent_req_done(req
);
2573 subreq
= cli_notify_send(
2574 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2575 state
->completion_filter
, state
->recursive
);
2576 if (tevent_req_nomem(subreq
, req
)) {
2579 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2582 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2584 struct tevent_req
*req
= tevent_req_callback_data(
2585 subreq
, struct tevent_req
);
2586 struct smbc_notify_cb_state
*state
= tevent_req_data(
2587 req
, struct smbc_notify_cb_state
);
2591 ok
= tevent_wakeup_recv(subreq
);
2592 TALLOC_FREE(subreq
);
2594 tevent_req_oom(req
);
2598 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2600 tevent_req_done(req
);
2604 subreq
= tevent_wakeup_send(
2606 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2607 state
->callback_timeout_ms
*1000));
2608 if (tevent_req_nomem(subreq
, req
)) {
2611 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2614 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2616 return tevent_req_simple_recv_ntstatus(req
);
2619 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2620 bool recursive
, uint32_t completion_filter
,
2621 unsigned callback_timeout_ms
,
2622 smbc_notify_callback_fn cb
, void *private_data
)
2624 TALLOC_CTX
*frame
= talloc_stackframe();
2625 struct tevent_context
*ev
;
2626 struct tevent_req
*req
;
2627 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2629 ev
= samba_tevent_context_init(frame
);
2633 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2635 callback_timeout_ms
, cb
, private_data
);
2639 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2642 status
= smbc_notify_cb_recv(req
);
2650 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2651 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2652 smbc_notify_callback_fn cb
, void *private_data
)
2654 TALLOC_CTX
*frame
= talloc_stackframe();
2655 struct cli_state
*cli
;
2656 char *server
= NULL
;
2659 char *password
= NULL
;
2660 char *options
= NULL
;
2661 char *workgroup
= NULL
;
2667 if ((context
== NULL
) || !context
->internal
->initialized
) {
2672 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2678 if (SMBC_parse_path(frame
,
2689 DEBUG(4, ("no valid path\n"));
2691 errno
= EINVAL
+ 8194;
2695 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2696 "path='%s' options='%s'\n",
2697 dir
->fname
, server
, share
, path
, options
));
2699 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2700 (int)recursive
, completion_filter
));
2702 cli
= dir
->srv
->cli
;
2703 status
= cli_ntcreate(
2704 cli
, path
, 0, FILE_READ_DATA
, 0,
2705 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2706 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2707 if (!NT_STATUS_IS_OK(status
)) {
2709 errno
= cli_status_to_errno(status
);
2713 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2714 callback_timeout_ms
, cb
, private_data
);
2715 if (!NT_STATUS_IS_OK(status
)) {
2716 cli_close(cli
, fnum
);
2718 errno
= cli_status_to_errno(status
);
2722 cli_close(cli
, fnum
);