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 "popt_common.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "rpc_client/cli_pipe.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "libsmb/nmblib.h"
34 * Routine to open a directory
35 * We accept the URL syntax explained in SMBC_parse_path(), above.
39 remove_dir(SMBCFILE *dir)
41 struct smbc_dir_list *d,*f;
53 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
58 add_dirent(SMBCFILE *dir,
63 struct smbc_dirent *dirent;
65 int name_length = (name == NULL ? 0 : strlen(name));
66 int comment_len = (comment == NULL ? 0 : strlen(comment));
69 * Allocate space for the dirent, which must be increased by the
70 * size of the name and the comment and 1 each for the null terminator.
73 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
75 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
79 dir->dir_error = ENOMEM;
86 if (dir->dir_list == NULL) {
88 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
92 dir->dir_error = ENOMEM;
96 ZERO_STRUCTP(dir->dir_list);
98 dir->dir_end = dir->dir_next = dir->dir_list;
102 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
104 if (!dir->dir_end->next) {
107 dir->dir_error = ENOMEM;
111 ZERO_STRUCTP(dir->dir_end->next);
113 dir->dir_end = dir->dir_end->next;
116 dir->dir_end->next = NULL;
117 dir->dir_end->dirent = dirent;
119 dirent->smbc_type = type;
120 dirent->namelen = name_length;
121 dirent->commentlen = comment_len;
122 dirent->dirlen = size;
125 * dirent->namelen + 1 includes the null (no null termination needed)
126 * Ditto for dirent->commentlen.
127 * The space for the two null bytes was allocated.
129 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
130 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
131 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
138 list_unique_wg_fn(const char *name,
143 SMBCFILE *dir = (SMBCFILE *)state;
144 struct smbc_dir_list *dir_list;
145 struct smbc_dirent *dirent;
149 dirent_type = dir->dir_type;
151 if (add_dirent(dir, name, comment, dirent_type) < 0) {
152 /* An error occurred, what do we do? */
153 /* FIXME: Add some code here */
154 /* Change cli_NetServerEnum to take a fn
155 returning NTSTATUS... JRA. */
158 /* Point to the one just added */
159 dirent = dir->dir_end->dirent;
161 /* See if this was a duplicate */
162 for (dir_list = dir->dir_list;
163 dir_list != dir->dir_end;
164 dir_list = dir_list->next) {
166 strcmp(dir_list->dirent->name, dirent->name) == 0) {
167 /* Duplicate. End end of list need to be removed. */
171 if (do_remove && dir_list->next == dir->dir_end) {
172 /* Found the end of the list. Remove it. */
173 dir->dir_end = dir_list;
174 free(dir_list->next);
176 dir_list->next = NULL;
183 list_fn(const char *name,
188 SMBCFILE *dir = (SMBCFILE *)state;
192 * We need to process the type a little ...
194 * Disk share = 0x00000000
195 * Print share = 0x00000001
196 * Comms share = 0x00000002 (obsolete?)
197 * IPC$ share = 0x00000003
199 * administrative shares:
200 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
203 if (dir->dir_type == SMBC_FILE_SHARE) {
207 dirent_type = SMBC_FILE_SHARE;
211 dirent_type = SMBC_PRINTER_SHARE;
215 dirent_type = SMBC_COMMS_SHARE;
220 dirent_type = SMBC_IPC_SHARE;
224 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
229 dirent_type = dir->dir_type;
232 if (add_dirent(dir, name, comment, dirent_type) < 0) {
233 /* An error occurred, what do we do? */
234 /* FIXME: Add some code here */
235 /* Change cli_NetServerEnum to take a fn
236 returning NTSTATUS... JRA. */
241 dir_list_fn(const char *mnt,
242 struct file_info *finfo,
247 if (add_dirent((SMBCFILE *)state, finfo->name, "",
248 (finfo->mode&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
249 SMBCFILE *dir = (SMBCFILE *)state;
250 return map_nt_error_from_unix(dir->dir_error);
256 net_share_enum_rpc(struct cli_state *cli,
257 void (*fn)(const char *name,
265 uint32 preferred_len = 0xffffffff;
267 struct srvsvc_NetShareInfoCtr info_ctr;
268 struct srvsvc_NetShareCtr1 ctr1;
270 fstring comment = "";
271 struct rpc_pipe_client *pipe_hnd = NULL;
273 uint32_t resume_handle = 0;
274 uint32_t total_entries = 0;
275 struct dcerpc_binding_handle *b;
277 /* Open the server service pipe */
278 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
280 if (!NT_STATUS_IS_OK(nt_status)) {
281 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
285 ZERO_STRUCT(info_ctr);
289 info_ctr.ctr.ctr1 = &ctr1;
291 b = pipe_hnd->binding_handle;
293 /* Issue the NetShareEnum RPC call and retrieve the response */
294 nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
302 /* Was it successful? */
303 if (!NT_STATUS_IS_OK(nt_status)) {
304 /* Nope. Go clean up. */
305 result = ntstatus_to_werror(nt_status);
309 if (!W_ERROR_IS_OK(result)) {
310 /* Nope. Go clean up. */
314 if (total_entries == 0) {
315 /* Nope. Go clean up. */
316 result = WERR_GENERAL_FAILURE;
320 /* For each returned entry... */
321 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
323 /* pull out the share name */
324 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
326 /* pull out the share's comment */
327 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
329 /* Get the type value */
330 type = info_ctr.ctr.ctr1->array[i].type;
332 /* Add this share to the list */
333 (*fn)(name, type, comment, state);
337 /* Close the server service pipe */
338 TALLOC_FREE(pipe_hnd);
340 /* Tell 'em if it worked */
341 return W_ERROR_IS_OK(result) ? 0 : -1;
346 * Verify that the options specified in a URL are valid
349 SMBC_check_options(char *server,
354 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
355 "path='%s' options='%s'\n",
356 server, share, path, options));
358 /* No options at all is always ok */
359 if (! *options) return 0;
361 /* Currently, we don't support any options. */
367 SMBC_opendir_ctx(SMBCCTX *context,
374 char *password = NULL;
375 char *options = NULL;
376 char *workgroup = NULL;
381 SMBCFILE *dir = NULL;
382 struct sockaddr_storage rem_ss;
383 TALLOC_CTX *frame = talloc_stackframe();
385 if (!context || !context->internal->initialized) {
386 DEBUG(4, ("no valid context\n"));
388 errno = EINVAL + 8192;
394 DEBUG(4, ("no valid fname\n"));
396 errno = EINVAL + 8193;
400 if (SMBC_parse_path(frame,
410 DEBUG(4, ("no valid path\n"));
412 errno = EINVAL + 8194;
416 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
417 "path='%s' options='%s'\n",
418 fname, server, share, path, options));
420 /* Ensure the options are valid */
421 if (SMBC_check_options(server, share, path, options)) {
422 DEBUG(4, ("unacceptable options (%s)\n", options));
424 errno = EINVAL + 8195;
428 if (!user || user[0] == (char)0) {
429 user = talloc_strdup(frame, smbc_getUser(context));
437 dir = SMB_MALLOC_P(SMBCFILE);
448 dir->fname = SMB_STRDUP(fname);
452 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
454 if (server[0] == (char)0) {
459 struct ip_service *ip_list;
460 struct ip_service server_addr;
461 struct user_auth_info u_info;
463 if (share[0] != (char)0 || path[0] != (char)0) {
466 SAFE_FREE(dir->fname);
470 errno = EINVAL + 8196;
474 /* Determine how many local master browsers to query */
475 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
477 : smbc_getOptionBrowseMaxLmbCount(context));
479 memset(&u_info, '\0', sizeof(u_info));
480 u_info.username = talloc_strdup(frame,user);
481 u_info.password = talloc_strdup(frame,password);
482 if (!u_info.username || !u_info.password) {
484 SAFE_FREE(dir->fname);
492 * We have server and share and path empty but options
493 * requesting that we scan all master browsers for their list
494 * of workgroups/domains. This implies that we must first try
495 * broadcast queries to find all master browsers, and if that
496 * doesn't work, then try our other methods which return only
497 * a single master browser.
501 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
507 if (!find_master_ip(workgroup, &server_addr.ss)) {
510 SAFE_FREE(dir->fname);
518 ip_list = (struct ip_service *)memdup(
519 &server_addr, sizeof(server_addr));
520 if (ip_list == NULL) {
522 SAFE_FREE(dir->fname);
532 for (i = 0; i < count && i < max_lmb_count; i++) {
533 char addr[INET6_ADDRSTRLEN];
535 struct cli_state *cli = NULL;
537 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
538 DEBUG(99, ("Found master browser %d of %d: %s\n",
539 i+1, MAX(count, max_lmb_count),
542 cli = get_ipc_connect_master_ip(talloc_tos(),
546 /* cli == NULL is the master browser refused to talk or
547 could not be found */
552 workgroup = talloc_strdup(frame, wg_ptr);
553 server = talloc_strdup(frame, cli->desthost);
557 if (!workgroup || !server) {
559 SAFE_FREE(dir->fname);
567 DEBUG(4, ("using workgroup %s %s\n",
571 * For each returned master browser IP address, get a
572 * connection to IPC$ on the server if we do not
573 * already have one, and determine the
574 * workgroups/domains that it knows about.
577 srv = SMBC_server(frame, context, True, server, "IPC$",
578 &workgroup, &user, &password);
584 dir->dir_type = SMBC_WORKGROUP;
586 /* Now, list the stuff ... */
588 if (!cli_NetServerEnum(srv->cli,
600 * Server not an empty string ... Check the rest and see what
603 if (*share == '\0') {
606 /* Should not have empty share with path */
608 SAFE_FREE(dir->fname);
612 errno = EINVAL + 8197;
618 * We don't know if <server> is really a server name
619 * or is a workgroup/domain name. If we already have
620 * a server structure for it, we'll use it.
621 * Otherwise, check to see if <server><1D>,
622 * <server><1B>, or <server><20> translates. We check
623 * to see if <server> is an IP address first.
627 * See if we have an existing server. Do not
628 * establish a connection if one does not already
631 srv = SMBC_server(frame, context, False,
633 &workgroup, &user, &password);
636 * If no existing server and not an IP addr, look for
640 !is_ipaddress(server) &&
641 (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
642 resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
644 * "server" is actually a workgroup name,
645 * not a server. Make this clear.
647 char *wgroup = server;
650 dir->dir_type = SMBC_SERVER;
653 * Get the backup list ...
655 if (!name_status_find(wgroup, 0, 0,
656 &rem_ss, buserver)) {
657 char addr[INET6_ADDRSTRLEN];
659 print_sockaddr(addr, sizeof(addr), &rem_ss);
660 DEBUG(0,("Could not get name of "
661 "local/domain master browser "
662 "for workgroup %s from "
667 SAFE_FREE(dir->fname);
677 * Get a connection to IPC$ on the server if
678 * we do not already have one
680 srv = SMBC_server(frame, context, True,
685 DEBUG(0, ("got no contact to IPC$\n"));
687 SAFE_FREE(dir->fname);
697 /* Now, list the servers ... */
698 if (!cli_NetServerEnum(srv->cli, wgroup,
703 SAFE_FREE(dir->fname);
710 (resolve_name(server, &rem_ss, 0x20, false))) {
713 * If we hadn't found the server, get one now
716 srv = SMBC_server(frame, context, True,
724 SAFE_FREE(dir->fname);
732 dir->dir_type = SMBC_FILE_SHARE;
735 /* List the shares ... */
737 if (net_share_enum_rpc(
746 errno = cli_errno(srv->cli);
748 SAFE_FREE(dir->fname);
756 /* Neither the workgroup nor server exists */
757 errno = ECONNREFUSED;
759 SAFE_FREE(dir->fname);
769 * The server and share are specified ... work from
773 struct cli_state *targetcli;
776 /* We connect to the server and list the directory */
777 dir->dir_type = SMBC_FILE_SHARE;
779 srv = SMBC_server(frame, context, True, server, share,
780 &workgroup, &user, &password);
784 SAFE_FREE(dir->fname);
793 /* Now, list the files ... */
795 p = path + strlen(path);
796 path = talloc_asprintf_append(path, "\\*");
799 SAFE_FREE(dir->fname);
806 if (!cli_resolve_path(frame, "", context->internal->auth_info,
808 &targetcli, &targetpath)) {
809 d_printf("Could not resolve %s\n", path);
811 SAFE_FREE(dir->fname);
818 status = cli_list(targetcli, targetpath,
819 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
820 dir_list_fn, (void *)dir);
821 if (!NT_STATUS_IS_OK(status)) {
823 SAFE_FREE(dir->fname);
826 saved_errno = SMBC_errno(context, targetcli);
828 if (saved_errno == EINVAL) {
830 * See if they asked to opendir
831 * something other than a directory.
832 * If so, the converted error value we
833 * got would have been EINVAL rather
836 *p = '\0'; /* restore original path */
838 if (SMBC_getatr(context, srv, path,
840 NULL, NULL, NULL, NULL,
842 ! IS_DOS_DIR(mode)) {
844 /* It is. Correct the error value */
845 saved_errno = ENOTDIR;
850 * If there was an error and the server is no
853 if (cli_is_error(targetcli) &&
854 smbc_getFunctionCheckServer(context)(context, srv)) {
856 /* ... then remove it. */
857 if (smbc_getFunctionRemoveUnusedServer(context)(context,
860 * We could not remove the
861 * server completely, remove
862 * it from the cache so we
863 * will not get it again. It
864 * will be removed when the
865 * last file/dir is closed.
867 smbc_getFunctionRemoveCachedServer(context)(context, srv);
879 DLIST_ADD(context->internal->files, dir);
886 * Routine to close a directory
890 SMBC_closedir_ctx(SMBCCTX *context,
893 TALLOC_CTX *frame = talloc_stackframe();
895 if (!context || !context->internal->initialized) {
901 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
907 remove_dir(dir); /* Clean it up */
909 DLIST_REMOVE(context->internal->files, dir);
913 SAFE_FREE(dir->fname);
914 SAFE_FREE(dir); /* Free the space too */
923 smbc_readdir_internal(SMBCCTX * context,
924 struct smbc_dirent *dest,
925 struct smbc_dirent *src,
928 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
930 /* url-encode the name. get back remaining buffer space */
932 smbc_urlencode(dest->name, src->name, max_namebuf_len);
934 /* We now know the name length */
935 dest->namelen = strlen(dest->name);
937 /* Save the pointer to the beginning of the comment */
938 dest->comment = dest->name + dest->namelen + 1;
940 /* Copy the comment */
941 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
942 dest->comment[max_namebuf_len - 1] = '\0';
944 /* Save other fields */
945 dest->smbc_type = src->smbc_type;
946 dest->commentlen = strlen(dest->comment);
947 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
951 /* No encoding. Just copy the entry as is. */
952 memcpy(dest, src, src->dirlen);
953 dest->comment = (char *)(&dest->name + src->namelen + 1);
959 * Routine to get a directory entry
963 SMBC_readdir_ctx(SMBCCTX *context,
967 struct smbc_dirent *dirp, *dirent;
968 TALLOC_CTX *frame = talloc_stackframe();
970 /* Check that all is ok first ... */
972 if (!context || !context->internal->initialized) {
975 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
981 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
984 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
990 if (dir->file != False) { /* FIXME, should be dir, perhaps */
993 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
999 if (!dir->dir_next) {
1004 dirent = dir->dir_next->dirent;
1013 dirp = &context->internal->dirent;
1014 maxlen = sizeof(context->internal->_dirent_name);
1016 smbc_readdir_internal(context, dirp, dirent, maxlen);
1018 dir->dir_next = dir->dir_next->next;
1025 * Routine to get directory entries
1029 SMBC_getdents_ctx(SMBCCTX *context,
1031 struct smbc_dirent *dirp,
1037 char *ndir = (char *)dirp;
1038 struct smbc_dir_list *dirlist;
1039 TALLOC_CTX *frame = talloc_stackframe();
1041 /* Check that all is ok first ... */
1043 if (!context || !context->internal->initialized) {
1051 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1059 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1068 * Now, retrieve the number of entries that will fit in what was passed
1069 * We have to figure out if the info is in the list, or we need to
1070 * send a request to the server to get the info.
1073 while ((dirlist = dir->dir_next)) {
1074 struct smbc_dirent *dirent;
1075 struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
1077 if (!dirlist->dirent) {
1079 errno = ENOENT; /* Bad error */
1085 /* Do urlencoding of next entry, if so selected */
1086 dirent = &context->internal->dirent;
1087 maxlen = sizeof(context->internal->_dirent_name);
1088 smbc_readdir_internal(context, dirent,
1089 dirlist->dirent, maxlen);
1091 reqd = dirent->dirlen;
1095 if (rem < count) { /* We managed to copy something */
1102 else { /* Nothing copied ... */
1104 errno = EINVAL; /* Not enough space ... */
1112 memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
1114 currentEntry->comment = ¤tEntry->name[0] +
1115 dirent->namelen + 1;
1120 /* Try and align the struct for the next entry
1121 on a valid pointer boundary by appending zeros */
1122 while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
1126 currentEntry->dirlen++;
1129 dir->dir_next = dirlist = dirlist -> next;
1142 * Routine to create a directory ...
1146 SMBC_mkdir_ctx(SMBCCTX *context,
1150 SMBCSRV *srv = NULL;
1151 char *server = NULL;
1154 char *password = NULL;
1155 char *workgroup = NULL;
1157 char *targetpath = NULL;
1158 struct cli_state *targetcli = NULL;
1159 TALLOC_CTX *frame = talloc_stackframe();
1161 if (!context || !context->internal->initialized) {
1173 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1175 if (SMBC_parse_path(frame,
1190 if (!user || user[0] == (char)0) {
1191 user = talloc_strdup(frame, smbc_getUser(context));
1199 srv = SMBC_server(frame, context, True,
1200 server, share, &workgroup, &user, &password);
1205 return -1; /* errno set by SMBC_server */
1209 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1210 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1212 &targetcli, &targetpath)) {
1213 d_printf("Could not resolve %s\n", path);
1218 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1220 if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1221 errno = SMBC_errno(context, targetcli);
1233 * Our list function simply checks to see if a directory is not empty
1237 rmdir_list_fn(const char *mnt,
1238 struct file_info *finfo,
1242 if (strncmp(finfo->name, ".", 1) != 0 &&
1243 strncmp(finfo->name, "..", 2) != 0) {
1244 bool *smbc_rmdir_dirempty = (bool *)state;
1245 *smbc_rmdir_dirempty = false;
1247 return NT_STATUS_OK;
1251 * Routine to remove a directory
1255 SMBC_rmdir_ctx(SMBCCTX *context,
1258 SMBCSRV *srv = NULL;
1259 char *server = NULL;
1262 char *password = NULL;
1263 char *workgroup = NULL;
1265 char *targetpath = NULL;
1266 struct cli_state *targetcli = NULL;
1267 TALLOC_CTX *frame = talloc_stackframe();
1269 if (!context || !context->internal->initialized) {
1281 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1283 if (SMBC_parse_path(frame,
1298 if (!user || user[0] == (char)0) {
1299 user = talloc_strdup(frame, smbc_getUser(context));
1307 srv = SMBC_server(frame, context, True,
1308 server, share, &workgroup, &user, &password);
1313 return -1; /* errno set by SMBC_server */
1317 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1318 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1320 &targetcli, &targetpath)) {
1321 d_printf("Could not resolve %s\n", path);
1326 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1328 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1330 errno = SMBC_errno(context, targetcli);
1332 if (errno == EACCES) { /* Check if the dir empty or not */
1334 /* Local storage to avoid buffer overflows */
1336 bool smbc_rmdir_dirempty = true;
1339 lpath = talloc_asprintf(frame, "%s\\*",
1347 status = cli_list(targetcli, lpath,
1348 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
1350 &smbc_rmdir_dirempty);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 /* Fix errno to ignore latest error ... */
1354 DEBUG(5, ("smbc_rmdir: "
1355 "cli_list returned an error: %d\n",
1356 SMBC_errno(context, targetcli)));
1361 if (smbc_rmdir_dirempty)
1379 * Routine to return the current directory position
1383 SMBC_telldir_ctx(SMBCCTX *context,
1386 TALLOC_CTX *frame = talloc_stackframe();
1388 if (!context || !context->internal->initialized) {
1396 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1404 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1412 /* See if we're already at the end. */
1413 if (dir->dir_next == NULL) {
1420 * We return the pointer here as the offset
1423 return (off_t)(long)dir->dir_next->dirent;
1427 * A routine to run down the list and see if the entry is OK
1430 static struct smbc_dir_list *
1431 check_dir_ent(struct smbc_dir_list *list,
1432 struct smbc_dirent *dirent)
1435 /* Run down the list looking for what we want */
1439 struct smbc_dir_list *tmp = list;
1443 if (tmp->dirent == dirent)
1452 return NULL; /* Not found, or an error */
1458 * Routine to seek on a directory
1462 SMBC_lseekdir_ctx(SMBCCTX *context,
1466 long int l_offset = offset; /* Handle problems of size */
1467 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1468 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1469 TALLOC_CTX *frame = talloc_stackframe();
1471 if (!context || !context->internal->initialized) {
1479 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1487 /* Now, check what we were passed and see if it is OK ... */
1489 if (dirent == NULL) { /* Seek to the begining of the list */
1491 dir->dir_next = dir->dir_list;
1497 if (offset == -1) { /* Seek to the end of the list */
1498 dir->dir_next = NULL;
1503 /* Now, run down the list and make sure that the entry is OK */
1504 /* This may need to be changed if we change the format of the list */
1506 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1507 errno = EINVAL; /* Bad entry */
1512 dir->dir_next = list_ent;
1519 * Routine to fstat a dir
1523 SMBC_fstatdir_ctx(SMBCCTX *context,
1528 if (!context || !context->internal->initialized) {
1534 /* No code yet ... */
1539 SMBC_chmod_ctx(SMBCCTX *context,
1543 SMBCSRV *srv = NULL;
1544 char *server = NULL;
1547 char *password = NULL;
1548 char *workgroup = NULL;
1549 char *targetpath = NULL;
1550 struct cli_state *targetcli = NULL;
1553 TALLOC_CTX *frame = talloc_stackframe();
1555 if (!context || !context->internal->initialized) {
1557 errno = EINVAL; /* Best I can think of ... */
1568 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1570 if (SMBC_parse_path(frame,
1585 if (!user || user[0] == (char)0) {
1586 user = talloc_strdup(frame, smbc_getUser(context));
1594 srv = SMBC_server(frame, context, True,
1595 server, share, &workgroup, &user, &password);
1599 return -1; /* errno set by SMBC_server */
1602 /*d_printf(">>>unlink: resolving %s\n", path);*/
1603 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1605 &targetcli, &targetpath)) {
1606 d_printf("Could not resolve %s\n", path);
1614 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
1615 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1616 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
1617 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
1619 if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
1620 errno = SMBC_errno(context, targetcli);
1630 SMBC_utimes_ctx(SMBCCTX *context,
1632 struct timeval *tbuf)
1634 SMBCSRV *srv = NULL;
1635 char *server = NULL;
1638 char *password = NULL;
1639 char *workgroup = NULL;
1643 TALLOC_CTX *frame = talloc_stackframe();
1645 if (!context || !context->internal->initialized) {
1647 errno = EINVAL; /* Best I can think of ... */
1659 access_time = write_time = time(NULL);
1661 access_time = tbuf[0].tv_sec;
1662 write_time = tbuf[1].tv_sec;
1670 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1671 atimebuf[sizeof(atimebuf) - 1] = '\0';
1672 if ((p = strchr(atimebuf, '\n')) != NULL) {
1676 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1677 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1678 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1682 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1683 fname, atimebuf, mtimebuf);
1686 if (SMBC_parse_path(frame,
1701 if (!user || user[0] == (char)0) {
1702 user = talloc_strdup(frame, smbc_getUser(context));
1710 srv = SMBC_server(frame, context, True,
1711 server, share, &workgroup, &user, &password);
1715 return -1; /* errno set by SMBC_server */
1718 if (!SMBC_setatr(context, srv, path,
1719 0, access_time, write_time, 0, 0)) {
1721 return -1; /* errno set by SMBC_setatr */
1729 * Routine to unlink() a file
1733 SMBC_unlink_ctx(SMBCCTX *context,
1736 char *server = NULL;
1739 char *password = NULL;
1740 char *workgroup = NULL;
1742 char *targetpath = NULL;
1743 struct cli_state *targetcli = NULL;
1744 SMBCSRV *srv = NULL;
1745 TALLOC_CTX *frame = talloc_stackframe();
1747 if (!context || !context->internal->initialized) {
1749 errno = EINVAL; /* Best I can think of ... */
1762 if (SMBC_parse_path(frame,
1777 if (!user || user[0] == (char)0) {
1778 user = talloc_strdup(frame, smbc_getUser(context));
1786 srv = SMBC_server(frame, context, True,
1787 server, share, &workgroup, &user, &password);
1791 return -1; /* SMBC_server sets errno */
1795 /*d_printf(">>>unlink: resolving %s\n", path);*/
1796 if (!cli_resolve_path(frame, "", context->internal->auth_info,
1798 &targetcli, &targetpath)) {
1799 d_printf("Could not resolve %s\n", path);
1804 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1806 if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1808 errno = SMBC_errno(context, targetcli);
1810 if (errno == EACCES) { /* Check if the file is a directory */
1815 struct timespec write_time_ts;
1816 struct timespec access_time_ts;
1817 struct timespec change_time_ts;
1820 if (!SMBC_getatr(context, srv, path, &mode, &size,
1827 /* Hmmm, bad error ... What? */
1829 errno = SMBC_errno(context, targetcli);
1836 if (IS_DOS_DIR(mode))
1839 errno = saverr; /* Restore this */
1850 return 0; /* Success ... */
1855 * Routine to rename() a file
1859 SMBC_rename_ctx(SMBCCTX *ocontext,
1864 char *server1 = NULL;
1865 char *share1 = NULL;
1866 char *server2 = NULL;
1867 char *share2 = NULL;
1870 char *password1 = NULL;
1871 char *password2 = NULL;
1872 char *workgroup = NULL;
1875 char *targetpath1 = NULL;
1876 char *targetpath2 = NULL;
1877 struct cli_state *targetcli1 = NULL;
1878 struct cli_state *targetcli2 = NULL;
1879 SMBCSRV *srv = NULL;
1880 TALLOC_CTX *frame = talloc_stackframe();
1882 if (!ocontext || !ncontext ||
1883 !ocontext->internal->initialized ||
1884 !ncontext->internal->initialized) {
1886 errno = EINVAL; /* Best I can think of ... */
1891 if (!oname || !nname) {
1897 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1899 if (SMBC_parse_path(frame,
1914 if (!user1 || user1[0] == (char)0) {
1915 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1923 if (SMBC_parse_path(frame,
1938 if (!user2 || user2[0] == (char)0) {
1939 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1947 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1948 strcmp(user1, user2)) {
1949 /* Can't rename across file systems, or users?? */
1955 srv = SMBC_server(frame, ocontext, True,
1956 server1, share1, &workgroup, &user1, &password1);
1963 /* set the credentials to make DFS work */
1964 smbc_set_credentials_with_fallback(ocontext,
1969 /*d_printf(">>>rename: resolving %s\n", path1);*/
1970 if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1973 &targetcli1, &targetpath1)) {
1974 d_printf("Could not resolve %s\n", path1);
1980 /* set the credentials to make DFS work */
1981 smbc_set_credentials_with_fallback(ncontext,
1986 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1987 /*d_printf(">>>rename: resolving %s\n", path2);*/
1988 if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1991 &targetcli2, &targetpath2)) {
1992 d_printf("Could not resolve %s\n", path2);
1997 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1999 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2000 strcmp(targetcli1->share, targetcli2->share))
2002 /* can't rename across file systems */
2008 if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2009 int eno = SMBC_errno(ocontext, targetcli1);
2011 if (eno != EEXIST ||
2012 !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
2013 !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
2023 return 0; /* Success */