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
= talloc_stackframe();
1046 if (!context
|| !context
->internal
->initialized
) {
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
);
1065 SAFE_FREE(dir
->fname
);
1066 SAFE_FREE(dir
); /* Free the space too */
1075 smbc_readdir_internal(SMBCCTX
* context
,
1076 struct smbc_dirent
*dest
,
1077 struct smbc_dirent
*src
,
1078 int max_namebuf_len
)
1080 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
1083 /* url-encode the name. get back remaining buffer space */
1085 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
1087 /* -1 means no null termination. */
1088 if (remaining_len
< 0) {
1092 /* We now know the name length */
1093 dest
->namelen
= strlen(dest
->name
);
1095 if (dest
->namelen
+ 1 < 1) {
1100 if (dest
->namelen
+ 1 >= max_namebuf_len
) {
1101 /* Out of space for comment. */
1105 /* Save the pointer to the beginning of the comment */
1106 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
1108 if (remaining_len
< 1) {
1109 /* No room for comment null termination. */
1113 /* Copy the comment */
1114 strlcpy(dest
->comment
, src
->comment
, remaining_len
);
1116 /* Save other fields */
1117 dest
->smbc_type
= src
->smbc_type
;
1118 dest
->commentlen
= strlen(dest
->comment
);
1119 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
1123 /* No encoding. Just copy the entry as is. */
1124 if (src
->dirlen
> max_namebuf_len
) {
1127 memcpy(dest
, src
, src
->dirlen
);
1128 if (src
->namelen
+ 1 < 1) {
1132 if (src
->namelen
+ 1 >= max_namebuf_len
) {
1133 /* Comment off the end. */
1136 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
1142 * Routine to get a directory entry
1145 struct smbc_dirent
*
1146 SMBC_readdir_ctx(SMBCCTX
*context
,
1151 struct smbc_dirent
*dirp
, *dirent
;
1152 TALLOC_CTX
*frame
= talloc_stackframe();
1154 /* Check that all is ok first ... */
1156 if (!context
|| !context
->internal
->initialized
) {
1159 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1165 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1168 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1174 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1177 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1183 if (!dir
->dir_next
) {
1188 dirent
= dir
->dir_next
->dirent
;
1197 dirp
= &context
->internal
->dirent
;
1198 maxlen
= sizeof(context
->internal
->_dirent_name
);
1200 ret
= smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1207 dir
->dir_next
= dir
->dir_next
->next
;
1210 * If we are returning file entries, we
1211 * have a duplicate list in dirplus.
1213 * Update dirplus_next also so readdir and
1214 * readdirplus are kept in sync.
1216 if (dir
->dirplus_list
!= NULL
) {
1217 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1225 * Routine to get a directory entry with all attributes
1228 const struct libsmb_file_info
*
1229 SMBC_readdirplus_ctx(SMBCCTX
*context
,
1232 struct libsmb_file_info
*smb_finfo
= NULL
;
1233 TALLOC_CTX
*frame
= talloc_stackframe();
1235 /* Check that all is ok first ... */
1237 if (context
== NULL
|| !context
->internal
->initialized
) {
1238 DBG_ERR("Invalid context in SMBC_readdirplus_ctx()\n");
1244 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1245 DBG_ERR("Invalid dir in SMBC_readdirplus_ctx()\n");
1251 if (dir
->dirplus_next
== NULL
) {
1256 smb_finfo
= dir
->dirplus_next
->smb_finfo
;
1257 if (smb_finfo
== NULL
) {
1262 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1265 * If we are returning file entries, we
1266 * have a duplicate list in dir_list
1268 * Update dir_next also so readdir and
1269 * readdirplus are kept in sync.
1271 if (dir
->dir_list
) {
1272 dir
->dir_next
= dir
->dir_next
->next
;
1280 * Routine to get a directory entry plus a filled in stat structure if
1284 const struct libsmb_file_info
*SMBC_readdirplus2_ctx(SMBCCTX
*context
,
1288 struct libsmb_file_info
*smb_finfo
= NULL
;
1289 struct smbc_dirplus_list
*dp_list
= NULL
;
1291 char *full_pathname
= NULL
;
1292 char *workgroup
= NULL
;
1293 char *server
= NULL
;
1298 char *password
= NULL
;
1299 char *options
= NULL
;
1301 TALLOC_CTX
*frame
= NULL
;
1304 * Allow caller to pass in NULL for stat pointer if
1305 * required. This makes this call identical to
1306 * smbc_readdirplus().
1310 return SMBC_readdirplus_ctx(context
, dir
);
1313 frame
= talloc_stackframe();
1315 /* Check that all is ok first ... */
1316 if (context
== NULL
|| !context
->internal
->initialized
) {
1317 DBG_ERR("Invalid context in SMBC_readdirplus2_ctx()\n");
1323 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1324 DBG_ERR("Invalid dir in SMBC_readdirplus2_ctx()\n");
1330 dp_list
= dir
->dirplus_next
;
1331 if (dp_list
== NULL
) {
1336 ino
= (ino_t
)dp_list
->ino
;
1338 smb_finfo
= dp_list
->smb_finfo
;
1339 if (smb_finfo
== NULL
) {
1345 full_pathname
= talloc_asprintf(frame
,
1349 if (full_pathname
== NULL
) {
1355 rc
= SMBC_parse_path(frame
,
1378 smb_finfo
->atime_ts
,
1379 smb_finfo
->ctime_ts
,
1380 smb_finfo
->mtime_ts
);
1382 TALLOC_FREE(full_pathname
);
1384 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1387 * If we are returning file entries, we
1388 * have a duplicate list in dir_list
1390 * Update dir_next also so readdir and
1391 * readdirplus are kept in sync.
1393 if (dir
->dir_list
) {
1394 dir
->dir_next
= dir
->dir_next
->next
;
1402 * Routine to get directory entries
1406 SMBC_getdents_ctx(SMBCCTX
*context
,
1408 struct smbc_dirent
*dirp
,
1414 char *ndir
= (char *)dirp
;
1415 struct smbc_dir_list
*dirlist
;
1416 TALLOC_CTX
*frame
= talloc_stackframe();
1418 /* Check that all is ok first ... */
1420 if (!context
|| !context
->internal
->initialized
) {
1428 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1436 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1445 * Now, retrieve the number of entries that will fit in what was passed
1446 * We have to figure out if the info is in the list, or we need to
1447 * send a request to the server to get the info.
1450 while ((dirlist
= dir
->dir_next
)) {
1452 struct smbc_dirent
*dirent
;
1453 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1455 if (!dirlist
->dirent
) {
1457 errno
= ENOENT
; /* Bad error */
1463 /* Do urlencoding of next entry, if so selected */
1464 dirent
= &context
->internal
->dirent
;
1465 maxlen
= sizeof(context
->internal
->_dirent_name
);
1466 ret
= smbc_readdir_internal(context
, dirent
,
1467 dirlist
->dirent
, maxlen
);
1474 reqd
= dirent
->dirlen
;
1478 if (rem
< count
) { /* We managed to copy something */
1485 else { /* Nothing copied ... */
1487 errno
= EINVAL
; /* Not enough space ... */
1495 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1497 currentEntry
->comment
= ¤tEntry
->name
[0] +
1498 dirent
->namelen
+ 1;
1503 /* Try and align the struct for the next entry
1504 on a valid pointer boundary by appending zeros */
1505 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1509 currentEntry
->dirlen
++;
1512 dir
->dir_next
= dirlist
= dirlist
-> next
;
1515 * If we are returning file entries, we
1516 * have a duplicate list in dirplus.
1518 * Update dirplus_next also so readdir and
1519 * readdirplus are kept in sync.
1521 if (dir
->dirplus_list
!= NULL
) {
1522 dir
->dirplus_next
= dir
->dirplus_next
->next
;
1536 * Routine to create a directory ...
1540 SMBC_mkdir_ctx(SMBCCTX
*context
,
1544 SMBCSRV
*srv
= NULL
;
1545 char *server
= NULL
;
1548 char *password
= NULL
;
1549 char *workgroup
= NULL
;
1551 char *targetpath
= NULL
;
1553 struct cli_state
*targetcli
= NULL
;
1554 struct cli_credentials
*creds
= NULL
;
1555 TALLOC_CTX
*frame
= talloc_stackframe();
1558 if (!context
|| !context
->internal
->initialized
) {
1570 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1572 if (SMBC_parse_path(frame
,
1588 if (!user
|| user
[0] == (char)0) {
1589 user
= talloc_strdup(frame
, smbc_getUser(context
));
1597 srv
= SMBC_server(frame
, context
, True
,
1598 server
, port
, share
, &workgroup
, &user
, &password
);
1603 return -1; /* errno set by SMBC_server */
1607 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
1609 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1610 status
= cli_resolve_path(frame
, "",
1612 srv
->cli
, path
, &targetcli
, &targetpath
);
1613 if (!NT_STATUS_IS_OK(status
)) {
1614 d_printf("Could not resolve %s\n", path
);
1619 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1621 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1622 errno
= SMBC_errno(context
, targetcli
);
1634 * Our list function simply checks to see if a directory is not empty
1638 rmdir_list_fn(struct file_info
*finfo
,
1642 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1643 strncmp(finfo
->name
, "..", 2) != 0) {
1644 bool *smbc_rmdir_dirempty
= (bool *)state
;
1645 *smbc_rmdir_dirempty
= false;
1647 return NT_STATUS_OK
;
1651 * Routine to remove a directory
1655 SMBC_rmdir_ctx(SMBCCTX
*context
,
1658 SMBCSRV
*srv
= NULL
;
1659 char *server
= NULL
;
1662 char *password
= NULL
;
1663 char *workgroup
= NULL
;
1665 char *targetpath
= NULL
;
1667 struct cli_state
*targetcli
= NULL
;
1668 struct cli_credentials
*creds
= NULL
;
1669 TALLOC_CTX
*frame
= talloc_stackframe();
1672 if (!context
|| !context
->internal
->initialized
) {
1684 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1686 if (SMBC_parse_path(frame
,
1702 if (!user
|| user
[0] == (char)0) {
1703 user
= talloc_strdup(frame
, smbc_getUser(context
));
1711 srv
= SMBC_server(frame
, context
, True
,
1712 server
, port
, share
, &workgroup
, &user
, &password
);
1717 return -1; /* errno set by SMBC_server */
1721 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
),
1723 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1724 status
= cli_resolve_path(frame
, "",
1726 srv
->cli
, path
, &targetcli
, &targetpath
);
1727 if (!NT_STATUS_IS_OK(status
)) {
1728 d_printf("Could not resolve %s\n", path
);
1733 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1735 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1737 errno
= SMBC_errno(context
, targetcli
);
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 DEBUG(5, ("smbc_rmdir: "
1761 "cli_list returned an error: %d\n",
1762 SMBC_errno(context
, targetcli
)));
1767 if (smbc_rmdir_dirempty
)
1785 * Routine to return the current directory position
1789 SMBC_telldir_ctx(SMBCCTX
*context
,
1792 TALLOC_CTX
*frame
= talloc_stackframe();
1794 if (!context
|| !context
->internal
->initialized
) {
1802 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1810 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1818 /* See if we're already at the end. */
1819 if (dir
->dir_next
== NULL
) {
1826 * We return the pointer here as the offset
1829 return (off_t
)(long)dir
->dir_next
->dirent
;
1833 * A routine to run down the list and see if the entry is OK
1834 * Modifies the dir list and the dirplus list (if it exists)
1835 * to point at the correct next entry on success.
1838 static bool update_dir_ents(SMBCFILE
*dir
, struct smbc_dirent
*dirent
)
1840 struct smbc_dir_list
*tmp_dir
= dir
->dir_list
;
1841 struct smbc_dirplus_list
*tmp_dirplus
= dir
->dirplus_list
;
1844 * Run down the list looking for what we want.
1845 * If we're enumerating files both dir_list
1846 * and dirplus_list contain the same entry
1847 * list, as they were seeded from the same
1848 * cli_list callback.
1850 * If we're enumerating servers then
1851 * dirplus_list will be NULL, so don't
1852 * update in that case.
1855 while (tmp_dir
!= NULL
) {
1856 if (tmp_dir
->dirent
== dirent
) {
1857 dir
->dir_next
= tmp_dir
;
1858 if (tmp_dirplus
!= NULL
) {
1859 dir
->dirplus_next
= tmp_dirplus
;
1863 tmp_dir
= tmp_dir
->next
;
1864 if (tmp_dirplus
!= NULL
) {
1865 tmp_dirplus
= tmp_dirplus
->next
;
1872 * Routine to seek on a directory
1876 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1880 long int l_offset
= offset
; /* Handle problems of size */
1881 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1882 TALLOC_CTX
*frame
= talloc_stackframe();
1885 if (!context
|| !context
->internal
->initialized
) {
1893 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1901 /* Now, check what we were passed and see if it is OK ... */
1903 if (dirent
== NULL
) { /* Seek to the begining of the list */
1905 dir
->dir_next
= dir
->dir_list
;
1907 /* Do the same for dirplus. */
1908 dir
->dirplus_next
= dir
->dirplus_list
;
1915 if (offset
== -1) { /* Seek to the end of the list */
1916 dir
->dir_next
= NULL
;
1918 /* Do the same for dirplus. */
1919 dir
->dirplus_next
= NULL
;
1926 * Run down the list and make sure that the entry is OK.
1927 * Update the position of both dir and dirplus lists.
1930 ok
= update_dir_ents(dir
, dirent
);
1932 errno
= EINVAL
; /* Bad entry */
1942 * Routine to fstat a dir
1946 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1951 if (!context
|| !context
->internal
->initialized
) {
1957 /* No code yet ... */
1962 SMBC_chmod_ctx(SMBCCTX
*context
,
1966 SMBCSRV
*srv
= NULL
;
1967 char *server
= NULL
;
1970 char *password
= NULL
;
1971 char *workgroup
= NULL
;
1972 char *targetpath
= NULL
;
1973 struct cli_state
*targetcli
= NULL
;
1977 struct cli_credentials
*creds
= NULL
;
1978 TALLOC_CTX
*frame
= talloc_stackframe();
1981 if (!context
|| !context
->internal
->initialized
) {
1983 errno
= EINVAL
; /* Best I can think of ... */
1994 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1996 if (SMBC_parse_path(frame
,
2012 if (!user
|| user
[0] == (char)0) {
2013 user
= talloc_strdup(frame
, smbc_getUser(context
));
2021 srv
= SMBC_server(frame
, context
, True
,
2022 server
, port
, share
, &workgroup
, &user
, &password
);
2026 return -1; /* errno set by SMBC_server */
2029 creds
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
2031 /*d_printf(">>>unlink: resolving %s\n", path);*/
2032 status
= cli_resolve_path(frame
, "",
2034 srv
->cli
, path
, &targetcli
, &targetpath
);
2035 if (!NT_STATUS_IS_OK(status
)) {
2036 d_printf("Could not resolve %s\n", path
);
2044 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) attr
|= FILE_ATTRIBUTE_READONLY
;
2045 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) attr
|= FILE_ATTRIBUTE_ARCHIVE
;
2046 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) attr
|= FILE_ATTRIBUTE_SYSTEM
;
2047 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) attr
|= FILE_ATTRIBUTE_HIDDEN
;
2049 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, attr
, 0))) {
2050 errno
= SMBC_errno(context
, targetcli
);
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
= get_cmdline_auth_info_creds(context
->internal
->auth_info
);
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 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
2243 errno
= SMBC_errno(context
, targetcli
);
2245 if (errno
== EACCES
) { /* Check if the file is a directory */
2248 struct stat sb
= {0};
2251 ok
= SMBC_getatr(context
, srv
, path
, &sb
);
2253 /* Hmmm, bad error ... What? */
2255 errno
= SMBC_errno(context
, targetcli
);
2262 if (S_ISDIR(sb
.st_mode
))
2265 errno
= saverr
; /* Restore this */
2276 return 0; /* Success ... */
2281 * Routine to rename() a file
2285 SMBC_rename_ctx(SMBCCTX
*ocontext
,
2290 char *server1
= NULL
;
2291 char *share1
= NULL
;
2292 char *server2
= NULL
;
2293 char *share2
= NULL
;
2296 char *password1
= NULL
;
2297 char *password2
= NULL
;
2298 char *workgroup
= NULL
;
2301 char *targetpath1
= NULL
;
2302 char *targetpath2
= NULL
;
2303 struct cli_state
*targetcli1
= NULL
;
2304 struct cli_state
*targetcli2
= NULL
;
2305 SMBCSRV
*srv
= NULL
;
2308 struct cli_credentials
*ocreds
= NULL
;
2309 struct cli_credentials
*ncreds
= NULL
;
2310 TALLOC_CTX
*frame
= talloc_stackframe();
2313 if (!ocontext
|| !ncontext
||
2314 !ocontext
->internal
->initialized
||
2315 !ncontext
->internal
->initialized
) {
2317 errno
= EINVAL
; /* Best I can think of ... */
2322 if (!oname
|| !nname
) {
2328 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
2330 if (SMBC_parse_path(frame
,
2346 if (!user1
|| user1
[0] == (char)0) {
2347 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
2355 if (SMBC_parse_path(frame
,
2371 if (!user2
|| user2
[0] == (char)0) {
2372 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
2380 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2381 strcmp(user1
, user2
)) {
2382 /* Can't rename across file systems, or users?? */
2388 srv
= SMBC_server(frame
, ocontext
, True
,
2389 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2396 /* set the credentials to make DFS work */
2397 smbc_set_credentials_with_fallback(ocontext
,
2402 /*d_printf(">>>rename: resolving %s\n", path1);*/
2403 ocreds
= get_cmdline_auth_info_creds(ocontext
->internal
->auth_info
);
2405 status
= cli_resolve_path(frame
, "",
2407 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2408 if (!NT_STATUS_IS_OK(status
)) {
2409 d_printf("Could not resolve %s\n", path1
);
2415 /* set the credentials to make DFS work */
2416 smbc_set_credentials_with_fallback(ncontext
,
2421 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2422 /*d_printf(">>>rename: resolving %s\n", path2);*/
2423 ncreds
= get_cmdline_auth_info_creds(ncontext
->internal
->auth_info
);
2425 status
= cli_resolve_path(frame
, "",
2427 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2428 if (!NT_STATUS_IS_OK(status
)) {
2429 d_printf("Could not resolve %s\n", path2
);
2434 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2436 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2437 strcmp(targetcli1
->share
, targetcli2
->share
))
2439 /* can't rename across file systems */
2445 if (!NT_STATUS_IS_OK(
2446 cli_rename(targetcli1
, targetpath1
, targetpath2
, false))) {
2447 int eno
= SMBC_errno(ocontext
, targetcli1
);
2449 if (eno
!= EEXIST
||
2450 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2451 FILE_ATTRIBUTE_SYSTEM
|
2452 FILE_ATTRIBUTE_HIDDEN
)) ||
2453 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2454 targetpath2
, false))) {
2464 return 0; /* Success */
2467 struct smbc_notify_cb_state
{
2468 struct tevent_context
*ev
;
2469 struct cli_state
*cli
;
2472 uint32_t completion_filter
;
2473 unsigned callback_timeout_ms
;
2474 smbc_notify_callback_fn cb
;
2478 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2479 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2481 static struct tevent_req
*smbc_notify_cb_send(
2482 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2483 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2484 unsigned callback_timeout_ms
,
2485 smbc_notify_callback_fn cb
, void *private_data
)
2487 struct tevent_req
*req
, *subreq
;
2488 struct smbc_notify_cb_state
*state
;
2490 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2497 state
->recursive
= recursive
;
2498 state
->completion_filter
= completion_filter
;
2499 state
->callback_timeout_ms
= callback_timeout_ms
;
2501 state
->private_data
= private_data
;
2503 subreq
= cli_notify_send(
2504 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2505 state
->completion_filter
, state
->recursive
);
2506 if (tevent_req_nomem(subreq
, req
)) {
2507 return tevent_req_post(req
, ev
);
2509 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2511 if (state
->callback_timeout_ms
== 0) {
2515 subreq
= tevent_wakeup_send(
2517 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2518 state
->callback_timeout_ms
*1000));
2519 if (tevent_req_nomem(subreq
, req
)) {
2520 return tevent_req_post(req
, ev
);
2522 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2527 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2529 struct tevent_req
*req
= tevent_req_callback_data(
2530 subreq
, struct tevent_req
);
2531 struct smbc_notify_cb_state
*state
= tevent_req_data(
2532 req
, struct smbc_notify_cb_state
);
2533 uint32_t num_changes
;
2534 struct notify_change
*changes
;
2538 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2539 TALLOC_FREE(subreq
);
2540 if (tevent_req_nterror(req
, status
)) {
2545 struct smbc_notify_callback_action actions
[num_changes
];
2548 for (i
=0; i
<num_changes
; i
++) {
2549 actions
[i
].action
= changes
[i
].action
;
2550 actions
[i
].filename
= changes
[i
].name
;
2553 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2556 TALLOC_FREE(changes
);
2559 tevent_req_done(req
);
2563 subreq
= cli_notify_send(
2564 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2565 state
->completion_filter
, state
->recursive
);
2566 if (tevent_req_nomem(subreq
, req
)) {
2569 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2572 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2574 struct tevent_req
*req
= tevent_req_callback_data(
2575 subreq
, struct tevent_req
);
2576 struct smbc_notify_cb_state
*state
= tevent_req_data(
2577 req
, struct smbc_notify_cb_state
);
2581 ok
= tevent_wakeup_recv(subreq
);
2582 TALLOC_FREE(subreq
);
2584 tevent_req_oom(req
);
2588 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2590 tevent_req_done(req
);
2594 subreq
= tevent_wakeup_send(
2596 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2597 state
->callback_timeout_ms
*1000));
2598 if (tevent_req_nomem(subreq
, req
)) {
2601 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2604 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2606 return tevent_req_simple_recv_ntstatus(req
);
2609 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2610 bool recursive
, uint32_t completion_filter
,
2611 unsigned callback_timeout_ms
,
2612 smbc_notify_callback_fn cb
, void *private_data
)
2614 TALLOC_CTX
*frame
= talloc_stackframe();
2615 struct tevent_context
*ev
;
2616 struct tevent_req
*req
;
2617 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2619 ev
= samba_tevent_context_init(frame
);
2623 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2625 callback_timeout_ms
, cb
, private_data
);
2629 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2632 status
= smbc_notify_cb_recv(req
);
2640 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2641 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2642 smbc_notify_callback_fn cb
, void *private_data
)
2644 TALLOC_CTX
*frame
= talloc_stackframe();
2645 struct cli_state
*cli
;
2646 char *server
= NULL
;
2649 char *password
= NULL
;
2650 char *options
= NULL
;
2651 char *workgroup
= NULL
;
2657 if ((context
== NULL
) || !context
->internal
->initialized
) {
2662 if (!SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2668 if (SMBC_parse_path(frame
,
2679 DEBUG(4, ("no valid path\n"));
2681 errno
= EINVAL
+ 8194;
2685 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2686 "path='%s' options='%s'\n",
2687 dir
->fname
, server
, share
, path
, options
));
2689 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2690 (int)recursive
, completion_filter
));
2692 cli
= dir
->srv
->cli
;
2693 status
= cli_ntcreate(
2694 cli
, path
, 0, FILE_READ_DATA
, 0,
2695 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2696 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2697 if (!NT_STATUS_IS_OK(status
)) {
2698 int err
= SMBC_errno(context
, cli
);
2704 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2705 callback_timeout_ms
, cb
, private_data
);
2706 if (!NT_STATUS_IS_OK(status
)) {
2707 int err
= SMBC_errno(context
, cli
);
2708 cli_close(cli
, fnum
);
2714 cli_close(cli
, fnum
);