s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob7661ecf2f8a6250eda414cb07d86c9d8c5a3f899
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 "popt_common.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "../librpc/gen_ndr/cli_srvsvc.h"
32 * Routine to open a directory
33 * We accept the URL syntax explained in SMBC_parse_path(), above.
36 static void
37 remove_dir(SMBCFILE *dir)
39 struct smbc_dir_list *d,*f;
41 d = dir->dir_list;
42 while (d) {
44 f = d; d = d->next;
46 SAFE_FREE(f->dirent);
47 SAFE_FREE(f);
51 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
55 static int
56 add_dirent(SMBCFILE *dir,
57 const char *name,
58 const char *comment,
59 uint32 type)
61 struct smbc_dirent *dirent;
62 int size;
63 int name_length = (name == NULL ? 0 : strlen(name));
64 int comment_len = (comment == NULL ? 0 : strlen(comment));
67 * Allocate space for the dirent, which must be increased by the
68 * size of the name and the comment and 1 each for the null terminator.
71 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
73 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
75 if (!dirent) {
77 dir->dir_error = ENOMEM;
78 return -1;
82 ZERO_STRUCTP(dirent);
84 if (dir->dir_list == NULL) {
86 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
87 if (!dir->dir_list) {
89 SAFE_FREE(dirent);
90 dir->dir_error = ENOMEM;
91 return -1;
94 ZERO_STRUCTP(dir->dir_list);
96 dir->dir_end = dir->dir_next = dir->dir_list;
98 else {
100 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
102 if (!dir->dir_end->next) {
104 SAFE_FREE(dirent);
105 dir->dir_error = ENOMEM;
106 return -1;
109 ZERO_STRUCTP(dir->dir_end->next);
111 dir->dir_end = dir->dir_end->next;
114 dir->dir_end->next = NULL;
115 dir->dir_end->dirent = dirent;
117 dirent->smbc_type = type;
118 dirent->namelen = name_length;
119 dirent->commentlen = comment_len;
120 dirent->dirlen = size;
123 * dirent->namelen + 1 includes the null (no null termination needed)
124 * Ditto for dirent->commentlen.
125 * The space for the two null bytes was allocated.
127 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
128 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
129 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
131 return 0;
135 static void
136 list_unique_wg_fn(const char *name,
137 uint32 type,
138 const char *comment,
139 void *state)
141 SMBCFILE *dir = (SMBCFILE *)state;
142 struct smbc_dir_list *dir_list;
143 struct smbc_dirent *dirent;
144 int dirent_type;
145 int do_remove = 0;
147 dirent_type = dir->dir_type;
149 if (add_dirent(dir, name, comment, dirent_type) < 0) {
151 /* An error occurred, what do we do? */
152 /* FIXME: Add some code here */
155 /* Point to the one just added */
156 dirent = dir->dir_end->dirent;
158 /* See if this was a duplicate */
159 for (dir_list = dir->dir_list;
160 dir_list != dir->dir_end;
161 dir_list = dir_list->next) {
162 if (! do_remove &&
163 strcmp(dir_list->dirent->name, dirent->name) == 0) {
164 /* Duplicate. End end of list need to be removed. */
165 do_remove = 1;
168 if (do_remove && dir_list->next == dir->dir_end) {
169 /* Found the end of the list. Remove it. */
170 dir->dir_end = dir_list;
171 free(dir_list->next);
172 free(dirent);
173 dir_list->next = NULL;
174 break;
179 static void
180 list_fn(const char *name,
181 uint32 type,
182 const char *comment,
183 void *state)
185 SMBCFILE *dir = (SMBCFILE *)state;
186 int dirent_type;
189 * We need to process the type a little ...
191 * Disk share = 0x00000000
192 * Print share = 0x00000001
193 * Comms share = 0x00000002 (obsolete?)
194 * IPC$ share = 0x00000003
196 * administrative shares:
197 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
200 if (dir->dir_type == SMBC_FILE_SHARE) {
201 switch (type) {
202 case 0 | 0x80000000:
203 case 0:
204 dirent_type = SMBC_FILE_SHARE;
205 break;
207 case 1:
208 dirent_type = SMBC_PRINTER_SHARE;
209 break;
211 case 2:
212 dirent_type = SMBC_COMMS_SHARE;
213 break;
215 case 3 | 0x80000000:
216 case 3:
217 dirent_type = SMBC_IPC_SHARE;
218 break;
220 default:
221 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
222 break;
225 else {
226 dirent_type = dir->dir_type;
229 if (add_dirent(dir, name, comment, dirent_type) < 0) {
231 /* An error occurred, what do we do? */
232 /* FIXME: Add some code here */
237 static void
238 dir_list_fn(const char *mnt,
239 struct file_info *finfo,
240 const char *mask,
241 void *state)
244 if (add_dirent((SMBCFILE *)state, finfo->name, "",
245 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
247 /* Handle an error ... */
249 /* FIXME: Add some code ... */
255 static int
256 net_share_enum_rpc(struct cli_state *cli,
257 void (*fn)(const char *name,
258 uint32 type,
259 const char *comment,
260 void *state),
261 void *state)
263 int i;
264 WERROR result;
265 uint32 preferred_len = 0xffffffff;
266 uint32 type;
267 struct srvsvc_NetShareInfoCtr info_ctr;
268 struct srvsvc_NetShareCtr1 ctr1;
269 fstring name = "";
270 fstring comment = "";
271 struct rpc_pipe_client *pipe_hnd = NULL;
272 NTSTATUS nt_status;
273 uint32_t resume_handle = 0;
274 uint32_t total_entries = 0;
276 /* Open the server service pipe */
277 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
278 &pipe_hnd);
279 if (!NT_STATUS_IS_OK(nt_status)) {
280 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
281 return -1;
284 ZERO_STRUCT(info_ctr);
285 ZERO_STRUCT(ctr1);
287 info_ctr.level = 1;
288 info_ctr.ctr.ctr1 = &ctr1;
290 /* Issue the NetShareEnum RPC call and retrieve the response */
291 nt_status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, talloc_tos(),
292 pipe_hnd->desthost,
293 &info_ctr,
294 preferred_len,
295 &total_entries,
296 &resume_handle,
297 &result);
299 /* Was it successful? */
300 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result) ||
301 total_entries == 0) {
302 /* Nope. Go clean up. */
303 goto done;
306 /* For each returned entry... */
307 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
309 /* pull out the share name */
310 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
312 /* pull out the share's comment */
313 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
315 /* Get the type value */
316 type = info_ctr.ctr.ctr1->array[i].type;
318 /* Add this share to the list */
319 (*fn)(name, type, comment, state);
322 done:
323 /* Close the server service pipe */
324 TALLOC_FREE(pipe_hnd);
326 /* Tell 'em if it worked */
327 return W_ERROR_IS_OK(result) ? 0 : -1;
332 * Verify that the options specified in a URL are valid
335 SMBC_check_options(char *server,
336 char *share,
337 char *path,
338 char *options)
340 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
341 "path='%s' options='%s'\n",
342 server, share, path, options));
344 /* No options at all is always ok */
345 if (! *options) return 0;
347 /* Currently, we don't support any options. */
348 return -1;
352 SMBCFILE *
353 SMBC_opendir_ctx(SMBCCTX *context,
354 const char *fname)
356 int saved_errno;
357 char *server = NULL;
358 char *share = NULL;
359 char *user = NULL;
360 char *password = NULL;
361 char *options = NULL;
362 char *workgroup = NULL;
363 char *path = NULL;
364 uint16 mode;
365 char *p = NULL;
366 SMBCSRV *srv = NULL;
367 SMBCFILE *dir = NULL;
368 struct sockaddr_storage rem_ss;
369 TALLOC_CTX *frame = talloc_stackframe();
371 if (!context || !context->internal->initialized) {
372 DEBUG(4, ("no valid context\n"));
373 errno = EINVAL + 8192;
374 TALLOC_FREE(frame);
375 return NULL;
379 if (!fname) {
380 DEBUG(4, ("no valid fname\n"));
381 errno = EINVAL + 8193;
382 TALLOC_FREE(frame);
383 return NULL;
386 if (SMBC_parse_path(frame,
387 context,
388 fname,
389 &workgroup,
390 &server,
391 &share,
392 &path,
393 &user,
394 &password,
395 &options)) {
396 DEBUG(4, ("no valid path\n"));
397 errno = EINVAL + 8194;
398 TALLOC_FREE(frame);
399 return NULL;
402 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
403 "path='%s' options='%s'\n",
404 fname, server, share, path, options));
406 /* Ensure the options are valid */
407 if (SMBC_check_options(server, share, path, options)) {
408 DEBUG(4, ("unacceptable options (%s)\n", options));
409 errno = EINVAL + 8195;
410 TALLOC_FREE(frame);
411 return NULL;
414 if (!user || user[0] == (char)0) {
415 user = talloc_strdup(frame, smbc_getUser(context));
416 if (!user) {
417 errno = ENOMEM;
418 TALLOC_FREE(frame);
419 return NULL;
423 dir = SMB_MALLOC_P(SMBCFILE);
425 if (!dir) {
426 errno = ENOMEM;
427 TALLOC_FREE(frame);
428 return NULL;
431 ZERO_STRUCTP(dir);
433 dir->cli_fd = 0;
434 dir->fname = SMB_STRDUP(fname);
435 dir->srv = NULL;
436 dir->offset = 0;
437 dir->file = False;
438 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
440 if (server[0] == (char)0) {
442 int i;
443 int count;
444 int max_lmb_count;
445 struct ip_service *ip_list;
446 struct ip_service server_addr;
447 struct user_auth_info u_info;
449 if (share[0] != (char)0 || path[0] != (char)0) {
451 errno = EINVAL + 8196;
452 if (dir) {
453 SAFE_FREE(dir->fname);
454 SAFE_FREE(dir);
456 TALLOC_FREE(frame);
457 return NULL;
460 /* Determine how many local master browsers to query */
461 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
462 ? INT_MAX
463 : smbc_getOptionBrowseMaxLmbCount(context));
465 memset(&u_info, '\0', sizeof(u_info));
466 u_info.username = talloc_strdup(frame,user);
467 u_info.password = talloc_strdup(frame,password);
468 if (!u_info.username || !u_info.password) {
469 if (dir) {
470 SAFE_FREE(dir->fname);
471 SAFE_FREE(dir);
473 TALLOC_FREE(frame);
474 return NULL;
478 * We have server and share and path empty but options
479 * requesting that we scan all master browsers for their list
480 * of workgroups/domains. This implies that we must first try
481 * broadcast queries to find all master browsers, and if that
482 * doesn't work, then try our other methods which return only
483 * a single master browser.
486 ip_list = NULL;
487 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
488 &count)))
491 SAFE_FREE(ip_list);
493 if (!find_master_ip(workgroup, &server_addr.ss)) {
495 if (dir) {
496 SAFE_FREE(dir->fname);
497 SAFE_FREE(dir);
499 errno = ENOENT;
500 TALLOC_FREE(frame);
501 return NULL;
504 ip_list = (struct ip_service *)memdup(
505 &server_addr, sizeof(server_addr));
506 if (ip_list == NULL) {
507 errno = ENOMEM;
508 TALLOC_FREE(frame);
509 return NULL;
511 count = 1;
514 for (i = 0; i < count && i < max_lmb_count; i++) {
515 char addr[INET6_ADDRSTRLEN];
516 char *wg_ptr = NULL;
517 struct cli_state *cli = NULL;
519 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
520 DEBUG(99, ("Found master browser %d of %d: %s\n",
521 i+1, MAX(count, max_lmb_count),
522 addr));
524 cli = get_ipc_connect_master_ip(talloc_tos(),
525 &ip_list[i],
526 &u_info,
527 &wg_ptr);
528 /* cli == NULL is the master browser refused to talk or
529 could not be found */
530 if (!cli) {
531 continue;
534 workgroup = talloc_strdup(frame, wg_ptr);
535 server = talloc_strdup(frame, cli->desthost);
537 cli_shutdown(cli);
539 if (!workgroup || !server) {
540 errno = ENOMEM;
541 TALLOC_FREE(frame);
542 return NULL;
545 DEBUG(4, ("using workgroup %s %s\n",
546 workgroup, server));
549 * For each returned master browser IP address, get a
550 * connection to IPC$ on the server if we do not
551 * already have one, and determine the
552 * workgroups/domains that it knows about.
555 srv = SMBC_server(frame, context, True, server, "IPC$",
556 &workgroup, &user, &password);
557 if (!srv) {
558 continue;
561 dir->srv = srv;
562 dir->dir_type = SMBC_WORKGROUP;
564 /* Now, list the stuff ... */
566 if (!cli_NetServerEnum(srv->cli,
567 workgroup,
568 SV_TYPE_DOMAIN_ENUM,
569 list_unique_wg_fn,
570 (void *)dir)) {
571 continue;
575 SAFE_FREE(ip_list);
576 } else {
578 * Server not an empty string ... Check the rest and see what
579 * gives
581 if (*share == '\0') {
582 if (*path != '\0') {
584 /* Should not have empty share with path */
585 errno = EINVAL + 8197;
586 if (dir) {
587 SAFE_FREE(dir->fname);
588 SAFE_FREE(dir);
590 TALLOC_FREE(frame);
591 return NULL;
596 * We don't know if <server> is really a server name
597 * or is a workgroup/domain name. If we already have
598 * a server structure for it, we'll use it.
599 * Otherwise, check to see if <server><1D>,
600 * <server><1B>, or <server><20> translates. We check
601 * to see if <server> is an IP address first.
605 * See if we have an existing server. Do not
606 * establish a connection if one does not already
607 * exist.
609 srv = SMBC_server(frame, context, False,
610 server, "IPC$",
611 &workgroup, &user, &password);
614 * If no existing server and not an IP addr, look for
615 * LMB or DMB
617 if (!srv &&
618 !is_ipaddress(server) &&
619 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
620 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
622 * "server" is actually a workgroup name,
623 * not a server. Make this clear.
625 char *wgroup = server;
626 fstring buserver;
628 dir->dir_type = SMBC_SERVER;
631 * Get the backup list ...
633 if (!name_status_find(wgroup, 0, 0,
634 &rem_ss, buserver)) {
635 char addr[INET6_ADDRSTRLEN];
637 print_sockaddr(addr, sizeof(addr), &rem_ss);
638 DEBUG(0,("Could not get name of "
639 "local/domain master browser "
640 "for workgroup %s from "
641 "address %s\n",
642 wgroup,
643 addr));
644 if (dir) {
645 SAFE_FREE(dir->fname);
646 SAFE_FREE(dir);
648 errno = EPERM;
649 TALLOC_FREE(frame);
650 return NULL;
655 * Get a connection to IPC$ on the server if
656 * we do not already have one
658 srv = SMBC_server(frame, context, True,
659 buserver, "IPC$",
660 &workgroup,
661 &user, &password);
662 if (!srv) {
663 DEBUG(0, ("got no contact to IPC$\n"));
664 if (dir) {
665 SAFE_FREE(dir->fname);
666 SAFE_FREE(dir);
668 TALLOC_FREE(frame);
669 return NULL;
673 dir->srv = srv;
675 /* Now, list the servers ... */
676 if (!cli_NetServerEnum(srv->cli, wgroup,
677 0x0000FFFE, list_fn,
678 (void *)dir)) {
680 if (dir) {
681 SAFE_FREE(dir->fname);
682 SAFE_FREE(dir);
684 TALLOC_FREE(frame);
685 return NULL;
687 } else if (srv ||
688 (resolve_name(server, &rem_ss, 0x20, false))) {
691 * If we hadn't found the server, get one now
693 if (!srv) {
694 srv = SMBC_server(frame, context, True,
695 server, "IPC$",
696 &workgroup,
697 &user, &password);
700 if (!srv) {
701 if (dir) {
702 SAFE_FREE(dir->fname);
703 SAFE_FREE(dir);
705 TALLOC_FREE(frame);
706 return NULL;
710 dir->dir_type = SMBC_FILE_SHARE;
711 dir->srv = srv;
713 /* List the shares ... */
715 if (net_share_enum_rpc(
716 srv->cli,
717 list_fn,
718 (void *) dir) < 0 &&
719 cli_RNetShareEnum(
720 srv->cli,
721 list_fn,
722 (void *)dir) < 0) {
724 errno = cli_errno(srv->cli);
725 if (dir) {
726 SAFE_FREE(dir->fname);
727 SAFE_FREE(dir);
729 TALLOC_FREE(frame);
730 return NULL;
733 } else {
734 /* Neither the workgroup nor server exists */
735 errno = ECONNREFUSED;
736 if (dir) {
737 SAFE_FREE(dir->fname);
738 SAFE_FREE(dir);
740 TALLOC_FREE(frame);
741 return NULL;
745 else {
747 * The server and share are specified ... work from
748 * there ...
750 char *targetpath;
751 struct cli_state *targetcli;
752 NTSTATUS status;
754 /* We connect to the server and list the directory */
755 dir->dir_type = SMBC_FILE_SHARE;
757 srv = SMBC_server(frame, context, True, server, share,
758 &workgroup, &user, &password);
760 if (!srv) {
761 if (dir) {
762 SAFE_FREE(dir->fname);
763 SAFE_FREE(dir);
765 TALLOC_FREE(frame);
766 return NULL;
769 dir->srv = srv;
771 /* Now, list the files ... */
773 p = path + strlen(path);
774 path = talloc_asprintf_append(path, "\\*");
775 if (!path) {
776 if (dir) {
777 SAFE_FREE(dir->fname);
778 SAFE_FREE(dir);
780 TALLOC_FREE(frame);
781 return NULL;
784 if (!cli_resolve_path(frame, "", context->internal->auth_info,
785 srv->cli, path,
786 &targetcli, &targetpath)) {
787 d_printf("Could not resolve %s\n", path);
788 if (dir) {
789 SAFE_FREE(dir->fname);
790 SAFE_FREE(dir);
792 TALLOC_FREE(frame);
793 return NULL;
796 status = cli_list(targetcli, targetpath,
797 aDIR | aSYSTEM | aHIDDEN,
798 dir_list_fn, (void *)dir);
799 if (!NT_STATUS_IS_OK(status)) {
800 if (dir) {
801 SAFE_FREE(dir->fname);
802 SAFE_FREE(dir);
804 saved_errno = SMBC_errno(context, targetcli);
806 if (saved_errno == EINVAL) {
808 * See if they asked to opendir
809 * something other than a directory.
810 * If so, the converted error value we
811 * got would have been EINVAL rather
812 * than ENOTDIR.
814 *p = '\0'; /* restore original path */
816 if (SMBC_getatr(context, srv, path,
817 &mode, NULL,
818 NULL, NULL, NULL, NULL,
819 NULL) &&
820 ! IS_DOS_DIR(mode)) {
822 /* It is. Correct the error value */
823 saved_errno = ENOTDIR;
828 * If there was an error and the server is no
829 * good any more...
831 if (cli_is_error(targetcli) &&
832 smbc_getFunctionCheckServer(context)(context, srv)) {
834 /* ... then remove it. */
835 if (smbc_getFunctionRemoveUnusedServer(context)(context,
836 srv)) {
838 * We could not remove the
839 * server completely, remove
840 * it from the cache so we
841 * will not get it again. It
842 * will be removed when the
843 * last file/dir is closed.
845 smbc_getFunctionRemoveCachedServer(context)(context, srv);
849 errno = saved_errno;
850 TALLOC_FREE(frame);
851 return NULL;
857 DLIST_ADD(context->internal->files, dir);
858 TALLOC_FREE(frame);
859 return dir;
864 * Routine to close a directory
868 SMBC_closedir_ctx(SMBCCTX *context,
869 SMBCFILE *dir)
871 TALLOC_CTX *frame = talloc_stackframe();
873 if (!context || !context->internal->initialized) {
874 errno = EINVAL;
875 TALLOC_FREE(frame);
876 return -1;
879 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
880 errno = EBADF;
881 TALLOC_FREE(frame);
882 return -1;
885 remove_dir(dir); /* Clean it up */
887 DLIST_REMOVE(context->internal->files, dir);
889 if (dir) {
891 SAFE_FREE(dir->fname);
892 SAFE_FREE(dir); /* Free the space too */
895 TALLOC_FREE(frame);
896 return 0;
900 static void
901 smbc_readdir_internal(SMBCCTX * context,
902 struct smbc_dirent *dest,
903 struct smbc_dirent *src,
904 int max_namebuf_len)
906 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
908 /* url-encode the name. get back remaining buffer space */
909 max_namebuf_len =
910 smbc_urlencode(dest->name, src->name, max_namebuf_len);
912 /* We now know the name length */
913 dest->namelen = strlen(dest->name);
915 /* Save the pointer to the beginning of the comment */
916 dest->comment = dest->name + dest->namelen + 1;
918 /* Copy the comment */
919 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
920 dest->comment[max_namebuf_len - 1] = '\0';
922 /* Save other fields */
923 dest->smbc_type = src->smbc_type;
924 dest->commentlen = strlen(dest->comment);
925 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
926 (char *) dest);
927 } else {
929 /* No encoding. Just copy the entry as is. */
930 memcpy(dest, src, src->dirlen);
931 dest->comment = (char *)(&dest->name + src->namelen + 1);
937 * Routine to get a directory entry
940 struct smbc_dirent *
941 SMBC_readdir_ctx(SMBCCTX *context,
942 SMBCFILE *dir)
944 int maxlen;
945 struct smbc_dirent *dirp, *dirent;
946 TALLOC_CTX *frame = talloc_stackframe();
948 /* Check that all is ok first ... */
950 if (!context || !context->internal->initialized) {
952 errno = EINVAL;
953 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
954 TALLOC_FREE(frame);
955 return NULL;
959 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
961 errno = EBADF;
962 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
963 TALLOC_FREE(frame);
964 return NULL;
968 if (dir->file != False) { /* FIXME, should be dir, perhaps */
970 errno = ENOTDIR;
971 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
972 TALLOC_FREE(frame);
973 return NULL;
977 if (!dir->dir_next) {
978 TALLOC_FREE(frame);
979 return NULL;
982 dirent = dir->dir_next->dirent;
983 if (!dirent) {
985 errno = ENOENT;
986 TALLOC_FREE(frame);
987 return NULL;
991 dirp = &context->internal->dirent;
992 maxlen = sizeof(context->internal->_dirent_name);
994 smbc_readdir_internal(context, dirp, dirent, maxlen);
996 dir->dir_next = dir->dir_next->next;
998 TALLOC_FREE(frame);
999 return dirp;
1003 * Routine to get directory entries
1007 SMBC_getdents_ctx(SMBCCTX *context,
1008 SMBCFILE *dir,
1009 struct smbc_dirent *dirp,
1010 int count)
1012 int rem = count;
1013 int reqd;
1014 int maxlen;
1015 char *ndir = (char *)dirp;
1016 struct smbc_dir_list *dirlist;
1017 TALLOC_CTX *frame = talloc_stackframe();
1019 /* Check that all is ok first ... */
1021 if (!context || !context->internal->initialized) {
1023 errno = EINVAL;
1024 TALLOC_FREE(frame);
1025 return -1;
1029 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1031 errno = EBADF;
1032 TALLOC_FREE(frame);
1033 return -1;
1037 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1039 errno = ENOTDIR;
1040 TALLOC_FREE(frame);
1041 return -1;
1046 * Now, retrieve the number of entries that will fit in what was passed
1047 * We have to figure out if the info is in the list, or we need to
1048 * send a request to the server to get the info.
1051 while ((dirlist = dir->dir_next)) {
1052 struct smbc_dirent *dirent;
1054 if (!dirlist->dirent) {
1056 errno = ENOENT; /* Bad error */
1057 TALLOC_FREE(frame);
1058 return -1;
1062 /* Do urlencoding of next entry, if so selected */
1063 dirent = &context->internal->dirent;
1064 maxlen = sizeof(context->internal->_dirent_name);
1065 smbc_readdir_internal(context, dirent,
1066 dirlist->dirent, maxlen);
1068 reqd = dirent->dirlen;
1070 if (rem < reqd) {
1072 if (rem < count) { /* We managed to copy something */
1074 errno = 0;
1075 TALLOC_FREE(frame);
1076 return count - rem;
1079 else { /* Nothing copied ... */
1081 errno = EINVAL; /* Not enough space ... */
1082 TALLOC_FREE(frame);
1083 return -1;
1089 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1091 ((struct smbc_dirent *)ndir)->comment =
1092 (char *)(&((struct smbc_dirent *)ndir)->name +
1093 dirent->namelen +
1096 ndir += reqd;
1098 rem -= reqd;
1100 dir->dir_next = dirlist = dirlist -> next;
1103 TALLOC_FREE(frame);
1105 if (rem == count)
1106 return 0;
1107 else
1108 return count - rem;
1113 * Routine to create a directory ...
1117 SMBC_mkdir_ctx(SMBCCTX *context,
1118 const char *fname,
1119 mode_t mode)
1121 SMBCSRV *srv = NULL;
1122 char *server = NULL;
1123 char *share = NULL;
1124 char *user = NULL;
1125 char *password = NULL;
1126 char *workgroup = NULL;
1127 char *path = NULL;
1128 char *targetpath = NULL;
1129 struct cli_state *targetcli = NULL;
1130 TALLOC_CTX *frame = talloc_stackframe();
1132 if (!context || !context->internal->initialized) {
1133 errno = EINVAL;
1134 TALLOC_FREE(frame);
1135 return -1;
1138 if (!fname) {
1139 errno = EINVAL;
1140 TALLOC_FREE(frame);
1141 return -1;
1144 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1146 if (SMBC_parse_path(frame,
1147 context,
1148 fname,
1149 &workgroup,
1150 &server,
1151 &share,
1152 &path,
1153 &user,
1154 &password,
1155 NULL)) {
1156 errno = EINVAL;
1157 TALLOC_FREE(frame);
1158 return -1;
1161 if (!user || user[0] == (char)0) {
1162 user = talloc_strdup(frame, smbc_getUser(context));
1163 if (!user) {
1164 errno = ENOMEM;
1165 TALLOC_FREE(frame);
1166 return -1;
1170 srv = SMBC_server(frame, context, True,
1171 server, share, &workgroup, &user, &password);
1173 if (!srv) {
1175 TALLOC_FREE(frame);
1176 return -1; /* errno set by SMBC_server */
1180 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1181 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1182 srv->cli, path,
1183 &targetcli, &targetpath)) {
1184 d_printf("Could not resolve %s\n", path);
1185 errno = ENOENT;
1186 TALLOC_FREE(frame);
1187 return -1;
1189 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1191 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1192 errno = SMBC_errno(context, targetcli);
1193 TALLOC_FREE(frame);
1194 return -1;
1198 TALLOC_FREE(frame);
1199 return 0;
1204 * Our list function simply checks to see if a directory is not empty
1207 static void
1208 rmdir_list_fn(const char *mnt,
1209 struct file_info *finfo,
1210 const char *mask,
1211 void *state)
1213 if (strncmp(finfo->name, ".", 1) != 0 &&
1214 strncmp(finfo->name, "..", 2) != 0) {
1215 bool *smbc_rmdir_dirempty = (bool *)state;
1216 *smbc_rmdir_dirempty = false;
1221 * Routine to remove a directory
1225 SMBC_rmdir_ctx(SMBCCTX *context,
1226 const char *fname)
1228 SMBCSRV *srv = NULL;
1229 char *server = NULL;
1230 char *share = NULL;
1231 char *user = NULL;
1232 char *password = NULL;
1233 char *workgroup = NULL;
1234 char *path = NULL;
1235 char *targetpath = NULL;
1236 struct cli_state *targetcli = NULL;
1237 TALLOC_CTX *frame = talloc_stackframe();
1239 if (!context || !context->internal->initialized) {
1240 errno = EINVAL;
1241 TALLOC_FREE(frame);
1242 return -1;
1245 if (!fname) {
1246 errno = EINVAL;
1247 TALLOC_FREE(frame);
1248 return -1;
1251 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1253 if (SMBC_parse_path(frame,
1254 context,
1255 fname,
1256 &workgroup,
1257 &server,
1258 &share,
1259 &path,
1260 &user,
1261 &password,
1262 NULL)) {
1263 errno = EINVAL;
1264 TALLOC_FREE(frame);
1265 return -1;
1268 if (!user || user[0] == (char)0) {
1269 user = talloc_strdup(frame, smbc_getUser(context));
1270 if (!user) {
1271 errno = ENOMEM;
1272 TALLOC_FREE(frame);
1273 return -1;
1277 srv = SMBC_server(frame, context, True,
1278 server, share, &workgroup, &user, &password);
1280 if (!srv) {
1282 TALLOC_FREE(frame);
1283 return -1; /* errno set by SMBC_server */
1287 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1288 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1289 srv->cli, path,
1290 &targetcli, &targetpath)) {
1291 d_printf("Could not resolve %s\n", path);
1292 errno = ENOENT;
1293 TALLOC_FREE(frame);
1294 return -1;
1296 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1298 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1300 errno = SMBC_errno(context, targetcli);
1302 if (errno == EACCES) { /* Check if the dir empty or not */
1304 /* Local storage to avoid buffer overflows */
1305 char *lpath;
1306 bool smbc_rmdir_dirempty = true;
1307 NTSTATUS status;
1309 lpath = talloc_asprintf(frame, "%s\\*",
1310 targetpath);
1311 if (!lpath) {
1312 errno = ENOMEM;
1313 TALLOC_FREE(frame);
1314 return -1;
1317 status = cli_list(targetcli, lpath,
1318 aDIR | aSYSTEM | aHIDDEN,
1319 rmdir_list_fn,
1320 &smbc_rmdir_dirempty);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 /* Fix errno to ignore latest error ... */
1324 DEBUG(5, ("smbc_rmdir: "
1325 "cli_list returned an error: %d\n",
1326 SMBC_errno(context, targetcli)));
1327 errno = EACCES;
1331 if (smbc_rmdir_dirempty)
1332 errno = EACCES;
1333 else
1334 errno = ENOTEMPTY;
1338 TALLOC_FREE(frame);
1339 return -1;
1343 TALLOC_FREE(frame);
1344 return 0;
1349 * Routine to return the current directory position
1352 off_t
1353 SMBC_telldir_ctx(SMBCCTX *context,
1354 SMBCFILE *dir)
1356 TALLOC_CTX *frame = talloc_stackframe();
1358 if (!context || !context->internal->initialized) {
1360 errno = EINVAL;
1361 TALLOC_FREE(frame);
1362 return -1;
1366 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1368 errno = EBADF;
1369 TALLOC_FREE(frame);
1370 return -1;
1374 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1376 errno = ENOTDIR;
1377 TALLOC_FREE(frame);
1378 return -1;
1382 /* See if we're already at the end. */
1383 if (dir->dir_next == NULL) {
1384 /* We are. */
1385 TALLOC_FREE(frame);
1386 return -1;
1390 * We return the pointer here as the offset
1392 TALLOC_FREE(frame);
1393 return (off_t)(long)dir->dir_next->dirent;
1397 * A routine to run down the list and see if the entry is OK
1400 static struct smbc_dir_list *
1401 check_dir_ent(struct smbc_dir_list *list,
1402 struct smbc_dirent *dirent)
1405 /* Run down the list looking for what we want */
1407 if (dirent) {
1409 struct smbc_dir_list *tmp = list;
1411 while (tmp) {
1413 if (tmp->dirent == dirent)
1414 return tmp;
1416 tmp = tmp->next;
1422 return NULL; /* Not found, or an error */
1428 * Routine to seek on a directory
1432 SMBC_lseekdir_ctx(SMBCCTX *context,
1433 SMBCFILE *dir,
1434 off_t offset)
1436 long int l_offset = offset; /* Handle problems of size */
1437 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1438 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1439 TALLOC_CTX *frame = talloc_stackframe();
1441 if (!context || !context->internal->initialized) {
1443 errno = EINVAL;
1444 TALLOC_FREE(frame);
1445 return -1;
1449 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1451 errno = ENOTDIR;
1452 TALLOC_FREE(frame);
1453 return -1;
1457 /* Now, check what we were passed and see if it is OK ... */
1459 if (dirent == NULL) { /* Seek to the begining of the list */
1461 dir->dir_next = dir->dir_list;
1462 TALLOC_FREE(frame);
1463 return 0;
1467 if (offset == -1) { /* Seek to the end of the list */
1468 dir->dir_next = NULL;
1469 TALLOC_FREE(frame);
1470 return 0;
1473 /* Now, run down the list and make sure that the entry is OK */
1474 /* This may need to be changed if we change the format of the list */
1476 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1477 errno = EINVAL; /* Bad entry */
1478 TALLOC_FREE(frame);
1479 return -1;
1482 dir->dir_next = list_ent;
1484 TALLOC_FREE(frame);
1485 return 0;
1489 * Routine to fstat a dir
1493 SMBC_fstatdir_ctx(SMBCCTX *context,
1494 SMBCFILE *dir,
1495 struct stat *st)
1498 if (!context || !context->internal->initialized) {
1500 errno = EINVAL;
1501 return -1;
1504 /* No code yet ... */
1505 return 0;
1509 SMBC_chmod_ctx(SMBCCTX *context,
1510 const char *fname,
1511 mode_t newmode)
1513 SMBCSRV *srv = NULL;
1514 char *server = NULL;
1515 char *share = NULL;
1516 char *user = NULL;
1517 char *password = NULL;
1518 char *workgroup = NULL;
1519 char *targetpath = NULL;
1520 struct cli_state *targetcli = NULL;
1521 char *path = NULL;
1522 uint16 mode;
1523 TALLOC_CTX *frame = talloc_stackframe();
1525 if (!context || !context->internal->initialized) {
1527 errno = EINVAL; /* Best I can think of ... */
1528 TALLOC_FREE(frame);
1529 return -1;
1532 if (!fname) {
1533 errno = EINVAL;
1534 TALLOC_FREE(frame);
1535 return -1;
1538 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1540 if (SMBC_parse_path(frame,
1541 context,
1542 fname,
1543 &workgroup,
1544 &server,
1545 &share,
1546 &path,
1547 &user,
1548 &password,
1549 NULL)) {
1550 errno = EINVAL;
1551 TALLOC_FREE(frame);
1552 return -1;
1555 if (!user || user[0] == (char)0) {
1556 user = talloc_strdup(frame, smbc_getUser(context));
1557 if (!user) {
1558 errno = ENOMEM;
1559 TALLOC_FREE(frame);
1560 return -1;
1564 srv = SMBC_server(frame, context, True,
1565 server, share, &workgroup, &user, &password);
1567 if (!srv) {
1568 TALLOC_FREE(frame);
1569 return -1; /* errno set by SMBC_server */
1572 /*d_printf(">>>unlink: resolving %s\n", path);*/
1573 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1574 srv->cli, path,
1575 &targetcli, &targetpath)) {
1576 d_printf("Could not resolve %s\n", path);
1577 errno = ENOENT;
1578 TALLOC_FREE(frame);
1579 return -1;
1582 mode = 0;
1584 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1585 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1586 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1587 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1589 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1590 errno = SMBC_errno(context, targetcli);
1591 TALLOC_FREE(frame);
1592 return -1;
1595 TALLOC_FREE(frame);
1596 return 0;
1600 SMBC_utimes_ctx(SMBCCTX *context,
1601 const char *fname,
1602 struct timeval *tbuf)
1604 SMBCSRV *srv = NULL;
1605 char *server = NULL;
1606 char *share = NULL;
1607 char *user = NULL;
1608 char *password = NULL;
1609 char *workgroup = NULL;
1610 char *path = NULL;
1611 time_t access_time;
1612 time_t write_time;
1613 TALLOC_CTX *frame = talloc_stackframe();
1615 if (!context || !context->internal->initialized) {
1617 errno = EINVAL; /* Best I can think of ... */
1618 TALLOC_FREE(frame);
1619 return -1;
1622 if (!fname) {
1623 errno = EINVAL;
1624 TALLOC_FREE(frame);
1625 return -1;
1628 if (tbuf == NULL) {
1629 access_time = write_time = time(NULL);
1630 } else {
1631 access_time = tbuf[0].tv_sec;
1632 write_time = tbuf[1].tv_sec;
1635 if (DEBUGLVL(4)) {
1636 char *p;
1637 char atimebuf[32];
1638 char mtimebuf[32];
1640 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1641 atimebuf[sizeof(atimebuf) - 1] = '\0';
1642 if ((p = strchr(atimebuf, '\n')) != NULL) {
1643 *p = '\0';
1646 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1647 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1648 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1649 *p = '\0';
1652 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1653 fname, atimebuf, mtimebuf);
1656 if (SMBC_parse_path(frame,
1657 context,
1658 fname,
1659 &workgroup,
1660 &server,
1661 &share,
1662 &path,
1663 &user,
1664 &password,
1665 NULL)) {
1666 errno = EINVAL;
1667 TALLOC_FREE(frame);
1668 return -1;
1671 if (!user || user[0] == (char)0) {
1672 user = talloc_strdup(frame, smbc_getUser(context));
1673 if (!user) {
1674 errno = ENOMEM;
1675 TALLOC_FREE(frame);
1676 return -1;
1680 srv = SMBC_server(frame, context, True,
1681 server, share, &workgroup, &user, &password);
1683 if (!srv) {
1684 TALLOC_FREE(frame);
1685 return -1; /* errno set by SMBC_server */
1688 if (!SMBC_setatr(context, srv, path,
1689 0, access_time, write_time, 0, 0)) {
1690 TALLOC_FREE(frame);
1691 return -1; /* errno set by SMBC_setatr */
1694 TALLOC_FREE(frame);
1695 return 0;
1699 * Routine to unlink() a file
1703 SMBC_unlink_ctx(SMBCCTX *context,
1704 const char *fname)
1706 char *server = NULL;
1707 char *share = NULL;
1708 char *user = NULL;
1709 char *password = NULL;
1710 char *workgroup = NULL;
1711 char *path = NULL;
1712 char *targetpath = NULL;
1713 struct cli_state *targetcli = NULL;
1714 SMBCSRV *srv = NULL;
1715 TALLOC_CTX *frame = talloc_stackframe();
1717 if (!context || !context->internal->initialized) {
1719 errno = EINVAL; /* Best I can think of ... */
1720 TALLOC_FREE(frame);
1721 return -1;
1725 if (!fname) {
1726 errno = EINVAL;
1727 TALLOC_FREE(frame);
1728 return -1;
1732 if (SMBC_parse_path(frame,
1733 context,
1734 fname,
1735 &workgroup,
1736 &server,
1737 &share,
1738 &path,
1739 &user,
1740 &password,
1741 NULL)) {
1742 errno = EINVAL;
1743 TALLOC_FREE(frame);
1744 return -1;
1747 if (!user || user[0] == (char)0) {
1748 user = talloc_strdup(frame, smbc_getUser(context));
1749 if (!user) {
1750 errno = ENOMEM;
1751 TALLOC_FREE(frame);
1752 return -1;
1756 srv = SMBC_server(frame, context, True,
1757 server, share, &workgroup, &user, &password);
1759 if (!srv) {
1760 TALLOC_FREE(frame);
1761 return -1; /* SMBC_server sets errno */
1765 /*d_printf(">>>unlink: resolving %s\n", path);*/
1766 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1767 srv->cli, path,
1768 &targetcli, &targetpath)) {
1769 d_printf("Could not resolve %s\n", path);
1770 errno = ENOENT;
1771 TALLOC_FREE(frame);
1772 return -1;
1774 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1776 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, aSYSTEM | aHIDDEN))) {
1778 errno = SMBC_errno(context, targetcli);
1780 if (errno == EACCES) { /* Check if the file is a directory */
1782 int saverr = errno;
1783 SMB_OFF_T size = 0;
1784 uint16 mode = 0;
1785 struct timespec write_time_ts;
1786 struct timespec access_time_ts;
1787 struct timespec change_time_ts;
1788 SMB_INO_T ino = 0;
1790 if (!SMBC_getatr(context, srv, path, &mode, &size,
1791 NULL,
1792 &access_time_ts,
1793 &write_time_ts,
1794 &change_time_ts,
1795 &ino)) {
1797 /* Hmmm, bad error ... What? */
1799 errno = SMBC_errno(context, targetcli);
1800 TALLOC_FREE(frame);
1801 return -1;
1804 else {
1806 if (IS_DOS_DIR(mode))
1807 errno = EISDIR;
1808 else
1809 errno = saverr; /* Restore this */
1814 TALLOC_FREE(frame);
1815 return -1;
1819 TALLOC_FREE(frame);
1820 return 0; /* Success ... */
1825 * Routine to rename() a file
1829 SMBC_rename_ctx(SMBCCTX *ocontext,
1830 const char *oname,
1831 SMBCCTX *ncontext,
1832 const char *nname)
1834 char *server1 = NULL;
1835 char *share1 = NULL;
1836 char *server2 = NULL;
1837 char *share2 = NULL;
1838 char *user1 = NULL;
1839 char *user2 = NULL;
1840 char *password1 = NULL;
1841 char *password2 = NULL;
1842 char *workgroup = NULL;
1843 char *path1 = NULL;
1844 char *path2 = NULL;
1845 char *targetpath1 = NULL;
1846 char *targetpath2 = NULL;
1847 struct cli_state *targetcli1 = NULL;
1848 struct cli_state *targetcli2 = NULL;
1849 SMBCSRV *srv = NULL;
1850 TALLOC_CTX *frame = talloc_stackframe();
1852 if (!ocontext || !ncontext ||
1853 !ocontext->internal->initialized ||
1854 !ncontext->internal->initialized) {
1856 errno = EINVAL; /* Best I can think of ... */
1857 TALLOC_FREE(frame);
1858 return -1;
1861 if (!oname || !nname) {
1862 errno = EINVAL;
1863 TALLOC_FREE(frame);
1864 return -1;
1867 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1869 if (SMBC_parse_path(frame,
1870 ocontext,
1871 oname,
1872 &workgroup,
1873 &server1,
1874 &share1,
1875 &path1,
1876 &user1,
1877 &password1,
1878 NULL)) {
1879 errno = EINVAL;
1880 TALLOC_FREE(frame);
1881 return -1;
1884 if (!user1 || user1[0] == (char)0) {
1885 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1886 if (!user1) {
1887 errno = ENOMEM;
1888 TALLOC_FREE(frame);
1889 return -1;
1893 if (SMBC_parse_path(frame,
1894 ncontext,
1895 nname,
1896 NULL,
1897 &server2,
1898 &share2,
1899 &path2,
1900 &user2,
1901 &password2,
1902 NULL)) {
1903 errno = EINVAL;
1904 TALLOC_FREE(frame);
1905 return -1;
1908 if (!user2 || user2[0] == (char)0) {
1909 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1910 if (!user2) {
1911 errno = ENOMEM;
1912 TALLOC_FREE(frame);
1913 return -1;
1917 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1918 strcmp(user1, user2)) {
1919 /* Can't rename across file systems, or users?? */
1920 errno = EXDEV;
1921 TALLOC_FREE(frame);
1922 return -1;
1925 srv = SMBC_server(frame, ocontext, True,
1926 server1, share1, &workgroup, &user1, &password1);
1927 if (!srv) {
1928 TALLOC_FREE(frame);
1929 return -1;
1933 /* set the credentials to make DFS work */
1934 smbc_set_credentials_with_fallback(ocontext,
1935 workgroup,
1936 user1,
1937 password1);
1939 /*d_printf(">>>rename: resolving %s\n", path1);*/
1940 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1941 srv->cli,
1942 path1,
1943 &targetcli1, &targetpath1)) {
1944 d_printf("Could not resolve %s\n", path1);
1945 errno = ENOENT;
1946 TALLOC_FREE(frame);
1947 return -1;
1950 /* set the credentials to make DFS work */
1951 smbc_set_credentials_with_fallback(ncontext,
1952 workgroup,
1953 user2,
1954 password2);
1956 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1957 /*d_printf(">>>rename: resolving %s\n", path2);*/
1958 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1959 srv->cli,
1960 path2,
1961 &targetcli2, &targetpath2)) {
1962 d_printf("Could not resolve %s\n", path2);
1963 errno = ENOENT;
1964 TALLOC_FREE(frame);
1965 return -1;
1967 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1969 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1970 strcmp(targetcli1->share, targetcli2->share))
1972 /* can't rename across file systems */
1973 errno = EXDEV;
1974 TALLOC_FREE(frame);
1975 return -1;
1978 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
1979 int eno = SMBC_errno(ocontext, targetcli1);
1981 if (eno != EEXIST ||
1982 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, aSYSTEM | aHIDDEN)) ||
1983 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
1985 errno = eno;
1986 TALLOC_FREE(frame);
1987 return -1;
1992 TALLOC_FREE(frame);
1993 return 0; /* Success */