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
&aDIR
?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 ip_service
*ip_list
;
460 struct ip_service server_addr
;
461 struct user_auth_info u_info
;
463 if (share
[0] != (char)0 || path
[0] != (char)0) {
466 SAFE_FREE(dir
->fname
);
470 errno
= EINVAL
+ 8196;
474 /* Determine how many local master browsers to query */
475 max_lmb_count
= (smbc_getOptionBrowseMaxLmbCount(context
) == 0
477 : smbc_getOptionBrowseMaxLmbCount(context
));
479 memset(&u_info
, '\0', sizeof(u_info
));
480 u_info
.username
= talloc_strdup(frame
,user
);
481 u_info
.password
= talloc_strdup(frame
,password
);
482 if (!u_info
.username
|| !u_info
.password
) {
484 SAFE_FREE(dir
->fname
);
492 * We have server and share and path empty but options
493 * requesting that we scan all master browsers for their list
494 * of workgroups/domains. This implies that we must first try
495 * broadcast queries to find all master browsers, and if that
496 * doesn't work, then try our other methods which return only
497 * a single master browser.
501 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
507 if (!find_master_ip(workgroup
, &server_addr
.ss
)) {
510 SAFE_FREE(dir
->fname
);
518 ip_list
= (struct ip_service
*)memdup(
519 &server_addr
, sizeof(server_addr
));
520 if (ip_list
== NULL
) {
528 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
529 char addr
[INET6_ADDRSTRLEN
];
531 struct cli_state
*cli
= NULL
;
533 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
534 DEBUG(99, ("Found master browser %d of %d: %s\n",
535 i
+1, MAX(count
, max_lmb_count
),
538 cli
= get_ipc_connect_master_ip(talloc_tos(),
542 /* cli == NULL is the master browser refused to talk or
543 could not be found */
548 workgroup
= talloc_strdup(frame
, wg_ptr
);
549 server
= talloc_strdup(frame
, cli
->desthost
);
553 if (!workgroup
|| !server
) {
555 SAFE_FREE(dir
->fname
);
563 DEBUG(4, ("using workgroup %s %s\n",
567 * For each returned master browser IP address, get a
568 * connection to IPC$ on the server if we do not
569 * already have one, and determine the
570 * workgroups/domains that it knows about.
573 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
574 &workgroup
, &user
, &password
);
580 dir
->dir_type
= SMBC_WORKGROUP
;
582 /* Now, list the stuff ... */
584 if (!cli_NetServerEnum(srv
->cli
,
596 * Server not an empty string ... Check the rest and see what
599 if (*share
== '\0') {
602 /* Should not have empty share with path */
604 SAFE_FREE(dir
->fname
);
608 errno
= EINVAL
+ 8197;
614 * We don't know if <server> is really a server name
615 * or is a workgroup/domain name. If we already have
616 * a server structure for it, we'll use it.
617 * Otherwise, check to see if <server><1D>,
618 * <server><1B>, or <server><20> translates. We check
619 * to see if <server> is an IP address first.
623 * See if we have an existing server. Do not
624 * establish a connection if one does not already
627 srv
= SMBC_server(frame
, context
, False
,
629 &workgroup
, &user
, &password
);
632 * If no existing server and not an IP addr, look for
636 !is_ipaddress(server
) &&
637 (resolve_name(server
, &rem_ss
, 0x1d, false) || /* LMB */
638 resolve_name(server
, &rem_ss
, 0x1b, false) )) { /* DMB */
640 * "server" is actually a workgroup name,
641 * not a server. Make this clear.
643 char *wgroup
= server
;
646 dir
->dir_type
= SMBC_SERVER
;
649 * Get the backup list ...
651 if (!name_status_find(wgroup
, 0, 0,
652 &rem_ss
, buserver
)) {
653 char addr
[INET6_ADDRSTRLEN
];
655 print_sockaddr(addr
, sizeof(addr
), &rem_ss
);
656 DEBUG(0,("Could not get name of "
657 "local/domain master browser "
658 "for workgroup %s from "
663 SAFE_FREE(dir
->fname
);
673 * Get a connection to IPC$ on the server if
674 * we do not already have one
676 srv
= SMBC_server(frame
, context
, True
,
681 DEBUG(0, ("got no contact to IPC$\n"));
683 SAFE_FREE(dir
->fname
);
693 /* Now, list the servers ... */
694 if (!cli_NetServerEnum(srv
->cli
, wgroup
,
699 SAFE_FREE(dir
->fname
);
706 (resolve_name(server
, &rem_ss
, 0x20, false))) {
709 * If we hadn't found the server, get one now
712 srv
= SMBC_server(frame
, context
, True
,
720 SAFE_FREE(dir
->fname
);
728 dir
->dir_type
= SMBC_FILE_SHARE
;
731 /* List the shares ... */
733 if (net_share_enum_rpc(
742 errno
= cli_errno(srv
->cli
);
744 SAFE_FREE(dir
->fname
);
752 /* Neither the workgroup nor server exists */
753 errno
= ECONNREFUSED
;
755 SAFE_FREE(dir
->fname
);
765 * The server and share are specified ... work from
769 struct cli_state
*targetcli
;
772 /* We connect to the server and list the directory */
773 dir
->dir_type
= SMBC_FILE_SHARE
;
775 srv
= SMBC_server(frame
, context
, True
, server
, share
,
776 &workgroup
, &user
, &password
);
780 SAFE_FREE(dir
->fname
);
789 /* Now, list the files ... */
791 p
= path
+ strlen(path
);
792 path
= talloc_asprintf_append(path
, "\\*");
795 SAFE_FREE(dir
->fname
);
802 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
804 &targetcli
, &targetpath
)) {
805 d_printf("Could not resolve %s\n", path
);
807 SAFE_FREE(dir
->fname
);
814 status
= cli_list(targetcli
, targetpath
,
815 aDIR
| aSYSTEM
| aHIDDEN
,
816 dir_list_fn
, (void *)dir
);
817 if (!NT_STATUS_IS_OK(status
)) {
819 SAFE_FREE(dir
->fname
);
822 saved_errno
= SMBC_errno(context
, targetcli
);
824 if (saved_errno
== EINVAL
) {
826 * See if they asked to opendir
827 * something other than a directory.
828 * If so, the converted error value we
829 * got would have been EINVAL rather
832 *p
= '\0'; /* restore original path */
834 if (SMBC_getatr(context
, srv
, path
,
836 NULL
, NULL
, NULL
, NULL
,
838 ! IS_DOS_DIR(mode
)) {
840 /* It is. Correct the error value */
841 saved_errno
= ENOTDIR
;
846 * If there was an error and the server is no
849 if (cli_is_error(targetcli
) &&
850 smbc_getFunctionCheckServer(context
)(context
, srv
)) {
852 /* ... then remove it. */
853 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
856 * We could not remove the
857 * server completely, remove
858 * it from the cache so we
859 * will not get it again. It
860 * will be removed when the
861 * last file/dir is closed.
863 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
875 DLIST_ADD(context
->internal
->files
, dir
);
882 * Routine to close a directory
886 SMBC_closedir_ctx(SMBCCTX
*context
,
889 TALLOC_CTX
*frame
= talloc_stackframe();
891 if (!context
|| !context
->internal
->initialized
) {
897 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
903 remove_dir(dir
); /* Clean it up */
905 DLIST_REMOVE(context
->internal
->files
, dir
);
909 SAFE_FREE(dir
->fname
);
910 SAFE_FREE(dir
); /* Free the space too */
919 smbc_readdir_internal(SMBCCTX
* context
,
920 struct smbc_dirent
*dest
,
921 struct smbc_dirent
*src
,
924 if (smbc_getOptionUrlEncodeReaddirEntries(context
)) {
926 /* url-encode the name. get back remaining buffer space */
928 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
930 /* We now know the name length */
931 dest
->namelen
= strlen(dest
->name
);
933 /* Save the pointer to the beginning of the comment */
934 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
936 /* Copy the comment */
937 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
938 dest
->comment
[max_namebuf_len
- 1] = '\0';
940 /* Save other fields */
941 dest
->smbc_type
= src
->smbc_type
;
942 dest
->commentlen
= strlen(dest
->comment
);
943 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
947 /* No encoding. Just copy the entry as is. */
948 memcpy(dest
, src
, src
->dirlen
);
949 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
955 * Routine to get a directory entry
959 SMBC_readdir_ctx(SMBCCTX
*context
,
963 struct smbc_dirent
*dirp
, *dirent
;
964 TALLOC_CTX
*frame
= talloc_stackframe();
966 /* Check that all is ok first ... */
968 if (!context
|| !context
->internal
->initialized
) {
971 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
977 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
980 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
986 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
989 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
995 if (!dir
->dir_next
) {
1000 dirent
= dir
->dir_next
->dirent
;
1009 dirp
= &context
->internal
->dirent
;
1010 maxlen
= sizeof(context
->internal
->_dirent_name
);
1012 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
1014 dir
->dir_next
= dir
->dir_next
->next
;
1021 * Routine to get directory entries
1025 SMBC_getdents_ctx(SMBCCTX
*context
,
1027 struct smbc_dirent
*dirp
,
1033 char *ndir
= (char *)dirp
;
1034 struct smbc_dir_list
*dirlist
;
1035 TALLOC_CTX
*frame
= talloc_stackframe();
1037 /* Check that all is ok first ... */
1039 if (!context
|| !context
->internal
->initialized
) {
1047 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1055 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1064 * Now, retrieve the number of entries that will fit in what was passed
1065 * We have to figure out if the info is in the list, or we need to
1066 * send a request to the server to get the info.
1069 while ((dirlist
= dir
->dir_next
)) {
1070 struct smbc_dirent
*dirent
;
1071 struct smbc_dirent
*currentEntry
= (struct smbc_dirent
*)ndir
;
1073 if (!dirlist
->dirent
) {
1075 errno
= ENOENT
; /* Bad error */
1081 /* Do urlencoding of next entry, if so selected */
1082 dirent
= &context
->internal
->dirent
;
1083 maxlen
= sizeof(context
->internal
->_dirent_name
);
1084 smbc_readdir_internal(context
, dirent
,
1085 dirlist
->dirent
, maxlen
);
1087 reqd
= dirent
->dirlen
;
1091 if (rem
< count
) { /* We managed to copy something */
1098 else { /* Nothing copied ... */
1100 errno
= EINVAL
; /* Not enough space ... */
1108 memcpy(currentEntry
, dirent
, reqd
); /* Copy the data in ... */
1110 currentEntry
->comment
= ¤tEntry
->name
[0] +
1111 dirent
->namelen
+ 1;
1116 /* Try and align the struct for the next entry
1117 on a valid pointer boundary by appending zeros */
1118 while((rem
> 0) && ((unsigned long long)ndir
& (sizeof(void*) - 1))) {
1122 currentEntry
->dirlen
++;
1125 dir
->dir_next
= dirlist
= dirlist
-> next
;
1138 * Routine to create a directory ...
1142 SMBC_mkdir_ctx(SMBCCTX
*context
,
1146 SMBCSRV
*srv
= NULL
;
1147 char *server
= NULL
;
1150 char *password
= NULL
;
1151 char *workgroup
= NULL
;
1153 char *targetpath
= NULL
;
1154 struct cli_state
*targetcli
= NULL
;
1155 TALLOC_CTX
*frame
= talloc_stackframe();
1157 if (!context
|| !context
->internal
->initialized
) {
1169 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1171 if (SMBC_parse_path(frame
,
1186 if (!user
|| user
[0] == (char)0) {
1187 user
= talloc_strdup(frame
, smbc_getUser(context
));
1195 srv
= SMBC_server(frame
, context
, True
,
1196 server
, share
, &workgroup
, &user
, &password
);
1201 return -1; /* errno set by SMBC_server */
1205 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1206 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1208 &targetcli
, &targetpath
)) {
1209 d_printf("Could not resolve %s\n", path
);
1214 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1216 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli
, targetpath
))) {
1217 errno
= SMBC_errno(context
, targetcli
);
1229 * Our list function simply checks to see if a directory is not empty
1233 rmdir_list_fn(const char *mnt
,
1234 struct file_info
*finfo
,
1238 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1239 strncmp(finfo
->name
, "..", 2) != 0) {
1240 bool *smbc_rmdir_dirempty
= (bool *)state
;
1241 *smbc_rmdir_dirempty
= false;
1243 return NT_STATUS_OK
;
1247 * Routine to remove a directory
1251 SMBC_rmdir_ctx(SMBCCTX
*context
,
1254 SMBCSRV
*srv
= NULL
;
1255 char *server
= NULL
;
1258 char *password
= NULL
;
1259 char *workgroup
= NULL
;
1261 char *targetpath
= NULL
;
1262 struct cli_state
*targetcli
= NULL
;
1263 TALLOC_CTX
*frame
= talloc_stackframe();
1265 if (!context
|| !context
->internal
->initialized
) {
1277 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1279 if (SMBC_parse_path(frame
,
1294 if (!user
|| user
[0] == (char)0) {
1295 user
= talloc_strdup(frame
, smbc_getUser(context
));
1303 srv
= SMBC_server(frame
, context
, True
,
1304 server
, share
, &workgroup
, &user
, &password
);
1309 return -1; /* errno set by SMBC_server */
1313 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1314 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1316 &targetcli
, &targetpath
)) {
1317 d_printf("Could not resolve %s\n", path
);
1322 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1324 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetpath
))) {
1326 errno
= SMBC_errno(context
, targetcli
);
1328 if (errno
== EACCES
) { /* Check if the dir empty or not */
1330 /* Local storage to avoid buffer overflows */
1332 bool smbc_rmdir_dirempty
= true;
1335 lpath
= talloc_asprintf(frame
, "%s\\*",
1343 status
= cli_list(targetcli
, lpath
,
1344 aDIR
| aSYSTEM
| aHIDDEN
,
1346 &smbc_rmdir_dirempty
);
1348 if (!NT_STATUS_IS_OK(status
)) {
1349 /* Fix errno to ignore latest error ... */
1350 DEBUG(5, ("smbc_rmdir: "
1351 "cli_list returned an error: %d\n",
1352 SMBC_errno(context
, targetcli
)));
1357 if (smbc_rmdir_dirempty
)
1375 * Routine to return the current directory position
1379 SMBC_telldir_ctx(SMBCCTX
*context
,
1382 TALLOC_CTX
*frame
= talloc_stackframe();
1384 if (!context
|| !context
->internal
->initialized
) {
1392 if (!dir
|| !SMBC_dlist_contains(context
->internal
->files
, dir
)) {
1400 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1408 /* See if we're already at the end. */
1409 if (dir
->dir_next
== NULL
) {
1416 * We return the pointer here as the offset
1419 return (off_t
)(long)dir
->dir_next
->dirent
;
1423 * A routine to run down the list and see if the entry is OK
1426 static struct smbc_dir_list
*
1427 check_dir_ent(struct smbc_dir_list
*list
,
1428 struct smbc_dirent
*dirent
)
1431 /* Run down the list looking for what we want */
1435 struct smbc_dir_list
*tmp
= list
;
1439 if (tmp
->dirent
== dirent
)
1448 return NULL
; /* Not found, or an error */
1454 * Routine to seek on a directory
1458 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1462 long int l_offset
= offset
; /* Handle problems of size */
1463 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1464 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1465 TALLOC_CTX
*frame
= talloc_stackframe();
1467 if (!context
|| !context
->internal
->initialized
) {
1475 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1483 /* Now, check what we were passed and see if it is OK ... */
1485 if (dirent
== NULL
) { /* Seek to the begining of the list */
1487 dir
->dir_next
= dir
->dir_list
;
1493 if (offset
== -1) { /* Seek to the end of the list */
1494 dir
->dir_next
= NULL
;
1499 /* Now, run down the list and make sure that the entry is OK */
1500 /* This may need to be changed if we change the format of the list */
1502 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1503 errno
= EINVAL
; /* Bad entry */
1508 dir
->dir_next
= list_ent
;
1515 * Routine to fstat a dir
1519 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1524 if (!context
|| !context
->internal
->initialized
) {
1530 /* No code yet ... */
1535 SMBC_chmod_ctx(SMBCCTX
*context
,
1539 SMBCSRV
*srv
= NULL
;
1540 char *server
= NULL
;
1543 char *password
= NULL
;
1544 char *workgroup
= NULL
;
1545 char *targetpath
= NULL
;
1546 struct cli_state
*targetcli
= NULL
;
1549 TALLOC_CTX
*frame
= talloc_stackframe();
1551 if (!context
|| !context
->internal
->initialized
) {
1553 errno
= EINVAL
; /* Best I can think of ... */
1564 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, (unsigned int)newmode
));
1566 if (SMBC_parse_path(frame
,
1581 if (!user
|| user
[0] == (char)0) {
1582 user
= talloc_strdup(frame
, smbc_getUser(context
));
1590 srv
= SMBC_server(frame
, context
, True
,
1591 server
, share
, &workgroup
, &user
, &password
);
1595 return -1; /* errno set by SMBC_server */
1598 /*d_printf(">>>unlink: resolving %s\n", path);*/
1599 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1601 &targetcli
, &targetpath
)) {
1602 d_printf("Could not resolve %s\n", path
);
1610 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
1611 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
1612 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
1613 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
1615 if (!NT_STATUS_IS_OK(cli_setatr(targetcli
, targetpath
, mode
, 0))) {
1616 errno
= SMBC_errno(context
, targetcli
);
1626 SMBC_utimes_ctx(SMBCCTX
*context
,
1628 struct timeval
*tbuf
)
1630 SMBCSRV
*srv
= NULL
;
1631 char *server
= NULL
;
1634 char *password
= NULL
;
1635 char *workgroup
= NULL
;
1639 TALLOC_CTX
*frame
= talloc_stackframe();
1641 if (!context
|| !context
->internal
->initialized
) {
1643 errno
= EINVAL
; /* Best I can think of ... */
1655 access_time
= write_time
= time(NULL
);
1657 access_time
= tbuf
[0].tv_sec
;
1658 write_time
= tbuf
[1].tv_sec
;
1666 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1667 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1668 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1672 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1673 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1674 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1678 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1679 fname
, atimebuf
, mtimebuf
);
1682 if (SMBC_parse_path(frame
,
1697 if (!user
|| user
[0] == (char)0) {
1698 user
= talloc_strdup(frame
, smbc_getUser(context
));
1706 srv
= SMBC_server(frame
, context
, True
,
1707 server
, share
, &workgroup
, &user
, &password
);
1711 return -1; /* errno set by SMBC_server */
1714 if (!SMBC_setatr(context
, srv
, path
,
1715 0, access_time
, write_time
, 0, 0)) {
1717 return -1; /* errno set by SMBC_setatr */
1725 * Routine to unlink() a file
1729 SMBC_unlink_ctx(SMBCCTX
*context
,
1732 char *server
= NULL
;
1735 char *password
= NULL
;
1736 char *workgroup
= NULL
;
1738 char *targetpath
= NULL
;
1739 struct cli_state
*targetcli
= NULL
;
1740 SMBCSRV
*srv
= NULL
;
1741 TALLOC_CTX
*frame
= talloc_stackframe();
1743 if (!context
|| !context
->internal
->initialized
) {
1745 errno
= EINVAL
; /* Best I can think of ... */
1758 if (SMBC_parse_path(frame
,
1773 if (!user
|| user
[0] == (char)0) {
1774 user
= talloc_strdup(frame
, smbc_getUser(context
));
1782 srv
= SMBC_server(frame
, context
, True
,
1783 server
, share
, &workgroup
, &user
, &password
);
1787 return -1; /* SMBC_server sets errno */
1791 /*d_printf(">>>unlink: resolving %s\n", path);*/
1792 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
1794 &targetcli
, &targetpath
)) {
1795 d_printf("Could not resolve %s\n", path
);
1800 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1802 if (!NT_STATUS_IS_OK(cli_unlink(targetcli
, targetpath
, aSYSTEM
| aHIDDEN
))) {
1804 errno
= SMBC_errno(context
, targetcli
);
1806 if (errno
== EACCES
) { /* Check if the file is a directory */
1811 struct timespec write_time_ts
;
1812 struct timespec access_time_ts
;
1813 struct timespec change_time_ts
;
1816 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1823 /* Hmmm, bad error ... What? */
1825 errno
= SMBC_errno(context
, targetcli
);
1832 if (IS_DOS_DIR(mode
))
1835 errno
= saverr
; /* Restore this */
1846 return 0; /* Success ... */
1851 * Routine to rename() a file
1855 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1860 char *server1
= NULL
;
1861 char *share1
= NULL
;
1862 char *server2
= NULL
;
1863 char *share2
= NULL
;
1866 char *password1
= NULL
;
1867 char *password2
= NULL
;
1868 char *workgroup
= NULL
;
1871 char *targetpath1
= NULL
;
1872 char *targetpath2
= NULL
;
1873 struct cli_state
*targetcli1
= NULL
;
1874 struct cli_state
*targetcli2
= NULL
;
1875 SMBCSRV
*srv
= NULL
;
1876 TALLOC_CTX
*frame
= talloc_stackframe();
1878 if (!ocontext
|| !ncontext
||
1879 !ocontext
->internal
->initialized
||
1880 !ncontext
->internal
->initialized
) {
1882 errno
= EINVAL
; /* Best I can think of ... */
1887 if (!oname
|| !nname
) {
1893 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1895 if (SMBC_parse_path(frame
,
1910 if (!user1
|| user1
[0] == (char)0) {
1911 user1
= talloc_strdup(frame
, smbc_getUser(ocontext
));
1919 if (SMBC_parse_path(frame
,
1934 if (!user2
|| user2
[0] == (char)0) {
1935 user2
= talloc_strdup(frame
, smbc_getUser(ncontext
));
1943 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1944 strcmp(user1
, user2
)) {
1945 /* Can't rename across file systems, or users?? */
1951 srv
= SMBC_server(frame
, ocontext
, True
,
1952 server1
, share1
, &workgroup
, &user1
, &password1
);
1959 /* set the credentials to make DFS work */
1960 smbc_set_credentials_with_fallback(ocontext
,
1965 /*d_printf(">>>rename: resolving %s\n", path1);*/
1966 if (!cli_resolve_path(frame
, "", ocontext
->internal
->auth_info
,
1969 &targetcli1
, &targetpath1
)) {
1970 d_printf("Could not resolve %s\n", path1
);
1976 /* set the credentials to make DFS work */
1977 smbc_set_credentials_with_fallback(ncontext
,
1982 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1983 /*d_printf(">>>rename: resolving %s\n", path2);*/
1984 if (!cli_resolve_path(frame
, "", ncontext
->internal
->auth_info
,
1987 &targetcli2
, &targetpath2
)) {
1988 d_printf("Could not resolve %s\n", path2
);
1993 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1995 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1996 strcmp(targetcli1
->share
, targetcli2
->share
))
1998 /* can't rename across file systems */
2004 if (!NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2005 int eno
= SMBC_errno(ocontext
, targetcli1
);
2007 if (eno
!= EEXIST
||
2008 !NT_STATUS_IS_OK(cli_unlink(targetcli1
, targetpath2
, aSYSTEM
| aHIDDEN
)) ||
2009 !NT_STATUS_IS_OK(cli_rename(targetcli1
, targetpath1
, targetpath2
))) {
2019 return 0; /* Success */