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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open a directory
32 * We accept the URL syntax explained in SMBC_parse_path(), above.
36 remove_dir(SMBCFILE
*dir
)
38 struct smbc_dir_list
*d
,*f
;
50 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
55 add_dirent(SMBCFILE
*dir
,
60 struct smbc_dirent
*dirent
;
62 int name_length
= (name
== NULL
? 0 : strlen(name
));
63 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
66 * Allocate space for the dirent, which must be increased by the
67 * size of the name and the comment and 1 each for the null terminator.
70 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
72 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
76 dir
->dir_error
= ENOMEM
;
83 if (dir
->dir_list
== NULL
) {
85 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
89 dir
->dir_error
= ENOMEM
;
93 ZERO_STRUCTP(dir
->dir_list
);
95 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
99 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
101 if (!dir
->dir_end
->next
) {
104 dir
->dir_error
= ENOMEM
;
108 ZERO_STRUCTP(dir
->dir_end
->next
);
110 dir
->dir_end
= dir
->dir_end
->next
;
113 dir
->dir_end
->next
= NULL
;
114 dir
->dir_end
->dirent
= dirent
;
116 dirent
->smbc_type
= type
;
117 dirent
->namelen
= name_length
;
118 dirent
->commentlen
= comment_len
;
119 dirent
->dirlen
= size
;
122 * dirent->namelen + 1 includes the null (no null termination needed)
123 * Ditto for dirent->commentlen.
124 * The space for the two null bytes was allocated.
126 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
127 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
128 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
135 list_unique_wg_fn(const char *name
,
140 SMBCFILE
*dir
= (SMBCFILE
*)state
;
141 struct smbc_dir_list
*dir_list
;
142 struct smbc_dirent
*dirent
;
146 dirent_type
= dir
->dir_type
;
148 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
150 /* An error occurred, what do we do? */
151 /* FIXME: Add some code here */
154 /* Point to the one just added */
155 dirent
= dir
->dir_end
->dirent
;
157 /* See if this was a duplicate */
158 for (dir_list
= dir
->dir_list
;
159 dir_list
!= dir
->dir_end
;
160 dir_list
= dir_list
->next
) {
162 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
163 /* Duplicate. End end of list need to be removed. */
167 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
168 /* Found the end of the list. Remove it. */
169 dir
->dir_end
= dir_list
;
170 free(dir_list
->next
);
172 dir_list
->next
= NULL
;
179 list_fn(const char *name
,
184 SMBCFILE
*dir
= (SMBCFILE
*)state
;
188 * We need to process the type a little ...
190 * Disk share = 0x00000000
191 * Print share = 0x00000001
192 * Comms share = 0x00000002 (obsolete?)
193 * IPC$ share = 0x00000003
195 * administrative shares:
196 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
199 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
203 dirent_type
= SMBC_FILE_SHARE
;
207 dirent_type
= SMBC_PRINTER_SHARE
;
211 dirent_type
= SMBC_COMMS_SHARE
;
216 dirent_type
= SMBC_IPC_SHARE
;
220 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
225 dirent_type
= dir
->dir_type
;
228 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
230 /* An error occurred, what do we do? */
231 /* FIXME: Add some code here */
237 dir_list_fn(const char *mnt
,
243 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
244 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
246 /* Handle an error ... */
248 /* FIXME: Add some code ... */
255 net_share_enum_rpc(struct cli_state
*cli
,
256 void (*fn
)(const char *name
,
265 uint32 info_level
= 1;
266 uint32 preferred_len
= 0xffffffff;
268 SRV_SHARE_INFO_CTR ctr
;
270 fstring comment
= "";
271 struct rpc_pipe_client
*pipe_hnd
;
274 /* Open the server service pipe */
275 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &nt_status
);
277 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
281 /* Issue the NetShareEnum RPC call and retrieve the response */
282 init_enum_hnd(&enum_hnd
, 0);
283 result
= rpccli_srvsvc_net_share_enum(pipe_hnd
,
290 /* Was it successful? */
291 if (!W_ERROR_IS_OK(result
) || ctr
.num_entries
== 0) {
292 /* Nope. Go clean up. */
296 /* For each returned entry... */
297 for (i
= 0; i
< ctr
.num_entries
; i
++) {
299 /* pull out the share name */
300 rpcstr_pull_unistr2_fstring(
301 name
, &ctr
.share
.info1
[i
].info_1_str
.uni_netname
);
303 /* pull out the share's comment */
304 rpcstr_pull_unistr2_fstring(
305 comment
, &ctr
.share
.info1
[i
].info_1_str
.uni_remark
);
307 /* Get the type value */
308 type
= ctr
.share
.info1
[i
].info_1
.type
;
310 /* Add this share to the list */
311 (*fn
)(name
, type
, comment
, state
);
315 /* Close the server service pipe */
316 cli_rpc_pipe_close(pipe_hnd
);
318 /* Tell 'em if it worked */
319 return W_ERROR_IS_OK(result
) ? 0 : -1;
324 * Verify that the options specified in a URL are valid
327 SMBC_check_options(char *server
,
332 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
333 "path='%s' options='%s'\n",
334 server
, share
, path
, options
));
336 /* No options at all is always ok */
337 if (! *options
) return 0;
339 /* Currently, we don't support any options. */
345 SMBC_opendir_ctx(SMBCCTX
*context
,
349 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
, *options
= NULL
;
350 char *workgroup
= NULL
;
355 SMBCFILE
*dir
= NULL
;
356 struct sockaddr_storage rem_ss
;
357 TALLOC_CTX
*frame
= talloc_stackframe();
359 if (!context
|| !context
->initialized
) {
360 DEBUG(4, ("no valid context\n"));
361 errno
= EINVAL
+ 8192;
368 DEBUG(4, ("no valid fname\n"));
369 errno
= EINVAL
+ 8193;
374 if (SMBC_parse_path(frame
,
384 DEBUG(4, ("no valid path\n"));
385 errno
= EINVAL
+ 8194;
390 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
391 "path='%s' options='%s'\n",
392 fname
, server
, share
, path
, options
));
394 /* Ensure the options are valid */
395 if (SMBC_check_options(server
, share
, path
, options
)) {
396 DEBUG(4, ("unacceptable options (%s)\n", options
));
397 errno
= EINVAL
+ 8195;
402 if (!user
|| user
[0] == (char)0) {
403 user
= talloc_strdup(frame
, context
->user
);
411 dir
= SMB_MALLOC_P(SMBCFILE
);
422 dir
->fname
= SMB_STRDUP(fname
);
426 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
428 if (server
[0] == (char)0) {
433 struct ip_service
*ip_list
;
434 struct ip_service server_addr
;
435 struct user_auth_info u_info
;
437 if (share
[0] != (char)0 || path
[0] != (char)0) {
439 errno
= EINVAL
+ 8196;
441 SAFE_FREE(dir
->fname
);
448 /* Determine how many local master browsers to query */
449 max_lmb_count
= (context
->browse_max_lmb_count
== 0
451 : context
->browse_max_lmb_count
);
453 memset(&u_info
, '\0', sizeof(u_info
));
454 u_info
.username
= talloc_strdup(frame
,user
);
455 u_info
.password
= talloc_strdup(frame
,password
);
456 if (!u_info
.username
|| !u_info
.password
) {
458 SAFE_FREE(dir
->fname
);
466 * We have server and share and path empty but options
467 * requesting that we scan all master browsers for their list
468 * of workgroups/domains. This implies that we must first try
469 * broadcast queries to find all master browsers, and if that
470 * doesn't work, then try our other methods which return only
471 * a single master browser.
475 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
481 if (!find_master_ip(workgroup
, &server_addr
.ss
)) {
484 SAFE_FREE(dir
->fname
);
492 ip_list
= (struct ip_service
*)memdup(
493 &server_addr
, sizeof(server_addr
));
494 if (ip_list
== NULL
) {
502 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
503 char addr
[INET6_ADDRSTRLEN
];
505 struct cli_state
*cli
= NULL
;
507 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
508 DEBUG(99, ("Found master browser %d of %d: %s\n",
509 i
+1, MAX(count
, max_lmb_count
),
512 cli
= get_ipc_connect_master_ip(talloc_tos(),
516 /* cli == NULL is the master browser refused to talk or
517 could not be found */
522 workgroup
= talloc_strdup(frame
, wg_ptr
);
523 server
= talloc_strdup(frame
, cli
->desthost
);
527 if (!workgroup
|| !server
) {
533 DEBUG(4, ("using workgroup %s %s\n",
537 * For each returned master browser IP address, get a
538 * connection to IPC$ on the server if we do not
539 * already have one, and determine the
540 * workgroups/domains that it knows about.
543 srv
= SMBC_server(frame
, context
, True
, server
, "IPC$",
544 &workgroup
, &user
, &password
);
550 dir
->dir_type
= SMBC_WORKGROUP
;
552 /* Now, list the stuff ... */
554 if (!cli_NetServerEnum(srv
->cli
,
566 * Server not an empty string ... Check the rest and see what
569 if (*share
== '\0') {
572 /* Should not have empty share with path */
573 errno
= EINVAL
+ 8197;
575 SAFE_FREE(dir
->fname
);
584 * We don't know if <server> is really a server name
585 * or is a workgroup/domain name. If we already have
586 * a server structure for it, we'll use it.
587 * Otherwise, check to see if <server><1D>,
588 * <server><1B>, or <server><20> translates. We check
589 * to see if <server> is an IP address first.
593 * See if we have an existing server. Do not
594 * establish a connection if one does not already
597 srv
= SMBC_server(frame
, context
, False
, server
, "IPC$",
598 &workgroup
, &user
, &password
);
601 * If no existing server and not an IP addr, look for
605 !is_ipaddress(server
) &&
606 (resolve_name(server
, &rem_ss
, 0x1d) || /* LMB */
607 resolve_name(server
, &rem_ss
, 0x1b) )) { /* DMB */
611 dir
->dir_type
= SMBC_SERVER
;
614 * Get the backup list ...
616 if (!name_status_find(server
, 0, 0,
617 &rem_ss
, buserver
)) {
619 DEBUG(0, ("Could not get name of "
620 "local/domain master browser "
621 "for server %s\n", server
));
623 SAFE_FREE(dir
->fname
);
633 * Get a connection to IPC$ on the server if
634 * we do not already have one
636 srv
= SMBC_server(frame
, context
, True
,
638 &workgroup
, &user
, &password
);
640 DEBUG(0, ("got no contact to IPC$\n"));
642 SAFE_FREE(dir
->fname
);
652 /* Now, list the servers ... */
653 if (!cli_NetServerEnum(srv
->cli
, server
,
658 SAFE_FREE(dir
->fname
);
665 (resolve_name(server
, &rem_ss
, 0x20))) {
667 /* If we hadn't found the server, get one now */
669 srv
= SMBC_server(frame
, context
, True
,
677 SAFE_FREE(dir
->fname
);
685 dir
->dir_type
= SMBC_FILE_SHARE
;
688 /* List the shares ... */
690 if (net_share_enum_rpc(
699 errno
= cli_errno(srv
->cli
);
701 SAFE_FREE(dir
->fname
);
709 /* Neither the workgroup nor server exists */
710 errno
= ECONNREFUSED
;
712 SAFE_FREE(dir
->fname
);
722 * The server and share are specified ... work from
726 struct cli_state
*targetcli
;
728 /* We connect to the server and list the directory */
729 dir
->dir_type
= SMBC_FILE_SHARE
;
731 srv
= SMBC_server(frame
, context
, True
, server
, share
,
732 &workgroup
, &user
, &password
);
736 SAFE_FREE(dir
->fname
);
745 /* Now, list the files ... */
747 p
= path
+ strlen(path
);
748 path
= talloc_asprintf_append(path
, "\\*");
751 SAFE_FREE(dir
->fname
);
758 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
759 &targetcli
, &targetpath
)) {
760 d_printf("Could not resolve %s\n", path
);
762 SAFE_FREE(dir
->fname
);
769 if (cli_list(targetcli
, targetpath
,
770 aDIR
| aSYSTEM
| aHIDDEN
,
771 dir_list_fn
, (void *)dir
) < 0) {
774 SAFE_FREE(dir
->fname
);
777 saved_errno
= SMBC_errno(context
, targetcli
);
779 if (saved_errno
== EINVAL
) {
781 * See if they asked to opendir something
782 * other than a directory. If so, the
783 * converted error value we got would have
784 * been EINVAL rather than ENOTDIR.
786 *p
= '\0'; /* restore original path */
788 if (SMBC_getatr(context
, srv
, path
,
790 NULL
, NULL
, NULL
, NULL
,
792 ! IS_DOS_DIR(mode
)) {
794 /* It is. Correct the error value */
795 saved_errno
= ENOTDIR
;
800 * If there was an error and the server is no
803 if (cli_is_error(targetcli
) &&
804 (context
->server
.check_server_fn
)(context
, srv
)) {
806 /* ... then remove it. */
807 if ((context
->server
.remove_unused_server_fn
)(context
,
810 * We could not remove the
811 * server completely, remove
812 * it from the cache so we
813 * will not get it again. It
814 * will be removed when the
815 * last file/dir is closed.
817 (context
->cache
.remove_cached_server_fn
)(context
, srv
);
829 DLIST_ADD(context
->files
, dir
);
836 * Routine to close a directory
840 SMBC_closedir_ctx(SMBCCTX
*context
,
843 TALLOC_CTX
*frame
= talloc_stackframe();
845 if (!context
|| !context
->initialized
) {
851 if (!dir
|| !SMBC_dlist_contains(context
->files
, dir
)) {
857 remove_dir(dir
); /* Clean it up */
859 DLIST_REMOVE(context
->files
, dir
);
863 SAFE_FREE(dir
->fname
);
864 SAFE_FREE(dir
); /* Free the space too */
873 smbc_readdir_internal(SMBCCTX
* context
,
874 struct smbc_dirent
*dest
,
875 struct smbc_dirent
*src
,
878 if (context
->urlencode_readdir_entries
) {
880 /* url-encode the name. get back remaining buffer space */
882 SMBC_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
884 /* We now know the name length */
885 dest
->namelen
= strlen(dest
->name
);
887 /* Save the pointer to the beginning of the comment */
888 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
890 /* Copy the comment */
891 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
892 dest
->comment
[max_namebuf_len
- 1] = '\0';
894 /* Save other fields */
895 dest
->smbc_type
= src
->smbc_type
;
896 dest
->commentlen
= strlen(dest
->comment
);
897 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
901 /* No encoding. Just copy the entry as is. */
902 memcpy(dest
, src
, src
->dirlen
);
903 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
909 * Routine to get a directory entry
913 SMBC_readdir_ctx(SMBCCTX
*context
,
917 struct smbc_dirent
*dirp
, *dirent
;
918 TALLOC_CTX
*frame
= talloc_stackframe();
920 /* Check that all is ok first ... */
922 if (!context
|| !context
->initialized
) {
925 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
931 if (!dir
|| !SMBC_dlist_contains(context
->files
, dir
)) {
934 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
940 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
943 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
949 if (!dir
->dir_next
) {
954 dirent
= dir
->dir_next
->dirent
;
963 dirp
= (struct smbc_dirent
*)context
->dirent
;
964 maxlen
= (sizeof(context
->dirent
) -
965 sizeof(struct smbc_dirent
));
967 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
969 dir
->dir_next
= dir
->dir_next
->next
;
976 * Routine to get directory entries
980 SMBC_getdents_ctx(SMBCCTX
*context
,
982 struct smbc_dirent
*dirp
,
988 char *ndir
= (char *)dirp
;
989 struct smbc_dir_list
*dirlist
;
990 TALLOC_CTX
*frame
= talloc_stackframe();
992 /* Check that all is ok first ... */
994 if (!context
|| !context
->initialized
) {
1002 if (!dir
|| !SMBC_dlist_contains(context
->files
, dir
)) {
1010 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1019 * Now, retrieve the number of entries that will fit in what was passed
1020 * We have to figure out if the info is in the list, or we need to
1021 * send a request to the server to get the info.
1024 while ((dirlist
= dir
->dir_next
)) {
1025 struct smbc_dirent
*dirent
;
1027 if (!dirlist
->dirent
) {
1029 errno
= ENOENT
; /* Bad error */
1035 /* Do urlencoding of next entry, if so selected */
1036 dirent
= (struct smbc_dirent
*)context
->dirent
;
1037 maxlen
= (sizeof(context
->dirent
) -
1038 sizeof(struct smbc_dirent
));
1039 smbc_readdir_internal(context
, dirent
, dirlist
->dirent
, maxlen
);
1041 reqd
= dirent
->dirlen
;
1045 if (rem
< count
) { /* We managed to copy something */
1052 else { /* Nothing copied ... */
1054 errno
= EINVAL
; /* Not enough space ... */
1062 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1064 ((struct smbc_dirent
*)ndir
)->comment
=
1065 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
1073 dir
->dir_next
= dirlist
= dirlist
-> next
;
1086 * Routine to create a directory ...
1090 SMBC_mkdir_ctx(SMBCCTX
*context
,
1094 SMBCSRV
*srv
= NULL
;
1095 char *server
= NULL
;
1098 char *password
= NULL
;
1099 char *workgroup
= NULL
;
1101 char *targetpath
= NULL
;
1102 struct cli_state
*targetcli
= NULL
;
1103 TALLOC_CTX
*frame
= talloc_stackframe();
1105 if (!context
|| !context
->initialized
) {
1117 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1119 if (SMBC_parse_path(frame
,
1134 if (!user
|| user
[0] == (char)0) {
1135 user
= talloc_strdup(frame
, context
->user
);
1143 srv
= SMBC_server(frame
, context
, True
,
1144 server
, share
, &workgroup
, &user
, &password
);
1149 return -1; /* errno set by SMBC_server */
1153 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1154 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
1155 &targetcli
, &targetpath
)) {
1156 d_printf("Could not resolve %s\n", path
);
1160 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1162 if (!cli_mkdir(targetcli
, targetpath
)) {
1164 errno
= SMBC_errno(context
, targetcli
);
1176 * Our list function simply checks to see if a directory is not empty
1179 static int smbc_rmdir_dirempty
= True
;
1182 rmdir_list_fn(const char *mnt
,
1187 if (strncmp(finfo
->name
, ".", 1) != 0 &&
1188 strncmp(finfo
->name
, "..", 2) != 0) {
1189 smbc_rmdir_dirempty
= False
;
1194 * Routine to remove a directory
1198 SMBC_rmdir_ctx(SMBCCTX
*context
,
1201 SMBCSRV
*srv
= NULL
;
1202 char *server
= NULL
;
1205 char *password
= NULL
;
1206 char *workgroup
= NULL
;
1208 char *targetpath
= NULL
;
1209 struct cli_state
*targetcli
= NULL
;
1210 TALLOC_CTX
*frame
= talloc_stackframe();
1212 if (!context
|| !context
->initialized
) {
1224 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1226 if (SMBC_parse_path(frame
,
1241 if (!user
|| user
[0] == (char)0) {
1242 user
= talloc_strdup(frame
, context
->user
);
1250 srv
= SMBC_server(frame
, context
, True
,
1251 server
, share
, &workgroup
, &user
, &password
);
1256 return -1; /* errno set by SMBC_server */
1260 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1261 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
1262 &targetcli
, &targetpath
)) {
1263 d_printf("Could not resolve %s\n", path
);
1267 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1270 if (!cli_rmdir(targetcli
, targetpath
)) {
1272 errno
= SMBC_errno(context
, targetcli
);
1274 if (errno
== EACCES
) { /* Check if the dir empty or not */
1276 /* Local storage to avoid buffer overflows */
1279 smbc_rmdir_dirempty
= True
; /* Make this so ... */
1281 lpath
= talloc_asprintf(frame
, "%s\\*",
1289 if (cli_list(targetcli
, lpath
,
1290 aDIR
| aSYSTEM
| aHIDDEN
,
1291 rmdir_list_fn
, NULL
) < 0) {
1293 /* Fix errno to ignore latest error ... */
1294 DEBUG(5, ("smbc_rmdir: "
1295 "cli_list returned an error: %d\n",
1296 SMBC_errno(context
, targetcli
)));
1301 if (smbc_rmdir_dirempty
)
1319 * Routine to return the current directory position
1323 SMBC_telldir_ctx(SMBCCTX
*context
,
1326 TALLOC_CTX
*frame
= talloc_stackframe();
1328 if (!context
|| !context
->initialized
) {
1336 if (!dir
|| !SMBC_dlist_contains(context
->files
, dir
)) {
1344 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1352 /* See if we're already at the end. */
1353 if (dir
->dir_next
== NULL
) {
1360 * We return the pointer here as the offset
1363 return (off_t
)(long)dir
->dir_next
->dirent
;
1367 * A routine to run down the list and see if the entry is OK
1370 static struct smbc_dir_list
*
1371 check_dir_ent(struct smbc_dir_list
*list
,
1372 struct smbc_dirent
*dirent
)
1375 /* Run down the list looking for what we want */
1379 struct smbc_dir_list
*tmp
= list
;
1383 if (tmp
->dirent
== dirent
)
1392 return NULL
; /* Not found, or an error */
1398 * Routine to seek on a directory
1402 SMBC_lseekdir_ctx(SMBCCTX
*context
,
1406 long int l_offset
= offset
; /* Handle problems of size */
1407 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
1408 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
1409 TALLOC_CTX
*frame
= talloc_stackframe();
1411 if (!context
|| !context
->initialized
) {
1419 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1427 /* Now, check what we were passed and see if it is OK ... */
1429 if (dirent
== NULL
) { /* Seek to the begining of the list */
1431 dir
->dir_next
= dir
->dir_list
;
1437 if (offset
== -1) { /* Seek to the end of the list */
1438 dir
->dir_next
= NULL
;
1443 /* Now, run down the list and make sure that the entry is OK */
1444 /* This may need to be changed if we change the format of the list */
1446 if ((list_ent
= check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
1447 errno
= EINVAL
; /* Bad entry */
1452 dir
->dir_next
= list_ent
;
1459 * Routine to fstat a dir
1463 SMBC_fstatdir_ctx(SMBCCTX
*context
,
1468 if (!context
|| !context
->initialized
) {
1474 /* No code yet ... */
1479 SMBC_chmod_ctx(SMBCCTX
*context
,
1483 SMBCSRV
*srv
= NULL
;
1484 char *server
= NULL
;
1487 char *password
= NULL
;
1488 char *workgroup
= NULL
;
1491 TALLOC_CTX
*frame
= talloc_stackframe();
1493 if (!context
|| !context
->initialized
) {
1495 errno
= EINVAL
; /* Best I can think of ... */
1506 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
1508 if (SMBC_parse_path(frame
,
1523 if (!user
|| user
[0] == (char)0) {
1524 user
= talloc_strdup(frame
, context
->user
);
1532 srv
= SMBC_server(frame
, context
, True
,
1533 server
, share
, &workgroup
, &user
, &password
);
1537 return -1; /* errno set by SMBC_server */
1542 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
1543 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
1544 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
1545 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
1547 if (!cli_setatr(srv
->cli
, path
, mode
, 0)) {
1548 errno
= SMBC_errno(context
, srv
->cli
);
1558 SMBC_utimes_ctx(SMBCCTX
*context
,
1560 struct timeval
*tbuf
)
1562 SMBCSRV
*srv
= NULL
;
1563 char *server
= NULL
;
1566 char *password
= NULL
;
1567 char *workgroup
= NULL
;
1571 TALLOC_CTX
*frame
= talloc_stackframe();
1573 if (!context
|| !context
->initialized
) {
1575 errno
= EINVAL
; /* Best I can think of ... */
1587 access_time
= write_time
= time(NULL
);
1589 access_time
= tbuf
[0].tv_sec
;
1590 write_time
= tbuf
[1].tv_sec
;
1598 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
1599 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
1600 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
1604 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
1605 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
1606 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
1610 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1611 fname
, atimebuf
, mtimebuf
);
1614 if (SMBC_parse_path(frame
,
1629 if (!user
|| user
[0] == (char)0) {
1630 user
= talloc_strdup(frame
, context
->user
);
1638 srv
= SMBC_server(frame
, context
, True
,
1639 server
, share
, &workgroup
, &user
, &password
);
1643 return -1; /* errno set by SMBC_server */
1646 if (!SMBC_setatr(context
, srv
, path
,
1647 0, access_time
, write_time
, 0, 0)) {
1649 return -1; /* errno set by SMBC_setatr */
1657 * Routine to unlink() a file
1661 SMBC_unlink_ctx(SMBCCTX
*context
,
1664 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
, *workgroup
= NULL
;
1666 char *targetpath
= NULL
;
1667 struct cli_state
*targetcli
= NULL
;
1668 SMBCSRV
*srv
= NULL
;
1669 TALLOC_CTX
*frame
= talloc_stackframe();
1671 if (!context
|| !context
->initialized
) {
1673 errno
= EINVAL
; /* Best I can think of ... */
1686 if (SMBC_parse_path(frame
,
1701 if (!user
|| user
[0] == (char)0) {
1702 user
= talloc_strdup(frame
, context
->user
);
1710 srv
= SMBC_server(frame
, context
, True
,
1711 server
, share
, &workgroup
, &user
, &password
);
1715 return -1; /* SMBC_server sets errno */
1719 /*d_printf(">>>unlink: resolving %s\n", path);*/
1720 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
1721 &targetcli
, &targetpath
)) {
1722 d_printf("Could not resolve %s\n", path
);
1726 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1728 if (!cli_unlink(targetcli
, targetpath
)) {
1730 errno
= SMBC_errno(context
, targetcli
);
1732 if (errno
== EACCES
) { /* Check if the file is a directory */
1737 struct timespec write_time_ts
;
1738 struct timespec access_time_ts
;
1739 struct timespec change_time_ts
;
1742 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
1749 /* Hmmm, bad error ... What? */
1751 errno
= SMBC_errno(context
, targetcli
);
1758 if (IS_DOS_DIR(mode
))
1761 errno
= saverr
; /* Restore this */
1772 return 0; /* Success ... */
1777 * Routine to rename() a file
1781 SMBC_rename_ctx(SMBCCTX
*ocontext
,
1786 char *server1
= NULL
;
1787 char *share1
= NULL
;
1788 char *server2
= NULL
;
1789 char *share2
= NULL
;
1792 char *password1
= NULL
;
1793 char *password2
= NULL
;
1794 char *workgroup
= NULL
;
1797 char *targetpath1
= NULL
;
1798 char *targetpath2
= NULL
;
1799 struct cli_state
*targetcli1
= NULL
;
1800 struct cli_state
*targetcli2
= NULL
;
1801 SMBCSRV
*srv
= NULL
;
1802 TALLOC_CTX
*frame
= talloc_stackframe();
1804 if (!ocontext
|| !ncontext
||
1805 !ocontext
->initialized
||
1806 !ncontext
->initialized
) {
1808 errno
= EINVAL
; /* Best I can think of ... */
1813 if (!oname
|| !nname
) {
1819 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1821 if (SMBC_parse_path(frame
,
1836 if (!user1
|| user1
[0] == (char)0) {
1837 user1
= talloc_strdup(frame
, ocontext
->user
);
1845 if (SMBC_parse_path(frame
,
1860 if (!user2
|| user2
[0] == (char)0) {
1861 user2
= talloc_strdup(frame
, ncontext
->user
);
1869 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1870 strcmp(user1
, user2
)) {
1871 /* Can't rename across file systems, or users?? */
1877 srv
= SMBC_server(frame
, ocontext
, True
,
1878 server1
, share1
, &workgroup
, &user1
, &password1
);
1885 /*d_printf(">>>rename: resolving %s\n", path1);*/
1886 if (!cli_resolve_path(frame
, "", srv
->cli
, path1
,
1887 &targetcli1
, &targetpath1
)) {
1888 d_printf("Could not resolve %s\n", path1
);
1892 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1893 /*d_printf(">>>rename: resolving %s\n", path2);*/
1894 if (!cli_resolve_path(frame
, "", srv
->cli
, path2
,
1895 &targetcli2
, &targetpath2
)) {
1896 d_printf("Could not resolve %s\n", path2
);
1900 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1902 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1903 strcmp(targetcli1
->share
, targetcli2
->share
))
1905 /* can't rename across file systems */
1911 if (!cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1912 int eno
= SMBC_errno(ocontext
, targetcli1
);
1914 if (eno
!= EEXIST
||
1915 !cli_unlink(targetcli1
, targetpath2
) ||
1916 !cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1926 return 0; /* Success */