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 "auth_info.h"
29 #include "libsmbclient.h"
30 #include "libsmb_internal.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
33 #include "libsmb/nmblib.h"
34 #include "../libcli/smb/smbXcli_base.h"
35 #include "../libcli/security/security.h"
36 #include "lib/util/tevent_ntstatus.h"
37 #include "lib/util/time_basic.h"
38 #include "lib/util/string_wrappers.h"
41 * Routine to open a directory
42 * We accept the URL syntax explained in SMBC_parse_path(), above.
45 static void remove_dirplus(SMBCFILE
*dir
)
47 struct smbc_dirplus_list
*d
= NULL
;
49 d
= dir
->dirplus_list
;
51 struct smbc_dirplus_list
*f
= d
;
54 SAFE_FREE(f
->smb_finfo
->short_name
);
55 SAFE_FREE(f
->smb_finfo
->name
);
56 SAFE_FREE(f
->smb_finfo
);
60 dir
->dirplus_list
= NULL
;
61 dir
->dirplus_end
= NULL
;
62 dir
->dirplus_next
= NULL
;
66 remove_dir(SMBCFILE
*dir
)
68 struct smbc_dir_list
*d
,*f
;
80 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
85 add_dirent(SMBCFILE
*dir
,
90 struct smbc_dirent
*dirent
;
92 int name_length
= (name
== NULL
? 0 : strlen(name
));
93 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
96 * Allocate space for the dirent, which must be increased by the
97 * size of the name and the comment and 1 each for the null terminator.
100 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
102 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
106 dir
->dir_error
= ENOMEM
;
111 ZERO_STRUCTP(dirent
);
113 if (dir
->dir_list
== NULL
) {
115 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
116 if (!dir
->dir_list
) {
119 dir
->dir_error
= ENOMEM
;
123 ZERO_STRUCTP(dir
->dir_list
);
125 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
129 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
131 if (!dir
->dir_end
->next
) {
134 dir
->dir_error
= ENOMEM
;
138 ZERO_STRUCTP(dir
->dir_end
->next
);
140 dir
->dir_end
= dir
->dir_end
->next
;
143 dir
->dir_end
->next
= NULL
;
144 dir
->dir_end
->dirent
= dirent
;
146 dirent
->smbc_type
= type
;
147 dirent
->namelen
= name_length
;
148 dirent
->commentlen
= comment_len
;
149 dirent
->dirlen
= size
;
152 * dirent->namelen + 1 includes the null (no null termination needed)
153 * Ditto for dirent->commentlen.
154 * The space for the two null bytes was allocated.
156 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
157 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
158 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
164 static int add_dirplus(SMBCFILE
*dir
, struct file_info
*finfo
)
166 struct smbc_dirplus_list
*new_entry
= NULL
;
167 struct libsmb_file_info
*info
= NULL
;
169 new_entry
= SMB_MALLOC_P(struct smbc_dirplus_list
);
170 if (new_entry
== NULL
) {
171 dir
->dir_error
= ENOMEM
;
174 ZERO_STRUCTP(new_entry
);
175 new_entry
->ino
= finfo
->ino
;
177 info
= SMB_MALLOC_P(struct libsmb_file_info
);
179 SAFE_FREE(new_entry
);
180 dir
->dir_error
= ENOMEM
;
186 info
->btime_ts
= finfo
->btime_ts
;
187 info
->atime_ts
= finfo
->atime_ts
;
188 info
->ctime_ts
= finfo
->ctime_ts
;
189 info
->mtime_ts
= finfo
->mtime_ts
;
190 info
->gid
= finfo
->gid
;
191 info
->attrs
= finfo
->attr
;
192 info
->size
= finfo
->size
;
193 info
->uid
= finfo
->uid
;
194 info
->name
= SMB_STRDUP(finfo
->name
);
195 if (info
->name
== NULL
) {
197 SAFE_FREE(new_entry
);
198 dir
->dir_error
= ENOMEM
;
202 if (finfo
->short_name
) {
203 info
->short_name
= SMB_STRDUP(finfo
->short_name
);
205 info
->short_name
= SMB_STRDUP("");
208 if (info
->short_name
== NULL
) {
209 SAFE_FREE(info
->name
);
211 SAFE_FREE(new_entry
);
212 dir
->dir_error
= ENOMEM
;
215 new_entry
->smb_finfo
= info
;
217 /* Now add to the list. */
218 if (dir
->dirplus_list
== NULL
) {
219 /* Empty list - point everything at new_entry. */
220 dir
->dirplus_list
= new_entry
;
221 dir
->dirplus_end
= new_entry
;
222 dir
->dirplus_next
= new_entry
;
224 /* Append to list but leave the ->next cursor alone. */
225 dir
->dirplus_end
->next
= new_entry
;
226 dir
->dirplus_end
= new_entry
;
233 list_unique_wg_fn(const char *name
,
238 SMBCFILE
*dir
= (SMBCFILE
*)state
;
239 struct smbc_dir_list
*dir_list
;
240 struct smbc_dirent
*dirent
;
244 dirent_type
= dir
->dir_type
;
246 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
247 /* An error occurred, what do we do? */
248 /* FIXME: Add some code here */
249 /* Change cli_NetServerEnum to take a fn
250 returning NTSTATUS... JRA. */
253 /* Point to the one just added */
254 dirent
= dir
->dir_end
->dirent
;
256 /* See if this was a duplicate */
257 for (dir_list
= dir
->dir_list
;
258 dir_list
!= dir
->dir_end
;
259 dir_list
= dir_list
->next
) {
261 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
262 /* Duplicate. End end of list need to be removed. */
266 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
267 /* Found the end of the list. Remove it. */
268 dir
->dir_end
= dir_list
;
269 free(dir_list
->next
);
271 dir_list
->next
= NULL
;
278 list_fn(const char *name
,
283 SMBCFILE
*dir
= (SMBCFILE
*)state
;
287 * We need to process the type a little ...
289 * Disk share = 0x00000000
290 * Print share = 0x00000001
291 * Comms share = 0x00000002 (obsolete?)
292 * IPC$ share = 0x00000003
294 * administrative shares:
295 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
298 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
302 dirent_type
= SMBC_FILE_SHARE
;
306 dirent_type
= SMBC_PRINTER_SHARE
;
310 dirent_type
= SMBC_COMMS_SHARE
;
315 dirent_type
= SMBC_IPC_SHARE
;
319 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
324 dirent_type
= dir
->dir_type
;
327 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
328 /* An error occurred, what do we do? */
329 /* FIXME: Add some code here */
330 /* Change cli_NetServerEnum to take a fn
331 returning NTSTATUS... JRA. */
336 dir_list_fn(struct file_info
*finfo
,
340 SMBCFILE
*dirp
= (SMBCFILE
*)state
;
343 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
344 (finfo
->attr
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
345 SMBCFILE
*dir
= (SMBCFILE
*)state
;
346 return map_nt_error_from_unix(dir
->dir_error
);
348 ret
= add_dirplus(dirp
, finfo
);
350 return map_nt_error_from_unix(dirp
->dir_error
);
356 net_share_enum_rpc(struct cli_state
*cli
,
357 void (*fn
)(const char *name
,
365 uint32_t preferred_len
= 0xffffffff;
367 struct srvsvc_NetShareInfoCtr info_ctr
;
368 struct srvsvc_NetShareCtr1 ctr1
;
370 fstring comment
= "";
371 struct rpc_pipe_client
*pipe_hnd
= NULL
;
373 uint32_t resume_handle
= 0;
374 uint32_t total_entries
= 0;
375 struct dcerpc_binding_handle
*b
;
377 /* Open the server service pipe */
378 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
380 if (!NT_STATUS_IS_OK(nt_status
)) {
381 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
385 ZERO_STRUCT(info_ctr
);
389 info_ctr
.ctr
.ctr1
= &ctr1
;
391 b
= pipe_hnd
->binding_handle
;
393 /* Issue the NetShareEnum RPC call and retrieve the response */
394 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
402 /* Was it successful? */
403 if (!NT_STATUS_IS_OK(nt_status
)) {
404 /* Nope. Go clean up. */
408 if (!W_ERROR_IS_OK(result
)) {
409 /* Nope. Go clean up. */
410 nt_status
= werror_to_ntstatus(result
);
414 if (total_entries
== 0) {
415 /* Nope. Go clean up. */
416 nt_status
= NT_STATUS_NOT_FOUND
;
420 /* For each returned entry... */
421 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
423 /* pull out the share name */
424 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
426 /* pull out the share's comment */
427 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
429 /* Get the type value */
430 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
432 /* Add this share to the list */
433 (*fn
)(name
, type
, comment
, state
);
437 /* Close the server service pipe */
438 TALLOC_FREE(pipe_hnd
);
440 /* Tell 'em if it worked */
446 * Verify that the options specified in a URL are valid
449 SMBC_check_options(char *server
,
454 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
455 "path='%s' options='%s'\n",
456 server
, share
, path
, options
));
458 /* No options at all is always ok */
459 if (! *options
) return 0;
461 /* Currently, we don't support any options. */
467 SMBC_opendir_ctx(SMBCCTX
*context
,
473 char *password
= NULL
;
474 char *options
= NULL
;
475 char *workgroup
= NULL
;
480 SMBCFILE
*dir
= NULL
;
481 struct sockaddr_storage rem_ss
;
482 TALLOC_CTX
*frame
= talloc_stackframe();
484 if (!context
|| !context
->internal
->initialized
) {
485 DEBUG(4, ("no valid context\n"));
487 errno
= EINVAL
+ 8192;
493 DEBUG(4, ("no valid fname\n"));
495 errno
= EINVAL
+ 8193;
499 if (SMBC_parse_path(frame
,
510 DEBUG(4, ("no valid path\n"));
512 errno
= EINVAL
+ 8194;
516 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
517 "path='%s' options='%s'\n",
518 fname
, server
, share
, path
, options
));
520 /* Ensure the options are valid */
521 if (SMBC_check_options(server
, share
, path
, options
)) {
522 DEBUG(4, ("unacceptable options (%s)\n", options
));
524 errno
= EINVAL
+ 8195;
528 if (!user
|| user
[0] == (char)0) {
529 user
= talloc_strdup(frame
, smbc_getUser(context
));
537 dir
= SMB_MALLOC_P(SMBCFILE
);
548 dir
->fname
= SMB_STRDUP(fname
);
549 if (dir
->fname
== NULL
) {
558 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
560 if (server
[0] == (char)0) {
564 size_t max_lmb_count
;
565 struct sockaddr_storage
*ip_list
;
566 struct sockaddr_storage server_addr
;
567 struct user_auth_info
*u_info
;
570 if (share
[0] != (char)0 || path
[0] != (char)0) {
573 SAFE_FREE(dir
->fname
);
577 errno
= EINVAL
+ 8196;
581 /* Determine how many local master browsers to query */
582 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
584 : smbc_getOptionBrowseMaxLmbCount(context
));
586 u_info
= user_auth_info_init(frame
);
587 if (u_info
== NULL
) {
589 SAFE_FREE(dir
->fname
);
596 set_cmdline_auth_info_username(u_info
, user
);
597 set_cmdline_auth_info_password(u_info
, password
);
600 * We have server and share and path empty but options
601 * requesting that we scan all master browsers for their list
602 * of workgroups/domains. This implies that we must first try
603 * broadcast queries to find all master browsers, and if that
604 * doesn't work, then try our other methods which return only
605 * a single master browser.
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
= get_cmdline_auth_info_creds(
951 context
->internal
->auth_info
);
953 status
= cli_resolve_path(
956 srv
->cli
, path
, &targetcli
, &targetpath
);
957 if (!NT_STATUS_IS_OK(status
)) {
958 d_printf("Could not resolve %s\n", path
);
960 SAFE_FREE(dir
->fname
);
967 status
= cli_list(targetcli
, targetpath
,
968 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
969 dir_list_fn
, (void *)dir
);
970 if (!NT_STATUS_IS_OK(status
)) {
973 SAFE_FREE(dir
->fname
);
976 saved_errno
= SMBC_errno(context
, targetcli
);
978 if (saved_errno
== EINVAL
) {
979 struct stat sb
= {0};
981 * See if they asked to opendir
982 * something other than a directory.
983 * If so, the converted error value we
984 * got would have been EINVAL rather
987 path
[path_len
] = '\0'; /* restore original path */
989 if (SMBC_getatr(context
,
993 !S_ISDIR(sb
.st_mode
)) {
995 /* It is. Correct the error value */
996 saved_errno
= ENOTDIR
;
1001 * If there was an error and the server is no
1004 if (cli_is_error(targetcli
) &&
1005 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
1007 /* ... then remove it. */
1008 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
1011 * We could not remove the
1012 * server completely, remove
1013 * it from the cache so we
1014 * will not get it again. It
1015 * will be removed when the
1016 * last file/dir is closed.
1018 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
1023 errno
= saved_errno
;
1030 DLIST_ADD(context
->internal
->files
, dir
);
1037 * Routine to close a directory
1041 SMBC_closedir_ctx(SMBCCTX
*context
,
1044 TALLOC_CTX
*frame
= NULL
;
1046 if (!context
|| !context
->internal
->initialized
) {
1055 frame
= talloc_stackframe();
1057 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1063 remove_dir(dir
); /* Clean it up */
1064 remove_dirplus(dir
);
1066 DLIST_REMOVE(context
->internal
->files
, dir
);
1068 SAFE_FREE(dir
->fname
);
1069 SAFE_FREE(dir
); /* Free the space too */
1077 smbc_readdir_internal(SMBCCTX
* context
,
1078 struct smbc_dirent
*dest
,
1079 struct smbc_dirent
*src
,
1080 int max_namebuf_len
)
1082 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
1085 /* url-encode the name. get back remaining buffer space */
1087 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
1089 /* -1 means no null termination. */
1090 if (remaining_len
< 0) {
1094 /* We now know the name length */
1095 dest
->namelen
= strlen(dest
->name
);
1097 if (dest
->namelen
+ 1 < 1) {
1102 if (dest
->namelen
+ 1 >= max_namebuf_len
) {
1103 /* Out of space for comment. */
1107 /* Save the pointer to the beginning of the comment */
1108 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
1110 if (remaining_len
< 1) {
1111 /* No room for comment null termination. */
1115 /* Copy the comment */
1116 strlcpy(dest
->comment
, src
->comment
, remaining_len
);
1118 /* Save other fields */
1119 dest
->smbc_type
= src
->smbc_type
;
1120 dest
->commentlen
= strlen(dest
->comment
);
1121 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
1125 /* No encoding. Just copy the entry as is. */
1126 if (src
->dirlen
> max_namebuf_len
) {
1129 memcpy(dest
, src
, src
->dirlen
);
1130 if (src
->namelen
+ 1 < 1) {
1134 if (src
->namelen
+ 1 >= max_namebuf_len
) {
1135 /* Comment off the end. */
1138 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
1144 * Routine to get a directory entry
1147 struct smbc_dirent
*
1148 SMBC_readdir_ctx(SMBCCTX
*context
,
1153 struct smbc_dirent
*dirp
, *dirent
;
1154 TALLOC_CTX
*frame
= talloc_stackframe();
1156 /* Check that all is ok first ... */
1158 if (!context
|| !context
->internal
->initialized
) {
1161 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1167 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1170 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1176 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1179 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1185 if (!dir
->dir_next
) {
1190 dirent
= dir
->dir_next
->dirent
;
1199 dirp
= &context
->internal
->dirent
;
1200 maxlen
= sizeof(context
->internal
->_dirent_name
);
1202 ret
= smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1209 dir
->dir_next
= dir
->dir_next
->next
;
1212 * If we are returning file entries, we
1213 * have a duplicate list in dirplus.
1215 * Update dirplus_next also so readdir and
1216 * readdirplus are kept in sync.
1218 if (dir
->dirplus_list
!= NULL
) {
1219 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1227 * Routine to get a directory entry with all attributes
1230 const struct libsmb_file_info
*
1231 SMBC_readdirplus_ctx(SMBCCTX
*context
,
1234 struct libsmb_file_info
*smb_finfo
= NULL
;
1235 TALLOC_CTX
*frame
= talloc_stackframe();
1237 /* Check that all is ok first ... */
1239 if (context
== NULL
|| !context
->internal
->initialized
) {
1240 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1246 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1247 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1253 if (dir
->dirplus_next
== NULL
) {
1258 smb_finfo
= dir
->dirplus_next
->smb_finfo
;
1259 if (smb_finfo
== NULL
) {
1264 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1267 * If we are returning file entries, we
1268 * have a duplicate list in dir_list
1270 * Update dir_next also so readdir and
1271 * readdirplus are kept in sync.
1273 if (dir
->dir_list
) {
1274 dir
->dir_next
= dir
->dir_next
->next
;
1282 * Routine to get a directory entry plus a filled in stat structure if
1286 const struct libsmb_file_info
*SMBC_readdirplus2_ctx(SMBCCTX
*context
,
1290 struct libsmb_file_info
*smb_finfo
= NULL
;
1291 struct smbc_dirplus_list
*dp_list
= NULL
;
1293 char *full_pathname
= NULL
;
1294 char *workgroup
= NULL
;
1295 char *server
= NULL
;
1300 char *password
= NULL
;
1301 char *options
= NULL
;
1303 TALLOC_CTX
*frame
= NULL
;
1306 * Allow caller to pass in NULL for stat pointer if
1307 * required. This makes this call identical to
1308 * smbc_readdirplus().
1312 return SMBC_readdirplus_ctx(context
, dir
);
1315 frame
= talloc_stackframe();
1317 /* Check that all is ok first ... */
1318 if (context
== NULL
|| !context
->internal
->initialized
) {
1319 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1325 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1326 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1332 dp_list
= dir
->dirplus_next
;
1333 if (dp_list
== NULL
) {
1338 ino
= (ino_t
)dp_list
->ino
;
1340 smb_finfo
= dp_list
->smb_finfo
;
1341 if (smb_finfo
== NULL
) {
1347 full_pathname
= talloc_asprintf(frame
,
1351 if (full_pathname
== NULL
) {
1357 rc
= SMBC_parse_path(frame
,
1380 smb_finfo
->atime_ts
,
1381 smb_finfo
->ctime_ts
,
1382 smb_finfo
->mtime_ts
);
1384 TALLOC_FREE(full_pathname
);
1386 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1389 * If we are returning file entries, we
1390 * have a duplicate list in dir_list
1392 * Update dir_next also so readdir and
1393 * readdirplus are kept in sync.
1395 if (dir
->dir_list
) {
1396 dir
->dir_next
= dir
->dir_next
->next
;
1404 * Routine to get directory entries
1408 SMBC_getdents_ctx(SMBCCTX
*context
,
1410 struct smbc_dirent
*dirp
,
1416 char *ndir
= (char *)dirp
;
1417 struct smbc_dir_list
*dirlist
;
1418 TALLOC_CTX
*frame
= talloc_stackframe();
1420 /* Check that all is ok first ... */
1422 if (!context
|| !context
->internal
->initialized
) {
1430 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1438 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1447 * Now, retrieve the number of entries that will fit in what was passed
1448 * We have to figure out if the info is in the list, or we need to
1449 * send a request to the server to get the info.
1452 while ((dirlist
= dir
->dir_next
)) {
1454 struct smbc_dirent
*dirent
;
1455 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1457 if (!dirlist
->dirent
) {
1459 errno
= ENOENT
; /* Bad error */
1465 /* Do urlencoding of next entry, if so selected */
1466 dirent
= &context
->internal
->dirent
;
1467 maxlen
= sizeof(context
->internal
->_dirent_name
);
1468 ret
= smbc_readdir_internal(context
, dirent
,
1469 dirlist
->dirent
, maxlen
);
1476 reqd
= dirent
->dirlen
;
1480 if (rem
< count
) { /* We managed to copy something */
1487 else { /* Nothing copied ... */
1489 errno
= EINVAL
; /* Not enough space ... */
1497 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1499 currentEntry
->comment
= ¤tEntry
->name
[0] +
1500 dirent
->namelen
+ 1;
1505 /* Try and align the struct for the next entry
1506 on a valid pointer boundary by appending zeros */
1507 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1511 currentEntry
->dirlen
++;
1514 dir
->dir_next
= dirlist
= dirlist
-> next
;
1517 * If we are returning file entries, we
1518 * have a duplicate list in dirplus.
1520 * Update dirplus_next also so readdir and
1521 * readdirplus are kept in sync.
1523 if (dir
->dirplus_list
!= NULL
) {
1524 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1538 * Routine to create a directory ...
1542 SMBC_mkdir_ctx(SMBCCTX
*context
,
1546 SMBCSRV
*srv
= NULL
;
1547 char *server
= NULL
;
1550 char *password
= NULL
;
1551 char *workgroup
= NULL
;
1553 char *targetpath
= NULL
;
1555 struct cli_state
*targetcli
= NULL
;
1556 struct cli_credentials
*creds
= NULL
;
1557 TALLOC_CTX
*frame
= talloc_stackframe();
1560 if (!context
|| !context
->internal
->initialized
) {
1572 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1574 if (SMBC_parse_path(frame
,
1590 if (!user
|| user
[0] == (char)0) {
1591 user
= talloc_strdup(frame
, smbc_getUser(context
));
1599 srv
= SMBC_server(frame
, context
, True
,
1600 server
, port
, share
, &workgroup
, &user
, &password
);
1605 return -1; /* errno set by SMBC_server */
1609 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
1611 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1612 status
= cli_resolve_path(frame
, "",
1614 srv
->cli
, path
, &targetcli
, &targetpath
);
1615 if (!NT_STATUS_IS_OK(status
)) {
1616 d_printf("Could not resolve %s\n", path
);
1621 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1623 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1624 errno
= SMBC_errno(context
, targetcli
);
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
= get_cmdline_auth_info_creds(context
->internal
->auth_info
),
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 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1739 errno
= SMBC_errno(context
, targetcli
);
1741 if (errno
== EACCES
) { /* Check if the dir empty or not */
1743 /* Local storage to avoid buffer overflows */
1745 bool smbc_rmdir_dirempty
= true;
1747 lpath
= talloc_asprintf(frame
, "%s\\*",
1755 status
= cli_list(targetcli
, lpath
,
1756 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1758 &smbc_rmdir_dirempty
);
1760 if (!NT_STATUS_IS_OK(status
)) {
1761 /* Fix errno to ignore latest error ... */
1762 DEBUG(5, ("smbc_rmdir: "
1763 "cli_list returned an error: %d\n",
1764 SMBC_errno(context
, targetcli
)));
1769 if (smbc_rmdir_dirempty
)
1787 * Routine to return the current directory position
1791 SMBC_telldir_ctx(SMBCCTX
*context
,
1794 TALLOC_CTX
*frame
= talloc_stackframe();
1796 if (!context
|| !context
->internal
->initialized
) {
1804 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1812 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1820 /* See if we're already at the end. */
1821 if (dir
->dir_next
== NULL
) {
1828 * We return the pointer here as the offset
1831 return (off_t
)(long)dir
->dir_next
->dirent
;
1835 * A routine to run down the list and see if the entry is OK
1836 * Modifies the dir list and the dirplus list (if it exists)
1837 * to point at the correct next entry on success.
1840 static bool update_dir_ents(SMBCFILE
*dir
, struct smbc_dirent
*dirent
)
1842 struct smbc_dir_list
*tmp_dir
= dir
->dir_list
;
1843 struct smbc_dirplus_list
*tmp_dirplus
= dir
->dirplus_list
;
1846 * Run down the list looking for what we want.
1847 * If we're enumerating files both dir_list
1848 * and dirplus_list contain the same entry
1849 * list, as they were seeded from the same
1850 * cli_list callback.
1852 * If we're enumerating servers then
1853 * dirplus_list will be NULL, so don't
1854 * update in that case.
1857 while (tmp_dir
!= NULL
) {
1858 if (tmp_dir
->dirent
== dirent
) {
1859 dir
->dir_next
= tmp_dir
;
1860 if (tmp_dirplus
!= NULL
) {
1861 dir
->dirplus_next
= tmp_dirplus
;
1865 tmp_dir
= tmp_dir
->next
;
1866 if (tmp_dirplus
!= NULL
) {
1867 tmp_dirplus
= tmp_dirplus
->next
;
1874 * Routine to seek on a directory
1878 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1882 long int l_offset
= offset
; /* Handle problems of size */
1883 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1884 TALLOC_CTX
*frame
= talloc_stackframe();
1887 if (!context
|| !context
->internal
->initialized
) {
1895 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1903 /* Now, check what we were passed and see if it is OK ... */
1905 if (dirent
== NULL
) { /* Seek to the begining of the list */
1907 dir
->dir_next
= dir
->dir_list
;
1909 /* Do the same for dirplus. */
1910 dir
->dirplus_next
= dir
->dirplus_list
;
1917 if (offset
== -1) { /* Seek to the end of the list */
1918 dir
->dir_next
= NULL
;
1920 /* Do the same for dirplus. */
1921 dir
->dirplus_next
= NULL
;
1928 * Run down the list and make sure that the entry is OK.
1929 * Update the position of both dir and dirplus lists.
1932 ok
= update_dir_ents(dir
, dirent
);
1934 errno
= EINVAL
; /* Bad entry */
1944 * Routine to fstat a dir
1948 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1953 if (!context
|| !context
->internal
->initialized
) {
1959 /* No code yet ... */
1964 SMBC_chmod_ctx(SMBCCTX
*context
,
1968 SMBCSRV
*srv
= NULL
;
1969 char *server
= NULL
;
1972 char *password
= NULL
;
1973 char *workgroup
= NULL
;
1974 char *targetpath
= NULL
;
1975 struct cli_state
*targetcli
= NULL
;
1979 struct cli_credentials
*creds
= NULL
;
1980 TALLOC_CTX
*frame
= talloc_stackframe();
1983 if (!context
|| !context
->internal
->initialized
) {
1985 errno
= EINVAL
; /* Best I can think of ... */
1996 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1998 if (SMBC_parse_path(frame
,
2014 if (!user
|| user
[0] == (char)0) {
2015 user
= talloc_strdup(frame
, smbc_getUser(context
));
2023 srv
= SMBC_server(frame
, context
, True
,
2024 server
, port
, share
, &workgroup
, &user
, &password
);
2028 return -1; /* errno set by SMBC_server */
2031 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
2033 /*d_printf(">>>unlink: resolving %s\n", path);*/
2034 status
= cli_resolve_path(frame
, "",
2036 srv
->cli
, path
, &targetcli
, &targetpath
);
2037 if (!NT_STATUS_IS_OK(status
)) {
2038 d_printf("Could not resolve %s\n", path
);
2046 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) attr
|= FILE_ATTRIBUTE_READONLY
;
2047 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) attr
|= FILE_ATTRIBUTE_ARCHIVE
;
2048 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) attr
|= FILE_ATTRIBUTE_SYSTEM
;
2049 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) attr
|= FILE_ATTRIBUTE_HIDDEN
;
2051 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, attr
, 0))) {
2052 errno
= SMBC_errno(context
, targetcli
);
2062 SMBC_utimes_ctx(SMBCCTX
*context
,
2064 struct timeval
*tbuf
)
2066 SMBCSRV
*srv
= NULL
;
2067 char *server
= NULL
;
2070 char *password
= NULL
;
2071 char *workgroup
= NULL
;
2073 struct timespec access_time
, write_time
;
2075 TALLOC_CTX
*frame
= talloc_stackframe();
2078 if (!context
|| !context
->internal
->initialized
) {
2080 errno
= EINVAL
; /* Best I can think of ... */
2092 access_time
= write_time
= timespec_current();
2094 access_time
= convert_timeval_to_timespec(tbuf
[0]);
2095 write_time
= convert_timeval_to_timespec(tbuf
[1]);
2099 struct timeval_buf abuf
, wbuf
;
2101 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
2103 timespec_string_buf(&access_time
, false, &abuf
),
2104 timespec_string_buf(&write_time
, false, &wbuf
));
2107 if (SMBC_parse_path(frame
,
2123 if (!user
|| user
[0] == (char)0) {
2124 user
= talloc_strdup(frame
, smbc_getUser(context
));
2132 srv
= SMBC_server(frame
, context
, True
,
2133 server
, port
, share
, &workgroup
, &user
, &password
);
2137 return -1; /* errno set by SMBC_server */
2144 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2147 (struct timespec
) { .tv_nsec
= SAMBA_UTIME_OMIT
},
2151 return -1; /* errno set by SMBC_setatr */
2159 * Routine to unlink() a file
2163 SMBC_unlink_ctx(SMBCCTX
*context
,
2166 char *server
= NULL
;
2169 char *password
= NULL
;
2170 char *workgroup
= NULL
;
2172 char *targetpath
= NULL
;
2174 struct cli_state
*targetcli
= NULL
;
2175 SMBCSRV
*srv
= NULL
;
2176 struct cli_credentials
*creds
= NULL
;
2177 TALLOC_CTX
*frame
= talloc_stackframe();
2180 if (!context
|| !context
->internal
->initialized
) {
2182 errno
= EINVAL
; /* Best I can think of ... */
2195 if (SMBC_parse_path(frame
,
2211 if (!user
|| user
[0] == (char)0) {
2212 user
= talloc_strdup(frame
, smbc_getUser(context
));
2220 srv
= SMBC_server(frame
, context
, True
,
2221 server
, port
, share
, &workgroup
, &user
, &password
);
2225 return -1; /* SMBC_server sets errno */
2229 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
2231 /*d_printf(">>>unlink: resolving %s\n", path);*/
2232 status
= cli_resolve_path(frame
, "",
2234 srv
->cli
, path
, &targetcli
, &targetpath
);
2235 if (!NT_STATUS_IS_OK(status
)) {
2236 d_printf("Could not resolve %s\n", path
);
2241 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2243 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
2245 errno
= SMBC_errno(context
, targetcli
);
2247 if (errno
== EACCES
) { /* Check if the file is a directory */
2250 struct stat sb
= {0};
2253 ok
= SMBC_getatr(context
, srv
, path
, &sb
);
2255 /* Hmmm, bad error ... What? */
2257 errno
= SMBC_errno(context
, targetcli
);
2264 if (S_ISDIR(sb
.st_mode
))
2267 errno
= saverr
; /* Restore this */
2278 return 0; /* Success ... */
2283 * Routine to rename() a file
2287 SMBC_rename_ctx(SMBCCTX
*ocontext
,
2292 char *server1
= NULL
;
2293 char *share1
= NULL
;
2294 char *server2
= NULL
;
2295 char *share2
= NULL
;
2298 char *password1
= NULL
;
2299 char *password2
= NULL
;
2300 char *workgroup
= NULL
;
2303 char *targetpath1
= NULL
;
2304 char *targetpath2
= NULL
;
2305 struct cli_state
*targetcli1
= NULL
;
2306 struct cli_state
*targetcli2
= NULL
;
2307 SMBCSRV
*srv
= NULL
;
2310 struct cli_credentials
*ocreds
= NULL
;
2311 struct cli_credentials
*ncreds
= NULL
;
2312 TALLOC_CTX
*frame
= talloc_stackframe();
2315 if (!ocontext
|| !ncontext
||
2316 !ocontext
->internal
->initialized
||
2317 !ncontext
->internal
->initialized
) {
2319 errno
= EINVAL
; /* Best I can think of ... */
2324 if (!oname
|| !nname
) {
2330 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
2332 if (SMBC_parse_path(frame
,
2348 if (!user1
|| user1
[0] == (char)0) {
2349 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
2357 if (SMBC_parse_path(frame
,
2373 if (!user2
|| user2
[0] == (char)0) {
2374 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
2382 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2383 strcmp(user1
, user2
)) {
2384 /* Can't rename across file systems, or users?? */
2390 srv
= SMBC_server(frame
, ocontext
, True
,
2391 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2398 /* set the credentials to make DFS work */
2399 smbc_set_credentials_with_fallback(ocontext
,
2404 /*d_printf(">>>rename: resolving %s\n", path1);*/
2405 ocreds
= get_cmdline_auth_info_creds(ocontext
->internal
->auth_info
);
2407 status
= cli_resolve_path(frame
, "",
2409 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2410 if (!NT_STATUS_IS_OK(status
)) {
2411 d_printf("Could not resolve %s\n", path1
);
2417 /* set the credentials to make DFS work */
2418 smbc_set_credentials_with_fallback(ncontext
,
2423 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2424 /*d_printf(">>>rename: resolving %s\n", path2);*/
2425 ncreds
= get_cmdline_auth_info_creds(ncontext
->internal
->auth_info
);
2427 status
= cli_resolve_path(frame
, "",
2429 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2430 if (!NT_STATUS_IS_OK(status
)) {
2431 d_printf("Could not resolve %s\n", path2
);
2436 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2438 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2439 strcmp(targetcli1
->share
, targetcli2
->share
))
2441 /* can't rename across file systems */
2447 if (!NT_STATUS_IS_OK(
2448 cli_rename(targetcli1
, targetpath1
, targetpath2
, false))) {
2449 int eno
= SMBC_errno(ocontext
, targetcli1
);
2451 if (eno
!= EEXIST
||
2452 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2453 FILE_ATTRIBUTE_SYSTEM
|
2454 FILE_ATTRIBUTE_HIDDEN
)) ||
2455 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2456 targetpath2
, false))) {
2466 return 0; /* Success */
2469 struct smbc_notify_cb_state
{
2470 struct tevent_context
*ev
;
2471 struct cli_state
*cli
;
2474 uint32_t completion_filter
;
2475 unsigned callback_timeout_ms
;
2476 smbc_notify_callback_fn cb
;
2480 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2481 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2483 static struct tevent_req
*smbc_notify_cb_send(
2484 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2485 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2486 unsigned callback_timeout_ms
,
2487 smbc_notify_callback_fn cb
, void *private_data
)
2489 struct tevent_req
*req
, *subreq
;
2490 struct smbc_notify_cb_state
*state
;
2492 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2499 state
->recursive
= recursive
;
2500 state
->completion_filter
= completion_filter
;
2501 state
->callback_timeout_ms
= callback_timeout_ms
;
2503 state
->private_data
= private_data
;
2505 subreq
= cli_notify_send(
2506 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2507 state
->completion_filter
, state
->recursive
);
2508 if (tevent_req_nomem(subreq
, req
)) {
2509 return tevent_req_post(req
, ev
);
2511 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2513 if (state
->callback_timeout_ms
== 0) {
2517 subreq
= tevent_wakeup_send(
2519 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2520 state
->callback_timeout_ms
*1000));
2521 if (tevent_req_nomem(subreq
, req
)) {
2522 return tevent_req_post(req
, ev
);
2524 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2529 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2531 struct tevent_req
*req
= tevent_req_callback_data(
2532 subreq
, struct tevent_req
);
2533 struct smbc_notify_cb_state
*state
= tevent_req_data(
2534 req
, struct smbc_notify_cb_state
);
2535 uint32_t num_changes
;
2536 struct notify_change
*changes
;
2540 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2541 TALLOC_FREE(subreq
);
2542 if (tevent_req_nterror(req
, status
)) {
2547 struct smbc_notify_callback_action actions
[num_changes
];
2550 for (i
=0; i
<num_changes
; i
++) {
2551 actions
[i
].action
= changes
[i
].action
;
2552 actions
[i
].filename
= changes
[i
].name
;
2555 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2558 TALLOC_FREE(changes
);
2561 tevent_req_done(req
);
2565 subreq
= cli_notify_send(
2566 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2567 state
->completion_filter
, state
->recursive
);
2568 if (tevent_req_nomem(subreq
, req
)) {
2571 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2574 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2576 struct tevent_req
*req
= tevent_req_callback_data(
2577 subreq
, struct tevent_req
);
2578 struct smbc_notify_cb_state
*state
= tevent_req_data(
2579 req
, struct smbc_notify_cb_state
);
2583 ok
= tevent_wakeup_recv(subreq
);
2584 TALLOC_FREE(subreq
);
2586 tevent_req_oom(req
);
2590 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2592 tevent_req_done(req
);
2596 subreq
= tevent_wakeup_send(
2598 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2599 state
->callback_timeout_ms
*1000));
2600 if (tevent_req_nomem(subreq
, req
)) {
2603 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2606 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2608 return tevent_req_simple_recv_ntstatus(req
);
2611 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2612 bool recursive
, uint32_t completion_filter
,
2613 unsigned callback_timeout_ms
,
2614 smbc_notify_callback_fn cb
, void *private_data
)
2616 TALLOC_CTX
*frame
= talloc_stackframe();
2617 struct tevent_context
*ev
;
2618 struct tevent_req
*req
;
2619 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2621 ev
= samba_tevent_context_init(frame
);
2625 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2627 callback_timeout_ms
, cb
, private_data
);
2631 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2634 status
= smbc_notify_cb_recv(req
);
2642 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2643 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2644 smbc_notify_callback_fn cb
, void *private_data
)
2646 TALLOC_CTX
*frame
= talloc_stackframe();
2647 struct cli_state
*cli
;
2648 char *server
= NULL
;
2651 char *password
= NULL
;
2652 char *options
= NULL
;
2653 char *workgroup
= NULL
;
2659 if ((context
== NULL
) || !context
->internal
->initialized
) {
2664 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2670 if (SMBC_parse_path(frame
,
2681 DEBUG(4, ("no valid path\n"));
2683 errno
= EINVAL
+ 8194;
2687 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2688 "path='%s' options='%s'\n",
2689 dir
->fname
, server
, share
, path
, options
));
2691 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2692 (int)recursive
, completion_filter
));
2694 cli
= dir
->srv
->cli
;
2695 status
= cli_ntcreate(
2696 cli
, path
, 0, FILE_READ_DATA
, 0,
2697 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2698 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2699 if (!NT_STATUS_IS_OK(status
)) {
2700 int err
= SMBC_errno(context
, cli
);
2706 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2707 callback_timeout_ms
, cb
, private_data
);
2708 if (!NT_STATUS_IS_OK(status
)) {
2709 int err
= SMBC_errno(context
, cli
);
2710 cli_close(cli
, fnum
);
2716 cli_close(cli
, fnum
);