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"
36 * Routine to open a directory
37 * We accept the URL syntax explained in SMBC_parse_path(), above.
41 remove_dir(SMBCFILE *dir)
43 struct smbc_dir_list *d,*f;
55 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
60 add_dirent(SMBCFILE *dir,
65 struct smbc_dirent *dirent;
67 int name_length = (name == NULL ? 0 : strlen(name));
68 int comment_len = (comment == NULL ? 0 : strlen(comment));
71 * Allocate space for the dirent, which must be increased by the
72 * size of the name and the comment and 1 each for the null terminator.
75 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
77 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
81 dir->dir_error = ENOMEM;
88 if (dir->dir_list == NULL) {
90 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
94 dir->dir_error = ENOMEM;
98 ZERO_STRUCTP(dir->dir_list);
100 dir->dir_end = dir->dir_next = dir->dir_list;
104 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
106 if (!dir->dir_end->next) {
109 dir->dir_error = ENOMEM;
113 ZERO_STRUCTP(dir->dir_end->next);
115 dir->dir_end = dir->dir_end->next;
118 dir->dir_end->next = NULL;
119 dir->dir_end->dirent = dirent;
121 dirent->smbc_type = type;
122 dirent->namelen = name_length;
123 dirent->commentlen = comment_len;
124 dirent->dirlen = size;
127 * dirent->namelen + 1 includes the null (no null termination needed)
128 * Ditto for dirent->commentlen.
129 * The space for the two null bytes was allocated.
131 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
132 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
133 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
140 list_unique_wg_fn(const char *name,
145 SMBCFILE *dir = (SMBCFILE *)state;
146 struct smbc_dir_list *dir_list;
147 struct smbc_dirent *dirent;
151 dirent_type = dir->dir_type;
153 if (add_dirent(dir, name, comment, dirent_type) < 0) {
154 /* An error occurred, what do we do? */
155 /* FIXME: Add some code here */
156 /* Change cli_NetServerEnum to take a fn
157 returning NTSTATUS... JRA. */
160 /* Point to the one just added */
161 dirent = dir->dir_end->dirent;
163 /* See if this was a duplicate */
164 for (dir_list = dir->dir_list;
165 dir_list != dir->dir_end;
166 dir_list = dir_list->next) {
168 strcmp(dir_list->dirent->name, dirent->name) == 0) {
169 /* Duplicate. End end of list need to be removed. */
173 if (do_remove && dir_list->next == dir->dir_end) {
174 /* Found the end of the list. Remove it. */
175 dir->dir_end = dir_list;
176 free(dir_list->next);
178 dir_list->next = NULL;
185 list_fn(const char *name,
190 SMBCFILE *dir = (SMBCFILE *)state;
194 * We need to process the type a little ...
196 * Disk share = 0x00000000
197 * Print share = 0x00000001
198 * Comms share = 0x00000002 (obsolete?)
199 * IPC$ share = 0x00000003
201 * administrative shares:
202 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
205 if (dir->dir_type == SMBC_FILE_SHARE) {
209 dirent_type = SMBC_FILE_SHARE;
213 dirent_type = SMBC_PRINTER_SHARE;
217 dirent_type = SMBC_COMMS_SHARE;
222 dirent_type = SMBC_IPC_SHARE;
226 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
231 dirent_type = dir->dir_type;
234 if (add_dirent(dir, name, comment, dirent_type) < 0) {
235 /* An error occurred, what do we do? */
236 /* FIXME: Add some code here */
237 /* Change cli_NetServerEnum to take a fn
238 returning NTSTATUS... JRA. */
243 dir_list_fn(const char *mnt,
244 struct file_info *finfo,
249 if (add_dirent((SMBCFILE *)state, finfo->name, "",
250 (finfo->mode&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
251 SMBCFILE *dir = (SMBCFILE *)state;
252 return map_nt_error_from_unix(dir->dir_error);
258 net_share_enum_rpc(struct cli_state *cli,
259 void (*fn)(const char *name,
267 uint32_t preferred_len = 0xffffffff;
269 struct srvsvc_NetShareInfoCtr info_ctr;
270 struct srvsvc_NetShareCtr1 ctr1;
272 fstring comment = "";
273 struct rpc_pipe_client *pipe_hnd = NULL;
275 uint32_t resume_handle = 0;
276 uint32_t total_entries = 0;
277 struct dcerpc_binding_handle *b;
279 /* Open the server service pipe */
280 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
282 if (!NT_STATUS_IS_OK(nt_status)) {
283 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
287 ZERO_STRUCT(info_ctr);
291 info_ctr.ctr.ctr1 = &ctr1;
293 b = pipe_hnd->binding_handle;
295 /* Issue the NetShareEnum RPC call and retrieve the response */
296 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
304 /* Was it successful? */
305 if (!NT_STATUS_IS_OK(nt_status)) {
306 /* Nope. Go clean up. */
307 result = ntstatus_to_werror(nt_status);
311 if (!W_ERROR_IS_OK(result)) {
312 /* Nope. Go clean up. */
316 if (total_entries == 0) {
317 /* Nope. Go clean up. */
318 result = WERR_GENERAL_FAILURE;
322 /* For each returned entry... */
323 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
325 /* pull out the share name */
326 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
328 /* pull out the share's comment */
329 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
331 /* Get the type value */
332 type = info_ctr.ctr.ctr1->array[i].type;
334 /* Add this share to the list */
335 (*fn)(name, type, comment, state);
339 /* Close the server service pipe */
340 TALLOC_FREE(pipe_hnd);
342 /* Tell 'em if it worked */
343 return W_ERROR_IS_OK(result) ? 0 : -1;
348 * Verify that the options specified in a URL are valid
351 SMBC_check_options(char *server,
356 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
357 "path='%s' options='%s'\n",
358 server, share, path, options));
360 /* No options at all is always ok */
361 if (! *options) return 0;
363 /* Currently, we don't support any options. */
369 SMBC_opendir_ctx(SMBCCTX *context,
376 char *password = NULL;
377 char *options = NULL;
378 char *workgroup = NULL;
384 SMBCFILE *dir = NULL;
385 struct sockaddr_storage rem_ss;
386 TALLOC_CTX *frame = talloc_stackframe();
388 if (!context || !context->internal->initialized) {
389 DEBUG(4, ("no valid context\n"));
391 errno = EINVAL + 8192;
397 DEBUG(4, ("no valid fname\n"));
399 errno = EINVAL + 8193;
403 if (SMBC_parse_path(frame,
414 DEBUG(4, ("no valid path\n"));
416 errno = EINVAL + 8194;
420 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
421 "path='%s' options='%s'\n",
422 fname, server, share, path, options));
424 /* Ensure the options are valid */
425 if (SMBC_check_options(server, share, path, options)) {
426 DEBUG(4, ("unacceptable options (%s)\n", options));
428 errno = EINVAL + 8195;
432 if (!user || user[0] == (char)0) {
433 user = talloc_strdup(frame, smbc_getUser(context));
441 dir = SMB_MALLOC_P(SMBCFILE);
452 dir->fname = SMB_STRDUP(fname);
456 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
458 if (server[0] == (char)0) {
463 struct sockaddr_storage *ip_list;
464 struct sockaddr_storage server_addr;
465 struct user_auth_info u_info;
468 if (share[0] != (char)0 || path[0] != (char)0) {
471 SAFE_FREE(dir->fname);
475 errno = EINVAL + 8196;
479 /* Determine how many local master browsers to query */
480 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
482 : smbc_getOptionBrowseMaxLmbCount(context));
484 memset(&u_info, '\0', sizeof(u_info));
485 u_info.username = talloc_strdup(frame,user);
486 u_info.password = talloc_strdup(frame,password);
487 if (!u_info.username || !u_info.password) {
489 SAFE_FREE(dir->fname);
497 * We have server and share and path empty but options
498 * requesting that we scan all master browsers for their list
499 * of workgroups/domains. This implies that we must first try
500 * broadcast queries to find all master browsers, and if that
501 * doesn't work, then try our other methods which return only
502 * a single master browser.
506 status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
508 if (!NT_STATUS_IS_OK(status))
511 TALLOC_FREE(ip_list);
513 if (!find_master_ip(workgroup, &server_addr)) {
516 SAFE_FREE(dir->fname);
524 ip_list = (struct sockaddr_storage *)talloc_memdup(
525 talloc_tos(), &server_addr,
526 sizeof(server_addr));
527 if (ip_list == NULL) {
529 SAFE_FREE(dir->fname);
539 for (i = 0; i < count && i < max_lmb_count; i++) {
540 char addr[INET6_ADDRSTRLEN];
542 struct cli_state *cli = NULL;
544 print_sockaddr(addr, sizeof(addr), &ip_list[i]);
545 DEBUG(99, ("Found master browser %d of %d: %s\n",
546 i+1, MAX(count, max_lmb_count),
549 cli = get_ipc_connect_master_ip(talloc_tos(),
553 /* cli == NULL is the master browser refused to talk or
554 could not be found */
559 workgroup = talloc_strdup(frame, wg_ptr);
560 server = talloc_strdup(frame, smbXcli_conn_remote_name(cli->conn));
564 if (!workgroup || !server) {
566 SAFE_FREE(dir->fname);
574 DEBUG(4, ("using workgroup %s %s\n",
578 * For each returned master browser IP address, get a
579 * connection to IPC$ on the server if we do not
580 * already have one, and determine the
581 * workgroups/domains that it knows about.
584 srv = SMBC_server(frame, context, True, server, port, "IPC$",
585 &workgroup, &user, &password);
591 dir->dir_type = SMBC_WORKGROUP;
593 /* Now, list the stuff ... */
595 if (!cli_NetServerEnum(srv->cli,
604 TALLOC_FREE(ip_list);
607 * Server not an empty string ... Check the rest and see what
610 if (*share == '\0') {
613 /* Should not have empty share with path */
615 SAFE_FREE(dir->fname);
619 errno = EINVAL + 8197;
625 * We don't know if <server> is really a server name
626 * or is a workgroup/domain name. If we already have
627 * a server structure for it, we'll use it.
628 * Otherwise, check to see if <server><1D>,
629 * <server><1B>, or <server><20> translates. We check
630 * to see if <server> is an IP address first.
634 * See if we have an existing server. Do not
635 * establish a connection if one does not already
638 srv = SMBC_server(frame, context, False,
639 server, port, "IPC$",
640 &workgroup, &user, &password);
643 * If no existing server and not an IP addr, look for
647 !is_ipaddress(server) &&
648 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
649 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
651 * "server" is actually a workgroup name,
652 * not a server. Make this clear.
654 char *wgroup = server;
657 dir->dir_type = SMBC_SERVER;
660 * Get the backup list ...
662 if (!name_status_find(wgroup, 0, 0,
663 &rem_ss, buserver)) {
664 char addr[INET6_ADDRSTRLEN];
666 print_sockaddr(addr, sizeof(addr), &rem_ss);
667 DEBUG(0,("Could not get name of "
668 "local/domain master browser "
669 "for workgroup %s from "
674 SAFE_FREE(dir->fname);
684 * Get a connection to IPC$ on the server if
685 * we do not already have one
687 srv = SMBC_server(frame, context, True,
688 buserver, port, "IPC$",
692 DEBUG(0, ("got no contact to IPC$\n"));
694 SAFE_FREE(dir->fname);
704 /* Now, list the servers ... */
705 if (!cli_NetServerEnum(srv->cli, wgroup,
710 SAFE_FREE(dir->fname);
717 (resolve_name(server, &rem_ss, 0x20, false))) {
720 * If we hadn't found the server, get one now
723 srv = SMBC_server(frame, context, True,
724 server, port, "IPC$",
731 SAFE_FREE(dir->fname);
739 dir->dir_type = SMBC_FILE_SHARE;
742 /* List the shares ... */
744 if (net_share_enum_rpc(
753 errno = cli_errno(srv->cli);
755 SAFE_FREE(dir->fname);
763 /* Neither the workgroup nor server exists */
764 errno = ECONNREFUSED;
766 SAFE_FREE(dir->fname);
776 * The server and share are specified ... work from
780 struct cli_state *targetcli;
783 /* We connect to the server and list the directory */
784 dir->dir_type = SMBC_FILE_SHARE;
786 srv = SMBC_server(frame, context, True, server, port, share,
787 &workgroup, &user, &password);
791 SAFE_FREE(dir->fname);
800 /* Now, list the files ... */
802 p = path + strlen(path);
803 path = talloc_asprintf_append(path, "\\*");
806 SAFE_FREE(dir->fname);
813 status = cli_resolve_path(
814 frame, "", context->internal->auth_info,
815 srv->cli, path, &targetcli, &targetpath);
816 if (!NT_STATUS_IS_OK(status)) {
817 d_printf("Could not resolve %s\n", path);
819 SAFE_FREE(dir->fname);
826 status = cli_list(targetcli, targetpath,
827 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
828 dir_list_fn, (void *)dir);
829 if (!NT_STATUS_IS_OK(status)) {
831 SAFE_FREE(dir->fname);
834 saved_errno = SMBC_errno(context, targetcli);
836 if (saved_errno == EINVAL) {
838 * See if they asked to opendir
839 * something other than a directory.
840 * If so, the converted error value we
841 * got would have been EINVAL rather
844 *p = '\0'; /* restore original path */
846 if (SMBC_getatr(context, srv, path,
848 NULL, NULL, NULL, NULL,
850 ! IS_DOS_DIR(mode)) {
852 /* It is. Correct the error value */
853 saved_errno = ENOTDIR;
858 * If there was an error and the server is no
861 if (cli_is_error(targetcli) &&
862 smbc_getFunctionCheckServer(context)(context, srv)) {
864 /* ... then remove it. */
865 if (smbc_getFunctionRemoveUnusedServer(context)(context,
868 * We could not remove the
869 * server completely, remove
870 * it from the cache so we
871 * will not get it again. It
872 * will be removed when the
873 * last file/dir is closed.
875 smbc_getFunctionRemoveCachedServer(context)(context, srv);
887 DLIST_ADD(context->internal->files, dir);
894 * Routine to close a directory
898 SMBC_closedir_ctx(SMBCCTX *context,
901 TALLOC_CTX *frame = talloc_stackframe();
903 if (!context || !context->internal->initialized) {
909 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
915 remove_dir(dir); /* Clean it up */
917 DLIST_REMOVE(context->internal->files, dir);
921 SAFE_FREE(dir->fname);
922 SAFE_FREE(dir); /* Free the space too */
931 smbc_readdir_internal(SMBCCTX * context,
932 struct smbc_dirent *dest,
933 struct smbc_dirent *src,
936 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
938 /* url-encode the name. get back remaining buffer space */
940 smbc_urlencode(dest->name, src->name, max_namebuf_len);
942 /* We now know the name length */
943 dest->namelen = strlen(dest->name);
945 /* Save the pointer to the beginning of the comment */
946 dest->comment = dest->name + dest->namelen + 1;
948 /* Copy the comment */
949 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
950 dest->comment[max_namebuf_len - 1] = '\0';
952 /* Save other fields */
953 dest->smbc_type = src->smbc_type;
954 dest->commentlen = strlen(dest->comment);
955 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
959 /* No encoding. Just copy the entry as is. */
960 memcpy(dest, src, src->dirlen);
961 dest->comment = (char *)(&dest->name + src->namelen + 1);
967 * Routine to get a directory entry
971 SMBC_readdir_ctx(SMBCCTX *context,
975 struct smbc_dirent *dirp, *dirent;
976 TALLOC_CTX *frame = talloc_stackframe();
978 /* Check that all is ok first ... */
980 if (!context || !context->internal->initialized) {
983 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
989 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
992 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
998 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1001 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
1007 if (!dir->dir_next) {
1012 dirent = dir->dir_next->dirent;
1021 dirp = &context->internal->dirent;
1022 maxlen = sizeof(context->internal->_dirent_name);
1024 smbc_readdir_internal(context, dirp, dirent, maxlen);
1026 dir->dir_next = dir->dir_next->next;
1033 * Routine to get directory entries
1037 SMBC_getdents_ctx(SMBCCTX *context,
1039 struct smbc_dirent *dirp,
1045 char *ndir = (char *)dirp;
1046 struct smbc_dir_list *dirlist;
1047 TALLOC_CTX *frame = talloc_stackframe();
1049 /* Check that all is ok first ... */
1051 if (!context || !context->internal->initialized) {
1059 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1067 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1076 * Now, retrieve the number of entries that will fit in what was passed
1077 * We have to figure out if the info is in the list, or we need to
1078 * send a request to the server to get the info.
1081 while ((dirlist = dir->dir_next)) {
1082 struct smbc_dirent *dirent;
1083 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1085 if (!dirlist->dirent) {
1087 errno = ENOENT; /* Bad error */
1093 /* Do urlencoding of next entry, if so selected */
1094 dirent = &context->internal->dirent;
1095 maxlen = sizeof(context->internal->_dirent_name);
1096 smbc_readdir_internal(context, dirent,
1097 dirlist->dirent, maxlen);
1099 reqd = dirent->dirlen;
1103 if (rem < count) { /* We managed to copy something */
1110 else { /* Nothing copied ... */
1112 errno = EINVAL; /* Not enough space ... */
1120 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1122 currentEntry->comment = ¤tEntry->name[0] +
1123 dirent->namelen + 1;
1128 /* Try and align the struct for the next entry
1129 on a valid pointer boundary by appending zeros */
1130 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1134 currentEntry->dirlen++;
1137 dir->dir_next = dirlist = dirlist -> next;
1150 * Routine to create a directory ...
1154 SMBC_mkdir_ctx(SMBCCTX *context,
1158 SMBCSRV *srv = NULL;
1159 char *server = NULL;
1162 char *password = NULL;
1163 char *workgroup = NULL;
1165 char *targetpath = NULL;
1167 struct cli_state *targetcli = NULL;
1168 TALLOC_CTX *frame = talloc_stackframe();
1171 if (!context || !context->internal->initialized) {
1183 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1185 if (SMBC_parse_path(frame,
1201 if (!user || user[0] == (char)0) {
1202 user = talloc_strdup(frame, smbc_getUser(context));
1210 srv = SMBC_server(frame, context, True,
1211 server, port, share, &workgroup, &user, &password);
1216 return -1; /* errno set by SMBC_server */
1220 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1221 status = cli_resolve_path(frame, "", context->internal->auth_info,
1222 srv->cli, path, &targetcli, &targetpath);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 d_printf("Could not resolve %s\n", path);
1229 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1231 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1232 errno = SMBC_errno(context, targetcli);
1244 * Our list function simply checks to see if a directory is not empty
1248 rmdir_list_fn(const char *mnt,
1249 struct file_info *finfo,
1253 if (strncmp(finfo->name, ".", 1) != 0 &&
1254 strncmp(finfo->name, "..", 2) != 0) {
1255 bool *smbc_rmdir_dirempty = (bool *)state;
1256 *smbc_rmdir_dirempty = false;
1258 return NT_STATUS_OK;
1262 * Routine to remove a directory
1266 SMBC_rmdir_ctx(SMBCCTX *context,
1269 SMBCSRV *srv = NULL;
1270 char *server = NULL;
1273 char *password = NULL;
1274 char *workgroup = NULL;
1276 char *targetpath = NULL;
1278 struct cli_state *targetcli = NULL;
1279 TALLOC_CTX *frame = talloc_stackframe();
1282 if (!context || !context->internal->initialized) {
1294 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1296 if (SMBC_parse_path(frame,
1312 if (!user || user[0] == (char)0) {
1313 user = talloc_strdup(frame, smbc_getUser(context));
1321 srv = SMBC_server(frame, context, True,
1322 server, port, share, &workgroup, &user, &password);
1327 return -1; /* errno set by SMBC_server */
1331 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1332 status = cli_resolve_path(frame, "", context->internal->auth_info,
1333 srv->cli, path, &targetcli, &targetpath);
1334 if (!NT_STATUS_IS_OK(status)) {
1335 d_printf("Could not resolve %s\n", path);
1340 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1342 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1344 errno = SMBC_errno(context, targetcli);
1346 if (errno == EACCES) { /* Check if the dir empty or not */
1348 /* Local storage to avoid buffer overflows */
1350 bool smbc_rmdir_dirempty = true;
1352 lpath = talloc_asprintf(frame, "%s\\*",
1360 status = cli_list(targetcli, lpath,
1361 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1363 &smbc_rmdir_dirempty);
1365 if (!NT_STATUS_IS_OK(status)) {
1366 /* Fix errno to ignore latest error ... */
1367 DEBUG(5, ("smbc_rmdir: "
1368 "cli_list returned an error: %d\n",
1369 SMBC_errno(context, targetcli)));
1374 if (smbc_rmdir_dirempty)
1392 * Routine to return the current directory position
1396 SMBC_telldir_ctx(SMBCCTX *context,
1399 TALLOC_CTX *frame = talloc_stackframe();
1401 if (!context || !context->internal->initialized) {
1409 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1417 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1425 /* See if we're already at the end. */
1426 if (dir->dir_next == NULL) {
1433 * We return the pointer here as the offset
1436 return (off_t)(long)dir->dir_next->dirent;
1440 * A routine to run down the list and see if the entry is OK
1443 static struct smbc_dir_list *
1444 check_dir_ent(struct smbc_dir_list *list,
1445 struct smbc_dirent *dirent)
1448 /* Run down the list looking for what we want */
1452 struct smbc_dir_list *tmp = list;
1456 if (tmp->dirent == dirent)
1465 return NULL; /* Not found, or an error */
1471 * Routine to seek on a directory
1475 SMBC_lseekdir_ctx(SMBCCTX *context,
1479 long int l_offset = offset; /* Handle problems of size */
1480 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1481 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1482 TALLOC_CTX *frame = talloc_stackframe();
1484 if (!context || !context->internal->initialized) {
1492 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1500 /* Now, check what we were passed and see if it is OK ... */
1502 if (dirent == NULL) { /* Seek to the begining of the list */
1504 dir->dir_next = dir->dir_list;
1510 if (offset == -1) { /* Seek to the end of the list */
1511 dir->dir_next = NULL;
1516 /* Now, run down the list and make sure that the entry is OK */
1517 /* This may need to be changed if we change the format of the list */
1519 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1520 errno = EINVAL; /* Bad entry */
1525 dir->dir_next = list_ent;
1532 * Routine to fstat a dir
1536 SMBC_fstatdir_ctx(SMBCCTX *context,
1541 if (!context || !context->internal->initialized) {
1547 /* No code yet ... */
1552 SMBC_chmod_ctx(SMBCCTX *context,
1556 SMBCSRV *srv = NULL;
1557 char *server = NULL;
1560 char *password = NULL;
1561 char *workgroup = NULL;
1562 char *targetpath = NULL;
1563 struct cli_state *targetcli = NULL;
1567 TALLOC_CTX *frame = talloc_stackframe();
1570 if (!context || !context->internal->initialized) {
1572 errno = EINVAL; /* Best I can think of ... */
1583 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1585 if (SMBC_parse_path(frame,
1601 if (!user || user[0] == (char)0) {
1602 user = talloc_strdup(frame, smbc_getUser(context));
1610 srv = SMBC_server(frame, context, True,
1611 server, port, share, &workgroup, &user, &password);
1615 return -1; /* errno set by SMBC_server */
1618 /*d_printf(">>>unlink: resolving %s\n", path);*/
1619 status = cli_resolve_path(frame, "", context->internal->auth_info,
1620 srv->cli, path, &targetcli, &targetpath);
1621 if (!NT_STATUS_IS_OK(status)) {
1622 d_printf("Could not resolve %s\n", path);
1630 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1631 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
1632 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1633 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1635 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1636 errno = SMBC_errno(context, targetcli);
1646 SMBC_utimes_ctx(SMBCCTX *context,
1648 struct timeval *tbuf)
1650 SMBCSRV *srv = NULL;
1651 char *server = NULL;
1654 char *password = NULL;
1655 char *workgroup = NULL;
1660 TALLOC_CTX *frame = talloc_stackframe();
1662 if (!context || !context->internal->initialized) {
1664 errno = EINVAL; /* Best I can think of ... */
1676 access_time = write_time = time(NULL);
1678 access_time = tbuf[0].tv_sec;
1679 write_time = tbuf[1].tv_sec;
1687 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1688 atimebuf[sizeof(atimebuf) - 1] = '\0';
1689 if ((p = strchr(atimebuf, '\n')) != NULL) {
1693 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1694 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1695 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1699 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1700 fname, atimebuf, mtimebuf);
1703 if (SMBC_parse_path(frame,
1719 if (!user || user[0] == (char)0) {
1720 user = talloc_strdup(frame, smbc_getUser(context));
1728 srv = SMBC_server(frame, context, True,
1729 server, port, share, &workgroup, &user, &password);
1733 return -1; /* errno set by SMBC_server */
1736 if (!SMBC_setatr(context, srv, path,
1737 0, access_time, write_time, 0, 0)) {
1739 return -1; /* errno set by SMBC_setatr */
1747 * Routine to unlink() a file
1751 SMBC_unlink_ctx(SMBCCTX *context,
1754 char *server = NULL;
1757 char *password = NULL;
1758 char *workgroup = NULL;
1760 char *targetpath = NULL;
1762 struct cli_state *targetcli = NULL;
1763 SMBCSRV *srv = NULL;
1764 TALLOC_CTX *frame = talloc_stackframe();
1767 if (!context || !context->internal->initialized) {
1769 errno = EINVAL; /* Best I can think of ... */
1782 if (SMBC_parse_path(frame,
1798 if (!user || user[0] == (char)0) {
1799 user = talloc_strdup(frame, smbc_getUser(context));
1807 srv = SMBC_server(frame, context, True,
1808 server, port, share, &workgroup, &user, &password);
1812 return -1; /* SMBC_server sets errno */
1816 /*d_printf(">>>unlink: resolving %s\n", path);*/
1817 status = cli_resolve_path(frame, "", context->internal->auth_info,
1818 srv->cli, path, &targetcli, &targetpath);
1819 if (!NT_STATUS_IS_OK(status)) {
1820 d_printf("Could not resolve %s\n", path);
1825 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1827 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1829 errno = SMBC_errno(context, targetcli);
1831 if (errno == EACCES) { /* Check if the file is a directory */
1836 struct timespec write_time_ts;
1837 struct timespec access_time_ts;
1838 struct timespec change_time_ts;
1841 if (!SMBC_getatr(context, srv, path, &mode, &size,
1848 /* Hmmm, bad error ... What? */
1850 errno = SMBC_errno(context, targetcli);
1857 if (IS_DOS_DIR(mode))
1860 errno = saverr; /* Restore this */
1871 return 0; /* Success ... */
1876 * Routine to rename() a file
1880 SMBC_rename_ctx(SMBCCTX *ocontext,
1885 char *server1 = NULL;
1886 char *share1 = NULL;
1887 char *server2 = NULL;
1888 char *share2 = NULL;
1891 char *password1 = NULL;
1892 char *password2 = NULL;
1893 char *workgroup = NULL;
1896 char *targetpath1 = NULL;
1897 char *targetpath2 = NULL;
1898 struct cli_state *targetcli1 = NULL;
1899 struct cli_state *targetcli2 = NULL;
1900 SMBCSRV *srv = NULL;
1903 TALLOC_CTX *frame = talloc_stackframe();
1906 if (!ocontext || !ncontext ||
1907 !ocontext->internal->initialized ||
1908 !ncontext->internal->initialized) {
1910 errno = EINVAL; /* Best I can think of ... */
1915 if (!oname || !nname) {
1921 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1923 if (SMBC_parse_path(frame,
1939 if (!user1 || user1[0] == (char)0) {
1940 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1948 if (SMBC_parse_path(frame,
1964 if (!user2 || user2[0] == (char)0) {
1965 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1973 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1974 strcmp(user1, user2)) {
1975 /* Can't rename across file systems, or users?? */
1981 srv = SMBC_server(frame, ocontext, True,
1982 server1, port1, share1, &workgroup, &user1, &password1);
1989 /* set the credentials to make DFS work */
1990 smbc_set_credentials_with_fallback(ocontext,
1995 /*d_printf(">>>rename: resolving %s\n", path1);*/
1996 status = cli_resolve_path(frame, "", ocontext->internal->auth_info,
1997 srv->cli, path1, &targetcli1, &targetpath1);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 d_printf("Could not resolve %s\n", path1);
2005 /* set the credentials to make DFS work */
2006 smbc_set_credentials_with_fallback(ncontext,
2011 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2012 /*d_printf(">>>rename: resolving %s\n", path2);*/
2013 status = cli_resolve_path(frame, "", ncontext->internal->auth_info,
2014 srv->cli, path2, &targetcli2, &targetpath2);
2015 if (!NT_STATUS_IS_OK(status)) {
2016 d_printf("Could not resolve %s\n", path2);
2021 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2023 if (strcmp(smbXcli_conn_remote_name(targetcli1->conn), smbXcli_conn_remote_name(targetcli2->conn)) ||
2024 strcmp(targetcli1->share, targetcli2->share))
2026 /* can't rename across file systems */
2032 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2033 int eno = SMBC_errno(ocontext, targetcli1);
2035 if (eno != EEXIST ||
2036 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2037 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2047 return 0; /* Success */