1 | /*
|
---|
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
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "includes.h"
|
---|
26 | #include "libsmb/libsmb.h"
|
---|
27 | #include "popt_common.h"
|
---|
28 | #include "libsmbclient.h"
|
---|
29 | #include "libsmb_internal.h"
|
---|
30 | #include "rpc_client/cli_pipe.h"
|
---|
31 | #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
|
---|
32 | #include "libsmb/nmblib.h"
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Routine to open a directory
|
---|
36 | * We accept the URL syntax explained in SMBC_parse_path(), above.
|
---|
37 | */
|
---|
38 |
|
---|
39 | static void
|
---|
40 | remove_dir(SMBCFILE *dir)
|
---|
41 | {
|
---|
42 | struct smbc_dir_list *d,*f;
|
---|
43 |
|
---|
44 | d = dir->dir_list;
|
---|
45 | while (d) {
|
---|
46 |
|
---|
47 | f = d; d = d->next;
|
---|
48 |
|
---|
49 | SAFE_FREE(f->dirent);
|
---|
50 | SAFE_FREE(f);
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 | dir->dir_list = dir->dir_end = dir->dir_next = NULL;
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int
|
---|
59 | add_dirent(SMBCFILE *dir,
|
---|
60 | const char *name,
|
---|
61 | const char *comment,
|
---|
62 | uint32 type)
|
---|
63 | {
|
---|
64 | struct smbc_dirent *dirent;
|
---|
65 | int size;
|
---|
66 | int name_length = (name == NULL ? 0 : strlen(name));
|
---|
67 | int comment_len = (comment == NULL ? 0 : strlen(comment));
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Allocate space for the dirent, which must be increased by the
|
---|
71 | * size of the name and the comment and 1 each for the null terminator.
|
---|
72 | */
|
---|
73 |
|
---|
74 | size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
|
---|
75 |
|
---|
76 | dirent = (struct smbc_dirent *)SMB_MALLOC(size);
|
---|
77 |
|
---|
78 | if (!dirent) {
|
---|
79 |
|
---|
80 | dir->dir_error = ENOMEM;
|
---|
81 | return -1;
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | ZERO_STRUCTP(dirent);
|
---|
86 |
|
---|
87 | if (dir->dir_list == NULL) {
|
---|
88 |
|
---|
89 | dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
|
---|
90 | if (!dir->dir_list) {
|
---|
91 |
|
---|
92 | SAFE_FREE(dirent);
|
---|
93 | dir->dir_error = ENOMEM;
|
---|
94 | return -1;
|
---|
95 |
|
---|
96 | }
|
---|
97 | ZERO_STRUCTP(dir->dir_list);
|
---|
98 |
|
---|
99 | dir->dir_end = dir->dir_next = dir->dir_list;
|
---|
100 | }
|
---|
101 | else {
|
---|
102 |
|
---|
103 | dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
|
---|
104 |
|
---|
105 | if (!dir->dir_end->next) {
|
---|
106 |
|
---|
107 | SAFE_FREE(dirent);
|
---|
108 | dir->dir_error = ENOMEM;
|
---|
109 | return -1;
|
---|
110 |
|
---|
111 | }
|
---|
112 | ZERO_STRUCTP(dir->dir_end->next);
|
---|
113 |
|
---|
114 | dir->dir_end = dir->dir_end->next;
|
---|
115 | }
|
---|
116 |
|
---|
117 | dir->dir_end->next = NULL;
|
---|
118 | dir->dir_end->dirent = dirent;
|
---|
119 |
|
---|
120 | dirent->smbc_type = type;
|
---|
121 | dirent->namelen = name_length;
|
---|
122 | dirent->commentlen = comment_len;
|
---|
123 | dirent->dirlen = size;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * dirent->namelen + 1 includes the null (no null termination needed)
|
---|
127 | * Ditto for dirent->commentlen.
|
---|
128 | * The space for the two null bytes was allocated.
|
---|
129 | */
|
---|
130 | strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
|
---|
131 | dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
|
---|
132 | strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
|
---|
133 |
|
---|
134 | return 0;
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void
|
---|
139 | list_unique_wg_fn(const char *name,
|
---|
140 | uint32 type,
|
---|
141 | const char *comment,
|
---|
142 | void *state)
|
---|
143 | {
|
---|
144 | SMBCFILE *dir = (SMBCFILE *)state;
|
---|
145 | struct smbc_dir_list *dir_list;
|
---|
146 | struct smbc_dirent *dirent;
|
---|
147 | int dirent_type;
|
---|
148 | int do_remove = 0;
|
---|
149 |
|
---|
150 | dirent_type = dir->dir_type;
|
---|
151 |
|
---|
152 | if (add_dirent(dir, name, comment, dirent_type) < 0) {
|
---|
153 | /* An error occurred, what do we do? */
|
---|
154 | /* FIXME: Add some code here */
|
---|
155 | /* Change cli_NetServerEnum to take a fn
|
---|
156 | returning NTSTATUS... JRA. */
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* Point to the one just added */
|
---|
160 | dirent = dir->dir_end->dirent;
|
---|
161 |
|
---|
162 | /* See if this was a duplicate */
|
---|
163 | for (dir_list = dir->dir_list;
|
---|
164 | dir_list != dir->dir_end;
|
---|
165 | dir_list = dir_list->next) {
|
---|
166 | if (! do_remove &&
|
---|
167 | strcmp(dir_list->dirent->name, dirent->name) == 0) {
|
---|
168 | /* Duplicate. End end of list need to be removed. */
|
---|
169 | do_remove = 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (do_remove && dir_list->next == dir->dir_end) {
|
---|
173 | /* Found the end of the list. Remove it. */
|
---|
174 | dir->dir_end = dir_list;
|
---|
175 | free(dir_list->next);
|
---|
176 | free(dirent);
|
---|
177 | dir_list->next = NULL;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | static void
|
---|
184 | list_fn(const char *name,
|
---|
185 | uint32 type,
|
---|
186 | const char *comment,
|
---|
187 | void *state)
|
---|
188 | {
|
---|
189 | SMBCFILE *dir = (SMBCFILE *)state;
|
---|
190 | int dirent_type;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * We need to process the type a little ...
|
---|
194 | *
|
---|
195 | * Disk share = 0x00000000
|
---|
196 | * Print share = 0x00000001
|
---|
197 | * Comms share = 0x00000002 (obsolete?)
|
---|
198 | * IPC$ share = 0x00000003
|
---|
199 | *
|
---|
200 | * administrative shares:
|
---|
201 | * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
|
---|
202 | */
|
---|
203 |
|
---|
204 | if (dir->dir_type == SMBC_FILE_SHARE) {
|
---|
205 | switch (type) {
|
---|
206 | case 0 | 0x80000000:
|
---|
207 | case 0:
|
---|
208 | dirent_type = SMBC_FILE_SHARE;
|
---|
209 | break;
|
---|
210 |
|
---|
211 | case 1:
|
---|
212 | dirent_type = SMBC_PRINTER_SHARE;
|
---|
213 | break;
|
---|
214 |
|
---|
215 | case 2:
|
---|
216 | dirent_type = SMBC_COMMS_SHARE;
|
---|
217 | break;
|
---|
218 |
|
---|
219 | case 3 | 0x80000000:
|
---|
220 | case 3:
|
---|
221 | dirent_type = SMBC_IPC_SHARE;
|
---|
222 | break;
|
---|
223 |
|
---|
224 | default:
|
---|
225 | dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else {
|
---|
230 | dirent_type = dir->dir_type;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (add_dirent(dir, name, comment, dirent_type) < 0) {
|
---|
234 | /* An error occurred, what do we do? */
|
---|
235 | /* FIXME: Add some code here */
|
---|
236 | /* Change cli_NetServerEnum to take a fn
|
---|
237 | returning NTSTATUS... JRA. */
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | static NTSTATUS
|
---|
242 | dir_list_fn(const char *mnt,
|
---|
243 | struct file_info *finfo,
|
---|
244 | const char *mask,
|
---|
245 | void *state)
|
---|
246 | {
|
---|
247 |
|
---|
248 | if (add_dirent((SMBCFILE *)state, finfo->name, "",
|
---|
249 | (finfo->mode&FILE_ATTRIBUTE_DIRECTORY?SMBC_DIR:SMBC_FILE)) < 0) {
|
---|
250 | SMBCFILE *dir = (SMBCFILE *)state;
|
---|
251 | return map_nt_error_from_unix(dir->dir_error);
|
---|
252 | }
|
---|
253 | return NT_STATUS_OK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | static int
|
---|
257 | net_share_enum_rpc(struct cli_state *cli,
|
---|
258 | void (*fn)(const char *name,
|
---|
259 | uint32 type,
|
---|
260 | const char *comment,
|
---|
261 | void *state),
|
---|
262 | void *state)
|
---|
263 | {
|
---|
264 | int i;
|
---|
265 | WERROR result;
|
---|
266 | uint32 preferred_len = 0xffffffff;
|
---|
267 | uint32 type;
|
---|
268 | struct srvsvc_NetShareInfoCtr info_ctr;
|
---|
269 | struct srvsvc_NetShareCtr1 ctr1;
|
---|
270 | fstring name = "";
|
---|
271 | fstring comment = "";
|
---|
272 | struct rpc_pipe_client *pipe_hnd = NULL;
|
---|
273 | NTSTATUS nt_status;
|
---|
274 | uint32_t resume_handle = 0;
|
---|
275 | uint32_t total_entries = 0;
|
---|
276 | struct dcerpc_binding_handle *b;
|
---|
277 |
|
---|
278 | /* Open the server service pipe */
|
---|
279 | nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
|
---|
280 | &pipe_hnd);
|
---|
281 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
282 | DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
|
---|
283 | return -1;
|
---|
284 | }
|
---|
285 |
|
---|
286 | ZERO_STRUCT(info_ctr);
|
---|
287 | ZERO_STRUCT(ctr1);
|
---|
288 |
|
---|
289 | info_ctr.level = 1;
|
---|
290 | info_ctr.ctr.ctr1 = &ctr1;
|
---|
291 |
|
---|
292 | b = pipe_hnd->binding_handle;
|
---|
293 |
|
---|
294 | /* Issue the NetShareEnum RPC call and retrieve the response */
|
---|
295 | nt_status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
|
---|
296 | pipe_hnd->desthost,
|
---|
297 | &info_ctr,
|
---|
298 | preferred_len,
|
---|
299 | &total_entries,
|
---|
300 | &resume_handle,
|
---|
301 | &result);
|
---|
302 |
|
---|
303 | /* Was it successful? */
|
---|
304 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
305 | /* Nope. Go clean up. */
|
---|
306 | result = ntstatus_to_werror(nt_status);
|
---|
307 | goto done;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (!W_ERROR_IS_OK(result)) {
|
---|
311 | /* Nope. Go clean up. */
|
---|
312 | goto done;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (total_entries == 0) {
|
---|
316 | /* Nope. Go clean up. */
|
---|
317 | result = WERR_GENERAL_FAILURE;
|
---|
318 | goto done;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* For each returned entry... */
|
---|
322 | for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
|
---|
323 |
|
---|
324 | /* pull out the share name */
|
---|
325 | fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
|
---|
326 |
|
---|
327 | /* pull out the share's comment */
|
---|
328 | fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
|
---|
329 |
|
---|
330 | /* Get the type value */
|
---|
331 | type = info_ctr.ctr.ctr1->array[i].type;
|
---|
332 |
|
---|
333 | /* Add this share to the list */
|
---|
334 | (*fn)(name, type, comment, state);
|
---|
335 | }
|
---|
336 |
|
---|
337 | done:
|
---|
338 | /* Close the server service pipe */
|
---|
339 | TALLOC_FREE(pipe_hnd);
|
---|
340 |
|
---|
341 | /* Tell 'em if it worked */
|
---|
342 | return W_ERROR_IS_OK(result) ? 0 : -1;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Verify that the options specified in a URL are valid
|
---|
348 | */
|
---|
349 | int
|
---|
350 | SMBC_check_options(char *server,
|
---|
351 | char *share,
|
---|
352 | char *path,
|
---|
353 | char *options)
|
---|
354 | {
|
---|
355 | DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
|
---|
356 | "path='%s' options='%s'\n",
|
---|
357 | server, share, path, options));
|
---|
358 |
|
---|
359 | /* No options at all is always ok */
|
---|
360 | if (! *options) return 0;
|
---|
361 |
|
---|
362 | /* Currently, we don't support any options. */
|
---|
363 | return -1;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | SMBCFILE *
|
---|
368 | SMBC_opendir_ctx(SMBCCTX *context,
|
---|
369 | const char *fname)
|
---|
370 | {
|
---|
371 | int saved_errno;
|
---|
372 | char *server = NULL;
|
---|
373 | char *share = NULL;
|
---|
374 | char *user = NULL;
|
---|
375 | char *password = NULL;
|
---|
376 | char *options = NULL;
|
---|
377 | char *workgroup = NULL;
|
---|
378 | char *path = NULL;
|
---|
379 | uint16 mode;
|
---|
380 | char *p = NULL;
|
---|
381 | SMBCSRV *srv = NULL;
|
---|
382 | SMBCFILE *dir = NULL;
|
---|
383 | struct sockaddr_storage rem_ss;
|
---|
384 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
385 |
|
---|
386 | if (!context || !context->internal->initialized) {
|
---|
387 | DEBUG(4, ("no valid context\n"));
|
---|
388 | TALLOC_FREE(frame);
|
---|
389 | errno = EINVAL + 8192;
|
---|
390 | return NULL;
|
---|
391 |
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (!fname) {
|
---|
395 | DEBUG(4, ("no valid fname\n"));
|
---|
396 | TALLOC_FREE(frame);
|
---|
397 | errno = EINVAL + 8193;
|
---|
398 | return NULL;
|
---|
399 | }
|
---|
400 |
|
---|
401 | if (SMBC_parse_path(frame,
|
---|
402 | context,
|
---|
403 | fname,
|
---|
404 | &workgroup,
|
---|
405 | &server,
|
---|
406 | &share,
|
---|
407 | &path,
|
---|
408 | &user,
|
---|
409 | &password,
|
---|
410 | &options)) {
|
---|
411 | DEBUG(4, ("no valid path\n"));
|
---|
412 | TALLOC_FREE(frame);
|
---|
413 | errno = EINVAL + 8194;
|
---|
414 | return NULL;
|
---|
415 | }
|
---|
416 |
|
---|
417 | DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
|
---|
418 | "path='%s' options='%s'\n",
|
---|
419 | fname, server, share, path, options));
|
---|
420 |
|
---|
421 | /* Ensure the options are valid */
|
---|
422 | if (SMBC_check_options(server, share, path, options)) {
|
---|
423 | DEBUG(4, ("unacceptable options (%s)\n", options));
|
---|
424 | TALLOC_FREE(frame);
|
---|
425 | errno = EINVAL + 8195;
|
---|
426 | return NULL;
|
---|
427 | }
|
---|
428 |
|
---|
429 | if (!user || user[0] == (char)0) {
|
---|
430 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
431 | if (!user) {
|
---|
432 | TALLOC_FREE(frame);
|
---|
433 | errno = ENOMEM;
|
---|
434 | return NULL;
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | dir = SMB_MALLOC_P(SMBCFILE);
|
---|
439 |
|
---|
440 | if (!dir) {
|
---|
441 | TALLOC_FREE(frame);
|
---|
442 | errno = ENOMEM;
|
---|
443 | return NULL;
|
---|
444 | }
|
---|
445 |
|
---|
446 | ZERO_STRUCTP(dir);
|
---|
447 |
|
---|
448 | dir->cli_fd = 0;
|
---|
449 | dir->fname = SMB_STRDUP(fname);
|
---|
450 | dir->srv = NULL;
|
---|
451 | dir->offset = 0;
|
---|
452 | dir->file = False;
|
---|
453 | dir->dir_list = dir->dir_next = dir->dir_end = NULL;
|
---|
454 |
|
---|
455 | if (server[0] == (char)0) {
|
---|
456 |
|
---|
457 | int i;
|
---|
458 | int count;
|
---|
459 | int max_lmb_count;
|
---|
460 | struct sockaddr_storage *ip_list;
|
---|
461 | struct sockaddr_storage server_addr;
|
---|
462 | struct user_auth_info u_info;
|
---|
463 | NTSTATUS status;
|
---|
464 |
|
---|
465 | if (share[0] != (char)0 || path[0] != (char)0) {
|
---|
466 |
|
---|
467 | if (dir) {
|
---|
468 | SAFE_FREE(dir->fname);
|
---|
469 | SAFE_FREE(dir);
|
---|
470 | }
|
---|
471 | TALLOC_FREE(frame);
|
---|
472 | errno = EINVAL + 8196;
|
---|
473 | return NULL;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /* Determine how many local master browsers to query */
|
---|
477 | max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
|
---|
478 | ? INT_MAX
|
---|
479 | : smbc_getOptionBrowseMaxLmbCount(context));
|
---|
480 |
|
---|
481 | memset(&u_info, '\0', sizeof(u_info));
|
---|
482 | u_info.username = talloc_strdup(frame,user);
|
---|
483 | u_info.password = talloc_strdup(frame,password);
|
---|
484 | if (!u_info.username || !u_info.password) {
|
---|
485 | if (dir) {
|
---|
486 | SAFE_FREE(dir->fname);
|
---|
487 | SAFE_FREE(dir);
|
---|
488 | }
|
---|
489 | TALLOC_FREE(frame);
|
---|
490 | return NULL;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * We have server and share and path empty but options
|
---|
495 | * requesting that we scan all master browsers for their list
|
---|
496 | * of workgroups/domains. This implies that we must first try
|
---|
497 | * broadcast queries to find all master browsers, and if that
|
---|
498 | * doesn't work, then try our other methods which return only
|
---|
499 | * a single master browser.
|
---|
500 | */
|
---|
501 |
|
---|
502 | ip_list = NULL;
|
---|
503 | status = name_resolve_bcast(MSBROWSE, 1, talloc_tos(),
|
---|
504 | &ip_list, &count);
|
---|
505 | if (!NT_STATUS_IS_OK(status))
|
---|
506 | {
|
---|
507 |
|
---|
508 | TALLOC_FREE(ip_list);
|
---|
509 |
|
---|
510 | if (!find_master_ip(workgroup, &server_addr)) {
|
---|
511 |
|
---|
512 | if (dir) {
|
---|
513 | SAFE_FREE(dir->fname);
|
---|
514 | SAFE_FREE(dir);
|
---|
515 | }
|
---|
516 | TALLOC_FREE(frame);
|
---|
517 | errno = ENOENT;
|
---|
518 | return NULL;
|
---|
519 | }
|
---|
520 |
|
---|
521 | ip_list = (struct sockaddr_storage *)talloc_memdup(
|
---|
522 | talloc_tos(), &server_addr,
|
---|
523 | sizeof(server_addr));
|
---|
524 | if (ip_list == NULL) {
|
---|
525 | if (dir) {
|
---|
526 | SAFE_FREE(dir->fname);
|
---|
527 | SAFE_FREE(dir);
|
---|
528 | }
|
---|
529 | TALLOC_FREE(frame);
|
---|
530 | errno = ENOMEM;
|
---|
531 | return NULL;
|
---|
532 | }
|
---|
533 | count = 1;
|
---|
534 | }
|
---|
535 |
|
---|
536 | for (i = 0; i < count && i < max_lmb_count; i++) {
|
---|
537 | char addr[INET6_ADDRSTRLEN];
|
---|
538 | char *wg_ptr = NULL;
|
---|
539 | struct cli_state *cli = NULL;
|
---|
540 |
|
---|
541 | print_sockaddr(addr, sizeof(addr), &ip_list[i]);
|
---|
542 | DEBUG(99, ("Found master browser %d of %d: %s\n",
|
---|
543 | i+1, MAX(count, max_lmb_count),
|
---|
544 | addr));
|
---|
545 |
|
---|
546 | cli = get_ipc_connect_master_ip(talloc_tos(),
|
---|
547 | &ip_list[i],
|
---|
548 | &u_info,
|
---|
549 | &wg_ptr);
|
---|
550 | /* cli == NULL is the master browser refused to talk or
|
---|
551 | could not be found */
|
---|
552 | if (!cli) {
|
---|
553 | continue;
|
---|
554 | }
|
---|
555 |
|
---|
556 | workgroup = talloc_strdup(frame, wg_ptr);
|
---|
557 | server = talloc_strdup(frame, cli->desthost);
|
---|
558 |
|
---|
559 | cli_shutdown(cli);
|
---|
560 |
|
---|
561 | if (!workgroup || !server) {
|
---|
562 | if (dir) {
|
---|
563 | SAFE_FREE(dir->fname);
|
---|
564 | SAFE_FREE(dir);
|
---|
565 | }
|
---|
566 | TALLOC_FREE(frame);
|
---|
567 | errno = ENOMEM;
|
---|
568 | return NULL;
|
---|
569 | }
|
---|
570 |
|
---|
571 | DEBUG(4, ("using workgroup %s %s\n",
|
---|
572 | workgroup, server));
|
---|
573 |
|
---|
574 | /*
|
---|
575 | * For each returned master browser IP address, get a
|
---|
576 | * connection to IPC$ on the server if we do not
|
---|
577 | * already have one, and determine the
|
---|
578 | * workgroups/domains that it knows about.
|
---|
579 | */
|
---|
580 |
|
---|
581 | srv = SMBC_server(frame, context, True, server, "IPC$",
|
---|
582 | &workgroup, &user, &password);
|
---|
583 | if (!srv) {
|
---|
584 | continue;
|
---|
585 | }
|
---|
586 |
|
---|
587 | dir->srv = srv;
|
---|
588 | dir->dir_type = SMBC_WORKGROUP;
|
---|
589 |
|
---|
590 | /* Now, list the stuff ... */
|
---|
591 |
|
---|
592 | if (!cli_NetServerEnum(srv->cli,
|
---|
593 | workgroup,
|
---|
594 | SV_TYPE_DOMAIN_ENUM,
|
---|
595 | list_unique_wg_fn,
|
---|
596 | (void *)dir)) {
|
---|
597 | continue;
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | TALLOC_FREE(ip_list);
|
---|
602 | } else {
|
---|
603 | /*
|
---|
604 | * Server not an empty string ... Check the rest and see what
|
---|
605 | * gives
|
---|
606 | */
|
---|
607 | if (*share == '\0') {
|
---|
608 | if (*path != '\0') {
|
---|
609 |
|
---|
610 | /* Should not have empty share with path */
|
---|
611 | if (dir) {
|
---|
612 | SAFE_FREE(dir->fname);
|
---|
613 | SAFE_FREE(dir);
|
---|
614 | }
|
---|
615 | TALLOC_FREE(frame);
|
---|
616 | errno = EINVAL + 8197;
|
---|
617 | return NULL;
|
---|
618 |
|
---|
619 | }
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * We don't know if <server> is really a server name
|
---|
623 | * or is a workgroup/domain name. If we already have
|
---|
624 | * a server structure for it, we'll use it.
|
---|
625 | * Otherwise, check to see if <server><1D>,
|
---|
626 | * <server><1B>, or <server><20> translates. We check
|
---|
627 | * to see if <server> is an IP address first.
|
---|
628 | */
|
---|
629 |
|
---|
630 | /*
|
---|
631 | * See if we have an existing server. Do not
|
---|
632 | * establish a connection if one does not already
|
---|
633 | * exist.
|
---|
634 | */
|
---|
635 | srv = SMBC_server(frame, context, False,
|
---|
636 | server, "IPC$",
|
---|
637 | &workgroup, &user, &password);
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * If no existing server and not an IP addr, look for
|
---|
641 | * LMB or DMB
|
---|
642 | */
|
---|
643 | if (!srv &&
|
---|
644 | !is_ipaddress(server) &&
|
---|
645 | (resolve_name(server, &rem_ss, 0x1d, false) || /* LMB */
|
---|
646 | resolve_name(server, &rem_ss, 0x1b, false) )) { /* DMB */
|
---|
647 | /*
|
---|
648 | * "server" is actually a workgroup name,
|
---|
649 | * not a server. Make this clear.
|
---|
650 | */
|
---|
651 | char *wgroup = server;
|
---|
652 | fstring buserver;
|
---|
653 |
|
---|
654 | dir->dir_type = SMBC_SERVER;
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Get the backup list ...
|
---|
658 | */
|
---|
659 | if (!name_status_find(wgroup, 0, 0,
|
---|
660 | &rem_ss, buserver)) {
|
---|
661 | char addr[INET6_ADDRSTRLEN];
|
---|
662 |
|
---|
663 | print_sockaddr(addr, sizeof(addr), &rem_ss);
|
---|
664 | DEBUG(0,("Could not get name of "
|
---|
665 | "local/domain master browser "
|
---|
666 | "for workgroup %s from "
|
---|
667 | "address %s\n",
|
---|
668 | wgroup,
|
---|
669 | addr));
|
---|
670 | if (dir) {
|
---|
671 | SAFE_FREE(dir->fname);
|
---|
672 | SAFE_FREE(dir);
|
---|
673 | }
|
---|
674 | TALLOC_FREE(frame);
|
---|
675 | errno = EPERM;
|
---|
676 | return NULL;
|
---|
677 |
|
---|
678 | }
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * Get a connection to IPC$ on the server if
|
---|
682 | * we do not already have one
|
---|
683 | */
|
---|
684 | srv = SMBC_server(frame, context, True,
|
---|
685 | buserver, "IPC$",
|
---|
686 | &workgroup,
|
---|
687 | &user, &password);
|
---|
688 | if (!srv) {
|
---|
689 | DEBUG(0, ("got no contact to IPC$\n"));
|
---|
690 | if (dir) {
|
---|
691 | SAFE_FREE(dir->fname);
|
---|
692 | SAFE_FREE(dir);
|
---|
693 | }
|
---|
694 | TALLOC_FREE(frame);
|
---|
695 | return NULL;
|
---|
696 |
|
---|
697 | }
|
---|
698 |
|
---|
699 | dir->srv = srv;
|
---|
700 |
|
---|
701 | /* Now, list the servers ... */
|
---|
702 | if (!cli_NetServerEnum(srv->cli, wgroup,
|
---|
703 | 0x0000FFFE, list_fn,
|
---|
704 | (void *)dir)) {
|
---|
705 |
|
---|
706 | if (dir) {
|
---|
707 | SAFE_FREE(dir->fname);
|
---|
708 | SAFE_FREE(dir);
|
---|
709 | }
|
---|
710 | TALLOC_FREE(frame);
|
---|
711 | return NULL;
|
---|
712 | }
|
---|
713 | } else if (srv ||
|
---|
714 | (resolve_name(server, &rem_ss, 0x20, false))) {
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * If we hadn't found the server, get one now
|
---|
718 | */
|
---|
719 | if (!srv) {
|
---|
720 | srv = SMBC_server(frame, context, True,
|
---|
721 | server, "IPC$",
|
---|
722 | &workgroup,
|
---|
723 | &user, &password);
|
---|
724 | }
|
---|
725 |
|
---|
726 | if (!srv) {
|
---|
727 | if (dir) {
|
---|
728 | SAFE_FREE(dir->fname);
|
---|
729 | SAFE_FREE(dir);
|
---|
730 | }
|
---|
731 | TALLOC_FREE(frame);
|
---|
732 | return NULL;
|
---|
733 |
|
---|
734 | }
|
---|
735 |
|
---|
736 | dir->dir_type = SMBC_FILE_SHARE;
|
---|
737 | dir->srv = srv;
|
---|
738 |
|
---|
739 | /* List the shares ... */
|
---|
740 |
|
---|
741 | if (net_share_enum_rpc(
|
---|
742 | srv->cli,
|
---|
743 | list_fn,
|
---|
744 | (void *) dir) < 0 &&
|
---|
745 | cli_RNetShareEnum(
|
---|
746 | srv->cli,
|
---|
747 | list_fn,
|
---|
748 | (void *)dir) < 0) {
|
---|
749 |
|
---|
750 | errno = cli_errno(srv->cli);
|
---|
751 | if (dir) {
|
---|
752 | SAFE_FREE(dir->fname);
|
---|
753 | SAFE_FREE(dir);
|
---|
754 | }
|
---|
755 | TALLOC_FREE(frame);
|
---|
756 | return NULL;
|
---|
757 |
|
---|
758 | }
|
---|
759 | } else {
|
---|
760 | /* Neither the workgroup nor server exists */
|
---|
761 | errno = ECONNREFUSED;
|
---|
762 | if (dir) {
|
---|
763 | SAFE_FREE(dir->fname);
|
---|
764 | SAFE_FREE(dir);
|
---|
765 | }
|
---|
766 | TALLOC_FREE(frame);
|
---|
767 | return NULL;
|
---|
768 | }
|
---|
769 |
|
---|
770 | }
|
---|
771 | else {
|
---|
772 | /*
|
---|
773 | * The server and share are specified ... work from
|
---|
774 | * there ...
|
---|
775 | */
|
---|
776 | char *targetpath;
|
---|
777 | struct cli_state *targetcli;
|
---|
778 | NTSTATUS status;
|
---|
779 |
|
---|
780 | /* We connect to the server and list the directory */
|
---|
781 | dir->dir_type = SMBC_FILE_SHARE;
|
---|
782 |
|
---|
783 | srv = SMBC_server(frame, context, True, server, share,
|
---|
784 | &workgroup, &user, &password);
|
---|
785 |
|
---|
786 | if (!srv) {
|
---|
787 | if (dir) {
|
---|
788 | SAFE_FREE(dir->fname);
|
---|
789 | SAFE_FREE(dir);
|
---|
790 | }
|
---|
791 | TALLOC_FREE(frame);
|
---|
792 | return NULL;
|
---|
793 | }
|
---|
794 |
|
---|
795 | dir->srv = srv;
|
---|
796 |
|
---|
797 | /* Now, list the files ... */
|
---|
798 |
|
---|
799 | p = path + strlen(path);
|
---|
800 | path = talloc_asprintf_append(path, "\\*");
|
---|
801 | if (!path) {
|
---|
802 | if (dir) {
|
---|
803 | SAFE_FREE(dir->fname);
|
---|
804 | SAFE_FREE(dir);
|
---|
805 | }
|
---|
806 | TALLOC_FREE(frame);
|
---|
807 | return NULL;
|
---|
808 | }
|
---|
809 |
|
---|
810 | if (!cli_resolve_path(frame, "", context->internal->auth_info,
|
---|
811 | srv->cli, path,
|
---|
812 | &targetcli, &targetpath)) {
|
---|
813 | d_printf("Could not resolve %s\n", path);
|
---|
814 | if (dir) {
|
---|
815 | SAFE_FREE(dir->fname);
|
---|
816 | SAFE_FREE(dir);
|
---|
817 | }
|
---|
818 | TALLOC_FREE(frame);
|
---|
819 | return NULL;
|
---|
820 | }
|
---|
821 |
|
---|
822 | status = cli_list(targetcli, targetpath,
|
---|
823 | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
|
---|
824 | dir_list_fn, (void *)dir);
|
---|
825 | if (!NT_STATUS_IS_OK(status)) {
|
---|
826 | if (dir) {
|
---|
827 | SAFE_FREE(dir->fname);
|
---|
828 | SAFE_FREE(dir);
|
---|
829 | }
|
---|
830 | saved_errno = SMBC_errno(context, targetcli);
|
---|
831 |
|
---|
832 | if (saved_errno == EINVAL) {
|
---|
833 | /*
|
---|
834 | * See if they asked to opendir
|
---|
835 | * something other than a directory.
|
---|
836 | * If so, the converted error value we
|
---|
837 | * got would have been EINVAL rather
|
---|
838 | * than ENOTDIR.
|
---|
839 | */
|
---|
840 | *p = '\0'; /* restore original path */
|
---|
841 |
|
---|
842 | if (SMBC_getatr(context, srv, path,
|
---|
843 | &mode, NULL,
|
---|
844 | NULL, NULL, NULL, NULL,
|
---|
845 | NULL) &&
|
---|
846 | ! IS_DOS_DIR(mode)) {
|
---|
847 |
|
---|
848 | /* It is. Correct the error value */
|
---|
849 | saved_errno = ENOTDIR;
|
---|
850 | }
|
---|
851 | }
|
---|
852 |
|
---|
853 | /*
|
---|
854 | * If there was an error and the server is no
|
---|
855 | * good any more...
|
---|
856 | */
|
---|
857 | if (cli_is_error(targetcli) &&
|
---|
858 | smbc_getFunctionCheckServer(context)(context, srv)) {
|
---|
859 |
|
---|
860 | /* ... then remove it. */
|
---|
861 | if (smbc_getFunctionRemoveUnusedServer(context)(context,
|
---|
862 | srv)) {
|
---|
863 | /*
|
---|
864 | * We could not remove the
|
---|
865 | * server completely, remove
|
---|
866 | * it from the cache so we
|
---|
867 | * will not get it again. It
|
---|
868 | * will be removed when the
|
---|
869 | * last file/dir is closed.
|
---|
870 | */
|
---|
871 | smbc_getFunctionRemoveCachedServer(context)(context, srv);
|
---|
872 | }
|
---|
873 | }
|
---|
874 |
|
---|
875 | TALLOC_FREE(frame);
|
---|
876 | errno = saved_errno;
|
---|
877 | return NULL;
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | }
|
---|
882 |
|
---|
883 | DLIST_ADD(context->internal->files, dir);
|
---|
884 | TALLOC_FREE(frame);
|
---|
885 | return dir;
|
---|
886 |
|
---|
887 | }
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * Routine to close a directory
|
---|
891 | */
|
---|
892 |
|
---|
893 | int
|
---|
894 | SMBC_closedir_ctx(SMBCCTX *context,
|
---|
895 | SMBCFILE *dir)
|
---|
896 | {
|
---|
897 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
898 |
|
---|
899 | if (!context || !context->internal->initialized) {
|
---|
900 | errno = EINVAL;
|
---|
901 | TALLOC_FREE(frame);
|
---|
902 | return -1;
|
---|
903 | }
|
---|
904 |
|
---|
905 | if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
|
---|
906 | errno = EBADF;
|
---|
907 | TALLOC_FREE(frame);
|
---|
908 | return -1;
|
---|
909 | }
|
---|
910 |
|
---|
911 | remove_dir(dir); /* Clean it up */
|
---|
912 |
|
---|
913 | DLIST_REMOVE(context->internal->files, dir);
|
---|
914 |
|
---|
915 | if (dir) {
|
---|
916 |
|
---|
917 | SAFE_FREE(dir->fname);
|
---|
918 | SAFE_FREE(dir); /* Free the space too */
|
---|
919 | }
|
---|
920 |
|
---|
921 | TALLOC_FREE(frame);
|
---|
922 | return 0;
|
---|
923 |
|
---|
924 | }
|
---|
925 |
|
---|
926 | static void
|
---|
927 | smbc_readdir_internal(SMBCCTX * context,
|
---|
928 | struct smbc_dirent *dest,
|
---|
929 | struct smbc_dirent *src,
|
---|
930 | int max_namebuf_len)
|
---|
931 | {
|
---|
932 | if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
|
---|
933 |
|
---|
934 | /* url-encode the name. get back remaining buffer space */
|
---|
935 | max_namebuf_len =
|
---|
936 | smbc_urlencode(dest->name, src->name, max_namebuf_len);
|
---|
937 |
|
---|
938 | /* We now know the name length */
|
---|
939 | dest->namelen = strlen(dest->name);
|
---|
940 |
|
---|
941 | /* Save the pointer to the beginning of the comment */
|
---|
942 | dest->comment = dest->name + dest->namelen + 1;
|
---|
943 |
|
---|
944 | /* Copy the comment */
|
---|
945 | strncpy(dest->comment, src->comment, max_namebuf_len - 1);
|
---|
946 | dest->comment[max_namebuf_len - 1] = '\0';
|
---|
947 |
|
---|
948 | /* Save other fields */
|
---|
949 | dest->smbc_type = src->smbc_type;
|
---|
950 | dest->commentlen = strlen(dest->comment);
|
---|
951 | dest->dirlen = ((dest->comment + dest->commentlen + 1) -
|
---|
952 | (char *) dest);
|
---|
953 | } else {
|
---|
954 |
|
---|
955 | /* No encoding. Just copy the entry as is. */
|
---|
956 | memcpy(dest, src, src->dirlen);
|
---|
957 | dest->comment = (char *)(&dest->name + src->namelen + 1);
|
---|
958 | }
|
---|
959 |
|
---|
960 | }
|
---|
961 |
|
---|
962 | /*
|
---|
963 | * Routine to get a directory entry
|
---|
964 | */
|
---|
965 |
|
---|
966 | struct smbc_dirent *
|
---|
967 | SMBC_readdir_ctx(SMBCCTX *context,
|
---|
968 | SMBCFILE *dir)
|
---|
969 | {
|
---|
970 | int maxlen;
|
---|
971 | struct smbc_dirent *dirp, *dirent;
|
---|
972 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
973 |
|
---|
974 | /* Check that all is ok first ... */
|
---|
975 |
|
---|
976 | if (!context || !context->internal->initialized) {
|
---|
977 |
|
---|
978 | errno = EINVAL;
|
---|
979 | DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
|
---|
980 | TALLOC_FREE(frame);
|
---|
981 | return NULL;
|
---|
982 |
|
---|
983 | }
|
---|
984 |
|
---|
985 | if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
|
---|
986 |
|
---|
987 | errno = EBADF;
|
---|
988 | DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
|
---|
989 | TALLOC_FREE(frame);
|
---|
990 | return NULL;
|
---|
991 |
|
---|
992 | }
|
---|
993 |
|
---|
994 | if (dir->file != False) { /* FIXME, should be dir, perhaps */
|
---|
995 |
|
---|
996 | errno = ENOTDIR;
|
---|
997 | DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
|
---|
998 | TALLOC_FREE(frame);
|
---|
999 | return NULL;
|
---|
1000 |
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | if (!dir->dir_next) {
|
---|
1004 | TALLOC_FREE(frame);
|
---|
1005 | return NULL;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | dirent = dir->dir_next->dirent;
|
---|
1009 | if (!dirent) {
|
---|
1010 |
|
---|
1011 | errno = ENOENT;
|
---|
1012 | TALLOC_FREE(frame);
|
---|
1013 | return NULL;
|
---|
1014 |
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | dirp = &context->internal->dirent;
|
---|
1018 | maxlen = sizeof(context->internal->_dirent_name);
|
---|
1019 |
|
---|
1020 | smbc_readdir_internal(context, dirp, dirent, maxlen);
|
---|
1021 |
|
---|
1022 | dir->dir_next = dir->dir_next->next;
|
---|
1023 |
|
---|
1024 | TALLOC_FREE(frame);
|
---|
1025 | return dirp;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | /*
|
---|
1029 | * Routine to get directory entries
|
---|
1030 | */
|
---|
1031 |
|
---|
1032 | int
|
---|
1033 | SMBC_getdents_ctx(SMBCCTX *context,
|
---|
1034 | SMBCFILE *dir,
|
---|
1035 | struct smbc_dirent *dirp,
|
---|
1036 | int count)
|
---|
1037 | {
|
---|
1038 | int rem = count;
|
---|
1039 | int reqd;
|
---|
1040 | int maxlen;
|
---|
1041 | char *ndir = (char *)dirp;
|
---|
1042 | struct smbc_dir_list *dirlist;
|
---|
1043 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1044 |
|
---|
1045 | /* Check that all is ok first ... */
|
---|
1046 |
|
---|
1047 | if (!context || !context->internal->initialized) {
|
---|
1048 |
|
---|
1049 | errno = EINVAL;
|
---|
1050 | TALLOC_FREE(frame);
|
---|
1051 | return -1;
|
---|
1052 |
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
|
---|
1056 |
|
---|
1057 | errno = EBADF;
|
---|
1058 | TALLOC_FREE(frame);
|
---|
1059 | return -1;
|
---|
1060 |
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if (dir->file != False) { /* FIXME, should be dir, perhaps */
|
---|
1064 |
|
---|
1065 | errno = ENOTDIR;
|
---|
1066 | TALLOC_FREE(frame);
|
---|
1067 | return -1;
|
---|
1068 |
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /*
|
---|
1072 | * Now, retrieve the number of entries that will fit in what was passed
|
---|
1073 | * We have to figure out if the info is in the list, or we need to
|
---|
1074 | * send a request to the server to get the info.
|
---|
1075 | */
|
---|
1076 |
|
---|
1077 | while ((dirlist = dir->dir_next)) {
|
---|
1078 | struct smbc_dirent *dirent;
|
---|
1079 | struct smbc_dirent *currentEntry = (struct smbc_dirent *)ndir;
|
---|
1080 |
|
---|
1081 | if (!dirlist->dirent) {
|
---|
1082 |
|
---|
1083 | errno = ENOENT; /* Bad error */
|
---|
1084 | TALLOC_FREE(frame);
|
---|
1085 | return -1;
|
---|
1086 |
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | /* Do urlencoding of next entry, if so selected */
|
---|
1090 | dirent = &context->internal->dirent;
|
---|
1091 | maxlen = sizeof(context->internal->_dirent_name);
|
---|
1092 | smbc_readdir_internal(context, dirent,
|
---|
1093 | dirlist->dirent, maxlen);
|
---|
1094 |
|
---|
1095 | reqd = dirent->dirlen;
|
---|
1096 |
|
---|
1097 | if (rem < reqd) {
|
---|
1098 |
|
---|
1099 | if (rem < count) { /* We managed to copy something */
|
---|
1100 |
|
---|
1101 | errno = 0;
|
---|
1102 | TALLOC_FREE(frame);
|
---|
1103 | return count - rem;
|
---|
1104 |
|
---|
1105 | }
|
---|
1106 | else { /* Nothing copied ... */
|
---|
1107 |
|
---|
1108 | errno = EINVAL; /* Not enough space ... */
|
---|
1109 | TALLOC_FREE(frame);
|
---|
1110 | return -1;
|
---|
1111 |
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | memcpy(currentEntry, dirent, reqd); /* Copy the data in ... */
|
---|
1117 |
|
---|
1118 | currentEntry->comment = ¤tEntry->name[0] +
|
---|
1119 | dirent->namelen + 1;
|
---|
1120 |
|
---|
1121 | ndir += reqd;
|
---|
1122 | rem -= reqd;
|
---|
1123 |
|
---|
1124 | /* Try and align the struct for the next entry
|
---|
1125 | on a valid pointer boundary by appending zeros */
|
---|
1126 | while((rem > 0) && ((unsigned long long)ndir & (sizeof(void*) - 1))) {
|
---|
1127 | *ndir = '\0';
|
---|
1128 | rem--;
|
---|
1129 | ndir++;
|
---|
1130 | currentEntry->dirlen++;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | dir->dir_next = dirlist = dirlist -> next;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | TALLOC_FREE(frame);
|
---|
1137 |
|
---|
1138 | if (rem == count)
|
---|
1139 | return 0;
|
---|
1140 | else
|
---|
1141 | return count - rem;
|
---|
1142 |
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | /*
|
---|
1146 | * Routine to create a directory ...
|
---|
1147 | */
|
---|
1148 |
|
---|
1149 | int
|
---|
1150 | SMBC_mkdir_ctx(SMBCCTX *context,
|
---|
1151 | const char *fname,
|
---|
1152 | mode_t mode)
|
---|
1153 | {
|
---|
1154 | SMBCSRV *srv = NULL;
|
---|
1155 | char *server = NULL;
|
---|
1156 | char *share = NULL;
|
---|
1157 | char *user = NULL;
|
---|
1158 | char *password = NULL;
|
---|
1159 | char *workgroup = NULL;
|
---|
1160 | char *path = NULL;
|
---|
1161 | char *targetpath = NULL;
|
---|
1162 | struct cli_state *targetcli = NULL;
|
---|
1163 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1164 |
|
---|
1165 | if (!context || !context->internal->initialized) {
|
---|
1166 | errno = EINVAL;
|
---|
1167 | TALLOC_FREE(frame);
|
---|
1168 | return -1;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | if (!fname) {
|
---|
1172 | errno = EINVAL;
|
---|
1173 | TALLOC_FREE(frame);
|
---|
1174 | return -1;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | DEBUG(4, ("smbc_mkdir(%s)\n", fname));
|
---|
1178 |
|
---|
1179 | if (SMBC_parse_path(frame,
|
---|
1180 | context,
|
---|
1181 | fname,
|
---|
1182 | &workgroup,
|
---|
1183 | &server,
|
---|
1184 | &share,
|
---|
1185 | &path,
|
---|
1186 | &user,
|
---|
1187 | &password,
|
---|
1188 | NULL)) {
|
---|
1189 | errno = EINVAL;
|
---|
1190 | TALLOC_FREE(frame);
|
---|
1191 | return -1;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | if (!user || user[0] == (char)0) {
|
---|
1195 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
1196 | if (!user) {
|
---|
1197 | errno = ENOMEM;
|
---|
1198 | TALLOC_FREE(frame);
|
---|
1199 | return -1;
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | srv = SMBC_server(frame, context, True,
|
---|
1204 | server, share, &workgroup, &user, &password);
|
---|
1205 |
|
---|
1206 | if (!srv) {
|
---|
1207 |
|
---|
1208 | TALLOC_FREE(frame);
|
---|
1209 | return -1; /* errno set by SMBC_server */
|
---|
1210 |
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | /*d_printf(">>>mkdir: resolving %s\n", path);*/
|
---|
1214 | if (!cli_resolve_path(frame, "", context->internal->auth_info,
|
---|
1215 | srv->cli, path,
|
---|
1216 | &targetcli, &targetpath)) {
|
---|
1217 | d_printf("Could not resolve %s\n", path);
|
---|
1218 | errno = ENOENT;
|
---|
1219 | TALLOC_FREE(frame);
|
---|
1220 | return -1;
|
---|
1221 | }
|
---|
1222 | /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
|
---|
1223 |
|
---|
1224 | if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
|
---|
1225 | errno = SMBC_errno(context, targetcli);
|
---|
1226 | TALLOC_FREE(frame);
|
---|
1227 | return -1;
|
---|
1228 |
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | TALLOC_FREE(frame);
|
---|
1232 | return 0;
|
---|
1233 |
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /*
|
---|
1237 | * Our list function simply checks to see if a directory is not empty
|
---|
1238 | */
|
---|
1239 |
|
---|
1240 | static NTSTATUS
|
---|
1241 | rmdir_list_fn(const char *mnt,
|
---|
1242 | struct file_info *finfo,
|
---|
1243 | const char *mask,
|
---|
1244 | void *state)
|
---|
1245 | {
|
---|
1246 | if (strncmp(finfo->name, ".", 1) != 0 &&
|
---|
1247 | strncmp(finfo->name, "..", 2) != 0) {
|
---|
1248 | bool *smbc_rmdir_dirempty = (bool *)state;
|
---|
1249 | *smbc_rmdir_dirempty = false;
|
---|
1250 | }
|
---|
1251 | return NT_STATUS_OK;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | /*
|
---|
1255 | * Routine to remove a directory
|
---|
1256 | */
|
---|
1257 |
|
---|
1258 | int
|
---|
1259 | SMBC_rmdir_ctx(SMBCCTX *context,
|
---|
1260 | const char *fname)
|
---|
1261 | {
|
---|
1262 | SMBCSRV *srv = NULL;
|
---|
1263 | char *server = NULL;
|
---|
1264 | char *share = NULL;
|
---|
1265 | char *user = NULL;
|
---|
1266 | char *password = NULL;
|
---|
1267 | char *workgroup = NULL;
|
---|
1268 | char *path = NULL;
|
---|
1269 | char *targetpath = NULL;
|
---|
1270 | struct cli_state *targetcli = NULL;
|
---|
1271 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1272 |
|
---|
1273 | if (!context || !context->internal->initialized) {
|
---|
1274 | errno = EINVAL;
|
---|
1275 | TALLOC_FREE(frame);
|
---|
1276 | return -1;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | if (!fname) {
|
---|
1280 | errno = EINVAL;
|
---|
1281 | TALLOC_FREE(frame);
|
---|
1282 | return -1;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | DEBUG(4, ("smbc_rmdir(%s)\n", fname));
|
---|
1286 |
|
---|
1287 | if (SMBC_parse_path(frame,
|
---|
1288 | context,
|
---|
1289 | fname,
|
---|
1290 | &workgroup,
|
---|
1291 | &server,
|
---|
1292 | &share,
|
---|
1293 | &path,
|
---|
1294 | &user,
|
---|
1295 | &password,
|
---|
1296 | NULL)) {
|
---|
1297 | errno = EINVAL;
|
---|
1298 | TALLOC_FREE(frame);
|
---|
1299 | return -1;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | if (!user || user[0] == (char)0) {
|
---|
1303 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
1304 | if (!user) {
|
---|
1305 | errno = ENOMEM;
|
---|
1306 | TALLOC_FREE(frame);
|
---|
1307 | return -1;
|
---|
1308 | }
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | srv = SMBC_server(frame, context, True,
|
---|
1312 | server, share, &workgroup, &user, &password);
|
---|
1313 |
|
---|
1314 | if (!srv) {
|
---|
1315 |
|
---|
1316 | TALLOC_FREE(frame);
|
---|
1317 | return -1; /* errno set by SMBC_server */
|
---|
1318 |
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /*d_printf(">>>rmdir: resolving %s\n", path);*/
|
---|
1322 | if (!cli_resolve_path(frame, "", context->internal->auth_info,
|
---|
1323 | srv->cli, path,
|
---|
1324 | &targetcli, &targetpath)) {
|
---|
1325 | d_printf("Could not resolve %s\n", path);
|
---|
1326 | errno = ENOENT;
|
---|
1327 | TALLOC_FREE(frame);
|
---|
1328 | return -1;
|
---|
1329 | }
|
---|
1330 | /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
|
---|
1331 |
|
---|
1332 | if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
|
---|
1333 |
|
---|
1334 | errno = SMBC_errno(context, targetcli);
|
---|
1335 |
|
---|
1336 | if (errno == EACCES) { /* Check if the dir empty or not */
|
---|
1337 |
|
---|
1338 | /* Local storage to avoid buffer overflows */
|
---|
1339 | char *lpath;
|
---|
1340 | bool smbc_rmdir_dirempty = true;
|
---|
1341 | NTSTATUS status;
|
---|
1342 |
|
---|
1343 | lpath = talloc_asprintf(frame, "%s\\*",
|
---|
1344 | targetpath);
|
---|
1345 | if (!lpath) {
|
---|
1346 | errno = ENOMEM;
|
---|
1347 | TALLOC_FREE(frame);
|
---|
1348 | return -1;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | status = cli_list(targetcli, lpath,
|
---|
1352 | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
|
---|
1353 | rmdir_list_fn,
|
---|
1354 | &smbc_rmdir_dirempty);
|
---|
1355 |
|
---|
1356 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1357 | /* Fix errno to ignore latest error ... */
|
---|
1358 | DEBUG(5, ("smbc_rmdir: "
|
---|
1359 | "cli_list returned an error: %d\n",
|
---|
1360 | SMBC_errno(context, targetcli)));
|
---|
1361 | errno = EACCES;
|
---|
1362 |
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | if (smbc_rmdir_dirempty)
|
---|
1366 | errno = EACCES;
|
---|
1367 | else
|
---|
1368 | errno = ENOTEMPTY;
|
---|
1369 |
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | TALLOC_FREE(frame);
|
---|
1373 | return -1;
|
---|
1374 |
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | TALLOC_FREE(frame);
|
---|
1378 | return 0;
|
---|
1379 |
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /*
|
---|
1383 | * Routine to return the current directory position
|
---|
1384 | */
|
---|
1385 |
|
---|
1386 | off_t
|
---|
1387 | SMBC_telldir_ctx(SMBCCTX *context,
|
---|
1388 | SMBCFILE *dir)
|
---|
1389 | {
|
---|
1390 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1391 |
|
---|
1392 | if (!context || !context->internal->initialized) {
|
---|
1393 |
|
---|
1394 | errno = EINVAL;
|
---|
1395 | TALLOC_FREE(frame);
|
---|
1396 | return -1;
|
---|
1397 |
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
|
---|
1401 |
|
---|
1402 | errno = EBADF;
|
---|
1403 | TALLOC_FREE(frame);
|
---|
1404 | return -1;
|
---|
1405 |
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | if (dir->file != False) { /* FIXME, should be dir, perhaps */
|
---|
1409 |
|
---|
1410 | errno = ENOTDIR;
|
---|
1411 | TALLOC_FREE(frame);
|
---|
1412 | return -1;
|
---|
1413 |
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /* See if we're already at the end. */
|
---|
1417 | if (dir->dir_next == NULL) {
|
---|
1418 | /* We are. */
|
---|
1419 | TALLOC_FREE(frame);
|
---|
1420 | return -1;
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | /*
|
---|
1424 | * We return the pointer here as the offset
|
---|
1425 | */
|
---|
1426 | TALLOC_FREE(frame);
|
---|
1427 | return (off_t)(long)dir->dir_next->dirent;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /*
|
---|
1431 | * A routine to run down the list and see if the entry is OK
|
---|
1432 | */
|
---|
1433 |
|
---|
1434 | static struct smbc_dir_list *
|
---|
1435 | check_dir_ent(struct smbc_dir_list *list,
|
---|
1436 | struct smbc_dirent *dirent)
|
---|
1437 | {
|
---|
1438 |
|
---|
1439 | /* Run down the list looking for what we want */
|
---|
1440 |
|
---|
1441 | if (dirent) {
|
---|
1442 |
|
---|
1443 | struct smbc_dir_list *tmp = list;
|
---|
1444 |
|
---|
1445 | while (tmp) {
|
---|
1446 |
|
---|
1447 | if (tmp->dirent == dirent)
|
---|
1448 | return tmp;
|
---|
1449 |
|
---|
1450 | tmp = tmp->next;
|
---|
1451 |
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | return NULL; /* Not found, or an error */
|
---|
1457 |
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 |
|
---|
1461 | /*
|
---|
1462 | * Routine to seek on a directory
|
---|
1463 | */
|
---|
1464 |
|
---|
1465 | int
|
---|
1466 | SMBC_lseekdir_ctx(SMBCCTX *context,
|
---|
1467 | SMBCFILE *dir,
|
---|
1468 | off_t offset)
|
---|
1469 | {
|
---|
1470 | long int l_offset = offset; /* Handle problems of size */
|
---|
1471 | struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
|
---|
1472 | struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
|
---|
1473 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1474 |
|
---|
1475 | if (!context || !context->internal->initialized) {
|
---|
1476 |
|
---|
1477 | errno = EINVAL;
|
---|
1478 | TALLOC_FREE(frame);
|
---|
1479 | return -1;
|
---|
1480 |
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | if (dir->file != False) { /* FIXME, should be dir, perhaps */
|
---|
1484 |
|
---|
1485 | errno = ENOTDIR;
|
---|
1486 | TALLOC_FREE(frame);
|
---|
1487 | return -1;
|
---|
1488 |
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | /* Now, check what we were passed and see if it is OK ... */
|
---|
1492 |
|
---|
1493 | if (dirent == NULL) { /* Seek to the begining of the list */
|
---|
1494 |
|
---|
1495 | dir->dir_next = dir->dir_list;
|
---|
1496 | TALLOC_FREE(frame);
|
---|
1497 | return 0;
|
---|
1498 |
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | if (offset == -1) { /* Seek to the end of the list */
|
---|
1502 | dir->dir_next = NULL;
|
---|
1503 | TALLOC_FREE(frame);
|
---|
1504 | return 0;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | /* Now, run down the list and make sure that the entry is OK */
|
---|
1508 | /* This may need to be changed if we change the format of the list */
|
---|
1509 |
|
---|
1510 | if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
|
---|
1511 | errno = EINVAL; /* Bad entry */
|
---|
1512 | TALLOC_FREE(frame);
|
---|
1513 | return -1;
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | dir->dir_next = list_ent;
|
---|
1517 |
|
---|
1518 | TALLOC_FREE(frame);
|
---|
1519 | return 0;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | /*
|
---|
1523 | * Routine to fstat a dir
|
---|
1524 | */
|
---|
1525 |
|
---|
1526 | int
|
---|
1527 | SMBC_fstatdir_ctx(SMBCCTX *context,
|
---|
1528 | SMBCFILE *dir,
|
---|
1529 | struct stat *st)
|
---|
1530 | {
|
---|
1531 |
|
---|
1532 | if (!context || !context->internal->initialized) {
|
---|
1533 |
|
---|
1534 | errno = EINVAL;
|
---|
1535 | return -1;
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | /* No code yet ... */
|
---|
1539 | return 0;
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | int
|
---|
1543 | SMBC_chmod_ctx(SMBCCTX *context,
|
---|
1544 | const char *fname,
|
---|
1545 | mode_t newmode)
|
---|
1546 | {
|
---|
1547 | SMBCSRV *srv = NULL;
|
---|
1548 | char *server = NULL;
|
---|
1549 | char *share = NULL;
|
---|
1550 | char *user = NULL;
|
---|
1551 | char *password = NULL;
|
---|
1552 | char *workgroup = NULL;
|
---|
1553 | char *targetpath = NULL;
|
---|
1554 | struct cli_state *targetcli = NULL;
|
---|
1555 | char *path = NULL;
|
---|
1556 | uint16 mode;
|
---|
1557 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1558 |
|
---|
1559 | if (!context || !context->internal->initialized) {
|
---|
1560 |
|
---|
1561 | errno = EINVAL; /* Best I can think of ... */
|
---|
1562 | TALLOC_FREE(frame);
|
---|
1563 | return -1;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | if (!fname) {
|
---|
1567 | errno = EINVAL;
|
---|
1568 | TALLOC_FREE(frame);
|
---|
1569 | return -1;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
|
---|
1573 |
|
---|
1574 | if (SMBC_parse_path(frame,
|
---|
1575 | context,
|
---|
1576 | fname,
|
---|
1577 | &workgroup,
|
---|
1578 | &server,
|
---|
1579 | &share,
|
---|
1580 | &path,
|
---|
1581 | &user,
|
---|
1582 | &password,
|
---|
1583 | NULL)) {
|
---|
1584 | errno = EINVAL;
|
---|
1585 | TALLOC_FREE(frame);
|
---|
1586 | return -1;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | if (!user || user[0] == (char)0) {
|
---|
1590 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
1591 | if (!user) {
|
---|
1592 | errno = ENOMEM;
|
---|
1593 | TALLOC_FREE(frame);
|
---|
1594 | return -1;
|
---|
1595 | }
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | srv = SMBC_server(frame, context, True,
|
---|
1599 | server, share, &workgroup, &user, &password);
|
---|
1600 |
|
---|
1601 | if (!srv) {
|
---|
1602 | TALLOC_FREE(frame);
|
---|
1603 | return -1; /* errno set by SMBC_server */
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 | /*d_printf(">>>unlink: resolving %s\n", path);*/
|
---|
1607 | if (!cli_resolve_path(frame, "", context->internal->auth_info,
|
---|
1608 | srv->cli, path,
|
---|
1609 | &targetcli, &targetpath)) {
|
---|
1610 | d_printf("Could not resolve %s\n", path);
|
---|
1611 | errno = ENOENT;
|
---|
1612 | TALLOC_FREE(frame);
|
---|
1613 | return -1;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | mode = 0;
|
---|
1617 |
|
---|
1618 | if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= FILE_ATTRIBUTE_READONLY;
|
---|
1619 | if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= FILE_ATTRIBUTE_ARCHIVE;
|
---|
1620 | if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= FILE_ATTRIBUTE_SYSTEM;
|
---|
1621 | if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= FILE_ATTRIBUTE_HIDDEN;
|
---|
1622 |
|
---|
1623 | if (!NT_STATUS_IS_OK(cli_setatr(targetcli, targetpath, mode, 0))) {
|
---|
1624 | errno = SMBC_errno(context, targetcli);
|
---|
1625 | TALLOC_FREE(frame);
|
---|
1626 | return -1;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | TALLOC_FREE(frame);
|
---|
1630 | return 0;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | int
|
---|
1634 | SMBC_utimes_ctx(SMBCCTX *context,
|
---|
1635 | const char *fname,
|
---|
1636 | struct timeval *tbuf)
|
---|
1637 | {
|
---|
1638 | SMBCSRV *srv = NULL;
|
---|
1639 | char *server = NULL;
|
---|
1640 | char *share = NULL;
|
---|
1641 | char *user = NULL;
|
---|
1642 | char *password = NULL;
|
---|
1643 | char *workgroup = NULL;
|
---|
1644 | char *path = NULL;
|
---|
1645 | time_t access_time;
|
---|
1646 | time_t write_time;
|
---|
1647 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1648 |
|
---|
1649 | if (!context || !context->internal->initialized) {
|
---|
1650 |
|
---|
1651 | errno = EINVAL; /* Best I can think of ... */
|
---|
1652 | TALLOC_FREE(frame);
|
---|
1653 | return -1;
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | if (!fname) {
|
---|
1657 | errno = EINVAL;
|
---|
1658 | TALLOC_FREE(frame);
|
---|
1659 | return -1;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | if (tbuf == NULL) {
|
---|
1663 | access_time = write_time = time(NULL);
|
---|
1664 | } else {
|
---|
1665 | access_time = tbuf[0].tv_sec;
|
---|
1666 | write_time = tbuf[1].tv_sec;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | if (DEBUGLVL(4)) {
|
---|
1670 | char *p;
|
---|
1671 | char atimebuf[32];
|
---|
1672 | char mtimebuf[32];
|
---|
1673 |
|
---|
1674 | strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
|
---|
1675 | atimebuf[sizeof(atimebuf) - 1] = '\0';
|
---|
1676 | if ((p = strchr(atimebuf, '\n')) != NULL) {
|
---|
1677 | *p = '\0';
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
|
---|
1681 | mtimebuf[sizeof(mtimebuf) - 1] = '\0';
|
---|
1682 | if ((p = strchr(mtimebuf, '\n')) != NULL) {
|
---|
1683 | *p = '\0';
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
|
---|
1687 | fname, atimebuf, mtimebuf);
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | if (SMBC_parse_path(frame,
|
---|
1691 | context,
|
---|
1692 | fname,
|
---|
1693 | &workgroup,
|
---|
1694 | &server,
|
---|
1695 | &share,
|
---|
1696 | &path,
|
---|
1697 | &user,
|
---|
1698 | &password,
|
---|
1699 | NULL)) {
|
---|
1700 | errno = EINVAL;
|
---|
1701 | TALLOC_FREE(frame);
|
---|
1702 | return -1;
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | if (!user || user[0] == (char)0) {
|
---|
1706 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
1707 | if (!user) {
|
---|
1708 | errno = ENOMEM;
|
---|
1709 | TALLOC_FREE(frame);
|
---|
1710 | return -1;
|
---|
1711 | }
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | srv = SMBC_server(frame, context, True,
|
---|
1715 | server, share, &workgroup, &user, &password);
|
---|
1716 |
|
---|
1717 | if (!srv) {
|
---|
1718 | TALLOC_FREE(frame);
|
---|
1719 | return -1; /* errno set by SMBC_server */
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | if (!SMBC_setatr(context, srv, path,
|
---|
1723 | 0, access_time, write_time, 0, 0)) {
|
---|
1724 | TALLOC_FREE(frame);
|
---|
1725 | return -1; /* errno set by SMBC_setatr */
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | TALLOC_FREE(frame);
|
---|
1729 | return 0;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | /*
|
---|
1733 | * Routine to unlink() a file
|
---|
1734 | */
|
---|
1735 |
|
---|
1736 | int
|
---|
1737 | SMBC_unlink_ctx(SMBCCTX *context,
|
---|
1738 | const char *fname)
|
---|
1739 | {
|
---|
1740 | char *server = NULL;
|
---|
1741 | char *share = NULL;
|
---|
1742 | char *user = NULL;
|
---|
1743 | char *password = NULL;
|
---|
1744 | char *workgroup = NULL;
|
---|
1745 | char *path = NULL;
|
---|
1746 | char *targetpath = NULL;
|
---|
1747 | struct cli_state *targetcli = NULL;
|
---|
1748 | SMBCSRV *srv = NULL;
|
---|
1749 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1750 |
|
---|
1751 | if (!context || !context->internal->initialized) {
|
---|
1752 |
|
---|
1753 | errno = EINVAL; /* Best I can think of ... */
|
---|
1754 | TALLOC_FREE(frame);
|
---|
1755 | return -1;
|
---|
1756 |
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | if (!fname) {
|
---|
1760 | errno = EINVAL;
|
---|
1761 | TALLOC_FREE(frame);
|
---|
1762 | return -1;
|
---|
1763 |
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | if (SMBC_parse_path(frame,
|
---|
1767 | context,
|
---|
1768 | fname,
|
---|
1769 | &workgroup,
|
---|
1770 | &server,
|
---|
1771 | &share,
|
---|
1772 | &path,
|
---|
1773 | &user,
|
---|
1774 | &password,
|
---|
1775 | NULL)) {
|
---|
1776 | errno = EINVAL;
|
---|
1777 | TALLOC_FREE(frame);
|
---|
1778 | return -1;
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | if (!user || user[0] == (char)0) {
|
---|
1782 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
1783 | if (!user) {
|
---|
1784 | errno = ENOMEM;
|
---|
1785 | TALLOC_FREE(frame);
|
---|
1786 | return -1;
|
---|
1787 | }
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | srv = SMBC_server(frame, context, True,
|
---|
1791 | server, share, &workgroup, &user, &password);
|
---|
1792 |
|
---|
1793 | if (!srv) {
|
---|
1794 | TALLOC_FREE(frame);
|
---|
1795 | return -1; /* SMBC_server sets errno */
|
---|
1796 |
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | /*d_printf(">>>unlink: resolving %s\n", path);*/
|
---|
1800 | if (!cli_resolve_path(frame, "", context->internal->auth_info,
|
---|
1801 | srv->cli, path,
|
---|
1802 | &targetcli, &targetpath)) {
|
---|
1803 | d_printf("Could not resolve %s\n", path);
|
---|
1804 | errno = ENOENT;
|
---|
1805 | TALLOC_FREE(frame);
|
---|
1806 | return -1;
|
---|
1807 | }
|
---|
1808 | /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
|
---|
1809 |
|
---|
1810 | if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetpath, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
|
---|
1811 |
|
---|
1812 | errno = SMBC_errno(context, targetcli);
|
---|
1813 |
|
---|
1814 | if (errno == EACCES) { /* Check if the file is a directory */
|
---|
1815 |
|
---|
1816 | int saverr = errno;
|
---|
1817 | SMB_OFF_T size = 0;
|
---|
1818 | uint16 mode = 0;
|
---|
1819 | struct timespec write_time_ts;
|
---|
1820 | struct timespec access_time_ts;
|
---|
1821 | struct timespec change_time_ts;
|
---|
1822 | SMB_INO_T ino = 0;
|
---|
1823 |
|
---|
1824 | if (!SMBC_getatr(context, srv, path, &mode, &size,
|
---|
1825 | NULL,
|
---|
1826 | &access_time_ts,
|
---|
1827 | &write_time_ts,
|
---|
1828 | &change_time_ts,
|
---|
1829 | &ino)) {
|
---|
1830 |
|
---|
1831 | /* Hmmm, bad error ... What? */
|
---|
1832 |
|
---|
1833 | errno = SMBC_errno(context, targetcli);
|
---|
1834 | TALLOC_FREE(frame);
|
---|
1835 | return -1;
|
---|
1836 |
|
---|
1837 | }
|
---|
1838 | else {
|
---|
1839 |
|
---|
1840 | if (IS_DOS_DIR(mode))
|
---|
1841 | errno = EISDIR;
|
---|
1842 | else
|
---|
1843 | errno = saverr; /* Restore this */
|
---|
1844 |
|
---|
1845 | }
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | TALLOC_FREE(frame);
|
---|
1849 | return -1;
|
---|
1850 |
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | TALLOC_FREE(frame);
|
---|
1854 | return 0; /* Success ... */
|
---|
1855 |
|
---|
1856 | }
|
---|
1857 |
|
---|
1858 | /*
|
---|
1859 | * Routine to rename() a file
|
---|
1860 | */
|
---|
1861 |
|
---|
1862 | int
|
---|
1863 | SMBC_rename_ctx(SMBCCTX *ocontext,
|
---|
1864 | const char *oname,
|
---|
1865 | SMBCCTX *ncontext,
|
---|
1866 | const char *nname)
|
---|
1867 | {
|
---|
1868 | char *server1 = NULL;
|
---|
1869 | char *share1 = NULL;
|
---|
1870 | char *server2 = NULL;
|
---|
1871 | char *share2 = NULL;
|
---|
1872 | char *user1 = NULL;
|
---|
1873 | char *user2 = NULL;
|
---|
1874 | char *password1 = NULL;
|
---|
1875 | char *password2 = NULL;
|
---|
1876 | char *workgroup = NULL;
|
---|
1877 | char *path1 = NULL;
|
---|
1878 | char *path2 = NULL;
|
---|
1879 | char *targetpath1 = NULL;
|
---|
1880 | char *targetpath2 = NULL;
|
---|
1881 | struct cli_state *targetcli1 = NULL;
|
---|
1882 | struct cli_state *targetcli2 = NULL;
|
---|
1883 | SMBCSRV *srv = NULL;
|
---|
1884 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1885 |
|
---|
1886 | if (!ocontext || !ncontext ||
|
---|
1887 | !ocontext->internal->initialized ||
|
---|
1888 | !ncontext->internal->initialized) {
|
---|
1889 |
|
---|
1890 | errno = EINVAL; /* Best I can think of ... */
|
---|
1891 | TALLOC_FREE(frame);
|
---|
1892 | return -1;
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | if (!oname || !nname) {
|
---|
1896 | errno = EINVAL;
|
---|
1897 | TALLOC_FREE(frame);
|
---|
1898 | return -1;
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
|
---|
1902 |
|
---|
1903 | if (SMBC_parse_path(frame,
|
---|
1904 | ocontext,
|
---|
1905 | oname,
|
---|
1906 | &workgroup,
|
---|
1907 | &server1,
|
---|
1908 | &share1,
|
---|
1909 | &path1,
|
---|
1910 | &user1,
|
---|
1911 | &password1,
|
---|
1912 | NULL)) {
|
---|
1913 | errno = EINVAL;
|
---|
1914 | TALLOC_FREE(frame);
|
---|
1915 | return -1;
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | if (!user1 || user1[0] == (char)0) {
|
---|
1919 | user1 = talloc_strdup(frame, smbc_getUser(ocontext));
|
---|
1920 | if (!user1) {
|
---|
1921 | errno = ENOMEM;
|
---|
1922 | TALLOC_FREE(frame);
|
---|
1923 | return -1;
|
---|
1924 | }
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | if (SMBC_parse_path(frame,
|
---|
1928 | ncontext,
|
---|
1929 | nname,
|
---|
1930 | NULL,
|
---|
1931 | &server2,
|
---|
1932 | &share2,
|
---|
1933 | &path2,
|
---|
1934 | &user2,
|
---|
1935 | &password2,
|
---|
1936 | NULL)) {
|
---|
1937 | errno = EINVAL;
|
---|
1938 | TALLOC_FREE(frame);
|
---|
1939 | return -1;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | if (!user2 || user2[0] == (char)0) {
|
---|
1943 | user2 = talloc_strdup(frame, smbc_getUser(ncontext));
|
---|
1944 | if (!user2) {
|
---|
1945 | errno = ENOMEM;
|
---|
1946 | TALLOC_FREE(frame);
|
---|
1947 | return -1;
|
---|
1948 | }
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 | if (strcmp(server1, server2) || strcmp(share1, share2) ||
|
---|
1952 | strcmp(user1, user2)) {
|
---|
1953 | /* Can't rename across file systems, or users?? */
|
---|
1954 | errno = EXDEV;
|
---|
1955 | TALLOC_FREE(frame);
|
---|
1956 | return -1;
|
---|
1957 | }
|
---|
1958 |
|
---|
1959 | srv = SMBC_server(frame, ocontext, True,
|
---|
1960 | server1, share1, &workgroup, &user1, &password1);
|
---|
1961 | if (!srv) {
|
---|
1962 | TALLOC_FREE(frame);
|
---|
1963 | return -1;
|
---|
1964 |
|
---|
1965 | }
|
---|
1966 |
|
---|
1967 | /* set the credentials to make DFS work */
|
---|
1968 | smbc_set_credentials_with_fallback(ocontext,
|
---|
1969 | workgroup,
|
---|
1970 | user1,
|
---|
1971 | password1);
|
---|
1972 |
|
---|
1973 | /*d_printf(">>>rename: resolving %s\n", path1);*/
|
---|
1974 | if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
|
---|
1975 | srv->cli,
|
---|
1976 | path1,
|
---|
1977 | &targetcli1, &targetpath1)) {
|
---|
1978 | d_printf("Could not resolve %s\n", path1);
|
---|
1979 | errno = ENOENT;
|
---|
1980 | TALLOC_FREE(frame);
|
---|
1981 | return -1;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | /* set the credentials to make DFS work */
|
---|
1985 | smbc_set_credentials_with_fallback(ncontext,
|
---|
1986 | workgroup,
|
---|
1987 | user2,
|
---|
1988 | password2);
|
---|
1989 |
|
---|
1990 | /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
|
---|
1991 | /*d_printf(">>>rename: resolving %s\n", path2);*/
|
---|
1992 | if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
|
---|
1993 | srv->cli,
|
---|
1994 | path2,
|
---|
1995 | &targetcli2, &targetpath2)) {
|
---|
1996 | d_printf("Could not resolve %s\n", path2);
|
---|
1997 | errno = ENOENT;
|
---|
1998 | TALLOC_FREE(frame);
|
---|
1999 | return -1;
|
---|
2000 | }
|
---|
2001 | /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
|
---|
2002 |
|
---|
2003 | if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
|
---|
2004 | strcmp(targetcli1->share, targetcli2->share))
|
---|
2005 | {
|
---|
2006 | /* can't rename across file systems */
|
---|
2007 | errno = EXDEV;
|
---|
2008 | TALLOC_FREE(frame);
|
---|
2009 | return -1;
|
---|
2010 | }
|
---|
2011 |
|
---|
2012 | if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
|
---|
2013 | int eno = SMBC_errno(ocontext, targetcli1);
|
---|
2014 |
|
---|
2015 | if (eno != EEXIST ||
|
---|
2016 | !NT_STATUS_IS_OK(cli_unlink(targetcli1, targetpath2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)) ||
|
---|
2017 | !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
|
---|
2018 |
|
---|
2019 | errno = eno;
|
---|
2020 | TALLOC_FREE(frame);
|
---|
2021 | return -1;
|
---|
2022 |
|
---|
2023 | }
|
---|
2024 | }
|
---|
2025 |
|
---|
2026 | TALLOC_FREE(frame);
|
---|
2027 | return 0; /* Success */
|
---|
2028 | }
|
---|
2029 |
|
---|