s3: Wrap creating the svcctl keys in a transaction
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob9939e25fb54447f14f4209b1daf9d790bceb82d7
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&aDIR?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 ip_service *ip_list;
460 struct ip_service server_addr;
461 struct user_auth_info u_info;
463 if (share[0] != (char)0 || path[0] != (char)0) {
465 if (dir) {
466 SAFE_FREE(dir->fname);
467 SAFE_FREE(dir);
469 TALLOC_FREE(frame);
470 errno = EINVAL + 8196;
471 return NULL;
474 /* Determine how many local master browsers to query */
475 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
476 ? INT_MAX
477 : smbc_getOptionBrowseMaxLmbCount(context));
479 memset(&u_info, '\0', sizeof(u_info));
480 u_info.username = talloc_strdup(frame,user);
481 u_info.password = talloc_strdup(frame,password);
482 if (!u_info.username || !u_info.password) {
483 if (dir) {
484 SAFE_FREE(dir->fname);
485 SAFE_FREE(dir);
487 TALLOC_FREE(frame);
488 return NULL;
492 * We have server and share and path empty but options
493 * requesting that we scan all master browsers for their list
494 * of workgroups/domains. This implies that we must first try
495 * broadcast queries to find all master browsers, and if that
496 * doesn't work, then try our other methods which return only
497 * a single master browser.
500 ip_list = NULL;
501 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
502 &count)))
505 SAFE_FREE(ip_list);
507 if (!find_master_ip(workgroup, &server_addr.ss)) {
509 if (dir) {
510 SAFE_FREE(dir->fname);
511 SAFE_FREE(dir);
513 TALLOC_FREE(frame);
514 errno = ENOENT;
515 return NULL;
518 ip_list = (struct ip_service *)memdup(
519 &server_addr, sizeof(server_addr));
520 if (ip_list == NULL) {
521 TALLOC_FREE(frame);
522 errno = ENOMEM;
523 return NULL;
525 count = 1;
528 for (i = 0; i < count && i < max_lmb_count; i++) {
529 char addr[INET6_ADDRSTRLEN];
530 char *wg_ptr = NULL;
531 struct cli_state *cli = NULL;
533 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
534 DEBUG(99, ("Found master browser %d of %d: %s\n",
535 i+1, MAX(count, max_lmb_count),
536 addr));
538 cli = get_ipc_connect_master_ip(talloc_tos(),
539 &ip_list[i],
540 &u_info,
541 &wg_ptr);
542 /* cli == NULL is the master browser refused to talk or
543 could not be found */
544 if (!cli) {
545 continue;
548 workgroup = talloc_strdup(frame, wg_ptr);
549 server = talloc_strdup(frame, cli->desthost);
551 cli_shutdown(cli);
553 if (!workgroup || !server) {
554 if (dir) {
555 SAFE_FREE(dir->fname);
556 SAFE_FREE(dir);
558 TALLOC_FREE(frame);
559 errno = ENOMEM;
560 return NULL;
563 DEBUG(4, ("using workgroup %s %s\n",
564 workgroup, server));
567 * For each returned master browser IP address, get a
568 * connection to IPC$ on the server if we do not
569 * already have one, and determine the
570 * workgroups/domains that it knows about.
573 srv = SMBC_server(frame, context, True, server, "IPC$",
574 &workgroup, &user, &password);
575 if (!srv) {
576 continue;
579 dir->srv = srv;
580 dir->dir_type = SMBC_WORKGROUP;
582 /* Now, list the stuff ... */
584 if (!cli_NetServerEnum(srv->cli,
585 workgroup,
586 SV_TYPE_DOMAIN_ENUM,
587 list_unique_wg_fn,
588 (void *)dir)) {
589 continue;
593 SAFE_FREE(ip_list);
594 } else {
596 * Server not an empty string ... Check the rest and see what
597 * gives
599 if (*share == '\0') {
600 if (*path != '\0') {
602 /* Should not have empty share with path */
603 if (dir) {
604 SAFE_FREE(dir->fname);
605 SAFE_FREE(dir);
607 TALLOC_FREE(frame);
608 errno = EINVAL + 8197;
609 return NULL;
614 * We don't know if <server> is really a server name
615 * or is a workgroup/domain name. If we already have
616 * a server structure for it, we'll use it.
617 * Otherwise, check to see if <server><1D>,
618 * <server><1B>, or <server><20> translates. We check
619 * to see if <server> is an IP address first.
623 * See if we have an existing server. Do not
624 * establish a connection if one does not already
625 * exist.
627 srv = SMBC_server(frame, context, False,
628 server, "IPC$",
629 &workgroup, &user, &password);
632 * If no existing server and not an IP addr, look for
633 * LMB or DMB
635 if (!srv &&
636 !is_ipaddress(server) &&
637 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
638 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
640 * "server" is actually a workgroup name,
641 * not a server. Make this clear.
643 char *wgroup = server;
644 fstring buserver;
646 dir->dir_type = SMBC_SERVER;
649 * Get the backup list ...
651 if (!name_status_find(wgroup, 0, 0,
652 &rem_ss, buserver)) {
653 char addr[INET6_ADDRSTRLEN];
655 print_sockaddr(addr, sizeof(addr), &rem_ss);
656 DEBUG(0,("Could not get name of "
657 "local/domain master browser "
658 "for workgroup %s from "
659 "address %s\n",
660 wgroup,
661 addr));
662 if (dir) {
663 SAFE_FREE(dir->fname);
664 SAFE_FREE(dir);
666 TALLOC_FREE(frame);
667 errno = EPERM;
668 return NULL;
673 * Get a connection to IPC$ on the server if
674 * we do not already have one
676 srv = SMBC_server(frame, context, True,
677 buserver, "IPC$",
678 &workgroup,
679 &user, &password);
680 if (!srv) {
681 DEBUG(0, ("got no contact to IPC$\n"));
682 if (dir) {
683 SAFE_FREE(dir->fname);
684 SAFE_FREE(dir);
686 TALLOC_FREE(frame);
687 return NULL;
691 dir->srv = srv;
693 /* Now, list the servers ... */
694 if (!cli_NetServerEnum(srv->cli, wgroup,
695 0x0000FFFE, list_fn,
696 (void *)dir)) {
698 if (dir) {
699 SAFE_FREE(dir->fname);
700 SAFE_FREE(dir);
702 TALLOC_FREE(frame);
703 return NULL;
705 } else if (srv ||
706 (resolve_name(server, &rem_ss, 0x20, false))) {
709 * If we hadn't found the server, get one now
711 if (!srv) {
712 srv = SMBC_server(frame, context, True,
713 server, "IPC$",
714 &workgroup,
715 &user, &password);
718 if (!srv) {
719 if (dir) {
720 SAFE_FREE(dir->fname);
721 SAFE_FREE(dir);
723 TALLOC_FREE(frame);
724 return NULL;
728 dir->dir_type = SMBC_FILE_SHARE;
729 dir->srv = srv;
731 /* List the shares ... */
733 if (net_share_enum_rpc(
734 srv->cli,
735 list_fn,
736 (void *) dir) < 0 &&
737 cli_RNetShareEnum(
738 srv->cli,
739 list_fn,
740 (void *)dir) < 0) {
742 errno = cli_errno(srv->cli);
743 if (dir) {
744 SAFE_FREE(dir->fname);
745 SAFE_FREE(dir);
747 TALLOC_FREE(frame);
748 return NULL;
751 } else {
752 /* Neither the workgroup nor server exists */
753 errno = ECONNREFUSED;
754 if (dir) {
755 SAFE_FREE(dir->fname);
756 SAFE_FREE(dir);
758 TALLOC_FREE(frame);
759 return NULL;
763 else {
765 * The server and share are specified ... work from
766 * there ...
768 char *targetpath;
769 struct cli_state *targetcli;
770 NTSTATUS status;
772 /* We connect to the server and list the directory */
773 dir->dir_type = SMBC_FILE_SHARE;
775 srv = SMBC_server(frame, context, True, server, share,
776 &workgroup, &user, &password);
778 if (!srv) {
779 if (dir) {
780 SAFE_FREE(dir->fname);
781 SAFE_FREE(dir);
783 TALLOC_FREE(frame);
784 return NULL;
787 dir->srv = srv;
789 /* Now, list the files ... */
791 p = path + strlen(path);
792 path = talloc_asprintf_append(path, "\\*");
793 if (!path) {
794 if (dir) {
795 SAFE_FREE(dir->fname);
796 SAFE_FREE(dir);
798 TALLOC_FREE(frame);
799 return NULL;
802 if (!cli_resolve_path(frame, "", context->internal->auth_info,
803 srv->cli, path,
804 &targetcli, &targetpath)) {
805 d_printf("Could not resolve %s\n", path);
806 if (dir) {
807 SAFE_FREE(dir->fname);
808 SAFE_FREE(dir);
810 TALLOC_FREE(frame);
811 return NULL;
814 status = cli_list(targetcli, targetpath,
815 aDIR | aSYSTEM | aHIDDEN,
816 dir_list_fn, (void *)dir);
817 if (!NT_STATUS_IS_OK(status)) {
818 if (dir) {
819 SAFE_FREE(dir->fname);
820 SAFE_FREE(dir);
822 saved_errno = SMBC_errno(context, targetcli);
824 if (saved_errno == EINVAL) {
826 * See if they asked to opendir
827 * something other than a directory.
828 * If so, the converted error value we
829 * got would have been EINVAL rather
830 * than ENOTDIR.
832 *p = '\0'; /* restore original path */
834 if (SMBC_getatr(context, srv, path,
835 &mode, NULL,
836 NULL, NULL, NULL, NULL,
837 NULL) &&
838 ! IS_DOS_DIR(mode)) {
840 /* It is. Correct the error value */
841 saved_errno = ENOTDIR;
846 * If there was an error and the server is no
847 * good any more...
849 if (cli_is_error(targetcli) &&
850 smbc_getFunctionCheckServer(context)(context, srv)) {
852 /* ... then remove it. */
853 if (smbc_getFunctionRemoveUnusedServer(context)(context,
854 srv)) {
856 * We could not remove the
857 * server completely, remove
858 * it from the cache so we
859 * will not get it again. It
860 * will be removed when the
861 * last file/dir is closed.
863 smbc_getFunctionRemoveCachedServer(context)(context, srv);
867 TALLOC_FREE(frame);
868 errno = saved_errno;
869 return NULL;
875 DLIST_ADD(context->internal->files, dir);
876 TALLOC_FREE(frame);
877 return dir;
882 * Routine to close a directory
886 SMBC_closedir_ctx(SMBCCTX *context,
887 SMBCFILE *dir)
889 TALLOC_CTX *frame = talloc_stackframe();
891 if (!context || !context->internal->initialized) {
892 errno = EINVAL;
893 TALLOC_FREE(frame);
894 return -1;
897 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
898 errno = EBADF;
899 TALLOC_FREE(frame);
900 return -1;
903 remove_dir(dir); /* Clean it up */
905 DLIST_REMOVE(context->internal->files, dir);
907 if (dir) {
909 SAFE_FREE(dir->fname);
910 SAFE_FREE(dir); /* Free the space too */
913 TALLOC_FREE(frame);
914 return 0;
918 static void
919 smbc_readdir_internal(SMBCCTX * context,
920 struct smbc_dirent *dest,
921 struct smbc_dirent *src,
922 int max_namebuf_len)
924 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
926 /* url-encode the name. get back remaining buffer space */
927 max_namebuf_len =
928 smbc_urlencode(dest->name, src->name, max_namebuf_len);
930 /* We now know the name length */
931 dest->namelen = strlen(dest->name);
933 /* Save the pointer to the beginning of the comment */
934 dest->comment = dest->name + dest->namelen + 1;
936 /* Copy the comment */
937 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
938 dest->comment[max_namebuf_len - 1] = '\0';
940 /* Save other fields */
941 dest->smbc_type = src->smbc_type;
942 dest->commentlen = strlen(dest->comment);
943 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
944 (char *) dest);
945 } else {
947 /* No encoding. Just copy the entry as is. */
948 memcpy(dest, src, src->dirlen);
949 dest->comment = (char *)(&dest->name + src->namelen + 1);
955 * Routine to get a directory entry
958 struct smbc_dirent *
959 SMBC_readdir_ctx(SMBCCTX *context,
960 SMBCFILE *dir)
962 int maxlen;
963 struct smbc_dirent *dirp, *dirent;
964 TALLOC_CTX *frame = talloc_stackframe();
966 /* Check that all is ok first ... */
968 if (!context || !context->internal->initialized) {
970 errno = EINVAL;
971 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
972 TALLOC_FREE(frame);
973 return NULL;
977 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
979 errno = EBADF;
980 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
981 TALLOC_FREE(frame);
982 return NULL;
986 if (dir->file != False) { /* FIXME, should be dir, perhaps */
988 errno = ENOTDIR;
989 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
990 TALLOC_FREE(frame);
991 return NULL;
995 if (!dir->dir_next) {
996 TALLOC_FREE(frame);
997 return NULL;
1000 dirent = dir->dir_next->dirent;
1001 if (!dirent) {
1003 errno = ENOENT;
1004 TALLOC_FREE(frame);
1005 return NULL;
1009 dirp = &context->internal->dirent;
1010 maxlen = sizeof(context->internal->_dirent_name);
1012 smbc_readdir_internal(context, dirp, dirent, maxlen);
1014 dir->dir_next = dir->dir_next->next;
1016 TALLOC_FREE(frame);
1017 return dirp;
1021 * Routine to get directory entries
1025 SMBC_getdents_ctx(SMBCCTX *context,
1026 SMBCFILE *dir,
1027 struct smbc_dirent *dirp,
1028 int count)
1030 int rem = count;
1031 int reqd;
1032 int maxlen;
1033 char *ndir = (char *)dirp;
1034 struct smbc_dir_list *dirlist;
1035 TALLOC_CTX *frame = talloc_stackframe();
1037 /* Check that all is ok first ... */
1039 if (!context || !context->internal->initialized) {
1041 errno = EINVAL;
1042 TALLOC_FREE(frame);
1043 return -1;
1047 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1049 errno = EBADF;
1050 TALLOC_FREE(frame);
1051 return -1;
1055 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1057 errno = ENOTDIR;
1058 TALLOC_FREE(frame);
1059 return -1;
1064 * Now, retrieve the number of entries that will fit in what was passed
1065 * We have to figure out if the info is in the list, or we need to
1066 * send a request to the server to get the info.
1069 while ((dirlist = dir->dir_next)) {
1070 struct smbc_dirent *dirent;
1071 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1073 if (!dirlist->dirent) {
1075 errno = ENOENT; /* Bad error */
1076 TALLOC_FREE(frame);
1077 return -1;
1081 /* Do urlencoding of next entry, if so selected */
1082 dirent = &context->internal->dirent;
1083 maxlen = sizeof(context->internal->_dirent_name);
1084 smbc_readdir_internal(context, dirent,
1085 dirlist->dirent, maxlen);
1087 reqd = dirent->dirlen;
1089 if (rem < reqd) {
1091 if (rem < count) { /* We managed to copy something */
1093 errno = 0;
1094 TALLOC_FREE(frame);
1095 return count - rem;
1098 else { /* Nothing copied ... */
1100 errno = EINVAL; /* Not enough space ... */
1101 TALLOC_FREE(frame);
1102 return -1;
1108 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1110 currentEntry->comment = &currentEntry->name[0] +
1111 dirent->namelen + 1;
1113 ndir += reqd;
1114 rem -= reqd;
1116 /* Try and align the struct for the next entry
1117 on a valid pointer boundary by appending zeros */
1118 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1119 *ndir = '\0';
1120 rem--;
1121 ndir++;
1122 currentEntry->dirlen++;
1125 dir->dir_next = dirlist = dirlist -> next;
1128 TALLOC_FREE(frame);
1130 if (rem == count)
1131 return 0;
1132 else
1133 return count - rem;
1138 * Routine to create a directory ...
1142 SMBC_mkdir_ctx(SMBCCTX *context,
1143 const char *fname,
1144 mode_t mode)
1146 SMBCSRV *srv = NULL;
1147 char *server = NULL;
1148 char *share = NULL;
1149 char *user = NULL;
1150 char *password = NULL;
1151 char *workgroup = NULL;
1152 char *path = NULL;
1153 char *targetpath = NULL;
1154 struct cli_state *targetcli = NULL;
1155 TALLOC_CTX *frame = talloc_stackframe();
1157 if (!context || !context->internal->initialized) {
1158 errno = EINVAL;
1159 TALLOC_FREE(frame);
1160 return -1;
1163 if (!fname) {
1164 errno = EINVAL;
1165 TALLOC_FREE(frame);
1166 return -1;
1169 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1171 if (SMBC_parse_path(frame,
1172 context,
1173 fname,
1174 &workgroup,
1175 &server,
1176 &share,
1177 &path,
1178 &user,
1179 &password,
1180 NULL)) {
1181 errno = EINVAL;
1182 TALLOC_FREE(frame);
1183 return -1;
1186 if (!user || user[0] == (char)0) {
1187 user = talloc_strdup(frame, smbc_getUser(context));
1188 if (!user) {
1189 errno = ENOMEM;
1190 TALLOC_FREE(frame);
1191 return -1;
1195 srv = SMBC_server(frame, context, True,
1196 server, share, &workgroup, &user, &password);
1198 if (!srv) {
1200 TALLOC_FREE(frame);
1201 return -1; /* errno set by SMBC_server */
1205 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1206 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1207 srv->cli, path,
1208 &targetcli, &targetpath)) {
1209 d_printf("Could not resolve %s\n", path);
1210 errno = ENOENT;
1211 TALLOC_FREE(frame);
1212 return -1;
1214 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1216 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1217 errno = SMBC_errno(context, targetcli);
1218 TALLOC_FREE(frame);
1219 return -1;
1223 TALLOC_FREE(frame);
1224 return 0;
1229 * Our list function simply checks to see if a directory is not empty
1232 static NTSTATUS
1233 rmdir_list_fn(const char *mnt,
1234 struct file_info *finfo,
1235 const char *mask,
1236 void *state)
1238 if (strncmp(finfo->name, ".", 1) != 0 &&
1239 strncmp(finfo->name, "..", 2) != 0) {
1240 bool *smbc_rmdir_dirempty = (bool *)state;
1241 *smbc_rmdir_dirempty = false;
1243 return NT_STATUS_OK;
1247 * Routine to remove a directory
1251 SMBC_rmdir_ctx(SMBCCTX *context,
1252 const char *fname)
1254 SMBCSRV *srv = NULL;
1255 char *server = NULL;
1256 char *share = NULL;
1257 char *user = NULL;
1258 char *password = NULL;
1259 char *workgroup = NULL;
1260 char *path = NULL;
1261 char *targetpath = NULL;
1262 struct cli_state *targetcli = NULL;
1263 TALLOC_CTX *frame = talloc_stackframe();
1265 if (!context || !context->internal->initialized) {
1266 errno = EINVAL;
1267 TALLOC_FREE(frame);
1268 return -1;
1271 if (!fname) {
1272 errno = EINVAL;
1273 TALLOC_FREE(frame);
1274 return -1;
1277 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1279 if (SMBC_parse_path(frame,
1280 context,
1281 fname,
1282 &workgroup,
1283 &server,
1284 &share,
1285 &path,
1286 &user,
1287 &password,
1288 NULL)) {
1289 errno = EINVAL;
1290 TALLOC_FREE(frame);
1291 return -1;
1294 if (!user || user[0] == (char)0) {
1295 user = talloc_strdup(frame, smbc_getUser(context));
1296 if (!user) {
1297 errno = ENOMEM;
1298 TALLOC_FREE(frame);
1299 return -1;
1303 srv = SMBC_server(frame, context, True,
1304 server, share, &workgroup, &user, &password);
1306 if (!srv) {
1308 TALLOC_FREE(frame);
1309 return -1; /* errno set by SMBC_server */
1313 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1314 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1315 srv->cli, path,
1316 &targetcli, &targetpath)) {
1317 d_printf("Could not resolve %s\n", path);
1318 errno = ENOENT;
1319 TALLOC_FREE(frame);
1320 return -1;
1322 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1324 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1326 errno = SMBC_errno(context, targetcli);
1328 if (errno == EACCES) { /* Check if the dir empty or not */
1330 /* Local storage to avoid buffer overflows */
1331 char *lpath;
1332 bool smbc_rmdir_dirempty = true;
1333 NTSTATUS status;
1335 lpath = talloc_asprintf(frame, "%s\\*",
1336 targetpath);
1337 if (!lpath) {
1338 errno = ENOMEM;
1339 TALLOC_FREE(frame);
1340 return -1;
1343 status = cli_list(targetcli, lpath,
1344 aDIR | aSYSTEM | aHIDDEN,
1345 rmdir_list_fn,
1346 &smbc_rmdir_dirempty);
1348 if (!NT_STATUS_IS_OK(status)) {
1349 /* Fix errno to ignore latest error ... */
1350 DEBUG(5, ("smbc_rmdir: "
1351 "cli_list returned an error: %d\n",
1352 SMBC_errno(context, targetcli)));
1353 errno = EACCES;
1357 if (smbc_rmdir_dirempty)
1358 errno = EACCES;
1359 else
1360 errno = ENOTEMPTY;
1364 TALLOC_FREE(frame);
1365 return -1;
1369 TALLOC_FREE(frame);
1370 return 0;
1375 * Routine to return the current directory position
1378 off_t
1379 SMBC_telldir_ctx(SMBCCTX *context,
1380 SMBCFILE *dir)
1382 TALLOC_CTX *frame = talloc_stackframe();
1384 if (!context || !context->internal->initialized) {
1386 errno = EINVAL;
1387 TALLOC_FREE(frame);
1388 return -1;
1392 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1394 errno = EBADF;
1395 TALLOC_FREE(frame);
1396 return -1;
1400 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1402 errno = ENOTDIR;
1403 TALLOC_FREE(frame);
1404 return -1;
1408 /* See if we're already at the end. */
1409 if (dir->dir_next == NULL) {
1410 /* We are. */
1411 TALLOC_FREE(frame);
1412 return -1;
1416 * We return the pointer here as the offset
1418 TALLOC_FREE(frame);
1419 return (off_t)(long)dir->dir_next->dirent;
1423 * A routine to run down the list and see if the entry is OK
1426 static struct smbc_dir_list *
1427 check_dir_ent(struct smbc_dir_list *list,
1428 struct smbc_dirent *dirent)
1431 /* Run down the list looking for what we want */
1433 if (dirent) {
1435 struct smbc_dir_list *tmp = list;
1437 while (tmp) {
1439 if (tmp->dirent == dirent)
1440 return tmp;
1442 tmp = tmp->next;
1448 return NULL; /* Not found, or an error */
1454 * Routine to seek on a directory
1458 SMBC_lseekdir_ctx(SMBCCTX *context,
1459 SMBCFILE *dir,
1460 off_t offset)
1462 long int l_offset = offset; /* Handle problems of size */
1463 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1464 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1465 TALLOC_CTX *frame = talloc_stackframe();
1467 if (!context || !context->internal->initialized) {
1469 errno = EINVAL;
1470 TALLOC_FREE(frame);
1471 return -1;
1475 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1477 errno = ENOTDIR;
1478 TALLOC_FREE(frame);
1479 return -1;
1483 /* Now, check what we were passed and see if it is OK ... */
1485 if (dirent == NULL) { /* Seek to the begining of the list */
1487 dir->dir_next = dir->dir_list;
1488 TALLOC_FREE(frame);
1489 return 0;
1493 if (offset == -1) { /* Seek to the end of the list */
1494 dir->dir_next = NULL;
1495 TALLOC_FREE(frame);
1496 return 0;
1499 /* Now, run down the list and make sure that the entry is OK */
1500 /* This may need to be changed if we change the format of the list */
1502 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1503 errno = EINVAL; /* Bad entry */
1504 TALLOC_FREE(frame);
1505 return -1;
1508 dir->dir_next = list_ent;
1510 TALLOC_FREE(frame);
1511 return 0;
1515 * Routine to fstat a dir
1519 SMBC_fstatdir_ctx(SMBCCTX *context,
1520 SMBCFILE *dir,
1521 struct stat *st)
1524 if (!context || !context->internal->initialized) {
1526 errno = EINVAL;
1527 return -1;
1530 /* No code yet ... */
1531 return 0;
1535 SMBC_chmod_ctx(SMBCCTX *context,
1536 const char *fname,
1537 mode_t newmode)
1539 SMBCSRV *srv = NULL;
1540 char *server = NULL;
1541 char *share = NULL;
1542 char *user = NULL;
1543 char *password = NULL;
1544 char *workgroup = NULL;
1545 char *targetpath = NULL;
1546 struct cli_state *targetcli = NULL;
1547 char *path = NULL;
1548 uint16 mode;
1549 TALLOC_CTX *frame = talloc_stackframe();
1551 if (!context || !context->internal->initialized) {
1553 errno = EINVAL; /* Best I can think of ... */
1554 TALLOC_FREE(frame);
1555 return -1;
1558 if (!fname) {
1559 errno = EINVAL;
1560 TALLOC_FREE(frame);
1561 return -1;
1564 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1566 if (SMBC_parse_path(frame,
1567 context,
1568 fname,
1569 &workgroup,
1570 &server,
1571 &share,
1572 &path,
1573 &user,
1574 &password,
1575 NULL)) {
1576 errno = EINVAL;
1577 TALLOC_FREE(frame);
1578 return -1;
1581 if (!user || user[0] == (char)0) {
1582 user = talloc_strdup(frame, smbc_getUser(context));
1583 if (!user) {
1584 errno = ENOMEM;
1585 TALLOC_FREE(frame);
1586 return -1;
1590 srv = SMBC_server(frame, context, True,
1591 server, share, &workgroup, &user, &password);
1593 if (!srv) {
1594 TALLOC_FREE(frame);
1595 return -1; /* errno set by SMBC_server */
1598 /*d_printf(">>>unlink: resolving %s\n", path);*/
1599 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1600 srv->cli, path,
1601 &targetcli, &targetpath)) {
1602 d_printf("Could not resolve %s\n", path);
1603 errno = ENOENT;
1604 TALLOC_FREE(frame);
1605 return -1;
1608 mode = 0;
1610 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1611 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1612 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1613 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1615 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1616 errno = SMBC_errno(context, targetcli);
1617 TALLOC_FREE(frame);
1618 return -1;
1621 TALLOC_FREE(frame);
1622 return 0;
1626 SMBC_utimes_ctx(SMBCCTX *context,
1627 const char *fname,
1628 struct timeval *tbuf)
1630 SMBCSRV *srv = NULL;
1631 char *server = NULL;
1632 char *share = NULL;
1633 char *user = NULL;
1634 char *password = NULL;
1635 char *workgroup = NULL;
1636 char *path = NULL;
1637 time_t access_time;
1638 time_t write_time;
1639 TALLOC_CTX *frame = talloc_stackframe();
1641 if (!context || !context->internal->initialized) {
1643 errno = EINVAL; /* Best I can think of ... */
1644 TALLOC_FREE(frame);
1645 return -1;
1648 if (!fname) {
1649 errno = EINVAL;
1650 TALLOC_FREE(frame);
1651 return -1;
1654 if (tbuf == NULL) {
1655 access_time = write_time = time(NULL);
1656 } else {
1657 access_time = tbuf[0].tv_sec;
1658 write_time = tbuf[1].tv_sec;
1661 if (DEBUGLVL(4)) {
1662 char *p;
1663 char atimebuf[32];
1664 char mtimebuf[32];
1666 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1667 atimebuf[sizeof(atimebuf) - 1] = '\0';
1668 if ((p = strchr(atimebuf, '\n')) != NULL) {
1669 *p = '\0';
1672 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1673 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1674 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1675 *p = '\0';
1678 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1679 fname, atimebuf, mtimebuf);
1682 if (SMBC_parse_path(frame,
1683 context,
1684 fname,
1685 &workgroup,
1686 &server,
1687 &share,
1688 &path,
1689 &user,
1690 &password,
1691 NULL)) {
1692 errno = EINVAL;
1693 TALLOC_FREE(frame);
1694 return -1;
1697 if (!user || user[0] == (char)0) {
1698 user = talloc_strdup(frame, smbc_getUser(context));
1699 if (!user) {
1700 errno = ENOMEM;
1701 TALLOC_FREE(frame);
1702 return -1;
1706 srv = SMBC_server(frame, context, True,
1707 server, share, &workgroup, &user, &password);
1709 if (!srv) {
1710 TALLOC_FREE(frame);
1711 return -1; /* errno set by SMBC_server */
1714 if (!SMBC_setatr(context, srv, path,
1715 0, access_time, write_time, 0, 0)) {
1716 TALLOC_FREE(frame);
1717 return -1; /* errno set by SMBC_setatr */
1720 TALLOC_FREE(frame);
1721 return 0;
1725 * Routine to unlink() a file
1729 SMBC_unlink_ctx(SMBCCTX *context,
1730 const char *fname)
1732 char *server = NULL;
1733 char *share = NULL;
1734 char *user = NULL;
1735 char *password = NULL;
1736 char *workgroup = NULL;
1737 char *path = NULL;
1738 char *targetpath = NULL;
1739 struct cli_state *targetcli = NULL;
1740 SMBCSRV *srv = NULL;
1741 TALLOC_CTX *frame = talloc_stackframe();
1743 if (!context || !context->internal->initialized) {
1745 errno = EINVAL; /* Best I can think of ... */
1746 TALLOC_FREE(frame);
1747 return -1;
1751 if (!fname) {
1752 errno = EINVAL;
1753 TALLOC_FREE(frame);
1754 return -1;
1758 if (SMBC_parse_path(frame,
1759 context,
1760 fname,
1761 &workgroup,
1762 &server,
1763 &share,
1764 &path,
1765 &user,
1766 &password,
1767 NULL)) {
1768 errno = EINVAL;
1769 TALLOC_FREE(frame);
1770 return -1;
1773 if (!user || user[0] == (char)0) {
1774 user = talloc_strdup(frame, smbc_getUser(context));
1775 if (!user) {
1776 errno = ENOMEM;
1777 TALLOC_FREE(frame);
1778 return -1;
1782 srv = SMBC_server(frame, context, True,
1783 server, share, &workgroup, &user, &password);
1785 if (!srv) {
1786 TALLOC_FREE(frame);
1787 return -1; /* SMBC_server sets errno */
1791 /*d_printf(">>>unlink: resolving %s\n", path);*/
1792 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1793 srv->cli, path,
1794 &targetcli, &targetpath)) {
1795 d_printf("Could not resolve %s\n", path);
1796 errno = ENOENT;
1797 TALLOC_FREE(frame);
1798 return -1;
1800 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1802 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, aSYSTEM | aHIDDEN))) {
1804 errno = SMBC_errno(context, targetcli);
1806 if (errno == EACCES) { /* Check if the file is a directory */
1808 int saverr = errno;
1809 SMB_OFF_T size = 0;
1810 uint16 mode = 0;
1811 struct timespec write_time_ts;
1812 struct timespec access_time_ts;
1813 struct timespec change_time_ts;
1814 SMB_INO_T ino = 0;
1816 if (!SMBC_getatr(context, srv, path, &mode, &size,
1817 NULL,
1818 &access_time_ts,
1819 &write_time_ts,
1820 &change_time_ts,
1821 &ino)) {
1823 /* Hmmm, bad error ... What? */
1825 errno = SMBC_errno(context, targetcli);
1826 TALLOC_FREE(frame);
1827 return -1;
1830 else {
1832 if (IS_DOS_DIR(mode))
1833 errno = EISDIR;
1834 else
1835 errno = saverr; /* Restore this */
1840 TALLOC_FREE(frame);
1841 return -1;
1845 TALLOC_FREE(frame);
1846 return 0; /* Success ... */
1851 * Routine to rename() a file
1855 SMBC_rename_ctx(SMBCCTX *ocontext,
1856 const char *oname,
1857 SMBCCTX *ncontext,
1858 const char *nname)
1860 char *server1 = NULL;
1861 char *share1 = NULL;
1862 char *server2 = NULL;
1863 char *share2 = NULL;
1864 char *user1 = NULL;
1865 char *user2 = NULL;
1866 char *password1 = NULL;
1867 char *password2 = NULL;
1868 char *workgroup = NULL;
1869 char *path1 = NULL;
1870 char *path2 = NULL;
1871 char *targetpath1 = NULL;
1872 char *targetpath2 = NULL;
1873 struct cli_state *targetcli1 = NULL;
1874 struct cli_state *targetcli2 = NULL;
1875 SMBCSRV *srv = NULL;
1876 TALLOC_CTX *frame = talloc_stackframe();
1878 if (!ocontext || !ncontext ||
1879 !ocontext->internal->initialized ||
1880 !ncontext->internal->initialized) {
1882 errno = EINVAL; /* Best I can think of ... */
1883 TALLOC_FREE(frame);
1884 return -1;
1887 if (!oname || !nname) {
1888 errno = EINVAL;
1889 TALLOC_FREE(frame);
1890 return -1;
1893 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1895 if (SMBC_parse_path(frame,
1896 ocontext,
1897 oname,
1898 &workgroup,
1899 &server1,
1900 &share1,
1901 &path1,
1902 &user1,
1903 &password1,
1904 NULL)) {
1905 errno = EINVAL;
1906 TALLOC_FREE(frame);
1907 return -1;
1910 if (!user1 || user1[0] == (char)0) {
1911 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1912 if (!user1) {
1913 errno = ENOMEM;
1914 TALLOC_FREE(frame);
1915 return -1;
1919 if (SMBC_parse_path(frame,
1920 ncontext,
1921 nname,
1922 NULL,
1923 &server2,
1924 &share2,
1925 &path2,
1926 &user2,
1927 &password2,
1928 NULL)) {
1929 errno = EINVAL;
1930 TALLOC_FREE(frame);
1931 return -1;
1934 if (!user2 || user2[0] == (char)0) {
1935 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1936 if (!user2) {
1937 errno = ENOMEM;
1938 TALLOC_FREE(frame);
1939 return -1;
1943 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1944 strcmp(user1, user2)) {
1945 /* Can't rename across file systems, or users?? */
1946 errno = EXDEV;
1947 TALLOC_FREE(frame);
1948 return -1;
1951 srv = SMBC_server(frame, ocontext, True,
1952 server1, share1, &workgroup, &user1, &password1);
1953 if (!srv) {
1954 TALLOC_FREE(frame);
1955 return -1;
1959 /* set the credentials to make DFS work */
1960 smbc_set_credentials_with_fallback(ocontext,
1961 workgroup,
1962 user1,
1963 password1);
1965 /*d_printf(">>>rename: resolving %s\n", path1);*/
1966 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1967 srv->cli,
1968 path1,
1969 &targetcli1, &targetpath1)) {
1970 d_printf("Could not resolve %s\n", path1);
1971 errno = ENOENT;
1972 TALLOC_FREE(frame);
1973 return -1;
1976 /* set the credentials to make DFS work */
1977 smbc_set_credentials_with_fallback(ncontext,
1978 workgroup,
1979 user2,
1980 password2);
1982 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1983 /*d_printf(">>>rename: resolving %s\n", path2);*/
1984 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1985 srv->cli,
1986 path2,
1987 &targetcli2, &targetpath2)) {
1988 d_printf("Could not resolve %s\n", path2);
1989 errno = ENOENT;
1990 TALLOC_FREE(frame);
1991 return -1;
1993 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1995 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1996 strcmp(targetcli1->share, targetcli2->share))
1998 /* can't rename across file systems */
1999 errno = EXDEV;
2000 TALLOC_FREE(frame);
2001 return -1;
2004 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2005 int eno = SMBC_errno(ocontext, targetcli1);
2007 if (eno != EEXIST ||
2008 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, aSYSTEM | aHIDDEN)) ||
2009 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2011 errno = eno;
2012 TALLOC_FREE(frame);
2013 return -1;
2018 TALLOC_FREE(frame);
2019 return 0; /* Success */