s3: Remove ip_sevice from get_ipc_connect_master_ip
[Samba.git] / source3 / libsmb / libsmb_dir.c
blobb0f3c904b4d5c47e8a90ed37eafca35631ce2b41
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 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 if (dir) {
522 SAFE_FREE(dir->fname);
523 SAFE_FREE(dir);
525 TALLOC_FREE(frame);
526 errno = ENOMEM;
527 return NULL;
529 count = 1;
532 for (i = 0; i < count && i < max_lmb_count; i++) {
533 char addr[INET6_ADDRSTRLEN];
534 char *wg_ptr = NULL;
535 struct cli_state *cli = NULL;
537 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
538 DEBUG(99, ("Found master browser %d of %d: %s\n",
539 i+1, MAX(count, max_lmb_count),
540 addr));
542 cli = get_ipc_connect_master_ip(talloc_tos(),
543 &ip_list[i].ss,
544 &u_info,
545 &wg_ptr);
546 /* cli == NULL is the master browser refused to talk or
547 could not be found */
548 if (!cli) {
549 continue;
552 workgroup = talloc_strdup(frame, wg_ptr);
553 server = talloc_strdup(frame, cli->desthost);
555 cli_shutdown(cli);
557 if (!workgroup || !server) {
558 if (dir) {
559 SAFE_FREE(dir->fname);
560 SAFE_FREE(dir);
562 TALLOC_FREE(frame);
563 errno = ENOMEM;
564 return NULL;
567 DEBUG(4, ("using workgroup %s %s\n",
568 workgroup, server));
571 * For each returned master browser IP address, get a
572 * connection to IPC$ on the server if we do not
573 * already have one, and determine the
574 * workgroups/domains that it knows about.
577 srv = SMBC_server(frame, context, True, server, "IPC$",
578 &workgroup, &user, &password);
579 if (!srv) {
580 continue;
583 dir->srv = srv;
584 dir->dir_type = SMBC_WORKGROUP;
586 /* Now, list the stuff ... */
588 if (!cli_NetServerEnum(srv->cli,
589 workgroup,
590 SV_TYPE_DOMAIN_ENUM,
591 list_unique_wg_fn,
592 (void *)dir)) {
593 continue;
597 SAFE_FREE(ip_list);
598 } else {
600 * Server not an empty string ... Check the rest and see what
601 * gives
603 if (*share == '\0') {
604 if (*path != '\0') {
606 /* Should not have empty share with path */
607 if (dir) {
608 SAFE_FREE(dir->fname);
609 SAFE_FREE(dir);
611 TALLOC_FREE(frame);
612 errno = EINVAL + 8197;
613 return NULL;
618 * We don't know if <server> is really a server name
619 * or is a workgroup/domain name. If we already have
620 * a server structure for it, we'll use it.
621 * Otherwise, check to see if <server><1D>,
622 * <server><1B>, or <server><20> translates. We check
623 * to see if <server> is an IP address first.
627 * See if we have an existing server. Do not
628 * establish a connection if one does not already
629 * exist.
631 srv = SMBC_server(frame, context, False,
632 server, "IPC$",
633 &workgroup, &user, &password);
636 * If no existing server and not an IP addr, look for
637 * LMB or DMB
639 if (!srv &&
640 !is_ipaddress(server) &&
641 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
642 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
644 * "server" is actually a workgroup name,
645 * not a server. Make this clear.
647 char *wgroup = server;
648 fstring buserver;
650 dir->dir_type = SMBC_SERVER;
653 * Get the backup list ...
655 if (!name_status_find(wgroup, 0, 0,
656 &rem_ss, buserver)) {
657 char addr[INET6_ADDRSTRLEN];
659 print_sockaddr(addr, sizeof(addr), &rem_ss);
660 DEBUG(0,("Could not get name of "
661 "local/domain master browser "
662 "for workgroup %s from "
663 "address %s\n",
664 wgroup,
665 addr));
666 if (dir) {
667 SAFE_FREE(dir->fname);
668 SAFE_FREE(dir);
670 TALLOC_FREE(frame);
671 errno = EPERM;
672 return NULL;
677 * Get a connection to IPC$ on the server if
678 * we do not already have one
680 srv = SMBC_server(frame, context, True,
681 buserver, "IPC$",
682 &workgroup,
683 &user, &password);
684 if (!srv) {
685 DEBUG(0, ("got no contact to IPC$\n"));
686 if (dir) {
687 SAFE_FREE(dir->fname);
688 SAFE_FREE(dir);
690 TALLOC_FREE(frame);
691 return NULL;
695 dir->srv = srv;
697 /* Now, list the servers ... */
698 if (!cli_NetServerEnum(srv->cli, wgroup,
699 0x0000FFFE, list_fn,
700 (void *)dir)) {
702 if (dir) {
703 SAFE_FREE(dir->fname);
704 SAFE_FREE(dir);
706 TALLOC_FREE(frame);
707 return NULL;
709 } else if (srv ||
710 (resolve_name(server, &rem_ss, 0x20, false))) {
713 * If we hadn't found the server, get one now
715 if (!srv) {
716 srv = SMBC_server(frame, context, True,
717 server, "IPC$",
718 &workgroup,
719 &user, &password);
722 if (!srv) {
723 if (dir) {
724 SAFE_FREE(dir->fname);
725 SAFE_FREE(dir);
727 TALLOC_FREE(frame);
728 return NULL;
732 dir->dir_type = SMBC_FILE_SHARE;
733 dir->srv = srv;
735 /* List the shares ... */
737 if (net_share_enum_rpc(
738 srv->cli,
739 list_fn,
740 (void *) dir) < 0 &&
741 cli_RNetShareEnum(
742 srv->cli,
743 list_fn,
744 (void *)dir) < 0) {
746 errno = cli_errno(srv->cli);
747 if (dir) {
748 SAFE_FREE(dir->fname);
749 SAFE_FREE(dir);
751 TALLOC_FREE(frame);
752 return NULL;
755 } else {
756 /* Neither the workgroup nor server exists */
757 errno = ECONNREFUSED;
758 if (dir) {
759 SAFE_FREE(dir->fname);
760 SAFE_FREE(dir);
762 TALLOC_FREE(frame);
763 return NULL;
767 else {
769 * The server and share are specified ... work from
770 * there ...
772 char *targetpath;
773 struct cli_state *targetcli;
774 NTSTATUS status;
776 /* We connect to the server and list the directory */
777 dir->dir_type = SMBC_FILE_SHARE;
779 srv = SMBC_server(frame, context, True, server, share,
780 &workgroup, &user, &password);
782 if (!srv) {
783 if (dir) {
784 SAFE_FREE(dir->fname);
785 SAFE_FREE(dir);
787 TALLOC_FREE(frame);
788 return NULL;
791 dir->srv = srv;
793 /* Now, list the files ... */
795 p = path + strlen(path);
796 path = talloc_asprintf_append(path, "\\*");
797 if (!path) {
798 if (dir) {
799 SAFE_FREE(dir->fname);
800 SAFE_FREE(dir);
802 TALLOC_FREE(frame);
803 return NULL;
806 if (!cli_resolve_path(frame, "", context->internal->auth_info,
807 srv->cli, path,
808 &targetcli, &targetpath)) {
809 d_printf("Could not resolve %s\n", path);
810 if (dir) {
811 SAFE_FREE(dir->fname);
812 SAFE_FREE(dir);
814 TALLOC_FREE(frame);
815 return NULL;
818 status = cli_list(targetcli, targetpath,
819 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
820 dir_list_fn, (void *)dir);
821 if (!NT_STATUS_IS_OK(status)) {
822 if (dir) {
823 SAFE_FREE(dir->fname);
824 SAFE_FREE(dir);
826 saved_errno = SMBC_errno(context, targetcli);
828 if (saved_errno == EINVAL) {
830 * See if they asked to opendir
831 * something other than a directory.
832 * If so, the converted error value we
833 * got would have been EINVAL rather
834 * than ENOTDIR.
836 *p = '\0'; /* restore original path */
838 if (SMBC_getatr(context, srv, path,
839 &mode, NULL,
840 NULL, NULL, NULL, NULL,
841 NULL) &&
842 ! IS_DOS_DIR(mode)) {
844 /* It is. Correct the error value */
845 saved_errno = ENOTDIR;
850 * If there was an error and the server is no
851 * good any more...
853 if (cli_is_error(targetcli) &&
854 smbc_getFunctionCheckServer(context)(context, srv)) {
856 /* ... then remove it. */
857 if (smbc_getFunctionRemoveUnusedServer(context)(context,
858 srv)) {
860 * We could not remove the
861 * server completely, remove
862 * it from the cache so we
863 * will not get it again. It
864 * will be removed when the
865 * last file/dir is closed.
867 smbc_getFunctionRemoveCachedServer(context)(context, srv);
871 TALLOC_FREE(frame);
872 errno = saved_errno;
873 return NULL;
879 DLIST_ADD(context->internal->files, dir);
880 TALLOC_FREE(frame);
881 return dir;
886 * Routine to close a directory
890 SMBC_closedir_ctx(SMBCCTX *context,
891 SMBCFILE *dir)
893 TALLOC_CTX *frame = talloc_stackframe();
895 if (!context || !context->internal->initialized) {
896 errno = EINVAL;
897 TALLOC_FREE(frame);
898 return -1;
901 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
902 errno = EBADF;
903 TALLOC_FREE(frame);
904 return -1;
907 remove_dir(dir); /* Clean it up */
909 DLIST_REMOVE(context->internal->files, dir);
911 if (dir) {
913 SAFE_FREE(dir->fname);
914 SAFE_FREE(dir); /* Free the space too */
917 TALLOC_FREE(frame);
918 return 0;
922 static void
923 smbc_readdir_internal(SMBCCTX * context,
924 struct smbc_dirent *dest,
925 struct smbc_dirent *src,
926 int max_namebuf_len)
928 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
930 /* url-encode the name. get back remaining buffer space */
931 max_namebuf_len =
932 smbc_urlencode(dest->name, src->name, max_namebuf_len);
934 /* We now know the name length */
935 dest->namelen = strlen(dest->name);
937 /* Save the pointer to the beginning of the comment */
938 dest->comment = dest->name + dest->namelen + 1;
940 /* Copy the comment */
941 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
942 dest->comment[max_namebuf_len - 1] = '\0';
944 /* Save other fields */
945 dest->smbc_type = src->smbc_type;
946 dest->commentlen = strlen(dest->comment);
947 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
948 (char *) dest);
949 } else {
951 /* No encoding. Just copy the entry as is. */
952 memcpy(dest, src, src->dirlen);
953 dest->comment = (char *)(&dest->name + src->namelen + 1);
959 * Routine to get a directory entry
962 struct smbc_dirent *
963 SMBC_readdir_ctx(SMBCCTX *context,
964 SMBCFILE *dir)
966 int maxlen;
967 struct smbc_dirent *dirp, *dirent;
968 TALLOC_CTX *frame = talloc_stackframe();
970 /* Check that all is ok first ... */
972 if (!context || !context->internal->initialized) {
974 errno = EINVAL;
975 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
976 TALLOC_FREE(frame);
977 return NULL;
981 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
983 errno = EBADF;
984 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
985 TALLOC_FREE(frame);
986 return NULL;
990 if (dir->file != False) { /* FIXME, should be dir, perhaps */
992 errno = ENOTDIR;
993 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
994 TALLOC_FREE(frame);
995 return NULL;
999 if (!dir->dir_next) {
1000 TALLOC_FREE(frame);
1001 return NULL;
1004 dirent = dir->dir_next->dirent;
1005 if (!dirent) {
1007 errno = ENOENT;
1008 TALLOC_FREE(frame);
1009 return NULL;
1013 dirp = &context->internal->dirent;
1014 maxlen = sizeof(context->internal->_dirent_name);
1016 smbc_readdir_internal(context, dirp, dirent, maxlen);
1018 dir->dir_next = dir->dir_next->next;
1020 TALLOC_FREE(frame);
1021 return dirp;
1025 * Routine to get directory entries
1029 SMBC_getdents_ctx(SMBCCTX *context,
1030 SMBCFILE *dir,
1031 struct smbc_dirent *dirp,
1032 int count)
1034 int rem = count;
1035 int reqd;
1036 int maxlen;
1037 char *ndir = (char *)dirp;
1038 struct smbc_dir_list *dirlist;
1039 TALLOC_CTX *frame = talloc_stackframe();
1041 /* Check that all is ok first ... */
1043 if (!context || !context->internal->initialized) {
1045 errno = EINVAL;
1046 TALLOC_FREE(frame);
1047 return -1;
1051 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1053 errno = EBADF;
1054 TALLOC_FREE(frame);
1055 return -1;
1059 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1061 errno = ENOTDIR;
1062 TALLOC_FREE(frame);
1063 return -1;
1068 * Now, retrieve the number of entries that will fit in what was passed
1069 * We have to figure out if the info is in the list, or we need to
1070 * send a request to the server to get the info.
1073 while ((dirlist = dir->dir_next)) {
1074 struct smbc_dirent *dirent;
1075 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1077 if (!dirlist->dirent) {
1079 errno = ENOENT; /* Bad error */
1080 TALLOC_FREE(frame);
1081 return -1;
1085 /* Do urlencoding of next entry, if so selected */
1086 dirent = &context->internal->dirent;
1087 maxlen = sizeof(context->internal->_dirent_name);
1088 smbc_readdir_internal(context, dirent,
1089 dirlist->dirent, maxlen);
1091 reqd = dirent->dirlen;
1093 if (rem < reqd) {
1095 if (rem < count) { /* We managed to copy something */
1097 errno = 0;
1098 TALLOC_FREE(frame);
1099 return count - rem;
1102 else { /* Nothing copied ... */
1104 errno = EINVAL; /* Not enough space ... */
1105 TALLOC_FREE(frame);
1106 return -1;
1112 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1114 currentEntry->comment = &currentEntry->name[0] +
1115 dirent->namelen + 1;
1117 ndir += reqd;
1118 rem -= reqd;
1120 /* Try and align the struct for the next entry
1121 on a valid pointer boundary by appending zeros */
1122 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1123 *ndir = '\0';
1124 rem--;
1125 ndir++;
1126 currentEntry->dirlen++;
1129 dir->dir_next = dirlist = dirlist -> next;
1132 TALLOC_FREE(frame);
1134 if (rem == count)
1135 return 0;
1136 else
1137 return count - rem;
1142 * Routine to create a directory ...
1146 SMBC_mkdir_ctx(SMBCCTX *context,
1147 const char *fname,
1148 mode_t mode)
1150 SMBCSRV *srv = NULL;
1151 char *server = NULL;
1152 char *share = NULL;
1153 char *user = NULL;
1154 char *password = NULL;
1155 char *workgroup = NULL;
1156 char *path = NULL;
1157 char *targetpath = NULL;
1158 struct cli_state *targetcli = NULL;
1159 TALLOC_CTX *frame = talloc_stackframe();
1161 if (!context || !context->internal->initialized) {
1162 errno = EINVAL;
1163 TALLOC_FREE(frame);
1164 return -1;
1167 if (!fname) {
1168 errno = EINVAL;
1169 TALLOC_FREE(frame);
1170 return -1;
1173 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1175 if (SMBC_parse_path(frame,
1176 context,
1177 fname,
1178 &workgroup,
1179 &server,
1180 &share,
1181 &path,
1182 &user,
1183 &password,
1184 NULL)) {
1185 errno = EINVAL;
1186 TALLOC_FREE(frame);
1187 return -1;
1190 if (!user || user[0] == (char)0) {
1191 user = talloc_strdup(frame, smbc_getUser(context));
1192 if (!user) {
1193 errno = ENOMEM;
1194 TALLOC_FREE(frame);
1195 return -1;
1199 srv = SMBC_server(frame, context, True,
1200 server, share, &workgroup, &user, &password);
1202 if (!srv) {
1204 TALLOC_FREE(frame);
1205 return -1; /* errno set by SMBC_server */
1209 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1210 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1211 srv->cli, path,
1212 &targetcli, &targetpath)) {
1213 d_printf("Could not resolve %s\n", path);
1214 errno = ENOENT;
1215 TALLOC_FREE(frame);
1216 return -1;
1218 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1220 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1221 errno = SMBC_errno(context, targetcli);
1222 TALLOC_FREE(frame);
1223 return -1;
1227 TALLOC_FREE(frame);
1228 return 0;
1233 * Our list function simply checks to see if a directory is not empty
1236 static NTSTATUS
1237 rmdir_list_fn(const char *mnt,
1238 struct file_info *finfo,
1239 const char *mask,
1240 void *state)
1242 if (strncmp(finfo->name, ".", 1) != 0 &&
1243 strncmp(finfo->name, "..", 2) != 0) {
1244 bool *smbc_rmdir_dirempty = (bool *)state;
1245 *smbc_rmdir_dirempty = false;
1247 return NT_STATUS_OK;
1251 * Routine to remove a directory
1255 SMBC_rmdir_ctx(SMBCCTX *context,
1256 const char *fname)
1258 SMBCSRV *srv = NULL;
1259 char *server = NULL;
1260 char *share = NULL;
1261 char *user = NULL;
1262 char *password = NULL;
1263 char *workgroup = NULL;
1264 char *path = NULL;
1265 char *targetpath = NULL;
1266 struct cli_state *targetcli = NULL;
1267 TALLOC_CTX *frame = talloc_stackframe();
1269 if (!context || !context->internal->initialized) {
1270 errno = EINVAL;
1271 TALLOC_FREE(frame);
1272 return -1;
1275 if (!fname) {
1276 errno = EINVAL;
1277 TALLOC_FREE(frame);
1278 return -1;
1281 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1283 if (SMBC_parse_path(frame,
1284 context,
1285 fname,
1286 &workgroup,
1287 &server,
1288 &share,
1289 &path,
1290 &user,
1291 &password,
1292 NULL)) {
1293 errno = EINVAL;
1294 TALLOC_FREE(frame);
1295 return -1;
1298 if (!user || user[0] == (char)0) {
1299 user = talloc_strdup(frame, smbc_getUser(context));
1300 if (!user) {
1301 errno = ENOMEM;
1302 TALLOC_FREE(frame);
1303 return -1;
1307 srv = SMBC_server(frame, context, True,
1308 server, share, &workgroup, &user, &password);
1310 if (!srv) {
1312 TALLOC_FREE(frame);
1313 return -1; /* errno set by SMBC_server */
1317 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1318 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1319 srv->cli, path,
1320 &targetcli, &targetpath)) {
1321 d_printf("Could not resolve %s\n", path);
1322 errno = ENOENT;
1323 TALLOC_FREE(frame);
1324 return -1;
1326 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1328 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1330 errno = SMBC_errno(context, targetcli);
1332 if (errno == EACCES) { /* Check if the dir empty or not */
1334 /* Local storage to avoid buffer overflows */
1335 char *lpath;
1336 bool smbc_rmdir_dirempty = true;
1337 NTSTATUS status;
1339 lpath = talloc_asprintf(frame, "%s\\*",
1340 targetpath);
1341 if (!lpath) {
1342 errno = ENOMEM;
1343 TALLOC_FREE(frame);
1344 return -1;
1347 status = cli_list(targetcli, lpath,
1348 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1349 rmdir_list_fn,
1350 &smbc_rmdir_dirempty);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 /* Fix errno to ignore latest error ... */
1354 DEBUG(5, ("smbc_rmdir: "
1355 "cli_list returned an error: %d\n",
1356 SMBC_errno(context, targetcli)));
1357 errno = EACCES;
1361 if (smbc_rmdir_dirempty)
1362 errno = EACCES;
1363 else
1364 errno = ENOTEMPTY;
1368 TALLOC_FREE(frame);
1369 return -1;
1373 TALLOC_FREE(frame);
1374 return 0;
1379 * Routine to return the current directory position
1382 off_t
1383 SMBC_telldir_ctx(SMBCCTX *context,
1384 SMBCFILE *dir)
1386 TALLOC_CTX *frame = talloc_stackframe();
1388 if (!context || !context->internal->initialized) {
1390 errno = EINVAL;
1391 TALLOC_FREE(frame);
1392 return -1;
1396 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1398 errno = EBADF;
1399 TALLOC_FREE(frame);
1400 return -1;
1404 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1406 errno = ENOTDIR;
1407 TALLOC_FREE(frame);
1408 return -1;
1412 /* See if we're already at the end. */
1413 if (dir->dir_next == NULL) {
1414 /* We are. */
1415 TALLOC_FREE(frame);
1416 return -1;
1420 * We return the pointer here as the offset
1422 TALLOC_FREE(frame);
1423 return (off_t)(long)dir->dir_next->dirent;
1427 * A routine to run down the list and see if the entry is OK
1430 static struct smbc_dir_list *
1431 check_dir_ent(struct smbc_dir_list *list,
1432 struct smbc_dirent *dirent)
1435 /* Run down the list looking for what we want */
1437 if (dirent) {
1439 struct smbc_dir_list *tmp = list;
1441 while (tmp) {
1443 if (tmp->dirent == dirent)
1444 return tmp;
1446 tmp = tmp->next;
1452 return NULL; /* Not found, or an error */
1458 * Routine to seek on a directory
1462 SMBC_lseekdir_ctx(SMBCCTX *context,
1463 SMBCFILE *dir,
1464 off_t offset)
1466 long int l_offset = offset; /* Handle problems of size */
1467 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1468 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1469 TALLOC_CTX *frame = talloc_stackframe();
1471 if (!context || !context->internal->initialized) {
1473 errno = EINVAL;
1474 TALLOC_FREE(frame);
1475 return -1;
1479 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1481 errno = ENOTDIR;
1482 TALLOC_FREE(frame);
1483 return -1;
1487 /* Now, check what we were passed and see if it is OK ... */
1489 if (dirent == NULL) { /* Seek to the begining of the list */
1491 dir->dir_next = dir->dir_list;
1492 TALLOC_FREE(frame);
1493 return 0;
1497 if (offset == -1) { /* Seek to the end of the list */
1498 dir->dir_next = NULL;
1499 TALLOC_FREE(frame);
1500 return 0;
1503 /* Now, run down the list and make sure that the entry is OK */
1504 /* This may need to be changed if we change the format of the list */
1506 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1507 errno = EINVAL; /* Bad entry */
1508 TALLOC_FREE(frame);
1509 return -1;
1512 dir->dir_next = list_ent;
1514 TALLOC_FREE(frame);
1515 return 0;
1519 * Routine to fstat a dir
1523 SMBC_fstatdir_ctx(SMBCCTX *context,
1524 SMBCFILE *dir,
1525 struct stat *st)
1528 if (!context || !context->internal->initialized) {
1530 errno = EINVAL;
1531 return -1;
1534 /* No code yet ... */
1535 return 0;
1539 SMBC_chmod_ctx(SMBCCTX *context,
1540 const char *fname,
1541 mode_t newmode)
1543 SMBCSRV *srv = NULL;
1544 char *server = NULL;
1545 char *share = NULL;
1546 char *user = NULL;
1547 char *password = NULL;
1548 char *workgroup = NULL;
1549 char *targetpath = NULL;
1550 struct cli_state *targetcli = NULL;
1551 char *path = NULL;
1552 uint16 mode;
1553 TALLOC_CTX *frame = talloc_stackframe();
1555 if (!context || !context->internal->initialized) {
1557 errno = EINVAL; /* Best I can think of ... */
1558 TALLOC_FREE(frame);
1559 return -1;
1562 if (!fname) {
1563 errno = EINVAL;
1564 TALLOC_FREE(frame);
1565 return -1;
1568 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1570 if (SMBC_parse_path(frame,
1571 context,
1572 fname,
1573 &workgroup,
1574 &server,
1575 &share,
1576 &path,
1577 &user,
1578 &password,
1579 NULL)) {
1580 errno = EINVAL;
1581 TALLOC_FREE(frame);
1582 return -1;
1585 if (!user || user[0] == (char)0) {
1586 user = talloc_strdup(frame, smbc_getUser(context));
1587 if (!user) {
1588 errno = ENOMEM;
1589 TALLOC_FREE(frame);
1590 return -1;
1594 srv = SMBC_server(frame, context, True,
1595 server, share, &workgroup, &user, &password);
1597 if (!srv) {
1598 TALLOC_FREE(frame);
1599 return -1; /* errno set by SMBC_server */
1602 /*d_printf(">>>unlink: resolving %s\n", path);*/
1603 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1604 srv->cli, path,
1605 &targetcli, &targetpath)) {
1606 d_printf("Could not resolve %s\n", path);
1607 errno = ENOENT;
1608 TALLOC_FREE(frame);
1609 return -1;
1612 mode = 0;
1614 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1615 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1616 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1617 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1619 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1620 errno = SMBC_errno(context, targetcli);
1621 TALLOC_FREE(frame);
1622 return -1;
1625 TALLOC_FREE(frame);
1626 return 0;
1630 SMBC_utimes_ctx(SMBCCTX *context,
1631 const char *fname,
1632 struct timeval *tbuf)
1634 SMBCSRV *srv = NULL;
1635 char *server = NULL;
1636 char *share = NULL;
1637 char *user = NULL;
1638 char *password = NULL;
1639 char *workgroup = NULL;
1640 char *path = NULL;
1641 time_t access_time;
1642 time_t write_time;
1643 TALLOC_CTX *frame = talloc_stackframe();
1645 if (!context || !context->internal->initialized) {
1647 errno = EINVAL; /* Best I can think of ... */
1648 TALLOC_FREE(frame);
1649 return -1;
1652 if (!fname) {
1653 errno = EINVAL;
1654 TALLOC_FREE(frame);
1655 return -1;
1658 if (tbuf == NULL) {
1659 access_time = write_time = time(NULL);
1660 } else {
1661 access_time = tbuf[0].tv_sec;
1662 write_time = tbuf[1].tv_sec;
1665 if (DEBUGLVL(4)) {
1666 char *p;
1667 char atimebuf[32];
1668 char mtimebuf[32];
1670 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1671 atimebuf[sizeof(atimebuf) - 1] = '\0';
1672 if ((p = strchr(atimebuf, '\n')) != NULL) {
1673 *p = '\0';
1676 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1677 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1678 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1679 *p = '\0';
1682 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1683 fname, atimebuf, mtimebuf);
1686 if (SMBC_parse_path(frame,
1687 context,
1688 fname,
1689 &workgroup,
1690 &server,
1691 &share,
1692 &path,
1693 &user,
1694 &password,
1695 NULL)) {
1696 errno = EINVAL;
1697 TALLOC_FREE(frame);
1698 return -1;
1701 if (!user || user[0] == (char)0) {
1702 user = talloc_strdup(frame, smbc_getUser(context));
1703 if (!user) {
1704 errno = ENOMEM;
1705 TALLOC_FREE(frame);
1706 return -1;
1710 srv = SMBC_server(frame, context, True,
1711 server, share, &workgroup, &user, &password);
1713 if (!srv) {
1714 TALLOC_FREE(frame);
1715 return -1; /* errno set by SMBC_server */
1718 if (!SMBC_setatr(context, srv, path,
1719 0, access_time, write_time, 0, 0)) {
1720 TALLOC_FREE(frame);
1721 return -1; /* errno set by SMBC_setatr */
1724 TALLOC_FREE(frame);
1725 return 0;
1729 * Routine to unlink() a file
1733 SMBC_unlink_ctx(SMBCCTX *context,
1734 const char *fname)
1736 char *server = NULL;
1737 char *share = NULL;
1738 char *user = NULL;
1739 char *password = NULL;
1740 char *workgroup = NULL;
1741 char *path = NULL;
1742 char *targetpath = NULL;
1743 struct cli_state *targetcli = NULL;
1744 SMBCSRV *srv = NULL;
1745 TALLOC_CTX *frame = talloc_stackframe();
1747 if (!context || !context->internal->initialized) {
1749 errno = EINVAL; /* Best I can think of ... */
1750 TALLOC_FREE(frame);
1751 return -1;
1755 if (!fname) {
1756 errno = EINVAL;
1757 TALLOC_FREE(frame);
1758 return -1;
1762 if (SMBC_parse_path(frame,
1763 context,
1764 fname,
1765 &workgroup,
1766 &server,
1767 &share,
1768 &path,
1769 &user,
1770 &password,
1771 NULL)) {
1772 errno = EINVAL;
1773 TALLOC_FREE(frame);
1774 return -1;
1777 if (!user || user[0] == (char)0) {
1778 user = talloc_strdup(frame, smbc_getUser(context));
1779 if (!user) {
1780 errno = ENOMEM;
1781 TALLOC_FREE(frame);
1782 return -1;
1786 srv = SMBC_server(frame, context, True,
1787 server, share, &workgroup, &user, &password);
1789 if (!srv) {
1790 TALLOC_FREE(frame);
1791 return -1; /* SMBC_server sets errno */
1795 /*d_printf(">>>unlink: resolving %s\n", path);*/
1796 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1797 srv->cli, path,
1798 &targetcli, &targetpath)) {
1799 d_printf("Could not resolve %s\n", path);
1800 errno = ENOENT;
1801 TALLOC_FREE(frame);
1802 return -1;
1804 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1806 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1808 errno = SMBC_errno(context, targetcli);
1810 if (errno == EACCES) { /* Check if the file is a directory */
1812 int saverr = errno;
1813 SMB_OFF_T size = 0;
1814 uint16 mode = 0;
1815 struct timespec write_time_ts;
1816 struct timespec access_time_ts;
1817 struct timespec change_time_ts;
1818 SMB_INO_T ino = 0;
1820 if (!SMBC_getatr(context, srv, path, &mode, &size,
1821 NULL,
1822 &access_time_ts,
1823 &write_time_ts,
1824 &change_time_ts,
1825 &ino)) {
1827 /* Hmmm, bad error ... What? */
1829 errno = SMBC_errno(context, targetcli);
1830 TALLOC_FREE(frame);
1831 return -1;
1834 else {
1836 if (IS_DOS_DIR(mode))
1837 errno = EISDIR;
1838 else
1839 errno = saverr; /* Restore this */
1844 TALLOC_FREE(frame);
1845 return -1;
1849 TALLOC_FREE(frame);
1850 return 0; /* Success ... */
1855 * Routine to rename() a file
1859 SMBC_rename_ctx(SMBCCTX *ocontext,
1860 const char *oname,
1861 SMBCCTX *ncontext,
1862 const char *nname)
1864 char *server1 = NULL;
1865 char *share1 = NULL;
1866 char *server2 = NULL;
1867 char *share2 = NULL;
1868 char *user1 = NULL;
1869 char *user2 = NULL;
1870 char *password1 = NULL;
1871 char *password2 = NULL;
1872 char *workgroup = NULL;
1873 char *path1 = NULL;
1874 char *path2 = NULL;
1875 char *targetpath1 = NULL;
1876 char *targetpath2 = NULL;
1877 struct cli_state *targetcli1 = NULL;
1878 struct cli_state *targetcli2 = NULL;
1879 SMBCSRV *srv = NULL;
1880 TALLOC_CTX *frame = talloc_stackframe();
1882 if (!ocontext || !ncontext ||
1883 !ocontext->internal->initialized ||
1884 !ncontext->internal->initialized) {
1886 errno = EINVAL; /* Best I can think of ... */
1887 TALLOC_FREE(frame);
1888 return -1;
1891 if (!oname || !nname) {
1892 errno = EINVAL;
1893 TALLOC_FREE(frame);
1894 return -1;
1897 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1899 if (SMBC_parse_path(frame,
1900 ocontext,
1901 oname,
1902 &workgroup,
1903 &server1,
1904 &share1,
1905 &path1,
1906 &user1,
1907 &password1,
1908 NULL)) {
1909 errno = EINVAL;
1910 TALLOC_FREE(frame);
1911 return -1;
1914 if (!user1 || user1[0] == (char)0) {
1915 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1916 if (!user1) {
1917 errno = ENOMEM;
1918 TALLOC_FREE(frame);
1919 return -1;
1923 if (SMBC_parse_path(frame,
1924 ncontext,
1925 nname,
1926 NULL,
1927 &server2,
1928 &share2,
1929 &path2,
1930 &user2,
1931 &password2,
1932 NULL)) {
1933 errno = EINVAL;
1934 TALLOC_FREE(frame);
1935 return -1;
1938 if (!user2 || user2[0] == (char)0) {
1939 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1940 if (!user2) {
1941 errno = ENOMEM;
1942 TALLOC_FREE(frame);
1943 return -1;
1947 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1948 strcmp(user1, user2)) {
1949 /* Can't rename across file systems, or users?? */
1950 errno = EXDEV;
1951 TALLOC_FREE(frame);
1952 return -1;
1955 srv = SMBC_server(frame, ocontext, True,
1956 server1, share1, &workgroup, &user1, &password1);
1957 if (!srv) {
1958 TALLOC_FREE(frame);
1959 return -1;
1963 /* set the credentials to make DFS work */
1964 smbc_set_credentials_with_fallback(ocontext,
1965 workgroup,
1966 user1,
1967 password1);
1969 /*d_printf(">>>rename: resolving %s\n", path1);*/
1970 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1971 srv->cli,
1972 path1,
1973 &targetcli1, &targetpath1)) {
1974 d_printf("Could not resolve %s\n", path1);
1975 errno = ENOENT;
1976 TALLOC_FREE(frame);
1977 return -1;
1980 /* set the credentials to make DFS work */
1981 smbc_set_credentials_with_fallback(ncontext,
1982 workgroup,
1983 user2,
1984 password2);
1986 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1987 /*d_printf(">>>rename: resolving %s\n", path2);*/
1988 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1989 srv->cli,
1990 path2,
1991 &targetcli2, &targetpath2)) {
1992 d_printf("Could not resolve %s\n", path2);
1993 errno = ENOENT;
1994 TALLOC_FREE(frame);
1995 return -1;
1997 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1999 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2000 strcmp(targetcli1->share, targetcli2->share))
2002 /* can't rename across file systems */
2003 errno = EXDEV;
2004 TALLOC_FREE(frame);
2005 return -1;
2008 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2009 int eno = SMBC_errno(ocontext, targetcli1);
2011 if (eno != EEXIST ||
2012 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2013 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2015 errno = eno;
2016 TALLOC_FREE(frame);
2017 return -1;
2022 TALLOC_FREE(frame);
2023 return 0; /* Success */