Make rmdir async.
[ira/wip.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, "", context->internal->auth_info,
774                                                 srv->cli, path,
775                                                 &targetcli, &targetpath)) {
776                                 d_printf("Could not resolve %s\n", path);
777                                 if (dir) {
778                                         SAFE_FREE(dir->fname);
779                                         SAFE_FREE(dir);
780                                 }
781                                 TALLOC_FREE(frame);
782                                 return NULL;
783                         }
784
785                         if (cli_list(targetcli, targetpath,
786                                      aDIR | aSYSTEM | aHIDDEN,
787                                      dir_list_fn, (void *)dir) < 0) {
788
789                                 if (dir) {
790                                         SAFE_FREE(dir->fname);
791                                         SAFE_FREE(dir);
792                                 }
793                                 saved_errno = SMBC_errno(context, targetcli);
794
795                                 if (saved_errno == EINVAL) {
796                                         /*
797                                          * See if they asked to opendir
798                                          * something other than a directory.
799                                          * If so, the converted error value we
800                                          * got would have been EINVAL rather
801                                          * than ENOTDIR.
802                                          */
803                                         *p = '\0'; /* restore original path */
804
805                                         if (SMBC_getatr(context, srv, path,
806                                                         &mode, NULL,
807                                                         NULL, NULL, NULL, NULL,
808                                                         NULL) &&
809                                             ! IS_DOS_DIR(mode)) {
810
811                                                 /* It is.  Correct the error value */
812                                                 saved_errno = ENOTDIR;
813                                         }
814                                 }
815
816                                 /*
817                                  * If there was an error and the server is no
818                                  * good any more...
819                                  */
820                                 if (cli_is_error(targetcli) &&
821                                     smbc_getFunctionCheckServer(context)(context, srv)) {
822
823                                         /* ... then remove it. */
824                                         if (smbc_getFunctionRemoveUnusedServer(context)(context,
825                                                                                         srv)) {
826                                                 /*
827                                                  * We could not remove the
828                                                  * server completely, remove
829                                                  * it from the cache so we
830                                                  * will not get it again. It
831                                                  * will be removed when the
832                                                  * last file/dir is closed.
833                                                  */
834                                                 smbc_getFunctionRemoveCachedServer(context)(context, srv);
835                                         }
836                                 }
837
838                                 errno = saved_errno;
839                                 TALLOC_FREE(frame);
840                                 return NULL;
841                         }
842                 }
843
844         }
845
846         DLIST_ADD(context->internal->files, dir);
847         TALLOC_FREE(frame);
848         return dir;
849
850 }
851
852 /*
853  * Routine to close a directory
854  */
855
856 int
857 SMBC_closedir_ctx(SMBCCTX *context,
858                   SMBCFILE *dir)
859 {
860         TALLOC_CTX *frame = talloc_stackframe();
861
862         if (!context || !context->internal->initialized) {
863                 errno = EINVAL;
864                 TALLOC_FREE(frame);
865                 return -1;
866         }
867
868         if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
869                 errno = EBADF;
870                 TALLOC_FREE(frame);
871                 return -1;
872         }
873
874         remove_dir(dir); /* Clean it up */
875
876         DLIST_REMOVE(context->internal->files, dir);
877
878         if (dir) {
879
880                 SAFE_FREE(dir->fname);
881                 SAFE_FREE(dir);    /* Free the space too */
882         }
883
884         TALLOC_FREE(frame);
885         return 0;
886
887 }
888
889 static void
890 smbc_readdir_internal(SMBCCTX * context,
891                       struct smbc_dirent *dest,
892                       struct smbc_dirent *src,
893                       int max_namebuf_len)
894 {
895         if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
896
897                 /* url-encode the name.  get back remaining buffer space */
898                 max_namebuf_len =
899                         smbc_urlencode(dest->name, src->name, max_namebuf_len);
900
901                 /* We now know the name length */
902                 dest->namelen = strlen(dest->name);
903
904                 /* Save the pointer to the beginning of the comment */
905                 dest->comment = dest->name + dest->namelen + 1;
906
907                 /* Copy the comment */
908                 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
909                 dest->comment[max_namebuf_len - 1] = '\0';
910
911                 /* Save other fields */
912                 dest->smbc_type = src->smbc_type;
913                 dest->commentlen = strlen(dest->comment);
914                 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
915                                 (char *) dest);
916         } else {
917
918                 /* No encoding.  Just copy the entry as is. */
919                 memcpy(dest, src, src->dirlen);
920                 dest->comment = (char *)(&dest->name + src->namelen + 1);
921         }
922
923 }
924
925 /*
926  * Routine to get a directory entry
927  */
928
929 struct smbc_dirent *
930 SMBC_readdir_ctx(SMBCCTX *context,
931                  SMBCFILE *dir)
932 {
933         int maxlen;
934         struct smbc_dirent *dirp, *dirent;
935         TALLOC_CTX *frame = talloc_stackframe();
936
937         /* Check that all is ok first ... */
938
939         if (!context || !context->internal->initialized) {
940
941                 errno = EINVAL;
942                 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
943                 TALLOC_FREE(frame);
944                 return NULL;
945
946         }
947
948         if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
949
950                 errno = EBADF;
951                 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
952                 TALLOC_FREE(frame);
953                 return NULL;
954
955         }
956
957         if (dir->file != False) { /* FIXME, should be dir, perhaps */
958
959                 errno = ENOTDIR;
960                 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
961                 TALLOC_FREE(frame);
962                 return NULL;
963
964         }
965
966         if (!dir->dir_next) {
967                 TALLOC_FREE(frame);
968                 return NULL;
969         }
970
971         dirent = dir->dir_next->dirent;
972         if (!dirent) {
973
974                 errno = ENOENT;
975                 TALLOC_FREE(frame);
976                 return NULL;
977
978         }
979
980         dirp = &context->internal->dirent;
981         maxlen = sizeof(context->internal->_dirent_name);
982
983         smbc_readdir_internal(context, dirp, dirent, maxlen);
984
985         dir->dir_next = dir->dir_next->next;
986
987         TALLOC_FREE(frame);
988         return dirp;
989 }
990
991 /*
992  * Routine to get directory entries
993  */
994
995 int
996 SMBC_getdents_ctx(SMBCCTX *context,
997                   SMBCFILE *dir,
998                   struct smbc_dirent *dirp,
999                   int count)
1000 {
1001         int rem = count;
1002         int reqd;
1003         int maxlen;
1004         char *ndir = (char *)dirp;
1005         struct smbc_dir_list *dirlist;
1006         TALLOC_CTX *frame = talloc_stackframe();
1007
1008         /* Check that all is ok first ... */
1009
1010         if (!context || !context->internal->initialized) {
1011
1012                 errno = EINVAL;
1013                 TALLOC_FREE(frame);
1014                 return -1;
1015
1016         }
1017
1018         if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1019
1020                 errno = EBADF;
1021                 TALLOC_FREE(frame);
1022                 return -1;
1023
1024         }
1025
1026         if (dir->file != False) { /* FIXME, should be dir, perhaps */
1027
1028                 errno = ENOTDIR;
1029                 TALLOC_FREE(frame);
1030                 return -1;
1031
1032         }
1033
1034         /*
1035          * Now, retrieve the number of entries that will fit in what was passed
1036          * We have to figure out if the info is in the list, or we need to
1037          * send a request to the server to get the info.
1038          */
1039
1040         while ((dirlist = dir->dir_next)) {
1041                 struct smbc_dirent *dirent;
1042
1043                 if (!dirlist->dirent) {
1044
1045                         errno = ENOENT;  /* Bad error */
1046                         TALLOC_FREE(frame);
1047                         return -1;
1048
1049                 }
1050
1051                 /* Do urlencoding of next entry, if so selected */
1052                 dirent = &context->internal->dirent;
1053                 maxlen = sizeof(context->internal->_dirent_name);
1054                 smbc_readdir_internal(context, dirent,
1055                                       dirlist->dirent, maxlen);
1056
1057                 reqd = dirent->dirlen;
1058
1059                 if (rem < reqd) {
1060
1061                         if (rem < count) { /* We managed to copy something */
1062
1063                                 errno = 0;
1064                                 TALLOC_FREE(frame);
1065                                 return count - rem;
1066
1067                         }
1068                         else { /* Nothing copied ... */
1069
1070                                 errno = EINVAL;  /* Not enough space ... */
1071                                 TALLOC_FREE(frame);
1072                                 return -1;
1073
1074                         }
1075
1076                 }
1077
1078                 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1079
1080                 ((struct smbc_dirent *)ndir)->comment =
1081                         (char *)(&((struct smbc_dirent *)ndir)->name +
1082                                  dirent->namelen +
1083                                  1);
1084
1085                 ndir += reqd;
1086
1087                 rem -= reqd;
1088
1089                 dir->dir_next = dirlist = dirlist -> next;
1090         }
1091
1092         TALLOC_FREE(frame);
1093
1094         if (rem == count)
1095                 return 0;
1096         else
1097                 return count - rem;
1098
1099 }
1100
1101 /*
1102  * Routine to create a directory ...
1103  */
1104
1105 int
1106 SMBC_mkdir_ctx(SMBCCTX *context,
1107                const char *fname,
1108                mode_t mode)
1109 {
1110         SMBCSRV *srv = NULL;
1111         char *server = NULL;
1112         char *share = NULL;
1113         char *user = NULL;
1114         char *password = NULL;
1115         char *workgroup = NULL;
1116         char *path = NULL;
1117         char *targetpath = NULL;
1118         struct cli_state *targetcli = NULL;
1119         TALLOC_CTX *frame = talloc_stackframe();
1120
1121         if (!context || !context->internal->initialized) {
1122                 errno = EINVAL;
1123                 TALLOC_FREE(frame);
1124                 return -1;
1125         }
1126
1127         if (!fname) {
1128                 errno = EINVAL;
1129                 TALLOC_FREE(frame);
1130                 return -1;
1131         }
1132
1133         DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1134
1135         if (SMBC_parse_path(frame,
1136                             context,
1137                             fname,
1138                             &workgroup,
1139                             &server,
1140                             &share,
1141                             &path,
1142                             &user,
1143                             &password,
1144                             NULL)) {
1145                 errno = EINVAL;
1146                 TALLOC_FREE(frame);
1147                 return -1;
1148         }
1149
1150         if (!user || user[0] == (char)0) {
1151                 user = talloc_strdup(frame, smbc_getUser(context));
1152                 if (!user) {
1153                         errno = ENOMEM;
1154                         TALLOC_FREE(frame);
1155                         return -1;
1156                 }
1157         }
1158
1159         srv = SMBC_server(frame, context, True,
1160                           server, share, &workgroup, &user, &password);
1161
1162         if (!srv) {
1163
1164                 TALLOC_FREE(frame);
1165                 return -1;  /* errno set by SMBC_server */
1166
1167         }
1168
1169         /*d_printf(">>>mkdir: resolving %s\n", path);*/
1170         if (!cli_resolve_path(frame, "", context->internal->auth_info,
1171                                 srv->cli, path,
1172                                 &targetcli, &targetpath)) {
1173                 d_printf("Could not resolve %s\n", path);
1174                 errno = ENOENT;
1175                 TALLOC_FREE(frame);
1176                 return -1;
1177         }
1178         /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1179
1180         if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetpath))) {
1181                 errno = SMBC_errno(context, targetcli);
1182                 TALLOC_FREE(frame);
1183                 return -1;
1184
1185         }
1186
1187         TALLOC_FREE(frame);
1188         return 0;
1189
1190 }
1191
1192 /*
1193  * Our list function simply checks to see if a directory is not empty
1194  */
1195
1196 static void
1197 rmdir_list_fn(const char *mnt,
1198               file_info *finfo,
1199               const char *mask,
1200               void *state)
1201 {
1202         if (strncmp(finfo->name, ".", 1) != 0 &&
1203             strncmp(finfo->name, "..", 2) != 0) {
1204                 bool *smbc_rmdir_dirempty = (bool *)state;
1205                 *smbc_rmdir_dirempty = false;
1206         }
1207 }
1208
1209 /*
1210  * Routine to remove a directory
1211  */
1212
1213 int
1214 SMBC_rmdir_ctx(SMBCCTX *context,
1215                const char *fname)
1216 {
1217         SMBCSRV *srv = NULL;
1218         char *server = NULL;
1219         char *share = NULL;
1220         char *user = NULL;
1221         char *password = NULL;
1222         char *workgroup = NULL;
1223         char *path = NULL;
1224         char *targetpath = NULL;
1225         struct cli_state *targetcli = NULL;
1226         TALLOC_CTX *frame = talloc_stackframe();
1227
1228         if (!context || !context->internal->initialized) {
1229                 errno = EINVAL;
1230                 TALLOC_FREE(frame);
1231                 return -1;
1232         }
1233
1234         if (!fname) {
1235                 errno = EINVAL;
1236                 TALLOC_FREE(frame);
1237                 return -1;
1238         }
1239
1240         DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1241
1242         if (SMBC_parse_path(frame,
1243                             context,
1244                             fname,
1245                             &workgroup,
1246                             &server,
1247                             &share,
1248                             &path,
1249                             &user,
1250                             &password,
1251                             NULL)) {
1252                 errno = EINVAL;
1253                 TALLOC_FREE(frame);
1254                 return -1;
1255         }
1256
1257         if (!user || user[0] == (char)0) {
1258                 user = talloc_strdup(frame, smbc_getUser(context));
1259                 if (!user) {
1260                         errno = ENOMEM;
1261                         TALLOC_FREE(frame);
1262                         return -1;
1263                 }
1264         }
1265
1266         srv = SMBC_server(frame, context, True,
1267                           server, share, &workgroup, &user, &password);
1268
1269         if (!srv) {
1270
1271                 TALLOC_FREE(frame);
1272                 return -1;  /* errno set by SMBC_server */
1273
1274         }
1275
1276         /*d_printf(">>>rmdir: resolving %s\n", path);*/
1277         if (!cli_resolve_path(frame, "", context->internal->auth_info,
1278                                 srv->cli, path,
1279                                 &targetcli, &targetpath)) {
1280                 d_printf("Could not resolve %s\n", path);
1281                 errno = ENOENT;
1282                 TALLOC_FREE(frame);
1283                 return -1;
1284         }
1285         /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1286
1287         if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetpath))) {
1288
1289                 errno = SMBC_errno(context, targetcli);
1290
1291                 if (errno == EACCES) {  /* Check if the dir empty or not */
1292
1293                         /* Local storage to avoid buffer overflows */
1294                         char *lpath;
1295                         bool smbc_rmdir_dirempty = true;
1296
1297                         lpath = talloc_asprintf(frame, "%s\\*",
1298                                                 targetpath);
1299                         if (!lpath) {
1300                                 errno = ENOMEM;
1301                                 TALLOC_FREE(frame);
1302                                 return -1;
1303                         }
1304
1305                         if (cli_list(targetcli, lpath,
1306                                      aDIR | aSYSTEM | aHIDDEN,
1307                                      rmdir_list_fn,
1308                                      &smbc_rmdir_dirempty) < 0) {
1309
1310                                 /* Fix errno to ignore latest error ... */
1311                                 DEBUG(5, ("smbc_rmdir: "
1312                                           "cli_list returned an error: %d\n",
1313                                           SMBC_errno(context, targetcli)));
1314                                 errno = EACCES;
1315
1316                         }
1317
1318                         if (smbc_rmdir_dirempty)
1319                                 errno = EACCES;
1320                         else
1321                                 errno = ENOTEMPTY;
1322
1323                 }
1324
1325                 TALLOC_FREE(frame);
1326                 return -1;
1327
1328         }
1329
1330         TALLOC_FREE(frame);
1331         return 0;
1332
1333 }
1334
1335 /*
1336  * Routine to return the current directory position
1337  */
1338
1339 off_t
1340 SMBC_telldir_ctx(SMBCCTX *context,
1341                  SMBCFILE *dir)
1342 {
1343         TALLOC_CTX *frame = talloc_stackframe();
1344
1345         if (!context || !context->internal->initialized) {
1346
1347                 errno = EINVAL;
1348                 TALLOC_FREE(frame);
1349                 return -1;
1350
1351         }
1352
1353         if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1354
1355                 errno = EBADF;
1356                 TALLOC_FREE(frame);
1357                 return -1;
1358
1359         }
1360
1361         if (dir->file != False) { /* FIXME, should be dir, perhaps */
1362
1363                 errno = ENOTDIR;
1364                 TALLOC_FREE(frame);
1365                 return -1;
1366
1367         }
1368
1369         /* See if we're already at the end. */
1370         if (dir->dir_next == NULL) {
1371                 /* We are. */
1372                 TALLOC_FREE(frame);
1373                 return -1;
1374         }
1375
1376         /*
1377          * We return the pointer here as the offset
1378          */
1379         TALLOC_FREE(frame);
1380         return (off_t)(long)dir->dir_next->dirent;
1381 }
1382
1383 /*
1384  * A routine to run down the list and see if the entry is OK
1385  */
1386
1387 static struct smbc_dir_list *
1388 check_dir_ent(struct smbc_dir_list *list,
1389               struct smbc_dirent *dirent)
1390 {
1391
1392         /* Run down the list looking for what we want */
1393
1394         if (dirent) {
1395
1396                 struct smbc_dir_list *tmp = list;
1397
1398                 while (tmp) {
1399
1400                         if (tmp->dirent == dirent)
1401                                 return tmp;
1402
1403                         tmp = tmp->next;
1404
1405                 }
1406
1407         }
1408
1409         return NULL;  /* Not found, or an error */
1410
1411 }
1412
1413
1414 /*
1415  * Routine to seek on a directory
1416  */
1417
1418 int
1419 SMBC_lseekdir_ctx(SMBCCTX *context,
1420                   SMBCFILE *dir,
1421                   off_t offset)
1422 {
1423         long int l_offset = offset;  /* Handle problems of size */
1424         struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1425         struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1426         TALLOC_CTX *frame = talloc_stackframe();
1427
1428         if (!context || !context->internal->initialized) {
1429
1430                 errno = EINVAL;
1431                 TALLOC_FREE(frame);
1432                 return -1;
1433
1434         }
1435
1436         if (dir->file != False) { /* FIXME, should be dir, perhaps */
1437
1438                 errno = ENOTDIR;
1439                 TALLOC_FREE(frame);
1440                 return -1;
1441
1442         }
1443
1444         /* Now, check what we were passed and see if it is OK ... */
1445
1446         if (dirent == NULL) {  /* Seek to the begining of the list */
1447
1448                 dir->dir_next = dir->dir_list;
1449                 TALLOC_FREE(frame);
1450                 return 0;
1451
1452         }
1453
1454         if (offset == -1) {     /* Seek to the end of the list */
1455                 dir->dir_next = NULL;
1456                 TALLOC_FREE(frame);
1457                 return 0;
1458         }
1459
1460         /* Now, run down the list and make sure that the entry is OK       */
1461         /* This may need to be changed if we change the format of the list */
1462
1463         if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1464                 errno = EINVAL;   /* Bad entry */
1465                 TALLOC_FREE(frame);
1466                 return -1;
1467         }
1468
1469         dir->dir_next = list_ent;
1470
1471         TALLOC_FREE(frame);
1472         return 0;
1473 }
1474
1475 /*
1476  * Routine to fstat a dir
1477  */
1478
1479 int
1480 SMBC_fstatdir_ctx(SMBCCTX *context,
1481                   SMBCFILE *dir,
1482                   struct stat *st)
1483 {
1484
1485         if (!context || !context->internal->initialized) {
1486
1487                 errno = EINVAL;
1488                 return -1;
1489         }
1490
1491         /* No code yet ... */
1492         return 0;
1493 }
1494
1495 int
1496 SMBC_chmod_ctx(SMBCCTX *context,
1497                const char *fname,
1498                mode_t newmode)
1499 {
1500         SMBCSRV *srv = NULL;
1501         char *server = NULL;
1502         char *share = NULL;
1503         char *user = NULL;
1504         char *password = NULL;
1505         char *workgroup = NULL;
1506         char *targetpath = NULL;
1507         struct cli_state *targetcli = NULL;
1508         char *path = NULL;
1509         uint16 mode;
1510         TALLOC_CTX *frame = talloc_stackframe();
1511
1512         if (!context || !context->internal->initialized) {
1513
1514                 errno = EINVAL;  /* Best I can think of ... */
1515                 TALLOC_FREE(frame);
1516                 return -1;
1517         }
1518
1519         if (!fname) {
1520                 errno = EINVAL;
1521                 TALLOC_FREE(frame);
1522                 return -1;
1523         }
1524
1525         DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1526
1527         if (SMBC_parse_path(frame,
1528                             context,
1529                             fname,
1530                             &workgroup,
1531                             &server,
1532                             &share,
1533                             &path,
1534                             &user,
1535                             &password,
1536                             NULL)) {
1537                 errno = EINVAL;
1538                 TALLOC_FREE(frame);
1539                 return -1;
1540         }
1541
1542         if (!user || user[0] == (char)0) {
1543                 user = talloc_strdup(frame, smbc_getUser(context));
1544                 if (!user) {
1545                         errno = ENOMEM;
1546                         TALLOC_FREE(frame);
1547                         return -1;
1548                 }
1549         }
1550
1551         srv = SMBC_server(frame, context, True,
1552                           server, share, &workgroup, &user, &password);
1553
1554         if (!srv) {
1555                 TALLOC_FREE(frame);
1556                 return -1;  /* errno set by SMBC_server */
1557         }
1558         
1559         /*d_printf(">>>unlink: resolving %s\n", path);*/
1560         if (!cli_resolve_path(frame, "", context->internal->auth_info,
1561                                 srv->cli, path,
1562                                 &targetcli, &targetpath)) {
1563                 d_printf("Could not resolve %s\n", path);
1564                 errno = ENOENT;
1565                 TALLOC_FREE(frame);
1566                 return -1;
1567         }
1568
1569         mode = 0;
1570
1571         if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1572         if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1573         if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1574         if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1575
1576         if (!cli_setatr(targetcli, targetpath, mode, 0)) {
1577                 errno = SMBC_errno(context, targetcli);
1578                 TALLOC_FREE(frame);
1579                 return -1;
1580         }
1581
1582         TALLOC_FREE(frame);
1583         return 0;
1584 }
1585
1586 int
1587 SMBC_utimes_ctx(SMBCCTX *context,
1588                 const char *fname,
1589                 struct timeval *tbuf)
1590 {
1591         SMBCSRV *srv = NULL;
1592         char *server = NULL;
1593         char *share = NULL;
1594         char *user = NULL;
1595         char *password = NULL;
1596         char *workgroup = NULL;
1597         char *path = NULL;
1598         time_t access_time;
1599         time_t write_time;
1600         TALLOC_CTX *frame = talloc_stackframe();
1601
1602         if (!context || !context->internal->initialized) {
1603
1604                 errno = EINVAL;  /* Best I can think of ... */
1605                 TALLOC_FREE(frame);
1606                 return -1;
1607         }
1608
1609         if (!fname) {
1610                 errno = EINVAL;
1611                 TALLOC_FREE(frame);
1612                 return -1;
1613         }
1614
1615         if (tbuf == NULL) {
1616                 access_time = write_time = time(NULL);
1617         } else {
1618                 access_time = tbuf[0].tv_sec;
1619                 write_time = tbuf[1].tv_sec;
1620         }
1621
1622         if (DEBUGLVL(4)) {
1623                 char *p;
1624                 char atimebuf[32];
1625                 char mtimebuf[32];
1626
1627                 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1628                 atimebuf[sizeof(atimebuf) - 1] = '\0';
1629                 if ((p = strchr(atimebuf, '\n')) != NULL) {
1630                         *p = '\0';
1631                 }
1632
1633                 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1634                 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1635                 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1636                         *p = '\0';
1637                 }
1638
1639                 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1640                         fname, atimebuf, mtimebuf);
1641         }
1642
1643         if (SMBC_parse_path(frame,
1644                             context,
1645                             fname,
1646                             &workgroup,
1647                             &server,
1648                             &share,
1649                             &path,
1650                             &user,
1651                             &password,
1652                             NULL)) {
1653                 errno = EINVAL;
1654                 TALLOC_FREE(frame);
1655                 return -1;
1656         }
1657
1658         if (!user || user[0] == (char)0) {
1659                 user = talloc_strdup(frame, smbc_getUser(context));
1660                 if (!user) {
1661                         errno = ENOMEM;
1662                         TALLOC_FREE(frame);
1663                         return -1;
1664                 }
1665         }
1666
1667         srv = SMBC_server(frame, context, True,
1668                           server, share, &workgroup, &user, &password);
1669
1670         if (!srv) {
1671                 TALLOC_FREE(frame);
1672                 return -1;      /* errno set by SMBC_server */
1673         }
1674
1675         if (!SMBC_setatr(context, srv, path,
1676                          0, access_time, write_time, 0, 0)) {
1677                 TALLOC_FREE(frame);
1678                 return -1;      /* errno set by SMBC_setatr */
1679         }
1680
1681         TALLOC_FREE(frame);
1682         return 0;
1683 }
1684
1685 /*
1686  * Routine to unlink() a file
1687  */
1688
1689 int
1690 SMBC_unlink_ctx(SMBCCTX *context,
1691                 const char *fname)
1692 {
1693         char *server = NULL;
1694         char *share = NULL;
1695         char *user = NULL;
1696         char *password = NULL;
1697         char *workgroup = NULL;
1698         char *path = NULL;
1699         char *targetpath = NULL;
1700         struct cli_state *targetcli = NULL;
1701         SMBCSRV *srv = NULL;
1702         TALLOC_CTX *frame = talloc_stackframe();
1703
1704         if (!context || !context->internal->initialized) {
1705
1706                 errno = EINVAL;  /* Best I can think of ... */
1707                 TALLOC_FREE(frame);
1708                 return -1;
1709
1710         }
1711
1712         if (!fname) {
1713                 errno = EINVAL;
1714                 TALLOC_FREE(frame);
1715                 return -1;
1716
1717         }
1718
1719         if (SMBC_parse_path(frame,
1720                             context,
1721                             fname,
1722                             &workgroup,
1723                             &server,
1724                             &share,
1725                             &path,
1726                             &user,
1727                             &password,
1728                             NULL)) {
1729                 errno = EINVAL;
1730                 TALLOC_FREE(frame);
1731                 return -1;
1732         }
1733
1734         if (!user || user[0] == (char)0) {
1735                 user = talloc_strdup(frame, smbc_getUser(context));
1736                 if (!user) {
1737                         errno = ENOMEM;
1738                         TALLOC_FREE(frame);
1739                         return -1;
1740                 }
1741         }
1742
1743         srv = SMBC_server(frame, context, True,
1744                           server, share, &workgroup, &user, &password);
1745
1746         if (!srv) {
1747                 TALLOC_FREE(frame);
1748                 return -1;  /* SMBC_server sets errno */
1749
1750         }
1751
1752         /*d_printf(">>>unlink: resolving %s\n", path);*/
1753         if (!cli_resolve_path(frame, "", context->internal->auth_info,
1754                                 srv->cli, path,
1755                                 &targetcli, &targetpath)) {
1756                 d_printf("Could not resolve %s\n", path);
1757                 errno = ENOENT;
1758                 TALLOC_FREE(frame);
1759                 return -1;
1760         }
1761         /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1762
1763         if (!cli_unlink(targetcli, targetpath)) {
1764
1765                 errno = SMBC_errno(context, targetcli);
1766
1767                 if (errno == EACCES) { /* Check if the file is a directory */
1768
1769                         int saverr = errno;
1770                         SMB_OFF_T size = 0;
1771                         uint16 mode = 0;
1772                         struct timespec write_time_ts;
1773                         struct timespec access_time_ts;
1774                         struct timespec change_time_ts;
1775                         SMB_INO_T ino = 0;
1776
1777                         if (!SMBC_getatr(context, srv, path, &mode, &size,
1778                                          NULL,
1779                                          &access_time_ts,
1780                                          &write_time_ts,
1781                                          &change_time_ts,
1782                                          &ino)) {
1783
1784                                 /* Hmmm, bad error ... What? */
1785
1786                                 errno = SMBC_errno(context, targetcli);
1787                                 TALLOC_FREE(frame);
1788                                 return -1;
1789
1790                         }
1791                         else {
1792
1793                                 if (IS_DOS_DIR(mode))
1794                                         errno = EISDIR;
1795                                 else
1796                                         errno = saverr;  /* Restore this */
1797
1798                         }
1799                 }
1800
1801                 TALLOC_FREE(frame);
1802                 return -1;
1803
1804         }
1805
1806         TALLOC_FREE(frame);
1807         return 0;  /* Success ... */
1808
1809 }
1810
1811 /*
1812  * Routine to rename() a file
1813  */
1814
1815 int
1816 SMBC_rename_ctx(SMBCCTX *ocontext,
1817                 const char *oname,
1818                 SMBCCTX *ncontext,
1819                 const char *nname)
1820 {
1821         char *server1 = NULL;
1822         char *share1 = NULL;
1823         char *server2 = NULL;
1824         char *share2 = NULL;
1825         char *user1 = NULL;
1826         char *user2 = NULL;
1827         char *password1 = NULL;
1828         char *password2 = NULL;
1829         char *workgroup = NULL;
1830         char *path1 = NULL;
1831         char *path2 = NULL;
1832         char *targetpath1 = NULL;
1833         char *targetpath2 = NULL;
1834         struct cli_state *targetcli1 = NULL;
1835         struct cli_state *targetcli2 = NULL;
1836         SMBCSRV *srv = NULL;
1837         TALLOC_CTX *frame = talloc_stackframe();
1838
1839         if (!ocontext || !ncontext ||
1840             !ocontext->internal->initialized ||
1841             !ncontext->internal->initialized) {
1842
1843                 errno = EINVAL;  /* Best I can think of ... */
1844                 TALLOC_FREE(frame);
1845                 return -1;
1846         }
1847
1848         if (!oname || !nname) {
1849                 errno = EINVAL;
1850                 TALLOC_FREE(frame);
1851                 return -1;
1852         }
1853
1854         DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1855
1856         if (SMBC_parse_path(frame,
1857                             ocontext,
1858                             oname,
1859                             &workgroup,
1860                             &server1,
1861                             &share1,
1862                             &path1,
1863                             &user1,
1864                             &password1,
1865                             NULL)) {
1866                 errno = EINVAL;
1867                 TALLOC_FREE(frame);
1868                 return -1;
1869         }
1870
1871         if (!user1 || user1[0] == (char)0) {
1872                 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1873                 if (!user1) {
1874                         errno = ENOMEM;
1875                         TALLOC_FREE(frame);
1876                         return -1;
1877                 }
1878         }
1879
1880         if (SMBC_parse_path(frame,
1881                             ncontext,
1882                             nname,
1883                             NULL,
1884                             &server2,
1885                             &share2,
1886                             &path2,
1887                             &user2,
1888                             &password2,
1889                             NULL)) {
1890                 errno = EINVAL;
1891                 TALLOC_FREE(frame);
1892                 return -1;
1893         }
1894
1895         if (!user2 || user2[0] == (char)0) {
1896                 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1897                 if (!user2) {
1898                         errno = ENOMEM;
1899                         TALLOC_FREE(frame);
1900                         return -1;
1901                 }
1902         }
1903
1904         if (strcmp(server1, server2) || strcmp(share1, share2) ||
1905             strcmp(user1, user2)) {
1906                 /* Can't rename across file systems, or users?? */
1907                 errno = EXDEV;
1908                 TALLOC_FREE(frame);
1909                 return -1;
1910         }
1911
1912         srv = SMBC_server(frame, ocontext, True,
1913                           server1, share1, &workgroup, &user1, &password1);
1914         if (!srv) {
1915                 TALLOC_FREE(frame);
1916                 return -1;
1917
1918         }
1919
1920         /* set the credentials to make DFS work */
1921         smbc_set_credentials_with_fallback(ocontext,
1922                                            workgroup,
1923                                            user1,
1924                                            password1);
1925
1926         /*d_printf(">>>rename: resolving %s\n", path1);*/
1927         if (!cli_resolve_path(frame, "", ocontext->internal->auth_info,
1928                                 srv->cli,
1929                                 path1,
1930                                 &targetcli1, &targetpath1)) {
1931                 d_printf("Could not resolve %s\n", path1);
1932                 errno = ENOENT;
1933                 TALLOC_FREE(frame);
1934                 return -1;
1935         }
1936         
1937         /* set the credentials to make DFS work */
1938         smbc_set_credentials_with_fallback(ncontext,
1939                                            workgroup,
1940                                            user2,
1941                                            password2);
1942         
1943         /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1944         /*d_printf(">>>rename: resolving %s\n", path2);*/
1945         if (!cli_resolve_path(frame, "", ncontext->internal->auth_info,
1946                                 srv->cli, 
1947                                 path2,
1948                                 &targetcli2, &targetpath2)) {
1949                 d_printf("Could not resolve %s\n", path2);
1950                 errno = ENOENT;
1951                 TALLOC_FREE(frame);
1952                 return -1;
1953         }
1954         /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1955
1956         if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1957             strcmp(targetcli1->share, targetcli2->share))
1958         {
1959                 /* can't rename across file systems */
1960                 errno = EXDEV;
1961                 TALLOC_FREE(frame);
1962                 return -1;
1963         }
1964
1965         if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1966                 int eno = SMBC_errno(ocontext, targetcli1);
1967
1968                 if (eno != EEXIST ||
1969                     !cli_unlink(targetcli1, targetpath2) ||
1970                     !cli_rename(targetcli1, targetpath1, targetpath2)) {
1971
1972                         errno = eno;
1973                         TALLOC_FREE(frame);
1974                         return -1;
1975
1976                 }
1977         }
1978
1979         TALLOC_FREE(frame);
1980         return 0; /* Success */
1981 }
1982