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 "../librpc/gen_ndr/cli_srvsvc.h"
32 * Routine to open a directory
33 * We accept the URL syntax explained in SMBC_parse_path(), above.
37 remove_dir(SMBCFILE
*dir
)
39 struct smbc_dir_list
*d
,*f
;
51 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
56 add_dirent(SMBCFILE
*dir
,
61 struct smbc_dirent
*dirent
;
63 int name_length
= (name
== NULL
? 0 : strlen(name
));
64 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
67 * Allocate space for the dirent, which must be increased by the
68 * size of the name and the comment and 1 each for the null terminator.
71 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
73 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
77 dir
->dir_error
= ENOMEM
;
84 if (dir
->dir_list
== NULL
) {
86 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
90 dir
->dir_error
= ENOMEM
;
94 ZERO_STRUCTP(dir
->dir_list
);
96 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
100 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
102 if (!dir
->dir_end
->next
) {
105 dir
->dir_error
= ENOMEM
;
109 ZERO_STRUCTP(dir
->dir_end
->next
);
111 dir
->dir_end
= dir
->dir_end
->next
;
114 dir
->dir_end
->next
= NULL
;
115 dir
->dir_end
->dirent
= dirent
;
117 dirent
->smbc_type
= type
;
118 dirent
->namelen
= name_length
;
119 dirent
->commentlen
= comment_len
;
120 dirent
->dirlen
= size
;
123 * dirent->namelen + 1 includes the null (no null termination needed)
124 * Ditto for dirent->commentlen.
125 * The space for the two null bytes was allocated.
127 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
128 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
129 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
136 list_unique_wg_fn(const char *name
,
141 SMBCFILE
*dir
= (SMBCFILE
*)state
;
142 struct smbc_dir_list
*dir_list
;
143 struct smbc_dirent
*dirent
;
147 dirent_type
= dir
->dir_type
;
149 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
151 /* An error occurred, what do we do? */
152 /* FIXME: Add some code here */
155 /* Point to the one just added */
156 dirent
= dir
->dir_end
->dirent
;
158 /* See if this was a duplicate */
159 for (dir_list
= dir
->dir_list
;
160 dir_list
!= dir
->dir_end
;
161 dir_list
= dir_list
->next
) {
163 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
164 /* Duplicate. End end of list need to be removed. */
168 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
169 /* Found the end of the list. Remove it. */
170 dir
->dir_end
= dir_list
;
171 free(dir_list
->next
);
173 dir_list
->next
= NULL
;
180 list_fn(const char *name
,
185 SMBCFILE
*dir
= (SMBCFILE
*)state
;
189 * We need to process the type a little ...
191 * Disk share = 0x00000000
192 * Print share = 0x00000001
193 * Comms share = 0x00000002 (obsolete?)
194 * IPC$ share = 0x00000003
196 * administrative shares:
197 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
200 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
204 dirent_type
= SMBC_FILE_SHARE
;
208 dirent_type
= SMBC_PRINTER_SHARE
;
212 dirent_type
= SMBC_COMMS_SHARE
;
217 dirent_type
= SMBC_IPC_SHARE
;
221 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
226 dirent_type
= dir
->dir_type
;
229 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
231 /* An error occurred, what do we do? */
232 /* FIXME: Add some code here */
238 dir_list_fn(const char *mnt
,
239 struct file_info
*finfo
,
244 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
245 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
247 /* Handle an error ... */
249 /* FIXME: Add some code ... */
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;
276 /* Open the server service pipe */
277 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
279 if (!NT_STATUS_IS_OK(nt_status
)) {
280 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
284 ZERO_STRUCT(info_ctr
);
288 info_ctr
.ctr
.ctr1
= &ctr1
;
290 /* Issue the NetShareEnum RPC call and retrieve the response */
291 nt_status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, talloc_tos(),
299 /* Was it successful? */
300 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
) ||
301 total_entries
== 0) {
302 /* Nope. Go clean up. */
306 /* For each returned entry... */
307 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
309 /* pull out the share name */
310 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
312 /* pull out the share's comment */
313 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
315 /* Get the type value */
316 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
318 /* Add this share to the list */
319 (*fn
)(name
, type
, comment
, state
);
323 /* Close the server service pipe */
324 TALLOC_FREE(pipe_hnd
);
326 /* Tell 'em if it worked */
327 return W_ERROR_IS_OK(result
) ? 0 : -1;
332 * Verify that the options specified in a URL are valid
335 SMBC_check_options(char *server
,
340 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
341 "path='%s' options='%s'\n",
342 server
, share
, path
, options
));
344 /* No options at all is always ok */
345 if (! *options
) return 0;
347 /* Currently, we don't support any options. */
353 SMBC_opendir_ctx(SMBCCTX
*context
,
360 char *password
= NULL
;
361 char *options
= NULL
;
362 char *workgroup
= NULL
;
367 SMBCFILE
*dir
= NULL
;
368 struct sockaddr_storage rem_ss
;
369 TALLOC_CTX
*frame
= talloc_stackframe();
371 if (!context
|| !context
->internal
->initialized
) {
372 DEBUG(4, ("no valid context\n"));
373 errno
= EINVAL
+ 8192;
380 DEBUG(4, ("no valid fname\n"));
381 errno
= EINVAL
+ 8193;
386 if (SMBC_parse_path(frame
,
396 DEBUG(4, ("no valid path\n"));
397 errno
= EINVAL
+ 8194;
402 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
403 "path='%s' options='%s'\n",
404 fname
, server
, share
, path
, options
));
406 /* Ensure the options are valid */
407 if (SMBC_check_options(server
, share
, path
, options
)) {
408 DEBUG(4, ("unacceptable options (%s)\n", options
));
409 errno
= EINVAL
+ 8195;
414 if (!user
|| user
[0] == (char)0) {
415 user
= talloc_strdup(frame
, smbc_getUser(context
));
423 dir
= SMB_MALLOC_P(SMBCFILE
);
434 dir
->fname
= SMB_STRDUP(fname
);
438 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
440 if (server
[0] == (char)0) {
445 struct ip_service
*ip_list
;
446 struct ip_service server_addr
;
447 struct user_auth_info u_info
;
449 if (share
[0] != (char)0 || path
[0] != (char)0) {
451 errno
= EINVAL
+ 8196;
453 SAFE_FREE(dir
->fname
);
460 /* Determine how many local master browsers to query */
461 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
463 : smbc_getOptionBrowseMaxLmbCount(context
));
465 memset(&u_info
, '\0', sizeof(u_info
));
466 u_info
.username
= talloc_strdup(frame
,user
);
467 u_info
.password
= talloc_strdup(frame
,password
);
468 if (!u_info
.username
|| !u_info
.password
) {
470 SAFE_FREE(dir
->fname
);
478 * We have server and share and path empty but options
479 * requesting that we scan all master browsers for their list
480 * of workgroups/domains. This implies that we must first try
481 * broadcast queries to find all master browsers, and if that
482 * doesn't work, then try our other methods which return only
483 * a single master browser.
487 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
493 if (!find_master_ip(workgroup
, &server_addr
.ss
)) {
496 SAFE_FREE(dir
->fname
);
504 ip_list
= (struct ip_service
*)memdup(
505 &server_addr
, sizeof(server_addr
));
506 if (ip_list
== NULL
) {
514 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
515 char addr
[INET6_ADDRSTRLEN
];
517 struct cli_state
*cli
= NULL
;
519 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
520 DEBUG(99, ("Found master browser %d of %d: %s\n",
521 i
+1, MAX(count
, max_lmb_count
),
524 cli
= get_ipc_connect_master_ip(talloc_tos(),
528 /* cli == NULL is the master browser refused to talk or
529 could not be found */
534 workgroup
= talloc_strdup(frame
, wg_ptr
);
535 server
= talloc_strdup(frame
, cli
->desthost
);
539 if (!workgroup
|| !server
) {
545 DEBUG(4, ("using workgroup %s %s\n",
549 * For each returned master browser IP address, get a
550 * connection to IPC$ on the server if we do not
551 * already have one, and determine the
552 * workgroups/domains that it knows about.
555 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
556 &workgroup
, &user
, &password
);
562 dir
->dir_type
= SMBC_WORKGROUP
;
564 /* Now, list the stuff ... */
566 if (!cli_NetServerEnum(srv
->cli
,
578 * Server not an empty string ... Check the rest and see what
581 if (*share
== '\0') {
584 /* Should not have empty share with path */
585 errno
= EINVAL
+ 8197;
587 SAFE_FREE(dir
->fname
);
596 * We don't know if <server> is really a server name
597 * or is a workgroup/domain name. If we already have
598 * a server structure for it, we'll use it.
599 * Otherwise, check to see if <server><1D>,
600 * <server><1B>, or <server><20> translates. We check
601 * to see if <server> is an IP address first.
605 * See if we have an existing server. Do not
606 * establish a connection if one does not already
609 srv
= SMBC_server(frame
, context
, False
,
611 &workgroup
, &user
, &password
);
614 * If no existing server and not an IP addr, look for
618 !is_ipaddress(server
) &&
619 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
620 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
622 * "server" is actually a workgroup name,
623 * not a server. Make this clear.
625 char *wgroup
= server
;
628 dir
->dir_type
= SMBC_SERVER
;
631 * Get the backup list ...
633 if (!name_status_find(wgroup
, 0, 0,
634 &rem_ss
, buserver
)) {
635 char addr
[INET6_ADDRSTRLEN
];
637 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
638 DEBUG(0,("Could not get name of "
639 "local/domain master browser "
640 "for workgroup %s from "
645 SAFE_FREE(dir
->fname
);
655 * Get a connection to IPC$ on the server if
656 * we do not already have one
658 srv
= SMBC_server(frame
, context
, True
,
663 DEBUG(0, ("got no contact to IPC$\n"));
665 SAFE_FREE(dir
->fname
);
675 /* Now, list the servers ... */
676 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
681 SAFE_FREE(dir
->fname
);
688 (resolve_name(server
, &rem_ss
, 0x20, false))) {
691 * If we hadn't found the server, get one now
694 srv
= SMBC_server(frame
, context
, True
,
702 SAFE_FREE(dir
->fname
);
710 dir
->dir_type
= SMBC_FILE_SHARE
;
713 /* List the shares ... */
715 if (net_share_enum_rpc(
724 errno
= cli_errno(srv
->cli
);
726 SAFE_FREE(dir
->fname
);
734 /* Neither the workgroup nor server exists */
735 errno
= ECONNREFUSED
;
737 SAFE_FREE(dir
->fname
);
747 * The server and share are specified ... work from
751 struct cli_state
*targetcli
;
754 /* We connect to the server and list the directory */
755 dir
->dir_type
= SMBC_FILE_SHARE
;
757 srv
= SMBC_server(frame
, context
, True
, server
, share
,
758 &workgroup
, &user
, &password
);
762 SAFE_FREE(dir
->fname
);
771 /* Now, list the files ... */
773 p
= path
+ strlen(path
);
774 path
= talloc_asprintf_append(path
, "\\*");
777 SAFE_FREE(dir
->fname
);
784 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
786 &targetcli
, &targetpath
)) {
787 d_printf("Could not resolve %s\n", path
);
789 SAFE_FREE(dir
->fname
);
796 status
= cli_list(targetcli
, targetpath
,
797 aDIR
| aSYSTEM
| aHIDDEN
,
798 dir_list_fn
, (void *)dir
);
799 if (!NT_STATUS_IS_OK(status
)) {
801 SAFE_FREE(dir
->fname
);
804 saved_errno
= SMBC_errno(context
, targetcli
);
806 if (saved_errno
== EINVAL
) {
808 * See if they asked to opendir
809 * something other than a directory.
810 * If so, the converted error value we
811 * got would have been EINVAL rather
814 *p
= '\0'; /* restore original path */
816 if (SMBC_getatr(context
, srv
, path
,
818 NULL
, NULL
, NULL
, NULL
,
820 ! IS_DOS_DIR(mode
)) {
822 /* It is. Correct the error value */
823 saved_errno
= ENOTDIR
;
828 * If there was an error and the server is no
831 if (cli_is_error(targetcli
) &&
832 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
834 /* ... then remove it. */
835 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
838 * We could not remove the
839 * server completely, remove
840 * it from the cache so we
841 * will not get it again. It
842 * will be removed when the
843 * last file/dir is closed.
845 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
857 DLIST_ADD(context
->internal
->files
, dir
);
864 * Routine to close a directory
868 SMBC_closedir_ctx(SMBCCTX
*context
,
871 TALLOC_CTX
*frame
= talloc_stackframe();
873 if (!context
|| !context
->internal
->initialized
) {
879 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
885 remove_dir(dir
); /* Clean it up */
887 DLIST_REMOVE(context
->internal
->files
, dir
);
891 SAFE_FREE(dir
->fname
);
892 SAFE_FREE(dir
); /* Free the space too */
901 smbc_readdir_internal(SMBCCTX
* context
,
902 struct smbc_dirent
*dest
,
903 struct smbc_dirent
*src
,
906 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
908 /* url-encode the name. get back remaining buffer space */
910 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
912 /* We now know the name length */
913 dest
->namelen
= strlen(dest
->name
);
915 /* Save the pointer to the beginning of the comment */
916 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
918 /* Copy the comment */
919 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
920 dest
->comment
[max_namebuf_len
- 1] = '\0';
922 /* Save other fields */
923 dest
->smbc_type
= src
->smbc_type
;
924 dest
->commentlen
= strlen(dest
->comment
);
925 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
929 /* No encoding. Just copy the entry as is. */
930 memcpy(dest
, src
, src
->dirlen
);
931 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
937 * Routine to get a directory entry
941 SMBC_readdir_ctx(SMBCCTX
*context
,
945 struct smbc_dirent
*dirp
, *dirent
;
946 TALLOC_CTX
*frame
= talloc_stackframe();
948 /* Check that all is ok first ... */
950 if (!context
|| !context
->internal
->initialized
) {
953 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
959 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
962 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
968 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
971 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
977 if (!dir
->dir_next
) {
982 dirent
= dir
->dir_next
->dirent
;
991 dirp
= &context
->internal
->dirent
;
992 maxlen
= sizeof(context
->internal
->_dirent_name
);
994 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
996 dir
->dir_next
= dir
->dir_next
->next
;
1003 * Routine to get directory entries
1007 SMBC_getdents_ctx(SMBCCTX
*context
,
1009 struct smbc_dirent
*dirp
,
1015 char *ndir
= (char *)dirp
;
1016 struct smbc_dir_list
*dirlist
;
1017 TALLOC_CTX
*frame
= talloc_stackframe();
1019 /* Check that all is ok first ... */
1021 if (!context
|| !context
->internal
->initialized
) {
1029 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1037 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1046 * Now, retrieve the number of entries that will fit in what was passed
1047 * We have to figure out if the info is in the list, or we need to
1048 * send a request to the server to get the info.
1051 while ((dirlist
= dir
->dir_next
)) {
1052 struct smbc_dirent
*dirent
;
1054 if (!dirlist
->dirent
) {
1056 errno
= ENOENT
; /* Bad error */
1062 /* Do urlencoding of next entry, if so selected */
1063 dirent
= &context
->internal
->dirent
;
1064 maxlen
= sizeof(context
->internal
->_dirent_name
);
1065 smbc_readdir_internal(context
, dirent
,
1066 dirlist
->dirent
, maxlen
);
1068 reqd
= dirent
->dirlen
;
1072 if (rem
< count
) { /* We managed to copy something */
1079 else { /* Nothing copied ... */
1081 errno
= EINVAL
; /* Not enough space ... */
1089 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1091 ((struct smbc_dirent
*)ndir
)->comment
=
1092 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
1100 dir
->dir_next
= dirlist
= dirlist
-> next
;
1113 * Routine to create a directory ...
1117 SMBC_mkdir_ctx(SMBCCTX
*context
,
1121 SMBCSRV
*srv
= NULL
;
1122 char *server
= NULL
;
1125 char *password
= NULL
;
1126 char *workgroup
= NULL
;
1128 char *targetpath
= NULL
;
1129 struct cli_state
*targetcli
= NULL
;
1130 TALLOC_CTX
*frame
= talloc_stackframe();
1132 if (!context
|| !context
->internal
->initialized
) {
1144 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1146 if (SMBC_parse_path(frame
,
1161 if (!user
|| user
[0] == (char)0) {
1162 user
= talloc_strdup(frame
, smbc_getUser(context
));
1170 srv
= SMBC_server(frame
, context
, True
,
1171 server
, share
, &workgroup
, &user
, &password
);
1176 return -1; /* errno set by SMBC_server */
1180 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1181 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1183 &targetcli
, &targetpath
)) {
1184 d_printf("Could not resolve %s\n", path
);
1189 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1191 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1192 errno
= SMBC_errno(context
, targetcli
);
1204 * Our list function simply checks to see if a directory is not empty
1208 rmdir_list_fn(const char *mnt
,
1209 struct file_info
*finfo
,
1213 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1214 strncmp(finfo
->name
, "..", 2) != 0) {
1215 bool *smbc_rmdir_dirempty
= (bool *)state
;
1216 *smbc_rmdir_dirempty
= false;
1221 * Routine to remove a directory
1225 SMBC_rmdir_ctx(SMBCCTX
*context
,
1228 SMBCSRV
*srv
= NULL
;
1229 char *server
= NULL
;
1232 char *password
= NULL
;
1233 char *workgroup
= NULL
;
1235 char *targetpath
= NULL
;
1236 struct cli_state
*targetcli
= NULL
;
1237 TALLOC_CTX
*frame
= talloc_stackframe();
1239 if (!context
|| !context
->internal
->initialized
) {
1251 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1253 if (SMBC_parse_path(frame
,
1268 if (!user
|| user
[0] == (char)0) {
1269 user
= talloc_strdup(frame
, smbc_getUser(context
));
1277 srv
= SMBC_server(frame
, context
, True
,
1278 server
, share
, &workgroup
, &user
, &password
);
1283 return -1; /* errno set by SMBC_server */
1287 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1288 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1290 &targetcli
, &targetpath
)) {
1291 d_printf("Could not resolve %s\n", path
);
1296 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1298 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1300 errno
= SMBC_errno(context
, targetcli
);
1302 if (errno
== EACCES
) { /* Check if the dir empty or not */
1304 /* Local storage to avoid buffer overflows */
1306 bool smbc_rmdir_dirempty
= true;
1309 lpath
= talloc_asprintf(frame
, "%s\\*",
1317 status
= cli_list(targetcli
, lpath
,
1318 aDIR
| aSYSTEM
| aHIDDEN
,
1320 &smbc_rmdir_dirempty
);
1322 if (!NT_STATUS_IS_OK(status
)) {
1323 /* Fix errno to ignore latest error ... */
1324 DEBUG(5, ("smbc_rmdir: "
1325 "cli_list returned an error: %d\n",
1326 SMBC_errno(context
, targetcli
)));
1331 if (smbc_rmdir_dirempty
)
1349 * Routine to return the current directory position
1353 SMBC_telldir_ctx(SMBCCTX
*context
,
1356 TALLOC_CTX
*frame
= talloc_stackframe();
1358 if (!context
|| !context
->internal
->initialized
) {
1366 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1374 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1382 /* See if we're already at the end. */
1383 if (dir
->dir_next
== NULL
) {
1390 * We return the pointer here as the offset
1393 return (off_t
)(long)dir
->dir_next
->dirent
;
1397 * A routine to run down the list and see if the entry is OK
1400 static struct smbc_dir_list
*
1401 check_dir_ent(struct smbc_dir_list
*list
,
1402 struct smbc_dirent
*dirent
)
1405 /* Run down the list looking for what we want */
1409 struct smbc_dir_list
*tmp
= list
;
1413 if (tmp
->dirent
== dirent
)
1422 return NULL
; /* Not found, or an error */
1428 * Routine to seek on a directory
1432 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1436 long int l_offset
= offset
; /* Handle problems of size */
1437 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1438 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1439 TALLOC_CTX
*frame
= talloc_stackframe();
1441 if (!context
|| !context
->internal
->initialized
) {
1449 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1457 /* Now, check what we were passed and see if it is OK ... */
1459 if (dirent
== NULL
) { /* Seek to the begining of the list */
1461 dir
->dir_next
= dir
->dir_list
;
1467 if (offset
== -1) { /* Seek to the end of the list */
1468 dir
->dir_next
= NULL
;
1473 /* Now, run down the list and make sure that the entry is OK */
1474 /* This may need to be changed if we change the format of the list */
1476 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1477 errno
= EINVAL
; /* Bad entry */
1482 dir
->dir_next
= list_ent
;
1489 * Routine to fstat a dir
1493 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1498 if (!context
|| !context
->internal
->initialized
) {
1504 /* No code yet ... */
1509 SMBC_chmod_ctx(SMBCCTX
*context
,
1513 SMBCSRV
*srv
= NULL
;
1514 char *server
= NULL
;
1517 char *password
= NULL
;
1518 char *workgroup
= NULL
;
1519 char *targetpath
= NULL
;
1520 struct cli_state
*targetcli
= NULL
;
1523 TALLOC_CTX
*frame
= talloc_stackframe();
1525 if (!context
|| !context
->internal
->initialized
) {
1527 errno
= EINVAL
; /* Best I can think of ... */
1538 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1540 if (SMBC_parse_path(frame
,
1555 if (!user
|| user
[0] == (char)0) {
1556 user
= talloc_strdup(frame
, smbc_getUser(context
));
1564 srv
= SMBC_server(frame
, context
, True
,
1565 server
, share
, &workgroup
, &user
, &password
);
1569 return -1; /* errno set by SMBC_server */
1572 /*d_printf(">>>unlink: resolving %s\n", path);*/
1573 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1575 &targetcli
, &targetpath
)) {
1576 d_printf("Could not resolve %s\n", path
);
1584 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
1585 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
1586 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
1587 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
1589 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1590 errno
= SMBC_errno(context
, targetcli
);
1600 SMBC_utimes_ctx(SMBCCTX
*context
,
1602 struct timeval
*tbuf
)
1604 SMBCSRV
*srv
= NULL
;
1605 char *server
= NULL
;
1608 char *password
= NULL
;
1609 char *workgroup
= NULL
;
1613 TALLOC_CTX
*frame
= talloc_stackframe();
1615 if (!context
|| !context
->internal
->initialized
) {
1617 errno
= EINVAL
; /* Best I can think of ... */
1629 access_time
= write_time
= time(NULL
);
1631 access_time
= tbuf
[0].tv_sec
;
1632 write_time
= tbuf
[1].tv_sec
;
1640 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1641 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1642 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1646 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1647 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1648 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1652 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1653 fname
, atimebuf
, mtimebuf
);
1656 if (SMBC_parse_path(frame
,
1671 if (!user
|| user
[0] == (char)0) {
1672 user
= talloc_strdup(frame
, smbc_getUser(context
));
1680 srv
= SMBC_server(frame
, context
, True
,
1681 server
, share
, &workgroup
, &user
, &password
);
1685 return -1; /* errno set by SMBC_server */
1688 if (!SMBC_setatr(context
, srv
, path
,
1689 0, access_time
, write_time
, 0, 0)) {
1691 return -1; /* errno set by SMBC_setatr */
1699 * Routine to unlink() a file
1703 SMBC_unlink_ctx(SMBCCTX
*context
,
1706 char *server
= NULL
;
1709 char *password
= NULL
;
1710 char *workgroup
= NULL
;
1712 char *targetpath
= NULL
;
1713 struct cli_state
*targetcli
= NULL
;
1714 SMBCSRV
*srv
= NULL
;
1715 TALLOC_CTX
*frame
= talloc_stackframe();
1717 if (!context
|| !context
->internal
->initialized
) {
1719 errno
= EINVAL
; /* Best I can think of ... */
1732 if (SMBC_parse_path(frame
,
1747 if (!user
|| user
[0] == (char)0) {
1748 user
= talloc_strdup(frame
, smbc_getUser(context
));
1756 srv
= SMBC_server(frame
, context
, True
,
1757 server
, share
, &workgroup
, &user
, &password
);
1761 return -1; /* SMBC_server sets errno */
1765 /*d_printf(">>>unlink: resolving %s\n", path);*/
1766 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1768 &targetcli
, &targetpath
)) {
1769 d_printf("Could not resolve %s\n", path
);
1774 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1776 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, aSYSTEM
| aHIDDEN
))) {
1778 errno
= SMBC_errno(context
, targetcli
);
1780 if (errno
== EACCES
) { /* Check if the file is a directory */
1785 struct timespec write_time_ts
;
1786 struct timespec access_time_ts
;
1787 struct timespec change_time_ts
;
1790 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1797 /* Hmmm, bad error ... What? */
1799 errno
= SMBC_errno(context
, targetcli
);
1806 if (IS_DOS_DIR(mode
))
1809 errno
= saverr
; /* Restore this */
1820 return 0; /* Success ... */
1825 * Routine to rename() a file
1829 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1834 char *server1
= NULL
;
1835 char *share1
= NULL
;
1836 char *server2
= NULL
;
1837 char *share2
= NULL
;
1840 char *password1
= NULL
;
1841 char *password2
= NULL
;
1842 char *workgroup
= NULL
;
1845 char *targetpath1
= NULL
;
1846 char *targetpath2
= NULL
;
1847 struct cli_state
*targetcli1
= NULL
;
1848 struct cli_state
*targetcli2
= NULL
;
1849 SMBCSRV
*srv
= NULL
;
1850 TALLOC_CTX
*frame
= talloc_stackframe();
1852 if (!ocontext
|| !ncontext
||
1853 !ocontext
->internal
->initialized
||
1854 !ncontext
->internal
->initialized
) {
1856 errno
= EINVAL
; /* Best I can think of ... */
1861 if (!oname
|| !nname
) {
1867 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1869 if (SMBC_parse_path(frame
,
1884 if (!user1
|| user1
[0] == (char)0) {
1885 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1893 if (SMBC_parse_path(frame
,
1908 if (!user2
|| user2
[0] == (char)0) {
1909 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1917 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1918 strcmp(user1
, user2
)) {
1919 /* Can't rename across file systems, or users?? */
1925 srv
= SMBC_server(frame
, ocontext
, True
,
1926 server1
, share1
, &workgroup
, &user1
, &password1
);
1933 /* set the credentials to make DFS work */
1934 smbc_set_credentials_with_fallback(ocontext
,
1939 /*d_printf(">>>rename: resolving %s\n", path1);*/
1940 if (!cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1943 &targetcli1
, &targetpath1
)) {
1944 d_printf("Could not resolve %s\n", path1
);
1950 /* set the credentials to make DFS work */
1951 smbc_set_credentials_with_fallback(ncontext
,
1956 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1957 /*d_printf(">>>rename: resolving %s\n", path2);*/
1958 if (!cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1961 &targetcli2
, &targetpath2
)) {
1962 d_printf("Could not resolve %s\n", path2
);
1967 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1969 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1970 strcmp(targetcli1
->share
, targetcli2
->share
))
1972 /* can't rename across file systems */
1978 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
1979 int eno
= SMBC_errno(ocontext
, targetcli1
);
1981 if (eno
!= EEXIST
||
1982 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, aSYSTEM
| aHIDDEN
)) ||
1983 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
1993 return 0; /* Success */