build: Move uid_wrapper to third_party
[Samba.git] / source3 / libsmb / libsmb_dir.c
blob72441c4673649cbe1289a2b56f0d77808733e4b1
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_GEN_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 size_t path_len = 0;
383 uint16_t mode;
384 uint16_t port = 0;
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 u_info = user_auth_info_init(frame);
487 if (u_info == NULL) {
488 if (dir) {
489 SAFE_FREE(dir->fname);
490 SAFE_FREE(dir);
492 TALLOC_FREE(frame);
493 errno = ENOMEM;
494 return NULL;
496 set_cmdline_auth_info_username(u_info, user);
497 set_cmdline_auth_info_password(u_info, password);
500 * We have server and share and path empty but options
501 * requesting that we scan all master browsers for their list
502 * of workgroups/domains. This implies that we must first try
503 * broadcast queries to find all master browsers, and if that
504 * doesn't work, then try our other methods which return only
505 * a single master browser.
508 ip_list = NULL;
509 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
510 &ip_list, &count);
511 if (!NT_STATUS_IS_OK(status))
514 TALLOC_FREE(ip_list);
516 if (!find_master_ip(workgroup, &server_addr)) {
518 if (dir) {
519 SAFE_FREE(dir->fname);
520 SAFE_FREE(dir);
522 TALLOC_FREE(frame);
523 errno = ENOENT;
524 return NULL;
527 ip_list = (struct sockaddr_storage *)talloc_memdup(
528 talloc_tos(), &server_addr,
529 sizeof(server_addr));
530 if (ip_list == NULL) {
531 if (dir) {
532 SAFE_FREE(dir->fname);
533 SAFE_FREE(dir);
535 TALLOC_FREE(frame);
536 errno = ENOMEM;
537 return NULL;
539 count = 1;
542 for (i = 0; i < count && i < max_lmb_count; i++) {
543 char addr[INET6_ADDRSTRLEN];
544 char *wg_ptr = NULL;
545 struct cli_state *cli = NULL;
547 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
548 DEBUG(99, ("Found master browser %d of %d: %s\n",
549 i+1, MAX(count, max_lmb_count),
550 addr));
552 cli = get_ipc_connect_master_ip(talloc_tos(),
553 &ip_list[i],
554 u_info,
555 &wg_ptr);
556 /* cli == NULL is the master browser refused to talk or
557 could not be found */
558 if (!cli) {
559 continue;
562 workgroup = talloc_strdup(frame, wg_ptr);
563 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
565 cli_shutdown(cli);
567 if (!workgroup || !server) {
568 if (dir) {
569 SAFE_FREE(dir->fname);
570 SAFE_FREE(dir);
572 TALLOC_FREE(frame);
573 errno = ENOMEM;
574 return NULL;
577 DEBUG(4, ("using workgroup %s %s\n",
578 workgroup, server));
581 * For each returned master browser IP address, get a
582 * connection to IPC$ on the server if we do not
583 * already have one, and determine the
584 * workgroups/domains that it knows about.
587 srv = SMBC_server(frame, context, True, server, port, "IPC$",
588 &workgroup, &user, &password);
589 if (!srv) {
590 continue;
593 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
594 continue;
597 dir->srv = srv;
598 dir->dir_type = SMBC_WORKGROUP;
600 /* Now, list the stuff ... */
602 if (!cli_NetServerEnum(srv->cli,
603 workgroup,
604 SV_TYPE_DOMAIN_ENUM,
605 list_unique_wg_fn,
606 (void *)dir)) {
607 continue;
611 TALLOC_FREE(ip_list);
612 } else {
614 * Server not an empty string ... Check the rest and see what
615 * gives
617 if (*share == '\0') {
618 if (*path != '\0') {
620 /* Should not have empty share with path */
621 if (dir) {
622 SAFE_FREE(dir->fname);
623 SAFE_FREE(dir);
625 TALLOC_FREE(frame);
626 errno = EINVAL + 8197;
627 return NULL;
632 * We don't know if <server> is really a server name
633 * or is a workgroup/domain name. If we already have
634 * a server structure for it, we'll use it.
635 * Otherwise, check to see if <server><1D>,
636 * <server><1B>, or <server><20> translates. We check
637 * to see if <server> is an IP address first.
641 * See if we have an existing server. Do not
642 * establish a connection if one does not already
643 * exist.
645 srv = SMBC_server(frame, context, False,
646 server, port, "IPC$",
647 &workgroup, &user, &password);
650 * If no existing server and not an IP addr, look for
651 * LMB or DMB
653 if (!srv &&
654 !is_ipaddress(server) &&
655 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
656 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
658 * "server" is actually a workgroup name,
659 * not a server. Make this clear.
661 char *wgroup = server;
662 fstring buserver;
664 dir->dir_type = SMBC_SERVER;
667 * Get the backup list ...
669 if (!name_status_find(wgroup, 0, 0,
670 &rem_ss, buserver)) {
671 char addr[INET6_ADDRSTRLEN];
673 print_sockaddr(addr, sizeof(addr), &rem_ss);
674 DEBUG(0,("Could not get name of "
675 "local/domain master browser "
676 "for workgroup %s from "
677 "address %s\n",
678 wgroup,
679 addr));
680 if (dir) {
681 SAFE_FREE(dir->fname);
682 SAFE_FREE(dir);
684 TALLOC_FREE(frame);
685 errno = EPERM;
686 return NULL;
691 * Get a connection to IPC$ on the server if
692 * we do not already have one
694 srv = SMBC_server(frame, context, True,
695 buserver, port, "IPC$",
696 &workgroup,
697 &user, &password);
698 if (!srv) {
699 DEBUG(0, ("got no contact to IPC$\n"));
700 if (dir) {
701 SAFE_FREE(dir->fname);
702 SAFE_FREE(dir);
704 TALLOC_FREE(frame);
705 return NULL;
709 dir->srv = srv;
711 if (smbXcli_conn_protocol(srv->cli->conn) > PROTOCOL_NT1) {
712 if (dir) {
713 SAFE_FREE(dir->fname);
714 SAFE_FREE(dir);
716 TALLOC_FREE(frame);
717 return NULL;
720 /* Now, list the servers ... */
721 if (!cli_NetServerEnum(srv->cli, wgroup,
722 0x0000FFFE, list_fn,
723 (void *)dir)) {
725 if (dir) {
726 SAFE_FREE(dir->fname);
727 SAFE_FREE(dir);
729 TALLOC_FREE(frame);
730 return NULL;
732 } else if (srv ||
733 (resolve_name(server, &rem_ss, 0x20, false))) {
736 * If we hadn't found the server, get one now
738 if (!srv) {
739 srv = SMBC_server(frame, context, True,
740 server, port, "IPC$",
741 &workgroup,
742 &user, &password);
745 if (!srv) {
746 if (dir) {
747 SAFE_FREE(dir->fname);
748 SAFE_FREE(dir);
750 TALLOC_FREE(frame);
751 return NULL;
755 dir->dir_type = SMBC_FILE_SHARE;
756 dir->srv = srv;
758 /* List the shares ... */
760 if (net_share_enum_rpc(
761 srv->cli,
762 list_fn,
763 (void *) dir) < 0 &&
764 cli_RNetShareEnum(
765 srv->cli,
766 list_fn,
767 (void *)dir) < 0) {
769 errno = cli_errno(srv->cli);
770 if (dir) {
771 SAFE_FREE(dir->fname);
772 SAFE_FREE(dir);
774 TALLOC_FREE(frame);
775 return NULL;
778 } else {
779 /* Neither the workgroup nor server exists */
780 errno = ECONNREFUSED;
781 if (dir) {
782 SAFE_FREE(dir->fname);
783 SAFE_FREE(dir);
785 TALLOC_FREE(frame);
786 return NULL;
790 else {
792 * The server and share are specified ... work from
793 * there ...
795 char *targetpath;
796 struct cli_state *targetcli;
797 NTSTATUS status;
799 /* We connect to the server and list the directory */
800 dir->dir_type = SMBC_FILE_SHARE;
802 srv = SMBC_server(frame, context, True, server, port, share,
803 &workgroup, &user, &password);
805 if (!srv) {
806 if (dir) {
807 SAFE_FREE(dir->fname);
808 SAFE_FREE(dir);
810 TALLOC_FREE(frame);
811 return NULL;
814 dir->srv = srv;
816 /* Now, list the files ... */
818 path_len = strlen(path);
819 path = talloc_asprintf_append(path, "\\*");
820 if (!path) {
821 if (dir) {
822 SAFE_FREE(dir->fname);
823 SAFE_FREE(dir);
825 TALLOC_FREE(frame);
826 return NULL;
829 status = cli_resolve_path(
830 frame, "", context->internal->auth_info,
831 srv->cli, path, &targetcli, &targetpath);
832 if (!NT_STATUS_IS_OK(status)) {
833 d_printf("Could not resolve %s\n", path);
834 if (dir) {
835 SAFE_FREE(dir->fname);
836 SAFE_FREE(dir);
838 TALLOC_FREE(frame);
839 return NULL;
842 status = cli_list(targetcli, targetpath,
843 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
844 dir_list_fn, (void *)dir);
845 if (!NT_STATUS_IS_OK(status)) {
846 if (dir) {
847 SAFE_FREE(dir->fname);
848 SAFE_FREE(dir);
850 saved_errno = SMBC_errno(context, targetcli);
852 if (saved_errno == EINVAL) {
854 * See if they asked to opendir
855 * something other than a directory.
856 * If so, the converted error value we
857 * got would have been EINVAL rather
858 * than ENOTDIR.
860 path[path_len] = '\0'; /* restore original path */
862 if (SMBC_getatr(context, srv, path,
863 &mode, NULL,
864 NULL, NULL, NULL, NULL,
865 NULL) &&
866 ! IS_DOS_DIR(mode)) {
868 /* It is. Correct the error value */
869 saved_errno = ENOTDIR;
874 * If there was an error and the server is no
875 * good any more...
877 if (cli_is_error(targetcli) &&
878 smbc_getFunctionCheckServer(context)(context, srv)) {
880 /* ... then remove it. */
881 if (smbc_getFunctionRemoveUnusedServer(context)(context,
882 srv)) {
884 * We could not remove the
885 * server completely, remove
886 * it from the cache so we
887 * will not get it again. It
888 * will be removed when the
889 * last file/dir is closed.
891 smbc_getFunctionRemoveCachedServer(context)(context, srv);
895 TALLOC_FREE(frame);
896 errno = saved_errno;
897 return NULL;
903 DLIST_ADD(context->internal->files, dir);
904 TALLOC_FREE(frame);
905 return dir;
910 * Routine to close a directory
914 SMBC_closedir_ctx(SMBCCTX *context,
915 SMBCFILE *dir)
917 TALLOC_CTX *frame = talloc_stackframe();
919 if (!context || !context->internal->initialized) {
920 errno = EINVAL;
921 TALLOC_FREE(frame);
922 return -1;
925 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
926 errno = EBADF;
927 TALLOC_FREE(frame);
928 return -1;
931 remove_dir(dir); /* Clean it up */
933 DLIST_REMOVE(context->internal->files, dir);
935 if (dir) {
937 SAFE_FREE(dir->fname);
938 SAFE_FREE(dir); /* Free the space too */
941 TALLOC_FREE(frame);
942 return 0;
946 static void
947 smbc_readdir_internal(SMBCCTX * context,
948 struct smbc_dirent *dest,
949 struct smbc_dirent *src,
950 int max_namebuf_len)
952 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
954 /* url-encode the name. get back remaining buffer space */
955 max_namebuf_len =
956 smbc_urlencode(dest->name, src->name, max_namebuf_len);
958 /* We now know the name length */
959 dest->namelen = strlen(dest->name);
961 /* Save the pointer to the beginning of the comment */
962 dest->comment = dest->name + dest->namelen + 1;
964 /* Copy the comment */
965 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
966 dest->comment[max_namebuf_len - 1] = '\0';
968 /* Save other fields */
969 dest->smbc_type = src->smbc_type;
970 dest->commentlen = strlen(dest->comment);
971 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
972 (char *) dest);
973 } else {
975 /* No encoding. Just copy the entry as is. */
976 memcpy(dest, src, src->dirlen);
977 dest->comment = (char *)(&dest->name + src->namelen + 1);
983 * Routine to get a directory entry
986 struct smbc_dirent *
987 SMBC_readdir_ctx(SMBCCTX *context,
988 SMBCFILE *dir)
990 int maxlen;
991 struct smbc_dirent *dirp, *dirent;
992 TALLOC_CTX *frame = talloc_stackframe();
994 /* Check that all is ok first ... */
996 if (!context || !context->internal->initialized) {
998 errno = EINVAL;
999 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
1000 TALLOC_FREE(frame);
1001 return NULL;
1005 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1007 errno = EBADF;
1008 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1009 TALLOC_FREE(frame);
1010 return NULL;
1014 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1016 errno = ENOTDIR;
1017 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1018 TALLOC_FREE(frame);
1019 return NULL;
1023 if (!dir->dir_next) {
1024 TALLOC_FREE(frame);
1025 return NULL;
1028 dirent = dir->dir_next->dirent;
1029 if (!dirent) {
1031 errno = ENOENT;
1032 TALLOC_FREE(frame);
1033 return NULL;
1037 dirp = &context->internal->dirent;
1038 maxlen = sizeof(context->internal->_dirent_name);
1040 smbc_readdir_internal(context, dirp, dirent, maxlen);
1042 dir->dir_next = dir->dir_next->next;
1044 TALLOC_FREE(frame);
1045 return dirp;
1049 * Routine to get directory entries
1053 SMBC_getdents_ctx(SMBCCTX *context,
1054 SMBCFILE *dir,
1055 struct smbc_dirent *dirp,
1056 int count)
1058 int rem = count;
1059 int reqd;
1060 int maxlen;
1061 char *ndir = (char *)dirp;
1062 struct smbc_dir_list *dirlist;
1063 TALLOC_CTX *frame = talloc_stackframe();
1065 /* Check that all is ok first ... */
1067 if (!context || !context->internal->initialized) {
1069 errno = EINVAL;
1070 TALLOC_FREE(frame);
1071 return -1;
1075 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1077 errno = EBADF;
1078 TALLOC_FREE(frame);
1079 return -1;
1083 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1085 errno = ENOTDIR;
1086 TALLOC_FREE(frame);
1087 return -1;
1092 * Now, retrieve the number of entries that will fit in what was passed
1093 * We have to figure out if the info is in the list, or we need to
1094 * send a request to the server to get the info.
1097 while ((dirlist = dir->dir_next)) {
1098 struct smbc_dirent *dirent;
1099 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1101 if (!dirlist->dirent) {
1103 errno = ENOENT; /* Bad error */
1104 TALLOC_FREE(frame);
1105 return -1;
1109 /* Do urlencoding of next entry, if so selected */
1110 dirent = &context->internal->dirent;
1111 maxlen = sizeof(context->internal->_dirent_name);
1112 smbc_readdir_internal(context, dirent,
1113 dirlist->dirent, maxlen);
1115 reqd = dirent->dirlen;
1117 if (rem < reqd) {
1119 if (rem < count) { /* We managed to copy something */
1121 errno = 0;
1122 TALLOC_FREE(frame);
1123 return count - rem;
1126 else { /* Nothing copied ... */
1128 errno = EINVAL; /* Not enough space ... */
1129 TALLOC_FREE(frame);
1130 return -1;
1136 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1138 currentEntry->comment = &currentEntry->name[0] +
1139 dirent->namelen + 1;
1141 ndir += reqd;
1142 rem -= reqd;
1144 /* Try and align the struct for the next entry
1145 on a valid pointer boundary by appending zeros */
1146 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1147 *ndir = '\0';
1148 rem--;
1149 ndir++;
1150 currentEntry->dirlen++;
1153 dir->dir_next = dirlist = dirlist -> next;
1156 TALLOC_FREE(frame);
1158 if (rem == count)
1159 return 0;
1160 else
1161 return count - rem;
1166 * Routine to create a directory ...
1170 SMBC_mkdir_ctx(SMBCCTX *context,
1171 const char *fname,
1172 mode_t mode)
1174 SMBCSRV *srv = NULL;
1175 char *server = NULL;
1176 char *share = NULL;
1177 char *user = NULL;
1178 char *password = NULL;
1179 char *workgroup = NULL;
1180 char *path = NULL;
1181 char *targetpath = NULL;
1182 uint16_t port = 0;
1183 struct cli_state *targetcli = NULL;
1184 TALLOC_CTX *frame = talloc_stackframe();
1185 NTSTATUS status;
1187 if (!context || !context->internal->initialized) {
1188 errno = EINVAL;
1189 TALLOC_FREE(frame);
1190 return -1;
1193 if (!fname) {
1194 errno = EINVAL;
1195 TALLOC_FREE(frame);
1196 return -1;
1199 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1201 if (SMBC_parse_path(frame,
1202 context,
1203 fname,
1204 &workgroup,
1205 &server,
1206 &port,
1207 &share,
1208 &path,
1209 &user,
1210 &password,
1211 NULL)) {
1212 errno = EINVAL;
1213 TALLOC_FREE(frame);
1214 return -1;
1217 if (!user || user[0] == (char)0) {
1218 user = talloc_strdup(frame, smbc_getUser(context));
1219 if (!user) {
1220 errno = ENOMEM;
1221 TALLOC_FREE(frame);
1222 return -1;
1226 srv = SMBC_server(frame, context, True,
1227 server, port, share, &workgroup, &user, &password);
1229 if (!srv) {
1231 TALLOC_FREE(frame);
1232 return -1; /* errno set by SMBC_server */
1236 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1237 status = cli_resolve_path(frame, "", context->internal->auth_info,
1238 srv->cli, path, &targetcli, &targetpath);
1239 if (!NT_STATUS_IS_OK(status)) {
1240 d_printf("Could not resolve %s\n", path);
1241 errno = ENOENT;
1242 TALLOC_FREE(frame);
1243 return -1;
1245 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1247 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1248 errno = SMBC_errno(context, targetcli);
1249 TALLOC_FREE(frame);
1250 return -1;
1254 TALLOC_FREE(frame);
1255 return 0;
1260 * Our list function simply checks to see if a directory is not empty
1263 static NTSTATUS
1264 rmdir_list_fn(const char *mnt,
1265 struct file_info *finfo,
1266 const char *mask,
1267 void *state)
1269 if (strncmp(finfo->name, ".", 1) != 0 &&
1270 strncmp(finfo->name, "..", 2) != 0) {
1271 bool *smbc_rmdir_dirempty = (bool *)state;
1272 *smbc_rmdir_dirempty = false;
1274 return NT_STATUS_OK;
1278 * Routine to remove a directory
1282 SMBC_rmdir_ctx(SMBCCTX *context,
1283 const char *fname)
1285 SMBCSRV *srv = NULL;
1286 char *server = NULL;
1287 char *share = NULL;
1288 char *user = NULL;
1289 char *password = NULL;
1290 char *workgroup = NULL;
1291 char *path = NULL;
1292 char *targetpath = NULL;
1293 uint16_t port = 0;
1294 struct cli_state *targetcli = NULL;
1295 TALLOC_CTX *frame = talloc_stackframe();
1296 NTSTATUS status;
1298 if (!context || !context->internal->initialized) {
1299 errno = EINVAL;
1300 TALLOC_FREE(frame);
1301 return -1;
1304 if (!fname) {
1305 errno = EINVAL;
1306 TALLOC_FREE(frame);
1307 return -1;
1310 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1312 if (SMBC_parse_path(frame,
1313 context,
1314 fname,
1315 &workgroup,
1316 &server,
1317 &port,
1318 &share,
1319 &path,
1320 &user,
1321 &password,
1322 NULL)) {
1323 errno = EINVAL;
1324 TALLOC_FREE(frame);
1325 return -1;
1328 if (!user || user[0] == (char)0) {
1329 user = talloc_strdup(frame, smbc_getUser(context));
1330 if (!user) {
1331 errno = ENOMEM;
1332 TALLOC_FREE(frame);
1333 return -1;
1337 srv = SMBC_server(frame, context, True,
1338 server, port, share, &workgroup, &user, &password);
1340 if (!srv) {
1342 TALLOC_FREE(frame);
1343 return -1; /* errno set by SMBC_server */
1347 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1348 status = cli_resolve_path(frame, "", context->internal->auth_info,
1349 srv->cli, path, &targetcli, &targetpath);
1350 if (!NT_STATUS_IS_OK(status)) {
1351 d_printf("Could not resolve %s\n", path);
1352 errno = ENOENT;
1353 TALLOC_FREE(frame);
1354 return -1;
1356 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1358 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1360 errno = SMBC_errno(context, targetcli);
1362 if (errno == EACCES) { /* Check if the dir empty or not */
1364 /* Local storage to avoid buffer overflows */
1365 char *lpath;
1366 bool smbc_rmdir_dirempty = true;
1368 lpath = talloc_asprintf(frame, "%s\\*",
1369 targetpath);
1370 if (!lpath) {
1371 errno = ENOMEM;
1372 TALLOC_FREE(frame);
1373 return -1;
1376 status = cli_list(targetcli, lpath,
1377 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1378 rmdir_list_fn,
1379 &smbc_rmdir_dirempty);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 /* Fix errno to ignore latest error ... */
1383 DEBUG(5, ("smbc_rmdir: "
1384 "cli_list returned an error: %d\n",
1385 SMBC_errno(context, targetcli)));
1386 errno = EACCES;
1390 if (smbc_rmdir_dirempty)
1391 errno = EACCES;
1392 else
1393 errno = ENOTEMPTY;
1397 TALLOC_FREE(frame);
1398 return -1;
1402 TALLOC_FREE(frame);
1403 return 0;
1408 * Routine to return the current directory position
1411 off_t
1412 SMBC_telldir_ctx(SMBCCTX *context,
1413 SMBCFILE *dir)
1415 TALLOC_CTX *frame = talloc_stackframe();
1417 if (!context || !context->internal->initialized) {
1419 errno = EINVAL;
1420 TALLOC_FREE(frame);
1421 return -1;
1425 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1427 errno = EBADF;
1428 TALLOC_FREE(frame);
1429 return -1;
1433 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1435 errno = ENOTDIR;
1436 TALLOC_FREE(frame);
1437 return -1;
1441 /* See if we're already at the end. */
1442 if (dir->dir_next == NULL) {
1443 /* We are. */
1444 TALLOC_FREE(frame);
1445 return -1;
1449 * We return the pointer here as the offset
1451 TALLOC_FREE(frame);
1452 return (off_t)(long)dir->dir_next->dirent;
1456 * A routine to run down the list and see if the entry is OK
1459 static struct smbc_dir_list *
1460 check_dir_ent(struct smbc_dir_list *list,
1461 struct smbc_dirent *dirent)
1464 /* Run down the list looking for what we want */
1466 if (dirent) {
1468 struct smbc_dir_list *tmp = list;
1470 while (tmp) {
1472 if (tmp->dirent == dirent)
1473 return tmp;
1475 tmp = tmp->next;
1481 return NULL; /* Not found, or an error */
1487 * Routine to seek on a directory
1491 SMBC_lseekdir_ctx(SMBCCTX *context,
1492 SMBCFILE *dir,
1493 off_t offset)
1495 long int l_offset = offset; /* Handle problems of size */
1496 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1497 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1498 TALLOC_CTX *frame = talloc_stackframe();
1500 if (!context || !context->internal->initialized) {
1502 errno = EINVAL;
1503 TALLOC_FREE(frame);
1504 return -1;
1508 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1510 errno = ENOTDIR;
1511 TALLOC_FREE(frame);
1512 return -1;
1516 /* Now, check what we were passed and see if it is OK ... */
1518 if (dirent == NULL) { /* Seek to the begining of the list */
1520 dir->dir_next = dir->dir_list;
1521 TALLOC_FREE(frame);
1522 return 0;
1526 if (offset == -1) { /* Seek to the end of the list */
1527 dir->dir_next = NULL;
1528 TALLOC_FREE(frame);
1529 return 0;
1532 /* Now, run down the list and make sure that the entry is OK */
1533 /* This may need to be changed if we change the format of the list */
1535 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1536 errno = EINVAL; /* Bad entry */
1537 TALLOC_FREE(frame);
1538 return -1;
1541 dir->dir_next = list_ent;
1543 TALLOC_FREE(frame);
1544 return 0;
1548 * Routine to fstat a dir
1552 SMBC_fstatdir_ctx(SMBCCTX *context,
1553 SMBCFILE *dir,
1554 struct stat *st)
1557 if (!context || !context->internal->initialized) {
1559 errno = EINVAL;
1560 return -1;
1563 /* No code yet ... */
1564 return 0;
1568 SMBC_chmod_ctx(SMBCCTX *context,
1569 const char *fname,
1570 mode_t newmode)
1572 SMBCSRV *srv = NULL;
1573 char *server = NULL;
1574 char *share = NULL;
1575 char *user = NULL;
1576 char *password = NULL;
1577 char *workgroup = NULL;
1578 char *targetpath = NULL;
1579 struct cli_state *targetcli = NULL;
1580 char *path = NULL;
1581 uint16_t mode;
1582 uint16_t port = 0;
1583 TALLOC_CTX *frame = talloc_stackframe();
1584 NTSTATUS status;
1586 if (!context || !context->internal->initialized) {
1588 errno = EINVAL; /* Best I can think of ... */
1589 TALLOC_FREE(frame);
1590 return -1;
1593 if (!fname) {
1594 errno = EINVAL;
1595 TALLOC_FREE(frame);
1596 return -1;
1599 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1601 if (SMBC_parse_path(frame,
1602 context,
1603 fname,
1604 &workgroup,
1605 &server,
1606 &port,
1607 &share,
1608 &path,
1609 &user,
1610 &password,
1611 NULL)) {
1612 errno = EINVAL;
1613 TALLOC_FREE(frame);
1614 return -1;
1617 if (!user || user[0] == (char)0) {
1618 user = talloc_strdup(frame, smbc_getUser(context));
1619 if (!user) {
1620 errno = ENOMEM;
1621 TALLOC_FREE(frame);
1622 return -1;
1626 srv = SMBC_server(frame, context, True,
1627 server, port, share, &workgroup, &user, &password);
1629 if (!srv) {
1630 TALLOC_FREE(frame);
1631 return -1; /* errno set by SMBC_server */
1634 /*d_printf(">>>unlink: resolving %s\n", path);*/
1635 status = cli_resolve_path(frame, "", context->internal->auth_info,
1636 srv->cli, path, &targetcli, &targetpath);
1637 if (!NT_STATUS_IS_OK(status)) {
1638 d_printf("Could not resolve %s\n", path);
1639 errno = ENOENT;
1640 TALLOC_FREE(frame);
1641 return -1;
1644 mode = 0;
1646 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1647 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1648 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1649 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1651 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1652 errno = SMBC_errno(context, targetcli);
1653 TALLOC_FREE(frame);
1654 return -1;
1657 TALLOC_FREE(frame);
1658 return 0;
1662 SMBC_utimes_ctx(SMBCCTX *context,
1663 const char *fname,
1664 struct timeval *tbuf)
1666 SMBCSRV *srv = NULL;
1667 char *server = NULL;
1668 char *share = NULL;
1669 char *user = NULL;
1670 char *password = NULL;
1671 char *workgroup = NULL;
1672 char *path = NULL;
1673 time_t access_time;
1674 time_t write_time;
1675 uint16_t port = 0;
1676 TALLOC_CTX *frame = talloc_stackframe();
1678 if (!context || !context->internal->initialized) {
1680 errno = EINVAL; /* Best I can think of ... */
1681 TALLOC_FREE(frame);
1682 return -1;
1685 if (!fname) {
1686 errno = EINVAL;
1687 TALLOC_FREE(frame);
1688 return -1;
1691 if (tbuf == NULL) {
1692 access_time = write_time = time(NULL);
1693 } else {
1694 access_time = tbuf[0].tv_sec;
1695 write_time = tbuf[1].tv_sec;
1698 if (DEBUGLVL(4)) {
1699 char *p;
1700 char atimebuf[32];
1701 char mtimebuf[32];
1703 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1704 atimebuf[sizeof(atimebuf) - 1] = '\0';
1705 if ((p = strchr(atimebuf, '\n')) != NULL) {
1706 *p = '\0';
1709 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1710 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1711 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1712 *p = '\0';
1715 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1716 fname, atimebuf, mtimebuf);
1719 if (SMBC_parse_path(frame,
1720 context,
1721 fname,
1722 &workgroup,
1723 &server,
1724 &port,
1725 &share,
1726 &path,
1727 &user,
1728 &password,
1729 NULL)) {
1730 errno = EINVAL;
1731 TALLOC_FREE(frame);
1732 return -1;
1735 if (!user || user[0] == (char)0) {
1736 user = talloc_strdup(frame, smbc_getUser(context));
1737 if (!user) {
1738 errno = ENOMEM;
1739 TALLOC_FREE(frame);
1740 return -1;
1744 srv = SMBC_server(frame, context, True,
1745 server, port, share, &workgroup, &user, &password);
1747 if (!srv) {
1748 TALLOC_FREE(frame);
1749 return -1; /* errno set by SMBC_server */
1752 if (!SMBC_setatr(context, srv, path,
1753 0, access_time, write_time, 0, 0)) {
1754 TALLOC_FREE(frame);
1755 return -1; /* errno set by SMBC_setatr */
1758 TALLOC_FREE(frame);
1759 return 0;
1763 * Routine to unlink() a file
1767 SMBC_unlink_ctx(SMBCCTX *context,
1768 const char *fname)
1770 char *server = NULL;
1771 char *share = NULL;
1772 char *user = NULL;
1773 char *password = NULL;
1774 char *workgroup = NULL;
1775 char *path = NULL;
1776 char *targetpath = NULL;
1777 uint16_t port = 0;
1778 struct cli_state *targetcli = NULL;
1779 SMBCSRV *srv = NULL;
1780 TALLOC_CTX *frame = talloc_stackframe();
1781 NTSTATUS status;
1783 if (!context || !context->internal->initialized) {
1785 errno = EINVAL; /* Best I can think of ... */
1786 TALLOC_FREE(frame);
1787 return -1;
1791 if (!fname) {
1792 errno = EINVAL;
1793 TALLOC_FREE(frame);
1794 return -1;
1798 if (SMBC_parse_path(frame,
1799 context,
1800 fname,
1801 &workgroup,
1802 &server,
1803 &port,
1804 &share,
1805 &path,
1806 &user,
1807 &password,
1808 NULL)) {
1809 errno = EINVAL;
1810 TALLOC_FREE(frame);
1811 return -1;
1814 if (!user || user[0] == (char)0) {
1815 user = talloc_strdup(frame, smbc_getUser(context));
1816 if (!user) {
1817 errno = ENOMEM;
1818 TALLOC_FREE(frame);
1819 return -1;
1823 srv = SMBC_server(frame, context, True,
1824 server, port, share, &workgroup, &user, &password);
1826 if (!srv) {
1827 TALLOC_FREE(frame);
1828 return -1; /* SMBC_server sets errno */
1832 /*d_printf(">>>unlink: resolving %s\n", path);*/
1833 status = cli_resolve_path(frame, "", context->internal->auth_info,
1834 srv->cli, path, &targetcli, &targetpath);
1835 if (!NT_STATUS_IS_OK(status)) {
1836 d_printf("Could not resolve %s\n", path);
1837 errno = ENOENT;
1838 TALLOC_FREE(frame);
1839 return -1;
1841 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1843 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1845 errno = SMBC_errno(context, targetcli);
1847 if (errno == EACCES) { /* Check if the file is a directory */
1849 int saverr = errno;
1850 off_t size = 0;
1851 uint16_t mode = 0;
1852 struct timespec write_time_ts;
1853 struct timespec access_time_ts;
1854 struct timespec change_time_ts;
1855 SMB_INO_T ino = 0;
1857 if (!SMBC_getatr(context, srv, path, &mode, &size,
1858 NULL,
1859 &access_time_ts,
1860 &write_time_ts,
1861 &change_time_ts,
1862 &ino)) {
1864 /* Hmmm, bad error ... What? */
1866 errno = SMBC_errno(context, targetcli);
1867 TALLOC_FREE(frame);
1868 return -1;
1871 else {
1873 if (IS_DOS_DIR(mode))
1874 errno = EISDIR;
1875 else
1876 errno = saverr; /* Restore this */
1881 TALLOC_FREE(frame);
1882 return -1;
1886 TALLOC_FREE(frame);
1887 return 0; /* Success ... */
1892 * Routine to rename() a file
1896 SMBC_rename_ctx(SMBCCTX *ocontext,
1897 const char *oname,
1898 SMBCCTX *ncontext,
1899 const char *nname)
1901 char *server1 = NULL;
1902 char *share1 = NULL;
1903 char *server2 = NULL;
1904 char *share2 = NULL;
1905 char *user1 = NULL;
1906 char *user2 = NULL;
1907 char *password1 = NULL;
1908 char *password2 = NULL;
1909 char *workgroup = NULL;
1910 char *path1 = NULL;
1911 char *path2 = NULL;
1912 char *targetpath1 = NULL;
1913 char *targetpath2 = NULL;
1914 struct cli_state *targetcli1 = NULL;
1915 struct cli_state *targetcli2 = NULL;
1916 SMBCSRV *srv = NULL;
1917 uint16_t port1 = 0;
1918 uint16_t port2 = 0;
1919 TALLOC_CTX *frame = talloc_stackframe();
1920 NTSTATUS status;
1922 if (!ocontext || !ncontext ||
1923 !ocontext->internal->initialized ||
1924 !ncontext->internal->initialized) {
1926 errno = EINVAL; /* Best I can think of ... */
1927 TALLOC_FREE(frame);
1928 return -1;
1931 if (!oname || !nname) {
1932 errno = EINVAL;
1933 TALLOC_FREE(frame);
1934 return -1;
1937 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1939 if (SMBC_parse_path(frame,
1940 ocontext,
1941 oname,
1942 &workgroup,
1943 &server1,
1944 &port1,
1945 &share1,
1946 &path1,
1947 &user1,
1948 &password1,
1949 NULL)) {
1950 errno = EINVAL;
1951 TALLOC_FREE(frame);
1952 return -1;
1955 if (!user1 || user1[0] == (char)0) {
1956 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1957 if (!user1) {
1958 errno = ENOMEM;
1959 TALLOC_FREE(frame);
1960 return -1;
1964 if (SMBC_parse_path(frame,
1965 ncontext,
1966 nname,
1967 NULL,
1968 &server2,
1969 &port2,
1970 &share2,
1971 &path2,
1972 &user2,
1973 &password2,
1974 NULL)) {
1975 errno = EINVAL;
1976 TALLOC_FREE(frame);
1977 return -1;
1980 if (!user2 || user2[0] == (char)0) {
1981 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1982 if (!user2) {
1983 errno = ENOMEM;
1984 TALLOC_FREE(frame);
1985 return -1;
1989 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1990 strcmp(user1, user2)) {
1991 /* Can't rename across file systems, or users?? */
1992 errno = EXDEV;
1993 TALLOC_FREE(frame);
1994 return -1;
1997 srv = SMBC_server(frame, ocontext, True,
1998 server1, port1, share1, &workgroup, &user1, &password1);
1999 if (!srv) {
2000 TALLOC_FREE(frame);
2001 return -1;
2005 /* set the credentials to make DFS work */
2006 smbc_set_credentials_with_fallback(ocontext,
2007 workgroup,
2008 user1,
2009 password1);
2011 /*d_printf(">>>rename: resolving %s\n", path1);*/
2012 status = cli_resolve_path(frame, "", ocontext->internal->auth_info,
2013 srv->cli, path1, &targetcli1, &targetpath1);
2014 if (!NT_STATUS_IS_OK(status)) {
2015 d_printf("Could not resolve %s\n", path1);
2016 errno = ENOENT;
2017 TALLOC_FREE(frame);
2018 return -1;
2021 /* set the credentials to make DFS work */
2022 smbc_set_credentials_with_fallback(ncontext,
2023 workgroup,
2024 user2,
2025 password2);
2027 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2028 /*d_printf(">>>rename: resolving %s\n", path2);*/
2029 status = cli_resolve_path(frame, "", ncontext->internal->auth_info,
2030 srv->cli, path2, &targetcli2, &targetpath2);
2031 if (!NT_STATUS_IS_OK(status)) {
2032 d_printf("Could not resolve %s\n", path2);
2033 errno = ENOENT;
2034 TALLOC_FREE(frame);
2035 return -1;
2037 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2039 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2040 strcmp(targetcli1->share, targetcli2->share))
2042 /* can't rename across file systems */
2043 errno = EXDEV;
2044 TALLOC_FREE(frame);
2045 return -1;
2048 if (!NT_STATUS_IS_OK(
2049 cli_rename(targetcli1, targetpath1, targetpath2, false))) {
2050 int eno = SMBC_errno(ocontext, targetcli1);
2052 if (eno != EEXIST ||
2053 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2,
2054 FILE_ATTRIBUTE_SYSTEM |
2055 FILE_ATTRIBUTE_HIDDEN)) ||
2056 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
2057 targetpath2, false))) {
2059 errno = eno;
2060 TALLOC_FREE(frame);
2061 return -1;
2066 TALLOC_FREE(frame);
2067 return 0; /* Success */
2070 struct smbc_notify_cb_state {
2071 struct tevent_context *ev;
2072 struct cli_state *cli;
2073 uint16_t fnum;
2074 bool recursive;
2075 uint32_t completion_filter;
2076 unsigned callback_timeout_ms;
2077 smbc_notify_callback_fn cb;
2078 void *private_data;
2081 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2082 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2084 static struct tevent_req *smbc_notify_cb_send(
2085 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2086 uint16_t fnum, bool recursive, uint32_t completion_filter,
2087 unsigned callback_timeout_ms,
2088 smbc_notify_callback_fn cb, void *private_data)
2090 struct tevent_req *req, *subreq;
2091 struct smbc_notify_cb_state *state;
2093 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2094 if (req == NULL) {
2095 return NULL;
2097 state->ev = ev;
2098 state->cli = cli;
2099 state->fnum = fnum;
2100 state->recursive = recursive;
2101 state->completion_filter = completion_filter;
2102 state->callback_timeout_ms = callback_timeout_ms;
2103 state->cb = cb;
2104 state->private_data = private_data;
2106 subreq = cli_notify_send(
2107 state, state->ev, state->cli, state->fnum, 1000,
2108 state->completion_filter, state->recursive);
2109 if (tevent_req_nomem(subreq, req)) {
2110 return tevent_req_post(req, ev);
2112 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2114 if (state->callback_timeout_ms == 0) {
2115 return req;
2118 subreq = tevent_wakeup_send(
2119 state, state->ev,
2120 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2121 state->callback_timeout_ms*1000));
2122 if (tevent_req_nomem(subreq, req)) {
2123 return tevent_req_post(req, ev);
2125 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2127 return req;
2130 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2132 struct tevent_req *req = tevent_req_callback_data(
2133 subreq, struct tevent_req);
2134 struct smbc_notify_cb_state *state = tevent_req_data(
2135 req, struct smbc_notify_cb_state);
2136 uint32_t num_changes;
2137 struct notify_change *changes;
2138 NTSTATUS status;
2139 int cb_ret;
2141 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2142 TALLOC_FREE(subreq);
2143 if (tevent_req_nterror(req, status)) {
2144 return;
2148 struct smbc_notify_callback_action actions[num_changes];
2149 uint32_t i;
2151 for (i=0; i<num_changes; i++) {
2152 actions[i].action = changes[i].action;
2153 actions[i].filename = changes[i].name;
2156 cb_ret = state->cb(actions, num_changes, state->private_data);
2159 TALLOC_FREE(changes);
2161 if (cb_ret != 0) {
2162 tevent_req_done(req);
2163 return;
2166 subreq = cli_notify_send(
2167 state, state->ev, state->cli, state->fnum, 1000,
2168 state->completion_filter, state->recursive);
2169 if (tevent_req_nomem(subreq, req)) {
2170 return;
2172 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2175 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2177 struct tevent_req *req = tevent_req_callback_data(
2178 subreq, struct tevent_req);
2179 struct smbc_notify_cb_state *state = tevent_req_data(
2180 req, struct smbc_notify_cb_state);
2181 int cb_ret;
2182 bool ok;
2184 ok = tevent_wakeup_recv(subreq);
2185 TALLOC_FREE(subreq);
2186 if (!ok) {
2187 tevent_req_oom(req);
2188 return;
2191 cb_ret = state->cb(NULL, 0, state->private_data);
2192 if (cb_ret != 0) {
2193 tevent_req_done(req);
2194 return;
2197 subreq = tevent_wakeup_send(
2198 state, state->ev,
2199 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2200 state->callback_timeout_ms*1000));
2201 if (tevent_req_nomem(subreq, req)) {
2202 return;
2204 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2207 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2209 return tevent_req_simple_recv_ntstatus(req);
2212 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2213 bool recursive, uint32_t completion_filter,
2214 unsigned callback_timeout_ms,
2215 smbc_notify_callback_fn cb, void *private_data)
2217 TALLOC_CTX *frame = talloc_stackframe();
2218 struct tevent_context *ev;
2219 struct tevent_req *req;
2220 NTSTATUS status = NT_STATUS_NO_MEMORY;
2222 ev = samba_tevent_context_init(frame);
2223 if (ev == NULL) {
2224 goto fail;
2226 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2227 completion_filter,
2228 callback_timeout_ms, cb, private_data);
2229 if (req == NULL) {
2230 goto fail;
2232 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2233 goto fail;
2235 status = smbc_notify_cb_recv(req);
2236 TALLOC_FREE(req);
2237 fail:
2238 TALLOC_FREE(frame);
2239 return status;
2243 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2244 uint32_t completion_filter, unsigned callback_timeout_ms,
2245 smbc_notify_callback_fn cb, void *private_data)
2247 TALLOC_CTX *frame = talloc_stackframe();
2248 struct cli_state *cli;
2249 char *server = NULL;
2250 char *share = NULL;
2251 char *user = NULL;
2252 char *password = NULL;
2253 char *options = NULL;
2254 char *workgroup = NULL;
2255 char *path = NULL;
2256 uint16_t port;
2257 NTSTATUS status;
2258 uint16_t fnum;
2260 if ((context == NULL) || !context->internal->initialized) {
2261 TALLOC_FREE(frame);
2262 errno = EINVAL;
2263 return -1;
2265 if ((dir == NULL) ||
2266 !SMBC_dlist_contains(context->internal->files, dir)) {
2267 TALLOC_FREE(frame);
2268 errno = EBADF;
2269 return -1;
2272 if (SMBC_parse_path(frame,
2273 context,
2274 dir->fname,
2275 &workgroup,
2276 &server,
2277 &port,
2278 &share,
2279 &path,
2280 &user,
2281 &password,
2282 &options)) {
2283 DEBUG(4, ("no valid path\n"));
2284 TALLOC_FREE(frame);
2285 errno = EINVAL + 8194;
2286 return -1;
2289 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2290 "path='%s' options='%s'\n",
2291 dir->fname, server, share, path, options));
2293 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2294 (int)recursive, completion_filter));
2296 cli = dir->srv->cli;
2297 status = cli_ntcreate(
2298 cli, path, 0, FILE_READ_DATA, 0,
2299 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2300 FILE_OPEN, 0, 0, &fnum, NULL);
2301 if (!NT_STATUS_IS_OK(status)) {
2302 int err = SMBC_errno(context, cli);
2303 TALLOC_FREE(frame);
2304 errno = err;
2305 return -1;
2308 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2309 callback_timeout_ms, cb, private_data);
2310 if (!NT_STATUS_IS_OK(status)) {
2311 int err = SMBC_errno(context, cli);
2312 cli_close(cli, fnum);
2313 TALLOC_FREE(frame);
2314 errno = err;
2315 return -1;
2318 cli_close(cli, fnum);
2320 TALLOC_FREE(frame);
2321 return 0;