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 "popt_common.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "rpc_client/cli_pipe.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "libsmb/nmblib.h"
34 * Routine to open a directory
35 * We accept the URL syntax explained in SMBC_parse_path(), above.
39 remove_dir(SMBCFILE
*dir
)
41 struct smbc_dir_list
*d
,*f
;
53 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
58 add_dirent(SMBCFILE
*dir
,
63 struct smbc_dirent
*dirent
;
65 int name_length
= (name
== NULL
? 0 : strlen(name
));
66 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
69 * Allocate space for the dirent, which must be increased by the
70 * size of the name and the comment and 1 each for the null terminator.
73 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
75 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
79 dir
->dir_error
= ENOMEM
;
86 if (dir
->dir_list
== NULL
) {
88 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
92 dir
->dir_error
= ENOMEM
;
96 ZERO_STRUCTP(dir
->dir_list
);
98 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
102 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
104 if (!dir
->dir_end
->next
) {
107 dir
->dir_error
= ENOMEM
;
111 ZERO_STRUCTP(dir
->dir_end
->next
);
113 dir
->dir_end
= dir
->dir_end
->next
;
116 dir
->dir_end
->next
= NULL
;
117 dir
->dir_end
->dirent
= dirent
;
119 dirent
->smbc_type
= type
;
120 dirent
->namelen
= name_length
;
121 dirent
->commentlen
= comment_len
;
122 dirent
->dirlen
= size
;
125 * dirent->namelen + 1 includes the null (no null termination needed)
126 * Ditto for dirent->commentlen.
127 * The space for the two null bytes was allocated.
129 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
130 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
131 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
138 list_unique_wg_fn(const char *name
,
143 SMBCFILE
*dir
= (SMBCFILE
*)state
;
144 struct smbc_dir_list
*dir_list
;
145 struct smbc_dirent
*dirent
;
149 dirent_type
= dir
->dir_type
;
151 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
152 /* An error occurred, what do we do? */
153 /* FIXME: Add some code here */
154 /* Change cli_NetServerEnum to take a fn
155 returning NTSTATUS... JRA. */
158 /* Point to the one just added */
159 dirent
= dir
->dir_end
->dirent
;
161 /* See if this was a duplicate */
162 for (dir_list
= dir
->dir_list
;
163 dir_list
!= dir
->dir_end
;
164 dir_list
= dir_list
->next
) {
166 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
167 /* Duplicate. End end of list need to be removed. */
171 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
172 /* Found the end of the list. Remove it. */
173 dir
->dir_end
= dir_list
;
174 free(dir_list
->next
);
176 dir_list
->next
= NULL
;
183 list_fn(const char *name
,
188 SMBCFILE
*dir
= (SMBCFILE
*)state
;
192 * We need to process the type a little ...
194 * Disk share = 0x00000000
195 * Print share = 0x00000001
196 * Comms share = 0x00000002 (obsolete?)
197 * IPC$ share = 0x00000003
199 * administrative shares:
200 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
203 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
207 dirent_type
= SMBC_FILE_SHARE
;
211 dirent_type
= SMBC_PRINTER_SHARE
;
215 dirent_type
= SMBC_COMMS_SHARE
;
220 dirent_type
= SMBC_IPC_SHARE
;
224 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
229 dirent_type
= dir
->dir_type
;
232 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
233 /* An error occurred, what do we do? */
234 /* FIXME: Add some code here */
235 /* Change cli_NetServerEnum to take a fn
236 returning NTSTATUS... JRA. */
241 dir_list_fn(const char *mnt
,
242 struct file_info
*finfo
,
247 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
248 (finfo
->mode
&FILE_ATTRIBUTE_DIRECTORY
?SMBC_DIR
:SMBC_FILE
)) < 0) {
249 SMBCFILE
*dir
= (SMBCFILE
*)state
;
250 return map_nt_error_from_unix(dir
->dir_error
);
256 net_share_enum_rpc(struct cli_state
*cli
,
257 void (*fn
)(const char *name
,
265 uint32 preferred_len
= 0xffffffff;
267 struct srvsvc_NetShareInfoCtr info_ctr
;
268 struct srvsvc_NetShareCtr1 ctr1
;
270 fstring comment
= "";
271 struct rpc_pipe_client
*pipe_hnd
= NULL
;
273 uint32_t resume_handle
= 0;
274 uint32_t total_entries
= 0;
275 struct dcerpc_binding_handle
*b
;
277 /* Open the server service pipe */
278 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
280 if (!NT_STATUS_IS_OK(nt_status
)) {
281 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
285 ZERO_STRUCT(info_ctr
);
289 info_ctr
.ctr
.ctr1
= &ctr1
;
291 b
= pipe_hnd
->binding_handle
;
293 /* Issue the NetShareEnum RPC call and retrieve the response */
294 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
, talloc_tos(),
302 /* Was it successful? */
303 if (!NT_STATUS_IS_OK(nt_status
)) {
304 /* Nope. Go clean up. */
305 result
= ntstatus_to_werror(nt_status
);
309 if (!W_ERROR_IS_OK(result
)) {
310 /* Nope. Go clean up. */
314 if (total_entries
== 0) {
315 /* Nope. Go clean up. */
316 result
= WERR_GENERAL_FAILURE
;
320 /* For each returned entry... */
321 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
323 /* pull out the share name */
324 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
326 /* pull out the share's comment */
327 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
329 /* Get the type value */
330 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
332 /* Add this share to the list */
333 (*fn
)(name
, type
, comment
, state
);
337 /* Close the server service pipe */
338 TALLOC_FREE(pipe_hnd
);
340 /* Tell 'em if it worked */
341 return W_ERROR_IS_OK(result
) ? 0 : -1;
346 * Verify that the options specified in a URL are valid
349 SMBC_check_options(char *server
,
354 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
355 "path='%s' options='%s'\n",
356 server
, share
, path
, options
));
358 /* No options at all is always ok */
359 if (! *options
) return 0;
361 /* Currently, we don't support any options. */
367 SMBC_opendir_ctx(SMBCCTX
*context
,
374 char *password
= NULL
;
375 char *options
= NULL
;
376 char *workgroup
= NULL
;
381 SMBCFILE
*dir
= NULL
;
382 struct sockaddr_storage rem_ss
;
383 TALLOC_CTX
*frame
= talloc_stackframe();
385 if (!context
|| !context
->internal
->initialized
) {
386 DEBUG(4, ("no valid context\n"));
388 errno
= EINVAL
+ 8192;
394 DEBUG(4, ("no valid fname\n"));
396 errno
= EINVAL
+ 8193;
400 if (SMBC_parse_path(frame
,
410 DEBUG(4, ("no valid path\n"));
412 errno
= EINVAL
+ 8194;
416 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
417 "path='%s' options='%s'\n",
418 fname
, server
, share
, path
, options
));
420 /* Ensure the options are valid */
421 if (SMBC_check_options(server
, share
, path
, options
)) {
422 DEBUG(4, ("unacceptable options (%s)\n", options
));
424 errno
= EINVAL
+ 8195;
428 if (!user
|| user
[0] == (char)0) {
429 user
= talloc_strdup(frame
, smbc_getUser(context
));
437 dir
= SMB_MALLOC_P(SMBCFILE
);
448 dir
->fname
= SMB_STRDUP(fname
);
452 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
454 if (server
[0] == (char)0) {
459 struct sockaddr_storage
*ip_list
;
460 struct sockaddr_storage server_addr
;
461 struct user_auth_info u_info
;
464 if (share
[0] != (char)0 || path
[0] != (char)0) {
467 SAFE_FREE(dir
->fname
);
471 errno
= EINVAL
+ 8196;
475 /* Determine how many local master browsers to query */
476 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
478 : smbc_getOptionBrowseMaxLmbCount(context
));
480 memset(&u_info
, '\0', sizeof(u_info
));
481 u_info
.username
= talloc_strdup(frame
,user
);
482 u_info
.password
= talloc_strdup(frame
,password
);
483 if (!u_info
.username
|| !u_info
.password
) {
485 SAFE_FREE(dir
->fname
);
493 * We have server and share and path empty but options
494 * requesting that we scan all master browsers for their list
495 * of workgroups/domains. This implies that we must first try
496 * broadcast queries to find all master browsers, and if that
497 * doesn't work, then try our other methods which return only
498 * a single master browser.
502 status
= name_resolve_bcast(MSBROWSE
, 1, talloc_tos(),
504 if (!NT_STATUS_IS_OK(status
))
507 TALLOC_FREE(ip_list
);
509 if (!find_master_ip(workgroup
, &server_addr
)) {
512 SAFE_FREE(dir
->fname
);
520 ip_list
= (struct sockaddr_storage
*)talloc_memdup(
521 talloc_tos(), &server_addr
,
522 sizeof(server_addr
));
523 if (ip_list
== NULL
) {
525 SAFE_FREE(dir
->fname
);
535 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
536 char addr
[INET6_ADDRSTRLEN
];
538 struct cli_state
*cli
= NULL
;
540 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
]);
541 DEBUG(99, ("Found master browser %d of %d: %s\n",
542 i
+1, MAX(count
, max_lmb_count
),
545 cli
= get_ipc_connect_master_ip(talloc_tos(),
549 /* cli == NULL is the master browser refused to talk or
550 could not be found */
555 workgroup
= talloc_strdup(frame
, wg_ptr
);
556 server
= talloc_strdup(frame
, cli
->desthost
);
560 if (!workgroup
|| !server
) {
562 SAFE_FREE(dir
->fname
);
570 DEBUG(4, ("using workgroup %s %s\n",
574 * For each returned master browser IP address, get a
575 * connection to IPC$ on the server if we do not
576 * already have one, and determine the
577 * workgroups/domains that it knows about.
580 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
581 &workgroup
, &user
, &password
);
587 dir
->dir_type
= SMBC_WORKGROUP
;
589 /* Now, list the stuff ... */
591 if (!cli_NetServerEnum(srv
->cli
,
600 TALLOC_FREE(ip_list
);
603 * Server not an empty string ... Check the rest and see what
606 if (*share
== '\0') {
609 /* Should not have empty share with path */
611 SAFE_FREE(dir
->fname
);
615 errno
= EINVAL
+ 8197;
621 * We don't know if <server> is really a server name
622 * or is a workgroup/domain name. If we already have
623 * a server structure for it, we'll use it.
624 * Otherwise, check to see if <server><1D>,
625 * <server><1B>, or <server><20> translates. We check
626 * to see if <server> is an IP address first.
630 * See if we have an existing server. Do not
631 * establish a connection if one does not already
634 srv
= SMBC_server(frame
, context
, False
,
636 &workgroup
, &user
, &password
);
639 * If no existing server and not an IP addr, look for
643 !is_ipaddress(server
) &&
644 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
645 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
647 * "server" is actually a workgroup name,
648 * not a server. Make this clear.
650 char *wgroup
= server
;
653 dir
->dir_type
= SMBC_SERVER
;
656 * Get the backup list ...
658 if (!name_status_find(wgroup
, 0, 0,
659 &rem_ss
, buserver
)) {
660 char addr
[INET6_ADDRSTRLEN
];
662 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
663 DEBUG(0,("Could not get name of "
664 "local/domain master browser "
665 "for workgroup %s from "
670 SAFE_FREE(dir
->fname
);
680 * Get a connection to IPC$ on the server if
681 * we do not already have one
683 srv
= SMBC_server(frame
, context
, True
,
688 DEBUG(0, ("got no contact to IPC$\n"));
690 SAFE_FREE(dir
->fname
);
700 /* Now, list the servers ... */
701 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
706 SAFE_FREE(dir
->fname
);
713 (resolve_name(server
, &rem_ss
, 0x20, false))) {
716 * If we hadn't found the server, get one now
719 srv
= SMBC_server(frame
, context
, True
,
727 SAFE_FREE(dir
->fname
);
735 dir
->dir_type
= SMBC_FILE_SHARE
;
738 /* List the shares ... */
740 if (net_share_enum_rpc(
749 errno
= cli_errno(srv
->cli
);
751 SAFE_FREE(dir
->fname
);
759 /* Neither the workgroup nor server exists */
760 errno
= ECONNREFUSED
;
762 SAFE_FREE(dir
->fname
);
772 * The server and share are specified ... work from
776 struct cli_state
*targetcli
;
779 /* We connect to the server and list the directory */
780 dir
->dir_type
= SMBC_FILE_SHARE
;
782 srv
= SMBC_server(frame
, context
, True
, server
, share
,
783 &workgroup
, &user
, &password
);
787 SAFE_FREE(dir
->fname
);
796 /* Now, list the files ... */
798 p
= path
+ strlen(path
);
799 path
= talloc_asprintf_append(path
, "\\*");
802 SAFE_FREE(dir
->fname
);
809 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
811 &targetcli
, &targetpath
)) {
812 d_printf("Could not resolve %s\n", path
);
814 SAFE_FREE(dir
->fname
);
821 status
= cli_list(targetcli
, targetpath
,
822 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
823 dir_list_fn
, (void *)dir
);
824 if (!NT_STATUS_IS_OK(status
)) {
826 SAFE_FREE(dir
->fname
);
829 saved_errno
= SMBC_errno(context
, targetcli
);
831 if (saved_errno
== EINVAL
) {
833 * See if they asked to opendir
834 * something other than a directory.
835 * If so, the converted error value we
836 * got would have been EINVAL rather
839 *p
= '\0'; /* restore original path */
841 if (SMBC_getatr(context
, srv
, path
,
843 NULL
, NULL
, NULL
, NULL
,
845 ! IS_DOS_DIR(mode
)) {
847 /* It is. Correct the error value */
848 saved_errno
= ENOTDIR
;
853 * If there was an error and the server is no
856 if (cli_is_error(targetcli
) &&
857 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
859 /* ... then remove it. */
860 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
863 * We could not remove the
864 * server completely, remove
865 * it from the cache so we
866 * will not get it again. It
867 * will be removed when the
868 * last file/dir is closed.
870 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
882 DLIST_ADD(context
->internal
->files
, dir
);
889 * Routine to close a directory
893 SMBC_closedir_ctx(SMBCCTX
*context
,
896 TALLOC_CTX
*frame
= talloc_stackframe();
898 if (!context
|| !context
->internal
->initialized
) {
904 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
910 remove_dir(dir
); /* Clean it up */
912 DLIST_REMOVE(context
->internal
->files
, dir
);
916 SAFE_FREE(dir
->fname
);
917 SAFE_FREE(dir
); /* Free the space too */
926 smbc_readdir_internal(SMBCCTX
* context
,
927 struct smbc_dirent
*dest
,
928 struct smbc_dirent
*src
,
931 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
933 /* url-encode the name. get back remaining buffer space */
935 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
937 /* We now know the name length */
938 dest
->namelen
= strlen(dest
->name
);
940 /* Save the pointer to the beginning of the comment */
941 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
943 /* Copy the comment */
944 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
945 dest
->comment
[max_namebuf_len
- 1] = '\0';
947 /* Save other fields */
948 dest
->smbc_type
= src
->smbc_type
;
949 dest
->commentlen
= strlen(dest
->comment
);
950 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
954 /* No encoding. Just copy the entry as is. */
955 memcpy(dest
, src
, src
->dirlen
);
956 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
962 * Routine to get a directory entry
966 SMBC_readdir_ctx(SMBCCTX
*context
,
970 struct smbc_dirent
*dirp
, *dirent
;
971 TALLOC_CTX
*frame
= talloc_stackframe();
973 /* Check that all is ok first ... */
975 if (!context
|| !context
->internal
->initialized
) {
978 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
984 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
987 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
993 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
996 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1002 if (!dir
->dir_next
) {
1007 dirent
= dir
->dir_next
->dirent
;
1016 dirp
= &context
->internal
->dirent
;
1017 maxlen
= sizeof(context
->internal
->_dirent_name
);
1019 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1021 dir
->dir_next
= dir
->dir_next
->next
;
1028 * Routine to get directory entries
1032 SMBC_getdents_ctx(SMBCCTX
*context
,
1034 struct smbc_dirent
*dirp
,
1040 char *ndir
= (char *)dirp
;
1041 struct smbc_dir_list
*dirlist
;
1042 TALLOC_CTX
*frame
= talloc_stackframe();
1044 /* Check that all is ok first ... */
1046 if (!context
|| !context
->internal
->initialized
) {
1054 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1062 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1071 * Now, retrieve the number of entries that will fit in what was passed
1072 * We have to figure out if the info is in the list, or we need to
1073 * send a request to the server to get the info.
1076 while ((dirlist
= dir
->dir_next
)) {
1077 struct smbc_dirent
*dirent
;
1078 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1080 if (!dirlist
->dirent
) {
1082 errno
= ENOENT
; /* Bad error */
1088 /* Do urlencoding of next entry, if so selected */
1089 dirent
= &context
->internal
->dirent
;
1090 maxlen
= sizeof(context
->internal
->_dirent_name
);
1091 smbc_readdir_internal(context
, dirent
,
1092 dirlist
->dirent
, maxlen
);
1094 reqd
= dirent
->dirlen
;
1098 if (rem
< count
) { /* We managed to copy something */
1105 else { /* Nothing copied ... */
1107 errno
= EINVAL
; /* Not enough space ... */
1115 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1117 currentEntry
->comment
= ¤tEntry
->name
[0] +
1118 dirent
->namelen
+ 1;
1123 /* Try and align the struct for the next entry
1124 on a valid pointer boundary by appending zeros */
1125 while((rem
> 0) && ((unsigned long long)ndir
& (sizeof(void*) - 1))) {
1129 currentEntry
->dirlen
++;
1132 dir
->dir_next
= dirlist
= dirlist
-> next
;
1145 * Routine to create a directory ...
1149 SMBC_mkdir_ctx(SMBCCTX
*context
,
1153 SMBCSRV
*srv
= NULL
;
1154 char *server
= NULL
;
1157 char *password
= NULL
;
1158 char *workgroup
= NULL
;
1160 char *targetpath
= NULL
;
1161 struct cli_state
*targetcli
= NULL
;
1162 TALLOC_CTX
*frame
= talloc_stackframe();
1164 if (!context
|| !context
->internal
->initialized
) {
1176 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1178 if (SMBC_parse_path(frame
,
1193 if (!user
|| user
[0] == (char)0) {
1194 user
= talloc_strdup(frame
, smbc_getUser(context
));
1202 srv
= SMBC_server(frame
, context
, True
,
1203 server
, share
, &workgroup
, &user
, &password
);
1208 return -1; /* errno set by SMBC_server */
1212 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1213 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1215 &targetcli
, &targetpath
)) {
1216 d_printf("Could not resolve %s\n", path
);
1221 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1223 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1224 errno
= SMBC_errno(context
, targetcli
);
1236 * Our list function simply checks to see if a directory is not empty
1240 rmdir_list_fn(const char *mnt
,
1241 struct file_info
*finfo
,
1245 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1246 strncmp(finfo
->name
, "..", 2) != 0) {
1247 bool *smbc_rmdir_dirempty
= (bool *)state
;
1248 *smbc_rmdir_dirempty
= false;
1250 return NT_STATUS_OK
;
1254 * Routine to remove a directory
1258 SMBC_rmdir_ctx(SMBCCTX
*context
,
1261 SMBCSRV
*srv
= NULL
;
1262 char *server
= NULL
;
1265 char *password
= NULL
;
1266 char *workgroup
= NULL
;
1268 char *targetpath
= NULL
;
1269 struct cli_state
*targetcli
= NULL
;
1270 TALLOC_CTX
*frame
= talloc_stackframe();
1272 if (!context
|| !context
->internal
->initialized
) {
1284 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1286 if (SMBC_parse_path(frame
,
1301 if (!user
|| user
[0] == (char)0) {
1302 user
= talloc_strdup(frame
, smbc_getUser(context
));
1310 srv
= SMBC_server(frame
, context
, True
,
1311 server
, share
, &workgroup
, &user
, &password
);
1316 return -1; /* errno set by SMBC_server */
1320 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1321 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1323 &targetcli
, &targetpath
)) {
1324 d_printf("Could not resolve %s\n", path
);
1329 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1331 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1333 errno
= SMBC_errno(context
, targetcli
);
1335 if (errno
== EACCES
) { /* Check if the dir empty or not */
1337 /* Local storage to avoid buffer overflows */
1339 bool smbc_rmdir_dirempty
= true;
1342 lpath
= talloc_asprintf(frame
, "%s\\*",
1350 status
= cli_list(targetcli
, lpath
,
1351 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
1353 &smbc_rmdir_dirempty
);
1355 if (!NT_STATUS_IS_OK(status
)) {
1356 /* Fix errno to ignore latest error ... */
1357 DEBUG(5, ("smbc_rmdir: "
1358 "cli_list returned an error: %d\n",
1359 SMBC_errno(context
, targetcli
)));
1364 if (smbc_rmdir_dirempty
)
1382 * Routine to return the current directory position
1386 SMBC_telldir_ctx(SMBCCTX
*context
,
1389 TALLOC_CTX
*frame
= talloc_stackframe();
1391 if (!context
|| !context
->internal
->initialized
) {
1399 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1407 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1415 /* See if we're already at the end. */
1416 if (dir
->dir_next
== NULL
) {
1423 * We return the pointer here as the offset
1426 return (off_t
)(long)dir
->dir_next
->dirent
;
1430 * A routine to run down the list and see if the entry is OK
1433 static struct smbc_dir_list
*
1434 check_dir_ent(struct smbc_dir_list
*list
,
1435 struct smbc_dirent
*dirent
)
1438 /* Run down the list looking for what we want */
1442 struct smbc_dir_list
*tmp
= list
;
1446 if (tmp
->dirent
== dirent
)
1455 return NULL
; /* Not found, or an error */
1461 * Routine to seek on a directory
1465 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1469 long int l_offset
= offset
; /* Handle problems of size */
1470 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1471 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1472 TALLOC_CTX
*frame
= talloc_stackframe();
1474 if (!context
|| !context
->internal
->initialized
) {
1482 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1490 /* Now, check what we were passed and see if it is OK ... */
1492 if (dirent
== NULL
) { /* Seek to the begining of the list */
1494 dir
->dir_next
= dir
->dir_list
;
1500 if (offset
== -1) { /* Seek to the end of the list */
1501 dir
->dir_next
= NULL
;
1506 /* Now, run down the list and make sure that the entry is OK */
1507 /* This may need to be changed if we change the format of the list */
1509 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1510 errno
= EINVAL
; /* Bad entry */
1515 dir
->dir_next
= list_ent
;
1522 * Routine to fstat a dir
1526 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1531 if (!context
|| !context
->internal
->initialized
) {
1537 /* No code yet ... */
1542 SMBC_chmod_ctx(SMBCCTX
*context
,
1546 SMBCSRV
*srv
= NULL
;
1547 char *server
= NULL
;
1550 char *password
= NULL
;
1551 char *workgroup
= NULL
;
1552 char *targetpath
= NULL
;
1553 struct cli_state
*targetcli
= NULL
;
1556 TALLOC_CTX
*frame
= talloc_stackframe();
1558 if (!context
|| !context
->internal
->initialized
) {
1560 errno
= EINVAL
; /* Best I can think of ... */
1571 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1573 if (SMBC_parse_path(frame
,
1588 if (!user
|| user
[0] == (char)0) {
1589 user
= talloc_strdup(frame
, smbc_getUser(context
));
1597 srv
= SMBC_server(frame
, context
, True
,
1598 server
, share
, &workgroup
, &user
, &password
);
1602 return -1; /* errno set by SMBC_server */
1605 /*d_printf(">>>unlink: resolving %s\n", path);*/
1606 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1608 &targetcli
, &targetpath
)) {
1609 d_printf("Could not resolve %s\n", path
);
1617 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= FILE_ATTRIBUTE_READONLY
;
1618 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= FILE_ATTRIBUTE_ARCHIVE
;
1619 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= FILE_ATTRIBUTE_SYSTEM
;
1620 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= FILE_ATTRIBUTE_HIDDEN
;
1622 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1623 errno
= SMBC_errno(context
, targetcli
);
1633 SMBC_utimes_ctx(SMBCCTX
*context
,
1635 struct timeval
*tbuf
)
1637 SMBCSRV
*srv
= NULL
;
1638 char *server
= NULL
;
1641 char *password
= NULL
;
1642 char *workgroup
= NULL
;
1646 TALLOC_CTX
*frame
= talloc_stackframe();
1648 if (!context
|| !context
->internal
->initialized
) {
1650 errno
= EINVAL
; /* Best I can think of ... */
1662 access_time
= write_time
= time(NULL
);
1664 access_time
= tbuf
[0].tv_sec
;
1665 write_time
= tbuf
[1].tv_sec
;
1673 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1674 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1675 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1679 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1680 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1681 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1685 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1686 fname
, atimebuf
, mtimebuf
);
1689 if (SMBC_parse_path(frame
,
1704 if (!user
|| user
[0] == (char)0) {
1705 user
= talloc_strdup(frame
, smbc_getUser(context
));
1713 srv
= SMBC_server(frame
, context
, True
,
1714 server
, share
, &workgroup
, &user
, &password
);
1718 return -1; /* errno set by SMBC_server */
1721 if (!SMBC_setatr(context
, srv
, path
,
1722 0, access_time
, write_time
, 0, 0)) {
1724 return -1; /* errno set by SMBC_setatr */
1732 * Routine to unlink() a file
1736 SMBC_unlink_ctx(SMBCCTX
*context
,
1739 char *server
= NULL
;
1742 char *password
= NULL
;
1743 char *workgroup
= NULL
;
1745 char *targetpath
= NULL
;
1746 struct cli_state
*targetcli
= NULL
;
1747 SMBCSRV
*srv
= NULL
;
1748 TALLOC_CTX
*frame
= talloc_stackframe();
1750 if (!context
|| !context
->internal
->initialized
) {
1752 errno
= EINVAL
; /* Best I can think of ... */
1765 if (SMBC_parse_path(frame
,
1780 if (!user
|| user
[0] == (char)0) {
1781 user
= talloc_strdup(frame
, smbc_getUser(context
));
1789 srv
= SMBC_server(frame
, context
, True
,
1790 server
, share
, &workgroup
, &user
, &password
);
1794 return -1; /* SMBC_server sets errno */
1798 /*d_printf(">>>unlink: resolving %s\n", path);*/
1799 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1801 &targetcli
, &targetpath
)) {
1802 d_printf("Could not resolve %s\n", path
);
1807 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1809 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
))) {
1811 errno
= SMBC_errno(context
, targetcli
);
1813 if (errno
== EACCES
) { /* Check if the file is a directory */
1818 struct timespec write_time_ts
;
1819 struct timespec access_time_ts
;
1820 struct timespec change_time_ts
;
1823 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1830 /* Hmmm, bad error ... What? */
1832 errno
= SMBC_errno(context
, targetcli
);
1839 if (IS_DOS_DIR(mode
))
1842 errno
= saverr
; /* Restore this */
1853 return 0; /* Success ... */
1858 * Routine to rename() a file
1862 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1867 char *server1
= NULL
;
1868 char *share1
= NULL
;
1869 char *server2
= NULL
;
1870 char *share2
= NULL
;
1873 char *password1
= NULL
;
1874 char *password2
= NULL
;
1875 char *workgroup
= NULL
;
1878 char *targetpath1
= NULL
;
1879 char *targetpath2
= NULL
;
1880 struct cli_state
*targetcli1
= NULL
;
1881 struct cli_state
*targetcli2
= NULL
;
1882 SMBCSRV
*srv
= NULL
;
1883 TALLOC_CTX
*frame
= talloc_stackframe();
1885 if (!ocontext
|| !ncontext
||
1886 !ocontext
->internal
->initialized
||
1887 !ncontext
->internal
->initialized
) {
1889 errno
= EINVAL
; /* Best I can think of ... */
1894 if (!oname
|| !nname
) {
1900 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1902 if (SMBC_parse_path(frame
,
1917 if (!user1
|| user1
[0] == (char)0) {
1918 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1926 if (SMBC_parse_path(frame
,
1941 if (!user2
|| user2
[0] == (char)0) {
1942 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1950 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1951 strcmp(user1
, user2
)) {
1952 /* Can't rename across file systems, or users?? */
1958 srv
= SMBC_server(frame
, ocontext
, True
,
1959 server1
, share1
, &workgroup
, &user1
, &password1
);
1966 /* set the credentials to make DFS work */
1967 smbc_set_credentials_with_fallback(ocontext
,
1972 /*d_printf(">>>rename: resolving %s\n", path1);*/
1973 if (!cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1976 &targetcli1
, &targetpath1
)) {
1977 d_printf("Could not resolve %s\n", path1
);
1983 /* set the credentials to make DFS work */
1984 smbc_set_credentials_with_fallback(ncontext
,
1989 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1990 /*d_printf(">>>rename: resolving %s\n", path2);*/
1991 if (!cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1994 &targetcli2
, &targetpath2
)) {
1995 d_printf("Could not resolve %s\n", path2
);
2000 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2002 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
2003 strcmp(targetcli1
->share
, targetcli2
->share
))
2005 /* can't rename across file systems */
2011 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2012 int eno
= SMBC_errno(ocontext
, targetcli1
);
2014 if (eno
!= EEXIST
||
2015 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
)) ||
2016 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2026 return 0; /* Success */