s4:torture/remote_pac: verify the order of PAC elements
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob97ecaa405a78d904db9a1953a71a5a20bf07463d
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 "libsmb/libsmb.h"
27 #include "auth_info.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
32 #include "libsmb/nmblib.h"
33 #include "../libcli/smb/smbXcli_base.h"
34 #include "../libcli/security/security.h"
35 #include "lib/util/tevent_ntstatus.h"
38 * Routine to open a directory
39 * We accept the URL syntax explained in SMBC_parse_path(), above.
42 static void
43 remove_dir(SMBCFILE *dir)
45 struct smbc_dir_list *d,*f;
47 d = dir->dir_list;
48 while (d) {
50 f = d; d = d->next;
52 SAFE_FREE(f->dirent);
53 SAFE_FREE(f);
57 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
61 static int
62 add_dirent(SMBCFILE *dir,
63 const char *name,
64 const char *comment,
65 uint32_t type)
67 struct smbc_dirent *dirent;
68 int size;
69 int name_length = (name == NULL ? 0 : strlen(name));
70 int comment_len = (comment == NULL ? 0 : strlen(comment));
73 * Allocate space for the dirent, which must be increased by the
74 * size of the name and the comment and 1 each for the null terminator.
77 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
79 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
81 if (!dirent) {
83 dir->dir_error = ENOMEM;
84 return -1;
88 ZERO_STRUCTP(dirent);
90 if (dir->dir_list == NULL) {
92 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
93 if (!dir->dir_list) {
95 SAFE_FREE(dirent);
96 dir->dir_error = ENOMEM;
97 return -1;
100 ZERO_STRUCTP(dir->dir_list);
102 dir->dir_end = dir->dir_next = dir->dir_list;
104 else {
106 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
108 if (!dir->dir_end->next) {
110 SAFE_FREE(dirent);
111 dir->dir_error = ENOMEM;
112 return -1;
115 ZERO_STRUCTP(dir->dir_end->next);
117 dir->dir_end = dir->dir_end->next;
120 dir->dir_end->next = NULL;
121 dir->dir_end->dirent = dirent;
123 dirent->smbc_type = type;
124 dirent->namelen = name_length;
125 dirent->commentlen = comment_len;
126 dirent->dirlen = size;
129 * dirent->namelen + 1 includes the null (no null termination needed)
130 * Ditto for dirent->commentlen.
131 * The space for the two null bytes was allocated.
133 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
134 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
135 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
137 return 0;
141 static void
142 list_unique_wg_fn(const char *name,
143 uint32_t type,
144 const char *comment,
145 void *state)
147 SMBCFILE *dir = (SMBCFILE *)state;
148 struct smbc_dir_list *dir_list;
149 struct smbc_dirent *dirent;
150 int dirent_type;
151 int do_remove = 0;
153 dirent_type = dir->dir_type;
155 if (add_dirent(dir, name, comment, dirent_type) < 0) {
156 /* An error occurred, what do we do? */
157 /* FIXME: Add some code here */
158 /* Change cli_NetServerEnum to take a fn
159 returning NTSTATUS... JRA. */
162 /* Point to the one just added */
163 dirent = dir->dir_end->dirent;
165 /* See if this was a duplicate */
166 for (dir_list = dir->dir_list;
167 dir_list != dir->dir_end;
168 dir_list = dir_list->next) {
169 if (! do_remove &&
170 strcmp(dir_list->dirent->name, dirent->name) == 0) {
171 /* Duplicate. End end of list need to be removed. */
172 do_remove = 1;
175 if (do_remove && dir_list->next == dir->dir_end) {
176 /* Found the end of the list. Remove it. */
177 dir->dir_end = dir_list;
178 free(dir_list->next);
179 free(dirent);
180 dir_list->next = NULL;
181 break;
186 static void
187 list_fn(const char *name,
188 uint32_t type,
189 const char *comment,
190 void *state)
192 SMBCFILE *dir = (SMBCFILE *)state;
193 int dirent_type;
196 * We need to process the type a little ...
198 * Disk share = 0x00000000
199 * Print share = 0x00000001
200 * Comms share = 0x00000002 (obsolete?)
201 * IPC$ share = 0x00000003
203 * administrative shares:
204 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
207 if (dir->dir_type == SMBC_FILE_SHARE) {
208 switch (type) {
209 case 0 | 0x80000000:
210 case 0:
211 dirent_type = SMBC_FILE_SHARE;
212 break;
214 case 1:
215 dirent_type = SMBC_PRINTER_SHARE;
216 break;
218 case 2:
219 dirent_type = SMBC_COMMS_SHARE;
220 break;
222 case 3 | 0x80000000:
223 case 3:
224 dirent_type = SMBC_IPC_SHARE;
225 break;
227 default:
228 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
229 break;
232 else {
233 dirent_type = dir->dir_type;
236 if (add_dirent(dir, name, comment, dirent_type) < 0) {
237 /* An error occurred, what do we do? */
238 /* FIXME: Add some code here */
239 /* Change cli_NetServerEnum to take a fn
240 returning NTSTATUS... JRA. */
244 static NTSTATUS
245 dir_list_fn(const char *mnt,
246 struct file_info *finfo,
247 const char *mask,
248 void *state)
251 if (add_dirent((SMBCFILE *)state, finfo->name, "",
252 (finfo->mode&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
253 SMBCFILE *dir = (SMBCFILE *)state;
254 return map_nt_error_from_unix(dir->dir_error);
256 return NT_STATUS_OK;
259 static int
260 net_share_enum_rpc(struct cli_state *cli,
261 void (*fn)(const char *name,
262 uint32_t type,
263 const char *comment,
264 void *state),
265 void *state)
267 int i;
268 WERROR result;
269 uint32_t preferred_len = 0xffffffff;
270 uint32_t type;
271 struct srvsvc_NetShareInfoCtr info_ctr;
272 struct srvsvc_NetShareCtr1 ctr1;
273 fstring name = "";
274 fstring comment = "";
275 struct rpc_pipe_client *pipe_hnd = NULL;
276 NTSTATUS nt_status;
277 uint32_t resume_handle = 0;
278 uint32_t total_entries = 0;
279 struct dcerpc_binding_handle *b;
281 /* Open the server service pipe */
282 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
283 &pipe_hnd);
284 if (!NT_STATUS_IS_OK(nt_status)) {
285 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
286 return -1;
289 ZERO_STRUCT(info_ctr);
290 ZERO_STRUCT(ctr1);
292 info_ctr.level = 1;
293 info_ctr.ctr.ctr1 = &ctr1;
295 b = pipe_hnd->binding_handle;
297 /* Issue the NetShareEnum RPC call and retrieve the response */
298 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
299 pipe_hnd->desthost,
300 &info_ctr,
301 preferred_len,
302 &total_entries,
303 &resume_handle,
304 &result);
306 /* Was it successful? */
307 if (!NT_STATUS_IS_OK(nt_status)) {
308 /* Nope. Go clean up. */
309 result = ntstatus_to_werror(nt_status);
310 goto done;
313 if (!W_ERROR_IS_OK(result)) {
314 /* Nope. Go clean up. */
315 goto done;
318 if (total_entries == 0) {
319 /* Nope. Go clean up. */
320 result = WERR_GENERAL_FAILURE;
321 goto done;
324 /* For each returned entry... */
325 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
327 /* pull out the share name */
328 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
330 /* pull out the share's comment */
331 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
333 /* Get the type value */
334 type = info_ctr.ctr.ctr1->array[i].type;
336 /* Add this share to the list */
337 (*fn)(name, type, comment, state);
340 done:
341 /* Close the server service pipe */
342 TALLOC_FREE(pipe_hnd);
344 /* Tell 'em if it worked */
345 return W_ERROR_IS_OK(result) ? 0 : -1;
350 * Verify that the options specified in a URL are valid
353 SMBC_check_options(char *server,
354 char *share,
355 char *path,
356 char *options)
358 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
359 "path='%s' options='%s'\n",
360 server, share, path, options));
362 /* No options at all is always ok */
363 if (! *options) return 0;
365 /* Currently, we don't support any options. */
366 return -1;
370 SMBCFILE *
371 SMBC_opendir_ctx(SMBCCTX *context,
372 const char *fname)
374 int saved_errno;
375 char *server = NULL;
376 char *share = NULL;
377 char *user = NULL;
378 char *password = NULL;
379 char *options = NULL;
380 char *workgroup = NULL;
381 char *path = NULL;
382 uint16_t mode;
383 uint16_t port = 0;
384 char *p = NULL;
385 SMBCSRV *srv = NULL;
386 SMBCFILE *dir = NULL;
387 struct sockaddr_storage rem_ss;
388 TALLOC_CTX *frame = talloc_stackframe();
390 if (!context || !context->internal->initialized) {
391 DEBUG(4, ("no valid context\n"));
392 TALLOC_FREE(frame);
393 errno = EINVAL + 8192;
394 return NULL;
398 if (!fname) {
399 DEBUG(4, ("no valid fname\n"));
400 TALLOC_FREE(frame);
401 errno = EINVAL + 8193;
402 return NULL;
405 if (SMBC_parse_path(frame,
406 context,
407 fname,
408 &workgroup,
409 &server,
410 &port,
411 &share,
412 &path,
413 &user,
414 &password,
415 &options)) {
416 DEBUG(4, ("no valid path\n"));
417 TALLOC_FREE(frame);
418 errno = EINVAL + 8194;
419 return NULL;
422 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
423 "path='%s' options='%s'\n",
424 fname, server, share, path, options));
426 /* Ensure the options are valid */
427 if (SMBC_check_options(server, share, path, options)) {
428 DEBUG(4, ("unacceptable options (%s)\n", options));
429 TALLOC_FREE(frame);
430 errno = EINVAL + 8195;
431 return NULL;
434 if (!user || user[0] == (char)0) {
435 user = talloc_strdup(frame, smbc_getUser(context));
436 if (!user) {
437 TALLOC_FREE(frame);
438 errno = ENOMEM;
439 return NULL;
443 dir = SMB_MALLOC_P(SMBCFILE);
445 if (!dir) {
446 TALLOC_FREE(frame);
447 errno = ENOMEM;
448 return NULL;
451 ZERO_STRUCTP(dir);
453 dir->cli_fd = 0;
454 dir->fname = SMB_STRDUP(fname);
455 dir->srv = NULL;
456 dir->offset = 0;
457 dir->file = False;
458 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
460 if (server[0] == (char)0) {
462 int i;
463 int count;
464 int max_lmb_count;
465 struct sockaddr_storage *ip_list;
466 struct sockaddr_storage server_addr;
467 struct user_auth_info u_info;
468 NTSTATUS status;
470 if (share[0] != (char)0 || path[0] != (char)0) {
472 if (dir) {
473 SAFE_FREE(dir->fname);
474 SAFE_FREE(dir);
476 TALLOC_FREE(frame);
477 errno = EINVAL + 8196;
478 return NULL;
481 /* Determine how many local master browsers to query */
482 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
483 ? INT_MAX
484 : smbc_getOptionBrowseMaxLmbCount(context));
486 memset(&u_info, '\0', sizeof(u_info));
487 u_info.username = talloc_strdup(frame,user);
488 u_info.password = talloc_strdup(frame,password);
489 if (!u_info.username || !u_info.password) {
490 if (dir) {
491 SAFE_FREE(dir->fname);
492 SAFE_FREE(dir);
494 TALLOC_FREE(frame);
495 return NULL;
499 * We have server and share and path empty but options
500 * requesting that we scan all master browsers for their list
501 * of workgroups/domains. This implies that we must first try
502 * broadcast queries to find all master browsers, and if that
503 * doesn't work, then try our other methods which return only
504 * a single master browser.
507 ip_list = NULL;
508 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
509 &ip_list, &count);
510 if (!NT_STATUS_IS_OK(status))
513 TALLOC_FREE(ip_list);
515 if (!find_master_ip(workgroup, &server_addr)) {
517 if (dir) {
518 SAFE_FREE(dir->fname);
519 SAFE_FREE(dir);
521 TALLOC_FREE(frame);
522 errno = ENOENT;
523 return NULL;
526 ip_list = (struct sockaddr_storage *)talloc_memdup(
527 talloc_tos(), &server_addr,
528 sizeof(server_addr));
529 if (ip_list == NULL) {
530 if (dir) {
531 SAFE_FREE(dir->fname);
532 SAFE_FREE(dir);
534 TALLOC_FREE(frame);
535 errno = ENOMEM;
536 return NULL;
538 count = 1;
541 for (i = 0; i < count && i < max_lmb_count; i++) {
542 char addr[INET6_ADDRSTRLEN];
543 char *wg_ptr = NULL;
544 struct cli_state *cli = NULL;
546 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
547 DEBUG(99, ("Found master browser %d of %d: %s\n",
548 i+1, MAX(count, max_lmb_count),
549 addr));
551 cli = get_ipc_connect_master_ip(talloc_tos(),
552 &ip_list[i],
553 &u_info,
554 &wg_ptr);
555 /* cli == NULL is the master browser refused to talk or
556 could not be found */
557 if (!cli) {
558 continue;
561 workgroup = talloc_strdup(frame, wg_ptr);
562 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
564 cli_shutdown(cli);
566 if (!workgroup || !server) {
567 if (dir) {
568 SAFE_FREE(dir->fname);
569 SAFE_FREE(dir);
571 TALLOC_FREE(frame);
572 errno = ENOMEM;
573 return NULL;
576 DEBUG(4, ("using workgroup %s %s\n",
577 workgroup, server));
580 * For each returned master browser IP address, get a
581 * connection to IPC$ on the server if we do not
582 * already have one, and determine the
583 * workgroups/domains that it knows about.
586 srv = SMBC_server(frame, context, True, server, port, "IPC$",
587 &workgroup, &user, &password);
588 if (!srv) {
589 continue;
592 dir->srv = srv;
593 dir->dir_type = SMBC_WORKGROUP;
595 /* Now, list the stuff ... */
597 if (!cli_NetServerEnum(srv->cli,
598 workgroup,
599 SV_TYPE_DOMAIN_ENUM,
600 list_unique_wg_fn,
601 (void *)dir)) {
602 continue;
606 TALLOC_FREE(ip_list);
607 } else {
609 * Server not an empty string ... Check the rest and see what
610 * gives
612 if (*share == '\0') {
613 if (*path != '\0') {
615 /* Should not have empty share with path */
616 if (dir) {
617 SAFE_FREE(dir->fname);
618 SAFE_FREE(dir);
620 TALLOC_FREE(frame);
621 errno = EINVAL + 8197;
622 return NULL;
627 * We don't know if <server> is really a server name
628 * or is a workgroup/domain name. If we already have
629 * a server structure for it, we'll use it.
630 * Otherwise, check to see if <server><1D>,
631 * <server><1B>, or <server><20> translates. We check
632 * to see if <server> is an IP address first.
636 * See if we have an existing server. Do not
637 * establish a connection if one does not already
638 * exist.
640 srv = SMBC_server(frame, context, False,
641 server, port, "IPC$",
642 &workgroup, &user, &password);
645 * If no existing server and not an IP addr, look for
646 * LMB or DMB
648 if (!srv &&
649 !is_ipaddress(server) &&
650 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
651 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
653 * "server" is actually a workgroup name,
654 * not a server. Make this clear.
656 char *wgroup = server;
657 fstring buserver;
659 dir->dir_type = SMBC_SERVER;
662 * Get the backup list ...
664 if (!name_status_find(wgroup, 0, 0,
665 &rem_ss, buserver)) {
666 char addr[INET6_ADDRSTRLEN];
668 print_sockaddr(addr, sizeof(addr), &rem_ss);
669 DEBUG(0,("Could not get name of "
670 "local/domain master browser "
671 "for workgroup %s from "
672 "address %s\n",
673 wgroup,
674 addr));
675 if (dir) {
676 SAFE_FREE(dir->fname);
677 SAFE_FREE(dir);
679 TALLOC_FREE(frame);
680 errno = EPERM;
681 return NULL;
686 * Get a connection to IPC$ on the server if
687 * we do not already have one
689 srv = SMBC_server(frame, context, True,
690 buserver, port, "IPC$",
691 &workgroup,
692 &user, &password);
693 if (!srv) {
694 DEBUG(0, ("got no contact to IPC$\n"));
695 if (dir) {
696 SAFE_FREE(dir->fname);
697 SAFE_FREE(dir);
699 TALLOC_FREE(frame);
700 return NULL;
704 dir->srv = srv;
706 /* Now, list the servers ... */
707 if (!cli_NetServerEnum(srv->cli, wgroup,
708 0x0000FFFE, list_fn,
709 (void *)dir)) {
711 if (dir) {
712 SAFE_FREE(dir->fname);
713 SAFE_FREE(dir);
715 TALLOC_FREE(frame);
716 return NULL;
718 } else if (srv ||
719 (resolve_name(server, &rem_ss, 0x20, false))) {
722 * If we hadn't found the server, get one now
724 if (!srv) {
725 srv = SMBC_server(frame, context, True,
726 server, port, "IPC$",
727 &workgroup,
728 &user, &password);
731 if (!srv) {
732 if (dir) {
733 SAFE_FREE(dir->fname);
734 SAFE_FREE(dir);
736 TALLOC_FREE(frame);
737 return NULL;
741 dir->dir_type = SMBC_FILE_SHARE;
742 dir->srv = srv;
744 /* List the shares ... */
746 if (net_share_enum_rpc(
747 srv->cli,
748 list_fn,
749 (void *) dir) < 0 &&
750 cli_RNetShareEnum(
751 srv->cli,
752 list_fn,
753 (void *)dir) < 0) {
755 errno = cli_errno(srv->cli);
756 if (dir) {
757 SAFE_FREE(dir->fname);
758 SAFE_FREE(dir);
760 TALLOC_FREE(frame);
761 return NULL;
764 } else {
765 /* Neither the workgroup nor server exists */
766 errno = ECONNREFUSED;
767 if (dir) {
768 SAFE_FREE(dir->fname);
769 SAFE_FREE(dir);
771 TALLOC_FREE(frame);
772 return NULL;
776 else {
778 * The server and share are specified ... work from
779 * there ...
781 char *targetpath;
782 struct cli_state *targetcli;
783 NTSTATUS status;
785 /* We connect to the server and list the directory */
786 dir->dir_type = SMBC_FILE_SHARE;
788 srv = SMBC_server(frame, context, True, server, port, share,
789 &workgroup, &user, &password);
791 if (!srv) {
792 if (dir) {
793 SAFE_FREE(dir->fname);
794 SAFE_FREE(dir);
796 TALLOC_FREE(frame);
797 return NULL;
800 dir->srv = srv;
802 /* Now, list the files ... */
804 p = path + strlen(path);
805 path = talloc_asprintf_append(path, "\\*");
806 if (!path) {
807 if (dir) {
808 SAFE_FREE(dir->fname);
809 SAFE_FREE(dir);
811 TALLOC_FREE(frame);
812 return NULL;
815 status = cli_resolve_path(
816 frame, "", context->internal->auth_info,
817 srv->cli, path, &targetcli, &targetpath);
818 if (!NT_STATUS_IS_OK(status)) {
819 d_printf("Could not resolve %s\n", path);
820 if (dir) {
821 SAFE_FREE(dir->fname);
822 SAFE_FREE(dir);
824 TALLOC_FREE(frame);
825 return NULL;
828 status = cli_list(targetcli, targetpath,
829 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
830 dir_list_fn, (void *)dir);
831 if (!NT_STATUS_IS_OK(status)) {
832 if (dir) {
833 SAFE_FREE(dir->fname);
834 SAFE_FREE(dir);
836 saved_errno = SMBC_errno(context, targetcli);
838 if (saved_errno == EINVAL) {
840 * See if they asked to opendir
841 * something other than a directory.
842 * If so, the converted error value we
843 * got would have been EINVAL rather
844 * than ENOTDIR.
846 *p = '\0'; /* restore original path */
848 if (SMBC_getatr(context, srv, path,
849 &mode, NULL,
850 NULL, NULL, NULL, NULL,
851 NULL) &&
852 ! IS_DOS_DIR(mode)) {
854 /* It is. Correct the error value */
855 saved_errno = ENOTDIR;
860 * If there was an error and the server is no
861 * good any more...
863 if (cli_is_error(targetcli) &&
864 smbc_getFunctionCheckServer(context)(context, srv)) {
866 /* ... then remove it. */
867 if (smbc_getFunctionRemoveUnusedServer(context)(context,
868 srv)) {
870 * We could not remove the
871 * server completely, remove
872 * it from the cache so we
873 * will not get it again. It
874 * will be removed when the
875 * last file/dir is closed.
877 smbc_getFunctionRemoveCachedServer(context)(context, srv);
881 TALLOC_FREE(frame);
882 errno = saved_errno;
883 return NULL;
889 DLIST_ADD(context->internal->files, dir);
890 TALLOC_FREE(frame);
891 return dir;
896 * Routine to close a directory
900 SMBC_closedir_ctx(SMBCCTX *context,
901 SMBCFILE *dir)
903 TALLOC_CTX *frame = talloc_stackframe();
905 if (!context || !context->internal->initialized) {
906 errno = EINVAL;
907 TALLOC_FREE(frame);
908 return -1;
911 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
912 errno = EBADF;
913 TALLOC_FREE(frame);
914 return -1;
917 remove_dir(dir); /* Clean it up */
919 DLIST_REMOVE(context->internal->files, dir);
921 if (dir) {
923 SAFE_FREE(dir->fname);
924 SAFE_FREE(dir); /* Free the space too */
927 TALLOC_FREE(frame);
928 return 0;
932 static void
933 smbc_readdir_internal(SMBCCTX * context,
934 struct smbc_dirent *dest,
935 struct smbc_dirent *src,
936 int max_namebuf_len)
938 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
940 /* url-encode the name. get back remaining buffer space */
941 max_namebuf_len =
942 smbc_urlencode(dest->name, src->name, max_namebuf_len);
944 /* We now know the name length */
945 dest->namelen = strlen(dest->name);
947 /* Save the pointer to the beginning of the comment */
948 dest->comment = dest->name + dest->namelen + 1;
950 /* Copy the comment */
951 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
952 dest->comment[max_namebuf_len - 1] = '\0';
954 /* Save other fields */
955 dest->smbc_type = src->smbc_type;
956 dest->commentlen = strlen(dest->comment);
957 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
958 (char *) dest);
959 } else {
961 /* No encoding. Just copy the entry as is. */
962 memcpy(dest, src, src->dirlen);
963 dest->comment = (char *)(&dest->name + src->namelen + 1);
969 * Routine to get a directory entry
972 struct smbc_dirent *
973 SMBC_readdir_ctx(SMBCCTX *context,
974 SMBCFILE *dir)
976 int maxlen;
977 struct smbc_dirent *dirp, *dirent;
978 TALLOC_CTX *frame = talloc_stackframe();
980 /* Check that all is ok first ... */
982 if (!context || !context->internal->initialized) {
984 errno = EINVAL;
985 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
986 TALLOC_FREE(frame);
987 return NULL;
991 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
993 errno = EBADF;
994 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
995 TALLOC_FREE(frame);
996 return NULL;
1000 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1002 errno = ENOTDIR;
1003 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1004 TALLOC_FREE(frame);
1005 return NULL;
1009 if (!dir->dir_next) {
1010 TALLOC_FREE(frame);
1011 return NULL;
1014 dirent = dir->dir_next->dirent;
1015 if (!dirent) {
1017 errno = ENOENT;
1018 TALLOC_FREE(frame);
1019 return NULL;
1023 dirp = &context->internal->dirent;
1024 maxlen = sizeof(context->internal->_dirent_name);
1026 smbc_readdir_internal(context, dirp, dirent, maxlen);
1028 dir->dir_next = dir->dir_next->next;
1030 TALLOC_FREE(frame);
1031 return dirp;
1035 * Routine to get directory entries
1039 SMBC_getdents_ctx(SMBCCTX *context,
1040 SMBCFILE *dir,
1041 struct smbc_dirent *dirp,
1042 int count)
1044 int rem = count;
1045 int reqd;
1046 int maxlen;
1047 char *ndir = (char *)dirp;
1048 struct smbc_dir_list *dirlist;
1049 TALLOC_CTX *frame = talloc_stackframe();
1051 /* Check that all is ok first ... */
1053 if (!context || !context->internal->initialized) {
1055 errno = EINVAL;
1056 TALLOC_FREE(frame);
1057 return -1;
1061 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1063 errno = EBADF;
1064 TALLOC_FREE(frame);
1065 return -1;
1069 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1071 errno = ENOTDIR;
1072 TALLOC_FREE(frame);
1073 return -1;
1078 * Now, retrieve the number of entries that will fit in what was passed
1079 * We have to figure out if the info is in the list, or we need to
1080 * send a request to the server to get the info.
1083 while ((dirlist = dir->dir_next)) {
1084 struct smbc_dirent *dirent;
1085 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1087 if (!dirlist->dirent) {
1089 errno = ENOENT; /* Bad error */
1090 TALLOC_FREE(frame);
1091 return -1;
1095 /* Do urlencoding of next entry, if so selected */
1096 dirent = &context->internal->dirent;
1097 maxlen = sizeof(context->internal->_dirent_name);
1098 smbc_readdir_internal(context, dirent,
1099 dirlist->dirent, maxlen);
1101 reqd = dirent->dirlen;
1103 if (rem < reqd) {
1105 if (rem < count) { /* We managed to copy something */
1107 errno = 0;
1108 TALLOC_FREE(frame);
1109 return count - rem;
1112 else { /* Nothing copied ... */
1114 errno = EINVAL; /* Not enough space ... */
1115 TALLOC_FREE(frame);
1116 return -1;
1122 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1124 currentEntry->comment = &currentEntry->name[0] +
1125 dirent->namelen + 1;
1127 ndir += reqd;
1128 rem -= reqd;
1130 /* Try and align the struct for the next entry
1131 on a valid pointer boundary by appending zeros */
1132 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1133 *ndir = '\0';
1134 rem--;
1135 ndir++;
1136 currentEntry->dirlen++;
1139 dir->dir_next = dirlist = dirlist -> next;
1142 TALLOC_FREE(frame);
1144 if (rem == count)
1145 return 0;
1146 else
1147 return count - rem;
1152 * Routine to create a directory ...
1156 SMBC_mkdir_ctx(SMBCCTX *context,
1157 const char *fname,
1158 mode_t mode)
1160 SMBCSRV *srv = NULL;
1161 char *server = NULL;
1162 char *share = NULL;
1163 char *user = NULL;
1164 char *password = NULL;
1165 char *workgroup = NULL;
1166 char *path = NULL;
1167 char *targetpath = NULL;
1168 uint16_t port = 0;
1169 struct cli_state *targetcli = NULL;
1170 TALLOC_CTX *frame = talloc_stackframe();
1171 NTSTATUS status;
1173 if (!context || !context->internal->initialized) {
1174 errno = EINVAL;
1175 TALLOC_FREE(frame);
1176 return -1;
1179 if (!fname) {
1180 errno = EINVAL;
1181 TALLOC_FREE(frame);
1182 return -1;
1185 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1187 if (SMBC_parse_path(frame,
1188 context,
1189 fname,
1190 &workgroup,
1191 &server,
1192 &port,
1193 &share,
1194 &path,
1195 &user,
1196 &password,
1197 NULL)) {
1198 errno = EINVAL;
1199 TALLOC_FREE(frame);
1200 return -1;
1203 if (!user || user[0] == (char)0) {
1204 user = talloc_strdup(frame, smbc_getUser(context));
1205 if (!user) {
1206 errno = ENOMEM;
1207 TALLOC_FREE(frame);
1208 return -1;
1212 srv = SMBC_server(frame, context, True,
1213 server, port, share, &workgroup, &user, &password);
1215 if (!srv) {
1217 TALLOC_FREE(frame);
1218 return -1; /* errno set by SMBC_server */
1222 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1223 status = cli_resolve_path(frame, "", context->internal->auth_info,
1224 srv->cli, path, &targetcli, &targetpath);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 d_printf("Could not resolve %s\n", path);
1227 errno = ENOENT;
1228 TALLOC_FREE(frame);
1229 return -1;
1231 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1233 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1234 errno = SMBC_errno(context, targetcli);
1235 TALLOC_FREE(frame);
1236 return -1;
1240 TALLOC_FREE(frame);
1241 return 0;
1246 * Our list function simply checks to see if a directory is not empty
1249 static NTSTATUS
1250 rmdir_list_fn(const char *mnt,
1251 struct file_info *finfo,
1252 const char *mask,
1253 void *state)
1255 if (strncmp(finfo->name, ".", 1) != 0 &&
1256 strncmp(finfo->name, "..", 2) != 0) {
1257 bool *smbc_rmdir_dirempty = (bool *)state;
1258 *smbc_rmdir_dirempty = false;
1260 return NT_STATUS_OK;
1264 * Routine to remove a directory
1268 SMBC_rmdir_ctx(SMBCCTX *context,
1269 const char *fname)
1271 SMBCSRV *srv = NULL;
1272 char *server = NULL;
1273 char *share = NULL;
1274 char *user = NULL;
1275 char *password = NULL;
1276 char *workgroup = NULL;
1277 char *path = NULL;
1278 char *targetpath = NULL;
1279 uint16_t port = 0;
1280 struct cli_state *targetcli = NULL;
1281 TALLOC_CTX *frame = talloc_stackframe();
1282 NTSTATUS status;
1284 if (!context || !context->internal->initialized) {
1285 errno = EINVAL;
1286 TALLOC_FREE(frame);
1287 return -1;
1290 if (!fname) {
1291 errno = EINVAL;
1292 TALLOC_FREE(frame);
1293 return -1;
1296 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1298 if (SMBC_parse_path(frame,
1299 context,
1300 fname,
1301 &workgroup,
1302 &server,
1303 &port,
1304 &share,
1305 &path,
1306 &user,
1307 &password,
1308 NULL)) {
1309 errno = EINVAL;
1310 TALLOC_FREE(frame);
1311 return -1;
1314 if (!user || user[0] == (char)0) {
1315 user = talloc_strdup(frame, smbc_getUser(context));
1316 if (!user) {
1317 errno = ENOMEM;
1318 TALLOC_FREE(frame);
1319 return -1;
1323 srv = SMBC_server(frame, context, True,
1324 server, port, share, &workgroup, &user, &password);
1326 if (!srv) {
1328 TALLOC_FREE(frame);
1329 return -1; /* errno set by SMBC_server */
1333 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1334 status = cli_resolve_path(frame, "", context->internal->auth_info,
1335 srv->cli, path, &targetcli, &targetpath);
1336 if (!NT_STATUS_IS_OK(status)) {
1337 d_printf("Could not resolve %s\n", path);
1338 errno = ENOENT;
1339 TALLOC_FREE(frame);
1340 return -1;
1342 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1344 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1346 errno = SMBC_errno(context, targetcli);
1348 if (errno == EACCES) { /* Check if the dir empty or not */
1350 /* Local storage to avoid buffer overflows */
1351 char *lpath;
1352 bool smbc_rmdir_dirempty = true;
1354 lpath = talloc_asprintf(frame, "%s\\*",
1355 targetpath);
1356 if (!lpath) {
1357 errno = ENOMEM;
1358 TALLOC_FREE(frame);
1359 return -1;
1362 status = cli_list(targetcli, lpath,
1363 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1364 rmdir_list_fn,
1365 &smbc_rmdir_dirempty);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 /* Fix errno to ignore latest error ... */
1369 DEBUG(5, ("smbc_rmdir: "
1370 "cli_list returned an error: %d\n",
1371 SMBC_errno(context, targetcli)));
1372 errno = EACCES;
1376 if (smbc_rmdir_dirempty)
1377 errno = EACCES;
1378 else
1379 errno = ENOTEMPTY;
1383 TALLOC_FREE(frame);
1384 return -1;
1388 TALLOC_FREE(frame);
1389 return 0;
1394 * Routine to return the current directory position
1397 off_t
1398 SMBC_telldir_ctx(SMBCCTX *context,
1399 SMBCFILE *dir)
1401 TALLOC_CTX *frame = talloc_stackframe();
1403 if (!context || !context->internal->initialized) {
1405 errno = EINVAL;
1406 TALLOC_FREE(frame);
1407 return -1;
1411 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1413 errno = EBADF;
1414 TALLOC_FREE(frame);
1415 return -1;
1419 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1421 errno = ENOTDIR;
1422 TALLOC_FREE(frame);
1423 return -1;
1427 /* See if we're already at the end. */
1428 if (dir->dir_next == NULL) {
1429 /* We are. */
1430 TALLOC_FREE(frame);
1431 return -1;
1435 * We return the pointer here as the offset
1437 TALLOC_FREE(frame);
1438 return (off_t)(long)dir->dir_next->dirent;
1442 * A routine to run down the list and see if the entry is OK
1445 static struct smbc_dir_list *
1446 check_dir_ent(struct smbc_dir_list *list,
1447 struct smbc_dirent *dirent)
1450 /* Run down the list looking for what we want */
1452 if (dirent) {
1454 struct smbc_dir_list *tmp = list;
1456 while (tmp) {
1458 if (tmp->dirent == dirent)
1459 return tmp;
1461 tmp = tmp->next;
1467 return NULL; /* Not found, or an error */
1473 * Routine to seek on a directory
1477 SMBC_lseekdir_ctx(SMBCCTX *context,
1478 SMBCFILE *dir,
1479 off_t offset)
1481 long int l_offset = offset; /* Handle problems of size */
1482 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1483 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1484 TALLOC_CTX *frame = talloc_stackframe();
1486 if (!context || !context->internal->initialized) {
1488 errno = EINVAL;
1489 TALLOC_FREE(frame);
1490 return -1;
1494 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1496 errno = ENOTDIR;
1497 TALLOC_FREE(frame);
1498 return -1;
1502 /* Now, check what we were passed and see if it is OK ... */
1504 if (dirent == NULL) { /* Seek to the begining of the list */
1506 dir->dir_next = dir->dir_list;
1507 TALLOC_FREE(frame);
1508 return 0;
1512 if (offset == -1) { /* Seek to the end of the list */
1513 dir->dir_next = NULL;
1514 TALLOC_FREE(frame);
1515 return 0;
1518 /* Now, run down the list and make sure that the entry is OK */
1519 /* This may need to be changed if we change the format of the list */
1521 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1522 errno = EINVAL; /* Bad entry */
1523 TALLOC_FREE(frame);
1524 return -1;
1527 dir->dir_next = list_ent;
1529 TALLOC_FREE(frame);
1530 return 0;
1534 * Routine to fstat a dir
1538 SMBC_fstatdir_ctx(SMBCCTX *context,
1539 SMBCFILE *dir,
1540 struct stat *st)
1543 if (!context || !context->internal->initialized) {
1545 errno = EINVAL;
1546 return -1;
1549 /* No code yet ... */
1550 return 0;
1554 SMBC_chmod_ctx(SMBCCTX *context,
1555 const char *fname,
1556 mode_t newmode)
1558 SMBCSRV *srv = NULL;
1559 char *server = NULL;
1560 char *share = NULL;
1561 char *user = NULL;
1562 char *password = NULL;
1563 char *workgroup = NULL;
1564 char *targetpath = NULL;
1565 struct cli_state *targetcli = NULL;
1566 char *path = NULL;
1567 uint16_t mode;
1568 uint16_t port = 0;
1569 TALLOC_CTX *frame = talloc_stackframe();
1570 NTSTATUS status;
1572 if (!context || !context->internal->initialized) {
1574 errno = EINVAL; /* Best I can think of ... */
1575 TALLOC_FREE(frame);
1576 return -1;
1579 if (!fname) {
1580 errno = EINVAL;
1581 TALLOC_FREE(frame);
1582 return -1;
1585 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1587 if (SMBC_parse_path(frame,
1588 context,
1589 fname,
1590 &workgroup,
1591 &server,
1592 &port,
1593 &share,
1594 &path,
1595 &user,
1596 &password,
1597 NULL)) {
1598 errno = EINVAL;
1599 TALLOC_FREE(frame);
1600 return -1;
1603 if (!user || user[0] == (char)0) {
1604 user = talloc_strdup(frame, smbc_getUser(context));
1605 if (!user) {
1606 errno = ENOMEM;
1607 TALLOC_FREE(frame);
1608 return -1;
1612 srv = SMBC_server(frame, context, True,
1613 server, port, share, &workgroup, &user, &password);
1615 if (!srv) {
1616 TALLOC_FREE(frame);
1617 return -1; /* errno set by SMBC_server */
1620 /*d_printf(">>>unlink: resolving %s\n", path);*/
1621 status = cli_resolve_path(frame, "", context->internal->auth_info,
1622 srv->cli, path, &targetcli, &targetpath);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 d_printf("Could not resolve %s\n", path);
1625 errno = ENOENT;
1626 TALLOC_FREE(frame);
1627 return -1;
1630 mode = 0;
1632 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1633 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1634 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1635 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1637 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1638 errno = SMBC_errno(context, targetcli);
1639 TALLOC_FREE(frame);
1640 return -1;
1643 TALLOC_FREE(frame);
1644 return 0;
1648 SMBC_utimes_ctx(SMBCCTX *context,
1649 const char *fname,
1650 struct timeval *tbuf)
1652 SMBCSRV *srv = NULL;
1653 char *server = NULL;
1654 char *share = NULL;
1655 char *user = NULL;
1656 char *password = NULL;
1657 char *workgroup = NULL;
1658 char *path = NULL;
1659 time_t access_time;
1660 time_t write_time;
1661 uint16_t port = 0;
1662 TALLOC_CTX *frame = talloc_stackframe();
1664 if (!context || !context->internal->initialized) {
1666 errno = EINVAL; /* Best I can think of ... */
1667 TALLOC_FREE(frame);
1668 return -1;
1671 if (!fname) {
1672 errno = EINVAL;
1673 TALLOC_FREE(frame);
1674 return -1;
1677 if (tbuf == NULL) {
1678 access_time = write_time = time(NULL);
1679 } else {
1680 access_time = tbuf[0].tv_sec;
1681 write_time = tbuf[1].tv_sec;
1684 if (DEBUGLVL(4)) {
1685 char *p;
1686 char atimebuf[32];
1687 char mtimebuf[32];
1689 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1690 atimebuf[sizeof(atimebuf) - 1] = '\0';
1691 if ((p = strchr(atimebuf, '\n')) != NULL) {
1692 *p = '\0';
1695 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1696 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1697 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1698 *p = '\0';
1701 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1702 fname, atimebuf, mtimebuf);
1705 if (SMBC_parse_path(frame,
1706 context,
1707 fname,
1708 &workgroup,
1709 &server,
1710 &port,
1711 &share,
1712 &path,
1713 &user,
1714 &password,
1715 NULL)) {
1716 errno = EINVAL;
1717 TALLOC_FREE(frame);
1718 return -1;
1721 if (!user || user[0] == (char)0) {
1722 user = talloc_strdup(frame, smbc_getUser(context));
1723 if (!user) {
1724 errno = ENOMEM;
1725 TALLOC_FREE(frame);
1726 return -1;
1730 srv = SMBC_server(frame, context, True,
1731 server, port, share, &workgroup, &user, &password);
1733 if (!srv) {
1734 TALLOC_FREE(frame);
1735 return -1; /* errno set by SMBC_server */
1738 if (!SMBC_setatr(context, srv, path,
1739 0, access_time, write_time, 0, 0)) {
1740 TALLOC_FREE(frame);
1741 return -1; /* errno set by SMBC_setatr */
1744 TALLOC_FREE(frame);
1745 return 0;
1749 * Routine to unlink() a file
1753 SMBC_unlink_ctx(SMBCCTX *context,
1754 const char *fname)
1756 char *server = NULL;
1757 char *share = NULL;
1758 char *user = NULL;
1759 char *password = NULL;
1760 char *workgroup = NULL;
1761 char *path = NULL;
1762 char *targetpath = NULL;
1763 uint16_t port = 0;
1764 struct cli_state *targetcli = NULL;
1765 SMBCSRV *srv = NULL;
1766 TALLOC_CTX *frame = talloc_stackframe();
1767 NTSTATUS status;
1769 if (!context || !context->internal->initialized) {
1771 errno = EINVAL; /* Best I can think of ... */
1772 TALLOC_FREE(frame);
1773 return -1;
1777 if (!fname) {
1778 errno = EINVAL;
1779 TALLOC_FREE(frame);
1780 return -1;
1784 if (SMBC_parse_path(frame,
1785 context,
1786 fname,
1787 &workgroup,
1788 &server,
1789 &port,
1790 &share,
1791 &path,
1792 &user,
1793 &password,
1794 NULL)) {
1795 errno = EINVAL;
1796 TALLOC_FREE(frame);
1797 return -1;
1800 if (!user || user[0] == (char)0) {
1801 user = talloc_strdup(frame, smbc_getUser(context));
1802 if (!user) {
1803 errno = ENOMEM;
1804 TALLOC_FREE(frame);
1805 return -1;
1809 srv = SMBC_server(frame, context, True,
1810 server, port, share, &workgroup, &user, &password);
1812 if (!srv) {
1813 TALLOC_FREE(frame);
1814 return -1; /* SMBC_server sets errno */
1818 /*d_printf(">>>unlink: resolving %s\n", path);*/
1819 status = cli_resolve_path(frame, "", context->internal->auth_info,
1820 srv->cli, path, &targetcli, &targetpath);
1821 if (!NT_STATUS_IS_OK(status)) {
1822 d_printf("Could not resolve %s\n", path);
1823 errno = ENOENT;
1824 TALLOC_FREE(frame);
1825 return -1;
1827 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1829 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1831 errno = SMBC_errno(context, targetcli);
1833 if (errno == EACCES) { /* Check if the file is a directory */
1835 int saverr = errno;
1836 off_t size = 0;
1837 uint16_t mode = 0;
1838 struct timespec write_time_ts;
1839 struct timespec access_time_ts;
1840 struct timespec change_time_ts;
1841 SMB_INO_T ino = 0;
1843 if (!SMBC_getatr(context, srv, path, &mode, &size,
1844 NULL,
1845 &access_time_ts,
1846 &write_time_ts,
1847 &change_time_ts,
1848 &ino)) {
1850 /* Hmmm, bad error ... What? */
1852 errno = SMBC_errno(context, targetcli);
1853 TALLOC_FREE(frame);
1854 return -1;
1857 else {
1859 if (IS_DOS_DIR(mode))
1860 errno = EISDIR;
1861 else
1862 errno = saverr; /* Restore this */
1867 TALLOC_FREE(frame);
1868 return -1;
1872 TALLOC_FREE(frame);
1873 return 0; /* Success ... */
1878 * Routine to rename() a file
1882 SMBC_rename_ctx(SMBCCTX *ocontext,
1883 const char *oname,
1884 SMBCCTX *ncontext,
1885 const char *nname)
1887 char *server1 = NULL;
1888 char *share1 = NULL;
1889 char *server2 = NULL;
1890 char *share2 = NULL;
1891 char *user1 = NULL;
1892 char *user2 = NULL;
1893 char *password1 = NULL;
1894 char *password2 = NULL;
1895 char *workgroup = NULL;
1896 char *path1 = NULL;
1897 char *path2 = NULL;
1898 char *targetpath1 = NULL;
1899 char *targetpath2 = NULL;
1900 struct cli_state *targetcli1 = NULL;
1901 struct cli_state *targetcli2 = NULL;
1902 SMBCSRV *srv = NULL;
1903 uint16_t port1 = 0;
1904 uint16_t port2 = 0;
1905 TALLOC_CTX *frame = talloc_stackframe();
1906 NTSTATUS status;
1908 if (!ocontext || !ncontext ||
1909 !ocontext->internal->initialized ||
1910 !ncontext->internal->initialized) {
1912 errno = EINVAL; /* Best I can think of ... */
1913 TALLOC_FREE(frame);
1914 return -1;
1917 if (!oname || !nname) {
1918 errno = EINVAL;
1919 TALLOC_FREE(frame);
1920 return -1;
1923 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1925 if (SMBC_parse_path(frame,
1926 ocontext,
1927 oname,
1928 &workgroup,
1929 &server1,
1930 &port1,
1931 &share1,
1932 &path1,
1933 &user1,
1934 &password1,
1935 NULL)) {
1936 errno = EINVAL;
1937 TALLOC_FREE(frame);
1938 return -1;
1941 if (!user1 || user1[0] == (char)0) {
1942 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1943 if (!user1) {
1944 errno = ENOMEM;
1945 TALLOC_FREE(frame);
1946 return -1;
1950 if (SMBC_parse_path(frame,
1951 ncontext,
1952 nname,
1953 NULL,
1954 &server2,
1955 &port2,
1956 &share2,
1957 &path2,
1958 &user2,
1959 &password2,
1960 NULL)) {
1961 errno = EINVAL;
1962 TALLOC_FREE(frame);
1963 return -1;
1966 if (!user2 || user2[0] == (char)0) {
1967 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1968 if (!user2) {
1969 errno = ENOMEM;
1970 TALLOC_FREE(frame);
1971 return -1;
1975 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1976 strcmp(user1, user2)) {
1977 /* Can't rename across file systems, or users?? */
1978 errno = EXDEV;
1979 TALLOC_FREE(frame);
1980 return -1;
1983 srv = SMBC_server(frame, ocontext, True,
1984 server1, port1, share1, &workgroup, &user1, &password1);
1985 if (!srv) {
1986 TALLOC_FREE(frame);
1987 return -1;
1991 /* set the credentials to make DFS work */
1992 smbc_set_credentials_with_fallback(ocontext,
1993 workgroup,
1994 user1,
1995 password1);
1997 /*d_printf(">>>rename: resolving %s\n", path1);*/
1998 status = cli_resolve_path(frame, "", ocontext->internal->auth_info,
1999 srv->cli, path1, &targetcli1, &targetpath1);
2000 if (!NT_STATUS_IS_OK(status)) {
2001 d_printf("Could not resolve %s\n", path1);
2002 errno = ENOENT;
2003 TALLOC_FREE(frame);
2004 return -1;
2007 /* set the credentials to make DFS work */
2008 smbc_set_credentials_with_fallback(ncontext,
2009 workgroup,
2010 user2,
2011 password2);
2013 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2014 /*d_printf(">>>rename: resolving %s\n", path2);*/
2015 status = cli_resolve_path(frame, "", ncontext->internal->auth_info,
2016 srv->cli, path2, &targetcli2, &targetpath2);
2017 if (!NT_STATUS_IS_OK(status)) {
2018 d_printf("Could not resolve %s\n", path2);
2019 errno = ENOENT;
2020 TALLOC_FREE(frame);
2021 return -1;
2023 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2025 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2026 strcmp(targetcli1->share, targetcli2->share))
2028 /* can't rename across file systems */
2029 errno = EXDEV;
2030 TALLOC_FREE(frame);
2031 return -1;
2034 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2035 int eno = SMBC_errno(ocontext, targetcli1);
2037 if (eno != EEXIST ||
2038 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2039 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2041 errno = eno;
2042 TALLOC_FREE(frame);
2043 return -1;
2048 TALLOC_FREE(frame);
2049 return 0; /* Success */
2052 struct smbc_notify_cb_state {
2053 struct tevent_context *ev;
2054 struct cli_state *cli;
2055 uint16_t fnum;
2056 bool recursive;
2057 uint32_t completion_filter;
2058 unsigned callback_timeout_ms;
2059 smbc_notify_callback_fn cb;
2060 void *private_data;
2063 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2064 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2066 static struct tevent_req *smbc_notify_cb_send(
2067 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2068 uint16_t fnum, bool recursive, uint32_t completion_filter,
2069 unsigned callback_timeout_ms,
2070 smbc_notify_callback_fn cb, void *private_data)
2072 struct tevent_req *req, *subreq;
2073 struct smbc_notify_cb_state *state;
2075 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2076 if (req == NULL) {
2077 return NULL;
2079 state->ev = ev;
2080 state->cli = cli;
2081 state->fnum = fnum;
2082 state->recursive = recursive;
2083 state->completion_filter = completion_filter;
2084 state->callback_timeout_ms = callback_timeout_ms;
2085 state->cb = cb;
2086 state->private_data = private_data;
2088 subreq = cli_notify_send(
2089 state, state->ev, state->cli, state->fnum, 1000,
2090 state->completion_filter, state->recursive);
2091 if (tevent_req_nomem(subreq, req)) {
2092 return tevent_req_post(req, ev);
2094 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2096 if (state->callback_timeout_ms == 0) {
2097 return req;
2100 subreq = tevent_wakeup_send(
2101 state, state->ev,
2102 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2103 state->callback_timeout_ms*1000));
2104 if (tevent_req_nomem(subreq, req)) {
2105 return tevent_req_post(req, ev);
2107 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2109 return req;
2112 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2114 struct tevent_req *req = tevent_req_callback_data(
2115 subreq, struct tevent_req);
2116 struct smbc_notify_cb_state *state = tevent_req_data(
2117 req, struct smbc_notify_cb_state);
2118 uint32_t num_changes;
2119 struct notify_change *changes;
2120 NTSTATUS status;
2121 int cb_ret;
2123 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2124 TALLOC_FREE(subreq);
2125 if (tevent_req_nterror(req, status)) {
2126 return;
2130 struct smbc_notify_callback_action actions[num_changes];
2131 uint32_t i;
2133 for (i=0; i<num_changes; i++) {
2134 actions[i].action = changes[i].action;
2135 actions[i].filename = changes[i].name;
2138 cb_ret = state->cb(actions, num_changes, state->private_data);
2141 TALLOC_FREE(changes);
2143 if (cb_ret != 0) {
2144 tevent_req_done(req);
2145 return;
2148 subreq = cli_notify_send(
2149 state, state->ev, state->cli, state->fnum, 1000,
2150 state->completion_filter, state->recursive);
2151 if (tevent_req_nomem(subreq, req)) {
2152 return;
2154 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2157 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2159 struct tevent_req *req = tevent_req_callback_data(
2160 subreq, struct tevent_req);
2161 struct smbc_notify_cb_state *state = tevent_req_data(
2162 req, struct smbc_notify_cb_state);
2163 int cb_ret;
2164 bool ok;
2166 ok = tevent_wakeup_recv(subreq);
2167 TALLOC_FREE(subreq);
2168 if (!ok) {
2169 tevent_req_oom(req);
2170 return;
2173 cb_ret = state->cb(NULL, 0, state->private_data);
2174 if (cb_ret != 0) {
2175 tevent_req_done(req);
2176 return;
2179 subreq = tevent_wakeup_send(
2180 state, state->ev,
2181 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2182 state->callback_timeout_ms*1000));
2183 if (tevent_req_nomem(subreq, req)) {
2184 return;
2186 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2189 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2191 return tevent_req_simple_recv_ntstatus(req);
2194 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2195 bool recursive, uint32_t completion_filter,
2196 unsigned callback_timeout_ms,
2197 smbc_notify_callback_fn cb, void *private_data)
2199 TALLOC_CTX *frame = talloc_stackframe();
2200 struct tevent_context *ev;
2201 struct tevent_req *req;
2202 NTSTATUS status = NT_STATUS_NO_MEMORY;
2204 ev = samba_tevent_context_init(frame);
2205 if (ev == NULL) {
2206 goto fail;
2208 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2209 completion_filter,
2210 callback_timeout_ms, cb, private_data);
2211 if (req == NULL) {
2212 goto fail;
2214 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2215 goto fail;
2217 status = smbc_notify_cb_recv(req);
2218 TALLOC_FREE(req);
2219 fail:
2220 TALLOC_FREE(frame);
2221 return status;
2225 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2226 uint32_t completion_filter, unsigned callback_timeout_ms,
2227 smbc_notify_callback_fn cb, void *private_data)
2229 TALLOC_CTX *frame = talloc_stackframe();
2230 struct cli_state *cli;
2231 char *server = NULL;
2232 char *share = NULL;
2233 char *user = NULL;
2234 char *password = NULL;
2235 char *options = NULL;
2236 char *workgroup = NULL;
2237 char *path = NULL;
2238 uint16_t port;
2239 NTSTATUS status;
2240 uint16_t fnum;
2242 if ((context == NULL) || !context->internal->initialized) {
2243 TALLOC_FREE(frame);
2244 errno = EINVAL;
2245 return -1;
2247 if ((dir == NULL) ||
2248 !SMBC_dlist_contains(context->internal->files, dir)) {
2249 TALLOC_FREE(frame);
2250 errno = EBADF;
2251 return -1;
2254 if (SMBC_parse_path(frame,
2255 context,
2256 dir->fname,
2257 &workgroup,
2258 &server,
2259 &port,
2260 &share,
2261 &path,
2262 &user,
2263 &password,
2264 &options)) {
2265 DEBUG(4, ("no valid path\n"));
2266 TALLOC_FREE(frame);
2267 errno = EINVAL + 8194;
2268 return -1;
2271 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2272 "path='%s' options='%s'\n",
2273 dir->fname, server, share, path, options));
2275 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2276 (int)recursive, completion_filter));
2278 cli = dir->srv->cli;
2279 status = cli_ntcreate(
2280 cli, path, 0, FILE_READ_DATA, 0,
2281 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2282 FILE_OPEN, 0, 0, &fnum, NULL);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 int err = SMBC_errno(context, cli);
2285 TALLOC_FREE(frame);
2286 errno = err;
2287 return -1;
2290 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2291 callback_timeout_ms, cb, private_data);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 int err = SMBC_errno(context, cli);
2294 cli_close(cli, fnum);
2295 TALLOC_FREE(frame);
2296 errno = err;
2297 return -1;
2300 cli_close(cli, fnum);
2302 TALLOC_FREE(frame);
2303 return 0;