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 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
278 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
282 ZERO_STRUCT(info_ctr);
286 info_ctr.ctr.ctr1 = &ctr1;
288 /* Issue the NetShareEnum RPC call and retrieve the response */
289 nt_status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, talloc_tos(),
297 /* Was it successful? */
298 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result) ||
299 total_entries == 0) {
300 /* Nope. Go clean up. */
304 /* For each returned entry... */
305 for (i = 0; i < total_entries; i++) {
307 /* pull out the share name */
308 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
310 /* pull out the share's comment */
311 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
313 /* Get the type value */
314 type = info_ctr.ctr.ctr1->array[i].type;
316 /* Add this share to the list */
317 (*fn)(name, type, comment, state);
321 /* Close the server service pipe */
322 TALLOC_FREE(pipe_hnd);
324 /* Tell 'em if it worked */
325 return W_ERROR_IS_OK(result) ? 0 : -1;
330 * Verify that the options specified in a URL are valid
333 SMBC_check_options(char *server,
338 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
339 "path='%s' options='%s'\n",
340 server, share, path, options));
342 /* No options at all is always ok */
343 if (! *options) return 0;
345 /* Currently, we don't support any options. */
351 SMBC_opendir_ctx(SMBCCTX *context,
358 char *password = NULL;
359 char *options = NULL;
360 char *workgroup = NULL;
365 SMBCFILE *dir = NULL;
366 struct sockaddr_storage rem_ss;
367 TALLOC_CTX *frame = talloc_stackframe();
369 if (!context || !context->internal->initialized) {
370 DEBUG(4, ("no valid context\n"));
371 errno = EINVAL + 8192;
378 DEBUG(4, ("no valid fname\n"));
379 errno = EINVAL + 8193;
384 if (SMBC_parse_path(frame,
394 DEBUG(4, ("no valid path\n"));
395 errno = EINVAL + 8194;
400 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
401 "path='%s' options='%s'\n",
402 fname, server, share, path, options));
404 /* Ensure the options are valid */
405 if (SMBC_check_options(server, share, path, options)) {
406 DEBUG(4, ("unacceptable options (%s)\n", options));
407 errno = EINVAL + 8195;
412 if (!user || user[0] == (char)0) {
413 user = talloc_strdup(frame, smbc_getUser(context));
421 dir = SMB_MALLOC_P(SMBCFILE);
432 dir->fname = SMB_STRDUP(fname);
436 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
438 if (server[0] == (char)0) {
443 struct ip_service *ip_list;
444 struct ip_service server_addr;
445 struct user_auth_info u_info;
447 if (share[0] != (char)0 || path[0] != (char)0) {
449 errno = EINVAL + 8196;
451 SAFE_FREE(dir->fname);
458 /* Determine how many local master browsers to query */
459 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
461 : smbc_getOptionBrowseMaxLmbCount(context));
463 memset(&u_info, '\0', sizeof(u_info));
464 u_info.username = talloc_strdup(frame,user);
465 u_info.password = talloc_strdup(frame,password);
466 if (!u_info.username || !u_info.password) {
468 SAFE_FREE(dir->fname);
476 * We have server and share and path empty but options
477 * requesting that we scan all master browsers for their list
478 * of workgroups/domains. This implies that we must first try
479 * broadcast queries to find all master browsers, and if that
480 * doesn't work, then try our other methods which return only
481 * a single master browser.
485 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
491 if (!find_master_ip(workgroup, &server_addr.ss)) {
494 SAFE_FREE(dir->fname);
502 ip_list = (struct ip_service *)memdup(
503 &server_addr, sizeof(server_addr));
504 if (ip_list == NULL) {
512 for (i = 0; i < count && i < max_lmb_count; i++) {
513 char addr[INET6_ADDRSTRLEN];
515 struct cli_state *cli = NULL;
517 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
518 DEBUG(99, ("Found master browser %d of %d: %s\n",
519 i+1, MAX(count, max_lmb_count),
522 cli = get_ipc_connect_master_ip(talloc_tos(),
526 /* cli == NULL is the master browser refused to talk or
527 could not be found */
532 workgroup = talloc_strdup(frame, wg_ptr);
533 server = talloc_strdup(frame, cli->desthost);
537 if (!workgroup || !server) {
543 DEBUG(4, ("using workgroup %s %s\n",
547 * For each returned master browser IP address, get a
548 * connection to IPC$ on the server if we do not
549 * already have one, and determine the
550 * workgroups/domains that it knows about.
553 srv = SMBC_server(frame, context, True, server, "IPC$",
554 &workgroup, &user, &password);
560 dir->dir_type = SMBC_WORKGROUP;
562 /* Now, list the stuff ... */
564 if (!cli_NetServerEnum(srv->cli,
576 * Server not an empty string ... Check the rest and see what
579 if (*share == '\0') {
582 /* Should not have empty share with path */
583 errno = EINVAL + 8197;
585 SAFE_FREE(dir->fname);
594 * We don't know if <server> is really a server name
595 * or is a workgroup/domain name. If we already have
596 * a server structure for it, we'll use it.
597 * Otherwise, check to see if <server><1D>,
598 * <server><1B>, or <server><20> translates. We check
599 * to see if <server> is an IP address first.
603 * See if we have an existing server. Do not
604 * establish a connection if one does not already
607 srv = SMBC_server(frame, context, False,
609 &workgroup, &user, &password);
612 * If no existing server and not an IP addr, look for
616 !is_ipaddress(server) &&
617 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
618 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
622 dir->dir_type = SMBC_SERVER;
625 * Get the backup list ...
627 if (!name_status_find(server, 0, 0,
628 &rem_ss, buserver)) {
630 DEBUG(0,("Could not get name of "
631 "local/domain master browser "
632 "for server %s\n", server));
634 SAFE_FREE(dir->fname);
644 * Get a connection to IPC$ on the server if
645 * we do not already have one
647 srv = SMBC_server(frame, context, True,
652 DEBUG(0, ("got no contact to IPC$\n"));
654 SAFE_FREE(dir->fname);
664 /* Now, list the servers ... */
665 if (!cli_NetServerEnum(srv->cli, server,
670 SAFE_FREE(dir->fname);
677 (resolve_name(server, &rem_ss, 0x20))) {
680 * If we hadn't found the server, get one now
683 srv = SMBC_server(frame, context, True,
691 SAFE_FREE(dir->fname);
699 dir->dir_type = SMBC_FILE_SHARE;
702 /* List the shares ... */
704 if (net_share_enum_rpc(
713 errno = cli_errno(srv->cli);
715 SAFE_FREE(dir->fname);
723 /* Neither the workgroup nor server exists */
724 errno = ECONNREFUSED;
726 SAFE_FREE(dir->fname);
736 * The server and share are specified ... work from
740 struct cli_state *targetcli;
742 /* We connect to the server and list the directory */
743 dir->dir_type = SMBC_FILE_SHARE;
745 srv = SMBC_server(frame, context, True, server, share,
746 &workgroup, &user, &password);
750 SAFE_FREE(dir->fname);
759 /* Now, list the files ... */
761 p = path + strlen(path);
762 path = talloc_asprintf_append(path, "\\*");
765 SAFE_FREE(dir->fname);
772 if (!cli_resolve_path(frame, "", srv->cli, path,
773 &targetcli, &targetpath)) {
774 d_printf("Could not resolve %s\n", path);
776 SAFE_FREE(dir->fname);
783 if (cli_list(targetcli, targetpath,
784 aDIR | aSYSTEM | aHIDDEN,
785 dir_list_fn, (void *)dir) < 0) {
788 SAFE_FREE(dir->fname);
791 saved_errno = SMBC_errno(context, targetcli);
793 if (saved_errno == EINVAL) {
795 * See if they asked to opendir
796 * something other than a directory.
797 * If so, the converted error value we
798 * got would have been EINVAL rather
801 *p = '\0'; /* restore original path */
803 if (SMBC_getatr(context, srv, path,
805 NULL, NULL, NULL, NULL,
807 ! IS_DOS_DIR(mode)) {
809 /* It is. Correct the error value */
810 saved_errno = ENOTDIR;
815 * If there was an error and the server is no
818 if (cli_is_error(targetcli) &&
819 smbc_getFunctionCheckServer(context)(context, srv)) {
821 /* ... then remove it. */
822 if (smbc_getFunctionRemoveUnusedServer(context)(context,
825 * We could not remove the
826 * server completely, remove
827 * it from the cache so we
828 * will not get it again. It
829 * will be removed when the
830 * last file/dir is closed.
832 smbc_getFunctionRemoveCachedServer(context)(context, srv);
844 DLIST_ADD(context->internal->files, dir);
851 * Routine to close a directory
855 SMBC_closedir_ctx(SMBCCTX *context,
858 TALLOC_CTX *frame = talloc_stackframe();
860 if (!context || !context->internal->initialized) {
866 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
872 remove_dir(dir); /* Clean it up */
874 DLIST_REMOVE(context->internal->files, dir);
878 SAFE_FREE(dir->fname);
879 SAFE_FREE(dir); /* Free the space too */
888 smbc_readdir_internal(SMBCCTX * context,
889 struct smbc_dirent *dest,
890 struct smbc_dirent *src,
893 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
895 /* url-encode the name. get back remaining buffer space */
897 SMBC_urlencode(dest->name, src->name, max_namebuf_len);
899 /* We now know the name length */
900 dest->namelen = strlen(dest->name);
902 /* Save the pointer to the beginning of the comment */
903 dest->comment = dest->name + dest->namelen + 1;
905 /* Copy the comment */
906 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
907 dest->comment[max_namebuf_len - 1] = '\0';
909 /* Save other fields */
910 dest->smbc_type = src->smbc_type;
911 dest->commentlen = strlen(dest->comment);
912 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
916 /* No encoding. Just copy the entry as is. */
917 memcpy(dest, src, src->dirlen);
918 dest->comment = (char *)(&dest->name + src->namelen + 1);
924 * Routine to get a directory entry
928 SMBC_readdir_ctx(SMBCCTX *context,
932 struct smbc_dirent *dirp, *dirent;
933 TALLOC_CTX *frame = talloc_stackframe();
935 /* Check that all is ok first ... */
937 if (!context || !context->internal->initialized) {
940 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
946 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
949 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
955 if (dir->file != False) { /* FIXME, should be dir, perhaps */
958 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
964 if (!dir->dir_next) {
969 dirent = dir->dir_next->dirent;
978 dirp = (struct smbc_dirent *)context->internal->dirent;
979 maxlen = (sizeof(context->internal->dirent) -
980 sizeof(struct smbc_dirent));
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 = (struct smbc_dirent *)context->internal->dirent;
1052 maxlen = (sizeof(context->internal->dirent) -
1053 sizeof(struct smbc_dirent));
1054 smbc_readdir_internal(context, dirent,
1055 dirlist->dirent, maxlen);
1057 reqd = dirent->dirlen;
1061 if (rem < count) { /* We managed to copy something */
1068 else { /* Nothing copied ... */
1070 errno = EINVAL; /* Not enough space ... */
1078 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1080 ((struct smbc_dirent *)ndir)->comment =
1081 (char *)(&((struct smbc_dirent *)ndir)->name +
1089 dir->dir_next = dirlist = dirlist -> next;
1102 * Routine to create a directory ...
1106 SMBC_mkdir_ctx(SMBCCTX *context,
1110 SMBCSRV *srv = NULL;
1111 char *server = NULL;
1114 char *password = NULL;
1115 char *workgroup = NULL;
1117 char *targetpath = NULL;
1118 struct cli_state *targetcli = NULL;
1119 TALLOC_CTX *frame = talloc_stackframe();
1121 if (!context || !context->internal->initialized) {
1133 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1135 if (SMBC_parse_path(frame,
1150 if (!user || user[0] == (char)0) {
1151 user = talloc_strdup(frame, smbc_getUser(context));
1159 srv = SMBC_server(frame, context, True,
1160 server, share, &workgroup, &user, &password);
1165 return -1; /* errno set by SMBC_server */
1169 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1170 if (!cli_resolve_path(frame, "", srv->cli, path,
1171 &targetcli, &targetpath)) {
1172 d_printf("Could not resolve %s\n", path);
1176 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1178 if (!cli_mkdir(targetcli, targetpath)) {
1180 errno = SMBC_errno(context, targetcli);
1192 * Our list function simply checks to see if a directory is not empty
1195 static int smbc_rmdir_dirempty = True;
1198 rmdir_list_fn(const char *mnt,
1203 if (strncmp(finfo->name, ".", 1) != 0 &&
1204 strncmp(finfo->name, "..", 2) != 0) {
1205 smbc_rmdir_dirempty = False;
1210 * Routine to remove a directory
1214 SMBC_rmdir_ctx(SMBCCTX *context,
1217 SMBCSRV *srv = NULL;
1218 char *server = NULL;
1221 char *password = NULL;
1222 char *workgroup = NULL;
1224 char *targetpath = NULL;
1225 struct cli_state *targetcli = NULL;
1226 TALLOC_CTX *frame = talloc_stackframe();
1228 if (!context || !context->internal->initialized) {
1240 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1242 if (SMBC_parse_path(frame,
1257 if (!user || user[0] == (char)0) {
1258 user = talloc_strdup(frame, smbc_getUser(context));
1266 srv = SMBC_server(frame, context, True,
1267 server, share, &workgroup, &user, &password);
1272 return -1; /* errno set by SMBC_server */
1276 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1277 if (!cli_resolve_path(frame, "", srv->cli, path,
1278 &targetcli, &targetpath)) {
1279 d_printf("Could not resolve %s\n", path);
1283 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1286 if (!cli_rmdir(targetcli, targetpath)) {
1288 errno = SMBC_errno(context, targetcli);
1290 if (errno == EACCES) { /* Check if the dir empty or not */
1292 /* Local storage to avoid buffer overflows */
1295 smbc_rmdir_dirempty = True; /* Make this so ... */
1297 lpath = talloc_asprintf(frame, "%s\\*",
1305 if (cli_list(targetcli, lpath,
1306 aDIR | aSYSTEM | aHIDDEN,
1307 rmdir_list_fn, NULL) < 0) {
1309 /* Fix errno to ignore latest error ... */
1310 DEBUG(5, ("smbc_rmdir: "
1311 "cli_list returned an error: %d\n",
1312 SMBC_errno(context, targetcli)));
1317 if (smbc_rmdir_dirempty)
1335 * Routine to return the current directory position
1339 SMBC_telldir_ctx(SMBCCTX *context,
1342 TALLOC_CTX *frame = talloc_stackframe();
1344 if (!context || !context->internal->initialized) {
1352 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1360 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1368 /* See if we're already at the end. */
1369 if (dir->dir_next == NULL) {
1376 * We return the pointer here as the offset
1379 return (off_t)(long)dir->dir_next->dirent;
1383 * A routine to run down the list and see if the entry is OK
1386 static struct smbc_dir_list *
1387 check_dir_ent(struct smbc_dir_list *list,
1388 struct smbc_dirent *dirent)
1391 /* Run down the list looking for what we want */
1395 struct smbc_dir_list *tmp = list;
1399 if (tmp->dirent == dirent)
1408 return NULL; /* Not found, or an error */
1414 * Routine to seek on a directory
1418 SMBC_lseekdir_ctx(SMBCCTX *context,
1422 long int l_offset = offset; /* Handle problems of size */
1423 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1424 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1425 TALLOC_CTX *frame = talloc_stackframe();
1427 if (!context || !context->internal->initialized) {
1435 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1443 /* Now, check what we were passed and see if it is OK ... */
1445 if (dirent == NULL) { /* Seek to the begining of the list */
1447 dir->dir_next = dir->dir_list;
1453 if (offset == -1) { /* Seek to the end of the list */
1454 dir->dir_next = NULL;
1459 /* Now, run down the list and make sure that the entry is OK */
1460 /* This may need to be changed if we change the format of the list */
1462 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1463 errno = EINVAL; /* Bad entry */
1468 dir->dir_next = list_ent;
1475 * Routine to fstat a dir
1479 SMBC_fstatdir_ctx(SMBCCTX *context,
1484 if (!context || !context->internal->initialized) {
1490 /* No code yet ... */
1495 SMBC_chmod_ctx(SMBCCTX *context,
1499 SMBCSRV *srv = NULL;
1500 char *server = NULL;
1503 char *password = NULL;
1504 char *workgroup = NULL;
1507 TALLOC_CTX *frame = talloc_stackframe();
1509 if (!context || !context->internal->initialized) {
1511 errno = EINVAL; /* Best I can think of ... */
1522 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
1524 if (SMBC_parse_path(frame,
1539 if (!user || user[0] == (char)0) {
1540 user = talloc_strdup(frame, smbc_getUser(context));
1548 srv = SMBC_server(frame, context, True,
1549 server, share, &workgroup, &user, &password);
1553 return -1; /* errno set by SMBC_server */
1558 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1559 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1560 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1561 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1563 if (!cli_setatr(srv->cli, path, mode, 0)) {
1564 errno = SMBC_errno(context, srv->cli);
1574 SMBC_utimes_ctx(SMBCCTX *context,
1576 struct timeval *tbuf)
1578 SMBCSRV *srv = NULL;
1579 char *server = NULL;
1582 char *password = NULL;
1583 char *workgroup = NULL;
1587 TALLOC_CTX *frame = talloc_stackframe();
1589 if (!context || !context->internal->initialized) {
1591 errno = EINVAL; /* Best I can think of ... */
1603 access_time = write_time = time(NULL);
1605 access_time = tbuf[0].tv_sec;
1606 write_time = tbuf[1].tv_sec;
1614 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1615 atimebuf[sizeof(atimebuf) - 1] = '\0';
1616 if ((p = strchr(atimebuf, '\n')) != NULL) {
1620 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1621 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1622 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1626 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1627 fname, atimebuf, mtimebuf);
1630 if (SMBC_parse_path(frame,
1645 if (!user || user[0] == (char)0) {
1646 user = talloc_strdup(frame, smbc_getUser(context));
1654 srv = SMBC_server(frame, context, True,
1655 server, share, &workgroup, &user, &password);
1659 return -1; /* errno set by SMBC_server */
1662 if (!SMBC_setatr(context, srv, path,
1663 0, access_time, write_time, 0, 0)) {
1665 return -1; /* errno set by SMBC_setatr */
1673 * Routine to unlink() a file
1677 SMBC_unlink_ctx(SMBCCTX *context,
1680 char *server = NULL;
1683 char *password = NULL;
1684 char *workgroup = NULL;
1686 char *targetpath = NULL;
1687 struct cli_state *targetcli = NULL;
1688 SMBCSRV *srv = NULL;
1689 TALLOC_CTX *frame = talloc_stackframe();
1691 if (!context || !context->internal->initialized) {
1693 errno = EINVAL; /* Best I can think of ... */
1706 if (SMBC_parse_path(frame,
1721 if (!user || user[0] == (char)0) {
1722 user = talloc_strdup(frame, smbc_getUser(context));
1730 srv = SMBC_server(frame, context, True,
1731 server, share, &workgroup, &user, &password);
1735 return -1; /* SMBC_server sets errno */
1739 /*d_printf(">>>unlink: resolving %s\n", path);*/
1740 if (!cli_resolve_path(frame, "", srv->cli, path,
1741 &targetcli, &targetpath)) {
1742 d_printf("Could not resolve %s\n", path);
1746 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1748 if (!cli_unlink(targetcli, targetpath)) {
1750 errno = SMBC_errno(context, targetcli);
1752 if (errno == EACCES) { /* Check if the file is a directory */
1757 struct timespec write_time_ts;
1758 struct timespec access_time_ts;
1759 struct timespec change_time_ts;
1762 if (!SMBC_getatr(context, srv, path, &mode, &size,
1769 /* Hmmm, bad error ... What? */
1771 errno = SMBC_errno(context, targetcli);
1778 if (IS_DOS_DIR(mode))
1781 errno = saverr; /* Restore this */
1792 return 0; /* Success ... */
1797 * Routine to rename() a file
1801 SMBC_rename_ctx(SMBCCTX *ocontext,
1806 char *server1 = NULL;
1807 char *share1 = NULL;
1808 char *server2 = NULL;
1809 char *share2 = NULL;
1812 char *password1 = NULL;
1813 char *password2 = NULL;
1814 char *workgroup = NULL;
1817 char *targetpath1 = NULL;
1818 char *targetpath2 = NULL;
1819 struct cli_state *targetcli1 = NULL;
1820 struct cli_state *targetcli2 = NULL;
1821 SMBCSRV *srv = NULL;
1822 TALLOC_CTX *frame = talloc_stackframe();
1824 if (!ocontext || !ncontext ||
1825 !ocontext->internal->initialized ||
1826 !ncontext->internal->initialized) {
1828 errno = EINVAL; /* Best I can think of ... */
1833 if (!oname || !nname) {
1839 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1841 if (SMBC_parse_path(frame,
1856 if (!user1 || user1[0] == (char)0) {
1857 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1865 if (SMBC_parse_path(frame,
1880 if (!user2 || user2[0] == (char)0) {
1881 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1889 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1890 strcmp(user1, user2)) {
1891 /* Can't rename across file systems, or users?? */
1897 srv = SMBC_server(frame, ocontext, True,
1898 server1, share1, &workgroup, &user1, &password1);
1905 /*d_printf(">>>rename: resolving %s\n", path1);*/
1906 if (!cli_resolve_path(frame, "", srv->cli, path1,
1907 &targetcli1, &targetpath1)) {
1908 d_printf("Could not resolve %s\n", path1);
1912 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1913 /*d_printf(">>>rename: resolving %s\n", path2);*/
1914 if (!cli_resolve_path(frame, "", srv->cli, path2,
1915 &targetcli2, &targetpath2)) {
1916 d_printf("Could not resolve %s\n", path2);
1920 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1922 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1923 strcmp(targetcli1->share, targetcli2->share))
1925 /* can't rename across file systems */
1931 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1932 int eno = SMBC_errno(ocontext, targetcli1);
1934 if (eno != EEXIST ||
1935 !cli_unlink(targetcli1, targetpath2) ||
1936 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1946 return 0; /* Success */