s3: Fix an uninitialized variable reference
[Samba.git] / source / libsmb / libsmb_dir.c
blob845ee5ef656d8eaee4b8af08170087047520bcd6
1 /*
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/>.
25 #include "includes.h"
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.
35 static void
36 remove_dir(SMBCFILE *dir)
38 struct smbc_dir_list *d,*f;
40 d = dir->dir_list;
41 while (d) {
43 f = d; d = d->next;
45 SAFE_FREE(f->dirent);
46 SAFE_FREE(f);
50 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
54 static int
55 add_dirent(SMBCFILE *dir,
56 const char *name,
57 const char *comment,
58 uint32 type)
60 struct smbc_dirent *dirent;
61 int size;
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);
74 if (!dirent) {
76 dir->dir_error = ENOMEM;
77 return -1;
81 ZERO_STRUCTP(dirent);
83 if (dir->dir_list == NULL) {
85 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
86 if (!dir->dir_list) {
88 SAFE_FREE(dirent);
89 dir->dir_error = ENOMEM;
90 return -1;
93 ZERO_STRUCTP(dir->dir_list);
95 dir->dir_end = dir->dir_next = dir->dir_list;
97 else {
99 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
101 if (!dir->dir_end->next) {
103 SAFE_FREE(dirent);
104 dir->dir_error = ENOMEM;
105 return -1;
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);
130 return 0;
134 static void
135 list_unique_wg_fn(const char *name,
136 uint32 type,
137 const char *comment,
138 void *state)
140 SMBCFILE *dir = (SMBCFILE *)state;
141 struct smbc_dir_list *dir_list;
142 struct smbc_dirent *dirent;
143 int dirent_type;
144 int do_remove = 0;
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) {
161 if (! do_remove &&
162 strcmp(dir_list->dirent->name, dirent->name) == 0) {
163 /* Duplicate. End end of list need to be removed. */
164 do_remove = 1;
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);
171 free(dirent);
172 dir_list->next = NULL;
173 break;
178 static void
179 list_fn(const char *name,
180 uint32 type,
181 const char *comment,
182 void *state)
184 SMBCFILE *dir = (SMBCFILE *)state;
185 int dirent_type;
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) {
200 switch (type) {
201 case 0 | 0x80000000:
202 case 0:
203 dirent_type = SMBC_FILE_SHARE;
204 break;
206 case 1:
207 dirent_type = SMBC_PRINTER_SHARE;
208 break;
210 case 2:
211 dirent_type = SMBC_COMMS_SHARE;
212 break;
214 case 3 | 0x80000000:
215 case 3:
216 dirent_type = SMBC_IPC_SHARE;
217 break;
219 default:
220 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
221 break;
224 else {
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 */
236 static void
237 dir_list_fn(const char *mnt,
238 file_info *finfo,
239 const char *mask,
240 void *state)
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 ... */
254 static int
255 net_share_enum_rpc(struct cli_state *cli,
256 void (*fn)(const char *name,
257 uint32 type,
258 const char *comment,
259 void *state),
260 void *state)
262 int i;
263 WERROR result;
264 uint32 preferred_len = 0xffffffff;
265 uint32 type;
266 struct srvsvc_NetShareInfoCtr info_ctr;
267 struct srvsvc_NetShareCtr1 ctr1;
268 fstring name = "";
269 fstring comment = "";
270 struct rpc_pipe_client *pipe_hnd;
271 NTSTATUS nt_status;
272 uint32_t resume_handle = 0;
273 uint32_t total_entries = 0;
275 /* Open the server service pipe */
276 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
277 &pipe_hnd);
278 if (!NT_STATUS_IS_OK(nt_status)) {
279 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
280 return -1;
283 ZERO_STRUCT(info_ctr);
284 ZERO_STRUCT(ctr1);
286 info_ctr.level = 1;
287 info_ctr.ctr.ctr1 = &ctr1;
289 /* Issue the NetShareEnum RPC call and retrieve the response */
290 nt_status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, talloc_tos(),
291 pipe_hnd->desthost,
292 &info_ctr,
293 preferred_len,
294 &total_entries,
295 &resume_handle,
296 &result);
298 /* Was it successful? */
299 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result) ||
300 total_entries == 0) {
301 /* Nope. Go clean up. */
302 goto done;
305 /* For each returned entry... */
306 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
308 /* pull out the share name */
309 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
311 /* pull out the share's comment */
312 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
314 /* Get the type value */
315 type = info_ctr.ctr.ctr1->array[i].type;
317 /* Add this share to the list */
318 (*fn)(name, type, comment, state);
321 done:
322 /* Close the server service pipe */
323 TALLOC_FREE(pipe_hnd);
325 /* Tell 'em if it worked */
326 return W_ERROR_IS_OK(result) ? 0 : -1;
331 * Verify that the options specified in a URL are valid
334 SMBC_check_options(char *server,
335 char *share,
336 char *path,
337 char *options)
339 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
340 "path='%s' options='%s'\n",
341 server, share, path, options));
343 /* No options at all is always ok */
344 if (! *options) return 0;
346 /* Currently, we don't support any options. */
347 return -1;
351 SMBCFILE *
352 SMBC_opendir_ctx(SMBCCTX *context,
353 const char *fname)
355 int saved_errno;
356 char *server = NULL;
357 char *share = NULL;
358 char *user = NULL;
359 char *password = NULL;
360 char *options = NULL;
361 char *workgroup = NULL;
362 char *path = NULL;
363 uint16 mode;
364 char *p = NULL;
365 SMBCSRV *srv = NULL;
366 SMBCFILE *dir = NULL;
367 struct sockaddr_storage rem_ss;
368 TALLOC_CTX *frame = talloc_stackframe();
370 if (!context || !context->internal->initialized) {
371 DEBUG(4, ("no valid context\n"));
372 errno = EINVAL + 8192;
373 TALLOC_FREE(frame);
374 return NULL;
378 if (!fname) {
379 DEBUG(4, ("no valid fname\n"));
380 errno = EINVAL + 8193;
381 TALLOC_FREE(frame);
382 return NULL;
385 if (SMBC_parse_path(frame,
386 context,
387 fname,
388 &workgroup,
389 &server,
390 &share,
391 &path,
392 &user,
393 &password,
394 &options)) {
395 DEBUG(4, ("no valid path\n"));
396 errno = EINVAL + 8194;
397 TALLOC_FREE(frame);
398 return NULL;
401 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
402 "path='%s' options='%s'\n",
403 fname, server, share, path, options));
405 /* Ensure the options are valid */
406 if (SMBC_check_options(server, share, path, options)) {
407 DEBUG(4, ("unacceptable options (%s)\n", options));
408 errno = EINVAL + 8195;
409 TALLOC_FREE(frame);
410 return NULL;
413 if (!user || user[0] == (char)0) {
414 user = talloc_strdup(frame, smbc_getUser(context));
415 if (!user) {
416 errno = ENOMEM;
417 TALLOC_FREE(frame);
418 return NULL;
422 dir = SMB_MALLOC_P(SMBCFILE);
424 if (!dir) {
425 errno = ENOMEM;
426 TALLOC_FREE(frame);
427 return NULL;
430 ZERO_STRUCTP(dir);
432 dir->cli_fd = 0;
433 dir->fname = SMB_STRDUP(fname);
434 dir->srv = NULL;
435 dir->offset = 0;
436 dir->file = False;
437 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
439 if (server[0] == (char)0) {
441 int i;
442 int count;
443 int max_lmb_count;
444 struct ip_service *ip_list;
445 struct ip_service server_addr;
446 struct user_auth_info u_info;
448 if (share[0] != (char)0 || path[0] != (char)0) {
450 errno = EINVAL + 8196;
451 if (dir) {
452 SAFE_FREE(dir->fname);
453 SAFE_FREE(dir);
455 TALLOC_FREE(frame);
456 return NULL;
459 /* Determine how many local master browsers to query */
460 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
461 ? INT_MAX
462 : smbc_getOptionBrowseMaxLmbCount(context));
464 memset(&u_info, '\0', sizeof(u_info));
465 u_info.username = talloc_strdup(frame,user);
466 u_info.password = talloc_strdup(frame,password);
467 if (!u_info.username || !u_info.password) {
468 if (dir) {
469 SAFE_FREE(dir->fname);
470 SAFE_FREE(dir);
472 TALLOC_FREE(frame);
473 return NULL;
477 * We have server and share and path empty but options
478 * requesting that we scan all master browsers for their list
479 * of workgroups/domains. This implies that we must first try
480 * broadcast queries to find all master browsers, and if that
481 * doesn't work, then try our other methods which return only
482 * a single master browser.
485 ip_list = NULL;
486 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
487 &count)))
490 SAFE_FREE(ip_list);
492 if (!find_master_ip(workgroup, &server_addr.ss)) {
494 if (dir) {
495 SAFE_FREE(dir->fname);
496 SAFE_FREE(dir);
498 errno = ENOENT;
499 TALLOC_FREE(frame);
500 return NULL;
503 ip_list = (struct ip_service *)memdup(
504 &server_addr, sizeof(server_addr));
505 if (ip_list == NULL) {
506 errno = ENOMEM;
507 TALLOC_FREE(frame);
508 return NULL;
510 count = 1;
513 for (i = 0; i < count && i < max_lmb_count; i++) {
514 char addr[INET6_ADDRSTRLEN];
515 char *wg_ptr = NULL;
516 struct cli_state *cli = NULL;
518 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
519 DEBUG(99, ("Found master browser %d of %d: %s\n",
520 i+1, MAX(count, max_lmb_count),
521 addr));
523 cli = get_ipc_connect_master_ip(talloc_tos(),
524 &ip_list[i],
525 &u_info,
526 &wg_ptr);
527 /* cli == NULL is the master browser refused to talk or
528 could not be found */
529 if (!cli) {
530 continue;
533 workgroup = talloc_strdup(frame, wg_ptr);
534 server = talloc_strdup(frame, cli->desthost);
536 cli_shutdown(cli);
538 if (!workgroup || !server) {
539 errno = ENOMEM;
540 TALLOC_FREE(frame);
541 return NULL;
544 DEBUG(4, ("using workgroup %s %s\n",
545 workgroup, server));
548 * For each returned master browser IP address, get a
549 * connection to IPC$ on the server if we do not
550 * already have one, and determine the
551 * workgroups/domains that it knows about.
554 srv = SMBC_server(frame, context, True, server, "IPC$",
555 &workgroup, &user, &password);
556 if (!srv) {
557 continue;
560 dir->srv = srv;
561 dir->dir_type = SMBC_WORKGROUP;
563 /* Now, list the stuff ... */
565 if (!cli_NetServerEnum(srv->cli,
566 workgroup,
567 SV_TYPE_DOMAIN_ENUM,
568 list_unique_wg_fn,
569 (void *)dir)) {
570 continue;
574 SAFE_FREE(ip_list);
575 } else {
577 * Server not an empty string ... Check the rest and see what
578 * gives
580 if (*share == '\0') {
581 if (*path != '\0') {
583 /* Should not have empty share with path */
584 errno = EINVAL + 8197;
585 if (dir) {
586 SAFE_FREE(dir->fname);
587 SAFE_FREE(dir);
589 TALLOC_FREE(frame);
590 return NULL;
595 * We don't know if <server> is really a server name
596 * or is a workgroup/domain name. If we already have
597 * a server structure for it, we'll use it.
598 * Otherwise, check to see if <server><1D>,
599 * <server><1B>, or <server><20> translates. We check
600 * to see if <server> is an IP address first.
604 * See if we have an existing server. Do not
605 * establish a connection if one does not already
606 * exist.
608 srv = SMBC_server(frame, context, False,
609 server, "IPC$",
610 &workgroup, &user, &password);
613 * If no existing server and not an IP addr, look for
614 * LMB or DMB
616 if (!srv &&
617 !is_ipaddress(server) &&
618 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
619 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
621 * "server" is actually a workgroup name,
622 * not a server. Make this clear.
624 char *wgroup = server;
625 fstring buserver;
627 dir->dir_type = SMBC_SERVER;
630 * Get the backup list ...
632 if (!name_status_find(wgroup, 0, 0,
633 &rem_ss, buserver)) {
634 char addr[INET6_ADDRSTRLEN];
636 print_sockaddr(addr, sizeof(addr), &rem_ss);
637 DEBUG(0,("Could not get name of "
638 "local/domain master browser "
639 "for workgroup %s fro m"
640 "address %s\n",
641 wgroup,
642 addr));
643 if (dir) {
644 SAFE_FREE(dir->fname);
645 SAFE_FREE(dir);
647 errno = EPERM;
648 TALLOC_FREE(frame);
649 return NULL;
654 * Get a connection to IPC$ on the server if
655 * we do not already have one
657 srv = SMBC_server(frame, context, True,
658 buserver, "IPC$",
659 &workgroup,
660 &user, &password);
661 if (!srv) {
662 DEBUG(0, ("got no contact to IPC$\n"));
663 if (dir) {
664 SAFE_FREE(dir->fname);
665 SAFE_FREE(dir);
667 TALLOC_FREE(frame);
668 return NULL;
672 dir->srv = srv;
674 /* Now, list the servers ... */
675 if (!cli_NetServerEnum(srv->cli, wgroup,
676 0x0000FFFE, list_fn,
677 (void *)dir)) {
679 if (dir) {
680 SAFE_FREE(dir->fname);
681 SAFE_FREE(dir);
683 TALLOC_FREE(frame);
684 return NULL;
686 } else if (srv ||
687 (resolve_name(server, &rem_ss, 0x20))) {
690 * If we hadn't found the server, get one now
692 if (!srv) {
693 srv = SMBC_server(frame, context, True,
694 server, "IPC$",
695 &workgroup,
696 &user, &password);
699 if (!srv) {
700 if (dir) {
701 SAFE_FREE(dir->fname);
702 SAFE_FREE(dir);
704 TALLOC_FREE(frame);
705 return NULL;
709 dir->dir_type = SMBC_FILE_SHARE;
710 dir->srv = srv;
712 /* List the shares ... */
714 if (net_share_enum_rpc(
715 srv->cli,
716 list_fn,
717 (void *) dir) < 0 &&
718 cli_RNetShareEnum(
719 srv->cli,
720 list_fn,
721 (void *)dir) < 0) {
723 errno = cli_errno(srv->cli);
724 if (dir) {
725 SAFE_FREE(dir->fname);
726 SAFE_FREE(dir);
728 TALLOC_FREE(frame);
729 return NULL;
732 } else {
733 /* Neither the workgroup nor server exists */
734 errno = ECONNREFUSED;
735 if (dir) {
736 SAFE_FREE(dir->fname);
737 SAFE_FREE(dir);
739 TALLOC_FREE(frame);
740 return NULL;
744 else {
746 * The server and share are specified ... work from
747 * there ...
749 char *targetpath;
750 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);
758 if (!srv) {
759 if (dir) {
760 SAFE_FREE(dir->fname);
761 SAFE_FREE(dir);
763 TALLOC_FREE(frame);
764 return NULL;
767 dir->srv = srv;
769 /* Now, list the files ... */
771 p = path + strlen(path);
772 path = talloc_asprintf_append(path, "\\*");
773 if (!path) {
774 if (dir) {
775 SAFE_FREE(dir->fname);
776 SAFE_FREE(dir);
778 TALLOC_FREE(frame);
779 return NULL;
782 if (!cli_resolve_path(frame, "", srv->cli, path,
783 &targetcli, &targetpath)) {
784 d_printf("Could not resolve %s\n", path);
785 if (dir) {
786 SAFE_FREE(dir->fname);
787 SAFE_FREE(dir);
789 TALLOC_FREE(frame);
790 return NULL;
793 if (cli_list(targetcli, targetpath,
794 aDIR | aSYSTEM | aHIDDEN,
795 dir_list_fn, (void *)dir) < 0) {
797 if (dir) {
798 SAFE_FREE(dir->fname);
799 SAFE_FREE(dir);
801 saved_errno = SMBC_errno(context, targetcli);
803 if (saved_errno == EINVAL) {
805 * See if they asked to opendir
806 * something other than a directory.
807 * If so, the converted error value we
808 * got would have been EINVAL rather
809 * than ENOTDIR.
811 *p = '\0'; /* restore original path */
813 if (SMBC_getatr(context, srv, path,
814 &mode, NULL,
815 NULL, NULL, NULL, NULL,
816 NULL) &&
817 ! IS_DOS_DIR(mode)) {
819 /* It is. Correct the error value */
820 saved_errno = ENOTDIR;
825 * If there was an error and the server is no
826 * good any more...
828 if (cli_is_error(targetcli) &&
829 smbc_getFunctionCheckServer(context)(context, srv)) {
831 /* ... then remove it. */
832 if (smbc_getFunctionRemoveUnusedServer(context)(context,
833 srv)) {
835 * We could not remove the
836 * server completely, remove
837 * it from the cache so we
838 * will not get it again. It
839 * will be removed when the
840 * last file/dir is closed.
842 smbc_getFunctionRemoveCachedServer(context)(context, srv);
846 errno = saved_errno;
847 TALLOC_FREE(frame);
848 return NULL;
854 DLIST_ADD(context->internal->files, dir);
855 TALLOC_FREE(frame);
856 return dir;
861 * Routine to close a directory
865 SMBC_closedir_ctx(SMBCCTX *context,
866 SMBCFILE *dir)
868 TALLOC_CTX *frame = talloc_stackframe();
870 if (!context || !context->internal->initialized) {
871 errno = EINVAL;
872 TALLOC_FREE(frame);
873 return -1;
876 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
877 errno = EBADF;
878 TALLOC_FREE(frame);
879 return -1;
882 remove_dir(dir); /* Clean it up */
884 DLIST_REMOVE(context->internal->files, dir);
886 if (dir) {
888 SAFE_FREE(dir->fname);
889 SAFE_FREE(dir); /* Free the space too */
892 TALLOC_FREE(frame);
893 return 0;
897 static void
898 smbc_readdir_internal(SMBCCTX * context,
899 struct smbc_dirent *dest,
900 struct smbc_dirent *src,
901 int max_namebuf_len)
903 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
905 /* url-encode the name. get back remaining buffer space */
906 max_namebuf_len =
907 smbc_urlencode(dest->name, src->name, max_namebuf_len);
909 /* We now know the name length */
910 dest->namelen = strlen(dest->name);
912 /* Save the pointer to the beginning of the comment */
913 dest->comment = dest->name + dest->namelen + 1;
915 /* Copy the comment */
916 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
917 dest->comment[max_namebuf_len - 1] = '\0';
919 /* Save other fields */
920 dest->smbc_type = src->smbc_type;
921 dest->commentlen = strlen(dest->comment);
922 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
923 (char *) dest);
924 } else {
926 /* No encoding. Just copy the entry as is. */
927 memcpy(dest, src, src->dirlen);
928 dest->comment = (char *)(&dest->name + src->namelen + 1);
934 * Routine to get a directory entry
937 struct smbc_dirent *
938 SMBC_readdir_ctx(SMBCCTX *context,
939 SMBCFILE *dir)
941 int maxlen;
942 struct smbc_dirent *dirp, *dirent;
943 TALLOC_CTX *frame = talloc_stackframe();
945 /* Check that all is ok first ... */
947 if (!context || !context->internal->initialized) {
949 errno = EINVAL;
950 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
951 TALLOC_FREE(frame);
952 return NULL;
956 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
958 errno = EBADF;
959 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
960 TALLOC_FREE(frame);
961 return NULL;
965 if (dir->file != False) { /* FIXME, should be dir, perhaps */
967 errno = ENOTDIR;
968 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
969 TALLOC_FREE(frame);
970 return NULL;
974 if (!dir->dir_next) {
975 TALLOC_FREE(frame);
976 return NULL;
979 dirent = dir->dir_next->dirent;
980 if (!dirent) {
982 errno = ENOENT;
983 TALLOC_FREE(frame);
984 return NULL;
988 dirp = &context->internal->dirent;
989 maxlen = sizeof(context->internal->_dirent_name);
991 smbc_readdir_internal(context, dirp, dirent, maxlen);
993 dir->dir_next = dir->dir_next->next;
995 TALLOC_FREE(frame);
996 return dirp;
1000 * Routine to get directory entries
1004 SMBC_getdents_ctx(SMBCCTX *context,
1005 SMBCFILE *dir,
1006 struct smbc_dirent *dirp,
1007 int count)
1009 int rem = count;
1010 int reqd;
1011 int maxlen;
1012 char *ndir = (char *)dirp;
1013 struct smbc_dir_list *dirlist;
1014 TALLOC_CTX *frame = talloc_stackframe();
1016 /* Check that all is ok first ... */
1018 if (!context || !context->internal->initialized) {
1020 errno = EINVAL;
1021 TALLOC_FREE(frame);
1022 return -1;
1026 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1028 errno = EBADF;
1029 TALLOC_FREE(frame);
1030 return -1;
1034 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1036 errno = ENOTDIR;
1037 TALLOC_FREE(frame);
1038 return -1;
1043 * Now, retrieve the number of entries that will fit in what was passed
1044 * We have to figure out if the info is in the list, or we need to
1045 * send a request to the server to get the info.
1048 while ((dirlist = dir->dir_next)) {
1049 struct smbc_dirent *dirent;
1051 if (!dirlist->dirent) {
1053 errno = ENOENT; /* Bad error */
1054 TALLOC_FREE(frame);
1055 return -1;
1059 /* Do urlencoding of next entry, if so selected */
1060 dirent = &context->internal->dirent;
1061 maxlen = sizeof(context->internal->_dirent_name);
1062 smbc_readdir_internal(context, dirent,
1063 dirlist->dirent, maxlen);
1065 reqd = dirent->dirlen;
1067 if (rem < reqd) {
1069 if (rem < count) { /* We managed to copy something */
1071 errno = 0;
1072 TALLOC_FREE(frame);
1073 return count - rem;
1076 else { /* Nothing copied ... */
1078 errno = EINVAL; /* Not enough space ... */
1079 TALLOC_FREE(frame);
1080 return -1;
1086 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1088 ((struct smbc_dirent *)ndir)->comment =
1089 (char *)(&((struct smbc_dirent *)ndir)->name +
1090 dirent->namelen +
1093 ndir += reqd;
1095 rem -= reqd;
1097 dir->dir_next = dirlist = dirlist -> next;
1100 TALLOC_FREE(frame);
1102 if (rem == count)
1103 return 0;
1104 else
1105 return count - rem;
1110 * Routine to create a directory ...
1114 SMBC_mkdir_ctx(SMBCCTX *context,
1115 const char *fname,
1116 mode_t mode)
1118 SMBCSRV *srv = NULL;
1119 char *server = NULL;
1120 char *share = NULL;
1121 char *user = NULL;
1122 char *password = NULL;
1123 char *workgroup = NULL;
1124 char *path = NULL;
1125 char *targetpath = NULL;
1126 struct cli_state *targetcli = NULL;
1127 TALLOC_CTX *frame = talloc_stackframe();
1129 if (!context || !context->internal->initialized) {
1130 errno = EINVAL;
1131 TALLOC_FREE(frame);
1132 return -1;
1135 if (!fname) {
1136 errno = EINVAL;
1137 TALLOC_FREE(frame);
1138 return -1;
1141 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1143 if (SMBC_parse_path(frame,
1144 context,
1145 fname,
1146 &workgroup,
1147 &server,
1148 &share,
1149 &path,
1150 &user,
1151 &password,
1152 NULL)) {
1153 errno = EINVAL;
1154 TALLOC_FREE(frame);
1155 return -1;
1158 if (!user || user[0] == (char)0) {
1159 user = talloc_strdup(frame, smbc_getUser(context));
1160 if (!user) {
1161 errno = ENOMEM;
1162 TALLOC_FREE(frame);
1163 return -1;
1167 srv = SMBC_server(frame, context, True,
1168 server, share, &workgroup, &user, &password);
1170 if (!srv) {
1172 TALLOC_FREE(frame);
1173 return -1; /* errno set by SMBC_server */
1177 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1178 if (!cli_resolve_path(frame, "", srv->cli, path,
1179 &targetcli, &targetpath)) {
1180 d_printf("Could not resolve %s\n", path);
1181 errno = ENOENT;
1182 TALLOC_FREE(frame);
1183 return -1;
1185 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1187 if (!cli_mkdir(targetcli, targetpath)) {
1189 errno = SMBC_errno(context, targetcli);
1190 TALLOC_FREE(frame);
1191 return -1;
1195 TALLOC_FREE(frame);
1196 return 0;
1201 * Our list function simply checks to see if a directory is not empty
1204 static int smbc_rmdir_dirempty = True;
1206 static void
1207 rmdir_list_fn(const char *mnt,
1208 file_info *finfo,
1209 const char *mask,
1210 void *state)
1212 if (strncmp(finfo->name, ".", 1) != 0 &&
1213 strncmp(finfo->name, "..", 2) != 0) {
1214 smbc_rmdir_dirempty = False;
1219 * Routine to remove a directory
1223 SMBC_rmdir_ctx(SMBCCTX *context,
1224 const char *fname)
1226 SMBCSRV *srv = NULL;
1227 char *server = NULL;
1228 char *share = NULL;
1229 char *user = NULL;
1230 char *password = NULL;
1231 char *workgroup = NULL;
1232 char *path = NULL;
1233 char *targetpath = NULL;
1234 struct cli_state *targetcli = NULL;
1235 TALLOC_CTX *frame = talloc_stackframe();
1237 if (!context || !context->internal->initialized) {
1238 errno = EINVAL;
1239 TALLOC_FREE(frame);
1240 return -1;
1243 if (!fname) {
1244 errno = EINVAL;
1245 TALLOC_FREE(frame);
1246 return -1;
1249 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1251 if (SMBC_parse_path(frame,
1252 context,
1253 fname,
1254 &workgroup,
1255 &server,
1256 &share,
1257 &path,
1258 &user,
1259 &password,
1260 NULL)) {
1261 errno = EINVAL;
1262 TALLOC_FREE(frame);
1263 return -1;
1266 if (!user || user[0] == (char)0) {
1267 user = talloc_strdup(frame, smbc_getUser(context));
1268 if (!user) {
1269 errno = ENOMEM;
1270 TALLOC_FREE(frame);
1271 return -1;
1275 srv = SMBC_server(frame, context, True,
1276 server, share, &workgroup, &user, &password);
1278 if (!srv) {
1280 TALLOC_FREE(frame);
1281 return -1; /* errno set by SMBC_server */
1285 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1286 if (!cli_resolve_path(frame, "", srv->cli, path,
1287 &targetcli, &targetpath)) {
1288 d_printf("Could not resolve %s\n", path);
1289 errno = ENOENT;
1290 TALLOC_FREE(frame);
1291 return -1;
1293 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1296 if (!cli_rmdir(targetcli, targetpath)) {
1298 errno = SMBC_errno(context, targetcli);
1300 if (errno == EACCES) { /* Check if the dir empty or not */
1302 /* Local storage to avoid buffer overflows */
1303 char *lpath;
1305 smbc_rmdir_dirempty = True; /* Make this so ... */
1307 lpath = talloc_asprintf(frame, "%s\\*",
1308 targetpath);
1309 if (!lpath) {
1310 errno = ENOMEM;
1311 TALLOC_FREE(frame);
1312 return -1;
1315 if (cli_list(targetcli, lpath,
1316 aDIR | aSYSTEM | aHIDDEN,
1317 rmdir_list_fn, NULL) < 0) {
1319 /* Fix errno to ignore latest error ... */
1320 DEBUG(5, ("smbc_rmdir: "
1321 "cli_list returned an error: %d\n",
1322 SMBC_errno(context, targetcli)));
1323 errno = EACCES;
1327 if (smbc_rmdir_dirempty)
1328 errno = EACCES;
1329 else
1330 errno = ENOTEMPTY;
1334 TALLOC_FREE(frame);
1335 return -1;
1339 TALLOC_FREE(frame);
1340 return 0;
1345 * Routine to return the current directory position
1348 off_t
1349 SMBC_telldir_ctx(SMBCCTX *context,
1350 SMBCFILE *dir)
1352 TALLOC_CTX *frame = talloc_stackframe();
1354 if (!context || !context->internal->initialized) {
1356 errno = EINVAL;
1357 TALLOC_FREE(frame);
1358 return -1;
1362 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1364 errno = EBADF;
1365 TALLOC_FREE(frame);
1366 return -1;
1370 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1372 errno = ENOTDIR;
1373 TALLOC_FREE(frame);
1374 return -1;
1378 /* See if we're already at the end. */
1379 if (dir->dir_next == NULL) {
1380 /* We are. */
1381 TALLOC_FREE(frame);
1382 return -1;
1386 * We return the pointer here as the offset
1388 TALLOC_FREE(frame);
1389 return (off_t)(long)dir->dir_next->dirent;
1393 * A routine to run down the list and see if the entry is OK
1396 static struct smbc_dir_list *
1397 check_dir_ent(struct smbc_dir_list *list,
1398 struct smbc_dirent *dirent)
1401 /* Run down the list looking for what we want */
1403 if (dirent) {
1405 struct smbc_dir_list *tmp = list;
1407 while (tmp) {
1409 if (tmp->dirent == dirent)
1410 return tmp;
1412 tmp = tmp->next;
1418 return NULL; /* Not found, or an error */
1424 * Routine to seek on a directory
1428 SMBC_lseekdir_ctx(SMBCCTX *context,
1429 SMBCFILE *dir,
1430 off_t offset)
1432 long int l_offset = offset; /* Handle problems of size */
1433 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1434 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1435 TALLOC_CTX *frame = talloc_stackframe();
1437 if (!context || !context->internal->initialized) {
1439 errno = EINVAL;
1440 TALLOC_FREE(frame);
1441 return -1;
1445 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1447 errno = ENOTDIR;
1448 TALLOC_FREE(frame);
1449 return -1;
1453 /* Now, check what we were passed and see if it is OK ... */
1455 if (dirent == NULL) { /* Seek to the begining of the list */
1457 dir->dir_next = dir->dir_list;
1458 TALLOC_FREE(frame);
1459 return 0;
1463 if (offset == -1) { /* Seek to the end of the list */
1464 dir->dir_next = NULL;
1465 TALLOC_FREE(frame);
1466 return 0;
1469 /* Now, run down the list and make sure that the entry is OK */
1470 /* This may need to be changed if we change the format of the list */
1472 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1473 errno = EINVAL; /* Bad entry */
1474 TALLOC_FREE(frame);
1475 return -1;
1478 dir->dir_next = list_ent;
1480 TALLOC_FREE(frame);
1481 return 0;
1485 * Routine to fstat a dir
1489 SMBC_fstatdir_ctx(SMBCCTX *context,
1490 SMBCFILE *dir,
1491 struct stat *st)
1494 if (!context || !context->internal->initialized) {
1496 errno = EINVAL;
1497 return -1;
1500 /* No code yet ... */
1501 return 0;
1505 SMBC_chmod_ctx(SMBCCTX *context,
1506 const char *fname,
1507 mode_t newmode)
1509 SMBCSRV *srv = NULL;
1510 char *server = NULL;
1511 char *share = NULL;
1512 char *user = NULL;
1513 char *password = NULL;
1514 char *workgroup = NULL;
1515 char *targetpath = NULL;
1516 struct cli_state *targetcli = NULL;
1517 char *path = NULL;
1518 uint16 mode;
1519 TALLOC_CTX *frame = talloc_stackframe();
1521 if (!context || !context->internal->initialized) {
1523 errno = EINVAL; /* Best I can think of ... */
1524 TALLOC_FREE(frame);
1525 return -1;
1528 if (!fname) {
1529 errno = EINVAL;
1530 TALLOC_FREE(frame);
1531 return -1;
1534 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1536 if (SMBC_parse_path(frame,
1537 context,
1538 fname,
1539 &workgroup,
1540 &server,
1541 &share,
1542 &path,
1543 &user,
1544 &password,
1545 NULL)) {
1546 errno = EINVAL;
1547 TALLOC_FREE(frame);
1548 return -1;
1551 if (!user || user[0] == (char)0) {
1552 user = talloc_strdup(frame, smbc_getUser(context));
1553 if (!user) {
1554 errno = ENOMEM;
1555 TALLOC_FREE(frame);
1556 return -1;
1560 srv = SMBC_server(frame, context, True,
1561 server, share, &workgroup, &user, &password);
1563 if (!srv) {
1564 TALLOC_FREE(frame);
1565 return -1; /* errno set by SMBC_server */
1568 /*d_printf(">>>unlink: resolving %s\n", path);*/
1569 if (!cli_resolve_path(frame, "", srv->cli, path,
1570 &targetcli, &targetpath)) {
1571 d_printf("Could not resolve %s\n", path);
1572 errno = ENOENT;
1573 TALLOC_FREE(frame);
1574 return -1;
1577 mode = 0;
1579 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1580 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1581 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1582 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1584 if (!cli_setatr(targetcli, targetpath, mode, 0)) {
1585 errno = SMBC_errno(context, targetcli);
1586 TALLOC_FREE(frame);
1587 return -1;
1590 TALLOC_FREE(frame);
1591 return 0;
1595 SMBC_utimes_ctx(SMBCCTX *context,
1596 const char *fname,
1597 struct timeval *tbuf)
1599 SMBCSRV *srv = NULL;
1600 char *server = NULL;
1601 char *share = NULL;
1602 char *user = NULL;
1603 char *password = NULL;
1604 char *workgroup = NULL;
1605 char *path = NULL;
1606 time_t access_time;
1607 time_t write_time;
1608 TALLOC_CTX *frame = talloc_stackframe();
1610 if (!context || !context->internal->initialized) {
1612 errno = EINVAL; /* Best I can think of ... */
1613 TALLOC_FREE(frame);
1614 return -1;
1617 if (!fname) {
1618 errno = EINVAL;
1619 TALLOC_FREE(frame);
1620 return -1;
1623 if (tbuf == NULL) {
1624 access_time = write_time = time(NULL);
1625 } else {
1626 access_time = tbuf[0].tv_sec;
1627 write_time = tbuf[1].tv_sec;
1630 if (DEBUGLVL(4)) {
1631 char *p;
1632 char atimebuf[32];
1633 char mtimebuf[32];
1635 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1636 atimebuf[sizeof(atimebuf) - 1] = '\0';
1637 if ((p = strchr(atimebuf, '\n')) != NULL) {
1638 *p = '\0';
1641 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1642 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1643 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1644 *p = '\0';
1647 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1648 fname, atimebuf, mtimebuf);
1651 if (SMBC_parse_path(frame,
1652 context,
1653 fname,
1654 &workgroup,
1655 &server,
1656 &share,
1657 &path,
1658 &user,
1659 &password,
1660 NULL)) {
1661 errno = EINVAL;
1662 TALLOC_FREE(frame);
1663 return -1;
1666 if (!user || user[0] == (char)0) {
1667 user = talloc_strdup(frame, smbc_getUser(context));
1668 if (!user) {
1669 errno = ENOMEM;
1670 TALLOC_FREE(frame);
1671 return -1;
1675 srv = SMBC_server(frame, context, True,
1676 server, share, &workgroup, &user, &password);
1678 if (!srv) {
1679 TALLOC_FREE(frame);
1680 return -1; /* errno set by SMBC_server */
1683 if (!SMBC_setatr(context, srv, path,
1684 0, access_time, write_time, 0, 0)) {
1685 TALLOC_FREE(frame);
1686 return -1; /* errno set by SMBC_setatr */
1689 TALLOC_FREE(frame);
1690 return 0;
1694 * Routine to unlink() a file
1698 SMBC_unlink_ctx(SMBCCTX *context,
1699 const char *fname)
1701 char *server = NULL;
1702 char *share = NULL;
1703 char *user = NULL;
1704 char *password = NULL;
1705 char *workgroup = NULL;
1706 char *path = NULL;
1707 char *targetpath = NULL;
1708 struct cli_state *targetcli = NULL;
1709 SMBCSRV *srv = NULL;
1710 TALLOC_CTX *frame = talloc_stackframe();
1712 if (!context || !context->internal->initialized) {
1714 errno = EINVAL; /* Best I can think of ... */
1715 TALLOC_FREE(frame);
1716 return -1;
1720 if (!fname) {
1721 errno = EINVAL;
1722 TALLOC_FREE(frame);
1723 return -1;
1727 if (SMBC_parse_path(frame,
1728 context,
1729 fname,
1730 &workgroup,
1731 &server,
1732 &share,
1733 &path,
1734 &user,
1735 &password,
1736 NULL)) {
1737 errno = EINVAL;
1738 TALLOC_FREE(frame);
1739 return -1;
1742 if (!user || user[0] == (char)0) {
1743 user = talloc_strdup(frame, smbc_getUser(context));
1744 if (!user) {
1745 errno = ENOMEM;
1746 TALLOC_FREE(frame);
1747 return -1;
1751 srv = SMBC_server(frame, context, True,
1752 server, share, &workgroup, &user, &password);
1754 if (!srv) {
1755 TALLOC_FREE(frame);
1756 return -1; /* SMBC_server sets errno */
1760 /*d_printf(">>>unlink: resolving %s\n", path);*/
1761 if (!cli_resolve_path(frame, "", srv->cli, path,
1762 &targetcli, &targetpath)) {
1763 d_printf("Could not resolve %s\n", path);
1764 errno = ENOENT;
1765 TALLOC_FREE(frame);
1766 return -1;
1768 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1770 if (!cli_unlink(targetcli, targetpath)) {
1772 errno = SMBC_errno(context, targetcli);
1774 if (errno == EACCES) { /* Check if the file is a directory */
1776 int saverr = errno;
1777 SMB_OFF_T size = 0;
1778 uint16 mode = 0;
1779 struct timespec write_time_ts;
1780 struct timespec access_time_ts;
1781 struct timespec change_time_ts;
1782 SMB_INO_T ino = 0;
1784 if (!SMBC_getatr(context, srv, path, &mode, &size,
1785 NULL,
1786 &access_time_ts,
1787 &write_time_ts,
1788 &change_time_ts,
1789 &ino)) {
1791 /* Hmmm, bad error ... What? */
1793 errno = SMBC_errno(context, targetcli);
1794 TALLOC_FREE(frame);
1795 return -1;
1798 else {
1800 if (IS_DOS_DIR(mode))
1801 errno = EISDIR;
1802 else
1803 errno = saverr; /* Restore this */
1808 TALLOC_FREE(frame);
1809 return -1;
1813 TALLOC_FREE(frame);
1814 return 0; /* Success ... */
1819 * Routine to rename() a file
1823 SMBC_rename_ctx(SMBCCTX *ocontext,
1824 const char *oname,
1825 SMBCCTX *ncontext,
1826 const char *nname)
1828 char *server1 = NULL;
1829 char *share1 = NULL;
1830 char *server2 = NULL;
1831 char *share2 = NULL;
1832 char *user1 = NULL;
1833 char *user2 = NULL;
1834 char *password1 = NULL;
1835 char *password2 = NULL;
1836 char *workgroup = NULL;
1837 char *path1 = NULL;
1838 char *path2 = NULL;
1839 char *targetpath1 = NULL;
1840 char *targetpath2 = NULL;
1841 struct cli_state *targetcli1 = NULL;
1842 struct cli_state *targetcli2 = NULL;
1843 SMBCSRV *srv = NULL;
1844 TALLOC_CTX *frame = talloc_stackframe();
1846 if (!ocontext || !ncontext ||
1847 !ocontext->internal->initialized ||
1848 !ncontext->internal->initialized) {
1850 errno = EINVAL; /* Best I can think of ... */
1851 TALLOC_FREE(frame);
1852 return -1;
1855 if (!oname || !nname) {
1856 errno = EINVAL;
1857 TALLOC_FREE(frame);
1858 return -1;
1861 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1863 if (SMBC_parse_path(frame,
1864 ocontext,
1865 oname,
1866 &workgroup,
1867 &server1,
1868 &share1,
1869 &path1,
1870 &user1,
1871 &password1,
1872 NULL)) {
1873 errno = EINVAL;
1874 TALLOC_FREE(frame);
1875 return -1;
1878 if (!user1 || user1[0] == (char)0) {
1879 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1880 if (!user1) {
1881 errno = ENOMEM;
1882 TALLOC_FREE(frame);
1883 return -1;
1887 if (SMBC_parse_path(frame,
1888 ncontext,
1889 nname,
1890 NULL,
1891 &server2,
1892 &share2,
1893 &path2,
1894 &user2,
1895 &password2,
1896 NULL)) {
1897 errno = EINVAL;
1898 TALLOC_FREE(frame);
1899 return -1;
1902 if (!user2 || user2[0] == (char)0) {
1903 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1904 if (!user2) {
1905 errno = ENOMEM;
1906 TALLOC_FREE(frame);
1907 return -1;
1911 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1912 strcmp(user1, user2)) {
1913 /* Can't rename across file systems, or users?? */
1914 errno = EXDEV;
1915 TALLOC_FREE(frame);
1916 return -1;
1919 srv = SMBC_server(frame, ocontext, True,
1920 server1, share1, &workgroup, &user1, &password1);
1921 if (!srv) {
1922 TALLOC_FREE(frame);
1923 return -1;
1927 /* set the credentials to make DFS work */
1928 smbc_set_credentials_with_fallback(ocontext,
1929 workgroup,
1930 user1,
1931 password1);
1933 /*d_printf(">>>rename: resolving %s\n", path1);*/
1934 if (!cli_resolve_path(frame, "", srv->cli, path1,
1935 &targetcli1, &targetpath1)) {
1936 d_printf("Could not resolve %s\n", path1);
1937 errno = ENOENT;
1938 TALLOC_FREE(frame);
1939 return -1;
1942 /* set the credentials to make DFS work */
1943 smbc_set_credentials_with_fallback(ncontext,
1944 workgroup,
1945 user2,
1946 password2);
1948 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1949 /*d_printf(">>>rename: resolving %s\n", path2);*/
1950 if (!cli_resolve_path(frame, "", srv->cli, path2,
1951 &targetcli2, &targetpath2)) {
1952 d_printf("Could not resolve %s\n", path2);
1953 errno = ENOENT;
1954 TALLOC_FREE(frame);
1955 return -1;
1957 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1959 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1960 strcmp(targetcli1->share, targetcli2->share))
1962 /* can't rename across file systems */
1963 errno = EXDEV;
1964 TALLOC_FREE(frame);
1965 return -1;
1968 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1969 int eno = SMBC_errno(ocontext, targetcli1);
1971 if (eno != EEXIST ||
1972 !cli_unlink(targetcli1, targetpath2) ||
1973 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1975 errno = eno;
1976 TALLOC_FREE(frame);
1977 return -1;
1982 TALLOC_FREE(frame);
1983 return 0; /* Success */