s3: Fix some errno assignments in SMBC_opendir_ctx
[Samba.git] / source3 / libsmb / libsmb_dir.c
blobd46f72a763e44eafd82a085af426f37d9b6737fc
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 "rpc_client/cli_pipe.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
33 * Routine to open a directory
34 * We accept the URL syntax explained in SMBC_parse_path(), above.
37 static void
38 remove_dir(SMBCFILE *dir)
40 struct smbc_dir_list *d,*f;
42 d = dir->dir_list;
43 while (d) {
45 f = d; d = d->next;
47 SAFE_FREE(f->dirent);
48 SAFE_FREE(f);
52 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
56 static int
57 add_dirent(SMBCFILE *dir,
58 const char *name,
59 const char *comment,
60 uint32 type)
62 struct smbc_dirent *dirent;
63 int size;
64 int name_length = (name == NULL ? 0 : strlen(name));
65 int comment_len = (comment == NULL ? 0 : strlen(comment));
68 * Allocate space for the dirent, which must be increased by the
69 * size of the name and the comment and 1 each for the null terminator.
72 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
74 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
76 if (!dirent) {
78 dir->dir_error = ENOMEM;
79 return -1;
83 ZERO_STRUCTP(dirent);
85 if (dir->dir_list == NULL) {
87 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
88 if (!dir->dir_list) {
90 SAFE_FREE(dirent);
91 dir->dir_error = ENOMEM;
92 return -1;
95 ZERO_STRUCTP(dir->dir_list);
97 dir->dir_end = dir->dir_next = dir->dir_list;
99 else {
101 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
103 if (!dir->dir_end->next) {
105 SAFE_FREE(dirent);
106 dir->dir_error = ENOMEM;
107 return -1;
110 ZERO_STRUCTP(dir->dir_end->next);
112 dir->dir_end = dir->dir_end->next;
115 dir->dir_end->next = NULL;
116 dir->dir_end->dirent = dirent;
118 dirent->smbc_type = type;
119 dirent->namelen = name_length;
120 dirent->commentlen = comment_len;
121 dirent->dirlen = size;
124 * dirent->namelen + 1 includes the null (no null termination needed)
125 * Ditto for dirent->commentlen.
126 * The space for the two null bytes was allocated.
128 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
129 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
130 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
132 return 0;
136 static void
137 list_unique_wg_fn(const char *name,
138 uint32 type,
139 const char *comment,
140 void *state)
142 SMBCFILE *dir = (SMBCFILE *)state;
143 struct smbc_dir_list *dir_list;
144 struct smbc_dirent *dirent;
145 int dirent_type;
146 int do_remove = 0;
148 dirent_type = dir->dir_type;
150 if (add_dirent(dir, name, comment, dirent_type) < 0) {
151 /* An error occurred, what do we do? */
152 /* FIXME: Add some code here */
153 /* Change cli_NetServerEnum to take a fn
154 returning NTSTATUS... JRA. */
157 /* Point to the one just added */
158 dirent = dir->dir_end->dirent;
160 /* See if this was a duplicate */
161 for (dir_list = dir->dir_list;
162 dir_list != dir->dir_end;
163 dir_list = dir_list->next) {
164 if (! do_remove &&
165 strcmp(dir_list->dirent->name, dirent->name) == 0) {
166 /* Duplicate. End end of list need to be removed. */
167 do_remove = 1;
170 if (do_remove && dir_list->next == dir->dir_end) {
171 /* Found the end of the list. Remove it. */
172 dir->dir_end = dir_list;
173 free(dir_list->next);
174 free(dirent);
175 dir_list->next = NULL;
176 break;
181 static void
182 list_fn(const char *name,
183 uint32 type,
184 const char *comment,
185 void *state)
187 SMBCFILE *dir = (SMBCFILE *)state;
188 int dirent_type;
191 * We need to process the type a little ...
193 * Disk share = 0x00000000
194 * Print share = 0x00000001
195 * Comms share = 0x00000002 (obsolete?)
196 * IPC$ share = 0x00000003
198 * administrative shares:
199 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
202 if (dir->dir_type == SMBC_FILE_SHARE) {
203 switch (type) {
204 case 0 | 0x80000000:
205 case 0:
206 dirent_type = SMBC_FILE_SHARE;
207 break;
209 case 1:
210 dirent_type = SMBC_PRINTER_SHARE;
211 break;
213 case 2:
214 dirent_type = SMBC_COMMS_SHARE;
215 break;
217 case 3 | 0x80000000:
218 case 3:
219 dirent_type = SMBC_IPC_SHARE;
220 break;
222 default:
223 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
224 break;
227 else {
228 dirent_type = dir->dir_type;
231 if (add_dirent(dir, name, comment, dirent_type) < 0) {
232 /* An error occurred, what do we do? */
233 /* FIXME: Add some code here */
234 /* Change cli_NetServerEnum to take a fn
235 returning NTSTATUS... JRA. */
239 static NTSTATUS
240 dir_list_fn(const char *mnt,
241 struct file_info *finfo,
242 const char *mask,
243 void *state)
246 if (add_dirent((SMBCFILE *)state, finfo->name, "",
247 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
248 SMBCFILE *dir = (SMBCFILE *)state;
249 return map_nt_error_from_unix(dir->dir_error);
251 return NT_STATUS_OK;
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 = NULL;
271 NTSTATUS nt_status;
272 uint32_t resume_handle = 0;
273 uint32_t total_entries = 0;
274 struct dcerpc_binding_handle *b;
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 b = pipe_hnd->binding_handle;
292 /* Issue the NetShareEnum RPC call and retrieve the response */
293 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
294 pipe_hnd->desthost,
295 &info_ctr,
296 preferred_len,
297 &total_entries,
298 &resume_handle,
299 &result);
301 /* Was it successful? */
302 if (!NT_STATUS_IS_OK(nt_status)) {
303 /* Nope. Go clean up. */
304 result = ntstatus_to_werror(nt_status);
305 goto done;
308 if (!W_ERROR_IS_OK(result)) {
309 /* Nope. Go clean up. */
310 goto done;
313 if (total_entries == 0) {
314 /* Nope. Go clean up. */
315 result = WERR_GENERAL_FAILURE;
316 goto done;
319 /* For each returned entry... */
320 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
322 /* pull out the share name */
323 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
325 /* pull out the share's comment */
326 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
328 /* Get the type value */
329 type = info_ctr.ctr.ctr1->array[i].type;
331 /* Add this share to the list */
332 (*fn)(name, type, comment, state);
335 done:
336 /* Close the server service pipe */
337 TALLOC_FREE(pipe_hnd);
339 /* Tell 'em if it worked */
340 return W_ERROR_IS_OK(result) ? 0 : -1;
345 * Verify that the options specified in a URL are valid
348 SMBC_check_options(char *server,
349 char *share,
350 char *path,
351 char *options)
353 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
354 "path='%s' options='%s'\n",
355 server, share, path, options));
357 /* No options at all is always ok */
358 if (! *options) return 0;
360 /* Currently, we don't support any options. */
361 return -1;
365 SMBCFILE *
366 SMBC_opendir_ctx(SMBCCTX *context,
367 const char *fname)
369 int saved_errno;
370 char *server = NULL;
371 char *share = NULL;
372 char *user = NULL;
373 char *password = NULL;
374 char *options = NULL;
375 char *workgroup = NULL;
376 char *path = NULL;
377 uint16 mode;
378 char *p = NULL;
379 SMBCSRV *srv = NULL;
380 SMBCFILE *dir = NULL;
381 struct sockaddr_storage rem_ss;
382 TALLOC_CTX *frame = talloc_stackframe();
384 if (!context || !context->internal->initialized) {
385 DEBUG(4, ("no valid context\n"));
386 TALLOC_FREE(frame);
387 errno = EINVAL + 8192;
388 return NULL;
392 if (!fname) {
393 DEBUG(4, ("no valid fname\n"));
394 TALLOC_FREE(frame);
395 errno = EINVAL + 8193;
396 return NULL;
399 if (SMBC_parse_path(frame,
400 context,
401 fname,
402 &workgroup,
403 &server,
404 &share,
405 &path,
406 &user,
407 &password,
408 &options)) {
409 DEBUG(4, ("no valid path\n"));
410 TALLOC_FREE(frame);
411 errno = EINVAL + 8194;
412 return NULL;
415 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
416 "path='%s' options='%s'\n",
417 fname, server, share, path, options));
419 /* Ensure the options are valid */
420 if (SMBC_check_options(server, share, path, options)) {
421 DEBUG(4, ("unacceptable options (%s)\n", options));
422 TALLOC_FREE(frame);
423 errno = EINVAL + 8195;
424 return NULL;
427 if (!user || user[0] == (char)0) {
428 user = talloc_strdup(frame, smbc_getUser(context));
429 if (!user) {
430 TALLOC_FREE(frame);
431 errno = ENOMEM;
432 return NULL;
436 dir = SMB_MALLOC_P(SMBCFILE);
438 if (!dir) {
439 TALLOC_FREE(frame);
440 errno = ENOMEM;
441 return NULL;
444 ZERO_STRUCTP(dir);
446 dir->cli_fd = 0;
447 dir->fname = SMB_STRDUP(fname);
448 dir->srv = NULL;
449 dir->offset = 0;
450 dir->file = False;
451 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
453 if (server[0] == (char)0) {
455 int i;
456 int count;
457 int max_lmb_count;
458 struct ip_service *ip_list;
459 struct ip_service server_addr;
460 struct user_auth_info u_info;
462 if (share[0] != (char)0 || path[0] != (char)0) {
464 if (dir) {
465 SAFE_FREE(dir->fname);
466 SAFE_FREE(dir);
468 TALLOC_FREE(frame);
469 errno = EINVAL + 8196;
470 return NULL;
473 /* Determine how many local master browsers to query */
474 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
475 ? INT_MAX
476 : smbc_getOptionBrowseMaxLmbCount(context));
478 memset(&u_info, '\0', sizeof(u_info));
479 u_info.username = talloc_strdup(frame,user);
480 u_info.password = talloc_strdup(frame,password);
481 if (!u_info.username || !u_info.password) {
482 if (dir) {
483 SAFE_FREE(dir->fname);
484 SAFE_FREE(dir);
486 TALLOC_FREE(frame);
487 return NULL;
491 * We have server and share and path empty but options
492 * requesting that we scan all master browsers for their list
493 * of workgroups/domains. This implies that we must first try
494 * broadcast queries to find all master browsers, and if that
495 * doesn't work, then try our other methods which return only
496 * a single master browser.
499 ip_list = NULL;
500 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
501 &count)))
504 SAFE_FREE(ip_list);
506 if (!find_master_ip(workgroup, &server_addr.ss)) {
508 if (dir) {
509 SAFE_FREE(dir->fname);
510 SAFE_FREE(dir);
512 TALLOC_FREE(frame);
513 errno = ENOENT;
514 return NULL;
517 ip_list = (struct ip_service *)memdup(
518 &server_addr, sizeof(server_addr));
519 if (ip_list == NULL) {
520 TALLOC_FREE(frame);
521 errno = ENOMEM;
522 return NULL;
524 count = 1;
527 for (i = 0; i < count && i < max_lmb_count; i++) {
528 char addr[INET6_ADDRSTRLEN];
529 char *wg_ptr = NULL;
530 struct cli_state *cli = NULL;
532 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
533 DEBUG(99, ("Found master browser %d of %d: %s\n",
534 i+1, MAX(count, max_lmb_count),
535 addr));
537 cli = get_ipc_connect_master_ip(talloc_tos(),
538 &ip_list[i],
539 &u_info,
540 &wg_ptr);
541 /* cli == NULL is the master browser refused to talk or
542 could not be found */
543 if (!cli) {
544 continue;
547 workgroup = talloc_strdup(frame, wg_ptr);
548 server = talloc_strdup(frame, cli->desthost);
550 cli_shutdown(cli);
552 if (!workgroup || !server) {
553 if (dir) {
554 SAFE_FREE(dir->fname);
555 SAFE_FREE(dir);
557 TALLOC_FREE(frame);
558 errno = ENOMEM;
559 return NULL;
562 DEBUG(4, ("using workgroup %s %s\n",
563 workgroup, server));
566 * For each returned master browser IP address, get a
567 * connection to IPC$ on the server if we do not
568 * already have one, and determine the
569 * workgroups/domains that it knows about.
572 srv = SMBC_server(frame, context, True, server, "IPC$",
573 &workgroup, &user, &password);
574 if (!srv) {
575 continue;
578 dir->srv = srv;
579 dir->dir_type = SMBC_WORKGROUP;
581 /* Now, list the stuff ... */
583 if (!cli_NetServerEnum(srv->cli,
584 workgroup,
585 SV_TYPE_DOMAIN_ENUM,
586 list_unique_wg_fn,
587 (void *)dir)) {
588 continue;
592 SAFE_FREE(ip_list);
593 } else {
595 * Server not an empty string ... Check the rest and see what
596 * gives
598 if (*share == '\0') {
599 if (*path != '\0') {
601 /* Should not have empty share with path */
602 if (dir) {
603 SAFE_FREE(dir->fname);
604 SAFE_FREE(dir);
606 TALLOC_FREE(frame);
607 errno = EINVAL + 8197;
608 return NULL;
613 * We don't know if <server> is really a server name
614 * or is a workgroup/domain name. If we already have
615 * a server structure for it, we'll use it.
616 * Otherwise, check to see if <server><1D>,
617 * <server><1B>, or <server><20> translates. We check
618 * to see if <server> is an IP address first.
622 * See if we have an existing server. Do not
623 * establish a connection if one does not already
624 * exist.
626 srv = SMBC_server(frame, context, False,
627 server, "IPC$",
628 &workgroup, &user, &password);
631 * If no existing server and not an IP addr, look for
632 * LMB or DMB
634 if (!srv &&
635 !is_ipaddress(server) &&
636 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
637 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
639 * "server" is actually a workgroup name,
640 * not a server. Make this clear.
642 char *wgroup = server;
643 fstring buserver;
645 dir->dir_type = SMBC_SERVER;
648 * Get the backup list ...
650 if (!name_status_find(wgroup, 0, 0,
651 &rem_ss, buserver)) {
652 char addr[INET6_ADDRSTRLEN];
654 print_sockaddr(addr, sizeof(addr), &rem_ss);
655 DEBUG(0,("Could not get name of "
656 "local/domain master browser "
657 "for workgroup %s from "
658 "address %s\n",
659 wgroup,
660 addr));
661 if (dir) {
662 SAFE_FREE(dir->fname);
663 SAFE_FREE(dir);
665 TALLOC_FREE(frame);
666 errno = EPERM;
667 return NULL;
672 * Get a connection to IPC$ on the server if
673 * we do not already have one
675 srv = SMBC_server(frame, context, True,
676 buserver, "IPC$",
677 &workgroup,
678 &user, &password);
679 if (!srv) {
680 DEBUG(0, ("got no contact to IPC$\n"));
681 if (dir) {
682 SAFE_FREE(dir->fname);
683 SAFE_FREE(dir);
685 TALLOC_FREE(frame);
686 return NULL;
690 dir->srv = srv;
692 /* Now, list the servers ... */
693 if (!cli_NetServerEnum(srv->cli, wgroup,
694 0x0000FFFE, list_fn,
695 (void *)dir)) {
697 if (dir) {
698 SAFE_FREE(dir->fname);
699 SAFE_FREE(dir);
701 TALLOC_FREE(frame);
702 return NULL;
704 } else if (srv ||
705 (resolve_name(server, &rem_ss, 0x20, false))) {
708 * If we hadn't found the server, get one now
710 if (!srv) {
711 srv = SMBC_server(frame, context, True,
712 server, "IPC$",
713 &workgroup,
714 &user, &password);
717 if (!srv) {
718 if (dir) {
719 SAFE_FREE(dir->fname);
720 SAFE_FREE(dir);
722 TALLOC_FREE(frame);
723 return NULL;
727 dir->dir_type = SMBC_FILE_SHARE;
728 dir->srv = srv;
730 /* List the shares ... */
732 if (net_share_enum_rpc(
733 srv->cli,
734 list_fn,
735 (void *) dir) < 0 &&
736 cli_RNetShareEnum(
737 srv->cli,
738 list_fn,
739 (void *)dir) < 0) {
741 errno = cli_errno(srv->cli);
742 if (dir) {
743 SAFE_FREE(dir->fname);
744 SAFE_FREE(dir);
746 TALLOC_FREE(frame);
747 return NULL;
750 } else {
751 /* Neither the workgroup nor server exists */
752 errno = ECONNREFUSED;
753 if (dir) {
754 SAFE_FREE(dir->fname);
755 SAFE_FREE(dir);
757 TALLOC_FREE(frame);
758 return NULL;
762 else {
764 * The server and share are specified ... work from
765 * there ...
767 char *targetpath;
768 struct cli_state *targetcli;
769 NTSTATUS status;
771 /* We connect to the server and list the directory */
772 dir->dir_type = SMBC_FILE_SHARE;
774 srv = SMBC_server(frame, context, True, server, share,
775 &workgroup, &user, &password);
777 if (!srv) {
778 if (dir) {
779 SAFE_FREE(dir->fname);
780 SAFE_FREE(dir);
782 TALLOC_FREE(frame);
783 return NULL;
786 dir->srv = srv;
788 /* Now, list the files ... */
790 p = path + strlen(path);
791 path = talloc_asprintf_append(path, "\\*");
792 if (!path) {
793 if (dir) {
794 SAFE_FREE(dir->fname);
795 SAFE_FREE(dir);
797 TALLOC_FREE(frame);
798 return NULL;
801 if (!cli_resolve_path(frame, "", context->internal->auth_info,
802 srv->cli, path,
803 &targetcli, &targetpath)) {
804 d_printf("Could not resolve %s\n", path);
805 if (dir) {
806 SAFE_FREE(dir->fname);
807 SAFE_FREE(dir);
809 TALLOC_FREE(frame);
810 return NULL;
813 status = cli_list(targetcli, targetpath,
814 aDIR | aSYSTEM | aHIDDEN,
815 dir_list_fn, (void *)dir);
816 if (!NT_STATUS_IS_OK(status)) {
817 if (dir) {
818 SAFE_FREE(dir->fname);
819 SAFE_FREE(dir);
821 saved_errno = SMBC_errno(context, targetcli);
823 if (saved_errno == EINVAL) {
825 * See if they asked to opendir
826 * something other than a directory.
827 * If so, the converted error value we
828 * got would have been EINVAL rather
829 * than ENOTDIR.
831 *p = '\0'; /* restore original path */
833 if (SMBC_getatr(context, srv, path,
834 &mode, NULL,
835 NULL, NULL, NULL, NULL,
836 NULL) &&
837 ! IS_DOS_DIR(mode)) {
839 /* It is. Correct the error value */
840 saved_errno = ENOTDIR;
845 * If there was an error and the server is no
846 * good any more...
848 if (cli_is_error(targetcli) &&
849 smbc_getFunctionCheckServer(context)(context, srv)) {
851 /* ... then remove it. */
852 if (smbc_getFunctionRemoveUnusedServer(context)(context,
853 srv)) {
855 * We could not remove the
856 * server completely, remove
857 * it from the cache so we
858 * will not get it again. It
859 * will be removed when the
860 * last file/dir is closed.
862 smbc_getFunctionRemoveCachedServer(context)(context, srv);
866 TALLOC_FREE(frame);
867 errno = saved_errno;
868 return NULL;
874 DLIST_ADD(context->internal->files, dir);
875 TALLOC_FREE(frame);
876 return dir;
881 * Routine to close a directory
885 SMBC_closedir_ctx(SMBCCTX *context,
886 SMBCFILE *dir)
888 TALLOC_CTX *frame = talloc_stackframe();
890 if (!context || !context->internal->initialized) {
891 errno = EINVAL;
892 TALLOC_FREE(frame);
893 return -1;
896 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
897 errno = EBADF;
898 TALLOC_FREE(frame);
899 return -1;
902 remove_dir(dir); /* Clean it up */
904 DLIST_REMOVE(context->internal->files, dir);
906 if (dir) {
908 SAFE_FREE(dir->fname);
909 SAFE_FREE(dir); /* Free the space too */
912 TALLOC_FREE(frame);
913 return 0;
917 static void
918 smbc_readdir_internal(SMBCCTX * context,
919 struct smbc_dirent *dest,
920 struct smbc_dirent *src,
921 int max_namebuf_len)
923 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
925 /* url-encode the name. get back remaining buffer space */
926 max_namebuf_len =
927 smbc_urlencode(dest->name, src->name, max_namebuf_len);
929 /* We now know the name length */
930 dest->namelen = strlen(dest->name);
932 /* Save the pointer to the beginning of the comment */
933 dest->comment = dest->name + dest->namelen + 1;
935 /* Copy the comment */
936 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
937 dest->comment[max_namebuf_len - 1] = '\0';
939 /* Save other fields */
940 dest->smbc_type = src->smbc_type;
941 dest->commentlen = strlen(dest->comment);
942 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
943 (char *) dest);
944 } else {
946 /* No encoding. Just copy the entry as is. */
947 memcpy(dest, src, src->dirlen);
948 dest->comment = (char *)(&dest->name + src->namelen + 1);
954 * Routine to get a directory entry
957 struct smbc_dirent *
958 SMBC_readdir_ctx(SMBCCTX *context,
959 SMBCFILE *dir)
961 int maxlen;
962 struct smbc_dirent *dirp, *dirent;
963 TALLOC_CTX *frame = talloc_stackframe();
965 /* Check that all is ok first ... */
967 if (!context || !context->internal->initialized) {
969 errno = EINVAL;
970 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
971 TALLOC_FREE(frame);
972 return NULL;
976 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
978 errno = EBADF;
979 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
980 TALLOC_FREE(frame);
981 return NULL;
985 if (dir->file != False) { /* FIXME, should be dir, perhaps */
987 errno = ENOTDIR;
988 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
989 TALLOC_FREE(frame);
990 return NULL;
994 if (!dir->dir_next) {
995 TALLOC_FREE(frame);
996 return NULL;
999 dirent = dir->dir_next->dirent;
1000 if (!dirent) {
1002 errno = ENOENT;
1003 TALLOC_FREE(frame);
1004 return NULL;
1008 dirp = &context->internal->dirent;
1009 maxlen = sizeof(context->internal->_dirent_name);
1011 smbc_readdir_internal(context, dirp, dirent, maxlen);
1013 dir->dir_next = dir->dir_next->next;
1015 TALLOC_FREE(frame);
1016 return dirp;
1020 * Routine to get directory entries
1024 SMBC_getdents_ctx(SMBCCTX *context,
1025 SMBCFILE *dir,
1026 struct smbc_dirent *dirp,
1027 int count)
1029 int rem = count;
1030 int reqd;
1031 int maxlen;
1032 char *ndir = (char *)dirp;
1033 struct smbc_dir_list *dirlist;
1034 TALLOC_CTX *frame = talloc_stackframe();
1036 /* Check that all is ok first ... */
1038 if (!context || !context->internal->initialized) {
1040 errno = EINVAL;
1041 TALLOC_FREE(frame);
1042 return -1;
1046 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1048 errno = EBADF;
1049 TALLOC_FREE(frame);
1050 return -1;
1054 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1056 errno = ENOTDIR;
1057 TALLOC_FREE(frame);
1058 return -1;
1063 * Now, retrieve the number of entries that will fit in what was passed
1064 * We have to figure out if the info is in the list, or we need to
1065 * send a request to the server to get the info.
1068 while ((dirlist = dir->dir_next)) {
1069 struct smbc_dirent *dirent;
1070 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1072 if (!dirlist->dirent) {
1074 errno = ENOENT; /* Bad error */
1075 TALLOC_FREE(frame);
1076 return -1;
1080 /* Do urlencoding of next entry, if so selected */
1081 dirent = &context->internal->dirent;
1082 maxlen = sizeof(context->internal->_dirent_name);
1083 smbc_readdir_internal(context, dirent,
1084 dirlist->dirent, maxlen);
1086 reqd = dirent->dirlen;
1088 if (rem < reqd) {
1090 if (rem < count) { /* We managed to copy something */
1092 errno = 0;
1093 TALLOC_FREE(frame);
1094 return count - rem;
1097 else { /* Nothing copied ... */
1099 errno = EINVAL; /* Not enough space ... */
1100 TALLOC_FREE(frame);
1101 return -1;
1107 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1109 currentEntry->comment = &currentEntry->name[0] +
1110 dirent->namelen + 1;
1112 ndir += reqd;
1113 rem -= reqd;
1115 /* Try and align the struct for the next entry
1116 on a valid pointer boundary by appending zeros */
1117 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1118 *ndir = '\0';
1119 rem--;
1120 ndir++;
1121 currentEntry->dirlen++;
1124 dir->dir_next = dirlist = dirlist -> next;
1127 TALLOC_FREE(frame);
1129 if (rem == count)
1130 return 0;
1131 else
1132 return count - rem;
1137 * Routine to create a directory ...
1141 SMBC_mkdir_ctx(SMBCCTX *context,
1142 const char *fname,
1143 mode_t mode)
1145 SMBCSRV *srv = NULL;
1146 char *server = NULL;
1147 char *share = NULL;
1148 char *user = NULL;
1149 char *password = NULL;
1150 char *workgroup = NULL;
1151 char *path = NULL;
1152 char *targetpath = NULL;
1153 struct cli_state *targetcli = NULL;
1154 TALLOC_CTX *frame = talloc_stackframe();
1156 if (!context || !context->internal->initialized) {
1157 errno = EINVAL;
1158 TALLOC_FREE(frame);
1159 return -1;
1162 if (!fname) {
1163 errno = EINVAL;
1164 TALLOC_FREE(frame);
1165 return -1;
1168 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1170 if (SMBC_parse_path(frame,
1171 context,
1172 fname,
1173 &workgroup,
1174 &server,
1175 &share,
1176 &path,
1177 &user,
1178 &password,
1179 NULL)) {
1180 errno = EINVAL;
1181 TALLOC_FREE(frame);
1182 return -1;
1185 if (!user || user[0] == (char)0) {
1186 user = talloc_strdup(frame, smbc_getUser(context));
1187 if (!user) {
1188 errno = ENOMEM;
1189 TALLOC_FREE(frame);
1190 return -1;
1194 srv = SMBC_server(frame, context, True,
1195 server, share, &workgroup, &user, &password);
1197 if (!srv) {
1199 TALLOC_FREE(frame);
1200 return -1; /* errno set by SMBC_server */
1204 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1205 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1206 srv->cli, path,
1207 &targetcli, &targetpath)) {
1208 d_printf("Could not resolve %s\n", path);
1209 errno = ENOENT;
1210 TALLOC_FREE(frame);
1211 return -1;
1213 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1215 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1216 errno = SMBC_errno(context, targetcli);
1217 TALLOC_FREE(frame);
1218 return -1;
1222 TALLOC_FREE(frame);
1223 return 0;
1228 * Our list function simply checks to see if a directory is not empty
1231 static NTSTATUS
1232 rmdir_list_fn(const char *mnt,
1233 struct file_info *finfo,
1234 const char *mask,
1235 void *state)
1237 if (strncmp(finfo->name, ".", 1) != 0 &&
1238 strncmp(finfo->name, "..", 2) != 0) {
1239 bool *smbc_rmdir_dirempty = (bool *)state;
1240 *smbc_rmdir_dirempty = false;
1242 return NT_STATUS_OK;
1246 * Routine to remove a directory
1250 SMBC_rmdir_ctx(SMBCCTX *context,
1251 const char *fname)
1253 SMBCSRV *srv = NULL;
1254 char *server = NULL;
1255 char *share = NULL;
1256 char *user = NULL;
1257 char *password = NULL;
1258 char *workgroup = NULL;
1259 char *path = NULL;
1260 char *targetpath = NULL;
1261 struct cli_state *targetcli = NULL;
1262 TALLOC_CTX *frame = talloc_stackframe();
1264 if (!context || !context->internal->initialized) {
1265 errno = EINVAL;
1266 TALLOC_FREE(frame);
1267 return -1;
1270 if (!fname) {
1271 errno = EINVAL;
1272 TALLOC_FREE(frame);
1273 return -1;
1276 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1278 if (SMBC_parse_path(frame,
1279 context,
1280 fname,
1281 &workgroup,
1282 &server,
1283 &share,
1284 &path,
1285 &user,
1286 &password,
1287 NULL)) {
1288 errno = EINVAL;
1289 TALLOC_FREE(frame);
1290 return -1;
1293 if (!user || user[0] == (char)0) {
1294 user = talloc_strdup(frame, smbc_getUser(context));
1295 if (!user) {
1296 errno = ENOMEM;
1297 TALLOC_FREE(frame);
1298 return -1;
1302 srv = SMBC_server(frame, context, True,
1303 server, share, &workgroup, &user, &password);
1305 if (!srv) {
1307 TALLOC_FREE(frame);
1308 return -1; /* errno set by SMBC_server */
1312 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1313 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1314 srv->cli, path,
1315 &targetcli, &targetpath)) {
1316 d_printf("Could not resolve %s\n", path);
1317 errno = ENOENT;
1318 TALLOC_FREE(frame);
1319 return -1;
1321 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1323 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1325 errno = SMBC_errno(context, targetcli);
1327 if (errno == EACCES) { /* Check if the dir empty or not */
1329 /* Local storage to avoid buffer overflows */
1330 char *lpath;
1331 bool smbc_rmdir_dirempty = true;
1332 NTSTATUS status;
1334 lpath = talloc_asprintf(frame, "%s\\*",
1335 targetpath);
1336 if (!lpath) {
1337 errno = ENOMEM;
1338 TALLOC_FREE(frame);
1339 return -1;
1342 status = cli_list(targetcli, lpath,
1343 aDIR | aSYSTEM | aHIDDEN,
1344 rmdir_list_fn,
1345 &smbc_rmdir_dirempty);
1347 if (!NT_STATUS_IS_OK(status)) {
1348 /* Fix errno to ignore latest error ... */
1349 DEBUG(5, ("smbc_rmdir: "
1350 "cli_list returned an error: %d\n",
1351 SMBC_errno(context, targetcli)));
1352 errno = EACCES;
1356 if (smbc_rmdir_dirempty)
1357 errno = EACCES;
1358 else
1359 errno = ENOTEMPTY;
1363 TALLOC_FREE(frame);
1364 return -1;
1368 TALLOC_FREE(frame);
1369 return 0;
1374 * Routine to return the current directory position
1377 off_t
1378 SMBC_telldir_ctx(SMBCCTX *context,
1379 SMBCFILE *dir)
1381 TALLOC_CTX *frame = talloc_stackframe();
1383 if (!context || !context->internal->initialized) {
1385 errno = EINVAL;
1386 TALLOC_FREE(frame);
1387 return -1;
1391 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1393 errno = EBADF;
1394 TALLOC_FREE(frame);
1395 return -1;
1399 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1401 errno = ENOTDIR;
1402 TALLOC_FREE(frame);
1403 return -1;
1407 /* See if we're already at the end. */
1408 if (dir->dir_next == NULL) {
1409 /* We are. */
1410 TALLOC_FREE(frame);
1411 return -1;
1415 * We return the pointer here as the offset
1417 TALLOC_FREE(frame);
1418 return (off_t)(long)dir->dir_next->dirent;
1422 * A routine to run down the list and see if the entry is OK
1425 static struct smbc_dir_list *
1426 check_dir_ent(struct smbc_dir_list *list,
1427 struct smbc_dirent *dirent)
1430 /* Run down the list looking for what we want */
1432 if (dirent) {
1434 struct smbc_dir_list *tmp = list;
1436 while (tmp) {
1438 if (tmp->dirent == dirent)
1439 return tmp;
1441 tmp = tmp->next;
1447 return NULL; /* Not found, or an error */
1453 * Routine to seek on a directory
1457 SMBC_lseekdir_ctx(SMBCCTX *context,
1458 SMBCFILE *dir,
1459 off_t offset)
1461 long int l_offset = offset; /* Handle problems of size */
1462 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1463 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1464 TALLOC_CTX *frame = talloc_stackframe();
1466 if (!context || !context->internal->initialized) {
1468 errno = EINVAL;
1469 TALLOC_FREE(frame);
1470 return -1;
1474 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1476 errno = ENOTDIR;
1477 TALLOC_FREE(frame);
1478 return -1;
1482 /* Now, check what we were passed and see if it is OK ... */
1484 if (dirent == NULL) { /* Seek to the begining of the list */
1486 dir->dir_next = dir->dir_list;
1487 TALLOC_FREE(frame);
1488 return 0;
1492 if (offset == -1) { /* Seek to the end of the list */
1493 dir->dir_next = NULL;
1494 TALLOC_FREE(frame);
1495 return 0;
1498 /* Now, run down the list and make sure that the entry is OK */
1499 /* This may need to be changed if we change the format of the list */
1501 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1502 errno = EINVAL; /* Bad entry */
1503 TALLOC_FREE(frame);
1504 return -1;
1507 dir->dir_next = list_ent;
1509 TALLOC_FREE(frame);
1510 return 0;
1514 * Routine to fstat a dir
1518 SMBC_fstatdir_ctx(SMBCCTX *context,
1519 SMBCFILE *dir,
1520 struct stat *st)
1523 if (!context || !context->internal->initialized) {
1525 errno = EINVAL;
1526 return -1;
1529 /* No code yet ... */
1530 return 0;
1534 SMBC_chmod_ctx(SMBCCTX *context,
1535 const char *fname,
1536 mode_t newmode)
1538 SMBCSRV *srv = NULL;
1539 char *server = NULL;
1540 char *share = NULL;
1541 char *user = NULL;
1542 char *password = NULL;
1543 char *workgroup = NULL;
1544 char *targetpath = NULL;
1545 struct cli_state *targetcli = NULL;
1546 char *path = NULL;
1547 uint16 mode;
1548 TALLOC_CTX *frame = talloc_stackframe();
1550 if (!context || !context->internal->initialized) {
1552 errno = EINVAL; /* Best I can think of ... */
1553 TALLOC_FREE(frame);
1554 return -1;
1557 if (!fname) {
1558 errno = EINVAL;
1559 TALLOC_FREE(frame);
1560 return -1;
1563 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1565 if (SMBC_parse_path(frame,
1566 context,
1567 fname,
1568 &workgroup,
1569 &server,
1570 &share,
1571 &path,
1572 &user,
1573 &password,
1574 NULL)) {
1575 errno = EINVAL;
1576 TALLOC_FREE(frame);
1577 return -1;
1580 if (!user || user[0] == (char)0) {
1581 user = talloc_strdup(frame, smbc_getUser(context));
1582 if (!user) {
1583 errno = ENOMEM;
1584 TALLOC_FREE(frame);
1585 return -1;
1589 srv = SMBC_server(frame, context, True,
1590 server, share, &workgroup, &user, &password);
1592 if (!srv) {
1593 TALLOC_FREE(frame);
1594 return -1; /* errno set by SMBC_server */
1597 /*d_printf(">>>unlink: resolving %s\n", path);*/
1598 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1599 srv->cli, path,
1600 &targetcli, &targetpath)) {
1601 d_printf("Could not resolve %s\n", path);
1602 errno = ENOENT;
1603 TALLOC_FREE(frame);
1604 return -1;
1607 mode = 0;
1609 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1610 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1611 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1612 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1614 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1615 errno = SMBC_errno(context, targetcli);
1616 TALLOC_FREE(frame);
1617 return -1;
1620 TALLOC_FREE(frame);
1621 return 0;
1625 SMBC_utimes_ctx(SMBCCTX *context,
1626 const char *fname,
1627 struct timeval *tbuf)
1629 SMBCSRV *srv = NULL;
1630 char *server = NULL;
1631 char *share = NULL;
1632 char *user = NULL;
1633 char *password = NULL;
1634 char *workgroup = NULL;
1635 char *path = NULL;
1636 time_t access_time;
1637 time_t write_time;
1638 TALLOC_CTX *frame = talloc_stackframe();
1640 if (!context || !context->internal->initialized) {
1642 errno = EINVAL; /* Best I can think of ... */
1643 TALLOC_FREE(frame);
1644 return -1;
1647 if (!fname) {
1648 errno = EINVAL;
1649 TALLOC_FREE(frame);
1650 return -1;
1653 if (tbuf == NULL) {
1654 access_time = write_time = time(NULL);
1655 } else {
1656 access_time = tbuf[0].tv_sec;
1657 write_time = tbuf[1].tv_sec;
1660 if (DEBUGLVL(4)) {
1661 char *p;
1662 char atimebuf[32];
1663 char mtimebuf[32];
1665 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1666 atimebuf[sizeof(atimebuf) - 1] = '\0';
1667 if ((p = strchr(atimebuf, '\n')) != NULL) {
1668 *p = '\0';
1671 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1672 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1673 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1674 *p = '\0';
1677 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1678 fname, atimebuf, mtimebuf);
1681 if (SMBC_parse_path(frame,
1682 context,
1683 fname,
1684 &workgroup,
1685 &server,
1686 &share,
1687 &path,
1688 &user,
1689 &password,
1690 NULL)) {
1691 errno = EINVAL;
1692 TALLOC_FREE(frame);
1693 return -1;
1696 if (!user || user[0] == (char)0) {
1697 user = talloc_strdup(frame, smbc_getUser(context));
1698 if (!user) {
1699 errno = ENOMEM;
1700 TALLOC_FREE(frame);
1701 return -1;
1705 srv = SMBC_server(frame, context, True,
1706 server, share, &workgroup, &user, &password);
1708 if (!srv) {
1709 TALLOC_FREE(frame);
1710 return -1; /* errno set by SMBC_server */
1713 if (!SMBC_setatr(context, srv, path,
1714 0, access_time, write_time, 0, 0)) {
1715 TALLOC_FREE(frame);
1716 return -1; /* errno set by SMBC_setatr */
1719 TALLOC_FREE(frame);
1720 return 0;
1724 * Routine to unlink() a file
1728 SMBC_unlink_ctx(SMBCCTX *context,
1729 const char *fname)
1731 char *server = NULL;
1732 char *share = NULL;
1733 char *user = NULL;
1734 char *password = NULL;
1735 char *workgroup = NULL;
1736 char *path = NULL;
1737 char *targetpath = NULL;
1738 struct cli_state *targetcli = NULL;
1739 SMBCSRV *srv = NULL;
1740 TALLOC_CTX *frame = talloc_stackframe();
1742 if (!context || !context->internal->initialized) {
1744 errno = EINVAL; /* Best I can think of ... */
1745 TALLOC_FREE(frame);
1746 return -1;
1750 if (!fname) {
1751 errno = EINVAL;
1752 TALLOC_FREE(frame);
1753 return -1;
1757 if (SMBC_parse_path(frame,
1758 context,
1759 fname,
1760 &workgroup,
1761 &server,
1762 &share,
1763 &path,
1764 &user,
1765 &password,
1766 NULL)) {
1767 errno = EINVAL;
1768 TALLOC_FREE(frame);
1769 return -1;
1772 if (!user || user[0] == (char)0) {
1773 user = talloc_strdup(frame, smbc_getUser(context));
1774 if (!user) {
1775 errno = ENOMEM;
1776 TALLOC_FREE(frame);
1777 return -1;
1781 srv = SMBC_server(frame, context, True,
1782 server, share, &workgroup, &user, &password);
1784 if (!srv) {
1785 TALLOC_FREE(frame);
1786 return -1; /* SMBC_server sets errno */
1790 /*d_printf(">>>unlink: resolving %s\n", path);*/
1791 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1792 srv->cli, path,
1793 &targetcli, &targetpath)) {
1794 d_printf("Could not resolve %s\n", path);
1795 errno = ENOENT;
1796 TALLOC_FREE(frame);
1797 return -1;
1799 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1801 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, aSYSTEM | aHIDDEN))) {
1803 errno = SMBC_errno(context, targetcli);
1805 if (errno == EACCES) { /* Check if the file is a directory */
1807 int saverr = errno;
1808 SMB_OFF_T size = 0;
1809 uint16 mode = 0;
1810 struct timespec write_time_ts;
1811 struct timespec access_time_ts;
1812 struct timespec change_time_ts;
1813 SMB_INO_T ino = 0;
1815 if (!SMBC_getatr(context, srv, path, &mode, &size,
1816 NULL,
1817 &access_time_ts,
1818 &write_time_ts,
1819 &change_time_ts,
1820 &ino)) {
1822 /* Hmmm, bad error ... What? */
1824 errno = SMBC_errno(context, targetcli);
1825 TALLOC_FREE(frame);
1826 return -1;
1829 else {
1831 if (IS_DOS_DIR(mode))
1832 errno = EISDIR;
1833 else
1834 errno = saverr; /* Restore this */
1839 TALLOC_FREE(frame);
1840 return -1;
1844 TALLOC_FREE(frame);
1845 return 0; /* Success ... */
1850 * Routine to rename() a file
1854 SMBC_rename_ctx(SMBCCTX *ocontext,
1855 const char *oname,
1856 SMBCCTX *ncontext,
1857 const char *nname)
1859 char *server1 = NULL;
1860 char *share1 = NULL;
1861 char *server2 = NULL;
1862 char *share2 = NULL;
1863 char *user1 = NULL;
1864 char *user2 = NULL;
1865 char *password1 = NULL;
1866 char *password2 = NULL;
1867 char *workgroup = NULL;
1868 char *path1 = NULL;
1869 char *path2 = NULL;
1870 char *targetpath1 = NULL;
1871 char *targetpath2 = NULL;
1872 struct cli_state *targetcli1 = NULL;
1873 struct cli_state *targetcli2 = NULL;
1874 SMBCSRV *srv = NULL;
1875 TALLOC_CTX *frame = talloc_stackframe();
1877 if (!ocontext || !ncontext ||
1878 !ocontext->internal->initialized ||
1879 !ncontext->internal->initialized) {
1881 errno = EINVAL; /* Best I can think of ... */
1882 TALLOC_FREE(frame);
1883 return -1;
1886 if (!oname || !nname) {
1887 errno = EINVAL;
1888 TALLOC_FREE(frame);
1889 return -1;
1892 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1894 if (SMBC_parse_path(frame,
1895 ocontext,
1896 oname,
1897 &workgroup,
1898 &server1,
1899 &share1,
1900 &path1,
1901 &user1,
1902 &password1,
1903 NULL)) {
1904 errno = EINVAL;
1905 TALLOC_FREE(frame);
1906 return -1;
1909 if (!user1 || user1[0] == (char)0) {
1910 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1911 if (!user1) {
1912 errno = ENOMEM;
1913 TALLOC_FREE(frame);
1914 return -1;
1918 if (SMBC_parse_path(frame,
1919 ncontext,
1920 nname,
1921 NULL,
1922 &server2,
1923 &share2,
1924 &path2,
1925 &user2,
1926 &password2,
1927 NULL)) {
1928 errno = EINVAL;
1929 TALLOC_FREE(frame);
1930 return -1;
1933 if (!user2 || user2[0] == (char)0) {
1934 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1935 if (!user2) {
1936 errno = ENOMEM;
1937 TALLOC_FREE(frame);
1938 return -1;
1942 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1943 strcmp(user1, user2)) {
1944 /* Can't rename across file systems, or users?? */
1945 errno = EXDEV;
1946 TALLOC_FREE(frame);
1947 return -1;
1950 srv = SMBC_server(frame, ocontext, True,
1951 server1, share1, &workgroup, &user1, &password1);
1952 if (!srv) {
1953 TALLOC_FREE(frame);
1954 return -1;
1958 /* set the credentials to make DFS work */
1959 smbc_set_credentials_with_fallback(ocontext,
1960 workgroup,
1961 user1,
1962 password1);
1964 /*d_printf(">>>rename: resolving %s\n", path1);*/
1965 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1966 srv->cli,
1967 path1,
1968 &targetcli1, &targetpath1)) {
1969 d_printf("Could not resolve %s\n", path1);
1970 errno = ENOENT;
1971 TALLOC_FREE(frame);
1972 return -1;
1975 /* set the credentials to make DFS work */
1976 smbc_set_credentials_with_fallback(ncontext,
1977 workgroup,
1978 user2,
1979 password2);
1981 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1982 /*d_printf(">>>rename: resolving %s\n", path2);*/
1983 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1984 srv->cli,
1985 path2,
1986 &targetcli2, &targetpath2)) {
1987 d_printf("Could not resolve %s\n", path2);
1988 errno = ENOENT;
1989 TALLOC_FREE(frame);
1990 return -1;
1992 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1994 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1995 strcmp(targetcli1->share, targetcli2->share))
1997 /* can't rename across file systems */
1998 errno = EXDEV;
1999 TALLOC_FREE(frame);
2000 return -1;
2003 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2004 int eno = SMBC_errno(ocontext, targetcli1);
2006 if (eno != EEXIST ||
2007 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, aSYSTEM | aHIDDEN)) ||
2008 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2010 errno = eno;
2011 TALLOC_FREE(frame);
2012 return -1;
2017 TALLOC_FREE(frame);
2018 return 0; /* Success */