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_GENERAL_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 memset(&u_info, '\0', sizeof(u_info));
487 u_info.username = talloc_strdup(frame,user);
488 u_info.password = talloc_strdup(frame,password);
489 if (!u_info.username || !u_info.password) {
491 SAFE_FREE(dir->fname);
499 * We have server and share and path empty but options
500 * requesting that we scan all master browsers for their list
501 * of workgroups/domains. This implies that we must first try
502 * broadcast queries to find all master browsers, and if that
503 * doesn't work, then try our other methods which return only
504 * a single master browser.
508 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
510 if (!NT_STATUS_IS_OK(status))
513 TALLOC_FREE(ip_list);
515 if (!find_master_ip(workgroup, &server_addr)) {
518 SAFE_FREE(dir->fname);
526 ip_list = (struct sockaddr_storage *)talloc_memdup(
527 talloc_tos(), &server_addr,
528 sizeof(server_addr));
529 if (ip_list == NULL) {
531 SAFE_FREE(dir->fname);
541 for (i = 0; i < count && i < max_lmb_count; i++) {
542 char addr[INET6_ADDRSTRLEN];
544 struct cli_state *cli = NULL;
546 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
547 DEBUG(99, ("Found master browser %d of %d: %s\n",
548 i+1, MAX(count, max_lmb_count),
551 cli = get_ipc_connect_master_ip(talloc_tos(),
555 /* cli == NULL is the master browser refused to talk or
556 could not be found */
561 workgroup = talloc_strdup(frame, wg_ptr);
562 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
566 if (!workgroup || !server) {
568 SAFE_FREE(dir->fname);
576 DEBUG(4, ("using workgroup %s %s\n",
580 * For each returned master browser IP address, get a
581 * connection to IPC$ on the server if we do not
582 * already have one, and determine the
583 * workgroups/domains that it knows about.
586 srv = SMBC_server(frame, context, True, server, port, "IPC$",
587 &workgroup, &user, &password);
593 dir->dir_type = SMBC_WORKGROUP;
595 /* Now, list the stuff ... */
597 if (!cli_NetServerEnum(srv->cli,
606 TALLOC_FREE(ip_list);
609 * Server not an empty string ... Check the rest and see what
612 if (*share == '\0') {
615 /* Should not have empty share with path */
617 SAFE_FREE(dir->fname);
621 errno = EINVAL + 8197;
627 * We don't know if <server> is really a server name
628 * or is a workgroup/domain name. If we already have
629 * a server structure for it, we'll use it.
630 * Otherwise, check to see if <server><1D>,
631 * <server><1B>, or <server><20> translates. We check
632 * to see if <server> is an IP address first.
636 * See if we have an existing server. Do not
637 * establish a connection if one does not already
640 srv = SMBC_server(frame, context, False,
641 server, port, "IPC$",
642 &workgroup, &user, &password);
645 * If no existing server and not an IP addr, look for
649 !is_ipaddress(server) &&
650 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
651 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
653 * "server" is actually a workgroup name,
654 * not a server. Make this clear.
656 char *wgroup = server;
659 dir->dir_type = SMBC_SERVER;
662 * Get the backup list ...
664 if (!name_status_find(wgroup, 0, 0,
665 &rem_ss, buserver)) {
666 char addr[INET6_ADDRSTRLEN];
668 print_sockaddr(addr, sizeof(addr), &rem_ss);
669 DEBUG(0,("Could not get name of "
670 "local/domain master browser "
671 "for workgroup %s from "
676 SAFE_FREE(dir->fname);
686 * Get a connection to IPC$ on the server if
687 * we do not already have one
689 srv = SMBC_server(frame, context, True,
690 buserver, port, "IPC$",
694 DEBUG(0, ("got no contact to IPC$\n"));
696 SAFE_FREE(dir->fname);
706 /* Now, list the servers ... */
707 if (!cli_NetServerEnum(srv->cli, wgroup,
712 SAFE_FREE(dir->fname);
719 (resolve_name(server, &rem_ss, 0x20, false))) {
722 * If we hadn't found the server, get one now
725 srv = SMBC_server(frame, context, True,
726 server, port, "IPC$",
733 SAFE_FREE(dir->fname);
741 dir->dir_type = SMBC_FILE_SHARE;
744 /* List the shares ... */
746 if (net_share_enum_rpc(
755 errno = cli_errno(srv->cli);
757 SAFE_FREE(dir->fname);
765 /* Neither the workgroup nor server exists */
766 errno = ECONNREFUSED;
768 SAFE_FREE(dir->fname);
778 * The server and share are specified ... work from
782 struct cli_state *targetcli;
785 /* We connect to the server and list the directory */
786 dir->dir_type = SMBC_FILE_SHARE;
788 srv = SMBC_server(frame, context, True, server, port, share,
789 &workgroup, &user, &password);
793 SAFE_FREE(dir->fname);
802 /* Now, list the files ... */
804 p = path + strlen(path);
805 path = talloc_asprintf_append(path, "\\*");
808 SAFE_FREE(dir->fname);
815 status = cli_resolve_path(
816 frame, "", context->internal->auth_info,
817 srv->cli, path, &targetcli, &targetpath);
818 if (!NT_STATUS_IS_OK(status)) {
819 d_printf("Could not resolve %s\n", path);
821 SAFE_FREE(dir->fname);
828 status = cli_list(targetcli, targetpath,
829 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
830 dir_list_fn, (void *)dir);
831 if (!NT_STATUS_IS_OK(status)) {
833 SAFE_FREE(dir->fname);
836 saved_errno = SMBC_errno(context, targetcli);
838 if (saved_errno == EINVAL) {
840 * See if they asked to opendir
841 * something other than a directory.
842 * If so, the converted error value we
843 * got would have been EINVAL rather
846 *p = '\0'; /* restore original path */
848 if (SMBC_getatr(context, srv, path,
850 NULL, NULL, NULL, NULL,
852 ! IS_DOS_DIR(mode)) {
854 /* It is. Correct the error value */
855 saved_errno = ENOTDIR;
860 * If there was an error and the server is no
863 if (cli_is_error(targetcli) &&
864 smbc_getFunctionCheckServer(context)(context, srv)) {
866 /* ... then remove it. */
867 if (smbc_getFunctionRemoveUnusedServer(context)(context,
870 * We could not remove the
871 * server completely, remove
872 * it from the cache so we
873 * will not get it again. It
874 * will be removed when the
875 * last file/dir is closed.
877 smbc_getFunctionRemoveCachedServer(context)(context, srv);
889 DLIST_ADD(context->internal->files, dir);
896 * Routine to close a directory
900 SMBC_closedir_ctx(SMBCCTX *context,
903 TALLOC_CTX *frame = talloc_stackframe();
905 if (!context || !context->internal->initialized) {
911 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
917 remove_dir(dir); /* Clean it up */
919 DLIST_REMOVE(context->internal->files, dir);
923 SAFE_FREE(dir->fname);
924 SAFE_FREE(dir); /* Free the space too */
933 smbc_readdir_internal(SMBCCTX * context,
934 struct smbc_dirent *dest,
935 struct smbc_dirent *src,
938 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
940 /* url-encode the name. get back remaining buffer space */
942 smbc_urlencode(dest->name, src->name, max_namebuf_len);
944 /* We now know the name length */
945 dest->namelen = strlen(dest->name);
947 /* Save the pointer to the beginning of the comment */
948 dest->comment = dest->name + dest->namelen + 1;
950 /* Copy the comment */
951 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
952 dest->comment[max_namebuf_len - 1] = '\0';
954 /* Save other fields */
955 dest->smbc_type = src->smbc_type;
956 dest->commentlen = strlen(dest->comment);
957 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
961 /* No encoding. Just copy the entry as is. */
962 memcpy(dest, src, src->dirlen);
963 dest->comment = (char *)(&dest->name + src->namelen + 1);
969 * Routine to get a directory entry
973 SMBC_readdir_ctx(SMBCCTX *context,
977 struct smbc_dirent *dirp, *dirent;
978 TALLOC_CTX *frame = talloc_stackframe();
980 /* Check that all is ok first ... */
982 if (!context || !context->internal->initialized) {
985 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
991 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
994 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
1000 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1003 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1009 if (!dir->dir_next) {
1014 dirent = dir->dir_next->dirent;
1023 dirp = &context->internal->dirent;
1024 maxlen = sizeof(context->internal->_dirent_name);
1026 smbc_readdir_internal(context, dirp, dirent, maxlen);
1028 dir->dir_next = dir->dir_next->next;
1035 * Routine to get directory entries
1039 SMBC_getdents_ctx(SMBCCTX *context,
1041 struct smbc_dirent *dirp,
1047 char *ndir = (char *)dirp;
1048 struct smbc_dir_list *dirlist;
1049 TALLOC_CTX *frame = talloc_stackframe();
1051 /* Check that all is ok first ... */
1053 if (!context || !context->internal->initialized) {
1061 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1069 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1078 * Now, retrieve the number of entries that will fit in what was passed
1079 * We have to figure out if the info is in the list, or we need to
1080 * send a request to the server to get the info.
1083 while ((dirlist = dir->dir_next)) {
1084 struct smbc_dirent *dirent;
1085 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1087 if (!dirlist->dirent) {
1089 errno = ENOENT; /* Bad error */
1095 /* Do urlencoding of next entry, if so selected */
1096 dirent = &context->internal->dirent;
1097 maxlen = sizeof(context->internal->_dirent_name);
1098 smbc_readdir_internal(context, dirent,
1099 dirlist->dirent, maxlen);
1101 reqd = dirent->dirlen;
1105 if (rem < count) { /* We managed to copy something */
1112 else { /* Nothing copied ... */
1114 errno = EINVAL; /* Not enough space ... */
1122 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1124 currentEntry->comment = ¤tEntry->name[0] +
1125 dirent->namelen + 1;
1130 /* Try and align the struct for the next entry
1131 on a valid pointer boundary by appending zeros */
1132 while((rem > 0) && ((uintptr_t)ndir & (sizeof(void*) - 1))) {
1136 currentEntry->dirlen++;
1139 dir->dir_next = dirlist = dirlist -> next;
1152 * Routine to create a directory ...
1156 SMBC_mkdir_ctx(SMBCCTX *context,
1160 SMBCSRV *srv = NULL;
1161 char *server = NULL;
1164 char *password = NULL;
1165 char *workgroup = NULL;
1167 char *targetpath = NULL;
1169 struct cli_state *targetcli = NULL;
1170 TALLOC_CTX *frame = talloc_stackframe();
1173 if (!context || !context->internal->initialized) {
1185 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1187 if (SMBC_parse_path(frame,
1203 if (!user || user[0] == (char)0) {
1204 user = talloc_strdup(frame, smbc_getUser(context));
1212 srv = SMBC_server(frame, context, True,
1213 server, port, share, &workgroup, &user, &password);
1218 return -1; /* errno set by SMBC_server */
1222 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1223 status = cli_resolve_path(frame, "", context->internal->auth_info,
1224 srv->cli, path, &targetcli, &targetpath);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 d_printf("Could not resolve %s\n", path);
1231 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1233 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1234 errno = SMBC_errno(context, targetcli);
1246 * Our list function simply checks to see if a directory is not empty
1250 rmdir_list_fn(const char *mnt,
1251 struct file_info *finfo,
1255 if (strncmp(finfo->name, ".", 1) != 0 &&
1256 strncmp(finfo->name, "..", 2) != 0) {
1257 bool *smbc_rmdir_dirempty = (bool *)state;
1258 *smbc_rmdir_dirempty = false;
1260 return NT_STATUS_OK;
1264 * Routine to remove a directory
1268 SMBC_rmdir_ctx(SMBCCTX *context,
1271 SMBCSRV *srv = NULL;
1272 char *server = NULL;
1275 char *password = NULL;
1276 char *workgroup = NULL;
1278 char *targetpath = NULL;
1280 struct cli_state *targetcli = NULL;
1281 TALLOC_CTX *frame = talloc_stackframe();
1284 if (!context || !context->internal->initialized) {
1296 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1298 if (SMBC_parse_path(frame,
1314 if (!user || user[0] == (char)0) {
1315 user = talloc_strdup(frame, smbc_getUser(context));
1323 srv = SMBC_server(frame, context, True,
1324 server, port, share, &workgroup, &user, &password);
1329 return -1; /* errno set by SMBC_server */
1333 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1334 status = cli_resolve_path(frame, "", context->internal->auth_info,
1335 srv->cli, path, &targetcli, &targetpath);
1336 if (!NT_STATUS_IS_OK(status)) {
1337 d_printf("Could not resolve %s\n", path);
1342 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1344 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1346 errno = SMBC_errno(context, targetcli);
1348 if (errno == EACCES) { /* Check if the dir empty or not */
1350 /* Local storage to avoid buffer overflows */
1352 bool smbc_rmdir_dirempty = true;
1354 lpath = talloc_asprintf(frame, "%s\\*",
1362 status = cli_list(targetcli, lpath,
1363 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1365 &smbc_rmdir_dirempty);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 /* Fix errno to ignore latest error ... */
1369 DEBUG(5, ("smbc_rmdir: "
1370 "cli_list returned an error: %d\n",
1371 SMBC_errno(context, targetcli)));
1376 if (smbc_rmdir_dirempty)
1394 * Routine to return the current directory position
1398 SMBC_telldir_ctx(SMBCCTX *context,
1401 TALLOC_CTX *frame = talloc_stackframe();
1403 if (!context || !context->internal->initialized) {
1411 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1419 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1427 /* See if we're already at the end. */
1428 if (dir->dir_next == NULL) {
1435 * We return the pointer here as the offset
1438 return (off_t)(long)dir->dir_next->dirent;
1442 * A routine to run down the list and see if the entry is OK
1445 static struct smbc_dir_list *
1446 check_dir_ent(struct smbc_dir_list *list,
1447 struct smbc_dirent *dirent)
1450 /* Run down the list looking for what we want */
1454 struct smbc_dir_list *tmp = list;
1458 if (tmp->dirent == dirent)
1467 return NULL; /* Not found, or an error */
1473 * Routine to seek on a directory
1477 SMBC_lseekdir_ctx(SMBCCTX *context,
1481 long int l_offset = offset; /* Handle problems of size */
1482 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1483 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1484 TALLOC_CTX *frame = talloc_stackframe();
1486 if (!context || !context->internal->initialized) {
1494 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1502 /* Now, check what we were passed and see if it is OK ... */
1504 if (dirent == NULL) { /* Seek to the begining of the list */
1506 dir->dir_next = dir->dir_list;
1512 if (offset == -1) { /* Seek to the end of the list */
1513 dir->dir_next = NULL;
1518 /* Now, run down the list and make sure that the entry is OK */
1519 /* This may need to be changed if we change the format of the list */
1521 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1522 errno = EINVAL; /* Bad entry */
1527 dir->dir_next = list_ent;
1534 * Routine to fstat a dir
1538 SMBC_fstatdir_ctx(SMBCCTX *context,
1543 if (!context || !context->internal->initialized) {
1549 /* No code yet ... */
1554 SMBC_chmod_ctx(SMBCCTX *context,
1558 SMBCSRV *srv = NULL;
1559 char *server = NULL;
1562 char *password = NULL;
1563 char *workgroup = NULL;
1564 char *targetpath = NULL;
1565 struct cli_state *targetcli = NULL;
1569 TALLOC_CTX *frame = talloc_stackframe();
1572 if (!context || !context->internal->initialized) {
1574 errno = EINVAL; /* Best I can think of ... */
1585 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1587 if (SMBC_parse_path(frame,
1603 if (!user || user[0] == (char)0) {
1604 user = talloc_strdup(frame, smbc_getUser(context));
1612 srv = SMBC_server(frame, context, True,
1613 server, port, share, &workgroup, &user, &password);
1617 return -1; /* errno set by SMBC_server */
1620 /*d_printf(">>>unlink: resolving %s\n", path);*/
1621 status = cli_resolve_path(frame, "", context->internal->auth_info,
1622 srv->cli, path, &targetcli, &targetpath);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 d_printf("Could not resolve %s\n", path);
1632 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1633 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1634 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1635 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1637 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1638 errno = SMBC_errno(context, targetcli);
1648 SMBC_utimes_ctx(SMBCCTX *context,
1650 struct timeval *tbuf)
1652 SMBCSRV *srv = NULL;
1653 char *server = NULL;
1656 char *password = NULL;
1657 char *workgroup = NULL;
1662 TALLOC_CTX *frame = talloc_stackframe();
1664 if (!context || !context->internal->initialized) {
1666 errno = EINVAL; /* Best I can think of ... */
1678 access_time = write_time = time(NULL);
1680 access_time = tbuf[0].tv_sec;
1681 write_time = tbuf[1].tv_sec;
1689 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1690 atimebuf[sizeof(atimebuf) - 1] = '\0';
1691 if ((p = strchr(atimebuf, '\n')) != NULL) {
1695 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1696 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1697 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1701 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1702 fname, atimebuf, mtimebuf);
1705 if (SMBC_parse_path(frame,
1721 if (!user || user[0] == (char)0) {
1722 user = talloc_strdup(frame, smbc_getUser(context));
1730 srv = SMBC_server(frame, context, True,
1731 server, port, share, &workgroup, &user, &password);
1735 return -1; /* errno set by SMBC_server */
1738 if (!SMBC_setatr(context, srv, path,
1739 0, access_time, write_time, 0, 0)) {
1741 return -1; /* errno set by SMBC_setatr */
1749 * Routine to unlink() a file
1753 SMBC_unlink_ctx(SMBCCTX *context,
1756 char *server = NULL;
1759 char *password = NULL;
1760 char *workgroup = NULL;
1762 char *targetpath = NULL;
1764 struct cli_state *targetcli = NULL;
1765 SMBCSRV *srv = NULL;
1766 TALLOC_CTX *frame = talloc_stackframe();
1769 if (!context || !context->internal->initialized) {
1771 errno = EINVAL; /* Best I can think of ... */
1784 if (SMBC_parse_path(frame,
1800 if (!user || user[0] == (char)0) {
1801 user = talloc_strdup(frame, smbc_getUser(context));
1809 srv = SMBC_server(frame, context, True,
1810 server, port, share, &workgroup, &user, &password);
1814 return -1; /* SMBC_server sets errno */
1818 /*d_printf(">>>unlink: resolving %s\n", path);*/
1819 status = cli_resolve_path(frame, "", context->internal->auth_info,
1820 srv->cli, path, &targetcli, &targetpath);
1821 if (!NT_STATUS_IS_OK(status)) {
1822 d_printf("Could not resolve %s\n", path);
1827 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1829 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1831 errno = SMBC_errno(context, targetcli);
1833 if (errno == EACCES) { /* Check if the file is a directory */
1838 struct timespec write_time_ts;
1839 struct timespec access_time_ts;
1840 struct timespec change_time_ts;
1843 if (!SMBC_getatr(context, srv, path, &mode, &size,
1850 /* Hmmm, bad error ... What? */
1852 errno = SMBC_errno(context, targetcli);
1859 if (IS_DOS_DIR(mode))
1862 errno = saverr; /* Restore this */
1873 return 0; /* Success ... */
1878 * Routine to rename() a file
1882 SMBC_rename_ctx(SMBCCTX *ocontext,
1887 char *server1 = NULL;
1888 char *share1 = NULL;
1889 char *server2 = NULL;
1890 char *share2 = NULL;
1893 char *password1 = NULL;
1894 char *password2 = NULL;
1895 char *workgroup = NULL;
1898 char *targetpath1 = NULL;
1899 char *targetpath2 = NULL;
1900 struct cli_state *targetcli1 = NULL;
1901 struct cli_state *targetcli2 = NULL;
1902 SMBCSRV *srv = NULL;
1905 TALLOC_CTX *frame = talloc_stackframe();
1908 if (!ocontext || !ncontext ||
1909 !ocontext->internal->initialized ||
1910 !ncontext->internal->initialized) {
1912 errno = EINVAL; /* Best I can think of ... */
1917 if (!oname || !nname) {
1923 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1925 if (SMBC_parse_path(frame,
1941 if (!user1 || user1[0] == (char)0) {
1942 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1950 if (SMBC_parse_path(frame,
1966 if (!user2 || user2[0] == (char)0) {
1967 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1975 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1976 strcmp(user1, user2)) {
1977 /* Can't rename across file systems, or users?? */
1983 srv = SMBC_server(frame, ocontext, True,
1984 server1, port1, share1, &workgroup, &user1, &password1);
1991 /* set the credentials to make DFS work */
1992 smbc_set_credentials_with_fallback(ocontext,
1997 /*d_printf(">>>rename: resolving %s\n", path1);*/
1998 status = cli_resolve_path(frame, "", ocontext->internal->auth_info,
1999 srv->cli, path1, &targetcli1, &targetpath1);
2000 if (!NT_STATUS_IS_OK(status)) {
2001 d_printf("Could not resolve %s\n", path1);
2007 /* set the credentials to make DFS work */
2008 smbc_set_credentials_with_fallback(ncontext,
2013 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2014 /*d_printf(">>>rename: resolving %s\n", path2);*/
2015 status = cli_resolve_path(frame, "", ncontext->internal->auth_info,
2016 srv->cli, path2, &targetcli2, &targetpath2);
2017 if (!NT_STATUS_IS_OK(status)) {
2018 d_printf("Could not resolve %s\n", path2);
2023 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2025 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2026 strcmp(targetcli1->share, targetcli2->share))
2028 /* can't rename across file systems */
2034 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2035 int eno = SMBC_errno(ocontext, targetcli1);
2037 if (eno != EEXIST ||
2038 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2039 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2049 return 0; /* Success */
2052 struct smbc_notify_cb_state {
2053 struct tevent_context *ev;
2054 struct cli_state *cli;
2057 uint32_t completion_filter;
2058 unsigned callback_timeout_ms;
2059 smbc_notify_callback_fn cb;
2063 static void smbc_notify_cb_got_changes(struct tevent_req *subreq);
2064 static void smbc_notify_cb_timedout(struct tevent_req *subreq);
2066 static struct tevent_req *smbc_notify_cb_send(
2067 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
2068 uint16_t fnum, bool recursive, uint32_t completion_filter,
2069 unsigned callback_timeout_ms,
2070 smbc_notify_callback_fn cb, void *private_data)
2072 struct tevent_req *req, *subreq;
2073 struct smbc_notify_cb_state *state;
2075 req = tevent_req_create(mem_ctx, &state, struct smbc_notify_cb_state);
2082 state->recursive = recursive;
2083 state->completion_filter = completion_filter;
2084 state->callback_timeout_ms = callback_timeout_ms;
2086 state->private_data = private_data;
2088 subreq = cli_notify_send(
2089 state, state->ev, state->cli, state->fnum, 1000,
2090 state->completion_filter, state->recursive);
2091 if (tevent_req_nomem(subreq, req)) {
2092 return tevent_req_post(req, ev);
2094 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2096 if (state->callback_timeout_ms == 0) {
2100 subreq = tevent_wakeup_send(
2102 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2103 state->callback_timeout_ms*1000));
2104 if (tevent_req_nomem(subreq, req)) {
2105 return tevent_req_post(req, ev);
2107 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2112 static void smbc_notify_cb_got_changes(struct tevent_req *subreq)
2114 struct tevent_req *req = tevent_req_callback_data(
2115 subreq, struct tevent_req);
2116 struct smbc_notify_cb_state *state = tevent_req_data(
2117 req, struct smbc_notify_cb_state);
2118 uint32_t num_changes;
2119 struct notify_change *changes;
2123 status = cli_notify_recv(subreq, state, &num_changes, &changes);
2124 TALLOC_FREE(subreq);
2125 if (tevent_req_nterror(req, status)) {
2130 struct smbc_notify_callback_action actions[num_changes];
2133 for (i=0; i<num_changes; i++) {
2134 actions[i].action = changes[i].action;
2135 actions[i].filename = changes[i].name;
2138 cb_ret = state->cb(actions, num_changes, state->private_data);
2141 TALLOC_FREE(changes);
2144 tevent_req_done(req);
2148 subreq = cli_notify_send(
2149 state, state->ev, state->cli, state->fnum, 1000,
2150 state->completion_filter, state->recursive);
2151 if (tevent_req_nomem(subreq, req)) {
2154 tevent_req_set_callback(subreq, smbc_notify_cb_got_changes, req);
2157 static void smbc_notify_cb_timedout(struct tevent_req *subreq)
2159 struct tevent_req *req = tevent_req_callback_data(
2160 subreq, struct tevent_req);
2161 struct smbc_notify_cb_state *state = tevent_req_data(
2162 req, struct smbc_notify_cb_state);
2166 ok = tevent_wakeup_recv(subreq);
2167 TALLOC_FREE(subreq);
2169 tevent_req_oom(req);
2173 cb_ret = state->cb(NULL, 0, state->private_data);
2175 tevent_req_done(req);
2179 subreq = tevent_wakeup_send(
2181 tevent_timeval_current_ofs(state->callback_timeout_ms/1000,
2182 state->callback_timeout_ms*1000));
2183 if (tevent_req_nomem(subreq, req)) {
2186 tevent_req_set_callback(subreq, smbc_notify_cb_timedout, req);
2189 static NTSTATUS smbc_notify_cb_recv(struct tevent_req *req)
2191 return tevent_req_simple_recv_ntstatus(req);
2194 static NTSTATUS smbc_notify_cb(struct cli_state *cli, uint16_t fnum,
2195 bool recursive, uint32_t completion_filter,
2196 unsigned callback_timeout_ms,
2197 smbc_notify_callback_fn cb, void *private_data)
2199 TALLOC_CTX *frame = talloc_stackframe();
2200 struct tevent_context *ev;
2201 struct tevent_req *req;
2202 NTSTATUS status = NT_STATUS_NO_MEMORY;
2204 ev = samba_tevent_context_init(frame);
2208 req = smbc_notify_cb_send(frame, ev, cli, fnum, recursive,
2210 callback_timeout_ms, cb, private_data);
2214 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2217 status = smbc_notify_cb_recv(req);
2225 SMBC_notify_ctx(SMBCCTX *context, SMBCFILE *dir, smbc_bool recursive,
2226 uint32_t completion_filter, unsigned callback_timeout_ms,
2227 smbc_notify_callback_fn cb, void *private_data)
2229 TALLOC_CTX *frame = talloc_stackframe();
2230 struct cli_state *cli;
2231 char *server = NULL;
2234 char *password = NULL;
2235 char *options = NULL;
2236 char *workgroup = NULL;
2242 if ((context == NULL) || !context->internal->initialized) {
2247 if ((dir == NULL) ||
2248 !SMBC_dlist_contains(context->internal->files, dir)) {
2254 if (SMBC_parse_path(frame,
2265 DEBUG(4, ("no valid path\n"));
2267 errno = EINVAL + 8194;
2271 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2272 "path='%s' options='%s'\n",
2273 dir->fname, server, share, path, options));
2275 DEBUG(4, ("%s(%p, %d, %"PRIu32")\n", __func__, dir,
2276 (int)recursive, completion_filter));
2278 cli = dir->srv->cli;
2279 status = cli_ntcreate(
2280 cli, path, 0, FILE_READ_DATA, 0,
2281 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2282 FILE_OPEN, 0, 0, &fnum, NULL);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 int err = SMBC_errno(context, cli);
2290 status = smbc_notify_cb(cli, fnum, recursive != 0, completion_filter,
2291 callback_timeout_ms, cb, private_data);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 int err = SMBC_errno(context, cli);
2294 cli_close(cli, fnum);
2300 cli_close(cli, fnum);