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,
264 uint32 preferred_len = 0xffffffff;
266 struct srvsvc_NetShareInfoCtr info_ctr;
267 struct srvsvc_NetShareCtr1 ctr1;
269 fstring comment = "";
270 struct rpc_pipe_client *pipe_hnd;
272 uint32_t resume_handle = 0;
273 uint32_t total_entries = 0;
275 /* Open the server service pipe */
276 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
278 if (!NT_STATUS_IS_OK(nt_status)) {
279 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
283 ZERO_STRUCT(info_ctr);
287 info_ctr.ctr.ctr1 = &ctr1;
289 /* Issue the NetShareEnum RPC call and retrieve the response */
290 nt_status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, talloc_tos(),
298 /* Was it successful? */
299 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result) ||
300 total_entries == 0) {
301 /* Nope. Go clean up. */
305 /* For each returned entry... */
306 for (i = 0; i < total_entries; i++) {
308 /* pull out the share name */
309 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
311 /* pull out the share's comment */
312 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
314 /* Get the type value */
315 type = info_ctr.ctr.ctr1->array[i].type;
317 /* Add this share to the list */
318 (*fn)(name, type, comment, state);
322 /* Close the server service pipe */
323 TALLOC_FREE(pipe_hnd);
325 /* Tell 'em if it worked */
326 return W_ERROR_IS_OK(result) ? 0 : -1;
331 * Verify that the options specified in a URL are valid
334 SMBC_check_options(char *server,
339 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
340 "path='%s' options='%s'\n",
341 server, share, path, options));
343 /* No options at all is always ok */
344 if (! *options) return 0;
346 /* Currently, we don't support any options. */
352 SMBC_opendir_ctx(SMBCCTX *context,
359 char *password = NULL;
360 char *options = NULL;
361 char *workgroup = NULL;
366 SMBCFILE *dir = NULL;
367 struct sockaddr_storage rem_ss;
368 TALLOC_CTX *frame = talloc_stackframe();
370 if (!context || !context->internal->initialized) {
371 DEBUG(4, ("no valid context\n"));
372 errno = EINVAL + 8192;
379 DEBUG(4, ("no valid fname\n"));
380 errno = EINVAL + 8193;
385 if (SMBC_parse_path(frame,
395 DEBUG(4, ("no valid path\n"));
396 errno = EINVAL + 8194;
401 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
402 "path='%s' options='%s'\n",
403 fname, server, share, path, options));
405 /* Ensure the options are valid */
406 if (SMBC_check_options(server, share, path, options)) {
407 DEBUG(4, ("unacceptable options (%s)\n", options));
408 errno = EINVAL + 8195;
413 if (!user || user[0] == (char)0) {
414 user = talloc_strdup(frame, smbc_getUser(context));
422 dir = SMB_MALLOC_P(SMBCFILE);
433 dir->fname = SMB_STRDUP(fname);
437 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
439 if (server[0] == (char)0) {
444 struct ip_service *ip_list;
445 struct ip_service server_addr;
446 struct user_auth_info u_info;
448 if (share[0] != (char)0 || path[0] != (char)0) {
450 errno = EINVAL + 8196;
452 SAFE_FREE(dir->fname);
459 /* Determine how many local master browsers to query */
460 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
462 : smbc_getOptionBrowseMaxLmbCount(context));
464 memset(&u_info, '\0', sizeof(u_info));
465 u_info.username = talloc_strdup(frame,user);
466 u_info.password = talloc_strdup(frame,password);
467 if (!u_info.username || !u_info.password) {
469 SAFE_FREE(dir->fname);
477 * We have server and share and path empty but options
478 * requesting that we scan all master browsers for their list
479 * of workgroups/domains. This implies that we must first try
480 * broadcast queries to find all master browsers, and if that
481 * doesn't work, then try our other methods which return only
482 * a single master browser.
486 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
492 if (!find_master_ip(workgroup, &server_addr.ss)) {
495 SAFE_FREE(dir->fname);
503 ip_list = (struct ip_service *)memdup(
504 &server_addr, sizeof(server_addr));
505 if (ip_list == NULL) {
513 for (i = 0; i < count && i < max_lmb_count; i++) {
514 char addr[INET6_ADDRSTRLEN];
516 struct cli_state *cli = NULL;
518 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
519 DEBUG(99, ("Found master browser %d of %d: %s\n",
520 i+1, MAX(count, max_lmb_count),
523 cli = get_ipc_connect_master_ip(talloc_tos(),
527 /* cli == NULL is the master browser refused to talk or
528 could not be found */
533 workgroup = talloc_strdup(frame, wg_ptr);
534 server = talloc_strdup(frame, cli->desthost);
538 if (!workgroup || !server) {
544 DEBUG(4, ("using workgroup %s %s\n",
548 * For each returned master browser IP address, get a
549 * connection to IPC$ on the server if we do not
550 * already have one, and determine the
551 * workgroups/domains that it knows about.
554 srv = SMBC_server(frame, context, True, server, "IPC$",
555 &workgroup, &user, &password);
561 dir->dir_type = SMBC_WORKGROUP;
563 /* Now, list the stuff ... */
565 if (!cli_NetServerEnum(srv->cli,
577 * Server not an empty string ... Check the rest and see what
580 if (*share == '\0') {
583 /* Should not have empty share with path */
584 errno = EINVAL + 8197;
586 SAFE_FREE(dir->fname);
595 * We don't know if <server> is really a server name
596 * or is a workgroup/domain name. If we already have
597 * a server structure for it, we'll use it.
598 * Otherwise, check to see if <server><1D>,
599 * <server><1B>, or <server><20> translates. We check
600 * to see if <server> is an IP address first.
604 * See if we have an existing server. Do not
605 * establish a connection if one does not already
608 srv = SMBC_server(frame, context, False,
610 &workgroup, &user, &password);
613 * If no existing server and not an IP addr, look for
617 !is_ipaddress(server) &&
618 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
619 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
623 dir->dir_type = SMBC_SERVER;
626 * Get the backup list ...
628 if (!name_status_find(server, 0, 0,
629 &rem_ss, buserver)) {
631 DEBUG(0,("Could not get name of "
632 "local/domain master browser "
633 "for server %s\n", server));
635 SAFE_FREE(dir->fname);
645 * Get a connection to IPC$ on the server if
646 * we do not already have one
648 srv = SMBC_server(frame, context, True,
653 DEBUG(0, ("got no contact to IPC$\n"));
655 SAFE_FREE(dir->fname);
665 /* Now, list the servers ... */
666 if (!cli_NetServerEnum(srv->cli, server,
671 SAFE_FREE(dir->fname);
678 (resolve_name(server, &rem_ss, 0x20))) {
681 * If we hadn't found the server, get one now
684 srv = SMBC_server(frame, context, True,
692 SAFE_FREE(dir->fname);
700 dir->dir_type = SMBC_FILE_SHARE;
703 /* List the shares ... */
705 if (net_share_enum_rpc(
714 errno = cli_errno(srv->cli);
716 SAFE_FREE(dir->fname);
724 /* Neither the workgroup nor server exists */
725 errno = ECONNREFUSED;
727 SAFE_FREE(dir->fname);
737 * The server and share are specified ... work from
741 struct cli_state *targetcli;
743 /* We connect to the server and list the directory */
744 dir->dir_type = SMBC_FILE_SHARE;
746 srv = SMBC_server(frame, context, True, server, share,
747 &workgroup, &user, &password);
751 SAFE_FREE(dir->fname);
760 /* Now, list the files ... */
762 p = path + strlen(path);
763 path = talloc_asprintf_append(path, "\\*");
766 SAFE_FREE(dir->fname);
773 if (!cli_resolve_path(frame, "", srv->cli, path,
774 &targetcli, &targetpath)) {
775 d_printf("Could not resolve %s\n", path);
777 SAFE_FREE(dir->fname);
784 if (cli_list(targetcli, targetpath,
785 aDIR | aSYSTEM | aHIDDEN,
786 dir_list_fn, (void *)dir) < 0) {
789 SAFE_FREE(dir->fname);
792 saved_errno = SMBC_errno(context, targetcli);
794 if (saved_errno == EINVAL) {
796 * See if they asked to opendir
797 * something other than a directory.
798 * If so, the converted error value we
799 * got would have been EINVAL rather
802 *p = '\0'; /* restore original path */
804 if (SMBC_getatr(context, srv, path,
806 NULL, NULL, NULL, NULL,
808 ! IS_DOS_DIR(mode)) {
810 /* It is. Correct the error value */
811 saved_errno = ENOTDIR;
816 * If there was an error and the server is no
819 if (cli_is_error(targetcli) &&
820 smbc_getFunctionCheckServer(context)(context, srv)) {
822 /* ... then remove it. */
823 if (smbc_getFunctionRemoveUnusedServer(context)(context,
826 * We could not remove the
827 * server completely, remove
828 * it from the cache so we
829 * will not get it again. It
830 * will be removed when the
831 * last file/dir is closed.
833 smbc_getFunctionRemoveCachedServer(context)(context, srv);
845 DLIST_ADD(context->internal->files, dir);
852 * Routine to close a directory
856 SMBC_closedir_ctx(SMBCCTX *context,
859 TALLOC_CTX *frame = talloc_stackframe();
861 if (!context || !context->internal->initialized) {
867 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
873 remove_dir(dir); /* Clean it up */
875 DLIST_REMOVE(context->internal->files, dir);
879 SAFE_FREE(dir->fname);
880 SAFE_FREE(dir); /* Free the space too */
889 smbc_readdir_internal(SMBCCTX * context,
890 struct smbc_dirent *dest,
891 struct smbc_dirent *src,
894 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
896 /* url-encode the name. get back remaining buffer space */
898 SMBC_urlencode(dest->name, src->name, max_namebuf_len);
900 /* We now know the name length */
901 dest->namelen = strlen(dest->name);
903 /* Save the pointer to the beginning of the comment */
904 dest->comment = dest->name + dest->namelen + 1;
906 /* Copy the comment */
907 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
908 dest->comment[max_namebuf_len - 1] = '\0';
910 /* Save other fields */
911 dest->smbc_type = src->smbc_type;
912 dest->commentlen = strlen(dest->comment);
913 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
917 /* No encoding. Just copy the entry as is. */
918 memcpy(dest, src, src->dirlen);
919 dest->comment = (char *)(&dest->name + src->namelen + 1);
925 * Routine to get a directory entry
929 SMBC_readdir_ctx(SMBCCTX *context,
933 struct smbc_dirent *dirp, *dirent;
934 TALLOC_CTX *frame = talloc_stackframe();
936 /* Check that all is ok first ... */
938 if (!context || !context->internal->initialized) {
941 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
947 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
950 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
956 if (dir->file != False) { /* FIXME, should be dir, perhaps */
959 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
965 if (!dir->dir_next) {
970 dirent = dir->dir_next->dirent;
979 dirp = &context->internal->dirent;
980 maxlen = sizeof(context->internal->_dirent_name);
982 smbc_readdir_internal(context, dirp, dirent, maxlen);
984 dir->dir_next = dir->dir_next->next;
991 * Routine to get directory entries
995 SMBC_getdents_ctx(SMBCCTX *context,
997 struct smbc_dirent *dirp,
1003 char *ndir = (char *)dirp;
1004 struct smbc_dir_list *dirlist;
1005 TALLOC_CTX *frame = talloc_stackframe();
1007 /* Check that all is ok first ... */
1009 if (!context || !context->internal->initialized) {
1017 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1025 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1034 * Now, retrieve the number of entries that will fit in what was passed
1035 * We have to figure out if the info is in the list, or we need to
1036 * send a request to the server to get the info.
1039 while ((dirlist = dir->dir_next)) {
1040 struct smbc_dirent *dirent;
1042 if (!dirlist->dirent) {
1044 errno = ENOENT; /* Bad error */
1050 /* Do urlencoding of next entry, if so selected */
1051 dirent = &context->internal->dirent;
1052 maxlen = sizeof(context->internal->_dirent_name);
1053 smbc_readdir_internal(context, dirent,
1054 dirlist->dirent, maxlen);
1056 reqd = dirent->dirlen;
1060 if (rem < count) { /* We managed to copy something */
1067 else { /* Nothing copied ... */
1069 errno = EINVAL; /* Not enough space ... */
1077 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1079 ((struct smbc_dirent *)ndir)->comment =
1080 (char *)(&((struct smbc_dirent *)ndir)->name +
1088 dir->dir_next = dirlist = dirlist -> next;
1101 * Routine to create a directory ...
1105 SMBC_mkdir_ctx(SMBCCTX *context,
1109 SMBCSRV *srv = NULL;
1110 char *server = NULL;
1113 char *password = NULL;
1114 char *workgroup = NULL;
1116 char *targetpath = NULL;
1117 struct cli_state *targetcli = NULL;
1118 TALLOC_CTX *frame = talloc_stackframe();
1120 if (!context || !context->internal->initialized) {
1132 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1134 if (SMBC_parse_path(frame,
1149 if (!user || user[0] == (char)0) {
1150 user = talloc_strdup(frame, smbc_getUser(context));
1158 srv = SMBC_server(frame, context, True,
1159 server, share, &workgroup, &user, &password);
1164 return -1; /* errno set by SMBC_server */
1168 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1169 if (!cli_resolve_path(frame, "", srv->cli, path,
1170 &targetcli, &targetpath)) {
1171 d_printf("Could not resolve %s\n", path);
1175 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1177 if (!cli_mkdir(targetcli, targetpath)) {
1179 errno = SMBC_errno(context, targetcli);
1191 * Our list function simply checks to see if a directory is not empty
1195 rmdir_list_fn(const char *mnt,
1200 if (strncmp(finfo->name, ".", 1) != 0 &&
1201 strncmp(finfo->name, "..", 2) != 0) {
1202 bool *smbc_rmdir_dirempty = (bool *)state;
1203 *smbc_rmdir_dirempty = false;
1208 * Routine to remove a directory
1212 SMBC_rmdir_ctx(SMBCCTX *context,
1215 SMBCSRV *srv = NULL;
1216 char *server = NULL;
1219 char *password = NULL;
1220 char *workgroup = NULL;
1222 char *targetpath = NULL;
1223 struct cli_state *targetcli = NULL;
1224 TALLOC_CTX *frame = talloc_stackframe();
1226 if (!context || !context->internal->initialized) {
1238 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1240 if (SMBC_parse_path(frame,
1255 if (!user || user[0] == (char)0) {
1256 user = talloc_strdup(frame, smbc_getUser(context));
1264 srv = SMBC_server(frame, context, True,
1265 server, share, &workgroup, &user, &password);
1270 return -1; /* errno set by SMBC_server */
1274 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1275 if (!cli_resolve_path(frame, "", srv->cli, path,
1276 &targetcli, &targetpath)) {
1277 d_printf("Could not resolve %s\n", path);
1281 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1284 if (!cli_rmdir(targetcli, targetpath)) {
1286 errno = SMBC_errno(context, targetcli);
1288 if (errno == EACCES) { /* Check if the dir empty or not */
1290 /* Local storage to avoid buffer overflows */
1292 bool smbc_rmdir_dirempty = true;
1294 lpath = talloc_asprintf(frame, "%s\\*",
1302 if (cli_list(targetcli, lpath,
1303 aDIR | aSYSTEM | aHIDDEN,
1305 &smbc_rmdir_dirempty) < 0) {
1307 /* Fix errno to ignore latest error ... */
1308 DEBUG(5, ("smbc_rmdir: "
1309 "cli_list returned an error: %d\n",
1310 SMBC_errno(context, targetcli)));
1315 if (smbc_rmdir_dirempty)
1333 * Routine to return the current directory position
1337 SMBC_telldir_ctx(SMBCCTX *context,
1340 TALLOC_CTX *frame = talloc_stackframe();
1342 if (!context || !context->internal->initialized) {
1350 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1358 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1366 /* See if we're already at the end. */
1367 if (dir->dir_next == NULL) {
1374 * We return the pointer here as the offset
1377 return (off_t)(long)dir->dir_next->dirent;
1381 * A routine to run down the list and see if the entry is OK
1384 static struct smbc_dir_list *
1385 check_dir_ent(struct smbc_dir_list *list,
1386 struct smbc_dirent *dirent)
1389 /* Run down the list looking for what we want */
1393 struct smbc_dir_list *tmp = list;
1397 if (tmp->dirent == dirent)
1406 return NULL; /* Not found, or an error */
1412 * Routine to seek on a directory
1416 SMBC_lseekdir_ctx(SMBCCTX *context,
1420 long int l_offset = offset; /* Handle problems of size */
1421 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1422 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1423 TALLOC_CTX *frame = talloc_stackframe();
1425 if (!context || !context->internal->initialized) {
1433 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1441 /* Now, check what we were passed and see if it is OK ... */
1443 if (dirent == NULL) { /* Seek to the begining of the list */
1445 dir->dir_next = dir->dir_list;
1451 if (offset == -1) { /* Seek to the end of the list */
1452 dir->dir_next = NULL;
1457 /* Now, run down the list and make sure that the entry is OK */
1458 /* This may need to be changed if we change the format of the list */
1460 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1461 errno = EINVAL; /* Bad entry */
1466 dir->dir_next = list_ent;
1473 * Routine to fstat a dir
1477 SMBC_fstatdir_ctx(SMBCCTX *context,
1482 if (!context || !context->internal->initialized) {
1488 /* No code yet ... */
1493 SMBC_chmod_ctx(SMBCCTX *context,
1497 SMBCSRV *srv = NULL;
1498 char *server = NULL;
1501 char *password = NULL;
1502 char *workgroup = NULL;
1505 TALLOC_CTX *frame = talloc_stackframe();
1507 if (!context || !context->internal->initialized) {
1509 errno = EINVAL; /* Best I can think of ... */
1520 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
1522 if (SMBC_parse_path(frame,
1537 if (!user || user[0] == (char)0) {
1538 user = talloc_strdup(frame, smbc_getUser(context));
1546 srv = SMBC_server(frame, context, True,
1547 server, share, &workgroup, &user, &password);
1551 return -1; /* errno set by SMBC_server */
1556 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1557 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1558 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1559 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1561 if (!cli_setatr(srv->cli, path, mode, 0)) {
1562 errno = SMBC_errno(context, srv->cli);
1572 SMBC_utimes_ctx(SMBCCTX *context,
1574 struct timeval *tbuf)
1576 SMBCSRV *srv = NULL;
1577 char *server = NULL;
1580 char *password = NULL;
1581 char *workgroup = NULL;
1585 TALLOC_CTX *frame = talloc_stackframe();
1587 if (!context || !context->internal->initialized) {
1589 errno = EINVAL; /* Best I can think of ... */
1601 access_time = write_time = time(NULL);
1603 access_time = tbuf[0].tv_sec;
1604 write_time = tbuf[1].tv_sec;
1612 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1613 atimebuf[sizeof(atimebuf) - 1] = '\0';
1614 if ((p = strchr(atimebuf, '\n')) != NULL) {
1618 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1619 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1620 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1624 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1625 fname, atimebuf, mtimebuf);
1628 if (SMBC_parse_path(frame,
1643 if (!user || user[0] == (char)0) {
1644 user = talloc_strdup(frame, smbc_getUser(context));
1652 srv = SMBC_server(frame, context, True,
1653 server, share, &workgroup, &user, &password);
1657 return -1; /* errno set by SMBC_server */
1660 if (!SMBC_setatr(context, srv, path,
1661 0, access_time, write_time, 0, 0)) {
1663 return -1; /* errno set by SMBC_setatr */
1671 * Routine to unlink() a file
1675 SMBC_unlink_ctx(SMBCCTX *context,
1678 char *server = NULL;
1681 char *password = NULL;
1682 char *workgroup = NULL;
1684 char *targetpath = NULL;
1685 struct cli_state *targetcli = NULL;
1686 SMBCSRV *srv = NULL;
1687 TALLOC_CTX *frame = talloc_stackframe();
1689 if (!context || !context->internal->initialized) {
1691 errno = EINVAL; /* Best I can think of ... */
1704 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, share, &workgroup, &user, &password);
1733 return -1; /* SMBC_server sets errno */
1737 /*d_printf(">>>unlink: resolving %s\n", path);*/
1738 if (!cli_resolve_path(frame, "", srv->cli, path,
1739 &targetcli, &targetpath)) {
1740 d_printf("Could not resolve %s\n", path);
1744 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1746 if (!cli_unlink(targetcli, targetpath)) {
1748 errno = SMBC_errno(context, targetcli);
1750 if (errno == EACCES) { /* Check if the file is a directory */
1755 struct timespec write_time_ts;
1756 struct timespec access_time_ts;
1757 struct timespec change_time_ts;
1760 if (!SMBC_getatr(context, srv, path, &mode, &size,
1767 /* Hmmm, bad error ... What? */
1769 errno = SMBC_errno(context, targetcli);
1776 if (IS_DOS_DIR(mode))
1779 errno = saverr; /* Restore this */
1790 return 0; /* Success ... */
1795 * Routine to rename() a file
1799 SMBC_rename_ctx(SMBCCTX *ocontext,
1804 char *server1 = NULL;
1805 char *share1 = NULL;
1806 char *server2 = NULL;
1807 char *share2 = NULL;
1810 char *password1 = NULL;
1811 char *password2 = NULL;
1812 char *workgroup = NULL;
1815 char *targetpath1 = NULL;
1816 char *targetpath2 = NULL;
1817 struct cli_state *targetcli1 = NULL;
1818 struct cli_state *targetcli2 = NULL;
1819 SMBCSRV *srv = NULL;
1820 TALLOC_CTX *frame = talloc_stackframe();
1822 if (!ocontext || !ncontext ||
1823 !ocontext->internal->initialized ||
1824 !ncontext->internal->initialized) {
1826 errno = EINVAL; /* Best I can think of ... */
1831 if (!oname || !nname) {
1837 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1839 if (SMBC_parse_path(frame,
1854 if (!user1 || user1[0] == (char)0) {
1855 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1863 if (SMBC_parse_path(frame,
1878 if (!user2 || user2[0] == (char)0) {
1879 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1887 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1888 strcmp(user1, user2)) {
1889 /* Can't rename across file systems, or users?? */
1895 srv = SMBC_server(frame, ocontext, True,
1896 server1, share1, &workgroup, &user1, &password1);
1903 /*d_printf(">>>rename: resolving %s\n", path1);*/
1904 if (!cli_resolve_path(frame, "", srv->cli, path1,
1905 &targetcli1, &targetpath1)) {
1906 d_printf("Could not resolve %s\n", path1);
1910 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1911 /*d_printf(">>>rename: resolving %s\n", path2);*/
1912 if (!cli_resolve_path(frame, "", srv->cli, path2,
1913 &targetcli2, &targetpath2)) {
1914 d_printf("Could not resolve %s\n", path2);
1918 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1920 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1921 strcmp(targetcli1->share, targetcli2->share))
1923 /* can't rename across file systems */
1929 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1930 int eno = SMBC_errno(ocontext, targetcli1);
1932 if (eno != EEXIST ||
1933 !cli_unlink(targetcli1, targetpath2) ||
1934 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1944 return 0; /* Success */