s3: Make name_resolve_bcast return sockaddr_storage
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob3602bc5377cdf78aba3c27c4d2eb1266005c7c71
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"
31 #include "libsmb/nmblib.h"
34 * Routine to open a directory
35 * We accept the URL syntax explained in SMBC_parse_path(), above.
38 static void
39 remove_dir(SMBCFILE *dir)
41 struct smbc_dir_list *d,*f;
43 d = dir->dir_list;
44 while (d) {
46 f = d; d = d->next;
48 SAFE_FREE(f->dirent);
49 SAFE_FREE(f);
53 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
57 static int
58 add_dirent(SMBCFILE *dir,
59 const char *name,
60 const char *comment,
61 uint32 type)
63 struct smbc_dirent *dirent;
64 int size;
65 int name_length = (name == NULL ? 0 : strlen(name));
66 int comment_len = (comment == NULL ? 0 : strlen(comment));
69 * Allocate space for the dirent, which must be increased by the
70 * size of the name and the comment and 1 each for the null terminator.
73 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
75 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
77 if (!dirent) {
79 dir->dir_error = ENOMEM;
80 return -1;
84 ZERO_STRUCTP(dirent);
86 if (dir->dir_list == NULL) {
88 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
89 if (!dir->dir_list) {
91 SAFE_FREE(dirent);
92 dir->dir_error = ENOMEM;
93 return -1;
96 ZERO_STRUCTP(dir->dir_list);
98 dir->dir_end = dir->dir_next = dir->dir_list;
100 else {
102 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
104 if (!dir->dir_end->next) {
106 SAFE_FREE(dirent);
107 dir->dir_error = ENOMEM;
108 return -1;
111 ZERO_STRUCTP(dir->dir_end->next);
113 dir->dir_end = dir->dir_end->next;
116 dir->dir_end->next = NULL;
117 dir->dir_end->dirent = dirent;
119 dirent->smbc_type = type;
120 dirent->namelen = name_length;
121 dirent->commentlen = comment_len;
122 dirent->dirlen = size;
125 * dirent->namelen + 1 includes the null (no null termination needed)
126 * Ditto for dirent->commentlen.
127 * The space for the two null bytes was allocated.
129 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
130 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
131 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
133 return 0;
137 static void
138 list_unique_wg_fn(const char *name,
139 uint32 type,
140 const char *comment,
141 void *state)
143 SMBCFILE *dir = (SMBCFILE *)state;
144 struct smbc_dir_list *dir_list;
145 struct smbc_dirent *dirent;
146 int dirent_type;
147 int do_remove = 0;
149 dirent_type = dir->dir_type;
151 if (add_dirent(dir, name, comment, dirent_type) < 0) {
152 /* An error occurred, what do we do? */
153 /* FIXME: Add some code here */
154 /* Change cli_NetServerEnum to take a fn
155 returning NTSTATUS... JRA. */
158 /* Point to the one just added */
159 dirent = dir->dir_end->dirent;
161 /* See if this was a duplicate */
162 for (dir_list = dir->dir_list;
163 dir_list != dir->dir_end;
164 dir_list = dir_list->next) {
165 if (! do_remove &&
166 strcmp(dir_list->dirent->name, dirent->name) == 0) {
167 /* Duplicate. End end of list need to be removed. */
168 do_remove = 1;
171 if (do_remove && dir_list->next == dir->dir_end) {
172 /* Found the end of the list. Remove it. */
173 dir->dir_end = dir_list;
174 free(dir_list->next);
175 free(dirent);
176 dir_list->next = NULL;
177 break;
182 static void
183 list_fn(const char *name,
184 uint32 type,
185 const char *comment,
186 void *state)
188 SMBCFILE *dir = (SMBCFILE *)state;
189 int dirent_type;
192 * We need to process the type a little ...
194 * Disk share = 0x00000000
195 * Print share = 0x00000001
196 * Comms share = 0x00000002 (obsolete?)
197 * IPC$ share = 0x00000003
199 * administrative shares:
200 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
203 if (dir->dir_type == SMBC_FILE_SHARE) {
204 switch (type) {
205 case 0 | 0x80000000:
206 case 0:
207 dirent_type = SMBC_FILE_SHARE;
208 break;
210 case 1:
211 dirent_type = SMBC_PRINTER_SHARE;
212 break;
214 case 2:
215 dirent_type = SMBC_COMMS_SHARE;
216 break;
218 case 3 | 0x80000000:
219 case 3:
220 dirent_type = SMBC_IPC_SHARE;
221 break;
223 default:
224 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
225 break;
228 else {
229 dirent_type = dir->dir_type;
232 if (add_dirent(dir, name, comment, dirent_type) < 0) {
233 /* An error occurred, what do we do? */
234 /* FIXME: Add some code here */
235 /* Change cli_NetServerEnum to take a fn
236 returning NTSTATUS... JRA. */
240 static NTSTATUS
241 dir_list_fn(const char *mnt,
242 struct file_info *finfo,
243 const char *mask,
244 void *state)
247 if (add_dirent((SMBCFILE *)state, finfo->name, "",
248 (finfo->mode&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
249 SMBCFILE *dir = (SMBCFILE *)state;
250 return map_nt_error_from_unix(dir->dir_error);
252 return NT_STATUS_OK;
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;
275 struct dcerpc_binding_handle *b;
277 /* Open the server service pipe */
278 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
279 &pipe_hnd);
280 if (!NT_STATUS_IS_OK(nt_status)) {
281 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
282 return -1;
285 ZERO_STRUCT(info_ctr);
286 ZERO_STRUCT(ctr1);
288 info_ctr.level = 1;
289 info_ctr.ctr.ctr1 = &ctr1;
291 b = pipe_hnd->binding_handle;
293 /* Issue the NetShareEnum RPC call and retrieve the response */
294 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
295 pipe_hnd->desthost,
296 &info_ctr,
297 preferred_len,
298 &total_entries,
299 &resume_handle,
300 &result);
302 /* Was it successful? */
303 if (!NT_STATUS_IS_OK(nt_status)) {
304 /* Nope. Go clean up. */
305 result = ntstatus_to_werror(nt_status);
306 goto done;
309 if (!W_ERROR_IS_OK(result)) {
310 /* Nope. Go clean up. */
311 goto done;
314 if (total_entries == 0) {
315 /* Nope. Go clean up. */
316 result = WERR_GENERAL_FAILURE;
317 goto done;
320 /* For each returned entry... */
321 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
323 /* pull out the share name */
324 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
326 /* pull out the share's comment */
327 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
329 /* Get the type value */
330 type = info_ctr.ctr.ctr1->array[i].type;
332 /* Add this share to the list */
333 (*fn)(name, type, comment, state);
336 done:
337 /* Close the server service pipe */
338 TALLOC_FREE(pipe_hnd);
340 /* Tell 'em if it worked */
341 return W_ERROR_IS_OK(result) ? 0 : -1;
346 * Verify that the options specified in a URL are valid
349 SMBC_check_options(char *server,
350 char *share,
351 char *path,
352 char *options)
354 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
355 "path='%s' options='%s'\n",
356 server, share, path, options));
358 /* No options at all is always ok */
359 if (! *options) return 0;
361 /* Currently, we don't support any options. */
362 return -1;
366 SMBCFILE *
367 SMBC_opendir_ctx(SMBCCTX *context,
368 const char *fname)
370 int saved_errno;
371 char *server = NULL;
372 char *share = NULL;
373 char *user = NULL;
374 char *password = NULL;
375 char *options = NULL;
376 char *workgroup = NULL;
377 char *path = NULL;
378 uint16 mode;
379 char *p = NULL;
380 SMBCSRV *srv = NULL;
381 SMBCFILE *dir = NULL;
382 struct sockaddr_storage rem_ss;
383 TALLOC_CTX *frame = talloc_stackframe();
385 if (!context || !context->internal->initialized) {
386 DEBUG(4, ("no valid context\n"));
387 TALLOC_FREE(frame);
388 errno = EINVAL + 8192;
389 return NULL;
393 if (!fname) {
394 DEBUG(4, ("no valid fname\n"));
395 TALLOC_FREE(frame);
396 errno = EINVAL + 8193;
397 return NULL;
400 if (SMBC_parse_path(frame,
401 context,
402 fname,
403 &workgroup,
404 &server,
405 &share,
406 &path,
407 &user,
408 &password,
409 &options)) {
410 DEBUG(4, ("no valid path\n"));
411 TALLOC_FREE(frame);
412 errno = EINVAL + 8194;
413 return NULL;
416 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
417 "path='%s' options='%s'\n",
418 fname, server, share, path, options));
420 /* Ensure the options are valid */
421 if (SMBC_check_options(server, share, path, options)) {
422 DEBUG(4, ("unacceptable options (%s)\n", options));
423 TALLOC_FREE(frame);
424 errno = EINVAL + 8195;
425 return NULL;
428 if (!user || user[0] == (char)0) {
429 user = talloc_strdup(frame, smbc_getUser(context));
430 if (!user) {
431 TALLOC_FREE(frame);
432 errno = ENOMEM;
433 return NULL;
437 dir = SMB_MALLOC_P(SMBCFILE);
439 if (!dir) {
440 TALLOC_FREE(frame);
441 errno = ENOMEM;
442 return NULL;
445 ZERO_STRUCTP(dir);
447 dir->cli_fd = 0;
448 dir->fname = SMB_STRDUP(fname);
449 dir->srv = NULL;
450 dir->offset = 0;
451 dir->file = False;
452 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
454 if (server[0] == (char)0) {
456 int i;
457 int count;
458 int max_lmb_count;
459 struct sockaddr_storage *ip_list;
460 struct sockaddr_storage server_addr;
461 struct user_auth_info u_info;
462 NTSTATUS status;
464 if (share[0] != (char)0 || path[0] != (char)0) {
466 if (dir) {
467 SAFE_FREE(dir->fname);
468 SAFE_FREE(dir);
470 TALLOC_FREE(frame);
471 errno = EINVAL + 8196;
472 return NULL;
475 /* Determine how many local master browsers to query */
476 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
477 ? INT_MAX
478 : smbc_getOptionBrowseMaxLmbCount(context));
480 memset(&u_info, '\0', sizeof(u_info));
481 u_info.username = talloc_strdup(frame,user);
482 u_info.password = talloc_strdup(frame,password);
483 if (!u_info.username || !u_info.password) {
484 if (dir) {
485 SAFE_FREE(dir->fname);
486 SAFE_FREE(dir);
488 TALLOC_FREE(frame);
489 return NULL;
493 * We have server and share and path empty but options
494 * requesting that we scan all master browsers for their list
495 * of workgroups/domains. This implies that we must first try
496 * broadcast queries to find all master browsers, and if that
497 * doesn't work, then try our other methods which return only
498 * a single master browser.
501 ip_list = NULL;
502 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
503 &ip_list, &count);
504 if (!NT_STATUS_IS_OK(status))
507 TALLOC_FREE(ip_list);
509 if (!find_master_ip(workgroup, &server_addr)) {
511 if (dir) {
512 SAFE_FREE(dir->fname);
513 SAFE_FREE(dir);
515 TALLOC_FREE(frame);
516 errno = ENOENT;
517 return NULL;
520 ip_list = (struct sockaddr_storage *)talloc_memdup(
521 talloc_tos(), &server_addr,
522 sizeof(server_addr));
523 if (ip_list == NULL) {
524 if (dir) {
525 SAFE_FREE(dir->fname);
526 SAFE_FREE(dir);
528 TALLOC_FREE(frame);
529 errno = ENOMEM;
530 return NULL;
532 count = 1;
535 for (i = 0; i < count && i < max_lmb_count; i++) {
536 char addr[INET6_ADDRSTRLEN];
537 char *wg_ptr = NULL;
538 struct cli_state *cli = NULL;
540 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
541 DEBUG(99, ("Found master browser %d of %d: %s\n",
542 i+1, MAX(count, max_lmb_count),
543 addr));
545 cli = get_ipc_connect_master_ip(talloc_tos(),
546 &ip_list[i],
547 &u_info,
548 &wg_ptr);
549 /* cli == NULL is the master browser refused to talk or
550 could not be found */
551 if (!cli) {
552 continue;
555 workgroup = talloc_strdup(frame, wg_ptr);
556 server = talloc_strdup(frame, cli->desthost);
558 cli_shutdown(cli);
560 if (!workgroup || !server) {
561 if (dir) {
562 SAFE_FREE(dir->fname);
563 SAFE_FREE(dir);
565 TALLOC_FREE(frame);
566 errno = ENOMEM;
567 return NULL;
570 DEBUG(4, ("using workgroup %s %s\n",
571 workgroup, server));
574 * For each returned master browser IP address, get a
575 * connection to IPC$ on the server if we do not
576 * already have one, and determine the
577 * workgroups/domains that it knows about.
580 srv = SMBC_server(frame, context, True, server, "IPC$",
581 &workgroup, &user, &password);
582 if (!srv) {
583 continue;
586 dir->srv = srv;
587 dir->dir_type = SMBC_WORKGROUP;
589 /* Now, list the stuff ... */
591 if (!cli_NetServerEnum(srv->cli,
592 workgroup,
593 SV_TYPE_DOMAIN_ENUM,
594 list_unique_wg_fn,
595 (void *)dir)) {
596 continue;
600 TALLOC_FREE(ip_list);
601 } else {
603 * Server not an empty string ... Check the rest and see what
604 * gives
606 if (*share == '\0') {
607 if (*path != '\0') {
609 /* Should not have empty share with path */
610 if (dir) {
611 SAFE_FREE(dir->fname);
612 SAFE_FREE(dir);
614 TALLOC_FREE(frame);
615 errno = EINVAL + 8197;
616 return NULL;
621 * We don't know if <server> is really a server name
622 * or is a workgroup/domain name. If we already have
623 * a server structure for it, we'll use it.
624 * Otherwise, check to see if <server><1D>,
625 * <server><1B>, or <server><20> translates. We check
626 * to see if <server> is an IP address first.
630 * See if we have an existing server. Do not
631 * establish a connection if one does not already
632 * exist.
634 srv = SMBC_server(frame, context, False,
635 server, "IPC$",
636 &workgroup, &user, &password);
639 * If no existing server and not an IP addr, look for
640 * LMB or DMB
642 if (!srv &&
643 !is_ipaddress(server) &&
644 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
645 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
647 * "server" is actually a workgroup name,
648 * not a server. Make this clear.
650 char *wgroup = server;
651 fstring buserver;
653 dir->dir_type = SMBC_SERVER;
656 * Get the backup list ...
658 if (!name_status_find(wgroup, 0, 0,
659 &rem_ss, buserver)) {
660 char addr[INET6_ADDRSTRLEN];
662 print_sockaddr(addr, sizeof(addr), &rem_ss);
663 DEBUG(0,("Could not get name of "
664 "local/domain master browser "
665 "for workgroup %s from "
666 "address %s\n",
667 wgroup,
668 addr));
669 if (dir) {
670 SAFE_FREE(dir->fname);
671 SAFE_FREE(dir);
673 TALLOC_FREE(frame);
674 errno = EPERM;
675 return NULL;
680 * Get a connection to IPC$ on the server if
681 * we do not already have one
683 srv = SMBC_server(frame, context, True,
684 buserver, "IPC$",
685 &workgroup,
686 &user, &password);
687 if (!srv) {
688 DEBUG(0, ("got no contact to IPC$\n"));
689 if (dir) {
690 SAFE_FREE(dir->fname);
691 SAFE_FREE(dir);
693 TALLOC_FREE(frame);
694 return NULL;
698 dir->srv = srv;
700 /* Now, list the servers ... */
701 if (!cli_NetServerEnum(srv->cli, wgroup,
702 0x0000FFFE, list_fn,
703 (void *)dir)) {
705 if (dir) {
706 SAFE_FREE(dir->fname);
707 SAFE_FREE(dir);
709 TALLOC_FREE(frame);
710 return NULL;
712 } else if (srv ||
713 (resolve_name(server, &rem_ss, 0x20, false))) {
716 * If we hadn't found the server, get one now
718 if (!srv) {
719 srv = SMBC_server(frame, context, True,
720 server, "IPC$",
721 &workgroup,
722 &user, &password);
725 if (!srv) {
726 if (dir) {
727 SAFE_FREE(dir->fname);
728 SAFE_FREE(dir);
730 TALLOC_FREE(frame);
731 return NULL;
735 dir->dir_type = SMBC_FILE_SHARE;
736 dir->srv = srv;
738 /* List the shares ... */
740 if (net_share_enum_rpc(
741 srv->cli,
742 list_fn,
743 (void *) dir) < 0 &&
744 cli_RNetShareEnum(
745 srv->cli,
746 list_fn,
747 (void *)dir) < 0) {
749 errno = cli_errno(srv->cli);
750 if (dir) {
751 SAFE_FREE(dir->fname);
752 SAFE_FREE(dir);
754 TALLOC_FREE(frame);
755 return NULL;
758 } else {
759 /* Neither the workgroup nor server exists */
760 errno = ECONNREFUSED;
761 if (dir) {
762 SAFE_FREE(dir->fname);
763 SAFE_FREE(dir);
765 TALLOC_FREE(frame);
766 return NULL;
770 else {
772 * The server and share are specified ... work from
773 * there ...
775 char *targetpath;
776 struct cli_state *targetcli;
777 NTSTATUS status;
779 /* We connect to the server and list the directory */
780 dir->dir_type = SMBC_FILE_SHARE;
782 srv = SMBC_server(frame, context, True, server, share,
783 &workgroup, &user, &password);
785 if (!srv) {
786 if (dir) {
787 SAFE_FREE(dir->fname);
788 SAFE_FREE(dir);
790 TALLOC_FREE(frame);
791 return NULL;
794 dir->srv = srv;
796 /* Now, list the files ... */
798 p = path + strlen(path);
799 path = talloc_asprintf_append(path, "\\*");
800 if (!path) {
801 if (dir) {
802 SAFE_FREE(dir->fname);
803 SAFE_FREE(dir);
805 TALLOC_FREE(frame);
806 return NULL;
809 if (!cli_resolve_path(frame, "", context->internal->auth_info,
810 srv->cli, path,
811 &targetcli, &targetpath)) {
812 d_printf("Could not resolve %s\n", path);
813 if (dir) {
814 SAFE_FREE(dir->fname);
815 SAFE_FREE(dir);
817 TALLOC_FREE(frame);
818 return NULL;
821 status = cli_list(targetcli, targetpath,
822 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
823 dir_list_fn, (void *)dir);
824 if (!NT_STATUS_IS_OK(status)) {
825 if (dir) {
826 SAFE_FREE(dir->fname);
827 SAFE_FREE(dir);
829 saved_errno = SMBC_errno(context, targetcli);
831 if (saved_errno == EINVAL) {
833 * See if they asked to opendir
834 * something other than a directory.
835 * If so, the converted error value we
836 * got would have been EINVAL rather
837 * than ENOTDIR.
839 *p = '\0'; /* restore original path */
841 if (SMBC_getatr(context, srv, path,
842 &mode, NULL,
843 NULL, NULL, NULL, NULL,
844 NULL) &&
845 ! IS_DOS_DIR(mode)) {
847 /* It is. Correct the error value */
848 saved_errno = ENOTDIR;
853 * If there was an error and the server is no
854 * good any more...
856 if (cli_is_error(targetcli) &&
857 smbc_getFunctionCheckServer(context)(context, srv)) {
859 /* ... then remove it. */
860 if (smbc_getFunctionRemoveUnusedServer(context)(context,
861 srv)) {
863 * We could not remove the
864 * server completely, remove
865 * it from the cache so we
866 * will not get it again. It
867 * will be removed when the
868 * last file/dir is closed.
870 smbc_getFunctionRemoveCachedServer(context)(context, srv);
874 TALLOC_FREE(frame);
875 errno = saved_errno;
876 return NULL;
882 DLIST_ADD(context->internal->files, dir);
883 TALLOC_FREE(frame);
884 return dir;
889 * Routine to close a directory
893 SMBC_closedir_ctx(SMBCCTX *context,
894 SMBCFILE *dir)
896 TALLOC_CTX *frame = talloc_stackframe();
898 if (!context || !context->internal->initialized) {
899 errno = EINVAL;
900 TALLOC_FREE(frame);
901 return -1;
904 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
905 errno = EBADF;
906 TALLOC_FREE(frame);
907 return -1;
910 remove_dir(dir); /* Clean it up */
912 DLIST_REMOVE(context->internal->files, dir);
914 if (dir) {
916 SAFE_FREE(dir->fname);
917 SAFE_FREE(dir); /* Free the space too */
920 TALLOC_FREE(frame);
921 return 0;
925 static void
926 smbc_readdir_internal(SMBCCTX * context,
927 struct smbc_dirent *dest,
928 struct smbc_dirent *src,
929 int max_namebuf_len)
931 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
933 /* url-encode the name. get back remaining buffer space */
934 max_namebuf_len =
935 smbc_urlencode(dest->name, src->name, max_namebuf_len);
937 /* We now know the name length */
938 dest->namelen = strlen(dest->name);
940 /* Save the pointer to the beginning of the comment */
941 dest->comment = dest->name + dest->namelen + 1;
943 /* Copy the comment */
944 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
945 dest->comment[max_namebuf_len - 1] = '\0';
947 /* Save other fields */
948 dest->smbc_type = src->smbc_type;
949 dest->commentlen = strlen(dest->comment);
950 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
951 (char *) dest);
952 } else {
954 /* No encoding. Just copy the entry as is. */
955 memcpy(dest, src, src->dirlen);
956 dest->comment = (char *)(&dest->name + src->namelen + 1);
962 * Routine to get a directory entry
965 struct smbc_dirent *
966 SMBC_readdir_ctx(SMBCCTX *context,
967 SMBCFILE *dir)
969 int maxlen;
970 struct smbc_dirent *dirp, *dirent;
971 TALLOC_CTX *frame = talloc_stackframe();
973 /* Check that all is ok first ... */
975 if (!context || !context->internal->initialized) {
977 errno = EINVAL;
978 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
979 TALLOC_FREE(frame);
980 return NULL;
984 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
986 errno = EBADF;
987 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
988 TALLOC_FREE(frame);
989 return NULL;
993 if (dir->file != False) { /* FIXME, should be dir, perhaps */
995 errno = ENOTDIR;
996 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
997 TALLOC_FREE(frame);
998 return NULL;
1002 if (!dir->dir_next) {
1003 TALLOC_FREE(frame);
1004 return NULL;
1007 dirent = dir->dir_next->dirent;
1008 if (!dirent) {
1010 errno = ENOENT;
1011 TALLOC_FREE(frame);
1012 return NULL;
1016 dirp = &context->internal->dirent;
1017 maxlen = sizeof(context->internal->_dirent_name);
1019 smbc_readdir_internal(context, dirp, dirent, maxlen);
1021 dir->dir_next = dir->dir_next->next;
1023 TALLOC_FREE(frame);
1024 return dirp;
1028 * Routine to get directory entries
1032 SMBC_getdents_ctx(SMBCCTX *context,
1033 SMBCFILE *dir,
1034 struct smbc_dirent *dirp,
1035 int count)
1037 int rem = count;
1038 int reqd;
1039 int maxlen;
1040 char *ndir = (char *)dirp;
1041 struct smbc_dir_list *dirlist;
1042 TALLOC_CTX *frame = talloc_stackframe();
1044 /* Check that all is ok first ... */
1046 if (!context || !context->internal->initialized) {
1048 errno = EINVAL;
1049 TALLOC_FREE(frame);
1050 return -1;
1054 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1056 errno = EBADF;
1057 TALLOC_FREE(frame);
1058 return -1;
1062 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1064 errno = ENOTDIR;
1065 TALLOC_FREE(frame);
1066 return -1;
1071 * Now, retrieve the number of entries that will fit in what was passed
1072 * We have to figure out if the info is in the list, or we need to
1073 * send a request to the server to get the info.
1076 while ((dirlist = dir->dir_next)) {
1077 struct smbc_dirent *dirent;
1078 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1080 if (!dirlist->dirent) {
1082 errno = ENOENT; /* Bad error */
1083 TALLOC_FREE(frame);
1084 return -1;
1088 /* Do urlencoding of next entry, if so selected */
1089 dirent = &context->internal->dirent;
1090 maxlen = sizeof(context->internal->_dirent_name);
1091 smbc_readdir_internal(context, dirent,
1092 dirlist->dirent, maxlen);
1094 reqd = dirent->dirlen;
1096 if (rem < reqd) {
1098 if (rem < count) { /* We managed to copy something */
1100 errno = 0;
1101 TALLOC_FREE(frame);
1102 return count - rem;
1105 else { /* Nothing copied ... */
1107 errno = EINVAL; /* Not enough space ... */
1108 TALLOC_FREE(frame);
1109 return -1;
1115 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1117 currentEntry->comment = &currentEntry->name[0] +
1118 dirent->namelen + 1;
1120 ndir += reqd;
1121 rem -= reqd;
1123 /* Try and align the struct for the next entry
1124 on a valid pointer boundary by appending zeros */
1125 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1126 *ndir = '\0';
1127 rem--;
1128 ndir++;
1129 currentEntry->dirlen++;
1132 dir->dir_next = dirlist = dirlist -> next;
1135 TALLOC_FREE(frame);
1137 if (rem == count)
1138 return 0;
1139 else
1140 return count - rem;
1145 * Routine to create a directory ...
1149 SMBC_mkdir_ctx(SMBCCTX *context,
1150 const char *fname,
1151 mode_t mode)
1153 SMBCSRV *srv = NULL;
1154 char *server = NULL;
1155 char *share = NULL;
1156 char *user = NULL;
1157 char *password = NULL;
1158 char *workgroup = NULL;
1159 char *path = NULL;
1160 char *targetpath = NULL;
1161 struct cli_state *targetcli = NULL;
1162 TALLOC_CTX *frame = talloc_stackframe();
1164 if (!context || !context->internal->initialized) {
1165 errno = EINVAL;
1166 TALLOC_FREE(frame);
1167 return -1;
1170 if (!fname) {
1171 errno = EINVAL;
1172 TALLOC_FREE(frame);
1173 return -1;
1176 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1178 if (SMBC_parse_path(frame,
1179 context,
1180 fname,
1181 &workgroup,
1182 &server,
1183 &share,
1184 &path,
1185 &user,
1186 &password,
1187 NULL)) {
1188 errno = EINVAL;
1189 TALLOC_FREE(frame);
1190 return -1;
1193 if (!user || user[0] == (char)0) {
1194 user = talloc_strdup(frame, smbc_getUser(context));
1195 if (!user) {
1196 errno = ENOMEM;
1197 TALLOC_FREE(frame);
1198 return -1;
1202 srv = SMBC_server(frame, context, True,
1203 server, share, &workgroup, &user, &password);
1205 if (!srv) {
1207 TALLOC_FREE(frame);
1208 return -1; /* errno set by SMBC_server */
1212 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1213 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1214 srv->cli, path,
1215 &targetcli, &targetpath)) {
1216 d_printf("Could not resolve %s\n", path);
1217 errno = ENOENT;
1218 TALLOC_FREE(frame);
1219 return -1;
1221 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1223 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1224 errno = SMBC_errno(context, targetcli);
1225 TALLOC_FREE(frame);
1226 return -1;
1230 TALLOC_FREE(frame);
1231 return 0;
1236 * Our list function simply checks to see if a directory is not empty
1239 static NTSTATUS
1240 rmdir_list_fn(const char *mnt,
1241 struct file_info *finfo,
1242 const char *mask,
1243 void *state)
1245 if (strncmp(finfo->name, ".", 1) != 0 &&
1246 strncmp(finfo->name, "..", 2) != 0) {
1247 bool *smbc_rmdir_dirempty = (bool *)state;
1248 *smbc_rmdir_dirempty = false;
1250 return NT_STATUS_OK;
1254 * Routine to remove a directory
1258 SMBC_rmdir_ctx(SMBCCTX *context,
1259 const char *fname)
1261 SMBCSRV *srv = NULL;
1262 char *server = NULL;
1263 char *share = NULL;
1264 char *user = NULL;
1265 char *password = NULL;
1266 char *workgroup = NULL;
1267 char *path = NULL;
1268 char *targetpath = NULL;
1269 struct cli_state *targetcli = NULL;
1270 TALLOC_CTX *frame = talloc_stackframe();
1272 if (!context || !context->internal->initialized) {
1273 errno = EINVAL;
1274 TALLOC_FREE(frame);
1275 return -1;
1278 if (!fname) {
1279 errno = EINVAL;
1280 TALLOC_FREE(frame);
1281 return -1;
1284 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1286 if (SMBC_parse_path(frame,
1287 context,
1288 fname,
1289 &workgroup,
1290 &server,
1291 &share,
1292 &path,
1293 &user,
1294 &password,
1295 NULL)) {
1296 errno = EINVAL;
1297 TALLOC_FREE(frame);
1298 return -1;
1301 if (!user || user[0] == (char)0) {
1302 user = talloc_strdup(frame, smbc_getUser(context));
1303 if (!user) {
1304 errno = ENOMEM;
1305 TALLOC_FREE(frame);
1306 return -1;
1310 srv = SMBC_server(frame, context, True,
1311 server, share, &workgroup, &user, &password);
1313 if (!srv) {
1315 TALLOC_FREE(frame);
1316 return -1; /* errno set by SMBC_server */
1320 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1321 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1322 srv->cli, path,
1323 &targetcli, &targetpath)) {
1324 d_printf("Could not resolve %s\n", path);
1325 errno = ENOENT;
1326 TALLOC_FREE(frame);
1327 return -1;
1329 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1331 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1333 errno = SMBC_errno(context, targetcli);
1335 if (errno == EACCES) { /* Check if the dir empty or not */
1337 /* Local storage to avoid buffer overflows */
1338 char *lpath;
1339 bool smbc_rmdir_dirempty = true;
1340 NTSTATUS status;
1342 lpath = talloc_asprintf(frame, "%s\\*",
1343 targetpath);
1344 if (!lpath) {
1345 errno = ENOMEM;
1346 TALLOC_FREE(frame);
1347 return -1;
1350 status = cli_list(targetcli, lpath,
1351 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1352 rmdir_list_fn,
1353 &smbc_rmdir_dirempty);
1355 if (!NT_STATUS_IS_OK(status)) {
1356 /* Fix errno to ignore latest error ... */
1357 DEBUG(5, ("smbc_rmdir: "
1358 "cli_list returned an error: %d\n",
1359 SMBC_errno(context, targetcli)));
1360 errno = EACCES;
1364 if (smbc_rmdir_dirempty)
1365 errno = EACCES;
1366 else
1367 errno = ENOTEMPTY;
1371 TALLOC_FREE(frame);
1372 return -1;
1376 TALLOC_FREE(frame);
1377 return 0;
1382 * Routine to return the current directory position
1385 off_t
1386 SMBC_telldir_ctx(SMBCCTX *context,
1387 SMBCFILE *dir)
1389 TALLOC_CTX *frame = talloc_stackframe();
1391 if (!context || !context->internal->initialized) {
1393 errno = EINVAL;
1394 TALLOC_FREE(frame);
1395 return -1;
1399 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1401 errno = EBADF;
1402 TALLOC_FREE(frame);
1403 return -1;
1407 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1409 errno = ENOTDIR;
1410 TALLOC_FREE(frame);
1411 return -1;
1415 /* See if we're already at the end. */
1416 if (dir->dir_next == NULL) {
1417 /* We are. */
1418 TALLOC_FREE(frame);
1419 return -1;
1423 * We return the pointer here as the offset
1425 TALLOC_FREE(frame);
1426 return (off_t)(long)dir->dir_next->dirent;
1430 * A routine to run down the list and see if the entry is OK
1433 static struct smbc_dir_list *
1434 check_dir_ent(struct smbc_dir_list *list,
1435 struct smbc_dirent *dirent)
1438 /* Run down the list looking for what we want */
1440 if (dirent) {
1442 struct smbc_dir_list *tmp = list;
1444 while (tmp) {
1446 if (tmp->dirent == dirent)
1447 return tmp;
1449 tmp = tmp->next;
1455 return NULL; /* Not found, or an error */
1461 * Routine to seek on a directory
1465 SMBC_lseekdir_ctx(SMBCCTX *context,
1466 SMBCFILE *dir,
1467 off_t offset)
1469 long int l_offset = offset; /* Handle problems of size */
1470 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1471 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1472 TALLOC_CTX *frame = talloc_stackframe();
1474 if (!context || !context->internal->initialized) {
1476 errno = EINVAL;
1477 TALLOC_FREE(frame);
1478 return -1;
1482 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1484 errno = ENOTDIR;
1485 TALLOC_FREE(frame);
1486 return -1;
1490 /* Now, check what we were passed and see if it is OK ... */
1492 if (dirent == NULL) { /* Seek to the begining of the list */
1494 dir->dir_next = dir->dir_list;
1495 TALLOC_FREE(frame);
1496 return 0;
1500 if (offset == -1) { /* Seek to the end of the list */
1501 dir->dir_next = NULL;
1502 TALLOC_FREE(frame);
1503 return 0;
1506 /* Now, run down the list and make sure that the entry is OK */
1507 /* This may need to be changed if we change the format of the list */
1509 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1510 errno = EINVAL; /* Bad entry */
1511 TALLOC_FREE(frame);
1512 return -1;
1515 dir->dir_next = list_ent;
1517 TALLOC_FREE(frame);
1518 return 0;
1522 * Routine to fstat a dir
1526 SMBC_fstatdir_ctx(SMBCCTX *context,
1527 SMBCFILE *dir,
1528 struct stat *st)
1531 if (!context || !context->internal->initialized) {
1533 errno = EINVAL;
1534 return -1;
1537 /* No code yet ... */
1538 return 0;
1542 SMBC_chmod_ctx(SMBCCTX *context,
1543 const char *fname,
1544 mode_t newmode)
1546 SMBCSRV *srv = NULL;
1547 char *server = NULL;
1548 char *share = NULL;
1549 char *user = NULL;
1550 char *password = NULL;
1551 char *workgroup = NULL;
1552 char *targetpath = NULL;
1553 struct cli_state *targetcli = NULL;
1554 char *path = NULL;
1555 uint16 mode;
1556 TALLOC_CTX *frame = talloc_stackframe();
1558 if (!context || !context->internal->initialized) {
1560 errno = EINVAL; /* Best I can think of ... */
1561 TALLOC_FREE(frame);
1562 return -1;
1565 if (!fname) {
1566 errno = EINVAL;
1567 TALLOC_FREE(frame);
1568 return -1;
1571 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1573 if (SMBC_parse_path(frame,
1574 context,
1575 fname,
1576 &workgroup,
1577 &server,
1578 &share,
1579 &path,
1580 &user,
1581 &password,
1582 NULL)) {
1583 errno = EINVAL;
1584 TALLOC_FREE(frame);
1585 return -1;
1588 if (!user || user[0] == (char)0) {
1589 user = talloc_strdup(frame, smbc_getUser(context));
1590 if (!user) {
1591 errno = ENOMEM;
1592 TALLOC_FREE(frame);
1593 return -1;
1597 srv = SMBC_server(frame, context, True,
1598 server, share, &workgroup, &user, &password);
1600 if (!srv) {
1601 TALLOC_FREE(frame);
1602 return -1; /* errno set by SMBC_server */
1605 /*d_printf(">>>unlink: resolving %s\n", path);*/
1606 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1607 srv->cli, path,
1608 &targetcli, &targetpath)) {
1609 d_printf("Could not resolve %s\n", path);
1610 errno = ENOENT;
1611 TALLOC_FREE(frame);
1612 return -1;
1615 mode = 0;
1617 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1618 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1619 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1620 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1622 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1623 errno = SMBC_errno(context, targetcli);
1624 TALLOC_FREE(frame);
1625 return -1;
1628 TALLOC_FREE(frame);
1629 return 0;
1633 SMBC_utimes_ctx(SMBCCTX *context,
1634 const char *fname,
1635 struct timeval *tbuf)
1637 SMBCSRV *srv = NULL;
1638 char *server = NULL;
1639 char *share = NULL;
1640 char *user = NULL;
1641 char *password = NULL;
1642 char *workgroup = NULL;
1643 char *path = NULL;
1644 time_t access_time;
1645 time_t write_time;
1646 TALLOC_CTX *frame = talloc_stackframe();
1648 if (!context || !context->internal->initialized) {
1650 errno = EINVAL; /* Best I can think of ... */
1651 TALLOC_FREE(frame);
1652 return -1;
1655 if (!fname) {
1656 errno = EINVAL;
1657 TALLOC_FREE(frame);
1658 return -1;
1661 if (tbuf == NULL) {
1662 access_time = write_time = time(NULL);
1663 } else {
1664 access_time = tbuf[0].tv_sec;
1665 write_time = tbuf[1].tv_sec;
1668 if (DEBUGLVL(4)) {
1669 char *p;
1670 char atimebuf[32];
1671 char mtimebuf[32];
1673 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1674 atimebuf[sizeof(atimebuf) - 1] = '\0';
1675 if ((p = strchr(atimebuf, '\n')) != NULL) {
1676 *p = '\0';
1679 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1680 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1681 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1682 *p = '\0';
1685 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1686 fname, atimebuf, mtimebuf);
1689 if (SMBC_parse_path(frame,
1690 context,
1691 fname,
1692 &workgroup,
1693 &server,
1694 &share,
1695 &path,
1696 &user,
1697 &password,
1698 NULL)) {
1699 errno = EINVAL;
1700 TALLOC_FREE(frame);
1701 return -1;
1704 if (!user || user[0] == (char)0) {
1705 user = talloc_strdup(frame, smbc_getUser(context));
1706 if (!user) {
1707 errno = ENOMEM;
1708 TALLOC_FREE(frame);
1709 return -1;
1713 srv = SMBC_server(frame, context, True,
1714 server, share, &workgroup, &user, &password);
1716 if (!srv) {
1717 TALLOC_FREE(frame);
1718 return -1; /* errno set by SMBC_server */
1721 if (!SMBC_setatr(context, srv, path,
1722 0, access_time, write_time, 0, 0)) {
1723 TALLOC_FREE(frame);
1724 return -1; /* errno set by SMBC_setatr */
1727 TALLOC_FREE(frame);
1728 return 0;
1732 * Routine to unlink() a file
1736 SMBC_unlink_ctx(SMBCCTX *context,
1737 const char *fname)
1739 char *server = NULL;
1740 char *share = NULL;
1741 char *user = NULL;
1742 char *password = NULL;
1743 char *workgroup = NULL;
1744 char *path = NULL;
1745 char *targetpath = NULL;
1746 struct cli_state *targetcli = NULL;
1747 SMBCSRV *srv = NULL;
1748 TALLOC_CTX *frame = talloc_stackframe();
1750 if (!context || !context->internal->initialized) {
1752 errno = EINVAL; /* Best I can think of ... */
1753 TALLOC_FREE(frame);
1754 return -1;
1758 if (!fname) {
1759 errno = EINVAL;
1760 TALLOC_FREE(frame);
1761 return -1;
1765 if (SMBC_parse_path(frame,
1766 context,
1767 fname,
1768 &workgroup,
1769 &server,
1770 &share,
1771 &path,
1772 &user,
1773 &password,
1774 NULL)) {
1775 errno = EINVAL;
1776 TALLOC_FREE(frame);
1777 return -1;
1780 if (!user || user[0] == (char)0) {
1781 user = talloc_strdup(frame, smbc_getUser(context));
1782 if (!user) {
1783 errno = ENOMEM;
1784 TALLOC_FREE(frame);
1785 return -1;
1789 srv = SMBC_server(frame, context, True,
1790 server, share, &workgroup, &user, &password);
1792 if (!srv) {
1793 TALLOC_FREE(frame);
1794 return -1; /* SMBC_server sets errno */
1798 /*d_printf(">>>unlink: resolving %s\n", path);*/
1799 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1800 srv->cli, path,
1801 &targetcli, &targetpath)) {
1802 d_printf("Could not resolve %s\n", path);
1803 errno = ENOENT;
1804 TALLOC_FREE(frame);
1805 return -1;
1807 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1809 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1811 errno = SMBC_errno(context, targetcli);
1813 if (errno == EACCES) { /* Check if the file is a directory */
1815 int saverr = errno;
1816 SMB_OFF_T size = 0;
1817 uint16 mode = 0;
1818 struct timespec write_time_ts;
1819 struct timespec access_time_ts;
1820 struct timespec change_time_ts;
1821 SMB_INO_T ino = 0;
1823 if (!SMBC_getatr(context, srv, path, &mode, &size,
1824 NULL,
1825 &access_time_ts,
1826 &write_time_ts,
1827 &change_time_ts,
1828 &ino)) {
1830 /* Hmmm, bad error ... What? */
1832 errno = SMBC_errno(context, targetcli);
1833 TALLOC_FREE(frame);
1834 return -1;
1837 else {
1839 if (IS_DOS_DIR(mode))
1840 errno = EISDIR;
1841 else
1842 errno = saverr; /* Restore this */
1847 TALLOC_FREE(frame);
1848 return -1;
1852 TALLOC_FREE(frame);
1853 return 0; /* Success ... */
1858 * Routine to rename() a file
1862 SMBC_rename_ctx(SMBCCTX *ocontext,
1863 const char *oname,
1864 SMBCCTX *ncontext,
1865 const char *nname)
1867 char *server1 = NULL;
1868 char *share1 = NULL;
1869 char *server2 = NULL;
1870 char *share2 = NULL;
1871 char *user1 = NULL;
1872 char *user2 = NULL;
1873 char *password1 = NULL;
1874 char *password2 = NULL;
1875 char *workgroup = NULL;
1876 char *path1 = NULL;
1877 char *path2 = NULL;
1878 char *targetpath1 = NULL;
1879 char *targetpath2 = NULL;
1880 struct cli_state *targetcli1 = NULL;
1881 struct cli_state *targetcli2 = NULL;
1882 SMBCSRV *srv = NULL;
1883 TALLOC_CTX *frame = talloc_stackframe();
1885 if (!ocontext || !ncontext ||
1886 !ocontext->internal->initialized ||
1887 !ncontext->internal->initialized) {
1889 errno = EINVAL; /* Best I can think of ... */
1890 TALLOC_FREE(frame);
1891 return -1;
1894 if (!oname || !nname) {
1895 errno = EINVAL;
1896 TALLOC_FREE(frame);
1897 return -1;
1900 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1902 if (SMBC_parse_path(frame,
1903 ocontext,
1904 oname,
1905 &workgroup,
1906 &server1,
1907 &share1,
1908 &path1,
1909 &user1,
1910 &password1,
1911 NULL)) {
1912 errno = EINVAL;
1913 TALLOC_FREE(frame);
1914 return -1;
1917 if (!user1 || user1[0] == (char)0) {
1918 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1919 if (!user1) {
1920 errno = ENOMEM;
1921 TALLOC_FREE(frame);
1922 return -1;
1926 if (SMBC_parse_path(frame,
1927 ncontext,
1928 nname,
1929 NULL,
1930 &server2,
1931 &share2,
1932 &path2,
1933 &user2,
1934 &password2,
1935 NULL)) {
1936 errno = EINVAL;
1937 TALLOC_FREE(frame);
1938 return -1;
1941 if (!user2 || user2[0] == (char)0) {
1942 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1943 if (!user2) {
1944 errno = ENOMEM;
1945 TALLOC_FREE(frame);
1946 return -1;
1950 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1951 strcmp(user1, user2)) {
1952 /* Can't rename across file systems, or users?? */
1953 errno = EXDEV;
1954 TALLOC_FREE(frame);
1955 return -1;
1958 srv = SMBC_server(frame, ocontext, True,
1959 server1, share1, &workgroup, &user1, &password1);
1960 if (!srv) {
1961 TALLOC_FREE(frame);
1962 return -1;
1966 /* set the credentials to make DFS work */
1967 smbc_set_credentials_with_fallback(ocontext,
1968 workgroup,
1969 user1,
1970 password1);
1972 /*d_printf(">>>rename: resolving %s\n", path1);*/
1973 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1974 srv->cli,
1975 path1,
1976 &targetcli1, &targetpath1)) {
1977 d_printf("Could not resolve %s\n", path1);
1978 errno = ENOENT;
1979 TALLOC_FREE(frame);
1980 return -1;
1983 /* set the credentials to make DFS work */
1984 smbc_set_credentials_with_fallback(ncontext,
1985 workgroup,
1986 user2,
1987 password2);
1989 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1990 /*d_printf(">>>rename: resolving %s\n", path2);*/
1991 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1992 srv->cli,
1993 path2,
1994 &targetcli2, &targetpath2)) {
1995 d_printf("Could not resolve %s\n", path2);
1996 errno = ENOENT;
1997 TALLOC_FREE(frame);
1998 return -1;
2000 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2002 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2003 strcmp(targetcli1->share, targetcli2->share))
2005 /* can't rename across file systems */
2006 errno = EXDEV;
2007 TALLOC_FREE(frame);
2008 return -1;
2011 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2012 int eno = SMBC_errno(ocontext, targetcli1);
2014 if (eno != EEXIST ||
2015 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2016 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2018 errno = eno;
2019 TALLOC_FREE(frame);
2020 return -1;
2025 TALLOC_FREE(frame);
2026 return 0; /* Success */