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/libsmb.h"
27 #include "auth_info.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"
38 * Routine to open a directory
39 * We accept the URL syntax explained in SMBC_parse_path(), above.
43 remove_dir(SMBCFILE
*dir
)
45 struct smbc_dir_list
*d
,*f
;
57 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
62 add_dirent(SMBCFILE
*dir
,
67 struct smbc_dirent
*dirent
;
69 int name_length
= (name
== NULL
? 0 : strlen(name
));
70 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
73 * Allocate space for the dirent, which must be increased by the
74 * size of the name and the comment and 1 each for the null terminator.
77 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
79 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
83 dir
->dir_error
= ENOMEM
;
90 if (dir
->dir_list
== NULL
) {
92 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
96 dir
->dir_error
= ENOMEM
;
100 ZERO_STRUCTP(dir
->dir_list
);
102 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
106 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
108 if (!dir
->dir_end
->next
) {
111 dir
->dir_error
= ENOMEM
;
115 ZERO_STRUCTP(dir
->dir_end
->next
);
117 dir
->dir_end
= dir
->dir_end
->next
;
120 dir
->dir_end
->next
= NULL
;
121 dir
->dir_end
->dirent
= dirent
;
123 dirent
->smbc_type
= type
;
124 dirent
->namelen
= name_length
;
125 dirent
->commentlen
= comment_len
;
126 dirent
->dirlen
= size
;
129 * dirent->namelen + 1 includes the null (no null termination needed)
130 * Ditto for dirent->commentlen.
131 * The space for the two null bytes was allocated.
133 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
134 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
135 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
142 list_unique_wg_fn(const char *name
,
147 SMBCFILE
*dir
= (SMBCFILE
*)state
;
148 struct smbc_dir_list
*dir_list
;
149 struct smbc_dirent
*dirent
;
153 dirent_type
= dir
->dir_type
;
155 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
156 /* An error occurred, what do we do? */
157 /* FIXME: Add some code here */
158 /* Change cli_NetServerEnum to take a fn
159 returning NTSTATUS... JRA. */
162 /* Point to the one just added */
163 dirent
= dir
->dir_end
->dirent
;
165 /* See if this was a duplicate */
166 for (dir_list
= dir
->dir_list
;
167 dir_list
!= dir
->dir_end
;
168 dir_list
= dir_list
->next
) {
170 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
171 /* Duplicate. End end of list need to be removed. */
175 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
176 /* Found the end of the list. Remove it. */
177 dir
->dir_end
= dir_list
;
178 free(dir_list
->next
);
180 dir_list
->next
= NULL
;
187 list_fn(const char *name
,
192 SMBCFILE
*dir
= (SMBCFILE
*)state
;
196 * We need to process the type a little ...
198 * Disk share = 0x00000000
199 * Print share = 0x00000001
200 * Comms share = 0x00000002 (obsolete?)
201 * IPC$ share = 0x00000003
203 * administrative shares:
204 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
207 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
211 dirent_type
= SMBC_FILE_SHARE
;
215 dirent_type
= SMBC_PRINTER_SHARE
;
219 dirent_type
= SMBC_COMMS_SHARE
;
224 dirent_type
= SMBC_IPC_SHARE
;
228 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
233 dirent_type
= dir
->dir_type
;
236 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
237 /* An error occurred, what do we do? */
238 /* FIXME: Add some code here */
239 /* Change cli_NetServerEnum to take a fn
240 returning NTSTATUS... JRA. */
245 dir_list_fn(const char *mnt
,
246 struct file_info
*finfo
,
251 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
252 (finfo
->mode
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
253 SMBCFILE
*dir
= (SMBCFILE
*)state
;
254 return map_nt_error_from_unix(dir
->dir_error
);
260 net_share_enum_rpc(struct cli_state
*cli
,
261 void (*fn
)(const char *name
,
269 uint32_t preferred_len
= 0xffffffff;
271 struct srvsvc_NetShareInfoCtr info_ctr
;
272 struct srvsvc_NetShareCtr1 ctr1
;
274 fstring comment
= "";
275 struct rpc_pipe_client
*pipe_hnd
= NULL
;
277 uint32_t resume_handle
= 0;
278 uint32_t total_entries
= 0;
279 struct dcerpc_binding_handle
*b
;
281 /* Open the server service pipe */
282 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
284 if (!NT_STATUS_IS_OK(nt_status
)) {
285 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
289 ZERO_STRUCT(info_ctr
);
293 info_ctr
.ctr
.ctr1
= &ctr1
;
295 b
= pipe_hnd
->binding_handle
;
297 /* Issue the NetShareEnum RPC call and retrieve the response */
298 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
306 /* Was it successful? */
307 if (!NT_STATUS_IS_OK(nt_status
)) {
308 /* Nope. Go clean up. */
309 result
= ntstatus_to_werror(nt_status
);
313 if (!W_ERROR_IS_OK(result
)) {
314 /* Nope. Go clean up. */
318 if (total_entries
== 0) {
319 /* Nope. Go clean up. */
320 result
= WERR_GEN_FAILURE
;
324 /* For each returned entry... */
325 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
327 /* pull out the share name */
328 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
330 /* pull out the share's comment */
331 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
333 /* Get the type value */
334 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
336 /* Add this share to the list */
337 (*fn
)(name
, type
, comment
, state
);
341 /* Close the server service pipe */
342 TALLOC_FREE(pipe_hnd
);
344 /* Tell 'em if it worked */
345 return W_ERROR_IS_OK(result
) ? 0 : -1;
350 * Verify that the options specified in a URL are valid
353 SMBC_check_options(char *server
,
358 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
359 "path='%s' options='%s'\n",
360 server
, share
, path
, options
));
362 /* No options at all is always ok */
363 if (! *options
) return 0;
365 /* Currently, we don't support any options. */
371 SMBC_opendir_ctx(SMBCCTX
*context
,
378 char *password
= NULL
;
379 char *options
= NULL
;
380 char *workgroup
= NULL
;
386 SMBCFILE
*dir
= NULL
;
387 struct sockaddr_storage rem_ss
;
388 TALLOC_CTX
*frame
= talloc_stackframe();
390 if (!context
|| !context
->internal
->initialized
) {
391 DEBUG(4, ("no valid context\n"));
393 errno
= EINVAL
+ 8192;
399 DEBUG(4, ("no valid fname\n"));
401 errno
= EINVAL
+ 8193;
405 if (SMBC_parse_path(frame
,
416 DEBUG(4, ("no valid path\n"));
418 errno
= EINVAL
+ 8194;
422 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
423 "path='%s' options='%s'\n",
424 fname
, server
, share
, path
, options
));
426 /* Ensure the options are valid */
427 if (SMBC_check_options(server
, share
, path
, options
)) {
428 DEBUG(4, ("unacceptable options (%s)\n", options
));
430 errno
= EINVAL
+ 8195;
434 if (!user
|| user
[0] == (char)0) {
435 user
= talloc_strdup(frame
, smbc_getUser(context
));
443 dir
= SMB_MALLOC_P(SMBCFILE
);
454 dir
->fname
= SMB_STRDUP(fname
);
458 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
460 if (server
[0] == (char)0) {
465 struct sockaddr_storage
*ip_list
;
466 struct sockaddr_storage server_addr
;
467 struct user_auth_info
*u_info
;
470 if (share
[0] != (char)0 || path
[0] != (char)0) {
473 SAFE_FREE(dir
->fname
);
477 errno
= EINVAL
+ 8196;
481 /* Determine how many local master browsers to query */
482 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
484 : smbc_getOptionBrowseMaxLmbCount(context
));
486 u_info
= user_auth_info_init(frame
);
487 if (u_info
== NULL
) {
489 SAFE_FREE(dir
->fname
);
496 set_cmdline_auth_info_username(u_info
, user
);
497 set_cmdline_auth_info_password(u_info
, password
);
500 * We have server and share and path empty but options
501 * requesting that we scan all master browsers for their list
502 * of workgroups/domains. This implies that we must first try
503 * broadcast queries to find all master browsers, and if that
504 * doesn't work, then try our other methods which return only
505 * a single master browser.
509 status
= name_resolve_bcast(MSBROWSE
, 1, talloc_tos(),
511 if (!NT_STATUS_IS_OK(status
))
514 TALLOC_FREE(ip_list
);
516 if (!find_master_ip(workgroup
, &server_addr
)) {
519 SAFE_FREE(dir
->fname
);
527 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
528 talloc_tos(), &server_addr
,
529 sizeof(server_addr
));
530 if (ip_list
== NULL
) {
532 SAFE_FREE(dir
->fname
);
542 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
543 char addr
[INET6_ADDRSTRLEN
];
545 struct cli_state
*cli
= NULL
;
547 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
]);
548 DEBUG(99, ("Found master browser %d of %d: %s\n",
549 i
+1, MAX(count
, max_lmb_count
),
552 cli
= get_ipc_connect_master_ip(talloc_tos(),
556 /* cli == NULL is the master browser refused to talk or
557 could not be found */
562 workgroup
= talloc_strdup(frame
, wg_ptr
);
563 server
= talloc_strdup(frame
, smbXcli_conn_remote_name(cli
->conn
));
567 if (!workgroup
|| !server
) {
569 SAFE_FREE(dir
->fname
);
577 DEBUG(4, ("using workgroup %s %s\n",
581 * For each returned master browser IP address, get a
582 * connection to IPC$ on the server if we do not
583 * already have one, and determine the
584 * workgroups/domains that it knows about.
587 srv
= SMBC_server(frame
, context
, True
, server
, port
, "IPC$",
588 &workgroup
, &user
, &password
);
593 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
598 dir
->dir_type
= SMBC_WORKGROUP
;
600 /* Now, list the stuff ... */
602 if (!cli_NetServerEnum(srv
->cli
,
611 TALLOC_FREE(ip_list
);
614 * Server not an empty string ... Check the rest and see what
617 if (*share
== '\0') {
620 /* Should not have empty share with path */
622 SAFE_FREE(dir
->fname
);
626 errno
= EINVAL
+ 8197;
632 * We don't know if <server> is really a server name
633 * or is a workgroup/domain name. If we already have
634 * a server structure for it, we'll use it.
635 * Otherwise, check to see if <server><1D>,
636 * <server><1B>, or <server><20> translates. We check
637 * to see if <server> is an IP address first.
641 * See if we have an existing server. Do not
642 * establish a connection if one does not already
645 srv
= SMBC_server(frame
, context
, False
,
646 server
, port
, "IPC$",
647 &workgroup
, &user
, &password
);
650 * If no existing server and not an IP addr, look for
654 !is_ipaddress(server
) &&
655 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
656 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
658 * "server" is actually a workgroup name,
659 * not a server. Make this clear.
661 char *wgroup
= server
;
664 dir
->dir_type
= SMBC_SERVER
;
667 * Get the backup list ...
669 if (!name_status_find(wgroup
, 0, 0,
670 &rem_ss
, buserver
)) {
671 char addr
[INET6_ADDRSTRLEN
];
673 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
674 DEBUG(0,("Could not get name of "
675 "local/domain master browser "
676 "for workgroup %s from "
681 SAFE_FREE(dir
->fname
);
691 * Get a connection to IPC$ on the server if
692 * we do not already have one
694 srv
= SMBC_server(frame
, context
, True
,
695 buserver
, port
, "IPC$",
699 DEBUG(0, ("got no contact to IPC$\n"));
701 SAFE_FREE(dir
->fname
);
711 if (smbXcli_conn_protocol(srv
->cli
->conn
) > PROTOCOL_NT1
) {
713 SAFE_FREE(dir
->fname
);
720 /* Now, list the servers ... */
721 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
726 SAFE_FREE(dir
->fname
);
733 (resolve_name(server
, &rem_ss
, 0x20, false))) {
736 * If we hadn't found the server, get one now
739 srv
= SMBC_server(frame
, context
, True
,
740 server
, port
, "IPC$",
747 SAFE_FREE(dir
->fname
);
755 dir
->dir_type
= SMBC_FILE_SHARE
;
758 /* List the shares ... */
760 if (net_share_enum_rpc(
769 errno
= cli_errno(srv
->cli
);
771 SAFE_FREE(dir
->fname
);
779 /* Neither the workgroup nor server exists */
780 errno
= ECONNREFUSED
;
782 SAFE_FREE(dir
->fname
);
792 * The server and share are specified ... work from
796 struct cli_state
*targetcli
;
799 /* We connect to the server and list the directory */
800 dir
->dir_type
= SMBC_FILE_SHARE
;
802 srv
= SMBC_server(frame
, context
, True
, server
, port
, share
,
803 &workgroup
, &user
, &password
);
807 SAFE_FREE(dir
->fname
);
816 /* Now, list the files ... */
818 path_len
= strlen(path
);
819 path
= talloc_asprintf_append(path
, "\\*");
822 SAFE_FREE(dir
->fname
);
829 status
= cli_resolve_path(
830 frame
, "", context
->internal
->auth_info
,
831 srv
->cli
, path
, &targetcli
, &targetpath
);
832 if (!NT_STATUS_IS_OK(status
)) {
833 d_printf("Could not resolve %s\n", path
);
835 SAFE_FREE(dir
->fname
);
842 status
= cli_list(targetcli
, targetpath
,
843 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
844 dir_list_fn
, (void *)dir
);
845 if (!NT_STATUS_IS_OK(status
)) {
847 SAFE_FREE(dir
->fname
);
850 saved_errno
= SMBC_errno(context
, targetcli
);
852 if (saved_errno
== EINVAL
) {
854 * See if they asked to opendir
855 * something other than a directory.
856 * If so, the converted error value we
857 * got would have been EINVAL rather
860 path
[path_len
] = '\0'; /* restore original path */
862 if (SMBC_getatr(context
, srv
, path
,
864 NULL
, NULL
, NULL
, NULL
,
866 ! IS_DOS_DIR(mode
)) {
868 /* It is. Correct the error value */
869 saved_errno
= ENOTDIR
;
874 * If there was an error and the server is no
877 if (cli_is_error(targetcli
) &&
878 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
880 /* ... then remove it. */
881 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
884 * We could not remove the
885 * server completely, remove
886 * it from the cache so we
887 * will not get it again. It
888 * will be removed when the
889 * last file/dir is closed.
891 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
903 DLIST_ADD(context
->internal
->files
, dir
);
910 * Routine to close a directory
914 SMBC_closedir_ctx(SMBCCTX
*context
,
917 TALLOC_CTX
*frame
= talloc_stackframe();
919 if (!context
|| !context
->internal
->initialized
) {
925 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
931 remove_dir(dir
); /* Clean it up */
933 DLIST_REMOVE(context
->internal
->files
, dir
);
937 SAFE_FREE(dir
->fname
);
938 SAFE_FREE(dir
); /* Free the space too */
947 smbc_readdir_internal(SMBCCTX
* context
,
948 struct smbc_dirent
*dest
,
949 struct smbc_dirent
*src
,
952 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
954 /* url-encode the name. get back remaining buffer space */
956 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
958 /* We now know the name length */
959 dest
->namelen
= strlen(dest
->name
);
961 /* Save the pointer to the beginning of the comment */
962 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
964 /* Copy the comment */
965 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
966 dest
->comment
[max_namebuf_len
- 1] = '\0';
968 /* Save other fields */
969 dest
->smbc_type
= src
->smbc_type
;
970 dest
->commentlen
= strlen(dest
->comment
);
971 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
975 /* No encoding. Just copy the entry as is. */
976 memcpy(dest
, src
, src
->dirlen
);
977 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
983 * Routine to get a directory entry
987 SMBC_readdir_ctx(SMBCCTX
*context
,
991 struct smbc_dirent
*dirp
, *dirent
;
992 TALLOC_CTX
*frame
= talloc_stackframe();
994 /* Check that all is ok first ... */
996 if (!context
|| !context
->internal
->initialized
) {
999 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1005 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1008 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1014 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1017 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1023 if (!dir
->dir_next
) {
1028 dirent
= dir
->dir_next
->dirent
;
1037 dirp
= &context
->internal
->dirent
;
1038 maxlen
= sizeof(context
->internal
->_dirent_name
);
1040 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1042 dir
->dir_next
= dir
->dir_next
->next
;
1049 * Routine to get directory entries
1053 SMBC_getdents_ctx(SMBCCTX
*context
,
1055 struct smbc_dirent
*dirp
,
1061 char *ndir
= (char *)dirp
;
1062 struct smbc_dir_list
*dirlist
;
1063 TALLOC_CTX
*frame
= talloc_stackframe();
1065 /* Check that all is ok first ... */
1067 if (!context
|| !context
->internal
->initialized
) {
1075 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1083 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1092 * Now, retrieve the number of entries that will fit in what was passed
1093 * We have to figure out if the info is in the list, or we need to
1094 * send a request to the server to get the info.
1097 while ((dirlist
= dir
->dir_next
)) {
1098 struct smbc_dirent
*dirent
;
1099 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1101 if (!dirlist
->dirent
) {
1103 errno
= ENOENT
; /* Bad error */
1109 /* Do urlencoding of next entry, if so selected */
1110 dirent
= &context
->internal
->dirent
;
1111 maxlen
= sizeof(context
->internal
->_dirent_name
);
1112 smbc_readdir_internal(context
, dirent
,
1113 dirlist
->dirent
, maxlen
);
1115 reqd
= dirent
->dirlen
;
1119 if (rem
< count
) { /* We managed to copy something */
1126 else { /* Nothing copied ... */
1128 errno
= EINVAL
; /* Not enough space ... */
1136 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1138 currentEntry
->comment
= ¤tEntry
->name
[0] +
1139 dirent
->namelen
+ 1;
1144 /* Try and align the struct for the next entry
1145 on a valid pointer boundary by appending zeros */
1146 while((rem
> 0) && ((uintptr_t)ndir
& (sizeof(void*) - 1))) {
1150 currentEntry
->dirlen
++;
1153 dir
->dir_next
= dirlist
= dirlist
-> next
;
1166 * Routine to create a directory ...
1170 SMBC_mkdir_ctx(SMBCCTX
*context
,
1174 SMBCSRV
*srv
= NULL
;
1175 char *server
= NULL
;
1178 char *password
= NULL
;
1179 char *workgroup
= NULL
;
1181 char *targetpath
= NULL
;
1183 struct cli_state
*targetcli
= NULL
;
1184 TALLOC_CTX
*frame
= talloc_stackframe();
1187 if (!context
|| !context
->internal
->initialized
) {
1199 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1201 if (SMBC_parse_path(frame
,
1217 if (!user
|| user
[0] == (char)0) {
1218 user
= talloc_strdup(frame
, smbc_getUser(context
));
1226 srv
= SMBC_server(frame
, context
, True
,
1227 server
, port
, share
, &workgroup
, &user
, &password
);
1232 return -1; /* errno set by SMBC_server */
1236 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1237 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1238 srv
->cli
, path
, &targetcli
, &targetpath
);
1239 if (!NT_STATUS_IS_OK(status
)) {
1240 d_printf("Could not resolve %s\n", path
);
1245 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1247 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1248 errno
= SMBC_errno(context
, targetcli
);
1260 * Our list function simply checks to see if a directory is not empty
1264 rmdir_list_fn(const char *mnt
,
1265 struct file_info
*finfo
,
1269 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1270 strncmp(finfo
->name
, "..", 2) != 0) {
1271 bool *smbc_rmdir_dirempty
= (bool *)state
;
1272 *smbc_rmdir_dirempty
= false;
1274 return NT_STATUS_OK
;
1278 * Routine to remove a directory
1282 SMBC_rmdir_ctx(SMBCCTX
*context
,
1285 SMBCSRV
*srv
= NULL
;
1286 char *server
= NULL
;
1289 char *password
= NULL
;
1290 char *workgroup
= NULL
;
1292 char *targetpath
= NULL
;
1294 struct cli_state
*targetcli
= NULL
;
1295 TALLOC_CTX
*frame
= talloc_stackframe();
1298 if (!context
|| !context
->internal
->initialized
) {
1310 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1312 if (SMBC_parse_path(frame
,
1328 if (!user
|| user
[0] == (char)0) {
1329 user
= talloc_strdup(frame
, smbc_getUser(context
));
1337 srv
= SMBC_server(frame
, context
, True
,
1338 server
, port
, share
, &workgroup
, &user
, &password
);
1343 return -1; /* errno set by SMBC_server */
1347 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1348 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1349 srv
->cli
, path
, &targetcli
, &targetpath
);
1350 if (!NT_STATUS_IS_OK(status
)) {
1351 d_printf("Could not resolve %s\n", path
);
1356 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1358 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1360 errno
= SMBC_errno(context
, targetcli
);
1362 if (errno
== EACCES
) { /* Check if the dir empty or not */
1364 /* Local storage to avoid buffer overflows */
1366 bool smbc_rmdir_dirempty
= true;
1368 lpath
= talloc_asprintf(frame
, "%s\\*",
1376 status
= cli_list(targetcli
, lpath
,
1377 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1379 &smbc_rmdir_dirempty
);
1381 if (!NT_STATUS_IS_OK(status
)) {
1382 /* Fix errno to ignore latest error ... */
1383 DEBUG(5, ("smbc_rmdir: "
1384 "cli_list returned an error: %d\n",
1385 SMBC_errno(context
, targetcli
)));
1390 if (smbc_rmdir_dirempty
)
1408 * Routine to return the current directory position
1412 SMBC_telldir_ctx(SMBCCTX
*context
,
1415 TALLOC_CTX
*frame
= talloc_stackframe();
1417 if (!context
|| !context
->internal
->initialized
) {
1425 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1433 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1441 /* See if we're already at the end. */
1442 if (dir
->dir_next
== NULL
) {
1449 * We return the pointer here as the offset
1452 return (off_t
)(long)dir
->dir_next
->dirent
;
1456 * A routine to run down the list and see if the entry is OK
1459 static struct smbc_dir_list
*
1460 check_dir_ent(struct smbc_dir_list
*list
,
1461 struct smbc_dirent
*dirent
)
1464 /* Run down the list looking for what we want */
1468 struct smbc_dir_list
*tmp
= list
;
1472 if (tmp
->dirent
== dirent
)
1481 return NULL
; /* Not found, or an error */
1487 * Routine to seek on a directory
1491 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1495 long int l_offset
= offset
; /* Handle problems of size */
1496 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1497 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1498 TALLOC_CTX
*frame
= talloc_stackframe();
1500 if (!context
|| !context
->internal
->initialized
) {
1508 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1516 /* Now, check what we were passed and see if it is OK ... */
1518 if (dirent
== NULL
) { /* Seek to the begining of the list */
1520 dir
->dir_next
= dir
->dir_list
;
1526 if (offset
== -1) { /* Seek to the end of the list */
1527 dir
->dir_next
= NULL
;
1532 /* Now, run down the list and make sure that the entry is OK */
1533 /* This may need to be changed if we change the format of the list */
1535 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1536 errno
= EINVAL
; /* Bad entry */
1541 dir
->dir_next
= list_ent
;
1548 * Routine to fstat a dir
1552 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1557 if (!context
|| !context
->internal
->initialized
) {
1563 /* No code yet ... */
1568 SMBC_chmod_ctx(SMBCCTX
*context
,
1572 SMBCSRV
*srv
= NULL
;
1573 char *server
= NULL
;
1576 char *password
= NULL
;
1577 char *workgroup
= NULL
;
1578 char *targetpath
= NULL
;
1579 struct cli_state
*targetcli
= NULL
;
1583 TALLOC_CTX
*frame
= talloc_stackframe();
1586 if (!context
|| !context
->internal
->initialized
) {
1588 errno
= EINVAL
; /* Best I can think of ... */
1599 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1601 if (SMBC_parse_path(frame
,
1617 if (!user
|| user
[0] == (char)0) {
1618 user
= talloc_strdup(frame
, smbc_getUser(context
));
1626 srv
= SMBC_server(frame
, context
, True
,
1627 server
, port
, share
, &workgroup
, &user
, &password
);
1631 return -1; /* errno set by SMBC_server */
1634 /*d_printf(">>>unlink: resolving %s\n", path);*/
1635 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1636 srv
->cli
, path
, &targetcli
, &targetpath
);
1637 if (!NT_STATUS_IS_OK(status
)) {
1638 d_printf("Could not resolve %s\n", path
);
1646 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= FILE_ATTRIBUTE_READONLY
;
1647 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= FILE_ATTRIBUTE_ARCHIVE
;
1648 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= FILE_ATTRIBUTE_SYSTEM
;
1649 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= FILE_ATTRIBUTE_HIDDEN
;
1651 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1652 errno
= SMBC_errno(context
, targetcli
);
1662 SMBC_utimes_ctx(SMBCCTX
*context
,
1664 struct timeval
*tbuf
)
1666 SMBCSRV
*srv
= NULL
;
1667 char *server
= NULL
;
1670 char *password
= NULL
;
1671 char *workgroup
= NULL
;
1676 TALLOC_CTX
*frame
= talloc_stackframe();
1678 if (!context
|| !context
->internal
->initialized
) {
1680 errno
= EINVAL
; /* Best I can think of ... */
1692 access_time
= write_time
= time(NULL
);
1694 access_time
= tbuf
[0].tv_sec
;
1695 write_time
= tbuf
[1].tv_sec
;
1703 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1704 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1705 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1709 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1710 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1711 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1715 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1716 fname
, atimebuf
, mtimebuf
);
1719 if (SMBC_parse_path(frame
,
1735 if (!user
|| user
[0] == (char)0) {
1736 user
= talloc_strdup(frame
, smbc_getUser(context
));
1744 srv
= SMBC_server(frame
, context
, True
,
1745 server
, port
, share
, &workgroup
, &user
, &password
);
1749 return -1; /* errno set by SMBC_server */
1752 if (!SMBC_setatr(context
, srv
, path
,
1753 0, access_time
, write_time
, 0, 0)) {
1755 return -1; /* errno set by SMBC_setatr */
1763 * Routine to unlink() a file
1767 SMBC_unlink_ctx(SMBCCTX
*context
,
1770 char *server
= NULL
;
1773 char *password
= NULL
;
1774 char *workgroup
= NULL
;
1776 char *targetpath
= NULL
;
1778 struct cli_state
*targetcli
= NULL
;
1779 SMBCSRV
*srv
= NULL
;
1780 TALLOC_CTX
*frame
= talloc_stackframe();
1783 if (!context
|| !context
->internal
->initialized
) {
1785 errno
= EINVAL
; /* Best I can think of ... */
1798 if (SMBC_parse_path(frame
,
1814 if (!user
|| user
[0] == (char)0) {
1815 user
= talloc_strdup(frame
, smbc_getUser(context
));
1823 srv
= SMBC_server(frame
, context
, True
,
1824 server
, port
, share
, &workgroup
, &user
, &password
);
1828 return -1; /* SMBC_server sets errno */
1832 /*d_printf(">>>unlink: resolving %s\n", path);*/
1833 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1834 srv
->cli
, path
, &targetcli
, &targetpath
);
1835 if (!NT_STATUS_IS_OK(status
)) {
1836 d_printf("Could not resolve %s\n", path
);
1841 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1843 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
1845 errno
= SMBC_errno(context
, targetcli
);
1847 if (errno
== EACCES
) { /* Check if the file is a directory */
1852 struct timespec write_time_ts
;
1853 struct timespec access_time_ts
;
1854 struct timespec change_time_ts
;
1857 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1864 /* Hmmm, bad error ... What? */
1866 errno
= SMBC_errno(context
, targetcli
);
1873 if (IS_DOS_DIR(mode
))
1876 errno
= saverr
; /* Restore this */
1887 return 0; /* Success ... */
1892 * Routine to rename() a file
1896 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1901 char *server1
= NULL
;
1902 char *share1
= NULL
;
1903 char *server2
= NULL
;
1904 char *share2
= NULL
;
1907 char *password1
= NULL
;
1908 char *password2
= NULL
;
1909 char *workgroup
= NULL
;
1912 char *targetpath1
= NULL
;
1913 char *targetpath2
= NULL
;
1914 struct cli_state
*targetcli1
= NULL
;
1915 struct cli_state
*targetcli2
= NULL
;
1916 SMBCSRV
*srv
= NULL
;
1919 TALLOC_CTX
*frame
= talloc_stackframe();
1922 if (!ocontext
|| !ncontext
||
1923 !ocontext
->internal
->initialized
||
1924 !ncontext
->internal
->initialized
) {
1926 errno
= EINVAL
; /* Best I can think of ... */
1931 if (!oname
|| !nname
) {
1937 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1939 if (SMBC_parse_path(frame
,
1955 if (!user1
|| user1
[0] == (char)0) {
1956 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1964 if (SMBC_parse_path(frame
,
1980 if (!user2
|| user2
[0] == (char)0) {
1981 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1989 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1990 strcmp(user1
, user2
)) {
1991 /* Can't rename across file systems, or users?? */
1997 srv
= SMBC_server(frame
, ocontext
, True
,
1998 server1
, port1
, share1
, &workgroup
, &user1
, &password1
);
2005 /* set the credentials to make DFS work */
2006 smbc_set_credentials_with_fallback(ocontext
,
2011 /*d_printf(">>>rename: resolving %s\n", path1);*/
2012 status
= cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
2013 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
2014 if (!NT_STATUS_IS_OK(status
)) {
2015 d_printf("Could not resolve %s\n", path1
);
2021 /* set the credentials to make DFS work */
2022 smbc_set_credentials_with_fallback(ncontext
,
2027 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2028 /*d_printf(">>>rename: resolving %s\n", path2);*/
2029 status
= cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
2030 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
2031 if (!NT_STATUS_IS_OK(status
)) {
2032 d_printf("Could not resolve %s\n", path2
);
2037 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2039 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2040 strcmp(targetcli1
->share
, targetcli2
->share
))
2042 /* can't rename across file systems */
2048 if (!NT_STATUS_IS_OK(
2049 cli_rename(targetcli1
, targetpath1
, targetpath2
, false))) {
2050 int eno
= SMBC_errno(ocontext
, targetcli1
);
2052 if (eno
!= EEXIST
||
2053 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
,
2054 FILE_ATTRIBUTE_SYSTEM
|
2055 FILE_ATTRIBUTE_HIDDEN
)) ||
2056 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
,
2057 targetpath2
, false))) {
2067 return 0; /* Success */
2070 struct smbc_notify_cb_state
{
2071 struct tevent_context
*ev
;
2072 struct cli_state
*cli
;
2075 uint32_t completion_filter
;
2076 unsigned callback_timeout_ms
;
2077 smbc_notify_callback_fn cb
;
2081 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
);
2082 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
);
2084 static struct tevent_req
*smbc_notify_cb_send(
2085 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
2086 uint16_t fnum
, bool recursive
, uint32_t completion_filter
,
2087 unsigned callback_timeout_ms
,
2088 smbc_notify_callback_fn cb
, void *private_data
)
2090 struct tevent_req
*req
, *subreq
;
2091 struct smbc_notify_cb_state
*state
;
2093 req
= tevent_req_create(mem_ctx
, &state
, struct smbc_notify_cb_state
);
2100 state
->recursive
= recursive
;
2101 state
->completion_filter
= completion_filter
;
2102 state
->callback_timeout_ms
= callback_timeout_ms
;
2104 state
->private_data
= private_data
;
2106 subreq
= cli_notify_send(
2107 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2108 state
->completion_filter
, state
->recursive
);
2109 if (tevent_req_nomem(subreq
, req
)) {
2110 return tevent_req_post(req
, ev
);
2112 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2114 if (state
->callback_timeout_ms
== 0) {
2118 subreq
= tevent_wakeup_send(
2120 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2121 state
->callback_timeout_ms
*1000));
2122 if (tevent_req_nomem(subreq
, req
)) {
2123 return tevent_req_post(req
, ev
);
2125 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2130 static void smbc_notify_cb_got_changes(struct tevent_req
*subreq
)
2132 struct tevent_req
*req
= tevent_req_callback_data(
2133 subreq
, struct tevent_req
);
2134 struct smbc_notify_cb_state
*state
= tevent_req_data(
2135 req
, struct smbc_notify_cb_state
);
2136 uint32_t num_changes
;
2137 struct notify_change
*changes
;
2141 status
= cli_notify_recv(subreq
, state
, &num_changes
, &changes
);
2142 TALLOC_FREE(subreq
);
2143 if (tevent_req_nterror(req
, status
)) {
2148 struct smbc_notify_callback_action actions
[num_changes
];
2151 for (i
=0; i
<num_changes
; i
++) {
2152 actions
[i
].action
= changes
[i
].action
;
2153 actions
[i
].filename
= changes
[i
].name
;
2156 cb_ret
= state
->cb(actions
, num_changes
, state
->private_data
);
2159 TALLOC_FREE(changes
);
2162 tevent_req_done(req
);
2166 subreq
= cli_notify_send(
2167 state
, state
->ev
, state
->cli
, state
->fnum
, 1000,
2168 state
->completion_filter
, state
->recursive
);
2169 if (tevent_req_nomem(subreq
, req
)) {
2172 tevent_req_set_callback(subreq
, smbc_notify_cb_got_changes
, req
);
2175 static void smbc_notify_cb_timedout(struct tevent_req
*subreq
)
2177 struct tevent_req
*req
= tevent_req_callback_data(
2178 subreq
, struct tevent_req
);
2179 struct smbc_notify_cb_state
*state
= tevent_req_data(
2180 req
, struct smbc_notify_cb_state
);
2184 ok
= tevent_wakeup_recv(subreq
);
2185 TALLOC_FREE(subreq
);
2187 tevent_req_oom(req
);
2191 cb_ret
= state
->cb(NULL
, 0, state
->private_data
);
2193 tevent_req_done(req
);
2197 subreq
= tevent_wakeup_send(
2199 tevent_timeval_current_ofs(state
->callback_timeout_ms
/1000,
2200 state
->callback_timeout_ms
*1000));
2201 if (tevent_req_nomem(subreq
, req
)) {
2204 tevent_req_set_callback(subreq
, smbc_notify_cb_timedout
, req
);
2207 static NTSTATUS
smbc_notify_cb_recv(struct tevent_req
*req
)
2209 return tevent_req_simple_recv_ntstatus(req
);
2212 static NTSTATUS
smbc_notify_cb(struct cli_state
*cli
, uint16_t fnum
,
2213 bool recursive
, uint32_t completion_filter
,
2214 unsigned callback_timeout_ms
,
2215 smbc_notify_callback_fn cb
, void *private_data
)
2217 TALLOC_CTX
*frame
= talloc_stackframe();
2218 struct tevent_context
*ev
;
2219 struct tevent_req
*req
;
2220 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2222 ev
= samba_tevent_context_init(frame
);
2226 req
= smbc_notify_cb_send(frame
, ev
, cli
, fnum
, recursive
,
2228 callback_timeout_ms
, cb
, private_data
);
2232 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2235 status
= smbc_notify_cb_recv(req
);
2243 SMBC_notify_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, smbc_bool recursive
,
2244 uint32_t completion_filter
, unsigned callback_timeout_ms
,
2245 smbc_notify_callback_fn cb
, void *private_data
)
2247 TALLOC_CTX
*frame
= talloc_stackframe();
2248 struct cli_state
*cli
;
2249 char *server
= NULL
;
2252 char *password
= NULL
;
2253 char *options
= NULL
;
2254 char *workgroup
= NULL
;
2260 if ((context
== NULL
) || !context
->internal
->initialized
) {
2265 if ((dir
== NULL
) ||
2266 !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
2272 if (SMBC_parse_path(frame
,
2283 DEBUG(4, ("no valid path\n"));
2285 errno
= EINVAL
+ 8194;
2289 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2290 "path='%s' options='%s'\n",
2291 dir
->fname
, server
, share
, path
, options
));
2293 DEBUG(4, ("%s(%p, %d, %"PRIu32
")\n", __func__
, dir
,
2294 (int)recursive
, completion_filter
));
2296 cli
= dir
->srv
->cli
;
2297 status
= cli_ntcreate(
2298 cli
, path
, 0, FILE_READ_DATA
, 0,
2299 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
2300 FILE_OPEN
, 0, 0, &fnum
, NULL
);
2301 if (!NT_STATUS_IS_OK(status
)) {
2302 int err
= SMBC_errno(context
, cli
);
2308 status
= smbc_notify_cb(cli
, fnum
, recursive
!= 0, completion_filter
,
2309 callback_timeout_ms
, cb
, private_data
);
2310 if (!NT_STATUS_IS_OK(status
)) {
2311 int err
= SMBC_errno(context
, cli
);
2312 cli_close(cli
, fnum
);
2318 cli_close(cli
, fnum
);