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"
36 * Routine to open a directory
37 * We accept the URL syntax explained in SMBC_parse_path(), above.
41 remove_dir(SMBCFILE
*dir
)
43 struct smbc_dir_list
*d
,*f
;
55 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
60 add_dirent(SMBCFILE
*dir
,
65 struct smbc_dirent
*dirent
;
67 int name_length
= (name
== NULL
? 0 : strlen(name
));
68 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
71 * Allocate space for the dirent, which must be increased by the
72 * size of the name and the comment and 1 each for the null terminator.
75 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
77 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
81 dir
->dir_error
= ENOMEM
;
88 if (dir
->dir_list
== NULL
) {
90 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
94 dir
->dir_error
= ENOMEM
;
98 ZERO_STRUCTP(dir
->dir_list
);
100 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
104 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
106 if (!dir
->dir_end
->next
) {
109 dir
->dir_error
= ENOMEM
;
113 ZERO_STRUCTP(dir
->dir_end
->next
);
115 dir
->dir_end
= dir
->dir_end
->next
;
118 dir
->dir_end
->next
= NULL
;
119 dir
->dir_end
->dirent
= dirent
;
121 dirent
->smbc_type
= type
;
122 dirent
->namelen
= name_length
;
123 dirent
->commentlen
= comment_len
;
124 dirent
->dirlen
= size
;
127 * dirent->namelen + 1 includes the null (no null termination needed)
128 * Ditto for dirent->commentlen.
129 * The space for the two null bytes was allocated.
131 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
132 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
133 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
140 list_unique_wg_fn(const char *name
,
145 SMBCFILE
*dir
= (SMBCFILE
*)state
;
146 struct smbc_dir_list
*dir_list
;
147 struct smbc_dirent
*dirent
;
151 dirent_type
= dir
->dir_type
;
153 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
154 /* An error occurred, what do we do? */
155 /* FIXME: Add some code here */
156 /* Change cli_NetServerEnum to take a fn
157 returning NTSTATUS... JRA. */
160 /* Point to the one just added */
161 dirent
= dir
->dir_end
->dirent
;
163 /* See if this was a duplicate */
164 for (dir_list
= dir
->dir_list
;
165 dir_list
!= dir
->dir_end
;
166 dir_list
= dir_list
->next
) {
168 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
169 /* Duplicate. End end of list need to be removed. */
173 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
174 /* Found the end of the list. Remove it. */
175 dir
->dir_end
= dir_list
;
176 free(dir_list
->next
);
178 dir_list
->next
= NULL
;
185 list_fn(const char *name
,
190 SMBCFILE
*dir
= (SMBCFILE
*)state
;
194 * We need to process the type a little ...
196 * Disk share = 0x00000000
197 * Print share = 0x00000001
198 * Comms share = 0x00000002 (obsolete?)
199 * IPC$ share = 0x00000003
201 * administrative shares:
202 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
205 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
209 dirent_type
= SMBC_FILE_SHARE
;
213 dirent_type
= SMBC_PRINTER_SHARE
;
217 dirent_type
= SMBC_COMMS_SHARE
;
222 dirent_type
= SMBC_IPC_SHARE
;
226 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
231 dirent_type
= dir
->dir_type
;
234 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
235 /* An error occurred, what do we do? */
236 /* FIXME: Add some code here */
237 /* Change cli_NetServerEnum to take a fn
238 returning NTSTATUS... JRA. */
243 dir_list_fn(const char *mnt
,
244 struct file_info
*finfo
,
249 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
250 (finfo
->mode
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
251 SMBCFILE
*dir
= (SMBCFILE
*)state
;
252 return map_nt_error_from_unix(dir
->dir_error
);
258 net_share_enum_rpc(struct cli_state
*cli
,
259 void (*fn
)(const char *name
,
267 uint32 preferred_len
= 0xffffffff;
269 struct srvsvc_NetShareInfoCtr info_ctr
;
270 struct srvsvc_NetShareCtr1 ctr1
;
272 fstring comment
= "";
273 struct rpc_pipe_client
*pipe_hnd
= NULL
;
275 uint32_t resume_handle
= 0;
276 uint32_t total_entries
= 0;
277 struct dcerpc_binding_handle
*b
;
279 /* Open the server service pipe */
280 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
282 if (!NT_STATUS_IS_OK(nt_status
)) {
283 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
287 ZERO_STRUCT(info_ctr
);
291 info_ctr
.ctr
.ctr1
= &ctr1
;
293 b
= pipe_hnd
->binding_handle
;
295 /* Issue the NetShareEnum RPC call and retrieve the response */
296 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
304 /* Was it successful? */
305 if (!NT_STATUS_IS_OK(nt_status
)) {
306 /* Nope. Go clean up. */
307 result
= ntstatus_to_werror(nt_status
);
311 if (!W_ERROR_IS_OK(result
)) {
312 /* Nope. Go clean up. */
316 if (total_entries
== 0) {
317 /* Nope. Go clean up. */
318 result
= WERR_GENERAL_FAILURE
;
322 /* For each returned entry... */
323 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
325 /* pull out the share name */
326 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
328 /* pull out the share's comment */
329 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
331 /* Get the type value */
332 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
334 /* Add this share to the list */
335 (*fn
)(name
, type
, comment
, state
);
339 /* Close the server service pipe */
340 TALLOC_FREE(pipe_hnd
);
342 /* Tell 'em if it worked */
343 return W_ERROR_IS_OK(result
) ? 0 : -1;
348 * Verify that the options specified in a URL are valid
351 SMBC_check_options(char *server
,
356 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
357 "path='%s' options='%s'\n",
358 server
, share
, path
, options
));
360 /* No options at all is always ok */
361 if (! *options
) return 0;
363 /* Currently, we don't support any options. */
369 SMBC_opendir_ctx(SMBCCTX
*context
,
376 char *password
= NULL
;
377 char *options
= NULL
;
378 char *workgroup
= NULL
;
383 SMBCFILE
*dir
= NULL
;
384 struct sockaddr_storage rem_ss
;
385 TALLOC_CTX
*frame
= talloc_stackframe();
387 if (!context
|| !context
->internal
->initialized
) {
388 DEBUG(4, ("no valid context\n"));
390 errno
= EINVAL
+ 8192;
396 DEBUG(4, ("no valid fname\n"));
398 errno
= EINVAL
+ 8193;
402 if (SMBC_parse_path(frame
,
412 DEBUG(4, ("no valid path\n"));
414 errno
= EINVAL
+ 8194;
418 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
419 "path='%s' options='%s'\n",
420 fname
, server
, share
, path
, options
));
422 /* Ensure the options are valid */
423 if (SMBC_check_options(server
, share
, path
, options
)) {
424 DEBUG(4, ("unacceptable options (%s)\n", options
));
426 errno
= EINVAL
+ 8195;
430 if (!user
|| user
[0] == (char)0) {
431 user
= talloc_strdup(frame
, smbc_getUser(context
));
439 dir
= SMB_MALLOC_P(SMBCFILE
);
450 dir
->fname
= SMB_STRDUP(fname
);
454 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
456 if (server
[0] == (char)0) {
461 struct sockaddr_storage
*ip_list
;
462 struct sockaddr_storage server_addr
;
463 struct user_auth_info u_info
;
466 if (share
[0] != (char)0 || path
[0] != (char)0) {
469 SAFE_FREE(dir
->fname
);
473 errno
= EINVAL
+ 8196;
477 /* Determine how many local master browsers to query */
478 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
480 : smbc_getOptionBrowseMaxLmbCount(context
));
482 memset(&u_info
, '\0', sizeof(u_info
));
483 u_info
.username
= talloc_strdup(frame
,user
);
484 u_info
.password
= talloc_strdup(frame
,password
);
485 if (!u_info
.username
|| !u_info
.password
) {
487 SAFE_FREE(dir
->fname
);
495 * We have server and share and path empty but options
496 * requesting that we scan all master browsers for their list
497 * of workgroups/domains. This implies that we must first try
498 * broadcast queries to find all master browsers, and if that
499 * doesn't work, then try our other methods which return only
500 * a single master browser.
504 status
= name_resolve_bcast(MSBROWSE
, 1, talloc_tos(),
506 if (!NT_STATUS_IS_OK(status
))
509 TALLOC_FREE(ip_list
);
511 if (!find_master_ip(workgroup
, &server_addr
)) {
514 SAFE_FREE(dir
->fname
);
522 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
523 talloc_tos(), &server_addr
,
524 sizeof(server_addr
));
525 if (ip_list
== NULL
) {
527 SAFE_FREE(dir
->fname
);
537 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
538 char addr
[INET6_ADDRSTRLEN
];
540 struct cli_state
*cli
= NULL
;
542 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
]);
543 DEBUG(99, ("Found master browser %d of %d: %s\n",
544 i
+1, MAX(count
, max_lmb_count
),
547 cli
= get_ipc_connect_master_ip(talloc_tos(),
551 /* cli == NULL is the master browser refused to talk or
552 could not be found */
557 workgroup
= talloc_strdup(frame
, wg_ptr
);
558 server
= talloc_strdup(frame
, smbXcli_conn_remote_name(cli
->conn
));
562 if (!workgroup
|| !server
) {
564 SAFE_FREE(dir
->fname
);
572 DEBUG(4, ("using workgroup %s %s\n",
576 * For each returned master browser IP address, get a
577 * connection to IPC$ on the server if we do not
578 * already have one, and determine the
579 * workgroups/domains that it knows about.
582 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
583 &workgroup
, &user
, &password
);
589 dir
->dir_type
= SMBC_WORKGROUP
;
591 /* Now, list the stuff ... */
593 if (!cli_NetServerEnum(srv
->cli
,
602 TALLOC_FREE(ip_list
);
605 * Server not an empty string ... Check the rest and see what
608 if (*share
== '\0') {
611 /* Should not have empty share with path */
613 SAFE_FREE(dir
->fname
);
617 errno
= EINVAL
+ 8197;
623 * We don't know if <server> is really a server name
624 * or is a workgroup/domain name. If we already have
625 * a server structure for it, we'll use it.
626 * Otherwise, check to see if <server><1D>,
627 * <server><1B>, or <server><20> translates. We check
628 * to see if <server> is an IP address first.
632 * See if we have an existing server. Do not
633 * establish a connection if one does not already
636 srv
= SMBC_server(frame
, context
, False
,
638 &workgroup
, &user
, &password
);
641 * If no existing server and not an IP addr, look for
645 !is_ipaddress(server
) &&
646 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
647 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
649 * "server" is actually a workgroup name,
650 * not a server. Make this clear.
652 char *wgroup
= server
;
655 dir
->dir_type
= SMBC_SERVER
;
658 * Get the backup list ...
660 if (!name_status_find(wgroup
, 0, 0,
661 &rem_ss
, buserver
)) {
662 char addr
[INET6_ADDRSTRLEN
];
664 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
665 DEBUG(0,("Could not get name of "
666 "local/domain master browser "
667 "for workgroup %s from "
672 SAFE_FREE(dir
->fname
);
682 * Get a connection to IPC$ on the server if
683 * we do not already have one
685 srv
= SMBC_server(frame
, context
, True
,
690 DEBUG(0, ("got no contact to IPC$\n"));
692 SAFE_FREE(dir
->fname
);
702 /* Now, list the servers ... */
703 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
708 SAFE_FREE(dir
->fname
);
715 (resolve_name(server
, &rem_ss
, 0x20, false))) {
718 * If we hadn't found the server, get one now
721 srv
= SMBC_server(frame
, context
, True
,
729 SAFE_FREE(dir
->fname
);
737 dir
->dir_type
= SMBC_FILE_SHARE
;
740 /* List the shares ... */
742 if (net_share_enum_rpc(
751 errno
= cli_errno(srv
->cli
);
753 SAFE_FREE(dir
->fname
);
761 /* Neither the workgroup nor server exists */
762 errno
= ECONNREFUSED
;
764 SAFE_FREE(dir
->fname
);
774 * The server and share are specified ... work from
778 struct cli_state
*targetcli
;
781 /* We connect to the server and list the directory */
782 dir
->dir_type
= SMBC_FILE_SHARE
;
784 srv
= SMBC_server(frame
, context
, True
, server
, share
,
785 &workgroup
, &user
, &password
);
789 SAFE_FREE(dir
->fname
);
798 /* Now, list the files ... */
800 p
= path
+ strlen(path
);
801 path
= talloc_asprintf_append(path
, "\\*");
804 SAFE_FREE(dir
->fname
);
811 status
= cli_resolve_path(
812 frame
, "", context
->internal
->auth_info
,
813 srv
->cli
, path
, &targetcli
, &targetpath
);
814 if (!NT_STATUS_IS_OK(status
)) {
815 d_printf("Could not resolve %s\n", path
);
817 SAFE_FREE(dir
->fname
);
824 status
= cli_list(targetcli
, targetpath
,
825 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
826 dir_list_fn
, (void *)dir
);
827 if (!NT_STATUS_IS_OK(status
)) {
829 SAFE_FREE(dir
->fname
);
832 saved_errno
= SMBC_errno(context
, targetcli
);
834 if (saved_errno
== EINVAL
) {
836 * See if they asked to opendir
837 * something other than a directory.
838 * If so, the converted error value we
839 * got would have been EINVAL rather
842 *p
= '\0'; /* restore original path */
844 if (SMBC_getatr(context
, srv
, path
,
846 NULL
, NULL
, NULL
, NULL
,
848 ! IS_DOS_DIR(mode
)) {
850 /* It is. Correct the error value */
851 saved_errno
= ENOTDIR
;
856 * If there was an error and the server is no
859 if (cli_is_error(targetcli
) &&
860 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
862 /* ... then remove it. */
863 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
866 * We could not remove the
867 * server completely, remove
868 * it from the cache so we
869 * will not get it again. It
870 * will be removed when the
871 * last file/dir is closed.
873 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
885 DLIST_ADD(context
->internal
->files
, dir
);
892 * Routine to close a directory
896 SMBC_closedir_ctx(SMBCCTX
*context
,
899 TALLOC_CTX
*frame
= talloc_stackframe();
901 if (!context
|| !context
->internal
->initialized
) {
907 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
913 remove_dir(dir
); /* Clean it up */
915 DLIST_REMOVE(context
->internal
->files
, dir
);
919 SAFE_FREE(dir
->fname
);
920 SAFE_FREE(dir
); /* Free the space too */
929 smbc_readdir_internal(SMBCCTX
* context
,
930 struct smbc_dirent
*dest
,
931 struct smbc_dirent
*src
,
934 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
936 /* url-encode the name. get back remaining buffer space */
938 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
940 /* We now know the name length */
941 dest
->namelen
= strlen(dest
->name
);
943 /* Save the pointer to the beginning of the comment */
944 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
946 /* Copy the comment */
947 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
948 dest
->comment
[max_namebuf_len
- 1] = '\0';
950 /* Save other fields */
951 dest
->smbc_type
= src
->smbc_type
;
952 dest
->commentlen
= strlen(dest
->comment
);
953 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
957 /* No encoding. Just copy the entry as is. */
958 memcpy(dest
, src
, src
->dirlen
);
959 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
965 * Routine to get a directory entry
969 SMBC_readdir_ctx(SMBCCTX
*context
,
973 struct smbc_dirent
*dirp
, *dirent
;
974 TALLOC_CTX
*frame
= talloc_stackframe();
976 /* Check that all is ok first ... */
978 if (!context
|| !context
->internal
->initialized
) {
981 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
987 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
990 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
996 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
999 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1005 if (!dir
->dir_next
) {
1010 dirent
= dir
->dir_next
->dirent
;
1019 dirp
= &context
->internal
->dirent
;
1020 maxlen
= sizeof(context
->internal
->_dirent_name
);
1022 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1024 dir
->dir_next
= dir
->dir_next
->next
;
1031 * Routine to get directory entries
1035 SMBC_getdents_ctx(SMBCCTX
*context
,
1037 struct smbc_dirent
*dirp
,
1043 char *ndir
= (char *)dirp
;
1044 struct smbc_dir_list
*dirlist
;
1045 TALLOC_CTX
*frame
= talloc_stackframe();
1047 /* Check that all is ok first ... */
1049 if (!context
|| !context
->internal
->initialized
) {
1057 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1065 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1074 * Now, retrieve the number of entries that will fit in what was passed
1075 * We have to figure out if the info is in the list, or we need to
1076 * send a request to the server to get the info.
1079 while ((dirlist
= dir
->dir_next
)) {
1080 struct smbc_dirent
*dirent
;
1081 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1083 if (!dirlist
->dirent
) {
1085 errno
= ENOENT
; /* Bad error */
1091 /* Do urlencoding of next entry, if so selected */
1092 dirent
= &context
->internal
->dirent
;
1093 maxlen
= sizeof(context
->internal
->_dirent_name
);
1094 smbc_readdir_internal(context
, dirent
,
1095 dirlist
->dirent
, maxlen
);
1097 reqd
= dirent
->dirlen
;
1101 if (rem
< count
) { /* We managed to copy something */
1108 else { /* Nothing copied ... */
1110 errno
= EINVAL
; /* Not enough space ... */
1118 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1120 currentEntry
->comment
= ¤tEntry
->name
[0] +
1121 dirent
->namelen
+ 1;
1126 /* Try and align the struct for the next entry
1127 on a valid pointer boundary by appending zeros */
1128 while((rem
> 0) && ((unsigned long long)ndir
& (sizeof(void*) - 1))) {
1132 currentEntry
->dirlen
++;
1135 dir
->dir_next
= dirlist
= dirlist
-> next
;
1148 * Routine to create a directory ...
1152 SMBC_mkdir_ctx(SMBCCTX
*context
,
1156 SMBCSRV
*srv
= NULL
;
1157 char *server
= NULL
;
1160 char *password
= NULL
;
1161 char *workgroup
= NULL
;
1163 char *targetpath
= NULL
;
1164 struct cli_state
*targetcli
= NULL
;
1165 TALLOC_CTX
*frame
= talloc_stackframe();
1168 if (!context
|| !context
->internal
->initialized
) {
1180 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1182 if (SMBC_parse_path(frame
,
1197 if (!user
|| user
[0] == (char)0) {
1198 user
= talloc_strdup(frame
, smbc_getUser(context
));
1206 srv
= SMBC_server(frame
, context
, True
,
1207 server
, share
, &workgroup
, &user
, &password
);
1212 return -1; /* errno set by SMBC_server */
1216 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1217 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1218 srv
->cli
, path
, &targetcli
, &targetpath
);
1219 if (!NT_STATUS_IS_OK(status
)) {
1220 d_printf("Could not resolve %s\n", path
);
1225 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1227 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1228 errno
= SMBC_errno(context
, targetcli
);
1240 * Our list function simply checks to see if a directory is not empty
1244 rmdir_list_fn(const char *mnt
,
1245 struct file_info
*finfo
,
1249 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1250 strncmp(finfo
->name
, "..", 2) != 0) {
1251 bool *smbc_rmdir_dirempty
= (bool *)state
;
1252 *smbc_rmdir_dirempty
= false;
1254 return NT_STATUS_OK
;
1258 * Routine to remove a directory
1262 SMBC_rmdir_ctx(SMBCCTX
*context
,
1265 SMBCSRV
*srv
= NULL
;
1266 char *server
= NULL
;
1269 char *password
= NULL
;
1270 char *workgroup
= NULL
;
1272 char *targetpath
= NULL
;
1273 struct cli_state
*targetcli
= NULL
;
1274 TALLOC_CTX
*frame
= talloc_stackframe();
1277 if (!context
|| !context
->internal
->initialized
) {
1289 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1291 if (SMBC_parse_path(frame
,
1306 if (!user
|| user
[0] == (char)0) {
1307 user
= talloc_strdup(frame
, smbc_getUser(context
));
1315 srv
= SMBC_server(frame
, context
, True
,
1316 server
, share
, &workgroup
, &user
, &password
);
1321 return -1; /* errno set by SMBC_server */
1325 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1326 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1327 srv
->cli
, path
, &targetcli
, &targetpath
);
1328 if (!NT_STATUS_IS_OK(status
)) {
1329 d_printf("Could not resolve %s\n", path
);
1334 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1336 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1338 errno
= SMBC_errno(context
, targetcli
);
1340 if (errno
== EACCES
) { /* Check if the dir empty or not */
1342 /* Local storage to avoid buffer overflows */
1344 bool smbc_rmdir_dirempty
= true;
1346 lpath
= talloc_asprintf(frame
, "%s\\*",
1354 status
= cli_list(targetcli
, lpath
,
1355 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1357 &smbc_rmdir_dirempty
);
1359 if (!NT_STATUS_IS_OK(status
)) {
1360 /* Fix errno to ignore latest error ... */
1361 DEBUG(5, ("smbc_rmdir: "
1362 "cli_list returned an error: %d\n",
1363 SMBC_errno(context
, targetcli
)));
1368 if (smbc_rmdir_dirempty
)
1386 * Routine to return the current directory position
1390 SMBC_telldir_ctx(SMBCCTX
*context
,
1393 TALLOC_CTX
*frame
= talloc_stackframe();
1395 if (!context
|| !context
->internal
->initialized
) {
1403 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1411 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1419 /* See if we're already at the end. */
1420 if (dir
->dir_next
== NULL
) {
1427 * We return the pointer here as the offset
1430 return (off_t
)(long)dir
->dir_next
->dirent
;
1434 * A routine to run down the list and see if the entry is OK
1437 static struct smbc_dir_list
*
1438 check_dir_ent(struct smbc_dir_list
*list
,
1439 struct smbc_dirent
*dirent
)
1442 /* Run down the list looking for what we want */
1446 struct smbc_dir_list
*tmp
= list
;
1450 if (tmp
->dirent
== dirent
)
1459 return NULL
; /* Not found, or an error */
1465 * Routine to seek on a directory
1469 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1473 long int l_offset
= offset
; /* Handle problems of size */
1474 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1475 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1476 TALLOC_CTX
*frame
= talloc_stackframe();
1478 if (!context
|| !context
->internal
->initialized
) {
1486 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1494 /* Now, check what we were passed and see if it is OK ... */
1496 if (dirent
== NULL
) { /* Seek to the begining of the list */
1498 dir
->dir_next
= dir
->dir_list
;
1504 if (offset
== -1) { /* Seek to the end of the list */
1505 dir
->dir_next
= NULL
;
1510 /* Now, run down the list and make sure that the entry is OK */
1511 /* This may need to be changed if we change the format of the list */
1513 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1514 errno
= EINVAL
; /* Bad entry */
1519 dir
->dir_next
= list_ent
;
1526 * Routine to fstat a dir
1530 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1535 if (!context
|| !context
->internal
->initialized
) {
1541 /* No code yet ... */
1546 SMBC_chmod_ctx(SMBCCTX
*context
,
1550 SMBCSRV
*srv
= NULL
;
1551 char *server
= NULL
;
1554 char *password
= NULL
;
1555 char *workgroup
= NULL
;
1556 char *targetpath
= NULL
;
1557 struct cli_state
*targetcli
= NULL
;
1560 TALLOC_CTX
*frame
= talloc_stackframe();
1563 if (!context
|| !context
->internal
->initialized
) {
1565 errno
= EINVAL
; /* Best I can think of ... */
1576 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1578 if (SMBC_parse_path(frame
,
1593 if (!user
|| user
[0] == (char)0) {
1594 user
= talloc_strdup(frame
, smbc_getUser(context
));
1602 srv
= SMBC_server(frame
, context
, True
,
1603 server
, share
, &workgroup
, &user
, &password
);
1607 return -1; /* errno set by SMBC_server */
1610 /*d_printf(">>>unlink: resolving %s\n", path);*/
1611 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1612 srv
->cli
, path
, &targetcli
, &targetpath
);
1613 if (!NT_STATUS_IS_OK(status
)) {
1614 d_printf("Could not resolve %s\n", path
);
1622 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= FILE_ATTRIBUTE_READONLY
;
1623 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= FILE_ATTRIBUTE_ARCHIVE
;
1624 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= FILE_ATTRIBUTE_SYSTEM
;
1625 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= FILE_ATTRIBUTE_HIDDEN
;
1627 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1628 errno
= SMBC_errno(context
, targetcli
);
1638 SMBC_utimes_ctx(SMBCCTX
*context
,
1640 struct timeval
*tbuf
)
1642 SMBCSRV
*srv
= NULL
;
1643 char *server
= NULL
;
1646 char *password
= NULL
;
1647 char *workgroup
= NULL
;
1651 TALLOC_CTX
*frame
= talloc_stackframe();
1653 if (!context
|| !context
->internal
->initialized
) {
1655 errno
= EINVAL
; /* Best I can think of ... */
1667 access_time
= write_time
= time(NULL
);
1669 access_time
= tbuf
[0].tv_sec
;
1670 write_time
= tbuf
[1].tv_sec
;
1678 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1679 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1680 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1684 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1685 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1686 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1690 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1691 fname
, atimebuf
, mtimebuf
);
1694 if (SMBC_parse_path(frame
,
1709 if (!user
|| user
[0] == (char)0) {
1710 user
= talloc_strdup(frame
, smbc_getUser(context
));
1718 srv
= SMBC_server(frame
, context
, True
,
1719 server
, share
, &workgroup
, &user
, &password
);
1723 return -1; /* errno set by SMBC_server */
1726 if (!SMBC_setatr(context
, srv
, path
,
1727 0, access_time
, write_time
, 0, 0)) {
1729 return -1; /* errno set by SMBC_setatr */
1737 * Routine to unlink() a file
1741 SMBC_unlink_ctx(SMBCCTX
*context
,
1744 char *server
= NULL
;
1747 char *password
= NULL
;
1748 char *workgroup
= NULL
;
1750 char *targetpath
= NULL
;
1751 struct cli_state
*targetcli
= NULL
;
1752 SMBCSRV
*srv
= NULL
;
1753 TALLOC_CTX
*frame
= talloc_stackframe();
1756 if (!context
|| !context
->internal
->initialized
) {
1758 errno
= EINVAL
; /* Best I can think of ... */
1771 if (SMBC_parse_path(frame
,
1786 if (!user
|| user
[0] == (char)0) {
1787 user
= talloc_strdup(frame
, smbc_getUser(context
));
1795 srv
= SMBC_server(frame
, context
, True
,
1796 server
, share
, &workgroup
, &user
, &password
);
1800 return -1; /* SMBC_server sets errno */
1804 /*d_printf(">>>unlink: resolving %s\n", path);*/
1805 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1806 srv
->cli
, path
, &targetcli
, &targetpath
);
1807 if (!NT_STATUS_IS_OK(status
)) {
1808 d_printf("Could not resolve %s\n", path
);
1813 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1815 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
1817 errno
= SMBC_errno(context
, targetcli
);
1819 if (errno
== EACCES
) { /* Check if the file is a directory */
1824 struct timespec write_time_ts
;
1825 struct timespec access_time_ts
;
1826 struct timespec change_time_ts
;
1829 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1836 /* Hmmm, bad error ... What? */
1838 errno
= SMBC_errno(context
, targetcli
);
1845 if (IS_DOS_DIR(mode
))
1848 errno
= saverr
; /* Restore this */
1859 return 0; /* Success ... */
1864 * Routine to rename() a file
1868 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1873 char *server1
= NULL
;
1874 char *share1
= NULL
;
1875 char *server2
= NULL
;
1876 char *share2
= NULL
;
1879 char *password1
= NULL
;
1880 char *password2
= NULL
;
1881 char *workgroup
= NULL
;
1884 char *targetpath1
= NULL
;
1885 char *targetpath2
= NULL
;
1886 struct cli_state
*targetcli1
= NULL
;
1887 struct cli_state
*targetcli2
= NULL
;
1888 SMBCSRV
*srv
= NULL
;
1889 TALLOC_CTX
*frame
= talloc_stackframe();
1892 if (!ocontext
|| !ncontext
||
1893 !ocontext
->internal
->initialized
||
1894 !ncontext
->internal
->initialized
) {
1896 errno
= EINVAL
; /* Best I can think of ... */
1901 if (!oname
|| !nname
) {
1907 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1909 if (SMBC_parse_path(frame
,
1924 if (!user1
|| user1
[0] == (char)0) {
1925 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1933 if (SMBC_parse_path(frame
,
1948 if (!user2
|| user2
[0] == (char)0) {
1949 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1957 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1958 strcmp(user1
, user2
)) {
1959 /* Can't rename across file systems, or users?? */
1965 srv
= SMBC_server(frame
, ocontext
, True
,
1966 server1
, share1
, &workgroup
, &user1
, &password1
);
1973 /* set the credentials to make DFS work */
1974 smbc_set_credentials_with_fallback(ocontext
,
1979 /*d_printf(">>>rename: resolving %s\n", path1);*/
1980 status
= cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1981 srv
->cli
, path1
, &targetcli1
, &targetpath1
);
1982 if (!NT_STATUS_IS_OK(status
)) {
1983 d_printf("Could not resolve %s\n", path1
);
1989 /* set the credentials to make DFS work */
1990 smbc_set_credentials_with_fallback(ncontext
,
1995 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1996 /*d_printf(">>>rename: resolving %s\n", path2);*/
1997 status
= cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1998 srv
->cli
, path2
, &targetcli2
, &targetpath2
);
1999 if (!NT_STATUS_IS_OK(status
)) {
2000 d_printf("Could not resolve %s\n", path2
);
2005 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2007 if (strcmp(smbXcli_conn_remote_name(targetcli1
->conn
), smbXcli_conn_remote_name(targetcli2
->conn
)) ||
2008 strcmp(targetcli1
->share
, targetcli2
->share
))
2010 /* can't rename across file systems */
2016 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2017 int eno
= SMBC_errno(ocontext
, targetcli1
);
2019 if (eno
!= EEXIST
||
2020 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
)) ||
2021 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2031 return 0; /* Success */