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 "popt_common.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"
35 * Routine to open a directory
36 * We accept the URL syntax explained in SMBC_parse_path(), above.
40 remove_dir(SMBCFILE
*dir
)
42 struct smbc_dir_list
*d
,*f
;
54 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
59 add_dirent(SMBCFILE
*dir
,
64 struct smbc_dirent
*dirent
;
66 int name_length
= (name
== NULL
? 0 : strlen(name
));
67 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
70 * Allocate space for the dirent, which must be increased by the
71 * size of the name and the comment and 1 each for the null terminator.
74 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
76 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
80 dir
->dir_error
= ENOMEM
;
87 if (dir
->dir_list
== NULL
) {
89 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
93 dir
->dir_error
= ENOMEM
;
97 ZERO_STRUCTP(dir
->dir_list
);
99 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
103 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
105 if (!dir
->dir_end
->next
) {
108 dir
->dir_error
= ENOMEM
;
112 ZERO_STRUCTP(dir
->dir_end
->next
);
114 dir
->dir_end
= dir
->dir_end
->next
;
117 dir
->dir_end
->next
= NULL
;
118 dir
->dir_end
->dirent
= dirent
;
120 dirent
->smbc_type
= type
;
121 dirent
->namelen
= name_length
;
122 dirent
->commentlen
= comment_len
;
123 dirent
->dirlen
= size
;
126 * dirent->namelen + 1 includes the null (no null termination needed)
127 * Ditto for dirent->commentlen.
128 * The space for the two null bytes was allocated.
130 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
131 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
132 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
139 list_unique_wg_fn(const char *name
,
144 SMBCFILE
*dir
= (SMBCFILE
*)state
;
145 struct smbc_dir_list
*dir_list
;
146 struct smbc_dirent
*dirent
;
150 dirent_type
= dir
->dir_type
;
152 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
153 /* An error occurred, what do we do? */
154 /* FIXME: Add some code here */
155 /* Change cli_NetServerEnum to take a fn
156 returning NTSTATUS... JRA. */
159 /* Point to the one just added */
160 dirent
= dir
->dir_end
->dirent
;
162 /* See if this was a duplicate */
163 for (dir_list
= dir
->dir_list
;
164 dir_list
!= dir
->dir_end
;
165 dir_list
= dir_list
->next
) {
167 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
168 /* Duplicate. End end of list need to be removed. */
172 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
173 /* Found the end of the list. Remove it. */
174 dir
->dir_end
= dir_list
;
175 free(dir_list
->next
);
177 dir_list
->next
= NULL
;
184 list_fn(const char *name
,
189 SMBCFILE
*dir
= (SMBCFILE
*)state
;
193 * We need to process the type a little ...
195 * Disk share = 0x00000000
196 * Print share = 0x00000001
197 * Comms share = 0x00000002 (obsolete?)
198 * IPC$ share = 0x00000003
200 * administrative shares:
201 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
204 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
208 dirent_type
= SMBC_FILE_SHARE
;
212 dirent_type
= SMBC_PRINTER_SHARE
;
216 dirent_type
= SMBC_COMMS_SHARE
;
221 dirent_type
= SMBC_IPC_SHARE
;
225 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
230 dirent_type
= dir
->dir_type
;
233 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
234 /* An error occurred, what do we do? */
235 /* FIXME: Add some code here */
236 /* Change cli_NetServerEnum to take a fn
237 returning NTSTATUS... JRA. */
242 dir_list_fn(const char *mnt
,
243 struct file_info
*finfo
,
248 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
249 (finfo
->mode
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
250 SMBCFILE
*dir
= (SMBCFILE
*)state
;
251 return map_nt_error_from_unix(dir
->dir_error
);
257 net_share_enum_rpc(struct cli_state
*cli
,
258 void (*fn
)(const char *name
,
266 uint32 preferred_len
= 0xffffffff;
268 struct srvsvc_NetShareInfoCtr info_ctr
;
269 struct srvsvc_NetShareCtr1 ctr1
;
271 fstring comment
= "";
272 struct rpc_pipe_client
*pipe_hnd
= NULL
;
274 uint32_t resume_handle
= 0;
275 uint32_t total_entries
= 0;
276 struct dcerpc_binding_handle
*b
;
278 /* Open the server service pipe */
279 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
281 if (!NT_STATUS_IS_OK(nt_status
)) {
282 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
286 ZERO_STRUCT(info_ctr
);
290 info_ctr
.ctr
.ctr1
= &ctr1
;
292 b
= pipe_hnd
->binding_handle
;
294 /* Issue the NetShareEnum RPC call and retrieve the response */
295 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
303 /* Was it successful? */
304 if (!NT_STATUS_IS_OK(nt_status
)) {
305 /* Nope. Go clean up. */
306 result
= ntstatus_to_werror(nt_status
);
310 if (!W_ERROR_IS_OK(result
)) {
311 /* Nope. Go clean up. */
315 if (total_entries
== 0) {
316 /* Nope. Go clean up. */
317 result
= WERR_GENERAL_FAILURE
;
321 /* For each returned entry... */
322 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
324 /* pull out the share name */
325 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
327 /* pull out the share's comment */
328 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
330 /* Get the type value */
331 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
333 /* Add this share to the list */
334 (*fn
)(name
, type
, comment
, state
);
338 /* Close the server service pipe */
339 TALLOC_FREE(pipe_hnd
);
341 /* Tell 'em if it worked */
342 return W_ERROR_IS_OK(result
) ? 0 : -1;
347 * Verify that the options specified in a URL are valid
350 SMBC_check_options(char *server
,
355 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
356 "path='%s' options='%s'\n",
357 server
, share
, path
, options
));
359 /* No options at all is always ok */
360 if (! *options
) return 0;
362 /* Currently, we don't support any options. */
368 SMBC_opendir_ctx(SMBCCTX
*context
,
375 char *password
= NULL
;
376 char *options
= NULL
;
377 char *workgroup
= NULL
;
382 SMBCFILE
*dir
= NULL
;
383 struct sockaddr_storage rem_ss
;
384 TALLOC_CTX
*frame
= talloc_stackframe();
386 if (!context
|| !context
->internal
->initialized
) {
387 DEBUG(4, ("no valid context\n"));
389 errno
= EINVAL
+ 8192;
395 DEBUG(4, ("no valid fname\n"));
397 errno
= EINVAL
+ 8193;
401 if (SMBC_parse_path(frame
,
411 DEBUG(4, ("no valid path\n"));
413 errno
= EINVAL
+ 8194;
417 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
418 "path='%s' options='%s'\n",
419 fname
, server
, share
, path
, options
));
421 /* Ensure the options are valid */
422 if (SMBC_check_options(server
, share
, path
, options
)) {
423 DEBUG(4, ("unacceptable options (%s)\n", options
));
425 errno
= EINVAL
+ 8195;
429 if (!user
|| user
[0] == (char)0) {
430 user
= talloc_strdup(frame
, smbc_getUser(context
));
438 dir
= SMB_MALLOC_P(SMBCFILE
);
449 dir
->fname
= SMB_STRDUP(fname
);
453 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
455 if (server
[0] == (char)0) {
460 struct sockaddr_storage
*ip_list
;
461 struct sockaddr_storage server_addr
;
462 struct user_auth_info u_info
;
465 if (share
[0] != (char)0 || path
[0] != (char)0) {
468 SAFE_FREE(dir
->fname
);
472 errno
= EINVAL
+ 8196;
476 /* Determine how many local master browsers to query */
477 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
479 : smbc_getOptionBrowseMaxLmbCount(context
));
481 memset(&u_info
, '\0', sizeof(u_info
));
482 u_info
.username
= talloc_strdup(frame
,user
);
483 u_info
.password
= talloc_strdup(frame
,password
);
484 if (!u_info
.username
|| !u_info
.password
) {
486 SAFE_FREE(dir
->fname
);
494 * We have server and share and path empty but options
495 * requesting that we scan all master browsers for their list
496 * of workgroups/domains. This implies that we must first try
497 * broadcast queries to find all master browsers, and if that
498 * doesn't work, then try our other methods which return only
499 * a single master browser.
503 status
= name_resolve_bcast(MSBROWSE
, 1, talloc_tos(),
505 if (!NT_STATUS_IS_OK(status
))
508 TALLOC_FREE(ip_list
);
510 if (!find_master_ip(workgroup
, &server_addr
)) {
513 SAFE_FREE(dir
->fname
);
521 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
522 talloc_tos(), &server_addr
,
523 sizeof(server_addr
));
524 if (ip_list
== NULL
) {
526 SAFE_FREE(dir
->fname
);
536 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
537 char addr
[INET6_ADDRSTRLEN
];
539 struct cli_state
*cli
= NULL
;
541 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
]);
542 DEBUG(99, ("Found master browser %d of %d: %s\n",
543 i
+1, MAX(count
, max_lmb_count
),
546 cli
= get_ipc_connect_master_ip(talloc_tos(),
550 /* cli == NULL is the master browser refused to talk or
551 could not be found */
556 workgroup
= talloc_strdup(frame
, wg_ptr
);
557 server
= talloc_strdup(frame
, cli
->desthost
);
561 if (!workgroup
|| !server
) {
563 SAFE_FREE(dir
->fname
);
571 DEBUG(4, ("using workgroup %s %s\n",
575 * For each returned master browser IP address, get a
576 * connection to IPC$ on the server if we do not
577 * already have one, and determine the
578 * workgroups/domains that it knows about.
581 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
582 &workgroup
, &user
, &password
);
588 dir
->dir_type
= SMBC_WORKGROUP
;
590 /* Now, list the stuff ... */
592 if (!cli_NetServerEnum(srv
->cli
,
601 TALLOC_FREE(ip_list
);
604 * Server not an empty string ... Check the rest and see what
607 if (*share
== '\0') {
610 /* Should not have empty share with path */
612 SAFE_FREE(dir
->fname
);
616 errno
= EINVAL
+ 8197;
622 * We don't know if <server> is really a server name
623 * or is a workgroup/domain name. If we already have
624 * a server structure for it, we'll use it.
625 * Otherwise, check to see if <server><1D>,
626 * <server><1B>, or <server><20> translates. We check
627 * to see if <server> is an IP address first.
631 * See if we have an existing server. Do not
632 * establish a connection if one does not already
635 srv
= SMBC_server(frame
, context
, False
,
637 &workgroup
, &user
, &password
);
640 * If no existing server and not an IP addr, look for
644 !is_ipaddress(server
) &&
645 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
646 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
648 * "server" is actually a workgroup name,
649 * not a server. Make this clear.
651 char *wgroup
= server
;
654 dir
->dir_type
= SMBC_SERVER
;
657 * Get the backup list ...
659 if (!name_status_find(wgroup
, 0, 0,
660 &rem_ss
, buserver
)) {
661 char addr
[INET6_ADDRSTRLEN
];
663 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
664 DEBUG(0,("Could not get name of "
665 "local/domain master browser "
666 "for workgroup %s from "
671 SAFE_FREE(dir
->fname
);
681 * Get a connection to IPC$ on the server if
682 * we do not already have one
684 srv
= SMBC_server(frame
, context
, True
,
689 DEBUG(0, ("got no contact to IPC$\n"));
691 SAFE_FREE(dir
->fname
);
701 /* Now, list the servers ... */
702 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
707 SAFE_FREE(dir
->fname
);
714 (resolve_name(server
, &rem_ss
, 0x20, false))) {
717 * If we hadn't found the server, get one now
720 srv
= SMBC_server(frame
, context
, True
,
728 SAFE_FREE(dir
->fname
);
736 dir
->dir_type
= SMBC_FILE_SHARE
;
739 /* List the shares ... */
741 if (net_share_enum_rpc(
750 errno
= cli_errno(srv
->cli
);
752 SAFE_FREE(dir
->fname
);
760 /* Neither the workgroup nor server exists */
761 errno
= ECONNREFUSED
;
763 SAFE_FREE(dir
->fname
);
773 * The server and share are specified ... work from
777 struct cli_state
*targetcli
;
780 /* We connect to the server and list the directory */
781 dir
->dir_type
= SMBC_FILE_SHARE
;
783 srv
= SMBC_server(frame
, context
, True
, server
, share
,
784 &workgroup
, &user
, &password
);
788 SAFE_FREE(dir
->fname
);
797 /* Now, list the files ... */
799 p
= path
+ strlen(path
);
800 path
= talloc_asprintf_append(path
, "\\*");
803 SAFE_FREE(dir
->fname
);
810 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
812 &targetcli
, &targetpath
)) {
813 d_printf("Could not resolve %s\n", path
);
815 SAFE_FREE(dir
->fname
);
822 status
= cli_list(targetcli
, targetpath
,
823 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
824 dir_list_fn
, (void *)dir
);
825 if (!NT_STATUS_IS_OK(status
)) {
827 SAFE_FREE(dir
->fname
);
830 saved_errno
= SMBC_errno(context
, targetcli
);
832 if (saved_errno
== EINVAL
) {
834 * See if they asked to opendir
835 * something other than a directory.
836 * If so, the converted error value we
837 * got would have been EINVAL rather
840 *p
= '\0'; /* restore original path */
842 if (SMBC_getatr(context
, srv
, path
,
844 NULL
, NULL
, NULL
, NULL
,
846 ! IS_DOS_DIR(mode
)) {
848 /* It is. Correct the error value */
849 saved_errno
= ENOTDIR
;
854 * If there was an error and the server is no
857 if (cli_is_error(targetcli
) &&
858 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
860 /* ... then remove it. */
861 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
864 * We could not remove the
865 * server completely, remove
866 * it from the cache so we
867 * will not get it again. It
868 * will be removed when the
869 * last file/dir is closed.
871 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
883 DLIST_ADD(context
->internal
->files
, dir
);
890 * Routine to close a directory
894 SMBC_closedir_ctx(SMBCCTX
*context
,
897 TALLOC_CTX
*frame
= talloc_stackframe();
899 if (!context
|| !context
->internal
->initialized
) {
905 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
911 remove_dir(dir
); /* Clean it up */
913 DLIST_REMOVE(context
->internal
->files
, dir
);
917 SAFE_FREE(dir
->fname
);
918 SAFE_FREE(dir
); /* Free the space too */
927 smbc_readdir_internal(SMBCCTX
* context
,
928 struct smbc_dirent
*dest
,
929 struct smbc_dirent
*src
,
932 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
934 /* url-encode the name. get back remaining buffer space */
936 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
938 /* We now know the name length */
939 dest
->namelen
= strlen(dest
->name
);
941 /* Save the pointer to the beginning of the comment */
942 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
944 /* Copy the comment */
945 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
946 dest
->comment
[max_namebuf_len
- 1] = '\0';
948 /* Save other fields */
949 dest
->smbc_type
= src
->smbc_type
;
950 dest
->commentlen
= strlen(dest
->comment
);
951 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
955 /* No encoding. Just copy the entry as is. */
956 memcpy(dest
, src
, src
->dirlen
);
957 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
963 * Routine to get a directory entry
967 SMBC_readdir_ctx(SMBCCTX
*context
,
971 struct smbc_dirent
*dirp
, *dirent
;
972 TALLOC_CTX
*frame
= talloc_stackframe();
974 /* Check that all is ok first ... */
976 if (!context
|| !context
->internal
->initialized
) {
979 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
985 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
988 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
994 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
997 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1003 if (!dir
->dir_next
) {
1008 dirent
= dir
->dir_next
->dirent
;
1017 dirp
= &context
->internal
->dirent
;
1018 maxlen
= sizeof(context
->internal
->_dirent_name
);
1020 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1022 dir
->dir_next
= dir
->dir_next
->next
;
1029 * Routine to get directory entries
1033 SMBC_getdents_ctx(SMBCCTX
*context
,
1035 struct smbc_dirent
*dirp
,
1041 char *ndir
= (char *)dirp
;
1042 struct smbc_dir_list
*dirlist
;
1043 TALLOC_CTX
*frame
= talloc_stackframe();
1045 /* Check that all is ok first ... */
1047 if (!context
|| !context
->internal
->initialized
) {
1055 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1063 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1072 * Now, retrieve the number of entries that will fit in what was passed
1073 * We have to figure out if the info is in the list, or we need to
1074 * send a request to the server to get the info.
1077 while ((dirlist
= dir
->dir_next
)) {
1078 struct smbc_dirent
*dirent
;
1079 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1081 if (!dirlist
->dirent
) {
1083 errno
= ENOENT
; /* Bad error */
1089 /* Do urlencoding of next entry, if so selected */
1090 dirent
= &context
->internal
->dirent
;
1091 maxlen
= sizeof(context
->internal
->_dirent_name
);
1092 smbc_readdir_internal(context
, dirent
,
1093 dirlist
->dirent
, maxlen
);
1095 reqd
= dirent
->dirlen
;
1099 if (rem
< count
) { /* We managed to copy something */
1106 else { /* Nothing copied ... */
1108 errno
= EINVAL
; /* Not enough space ... */
1116 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1118 currentEntry
->comment
= ¤tEntry
->name
[0] +
1119 dirent
->namelen
+ 1;
1124 /* Try and align the struct for the next entry
1125 on a valid pointer boundary by appending zeros */
1126 while((rem
> 0) && ((unsigned long long)ndir
& (sizeof(void*) - 1))) {
1130 currentEntry
->dirlen
++;
1133 dir
->dir_next
= dirlist
= dirlist
-> next
;
1146 * Routine to create a directory ...
1150 SMBC_mkdir_ctx(SMBCCTX
*context
,
1154 SMBCSRV
*srv
= NULL
;
1155 char *server
= NULL
;
1158 char *password
= NULL
;
1159 char *workgroup
= NULL
;
1161 char *targetpath
= NULL
;
1162 struct cli_state
*targetcli
= NULL
;
1163 TALLOC_CTX
*frame
= talloc_stackframe();
1165 if (!context
|| !context
->internal
->initialized
) {
1177 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1179 if (SMBC_parse_path(frame
,
1194 if (!user
|| user
[0] == (char)0) {
1195 user
= talloc_strdup(frame
, smbc_getUser(context
));
1203 srv
= SMBC_server(frame
, context
, True
,
1204 server
, share
, &workgroup
, &user
, &password
);
1209 return -1; /* errno set by SMBC_server */
1213 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1214 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1216 &targetcli
, &targetpath
)) {
1217 d_printf("Could not resolve %s\n", path
);
1222 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1224 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1225 errno
= SMBC_errno(context
, targetcli
);
1237 * Our list function simply checks to see if a directory is not empty
1241 rmdir_list_fn(const char *mnt
,
1242 struct file_info
*finfo
,
1246 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1247 strncmp(finfo
->name
, "..", 2) != 0) {
1248 bool *smbc_rmdir_dirempty
= (bool *)state
;
1249 *smbc_rmdir_dirempty
= false;
1251 return NT_STATUS_OK
;
1255 * Routine to remove a directory
1259 SMBC_rmdir_ctx(SMBCCTX
*context
,
1262 SMBCSRV
*srv
= NULL
;
1263 char *server
= NULL
;
1266 char *password
= NULL
;
1267 char *workgroup
= NULL
;
1269 char *targetpath
= NULL
;
1270 struct cli_state
*targetcli
= NULL
;
1271 TALLOC_CTX
*frame
= talloc_stackframe();
1273 if (!context
|| !context
->internal
->initialized
) {
1285 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1287 if (SMBC_parse_path(frame
,
1302 if (!user
|| user
[0] == (char)0) {
1303 user
= talloc_strdup(frame
, smbc_getUser(context
));
1311 srv
= SMBC_server(frame
, context
, True
,
1312 server
, share
, &workgroup
, &user
, &password
);
1317 return -1; /* errno set by SMBC_server */
1321 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1322 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1324 &targetcli
, &targetpath
)) {
1325 d_printf("Could not resolve %s\n", path
);
1330 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1332 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1334 errno
= SMBC_errno(context
, targetcli
);
1336 if (errno
== EACCES
) { /* Check if the dir empty or not */
1338 /* Local storage to avoid buffer overflows */
1340 bool smbc_rmdir_dirempty
= true;
1343 lpath
= talloc_asprintf(frame
, "%s\\*",
1351 status
= cli_list(targetcli
, lpath
,
1352 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1354 &smbc_rmdir_dirempty
);
1356 if (!NT_STATUS_IS_OK(status
)) {
1357 /* Fix errno to ignore latest error ... */
1358 DEBUG(5, ("smbc_rmdir: "
1359 "cli_list returned an error: %d\n",
1360 SMBC_errno(context
, targetcli
)));
1365 if (smbc_rmdir_dirempty
)
1383 * Routine to return the current directory position
1387 SMBC_telldir_ctx(SMBCCTX
*context
,
1390 TALLOC_CTX
*frame
= talloc_stackframe();
1392 if (!context
|| !context
->internal
->initialized
) {
1400 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1408 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1416 /* See if we're already at the end. */
1417 if (dir
->dir_next
== NULL
) {
1424 * We return the pointer here as the offset
1427 return (off_t
)(long)dir
->dir_next
->dirent
;
1431 * A routine to run down the list and see if the entry is OK
1434 static struct smbc_dir_list
*
1435 check_dir_ent(struct smbc_dir_list
*list
,
1436 struct smbc_dirent
*dirent
)
1439 /* Run down the list looking for what we want */
1443 struct smbc_dir_list
*tmp
= list
;
1447 if (tmp
->dirent
== dirent
)
1456 return NULL
; /* Not found, or an error */
1462 * Routine to seek on a directory
1466 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1470 long int l_offset
= offset
; /* Handle problems of size */
1471 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1472 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1473 TALLOC_CTX
*frame
= talloc_stackframe();
1475 if (!context
|| !context
->internal
->initialized
) {
1483 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1491 /* Now, check what we were passed and see if it is OK ... */
1493 if (dirent
== NULL
) { /* Seek to the begining of the list */
1495 dir
->dir_next
= dir
->dir_list
;
1501 if (offset
== -1) { /* Seek to the end of the list */
1502 dir
->dir_next
= NULL
;
1507 /* Now, run down the list and make sure that the entry is OK */
1508 /* This may need to be changed if we change the format of the list */
1510 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1511 errno
= EINVAL
; /* Bad entry */
1516 dir
->dir_next
= list_ent
;
1523 * Routine to fstat a dir
1527 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1532 if (!context
|| !context
->internal
->initialized
) {
1538 /* No code yet ... */
1543 SMBC_chmod_ctx(SMBCCTX
*context
,
1547 SMBCSRV
*srv
= NULL
;
1548 char *server
= NULL
;
1551 char *password
= NULL
;
1552 char *workgroup
= NULL
;
1553 char *targetpath
= NULL
;
1554 struct cli_state
*targetcli
= NULL
;
1557 TALLOC_CTX
*frame
= talloc_stackframe();
1559 if (!context
|| !context
->internal
->initialized
) {
1561 errno
= EINVAL
; /* Best I can think of ... */
1572 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1574 if (SMBC_parse_path(frame
,
1589 if (!user
|| user
[0] == (char)0) {
1590 user
= talloc_strdup(frame
, smbc_getUser(context
));
1598 srv
= SMBC_server(frame
, context
, True
,
1599 server
, share
, &workgroup
, &user
, &password
);
1603 return -1; /* errno set by SMBC_server */
1606 /*d_printf(">>>unlink: resolving %s\n", path);*/
1607 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1609 &targetcli
, &targetpath
)) {
1610 d_printf("Could not resolve %s\n", path
);
1618 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= FILE_ATTRIBUTE_READONLY
;
1619 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= FILE_ATTRIBUTE_ARCHIVE
;
1620 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= FILE_ATTRIBUTE_SYSTEM
;
1621 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= FILE_ATTRIBUTE_HIDDEN
;
1623 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1624 errno
= SMBC_errno(context
, targetcli
);
1634 SMBC_utimes_ctx(SMBCCTX
*context
,
1636 struct timeval
*tbuf
)
1638 SMBCSRV
*srv
= NULL
;
1639 char *server
= NULL
;
1642 char *password
= NULL
;
1643 char *workgroup
= NULL
;
1647 TALLOC_CTX
*frame
= talloc_stackframe();
1649 if (!context
|| !context
->internal
->initialized
) {
1651 errno
= EINVAL
; /* Best I can think of ... */
1663 access_time
= write_time
= time(NULL
);
1665 access_time
= tbuf
[0].tv_sec
;
1666 write_time
= tbuf
[1].tv_sec
;
1674 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1675 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1676 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1680 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1681 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1682 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1686 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1687 fname
, atimebuf
, mtimebuf
);
1690 if (SMBC_parse_path(frame
,
1705 if (!user
|| user
[0] == (char)0) {
1706 user
= talloc_strdup(frame
, smbc_getUser(context
));
1714 srv
= SMBC_server(frame
, context
, True
,
1715 server
, share
, &workgroup
, &user
, &password
);
1719 return -1; /* errno set by SMBC_server */
1722 if (!SMBC_setatr(context
, srv
, path
,
1723 0, access_time
, write_time
, 0, 0)) {
1725 return -1; /* errno set by SMBC_setatr */
1733 * Routine to unlink() a file
1737 SMBC_unlink_ctx(SMBCCTX
*context
,
1740 char *server
= NULL
;
1743 char *password
= NULL
;
1744 char *workgroup
= NULL
;
1746 char *targetpath
= NULL
;
1747 struct cli_state
*targetcli
= NULL
;
1748 SMBCSRV
*srv
= NULL
;
1749 TALLOC_CTX
*frame
= talloc_stackframe();
1751 if (!context
|| !context
->internal
->initialized
) {
1753 errno
= EINVAL
; /* Best I can think of ... */
1766 if (SMBC_parse_path(frame
,
1781 if (!user
|| user
[0] == (char)0) {
1782 user
= talloc_strdup(frame
, smbc_getUser(context
));
1790 srv
= SMBC_server(frame
, context
, True
,
1791 server
, share
, &workgroup
, &user
, &password
);
1795 return -1; /* SMBC_server sets errno */
1799 /*d_printf(">>>unlink: resolving %s\n", path);*/
1800 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1802 &targetcli
, &targetpath
)) {
1803 d_printf("Could not resolve %s\n", path
);
1808 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1810 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
1812 errno
= SMBC_errno(context
, targetcli
);
1814 if (errno
== EACCES
) { /* Check if the file is a directory */
1819 struct timespec write_time_ts
;
1820 struct timespec access_time_ts
;
1821 struct timespec change_time_ts
;
1824 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1831 /* Hmmm, bad error ... What? */
1833 errno
= SMBC_errno(context
, targetcli
);
1840 if (IS_DOS_DIR(mode
))
1843 errno
= saverr
; /* Restore this */
1854 return 0; /* Success ... */
1859 * Routine to rename() a file
1863 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1868 char *server1
= NULL
;
1869 char *share1
= NULL
;
1870 char *server2
= NULL
;
1871 char *share2
= NULL
;
1874 char *password1
= NULL
;
1875 char *password2
= NULL
;
1876 char *workgroup
= NULL
;
1879 char *targetpath1
= NULL
;
1880 char *targetpath2
= NULL
;
1881 struct cli_state
*targetcli1
= NULL
;
1882 struct cli_state
*targetcli2
= NULL
;
1883 SMBCSRV
*srv
= NULL
;
1884 TALLOC_CTX
*frame
= talloc_stackframe();
1886 if (!ocontext
|| !ncontext
||
1887 !ocontext
->internal
->initialized
||
1888 !ncontext
->internal
->initialized
) {
1890 errno
= EINVAL
; /* Best I can think of ... */
1895 if (!oname
|| !nname
) {
1901 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1903 if (SMBC_parse_path(frame
,
1918 if (!user1
|| user1
[0] == (char)0) {
1919 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1927 if (SMBC_parse_path(frame
,
1942 if (!user2
|| user2
[0] == (char)0) {
1943 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1951 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1952 strcmp(user1
, user2
)) {
1953 /* Can't rename across file systems, or users?? */
1959 srv
= SMBC_server(frame
, ocontext
, True
,
1960 server1
, share1
, &workgroup
, &user1
, &password1
);
1967 /* set the credentials to make DFS work */
1968 smbc_set_credentials_with_fallback(ocontext
,
1973 /*d_printf(">>>rename: resolving %s\n", path1);*/
1974 if (!cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1977 &targetcli1
, &targetpath1
)) {
1978 d_printf("Could not resolve %s\n", path1
);
1984 /* set the credentials to make DFS work */
1985 smbc_set_credentials_with_fallback(ncontext
,
1990 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1991 /*d_printf(">>>rename: resolving %s\n", path2);*/
1992 if (!cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1995 &targetcli2
, &targetpath2
)) {
1996 d_printf("Could not resolve %s\n", path2
);
2001 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2003 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
2004 strcmp(targetcli1
->share
, targetcli2
->share
))
2006 /* can't rename across file systems */
2012 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2013 int eno
= SMBC_errno(ocontext
, targetcli1
);
2015 if (eno
!= EEXIST
||
2016 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
)) ||
2017 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2027 return 0; /* Success */