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