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) {
150 /* An error occurred, what do we do? */
151 /* FIXME: Add some code here */
152 /* Change cli_NetServerEnum to take a fn
153 returning NTSTATUS... JRA. */
156 /* Point to the one just added */
157 dirent
= dir
->dir_end
->dirent
;
159 /* See if this was a duplicate */
160 for (dir_list
= dir
->dir_list
;
161 dir_list
!= dir
->dir_end
;
162 dir_list
= dir_list
->next
) {
164 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
165 /* Duplicate. End end of list need to be removed. */
169 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
170 /* Found the end of the list. Remove it. */
171 dir
->dir_end
= dir_list
;
172 free(dir_list
->next
);
174 dir_list
->next
= NULL
;
181 list_fn(const char *name
,
186 SMBCFILE
*dir
= (SMBCFILE
*)state
;
190 * We need to process the type a little ...
192 * Disk share = 0x00000000
193 * Print share = 0x00000001
194 * Comms share = 0x00000002 (obsolete?)
195 * IPC$ share = 0x00000003
197 * administrative shares:
198 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
201 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
205 dirent_type
= SMBC_FILE_SHARE
;
209 dirent_type
= SMBC_PRINTER_SHARE
;
213 dirent_type
= SMBC_COMMS_SHARE
;
218 dirent_type
= SMBC_IPC_SHARE
;
222 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
227 dirent_type
= dir
->dir_type
;
230 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
231 /* An error occurred, what do we do? */
232 /* FIXME: Add some code here */
233 /* Change cli_NetServerEnum to take a fn
234 returning NTSTATUS... JRA. */
239 dir_list_fn(const char *mnt
,
240 struct file_info
*finfo
,
245 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
246 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
247 SMBCFILE
*dir
= (SMBCFILE
*)state
;
248 return map_nt_error_from_unix(dir
->dir_error
);
254 net_share_enum_rpc(struct cli_state
*cli
,
255 void (*fn
)(const char *name
,
263 uint32 preferred_len
= 0xffffffff;
265 struct srvsvc_NetShareInfoCtr info_ctr
;
266 struct srvsvc_NetShareCtr1 ctr1
;
268 fstring comment
= "";
269 struct rpc_pipe_client
*pipe_hnd
= NULL
;
271 uint32_t resume_handle
= 0;
272 uint32_t total_entries
= 0;
274 /* Open the server service pipe */
275 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
277 if (!NT_STATUS_IS_OK(nt_status
)) {
278 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
282 ZERO_STRUCT(info_ctr
);
286 info_ctr
.ctr
.ctr1
= &ctr1
;
288 /* Issue the NetShareEnum RPC call and retrieve the response */
289 nt_status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, talloc_tos(),
297 /* Was it successful? */
298 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
) ||
299 total_entries
== 0) {
300 /* Nope. Go clean up. */
304 /* For each returned entry... */
305 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
307 /* pull out the share name */
308 fstrcpy(name
, info_ctr
.ctr
.ctr1
->array
[i
].name
);
310 /* pull out the share's comment */
311 fstrcpy(comment
, info_ctr
.ctr
.ctr1
->array
[i
].comment
);
313 /* Get the type value */
314 type
= info_ctr
.ctr
.ctr1
->array
[i
].type
;
316 /* Add this share to the list */
317 (*fn
)(name
, type
, comment
, state
);
321 /* Close the server service pipe */
322 TALLOC_FREE(pipe_hnd
);
324 /* Tell 'em if it worked */
325 return W_ERROR_IS_OK(result
) ? 0 : -1;
330 * Verify that the options specified in a URL are valid
333 SMBC_check_options(char *server
,
338 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
339 "path='%s' options='%s'\n",
340 server
, share
, path
, options
));
342 /* No options at all is always ok */
343 if (! *options
) return 0;
345 /* Currently, we don't support any options. */
351 SMBC_opendir_ctx(SMBCCTX
*context
,
358 char *password
= NULL
;
359 char *options
= NULL
;
360 char *workgroup
= NULL
;
365 SMBCFILE
*dir
= NULL
;
366 struct sockaddr_storage rem_ss
;
367 TALLOC_CTX
*frame
= talloc_stackframe();
369 if (!context
|| !context
->internal
->initialized
) {
370 DEBUG(4, ("no valid context\n"));
371 errno
= EINVAL
+ 8192;
378 DEBUG(4, ("no valid fname\n"));
379 errno
= EINVAL
+ 8193;
384 if (SMBC_parse_path(frame
,
394 DEBUG(4, ("no valid path\n"));
395 errno
= EINVAL
+ 8194;
400 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
401 "path='%s' options='%s'\n",
402 fname
, server
, share
, path
, options
));
404 /* Ensure the options are valid */
405 if (SMBC_check_options(server
, share
, path
, options
)) {
406 DEBUG(4, ("unacceptable options (%s)\n", options
));
407 errno
= EINVAL
+ 8195;
412 if (!user
|| user
[0] == (char)0) {
413 user
= talloc_strdup(frame
, smbc_getUser(context
));
421 dir
= SMB_MALLOC_P(SMBCFILE
);
432 dir
->fname
= SMB_STRDUP(fname
);
436 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
438 if (server
[0] == (char)0) {
443 struct ip_service
*ip_list
;
444 struct ip_service server_addr
;
445 struct user_auth_info u_info
;
447 if (share
[0] != (char)0 || path
[0] != (char)0) {
449 errno
= EINVAL
+ 8196;
451 SAFE_FREE(dir
->fname
);
458 /* Determine how many local master browsers to query */
459 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
461 : smbc_getOptionBrowseMaxLmbCount(context
));
463 memset(&u_info
, '\0', sizeof(u_info
));
464 u_info
.username
= talloc_strdup(frame
,user
);
465 u_info
.password
= talloc_strdup(frame
,password
);
466 if (!u_info
.username
|| !u_info
.password
) {
468 SAFE_FREE(dir
->fname
);
476 * We have server and share and path empty but options
477 * requesting that we scan all master browsers for their list
478 * of workgroups/domains. This implies that we must first try
479 * broadcast queries to find all master browsers, and if that
480 * doesn't work, then try our other methods which return only
481 * a single master browser.
485 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
491 if (!find_master_ip(workgroup
, &server_addr
.ss
)) {
494 SAFE_FREE(dir
->fname
);
502 ip_list
= (struct ip_service
*)memdup(
503 &server_addr
, sizeof(server_addr
));
504 if (ip_list
== NULL
) {
512 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
513 char addr
[INET6_ADDRSTRLEN
];
515 struct cli_state
*cli
= NULL
;
517 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
518 DEBUG(99, ("Found master browser %d of %d: %s\n",
519 i
+1, MAX(count
, max_lmb_count
),
522 cli
= get_ipc_connect_master_ip(talloc_tos(),
526 /* cli == NULL is the master browser refused to talk or
527 could not be found */
532 workgroup
= talloc_strdup(frame
, wg_ptr
);
533 server
= talloc_strdup(frame
, cli
->desthost
);
537 if (!workgroup
|| !server
) {
543 DEBUG(4, ("using workgroup %s %s\n",
547 * For each returned master browser IP address, get a
548 * connection to IPC$ on the server if we do not
549 * already have one, and determine the
550 * workgroups/domains that it knows about.
553 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
554 &workgroup
, &user
, &password
);
560 dir
->dir_type
= SMBC_WORKGROUP
;
562 /* Now, list the stuff ... */
564 if (!cli_NetServerEnum(srv
->cli
,
576 * Server not an empty string ... Check the rest and see what
579 if (*share
== '\0') {
582 /* Should not have empty share with path */
583 errno
= EINVAL
+ 8197;
585 SAFE_FREE(dir
->fname
);
594 * We don't know if <server> is really a server name
595 * or is a workgroup/domain name. If we already have
596 * a server structure for it, we'll use it.
597 * Otherwise, check to see if <server><1D>,
598 * <server><1B>, or <server><20> translates. We check
599 * to see if <server> is an IP address first.
603 * See if we have an existing server. Do not
604 * establish a connection if one does not already
607 srv
= SMBC_server(frame
, context
, False
,
609 &workgroup
, &user
, &password
);
612 * If no existing server and not an IP addr, look for
616 !is_ipaddress(server
) &&
617 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
618 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
620 * "server" is actually a workgroup name,
621 * not a server. Make this clear.
623 char *wgroup
= server
;
626 dir
->dir_type
= SMBC_SERVER
;
629 * Get the backup list ...
631 if (!name_status_find(wgroup
, 0, 0,
632 &rem_ss
, buserver
)) {
633 char addr
[INET6_ADDRSTRLEN
];
635 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
636 DEBUG(0,("Could not get name of "
637 "local/domain master browser "
638 "for workgroup %s from "
643 SAFE_FREE(dir
->fname
);
653 * Get a connection to IPC$ on the server if
654 * we do not already have one
656 srv
= SMBC_server(frame
, context
, True
,
661 DEBUG(0, ("got no contact to IPC$\n"));
663 SAFE_FREE(dir
->fname
);
673 /* Now, list the servers ... */
674 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
679 SAFE_FREE(dir
->fname
);
686 (resolve_name(server
, &rem_ss
, 0x20, false))) {
689 * If we hadn't found the server, get one now
692 srv
= SMBC_server(frame
, context
, True
,
700 SAFE_FREE(dir
->fname
);
708 dir
->dir_type
= SMBC_FILE_SHARE
;
711 /* List the shares ... */
713 if (net_share_enum_rpc(
722 errno
= cli_errno(srv
->cli
);
724 SAFE_FREE(dir
->fname
);
732 /* Neither the workgroup nor server exists */
733 errno
= ECONNREFUSED
;
735 SAFE_FREE(dir
->fname
);
745 * The server and share are specified ... work from
749 struct cli_state
*targetcli
;
752 /* We connect to the server and list the directory */
753 dir
->dir_type
= SMBC_FILE_SHARE
;
755 srv
= SMBC_server(frame
, context
, True
, server
, share
,
756 &workgroup
, &user
, &password
);
760 SAFE_FREE(dir
->fname
);
769 /* Now, list the files ... */
771 p
= path
+ strlen(path
);
772 path
= talloc_asprintf_append(path
, "\\*");
775 SAFE_FREE(dir
->fname
);
782 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
784 &targetcli
, &targetpath
)) {
785 d_printf("Could not resolve %s\n", path
);
787 SAFE_FREE(dir
->fname
);
794 status
= cli_list(targetcli
, targetpath
,
795 aDIR
| aSYSTEM
| aHIDDEN
,
796 dir_list_fn
, (void *)dir
);
797 if (!NT_STATUS_IS_OK(status
)) {
799 SAFE_FREE(dir
->fname
);
802 saved_errno
= SMBC_errno(context
, targetcli
);
804 if (saved_errno
== EINVAL
) {
806 * See if they asked to opendir
807 * something other than a directory.
808 * If so, the converted error value we
809 * got would have been EINVAL rather
812 *p
= '\0'; /* restore original path */
814 if (SMBC_getatr(context
, srv
, path
,
816 NULL
, NULL
, NULL
, NULL
,
818 ! IS_DOS_DIR(mode
)) {
820 /* It is. Correct the error value */
821 saved_errno
= ENOTDIR
;
826 * If there was an error and the server is no
829 if (cli_is_error(targetcli
) &&
830 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
832 /* ... then remove it. */
833 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
836 * We could not remove the
837 * server completely, remove
838 * it from the cache so we
839 * will not get it again. It
840 * will be removed when the
841 * last file/dir is closed.
843 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
855 DLIST_ADD(context
->internal
->files
, dir
);
862 * Routine to close a directory
866 SMBC_closedir_ctx(SMBCCTX
*context
,
869 TALLOC_CTX
*frame
= talloc_stackframe();
871 if (!context
|| !context
->internal
->initialized
) {
877 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
883 remove_dir(dir
); /* Clean it up */
885 DLIST_REMOVE(context
->internal
->files
, dir
);
889 SAFE_FREE(dir
->fname
);
890 SAFE_FREE(dir
); /* Free the space too */
899 smbc_readdir_internal(SMBCCTX
* context
,
900 struct smbc_dirent
*dest
,
901 struct smbc_dirent
*src
,
904 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
906 /* url-encode the name. get back remaining buffer space */
908 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
910 /* We now know the name length */
911 dest
->namelen
= strlen(dest
->name
);
913 /* Save the pointer to the beginning of the comment */
914 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
916 /* Copy the comment */
917 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
918 dest
->comment
[max_namebuf_len
- 1] = '\0';
920 /* Save other fields */
921 dest
->smbc_type
= src
->smbc_type
;
922 dest
->commentlen
= strlen(dest
->comment
);
923 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
927 /* No encoding. Just copy the entry as is. */
928 memcpy(dest
, src
, src
->dirlen
);
929 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
935 * Routine to get a directory entry
939 SMBC_readdir_ctx(SMBCCTX
*context
,
943 struct smbc_dirent
*dirp
, *dirent
;
944 TALLOC_CTX
*frame
= talloc_stackframe();
946 /* Check that all is ok first ... */
948 if (!context
|| !context
->internal
->initialized
) {
951 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
957 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
960 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
966 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
969 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
975 if (!dir
->dir_next
) {
980 dirent
= dir
->dir_next
->dirent
;
989 dirp
= &context
->internal
->dirent
;
990 maxlen
= sizeof(context
->internal
->_dirent_name
);
992 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
994 dir
->dir_next
= dir
->dir_next
->next
;
1001 * Routine to get directory entries
1005 SMBC_getdents_ctx(SMBCCTX
*context
,
1007 struct smbc_dirent
*dirp
,
1013 char *ndir
= (char *)dirp
;
1014 struct smbc_dir_list
*dirlist
;
1015 TALLOC_CTX
*frame
= talloc_stackframe();
1017 /* Check that all is ok first ... */
1019 if (!context
|| !context
->internal
->initialized
) {
1027 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1035 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1044 * Now, retrieve the number of entries that will fit in what was passed
1045 * We have to figure out if the info is in the list, or we need to
1046 * send a request to the server to get the info.
1049 while ((dirlist
= dir
->dir_next
)) {
1050 struct smbc_dirent
*dirent
;
1051 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1053 if (!dirlist
->dirent
) {
1055 errno
= ENOENT
; /* Bad error */
1061 /* Do urlencoding of next entry, if so selected */
1062 dirent
= &context
->internal
->dirent
;
1063 maxlen
= sizeof(context
->internal
->_dirent_name
);
1064 smbc_readdir_internal(context
, dirent
,
1065 dirlist
->dirent
, maxlen
);
1067 reqd
= dirent
->dirlen
;
1071 if (rem
< count
) { /* We managed to copy something */
1078 else { /* Nothing copied ... */
1080 errno
= EINVAL
; /* Not enough space ... */
1088 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1090 currentEntry
->comment
= ¤tEntry
->name
[0] +
1091 dirent
->namelen
+ 1;
1096 /* Try and align the struct for the next entry
1097 on a valid pointer boundary by appending zeros */
1098 while((rem
> 0) && ((unsigned long long)ndir
& (sizeof(void*) - 1))) {
1102 currentEntry
->dirlen
++;
1105 dir
->dir_next
= dirlist
= dirlist
-> next
;
1118 * Routine to create a directory ...
1122 SMBC_mkdir_ctx(SMBCCTX
*context
,
1126 SMBCSRV
*srv
= NULL
;
1127 char *server
= NULL
;
1130 char *password
= NULL
;
1131 char *workgroup
= NULL
;
1133 char *targetpath
= NULL
;
1134 struct cli_state
*targetcli
= NULL
;
1135 TALLOC_CTX
*frame
= talloc_stackframe();
1137 if (!context
|| !context
->internal
->initialized
) {
1149 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1151 if (SMBC_parse_path(frame
,
1166 if (!user
|| user
[0] == (char)0) {
1167 user
= talloc_strdup(frame
, smbc_getUser(context
));
1175 srv
= SMBC_server(frame
, context
, True
,
1176 server
, share
, &workgroup
, &user
, &password
);
1181 return -1; /* errno set by SMBC_server */
1185 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1186 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1188 &targetcli
, &targetpath
)) {
1189 d_printf("Could not resolve %s\n", path
);
1194 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1196 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1197 errno
= SMBC_errno(context
, targetcli
);
1209 * Our list function simply checks to see if a directory is not empty
1213 rmdir_list_fn(const char *mnt
,
1214 struct file_info
*finfo
,
1218 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1219 strncmp(finfo
->name
, "..", 2) != 0) {
1220 bool *smbc_rmdir_dirempty
= (bool *)state
;
1221 *smbc_rmdir_dirempty
= false;
1223 return NT_STATUS_OK
;
1227 * Routine to remove a directory
1231 SMBC_rmdir_ctx(SMBCCTX
*context
,
1234 SMBCSRV
*srv
= NULL
;
1235 char *server
= NULL
;
1238 char *password
= NULL
;
1239 char *workgroup
= NULL
;
1241 char *targetpath
= NULL
;
1242 struct cli_state
*targetcli
= NULL
;
1243 TALLOC_CTX
*frame
= talloc_stackframe();
1245 if (!context
|| !context
->internal
->initialized
) {
1257 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1259 if (SMBC_parse_path(frame
,
1274 if (!user
|| user
[0] == (char)0) {
1275 user
= talloc_strdup(frame
, smbc_getUser(context
));
1283 srv
= SMBC_server(frame
, context
, True
,
1284 server
, share
, &workgroup
, &user
, &password
);
1289 return -1; /* errno set by SMBC_server */
1293 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1294 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1296 &targetcli
, &targetpath
)) {
1297 d_printf("Could not resolve %s\n", path
);
1302 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1304 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1306 errno
= SMBC_errno(context
, targetcli
);
1308 if (errno
== EACCES
) { /* Check if the dir empty or not */
1310 /* Local storage to avoid buffer overflows */
1312 bool smbc_rmdir_dirempty
= true;
1315 lpath
= talloc_asprintf(frame
, "%s\\*",
1323 status
= cli_list(targetcli
, lpath
,
1324 aDIR
| aSYSTEM
| aHIDDEN
,
1326 &smbc_rmdir_dirempty
);
1328 if (!NT_STATUS_IS_OK(status
)) {
1329 /* Fix errno to ignore latest error ... */
1330 DEBUG(5, ("smbc_rmdir: "
1331 "cli_list returned an error: %d\n",
1332 SMBC_errno(context
, targetcli
)));
1337 if (smbc_rmdir_dirempty
)
1355 * Routine to return the current directory position
1359 SMBC_telldir_ctx(SMBCCTX
*context
,
1362 TALLOC_CTX
*frame
= talloc_stackframe();
1364 if (!context
|| !context
->internal
->initialized
) {
1372 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1380 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1388 /* See if we're already at the end. */
1389 if (dir
->dir_next
== NULL
) {
1396 * We return the pointer here as the offset
1399 return (off_t
)(long)dir
->dir_next
->dirent
;
1403 * A routine to run down the list and see if the entry is OK
1406 static struct smbc_dir_list
*
1407 check_dir_ent(struct smbc_dir_list
*list
,
1408 struct smbc_dirent
*dirent
)
1411 /* Run down the list looking for what we want */
1415 struct smbc_dir_list
*tmp
= list
;
1419 if (tmp
->dirent
== dirent
)
1428 return NULL
; /* Not found, or an error */
1434 * Routine to seek on a directory
1438 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1442 long int l_offset
= offset
; /* Handle problems of size */
1443 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1444 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1445 TALLOC_CTX
*frame
= talloc_stackframe();
1447 if (!context
|| !context
->internal
->initialized
) {
1455 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1463 /* Now, check what we were passed and see if it is OK ... */
1465 if (dirent
== NULL
) { /* Seek to the begining of the list */
1467 dir
->dir_next
= dir
->dir_list
;
1473 if (offset
== -1) { /* Seek to the end of the list */
1474 dir
->dir_next
= NULL
;
1479 /* Now, run down the list and make sure that the entry is OK */
1480 /* This may need to be changed if we change the format of the list */
1482 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1483 errno
= EINVAL
; /* Bad entry */
1488 dir
->dir_next
= list_ent
;
1495 * Routine to fstat a dir
1499 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1504 if (!context
|| !context
->internal
->initialized
) {
1510 /* No code yet ... */
1515 SMBC_chmod_ctx(SMBCCTX
*context
,
1519 SMBCSRV
*srv
= NULL
;
1520 char *server
= NULL
;
1523 char *password
= NULL
;
1524 char *workgroup
= NULL
;
1525 char *targetpath
= NULL
;
1526 struct cli_state
*targetcli
= NULL
;
1529 TALLOC_CTX
*frame
= talloc_stackframe();
1531 if (!context
|| !context
->internal
->initialized
) {
1533 errno
= EINVAL
; /* Best I can think of ... */
1544 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1546 if (SMBC_parse_path(frame
,
1561 if (!user
|| user
[0] == (char)0) {
1562 user
= talloc_strdup(frame
, smbc_getUser(context
));
1570 srv
= SMBC_server(frame
, context
, True
,
1571 server
, share
, &workgroup
, &user
, &password
);
1575 return -1; /* errno set by SMBC_server */
1578 /*d_printf(">>>unlink: resolving %s\n", path);*/
1579 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1581 &targetcli
, &targetpath
)) {
1582 d_printf("Could not resolve %s\n", path
);
1590 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
1591 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
1592 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
1593 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
1595 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1596 errno
= SMBC_errno(context
, targetcli
);
1606 SMBC_utimes_ctx(SMBCCTX
*context
,
1608 struct timeval
*tbuf
)
1610 SMBCSRV
*srv
= NULL
;
1611 char *server
= NULL
;
1614 char *password
= NULL
;
1615 char *workgroup
= NULL
;
1619 TALLOC_CTX
*frame
= talloc_stackframe();
1621 if (!context
|| !context
->internal
->initialized
) {
1623 errno
= EINVAL
; /* Best I can think of ... */
1635 access_time
= write_time
= time(NULL
);
1637 access_time
= tbuf
[0].tv_sec
;
1638 write_time
= tbuf
[1].tv_sec
;
1646 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1647 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1648 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1652 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1653 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1654 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1658 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1659 fname
, atimebuf
, mtimebuf
);
1662 if (SMBC_parse_path(frame
,
1677 if (!user
|| user
[0] == (char)0) {
1678 user
= talloc_strdup(frame
, smbc_getUser(context
));
1686 srv
= SMBC_server(frame
, context
, True
,
1687 server
, share
, &workgroup
, &user
, &password
);
1691 return -1; /* errno set by SMBC_server */
1694 if (!SMBC_setatr(context
, srv
, path
,
1695 0, access_time
, write_time
, 0, 0)) {
1697 return -1; /* errno set by SMBC_setatr */
1705 * Routine to unlink() a file
1709 SMBC_unlink_ctx(SMBCCTX
*context
,
1712 char *server
= NULL
;
1715 char *password
= NULL
;
1716 char *workgroup
= NULL
;
1718 char *targetpath
= NULL
;
1719 struct cli_state
*targetcli
= NULL
;
1720 SMBCSRV
*srv
= NULL
;
1721 TALLOC_CTX
*frame
= talloc_stackframe();
1723 if (!context
|| !context
->internal
->initialized
) {
1725 errno
= EINVAL
; /* Best I can think of ... */
1738 if (SMBC_parse_path(frame
,
1753 if (!user
|| user
[0] == (char)0) {
1754 user
= talloc_strdup(frame
, smbc_getUser(context
));
1762 srv
= SMBC_server(frame
, context
, True
,
1763 server
, share
, &workgroup
, &user
, &password
);
1767 return -1; /* SMBC_server sets errno */
1771 /*d_printf(">>>unlink: resolving %s\n", path);*/
1772 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1774 &targetcli
, &targetpath
)) {
1775 d_printf("Could not resolve %s\n", path
);
1780 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1782 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, aSYSTEM
| aHIDDEN
))) {
1784 errno
= SMBC_errno(context
, targetcli
);
1786 if (errno
== EACCES
) { /* Check if the file is a directory */
1791 struct timespec write_time_ts
;
1792 struct timespec access_time_ts
;
1793 struct timespec change_time_ts
;
1796 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1803 /* Hmmm, bad error ... What? */
1805 errno
= SMBC_errno(context
, targetcli
);
1812 if (IS_DOS_DIR(mode
))
1815 errno
= saverr
; /* Restore this */
1826 return 0; /* Success ... */
1831 * Routine to rename() a file
1835 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1840 char *server1
= NULL
;
1841 char *share1
= NULL
;
1842 char *server2
= NULL
;
1843 char *share2
= NULL
;
1846 char *password1
= NULL
;
1847 char *password2
= NULL
;
1848 char *workgroup
= NULL
;
1851 char *targetpath1
= NULL
;
1852 char *targetpath2
= NULL
;
1853 struct cli_state
*targetcli1
= NULL
;
1854 struct cli_state
*targetcli2
= NULL
;
1855 SMBCSRV
*srv
= NULL
;
1856 TALLOC_CTX
*frame
= talloc_stackframe();
1858 if (!ocontext
|| !ncontext
||
1859 !ocontext
->internal
->initialized
||
1860 !ncontext
->internal
->initialized
) {
1862 errno
= EINVAL
; /* Best I can think of ... */
1867 if (!oname
|| !nname
) {
1873 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1875 if (SMBC_parse_path(frame
,
1890 if (!user1
|| user1
[0] == (char)0) {
1891 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1899 if (SMBC_parse_path(frame
,
1914 if (!user2
|| user2
[0] == (char)0) {
1915 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1923 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1924 strcmp(user1
, user2
)) {
1925 /* Can't rename across file systems, or users?? */
1931 srv
= SMBC_server(frame
, ocontext
, True
,
1932 server1
, share1
, &workgroup
, &user1
, &password1
);
1939 /* set the credentials to make DFS work */
1940 smbc_set_credentials_with_fallback(ocontext
,
1945 /*d_printf(">>>rename: resolving %s\n", path1);*/
1946 if (!cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1949 &targetcli1
, &targetpath1
)) {
1950 d_printf("Could not resolve %s\n", path1
);
1956 /* set the credentials to make DFS work */
1957 smbc_set_credentials_with_fallback(ncontext
,
1962 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1963 /*d_printf(">>>rename: resolving %s\n", path2);*/
1964 if (!cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1967 &targetcli2
, &targetpath2
)) {
1968 d_printf("Could not resolve %s\n", path2
);
1973 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1975 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1976 strcmp(targetcli1
->share
, targetcli2
->share
))
1978 /* can't rename across file systems */
1984 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
1985 int eno
= SMBC_errno(ocontext
, targetcli1
);
1987 if (eno
!= EEXIST
||
1988 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, aSYSTEM
| aHIDDEN
)) ||
1989 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
1999 return 0; /* Success */