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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open a directory
32 * We accept the URL syntax explained in SMBC_parse_path(), above.
36 remove_dir(SMBCFILE *dir)
38 struct smbc_dir_list *d,*f;
50 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
55 add_dirent(SMBCFILE *dir,
60 struct smbc_dirent *dirent;
62 int name_length = (name == NULL ? 0 : strlen(name));
63 int comment_len = (comment == NULL ? 0 : strlen(comment));
66 * Allocate space for the dirent, which must be increased by the
67 * size of the name and the comment and 1 each for the null terminator.
70 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
72 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
76 dir->dir_error = ENOMEM;
83 if (dir->dir_list == NULL) {
85 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
89 dir->dir_error = ENOMEM;
93 ZERO_STRUCTP(dir->dir_list);
95 dir->dir_end = dir->dir_next = dir->dir_list;
99 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
101 if (!dir->dir_end->next) {
104 dir->dir_error = ENOMEM;
108 ZERO_STRUCTP(dir->dir_end->next);
110 dir->dir_end = dir->dir_end->next;
113 dir->dir_end->next = NULL;
114 dir->dir_end->dirent = dirent;
116 dirent->smbc_type = type;
117 dirent->namelen = name_length;
118 dirent->commentlen = comment_len;
119 dirent->dirlen = size;
122 * dirent->namelen + 1 includes the null (no null termination needed)
123 * Ditto for dirent->commentlen.
124 * The space for the two null bytes was allocated.
126 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
127 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
128 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
135 list_unique_wg_fn(const char *name,
140 SMBCFILE *dir = (SMBCFILE *)state;
141 struct smbc_dir_list *dir_list;
142 struct smbc_dirent *dirent;
146 dirent_type = dir->dir_type;
148 if (add_dirent(dir, name, comment, dirent_type) < 0) {
150 /* An error occurred, what do we do? */
151 /* FIXME: Add some code here */
154 /* Point to the one just added */
155 dirent = dir->dir_end->dirent;
157 /* See if this was a duplicate */
158 for (dir_list = dir->dir_list;
159 dir_list != dir->dir_end;
160 dir_list = dir_list->next) {
162 strcmp(dir_list->dirent->name, dirent->name) == 0) {
163 /* Duplicate. End end of list need to be removed. */
167 if (do_remove && dir_list->next == dir->dir_end) {
168 /* Found the end of the list. Remove it. */
169 dir->dir_end = dir_list;
170 free(dir_list->next);
172 dir_list->next = NULL;
179 list_fn(const char *name,
184 SMBCFILE *dir = (SMBCFILE *)state;
188 * We need to process the type a little ...
190 * Disk share = 0x00000000
191 * Print share = 0x00000001
192 * Comms share = 0x00000002 (obsolete?)
193 * IPC$ share = 0x00000003
195 * administrative shares:
196 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
199 if (dir->dir_type == SMBC_FILE_SHARE) {
203 dirent_type = SMBC_FILE_SHARE;
207 dirent_type = SMBC_PRINTER_SHARE;
211 dirent_type = SMBC_COMMS_SHARE;
216 dirent_type = SMBC_IPC_SHARE;
220 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
225 dirent_type = dir->dir_type;
228 if (add_dirent(dir, name, comment, dirent_type) < 0) {
230 /* An error occurred, what do we do? */
231 /* FIXME: Add some code here */
237 dir_list_fn(const char *mnt,
243 if (add_dirent((SMBCFILE *)state, finfo->name, "",
244 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
246 /* Handle an error ... */
248 /* FIXME: Add some code ... */
255 net_share_enum_rpc(struct cli_state *cli,
256 void (*fn)(const char *name,
265 uint32 info_level = 1;
266 uint32 preferred_len = 0xffffffff;
268 SRV_SHARE_INFO_CTR ctr;
270 fstring comment = "";
271 struct rpc_pipe_client *pipe_hnd;
274 /* Open the server service pipe */
275 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
277 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
281 /* Issue the NetShareEnum RPC call and retrieve the response */
282 init_enum_hnd(&enum_hnd, 0);
283 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
290 /* Was it successful? */
291 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
292 /* Nope. Go clean up. */
296 /* For each returned entry... */
297 for (i = 0; i < ctr.num_entries; i++) {
299 /* pull out the share name */
300 rpcstr_pull_unistr2_fstring(
301 name, &ctr.share.info1[i].info_1_str.uni_netname);
303 /* pull out the share's comment */
304 rpcstr_pull_unistr2_fstring(
305 comment, &ctr.share.info1[i].info_1_str.uni_remark);
307 /* Get the type value */
308 type = ctr.share.info1[i].info_1.type;
310 /* Add this share to the list */
311 (*fn)(name, type, comment, state);
315 /* Close the server service pipe */
316 cli_rpc_pipe_close(pipe_hnd);
318 /* Tell 'em if it worked */
319 return W_ERROR_IS_OK(result) ? 0 : -1;
324 * Verify that the options specified in a URL are valid
327 SMBC_check_options(char *server,
332 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
333 "path='%s' options='%s'\n",
334 server, share, path, options));
336 /* No options at all is always ok */
337 if (! *options) return 0;
339 /* Currently, we don't support any options. */
345 SMBC_opendir_ctx(SMBCCTX *context,
352 char *password = NULL;
353 char *options = NULL;
354 char *workgroup = NULL;
359 SMBCFILE *dir = NULL;
360 struct sockaddr_storage rem_ss;
361 TALLOC_CTX *frame = talloc_stackframe();
363 if (!context || !context->internal->initialized) {
364 DEBUG(4, ("no valid context\n"));
365 errno = EINVAL + 8192;
372 DEBUG(4, ("no valid fname\n"));
373 errno = EINVAL + 8193;
378 if (SMBC_parse_path(frame,
388 DEBUG(4, ("no valid path\n"));
389 errno = EINVAL + 8194;
394 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
395 "path='%s' options='%s'\n",
396 fname, server, share, path, options));
398 /* Ensure the options are valid */
399 if (SMBC_check_options(server, share, path, options)) {
400 DEBUG(4, ("unacceptable options (%s)\n", options));
401 errno = EINVAL + 8195;
406 if (!user || user[0] == (char)0) {
407 user = talloc_strdup(frame, context->config.user);
415 dir = SMB_MALLOC_P(SMBCFILE);
426 dir->fname = SMB_STRDUP(fname);
430 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
432 if (server[0] == (char)0) {
437 struct ip_service *ip_list;
438 struct ip_service server_addr;
439 struct user_auth_info u_info;
441 if (share[0] != (char)0 || path[0] != (char)0) {
443 errno = EINVAL + 8196;
445 SAFE_FREE(dir->fname);
452 /* Determine how many local master browsers to query */
453 max_lmb_count = (context->options.browse_max_lmb_count == 0
455 : context->options.browse_max_lmb_count);
457 memset(&u_info, '\0', sizeof(u_info));
458 u_info.username = talloc_strdup(frame,user);
459 u_info.password = talloc_strdup(frame,password);
460 if (!u_info.username || !u_info.password) {
462 SAFE_FREE(dir->fname);
470 * We have server and share and path empty but options
471 * requesting that we scan all master browsers for their list
472 * of workgroups/domains. This implies that we must first try
473 * broadcast queries to find all master browsers, and if that
474 * doesn't work, then try our other methods which return only
475 * a single master browser.
479 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
485 if (!find_master_ip(workgroup, &server_addr.ss)) {
488 SAFE_FREE(dir->fname);
496 ip_list = (struct ip_service *)memdup(
497 &server_addr, sizeof(server_addr));
498 if (ip_list == NULL) {
506 for (i = 0; i < count && i < max_lmb_count; i++) {
507 char addr[INET6_ADDRSTRLEN];
509 struct cli_state *cli = NULL;
511 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
512 DEBUG(99, ("Found master browser %d of %d: %s\n",
513 i+1, MAX(count, max_lmb_count),
516 cli = get_ipc_connect_master_ip(talloc_tos(),
520 /* cli == NULL is the master browser refused to talk or
521 could not be found */
526 workgroup = talloc_strdup(frame, wg_ptr);
527 server = talloc_strdup(frame, cli->desthost);
531 if (!workgroup || !server) {
537 DEBUG(4, ("using workgroup %s %s\n",
541 * For each returned master browser IP address, get a
542 * connection to IPC$ on the server if we do not
543 * already have one, and determine the
544 * workgroups/domains that it knows about.
547 srv = SMBC_server(frame, context, True, server, "IPC$",
548 &workgroup, &user, &password);
554 dir->dir_type = SMBC_WORKGROUP;
556 /* Now, list the stuff ... */
558 if (!cli_NetServerEnum(srv->cli,
570 * Server not an empty string ... Check the rest and see what
573 if (*share == '\0') {
576 /* Should not have empty share with path */
577 errno = EINVAL + 8197;
579 SAFE_FREE(dir->fname);
588 * We don't know if <server> is really a server name
589 * or is a workgroup/domain name. If we already have
590 * a server structure for it, we'll use it.
591 * Otherwise, check to see if <server><1D>,
592 * <server><1B>, or <server><20> translates. We check
593 * to see if <server> is an IP address first.
597 * See if we have an existing server. Do not
598 * establish a connection if one does not already
601 srv = SMBC_server(frame, context, False,
603 &workgroup, &user, &password);
606 * If no existing server and not an IP addr, look for
610 !is_ipaddress(server) &&
611 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
612 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
616 dir->dir_type = SMBC_SERVER;
619 * Get the backup list ...
621 if (!name_status_find(server, 0, 0,
622 &rem_ss, buserver)) {
624 DEBUG(0,("Could not get name of "
625 "local/domain master browser "
626 "for server %s\n", server));
628 SAFE_FREE(dir->fname);
638 * Get a connection to IPC$ on the server if
639 * we do not already have one
641 srv = SMBC_server(frame, context, True,
646 DEBUG(0, ("got no contact to IPC$\n"));
648 SAFE_FREE(dir->fname);
658 /* Now, list the servers ... */
659 if (!cli_NetServerEnum(srv->cli, server,
664 SAFE_FREE(dir->fname);
671 (resolve_name(server, &rem_ss, 0x20))) {
674 * If we hadn't found the server, get one now
677 srv = SMBC_server(frame, context, True,
685 SAFE_FREE(dir->fname);
693 dir->dir_type = SMBC_FILE_SHARE;
696 /* List the shares ... */
698 if (net_share_enum_rpc(
707 errno = cli_errno(srv->cli);
709 SAFE_FREE(dir->fname);
717 /* Neither the workgroup nor server exists */
718 errno = ECONNREFUSED;
720 SAFE_FREE(dir->fname);
730 * The server and share are specified ... work from
734 struct cli_state *targetcli;
736 /* We connect to the server and list the directory */
737 dir->dir_type = SMBC_FILE_SHARE;
739 srv = SMBC_server(frame, context, True, server, share,
740 &workgroup, &user, &password);
744 SAFE_FREE(dir->fname);
753 /* Now, list the files ... */
755 p = path + strlen(path);
756 path = talloc_asprintf_append(path, "\\*");
759 SAFE_FREE(dir->fname);
766 if (!cli_resolve_path(frame, "", srv->cli, path,
767 &targetcli, &targetpath)) {
768 d_printf("Could not resolve %s\n", path);
770 SAFE_FREE(dir->fname);
777 if (cli_list(targetcli, targetpath,
778 aDIR | aSYSTEM | aHIDDEN,
779 dir_list_fn, (void *)dir) < 0) {
782 SAFE_FREE(dir->fname);
785 saved_errno = SMBC_errno(context, targetcli);
787 if (saved_errno == EINVAL) {
789 * See if they asked to opendir
790 * something other than a directory.
791 * If so, the converted error value we
792 * got would have been EINVAL rather
795 *p = '\0'; /* restore original path */
797 if (SMBC_getatr(context, srv, path,
799 NULL, NULL, NULL, NULL,
801 ! IS_DOS_DIR(mode)) {
803 /* It is. Correct the error value */
804 saved_errno = ENOTDIR;
809 * If there was an error and the server is no
812 if (cli_is_error(targetcli) &&
813 (context->server.check_server_fn)(context, srv)) {
815 /* ... then remove it. */
816 if ((context->server.remove_unused_server_fn)(context,
819 * We could not remove the
820 * server completely, remove
821 * it from the cache so we
822 * will not get it again. It
823 * will be removed when the
824 * last file/dir is closed.
826 (context->cache.remove_cached_server_fn)(context, srv);
838 DLIST_ADD(context->internal->files, dir);
845 * Routine to close a directory
849 SMBC_closedir_ctx(SMBCCTX *context,
852 TALLOC_CTX *frame = talloc_stackframe();
854 if (!context || !context->internal->initialized) {
860 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
866 remove_dir(dir); /* Clean it up */
868 DLIST_REMOVE(context->internal->files, dir);
872 SAFE_FREE(dir->fname);
873 SAFE_FREE(dir); /* Free the space too */
882 smbc_readdir_internal(SMBCCTX * context,
883 struct smbc_dirent *dest,
884 struct smbc_dirent *src,
887 if (context->options.urlencode_readdir_entries) {
889 /* url-encode the name. get back remaining buffer space */
891 SMBC_urlencode(dest->name, src->name, max_namebuf_len);
893 /* We now know the name length */
894 dest->namelen = strlen(dest->name);
896 /* Save the pointer to the beginning of the comment */
897 dest->comment = dest->name + dest->namelen + 1;
899 /* Copy the comment */
900 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
901 dest->comment[max_namebuf_len - 1] = '\0';
903 /* Save other fields */
904 dest->smbc_type = src->smbc_type;
905 dest->commentlen = strlen(dest->comment);
906 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
910 /* No encoding. Just copy the entry as is. */
911 memcpy(dest, src, src->dirlen);
912 dest->comment = (char *)(&dest->name + src->namelen + 1);
918 * Routine to get a directory entry
922 SMBC_readdir_ctx(SMBCCTX *context,
926 struct smbc_dirent *dirp, *dirent;
927 TALLOC_CTX *frame = talloc_stackframe();
929 /* Check that all is ok first ... */
931 if (!context || !context->internal->initialized) {
934 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
940 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
943 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
949 if (dir->file != False) { /* FIXME, should be dir, perhaps */
952 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
958 if (!dir->dir_next) {
963 dirent = dir->dir_next->dirent;
972 dirp = (struct smbc_dirent *)context->internal->dirent;
973 maxlen = (sizeof(context->internal->dirent) -
974 sizeof(struct smbc_dirent));
976 smbc_readdir_internal(context, dirp, dirent, maxlen);
978 dir->dir_next = dir->dir_next->next;
985 * Routine to get directory entries
989 SMBC_getdents_ctx(SMBCCTX *context,
991 struct smbc_dirent *dirp,
997 char *ndir = (char *)dirp;
998 struct smbc_dir_list *dirlist;
999 TALLOC_CTX *frame = talloc_stackframe();
1001 /* Check that all is ok first ... */
1003 if (!context || !context->internal->initialized) {
1011 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1019 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1028 * Now, retrieve the number of entries that will fit in what was passed
1029 * We have to figure out if the info is in the list, or we need to
1030 * send a request to the server to get the info.
1033 while ((dirlist = dir->dir_next)) {
1034 struct smbc_dirent *dirent;
1036 if (!dirlist->dirent) {
1038 errno = ENOENT; /* Bad error */
1044 /* Do urlencoding of next entry, if so selected */
1045 dirent = (struct smbc_dirent *)context->internal->dirent;
1046 maxlen = (sizeof(context->internal->dirent) -
1047 sizeof(struct smbc_dirent));
1048 smbc_readdir_internal(context, dirent,
1049 dirlist->dirent, maxlen);
1051 reqd = dirent->dirlen;
1055 if (rem < count) { /* We managed to copy something */
1062 else { /* Nothing copied ... */
1064 errno = EINVAL; /* Not enough space ... */
1072 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1074 ((struct smbc_dirent *)ndir)->comment =
1075 (char *)(&((struct smbc_dirent *)ndir)->name +
1083 dir->dir_next = dirlist = dirlist -> next;
1096 * Routine to create a directory ...
1100 SMBC_mkdir_ctx(SMBCCTX *context,
1104 SMBCSRV *srv = NULL;
1105 char *server = NULL;
1108 char *password = NULL;
1109 char *workgroup = NULL;
1111 char *targetpath = NULL;
1112 struct cli_state *targetcli = NULL;
1113 TALLOC_CTX *frame = talloc_stackframe();
1115 if (!context || !context->internal->initialized) {
1127 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1129 if (SMBC_parse_path(frame,
1144 if (!user || user[0] == (char)0) {
1145 user = talloc_strdup(frame, context->config.user);
1153 srv = SMBC_server(frame, context, True,
1154 server, share, &workgroup, &user, &password);
1159 return -1; /* errno set by SMBC_server */
1163 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1164 if (!cli_resolve_path(frame, "", srv->cli, path,
1165 &targetcli, &targetpath)) {
1166 d_printf("Could not resolve %s\n", path);
1170 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1172 if (!cli_mkdir(targetcli, targetpath)) {
1174 errno = SMBC_errno(context, targetcli);
1186 * Our list function simply checks to see if a directory is not empty
1189 static int smbc_rmdir_dirempty = True;
1192 rmdir_list_fn(const char *mnt,
1197 if (strncmp(finfo->name, ".", 1) != 0 &&
1198 strncmp(finfo->name, "..", 2) != 0) {
1199 smbc_rmdir_dirempty = False;
1204 * Routine to remove a directory
1208 SMBC_rmdir_ctx(SMBCCTX *context,
1211 SMBCSRV *srv = NULL;
1212 char *server = NULL;
1215 char *password = NULL;
1216 char *workgroup = NULL;
1218 char *targetpath = NULL;
1219 struct cli_state *targetcli = NULL;
1220 TALLOC_CTX *frame = talloc_stackframe();
1222 if (!context || !context->internal->initialized) {
1234 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1236 if (SMBC_parse_path(frame,
1251 if (!user || user[0] == (char)0) {
1252 user = talloc_strdup(frame, context->config.user);
1260 srv = SMBC_server(frame, context, True,
1261 server, share, &workgroup, &user, &password);
1266 return -1; /* errno set by SMBC_server */
1270 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1271 if (!cli_resolve_path(frame, "", srv->cli, path,
1272 &targetcli, &targetpath)) {
1273 d_printf("Could not resolve %s\n", path);
1277 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1280 if (!cli_rmdir(targetcli, targetpath)) {
1282 errno = SMBC_errno(context, targetcli);
1284 if (errno == EACCES) { /* Check if the dir empty or not */
1286 /* Local storage to avoid buffer overflows */
1289 smbc_rmdir_dirempty = True; /* Make this so ... */
1291 lpath = talloc_asprintf(frame, "%s\\*",
1299 if (cli_list(targetcli, lpath,
1300 aDIR | aSYSTEM | aHIDDEN,
1301 rmdir_list_fn, NULL) < 0) {
1303 /* Fix errno to ignore latest error ... */
1304 DEBUG(5, ("smbc_rmdir: "
1305 "cli_list returned an error: %d\n",
1306 SMBC_errno(context, targetcli)));
1311 if (smbc_rmdir_dirempty)
1329 * Routine to return the current directory position
1333 SMBC_telldir_ctx(SMBCCTX *context,
1336 TALLOC_CTX *frame = talloc_stackframe();
1338 if (!context || !context->internal->initialized) {
1346 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1354 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1362 /* See if we're already at the end. */
1363 if (dir->dir_next == NULL) {
1370 * We return the pointer here as the offset
1373 return (off_t)(long)dir->dir_next->dirent;
1377 * A routine to run down the list and see if the entry is OK
1380 static struct smbc_dir_list *
1381 check_dir_ent(struct smbc_dir_list *list,
1382 struct smbc_dirent *dirent)
1385 /* Run down the list looking for what we want */
1389 struct smbc_dir_list *tmp = list;
1393 if (tmp->dirent == dirent)
1402 return NULL; /* Not found, or an error */
1408 * Routine to seek on a directory
1412 SMBC_lseekdir_ctx(SMBCCTX *context,
1416 long int l_offset = offset; /* Handle problems of size */
1417 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1418 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1419 TALLOC_CTX *frame = talloc_stackframe();
1421 if (!context || !context->internal->initialized) {
1429 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1437 /* Now, check what we were passed and see if it is OK ... */
1439 if (dirent == NULL) { /* Seek to the begining of the list */
1441 dir->dir_next = dir->dir_list;
1447 if (offset == -1) { /* Seek to the end of the list */
1448 dir->dir_next = NULL;
1453 /* Now, run down the list and make sure that the entry is OK */
1454 /* This may need to be changed if we change the format of the list */
1456 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1457 errno = EINVAL; /* Bad entry */
1462 dir->dir_next = list_ent;
1469 * Routine to fstat a dir
1473 SMBC_fstatdir_ctx(SMBCCTX *context,
1478 if (!context || !context->internal->initialized) {
1484 /* No code yet ... */
1489 SMBC_chmod_ctx(SMBCCTX *context,
1493 SMBCSRV *srv = NULL;
1494 char *server = NULL;
1497 char *password = NULL;
1498 char *workgroup = NULL;
1501 TALLOC_CTX *frame = talloc_stackframe();
1503 if (!context || !context->internal->initialized) {
1505 errno = EINVAL; /* Best I can think of ... */
1516 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
1518 if (SMBC_parse_path(frame,
1533 if (!user || user[0] == (char)0) {
1534 user = talloc_strdup(frame, context->config.user);
1542 srv = SMBC_server(frame, context, True,
1543 server, share, &workgroup, &user, &password);
1547 return -1; /* errno set by SMBC_server */
1552 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1553 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1554 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1555 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1557 if (!cli_setatr(srv->cli, path, mode, 0)) {
1558 errno = SMBC_errno(context, srv->cli);
1568 SMBC_utimes_ctx(SMBCCTX *context,
1570 struct timeval *tbuf)
1572 SMBCSRV *srv = NULL;
1573 char *server = NULL;
1576 char *password = NULL;
1577 char *workgroup = NULL;
1581 TALLOC_CTX *frame = talloc_stackframe();
1583 if (!context || !context->internal->initialized) {
1585 errno = EINVAL; /* Best I can think of ... */
1597 access_time = write_time = time(NULL);
1599 access_time = tbuf[0].tv_sec;
1600 write_time = tbuf[1].tv_sec;
1608 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1609 atimebuf[sizeof(atimebuf) - 1] = '\0';
1610 if ((p = strchr(atimebuf, '\n')) != NULL) {
1614 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1615 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1616 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1620 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1621 fname, atimebuf, mtimebuf);
1624 if (SMBC_parse_path(frame,
1639 if (!user || user[0] == (char)0) {
1640 user = talloc_strdup(frame, context->config.user);
1648 srv = SMBC_server(frame, context, True,
1649 server, share, &workgroup, &user, &password);
1653 return -1; /* errno set by SMBC_server */
1656 if (!SMBC_setatr(context, srv, path,
1657 0, access_time, write_time, 0, 0)) {
1659 return -1; /* errno set by SMBC_setatr */
1667 * Routine to unlink() a file
1671 SMBC_unlink_ctx(SMBCCTX *context,
1674 char *server = NULL;
1677 char *password = NULL;
1678 char *workgroup = NULL;
1680 char *targetpath = NULL;
1681 struct cli_state *targetcli = NULL;
1682 SMBCSRV *srv = NULL;
1683 TALLOC_CTX *frame = talloc_stackframe();
1685 if (!context || !context->internal->initialized) {
1687 errno = EINVAL; /* Best I can think of ... */
1700 if (SMBC_parse_path(frame,
1715 if (!user || user[0] == (char)0) {
1716 user = talloc_strdup(frame, context->config.user);
1724 srv = SMBC_server(frame, context, True,
1725 server, share, &workgroup, &user, &password);
1729 return -1; /* SMBC_server sets errno */
1733 /*d_printf(">>>unlink: resolving %s\n", path);*/
1734 if (!cli_resolve_path(frame, "", srv->cli, path,
1735 &targetcli, &targetpath)) {
1736 d_printf("Could not resolve %s\n", path);
1740 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1742 if (!cli_unlink(targetcli, targetpath)) {
1744 errno = SMBC_errno(context, targetcli);
1746 if (errno == EACCES) { /* Check if the file is a directory */
1751 struct timespec write_time_ts;
1752 struct timespec access_time_ts;
1753 struct timespec change_time_ts;
1756 if (!SMBC_getatr(context, srv, path, &mode, &size,
1763 /* Hmmm, bad error ... What? */
1765 errno = SMBC_errno(context, targetcli);
1772 if (IS_DOS_DIR(mode))
1775 errno = saverr; /* Restore this */
1786 return 0; /* Success ... */
1791 * Routine to rename() a file
1795 SMBC_rename_ctx(SMBCCTX *ocontext,
1800 char *server1 = NULL;
1801 char *share1 = NULL;
1802 char *server2 = NULL;
1803 char *share2 = NULL;
1806 char *password1 = NULL;
1807 char *password2 = NULL;
1808 char *workgroup = NULL;
1811 char *targetpath1 = NULL;
1812 char *targetpath2 = NULL;
1813 struct cli_state *targetcli1 = NULL;
1814 struct cli_state *targetcli2 = NULL;
1815 SMBCSRV *srv = NULL;
1816 TALLOC_CTX *frame = talloc_stackframe();
1818 if (!ocontext || !ncontext ||
1819 !ocontext->internal->initialized ||
1820 !ncontext->internal->initialized) {
1822 errno = EINVAL; /* Best I can think of ... */
1827 if (!oname || !nname) {
1833 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1835 if (SMBC_parse_path(frame,
1850 if (!user1 || user1[0] == (char)0) {
1851 user1 = talloc_strdup(frame, ocontext->config.user);
1859 if (SMBC_parse_path(frame,
1874 if (!user2 || user2[0] == (char)0) {
1875 user2 = talloc_strdup(frame, ncontext->config.user);
1883 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1884 strcmp(user1, user2)) {
1885 /* Can't rename across file systems, or users?? */
1891 srv = SMBC_server(frame, ocontext, True,
1892 server1, share1, &workgroup, &user1, &password1);
1899 /*d_printf(">>>rename: resolving %s\n", path1);*/
1900 if (!cli_resolve_path(frame, "", srv->cli, path1,
1901 &targetcli1, &targetpath1)) {
1902 d_printf("Could not resolve %s\n", path1);
1906 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1907 /*d_printf(">>>rename: resolving %s\n", path2);*/
1908 if (!cli_resolve_path(frame, "", srv->cli, path2,
1909 &targetcli2, &targetpath2)) {
1910 d_printf("Could not resolve %s\n", path2);
1914 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1916 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1917 strcmp(targetcli1->share, targetcli2->share))
1919 /* can't rename across file systems */
1925 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1926 int eno = SMBC_errno(ocontext, targetcli1);
1928 if (eno != EEXIST ||
1929 !cli_unlink(targetcli1, targetpath2) ||
1930 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1940 return 0; /* Success */