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/>.
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.
43 remove_dir(SMBCFILE *dir)
45 struct smbc_dir_list *d,*f;
57 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
62 add_dirent(SMBCFILE *dir,
67 struct smbc_dirent *dirent;
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);
83 dir->dir_error = ENOMEM;
90 if (dir->dir_list == NULL) {
92 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
96 dir->dir_error = ENOMEM;
100 ZERO_STRUCTP(dir->dir_list);
102 dir->dir_end = dir->dir_next = dir->dir_list;
106 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
108 if (!dir->dir_end->next) {
111 dir->dir_error = ENOMEM;
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);
142 list_unique_wg_fn(const char *name,
147 SMBCFILE *dir = (SMBCFILE *)state;
148 struct smbc_dir_list *dir_list;
149 struct smbc_dirent *dirent;
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) {
170 strcmp(dir_list->dirent->name, dirent->name) == 0) {
171 /* Duplicate. End end of list need to be removed. */
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);
180 dir_list->next = NULL;
187 list_fn(const char *name,
192 SMBCFILE *dir = (SMBCFILE *)state;
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) {
211 dirent_type = SMBC_FILE_SHARE;
215 dirent_type = SMBC_PRINTER_SHARE;
219 dirent_type = SMBC_COMMS_SHARE;
224 dirent_type = SMBC_IPC_SHARE;
228 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
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. */
245 dir_list_fn(const char *mnt,
246 struct file_info *finfo,
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);
260 net_share_enum_rpc(struct cli_state *cli,
261 void (*fn)(const char *name,
269 uint32_t preferred_len = 0xffffffff;
271 struct srvsvc_NetShareInfoCtr info_ctr;
272 struct srvsvc_NetShareCtr1 ctr1;
274 fstring comment = "";
275 struct rpc_pipe_client *pipe_hnd = NULL;
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,
284 if (!NT_STATUS_IS_OK(nt_status)) {
285 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
289 ZERO_STRUCT(info_ctr);
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(),
306 /* Was it successful? */
307 if (!NT_STATUS_IS_OK(nt_status)) {
308 /* Nope. Go clean up. */
309 result = ntstatus_to_werror(nt_status);
313 if (!W_ERROR_IS_OK(result)) {
314 /* Nope. Go clean up. */
318 if (total_entries == 0) {
319 /* Nope. Go clean up. */
320 result = WERR_GEN_FAILURE;
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);
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,
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. */
371 SMBC_opendir_ctx(SMBCCTX *context,
378 char *password = NULL;
379 char *options = NULL;
380 char *workgroup = 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"));
393 errno = EINVAL + 8192;
399 DEBUG(4, ("no valid fname\n"));
401 errno = EINVAL + 8193;
405 if (SMBC_parse_path(frame,
416 DEBUG(4, ("no valid path\n"));
418 errno = EINVAL + 8194;
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));
430 errno = EINVAL + 8195;
434 if (!user || user[0] == (char)0) {
435 user = talloc_strdup(frame, smbc_getUser(context));
443 dir = SMB_MALLOC_P(SMBCFILE);
454 dir->fname = SMB_STRDUP(fname);
458 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
460 if (server[0] == (char)0) {
465 struct sockaddr_storage *ip_list;
466 struct sockaddr_storage server_addr;
467 struct user_auth_info *u_info;
470 if (share[0] != (char)0 || path[0] != (char)0) {
473 SAFE_FREE(dir->fname);
477 errno = EINVAL + 8196;
481 /* Determine how many local master browsers to query */
482 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
484 : smbc_getOptionBrowseMaxLmbCount(context));
486 u_info = user_auth_info_init(frame);
487 if (u_info == NULL) {
489 SAFE_FREE(dir->fname);
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.
509 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
511 if (!NT_STATUS_IS_OK(status))
514 TALLOC_FREE(ip_list);
516 if (!find_master_ip(workgroup, &server_addr)) {
519 SAFE_FREE(dir->fname);
527 ip_list = (struct sockaddr_storage *)talloc_memdup(
528 talloc_tos(), &server_addr,
529 sizeof(server_addr));
530 if (ip_list == NULL) {
532 SAFE_FREE(dir->fname);
542 for (i = 0; i < count && i < max_lmb_count; i++) {
543 char addr[INET6_ADDRSTRLEN];
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),
552 cli = get_ipc_connect_master_ip(talloc_tos(),
556 /* cli == NULL is the master browser refused to talk or
557 could not be found */
562 workgroup = talloc_strdup(frame, wg_ptr);
563 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
567 if (!workgroup || !server) {
569 SAFE_FREE(dir->fname);
577 DEBUG(4, ("using workgroup %s %s\n",
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);
594 dir->dir_type = SMBC_WORKGROUP;
596 /* Now, list the stuff ... */
598 if (!cli_NetServerEnum(srv->cli,
607 TALLOC_FREE(ip_list);
610 * Server not an empty string ... Check the rest and see what
613 if (*share == '\0') {
616 /* Should not have empty share with path */
618 SAFE_FREE(dir->fname);
622 errno = EINVAL + 8197;
628 * We don't know if <server> is really a server name
629 * or is a workgroup/domain name. If we already have
630 * a server structure for it, we'll use it.
631 * Otherwise, check to see if <server><1D>,
632 * <server><1B>, or <server><20> translates. We check
633 * to see if <server> is an IP address first.
637 * See if we have an existing server. Do not
638 * establish a connection if one does not already
641 srv = SMBC_server(frame, context, False,
642 server, port, "IPC$",
643 &workgroup, &user, &password);
646 * If no existing server and not an IP addr, look for
650 !is_ipaddress(server) &&
651 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
652 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
654 * "server" is actually a workgroup name,
655 * not a server. Make this clear.
657 char *wgroup = server;
660 dir->dir_type = SMBC_SERVER;
663 * Get the backup list ...
665 if (!name_status_find(wgroup, 0, 0,
666 &rem_ss, buserver)) {
667 char addr[INET6_ADDRSTRLEN];
669 print_sockaddr(addr, sizeof(addr), &rem_ss);
670 DEBUG(0,("Could not get name of "
671 "local/domain master browser "
672 "for workgroup %s from "
677 SAFE_FREE(dir->fname);
687 * Get a connection to IPC$ on the server if
688 * we do not already have one
690 srv = SMBC_server(frame, context, True,
691 buserver, port, "IPC$",
695 DEBUG(0, ("got no contact to IPC$\n"));
697 SAFE_FREE(dir->fname);
707 /* Now, list the servers ... */
708 if (!cli_NetServerEnum(srv->cli, wgroup,
713 SAFE_FREE(dir->fname);
720 (resolve_name(server, &rem_ss, 0x20, false))) {
723 * If we hadn't found the server, get one now
726 srv = SMBC_server(frame, context, True,
727 server, port, "IPC$",
734 SAFE_FREE(dir->fname);
742 dir->dir_type = SMBC_FILE_SHARE;
745 /* List the shares ... */
747 if (net_share_enum_rpc(
756 errno = cli_errno(srv->cli);
758 SAFE_FREE(dir->fname);
766 /* Neither the workgroup nor server exists */
767 errno = ECONNREFUSED;
769 SAFE_FREE(dir->fname);
779 * The server and share are specified ... work from
783 struct cli_state *targetcli;
786 /* We connect to the server and list the directory */
787 dir->dir_type = SMBC_FILE_SHARE;
789 srv = SMBC_server(frame, context, True, server, port, share,
790 &workgroup, &user, &password);
794 SAFE_FREE(dir->fname);
803 /* Now, list the files ... */
805 p = path + strlen(path);
806 path = talloc_asprintf_append(path, "\\*");
809 SAFE_FREE(dir->fname);
816 status = cli_resolve_path(
817 frame, "", context->internal->auth_info,
818 srv->cli, path, &targetcli, &targetpath);
819 if (!NT_STATUS_IS_OK(status)) {
820 d_printf("Could not resolve %s\n", path);
822 SAFE_FREE(dir->fname);
829 status = cli_list(targetcli, targetpath,
830 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
831 dir_list_fn, (void *)dir);
832 if (!NT_STATUS_IS_OK(status)) {
834 SAFE_FREE(dir->fname);
837 saved_errno = SMBC_errno(context, targetcli);
839 if (saved_errno == EINVAL) {
841 * See if they asked to opendir
842 * something other than a directory.
843 * If so, the converted error value we
844 * got would have been EINVAL rather
847 *p = '\0'; /* restore original path */
849 if (SMBC_getatr(context, srv, path,
851 NULL, NULL, NULL, NULL,
853 ! IS_DOS_DIR(mode)) {
855 /* It is. Correct the error value */
856 saved_errno = ENOTDIR;
861 * If there was an error and the server is no
864 if (cli_is_error(targetcli) &&
865 smbc_getFunctionCheckServer(context)(context, srv)) {
867 /* ... then remove it. */
868 if (smbc_getFunctionRemoveUnusedServer(context)(context,
871 * We could not remove the
872 * server completely, remove
873 * it from the cache so we
874 * will not get it again. It
875 * will be removed when the
876 * last file/dir is closed.
878 smbc_getFunctionRemoveCachedServer(context)(context, srv);
890 DLIST_ADD(context->internal->files, dir);
897 * Routine to close a directory
901 SMBC_closedir_ctx(SMBCCTX *context,
904 TALLOC_CTX *frame = talloc_stackframe();
906 if (!context || !context->internal->initialized) {
912 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
918 remove_dir(dir); /* Clean it up */
920 DLIST_REMOVE(context->internal->files, dir);
924 SAFE_FREE(dir->fname);
925 SAFE_FREE(dir); /* Free the space too */
934 smbc_readdir_internal(SMBCCTX * context,
935 struct smbc_dirent *dest,
936 struct smbc_dirent *src,
939 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
941 /* url-encode the name. get back remaining buffer space */
943 smbc_urlencode(dest->name, src->name, max_namebuf_len);
945 /* We now know the name length */
946 dest->namelen = strlen(dest->name);
948 /* Save the pointer to the beginning of the comment */
949 dest->comment = dest->name + dest->namelen + 1;
951 /* Copy the comment */
952 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
953 dest->comment[max_namebuf_len - 1] = '\0';
955 /* Save other fields */
956 dest->smbc_type = src->smbc_type;
957 dest->commentlen = strlen(dest->comment);
958 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
962 /* No encoding. Just copy the entry as is. */
963 memcpy(dest, src, src->dirlen);
964 dest->comment = (char *)(&dest->name + src->namelen + 1);
970 * Routine to get a directory entry
974 SMBC_readdir_ctx(SMBCCTX *context,
978 struct smbc_dirent *dirp, *dirent;
979 TALLOC_CTX *frame = talloc_stackframe();
981 /* Check that all is ok first ... */
983 if (!context || !context->internal->initialized) {
986 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
992 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
995 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1001 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1004 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1010 if (!dir->dir_next) {
1015 dirent = dir->dir_next->dirent;
1024 dirp = &context->internal->dirent;
1025 maxlen = sizeof(context->internal->_dirent_name);
1027 smbc_readdir_internal(context, dirp, dirent, maxlen);
1029 dir->dir_next = dir->dir_next->next;
1036 * Routine to get directory entries
1040 SMBC_getdents_ctx(SMBCCTX *context,
1042 struct smbc_dirent *dirp,
1048 char *ndir = (char *)dirp;
1049 struct smbc_dir_list *dirlist;
1050 TALLOC_CTX *frame = talloc_stackframe();
1052 /* Check that all is ok first ... */
1054 if (!context || !context->internal->initialized) {
1062 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1070 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1079 * Now, retrieve the number of entries that will fit in what was passed
1080 * We have to figure out if the info is in the list, or we need to
1081 * send a request to the server to get the info.
1084 while ((dirlist = dir->dir_next)) {
1085 struct smbc_dirent *dirent;
1086 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1088 if (!dirlist->dirent) {
1090 errno = ENOENT; /* Bad error */
1096 /* Do urlencoding of next entry, if so selected */
1097 dirent = &context->internal->dirent;
1098 maxlen = sizeof(context->internal->_dirent_name);
1099 smbc_readdir_internal(context, dirent,
1100 dirlist->dirent, maxlen);
1102 reqd = dirent->dirlen;
1106 if (rem < count) { /* We managed to copy something */
1113 else { /* Nothing copied ... */
1115 errno = EINVAL; /* Not enough space ... */
1123 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1125 currentEntry->comment = ¤tEntry->name[0] +
1126 dirent->namelen + 1;
1131 /* Try and align the struct for the next entry
1132 on a valid pointer boundary by appending zeros */
1133 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1137 currentEntry->dirlen++;
1140 dir->dir_next = dirlist = dirlist -> next;
1153 * Routine to create a directory ...
1157 SMBC_mkdir_ctx(SMBCCTX *context,
1161 SMBCSRV *srv = NULL;
1162 char *server = NULL;
1165 char *password = NULL;
1166 char *workgroup = NULL;
1168 char *targetpath = NULL;
1170 struct cli_state *targetcli = NULL;
1171 TALLOC_CTX *frame = talloc_stackframe();
1174 if (!context || !context->internal->initialized) {
1186 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1188 if (SMBC_parse_path(frame,
1204 if (!user || user[0] == (char)0) {
1205 user = talloc_strdup(frame, smbc_getUser(context));
1213 srv = SMBC_server(frame, context, True,
1214 server, port, share, &workgroup, &user, &password);
1219 return -1; /* errno set by SMBC_server */
1223 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1224 status = cli_resolve_path(frame, "", context->internal->auth_info,
1225 srv->cli, path, &targetcli, &targetpath);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 d_printf("Could not resolve %s\n", path);
1232 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1234 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1235 errno = SMBC_errno(context, targetcli);
1247 * Our list function simply checks to see if a directory is not empty
1251 rmdir_list_fn(const char *mnt,
1252 struct file_info *finfo,
1256 if (strncmp(finfo->name, ".", 1) != 0 &&
1257 strncmp(finfo->name, "..", 2) != 0) {
1258 bool *smbc_rmdir_dirempty = (bool *)state;
1259 *smbc_rmdir_dirempty = false;
1261 return NT_STATUS_OK;
1265 * Routine to remove a directory
1269 SMBC_rmdir_ctx(SMBCCTX *context,
1272 SMBCSRV *srv = NULL;
1273 char *server = NULL;
1276 char *password = NULL;
1277 char *workgroup = NULL;
1279 char *targetpath = NULL;
1281 struct cli_state *targetcli = NULL;
1282 TALLOC_CTX *frame = talloc_stackframe();
1285 if (!context || !context->internal->initialized) {
1297 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1299 if (SMBC_parse_path(frame,
1315 if (!user || user[0] == (char)0) {
1316 user = talloc_strdup(frame, smbc_getUser(context));
1324 srv = SMBC_server(frame, context, True,
1325 server, port, share, &workgroup, &user, &password);
1330 return -1; /* errno set by SMBC_server */
1334 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1335 status = cli_resolve_path(frame, "", context->internal->auth_info,
1336 srv->cli, path, &targetcli, &targetpath);
1337 if (!NT_STATUS_IS_OK(status)) {
1338 d_printf("Could not resolve %s\n", path);
1343 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1345 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1347 errno = SMBC_errno(context, targetcli);
1349 if (errno == EACCES) { /* Check if the dir empty or not */
1351 /* Local storage to avoid buffer overflows */
1353 bool smbc_rmdir_dirempty = true;
1355 lpath = talloc_asprintf(frame, "%s\\*",
1363 status = cli_list(targetcli, lpath,
1364 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1366 &smbc_rmdir_dirempty);
1368 if (!NT_STATUS_IS_OK(status)) {
1369 /* Fix errno to ignore latest error ... */
1370 DEBUG(5, ("smbc_rmdir: "
1371 "cli_list returned an error: %d\n",
1372 SMBC_errno(context, targetcli)));
1377 if (smbc_rmdir_dirempty)
1395 * Routine to return the current directory position
1399 SMBC_telldir_ctx(SMBCCTX *context,
1402 TALLOC_CTX *frame = talloc_stackframe();
1404 if (!context || !context->internal->initialized) {
1412 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1420 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1428 /* See if we're already at the end. */
1429 if (dir->dir_next == NULL) {
1436 * We return the pointer here as the offset
1439 return (off_t)(long)dir->dir_next->dirent;
1443 * A routine to run down the list and see if the entry is OK
1446 static struct smbc_dir_list *
1447 check_dir_ent(struct smbc_dir_list *list,
1448 struct smbc_dirent *dirent)
1451 /* Run down the list looking for what we want */
1455 struct smbc_dir_list *tmp = list;
1459 if (tmp->dirent == dirent)
1468 return NULL; /* Not found, or an error */
1474 * Routine to seek on a directory
1478 SMBC_lseekdir_ctx(SMBCCTX *context,
1482 long int l_offset = offset; /* Handle problems of size */
1483 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1484 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1485 TALLOC_CTX *frame = talloc_stackframe();
1487 if (!context || !context->internal->initialized) {
1495 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1503 /* Now, check what we were passed and see if it is OK ... */
1505 if (dirent == NULL) { /* Seek to the begining of the list */
1507 dir->dir_next = dir->dir_list;
1513 if (offset == -1) { /* Seek to the end of the list */
1514 dir->dir_next = NULL;
1519 /* Now, run down the list and make sure that the entry is OK */
1520 /* This may need to be changed if we change the format of the list */
1522 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1523 errno = EINVAL; /* Bad entry */
1528 dir->dir_next = list_ent;
1535 * Routine to fstat a dir
1539 SMBC_fstatdir_ctx(SMBCCTX *context,
1544 if (!context || !context->internal->initialized) {
1550 /* No code yet ... */
1555 SMBC_chmod_ctx(SMBCCTX *context,
1559 SMBCSRV *srv = NULL;
1560 char *server = NULL;
1563 char *password = NULL;
1564 char *workgroup = NULL;
1565 char *targetpath = NULL;
1566 struct cli_state *targetcli = NULL;
1570 TALLOC_CTX *frame = talloc_stackframe();
1573 if (!context || !context->internal->initialized) {
1575 errno = EINVAL; /* Best I can think of ... */
1586 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1588 if (SMBC_parse_path(frame,
1604 if (!user || user[0] == (char)0) {
1605 user = talloc_strdup(frame, smbc_getUser(context));
1613 srv = SMBC_server(frame, context, True,
1614 server, port, share, &workgroup, &user, &password);
1618 return -1; /* errno set by SMBC_server */
1621 /*d_printf(">>>unlink: resolving %s\n", path);*/
1622 status = cli_resolve_path(frame, "", context->internal->auth_info,
1623 srv->cli, path, &targetcli, &targetpath);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 d_printf("Could not resolve %s\n", path);
1633 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1634 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1635 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1636 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1638 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1639 errno = SMBC_errno(context, targetcli);
1649 SMBC_utimes_ctx(SMBCCTX *context,
1651 struct timeval *tbuf)
1653 SMBCSRV *srv = NULL;
1654 char *server = NULL;
1657 char *password = NULL;
1658 char *workgroup = NULL;
1663 TALLOC_CTX *frame = talloc_stackframe();
1665 if (!context || !context->internal->initialized) {
1667 errno = EINVAL; /* Best I can think of ... */
1679 access_time = write_time = time(NULL);
1681 access_time = tbuf[0].tv_sec;
1682 write_time = tbuf[1].tv_sec;
1690 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1691 atimebuf[sizeof(atimebuf) - 1] = '\0';
1692 if ((p = strchr(atimebuf, '\n')) != NULL) {
1696 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1697 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1698 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1702 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1703 fname, atimebuf, mtimebuf);
1706 if (SMBC_parse_path(frame,
1722 if (!user || user[0] == (char)0) {
1723 user = talloc_strdup(frame, smbc_getUser(context));
1731 srv = SMBC_server(frame, context, True,
1732 server, port, share, &workgroup, &user, &password);
1736 return -1; /* errno set by SMBC_server */
1739 if (!SMBC_setatr(context, srv, path,
1740 0, access_time, write_time, 0, 0)) {
1742 return -1; /* errno set by SMBC_setatr */
1750 * Routine to unlink() a file
1754 SMBC_unlink_ctx(SMBCCTX *context,
1757 char *server = NULL;
1760 char *password = NULL;
1761 char *workgroup = NULL;
1763 char *targetpath = NULL;
1765 struct cli_state *targetcli = NULL;
1766 SMBCSRV *srv = NULL;
1767 TALLOC_CTX *frame = talloc_stackframe();
1770 if (!context || !context->internal->initialized) {
1772 errno = EINVAL; /* Best I can think of ... */
1785 if (SMBC_parse_path(frame,
1801 if (!user || user[0] == (char)0) {
1802 user = talloc_strdup(frame, smbc_getUser(context));
1810 srv = SMBC_server(frame, context, True,
1811 server, port, share, &workgroup, &user, &password);
1815 return -1; /* SMBC_server sets errno */
1819 /*d_printf(">>>unlink: resolving %s\n", path);*/
1820 status = cli_resolve_path(frame, "", context->internal->auth_info,
1821 srv->cli, path, &targetcli, &targetpath);
1822 if (!NT_STATUS_IS_OK(status)) {
1823 d_printf("Could not resolve %s\n", path);
1828 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1830 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1832 errno = SMBC_errno(context, targetcli);
1834 if (errno == EACCES) { /* Check if the file is a directory */
1839 struct timespec write_time_ts;
1840 struct timespec access_time_ts;
1841 struct timespec change_time_ts;
1844 if (!SMBC_getatr(context, srv, path, &mode, &size,
1851 /* Hmmm, bad error ... What? */
1853 errno = SMBC_errno(context, targetcli);
1860 if (IS_DOS_DIR(mode))
1863 errno = saverr; /* Restore this */
1874 return 0; /* Success ... */
1879 * Routine to rename() a file
1883 SMBC_rename_ctx(SMBCCTX *ocontext,
1888 char *server1 = NULL;
1889 char *share1 = NULL;
1890 char *server2 = NULL;
1891 char *share2 = NULL;
1894 char *password1 = NULL;
1895 char *password2 = NULL;
1896 char *workgroup = NULL;
1899 char *targetpath1 = NULL;
1900 char *targetpath2 = NULL;
1901 struct cli_state *targetcli1 = NULL;
1902 struct cli_state *targetcli2 = NULL;
1903 SMBCSRV *srv = NULL;
1906 TALLOC_CTX *frame = talloc_stackframe();
1909 if (!ocontext || !ncontext ||
1910 !ocontext->internal->initialized ||
1911 !ncontext->internal->initialized) {
1913 errno = EINVAL; /* Best I can think of ... */
1918 if (!oname || !nname) {
1924 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1926 if (SMBC_parse_path(frame,
1942 if (!user1 || user1[0] == (char)0) {
1943 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1951 if (SMBC_parse_path(frame,
1967 if (!user2 || user2[0] == (char)0) {
1968 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1976 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1977 strcmp(user1, user2)) {
1978 /* Can't rename across file systems, or users?? */
1984 srv = SMBC_server(frame, ocontext, True,
1985 server1, port1, share1, &workgroup, &user1, &password1);
1992 /* set the credentials to make DFS work */
1993 smbc_set_credentials_with_fallback(ocontext,
1998 /*d_printf(">>>rename: resolving %s\n", path1);*/
1999 status = cli_resolve_path(frame, "", ocontext->internal->auth_info,
2000 srv->cli, path1, &targetcli1, &targetpath1);
2001 if (!NT_STATUS_IS_OK(status)) {
2002 d_printf("Could not resolve %s\n", path1);
2008 /* set the credentials to make DFS work */
2009 smbc_set_credentials_with_fallback(ncontext,
2014 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2015 /*d_printf(">>>rename: resolving %s\n", path2);*/
2016 status = cli_resolve_path(frame, "", ncontext->internal->auth_info,
2017 srv->cli, path2, &targetcli2, &targetpath2);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 d_printf("Could not resolve %s\n", path2);
2024 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2026 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2027 strcmp(targetcli1->share, targetcli2->share))
2029 /* can't rename across file systems */
2035 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2036 int eno = SMBC_errno(ocontext, targetcli1);
2038 if (eno != EEXIST ||
2039 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2040 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2050 return 0; /* Success */
2053 struct smbc_notify_cb_state {
2054 struct tevent_context *ev;
2055 struct cli_state *cli;
2058 uint32_t completion_filter;
2059 unsigned callback_timeout_ms;
2060 smbc_notify_callback_fn cb;
2064 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2065 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2067 static struct tevent_req *smbc_notify_cb_send(
2068 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2069 uint16_t fnum, bool recursive, uint32_t completion_filter,
2070 unsigned callback_timeout_ms,
2071 smbc_notify_callback_fn cb, void *private_data)
2073 struct tevent_req *req, *subreq;
2074 struct smbc_notify_cb_state *state;
2076 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2083 state->recursive = recursive;
2084 state->completion_filter = completion_filter;
2085 state->callback_timeout_ms = callback_timeout_ms;
2087 state->private_data = private_data;
2089 subreq = cli_notify_send(
2090 state, state->ev, state->cli, state->fnum, 1000,
2091 state->completion_filter, state->recursive);
2092 if (tevent_req_nomem(subreq, req)) {
2093 return tevent_req_post(req, ev);
2095 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2097 if (state->callback_timeout_ms == 0) {
2101 subreq = tevent_wakeup_send(
2103 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2104 state->callback_timeout_ms*1000));
2105 if (tevent_req_nomem(subreq, req)) {
2106 return tevent_req_post(req, ev);
2108 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2113 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2115 struct tevent_req *req = tevent_req_callback_data(
2116 subreq, struct tevent_req);
2117 struct smbc_notify_cb_state *state = tevent_req_data(
2118 req, struct smbc_notify_cb_state);
2119 uint32_t num_changes;
2120 struct notify_change *changes;
2124 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2125 TALLOC_FREE(subreq);
2126 if (tevent_req_nterror(req, status)) {
2131 struct smbc_notify_callback_action actions[num_changes];
2134 for (i=0; i<num_changes; i++) {
2135 actions[i].action = changes[i].action;
2136 actions[i].filename = changes[i].name;
2139 cb_ret = state->cb(actions, num_changes, state->private_data);
2142 TALLOC_FREE(changes);
2145 tevent_req_done(req);
2149 subreq = cli_notify_send(
2150 state, state->ev, state->cli, state->fnum, 1000,
2151 state->completion_filter, state->recursive);
2152 if (tevent_req_nomem(subreq, req)) {
2155 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2158 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2160 struct tevent_req *req = tevent_req_callback_data(
2161 subreq, struct tevent_req);
2162 struct smbc_notify_cb_state *state = tevent_req_data(
2163 req, struct smbc_notify_cb_state);
2167 ok = tevent_wakeup_recv(subreq);
2168 TALLOC_FREE(subreq);
2170 tevent_req_oom(req);
2174 cb_ret = state->cb(NULL, 0, state->private_data);
2176 tevent_req_done(req);
2180 subreq = tevent_wakeup_send(
2182 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2183 state->callback_timeout_ms*1000));
2184 if (tevent_req_nomem(subreq, req)) {
2187 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2190 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2192 return tevent_req_simple_recv_ntstatus(req);
2195 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2196 bool recursive, uint32_t completion_filter,
2197 unsigned callback_timeout_ms,
2198 smbc_notify_callback_fn cb, void *private_data)
2200 TALLOC_CTX *frame = talloc_stackframe();
2201 struct tevent_context *ev;
2202 struct tevent_req *req;
2203 NTSTATUS status = NT_STATUS_NO_MEMORY;
2205 ev = samba_tevent_context_init(frame);
2209 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2211 callback_timeout_ms, cb, private_data);
2215 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2218 status = smbc_notify_cb_recv(req);
2226 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2227 uint32_t completion_filter, unsigned callback_timeout_ms,
2228 smbc_notify_callback_fn cb, void *private_data)
2230 TALLOC_CTX *frame = talloc_stackframe();
2231 struct cli_state *cli;
2232 char *server = NULL;
2235 char *password = NULL;
2236 char *options = NULL;
2237 char *workgroup = NULL;
2243 if ((context == NULL) || !context->internal->initialized) {
2248 if ((dir == NULL) ||
2249 !SMBC_dlist_contains(context->internal->files, dir)) {
2255 if (SMBC_parse_path(frame,
2266 DEBUG(4, ("no valid path\n"));
2268 errno = EINVAL + 8194;
2272 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2273 "path='%s' options='%s'\n",
2274 dir->fname, server, share, path, options));
2276 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2277 (int)recursive, completion_filter));
2279 cli = dir->srv->cli;
2280 status = cli_ntcreate(
2281 cli, path, 0, FILE_READ_DATA, 0,
2282 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2283 FILE_OPEN, 0, 0, &fnum, NULL);
2284 if (!NT_STATUS_IS_OK(status)) {
2285 int err = SMBC_errno(context, cli);
2291 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2292 callback_timeout_ms, cb, private_data);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 int err = SMBC_errno(context, cli);
2295 cli_close(cli, fnum);
2301 cli_close(cli, fnum);