Initial revamp of the libsmbclient interface.
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_dir.c
blob9cb3351433c945301a0e4dfeb6358ef71d0a842c
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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open a directory
32 * We accept the URL syntax explained in SMBC_parse_path(), above.
35 static void
36 remove_dir(SMBCFILE *dir)
38 struct smbc_dir_list *d,*f;
40 d = dir->dir_list;
41 while (d) {
43 f = d; d = d->next;
45 SAFE_FREE(f->dirent);
46 SAFE_FREE(f);
50 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
54 static int
55 add_dirent(SMBCFILE *dir,
56 const char *name,
57 const char *comment,
58 uint32 type)
60 struct smbc_dirent *dirent;
61 int size;
62 int name_length = (name == NULL ? 0 : strlen(name));
63 int comment_len = (comment == NULL ? 0 : strlen(comment));
66 * Allocate space for the dirent, which must be increased by the
67 * size of the name and the comment and 1 each for the null terminator.
70 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
72 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
74 if (!dirent) {
76 dir->dir_error = ENOMEM;
77 return -1;
81 ZERO_STRUCTP(dirent);
83 if (dir->dir_list == NULL) {
85 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
86 if (!dir->dir_list) {
88 SAFE_FREE(dirent);
89 dir->dir_error = ENOMEM;
90 return -1;
93 ZERO_STRUCTP(dir->dir_list);
95 dir->dir_end = dir->dir_next = dir->dir_list;
97 else {
99 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
101 if (!dir->dir_end->next) {
103 SAFE_FREE(dirent);
104 dir->dir_error = ENOMEM;
105 return -1;
108 ZERO_STRUCTP(dir->dir_end->next);
110 dir->dir_end = dir->dir_end->next;
113 dir->dir_end->next = NULL;
114 dir->dir_end->dirent = dirent;
116 dirent->smbc_type = type;
117 dirent->namelen = name_length;
118 dirent->commentlen = comment_len;
119 dirent->dirlen = size;
122 * dirent->namelen + 1 includes the null (no null termination needed)
123 * Ditto for dirent->commentlen.
124 * The space for the two null bytes was allocated.
126 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
127 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
128 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
130 return 0;
134 static void
135 list_unique_wg_fn(const char *name,
136 uint32 type,
137 const char *comment,
138 void *state)
140 SMBCFILE *dir = (SMBCFILE *)state;
141 struct smbc_dir_list *dir_list;
142 struct smbc_dirent *dirent;
143 int dirent_type;
144 int do_remove = 0;
146 dirent_type = dir->dir_type;
148 if (add_dirent(dir, name, comment, dirent_type) < 0) {
150 /* An error occurred, what do we do? */
151 /* FIXME: Add some code here */
154 /* Point to the one just added */
155 dirent = dir->dir_end->dirent;
157 /* See if this was a duplicate */
158 for (dir_list = dir->dir_list;
159 dir_list != dir->dir_end;
160 dir_list = dir_list->next) {
161 if (! do_remove &&
162 strcmp(dir_list->dirent->name, dirent->name) == 0) {
163 /* Duplicate. End end of list need to be removed. */
164 do_remove = 1;
167 if (do_remove && dir_list->next == dir->dir_end) {
168 /* Found the end of the list. Remove it. */
169 dir->dir_end = dir_list;
170 free(dir_list->next);
171 free(dirent);
172 dir_list->next = NULL;
173 break;
178 static void
179 list_fn(const char *name,
180 uint32 type,
181 const char *comment,
182 void *state)
184 SMBCFILE *dir = (SMBCFILE *)state;
185 int dirent_type;
188 * We need to process the type a little ...
190 * Disk share = 0x00000000
191 * Print share = 0x00000001
192 * Comms share = 0x00000002 (obsolete?)
193 * IPC$ share = 0x00000003
195 * administrative shares:
196 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
199 if (dir->dir_type == SMBC_FILE_SHARE) {
200 switch (type) {
201 case 0 | 0x80000000:
202 case 0:
203 dirent_type = SMBC_FILE_SHARE;
204 break;
206 case 1:
207 dirent_type = SMBC_PRINTER_SHARE;
208 break;
210 case 2:
211 dirent_type = SMBC_COMMS_SHARE;
212 break;
214 case 3 | 0x80000000:
215 case 3:
216 dirent_type = SMBC_IPC_SHARE;
217 break;
219 default:
220 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
221 break;
224 else {
225 dirent_type = dir->dir_type;
228 if (add_dirent(dir, name, comment, dirent_type) < 0) {
230 /* An error occurred, what do we do? */
231 /* FIXME: Add some code here */
236 static void
237 dir_list_fn(const char *mnt,
238 file_info *finfo,
239 const char *mask,
240 void *state)
243 if (add_dirent((SMBCFILE *)state, finfo->name, "",
244 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
246 /* Handle an error ... */
248 /* FIXME: Add some code ... */
254 static int
255 net_share_enum_rpc(struct cli_state *cli,
256 void (*fn)(const char *name,
257 uint32 type,
258 const char *comment,
259 void *state),
260 void *state)
262 int i;
263 WERROR result;
264 ENUM_HND enum_hnd;
265 uint32 info_level = 1;
266 uint32 preferred_len = 0xffffffff;
267 uint32 type;
268 SRV_SHARE_INFO_CTR ctr;
269 fstring name = "";
270 fstring comment = "";
271 struct rpc_pipe_client *pipe_hnd;
272 NTSTATUS nt_status;
274 /* Open the server service pipe */
275 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
276 if (!pipe_hnd) {
277 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
278 return -1;
281 /* Issue the NetShareEnum RPC call and retrieve the response */
282 init_enum_hnd(&enum_hnd, 0);
283 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
284 talloc_tos(),
285 info_level,
286 &ctr,
287 preferred_len,
288 &enum_hnd);
290 /* Was it successful? */
291 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
292 /* Nope. Go clean up. */
293 goto done;
296 /* For each returned entry... */
297 for (i = 0; i < ctr.num_entries; i++) {
299 /* pull out the share name */
300 rpcstr_pull_unistr2_fstring(
301 name, &ctr.share.info1[i].info_1_str.uni_netname);
303 /* pull out the share's comment */
304 rpcstr_pull_unistr2_fstring(
305 comment, &ctr.share.info1[i].info_1_str.uni_remark);
307 /* Get the type value */
308 type = ctr.share.info1[i].info_1.type;
310 /* Add this share to the list */
311 (*fn)(name, type, comment, state);
314 done:
315 /* Close the server service pipe */
316 cli_rpc_pipe_close(pipe_hnd);
318 /* Tell 'em if it worked */
319 return W_ERROR_IS_OK(result) ? 0 : -1;
324 * Verify that the options specified in a URL are valid
327 SMBC_check_options(char *server,
328 char *share,
329 char *path,
330 char *options)
332 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
333 "path='%s' options='%s'\n",
334 server, share, path, options));
336 /* No options at all is always ok */
337 if (! *options) return 0;
339 /* Currently, we don't support any options. */
340 return -1;
344 SMBCFILE *
345 SMBC_opendir_ctx(SMBCCTX *context,
346 const char *fname)
348 int saved_errno;
349 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *options = NULL;
350 char *workgroup = NULL;
351 char *path = NULL;
352 uint16 mode;
353 char *p = NULL;
354 SMBCSRV *srv = NULL;
355 SMBCFILE *dir = NULL;
356 struct sockaddr_storage rem_ss;
357 TALLOC_CTX *frame = talloc_stackframe();
359 if (!context || !context->initialized) {
360 DEBUG(4, ("no valid context\n"));
361 errno = EINVAL + 8192;
362 TALLOC_FREE(frame);
363 return NULL;
367 if (!fname) {
368 DEBUG(4, ("no valid fname\n"));
369 errno = EINVAL + 8193;
370 TALLOC_FREE(frame);
371 return NULL;
374 if (SMBC_parse_path(frame,
375 context,
376 fname,
377 &workgroup,
378 &server,
379 &share,
380 &path,
381 &user,
382 &password,
383 &options)) {
384 DEBUG(4, ("no valid path\n"));
385 errno = EINVAL + 8194;
386 TALLOC_FREE(frame);
387 return NULL;
390 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
391 "path='%s' options='%s'\n",
392 fname, server, share, path, options));
394 /* Ensure the options are valid */
395 if (SMBC_check_options(server, share, path, options)) {
396 DEBUG(4, ("unacceptable options (%s)\n", options));
397 errno = EINVAL + 8195;
398 TALLOC_FREE(frame);
399 return NULL;
402 if (!user || user[0] == (char)0) {
403 user = talloc_strdup(frame, context->user);
404 if (!user) {
405 errno = ENOMEM;
406 TALLOC_FREE(frame);
407 return NULL;
411 dir = SMB_MALLOC_P(SMBCFILE);
413 if (!dir) {
414 errno = ENOMEM;
415 TALLOC_FREE(frame);
416 return NULL;
419 ZERO_STRUCTP(dir);
421 dir->cli_fd = 0;
422 dir->fname = SMB_STRDUP(fname);
423 dir->srv = NULL;
424 dir->offset = 0;
425 dir->file = False;
426 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
428 if (server[0] == (char)0) {
430 int i;
431 int count;
432 int max_lmb_count;
433 struct ip_service *ip_list;
434 struct ip_service server_addr;
435 struct user_auth_info u_info;
437 if (share[0] != (char)0 || path[0] != (char)0) {
439 errno = EINVAL + 8196;
440 if (dir) {
441 SAFE_FREE(dir->fname);
442 SAFE_FREE(dir);
444 TALLOC_FREE(frame);
445 return NULL;
448 /* Determine how many local master browsers to query */
449 max_lmb_count = (context->browse_max_lmb_count == 0
450 ? INT_MAX
451 : context->browse_max_lmb_count);
453 memset(&u_info, '\0', sizeof(u_info));
454 u_info.username = talloc_strdup(frame,user);
455 u_info.password = talloc_strdup(frame,password);
456 if (!u_info.username || !u_info.password) {
457 if (dir) {
458 SAFE_FREE(dir->fname);
459 SAFE_FREE(dir);
461 TALLOC_FREE(frame);
462 return NULL;
466 * We have server and share and path empty but options
467 * requesting that we scan all master browsers for their list
468 * of workgroups/domains. This implies that we must first try
469 * broadcast queries to find all master browsers, and if that
470 * doesn't work, then try our other methods which return only
471 * a single master browser.
474 ip_list = NULL;
475 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
476 &count)))
479 SAFE_FREE(ip_list);
481 if (!find_master_ip(workgroup, &server_addr.ss)) {
483 if (dir) {
484 SAFE_FREE(dir->fname);
485 SAFE_FREE(dir);
487 errno = ENOENT;
488 TALLOC_FREE(frame);
489 return NULL;
492 ip_list = (struct ip_service *)memdup(
493 &server_addr, sizeof(server_addr));
494 if (ip_list == NULL) {
495 errno = ENOMEM;
496 TALLOC_FREE(frame);
497 return NULL;
499 count = 1;
502 for (i = 0; i < count && i < max_lmb_count; i++) {
503 char addr[INET6_ADDRSTRLEN];
504 char *wg_ptr = NULL;
505 struct cli_state *cli = NULL;
507 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
508 DEBUG(99, ("Found master browser %d of %d: %s\n",
509 i+1, MAX(count, max_lmb_count),
510 addr));
512 cli = get_ipc_connect_master_ip(talloc_tos(),
513 &ip_list[i],
514 &u_info,
515 &wg_ptr);
516 /* cli == NULL is the master browser refused to talk or
517 could not be found */
518 if (!cli) {
519 continue;
522 workgroup = talloc_strdup(frame, wg_ptr);
523 server = talloc_strdup(frame, cli->desthost);
525 cli_shutdown(cli);
527 if (!workgroup || !server) {
528 errno = ENOMEM;
529 TALLOC_FREE(frame);
530 return NULL;
533 DEBUG(4, ("using workgroup %s %s\n",
534 workgroup, server));
537 * For each returned master browser IP address, get a
538 * connection to IPC$ on the server if we do not
539 * already have one, and determine the
540 * workgroups/domains that it knows about.
543 srv = SMBC_server(frame, context, True, server, "IPC$",
544 &workgroup, &user, &password);
545 if (!srv) {
546 continue;
549 dir->srv = srv;
550 dir->dir_type = SMBC_WORKGROUP;
552 /* Now, list the stuff ... */
554 if (!cli_NetServerEnum(srv->cli,
555 workgroup,
556 SV_TYPE_DOMAIN_ENUM,
557 list_unique_wg_fn,
558 (void *)dir)) {
559 continue;
563 SAFE_FREE(ip_list);
564 } else {
566 * Server not an empty string ... Check the rest and see what
567 * gives
569 if (*share == '\0') {
570 if (*path != '\0') {
572 /* Should not have empty share with path */
573 errno = EINVAL + 8197;
574 if (dir) {
575 SAFE_FREE(dir->fname);
576 SAFE_FREE(dir);
578 TALLOC_FREE(frame);
579 return NULL;
584 * We don't know if <server> is really a server name
585 * or is a workgroup/domain name. If we already have
586 * a server structure for it, we'll use it.
587 * Otherwise, check to see if <server><1D>,
588 * <server><1B>, or <server><20> translates. We check
589 * to see if <server> is an IP address first.
593 * See if we have an existing server. Do not
594 * establish a connection if one does not already
595 * exist.
597 srv = SMBC_server(frame, context, False, server, "IPC$",
598 &workgroup, &user, &password);
601 * If no existing server and not an IP addr, look for
602 * LMB or DMB
604 if (!srv &&
605 !is_ipaddress(server) &&
606 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
607 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
609 fstring buserver;
611 dir->dir_type = SMBC_SERVER;
614 * Get the backup list ...
616 if (!name_status_find(server, 0, 0,
617 &rem_ss, buserver)) {
619 DEBUG(0, ("Could not get name of "
620 "local/domain master browser "
621 "for server %s\n", server));
622 if (dir) {
623 SAFE_FREE(dir->fname);
624 SAFE_FREE(dir);
626 errno = EPERM;
627 TALLOC_FREE(frame);
628 return NULL;
633 * Get a connection to IPC$ on the server if
634 * we do not already have one
636 srv = SMBC_server(frame, context, True,
637 buserver, "IPC$",
638 &workgroup, &user, &password);
639 if (!srv) {
640 DEBUG(0, ("got no contact to IPC$\n"));
641 if (dir) {
642 SAFE_FREE(dir->fname);
643 SAFE_FREE(dir);
645 TALLOC_FREE(frame);
646 return NULL;
650 dir->srv = srv;
652 /* Now, list the servers ... */
653 if (!cli_NetServerEnum(srv->cli, server,
654 0x0000FFFE, list_fn,
655 (void *)dir)) {
657 if (dir) {
658 SAFE_FREE(dir->fname);
659 SAFE_FREE(dir);
661 TALLOC_FREE(frame);
662 return NULL;
664 } else if (srv ||
665 (resolve_name(server, &rem_ss, 0x20))) {
667 /* If we hadn't found the server, get one now */
668 if (!srv) {
669 srv = SMBC_server(frame, context, True,
670 server, "IPC$",
671 &workgroup,
672 &user, &password);
675 if (!srv) {
676 if (dir) {
677 SAFE_FREE(dir->fname);
678 SAFE_FREE(dir);
680 TALLOC_FREE(frame);
681 return NULL;
685 dir->dir_type = SMBC_FILE_SHARE;
686 dir->srv = srv;
688 /* List the shares ... */
690 if (net_share_enum_rpc(
691 srv->cli,
692 list_fn,
693 (void *) dir) < 0 &&
694 cli_RNetShareEnum(
695 srv->cli,
696 list_fn,
697 (void *)dir) < 0) {
699 errno = cli_errno(srv->cli);
700 if (dir) {
701 SAFE_FREE(dir->fname);
702 SAFE_FREE(dir);
704 TALLOC_FREE(frame);
705 return NULL;
708 } else {
709 /* Neither the workgroup nor server exists */
710 errno = ECONNREFUSED;
711 if (dir) {
712 SAFE_FREE(dir->fname);
713 SAFE_FREE(dir);
715 TALLOC_FREE(frame);
716 return NULL;
720 else {
722 * The server and share are specified ... work from
723 * there ...
725 char *targetpath;
726 struct cli_state *targetcli;
728 /* We connect to the server and list the directory */
729 dir->dir_type = SMBC_FILE_SHARE;
731 srv = SMBC_server(frame, context, True, server, share,
732 &workgroup, &user, &password);
734 if (!srv) {
735 if (dir) {
736 SAFE_FREE(dir->fname);
737 SAFE_FREE(dir);
739 TALLOC_FREE(frame);
740 return NULL;
743 dir->srv = srv;
745 /* Now, list the files ... */
747 p = path + strlen(path);
748 path = talloc_asprintf_append(path, "\\*");
749 if (!path) {
750 if (dir) {
751 SAFE_FREE(dir->fname);
752 SAFE_FREE(dir);
754 TALLOC_FREE(frame);
755 return NULL;
758 if (!cli_resolve_path(frame, "", srv->cli, path,
759 &targetcli, &targetpath)) {
760 d_printf("Could not resolve %s\n", path);
761 if (dir) {
762 SAFE_FREE(dir->fname);
763 SAFE_FREE(dir);
765 TALLOC_FREE(frame);
766 return NULL;
769 if (cli_list(targetcli, targetpath,
770 aDIR | aSYSTEM | aHIDDEN,
771 dir_list_fn, (void *)dir) < 0) {
773 if (dir) {
774 SAFE_FREE(dir->fname);
775 SAFE_FREE(dir);
777 saved_errno = SMBC_errno(context, targetcli);
779 if (saved_errno == EINVAL) {
781 * See if they asked to opendir something
782 * other than a directory. If so, the
783 * converted error value we got would have
784 * been EINVAL rather than ENOTDIR.
786 *p = '\0'; /* restore original path */
788 if (SMBC_getatr(context, srv, path,
789 &mode, NULL,
790 NULL, NULL, NULL, NULL,
791 NULL) &&
792 ! IS_DOS_DIR(mode)) {
794 /* It is. Correct the error value */
795 saved_errno = ENOTDIR;
800 * If there was an error and the server is no
801 * good any more...
803 if (cli_is_error(targetcli) &&
804 (context->server.check_server_fn)(context, srv)) {
806 /* ... then remove it. */
807 if ((context->server.remove_unused_server_fn)(context,
808 srv)) {
810 * We could not remove the
811 * server completely, remove
812 * it from the cache so we
813 * will not get it again. It
814 * will be removed when the
815 * last file/dir is closed.
817 (context->cache.remove_cached_server_fn)(context, srv);
821 errno = saved_errno;
822 TALLOC_FREE(frame);
823 return NULL;
829 DLIST_ADD(context->files, dir);
830 TALLOC_FREE(frame);
831 return dir;
836 * Routine to close a directory
840 SMBC_closedir_ctx(SMBCCTX *context,
841 SMBCFILE *dir)
843 TALLOC_CTX *frame = talloc_stackframe();
845 if (!context || !context->initialized) {
846 errno = EINVAL;
847 TALLOC_FREE(frame);
848 return -1;
851 if (!dir || !SMBC_dlist_contains(context->files, dir)) {
852 errno = EBADF;
853 TALLOC_FREE(frame);
854 return -1;
857 remove_dir(dir); /* Clean it up */
859 DLIST_REMOVE(context->files, dir);
861 if (dir) {
863 SAFE_FREE(dir->fname);
864 SAFE_FREE(dir); /* Free the space too */
867 TALLOC_FREE(frame);
868 return 0;
872 static void
873 smbc_readdir_internal(SMBCCTX * context,
874 struct smbc_dirent *dest,
875 struct smbc_dirent *src,
876 int max_namebuf_len)
878 if (context->urlencode_readdir_entries) {
880 /* url-encode the name. get back remaining buffer space */
881 max_namebuf_len =
882 SMBC_urlencode(dest->name, src->name, max_namebuf_len);
884 /* We now know the name length */
885 dest->namelen = strlen(dest->name);
887 /* Save the pointer to the beginning of the comment */
888 dest->comment = dest->name + dest->namelen + 1;
890 /* Copy the comment */
891 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
892 dest->comment[max_namebuf_len - 1] = '\0';
894 /* Save other fields */
895 dest->smbc_type = src->smbc_type;
896 dest->commentlen = strlen(dest->comment);
897 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
898 (char *) dest);
899 } else {
901 /* No encoding. Just copy the entry as is. */
902 memcpy(dest, src, src->dirlen);
903 dest->comment = (char *)(&dest->name + src->namelen + 1);
909 * Routine to get a directory entry
912 struct smbc_dirent *
913 SMBC_readdir_ctx(SMBCCTX *context,
914 SMBCFILE *dir)
916 int maxlen;
917 struct smbc_dirent *dirp, *dirent;
918 TALLOC_CTX *frame = talloc_stackframe();
920 /* Check that all is ok first ... */
922 if (!context || !context->initialized) {
924 errno = EINVAL;
925 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
926 TALLOC_FREE(frame);
927 return NULL;
931 if (!dir || !SMBC_dlist_contains(context->files, dir)) {
933 errno = EBADF;
934 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
935 TALLOC_FREE(frame);
936 return NULL;
940 if (dir->file != False) { /* FIXME, should be dir, perhaps */
942 errno = ENOTDIR;
943 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
944 TALLOC_FREE(frame);
945 return NULL;
949 if (!dir->dir_next) {
950 TALLOC_FREE(frame);
951 return NULL;
954 dirent = dir->dir_next->dirent;
955 if (!dirent) {
957 errno = ENOENT;
958 TALLOC_FREE(frame);
959 return NULL;
963 dirp = (struct smbc_dirent *)context->dirent;
964 maxlen = (sizeof(context->dirent) -
965 sizeof(struct smbc_dirent));
967 smbc_readdir_internal(context, dirp, dirent, maxlen);
969 dir->dir_next = dir->dir_next->next;
971 TALLOC_FREE(frame);
972 return dirp;
976 * Routine to get directory entries
980 SMBC_getdents_ctx(SMBCCTX *context,
981 SMBCFILE *dir,
982 struct smbc_dirent *dirp,
983 int count)
985 int rem = count;
986 int reqd;
987 int maxlen;
988 char *ndir = (char *)dirp;
989 struct smbc_dir_list *dirlist;
990 TALLOC_CTX *frame = talloc_stackframe();
992 /* Check that all is ok first ... */
994 if (!context || !context->initialized) {
996 errno = EINVAL;
997 TALLOC_FREE(frame);
998 return -1;
1002 if (!dir || !SMBC_dlist_contains(context->files, dir)) {
1004 errno = EBADF;
1005 TALLOC_FREE(frame);
1006 return -1;
1010 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1012 errno = ENOTDIR;
1013 TALLOC_FREE(frame);
1014 return -1;
1019 * Now, retrieve the number of entries that will fit in what was passed
1020 * We have to figure out if the info is in the list, or we need to
1021 * send a request to the server to get the info.
1024 while ((dirlist = dir->dir_next)) {
1025 struct smbc_dirent *dirent;
1027 if (!dirlist->dirent) {
1029 errno = ENOENT; /* Bad error */
1030 TALLOC_FREE(frame);
1031 return -1;
1035 /* Do urlencoding of next entry, if so selected */
1036 dirent = (struct smbc_dirent *)context->dirent;
1037 maxlen = (sizeof(context->dirent) -
1038 sizeof(struct smbc_dirent));
1039 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
1041 reqd = dirent->dirlen;
1043 if (rem < reqd) {
1045 if (rem < count) { /* We managed to copy something */
1047 errno = 0;
1048 TALLOC_FREE(frame);
1049 return count - rem;
1052 else { /* Nothing copied ... */
1054 errno = EINVAL; /* Not enough space ... */
1055 TALLOC_FREE(frame);
1056 return -1;
1062 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1064 ((struct smbc_dirent *)ndir)->comment =
1065 (char *)(&((struct smbc_dirent *)ndir)->name +
1066 dirent->namelen +
1069 ndir += reqd;
1071 rem -= reqd;
1073 dir->dir_next = dirlist = dirlist -> next;
1076 TALLOC_FREE(frame);
1078 if (rem == count)
1079 return 0;
1080 else
1081 return count - rem;
1086 * Routine to create a directory ...
1090 SMBC_mkdir_ctx(SMBCCTX *context,
1091 const char *fname,
1092 mode_t mode)
1094 SMBCSRV *srv = NULL;
1095 char *server = NULL;
1096 char *share = NULL;
1097 char *user = NULL;
1098 char *password = NULL;
1099 char *workgroup = NULL;
1100 char *path = NULL;
1101 char *targetpath = NULL;
1102 struct cli_state *targetcli = NULL;
1103 TALLOC_CTX *frame = talloc_stackframe();
1105 if (!context || !context->initialized) {
1106 errno = EINVAL;
1107 TALLOC_FREE(frame);
1108 return -1;
1111 if (!fname) {
1112 errno = EINVAL;
1113 TALLOC_FREE(frame);
1114 return -1;
1117 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1119 if (SMBC_parse_path(frame,
1120 context,
1121 fname,
1122 &workgroup,
1123 &server,
1124 &share,
1125 &path,
1126 &user,
1127 &password,
1128 NULL)) {
1129 errno = EINVAL;
1130 TALLOC_FREE(frame);
1131 return -1;
1134 if (!user || user[0] == (char)0) {
1135 user = talloc_strdup(frame, context->user);
1136 if (!user) {
1137 errno = ENOMEM;
1138 TALLOC_FREE(frame);
1139 return -1;
1143 srv = SMBC_server(frame, context, True,
1144 server, share, &workgroup, &user, &password);
1146 if (!srv) {
1148 TALLOC_FREE(frame);
1149 return -1; /* errno set by SMBC_server */
1153 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1154 if (!cli_resolve_path(frame, "", srv->cli, path,
1155 &targetcli, &targetpath)) {
1156 d_printf("Could not resolve %s\n", path);
1157 TALLOC_FREE(frame);
1158 return -1;
1160 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1162 if (!cli_mkdir(targetcli, targetpath)) {
1164 errno = SMBC_errno(context, targetcli);
1165 TALLOC_FREE(frame);
1166 return -1;
1170 TALLOC_FREE(frame);
1171 return 0;
1176 * Our list function simply checks to see if a directory is not empty
1179 static int smbc_rmdir_dirempty = True;
1181 static void
1182 rmdir_list_fn(const char *mnt,
1183 file_info *finfo,
1184 const char *mask,
1185 void *state)
1187 if (strncmp(finfo->name, ".", 1) != 0 &&
1188 strncmp(finfo->name, "..", 2) != 0) {
1189 smbc_rmdir_dirempty = False;
1194 * Routine to remove a directory
1198 SMBC_rmdir_ctx(SMBCCTX *context,
1199 const char *fname)
1201 SMBCSRV *srv = NULL;
1202 char *server = NULL;
1203 char *share = NULL;
1204 char *user = NULL;
1205 char *password = NULL;
1206 char *workgroup = NULL;
1207 char *path = NULL;
1208 char *targetpath = NULL;
1209 struct cli_state *targetcli = NULL;
1210 TALLOC_CTX *frame = talloc_stackframe();
1212 if (!context || !context->initialized) {
1213 errno = EINVAL;
1214 TALLOC_FREE(frame);
1215 return -1;
1218 if (!fname) {
1219 errno = EINVAL;
1220 TALLOC_FREE(frame);
1221 return -1;
1224 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1226 if (SMBC_parse_path(frame,
1227 context,
1228 fname,
1229 &workgroup,
1230 &server,
1231 &share,
1232 &path,
1233 &user,
1234 &password,
1235 NULL)) {
1236 errno = EINVAL;
1237 TALLOC_FREE(frame);
1238 return -1;
1241 if (!user || user[0] == (char)0) {
1242 user = talloc_strdup(frame, context->user);
1243 if (!user) {
1244 errno = ENOMEM;
1245 TALLOC_FREE(frame);
1246 return -1;
1250 srv = SMBC_server(frame, context, True,
1251 server, share, &workgroup, &user, &password);
1253 if (!srv) {
1255 TALLOC_FREE(frame);
1256 return -1; /* errno set by SMBC_server */
1260 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1261 if (!cli_resolve_path(frame, "", srv->cli, path,
1262 &targetcli, &targetpath)) {
1263 d_printf("Could not resolve %s\n", path);
1264 TALLOC_FREE(frame);
1265 return -1;
1267 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1270 if (!cli_rmdir(targetcli, targetpath)) {
1272 errno = SMBC_errno(context, targetcli);
1274 if (errno == EACCES) { /* Check if the dir empty or not */
1276 /* Local storage to avoid buffer overflows */
1277 char *lpath;
1279 smbc_rmdir_dirempty = True; /* Make this so ... */
1281 lpath = talloc_asprintf(frame, "%s\\*",
1282 targetpath);
1283 if (!lpath) {
1284 errno = ENOMEM;
1285 TALLOC_FREE(frame);
1286 return -1;
1289 if (cli_list(targetcli, lpath,
1290 aDIR | aSYSTEM | aHIDDEN,
1291 rmdir_list_fn, NULL) < 0) {
1293 /* Fix errno to ignore latest error ... */
1294 DEBUG(5, ("smbc_rmdir: "
1295 "cli_list returned an error: %d\n",
1296 SMBC_errno(context, targetcli)));
1297 errno = EACCES;
1301 if (smbc_rmdir_dirempty)
1302 errno = EACCES;
1303 else
1304 errno = ENOTEMPTY;
1308 TALLOC_FREE(frame);
1309 return -1;
1313 TALLOC_FREE(frame);
1314 return 0;
1319 * Routine to return the current directory position
1322 off_t
1323 SMBC_telldir_ctx(SMBCCTX *context,
1324 SMBCFILE *dir)
1326 TALLOC_CTX *frame = talloc_stackframe();
1328 if (!context || !context->initialized) {
1330 errno = EINVAL;
1331 TALLOC_FREE(frame);
1332 return -1;
1336 if (!dir || !SMBC_dlist_contains(context->files, dir)) {
1338 errno = EBADF;
1339 TALLOC_FREE(frame);
1340 return -1;
1344 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1346 errno = ENOTDIR;
1347 TALLOC_FREE(frame);
1348 return -1;
1352 /* See if we're already at the end. */
1353 if (dir->dir_next == NULL) {
1354 /* We are. */
1355 TALLOC_FREE(frame);
1356 return -1;
1360 * We return the pointer here as the offset
1362 TALLOC_FREE(frame);
1363 return (off_t)(long)dir->dir_next->dirent;
1367 * A routine to run down the list and see if the entry is OK
1370 static struct smbc_dir_list *
1371 check_dir_ent(struct smbc_dir_list *list,
1372 struct smbc_dirent *dirent)
1375 /* Run down the list looking for what we want */
1377 if (dirent) {
1379 struct smbc_dir_list *tmp = list;
1381 while (tmp) {
1383 if (tmp->dirent == dirent)
1384 return tmp;
1386 tmp = tmp->next;
1392 return NULL; /* Not found, or an error */
1398 * Routine to seek on a directory
1402 SMBC_lseekdir_ctx(SMBCCTX *context,
1403 SMBCFILE *dir,
1404 off_t offset)
1406 long int l_offset = offset; /* Handle problems of size */
1407 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1408 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1409 TALLOC_CTX *frame = talloc_stackframe();
1411 if (!context || !context->initialized) {
1413 errno = EINVAL;
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 /* Now, check what we were passed and see if it is OK ... */
1429 if (dirent == NULL) { /* Seek to the begining of the list */
1431 dir->dir_next = dir->dir_list;
1432 TALLOC_FREE(frame);
1433 return 0;
1437 if (offset == -1) { /* Seek to the end of the list */
1438 dir->dir_next = NULL;
1439 TALLOC_FREE(frame);
1440 return 0;
1443 /* Now, run down the list and make sure that the entry is OK */
1444 /* This may need to be changed if we change the format of the list */
1446 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1447 errno = EINVAL; /* Bad entry */
1448 TALLOC_FREE(frame);
1449 return -1;
1452 dir->dir_next = list_ent;
1454 TALLOC_FREE(frame);
1455 return 0;
1459 * Routine to fstat a dir
1463 SMBC_fstatdir_ctx(SMBCCTX *context,
1464 SMBCFILE *dir,
1465 struct stat *st)
1468 if (!context || !context->initialized) {
1470 errno = EINVAL;
1471 return -1;
1474 /* No code yet ... */
1475 return 0;
1479 SMBC_chmod_ctx(SMBCCTX *context,
1480 const char *fname,
1481 mode_t newmode)
1483 SMBCSRV *srv = NULL;
1484 char *server = NULL;
1485 char *share = NULL;
1486 char *user = NULL;
1487 char *password = NULL;
1488 char *workgroup = NULL;
1489 char *path = NULL;
1490 uint16 mode;
1491 TALLOC_CTX *frame = talloc_stackframe();
1493 if (!context || !context->initialized) {
1495 errno = EINVAL; /* Best I can think of ... */
1496 TALLOC_FREE(frame);
1497 return -1;
1500 if (!fname) {
1501 errno = EINVAL;
1502 TALLOC_FREE(frame);
1503 return -1;
1506 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
1508 if (SMBC_parse_path(frame,
1509 context,
1510 fname,
1511 &workgroup,
1512 &server,
1513 &share,
1514 &path,
1515 &user,
1516 &password,
1517 NULL)) {
1518 errno = EINVAL;
1519 TALLOC_FREE(frame);
1520 return -1;
1523 if (!user || user[0] == (char)0) {
1524 user = talloc_strdup(frame, context->user);
1525 if (!user) {
1526 errno = ENOMEM;
1527 TALLOC_FREE(frame);
1528 return -1;
1532 srv = SMBC_server(frame, context, True,
1533 server, share, &workgroup, &user, &password);
1535 if (!srv) {
1536 TALLOC_FREE(frame);
1537 return -1; /* errno set by SMBC_server */
1540 mode = 0;
1542 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1543 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1544 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1545 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1547 if (!cli_setatr(srv->cli, path, mode, 0)) {
1548 errno = SMBC_errno(context, srv->cli);
1549 TALLOC_FREE(frame);
1550 return -1;
1553 TALLOC_FREE(frame);
1554 return 0;
1558 SMBC_utimes_ctx(SMBCCTX *context,
1559 const char *fname,
1560 struct timeval *tbuf)
1562 SMBCSRV *srv = NULL;
1563 char *server = NULL;
1564 char *share = NULL;
1565 char *user = NULL;
1566 char *password = NULL;
1567 char *workgroup = NULL;
1568 char *path = NULL;
1569 time_t access_time;
1570 time_t write_time;
1571 TALLOC_CTX *frame = talloc_stackframe();
1573 if (!context || !context->initialized) {
1575 errno = EINVAL; /* Best I can think of ... */
1576 TALLOC_FREE(frame);
1577 return -1;
1580 if (!fname) {
1581 errno = EINVAL;
1582 TALLOC_FREE(frame);
1583 return -1;
1586 if (tbuf == NULL) {
1587 access_time = write_time = time(NULL);
1588 } else {
1589 access_time = tbuf[0].tv_sec;
1590 write_time = tbuf[1].tv_sec;
1593 if (DEBUGLVL(4)) {
1594 char *p;
1595 char atimebuf[32];
1596 char mtimebuf[32];
1598 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1599 atimebuf[sizeof(atimebuf) - 1] = '\0';
1600 if ((p = strchr(atimebuf, '\n')) != NULL) {
1601 *p = '\0';
1604 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1605 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1606 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1607 *p = '\0';
1610 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1611 fname, atimebuf, mtimebuf);
1614 if (SMBC_parse_path(frame,
1615 context,
1616 fname,
1617 &workgroup,
1618 &server,
1619 &share,
1620 &path,
1621 &user,
1622 &password,
1623 NULL)) {
1624 errno = EINVAL;
1625 TALLOC_FREE(frame);
1626 return -1;
1629 if (!user || user[0] == (char)0) {
1630 user = talloc_strdup(frame, context->user);
1631 if (!user) {
1632 errno = ENOMEM;
1633 TALLOC_FREE(frame);
1634 return -1;
1638 srv = SMBC_server(frame, context, True,
1639 server, share, &workgroup, &user, &password);
1641 if (!srv) {
1642 TALLOC_FREE(frame);
1643 return -1; /* errno set by SMBC_server */
1646 if (!SMBC_setatr(context, srv, path,
1647 0, access_time, write_time, 0, 0)) {
1648 TALLOC_FREE(frame);
1649 return -1; /* errno set by SMBC_setatr */
1652 TALLOC_FREE(frame);
1653 return 0;
1657 * Routine to unlink() a file
1661 SMBC_unlink_ctx(SMBCCTX *context,
1662 const char *fname)
1664 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
1665 char *path = NULL;
1666 char *targetpath = NULL;
1667 struct cli_state *targetcli = NULL;
1668 SMBCSRV *srv = NULL;
1669 TALLOC_CTX *frame = talloc_stackframe();
1671 if (!context || !context->initialized) {
1673 errno = EINVAL; /* Best I can think of ... */
1674 TALLOC_FREE(frame);
1675 return -1;
1679 if (!fname) {
1680 errno = EINVAL;
1681 TALLOC_FREE(frame);
1682 return -1;
1686 if (SMBC_parse_path(frame,
1687 context,
1688 fname,
1689 &workgroup,
1690 &server,
1691 &share,
1692 &path,
1693 &user,
1694 &password,
1695 NULL)) {
1696 errno = EINVAL;
1697 TALLOC_FREE(frame);
1698 return -1;
1701 if (!user || user[0] == (char)0) {
1702 user = talloc_strdup(frame, context->user);
1703 if (!user) {
1704 errno = ENOMEM;
1705 TALLOC_FREE(frame);
1706 return -1;
1710 srv = SMBC_server(frame, context, True,
1711 server, share, &workgroup, &user, &password);
1713 if (!srv) {
1714 TALLOC_FREE(frame);
1715 return -1; /* SMBC_server sets errno */
1719 /*d_printf(">>>unlink: resolving %s\n", path);*/
1720 if (!cli_resolve_path(frame, "", srv->cli, path,
1721 &targetcli, &targetpath)) {
1722 d_printf("Could not resolve %s\n", path);
1723 TALLOC_FREE(frame);
1724 return -1;
1726 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1728 if (!cli_unlink(targetcli, targetpath)) {
1730 errno = SMBC_errno(context, targetcli);
1732 if (errno == EACCES) { /* Check if the file is a directory */
1734 int saverr = errno;
1735 SMB_OFF_T size = 0;
1736 uint16 mode = 0;
1737 struct timespec write_time_ts;
1738 struct timespec access_time_ts;
1739 struct timespec change_time_ts;
1740 SMB_INO_T ino = 0;
1742 if (!SMBC_getatr(context, srv, path, &mode, &size,
1743 NULL,
1744 &access_time_ts,
1745 &write_time_ts,
1746 &change_time_ts,
1747 &ino)) {
1749 /* Hmmm, bad error ... What? */
1751 errno = SMBC_errno(context, targetcli);
1752 TALLOC_FREE(frame);
1753 return -1;
1756 else {
1758 if (IS_DOS_DIR(mode))
1759 errno = EISDIR;
1760 else
1761 errno = saverr; /* Restore this */
1766 TALLOC_FREE(frame);
1767 return -1;
1771 TALLOC_FREE(frame);
1772 return 0; /* Success ... */
1777 * Routine to rename() a file
1781 SMBC_rename_ctx(SMBCCTX *ocontext,
1782 const char *oname,
1783 SMBCCTX *ncontext,
1784 const char *nname)
1786 char *server1 = NULL;
1787 char *share1 = NULL;
1788 char *server2 = NULL;
1789 char *share2 = NULL;
1790 char *user1 = NULL;
1791 char *user2 = NULL;
1792 char *password1 = NULL;
1793 char *password2 = NULL;
1794 char *workgroup = NULL;
1795 char *path1 = NULL;
1796 char *path2 = NULL;
1797 char *targetpath1 = NULL;
1798 char *targetpath2 = NULL;
1799 struct cli_state *targetcli1 = NULL;
1800 struct cli_state *targetcli2 = NULL;
1801 SMBCSRV *srv = NULL;
1802 TALLOC_CTX *frame = talloc_stackframe();
1804 if (!ocontext || !ncontext ||
1805 !ocontext->initialized ||
1806 !ncontext->initialized) {
1808 errno = EINVAL; /* Best I can think of ... */
1809 TALLOC_FREE(frame);
1810 return -1;
1813 if (!oname || !nname) {
1814 errno = EINVAL;
1815 TALLOC_FREE(frame);
1816 return -1;
1819 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1821 if (SMBC_parse_path(frame,
1822 ocontext,
1823 oname,
1824 &workgroup,
1825 &server1,
1826 &share1,
1827 &path1,
1828 &user1,
1829 &password1,
1830 NULL)) {
1831 errno = EINVAL;
1832 TALLOC_FREE(frame);
1833 return -1;
1836 if (!user1 || user1[0] == (char)0) {
1837 user1 = talloc_strdup(frame, ocontext->user);
1838 if (!user1) {
1839 errno = ENOMEM;
1840 TALLOC_FREE(frame);
1841 return -1;
1845 if (SMBC_parse_path(frame,
1846 ncontext,
1847 nname,
1848 NULL,
1849 &server2,
1850 &share2,
1851 &path2,
1852 &user2,
1853 &password2,
1854 NULL)) {
1855 errno = EINVAL;
1856 TALLOC_FREE(frame);
1857 return -1;
1860 if (!user2 || user2[0] == (char)0) {
1861 user2 = talloc_strdup(frame, ncontext->user);
1862 if (!user2) {
1863 errno = ENOMEM;
1864 TALLOC_FREE(frame);
1865 return -1;
1869 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1870 strcmp(user1, user2)) {
1871 /* Can't rename across file systems, or users?? */
1872 errno = EXDEV;
1873 TALLOC_FREE(frame);
1874 return -1;
1877 srv = SMBC_server(frame, ocontext, True,
1878 server1, share1, &workgroup, &user1, &password1);
1879 if (!srv) {
1880 TALLOC_FREE(frame);
1881 return -1;
1885 /*d_printf(">>>rename: resolving %s\n", path1);*/
1886 if (!cli_resolve_path(frame, "", srv->cli, path1,
1887 &targetcli1, &targetpath1)) {
1888 d_printf("Could not resolve %s\n", path1);
1889 TALLOC_FREE(frame);
1890 return -1;
1892 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1893 /*d_printf(">>>rename: resolving %s\n", path2);*/
1894 if (!cli_resolve_path(frame, "", srv->cli, path2,
1895 &targetcli2, &targetpath2)) {
1896 d_printf("Could not resolve %s\n", path2);
1897 TALLOC_FREE(frame);
1898 return -1;
1900 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1902 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1903 strcmp(targetcli1->share, targetcli2->share))
1905 /* can't rename across file systems */
1906 errno = EXDEV;
1907 TALLOC_FREE(frame);
1908 return -1;
1911 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1912 int eno = SMBC_errno(ocontext, targetcli1);
1914 if (eno != EEXIST ||
1915 !cli_unlink(targetcli1, targetpath2) ||
1916 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1918 errno = eno;
1919 TALLOC_FREE(frame);
1920 return -1;
1925 TALLOC_FREE(frame);
1926 return 0; /* Success */