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